blob: 2d75bc912ecee3ce4c6623088de00d8f1f7b954f [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__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08006"compress($module, bytes, level=Z_DEFAULT_COMPRESSION, /)\n"
7"--\n"
8"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +02009"Returns a bytes object containing compressed data.\n"
10"\n"
11" bytes\n"
12" Binary data to be compressed.\n"
13" level\n"
14" Compression level, in 0-9.");
15
16#define ZLIB_COMPRESS_METHODDEF \
17 {"compress", (PyCFunction)zlib_compress, METH_VARARGS, zlib_compress__doc__},
18
19static PyObject *
20zlib_compress_impl(PyModuleDef *module, Py_buffer *bytes, int level);
21
22static PyObject *
23zlib_compress(PyModuleDef *module, PyObject *args)
24{
25 PyObject *return_value = NULL;
26 Py_buffer bytes = {NULL, NULL};
27 int level = Z_DEFAULT_COMPRESSION;
28
Serhiy Storchaka247789c2015-04-24 00:40:51 +030029 if (!PyArg_ParseTuple(args, "y*|i:compress",
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020030 &bytes, &level))
31 goto exit;
32 return_value = zlib_compress_impl(module, &bytes, level);
33
34exit:
35 /* Cleanup for bytes */
36 if (bytes.obj)
37 PyBuffer_Release(&bytes);
38
39 return return_value;
40}
41
42PyDoc_STRVAR(zlib_decompress__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -080043"decompress($module, data, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE, /)\n"
44"--\n"
45"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020046"Returns a bytes object containing the uncompressed data.\n"
47"\n"
48" data\n"
49" Compressed data.\n"
50" wbits\n"
51" The window buffer size.\n"
52" bufsize\n"
53" The initial output buffer size.");
54
55#define ZLIB_DECOMPRESS_METHODDEF \
56 {"decompress", (PyCFunction)zlib_decompress, METH_VARARGS, zlib_decompress__doc__},
57
58static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -040059zlib_decompress_impl(PyModuleDef *module, Py_buffer *data, int wbits,
60 unsigned int bufsize);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020061
62static PyObject *
63zlib_decompress(PyModuleDef *module, PyObject *args)
64{
65 PyObject *return_value = NULL;
66 Py_buffer data = {NULL, NULL};
67 int wbits = MAX_WBITS;
68 unsigned int bufsize = DEF_BUF_SIZE;
69
Serhiy Storchaka247789c2015-04-24 00:40:51 +030070 if (!PyArg_ParseTuple(args, "y*|iO&:decompress",
Martin Pantere99e9772015-11-20 08:13:35 +000071 &data, &wbits, capped_uint_converter, &bufsize))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020072 goto exit;
73 return_value = zlib_decompress_impl(module, &data, wbits, bufsize);
74
75exit:
76 /* Cleanup for data */
77 if (data.obj)
78 PyBuffer_Release(&data);
79
80 return return_value;
81}
82
83PyDoc_STRVAR(zlib_compressobj__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -080084"compressobj($module, /, level=Z_DEFAULT_COMPRESSION, method=DEFLATED,\n"
85" wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL,\n"
86" strategy=Z_DEFAULT_STRATEGY, zdict=None)\n"
87"--\n"
88"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020089"Return a compressor object.\n"
90"\n"
91" level\n"
Martin Panter567d5132016-02-03 07:06:33 +000092" The compression level (an integer in the range 0-9 or -1; default is\n"
93" currently equivalent to 6). Higher compression levels are slower,\n"
94" but produce smaller results.\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020095" method\n"
96" The compression algorithm. If given, this must be DEFLATED.\n"
97" wbits\n"
98" The base two logarithm of the window size (range: 8..15).\n"
99" memLevel\n"
100" Controls the amount of memory used for internal compression state.\n"
101" Valid values range from 1 to 9. Higher values result in higher memory\n"
102" usage, faster compression, and smaller output.\n"
103" strategy\n"
104" Used to tune the compression algorithm. Possible values are\n"
105" Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.\n"
106" zdict\n"
107" The predefined compression dictionary - a sequence of bytes\n"
108" containing subsequences that are likely to occur in the input data.");
109
110#define ZLIB_COMPRESSOBJ_METHODDEF \
111 {"compressobj", (PyCFunction)zlib_compressobj, METH_VARARGS|METH_KEYWORDS, zlib_compressobj__doc__},
112
113static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400114zlib_compressobj_impl(PyModuleDef *module, int level, int method, int wbits,
115 int memLevel, int strategy, Py_buffer *zdict);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200116
117static PyObject *
118zlib_compressobj(PyModuleDef *module, PyObject *args, PyObject *kwargs)
119{
120 PyObject *return_value = NULL;
121 static char *_keywords[] = {"level", "method", "wbits", "memLevel", "strategy", "zdict", NULL};
122 int level = Z_DEFAULT_COMPRESSION;
123 int method = DEFLATED;
124 int wbits = MAX_WBITS;
125 int memLevel = DEF_MEM_LEVEL;
126 int strategy = Z_DEFAULT_STRATEGY;
127 Py_buffer zdict = {NULL, NULL};
128
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300129 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiiy*:compressobj", _keywords,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200130 &level, &method, &wbits, &memLevel, &strategy, &zdict))
131 goto exit;
132 return_value = zlib_compressobj_impl(module, level, method, wbits, memLevel, strategy, &zdict);
133
134exit:
135 /* Cleanup for zdict */
136 if (zdict.obj)
137 PyBuffer_Release(&zdict);
138
139 return return_value;
140}
141
142PyDoc_STRVAR(zlib_decompressobj__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800143"decompressobj($module, /, wbits=MAX_WBITS, zdict=b\'\')\n"
144"--\n"
145"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200146"Return a decompressor object.\n"
147"\n"
148" wbits\n"
149" The window buffer size.\n"
150" zdict\n"
151" The predefined compression dictionary. This must be the same\n"
152" dictionary as used by the compressor that produced the input data.");
153
154#define ZLIB_DECOMPRESSOBJ_METHODDEF \
155 {"decompressobj", (PyCFunction)zlib_decompressobj, METH_VARARGS|METH_KEYWORDS, zlib_decompressobj__doc__},
156
157static PyObject *
158zlib_decompressobj_impl(PyModuleDef *module, int wbits, PyObject *zdict);
159
160static PyObject *
161zlib_decompressobj(PyModuleDef *module, PyObject *args, PyObject *kwargs)
162{
163 PyObject *return_value = NULL;
164 static char *_keywords[] = {"wbits", "zdict", NULL};
165 int wbits = MAX_WBITS;
166 PyObject *zdict = NULL;
167
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300168 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iO:decompressobj", _keywords,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200169 &wbits, &zdict))
170 goto exit;
171 return_value = zlib_decompressobj_impl(module, wbits, zdict);
172
173exit:
174 return return_value;
175}
176
177PyDoc_STRVAR(zlib_Compress_compress__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800178"compress($self, data, /)\n"
179"--\n"
180"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200181"Returns a bytes object containing compressed data.\n"
182"\n"
183" data\n"
184" Binary data to be compressed.\n"
185"\n"
186"After calling this function, some of the input data may still\n"
187"be stored in internal buffers for later processing.\n"
188"Call the flush() method to clear these buffers.");
189
190#define ZLIB_COMPRESS_COMPRESS_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300191 {"compress", (PyCFunction)zlib_Compress_compress, METH_O, zlib_Compress_compress__doc__},
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200192
193static PyObject *
194zlib_Compress_compress_impl(compobject *self, Py_buffer *data);
195
196static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300197zlib_Compress_compress(compobject *self, PyObject *arg)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200198{
199 PyObject *return_value = NULL;
200 Py_buffer data = {NULL, NULL};
201
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300202 if (!PyArg_Parse(arg, "y*:compress", &data))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200203 goto exit;
204 return_value = zlib_Compress_compress_impl(self, &data);
205
206exit:
207 /* Cleanup for data */
208 if (data.obj)
209 PyBuffer_Release(&data);
210
211 return return_value;
212}
213
214PyDoc_STRVAR(zlib_Decompress_decompress__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800215"decompress($self, data, max_length=0, /)\n"
216"--\n"
217"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200218"Return a bytes object containing the decompressed version of the data.\n"
219"\n"
220" data\n"
221" The binary data to decompress.\n"
222" max_length\n"
223" The maximum allowable length of the decompressed data.\n"
224" Unconsumed input data will be stored in\n"
225" the unconsumed_tail attribute.\n"
226"\n"
227"After calling this function, some of the input data may still be stored in\n"
228"internal buffers for later processing.\n"
229"Call the flush() method to clear these buffers.");
230
231#define ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF \
232 {"decompress", (PyCFunction)zlib_Decompress_decompress, METH_VARARGS, zlib_Decompress_decompress__doc__},
233
234static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400235zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
236 unsigned int max_length);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200237
238static PyObject *
239zlib_Decompress_decompress(compobject *self, PyObject *args)
240{
241 PyObject *return_value = NULL;
242 Py_buffer data = {NULL, NULL};
243 unsigned int max_length = 0;
244
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300245 if (!PyArg_ParseTuple(args, "y*|O&:decompress",
Martin Pantere99e9772015-11-20 08:13:35 +0000246 &data, capped_uint_converter, &max_length))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200247 goto exit;
248 return_value = zlib_Decompress_decompress_impl(self, &data, max_length);
249
250exit:
251 /* Cleanup for data */
252 if (data.obj)
253 PyBuffer_Release(&data);
254
255 return return_value;
256}
257
258PyDoc_STRVAR(zlib_Compress_flush__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800259"flush($self, mode=zlib.Z_FINISH, /)\n"
260"--\n"
261"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200262"Return a bytes object containing any remaining compressed data.\n"
263"\n"
264" mode\n"
265" One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.\n"
266" If mode == Z_FINISH, the compressor object can no longer be\n"
267" used after calling the flush() method. Otherwise, more data\n"
268" can still be compressed.");
269
270#define ZLIB_COMPRESS_FLUSH_METHODDEF \
271 {"flush", (PyCFunction)zlib_Compress_flush, METH_VARARGS, zlib_Compress_flush__doc__},
272
273static PyObject *
274zlib_Compress_flush_impl(compobject *self, int mode);
275
276static PyObject *
277zlib_Compress_flush(compobject *self, PyObject *args)
278{
279 PyObject *return_value = NULL;
280 int mode = Z_FINISH;
281
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300282 if (!PyArg_ParseTuple(args, "|i:flush",
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200283 &mode))
284 goto exit;
285 return_value = zlib_Compress_flush_impl(self, mode);
286
287exit:
288 return return_value;
289}
290
Larry Hastings7726ac92014-01-31 22:03:12 -0800291#if defined(HAVE_ZLIB_COPY)
292
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200293PyDoc_STRVAR(zlib_Compress_copy__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800294"copy($self, /)\n"
295"--\n"
296"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200297"Return a copy of the compression object.");
298
299#define ZLIB_COMPRESS_COPY_METHODDEF \
300 {"copy", (PyCFunction)zlib_Compress_copy, METH_NOARGS, zlib_Compress_copy__doc__},
301
302static PyObject *
303zlib_Compress_copy_impl(compobject *self);
304
305static PyObject *
306zlib_Compress_copy(compobject *self, PyObject *Py_UNUSED(ignored))
307{
308 return zlib_Compress_copy_impl(self);
309}
310
Larry Hastings7726ac92014-01-31 22:03:12 -0800311#endif /* defined(HAVE_ZLIB_COPY) */
312
Larry Hastings7726ac92014-01-31 22:03:12 -0800313#if defined(HAVE_ZLIB_COPY)
314
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200315PyDoc_STRVAR(zlib_Decompress_copy__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800316"copy($self, /)\n"
317"--\n"
318"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200319"Return a copy of the decompression object.");
320
321#define ZLIB_DECOMPRESS_COPY_METHODDEF \
322 {"copy", (PyCFunction)zlib_Decompress_copy, METH_NOARGS, zlib_Decompress_copy__doc__},
323
324static PyObject *
325zlib_Decompress_copy_impl(compobject *self);
326
327static PyObject *
328zlib_Decompress_copy(compobject *self, PyObject *Py_UNUSED(ignored))
329{
330 return zlib_Decompress_copy_impl(self);
331}
332
Larry Hastings7726ac92014-01-31 22:03:12 -0800333#endif /* defined(HAVE_ZLIB_COPY) */
334
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200335PyDoc_STRVAR(zlib_Decompress_flush__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800336"flush($self, length=zlib.DEF_BUF_SIZE, /)\n"
337"--\n"
338"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200339"Return a bytes object containing any remaining decompressed data.\n"
340"\n"
341" length\n"
342" the initial size of the output buffer.");
343
344#define ZLIB_DECOMPRESS_FLUSH_METHODDEF \
345 {"flush", (PyCFunction)zlib_Decompress_flush, METH_VARARGS, zlib_Decompress_flush__doc__},
346
347static PyObject *
348zlib_Decompress_flush_impl(compobject *self, unsigned int length);
349
350static PyObject *
351zlib_Decompress_flush(compobject *self, PyObject *args)
352{
353 PyObject *return_value = NULL;
354 unsigned int length = DEF_BUF_SIZE;
355
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300356 if (!PyArg_ParseTuple(args, "|O&:flush",
Martin Pantere99e9772015-11-20 08:13:35 +0000357 capped_uint_converter, &length))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200358 goto exit;
359 return_value = zlib_Decompress_flush_impl(self, length);
360
361exit:
362 return return_value;
363}
364
365PyDoc_STRVAR(zlib_adler32__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800366"adler32($module, data, value=1, /)\n"
367"--\n"
368"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200369"Compute an Adler-32 checksum of data.\n"
370"\n"
371" value\n"
372" Starting value of the checksum.\n"
373"\n"
374"The returned checksum is an integer.");
375
376#define ZLIB_ADLER32_METHODDEF \
377 {"adler32", (PyCFunction)zlib_adler32, METH_VARARGS, zlib_adler32__doc__},
378
379static PyObject *
380zlib_adler32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value);
381
382static PyObject *
383zlib_adler32(PyModuleDef *module, PyObject *args)
384{
385 PyObject *return_value = NULL;
386 Py_buffer data = {NULL, NULL};
387 unsigned int value = 1;
388
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300389 if (!PyArg_ParseTuple(args, "y*|I:adler32",
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200390 &data, &value))
391 goto exit;
392 return_value = zlib_adler32_impl(module, &data, value);
393
394exit:
395 /* Cleanup for data */
396 if (data.obj)
397 PyBuffer_Release(&data);
398
399 return return_value;
400}
401
402PyDoc_STRVAR(zlib_crc32__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800403"crc32($module, data, value=0, /)\n"
404"--\n"
405"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200406"Compute a CRC-32 checksum of data.\n"
407"\n"
408" value\n"
409" Starting value of the checksum.\n"
410"\n"
411"The returned checksum is an integer.");
412
413#define ZLIB_CRC32_METHODDEF \
414 {"crc32", (PyCFunction)zlib_crc32, METH_VARARGS, zlib_crc32__doc__},
415
416static PyObject *
417zlib_crc32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value);
418
419static PyObject *
420zlib_crc32(PyModuleDef *module, PyObject *args)
421{
422 PyObject *return_value = NULL;
423 Py_buffer data = {NULL, NULL};
424 unsigned int value = 0;
425
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300426 if (!PyArg_ParseTuple(args, "y*|I:crc32",
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200427 &data, &value))
428 goto exit;
429 return_value = zlib_crc32_impl(module, &data, value);
430
431exit:
432 /* Cleanup for data */
433 if (data.obj)
434 PyBuffer_Release(&data);
435
436 return return_value;
437}
Larry Hastings0759f842015-04-03 13:09:02 -0700438
439#ifndef ZLIB_COMPRESS_COPY_METHODDEF
440 #define ZLIB_COMPRESS_COPY_METHODDEF
441#endif /* !defined(ZLIB_COMPRESS_COPY_METHODDEF) */
Martin Panter567d5132016-02-03 07:06:33 +0000442/*[clinic end generated code: output=cf81e1deae3af0ce input=a9049054013a1b77]*/