blob: aa579209108d85dc51694bb62682cb0765d03e0b [file] [log] [blame]
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_STRVAR(zlib_compress__doc__,
Serhiy Storchaka95657cd2016-06-25 22:43:05 +03006"compress($module, data, /, level=Z_DEFAULT_COMPRESSION)\n"
Larry Hastings2623c8c2014-02-08 22:15:29 -08007"--\n"
8"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02009"Returns a bytes object containing compressed data.\n"
10"\n"
Martin Panter1fe0d132016-02-10 10:06:36 +000011" data\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020012" Binary data to be compressed.\n"
13" level\n"
Martin Panterb0cb42d2016-02-10 10:45:54 +000014" Compression level, in 0-9 or -1.");
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020015
16#define ZLIB_COMPRESS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020017 {"compress", (PyCFunction)(void(*)(void))zlib_compress, METH_FASTCALL|METH_KEYWORDS, zlib_compress__doc__},
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020018
19static PyObject *
Serhiy Storchaka2954f832016-07-07 18:20:03 +030020zlib_compress_impl(PyObject *module, Py_buffer *data, int level);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020021
22static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020023zlib_compress(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020024{
25 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030026 static const char * const _keywords[] = {"", "level", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +020027 static _PyArg_Parser _parser = {NULL, _keywords, "compress", 0};
28 PyObject *argsbuf[2];
29 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Martin Panter1fe0d132016-02-10 10:06:36 +000030 Py_buffer data = {NULL, NULL};
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020031 int level = Z_DEFAULT_COMPRESSION;
32
Serhiy Storchaka31913912019-03-14 10:32:22 +020033 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
34 if (!args) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020035 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030036 }
Serhiy Storchaka31913912019-03-14 10:32:22 +020037 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
38 goto exit;
39 }
40 if (!PyBuffer_IsContiguous(&data, 'C')) {
41 _PyArg_BadArgument("compress", 1, "contiguous buffer", args[0]);
42 goto exit;
43 }
44 if (!noptargs) {
45 goto skip_optional_pos;
46 }
47 if (PyFloat_Check(args[1])) {
48 PyErr_SetString(PyExc_TypeError,
49 "integer argument expected, got float" );
50 goto exit;
51 }
52 level = _PyLong_AsInt(args[1]);
53 if (level == -1 && PyErr_Occurred()) {
54 goto exit;
55 }
56skip_optional_pos:
Martin Panter1fe0d132016-02-10 10:06:36 +000057 return_value = zlib_compress_impl(module, &data, level);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020058
59exit:
Martin Panter1fe0d132016-02-10 10:06:36 +000060 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030061 if (data.obj) {
Martin Panter1fe0d132016-02-10 10:06:36 +000062 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030063 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020064
65 return return_value;
66}
67
68PyDoc_STRVAR(zlib_decompress__doc__,
Serhiy Storchaka15f32282016-08-15 10:06:16 +030069"decompress($module, data, /, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE)\n"
Larry Hastings2623c8c2014-02-08 22:15:29 -080070"--\n"
71"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020072"Returns a bytes object containing the uncompressed data.\n"
73"\n"
74" data\n"
75" Compressed data.\n"
76" wbits\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +000077" The window buffer size and container format.\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020078" bufsize\n"
79" The initial output buffer size.");
80
81#define ZLIB_DECOMPRESS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +020082 {"decompress", (PyCFunction)(void(*)(void))zlib_decompress, METH_FASTCALL|METH_KEYWORDS, zlib_decompress__doc__},
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020083
84static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030085zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
Martin Panter84544c12016-07-23 03:02:07 +000086 Py_ssize_t bufsize);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020087
88static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020089zlib_decompress(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020090{
91 PyObject *return_value = NULL;
Serhiy Storchaka15f32282016-08-15 10:06:16 +030092 static const char * const _keywords[] = {"", "wbits", "bufsize", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +020093 static _PyArg_Parser _parser = {NULL, _keywords, "decompress", 0};
94 PyObject *argsbuf[3];
95 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020096 Py_buffer data = {NULL, NULL};
97 int wbits = MAX_WBITS;
Martin Panter84544c12016-07-23 03:02:07 +000098 Py_ssize_t bufsize = DEF_BUF_SIZE;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020099
Serhiy Storchaka31913912019-03-14 10:32:22 +0200100 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
101 if (!args) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200102 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300103 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200104 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
105 goto exit;
106 }
107 if (!PyBuffer_IsContiguous(&data, 'C')) {
108 _PyArg_BadArgument("decompress", 1, "contiguous buffer", args[0]);
109 goto exit;
110 }
111 if (!noptargs) {
112 goto skip_optional_pos;
113 }
114 if (args[1]) {
115 if (PyFloat_Check(args[1])) {
116 PyErr_SetString(PyExc_TypeError,
117 "integer argument expected, got float" );
118 goto exit;
119 }
120 wbits = _PyLong_AsInt(args[1]);
121 if (wbits == -1 && PyErr_Occurred()) {
122 goto exit;
123 }
124 if (!--noptargs) {
125 goto skip_optional_pos;
126 }
127 }
128 if (!ssize_t_converter(args[2], &bufsize)) {
129 goto exit;
130 }
131skip_optional_pos:
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200132 return_value = zlib_decompress_impl(module, &data, wbits, bufsize);
133
134exit:
135 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300136 if (data.obj) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200137 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300138 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200139
140 return return_value;
141}
142
143PyDoc_STRVAR(zlib_compressobj__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800144"compressobj($module, /, level=Z_DEFAULT_COMPRESSION, method=DEFLATED,\n"
145" wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL,\n"
146" strategy=Z_DEFAULT_STRATEGY, zdict=None)\n"
147"--\n"
148"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200149"Return a compressor object.\n"
150"\n"
151" level\n"
Martin Panter567d5132016-02-03 07:06:33 +0000152" The compression level (an integer in the range 0-9 or -1; default is\n"
153" currently equivalent to 6). Higher compression levels are slower,\n"
154" but produce smaller results.\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200155" method\n"
156" The compression algorithm. If given, this must be DEFLATED.\n"
157" wbits\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +0000158" +9 to +15: The base-two logarithm of the window size. Include a zlib\n"
159" container.\n"
160" -9 to -15: Generate a raw stream.\n"
161" +25 to +31: Include a gzip container.\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200162" memLevel\n"
163" Controls the amount of memory used for internal compression state.\n"
164" Valid values range from 1 to 9. Higher values result in higher memory\n"
165" usage, faster compression, and smaller output.\n"
166" strategy\n"
167" Used to tune the compression algorithm. Possible values are\n"
168" Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.\n"
169" zdict\n"
170" The predefined compression dictionary - a sequence of bytes\n"
171" containing subsequences that are likely to occur in the input data.");
172
173#define ZLIB_COMPRESSOBJ_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200174 {"compressobj", (PyCFunction)(void(*)(void))zlib_compressobj, METH_FASTCALL|METH_KEYWORDS, zlib_compressobj__doc__},
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200175
176static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300177zlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
Larry Hastings89964c42015-04-14 18:07:59 -0400178 int memLevel, int strategy, Py_buffer *zdict);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200179
180static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200181zlib_compressobj(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200182{
183 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300184 static const char * const _keywords[] = {"level", "method", "wbits", "memLevel", "strategy", "zdict", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200185 static _PyArg_Parser _parser = {NULL, _keywords, "compressobj", 0};
186 PyObject *argsbuf[6];
187 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200188 int level = Z_DEFAULT_COMPRESSION;
189 int method = DEFLATED;
190 int wbits = MAX_WBITS;
191 int memLevel = DEF_MEM_LEVEL;
192 int strategy = Z_DEFAULT_STRATEGY;
193 Py_buffer zdict = {NULL, NULL};
194
Serhiy Storchaka31913912019-03-14 10:32:22 +0200195 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 6, 0, argsbuf);
196 if (!args) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200197 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300198 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200199 if (!noptargs) {
200 goto skip_optional_pos;
201 }
202 if (args[0]) {
203 if (PyFloat_Check(args[0])) {
204 PyErr_SetString(PyExc_TypeError,
205 "integer argument expected, got float" );
206 goto exit;
207 }
208 level = _PyLong_AsInt(args[0]);
209 if (level == -1 && PyErr_Occurred()) {
210 goto exit;
211 }
212 if (!--noptargs) {
213 goto skip_optional_pos;
214 }
215 }
216 if (args[1]) {
217 if (PyFloat_Check(args[1])) {
218 PyErr_SetString(PyExc_TypeError,
219 "integer argument expected, got float" );
220 goto exit;
221 }
222 method = _PyLong_AsInt(args[1]);
223 if (method == -1 && PyErr_Occurred()) {
224 goto exit;
225 }
226 if (!--noptargs) {
227 goto skip_optional_pos;
228 }
229 }
230 if (args[2]) {
231 if (PyFloat_Check(args[2])) {
232 PyErr_SetString(PyExc_TypeError,
233 "integer argument expected, got float" );
234 goto exit;
235 }
236 wbits = _PyLong_AsInt(args[2]);
237 if (wbits == -1 && PyErr_Occurred()) {
238 goto exit;
239 }
240 if (!--noptargs) {
241 goto skip_optional_pos;
242 }
243 }
244 if (args[3]) {
245 if (PyFloat_Check(args[3])) {
246 PyErr_SetString(PyExc_TypeError,
247 "integer argument expected, got float" );
248 goto exit;
249 }
250 memLevel = _PyLong_AsInt(args[3]);
251 if (memLevel == -1 && PyErr_Occurred()) {
252 goto exit;
253 }
254 if (!--noptargs) {
255 goto skip_optional_pos;
256 }
257 }
258 if (args[4]) {
259 if (PyFloat_Check(args[4])) {
260 PyErr_SetString(PyExc_TypeError,
261 "integer argument expected, got float" );
262 goto exit;
263 }
264 strategy = _PyLong_AsInt(args[4]);
265 if (strategy == -1 && PyErr_Occurred()) {
266 goto exit;
267 }
268 if (!--noptargs) {
269 goto skip_optional_pos;
270 }
271 }
272 if (PyObject_GetBuffer(args[5], &zdict, PyBUF_SIMPLE) != 0) {
273 goto exit;
274 }
275 if (!PyBuffer_IsContiguous(&zdict, 'C')) {
276 _PyArg_BadArgument("compressobj", 6, "contiguous buffer", args[5]);
277 goto exit;
278 }
279skip_optional_pos:
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200280 return_value = zlib_compressobj_impl(module, level, method, wbits, memLevel, strategy, &zdict);
281
282exit:
283 /* Cleanup for zdict */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300284 if (zdict.obj) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200285 PyBuffer_Release(&zdict);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300286 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200287
288 return return_value;
289}
290
291PyDoc_STRVAR(zlib_decompressobj__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800292"decompressobj($module, /, wbits=MAX_WBITS, zdict=b\'\')\n"
293"--\n"
294"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200295"Return a decompressor object.\n"
296"\n"
297" wbits\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +0000298" The window buffer size and container format.\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200299" zdict\n"
300" The predefined compression dictionary. This must be the same\n"
301" dictionary as used by the compressor that produced the input data.");
302
303#define ZLIB_DECOMPRESSOBJ_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200304 {"decompressobj", (PyCFunction)(void(*)(void))zlib_decompressobj, METH_FASTCALL|METH_KEYWORDS, zlib_decompressobj__doc__},
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200305
306static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300307zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200308
309static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200310zlib_decompressobj(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200311{
312 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300313 static const char * const _keywords[] = {"wbits", "zdict", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200314 static _PyArg_Parser _parser = {NULL, _keywords, "decompressobj", 0};
315 PyObject *argsbuf[2];
316 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200317 int wbits = MAX_WBITS;
318 PyObject *zdict = NULL;
319
Serhiy Storchaka31913912019-03-14 10:32:22 +0200320 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
321 if (!args) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200322 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300323 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200324 if (!noptargs) {
325 goto skip_optional_pos;
326 }
327 if (args[0]) {
328 if (PyFloat_Check(args[0])) {
329 PyErr_SetString(PyExc_TypeError,
330 "integer argument expected, got float" );
331 goto exit;
332 }
333 wbits = _PyLong_AsInt(args[0]);
334 if (wbits == -1 && PyErr_Occurred()) {
335 goto exit;
336 }
337 if (!--noptargs) {
338 goto skip_optional_pos;
339 }
340 }
341 zdict = args[1];
342skip_optional_pos:
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200343 return_value = zlib_decompressobj_impl(module, wbits, zdict);
344
345exit:
346 return return_value;
347}
348
349PyDoc_STRVAR(zlib_Compress_compress__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800350"compress($self, data, /)\n"
351"--\n"
352"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200353"Returns a bytes object containing compressed data.\n"
354"\n"
355" data\n"
356" Binary data to be compressed.\n"
357"\n"
358"After calling this function, some of the input data may still\n"
359"be stored in internal buffers for later processing.\n"
360"Call the flush() method to clear these buffers.");
361
362#define ZLIB_COMPRESS_COMPRESS_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300363 {"compress", (PyCFunction)zlib_Compress_compress, METH_O, zlib_Compress_compress__doc__},
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200364
365static PyObject *
366zlib_Compress_compress_impl(compobject *self, Py_buffer *data);
367
368static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300369zlib_Compress_compress(compobject *self, PyObject *arg)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200370{
371 PyObject *return_value = NULL;
372 Py_buffer data = {NULL, NULL};
373
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200374 if (PyObject_GetBuffer(arg, &data, PyBUF_SIMPLE) != 0) {
375 goto exit;
376 }
377 if (!PyBuffer_IsContiguous(&data, 'C')) {
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200378 _PyArg_BadArgument("compress", 0, "contiguous buffer", arg);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200379 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300380 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200381 return_value = zlib_Compress_compress_impl(self, &data);
382
383exit:
384 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300385 if (data.obj) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200386 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300387 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200388
389 return return_value;
390}
391
392PyDoc_STRVAR(zlib_Decompress_decompress__doc__,
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300393"decompress($self, data, /, max_length=0)\n"
Larry Hastings2623c8c2014-02-08 22:15:29 -0800394"--\n"
395"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200396"Return a bytes object containing the decompressed version of the data.\n"
397"\n"
398" data\n"
399" The binary data to decompress.\n"
400" max_length\n"
401" The maximum allowable length of the decompressed data.\n"
402" Unconsumed input data will be stored in\n"
403" the unconsumed_tail attribute.\n"
404"\n"
405"After calling this function, some of the input data may still be stored in\n"
406"internal buffers for later processing.\n"
407"Call the flush() method to clear these buffers.");
408
409#define ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200410 {"decompress", (PyCFunction)(void(*)(void))zlib_Decompress_decompress, METH_FASTCALL|METH_KEYWORDS, zlib_Decompress_decompress__doc__},
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200411
412static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400413zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
Martin Panter84544c12016-07-23 03:02:07 +0000414 Py_ssize_t max_length);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200415
416static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200417zlib_Decompress_decompress(compobject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200418{
419 PyObject *return_value = NULL;
Serhiy Storchaka15f32282016-08-15 10:06:16 +0300420 static const char * const _keywords[] = {"", "max_length", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200421 static _PyArg_Parser _parser = {NULL, _keywords, "decompress", 0};
422 PyObject *argsbuf[2];
423 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200424 Py_buffer data = {NULL, NULL};
Martin Panter84544c12016-07-23 03:02:07 +0000425 Py_ssize_t max_length = 0;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200426
Serhiy Storchaka31913912019-03-14 10:32:22 +0200427 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
428 if (!args) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200429 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300430 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200431 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
432 goto exit;
433 }
434 if (!PyBuffer_IsContiguous(&data, 'C')) {
435 _PyArg_BadArgument("decompress", 1, "contiguous buffer", args[0]);
436 goto exit;
437 }
438 if (!noptargs) {
439 goto skip_optional_pos;
440 }
441 if (!ssize_t_converter(args[1], &max_length)) {
442 goto exit;
443 }
444skip_optional_pos:
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200445 return_value = zlib_Decompress_decompress_impl(self, &data, max_length);
446
447exit:
448 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300449 if (data.obj) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200450 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300451 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200452
453 return return_value;
454}
455
456PyDoc_STRVAR(zlib_Compress_flush__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800457"flush($self, mode=zlib.Z_FINISH, /)\n"
458"--\n"
459"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200460"Return a bytes object containing any remaining compressed data.\n"
461"\n"
462" mode\n"
463" One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.\n"
464" If mode == Z_FINISH, the compressor object can no longer be\n"
465" used after calling the flush() method. Otherwise, more data\n"
466" can still be compressed.");
467
468#define ZLIB_COMPRESS_FLUSH_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200469 {"flush", (PyCFunction)(void(*)(void))zlib_Compress_flush, METH_FASTCALL, zlib_Compress_flush__doc__},
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200470
471static PyObject *
472zlib_Compress_flush_impl(compobject *self, int mode);
473
474static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200475zlib_Compress_flush(compobject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200476{
477 PyObject *return_value = NULL;
478 int mode = Z_FINISH;
479
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200480 if (!_PyArg_CheckPositional("flush", nargs, 0, 1)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100481 goto exit;
482 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200483 if (nargs < 1) {
484 goto skip_optional;
485 }
486 if (PyFloat_Check(args[0])) {
487 PyErr_SetString(PyExc_TypeError,
488 "integer argument expected, got float" );
489 goto exit;
490 }
491 mode = _PyLong_AsInt(args[0]);
492 if (mode == -1 && PyErr_Occurred()) {
493 goto exit;
494 }
495skip_optional:
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200496 return_value = zlib_Compress_flush_impl(self, mode);
497
498exit:
499 return return_value;
500}
501
Larry Hastings7726ac92014-01-31 22:03:12 -0800502#if defined(HAVE_ZLIB_COPY)
503
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200504PyDoc_STRVAR(zlib_Compress_copy__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800505"copy($self, /)\n"
506"--\n"
507"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200508"Return a copy of the compression object.");
509
510#define ZLIB_COMPRESS_COPY_METHODDEF \
511 {"copy", (PyCFunction)zlib_Compress_copy, METH_NOARGS, zlib_Compress_copy__doc__},
512
513static PyObject *
514zlib_Compress_copy_impl(compobject *self);
515
516static PyObject *
517zlib_Compress_copy(compobject *self, PyObject *Py_UNUSED(ignored))
518{
519 return zlib_Compress_copy_impl(self);
520}
521
Larry Hastings7726ac92014-01-31 22:03:12 -0800522#endif /* defined(HAVE_ZLIB_COPY) */
523
Larry Hastings7726ac92014-01-31 22:03:12 -0800524#if defined(HAVE_ZLIB_COPY)
525
Zackery Spytzd2cbfff2018-06-27 12:04:51 -0600526PyDoc_STRVAR(zlib_Compress___copy____doc__,
527"__copy__($self, /)\n"
528"--\n"
529"\n");
530
531#define ZLIB_COMPRESS___COPY___METHODDEF \
532 {"__copy__", (PyCFunction)zlib_Compress___copy__, METH_NOARGS, zlib_Compress___copy____doc__},
533
534static PyObject *
535zlib_Compress___copy___impl(compobject *self);
536
537static PyObject *
538zlib_Compress___copy__(compobject *self, PyObject *Py_UNUSED(ignored))
539{
540 return zlib_Compress___copy___impl(self);
541}
542
543#endif /* defined(HAVE_ZLIB_COPY) */
544
545#if defined(HAVE_ZLIB_COPY)
546
547PyDoc_STRVAR(zlib_Compress___deepcopy____doc__,
548"__deepcopy__($self, memo, /)\n"
549"--\n"
550"\n");
551
552#define ZLIB_COMPRESS___DEEPCOPY___METHODDEF \
553 {"__deepcopy__", (PyCFunction)zlib_Compress___deepcopy__, METH_O, zlib_Compress___deepcopy____doc__},
554
555#endif /* defined(HAVE_ZLIB_COPY) */
556
557#if defined(HAVE_ZLIB_COPY)
558
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200559PyDoc_STRVAR(zlib_Decompress_copy__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800560"copy($self, /)\n"
561"--\n"
562"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200563"Return a copy of the decompression object.");
564
565#define ZLIB_DECOMPRESS_COPY_METHODDEF \
566 {"copy", (PyCFunction)zlib_Decompress_copy, METH_NOARGS, zlib_Decompress_copy__doc__},
567
568static PyObject *
569zlib_Decompress_copy_impl(compobject *self);
570
571static PyObject *
572zlib_Decompress_copy(compobject *self, PyObject *Py_UNUSED(ignored))
573{
574 return zlib_Decompress_copy_impl(self);
575}
576
Larry Hastings7726ac92014-01-31 22:03:12 -0800577#endif /* defined(HAVE_ZLIB_COPY) */
578
Zackery Spytzd2cbfff2018-06-27 12:04:51 -0600579#if defined(HAVE_ZLIB_COPY)
580
581PyDoc_STRVAR(zlib_Decompress___copy____doc__,
582"__copy__($self, /)\n"
583"--\n"
584"\n");
585
586#define ZLIB_DECOMPRESS___COPY___METHODDEF \
587 {"__copy__", (PyCFunction)zlib_Decompress___copy__, METH_NOARGS, zlib_Decompress___copy____doc__},
588
589static PyObject *
590zlib_Decompress___copy___impl(compobject *self);
591
592static PyObject *
593zlib_Decompress___copy__(compobject *self, PyObject *Py_UNUSED(ignored))
594{
595 return zlib_Decompress___copy___impl(self);
596}
597
598#endif /* defined(HAVE_ZLIB_COPY) */
599
600#if defined(HAVE_ZLIB_COPY)
601
602PyDoc_STRVAR(zlib_Decompress___deepcopy____doc__,
603"__deepcopy__($self, memo, /)\n"
604"--\n"
605"\n");
606
607#define ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF \
608 {"__deepcopy__", (PyCFunction)zlib_Decompress___deepcopy__, METH_O, zlib_Decompress___deepcopy____doc__},
609
610#endif /* defined(HAVE_ZLIB_COPY) */
611
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200612PyDoc_STRVAR(zlib_Decompress_flush__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800613"flush($self, length=zlib.DEF_BUF_SIZE, /)\n"
614"--\n"
615"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200616"Return a bytes object containing any remaining decompressed data.\n"
617"\n"
618" length\n"
619" the initial size of the output buffer.");
620
621#define ZLIB_DECOMPRESS_FLUSH_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200622 {"flush", (PyCFunction)(void(*)(void))zlib_Decompress_flush, METH_FASTCALL, zlib_Decompress_flush__doc__},
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200623
624static PyObject *
Martin Panter84544c12016-07-23 03:02:07 +0000625zlib_Decompress_flush_impl(compobject *self, Py_ssize_t length);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200626
627static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200628zlib_Decompress_flush(compobject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200629{
630 PyObject *return_value = NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000631 Py_ssize_t length = DEF_BUF_SIZE;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200632
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200633 if (!_PyArg_CheckPositional("flush", nargs, 0, 1)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100634 goto exit;
635 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200636 if (nargs < 1) {
637 goto skip_optional;
638 }
639 if (!ssize_t_converter(args[0], &length)) {
640 goto exit;
641 }
642skip_optional:
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200643 return_value = zlib_Decompress_flush_impl(self, length);
644
645exit:
646 return return_value;
647}
648
649PyDoc_STRVAR(zlib_adler32__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800650"adler32($module, data, value=1, /)\n"
651"--\n"
652"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200653"Compute an Adler-32 checksum of data.\n"
654"\n"
655" value\n"
656" Starting value of the checksum.\n"
657"\n"
658"The returned checksum is an integer.");
659
660#define ZLIB_ADLER32_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200661 {"adler32", (PyCFunction)(void(*)(void))zlib_adler32, METH_FASTCALL, zlib_adler32__doc__},
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200662
663static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300664zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200665
666static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200667zlib_adler32(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200668{
669 PyObject *return_value = NULL;
670 Py_buffer data = {NULL, NULL};
671 unsigned int value = 1;
672
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200673 if (!_PyArg_CheckPositional("adler32", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100674 goto exit;
675 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200676 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
677 goto exit;
678 }
679 if (!PyBuffer_IsContiguous(&data, 'C')) {
680 _PyArg_BadArgument("adler32", 1, "contiguous buffer", args[0]);
681 goto exit;
682 }
683 if (nargs < 2) {
684 goto skip_optional;
685 }
686 if (PyFloat_Check(args[1])) {
687 PyErr_SetString(PyExc_TypeError,
688 "integer argument expected, got float" );
689 goto exit;
690 }
691 value = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
692 if (value == (unsigned int)-1 && PyErr_Occurred()) {
693 goto exit;
694 }
695skip_optional:
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200696 return_value = zlib_adler32_impl(module, &data, value);
697
698exit:
699 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300700 if (data.obj) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200701 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300702 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200703
704 return return_value;
705}
706
707PyDoc_STRVAR(zlib_crc32__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800708"crc32($module, data, value=0, /)\n"
709"--\n"
710"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200711"Compute a CRC-32 checksum of data.\n"
712"\n"
713" value\n"
714" Starting value of the checksum.\n"
715"\n"
716"The returned checksum is an integer.");
717
718#define ZLIB_CRC32_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200719 {"crc32", (PyCFunction)(void(*)(void))zlib_crc32, METH_FASTCALL, zlib_crc32__doc__},
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200720
721static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300722zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200723
724static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200725zlib_crc32(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200726{
727 PyObject *return_value = NULL;
728 Py_buffer data = {NULL, NULL};
729 unsigned int value = 0;
730
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200731 if (!_PyArg_CheckPositional("crc32", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100732 goto exit;
733 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200734 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
735 goto exit;
736 }
737 if (!PyBuffer_IsContiguous(&data, 'C')) {
738 _PyArg_BadArgument("crc32", 1, "contiguous buffer", args[0]);
739 goto exit;
740 }
741 if (nargs < 2) {
742 goto skip_optional;
743 }
744 if (PyFloat_Check(args[1])) {
745 PyErr_SetString(PyExc_TypeError,
746 "integer argument expected, got float" );
747 goto exit;
748 }
749 value = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
750 if (value == (unsigned int)-1 && PyErr_Occurred()) {
751 goto exit;
752 }
753skip_optional:
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200754 return_value = zlib_crc32_impl(module, &data, value);
755
756exit:
757 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300758 if (data.obj) {
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200759 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300760 }
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200761
762 return return_value;
763}
Larry Hastings0759f842015-04-03 13:09:02 -0700764
765#ifndef ZLIB_COMPRESS_COPY_METHODDEF
766 #define ZLIB_COMPRESS_COPY_METHODDEF
767#endif /* !defined(ZLIB_COMPRESS_COPY_METHODDEF) */
Tal Einat4f574092017-11-03 11:09:00 +0200768
Zackery Spytzd2cbfff2018-06-27 12:04:51 -0600769#ifndef ZLIB_COMPRESS___COPY___METHODDEF
770 #define ZLIB_COMPRESS___COPY___METHODDEF
771#endif /* !defined(ZLIB_COMPRESS___COPY___METHODDEF) */
772
773#ifndef ZLIB_COMPRESS___DEEPCOPY___METHODDEF
774 #define ZLIB_COMPRESS___DEEPCOPY___METHODDEF
775#endif /* !defined(ZLIB_COMPRESS___DEEPCOPY___METHODDEF) */
776
Tal Einat4f574092017-11-03 11:09:00 +0200777#ifndef ZLIB_DECOMPRESS_COPY_METHODDEF
778 #define ZLIB_DECOMPRESS_COPY_METHODDEF
779#endif /* !defined(ZLIB_DECOMPRESS_COPY_METHODDEF) */
Zackery Spytzd2cbfff2018-06-27 12:04:51 -0600780
781#ifndef ZLIB_DECOMPRESS___COPY___METHODDEF
782 #define ZLIB_DECOMPRESS___COPY___METHODDEF
783#endif /* !defined(ZLIB_DECOMPRESS___COPY___METHODDEF) */
784
785#ifndef ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
786 #define ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
787#endif /* !defined(ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF) */
Serhiy Storchaka31913912019-03-14 10:32:22 +0200788/*[clinic end generated code: output=feb079cebbbaacd6 input=a9049054013a1b77]*/