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