ยปCore Development>Code coverage>Modules/zlibmodule.c

Python code coverage for Modules/zlibmodule.c

#countcontent
1n/a/* zlibmodule.c -- gzip-compatible data compression */
2n/a/* See http://zlib.net/ */
3n/a
4n/a/* Windows users: read Python's PCbuild\readme.txt */
5n/a
6n/a#define PY_SSIZE_T_CLEAN
7n/a
8n/a#include "Python.h"
9n/a#include "structmember.h"
10n/a#include "zlib.h"
11n/a
12n/a
13n/a#ifdef WITH_THREAD
14n/a #include "pythread.h"
15n/a #define ENTER_ZLIB(obj) \
16n/a Py_BEGIN_ALLOW_THREADS; \
17n/a PyThread_acquire_lock((obj)->lock, 1); \
18n/a Py_END_ALLOW_THREADS;
19n/a #define LEAVE_ZLIB(obj) PyThread_release_lock((obj)->lock);
20n/a#else
21n/a #define ENTER_ZLIB(obj)
22n/a #define LEAVE_ZLIB(obj)
23n/a#endif
24n/a
25n/a#if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221
26n/a# define AT_LEAST_ZLIB_1_2_2_1
27n/a#endif
28n/a
29n/a/* The following parameters are copied from zutil.h, version 0.95 */
30n/a#define DEFLATED 8
31n/a#if MAX_MEM_LEVEL >= 8
32n/a# define DEF_MEM_LEVEL 8
33n/a#else
34n/a# define DEF_MEM_LEVEL MAX_MEM_LEVEL
35n/a#endif
36n/a
37n/a/* Initial buffer size. */
38n/a#define DEF_BUF_SIZE (16*1024)
39n/a
40n/astatic PyTypeObject Comptype;
41n/astatic PyTypeObject Decomptype;
42n/a
43n/astatic PyObject *ZlibError;
44n/a
45n/atypedef struct
46n/a{
47n/a PyObject_HEAD
48n/a z_stream zst;
49n/a PyObject *unused_data;
50n/a PyObject *unconsumed_tail;
51n/a char eof;
52n/a int is_initialised;
53n/a PyObject *zdict;
54n/a #ifdef WITH_THREAD
55n/a PyThread_type_lock lock;
56n/a #endif
57n/a} compobject;
58n/a
59n/astatic void
60n/azlib_error(z_stream zst, int err, const char *msg)
61n/a{
62n/a const char *zmsg = Z_NULL;
63n/a /* In case of a version mismatch, zst.msg won't be initialized.
64n/a Check for this case first, before looking at zst.msg. */
65n/a if (err == Z_VERSION_ERROR)
66n/a zmsg = "library version mismatch";
67n/a if (zmsg == Z_NULL)
68n/a zmsg = zst.msg;
69n/a if (zmsg == Z_NULL) {
70n/a switch (err) {
71n/a case Z_BUF_ERROR:
72n/a zmsg = "incomplete or truncated stream";
73n/a break;
74n/a case Z_STREAM_ERROR:
75n/a zmsg = "inconsistent stream state";
76n/a break;
77n/a case Z_DATA_ERROR:
78n/a zmsg = "invalid input data";
79n/a break;
80n/a }
81n/a }
82n/a if (zmsg == Z_NULL)
83n/a PyErr_Format(ZlibError, "Error %d %s", err, msg);
84n/a else
85n/a PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);
86n/a}
87n/a
88n/a/*[clinic input]
89n/amodule zlib
90n/aclass zlib.Compress "compobject *" "&Comptype"
91n/aclass zlib.Decompress "compobject *" "&Decomptype"
92n/a[clinic start generated code]*/
93n/a/*[clinic end generated code: output=da39a3ee5e6b4b0d input=093935115c3e3158]*/
94n/a
95n/astatic compobject *
96n/anewcompobject(PyTypeObject *type)
97n/a{
98n/a compobject *self;
99n/a self = PyObject_New(compobject, type);
100n/a if (self == NULL)
101n/a return NULL;
102n/a self->eof = 0;
103n/a self->is_initialised = 0;
104n/a self->zdict = NULL;
105n/a self->unused_data = PyBytes_FromStringAndSize("", 0);
106n/a if (self->unused_data == NULL) {
107n/a Py_DECREF(self);
108n/a return NULL;
109n/a }
110n/a self->unconsumed_tail = PyBytes_FromStringAndSize("", 0);
111n/a if (self->unconsumed_tail == NULL) {
112n/a Py_DECREF(self);
113n/a return NULL;
114n/a }
115n/a#ifdef WITH_THREAD
116n/a self->lock = PyThread_allocate_lock();
117n/a if (self->lock == NULL) {
118n/a Py_DECREF(self);
119n/a PyErr_SetString(PyExc_MemoryError, "Unable to allocate lock");
120n/a return NULL;
121n/a }
122n/a#endif
123n/a return self;
124n/a}
125n/a
126n/astatic void*
127n/aPyZlib_Malloc(voidpf ctx, uInt items, uInt size)
128n/a{
129n/a if (items > (size_t)PY_SSIZE_T_MAX / size)
130n/a return NULL;
131n/a /* PyMem_Malloc() cannot be used: the GIL is not held when
132n/a inflate() and deflate() are called */
133n/a return PyMem_RawMalloc(items * size);
134n/a}
135n/a
136n/astatic void
137n/aPyZlib_Free(voidpf ctx, void *ptr)
138n/a{
139n/a PyMem_RawFree(ptr);
140n/a}
141n/a
142n/astatic void
143n/aarrange_input_buffer(z_stream *zst, Py_ssize_t *remains)
144n/a{
145n/a zst->avail_in = Py_MIN((size_t)*remains, UINT_MAX);
146n/a *remains -= zst->avail_in;
147n/a}
148n/a
149n/astatic Py_ssize_t
150n/aarrange_output_buffer_with_maximum(z_stream *zst, PyObject **buffer,
151n/a Py_ssize_t length,
152n/a Py_ssize_t max_length)
153n/a{
154n/a Py_ssize_t occupied;
155n/a
156n/a if (*buffer == NULL) {
157n/a if (!(*buffer = PyBytes_FromStringAndSize(NULL, length)))
158n/a return -1;
159n/a occupied = 0;
160n/a }
161n/a else {
162n/a occupied = zst->next_out - (Byte *)PyBytes_AS_STRING(*buffer);
163n/a
164n/a if (length == occupied) {
165n/a Py_ssize_t new_length;
166n/a assert(length <= max_length);
167n/a /* can not scale the buffer over max_length */
168n/a if (length == max_length)
169n/a return -2;
170n/a if (length <= (max_length >> 1))
171n/a new_length = length << 1;
172n/a else
173n/a new_length = max_length;
174n/a if (_PyBytes_Resize(buffer, new_length) < 0)
175n/a return -1;
176n/a length = new_length;
177n/a }
178n/a }
179n/a
180n/a zst->avail_out = Py_MIN((size_t)(length - occupied), UINT_MAX);
181n/a zst->next_out = (Byte *)PyBytes_AS_STRING(*buffer) + occupied;
182n/a
183n/a return length;
184n/a}
185n/a
186n/astatic Py_ssize_t
187n/aarrange_output_buffer(z_stream *zst, PyObject **buffer, Py_ssize_t length)
188n/a{
189n/a Py_ssize_t ret;
190n/a
191n/a ret = arrange_output_buffer_with_maximum(zst, buffer, length,
192n/a PY_SSIZE_T_MAX);
193n/a if (ret == -2)
194n/a PyErr_NoMemory();
195n/a
196n/a return ret;
197n/a}
198n/a
199n/a/*[clinic input]
200n/azlib.compress
201n/a
202n/a data: Py_buffer
203n/a Binary data to be compressed.
204n/a /
205n/a level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
206n/a Compression level, in 0-9 or -1.
207n/a
208n/aReturns a bytes object containing compressed data.
209n/a[clinic start generated code]*/
210n/a
211n/astatic PyObject *
212n/azlib_compress_impl(PyObject *module, Py_buffer *data, int level)
213n/a/*[clinic end generated code: output=d80906d73f6294c8 input=638d54b6315dbed3]*/
214n/a{
215n/a PyObject *RetVal = NULL;
216n/a Byte *ibuf;
217n/a Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
218n/a int err, flush;
219n/a z_stream zst;
220n/a
221n/a ibuf = data->buf;
222n/a ibuflen = data->len;
223n/a
224n/a zst.opaque = NULL;
225n/a zst.zalloc = PyZlib_Malloc;
226n/a zst.zfree = PyZlib_Free;
227n/a zst.next_in = ibuf;
228n/a err = deflateInit(&zst, level);
229n/a
230n/a switch (err) {
231n/a case Z_OK:
232n/a break;
233n/a case Z_MEM_ERROR:
234n/a PyErr_SetString(PyExc_MemoryError,
235n/a "Out of memory while compressing data");
236n/a goto error;
237n/a case Z_STREAM_ERROR:
238n/a PyErr_SetString(ZlibError, "Bad compression level");
239n/a goto error;
240n/a default:
241n/a deflateEnd(&zst);
242n/a zlib_error(zst, err, "while compressing data");
243n/a goto error;
244n/a }
245n/a
246n/a do {
247n/a arrange_input_buffer(&zst, &ibuflen);
248n/a flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
249n/a
250n/a do {
251n/a obuflen = arrange_output_buffer(&zst, &RetVal, obuflen);
252n/a if (obuflen < 0) {
253n/a deflateEnd(&zst);
254n/a goto error;
255n/a }
256n/a
257n/a Py_BEGIN_ALLOW_THREADS
258n/a err = deflate(&zst, flush);
259n/a Py_END_ALLOW_THREADS
260n/a
261n/a if (err == Z_STREAM_ERROR) {
262n/a deflateEnd(&zst);
263n/a zlib_error(zst, err, "while compressing data");
264n/a goto error;
265n/a }
266n/a
267n/a } while (zst.avail_out == 0);
268n/a assert(zst.avail_in == 0);
269n/a
270n/a } while (flush != Z_FINISH);
271n/a assert(err == Z_STREAM_END);
272n/a
273n/a err = deflateEnd(&zst);
274n/a if (err == Z_OK) {
275n/a if (_PyBytes_Resize(&RetVal, zst.next_out -
276n/a (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
277n/a goto error;
278n/a return RetVal;
279n/a }
280n/a else
281n/a zlib_error(zst, err, "while finishing compression");
282n/a error:
283n/a Py_XDECREF(RetVal);
284n/a return NULL;
285n/a}
286n/a
287n/a/*[python input]
288n/a
289n/aclass ssize_t_converter(CConverter):
290n/a type = 'Py_ssize_t'
291n/a converter = 'ssize_t_converter'
292n/a c_ignored_default = "0"
293n/a
294n/a[python start generated code]*/
295n/a/*[python end generated code: output=da39a3ee5e6b4b0d input=5f34ba1b394cb8e7]*/
296n/a
297n/astatic int
298n/assize_t_converter(PyObject *obj, void *ptr)
299n/a{
300n/a PyObject *long_obj;
301n/a Py_ssize_t val;
302n/a
303n/a long_obj = (PyObject *)_PyLong_FromNbInt(obj);
304n/a if (long_obj == NULL) {
305n/a return 0;
306n/a }
307n/a val = PyLong_AsSsize_t(long_obj);
308n/a Py_DECREF(long_obj);
309n/a if (val == -1 && PyErr_Occurred()) {
310n/a return 0;
311n/a }
312n/a *(Py_ssize_t *)ptr = val;
313n/a return 1;
314n/a}
315n/a
316n/a/*[clinic input]
317n/azlib.decompress
318n/a
319n/a data: Py_buffer
320n/a Compressed data.
321n/a /
322n/a wbits: int(c_default="MAX_WBITS") = MAX_WBITS
323n/a The window buffer size and container format.
324n/a bufsize: ssize_t(c_default="DEF_BUF_SIZE") = DEF_BUF_SIZE
325n/a The initial output buffer size.
326n/a
327n/aReturns a bytes object containing the uncompressed data.
328n/a[clinic start generated code]*/
329n/a
330n/astatic PyObject *
331n/azlib_decompress_impl(PyObject *module, Py_buffer *data, int wbits,
332n/a Py_ssize_t bufsize)
333n/a/*[clinic end generated code: output=77c7e35111dc8c42 input=21960936208e9a5b]*/
334n/a{
335n/a PyObject *RetVal = NULL;
336n/a Byte *ibuf;
337n/a Py_ssize_t ibuflen;
338n/a int err, flush;
339n/a z_stream zst;
340n/a
341n/a if (bufsize < 0) {
342n/a PyErr_SetString(PyExc_ValueError, "bufsize must be non-negative");
343n/a return NULL;
344n/a } else if (bufsize == 0) {
345n/a bufsize = 1;
346n/a }
347n/a
348n/a ibuf = data->buf;
349n/a ibuflen = data->len;
350n/a
351n/a zst.opaque = NULL;
352n/a zst.zalloc = PyZlib_Malloc;
353n/a zst.zfree = PyZlib_Free;
354n/a zst.avail_in = 0;
355n/a zst.next_in = ibuf;
356n/a err = inflateInit2(&zst, wbits);
357n/a
358n/a switch (err) {
359n/a case Z_OK:
360n/a break;
361n/a case Z_MEM_ERROR:
362n/a PyErr_SetString(PyExc_MemoryError,
363n/a "Out of memory while decompressing data");
364n/a goto error;
365n/a default:
366n/a inflateEnd(&zst);
367n/a zlib_error(zst, err, "while preparing to decompress data");
368n/a goto error;
369n/a }
370n/a
371n/a do {
372n/a arrange_input_buffer(&zst, &ibuflen);
373n/a flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
374n/a
375n/a do {
376n/a bufsize = arrange_output_buffer(&zst, &RetVal, bufsize);
377n/a if (bufsize < 0) {
378n/a inflateEnd(&zst);
379n/a goto error;
380n/a }
381n/a
382n/a Py_BEGIN_ALLOW_THREADS
383n/a err = inflate(&zst, flush);
384n/a Py_END_ALLOW_THREADS
385n/a
386n/a switch (err) {
387n/a case Z_OK: /* fall through */
388n/a case Z_BUF_ERROR: /* fall through */
389n/a case Z_STREAM_END:
390n/a break;
391n/a case Z_MEM_ERROR:
392n/a inflateEnd(&zst);
393n/a PyErr_SetString(PyExc_MemoryError,
394n/a "Out of memory while decompressing data");
395n/a goto error;
396n/a default:
397n/a inflateEnd(&zst);
398n/a zlib_error(zst, err, "while decompressing data");
399n/a goto error;
400n/a }
401n/a
402n/a } while (zst.avail_out == 0);
403n/a
404n/a } while (err != Z_STREAM_END && ibuflen != 0);
405n/a
406n/a
407n/a if (err != Z_STREAM_END) {
408n/a inflateEnd(&zst);
409n/a zlib_error(zst, err, "while decompressing data");
410n/a goto error;
411n/a }
412n/a
413n/a err = inflateEnd(&zst);
414n/a if (err != Z_OK) {
415n/a zlib_error(zst, err, "while finishing decompression");
416n/a goto error;
417n/a }
418n/a
419n/a if (_PyBytes_Resize(&RetVal, zst.next_out -
420n/a (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
421n/a goto error;
422n/a
423n/a return RetVal;
424n/a
425n/a error:
426n/a Py_XDECREF(RetVal);
427n/a return NULL;
428n/a}
429n/a
430n/a/*[clinic input]
431n/azlib.compressobj
432n/a
433n/a level: int(c_default="Z_DEFAULT_COMPRESSION") = Z_DEFAULT_COMPRESSION
434n/a The compression level (an integer in the range 0-9 or -1; default is
435n/a currently equivalent to 6). Higher compression levels are slower,
436n/a but produce smaller results.
437n/a method: int(c_default="DEFLATED") = DEFLATED
438n/a The compression algorithm. If given, this must be DEFLATED.
439n/a wbits: int(c_default="MAX_WBITS") = MAX_WBITS
440n/a +9 to +15: The base-two logarithm of the window size. Include a zlib
441n/a container.
442n/a -9 to -15: Generate a raw stream.
443n/a +25 to +31: Include a gzip container.
444n/a memLevel: int(c_default="DEF_MEM_LEVEL") = DEF_MEM_LEVEL
445n/a Controls the amount of memory used for internal compression state.
446n/a Valid values range from 1 to 9. Higher values result in higher memory
447n/a usage, faster compression, and smaller output.
448n/a strategy: int(c_default="Z_DEFAULT_STRATEGY") = Z_DEFAULT_STRATEGY
449n/a Used to tune the compression algorithm. Possible values are
450n/a Z_DEFAULT_STRATEGY, Z_FILTERED, and Z_HUFFMAN_ONLY.
451n/a zdict: Py_buffer = None
452n/a The predefined compression dictionary - a sequence of bytes
453n/a containing subsequences that are likely to occur in the input data.
454n/a
455n/aReturn a compressor object.
456n/a[clinic start generated code]*/
457n/a
458n/astatic PyObject *
459n/azlib_compressobj_impl(PyObject *module, int level, int method, int wbits,
460n/a int memLevel, int strategy, Py_buffer *zdict)
461n/a/*[clinic end generated code: output=8b5bed9c8fc3814d input=2fa3d026f90ab8d5]*/
462n/a{
463n/a compobject *self = NULL;
464n/a int err;
465n/a
466n/a if (zdict->buf != NULL && (size_t)zdict->len > UINT_MAX) {
467n/a PyErr_SetString(PyExc_OverflowError,
468n/a "zdict length does not fit in an unsigned int");
469n/a goto error;
470n/a }
471n/a
472n/a self = newcompobject(&Comptype);
473n/a if (self == NULL)
474n/a goto error;
475n/a self->zst.opaque = NULL;
476n/a self->zst.zalloc = PyZlib_Malloc;
477n/a self->zst.zfree = PyZlib_Free;
478n/a self->zst.next_in = NULL;
479n/a self->zst.avail_in = 0;
480n/a err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);
481n/a switch (err) {
482n/a case Z_OK:
483n/a self->is_initialised = 1;
484n/a if (zdict->buf == NULL) {
485n/a goto success;
486n/a } else {
487n/a err = deflateSetDictionary(&self->zst,
488n/a zdict->buf, (unsigned int)zdict->len);
489n/a switch (err) {
490n/a case Z_OK:
491n/a goto success;
492n/a case Z_STREAM_ERROR:
493n/a PyErr_SetString(PyExc_ValueError, "Invalid dictionary");
494n/a goto error;
495n/a default:
496n/a PyErr_SetString(PyExc_ValueError, "deflateSetDictionary()");
497n/a goto error;
498n/a }
499n/a }
500n/a case Z_MEM_ERROR:
501n/a PyErr_SetString(PyExc_MemoryError,
502n/a "Can't allocate memory for compression object");
503n/a goto error;
504n/a case Z_STREAM_ERROR:
505n/a PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
506n/a goto error;
507n/a default:
508n/a zlib_error(self->zst, err, "while creating compression object");
509n/a goto error;
510n/a }
511n/a
512n/a error:
513n/a Py_CLEAR(self);
514n/a success:
515n/a return (PyObject *)self;
516n/a}
517n/a
518n/astatic int
519n/aset_inflate_zdict(compobject *self)
520n/a{
521n/a Py_buffer zdict_buf;
522n/a int err;
523n/a
524n/a if (PyObject_GetBuffer(self->zdict, &zdict_buf, PyBUF_SIMPLE) == -1) {
525n/a return -1;
526n/a }
527n/a if ((size_t)zdict_buf.len > UINT_MAX) {
528n/a PyErr_SetString(PyExc_OverflowError,
529n/a "zdict length does not fit in an unsigned int");
530n/a PyBuffer_Release(&zdict_buf);
531n/a return -1;
532n/a }
533n/a err = inflateSetDictionary(&self->zst,
534n/a zdict_buf.buf, (unsigned int)zdict_buf.len);
535n/a PyBuffer_Release(&zdict_buf);
536n/a if (err != Z_OK) {
537n/a zlib_error(self->zst, err, "while setting zdict");
538n/a return -1;
539n/a }
540n/a return 0;
541n/a}
542n/a
543n/a/*[clinic input]
544n/azlib.decompressobj
545n/a
546n/a wbits: int(c_default="MAX_WBITS") = MAX_WBITS
547n/a The window buffer size and container format.
548n/a zdict: object(c_default="NULL") = b''
549n/a The predefined compression dictionary. This must be the same
550n/a dictionary as used by the compressor that produced the input data.
551n/a
552n/aReturn a decompressor object.
553n/a[clinic start generated code]*/
554n/a
555n/astatic PyObject *
556n/azlib_decompressobj_impl(PyObject *module, int wbits, PyObject *zdict)
557n/a/*[clinic end generated code: output=3069b99994f36906 input=d3832b8511fc977b]*/
558n/a{
559n/a int err;
560n/a compobject *self;
561n/a
562n/a if (zdict != NULL && !PyObject_CheckBuffer(zdict)) {
563n/a PyErr_SetString(PyExc_TypeError,
564n/a "zdict argument must support the buffer protocol");
565n/a return NULL;
566n/a }
567n/a
568n/a self = newcompobject(&Decomptype);
569n/a if (self == NULL)
570n/a return NULL;
571n/a self->zst.opaque = NULL;
572n/a self->zst.zalloc = PyZlib_Malloc;
573n/a self->zst.zfree = PyZlib_Free;
574n/a self->zst.next_in = NULL;
575n/a self->zst.avail_in = 0;
576n/a if (zdict != NULL) {
577n/a Py_INCREF(zdict);
578n/a self->zdict = zdict;
579n/a }
580n/a err = inflateInit2(&self->zst, wbits);
581n/a switch (err) {
582n/a case Z_OK:
583n/a self->is_initialised = 1;
584n/a if (self->zdict != NULL && wbits < 0) {
585n/a#ifdef AT_LEAST_ZLIB_1_2_2_1
586n/a if (set_inflate_zdict(self) < 0) {
587n/a Py_DECREF(self);
588n/a return NULL;
589n/a }
590n/a#else
591n/a PyErr_Format(ZlibError,
592n/a "zlib version %s does not allow raw inflate with dictionary",
593n/a ZLIB_VERSION);
594n/a Py_DECREF(self);
595n/a return NULL;
596n/a#endif
597n/a }
598n/a return (PyObject *)self;
599n/a case Z_STREAM_ERROR:
600n/a Py_DECREF(self);
601n/a PyErr_SetString(PyExc_ValueError, "Invalid initialization option");
602n/a return NULL;
603n/a case Z_MEM_ERROR:
604n/a Py_DECREF(self);
605n/a PyErr_SetString(PyExc_MemoryError,
606n/a "Can't allocate memory for decompression object");
607n/a return NULL;
608n/a default:
609n/a zlib_error(self->zst, err, "while creating decompression object");
610n/a Py_DECREF(self);
611n/a return NULL;
612n/a }
613n/a}
614n/a
615n/astatic void
616n/aDealloc(compobject *self)
617n/a{
618n/a#ifdef WITH_THREAD
619n/a PyThread_free_lock(self->lock);
620n/a#endif
621n/a Py_XDECREF(self->unused_data);
622n/a Py_XDECREF(self->unconsumed_tail);
623n/a Py_XDECREF(self->zdict);
624n/a PyObject_Del(self);
625n/a}
626n/a
627n/astatic void
628n/aComp_dealloc(compobject *self)
629n/a{
630n/a if (self->is_initialised)
631n/a deflateEnd(&self->zst);
632n/a Dealloc(self);
633n/a}
634n/a
635n/astatic void
636n/aDecomp_dealloc(compobject *self)
637n/a{
638n/a if (self->is_initialised)
639n/a inflateEnd(&self->zst);
640n/a Dealloc(self);
641n/a}
642n/a
643n/a/*[clinic input]
644n/azlib.Compress.compress
645n/a
646n/a data: Py_buffer
647n/a Binary data to be compressed.
648n/a /
649n/a
650n/aReturns a bytes object containing compressed data.
651n/a
652n/aAfter calling this function, some of the input data may still
653n/abe stored in internal buffers for later processing.
654n/aCall the flush() method to clear these buffers.
655n/a[clinic start generated code]*/
656n/a
657n/astatic PyObject *
658n/azlib_Compress_compress_impl(compobject *self, Py_buffer *data)
659n/a/*[clinic end generated code: output=5d5cd791cbc6a7f4 input=0d95908d6e64fab8]*/
660n/a{
661n/a PyObject *RetVal = NULL;
662n/a Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE;
663n/a int err;
664n/a
665n/a self->zst.next_in = data->buf;
666n/a ibuflen = data->len;
667n/a
668n/a ENTER_ZLIB(self);
669n/a
670n/a do {
671n/a arrange_input_buffer(&self->zst, &ibuflen);
672n/a
673n/a do {
674n/a obuflen = arrange_output_buffer(&self->zst, &RetVal, obuflen);
675n/a if (obuflen < 0)
676n/a goto error;
677n/a
678n/a Py_BEGIN_ALLOW_THREADS
679n/a err = deflate(&self->zst, Z_NO_FLUSH);
680n/a Py_END_ALLOW_THREADS
681n/a
682n/a if (err == Z_STREAM_ERROR) {
683n/a zlib_error(self->zst, err, "while compressing data");
684n/a goto error;
685n/a }
686n/a
687n/a } while (self->zst.avail_out == 0);
688n/a assert(self->zst.avail_in == 0);
689n/a
690n/a } while (ibuflen != 0);
691n/a
692n/a if (_PyBytes_Resize(&RetVal, self->zst.next_out -
693n/a (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
694n/a goto success;
695n/a
696n/a error:
697n/a Py_CLEAR(RetVal);
698n/a success:
699n/a LEAVE_ZLIB(self);
700n/a return RetVal;
701n/a}
702n/a
703n/a/* Helper for objdecompress() and flush(). Saves any unconsumed input data in
704n/a self->unused_data or self->unconsumed_tail, as appropriate. */
705n/astatic int
706n/asave_unconsumed_input(compobject *self, Py_buffer *data, int err)
707n/a{
708n/a if (err == Z_STREAM_END) {
709n/a /* The end of the compressed data has been reached. Store the leftover
710n/a input data in self->unused_data. */
711n/a if (self->zst.avail_in > 0) {
712n/a Py_ssize_t old_size = PyBytes_GET_SIZE(self->unused_data);
713n/a Py_ssize_t new_size, left_size;
714n/a PyObject *new_data;
715n/a left_size = (Byte *)data->buf + data->len - self->zst.next_in;
716n/a if (left_size > (PY_SSIZE_T_MAX - old_size)) {
717n/a PyErr_NoMemory();
718n/a return -1;
719n/a }
720n/a new_size = old_size + left_size;
721n/a new_data = PyBytes_FromStringAndSize(NULL, new_size);
722n/a if (new_data == NULL)
723n/a return -1;
724n/a memcpy(PyBytes_AS_STRING(new_data),
725n/a PyBytes_AS_STRING(self->unused_data), old_size);
726n/a memcpy(PyBytes_AS_STRING(new_data) + old_size,
727n/a self->zst.next_in, left_size);
728n/a Py_SETREF(self->unused_data, new_data);
729n/a self->zst.avail_in = 0;
730n/a }
731n/a }
732n/a
733n/a if (self->zst.avail_in > 0 || PyBytes_GET_SIZE(self->unconsumed_tail)) {
734n/a /* This code handles two distinct cases:
735n/a 1. Output limit was reached. Save leftover input in unconsumed_tail.
736n/a 2. All input data was consumed. Clear unconsumed_tail. */
737n/a Py_ssize_t left_size = (Byte *)data->buf + data->len - self->zst.next_in;
738n/a PyObject *new_data = PyBytes_FromStringAndSize(
739n/a (char *)self->zst.next_in, left_size);
740n/a if (new_data == NULL)
741n/a return -1;
742n/a Py_SETREF(self->unconsumed_tail, new_data);
743n/a }
744n/a
745n/a return 0;
746n/a}
747n/a
748n/a/*[clinic input]
749n/azlib.Decompress.decompress
750n/a
751n/a data: Py_buffer
752n/a The binary data to decompress.
753n/a /
754n/a max_length: ssize_t = 0
755n/a The maximum allowable length of the decompressed data.
756n/a Unconsumed input data will be stored in
757n/a the unconsumed_tail attribute.
758n/a
759n/aReturn a bytes object containing the decompressed version of the data.
760n/a
761n/aAfter calling this function, some of the input data may still be stored in
762n/ainternal buffers for later processing.
763n/aCall the flush() method to clear these buffers.
764n/a[clinic start generated code]*/
765n/a
766n/astatic PyObject *
767n/azlib_Decompress_decompress_impl(compobject *self, Py_buffer *data,
768n/a Py_ssize_t max_length)
769n/a/*[clinic end generated code: output=6e5173c74e710352 input=b85a212a012b770a]*/
770n/a{
771n/a int err = Z_OK;
772n/a Py_ssize_t ibuflen, obuflen = DEF_BUF_SIZE, hard_limit;
773n/a PyObject *RetVal = NULL;
774n/a
775n/a if (max_length < 0) {
776n/a PyErr_SetString(PyExc_ValueError, "max_length must be non-negative");
777n/a return NULL;
778n/a } else if (max_length == 0)
779n/a hard_limit = PY_SSIZE_T_MAX;
780n/a else
781n/a hard_limit = max_length;
782n/a
783n/a self->zst.next_in = data->buf;
784n/a ibuflen = data->len;
785n/a
786n/a /* limit amount of data allocated to max_length */
787n/a if (max_length && obuflen > max_length)
788n/a obuflen = max_length;
789n/a
790n/a ENTER_ZLIB(self);
791n/a
792n/a do {
793n/a arrange_input_buffer(&self->zst, &ibuflen);
794n/a
795n/a do {
796n/a obuflen = arrange_output_buffer_with_maximum(&self->zst, &RetVal,
797n/a obuflen, hard_limit);
798n/a if (obuflen == -2) {
799n/a if (max_length > 0) {
800n/a goto save;
801n/a }
802n/a PyErr_NoMemory();
803n/a }
804n/a if (obuflen < 0) {
805n/a goto abort;
806n/a }
807n/a
808n/a Py_BEGIN_ALLOW_THREADS
809n/a err = inflate(&self->zst, Z_SYNC_FLUSH);
810n/a Py_END_ALLOW_THREADS
811n/a
812n/a switch (err) {
813n/a case Z_OK: /* fall through */
814n/a case Z_BUF_ERROR: /* fall through */
815n/a case Z_STREAM_END:
816n/a break;
817n/a default:
818n/a if (err == Z_NEED_DICT && self->zdict != NULL) {
819n/a if (set_inflate_zdict(self) < 0)
820n/a goto abort;
821n/a else
822n/a break;
823n/a }
824n/a goto save;
825n/a }
826n/a
827n/a } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
828n/a
829n/a } while (err != Z_STREAM_END && ibuflen != 0);
830n/a
831n/a save:
832n/a if (save_unconsumed_input(self, data, err) < 0)
833n/a goto abort;
834n/a
835n/a if (err == Z_STREAM_END) {
836n/a /* This is the logical place to call inflateEnd, but the old behaviour
837n/a of only calling it on flush() is preserved. */
838n/a self->eof = 1;
839n/a } else if (err != Z_OK && err != Z_BUF_ERROR) {
840n/a /* We will only get Z_BUF_ERROR if the output buffer was full
841n/a but there wasn't more output when we tried again, so it is
842n/a not an error condition.
843n/a */
844n/a zlib_error(self->zst, err, "while decompressing data");
845n/a goto abort;
846n/a }
847n/a
848n/a if (_PyBytes_Resize(&RetVal, self->zst.next_out -
849n/a (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
850n/a goto success;
851n/a
852n/a abort:
853n/a Py_CLEAR(RetVal);
854n/a success:
855n/a LEAVE_ZLIB(self);
856n/a return RetVal;
857n/a}
858n/a
859n/a/*[clinic input]
860n/azlib.Compress.flush
861n/a
862n/a mode: int(c_default="Z_FINISH") = zlib.Z_FINISH
863n/a One of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH.
864n/a If mode == Z_FINISH, the compressor object can no longer be
865n/a used after calling the flush() method. Otherwise, more data
866n/a can still be compressed.
867n/a /
868n/a
869n/aReturn a bytes object containing any remaining compressed data.
870n/a[clinic start generated code]*/
871n/a
872n/astatic PyObject *
873n/azlib_Compress_flush_impl(compobject *self, int mode)
874n/a/*[clinic end generated code: output=a203f4cefc9de727 input=73ed066794bd15bc]*/
875n/a{
876n/a int err;
877n/a Py_ssize_t length = DEF_BUF_SIZE;
878n/a PyObject *RetVal = NULL;
879n/a
880n/a /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
881n/a doing any work at all; just return an empty string. */
882n/a if (mode == Z_NO_FLUSH) {
883n/a return PyBytes_FromStringAndSize(NULL, 0);
884n/a }
885n/a
886n/a ENTER_ZLIB(self);
887n/a
888n/a self->zst.avail_in = 0;
889n/a
890n/a do {
891n/a length = arrange_output_buffer(&self->zst, &RetVal, length);
892n/a if (length < 0) {
893n/a Py_CLEAR(RetVal);
894n/a goto error;
895n/a }
896n/a
897n/a Py_BEGIN_ALLOW_THREADS
898n/a err = deflate(&self->zst, mode);
899n/a Py_END_ALLOW_THREADS
900n/a
901n/a if (err == Z_STREAM_ERROR) {
902n/a zlib_error(self->zst, err, "while flushing");
903n/a Py_CLEAR(RetVal);
904n/a goto error;
905n/a }
906n/a } while (self->zst.avail_out == 0);
907n/a assert(self->zst.avail_in == 0);
908n/a
909n/a /* If mode is Z_FINISH, we also have to call deflateEnd() to free
910n/a various data structures. Note we should only get Z_STREAM_END when
911n/a mode is Z_FINISH, but checking both for safety*/
912n/a if (err == Z_STREAM_END && mode == Z_FINISH) {
913n/a err = deflateEnd(&self->zst);
914n/a if (err != Z_OK) {
915n/a zlib_error(self->zst, err, "while finishing compression");
916n/a Py_CLEAR(RetVal);
917n/a goto error;
918n/a }
919n/a else
920n/a self->is_initialised = 0;
921n/a
922n/a /* We will only get Z_BUF_ERROR if the output buffer was full
923n/a but there wasn't more output when we tried again, so it is
924n/a not an error condition.
925n/a */
926n/a } else if (err != Z_OK && err != Z_BUF_ERROR) {
927n/a zlib_error(self->zst, err, "while flushing");
928n/a Py_CLEAR(RetVal);
929n/a goto error;
930n/a }
931n/a
932n/a if (_PyBytes_Resize(&RetVal, self->zst.next_out -
933n/a (Byte *)PyBytes_AS_STRING(RetVal)) < 0)
934n/a Py_CLEAR(RetVal);
935n/a
936n/a error:
937n/a LEAVE_ZLIB(self);
938n/a return RetVal;
939n/a}
940n/a
941n/a#ifdef HAVE_ZLIB_COPY
942n/a
943n/a/*[clinic input]
944n/azlib.Compress.copy
945n/a
946n/aReturn a copy of the compression object.
947n/a[clinic start generated code]*/
948n/a
949n/astatic PyObject *
950n/azlib_Compress_copy_impl(compobject *self)
951n/a/*[clinic end generated code: output=5144aa153c21e805 input=c656351f94b82718]*/
952n/a{
953n/a compobject *retval = NULL;
954n/a int err;
955n/a
956n/a retval = newcompobject(&Comptype);
957n/a if (!retval) return NULL;
958n/a
959n/a /* Copy the zstream state
960n/a * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
961n/a */
962n/a ENTER_ZLIB(self);
963n/a err = deflateCopy(&retval->zst, &self->zst);
964n/a switch (err) {
965n/a case Z_OK:
966n/a break;
967n/a case Z_STREAM_ERROR:
968n/a PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
969n/a goto error;
970n/a case Z_MEM_ERROR:
971n/a PyErr_SetString(PyExc_MemoryError,
972n/a "Can't allocate memory for compression object");
973n/a goto error;
974n/a default:
975n/a zlib_error(self->zst, err, "while copying compression object");
976n/a goto error;
977n/a }
978n/a Py_INCREF(self->unused_data);
979n/a Py_XSETREF(retval->unused_data, self->unused_data);
980n/a Py_INCREF(self->unconsumed_tail);
981n/a Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
982n/a Py_XINCREF(self->zdict);
983n/a Py_XSETREF(retval->zdict, self->zdict);
984n/a retval->eof = self->eof;
985n/a
986n/a /* Mark it as being initialized */
987n/a retval->is_initialised = 1;
988n/a
989n/a LEAVE_ZLIB(self);
990n/a return (PyObject *)retval;
991n/a
992n/aerror:
993n/a LEAVE_ZLIB(self);
994n/a Py_XDECREF(retval);
995n/a return NULL;
996n/a}
997n/a
998n/a/*[clinic input]
999n/azlib.Decompress.copy
1000n/a
1001n/aReturn a copy of the decompression object.
1002n/a[clinic start generated code]*/
1003n/a
1004n/astatic PyObject *
1005n/azlib_Decompress_copy_impl(compobject *self)
1006n/a/*[clinic end generated code: output=02a883a2a510c8cc input=ba6c3e96712a596b]*/
1007n/a{
1008n/a compobject *retval = NULL;
1009n/a int err;
1010n/a
1011n/a retval = newcompobject(&Decomptype);
1012n/a if (!retval) return NULL;
1013n/a
1014n/a /* Copy the zstream state
1015n/a * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe
1016n/a */
1017n/a ENTER_ZLIB(self);
1018n/a err = inflateCopy(&retval->zst, &self->zst);
1019n/a switch (err) {
1020n/a case Z_OK:
1021n/a break;
1022n/a case Z_STREAM_ERROR:
1023n/a PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");
1024n/a goto error;
1025n/a case Z_MEM_ERROR:
1026n/a PyErr_SetString(PyExc_MemoryError,
1027n/a "Can't allocate memory for decompression object");
1028n/a goto error;
1029n/a default:
1030n/a zlib_error(self->zst, err, "while copying decompression object");
1031n/a goto error;
1032n/a }
1033n/a
1034n/a Py_INCREF(self->unused_data);
1035n/a Py_XSETREF(retval->unused_data, self->unused_data);
1036n/a Py_INCREF(self->unconsumed_tail);
1037n/a Py_XSETREF(retval->unconsumed_tail, self->unconsumed_tail);
1038n/a Py_XINCREF(self->zdict);
1039n/a Py_XSETREF(retval->zdict, self->zdict);
1040n/a retval->eof = self->eof;
1041n/a
1042n/a /* Mark it as being initialized */
1043n/a retval->is_initialised = 1;
1044n/a
1045n/a LEAVE_ZLIB(self);
1046n/a return (PyObject *)retval;
1047n/a
1048n/aerror:
1049n/a LEAVE_ZLIB(self);
1050n/a Py_XDECREF(retval);
1051n/a return NULL;
1052n/a}
1053n/a#endif
1054n/a
1055n/a/*[clinic input]
1056n/azlib.Decompress.flush
1057n/a
1058n/a length: ssize_t(c_default="DEF_BUF_SIZE") = zlib.DEF_BUF_SIZE
1059n/a the initial size of the output buffer.
1060n/a /
1061n/a
1062n/aReturn a bytes object containing any remaining decompressed data.
1063n/a[clinic start generated code]*/
1064n/a
1065n/astatic PyObject *
1066n/azlib_Decompress_flush_impl(compobject *self, Py_ssize_t length)
1067n/a/*[clinic end generated code: output=68c75ea127cbe654 input=aa4ec37f3aef4da0]*/
1068n/a{
1069n/a int err, flush;
1070n/a Py_buffer data;
1071n/a PyObject *RetVal = NULL;
1072n/a Py_ssize_t ibuflen;
1073n/a
1074n/a if (length <= 0) {
1075n/a PyErr_SetString(PyExc_ValueError, "length must be greater than zero");
1076n/a return NULL;
1077n/a }
1078n/a
1079n/a if (PyObject_GetBuffer(self->unconsumed_tail, &data, PyBUF_SIMPLE) == -1)
1080n/a return NULL;
1081n/a
1082n/a ENTER_ZLIB(self);
1083n/a
1084n/a self->zst.next_in = data.buf;
1085n/a ibuflen = data.len;
1086n/a
1087n/a do {
1088n/a arrange_input_buffer(&self->zst, &ibuflen);
1089n/a flush = ibuflen == 0 ? Z_FINISH : Z_NO_FLUSH;
1090n/a
1091n/a do {
1092n/a length = arrange_output_buffer(&self->zst, &RetVal, length);
1093n/a if (length < 0)
1094n/a goto abort;
1095n/a
1096n/a Py_BEGIN_ALLOW_THREADS
1097n/a err = inflate(&self->zst, flush);
1098n/a Py_END_ALLOW_THREADS
1099n/a
1100n/a switch (err) {
1101n/a case Z_OK: /* fall through */
1102n/a case Z_BUF_ERROR: /* fall through */
1103n/a case Z_STREAM_END:
1104n/a break;
1105n/a default:
1106n/a if (err == Z_NEED_DICT && self->zdict != NULL) {
1107n/a if (set_inflate_zdict(self) < 0)
1108n/a goto abort;
1109n/a else
1110n/a break;
1111n/a }
1112n/a goto save;
1113n/a }
1114n/a
1115n/a } while (self->zst.avail_out == 0 || err == Z_NEED_DICT);
1116n/a
1117n/a } while (err != Z_STREAM_END && ibuflen != 0);
1118n/a
1119n/a save:
1120n/a if (save_unconsumed_input(self, &data, err) < 0)
1121n/a goto abort;
1122n/a
1123n/a /* If at end of stream, clean up any memory allocated by zlib. */
1124n/a if (err == Z_STREAM_END) {
1125n/a self->eof = 1;
1126n/a self->is_initialised = 0;
1127n/a err = inflateEnd(&self->zst);
1128n/a if (err != Z_OK) {
1129n/a zlib_error(self->zst, err, "while finishing decompression");
1130n/a goto abort;
1131n/a }
1132n/a }
1133n/a
1134n/a if (_PyBytes_Resize(&RetVal, self->zst.next_out -
1135n/a (Byte *)PyBytes_AS_STRING(RetVal)) == 0)
1136n/a goto success;
1137n/a
1138n/a abort:
1139n/a Py_CLEAR(RetVal);
1140n/a success:
1141n/a PyBuffer_Release(&data);
1142n/a LEAVE_ZLIB(self);
1143n/a return RetVal;
1144n/a}
1145n/a
1146n/a#include "clinic/zlibmodule.c.h"
1147n/a
1148n/astatic PyMethodDef comp_methods[] =
1149n/a{
1150n/a ZLIB_COMPRESS_COMPRESS_METHODDEF
1151n/a ZLIB_COMPRESS_FLUSH_METHODDEF
1152n/a#ifdef HAVE_ZLIB_COPY
1153n/a ZLIB_COMPRESS_COPY_METHODDEF
1154n/a#endif
1155n/a {NULL, NULL}
1156n/a};
1157n/a
1158n/astatic PyMethodDef Decomp_methods[] =
1159n/a{
1160n/a ZLIB_DECOMPRESS_DECOMPRESS_METHODDEF
1161n/a ZLIB_DECOMPRESS_FLUSH_METHODDEF
1162n/a#ifdef HAVE_ZLIB_COPY
1163n/a ZLIB_DECOMPRESS_COPY_METHODDEF
1164n/a#endif
1165n/a {NULL, NULL}
1166n/a};
1167n/a
1168n/a#define COMP_OFF(x) offsetof(compobject, x)
1169n/astatic PyMemberDef Decomp_members[] = {
1170n/a {"unused_data", T_OBJECT, COMP_OFF(unused_data), READONLY},
1171n/a {"unconsumed_tail", T_OBJECT, COMP_OFF(unconsumed_tail), READONLY},
1172n/a {"eof", T_BOOL, COMP_OFF(eof), READONLY},
1173n/a {NULL},
1174n/a};
1175n/a
1176n/a/*[clinic input]
1177n/azlib.adler32
1178n/a
1179n/a data: Py_buffer
1180n/a value: unsigned_int(bitwise=True) = 1
1181n/a Starting value of the checksum.
1182n/a /
1183n/a
1184n/aCompute an Adler-32 checksum of data.
1185n/a
1186n/aThe returned checksum is an integer.
1187n/a[clinic start generated code]*/
1188n/a
1189n/astatic PyObject *
1190n/azlib_adler32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1191n/a/*[clinic end generated code: output=422106f5ca8c92c0 input=6ff4557872160e88]*/
1192n/a{
1193n/a /* Releasing the GIL for very small buffers is inefficient
1194n/a and may lower performance */
1195n/a if (data->len > 1024*5) {
1196n/a unsigned char *buf = data->buf;
1197n/a Py_ssize_t len = data->len;
1198n/a
1199n/a Py_BEGIN_ALLOW_THREADS
1200n/a /* Avoid truncation of length for very large buffers. adler32() takes
1201n/a length as an unsigned int, which may be narrower than Py_ssize_t. */
1202n/a while ((size_t)len > UINT_MAX) {
1203n/a value = adler32(value, buf, UINT_MAX);
1204n/a buf += (size_t) UINT_MAX;
1205n/a len -= (size_t) UINT_MAX;
1206n/a }
1207n/a value = adler32(value, buf, (unsigned int)len);
1208n/a Py_END_ALLOW_THREADS
1209n/a } else {
1210n/a value = adler32(value, data->buf, (unsigned int)data->len);
1211n/a }
1212n/a return PyLong_FromUnsignedLong(value & 0xffffffffU);
1213n/a}
1214n/a
1215n/a/*[clinic input]
1216n/azlib.crc32
1217n/a
1218n/a data: Py_buffer
1219n/a value: unsigned_int(bitwise=True) = 0
1220n/a Starting value of the checksum.
1221n/a /
1222n/a
1223n/aCompute a CRC-32 checksum of data.
1224n/a
1225n/aThe returned checksum is an integer.
1226n/a[clinic start generated code]*/
1227n/a
1228n/astatic PyObject *
1229n/azlib_crc32_impl(PyObject *module, Py_buffer *data, unsigned int value)
1230n/a/*[clinic end generated code: output=63499fa20af7ea25 input=26c3ed430fa00b4c]*/
1231n/a{
1232n/a int signed_val;
1233n/a
1234n/a /* Releasing the GIL for very small buffers is inefficient
1235n/a and may lower performance */
1236n/a if (data->len > 1024*5) {
1237n/a unsigned char *buf = data->buf;
1238n/a Py_ssize_t len = data->len;
1239n/a
1240n/a Py_BEGIN_ALLOW_THREADS
1241n/a /* Avoid truncation of length for very large buffers. crc32() takes
1242n/a length as an unsigned int, which may be narrower than Py_ssize_t. */
1243n/a while ((size_t)len > UINT_MAX) {
1244n/a value = crc32(value, buf, UINT_MAX);
1245n/a buf += (size_t) UINT_MAX;
1246n/a len -= (size_t) UINT_MAX;
1247n/a }
1248n/a signed_val = crc32(value, buf, (unsigned int)len);
1249n/a Py_END_ALLOW_THREADS
1250n/a } else {
1251n/a signed_val = crc32(value, data->buf, (unsigned int)data->len);
1252n/a }
1253n/a return PyLong_FromUnsignedLong(signed_val & 0xffffffffU);
1254n/a}
1255n/a
1256n/a
1257n/astatic PyMethodDef zlib_methods[] =
1258n/a{
1259n/a ZLIB_ADLER32_METHODDEF
1260n/a ZLIB_COMPRESS_METHODDEF
1261n/a ZLIB_COMPRESSOBJ_METHODDEF
1262n/a ZLIB_CRC32_METHODDEF
1263n/a ZLIB_DECOMPRESS_METHODDEF
1264n/a ZLIB_DECOMPRESSOBJ_METHODDEF
1265n/a {NULL, NULL}
1266n/a};
1267n/a
1268n/astatic PyTypeObject Comptype = {
1269n/a PyVarObject_HEAD_INIT(0, 0)
1270n/a "zlib.Compress",
1271n/a sizeof(compobject),
1272n/a 0,
1273n/a (destructor)Comp_dealloc, /*tp_dealloc*/
1274n/a 0, /*tp_print*/
1275n/a 0, /*tp_getattr*/
1276n/a 0, /*tp_setattr*/
1277n/a 0, /*tp_reserved*/
1278n/a 0, /*tp_repr*/
1279n/a 0, /*tp_as_number*/
1280n/a 0, /*tp_as_sequence*/
1281n/a 0, /*tp_as_mapping*/
1282n/a 0, /*tp_hash*/
1283n/a 0, /*tp_call*/
1284n/a 0, /*tp_str*/
1285n/a 0, /*tp_getattro*/
1286n/a 0, /*tp_setattro*/
1287n/a 0, /*tp_as_buffer*/
1288n/a Py_TPFLAGS_DEFAULT, /*tp_flags*/
1289n/a 0, /*tp_doc*/
1290n/a 0, /*tp_traverse*/
1291n/a 0, /*tp_clear*/
1292n/a 0, /*tp_richcompare*/
1293n/a 0, /*tp_weaklistoffset*/
1294n/a 0, /*tp_iter*/
1295n/a 0, /*tp_iternext*/
1296n/a comp_methods, /*tp_methods*/
1297n/a};
1298n/a
1299n/astatic PyTypeObject Decomptype = {
1300n/a PyVarObject_HEAD_INIT(0, 0)
1301n/a "zlib.Decompress",
1302n/a sizeof(compobject),
1303n/a 0,
1304n/a (destructor)Decomp_dealloc, /*tp_dealloc*/
1305n/a 0, /*tp_print*/
1306n/a 0, /*tp_getattr*/
1307n/a 0, /*tp_setattr*/
1308n/a 0, /*tp_reserved*/
1309n/a 0, /*tp_repr*/
1310n/a 0, /*tp_as_number*/
1311n/a 0, /*tp_as_sequence*/
1312n/a 0, /*tp_as_mapping*/
1313n/a 0, /*tp_hash*/
1314n/a 0, /*tp_call*/
1315n/a 0, /*tp_str*/
1316n/a 0, /*tp_getattro*/
1317n/a 0, /*tp_setattro*/
1318n/a 0, /*tp_as_buffer*/
1319n/a Py_TPFLAGS_DEFAULT, /*tp_flags*/
1320n/a 0, /*tp_doc*/
1321n/a 0, /*tp_traverse*/
1322n/a 0, /*tp_clear*/
1323n/a 0, /*tp_richcompare*/
1324n/a 0, /*tp_weaklistoffset*/
1325n/a 0, /*tp_iter*/
1326n/a 0, /*tp_iternext*/
1327n/a Decomp_methods, /*tp_methods*/
1328n/a Decomp_members, /*tp_members*/
1329n/a};
1330n/a
1331n/aPyDoc_STRVAR(zlib_module_documentation,
1332n/a"The functions in this module allow compression and decompression using the\n"
1333n/a"zlib library, which is based on GNU zip.\n"
1334n/a"\n"
1335n/a"adler32(string[, start]) -- Compute an Adler-32 checksum.\n"
1336n/a"compress(data[, level]) -- Compress data, with compression level 0-9 or -1.\n"
1337n/a"compressobj([level[, ...]]) -- Return a compressor object.\n"
1338n/a"crc32(string[, start]) -- Compute a CRC-32 checksum.\n"
1339n/a"decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"
1340n/a"decompressobj([wbits[, zdict]]]) -- Return a decompressor object.\n"
1341n/a"\n"
1342n/a"'wbits' is window buffer size and container format.\n"
1343n/a"Compressor objects support compress() and flush() methods; decompressor\n"
1344n/a"objects support decompress() and flush().");
1345n/a
1346n/astatic struct PyModuleDef zlibmodule = {
1347n/a PyModuleDef_HEAD_INIT,
1348n/a "zlib",
1349n/a zlib_module_documentation,
1350n/a -1,
1351n/a zlib_methods,
1352n/a NULL,
1353n/a NULL,
1354n/a NULL,
1355n/a NULL
1356n/a};
1357n/a
1358n/aPyMODINIT_FUNC
1359n/aPyInit_zlib(void)
1360n/a{
1361n/a PyObject *m, *ver;
1362n/a if (PyType_Ready(&Comptype) < 0)
1363n/a return NULL;
1364n/a if (PyType_Ready(&Decomptype) < 0)
1365n/a return NULL;
1366n/a m = PyModule_Create(&zlibmodule);
1367n/a if (m == NULL)
1368n/a return NULL;
1369n/a
1370n/a ZlibError = PyErr_NewException("zlib.error", NULL, NULL);
1371n/a if (ZlibError != NULL) {
1372n/a Py_INCREF(ZlibError);
1373n/a PyModule_AddObject(m, "error", ZlibError);
1374n/a }
1375n/a PyModule_AddIntMacro(m, MAX_WBITS);
1376n/a PyModule_AddIntMacro(m, DEFLATED);
1377n/a PyModule_AddIntMacro(m, DEF_MEM_LEVEL);
1378n/a PyModule_AddIntMacro(m, DEF_BUF_SIZE);
1379n/a PyModule_AddIntMacro(m, Z_BEST_SPEED);
1380n/a PyModule_AddIntMacro(m, Z_BEST_COMPRESSION);
1381n/a PyModule_AddIntMacro(m, Z_DEFAULT_COMPRESSION);
1382n/a PyModule_AddIntMacro(m, Z_FILTERED);
1383n/a PyModule_AddIntMacro(m, Z_HUFFMAN_ONLY);
1384n/a PyModule_AddIntMacro(m, Z_DEFAULT_STRATEGY);
1385n/a
1386n/a PyModule_AddIntMacro(m, Z_FINISH);
1387n/a PyModule_AddIntMacro(m, Z_NO_FLUSH);
1388n/a PyModule_AddIntMacro(m, Z_SYNC_FLUSH);
1389n/a PyModule_AddIntMacro(m, Z_FULL_FLUSH);
1390n/a
1391n/a ver = PyUnicode_FromString(ZLIB_VERSION);
1392n/a if (ver != NULL)
1393n/a PyModule_AddObject(m, "ZLIB_VERSION", ver);
1394n/a
1395n/a ver = PyUnicode_FromString(zlibVersion());
1396n/a if (ver != NULL)
1397n/a PyModule_AddObject(m, "ZLIB_RUNTIME_VERSION", ver);
1398n/a
1399n/a PyModule_AddStringConstant(m, "__version__", "1.0");
1400n/a
1401n/a return m;
1402n/a}