blob: 222cb6779a1c0a70c0e5ac21c5d6b8979805ed36 [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__,
Martin Panter1fe0d132016-02-10 10:06:36 +00006"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 \
Martin Panter1fe0d132016-02-10 10:06:36 +000017 {"compress", (PyCFunction)zlib_compress, METH_VARARGS|METH_KEYWORDS, zlib_compress__doc__},
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020018
19static PyObject *
Martin Panter1fe0d132016-02-10 10:06:36 +000020zlib_compress_impl(PyModuleDef *module, Py_buffer *data, int level);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020021
22static PyObject *
Martin Panter1fe0d132016-02-10 10:06:36 +000023zlib_compress(PyModuleDef *module, PyObject *args, PyObject *kwargs)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020024{
25 PyObject *return_value = NULL;
Martin Panter1fe0d132016-02-10 10:06:36 +000026 static char *_keywords[] = {"data", "level", NULL};
27 Py_buffer data = {NULL, NULL};
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020028 int level = Z_DEFAULT_COMPRESSION;
29
Martin Panter1fe0d132016-02-10 10:06:36 +000030 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "y*|i:compress", _keywords,
31 &data, &level))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020032 goto exit;
Martin Panter1fe0d132016-02-10 10:06:36 +000033 return_value = zlib_compress_impl(module, &data, level);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020034
35exit:
Martin Panter1fe0d132016-02-10 10:06:36 +000036 /* Cleanup for data */
37 if (data.obj)
38 PyBuffer_Release(&data);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020039
40 return return_value;
41}
42
43PyDoc_STRVAR(zlib_decompress__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -080044"decompress($module, data, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE, /)\n"
45"--\n"
46"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020047"Returns a bytes object containing the uncompressed data.\n"
48"\n"
49" data\n"
50" Compressed data.\n"
51" wbits\n"
52" The window buffer size.\n"
53" bufsize\n"
54" The initial output buffer size.");
55
56#define ZLIB_DECOMPRESS_METHODDEF \
57 {"decompress", (PyCFunction)zlib_decompress, METH_VARARGS, zlib_decompress__doc__},
58
59static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -040060zlib_decompress_impl(PyModuleDef *module, Py_buffer *data, int wbits,
61 unsigned int bufsize);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020062
63static PyObject *
64zlib_decompress(PyModuleDef *module, PyObject *args)
65{
66 PyObject *return_value = NULL;
67 Py_buffer data = {NULL, NULL};
68 int wbits = MAX_WBITS;
69 unsigned int bufsize = DEF_BUF_SIZE;
70
Serhiy Storchaka247789c2015-04-24 00:40:51 +030071 if (!PyArg_ParseTuple(args, "y*|iO&:decompress",
Martin Pantere99e9772015-11-20 08:13:35 +000072 &data, &wbits, capped_uint_converter, &bufsize))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020073 goto exit;
74 return_value = zlib_decompress_impl(module, &data, wbits, bufsize);
75
76exit:
77 /* Cleanup for data */
78 if (data.obj)
79 PyBuffer_Release(&data);
80
81 return return_value;
82}
83
84PyDoc_STRVAR(zlib_compressobj__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -080085"compressobj($module, /, level=Z_DEFAULT_COMPRESSION, method=DEFLATED,\n"
86" wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL,\n"
87" strategy=Z_DEFAULT_STRATEGY, zdict=None)\n"
88"--\n"
89"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020090"Return a compressor object.\n"
91"\n"
92" level\n"
Martin Panter567d5132016-02-03 07:06:33 +000093" The compression level (an integer in the range 0-9 or -1; default is\n"
94" currently equivalent to 6). Higher compression levels are slower,\n"
95" but produce smaller results.\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020096" method\n"
97" The compression algorithm. If given, this must be DEFLATED.\n"
98" wbits\n"
99" The base two logarithm of the window size (range: 8..15).\n"
100" memLevel\n"
101" Controls the amount of memory used for internal compression state.\n"
102" Valid values range from 1 to 9. Higher values result in higher memory\n"
103" usage, faster compression, and smaller output.\n"
104" strategy\n"
105" Used to tune the compression algorithm. Possible values are\n"
106" Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.\n"
107" zdict\n"
108" The predefined compression dictionary - a sequence of bytes\n"
109" containing subsequences that are likely to occur in the input data.");
110
111#define ZLIB_COMPRESSOBJ_METHODDEF \
112 {"compressobj", (PyCFunction)zlib_compressobj, METH_VARARGS|METH_KEYWORDS, zlib_compressobj__doc__},
113
114static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400115zlib_compressobj_impl(PyModuleDef *module, int level, int method, int wbits,
116 int memLevel, int strategy, Py_buffer *zdict);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200117
118static PyObject *
119zlib_compressobj(PyModuleDef *module, PyObject *args, PyObject *kwargs)
120{
121 PyObject *return_value = NULL;
122 static char *_keywords[] = {"level", "method", "wbits", "memLevel", "strategy", "zdict", NULL};
123 int level = Z_DEFAULT_COMPRESSION;
124 int method = DEFLATED;
125 int wbits = MAX_WBITS;
126 int memLevel = DEF_MEM_LEVEL;
127 int strategy = Z_DEFAULT_STRATEGY;
128 Py_buffer zdict = {NULL, NULL};
129
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300130 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiiy*:compressobj", _keywords,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200131 &level, &method, &wbits, &memLevel, &strategy, &zdict))
132 goto exit;
133 return_value = zlib_compressobj_impl(module, level, method, wbits, memLevel, strategy, &zdict);
134
135exit:
136 /* Cleanup for zdict */
137 if (zdict.obj)
138 PyBuffer_Release(&zdict);
139
140 return return_value;
141}
142
143PyDoc_STRVAR(zlib_decompressobj__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800144"decompressobj($module, /, wbits=MAX_WBITS, zdict=b\'\')\n"
145"--\n"
146"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200147"Return a decompressor object.\n"
148"\n"
149" wbits\n"
150" The window buffer size.\n"
151" zdict\n"
152" The predefined compression dictionary. This must be the same\n"
153" dictionary as used by the compressor that produced the input data.");
154
155#define ZLIB_DECOMPRESSOBJ_METHODDEF \
156 {"decompressobj", (PyCFunction)zlib_decompressobj, METH_VARARGS|METH_KEYWORDS, zlib_decompressobj__doc__},
157
158static PyObject *
159zlib_decompressobj_impl(PyModuleDef *module, int wbits, PyObject *zdict);
160
161static PyObject *
162zlib_decompressobj(PyModuleDef *module, PyObject *args, PyObject *kwargs)
163{
164 PyObject *return_value = NULL;
165 static char *_keywords[] = {"wbits", "zdict", NULL};
166 int wbits = MAX_WBITS;
167 PyObject *zdict = NULL;
168
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300169 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iO:decompressobj", _keywords,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200170 &wbits, &zdict))
171 goto exit;
172 return_value = zlib_decompressobj_impl(module, wbits, zdict);
173
174exit:
175 return return_value;
176}
177
178PyDoc_STRVAR(zlib_Compress_compress__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800179"compress($self, data, /)\n"
180"--\n"
181"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200182"Returns a bytes object containing compressed data.\n"
183"\n"
184" data\n"
185" Binary data to be compressed.\n"
186"\n"
187"After calling this function, some of the input data may still\n"
188"be stored in internal buffers for later processing.\n"
189"Call the flush() method to clear these buffers.");
190
191#define ZLIB_COMPRESS_COMPRESS_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300192 {"compress", (PyCFunction)zlib_Compress_compress, METH_O, zlib_Compress_compress__doc__},
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200193
194static PyObject *
195zlib_Compress_compress_impl(compobject *self, Py_buffer *data);
196
197static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300198zlib_Compress_compress(compobject *self, PyObject *arg)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200199{
200 PyObject *return_value = NULL;
201 Py_buffer data = {NULL, NULL};
202
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300203 if (!PyArg_Parse(arg, "y*:compress", &data))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200204 goto exit;
205 return_value = zlib_Compress_compress_impl(self, &data);
206
207exit:
208 /* Cleanup for data */
209 if (data.obj)
210 PyBuffer_Release(&data);
211
212 return return_value;
213}
214
215PyDoc_STRVAR(zlib_Decompress_decompress__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800216"decompress($self, data, max_length=0, /)\n"
217"--\n"
218"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200219"Return a bytes object containing the decompressed version of the data.\n"
220"\n"
221" data\n"
222" The binary data to decompress.\n"
223" max_length\n"
224" The maximum allowable length of the decompressed data.\n"
225" Unconsumed input data will be stored in\n"
226" the unconsumed_tail attribute.\n"
227"\n"
228"After calling this function, some of the input data may still be stored in\n"
229"internal buffers for later processing.\n"
230"Call the flush() method to clear these buffers.");
231
232#define ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF \
233 {"decompress", (PyCFunction)zlib_Decompress_decompress, METH_VARARGS, zlib_Decompress_decompress__doc__},
234
235static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400236zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
237 unsigned int max_length);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200238
239static PyObject *
240zlib_Decompress_decompress(compobject *self, PyObject *args)
241{
242 PyObject *return_value = NULL;
243 Py_buffer data = {NULL, NULL};
244 unsigned int max_length = 0;
245
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300246 if (!PyArg_ParseTuple(args, "y*|O&:decompress",
Martin Pantere99e9772015-11-20 08:13:35 +0000247 &data, capped_uint_converter, &max_length))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200248 goto exit;
249 return_value = zlib_Decompress_decompress_impl(self, &data, max_length);
250
251exit:
252 /* Cleanup for data */
253 if (data.obj)
254 PyBuffer_Release(&data);
255
256 return return_value;
257}
258
259PyDoc_STRVAR(zlib_Compress_flush__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800260"flush($self, mode=zlib.Z_FINISH, /)\n"
261"--\n"
262"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200263"Return a bytes object containing any remaining compressed data.\n"
264"\n"
265" mode\n"
266" One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.\n"
267" If mode == Z_FINISH, the compressor object can no longer be\n"
268" used after calling the flush() method. Otherwise, more data\n"
269" can still be compressed.");
270
271#define ZLIB_COMPRESS_FLUSH_METHODDEF \
272 {"flush", (PyCFunction)zlib_Compress_flush, METH_VARARGS, zlib_Compress_flush__doc__},
273
274static PyObject *
275zlib_Compress_flush_impl(compobject *self, int mode);
276
277static PyObject *
278zlib_Compress_flush(compobject *self, PyObject *args)
279{
280 PyObject *return_value = NULL;
281 int mode = Z_FINISH;
282
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300283 if (!PyArg_ParseTuple(args, "|i:flush",
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200284 &mode))
285 goto exit;
286 return_value = zlib_Compress_flush_impl(self, mode);
287
288exit:
289 return return_value;
290}
291
Larry Hastings7726ac92014-01-31 22:03:12 -0800292#if defined(HAVE_ZLIB_COPY)
293
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200294PyDoc_STRVAR(zlib_Compress_copy__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800295"copy($self, /)\n"
296"--\n"
297"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200298"Return a copy of the compression object.");
299
300#define ZLIB_COMPRESS_COPY_METHODDEF \
301 {"copy", (PyCFunction)zlib_Compress_copy, METH_NOARGS, zlib_Compress_copy__doc__},
302
303static PyObject *
304zlib_Compress_copy_impl(compobject *self);
305
306static PyObject *
307zlib_Compress_copy(compobject *self, PyObject *Py_UNUSED(ignored))
308{
309 return zlib_Compress_copy_impl(self);
310}
311
Larry Hastings7726ac92014-01-31 22:03:12 -0800312#endif /* defined(HAVE_ZLIB_COPY) */
313
Larry Hastings7726ac92014-01-31 22:03:12 -0800314#if defined(HAVE_ZLIB_COPY)
315
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200316PyDoc_STRVAR(zlib_Decompress_copy__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800317"copy($self, /)\n"
318"--\n"
319"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200320"Return a copy of the decompression object.");
321
322#define ZLIB_DECOMPRESS_COPY_METHODDEF \
323 {"copy", (PyCFunction)zlib_Decompress_copy, METH_NOARGS, zlib_Decompress_copy__doc__},
324
325static PyObject *
326zlib_Decompress_copy_impl(compobject *self);
327
328static PyObject *
329zlib_Decompress_copy(compobject *self, PyObject *Py_UNUSED(ignored))
330{
331 return zlib_Decompress_copy_impl(self);
332}
333
Larry Hastings7726ac92014-01-31 22:03:12 -0800334#endif /* defined(HAVE_ZLIB_COPY) */
335
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200336PyDoc_STRVAR(zlib_Decompress_flush__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800337"flush($self, length=zlib.DEF_BUF_SIZE, /)\n"
338"--\n"
339"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200340"Return a bytes object containing any remaining decompressed data.\n"
341"\n"
342" length\n"
343" the initial size of the output buffer.");
344
345#define ZLIB_DECOMPRESS_FLUSH_METHODDEF \
346 {"flush", (PyCFunction)zlib_Decompress_flush, METH_VARARGS, zlib_Decompress_flush__doc__},
347
348static PyObject *
349zlib_Decompress_flush_impl(compobject *self, unsigned int length);
350
351static PyObject *
352zlib_Decompress_flush(compobject *self, PyObject *args)
353{
354 PyObject *return_value = NULL;
355 unsigned int length = DEF_BUF_SIZE;
356
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300357 if (!PyArg_ParseTuple(args, "|O&:flush",
Martin Pantere99e9772015-11-20 08:13:35 +0000358 capped_uint_converter, &length))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200359 goto exit;
360 return_value = zlib_Decompress_flush_impl(self, length);
361
362exit:
363 return return_value;
364}
365
366PyDoc_STRVAR(zlib_adler32__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800367"adler32($module, data, value=1, /)\n"
368"--\n"
369"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200370"Compute an Adler-32 checksum of data.\n"
371"\n"
372" value\n"
373" Starting value of the checksum.\n"
374"\n"
375"The returned checksum is an integer.");
376
377#define ZLIB_ADLER32_METHODDEF \
378 {"adler32", (PyCFunction)zlib_adler32, METH_VARARGS, zlib_adler32__doc__},
379
380static PyObject *
381zlib_adler32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value);
382
383static PyObject *
384zlib_adler32(PyModuleDef *module, PyObject *args)
385{
386 PyObject *return_value = NULL;
387 Py_buffer data = {NULL, NULL};
388 unsigned int value = 1;
389
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300390 if (!PyArg_ParseTuple(args, "y*|I:adler32",
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200391 &data, &value))
392 goto exit;
393 return_value = zlib_adler32_impl(module, &data, value);
394
395exit:
396 /* Cleanup for data */
397 if (data.obj)
398 PyBuffer_Release(&data);
399
400 return return_value;
401}
402
403PyDoc_STRVAR(zlib_crc32__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800404"crc32($module, data, value=0, /)\n"
405"--\n"
406"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200407"Compute a CRC-32 checksum of data.\n"
408"\n"
409" value\n"
410" Starting value of the checksum.\n"
411"\n"
412"The returned checksum is an integer.");
413
414#define ZLIB_CRC32_METHODDEF \
415 {"crc32", (PyCFunction)zlib_crc32, METH_VARARGS, zlib_crc32__doc__},
416
417static PyObject *
418zlib_crc32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value);
419
420static PyObject *
421zlib_crc32(PyModuleDef *module, PyObject *args)
422{
423 PyObject *return_value = NULL;
424 Py_buffer data = {NULL, NULL};
425 unsigned int value = 0;
426
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300427 if (!PyArg_ParseTuple(args, "y*|I:crc32",
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200428 &data, &value))
429 goto exit;
430 return_value = zlib_crc32_impl(module, &data, value);
431
432exit:
433 /* Cleanup for data */
434 if (data.obj)
435 PyBuffer_Release(&data);
436
437 return return_value;
438}
Larry Hastings0759f842015-04-03 13:09:02 -0700439
440#ifndef ZLIB_COMPRESS_COPY_METHODDEF
441 #define ZLIB_COMPRESS_COPY_METHODDEF
442#endif /* !defined(ZLIB_COMPRESS_COPY_METHODDEF) */
Martin Panterb0cb42d2016-02-10 10:45:54 +0000443/*[clinic end generated code: output=e6f3b79e051ecc35 input=a9049054013a1b77]*/