blob: 35661a5a46eea465e8c523f12c50977e9e1db164 [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",
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +020071 &data, &wbits, uint_converter, &bufsize))
72 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"
92" The compression level (an integer in the range 0-9; default is 6).\n"
93" Higher compression levels are slower, but produce smaller results.\n"
94" method\n"
95" The compression algorithm. If given, this must be DEFLATED.\n"
96" wbits\n"
97" The base two logarithm of the window size (range: 8..15).\n"
98" memLevel\n"
99" Controls the amount of memory used for internal compression state.\n"
100" Valid values range from 1 to 9. Higher values result in higher memory\n"
101" usage, faster compression, and smaller output.\n"
102" strategy\n"
103" Used to tune the compression algorithm. Possible values are\n"
104" Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.\n"
105" zdict\n"
106" The predefined compression dictionary - a sequence of bytes\n"
107" containing subsequences that are likely to occur in the input data.");
108
109#define ZLIB_COMPRESSOBJ_METHODDEF \
110 {"compressobj", (PyCFunction)zlib_compressobj, METH_VARARGS|METH_KEYWORDS, zlib_compressobj__doc__},
111
112static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400113zlib_compressobj_impl(PyModuleDef *module, int level, int method, int wbits,
114 int memLevel, int strategy, Py_buffer *zdict);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200115
116static PyObject *
117zlib_compressobj(PyModuleDef *module, PyObject *args, PyObject *kwargs)
118{
119 PyObject *return_value = NULL;
120 static char *_keywords[] = {"level", "method", "wbits", "memLevel", "strategy", "zdict", NULL};
121 int level = Z_DEFAULT_COMPRESSION;
122 int method = DEFLATED;
123 int wbits = MAX_WBITS;
124 int memLevel = DEF_MEM_LEVEL;
125 int strategy = Z_DEFAULT_STRATEGY;
126 Py_buffer zdict = {NULL, NULL};
127
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300128 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iiiiiy*:compressobj", _keywords,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200129 &level, &method, &wbits, &memLevel, &strategy, &zdict))
130 goto exit;
131 return_value = zlib_compressobj_impl(module, level, method, wbits, memLevel, strategy, &zdict);
132
133exit:
134 /* Cleanup for zdict */
135 if (zdict.obj)
136 PyBuffer_Release(&zdict);
137
138 return return_value;
139}
140
141PyDoc_STRVAR(zlib_decompressobj__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800142"decompressobj($module, /, wbits=MAX_WBITS, zdict=b\'\')\n"
143"--\n"
144"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200145"Return a decompressor object.\n"
146"\n"
147" wbits\n"
148" The window buffer size.\n"
149" zdict\n"
150" The predefined compression dictionary. This must be the same\n"
151" dictionary as used by the compressor that produced the input data.");
152
153#define ZLIB_DECOMPRESSOBJ_METHODDEF \
154 {"decompressobj", (PyCFunction)zlib_decompressobj, METH_VARARGS|METH_KEYWORDS, zlib_decompressobj__doc__},
155
156static PyObject *
157zlib_decompressobj_impl(PyModuleDef *module, int wbits, PyObject *zdict);
158
159static PyObject *
160zlib_decompressobj(PyModuleDef *module, PyObject *args, PyObject *kwargs)
161{
162 PyObject *return_value = NULL;
163 static char *_keywords[] = {"wbits", "zdict", NULL};
164 int wbits = MAX_WBITS;
165 PyObject *zdict = NULL;
166
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300167 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|iO:decompressobj", _keywords,
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200168 &wbits, &zdict))
169 goto exit;
170 return_value = zlib_decompressobj_impl(module, wbits, zdict);
171
172exit:
173 return return_value;
174}
175
176PyDoc_STRVAR(zlib_Compress_compress__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800177"compress($self, data, /)\n"
178"--\n"
179"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200180"Returns a bytes object containing compressed data.\n"
181"\n"
182" data\n"
183" Binary data to be compressed.\n"
184"\n"
185"After calling this function, some of the input data may still\n"
186"be stored in internal buffers for later processing.\n"
187"Call the flush() method to clear these buffers.");
188
189#define ZLIB_COMPRESS_COMPRESS_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300190 {"compress", (PyCFunction)zlib_Compress_compress, METH_O, zlib_Compress_compress__doc__},
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200191
192static PyObject *
193zlib_Compress_compress_impl(compobject *self, Py_buffer *data);
194
195static PyObject *
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300196zlib_Compress_compress(compobject *self, PyObject *arg)
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200197{
198 PyObject *return_value = NULL;
199 Py_buffer data = {NULL, NULL};
200
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300201 if (!PyArg_Parse(arg, "y*:compress", &data))
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200202 goto exit;
203 return_value = zlib_Compress_compress_impl(self, &data);
204
205exit:
206 /* Cleanup for data */
207 if (data.obj)
208 PyBuffer_Release(&data);
209
210 return return_value;
211}
212
213PyDoc_STRVAR(zlib_Decompress_decompress__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800214"decompress($self, data, max_length=0, /)\n"
215"--\n"
216"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200217"Return a bytes object containing the decompressed version of the data.\n"
218"\n"
219" data\n"
220" The binary data to decompress.\n"
221" max_length\n"
222" The maximum allowable length of the decompressed data.\n"
223" Unconsumed input data will be stored in\n"
224" the unconsumed_tail attribute.\n"
225"\n"
226"After calling this function, some of the input data may still be stored in\n"
227"internal buffers for later processing.\n"
228"Call the flush() method to clear these buffers.");
229
230#define ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF \
231 {"decompress", (PyCFunction)zlib_Decompress_decompress, METH_VARARGS, zlib_Decompress_decompress__doc__},
232
233static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400234zlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
235 unsigned int max_length);
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200236
237static PyObject *
238zlib_Decompress_decompress(compobject *self, PyObject *args)
239{
240 PyObject *return_value = NULL;
241 Py_buffer data = {NULL, NULL};
242 unsigned int max_length = 0;
243
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300244 if (!PyArg_ParseTuple(args, "y*|O&:decompress",
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200245 &data, uint_converter, &max_length))
246 goto exit;
247 return_value = zlib_Decompress_decompress_impl(self, &data, max_length);
248
249exit:
250 /* Cleanup for data */
251 if (data.obj)
252 PyBuffer_Release(&data);
253
254 return return_value;
255}
256
257PyDoc_STRVAR(zlib_Compress_flush__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800258"flush($self, mode=zlib.Z_FINISH, /)\n"
259"--\n"
260"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200261"Return a bytes object containing any remaining compressed data.\n"
262"\n"
263" mode\n"
264" One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.\n"
265" If mode == Z_FINISH, the compressor object can no longer be\n"
266" used after calling the flush() method. Otherwise, more data\n"
267" can still be compressed.");
268
269#define ZLIB_COMPRESS_FLUSH_METHODDEF \
270 {"flush", (PyCFunction)zlib_Compress_flush, METH_VARARGS, zlib_Compress_flush__doc__},
271
272static PyObject *
273zlib_Compress_flush_impl(compobject *self, int mode);
274
275static PyObject *
276zlib_Compress_flush(compobject *self, PyObject *args)
277{
278 PyObject *return_value = NULL;
279 int mode = Z_FINISH;
280
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300281 if (!PyArg_ParseTuple(args, "|i:flush",
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200282 &mode))
283 goto exit;
284 return_value = zlib_Compress_flush_impl(self, mode);
285
286exit:
287 return return_value;
288}
289
Larry Hastings7726ac92014-01-31 22:03:12 -0800290#if defined(HAVE_ZLIB_COPY)
291
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200292PyDoc_STRVAR(zlib_Compress_copy__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800293"copy($self, /)\n"
294"--\n"
295"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200296"Return a copy of the compression object.");
297
298#define ZLIB_COMPRESS_COPY_METHODDEF \
299 {"copy", (PyCFunction)zlib_Compress_copy, METH_NOARGS, zlib_Compress_copy__doc__},
300
301static PyObject *
302zlib_Compress_copy_impl(compobject *self);
303
304static PyObject *
305zlib_Compress_copy(compobject *self, PyObject *Py_UNUSED(ignored))
306{
307 return zlib_Compress_copy_impl(self);
308}
309
Larry Hastings7726ac92014-01-31 22:03:12 -0800310#endif /* defined(HAVE_ZLIB_COPY) */
311
Larry Hastings7726ac92014-01-31 22:03:12 -0800312#if defined(HAVE_ZLIB_COPY)
313
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200314PyDoc_STRVAR(zlib_Decompress_copy__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800315"copy($self, /)\n"
316"--\n"
317"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200318"Return a copy of the decompression object.");
319
320#define ZLIB_DECOMPRESS_COPY_METHODDEF \
321 {"copy", (PyCFunction)zlib_Decompress_copy, METH_NOARGS, zlib_Decompress_copy__doc__},
322
323static PyObject *
324zlib_Decompress_copy_impl(compobject *self);
325
326static PyObject *
327zlib_Decompress_copy(compobject *self, PyObject *Py_UNUSED(ignored))
328{
329 return zlib_Decompress_copy_impl(self);
330}
331
Larry Hastings7726ac92014-01-31 22:03:12 -0800332#endif /* defined(HAVE_ZLIB_COPY) */
333
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200334PyDoc_STRVAR(zlib_Decompress_flush__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800335"flush($self, length=zlib.DEF_BUF_SIZE, /)\n"
336"--\n"
337"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200338"Return a bytes object containing any remaining decompressed data.\n"
339"\n"
340" length\n"
341" the initial size of the output buffer.");
342
343#define ZLIB_DECOMPRESS_FLUSH_METHODDEF \
344 {"flush", (PyCFunction)zlib_Decompress_flush, METH_VARARGS, zlib_Decompress_flush__doc__},
345
346static PyObject *
347zlib_Decompress_flush_impl(compobject *self, unsigned int length);
348
349static PyObject *
350zlib_Decompress_flush(compobject *self, PyObject *args)
351{
352 PyObject *return_value = NULL;
353 unsigned int length = DEF_BUF_SIZE;
354
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300355 if (!PyArg_ParseTuple(args, "|O&:flush",
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200356 uint_converter, &length))
357 goto exit;
358 return_value = zlib_Decompress_flush_impl(self, length);
359
360exit:
361 return return_value;
362}
363
364PyDoc_STRVAR(zlib_adler32__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800365"adler32($module, data, value=1, /)\n"
366"--\n"
367"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200368"Compute an Adler-32 checksum of data.\n"
369"\n"
370" value\n"
371" Starting value of the checksum.\n"
372"\n"
373"The returned checksum is an integer.");
374
375#define ZLIB_ADLER32_METHODDEF \
376 {"adler32", (PyCFunction)zlib_adler32, METH_VARARGS, zlib_adler32__doc__},
377
378static PyObject *
379zlib_adler32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value);
380
381static PyObject *
382zlib_adler32(PyModuleDef *module, PyObject *args)
383{
384 PyObject *return_value = NULL;
385 Py_buffer data = {NULL, NULL};
386 unsigned int value = 1;
387
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300388 if (!PyArg_ParseTuple(args, "y*|I:adler32",
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200389 &data, &value))
390 goto exit;
391 return_value = zlib_adler32_impl(module, &data, value);
392
393exit:
394 /* Cleanup for data */
395 if (data.obj)
396 PyBuffer_Release(&data);
397
398 return return_value;
399}
400
401PyDoc_STRVAR(zlib_crc32__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800402"crc32($module, data, value=0, /)\n"
403"--\n"
404"\n"
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200405"Compute a CRC-32 checksum of data.\n"
406"\n"
407" value\n"
408" Starting value of the checksum.\n"
409"\n"
410"The returned checksum is an integer.");
411
412#define ZLIB_CRC32_METHODDEF \
413 {"crc32", (PyCFunction)zlib_crc32, METH_VARARGS, zlib_crc32__doc__},
414
415static PyObject *
416zlib_crc32_impl(PyModuleDef *module, Py_buffer *data, unsigned int value);
417
418static PyObject *
419zlib_crc32(PyModuleDef *module, PyObject *args)
420{
421 PyObject *return_value = NULL;
422 Py_buffer data = {NULL, NULL};
423 unsigned int value = 0;
424
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300425 if (!PyArg_ParseTuple(args, "y*|I:crc32",
Serhiy Storchaka2c5ddbe2014-01-27 00:03:31 +0200426 &data, &value))
427 goto exit;
428 return_value = zlib_crc32_impl(module, &data, value);
429
430exit:
431 /* Cleanup for data */
432 if (data.obj)
433 PyBuffer_Release(&data);
434
435 return return_value;
436}
Larry Hastings0759f842015-04-03 13:09:02 -0700437
438#ifndef ZLIB_COMPRESS_COPY_METHODDEF
439 #define ZLIB_COMPRESS_COPY_METHODDEF
440#endif /* !defined(ZLIB_COMPRESS_COPY_METHODDEF) */
Serhiy Storchaka247789c2015-04-24 00:40:51 +0300441/*[clinic end generated code: output=56ed1147bbbb4788 input=a9049054013a1b77]*/