]>
Commit | Line | Data |
---|---|---|
7eb75bcc DM |
1 | /* zlibmodule.c -- gzip-compatible data compression */\r |
2 | /* See http://www.gzip.org/zlib/ */\r | |
3 | \r | |
4 | /* Windows users: read Python's PCbuild\readme.txt */\r | |
5 | \r | |
6 | \r | |
7 | #include "Python.h"\r | |
8 | #include "zlib.h"\r | |
9 | \r | |
10 | #ifdef WITH_THREAD\r | |
11 | #include "pythread.h"\r | |
12 | \r | |
13 | /* #defs ripped off from _tkinter.c, even though the situation here is much\r | |
14 | simpler, because we don't have to worry about waiting for Tcl\r | |
15 | events! And, since zlib itself is threadsafe, we don't need to worry\r | |
16 | about re-entering zlib functions.\r | |
17 | \r | |
18 | N.B.\r | |
19 | \r | |
20 | Since ENTER_ZLIB and LEAVE_ZLIB only need to be called on functions\r | |
21 | that modify the components of preexisting de/compress objects, it\r | |
22 | could prove to be a performance gain on multiprocessor machines if\r | |
23 | there was an de/compress object-specific lock. However, for the\r | |
24 | moment the ENTER_ZLIB and LEAVE_ZLIB calls are global for ALL\r | |
25 | de/compress objects.\r | |
26 | */\r | |
27 | \r | |
28 | static PyThread_type_lock zlib_lock = NULL; /* initialized on module load */\r | |
29 | \r | |
30 | #define ENTER_ZLIB \\r | |
31 | Py_BEGIN_ALLOW_THREADS \\r | |
32 | PyThread_acquire_lock(zlib_lock, 1); \\r | |
33 | Py_END_ALLOW_THREADS\r | |
34 | \r | |
35 | #define LEAVE_ZLIB \\r | |
36 | PyThread_release_lock(zlib_lock);\r | |
37 | \r | |
38 | #else\r | |
39 | \r | |
40 | #define ENTER_ZLIB\r | |
41 | #define LEAVE_ZLIB\r | |
42 | \r | |
43 | #endif\r | |
44 | \r | |
45 | /* The following parameters are copied from zutil.h, version 0.95 */\r | |
46 | #define DEFLATED 8\r | |
47 | #if MAX_MEM_LEVEL >= 8\r | |
48 | # define DEF_MEM_LEVEL 8\r | |
49 | #else\r | |
50 | # define DEF_MEM_LEVEL MAX_MEM_LEVEL\r | |
51 | #endif\r | |
52 | #define DEF_WBITS MAX_WBITS\r | |
53 | \r | |
54 | /* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */\r | |
55 | #define DEFAULTALLOC (16*1024)\r | |
56 | #define PyInit_zlib initzlib\r | |
57 | \r | |
58 | static PyTypeObject Comptype;\r | |
59 | static PyTypeObject Decomptype;\r | |
60 | \r | |
61 | static PyObject *ZlibError;\r | |
62 | \r | |
63 | typedef struct\r | |
64 | {\r | |
65 | PyObject_HEAD\r | |
66 | z_stream zst;\r | |
67 | PyObject *unused_data;\r | |
68 | PyObject *unconsumed_tail;\r | |
69 | int is_initialised;\r | |
70 | } compobject;\r | |
71 | \r | |
72 | static void\r | |
73 | zlib_error(z_stream zst, int err, char *msg)\r | |
74 | {\r | |
75 | const char *zmsg = Z_NULL;\r | |
76 | /* In case of a version mismatch, zst.msg won't be initialized.\r | |
77 | Check for this case first, before looking at zst.msg. */\r | |
78 | if (err == Z_VERSION_ERROR)\r | |
79 | zmsg = "library version mismatch";\r | |
80 | if (zmsg == Z_NULL)\r | |
81 | zmsg = zst.msg;\r | |
82 | if (zmsg == Z_NULL) {\r | |
83 | switch (err) {\r | |
84 | case Z_BUF_ERROR:\r | |
85 | zmsg = "incomplete or truncated stream";\r | |
86 | break;\r | |
87 | case Z_STREAM_ERROR:\r | |
88 | zmsg = "inconsistent stream state";\r | |
89 | break;\r | |
90 | case Z_DATA_ERROR:\r | |
91 | zmsg = "invalid input data";\r | |
92 | break;\r | |
93 | }\r | |
94 | }\r | |
95 | if (zmsg == Z_NULL)\r | |
96 | PyErr_Format(ZlibError, "Error %d %s", err, msg);\r | |
97 | else\r | |
98 | PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zmsg);\r | |
99 | }\r | |
100 | \r | |
101 | PyDoc_STRVAR(compressobj__doc__,\r | |
102 | "compressobj([level]) -- Return a compressor object.\n"\r | |
103 | "\n"\r | |
104 | "Optional arg level is the compression level, in 0-9.");\r | |
105 | \r | |
106 | PyDoc_STRVAR(decompressobj__doc__,\r | |
107 | "decompressobj([wbits]) -- Return a decompressor object.\n"\r | |
108 | "\n"\r | |
109 | "Optional arg wbits is the window buffer size.");\r | |
110 | \r | |
111 | static compobject *\r | |
112 | newcompobject(PyTypeObject *type)\r | |
113 | {\r | |
114 | compobject *self;\r | |
115 | self = PyObject_New(compobject, type);\r | |
116 | if (self == NULL)\r | |
117 | return NULL;\r | |
118 | self->is_initialised = 0;\r | |
119 | self->unused_data = PyString_FromString("");\r | |
120 | if (self->unused_data == NULL) {\r | |
121 | Py_DECREF(self);\r | |
122 | return NULL;\r | |
123 | }\r | |
124 | self->unconsumed_tail = PyString_FromString("");\r | |
125 | if (self->unconsumed_tail == NULL) {\r | |
126 | Py_DECREF(self);\r | |
127 | return NULL;\r | |
128 | }\r | |
129 | return self;\r | |
130 | }\r | |
131 | \r | |
132 | PyDoc_STRVAR(compress__doc__,\r | |
133 | "compress(string[, level]) -- Returned compressed string.\n"\r | |
134 | "\n"\r | |
135 | "Optional arg level is the compression level, in 0-9.");\r | |
136 | \r | |
137 | static PyObject *\r | |
138 | PyZlib_compress(PyObject *self, PyObject *args)\r | |
139 | {\r | |
140 | PyObject *ReturnVal = NULL;\r | |
141 | Byte *input, *output;\r | |
142 | int length, level=Z_DEFAULT_COMPRESSION, err;\r | |
143 | z_stream zst;\r | |
144 | \r | |
145 | /* require Python string object, optional 'level' arg */\r | |
146 | if (!PyArg_ParseTuple(args, "s#|i:compress", &input, &length, &level))\r | |
147 | return NULL;\r | |
148 | \r | |
149 | zst.avail_out = length + length/1000 + 12 + 1;\r | |
150 | \r | |
151 | output = (Byte*)malloc(zst.avail_out);\r | |
152 | if (output == NULL) {\r | |
153 | PyErr_SetString(PyExc_MemoryError,\r | |
154 | "Can't allocate memory to compress data");\r | |
155 | return NULL;\r | |
156 | }\r | |
157 | \r | |
158 | /* Past the point of no return. From here on out, we need to make sure\r | |
159 | we clean up mallocs & INCREFs. */\r | |
160 | \r | |
161 | zst.zalloc = (alloc_func)NULL;\r | |
162 | zst.zfree = (free_func)Z_NULL;\r | |
163 | zst.next_out = (Byte *)output;\r | |
164 | zst.next_in = (Byte *)input;\r | |
165 | zst.avail_in = length;\r | |
166 | err = deflateInit(&zst, level);\r | |
167 | \r | |
168 | switch(err) {\r | |
169 | case(Z_OK):\r | |
170 | break;\r | |
171 | case(Z_MEM_ERROR):\r | |
172 | PyErr_SetString(PyExc_MemoryError,\r | |
173 | "Out of memory while compressing data");\r | |
174 | goto error;\r | |
175 | case(Z_STREAM_ERROR):\r | |
176 | PyErr_SetString(ZlibError,\r | |
177 | "Bad compression level");\r | |
178 | goto error;\r | |
179 | default:\r | |
180 | deflateEnd(&zst);\r | |
181 | zlib_error(zst, err, "while compressing data");\r | |
182 | goto error;\r | |
183 | }\r | |
184 | \r | |
185 | Py_BEGIN_ALLOW_THREADS;\r | |
186 | err = deflate(&zst, Z_FINISH);\r | |
187 | Py_END_ALLOW_THREADS;\r | |
188 | \r | |
189 | if (err != Z_STREAM_END) {\r | |
190 | zlib_error(zst, err, "while compressing data");\r | |
191 | deflateEnd(&zst);\r | |
192 | goto error;\r | |
193 | }\r | |
194 | \r | |
195 | err=deflateEnd(&zst);\r | |
196 | if (err == Z_OK)\r | |
197 | ReturnVal = PyString_FromStringAndSize((char *)output,\r | |
198 | zst.total_out);\r | |
199 | else\r | |
200 | zlib_error(zst, err, "while finishing compression");\r | |
201 | \r | |
202 | error:\r | |
203 | free(output);\r | |
204 | \r | |
205 | return ReturnVal;\r | |
206 | }\r | |
207 | \r | |
208 | PyDoc_STRVAR(decompress__doc__,\r | |
209 | "decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n"\r | |
210 | "\n"\r | |
211 | "Optional arg wbits is the window buffer size. Optional arg bufsize is\n"\r | |
212 | "the initial output buffer size.");\r | |
213 | \r | |
214 | static PyObject *\r | |
215 | PyZlib_decompress(PyObject *self, PyObject *args)\r | |
216 | {\r | |
217 | PyObject *result_str;\r | |
218 | Byte *input;\r | |
219 | int length, err;\r | |
220 | int wsize=DEF_WBITS;\r | |
221 | Py_ssize_t r_strlen=DEFAULTALLOC;\r | |
222 | z_stream zst;\r | |
223 | \r | |
224 | if (!PyArg_ParseTuple(args, "s#|in:decompress",\r | |
225 | &input, &length, &wsize, &r_strlen))\r | |
226 | return NULL;\r | |
227 | \r | |
228 | if (r_strlen <= 0)\r | |
229 | r_strlen = 1;\r | |
230 | \r | |
231 | zst.avail_in = length;\r | |
232 | zst.avail_out = r_strlen;\r | |
233 | \r | |
234 | if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen)))\r | |
235 | return NULL;\r | |
236 | \r | |
237 | zst.zalloc = (alloc_func)NULL;\r | |
238 | zst.zfree = (free_func)Z_NULL;\r | |
239 | zst.next_out = (Byte *)PyString_AS_STRING(result_str);\r | |
240 | zst.next_in = (Byte *)input;\r | |
241 | err = inflateInit2(&zst, wsize);\r | |
242 | \r | |
243 | switch(err) {\r | |
244 | case(Z_OK):\r | |
245 | break;\r | |
246 | case(Z_MEM_ERROR):\r | |
247 | PyErr_SetString(PyExc_MemoryError,\r | |
248 | "Out of memory while decompressing data");\r | |
249 | goto error;\r | |
250 | default:\r | |
251 | inflateEnd(&zst);\r | |
252 | zlib_error(zst, err, "while preparing to decompress data");\r | |
253 | goto error;\r | |
254 | }\r | |
255 | \r | |
256 | do {\r | |
257 | Py_BEGIN_ALLOW_THREADS\r | |
258 | err=inflate(&zst, Z_FINISH);\r | |
259 | Py_END_ALLOW_THREADS\r | |
260 | \r | |
261 | switch(err) {\r | |
262 | case(Z_STREAM_END):\r | |
263 | break;\r | |
264 | case(Z_BUF_ERROR):\r | |
265 | /*\r | |
266 | * If there is at least 1 byte of room according to zst.avail_out\r | |
267 | * and we get this error, assume that it means zlib cannot\r | |
268 | * process the inflate call() due to an error in the data.\r | |
269 | */\r | |
270 | if (zst.avail_out > 0) {\r | |
271 | zlib_error(zst, err, "while decompressing data");\r | |
272 | inflateEnd(&zst);\r | |
273 | goto error;\r | |
274 | }\r | |
275 | /* fall through */\r | |
276 | case(Z_OK):\r | |
277 | /* need more memory */\r | |
278 | if (_PyString_Resize(&result_str, r_strlen << 1) < 0) {\r | |
279 | inflateEnd(&zst);\r | |
280 | goto error;\r | |
281 | }\r | |
282 | zst.next_out = (unsigned char *)PyString_AS_STRING(result_str) \\r | |
283 | + r_strlen;\r | |
284 | zst.avail_out = r_strlen;\r | |
285 | r_strlen = r_strlen << 1;\r | |
286 | break;\r | |
287 | default:\r | |
288 | inflateEnd(&zst);\r | |
289 | zlib_error(zst, err, "while decompressing data");\r | |
290 | goto error;\r | |
291 | }\r | |
292 | } while (err != Z_STREAM_END);\r | |
293 | \r | |
294 | err = inflateEnd(&zst);\r | |
295 | if (err != Z_OK) {\r | |
296 | zlib_error(zst, err, "while finishing data decompression");\r | |
297 | goto error;\r | |
298 | }\r | |
299 | \r | |
300 | _PyString_Resize(&result_str, zst.total_out);\r | |
301 | return result_str;\r | |
302 | \r | |
303 | error:\r | |
304 | Py_XDECREF(result_str);\r | |
305 | return NULL;\r | |
306 | }\r | |
307 | \r | |
308 | static PyObject *\r | |
309 | PyZlib_compressobj(PyObject *selfptr, PyObject *args)\r | |
310 | {\r | |
311 | compobject *self;\r | |
312 | int level=Z_DEFAULT_COMPRESSION, method=DEFLATED;\r | |
313 | int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err;\r | |
314 | \r | |
315 | if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits,\r | |
316 | &memLevel, &strategy))\r | |
317 | return NULL;\r | |
318 | \r | |
319 | self = newcompobject(&Comptype);\r | |
320 | if (self==NULL)\r | |
321 | return(NULL);\r | |
322 | self->zst.zalloc = (alloc_func)NULL;\r | |
323 | self->zst.zfree = (free_func)Z_NULL;\r | |
324 | self->zst.next_in = NULL;\r | |
325 | self->zst.avail_in = 0;\r | |
326 | err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy);\r | |
327 | switch(err) {\r | |
328 | case (Z_OK):\r | |
329 | self->is_initialised = 1;\r | |
330 | return (PyObject*)self;\r | |
331 | case (Z_MEM_ERROR):\r | |
332 | Py_DECREF(self);\r | |
333 | PyErr_SetString(PyExc_MemoryError,\r | |
334 | "Can't allocate memory for compression object");\r | |
335 | return NULL;\r | |
336 | case(Z_STREAM_ERROR):\r | |
337 | Py_DECREF(self);\r | |
338 | PyErr_SetString(PyExc_ValueError, "Invalid initialization option");\r | |
339 | return NULL;\r | |
340 | default:\r | |
341 | zlib_error(self->zst, err, "while creating compression object");\r | |
342 | Py_DECREF(self);\r | |
343 | return NULL;\r | |
344 | }\r | |
345 | }\r | |
346 | \r | |
347 | static PyObject *\r | |
348 | PyZlib_decompressobj(PyObject *selfptr, PyObject *args)\r | |
349 | {\r | |
350 | int wbits=DEF_WBITS, err;\r | |
351 | compobject *self;\r | |
352 | if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits))\r | |
353 | return NULL;\r | |
354 | \r | |
355 | self = newcompobject(&Decomptype);\r | |
356 | if (self == NULL)\r | |
357 | return(NULL);\r | |
358 | self->zst.zalloc = (alloc_func)NULL;\r | |
359 | self->zst.zfree = (free_func)Z_NULL;\r | |
360 | self->zst.next_in = NULL;\r | |
361 | self->zst.avail_in = 0;\r | |
362 | err = inflateInit2(&self->zst, wbits);\r | |
363 | switch(err) {\r | |
364 | case (Z_OK):\r | |
365 | self->is_initialised = 1;\r | |
366 | return (PyObject*)self;\r | |
367 | case(Z_STREAM_ERROR):\r | |
368 | Py_DECREF(self);\r | |
369 | PyErr_SetString(PyExc_ValueError, "Invalid initialization option");\r | |
370 | return NULL;\r | |
371 | case (Z_MEM_ERROR):\r | |
372 | Py_DECREF(self);\r | |
373 | PyErr_SetString(PyExc_MemoryError,\r | |
374 | "Can't allocate memory for decompression object");\r | |
375 | return NULL;\r | |
376 | default:\r | |
377 | zlib_error(self->zst, err, "while creating decompression object");\r | |
378 | Py_DECREF(self);\r | |
379 | return NULL;\r | |
380 | }\r | |
381 | }\r | |
382 | \r | |
383 | static void\r | |
384 | Comp_dealloc(compobject *self)\r | |
385 | {\r | |
386 | if (self->is_initialised)\r | |
387 | deflateEnd(&self->zst);\r | |
388 | Py_XDECREF(self->unused_data);\r | |
389 | Py_XDECREF(self->unconsumed_tail);\r | |
390 | PyObject_Del(self);\r | |
391 | }\r | |
392 | \r | |
393 | static void\r | |
394 | Decomp_dealloc(compobject *self)\r | |
395 | {\r | |
396 | if (self->is_initialised)\r | |
397 | inflateEnd(&self->zst);\r | |
398 | Py_XDECREF(self->unused_data);\r | |
399 | Py_XDECREF(self->unconsumed_tail);\r | |
400 | PyObject_Del(self);\r | |
401 | }\r | |
402 | \r | |
403 | PyDoc_STRVAR(comp_compress__doc__,\r | |
404 | "compress(data) -- Return a string containing data compressed.\n"\r | |
405 | "\n"\r | |
406 | "After calling this function, some of the input data may still\n"\r | |
407 | "be stored in internal buffers for later processing.\n"\r | |
408 | "Call the flush() method to clear these buffers.");\r | |
409 | \r | |
410 | \r | |
411 | static PyObject *\r | |
412 | PyZlib_objcompress(compobject *self, PyObject *args)\r | |
413 | {\r | |
414 | int err, inplen;\r | |
415 | Py_ssize_t length = DEFAULTALLOC;\r | |
416 | PyObject *RetVal;\r | |
417 | Byte *input;\r | |
418 | unsigned long start_total_out;\r | |
419 | \r | |
420 | if (!PyArg_ParseTuple(args, "s#:compress", &input, &inplen))\r | |
421 | return NULL;\r | |
422 | \r | |
423 | if (!(RetVal = PyString_FromStringAndSize(NULL, length)))\r | |
424 | return NULL;\r | |
425 | \r | |
426 | ENTER_ZLIB\r | |
427 | \r | |
428 | start_total_out = self->zst.total_out;\r | |
429 | self->zst.avail_in = inplen;\r | |
430 | self->zst.next_in = input;\r | |
431 | self->zst.avail_out = length;\r | |
432 | self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);\r | |
433 | \r | |
434 | Py_BEGIN_ALLOW_THREADS\r | |
435 | err = deflate(&(self->zst), Z_NO_FLUSH);\r | |
436 | Py_END_ALLOW_THREADS\r | |
437 | \r | |
438 | /* while Z_OK and the output buffer is full, there might be more output,\r | |
439 | so extend the output buffer and try again */\r | |
440 | while (err == Z_OK && self->zst.avail_out == 0) {\r | |
441 | if (_PyString_Resize(&RetVal, length << 1) < 0)\r | |
442 | goto error;\r | |
443 | self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \\r | |
444 | + length;\r | |
445 | self->zst.avail_out = length;\r | |
446 | length = length << 1;\r | |
447 | \r | |
448 | Py_BEGIN_ALLOW_THREADS\r | |
449 | err = deflate(&(self->zst), Z_NO_FLUSH);\r | |
450 | Py_END_ALLOW_THREADS\r | |
451 | }\r | |
452 | /* We will only get Z_BUF_ERROR if the output buffer was full but\r | |
453 | there wasn't more output when we tried again, so it is not an error\r | |
454 | condition.\r | |
455 | */\r | |
456 | \r | |
457 | if (err != Z_OK && err != Z_BUF_ERROR) {\r | |
458 | zlib_error(self->zst, err, "while compressing");\r | |
459 | Py_DECREF(RetVal);\r | |
460 | RetVal = NULL;\r | |
461 | goto error;\r | |
462 | }\r | |
463 | _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);\r | |
464 | \r | |
465 | error:\r | |
466 | LEAVE_ZLIB\r | |
467 | return RetVal;\r | |
468 | }\r | |
469 | \r | |
470 | /* Helper for objdecompress() and unflush(). Saves any unconsumed input data in\r | |
471 | self->unused_data or self->unconsumed_tail, as appropriate. */\r | |
472 | static int\r | |
473 | save_unconsumed_input(compobject *self, int err)\r | |
474 | {\r | |
475 | if (err == Z_STREAM_END) {\r | |
476 | /* The end of the compressed data has been reached. Store the leftover\r | |
477 | input data in self->unused_data. */\r | |
478 | if (self->zst.avail_in > 0) {\r | |
479 | Py_ssize_t old_size = PyString_GET_SIZE(self->unused_data);\r | |
480 | Py_ssize_t new_size;\r | |
481 | PyObject *new_data;\r | |
482 | if (self->zst.avail_in > PY_SSIZE_T_MAX - old_size) {\r | |
483 | PyErr_NoMemory();\r | |
484 | return -1;\r | |
485 | }\r | |
486 | new_size = old_size + self->zst.avail_in;\r | |
487 | new_data = PyString_FromStringAndSize(NULL, new_size);\r | |
488 | if (new_data == NULL)\r | |
489 | return -1;\r | |
490 | Py_MEMCPY(PyString_AS_STRING(new_data),\r | |
491 | PyString_AS_STRING(self->unused_data), old_size);\r | |
492 | Py_MEMCPY(PyString_AS_STRING(new_data) + old_size,\r | |
493 | self->zst.next_in, self->zst.avail_in);\r | |
494 | Py_DECREF(self->unused_data);\r | |
495 | self->unused_data = new_data;\r | |
496 | self->zst.avail_in = 0;\r | |
497 | }\r | |
498 | }\r | |
499 | if (self->zst.avail_in > 0 || PyString_GET_SIZE(self->unconsumed_tail)) {\r | |
500 | /* This code handles two distinct cases:\r | |
501 | 1. Output limit was reached. Save leftover input in unconsumed_tail.\r | |
502 | 2. All input data was consumed. Clear unconsumed_tail. */\r | |
503 | PyObject *new_data = PyString_FromStringAndSize(\r | |
504 | (char *)self->zst.next_in, self->zst.avail_in);\r | |
505 | if (new_data == NULL)\r | |
506 | return -1;\r | |
507 | Py_DECREF(self->unconsumed_tail);\r | |
508 | self->unconsumed_tail = new_data;\r | |
509 | }\r | |
510 | return 0;\r | |
511 | }\r | |
512 | \r | |
513 | PyDoc_STRVAR(decomp_decompress__doc__,\r | |
514 | "decompress(data, max_length) -- Return a string containing the decompressed\n"\r | |
515 | "version of the data.\n"\r | |
516 | "\n"\r | |
517 | "After calling this function, some of the input data may still be stored in\n"\r | |
518 | "internal buffers for later processing.\n"\r | |
519 | "Call the flush() method to clear these buffers.\n"\r | |
520 | "If the max_length parameter is specified then the return value will be\n"\r | |
521 | "no longer than max_length. Unconsumed input data will be stored in\n"\r | |
522 | "the unconsumed_tail attribute.");\r | |
523 | \r | |
524 | static PyObject *\r | |
525 | PyZlib_objdecompress(compobject *self, PyObject *args)\r | |
526 | {\r | |
527 | int err, inplen, max_length = 0;\r | |
528 | Py_ssize_t old_length, length = DEFAULTALLOC;\r | |
529 | PyObject *RetVal;\r | |
530 | Byte *input;\r | |
531 | unsigned long start_total_out;\r | |
532 | \r | |
533 | if (!PyArg_ParseTuple(args, "s#|i:decompress", &input,\r | |
534 | &inplen, &max_length))\r | |
535 | return NULL;\r | |
536 | if (max_length < 0) {\r | |
537 | PyErr_SetString(PyExc_ValueError,\r | |
538 | "max_length must be greater than zero");\r | |
539 | return NULL;\r | |
540 | }\r | |
541 | \r | |
542 | /* limit amount of data allocated to max_length */\r | |
543 | if (max_length && length > max_length)\r | |
544 | length = max_length;\r | |
545 | if (!(RetVal = PyString_FromStringAndSize(NULL, length)))\r | |
546 | return NULL;\r | |
547 | \r | |
548 | ENTER_ZLIB\r | |
549 | \r | |
550 | start_total_out = self->zst.total_out;\r | |
551 | self->zst.avail_in = inplen;\r | |
552 | self->zst.next_in = input;\r | |
553 | self->zst.avail_out = length;\r | |
554 | self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);\r | |
555 | \r | |
556 | Py_BEGIN_ALLOW_THREADS\r | |
557 | err = inflate(&(self->zst), Z_SYNC_FLUSH);\r | |
558 | Py_END_ALLOW_THREADS\r | |
559 | \r | |
560 | /* While Z_OK and the output buffer is full, there might be more output.\r | |
561 | So extend the output buffer and try again.\r | |
562 | */\r | |
563 | while (err == Z_OK && self->zst.avail_out == 0) {\r | |
564 | /* If max_length set, don't continue decompressing if we've already\r | |
565 | reached the limit.\r | |
566 | */\r | |
567 | if (max_length && length >= max_length)\r | |
568 | break;\r | |
569 | \r | |
570 | /* otherwise, ... */\r | |
571 | old_length = length;\r | |
572 | length = length << 1;\r | |
573 | if (max_length && length > max_length)\r | |
574 | length = max_length;\r | |
575 | \r | |
576 | if (_PyString_Resize(&RetVal, length) < 0)\r | |
577 | goto error;\r | |
578 | self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \\r | |
579 | + old_length;\r | |
580 | self->zst.avail_out = length - old_length;\r | |
581 | \r | |
582 | Py_BEGIN_ALLOW_THREADS\r | |
583 | err = inflate(&(self->zst), Z_SYNC_FLUSH);\r | |
584 | Py_END_ALLOW_THREADS\r | |
585 | }\r | |
586 | \r | |
587 | if (save_unconsumed_input(self, err) < 0) {\r | |
588 | Py_DECREF(RetVal);\r | |
589 | RetVal = NULL;\r | |
590 | goto error;\r | |
591 | }\r | |
592 | \r | |
593 | /* This is the logical place to call inflateEnd, but the old behaviour of\r | |
594 | only calling it on flush() is preserved. */\r | |
595 | \r | |
596 | if (err != Z_STREAM_END && err != Z_OK && err != Z_BUF_ERROR) {\r | |
597 | /* We will only get Z_BUF_ERROR if the output buffer was full\r | |
598 | but there wasn't more output when we tried again, so it is\r | |
599 | not an error condition.\r | |
600 | */\r | |
601 | zlib_error(self->zst, err, "while decompressing");\r | |
602 | Py_DECREF(RetVal);\r | |
603 | RetVal = NULL;\r | |
604 | goto error;\r | |
605 | }\r | |
606 | \r | |
607 | _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);\r | |
608 | \r | |
609 | error:\r | |
610 | LEAVE_ZLIB\r | |
611 | \r | |
612 | return RetVal;\r | |
613 | }\r | |
614 | \r | |
615 | PyDoc_STRVAR(comp_flush__doc__,\r | |
616 | "flush( [mode] ) -- Return a string containing any remaining compressed data.\n"\r | |
617 | "\n"\r | |
618 | "mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n"\r | |
619 | "default value used when mode is not specified is Z_FINISH.\n"\r | |
620 | "If mode == Z_FINISH, the compressor object can no longer be used after\n"\r | |
621 | "calling the flush() method. Otherwise, more data can still be compressed.");\r | |
622 | \r | |
623 | static PyObject *\r | |
624 | PyZlib_flush(compobject *self, PyObject *args)\r | |
625 | {\r | |
626 | int err, length = DEFAULTALLOC;\r | |
627 | PyObject *RetVal;\r | |
628 | int flushmode = Z_FINISH;\r | |
629 | unsigned long start_total_out;\r | |
630 | \r | |
631 | if (!PyArg_ParseTuple(args, "|i:flush", &flushmode))\r | |
632 | return NULL;\r | |
633 | \r | |
634 | /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in\r | |
635 | doing any work at all; just return an empty string. */\r | |
636 | if (flushmode == Z_NO_FLUSH) {\r | |
637 | return PyString_FromStringAndSize(NULL, 0);\r | |
638 | }\r | |
639 | \r | |
640 | if (!(RetVal = PyString_FromStringAndSize(NULL, length)))\r | |
641 | return NULL;\r | |
642 | \r | |
643 | ENTER_ZLIB\r | |
644 | \r | |
645 | start_total_out = self->zst.total_out;\r | |
646 | self->zst.avail_in = 0;\r | |
647 | self->zst.avail_out = length;\r | |
648 | self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal);\r | |
649 | \r | |
650 | Py_BEGIN_ALLOW_THREADS\r | |
651 | err = deflate(&(self->zst), flushmode);\r | |
652 | Py_END_ALLOW_THREADS\r | |
653 | \r | |
654 | /* while Z_OK and the output buffer is full, there might be more output,\r | |
655 | so extend the output buffer and try again */\r | |
656 | while (err == Z_OK && self->zst.avail_out == 0) {\r | |
657 | if (_PyString_Resize(&RetVal, length << 1) < 0)\r | |
658 | goto error;\r | |
659 | self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \\r | |
660 | + length;\r | |
661 | self->zst.avail_out = length;\r | |
662 | length = length << 1;\r | |
663 | \r | |
664 | Py_BEGIN_ALLOW_THREADS\r | |
665 | err = deflate(&(self->zst), flushmode);\r | |
666 | Py_END_ALLOW_THREADS\r | |
667 | }\r | |
668 | \r | |
669 | /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free\r | |
670 | various data structures. Note we should only get Z_STREAM_END when\r | |
671 | flushmode is Z_FINISH, but checking both for safety*/\r | |
672 | if (err == Z_STREAM_END && flushmode == Z_FINISH) {\r | |
673 | err = deflateEnd(&(self->zst));\r | |
674 | if (err != Z_OK) {\r | |
675 | zlib_error(self->zst, err, "from deflateEnd()");\r | |
676 | Py_DECREF(RetVal);\r | |
677 | RetVal = NULL;\r | |
678 | goto error;\r | |
679 | }\r | |
680 | else\r | |
681 | self->is_initialised = 0;\r | |
682 | \r | |
683 | /* We will only get Z_BUF_ERROR if the output buffer was full\r | |
684 | but there wasn't more output when we tried again, so it is\r | |
685 | not an error condition.\r | |
686 | */\r | |
687 | } else if (err!=Z_OK && err!=Z_BUF_ERROR) {\r | |
688 | zlib_error(self->zst, err, "while flushing");\r | |
689 | Py_DECREF(RetVal);\r | |
690 | RetVal = NULL;\r | |
691 | goto error;\r | |
692 | }\r | |
693 | \r | |
694 | _PyString_Resize(&RetVal, self->zst.total_out - start_total_out);\r | |
695 | \r | |
696 | error:\r | |
697 | LEAVE_ZLIB\r | |
698 | \r | |
699 | return RetVal;\r | |
700 | }\r | |
701 | \r | |
702 | #ifdef HAVE_ZLIB_COPY\r | |
703 | PyDoc_STRVAR(comp_copy__doc__,\r | |
704 | "copy() -- Return a copy of the compression object.");\r | |
705 | \r | |
706 | static PyObject *\r | |
707 | PyZlib_copy(compobject *self)\r | |
708 | {\r | |
709 | compobject *retval = NULL;\r | |
710 | int err;\r | |
711 | \r | |
712 | retval = newcompobject(&Comptype);\r | |
713 | if (!retval) return NULL;\r | |
714 | \r | |
715 | /* Copy the zstream state\r | |
716 | * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe\r | |
717 | */\r | |
718 | ENTER_ZLIB\r | |
719 | err = deflateCopy(&retval->zst, &self->zst);\r | |
720 | switch(err) {\r | |
721 | case(Z_OK):\r | |
722 | break;\r | |
723 | case(Z_STREAM_ERROR):\r | |
724 | PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");\r | |
725 | goto error;\r | |
726 | case(Z_MEM_ERROR):\r | |
727 | PyErr_SetString(PyExc_MemoryError,\r | |
728 | "Can't allocate memory for compression object");\r | |
729 | goto error;\r | |
730 | default:\r | |
731 | zlib_error(self->zst, err, "while copying compression object");\r | |
732 | goto error;\r | |
733 | }\r | |
734 | \r | |
735 | Py_INCREF(self->unused_data);\r | |
736 | Py_INCREF(self->unconsumed_tail);\r | |
737 | Py_XDECREF(retval->unused_data);\r | |
738 | Py_XDECREF(retval->unconsumed_tail);\r | |
739 | retval->unused_data = self->unused_data;\r | |
740 | retval->unconsumed_tail = self->unconsumed_tail;\r | |
741 | \r | |
742 | /* Mark it as being initialized */\r | |
743 | retval->is_initialised = 1;\r | |
744 | \r | |
745 | LEAVE_ZLIB\r | |
746 | return (PyObject *)retval;\r | |
747 | \r | |
748 | error:\r | |
749 | LEAVE_ZLIB\r | |
750 | Py_XDECREF(retval);\r | |
751 | return NULL;\r | |
752 | }\r | |
753 | \r | |
754 | PyDoc_STRVAR(decomp_copy__doc__,\r | |
755 | "copy() -- Return a copy of the decompression object.");\r | |
756 | \r | |
757 | static PyObject *\r | |
758 | PyZlib_uncopy(compobject *self)\r | |
759 | {\r | |
760 | compobject *retval = NULL;\r | |
761 | int err;\r | |
762 | \r | |
763 | retval = newcompobject(&Decomptype);\r | |
764 | if (!retval) return NULL;\r | |
765 | \r | |
766 | /* Copy the zstream state\r | |
767 | * We use ENTER_ZLIB / LEAVE_ZLIB to make this thread-safe\r | |
768 | */\r | |
769 | ENTER_ZLIB\r | |
770 | err = inflateCopy(&retval->zst, &self->zst);\r | |
771 | switch(err) {\r | |
772 | case(Z_OK):\r | |
773 | break;\r | |
774 | case(Z_STREAM_ERROR):\r | |
775 | PyErr_SetString(PyExc_ValueError, "Inconsistent stream state");\r | |
776 | goto error;\r | |
777 | case(Z_MEM_ERROR):\r | |
778 | PyErr_SetString(PyExc_MemoryError,\r | |
779 | "Can't allocate memory for decompression object");\r | |
780 | goto error;\r | |
781 | default:\r | |
782 | zlib_error(self->zst, err, "while copying decompression object");\r | |
783 | goto error;\r | |
784 | }\r | |
785 | \r | |
786 | Py_INCREF(self->unused_data);\r | |
787 | Py_INCREF(self->unconsumed_tail);\r | |
788 | Py_XDECREF(retval->unused_data);\r | |
789 | Py_XDECREF(retval->unconsumed_tail);\r | |
790 | retval->unused_data = self->unused_data;\r | |
791 | retval->unconsumed_tail = self->unconsumed_tail;\r | |
792 | \r | |
793 | /* Mark it as being initialized */\r | |
794 | retval->is_initialised = 1;\r | |
795 | \r | |
796 | LEAVE_ZLIB\r | |
797 | return (PyObject *)retval;\r | |
798 | \r | |
799 | error:\r | |
800 | LEAVE_ZLIB\r | |
801 | Py_XDECREF(retval);\r | |
802 | return NULL;\r | |
803 | }\r | |
804 | #endif\r | |
805 | \r | |
806 | PyDoc_STRVAR(decomp_flush__doc__,\r | |
807 | "flush( [length] ) -- Return a string containing any remaining\n"\r | |
808 | "decompressed data. length, if given, is the initial size of the\n"\r | |
809 | "output buffer.\n"\r | |
810 | "\n"\r | |
811 | "The decompressor object can no longer be used after this call.");\r | |
812 | \r | |
813 | static PyObject *\r | |
814 | PyZlib_unflush(compobject *self, PyObject *args)\r | |
815 | {\r | |
816 | int err, length = DEFAULTALLOC;\r | |
817 | PyObject * retval = NULL;\r | |
818 | unsigned long start_total_out;\r | |
819 | \r | |
820 | if (!PyArg_ParseTuple(args, "|i:flush", &length))\r | |
821 | return NULL;\r | |
822 | if (length <= 0) {\r | |
823 | PyErr_SetString(PyExc_ValueError, "length must be greater than zero");\r | |
824 | return NULL;\r | |
825 | }\r | |
826 | if (!(retval = PyString_FromStringAndSize(NULL, length)))\r | |
827 | return NULL;\r | |
828 | \r | |
829 | \r | |
830 | ENTER_ZLIB\r | |
831 | \r | |
832 | start_total_out = self->zst.total_out;\r | |
833 | self->zst.avail_in = PyString_GET_SIZE(self->unconsumed_tail);\r | |
834 | self->zst.next_in = (Byte *)PyString_AS_STRING(self->unconsumed_tail);\r | |
835 | self->zst.avail_out = length;\r | |
836 | self->zst.next_out = (Byte *)PyString_AS_STRING(retval);\r | |
837 | \r | |
838 | Py_BEGIN_ALLOW_THREADS\r | |
839 | err = inflate(&(self->zst), Z_FINISH);\r | |
840 | Py_END_ALLOW_THREADS\r | |
841 | \r | |
842 | /* while Z_OK and the output buffer is full, there might be more output,\r | |
843 | so extend the output buffer and try again */\r | |
844 | while ((err == Z_OK || err == Z_BUF_ERROR) && self->zst.avail_out == 0) {\r | |
845 | if (_PyString_Resize(&retval, length << 1) < 0)\r | |
846 | goto error;\r | |
847 | self->zst.next_out = (Byte *)PyString_AS_STRING(retval) + length;\r | |
848 | self->zst.avail_out = length;\r | |
849 | length = length << 1;\r | |
850 | \r | |
851 | Py_BEGIN_ALLOW_THREADS\r | |
852 | err = inflate(&(self->zst), Z_FINISH);\r | |
853 | Py_END_ALLOW_THREADS\r | |
854 | }\r | |
855 | \r | |
856 | if (save_unconsumed_input(self, err) < 0) {\r | |
857 | Py_DECREF(retval);\r | |
858 | retval = NULL;\r | |
859 | goto error;\r | |
860 | }\r | |
861 | \r | |
862 | /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free\r | |
863 | various data structures. Note we should only get Z_STREAM_END when\r | |
864 | flushmode is Z_FINISH */\r | |
865 | if (err == Z_STREAM_END) {\r | |
866 | err = inflateEnd(&(self->zst));\r | |
867 | self->is_initialised = 0;\r | |
868 | if (err != Z_OK) {\r | |
869 | zlib_error(self->zst, err, "from inflateEnd()");\r | |
870 | Py_DECREF(retval);\r | |
871 | retval = NULL;\r | |
872 | goto error;\r | |
873 | }\r | |
874 | }\r | |
875 | \r | |
876 | _PyString_Resize(&retval, self->zst.total_out - start_total_out);\r | |
877 | \r | |
878 | error:\r | |
879 | \r | |
880 | LEAVE_ZLIB\r | |
881 | \r | |
882 | return retval;\r | |
883 | }\r | |
884 | \r | |
885 | static PyMethodDef comp_methods[] =\r | |
886 | {\r | |
887 | {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS,\r | |
888 | comp_compress__doc__},\r | |
889 | {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS,\r | |
890 | comp_flush__doc__},\r | |
891 | #ifdef HAVE_ZLIB_COPY\r | |
892 | {"copy", (PyCFunction)PyZlib_copy, METH_NOARGS,\r | |
893 | comp_copy__doc__},\r | |
894 | #endif\r | |
895 | {NULL, NULL}\r | |
896 | };\r | |
897 | \r | |
898 | static PyMethodDef Decomp_methods[] =\r | |
899 | {\r | |
900 | {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS,\r | |
901 | decomp_decompress__doc__},\r | |
902 | {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS,\r | |
903 | decomp_flush__doc__},\r | |
904 | #ifdef HAVE_ZLIB_COPY\r | |
905 | {"copy", (PyCFunction)PyZlib_uncopy, METH_NOARGS,\r | |
906 | decomp_copy__doc__},\r | |
907 | #endif\r | |
908 | {NULL, NULL}\r | |
909 | };\r | |
910 | \r | |
911 | static PyObject *\r | |
912 | Comp_getattr(compobject *self, char *name)\r | |
913 | {\r | |
914 | /* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch\r | |
915 | internal data. */\r | |
916 | \r | |
917 | return Py_FindMethod(comp_methods, (PyObject *)self, name);\r | |
918 | }\r | |
919 | \r | |
920 | static PyObject *\r | |
921 | Decomp_getattr(compobject *self, char *name)\r | |
922 | {\r | |
923 | PyObject * retval;\r | |
924 | \r | |
925 | ENTER_ZLIB\r | |
926 | \r | |
927 | if (strcmp(name, "unused_data") == 0) {\r | |
928 | Py_INCREF(self->unused_data);\r | |
929 | retval = self->unused_data;\r | |
930 | } else if (strcmp(name, "unconsumed_tail") == 0) {\r | |
931 | Py_INCREF(self->unconsumed_tail);\r | |
932 | retval = self->unconsumed_tail;\r | |
933 | } else\r | |
934 | retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name);\r | |
935 | \r | |
936 | LEAVE_ZLIB\r | |
937 | \r | |
938 | return retval;\r | |
939 | }\r | |
940 | \r | |
941 | PyDoc_STRVAR(adler32__doc__,\r | |
942 | "adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n"\r | |
943 | "\n"\r | |
944 | "An optional starting value can be specified. The returned checksum is\n"\r | |
945 | "a signed integer.");\r | |
946 | \r | |
947 | static PyObject *\r | |
948 | PyZlib_adler32(PyObject *self, PyObject *args)\r | |
949 | {\r | |
950 | unsigned int adler32val = 1; /* adler32(0L, Z_NULL, 0) */\r | |
951 | Byte *buf;\r | |
952 | int len, signed_val;\r | |
953 | \r | |
954 | if (!PyArg_ParseTuple(args, "s#|I:adler32", &buf, &len, &adler32val))\r | |
955 | return NULL;\r | |
956 | /* In Python 2.x we return a signed integer regardless of native platform\r | |
957 | * long size (the 32bit unsigned long is treated as 32-bit signed and sign\r | |
958 | * extended into a 64-bit long inside the integer object). 3.0 does the\r | |
959 | * right thing and returns unsigned. http://bugs.python.org/issue1202 */\r | |
960 | signed_val = adler32(adler32val, buf, len);\r | |
961 | return PyInt_FromLong(signed_val);\r | |
962 | }\r | |
963 | \r | |
964 | PyDoc_STRVAR(crc32__doc__,\r | |
965 | "crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n"\r | |
966 | "\n"\r | |
967 | "An optional starting value can be specified. The returned checksum is\n"\r | |
968 | "a signed integer.");\r | |
969 | \r | |
970 | static PyObject *\r | |
971 | PyZlib_crc32(PyObject *self, PyObject *args)\r | |
972 | {\r | |
973 | unsigned int crc32val = 0; /* crc32(0L, Z_NULL, 0) */\r | |
974 | Byte *buf;\r | |
975 | int len, signed_val;\r | |
976 | \r | |
977 | if (!PyArg_ParseTuple(args, "s#|I:crc32", &buf, &len, &crc32val))\r | |
978 | return NULL;\r | |
979 | /* In Python 2.x we return a signed integer regardless of native platform\r | |
980 | * long size (the 32bit unsigned long is treated as 32-bit signed and sign\r | |
981 | * extended into a 64-bit long inside the integer object). 3.0 does the\r | |
982 | * right thing and returns unsigned. http://bugs.python.org/issue1202 */\r | |
983 | signed_val = crc32(crc32val, buf, len);\r | |
984 | return PyInt_FromLong(signed_val);\r | |
985 | }\r | |
986 | \r | |
987 | \r | |
988 | static PyMethodDef zlib_methods[] =\r | |
989 | {\r | |
990 | {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS,\r | |
991 | adler32__doc__},\r | |
992 | {"compress", (PyCFunction)PyZlib_compress, METH_VARARGS,\r | |
993 | compress__doc__},\r | |
994 | {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS,\r | |
995 | compressobj__doc__},\r | |
996 | {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS,\r | |
997 | crc32__doc__},\r | |
998 | {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS,\r | |
999 | decompress__doc__},\r | |
1000 | {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS,\r | |
1001 | decompressobj__doc__},\r | |
1002 | {NULL, NULL}\r | |
1003 | };\r | |
1004 | \r | |
1005 | static PyTypeObject Comptype = {\r | |
1006 | PyVarObject_HEAD_INIT(0, 0)\r | |
1007 | "zlib.Compress",\r | |
1008 | sizeof(compobject),\r | |
1009 | 0,\r | |
1010 | (destructor)Comp_dealloc, /*tp_dealloc*/\r | |
1011 | 0, /*tp_print*/\r | |
1012 | (getattrfunc)Comp_getattr, /*tp_getattr*/\r | |
1013 | 0, /*tp_setattr*/\r | |
1014 | 0, /*tp_compare*/\r | |
1015 | 0, /*tp_repr*/\r | |
1016 | 0, /*tp_as_number*/\r | |
1017 | 0, /*tp_as_sequence*/\r | |
1018 | 0, /*tp_as_mapping*/\r | |
1019 | };\r | |
1020 | \r | |
1021 | static PyTypeObject Decomptype = {\r | |
1022 | PyVarObject_HEAD_INIT(0, 0)\r | |
1023 | "zlib.Decompress",\r | |
1024 | sizeof(compobject),\r | |
1025 | 0,\r | |
1026 | (destructor)Decomp_dealloc, /*tp_dealloc*/\r | |
1027 | 0, /*tp_print*/\r | |
1028 | (getattrfunc)Decomp_getattr, /*tp_getattr*/\r | |
1029 | 0, /*tp_setattr*/\r | |
1030 | 0, /*tp_compare*/\r | |
1031 | 0, /*tp_repr*/\r | |
1032 | 0, /*tp_as_number*/\r | |
1033 | 0, /*tp_as_sequence*/\r | |
1034 | 0, /*tp_as_mapping*/\r | |
1035 | };\r | |
1036 | \r | |
1037 | PyDoc_STRVAR(zlib_module_documentation,\r | |
1038 | "The functions in this module allow compression and decompression using the\n"\r | |
1039 | "zlib library, which is based on GNU zip.\n"\r | |
1040 | "\n"\r | |
1041 | "adler32(string[, start]) -- Compute an Adler-32 checksum.\n"\r | |
1042 | "compress(string[, level]) -- Compress string, with compression level in 0-9.\n"\r | |
1043 | "compressobj([level]) -- Return a compressor object.\n"\r | |
1044 | "crc32(string[, start]) -- Compute a CRC-32 checksum.\n"\r | |
1045 | "decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n"\r | |
1046 | "decompressobj([wbits]) -- Return a decompressor object.\n"\r | |
1047 | "\n"\r | |
1048 | "'wbits' is window buffer size.\n"\r | |
1049 | "Compressor objects support compress() and flush() methods; decompressor\n"\r | |
1050 | "objects support decompress() and flush().");\r | |
1051 | \r | |
1052 | PyMODINIT_FUNC\r | |
1053 | PyInit_zlib(void)\r | |
1054 | {\r | |
1055 | PyObject *m, *ver;\r | |
1056 | Py_TYPE(&Comptype) = &PyType_Type;\r | |
1057 | Py_TYPE(&Decomptype) = &PyType_Type;\r | |
1058 | m = Py_InitModule4("zlib", zlib_methods,\r | |
1059 | zlib_module_documentation,\r | |
1060 | (PyObject*)NULL,PYTHON_API_VERSION);\r | |
1061 | if (m == NULL)\r | |
1062 | return;\r | |
1063 | \r | |
1064 | ZlibError = PyErr_NewException("zlib.error", NULL, NULL);\r | |
1065 | if (ZlibError != NULL) {\r | |
1066 | Py_INCREF(ZlibError);\r | |
1067 | PyModule_AddObject(m, "error", ZlibError);\r | |
1068 | }\r | |
1069 | PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS);\r | |
1070 | PyModule_AddIntConstant(m, "DEFLATED", DEFLATED);\r | |
1071 | PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL);\r | |
1072 | PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED);\r | |
1073 | PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION);\r | |
1074 | PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION);\r | |
1075 | PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED);\r | |
1076 | PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY);\r | |
1077 | PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY);\r | |
1078 | \r | |
1079 | PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH);\r | |
1080 | PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH);\r | |
1081 | PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH);\r | |
1082 | PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH);\r | |
1083 | \r | |
1084 | ver = PyString_FromString(ZLIB_VERSION);\r | |
1085 | if (ver != NULL)\r | |
1086 | PyModule_AddObject(m, "ZLIB_VERSION", ver);\r | |
1087 | \r | |
1088 | PyModule_AddStringConstant(m, "__version__", "1.0");\r | |
1089 | \r | |
1090 | #ifdef WITH_THREAD\r | |
1091 | zlib_lock = PyThread_allocate_lock();\r | |
1092 | #endif /* WITH_THREAD */\r | |
1093 | }\r |