blob: b1af7ce57ca00a2a9bf3b9aaea1beb26424efb38 [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 *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030020zlib_compress_impl(PyObject *module, Py_buffer *bytes, int level);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020021
22static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030023zlib_compress(PyObject *module, PyObject *args)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020024{
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"
Martin Panter0fdf41d2016-05-27 07:32:11 +000051" The window buffer size and container format.\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020052" 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 *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030059zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
Martin Panter84544c12016-07-23 03:02:07 +000060 Py_ssize_t bufsize);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020061
62static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030063zlib_decompress(PyObject *module, PyObject *args)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020064{
65 PyObject *return_value = NULL;
66 Py_buffer data = {NULL, NULL};
67 int wbits = MAX_WBITS;
Martin Panter84544c12016-07-23 03:02:07 +000068 Py_ssize_t bufsize = DEF_BUF_SIZE;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020069
Serhiy Storchaka247789c2015-04-24 00:40:51 +030070 if (!PyArg_ParseTuple(args, "y*|iO&:decompress",
Martin Panter84544c12016-07-23 03:02:07 +000071 &data, &wbits, ssize_t_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"
Martin Panter0fdf41d2016-05-27 07:32:11 +000098" +9 to +15: The base-two logarithm of the window size. Include a zlib\n"
99" container.\n"
100" -9 to -15: Generate a raw stream.\n"
101" +25 to +31: Include a gzip container.\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200102" memLevel\n"
103" Controls the amount of memory used for internal compression state.\n"
104" Valid values range from 1 to 9. Higher values result in higher memory\n"
105" usage, faster compression, and smaller output.\n"
106" strategy\n"
107" Used to tune the compression algorithm. Possible values are\n"
108" Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.\n"
109" zdict\n"
110" The predefined compression dictionary - a sequence of bytes\n"
111" containing subsequences that are likely to occur in the input data.");
112
113#define ZLIB_COMPRESSOBJ_METHODDEF \
114 {"compressobj", (PyCFunction)zlib_compressobj, METH_VARARGS|METH_KEYWORDS, zlib_compressobj__doc__},
115
116static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300117zlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
Larry Hastings89964c42015-04-14 18:07:59 -0400118 int memLevel, int strategy, Py_buffer *zdict);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200119
120static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300121zlib_compressobj(PyObject *module, PyObject *args, PyObject *kwargs)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200122{
123 PyObject *return_value = NULL;
124 static char *_keywords[] = {"level", "method", "wbits", "memLevel", "strategy", "zdict", NULL};
125 int level = Z_DEFAULT_COMPRESSION;
126 int method = DEFLATED;
127 int wbits = MAX_WBITS;
128 int memLevel = DEF_MEM_LEVEL;
129 int strategy = Z_DEFAULT_STRATEGY;
130 Py_buffer zdict = {NULL, NULL};
131
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300132 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiiy*:compressobj", _keywords,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200133 &level, &method, &wbits, &memLevel, &strategy, &zdict))
134 goto exit;
135 return_value = zlib_compressobj_impl(module, level, method, wbits, memLevel, strategy, &zdict);
136
137exit:
138 /* Cleanup for zdict */
139 if (zdict.obj)
140 PyBuffer_Release(&zdict);
141
142 return return_value;
143}
144
145PyDoc_STRVAR(zlib_decompressobj__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800146"decompressobj($module, /, wbits=MAX_WBITS, zdict=b\'\')\n"
147"--\n"
148"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200149"Return a decompressor object.\n"
150"\n"
151" wbits\n"
Martin Panter0fdf41d2016-05-27 07:32:11 +0000152" The window buffer size and container format.\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200153" zdict\n"
154" The predefined compression dictionary. This must be the same\n"
155" dictionary as used by the compressor that produced the input data.");
156
157#define ZLIB_DECOMPRESSOBJ_METHODDEF \
158 {"decompressobj", (PyCFunction)zlib_decompressobj, METH_VARARGS|METH_KEYWORDS, zlib_decompressobj__doc__},
159
160static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300161zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200162
163static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300164zlib_decompressobj(PyObject *module, PyObject *args, PyObject *kwargs)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200165{
166 PyObject *return_value = NULL;
167 static char *_keywords[] = {"wbits", "zdict", NULL};
168 int wbits = MAX_WBITS;
169 PyObject *zdict = NULL;
170
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300171 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iO:decompressobj", _keywords,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200172 &wbits, &zdict))
173 goto exit;
174 return_value = zlib_decompressobj_impl(module, wbits, zdict);
175
176exit:
177 return return_value;
178}
179
180PyDoc_STRVAR(zlib_Compress_compress__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800181"compress($self, data, /)\n"
182"--\n"
183"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200184"Returns a bytes object containing compressed data.\n"
185"\n"
186" data\n"
187" Binary data to be compressed.\n"
188"\n"
189"After calling this function, some of the input data may still\n"
190"be stored in internal buffers for later processing.\n"
191"Call the flush() method to clear these buffers.");
192
193#define ZLIB_COMPRESS_COMPRESS_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300194 {"compress", (PyCFunction)zlib_Compress_compress, METH_O, zlib_Compress_compress__doc__},
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200195
196static PyObject *
197zlib_Compress_compress_impl(compobject *self, Py_buffer *data);
198
199static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300200zlib_Compress_compress(compobject *self, PyObject *arg)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200201{
202 PyObject *return_value = NULL;
203 Py_buffer data = {NULL, NULL};
204
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300205 if (!PyArg_Parse(arg, "y*:compress", &data))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200206 goto exit;
207 return_value = zlib_Compress_compress_impl(self, &data);
208
209exit:
210 /* Cleanup for data */
211 if (data.obj)
212 PyBuffer_Release(&data);
213
214 return return_value;
215}
216
217PyDoc_STRVAR(zlib_Decompress_decompress__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800218"decompress($self, data, max_length=0, /)\n"
219"--\n"
220"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200221"Return a bytes object containing the decompressed version of the data.\n"
222"\n"
223" data\n"
224" The binary data to decompress.\n"
225" max_length\n"
226" The maximum allowable length of the decompressed data.\n"
227" Unconsumed input data will be stored in\n"
228" the unconsumed_tail attribute.\n"
229"\n"
230"After calling this function, some of the input data may still be stored in\n"
231"internal buffers for later processing.\n"
232"Call the flush() method to clear these buffers.");
233
234#define ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF \
235 {"decompress", (PyCFunction)zlib_Decompress_decompress, METH_VARARGS, zlib_Decompress_decompress__doc__},
236
237static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400238zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
Martin Panter84544c12016-07-23 03:02:07 +0000239 Py_ssize_t max_length);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200240
241static PyObject *
242zlib_Decompress_decompress(compobject *self, PyObject *args)
243{
244 PyObject *return_value = NULL;
245 Py_buffer data = {NULL, NULL};
Martin Panter84544c12016-07-23 03:02:07 +0000246 Py_ssize_t max_length = 0;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200247
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300248 if (!PyArg_ParseTuple(args, "y*|O&:decompress",
Martin Panter84544c12016-07-23 03:02:07 +0000249 &data, ssize_t_converter, &max_length))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200250 goto exit;
251 return_value = zlib_Decompress_decompress_impl(self, &data, max_length);
252
253exit:
254 /* Cleanup for data */
255 if (data.obj)
256 PyBuffer_Release(&data);
257
258 return return_value;
259}
260
261PyDoc_STRVAR(zlib_Compress_flush__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800262"flush($self, mode=zlib.Z_FINISH, /)\n"
263"--\n"
264"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200265"Return a bytes object containing any remaining compressed data.\n"
266"\n"
267" mode\n"
268" One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.\n"
269" If mode == Z_FINISH, the compressor object can no longer be\n"
270" used after calling the flush() method. Otherwise, more data\n"
271" can still be compressed.");
272
273#define ZLIB_COMPRESS_FLUSH_METHODDEF \
274 {"flush", (PyCFunction)zlib_Compress_flush, METH_VARARGS, zlib_Compress_flush__doc__},
275
276static PyObject *
277zlib_Compress_flush_impl(compobject *self, int mode);
278
279static PyObject *
280zlib_Compress_flush(compobject *self, PyObject *args)
281{
282 PyObject *return_value = NULL;
283 int mode = Z_FINISH;
284
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300285 if (!PyArg_ParseTuple(args, "|i:flush",
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200286 &mode))
287 goto exit;
288 return_value = zlib_Compress_flush_impl(self, mode);
289
290exit:
291 return return_value;
292}
293
Larry Hastings7726ac92014-01-31 22:03:12 -0800294#if defined(HAVE_ZLIB_COPY)
295
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200296PyDoc_STRVAR(zlib_Compress_copy__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800297"copy($self, /)\n"
298"--\n"
299"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200300"Return a copy of the compression object.");
301
302#define ZLIB_COMPRESS_COPY_METHODDEF \
303 {"copy", (PyCFunction)zlib_Compress_copy, METH_NOARGS, zlib_Compress_copy__doc__},
304
305static PyObject *
306zlib_Compress_copy_impl(compobject *self);
307
308static PyObject *
309zlib_Compress_copy(compobject *self, PyObject *Py_UNUSED(ignored))
310{
311 return zlib_Compress_copy_impl(self);
312}
313
Larry Hastings7726ac92014-01-31 22:03:12 -0800314#endif /* defined(HAVE_ZLIB_COPY) */
315
Larry Hastings7726ac92014-01-31 22:03:12 -0800316#if defined(HAVE_ZLIB_COPY)
317
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200318PyDoc_STRVAR(zlib_Decompress_copy__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800319"copy($self, /)\n"
320"--\n"
321"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200322"Return a copy of the decompression object.");
323
324#define ZLIB_DECOMPRESS_COPY_METHODDEF \
325 {"copy", (PyCFunction)zlib_Decompress_copy, METH_NOARGS, zlib_Decompress_copy__doc__},
326
327static PyObject *
328zlib_Decompress_copy_impl(compobject *self);
329
330static PyObject *
331zlib_Decompress_copy(compobject *self, PyObject *Py_UNUSED(ignored))
332{
333 return zlib_Decompress_copy_impl(self);
334}
335
Larry Hastings7726ac92014-01-31 22:03:12 -0800336#endif /* defined(HAVE_ZLIB_COPY) */
337
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200338PyDoc_STRVAR(zlib_Decompress_flush__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800339"flush($self, length=zlib.DEF_BUF_SIZE, /)\n"
340"--\n"
341"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200342"Return a bytes object containing any remaining decompressed data.\n"
343"\n"
344" length\n"
345" the initial size of the output buffer.");
346
347#define ZLIB_DECOMPRESS_FLUSH_METHODDEF \
348 {"flush", (PyCFunction)zlib_Decompress_flush, METH_VARARGS, zlib_Decompress_flush__doc__},
349
350static PyObject *
Martin Panter84544c12016-07-23 03:02:07 +0000351zlib_Decompress_flush_impl(compobject *self, Py_ssize_t length);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200352
353static PyObject *
354zlib_Decompress_flush(compobject *self, PyObject *args)
355{
356 PyObject *return_value = NULL;
Martin Panter84544c12016-07-23 03:02:07 +0000357 Py_ssize_t length = DEF_BUF_SIZE;
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200358
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300359 if (!PyArg_ParseTuple(args, "|O&:flush",
Martin Panter84544c12016-07-23 03:02:07 +0000360 ssize_t_converter, &length))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200361 goto exit;
362 return_value = zlib_Decompress_flush_impl(self, length);
363
364exit:
365 return return_value;
366}
367
368PyDoc_STRVAR(zlib_adler32__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800369"adler32($module, data, value=1, /)\n"
370"--\n"
371"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200372"Compute an Adler-32 checksum of data.\n"
373"\n"
374" value\n"
375" Starting value of the checksum.\n"
376"\n"
377"The returned checksum is an integer.");
378
379#define ZLIB_ADLER32_METHODDEF \
380 {"adler32", (PyCFunction)zlib_adler32, METH_VARARGS, zlib_adler32__doc__},
381
382static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300383zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200384
385static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300386zlib_adler32(PyObject *module, PyObject *args)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200387{
388 PyObject *return_value = NULL;
389 Py_buffer data = {NULL, NULL};
390 unsigned int value = 1;
391
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300392 if (!PyArg_ParseTuple(args, "y*|I:adler32",
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200393 &data, &value))
394 goto exit;
395 return_value = zlib_adler32_impl(module, &data, value);
396
397exit:
398 /* Cleanup for data */
399 if (data.obj)
400 PyBuffer_Release(&data);
401
402 return return_value;
403}
404
405PyDoc_STRVAR(zlib_crc32__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800406"crc32($module, data, value=0, /)\n"
407"--\n"
408"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200409"Compute a CRC-32 checksum of data.\n"
410"\n"
411" value\n"
412" Starting value of the checksum.\n"
413"\n"
414"The returned checksum is an integer.");
415
416#define ZLIB_CRC32_METHODDEF \
417 {"crc32", (PyCFunction)zlib_crc32, METH_VARARGS, zlib_crc32__doc__},
418
419static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300420zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200421
422static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300423zlib_crc32(PyObject *module, PyObject *args)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200424{
425 PyObject *return_value = NULL;
426 Py_buffer data = {NULL, NULL};
427 unsigned int value = 0;
428
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300429 if (!PyArg_ParseTuple(args, "y*|I:crc32",
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200430 &data, &value))
431 goto exit;
432 return_value = zlib_crc32_impl(module, &data, value);
433
434exit:
435 /* Cleanup for data */
436 if (data.obj)
437 PyBuffer_Release(&data);
438
439 return return_value;
440}
Larry Hastings0759f842015-04-03 13:09:02 -0700441
442#ifndef ZLIB_COMPRESS_COPY_METHODDEF
443 #define ZLIB_COMPRESS_COPY_METHODDEF
444#endif /* !defined(ZLIB_COMPRESS_COPY_METHODDEF) */
Martin Panter84544c12016-07-23 03:02:07 +0000445/*[clinic end generated code: output=7711ef02d1d5776c input=a9049054013a1b77]*/