contrib/python-zstandard/c-ext/compressionparams.c
changeset 30924 c32454d69b85
parent 30507 d7a05d8351d0
child 31799 e0dc40530c5a
equal deleted inserted replaced
30923:5b60464efbde 30924:c32454d69b85
    23 	unsigned PY_LONG_LONG sourceSize = 0;
    23 	unsigned PY_LONG_LONG sourceSize = 0;
    24 	Py_ssize_t dictSize = 0;
    24 	Py_ssize_t dictSize = 0;
    25 	ZSTD_compressionParameters params;
    25 	ZSTD_compressionParameters params;
    26 	CompressionParametersObject* result;
    26 	CompressionParametersObject* result;
    27 
    27 
    28 	if (!PyArg_ParseTuple(args, "i|Kn", &compressionLevel, &sourceSize, &dictSize)) {
    28 	if (!PyArg_ParseTuple(args, "i|Kn:get_compression_parameters",
       
    29 		&compressionLevel, &sourceSize, &dictSize)) {
    29 		return NULL;
    30 		return NULL;
    30 	}
    31 	}
    31 
    32 
    32 	params = ZSTD_getCParams(compressionLevel, sourceSize, dictSize);
    33 	params = ZSTD_getCParams(compressionLevel, sourceSize, dictSize);
    33 
    34 
    45 	result->strategy = params.strategy;
    46 	result->strategy = params.strategy;
    46 
    47 
    47 	return result;
    48 	return result;
    48 }
    49 }
    49 
    50 
    50 PyObject* estimate_compression_context_size(PyObject* self, PyObject* args) {
    51 static int CompressionParameters_init(CompressionParametersObject* self, PyObject* args, PyObject* kwargs) {
    51 	CompressionParametersObject* params;
    52 	static char* kwlist[] = {
    52 	ZSTD_compressionParameters zparams;
    53 		"window_log",
    53 	PyObject* result;
    54 		"chain_log",
    54 
    55 		"hash_log",
    55 	if (!PyArg_ParseTuple(args, "O!", &CompressionParametersType, &params)) {
    56 		"search_log",
    56 		return NULL;
    57 		"search_length",
    57 	}
    58 		"target_length",
    58 
    59 		"strategy",
    59 	ztopy_compression_parameters(params, &zparams);
    60 		NULL
    60 	result = PyLong_FromSize_t(ZSTD_estimateCCtxSize(zparams));
    61 	};
    61 	return result;
    62 
    62 }
       
    63 
       
    64 PyDoc_STRVAR(CompressionParameters__doc__,
       
    65 "CompressionParameters: low-level control over zstd compression");
       
    66 
       
    67 static PyObject* CompressionParameters_new(PyTypeObject* subtype, PyObject* args, PyObject* kwargs) {
       
    68 	CompressionParametersObject* self;
       
    69 	unsigned windowLog;
    63 	unsigned windowLog;
    70 	unsigned chainLog;
    64 	unsigned chainLog;
    71 	unsigned hashLog;
    65 	unsigned hashLog;
    72 	unsigned searchLog;
    66 	unsigned searchLog;
    73 	unsigned searchLength;
    67 	unsigned searchLength;
    74 	unsigned targetLength;
    68 	unsigned targetLength;
    75 	unsigned strategy;
    69 	unsigned strategy;
    76 
    70 
    77 	if (!PyArg_ParseTuple(args, "IIIIIII", &windowLog, &chainLog, &hashLog, &searchLog,
    71 	if (!PyArg_ParseTupleAndKeywords(args, kwargs, "IIIIIII:CompressionParameters",
    78 		&searchLength, &targetLength, &strategy)) {
    72 		kwlist, &windowLog, &chainLog, &hashLog, &searchLog, &searchLength,
    79 		return NULL;
    73 		&targetLength, &strategy)) {
       
    74 		return -1;
    80 	}
    75 	}
    81 
    76 
    82 	if (windowLog < ZSTD_WINDOWLOG_MIN || windowLog > ZSTD_WINDOWLOG_MAX) {
    77 	if (windowLog < ZSTD_WINDOWLOG_MIN || windowLog > ZSTD_WINDOWLOG_MAX) {
    83 		PyErr_SetString(PyExc_ValueError, "invalid window log value");
    78 		PyErr_SetString(PyExc_ValueError, "invalid window log value");
    84 		return NULL;
    79 		return -1;
    85 	}
    80 	}
    86 
    81 
    87 	if (chainLog < ZSTD_CHAINLOG_MIN || chainLog > ZSTD_CHAINLOG_MAX) {
    82 	if (chainLog < ZSTD_CHAINLOG_MIN || chainLog > ZSTD_CHAINLOG_MAX) {
    88 		PyErr_SetString(PyExc_ValueError, "invalid chain log value");
    83 		PyErr_SetString(PyExc_ValueError, "invalid chain log value");
    89 		return NULL;
    84 		return -1;
    90 	}
    85 	}
    91 
    86 
    92 	if (hashLog < ZSTD_HASHLOG_MIN || hashLog > ZSTD_HASHLOG_MAX) {
    87 	if (hashLog < ZSTD_HASHLOG_MIN || hashLog > ZSTD_HASHLOG_MAX) {
    93 		PyErr_SetString(PyExc_ValueError, "invalid hash log value");
    88 		PyErr_SetString(PyExc_ValueError, "invalid hash log value");
    94 		return NULL;
    89 		return -1;
    95 	}
    90 	}
    96 
    91 
    97 	if (searchLog < ZSTD_SEARCHLOG_MIN || searchLog > ZSTD_SEARCHLOG_MAX) {
    92 	if (searchLog < ZSTD_SEARCHLOG_MIN || searchLog > ZSTD_SEARCHLOG_MAX) {
    98 		PyErr_SetString(PyExc_ValueError, "invalid search log value");
    93 		PyErr_SetString(PyExc_ValueError, "invalid search log value");
    99 		return NULL;
    94 		return -1;
   100 	}
    95 	}
   101 
    96 
   102 	if (searchLength < ZSTD_SEARCHLENGTH_MIN || searchLength > ZSTD_SEARCHLENGTH_MAX) {
    97 	if (searchLength < ZSTD_SEARCHLENGTH_MIN || searchLength > ZSTD_SEARCHLENGTH_MAX) {
   103 		PyErr_SetString(PyExc_ValueError, "invalid search length value");
    98 		PyErr_SetString(PyExc_ValueError, "invalid search length value");
   104 		return NULL;
    99 		return -1;
   105 	}
   100 	}
   106 
   101 
   107 	if (targetLength < ZSTD_TARGETLENGTH_MIN || targetLength > ZSTD_TARGETLENGTH_MAX) {
   102 	if (targetLength < ZSTD_TARGETLENGTH_MIN || targetLength > ZSTD_TARGETLENGTH_MAX) {
   108 		PyErr_SetString(PyExc_ValueError, "invalid target length value");
   103 		PyErr_SetString(PyExc_ValueError, "invalid target length value");
   109 		return NULL;
   104 		return -1;
   110 	}
   105 	}
   111 
   106 
   112 	if (strategy < ZSTD_fast || strategy > ZSTD_btopt) {
   107 	if (strategy < ZSTD_fast || strategy > ZSTD_btopt) {
   113 		PyErr_SetString(PyExc_ValueError, "invalid strategy value");
   108 		PyErr_SetString(PyExc_ValueError, "invalid strategy value");
   114 		return NULL;
   109 		return -1;
   115 	}
       
   116 
       
   117 	self = (CompressionParametersObject*)subtype->tp_alloc(subtype, 1);
       
   118 	if (!self) {
       
   119 		return NULL;
       
   120 	}
   110 	}
   121 
   111 
   122 	self->windowLog = windowLog;
   112 	self->windowLog = windowLog;
   123 	self->chainLog = chainLog;
   113 	self->chainLog = chainLog;
   124 	self->hashLog = hashLog;
   114 	self->hashLog = hashLog;
   125 	self->searchLog = searchLog;
   115 	self->searchLog = searchLog;
   126 	self->searchLength = searchLength;
   116 	self->searchLength = searchLength;
   127 	self->targetLength = targetLength;
   117 	self->targetLength = targetLength;
   128 	self->strategy = strategy;
   118 	self->strategy = strategy;
   129 
   119 
   130 	return (PyObject*)self;
   120 	return 0;
   131 }
   121 }
       
   122 
       
   123 PyObject* estimate_compression_context_size(PyObject* self, PyObject* args) {
       
   124 	CompressionParametersObject* params;
       
   125 	ZSTD_compressionParameters zparams;
       
   126 	PyObject* result;
       
   127 
       
   128 	if (!PyArg_ParseTuple(args, "O!:estimate_compression_context_size",
       
   129 		&CompressionParametersType, &params)) {
       
   130 		return NULL;
       
   131 	}
       
   132 
       
   133 	ztopy_compression_parameters(params, &zparams);
       
   134 	result = PyLong_FromSize_t(ZSTD_estimateCCtxSize(zparams));
       
   135 	return result;
       
   136 }
       
   137 
       
   138 PyDoc_STRVAR(CompressionParameters__doc__,
       
   139 "CompressionParameters: low-level control over zstd compression");
   132 
   140 
   133 static void CompressionParameters_dealloc(PyObject* self) {
   141 static void CompressionParameters_dealloc(PyObject* self) {
   134 	PyObject_Del(self);
   142 	PyObject_Del(self);
   135 }
   143 }
   136 
   144 
   137 static Py_ssize_t CompressionParameters_length(PyObject* self) {
   145 static PyMemberDef CompressionParameters_members[] = {
   138 	return 7;
   146 	{ "window_log", T_UINT,
   139 }
   147 	  offsetof(CompressionParametersObject, windowLog), READONLY,
   140 
   148 	  "window log" },
   141 static PyObject* CompressionParameters_item(PyObject* o, Py_ssize_t i) {
   149 	{ "chain_log", T_UINT,
   142 	CompressionParametersObject* self = (CompressionParametersObject*)o;
   150 	  offsetof(CompressionParametersObject, chainLog), READONLY,
   143 
   151 	  "chain log" },
   144 	switch (i) {
   152 	{ "hash_log", T_UINT,
   145 	case 0:
   153 	  offsetof(CompressionParametersObject, hashLog), READONLY,
   146 		return PyLong_FromLong(self->windowLog);
   154 	  "hash log" },
   147 	case 1:
   155 	{ "search_log", T_UINT,
   148 		return PyLong_FromLong(self->chainLog);
   156 	  offsetof(CompressionParametersObject, searchLog), READONLY,
   149 	case 2:
   157 	  "search log" },
   150 		return PyLong_FromLong(self->hashLog);
   158 	{ "search_length", T_UINT,
   151 	case 3:
   159 	  offsetof(CompressionParametersObject, searchLength), READONLY,
   152 		return PyLong_FromLong(self->searchLog);
   160 	  "search length" },
   153 	case 4:
   161 	{ "target_length", T_UINT,
   154 		return PyLong_FromLong(self->searchLength);
   162 	  offsetof(CompressionParametersObject, targetLength), READONLY,
   155 	case 5:
   163 	  "target length" },
   156 		return PyLong_FromLong(self->targetLength);
   164 	{ "strategy", T_INT,
   157 	case 6:
   165 	  offsetof(CompressionParametersObject, strategy), READONLY,
   158 		return PyLong_FromLong(self->strategy);
   166 	  "strategy" },
   159 	default:
   167 	{ NULL }
   160 		PyErr_SetString(PyExc_IndexError, "index out of range");
       
   161 		return NULL;
       
   162 	}
       
   163 }
       
   164 
       
   165 static PySequenceMethods CompressionParameters_sq = {
       
   166 	CompressionParameters_length, /* sq_length */
       
   167 	0,							  /* sq_concat */
       
   168 	0,                            /* sq_repeat */
       
   169 	CompressionParameters_item,   /* sq_item */
       
   170 	0,                            /* sq_ass_item */
       
   171 	0,                            /* sq_contains */
       
   172 	0,                            /* sq_inplace_concat */
       
   173 	0                             /* sq_inplace_repeat */
       
   174 };
   168 };
   175 
   169 
   176 PyTypeObject CompressionParametersType = {
   170 PyTypeObject CompressionParametersType = {
   177 	PyVarObject_HEAD_INIT(NULL, 0)
   171 	PyVarObject_HEAD_INIT(NULL, 0)
   178 	"CompressionParameters", /* tp_name */
   172 	"CompressionParameters", /* tp_name */
   183 	0,                         /* tp_getattr */
   177 	0,                         /* tp_getattr */
   184 	0,                         /* tp_setattr */
   178 	0,                         /* tp_setattr */
   185 	0,                         /* tp_compare */
   179 	0,                         /* tp_compare */
   186 	0,                         /* tp_repr */
   180 	0,                         /* tp_repr */
   187 	0,                         /* tp_as_number */
   181 	0,                         /* tp_as_number */
   188 	&CompressionParameters_sq, /* tp_as_sequence */
   182 	0,                         /* tp_as_sequence */
   189 	0,                         /* tp_as_mapping */
   183 	0,                         /* tp_as_mapping */
   190 	0,                         /* tp_hash  */
   184 	0,                         /* tp_hash  */
   191 	0,                         /* tp_call */
   185 	0,                         /* tp_call */
   192 	0,                         /* tp_str */
   186 	0,                         /* tp_str */
   193 	0,                         /* tp_getattro */
   187 	0,                         /* tp_getattro */
   194 	0,                         /* tp_setattro */
   188 	0,                         /* tp_setattro */
   195 	0,                         /* tp_as_buffer */
   189 	0,                         /* tp_as_buffer */
   196 	Py_TPFLAGS_DEFAULT,        /* tp_flags */
   190 	Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
   197 	CompressionParameters__doc__, /* tp_doc */
   191 	CompressionParameters__doc__, /* tp_doc */
   198 	0,                         /* tp_traverse */
   192 	0,                         /* tp_traverse */
   199 	0,                         /* tp_clear */
   193 	0,                         /* tp_clear */
   200 	0,                         /* tp_richcompare */
   194 	0,                         /* tp_richcompare */
   201 	0,                         /* tp_weaklistoffset */
   195 	0,                         /* tp_weaklistoffset */
   202 	0,                         /* tp_iter */
   196 	0,                         /* tp_iter */
   203 	0,                         /* tp_iternext */
   197 	0,                         /* tp_iternext */
   204 	0,                         /* tp_methods */
   198 	0,                         /* tp_methods */
   205 	0,                         /* tp_members */
   199 	CompressionParameters_members, /* tp_members */
   206 	0,                         /* tp_getset */
   200 	0,                         /* tp_getset */
   207 	0,                         /* tp_base */
   201 	0,                         /* tp_base */
   208 	0,                         /* tp_dict */
   202 	0,                         /* tp_dict */
   209 	0,                         /* tp_descr_get */
   203 	0,                         /* tp_descr_get */
   210 	0,                         /* tp_descr_set */
   204 	0,                         /* tp_descr_set */
   211 	0,                         /* tp_dictoffset */
   205 	0,                         /* tp_dictoffset */
   212 	0,                         /* tp_init */
   206 	(initproc)CompressionParameters_init, /* tp_init */
   213 	0,                         /* tp_alloc */
   207 	0,                         /* tp_alloc */
   214 	CompressionParameters_new, /* tp_new */
   208 	PyType_GenericNew,         /* tp_new */
   215 };
   209 };
   216 
   210 
   217 void compressionparams_module_init(PyObject* mod) {
   211 void compressionparams_module_init(PyObject* mod) {
   218 	Py_TYPE(&CompressionParametersType) = &PyType_Type;
   212 	Py_TYPE(&CompressionParametersType) = &PyType_Type;
   219 	if (PyType_Ready(&CompressionParametersType) < 0) {
   213 	if (PyType_Ready(&CompressionParametersType) < 0) {