1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4
5 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6 # include "pycore_gc.h" // PyGC_Head
7 # include "pycore_runtime.h" // _Py_ID()
8 #endif
9 #include "pycore_abstract.h" // _PyNumber_Index()
10 #include "pycore_modsupport.h" // _PyArg_UnpackKeywords()
11
12 PyDoc_STRVAR(zlib_compress__doc__,
13 "compress($module, data, /, level=Z_DEFAULT_COMPRESSION, wbits=MAX_WBITS)\n"
14 "--\n"
15 "\n"
16 "Returns a bytes object containing compressed data.\n"
17 "\n"
18 " data\n"
19 " Binary data to be compressed.\n"
20 " level\n"
21 " Compression level, in 0-9 or -1.\n"
22 " wbits\n"
23 " The window buffer size and container format.");
24
25 #define ZLIB_COMPRESS_METHODDEF \
26 {"compress", _PyCFunction_CAST(zlib_compress), METH_FASTCALL|METH_KEYWORDS, zlib_compress__doc__},
27
28 static PyObject *
29 zlib_compress_impl(PyObject *module, Py_buffer *data, int level, int wbits);
30
31 static PyObject *
zlib_compress(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)32 zlib_compress(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
33 {
34 PyObject *return_value = NULL;
35 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
36
37 #define NUM_KEYWORDS 2
38 static struct {
39 PyGC_Head _this_is_not_used;
40 PyObject_VAR_HEAD
41 PyObject *ob_item[NUM_KEYWORDS];
42 } _kwtuple = {
43 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
44 .ob_item = { &_Py_ID(level), &_Py_ID(wbits), },
45 };
46 #undef NUM_KEYWORDS
47 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
48
49 #else // !Py_BUILD_CORE
50 # define KWTUPLE NULL
51 #endif // !Py_BUILD_CORE
52
53 static const char * const _keywords[] = {"", "level", "wbits", NULL};
54 static _PyArg_Parser _parser = {
55 .keywords = _keywords,
56 .fname = "compress",
57 .kwtuple = KWTUPLE,
58 };
59 #undef KWTUPLE
60 PyObject *argsbuf[3];
61 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
62 Py_buffer data = {NULL, NULL};
63 int level = Z_DEFAULT_COMPRESSION;
64 int wbits = MAX_WBITS;
65
66 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
67 if (!args) {
68 goto exit;
69 }
70 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
71 goto exit;
72 }
73 if (!noptargs) {
74 goto skip_optional_pos;
75 }
76 if (args[1]) {
77 level = PyLong_AsInt(args[1]);
78 if (level == -1 && PyErr_Occurred()) {
79 goto exit;
80 }
81 if (!--noptargs) {
82 goto skip_optional_pos;
83 }
84 }
85 wbits = PyLong_AsInt(args[2]);
86 if (wbits == -1 && PyErr_Occurred()) {
87 goto exit;
88 }
89 skip_optional_pos:
90 return_value = zlib_compress_impl(module, &data, level, wbits);
91
92 exit:
93 /* Cleanup for data */
94 if (data.obj) {
95 PyBuffer_Release(&data);
96 }
97
98 return return_value;
99 }
100
101 PyDoc_STRVAR(zlib_decompress__doc__,
102 "decompress($module, data, /, wbits=MAX_WBITS, bufsize=DEF_BUF_SIZE)\n"
103 "--\n"
104 "\n"
105 "Returns a bytes object containing the uncompressed data.\n"
106 "\n"
107 " data\n"
108 " Compressed data.\n"
109 " wbits\n"
110 " The window buffer size and container format.\n"
111 " bufsize\n"
112 " The initial output buffer size.");
113
114 #define ZLIB_DECOMPRESS_METHODDEF \
115 {"decompress", _PyCFunction_CAST(zlib_decompress), METH_FASTCALL|METH_KEYWORDS, zlib_decompress__doc__},
116
117 static PyObject *
118 zlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
119 Py_ssize_t bufsize);
120
121 static PyObject *
zlib_decompress(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)122 zlib_decompress(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
123 {
124 PyObject *return_value = NULL;
125 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
126
127 #define NUM_KEYWORDS 2
128 static struct {
129 PyGC_Head _this_is_not_used;
130 PyObject_VAR_HEAD
131 PyObject *ob_item[NUM_KEYWORDS];
132 } _kwtuple = {
133 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
134 .ob_item = { &_Py_ID(wbits), &_Py_ID(bufsize), },
135 };
136 #undef NUM_KEYWORDS
137 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
138
139 #else // !Py_BUILD_CORE
140 # define KWTUPLE NULL
141 #endif // !Py_BUILD_CORE
142
143 static const char * const _keywords[] = {"", "wbits", "bufsize", NULL};
144 static _PyArg_Parser _parser = {
145 .keywords = _keywords,
146 .fname = "decompress",
147 .kwtuple = KWTUPLE,
148 };
149 #undef KWTUPLE
150 PyObject *argsbuf[3];
151 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
152 Py_buffer data = {NULL, NULL};
153 int wbits = MAX_WBITS;
154 Py_ssize_t bufsize = DEF_BUF_SIZE;
155
156 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
157 if (!args) {
158 goto exit;
159 }
160 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
161 goto exit;
162 }
163 if (!noptargs) {
164 goto skip_optional_pos;
165 }
166 if (args[1]) {
167 wbits = PyLong_AsInt(args[1]);
168 if (wbits == -1 && PyErr_Occurred()) {
169 goto exit;
170 }
171 if (!--noptargs) {
172 goto skip_optional_pos;
173 }
174 }
175 {
176 Py_ssize_t ival = -1;
177 PyObject *iobj = _PyNumber_Index(args[2]);
178 if (iobj != NULL) {
179 ival = PyLong_AsSsize_t(iobj);
180 Py_DECREF(iobj);
181 }
182 if (ival == -1 && PyErr_Occurred()) {
183 goto exit;
184 }
185 bufsize = ival;
186 }
187 skip_optional_pos:
188 return_value = zlib_decompress_impl(module, &data, wbits, bufsize);
189
190 exit:
191 /* Cleanup for data */
192 if (data.obj) {
193 PyBuffer_Release(&data);
194 }
195
196 return return_value;
197 }
198
199 PyDoc_STRVAR(zlib_compressobj__doc__,
200 "compressobj($module, /, level=Z_DEFAULT_COMPRESSION, method=DEFLATED,\n"
201 " wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL,\n"
202 " strategy=Z_DEFAULT_STRATEGY, zdict=None)\n"
203 "--\n"
204 "\n"
205 "Return a compressor object.\n"
206 "\n"
207 " level\n"
208 " The compression level (an integer in the range 0-9 or -1; default is\n"
209 " currently equivalent to 6). Higher compression levels are slower,\n"
210 " but produce smaller results.\n"
211 " method\n"
212 " The compression algorithm. If given, this must be DEFLATED.\n"
213 " wbits\n"
214 " +9 to +15: The base-two logarithm of the window size. Include a zlib\n"
215 " container.\n"
216 " -9 to -15: Generate a raw stream.\n"
217 " +25 to +31: Include a gzip container.\n"
218 " memLevel\n"
219 " Controls the amount of memory used for internal compression state.\n"
220 " Valid values range from 1 to 9. Higher values result in higher memory\n"
221 " usage, faster compression, and smaller output.\n"
222 " strategy\n"
223 " Used to tune the compression algorithm. Possible values are\n"
224 " Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.\n"
225 " zdict\n"
226 " The predefined compression dictionary - a sequence of bytes\n"
227 " containing subsequences that are likely to occur in the input data.");
228
229 #define ZLIB_COMPRESSOBJ_METHODDEF \
230 {"compressobj", _PyCFunction_CAST(zlib_compressobj), METH_FASTCALL|METH_KEYWORDS, zlib_compressobj__doc__},
231
232 static PyObject *
233 zlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
234 int memLevel, int strategy, Py_buffer *zdict);
235
236 static PyObject *
zlib_compressobj(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)237 zlib_compressobj(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
238 {
239 PyObject *return_value = NULL;
240 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
241
242 #define NUM_KEYWORDS 6
243 static struct {
244 PyGC_Head _this_is_not_used;
245 PyObject_VAR_HEAD
246 PyObject *ob_item[NUM_KEYWORDS];
247 } _kwtuple = {
248 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
249 .ob_item = { &_Py_ID(level), &_Py_ID(method), &_Py_ID(wbits), &_Py_ID(memLevel), &_Py_ID(strategy), &_Py_ID(zdict), },
250 };
251 #undef NUM_KEYWORDS
252 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
253
254 #else // !Py_BUILD_CORE
255 # define KWTUPLE NULL
256 #endif // !Py_BUILD_CORE
257
258 static const char * const _keywords[] = {"level", "method", "wbits", "memLevel", "strategy", "zdict", NULL};
259 static _PyArg_Parser _parser = {
260 .keywords = _keywords,
261 .fname = "compressobj",
262 .kwtuple = KWTUPLE,
263 };
264 #undef KWTUPLE
265 PyObject *argsbuf[6];
266 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
267 int level = Z_DEFAULT_COMPRESSION;
268 int method = DEFLATED;
269 int wbits = MAX_WBITS;
270 int memLevel = DEF_MEM_LEVEL;
271 int strategy = Z_DEFAULT_STRATEGY;
272 Py_buffer zdict = {NULL, NULL};
273
274 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 6, 0, argsbuf);
275 if (!args) {
276 goto exit;
277 }
278 if (!noptargs) {
279 goto skip_optional_pos;
280 }
281 if (args[0]) {
282 level = PyLong_AsInt(args[0]);
283 if (level == -1 && PyErr_Occurred()) {
284 goto exit;
285 }
286 if (!--noptargs) {
287 goto skip_optional_pos;
288 }
289 }
290 if (args[1]) {
291 method = PyLong_AsInt(args[1]);
292 if (method == -1 && PyErr_Occurred()) {
293 goto exit;
294 }
295 if (!--noptargs) {
296 goto skip_optional_pos;
297 }
298 }
299 if (args[2]) {
300 wbits = PyLong_AsInt(args[2]);
301 if (wbits == -1 && PyErr_Occurred()) {
302 goto exit;
303 }
304 if (!--noptargs) {
305 goto skip_optional_pos;
306 }
307 }
308 if (args[3]) {
309 memLevel = PyLong_AsInt(args[3]);
310 if (memLevel == -1 && PyErr_Occurred()) {
311 goto exit;
312 }
313 if (!--noptargs) {
314 goto skip_optional_pos;
315 }
316 }
317 if (args[4]) {
318 strategy = PyLong_AsInt(args[4]);
319 if (strategy == -1 && PyErr_Occurred()) {
320 goto exit;
321 }
322 if (!--noptargs) {
323 goto skip_optional_pos;
324 }
325 }
326 if (PyObject_GetBuffer(args[5], &zdict, PyBUF_SIMPLE) != 0) {
327 goto exit;
328 }
329 skip_optional_pos:
330 return_value = zlib_compressobj_impl(module, level, method, wbits, memLevel, strategy, &zdict);
331
332 exit:
333 /* Cleanup for zdict */
334 if (zdict.obj) {
335 PyBuffer_Release(&zdict);
336 }
337
338 return return_value;
339 }
340
341 PyDoc_STRVAR(zlib_decompressobj__doc__,
342 "decompressobj($module, /, wbits=MAX_WBITS, zdict=b\'\')\n"
343 "--\n"
344 "\n"
345 "Return a decompressor object.\n"
346 "\n"
347 " wbits\n"
348 " The window buffer size and container format.\n"
349 " zdict\n"
350 " The predefined compression dictionary. This must be the same\n"
351 " dictionary as used by the compressor that produced the input data.");
352
353 #define ZLIB_DECOMPRESSOBJ_METHODDEF \
354 {"decompressobj", _PyCFunction_CAST(zlib_decompressobj), METH_FASTCALL|METH_KEYWORDS, zlib_decompressobj__doc__},
355
356 static PyObject *
357 zlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict);
358
359 static PyObject *
zlib_decompressobj(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)360 zlib_decompressobj(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
361 {
362 PyObject *return_value = NULL;
363 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
364
365 #define NUM_KEYWORDS 2
366 static struct {
367 PyGC_Head _this_is_not_used;
368 PyObject_VAR_HEAD
369 PyObject *ob_item[NUM_KEYWORDS];
370 } _kwtuple = {
371 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
372 .ob_item = { &_Py_ID(wbits), &_Py_ID(zdict), },
373 };
374 #undef NUM_KEYWORDS
375 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
376
377 #else // !Py_BUILD_CORE
378 # define KWTUPLE NULL
379 #endif // !Py_BUILD_CORE
380
381 static const char * const _keywords[] = {"wbits", "zdict", NULL};
382 static _PyArg_Parser _parser = {
383 .keywords = _keywords,
384 .fname = "decompressobj",
385 .kwtuple = KWTUPLE,
386 };
387 #undef KWTUPLE
388 PyObject *argsbuf[2];
389 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0;
390 int wbits = MAX_WBITS;
391 PyObject *zdict = NULL;
392
393 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf);
394 if (!args) {
395 goto exit;
396 }
397 if (!noptargs) {
398 goto skip_optional_pos;
399 }
400 if (args[0]) {
401 wbits = PyLong_AsInt(args[0]);
402 if (wbits == -1 && PyErr_Occurred()) {
403 goto exit;
404 }
405 if (!--noptargs) {
406 goto skip_optional_pos;
407 }
408 }
409 zdict = args[1];
410 skip_optional_pos:
411 return_value = zlib_decompressobj_impl(module, wbits, zdict);
412
413 exit:
414 return return_value;
415 }
416
417 PyDoc_STRVAR(zlib_Compress_compress__doc__,
418 "compress($self, data, /)\n"
419 "--\n"
420 "\n"
421 "Returns a bytes object containing compressed data.\n"
422 "\n"
423 " data\n"
424 " Binary data to be compressed.\n"
425 "\n"
426 "After calling this function, some of the input data may still\n"
427 "be stored in internal buffers for later processing.\n"
428 "Call the flush() method to clear these buffers.");
429
430 #define ZLIB_COMPRESS_COMPRESS_METHODDEF \
431 {"compress", _PyCFunction_CAST(zlib_Compress_compress), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, zlib_Compress_compress__doc__},
432
433 static PyObject *
434 zlib_Compress_compress_impl(compobject *self, PyTypeObject *cls,
435 Py_buffer *data);
436
437 static PyObject *
zlib_Compress_compress(compobject * self,PyTypeObject * cls,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)438 zlib_Compress_compress(compobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
439 {
440 PyObject *return_value = NULL;
441 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
442 # define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
443 #else
444 # define KWTUPLE NULL
445 #endif
446
447 static const char * const _keywords[] = {"", NULL};
448 static _PyArg_Parser _parser = {
449 .keywords = _keywords,
450 .fname = "compress",
451 .kwtuple = KWTUPLE,
452 };
453 #undef KWTUPLE
454 PyObject *argsbuf[1];
455 Py_buffer data = {NULL, NULL};
456
457 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
458 if (!args) {
459 goto exit;
460 }
461 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
462 goto exit;
463 }
464 return_value = zlib_Compress_compress_impl(self, cls, &data);
465
466 exit:
467 /* Cleanup for data */
468 if (data.obj) {
469 PyBuffer_Release(&data);
470 }
471
472 return return_value;
473 }
474
475 PyDoc_STRVAR(zlib_Decompress_decompress__doc__,
476 "decompress($self, data, /, max_length=0)\n"
477 "--\n"
478 "\n"
479 "Return a bytes object containing the decompressed version of the data.\n"
480 "\n"
481 " data\n"
482 " The binary data to decompress.\n"
483 " max_length\n"
484 " The maximum allowable length of the decompressed data.\n"
485 " Unconsumed input data will be stored in\n"
486 " the unconsumed_tail attribute.\n"
487 "\n"
488 "After calling this function, some of the input data may still be stored in\n"
489 "internal buffers for later processing.\n"
490 "Call the flush() method to clear these buffers.");
491
492 #define ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF \
493 {"decompress", _PyCFunction_CAST(zlib_Decompress_decompress), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, zlib_Decompress_decompress__doc__},
494
495 static PyObject *
496 zlib_Decompress_decompress_impl(compobject *self, PyTypeObject *cls,
497 Py_buffer *data, Py_ssize_t max_length);
498
499 static PyObject *
zlib_Decompress_decompress(compobject * self,PyTypeObject * cls,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)500 zlib_Decompress_decompress(compobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
501 {
502 PyObject *return_value = NULL;
503 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
504
505 #define NUM_KEYWORDS 1
506 static struct {
507 PyGC_Head _this_is_not_used;
508 PyObject_VAR_HEAD
509 PyObject *ob_item[NUM_KEYWORDS];
510 } _kwtuple = {
511 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
512 .ob_item = { &_Py_ID(max_length), },
513 };
514 #undef NUM_KEYWORDS
515 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
516
517 #else // !Py_BUILD_CORE
518 # define KWTUPLE NULL
519 #endif // !Py_BUILD_CORE
520
521 static const char * const _keywords[] = {"", "max_length", NULL};
522 static _PyArg_Parser _parser = {
523 .keywords = _keywords,
524 .fname = "decompress",
525 .kwtuple = KWTUPLE,
526 };
527 #undef KWTUPLE
528 PyObject *argsbuf[2];
529 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
530 Py_buffer data = {NULL, NULL};
531 Py_ssize_t max_length = 0;
532
533 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
534 if (!args) {
535 goto exit;
536 }
537 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
538 goto exit;
539 }
540 if (!noptargs) {
541 goto skip_optional_pos;
542 }
543 {
544 Py_ssize_t ival = -1;
545 PyObject *iobj = _PyNumber_Index(args[1]);
546 if (iobj != NULL) {
547 ival = PyLong_AsSsize_t(iobj);
548 Py_DECREF(iobj);
549 }
550 if (ival == -1 && PyErr_Occurred()) {
551 goto exit;
552 }
553 max_length = ival;
554 }
555 skip_optional_pos:
556 return_value = zlib_Decompress_decompress_impl(self, cls, &data, max_length);
557
558 exit:
559 /* Cleanup for data */
560 if (data.obj) {
561 PyBuffer_Release(&data);
562 }
563
564 return return_value;
565 }
566
567 PyDoc_STRVAR(zlib_Compress_flush__doc__,
568 "flush($self, mode=zlib.Z_FINISH, /)\n"
569 "--\n"
570 "\n"
571 "Return a bytes object containing any remaining compressed data.\n"
572 "\n"
573 " mode\n"
574 " One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.\n"
575 " If mode == Z_FINISH, the compressor object can no longer be\n"
576 " used after calling the flush() method. Otherwise, more data\n"
577 " can still be compressed.");
578
579 #define ZLIB_COMPRESS_FLUSH_METHODDEF \
580 {"flush", _PyCFunction_CAST(zlib_Compress_flush), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, zlib_Compress_flush__doc__},
581
582 static PyObject *
583 zlib_Compress_flush_impl(compobject *self, PyTypeObject *cls, int mode);
584
585 static PyObject *
zlib_Compress_flush(compobject * self,PyTypeObject * cls,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)586 zlib_Compress_flush(compobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
587 {
588 PyObject *return_value = NULL;
589 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
590 # define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
591 #else
592 # define KWTUPLE NULL
593 #endif
594
595 static const char * const _keywords[] = {"", NULL};
596 static _PyArg_Parser _parser = {
597 .keywords = _keywords,
598 .fname = "flush",
599 .kwtuple = KWTUPLE,
600 };
601 #undef KWTUPLE
602 PyObject *argsbuf[1];
603 int mode = Z_FINISH;
604
605 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
606 if (!args) {
607 goto exit;
608 }
609 if (nargs < 1) {
610 goto skip_optional_posonly;
611 }
612 mode = PyLong_AsInt(args[0]);
613 if (mode == -1 && PyErr_Occurred()) {
614 goto exit;
615 }
616 skip_optional_posonly:
617 return_value = zlib_Compress_flush_impl(self, cls, mode);
618
619 exit:
620 return return_value;
621 }
622
623 #if defined(HAVE_ZLIB_COPY)
624
625 PyDoc_STRVAR(zlib_Compress_copy__doc__,
626 "copy($self, /)\n"
627 "--\n"
628 "\n"
629 "Return a copy of the compression object.");
630
631 #define ZLIB_COMPRESS_COPY_METHODDEF \
632 {"copy", _PyCFunction_CAST(zlib_Compress_copy), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, zlib_Compress_copy__doc__},
633
634 static PyObject *
635 zlib_Compress_copy_impl(compobject *self, PyTypeObject *cls);
636
637 static PyObject *
zlib_Compress_copy(compobject * self,PyTypeObject * cls,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)638 zlib_Compress_copy(compobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
639 {
640 if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
641 PyErr_SetString(PyExc_TypeError, "copy() takes no arguments");
642 return NULL;
643 }
644 return zlib_Compress_copy_impl(self, cls);
645 }
646
647 #endif /* defined(HAVE_ZLIB_COPY) */
648
649 #if defined(HAVE_ZLIB_COPY)
650
651 PyDoc_STRVAR(zlib_Compress___copy____doc__,
652 "__copy__($self, /)\n"
653 "--\n"
654 "\n");
655
656 #define ZLIB_COMPRESS___COPY___METHODDEF \
657 {"__copy__", _PyCFunction_CAST(zlib_Compress___copy__), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, zlib_Compress___copy____doc__},
658
659 static PyObject *
660 zlib_Compress___copy___impl(compobject *self, PyTypeObject *cls);
661
662 static PyObject *
zlib_Compress___copy__(compobject * self,PyTypeObject * cls,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)663 zlib_Compress___copy__(compobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
664 {
665 if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
666 PyErr_SetString(PyExc_TypeError, "__copy__() takes no arguments");
667 return NULL;
668 }
669 return zlib_Compress___copy___impl(self, cls);
670 }
671
672 #endif /* defined(HAVE_ZLIB_COPY) */
673
674 #if defined(HAVE_ZLIB_COPY)
675
676 PyDoc_STRVAR(zlib_Compress___deepcopy____doc__,
677 "__deepcopy__($self, memo, /)\n"
678 "--\n"
679 "\n");
680
681 #define ZLIB_COMPRESS___DEEPCOPY___METHODDEF \
682 {"__deepcopy__", _PyCFunction_CAST(zlib_Compress___deepcopy__), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, zlib_Compress___deepcopy____doc__},
683
684 static PyObject *
685 zlib_Compress___deepcopy___impl(compobject *self, PyTypeObject *cls,
686 PyObject *memo);
687
688 static PyObject *
zlib_Compress___deepcopy__(compobject * self,PyTypeObject * cls,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)689 zlib_Compress___deepcopy__(compobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
690 {
691 PyObject *return_value = NULL;
692 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
693 # define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
694 #else
695 # define KWTUPLE NULL
696 #endif
697
698 static const char * const _keywords[] = {"", NULL};
699 static _PyArg_Parser _parser = {
700 .keywords = _keywords,
701 .fname = "__deepcopy__",
702 .kwtuple = KWTUPLE,
703 };
704 #undef KWTUPLE
705 PyObject *argsbuf[1];
706 PyObject *memo;
707
708 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
709 if (!args) {
710 goto exit;
711 }
712 memo = args[0];
713 return_value = zlib_Compress___deepcopy___impl(self, cls, memo);
714
715 exit:
716 return return_value;
717 }
718
719 #endif /* defined(HAVE_ZLIB_COPY) */
720
721 #if defined(HAVE_ZLIB_COPY)
722
723 PyDoc_STRVAR(zlib_Decompress_copy__doc__,
724 "copy($self, /)\n"
725 "--\n"
726 "\n"
727 "Return a copy of the decompression object.");
728
729 #define ZLIB_DECOMPRESS_COPY_METHODDEF \
730 {"copy", _PyCFunction_CAST(zlib_Decompress_copy), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, zlib_Decompress_copy__doc__},
731
732 static PyObject *
733 zlib_Decompress_copy_impl(compobject *self, PyTypeObject *cls);
734
735 static PyObject *
zlib_Decompress_copy(compobject * self,PyTypeObject * cls,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)736 zlib_Decompress_copy(compobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
737 {
738 if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
739 PyErr_SetString(PyExc_TypeError, "copy() takes no arguments");
740 return NULL;
741 }
742 return zlib_Decompress_copy_impl(self, cls);
743 }
744
745 #endif /* defined(HAVE_ZLIB_COPY) */
746
747 #if defined(HAVE_ZLIB_COPY)
748
749 PyDoc_STRVAR(zlib_Decompress___copy____doc__,
750 "__copy__($self, /)\n"
751 "--\n"
752 "\n");
753
754 #define ZLIB_DECOMPRESS___COPY___METHODDEF \
755 {"__copy__", _PyCFunction_CAST(zlib_Decompress___copy__), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, zlib_Decompress___copy____doc__},
756
757 static PyObject *
758 zlib_Decompress___copy___impl(compobject *self, PyTypeObject *cls);
759
760 static PyObject *
zlib_Decompress___copy__(compobject * self,PyTypeObject * cls,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)761 zlib_Decompress___copy__(compobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
762 {
763 if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
764 PyErr_SetString(PyExc_TypeError, "__copy__() takes no arguments");
765 return NULL;
766 }
767 return zlib_Decompress___copy___impl(self, cls);
768 }
769
770 #endif /* defined(HAVE_ZLIB_COPY) */
771
772 #if defined(HAVE_ZLIB_COPY)
773
774 PyDoc_STRVAR(zlib_Decompress___deepcopy____doc__,
775 "__deepcopy__($self, memo, /)\n"
776 "--\n"
777 "\n");
778
779 #define ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF \
780 {"__deepcopy__", _PyCFunction_CAST(zlib_Decompress___deepcopy__), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, zlib_Decompress___deepcopy____doc__},
781
782 static PyObject *
783 zlib_Decompress___deepcopy___impl(compobject *self, PyTypeObject *cls,
784 PyObject *memo);
785
786 static PyObject *
zlib_Decompress___deepcopy__(compobject * self,PyTypeObject * cls,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)787 zlib_Decompress___deepcopy__(compobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
788 {
789 PyObject *return_value = NULL;
790 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
791 # define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
792 #else
793 # define KWTUPLE NULL
794 #endif
795
796 static const char * const _keywords[] = {"", NULL};
797 static _PyArg_Parser _parser = {
798 .keywords = _keywords,
799 .fname = "__deepcopy__",
800 .kwtuple = KWTUPLE,
801 };
802 #undef KWTUPLE
803 PyObject *argsbuf[1];
804 PyObject *memo;
805
806 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
807 if (!args) {
808 goto exit;
809 }
810 memo = args[0];
811 return_value = zlib_Decompress___deepcopy___impl(self, cls, memo);
812
813 exit:
814 return return_value;
815 }
816
817 #endif /* defined(HAVE_ZLIB_COPY) */
818
819 PyDoc_STRVAR(zlib_Decompress_flush__doc__,
820 "flush($self, length=zlib.DEF_BUF_SIZE, /)\n"
821 "--\n"
822 "\n"
823 "Return a bytes object containing any remaining decompressed data.\n"
824 "\n"
825 " length\n"
826 " the initial size of the output buffer.");
827
828 #define ZLIB_DECOMPRESS_FLUSH_METHODDEF \
829 {"flush", _PyCFunction_CAST(zlib_Decompress_flush), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, zlib_Decompress_flush__doc__},
830
831 static PyObject *
832 zlib_Decompress_flush_impl(compobject *self, PyTypeObject *cls,
833 Py_ssize_t length);
834
835 static PyObject *
zlib_Decompress_flush(compobject * self,PyTypeObject * cls,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)836 zlib_Decompress_flush(compobject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
837 {
838 PyObject *return_value = NULL;
839 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
840 # define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
841 #else
842 # define KWTUPLE NULL
843 #endif
844
845 static const char * const _keywords[] = {"", NULL};
846 static _PyArg_Parser _parser = {
847 .keywords = _keywords,
848 .fname = "flush",
849 .kwtuple = KWTUPLE,
850 };
851 #undef KWTUPLE
852 PyObject *argsbuf[1];
853 Py_ssize_t length = DEF_BUF_SIZE;
854
855 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf);
856 if (!args) {
857 goto exit;
858 }
859 if (nargs < 1) {
860 goto skip_optional_posonly;
861 }
862 {
863 Py_ssize_t ival = -1;
864 PyObject *iobj = _PyNumber_Index(args[0]);
865 if (iobj != NULL) {
866 ival = PyLong_AsSsize_t(iobj);
867 Py_DECREF(iobj);
868 }
869 if (ival == -1 && PyErr_Occurred()) {
870 goto exit;
871 }
872 length = ival;
873 }
874 skip_optional_posonly:
875 return_value = zlib_Decompress_flush_impl(self, cls, length);
876
877 exit:
878 return return_value;
879 }
880
881 PyDoc_STRVAR(zlib_ZlibDecompressor_decompress__doc__,
882 "decompress($self, /, data, max_length=-1)\n"
883 "--\n"
884 "\n"
885 "Decompress *data*, returning uncompressed data as bytes.\n"
886 "\n"
887 "If *max_length* is nonnegative, returns at most *max_length* bytes of\n"
888 "decompressed data. If this limit is reached and further output can be\n"
889 "produced, *self.needs_input* will be set to ``False``. In this case, the next\n"
890 "call to *decompress()* may provide *data* as b\'\' to obtain more of the output.\n"
891 "\n"
892 "If all of the input data was decompressed and returned (either because this\n"
893 "was less than *max_length* bytes, or because *max_length* was negative),\n"
894 "*self.needs_input* will be set to True.\n"
895 "\n"
896 "Attempting to decompress data after the end of stream is reached raises an\n"
897 "EOFError. Any data found after the end of the stream is ignored and saved in\n"
898 "the unused_data attribute.");
899
900 #define ZLIB_ZLIBDECOMPRESSOR_DECOMPRESS_METHODDEF \
901 {"decompress", _PyCFunction_CAST(zlib_ZlibDecompressor_decompress), METH_FASTCALL|METH_KEYWORDS, zlib_ZlibDecompressor_decompress__doc__},
902
903 static PyObject *
904 zlib_ZlibDecompressor_decompress_impl(ZlibDecompressor *self,
905 Py_buffer *data, Py_ssize_t max_length);
906
907 static PyObject *
zlib_ZlibDecompressor_decompress(ZlibDecompressor * self,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)908 zlib_ZlibDecompressor_decompress(ZlibDecompressor *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
909 {
910 PyObject *return_value = NULL;
911 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
912
913 #define NUM_KEYWORDS 2
914 static struct {
915 PyGC_Head _this_is_not_used;
916 PyObject_VAR_HEAD
917 PyObject *ob_item[NUM_KEYWORDS];
918 } _kwtuple = {
919 .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
920 .ob_item = { &_Py_ID(data), &_Py_ID(max_length), },
921 };
922 #undef NUM_KEYWORDS
923 #define KWTUPLE (&_kwtuple.ob_base.ob_base)
924
925 #else // !Py_BUILD_CORE
926 # define KWTUPLE NULL
927 #endif // !Py_BUILD_CORE
928
929 static const char * const _keywords[] = {"data", "max_length", NULL};
930 static _PyArg_Parser _parser = {
931 .keywords = _keywords,
932 .fname = "decompress",
933 .kwtuple = KWTUPLE,
934 };
935 #undef KWTUPLE
936 PyObject *argsbuf[2];
937 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
938 Py_buffer data = {NULL, NULL};
939 Py_ssize_t max_length = -1;
940
941 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
942 if (!args) {
943 goto exit;
944 }
945 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
946 goto exit;
947 }
948 if (!noptargs) {
949 goto skip_optional_pos;
950 }
951 {
952 Py_ssize_t ival = -1;
953 PyObject *iobj = _PyNumber_Index(args[1]);
954 if (iobj != NULL) {
955 ival = PyLong_AsSsize_t(iobj);
956 Py_DECREF(iobj);
957 }
958 if (ival == -1 && PyErr_Occurred()) {
959 goto exit;
960 }
961 max_length = ival;
962 }
963 skip_optional_pos:
964 return_value = zlib_ZlibDecompressor_decompress_impl(self, &data, max_length);
965
966 exit:
967 /* Cleanup for data */
968 if (data.obj) {
969 PyBuffer_Release(&data);
970 }
971
972 return return_value;
973 }
974
975 PyDoc_STRVAR(zlib_adler32__doc__,
976 "adler32($module, data, value=1, /)\n"
977 "--\n"
978 "\n"
979 "Compute an Adler-32 checksum of data.\n"
980 "\n"
981 " value\n"
982 " Starting value of the checksum.\n"
983 "\n"
984 "The returned checksum is an integer.");
985
986 #define ZLIB_ADLER32_METHODDEF \
987 {"adler32", _PyCFunction_CAST(zlib_adler32), METH_FASTCALL, zlib_adler32__doc__},
988
989 static PyObject *
990 zlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value);
991
992 static PyObject *
zlib_adler32(PyObject * module,PyObject * const * args,Py_ssize_t nargs)993 zlib_adler32(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
994 {
995 PyObject *return_value = NULL;
996 Py_buffer data = {NULL, NULL};
997 unsigned int value = 1;
998
999 if (!_PyArg_CheckPositional("adler32", nargs, 1, 2)) {
1000 goto exit;
1001 }
1002 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
1003 goto exit;
1004 }
1005 if (nargs < 2) {
1006 goto skip_optional;
1007 }
1008 value = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
1009 if (value == (unsigned int)-1 && PyErr_Occurred()) {
1010 goto exit;
1011 }
1012 skip_optional:
1013 return_value = zlib_adler32_impl(module, &data, value);
1014
1015 exit:
1016 /* Cleanup for data */
1017 if (data.obj) {
1018 PyBuffer_Release(&data);
1019 }
1020
1021 return return_value;
1022 }
1023
1024 PyDoc_STRVAR(zlib_crc32__doc__,
1025 "crc32($module, data, value=0, /)\n"
1026 "--\n"
1027 "\n"
1028 "Compute a CRC-32 checksum of data.\n"
1029 "\n"
1030 " value\n"
1031 " Starting value of the checksum.\n"
1032 "\n"
1033 "The returned checksum is an integer.");
1034
1035 #define ZLIB_CRC32_METHODDEF \
1036 {"crc32", _PyCFunction_CAST(zlib_crc32), METH_FASTCALL, zlib_crc32__doc__},
1037
1038 static unsigned int
1039 zlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value);
1040
1041 static PyObject *
zlib_crc32(PyObject * module,PyObject * const * args,Py_ssize_t nargs)1042 zlib_crc32(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
1043 {
1044 PyObject *return_value = NULL;
1045 Py_buffer data = {NULL, NULL};
1046 unsigned int value = 0;
1047 unsigned int _return_value;
1048
1049 if (!_PyArg_CheckPositional("crc32", nargs, 1, 2)) {
1050 goto exit;
1051 }
1052 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
1053 goto exit;
1054 }
1055 if (nargs < 2) {
1056 goto skip_optional;
1057 }
1058 value = (unsigned int)PyLong_AsUnsignedLongMask(args[1]);
1059 if (value == (unsigned int)-1 && PyErr_Occurred()) {
1060 goto exit;
1061 }
1062 skip_optional:
1063 _return_value = zlib_crc32_impl(module, &data, value);
1064 if ((_return_value == (unsigned int)-1) && PyErr_Occurred()) {
1065 goto exit;
1066 }
1067 return_value = PyLong_FromUnsignedLong((unsigned long)_return_value);
1068
1069 exit:
1070 /* Cleanup for data */
1071 if (data.obj) {
1072 PyBuffer_Release(&data);
1073 }
1074
1075 return return_value;
1076 }
1077
1078 #ifndef ZLIB_COMPRESS_COPY_METHODDEF
1079 #define ZLIB_COMPRESS_COPY_METHODDEF
1080 #endif /* !defined(ZLIB_COMPRESS_COPY_METHODDEF) */
1081
1082 #ifndef ZLIB_COMPRESS___COPY___METHODDEF
1083 #define ZLIB_COMPRESS___COPY___METHODDEF
1084 #endif /* !defined(ZLIB_COMPRESS___COPY___METHODDEF) */
1085
1086 #ifndef ZLIB_COMPRESS___DEEPCOPY___METHODDEF
1087 #define ZLIB_COMPRESS___DEEPCOPY___METHODDEF
1088 #endif /* !defined(ZLIB_COMPRESS___DEEPCOPY___METHODDEF) */
1089
1090 #ifndef ZLIB_DECOMPRESS_COPY_METHODDEF
1091 #define ZLIB_DECOMPRESS_COPY_METHODDEF
1092 #endif /* !defined(ZLIB_DECOMPRESS_COPY_METHODDEF) */
1093
1094 #ifndef ZLIB_DECOMPRESS___COPY___METHODDEF
1095 #define ZLIB_DECOMPRESS___COPY___METHODDEF
1096 #endif /* !defined(ZLIB_DECOMPRESS___COPY___METHODDEF) */
1097
1098 #ifndef ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
1099 #define ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF
1100 #endif /* !defined(ZLIB_DECOMPRESS___DEEPCOPY___METHODDEF) */
1101 /*[clinic end generated code: output=8bb840fb6af43dd4 input=a9049054013a1b77]*/
1102