| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | /* zlibmodule.c -- gzip-compatible data compression */ | 
					
						
							| 
									
										
										
										
											2001-10-09 10:54:31 +00:00
										 |  |  | /* See http://www.gzip.org/zlib/ */ | 
					
						
							| 
									
										
										
										
											2001-01-31 10:28:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-01-31 19:39:44 +00:00
										 |  |  | /* Windows users:  read Python's PCbuild\readme.txt */ | 
					
						
							| 
									
										
										
										
											2001-01-31 10:28:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-06-03 22:21:47 +00:00
										 |  |  | #include "Python.h"
 | 
					
						
							|  |  |  | #include "zlib.h"
 | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | #ifdef WITH_THREAD
 | 
					
						
							|  |  |  | #include "pythread.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* #defs ripped off from _tkinter.c, even though the situation here is much
 | 
					
						
							|  |  |  |    simpler, because we don't have to worry about waiting for Tcl | 
					
						
							|  |  |  |    events!  And, since zlib itself is threadsafe, we don't need to worry | 
					
						
							|  |  |  |    about re-entering zlib functions. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    N.B. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |    Since ENTER_ZLIB and LEAVE_ZLIB only need to be called on functions | 
					
						
							|  |  |  |    that modify the components of preexisting de/compress objects, it | 
					
						
							|  |  |  |    could prove to be a performance gain on multiprocessor machines if | 
					
						
							|  |  |  |    there was an de/compress object-specific lock.  However, for the | 
					
						
							|  |  |  |    moment the ENTER_ZLIB and LEAVE_ZLIB calls are global for ALL | 
					
						
							|  |  |  |    de/compress objects. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyThread_type_lock zlib_lock = NULL; /* initialized on module load */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define ENTER_ZLIB \
 | 
					
						
							| 
									
										
										
										
											2001-10-17 03:43:54 +00:00
										 |  |  | 	Py_BEGIN_ALLOW_THREADS \ | 
					
						
							|  |  |  | 	PyThread_acquire_lock(zlib_lock, 1); \ | 
					
						
							|  |  |  | 	Py_END_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #define LEAVE_ZLIB \
 | 
					
						
							| 
									
										
										
										
											2001-10-17 03:43:54 +00:00
										 |  |  | 	PyThread_release_lock(zlib_lock); | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #define ENTER_ZLIB
 | 
					
						
							|  |  |  | #define LEAVE_ZLIB
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | /* The following parameters are copied from zutil.h, version 0.95 */ | 
					
						
							|  |  |  | #define DEFLATED   8
 | 
					
						
							|  |  |  | #if MAX_MEM_LEVEL >= 8
 | 
					
						
							|  |  |  | #  define DEF_MEM_LEVEL 8
 | 
					
						
							|  |  |  | #else
 | 
					
						
							|  |  |  | #  define DEF_MEM_LEVEL  MAX_MEM_LEVEL
 | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | #define DEF_WBITS MAX_WBITS
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1999-04-07 20:23:17 +00:00
										 |  |  | /* The output buffer will be increased in chunks of DEFAULTALLOC bytes. */ | 
					
						
							|  |  |  | #define DEFAULTALLOC (16*1024)
 | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | #define PyInit_zlib initzlib
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | staticforward PyTypeObject Comptype; | 
					
						
							|  |  |  | staticforward PyTypeObject Decomptype; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject *ZlibError; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | typedef struct | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     PyObject_HEAD | 
					
						
							|  |  |  |     z_stream zst; | 
					
						
							|  |  |  |     PyObject *unused_data; | 
					
						
							|  |  |  |     PyObject *unconsumed_tail; | 
					
						
							|  |  |  |     int is_initialised; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | } compobject; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:56:09 +00:00
										 |  |  | static void | 
					
						
							|  |  |  | zlib_error(z_stream zst, int err, char *msg) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     if (zst.msg == Z_NULL) | 
					
						
							|  |  |  | 	PyErr_Format(ZlibError, "Error %d %s", err, msg); | 
					
						
							|  |  |  |     else | 
					
						
							|  |  |  | 	PyErr_Format(ZlibError, "Error %d %s: %.200s", err, msg, zst.msg); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | static char compressobj__doc__[] = | 
					
						
							| 
									
										
										
										
											2001-10-17 04:16:15 +00:00
										 |  |  | "compressobj([level]) -- Return a compressor object.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "Optional arg level is the compression level, in 1-9."; | 
					
						
							| 
									
										
										
										
											1997-06-03 22:21:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | static char decompressobj__doc__[] = | 
					
						
							| 
									
										
										
										
											2001-10-17 04:16:15 +00:00
										 |  |  | "decompressobj([wbits]) -- Return a decompressor object.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "Optional arg wbits is the window buffer size."; | 
					
						
							| 
									
										
										
										
											1997-06-03 22:21:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | static compobject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | newcompobject(PyTypeObject *type) | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     compobject *self; | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     self = PyObject_New(compobject, type); | 
					
						
							|  |  |  |     if (self == NULL) | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							|  |  |  |     self->is_initialised = 0; | 
					
						
							|  |  |  |     self->unused_data = PyString_FromString(""); | 
					
						
							|  |  |  |     if (self->unused_data == NULL) { | 
					
						
							|  |  |  | 	Py_DECREF(self); | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     self->unconsumed_tail = PyString_FromString(""); | 
					
						
							|  |  |  |     if (self->unconsumed_tail == NULL) { | 
					
						
							|  |  |  | 	Py_DECREF(self); | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  |     return self; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | static char compress__doc__[] = | 
					
						
							| 
									
										
										
										
											2001-10-17 04:16:15 +00:00
										 |  |  | "compress(string[, level]) -- Returned compressed string.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "Optional arg level is the compression level, in 1-9."; | 
					
						
							| 
									
										
										
										
											1997-06-03 22:21:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | PyZlib_compress(PyObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     PyObject *ReturnVal = NULL; | 
					
						
							|  |  |  |     Byte *input, *output; | 
					
						
							|  |  |  |     int length, level=Z_DEFAULT_COMPRESSION, err; | 
					
						
							|  |  |  |     z_stream zst; | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     /* require Python string object, optional 'level' arg */ | 
					
						
							| 
									
										
										
										
											2001-10-16 23:26:08 +00:00
										 |  |  |     if (!PyArg_ParseTuple(args, "s#|i:compress", &input, &length, &level)) | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     zst.avail_out = length + length/1000 + 12 + 1; | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  |     output = (Byte*)malloc(zst.avail_out); | 
					
						
							|  |  |  |     if (output == NULL) { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	PyErr_SetString(PyExc_MemoryError, | 
					
						
							|  |  |  | 			"Can't allocate memory to compress data"); | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											1998-12-18 22:13:11 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     /* Past the point of no return.  From here on out, we need to make sure
 | 
					
						
							|  |  |  |        we clean up mallocs & INCREFs. */ | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  |     zst.zalloc = (alloc_func)NULL; | 
					
						
							|  |  |  |     zst.zfree = (free_func)Z_NULL; | 
					
						
							|  |  |  |     zst.next_out = (Byte *)output; | 
					
						
							|  |  |  |     zst.next_in = (Byte *)input; | 
					
						
							|  |  |  |     zst.avail_in = length; | 
					
						
							|  |  |  |     err = deflateInit(&zst, level); | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  |     switch(err) { | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  |     case(Z_OK): | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	break; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  |     case(Z_MEM_ERROR): | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	PyErr_SetString(PyExc_MemoryError, | 
					
						
							|  |  |  | 			"Out of memory while compressing data"); | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 	goto error; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  |     case(Z_STREAM_ERROR): | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	PyErr_SetString(ZlibError, | 
					
						
							|  |  |  | 			"Bad compression level"); | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 	goto error; | 
					
						
							| 
									
										
										
										
											2001-10-16 21:56:09 +00:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  |         deflateEnd(&zst); | 
					
						
							| 
									
										
										
										
											2001-10-16 21:56:09 +00:00
										 |  |  | 	zlib_error(zst, err, "while compressing data"); | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 	goto error; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  |     Py_BEGIN_ALLOW_THREADS; | 
					
						
							|  |  |  |     err = deflate(&zst, Z_FINISH); | 
					
						
							|  |  |  |     Py_END_ALLOW_THREADS; | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  |     if (err != Z_STREAM_END) { | 
					
						
							|  |  |  | 	zlib_error(zst, err, "while compressing data"); | 
					
						
							|  |  |  | 	deflateEnd(&zst); | 
					
						
							|  |  |  | 	goto error; | 
					
						
							| 
									
										
										
										
											1997-09-04 23:39:23 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  |     err=deflateEnd(&zst); | 
					
						
							|  |  |  |     if (err == Z_OK) | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | 	ReturnVal = PyString_FromStringAndSize((char *)output, | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 					       zst.total_out); | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 	zlib_error(zst, err, "while finishing compression"); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |  error: | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     free(output); | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     return ReturnVal; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | static char decompress__doc__[] = | 
					
						
							| 
									
										
										
										
											2001-10-17 04:16:15 +00:00
										 |  |  | "decompress(string[, wbits[, bufsize]]) -- Return decompressed string.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "Optional arg wbits is the window buffer size.  Optional arg bufsize is\n" | 
					
						
							|  |  |  | "the initial output buffer size."; | 
					
						
							| 
									
										
										
										
											1997-06-03 22:21:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | PyZlib_decompress(PyObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     PyObject *result_str; | 
					
						
							|  |  |  |     Byte *input; | 
					
						
							|  |  |  |     int length, err; | 
					
						
							|  |  |  |     int wsize=DEF_WBITS, r_strlen=DEFAULTALLOC; | 
					
						
							|  |  |  |     z_stream zst; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     if (!PyArg_ParseTuple(args, "s#|ii:decompress", | 
					
						
							| 
									
										
										
										
											2001-10-16 23:26:08 +00:00
										 |  |  | 			  &input, &length, &wsize, &r_strlen)) | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     if (r_strlen <= 0) | 
					
						
							|  |  |  | 	r_strlen = 1; | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     zst.avail_in = length; | 
					
						
							|  |  |  |     zst.avail_out = r_strlen; | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:23:58 +00:00
										 |  |  |     if (!(result_str = PyString_FromStringAndSize(NULL, r_strlen))) | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     zst.zalloc = (alloc_func)NULL; | 
					
						
							|  |  |  |     zst.zfree = (free_func)Z_NULL; | 
					
						
							| 
									
										
										
										
											2001-10-16 23:26:08 +00:00
										 |  |  |     zst.next_out = (Byte *)PyString_AS_STRING(result_str); | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     zst.next_in = (Byte *)input; | 
					
						
							|  |  |  |     err = inflateInit2(&zst, wsize); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     switch(err) { | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  |     case(Z_OK): | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	break; | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     case(Z_MEM_ERROR): | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	PyErr_SetString(PyExc_MemoryError, | 
					
						
							|  |  |  | 			"Out of memory while decompressing data"); | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 	goto error; | 
					
						
							| 
									
										
										
										
											2001-10-16 21:56:09 +00:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  |         inflateEnd(&zst); | 
					
						
							| 
									
										
										
										
											2001-10-16 21:56:09 +00:00
										 |  |  | 	zlib_error(zst, err, "while preparing to decompress data"); | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 	goto error; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     do { | 
					
						
							|  |  |  | 	Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  | 	err=inflate(&zst, Z_FINISH); | 
					
						
							|  |  |  | 	Py_END_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	switch(err) { | 
					
						
							|  |  |  | 	case(Z_STREAM_END): | 
					
						
							| 
									
										
										
										
											1997-08-13 23:19:55 +00:00
										 |  |  | 	    break; | 
					
						
							| 
									
										
										
										
											1998-04-23 20:22:11 +00:00
										 |  |  | 	case(Z_BUF_ERROR): | 
					
						
							| 
									
										
										
										
											2000-10-09 14:18:10 +00:00
										 |  |  | 	    /*
 | 
					
						
							|  |  |  | 	     * If there is at least 1 byte of room according to zst.avail_out | 
					
						
							|  |  |  | 	     * and we get this error, assume that it means zlib cannot | 
					
						
							|  |  |  | 	     * process the inflate call() due to an error in the data. | 
					
						
							|  |  |  | 	     */ | 
					
						
							| 
									
										
										
										
											2001-10-16 21:56:09 +00:00
										 |  |  | 	    if (zst.avail_out > 0) { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 		PyErr_Format(ZlibError, "Error %i while decompressing data", | 
					
						
							|  |  |  | 			     err); | 
					
						
							|  |  |  | 		inflateEnd(&zst); | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 		goto error; | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	    } | 
					
						
							| 
									
										
										
										
											2000-10-09 14:18:10 +00:00
										 |  |  | 	    /* fall through */ | 
					
						
							|  |  |  | 	case(Z_OK): | 
					
						
							| 
									
										
										
										
											1997-08-13 23:19:55 +00:00
										 |  |  | 	    /* need more memory */ | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	    if (_PyString_Resize(&result_str, r_strlen << 1) == -1) { | 
					
						
							|  |  |  | 		inflateEnd(&zst); | 
					
						
							|  |  |  | 		result_str = NULL; | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 		goto error; | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	    } | 
					
						
							| 
									
										
										
										
											2001-10-16 23:26:08 +00:00
										 |  |  | 	    zst.next_out = (unsigned char *)PyString_AS_STRING(result_str) \ | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 		+ r_strlen; | 
					
						
							|  |  |  | 	    zst.avail_out = r_strlen; | 
					
						
							| 
									
										
										
										
											1997-08-13 23:19:55 +00:00
										 |  |  | 	    r_strlen = r_strlen << 1; | 
					
						
							|  |  |  | 	    break; | 
					
						
							| 
									
										
										
										
											2001-10-16 21:56:09 +00:00
										 |  |  | 	default: | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	    inflateEnd(&zst); | 
					
						
							| 
									
										
										
										
											2001-10-16 21:56:09 +00:00
										 |  |  | 	    zlib_error(zst, err, "while decompressing data"); | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 	    goto error; | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	} | 
					
						
							|  |  |  |     } while (err != Z_STREAM_END); | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  |     err = inflateEnd(&zst); | 
					
						
							|  |  |  |     if (err != Z_OK) { | 
					
						
							|  |  |  | 	zlib_error(zst, err, "while finishing data decompression"); | 
					
						
							| 
									
										
										
										
											2001-10-16 23:26:08 +00:00
										 |  |  | 	goto error; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  |     _PyString_Resize(&result_str, zst.total_out); | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     return result_str; | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |  error: | 
					
						
							|  |  |  |     Py_XDECREF(result_str); | 
					
						
							|  |  |  |     return NULL; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | PyZlib_compressobj(PyObject *selfptr, PyObject *args) | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:59:35 +00:00
										 |  |  |     compobject *self; | 
					
						
							|  |  |  |     int level=Z_DEFAULT_COMPRESSION, method=DEFLATED; | 
					
						
							|  |  |  |     int wbits=MAX_WBITS, memLevel=DEF_MEM_LEVEL, strategy=0, err; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (!PyArg_ParseTuple(args, "|iiiii:compressobj", &level, &method, &wbits, | 
					
						
							|  |  |  | 			  &memLevel, &strategy)) | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     self = newcompobject(&Comptype); | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     if (self==NULL) | 
					
						
							| 
									
										
										
										
											2001-10-16 21:59:35 +00:00
										 |  |  | 	return(NULL); | 
					
						
							|  |  |  |     self->zst.zalloc = (alloc_func)NULL; | 
					
						
							|  |  |  |     self->zst.zfree = (free_func)Z_NULL; | 
					
						
							|  |  |  |     err = deflateInit2(&self->zst, level, method, wbits, memLevel, strategy); | 
					
						
							|  |  |  |     switch(err) { | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  |     case (Z_OK): | 
					
						
							| 
									
										
										
										
											2001-10-16 21:59:35 +00:00
										 |  |  | 	self->is_initialised = 1; | 
					
						
							|  |  |  | 	return (PyObject*)self; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  |     case (Z_MEM_ERROR): | 
					
						
							| 
									
										
										
										
											2001-10-16 21:59:35 +00:00
										 |  |  | 	Py_DECREF(self); | 
					
						
							|  |  |  | 	PyErr_SetString(PyExc_MemoryError, | 
					
						
							|  |  |  | 			"Can't allocate memory for compression object"); | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  |     case(Z_STREAM_ERROR): | 
					
						
							| 
									
										
										
										
											2001-10-16 21:59:35 +00:00
										 |  |  | 	Py_DECREF(self); | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 	PyErr_SetString(PyExc_ValueError, "Invalid initialization option"); | 
					
						
							| 
									
										
										
										
											2001-10-16 21:59:35 +00:00
										 |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2001-10-16 21:56:09 +00:00
										 |  |  | 	zlib_error(self->zst, err, "while creating compression object"); | 
					
						
							| 
									
										
										
										
											1999-01-29 21:49:34 +00:00
										 |  |  |         Py_DECREF(self); | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | 	return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | PyZlib_decompressobj(PyObject *selfptr, PyObject *args) | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:59:35 +00:00
										 |  |  |     int wbits=DEF_WBITS, err; | 
					
						
							|  |  |  |     compobject *self; | 
					
						
							|  |  |  |     if (!PyArg_ParseTuple(args, "|i:decompressobj", &wbits)) | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     self = newcompobject(&Decomptype); | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     if (self == NULL) | 
					
						
							| 
									
										
										
										
											2001-10-16 21:59:35 +00:00
										 |  |  | 	return(NULL); | 
					
						
							|  |  |  |     self->zst.zalloc = (alloc_func)NULL; | 
					
						
							|  |  |  |     self->zst.zfree = (free_func)Z_NULL; | 
					
						
							|  |  |  |     err = inflateInit2(&self->zst, wbits); | 
					
						
							|  |  |  |     switch(err) { | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  |     case (Z_OK): | 
					
						
							| 
									
										
										
										
											2001-10-16 21:59:35 +00:00
										 |  |  | 	self->is_initialised = 1; | 
					
						
							|  |  |  | 	return (PyObject*)self; | 
					
						
							| 
									
										
										
										
											1997-09-04 23:39:23 +00:00
										 |  |  |     case(Z_STREAM_ERROR): | 
					
						
							| 
									
										
										
										
											2001-10-16 21:59:35 +00:00
										 |  |  | 	Py_DECREF(self); | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 	PyErr_SetString(PyExc_ValueError, "Invalid initialization option"); | 
					
						
							| 
									
										
										
										
											2001-10-16 21:59:35 +00:00
										 |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  |     case (Z_MEM_ERROR): | 
					
						
							| 
									
										
										
										
											2001-10-16 21:59:35 +00:00
										 |  |  | 	Py_DECREF(self); | 
					
						
							|  |  |  | 	PyErr_SetString(PyExc_MemoryError, | 
					
						
							|  |  |  | 			"Can't allocate memory for decompression object"); | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  |     default: | 
					
						
							| 
									
										
										
										
											2001-10-16 21:56:09 +00:00
										 |  |  | 	zlib_error(self->zst, err, "while creating decompression object"); | 
					
						
							| 
									
										
										
										
											1999-01-29 21:49:34 +00:00
										 |  |  |         Py_DECREF(self); | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											2001-10-16 21:59:35 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | Comp_dealloc(compobject *self) | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											1999-01-29 21:49:34 +00:00
										 |  |  |     if (self->is_initialised) | 
					
						
							| 
									
										
										
										
											2001-10-16 21:59:35 +00:00
										 |  |  | 	deflateEnd(&self->zst); | 
					
						
							| 
									
										
										
										
											1999-03-25 21:21:08 +00:00
										 |  |  |     Py_XDECREF(self->unused_data); | 
					
						
							| 
									
										
										
										
											2001-10-16 20:39:49 +00:00
										 |  |  |     Py_XDECREF(self->unconsumed_tail); | 
					
						
							| 
									
										
										
										
											2000-05-03 23:44:39 +00:00
										 |  |  |     PyObject_Del(self); | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | Decomp_dealloc(compobject *self) | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-02-21 02:15:56 +00:00
										 |  |  |     if (self->is_initialised) | 
					
						
							| 
									
										
										
										
											2001-10-16 21:59:35 +00:00
										 |  |  | 	inflateEnd(&self->zst); | 
					
						
							| 
									
										
										
										
											1999-03-25 21:21:08 +00:00
										 |  |  |     Py_XDECREF(self->unused_data); | 
					
						
							| 
									
										
										
										
											2001-10-16 20:39:49 +00:00
										 |  |  |     Py_XDECREF(self->unconsumed_tail); | 
					
						
							| 
									
										
										
										
											2000-05-03 23:44:39 +00:00
										 |  |  |     PyObject_Del(self); | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-06-03 22:21:03 +00:00
										 |  |  | static char comp_compress__doc__[] = | 
					
						
							| 
									
										
										
										
											2001-10-17 04:16:15 +00:00
										 |  |  | "compress(data) -- Return a string containing data compressed.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							| 
									
										
										
										
											1997-06-03 22:21:03 +00:00
										 |  |  | "After calling this function, some of the input data may still\n" | 
					
						
							|  |  |  | "be stored in internal buffers for later processing.\n" | 
					
						
							| 
									
										
										
										
											2001-10-17 04:16:15 +00:00
										 |  |  | "Call the flush() method to clear these buffers."; | 
					
						
							| 
									
										
										
										
											1997-06-03 22:21:03 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | PyZlib_objcompress(compobject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     int err, inplen, length = DEFAULTALLOC; | 
					
						
							|  |  |  |     PyObject *RetVal; | 
					
						
							|  |  |  |     Byte *input; | 
					
						
							|  |  |  |     unsigned long start_total_out; | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 23:26:08 +00:00
										 |  |  |     if (!PyArg_ParseTuple(args, "s#:compress", &input, &inplen)) | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:23:58 +00:00
										 |  |  |     if (!(RetVal = PyString_FromStringAndSize(NULL, length))) | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     ENTER_ZLIB | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     start_total_out = self->zst.total_out; | 
					
						
							|  |  |  |     self->zst.avail_in = inplen; | 
					
						
							|  |  |  |     self->zst.next_in = input; | 
					
						
							| 
									
										
										
										
											2001-02-21 02:15:56 +00:00
										 |  |  |     self->zst.avail_out = length; | 
					
						
							| 
									
										
										
										
											2001-10-16 23:26:08 +00:00
										 |  |  |     self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal); | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  |     Py_BEGIN_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2001-02-21 02:15:56 +00:00
										 |  |  |     err = deflate(&(self->zst), Z_NO_FLUSH); | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  |     Py_END_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     /* while Z_OK and the output buffer is full, there might be more output,
 | 
					
						
							|  |  |  |        so extend the output buffer and try again */ | 
					
						
							|  |  |  |     while (err == Z_OK && self->zst.avail_out == 0) { | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 	if (_PyString_Resize(&RetVal, length << 1) == -1) { | 
					
						
							|  |  |  | 	    RetVal = NULL; | 
					
						
							|  |  |  | 	    goto error; | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-10-16 23:26:08 +00:00
										 |  |  | 	self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \ | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	    + length; | 
					
						
							|  |  |  | 	self->zst.avail_out = length; | 
					
						
							|  |  |  | 	length = length << 1; | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  | 	err = deflate(&(self->zst), Z_NO_FLUSH); | 
					
						
							|  |  |  | 	Py_END_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     /* We will only get Z_BUF_ERROR if the output buffer was full but
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |        there wasn't more output when we tried again, so it is not an error | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |        condition. | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  |     if (err != Z_OK && err != Z_BUF_ERROR) { | 
					
						
							|  |  |  | 	zlib_error(self->zst, err, "while compressing"); | 
					
						
							|  |  |  | 	Py_DECREF(RetVal); | 
					
						
							|  |  |  | 	RetVal = NULL; | 
					
						
							|  |  |  | 	goto error; | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     if (_PyString_Resize(&RetVal, | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 			 self->zst.total_out - start_total_out) < 0) | 
					
						
							|  |  |  | 	RetVal = NULL; | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  |  error: | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     LEAVE_ZLIB | 
					
						
							|  |  |  |     return RetVal; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-06-03 22:21:03 +00:00
										 |  |  | static char decomp_decompress__doc__[] = | 
					
						
							| 
									
										
										
										
											2001-10-17 04:16:15 +00:00
										 |  |  | "decompress(data, max_length) -- Return a string containing the decompressed\n" | 
					
						
							|  |  |  | "version of the data.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "After calling this function, some of the input data may still be stored in\n" | 
					
						
							|  |  |  | "internal buffers for later processing.\n" | 
					
						
							| 
									
										
										
										
											2001-10-16 20:39:49 +00:00
										 |  |  | "Call the flush() method to clear these buffers.\n" | 
					
						
							|  |  |  | "If the max_length parameter is specified then the return value will be\n" | 
					
						
							|  |  |  | "no longer than max_length.  Unconsumed input data will be stored in\n" | 
					
						
							| 
									
										
										
										
											2001-10-17 04:16:15 +00:00
										 |  |  | "the unconsumed_tail attribute."; | 
					
						
							| 
									
										
										
										
											1997-06-03 22:21:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | PyZlib_objdecompress(compobject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     int err, inplen, old_length, length = DEFAULTALLOC; | 
					
						
							|  |  |  |     int max_length = 0; | 
					
						
							|  |  |  |     PyObject *RetVal; | 
					
						
							|  |  |  |     Byte *input; | 
					
						
							|  |  |  |     unsigned long start_total_out; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     if (!PyArg_ParseTuple(args, "s#|i:decompress", &input, | 
					
						
							| 
									
										
										
										
											2001-10-16 23:26:08 +00:00
										 |  |  | 			  &inplen, &max_length)) | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	return NULL; | 
					
						
							|  |  |  |     if (max_length < 0) { | 
					
						
							|  |  |  | 	PyErr_SetString(PyExc_ValueError, | 
					
						
							|  |  |  | 			"max_length must be greater than zero"); | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-10-16 20:39:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     /* limit amount of data allocated to max_length */ | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     if (max_length && length > max_length) | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	length = max_length; | 
					
						
							| 
									
										
										
										
											2001-10-16 21:23:58 +00:00
										 |  |  |     if (!(RetVal = PyString_FromStringAndSize(NULL, length))) | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     ENTER_ZLIB | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     start_total_out = self->zst.total_out; | 
					
						
							|  |  |  |     self->zst.avail_in = inplen; | 
					
						
							|  |  |  |     self->zst.next_in = input; | 
					
						
							|  |  |  |     self->zst.avail_out = length; | 
					
						
							| 
									
										
										
										
											2001-10-16 23:26:08 +00:00
										 |  |  |     self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal); | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  |     err = inflate(&(self->zst), Z_SYNC_FLUSH); | 
					
						
							|  |  |  |     Py_END_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     /* While Z_OK and the output buffer is full, there might be more output.
 | 
					
						
							|  |  |  |        So extend the output buffer and try again. | 
					
						
							| 
									
										
										
										
											2001-10-16 20:39:49 +00:00
										 |  |  |     */ | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     while (err == Z_OK && self->zst.avail_out == 0) { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	/* If max_length set, don't continue decompressing if we've already
 | 
					
						
							|  |  |  | 	   reached the limit. | 
					
						
							|  |  |  | 	*/ | 
					
						
							|  |  |  | 	if (max_length && length >= max_length) | 
					
						
							|  |  |  | 	    break; | 
					
						
							| 
									
										
										
										
											2001-10-16 20:39:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	/* otherwise, ... */ | 
					
						
							|  |  |  | 	old_length = length; | 
					
						
							|  |  |  | 	length = length << 1; | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | 	if (max_length && length > max_length) | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	    length = max_length; | 
					
						
							| 
									
										
										
										
											2001-10-16 20:39:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	if (_PyString_Resize(&RetVal, length) == -1) { | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 	    RetVal = NULL; | 
					
						
							|  |  |  | 	    goto error; | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-10-16 23:26:08 +00:00
										 |  |  | 	self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \ | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	    + old_length; | 
					
						
							|  |  |  | 	self->zst.avail_out = length - old_length; | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  | 	err = inflate(&(self->zst), Z_SYNC_FLUSH); | 
					
						
							|  |  |  | 	Py_END_ALLOW_THREADS | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-10-16 20:39:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     /* Not all of the compressed data could be accomodated in the output buffer
 | 
					
						
							|  |  |  |        of specified size. Return the unconsumed tail in an attribute.*/ | 
					
						
							|  |  |  |     if(max_length) { | 
					
						
							|  |  |  | 	Py_DECREF(self->unconsumed_tail); | 
					
						
							| 
									
										
										
										
											2001-10-23 22:29:06 +00:00
										 |  |  | 	self->unconsumed_tail = PyString_FromStringAndSize((char *)self->zst.next_in, | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 							   self->zst.avail_in); | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 	if(!self->unconsumed_tail) { | 
					
						
							|  |  |  | 	    Py_DECREF(RetVal); | 
					
						
							|  |  |  | 	    RetVal = NULL; | 
					
						
							|  |  |  | 	    goto error; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     /* The end of the compressed data has been reached, so set the
 | 
					
						
							|  |  |  |        unused_data attribute to a string containing the remainder of the | 
					
						
							|  |  |  |        data in the string.  Note that this is also a logical place to call | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |        inflateEnd, but the old behaviour of only calling it on flush() is | 
					
						
							|  |  |  |        preserved. | 
					
						
							|  |  |  |     */ | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  |     if (err == Z_STREAM_END) { | 
					
						
							|  |  |  | 	Py_XDECREF(self->unused_data);  /* Free original empty string */ | 
					
						
							|  |  |  | 	self->unused_data = PyString_FromStringAndSize( | 
					
						
							|  |  |  | 	    (char *)self->zst.next_in, self->zst.avail_in); | 
					
						
							|  |  |  | 	if (self->unused_data == NULL) { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	    Py_DECREF(RetVal); | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 	    goto error; | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | 	/* We will only get Z_BUF_ERROR if the output buffer was full
 | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 	   but there wasn't more output when we tried again, so it is | 
					
						
							|  |  |  | 	   not an error condition. | 
					
						
							|  |  |  | 	*/ | 
					
						
							|  |  |  |     } else if (err != Z_OK && err != Z_BUF_ERROR) { | 
					
						
							|  |  |  | 	zlib_error(self->zst, err, "while decompressing"); | 
					
						
							|  |  |  | 	Py_DECREF(RetVal); | 
					
						
							|  |  |  | 	RetVal = NULL; | 
					
						
							|  |  |  | 	goto error; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  |     if (_PyString_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) | 
					
						
							|  |  |  | 	RetVal = NULL; | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  |  error: | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     LEAVE_ZLIB | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     return RetVal; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-06-03 22:21:03 +00:00
										 |  |  | static char comp_flush__doc__[] = | 
					
						
							| 
									
										
										
										
											1998-12-18 22:13:11 +00:00
										 |  |  | "flush( [mode] ) -- Return a string containing any remaining compressed data.\n" | 
					
						
							| 
									
										
										
										
											2001-10-17 04:16:15 +00:00
										 |  |  | "\n" | 
					
						
							|  |  |  | "mode can be one of the constants Z_SYNC_FLUSH, Z_FULL_FLUSH, Z_FINISH; the\n" | 
					
						
							| 
									
										
										
										
											1998-12-18 22:13:11 +00:00
										 |  |  | "default value used when mode is not specified is Z_FINISH.\n" | 
					
						
							|  |  |  | "If mode == Z_FINISH, the compressor object can no longer be used after\n" | 
					
						
							| 
									
										
										
										
											2001-10-17 04:16:15 +00:00
										 |  |  | "calling the flush() method.  Otherwise, more data can still be compressed.\n"; | 
					
						
							| 
									
										
										
										
											1997-06-03 22:21:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | PyZlib_flush(compobject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     int err, length = DEFAULTALLOC; | 
					
						
							|  |  |  |     PyObject *RetVal; | 
					
						
							|  |  |  |     int flushmode = Z_FINISH; | 
					
						
							|  |  |  |     unsigned long start_total_out; | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     if (!PyArg_ParseTuple(args, "|i:flush", &flushmode)) | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     /* Flushing with Z_NO_FLUSH is a no-op, so there's no point in
 | 
					
						
							|  |  |  |        doing any work at all; just return an empty string. */ | 
					
						
							|  |  |  |     if (flushmode == Z_NO_FLUSH) { | 
					
						
							|  |  |  | 	return PyString_FromStringAndSize(NULL, 0); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:23:58 +00:00
										 |  |  |     if (!(RetVal = PyString_FromStringAndSize(NULL, length))) | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	return NULL; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ENTER_ZLIB | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     start_total_out = self->zst.total_out; | 
					
						
							|  |  |  |     self->zst.avail_in = 0; | 
					
						
							| 
									
										
										
										
											2001-02-21 02:15:56 +00:00
										 |  |  |     self->zst.avail_out = length; | 
					
						
							| 
									
										
										
										
											2001-10-16 23:26:08 +00:00
										 |  |  |     self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal); | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     Py_BEGIN_ALLOW_THREADS | 
					
						
							| 
									
										
										
										
											2001-02-21 02:15:56 +00:00
										 |  |  |     err = deflate(&(self->zst), flushmode); | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  |     Py_END_ALLOW_THREADS | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     /* while Z_OK and the output buffer is full, there might be more output,
 | 
					
						
							|  |  |  |        so extend the output buffer and try again */ | 
					
						
							|  |  |  |     while (err == Z_OK && self->zst.avail_out == 0) { | 
					
						
							|  |  |  | 	if (_PyString_Resize(&RetVal, length << 1) == -1)  { | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 	    RetVal = NULL; | 
					
						
							|  |  |  | 	    goto error; | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-10-16 23:26:08 +00:00
										 |  |  | 	self->zst.next_out = (unsigned char *)PyString_AS_STRING(RetVal) \ | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	    + length; | 
					
						
							|  |  |  | 	self->zst.avail_out = length; | 
					
						
							|  |  |  | 	length = length << 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	Py_BEGIN_ALLOW_THREADS | 
					
						
							|  |  |  | 	err = deflate(&(self->zst), flushmode); | 
					
						
							|  |  |  | 	Py_END_ALLOW_THREADS | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     /* If flushmode is Z_FINISH, we also have to call deflateEnd() to free
 | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |        various data structures. Note we should only get Z_STREAM_END when | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |        flushmode is Z_FINISH, but checking both for safety*/ | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  |     if (err == Z_STREAM_END && flushmode == Z_FINISH) { | 
					
						
							|  |  |  | 	err = deflateEnd(&(self->zst)); | 
					
						
							|  |  |  | 	if (err != Z_OK) { | 
					
						
							|  |  |  | 	    zlib_error(self->zst, err, "from deflateEnd()"); | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	    Py_DECREF(RetVal); | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 	    RetVal = NULL; | 
					
						
							|  |  |  | 	    goto error; | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 	else | 
					
						
							|  |  |  | 	    self->is_initialised = 0; | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* We will only get Z_BUF_ERROR if the output buffer was full
 | 
					
						
							|  |  |  | 	   but there wasn't more output when we tried again, so it is | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 	   not an error condition. | 
					
						
							|  |  |  | 	*/ | 
					
						
							|  |  |  |     } else if (err!=Z_OK && err!=Z_BUF_ERROR) { | 
					
						
							|  |  |  | 	zlib_error(self->zst, err, "while flushing"); | 
					
						
							|  |  |  | 	Py_DECREF(RetVal); | 
					
						
							|  |  |  | 	RetVal = NULL; | 
					
						
							| 
									
										
										
										
											1998-12-18 22:13:11 +00:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  |     if (_PyString_Resize(&RetVal, self->zst.total_out - start_total_out) < 0) | 
					
						
							|  |  |  | 	RetVal = NULL; | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |  error: | 
					
						
							| 
									
										
										
										
											2001-10-17 03:56:45 +00:00
										 |  |  |     LEAVE_ZLIB | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     return RetVal; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-06-03 22:21:03 +00:00
										 |  |  | static char decomp_flush__doc__[] = | 
					
						
							| 
									
										
										
										
											2001-10-17 04:16:15 +00:00
										 |  |  | "flush() -- Return a string containing any remaining decompressed data.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "The decompressor object can no longer be used after this call."; | 
					
						
							| 
									
										
										
										
											1997-06-03 22:21:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | PyZlib_unflush(compobject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | /*decompressor flush is a no-op because all pending data would have been
 | 
					
						
							| 
									
										
										
										
											2001-02-21 02:15:56 +00:00
										 |  |  |   flushed by the decompress method. However, this routine previously called | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |   inflateEnd, causing any further decompress or flush calls to raise | 
					
						
							| 
									
										
										
										
											2001-02-21 02:15:56 +00:00
										 |  |  |   exceptions. This behaviour has been preserved.*/ | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     int err; | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  |     PyObject * retval = NULL; | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     if (!PyArg_ParseTuple(args, "")) | 
					
						
							|  |  |  | 	return NULL; | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     ENTER_ZLIB | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     err = inflateEnd(&(self->zst)); | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  |     if (err != Z_OK) | 
					
						
							| 
									
										
										
										
											2001-10-16 21:56:09 +00:00
										 |  |  | 	zlib_error(self->zst, err, "from inflateEnd()"); | 
					
						
							| 
									
										
										
										
											2001-10-16 23:02:32 +00:00
										 |  |  |     else { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	self->is_initialised = 0; | 
					
						
							|  |  |  | 	retval = PyString_FromStringAndSize(NULL, 0); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     LEAVE_ZLIB | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     return retval; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyMethodDef comp_methods[] = | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     {"compress", (binaryfunc)PyZlib_objcompress, METH_VARARGS, | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |                  comp_compress__doc__}, | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     {"flush", (binaryfunc)PyZlib_flush, METH_VARARGS, | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |               comp_flush__doc__}, | 
					
						
							|  |  |  |     {NULL, NULL} | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyMethodDef Decomp_methods[] = | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     {"decompress", (binaryfunc)PyZlib_objdecompress, METH_VARARGS, | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |                    decomp_decompress__doc__}, | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     {"flush", (binaryfunc)PyZlib_unflush, METH_VARARGS, | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |               decomp_flush__doc__}, | 
					
						
							|  |  |  |     {NULL, NULL} | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | Comp_getattr(compobject *self, char *name) | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  |   /* No ENTER/LEAVE_ZLIB is necessary because this fn doesn't touch
 | 
					
						
							|  |  |  |      internal data. */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   return Py_FindMethod(comp_methods, (PyObject *)self, name); | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | Decomp_getattr(compobject *self, char *name) | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     PyObject * retval; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     ENTER_ZLIB | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     if (strcmp(name, "unused_data") == 0) { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	Py_INCREF(self->unused_data); | 
					
						
							|  |  |  | 	retval = self->unused_data; | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     } else if (strcmp(name, "unconsumed_tail") == 0) { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	Py_INCREF(self->unconsumed_tail); | 
					
						
							|  |  |  | 	retval = self->unconsumed_tail; | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     } else | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  | 	retval = Py_FindMethod(Decomp_methods, (PyObject *)self, name); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     LEAVE_ZLIB | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return retval; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | static char adler32__doc__[] = | 
					
						
							| 
									
										
										
										
											2001-10-17 04:16:15 +00:00
										 |  |  | "adler32(string[, start]) -- Compute an Adler-32 checksum of string.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "An optional starting value can be specified.  The returned checksum is\n" | 
					
						
							|  |  |  | "an integer."; | 
					
						
							| 
									
										
										
										
											1997-06-03 22:21:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | PyZlib_adler32(PyObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     uLong adler32val = adler32(0L, Z_NULL, 0); | 
					
						
							| 
									
										
										
										
											1997-09-04 23:39:23 +00:00
										 |  |  |     Byte *buf; | 
					
						
							|  |  |  |     int len; | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2000-02-29 13:59:29 +00:00
										 |  |  |     if (!PyArg_ParseTuple(args, "s#|l:adler32", &buf, &len, &adler32val)) | 
					
						
							| 
									
										
										
										
											1997-09-04 23:39:23 +00:00
										 |  |  | 	return NULL; | 
					
						
							|  |  |  |     adler32val = adler32(adler32val, buf, len); | 
					
						
							|  |  |  |     return PyInt_FromLong(adler32val); | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static char crc32__doc__[] = | 
					
						
							| 
									
										
										
										
											2001-10-17 04:16:15 +00:00
										 |  |  | "crc32(string[, start]) -- Compute a CRC-32 checksum of string.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "An optional starting value can be specified.  The returned checksum is\n" | 
					
						
							|  |  |  | "an integer."; | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyObject * | 
					
						
							| 
									
										
										
										
											2000-07-10 09:57:19 +00:00
										 |  |  | PyZlib_crc32(PyObject *self, PyObject *args) | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     uLong crc32val = crc32(0L, Z_NULL, 0); | 
					
						
							| 
									
										
										
										
											1997-09-04 23:39:23 +00:00
										 |  |  |     Byte *buf; | 
					
						
							|  |  |  |     int len; | 
					
						
							| 
									
										
										
										
											2000-02-29 13:59:29 +00:00
										 |  |  |     if (!PyArg_ParseTuple(args, "s#|l:crc32", &buf, &len, &crc32val)) | 
					
						
							| 
									
										
										
										
											1997-09-04 23:39:23 +00:00
										 |  |  | 	return NULL; | 
					
						
							|  |  |  |     crc32val = crc32(crc32val, buf, len); | 
					
						
							|  |  |  |     return PyInt_FromLong(crc32val); | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | static PyMethodDef zlib_methods[] = | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     {"adler32", (PyCFunction)PyZlib_adler32, METH_VARARGS, | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |                 adler32__doc__}, | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     {"compress", (PyCFunction)PyZlib_compress,  METH_VARARGS, | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |                  compress__doc__}, | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     {"compressobj", (PyCFunction)PyZlib_compressobj, METH_VARARGS, | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |                     compressobj__doc__}, | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     {"crc32", (PyCFunction)PyZlib_crc32, METH_VARARGS, | 
					
						
							|  |  |  |               crc32__doc__}, | 
					
						
							|  |  |  |     {"decompress", (PyCFunction)PyZlib_decompress, METH_VARARGS, | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |                    decompress__doc__}, | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  |     {"decompressobj", (PyCFunction)PyZlib_decompressobj, METH_VARARGS, | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |                    decompressobj__doc__}, | 
					
						
							|  |  |  |     {NULL, NULL} | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | statichere PyTypeObject Comptype = { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     PyObject_HEAD_INIT(0) | 
					
						
							|  |  |  |     0, | 
					
						
							| 
									
										
										
										
											2001-12-08 18:02:58 +00:00
										 |  |  |     "zlib.Compress", | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     sizeof(compobject), | 
					
						
							|  |  |  |     0, | 
					
						
							|  |  |  |     (destructor)Comp_dealloc,       /*tp_dealloc*/ | 
					
						
							|  |  |  |     0,                              /*tp_print*/ | 
					
						
							|  |  |  |     (getattrfunc)Comp_getattr,      /*tp_getattr*/ | 
					
						
							|  |  |  |     0,                              /*tp_setattr*/ | 
					
						
							|  |  |  |     0,                              /*tp_compare*/ | 
					
						
							|  |  |  |     0,                              /*tp_repr*/ | 
					
						
							|  |  |  |     0,                              /*tp_as_number*/ | 
					
						
							|  |  |  |     0,                              /*tp_as_sequence*/ | 
					
						
							|  |  |  |     0,                              /*tp_as_mapping*/ | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | statichere PyTypeObject Decomptype = { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     PyObject_HEAD_INIT(0) | 
					
						
							|  |  |  |     0, | 
					
						
							| 
									
										
										
										
											2001-12-08 18:02:58 +00:00
										 |  |  |     "zlib.Decompress", | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     sizeof(compobject), | 
					
						
							|  |  |  |     0, | 
					
						
							|  |  |  |     (destructor)Decomp_dealloc,     /*tp_dealloc*/ | 
					
						
							|  |  |  |     0,                              /*tp_print*/ | 
					
						
							|  |  |  |     (getattrfunc)Decomp_getattr,    /*tp_getattr*/ | 
					
						
							|  |  |  |     0,                              /*tp_setattr*/ | 
					
						
							|  |  |  |     0,                              /*tp_compare*/ | 
					
						
							|  |  |  |     0,                              /*tp_repr*/ | 
					
						
							|  |  |  |     0,                              /*tp_as_number*/ | 
					
						
							|  |  |  |     0,                              /*tp_as_sequence*/ | 
					
						
							|  |  |  |     0,                              /*tp_as_mapping*/ | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											1997-06-03 22:21:03 +00:00
										 |  |  | static char zlib_module_documentation[]= | 
					
						
							| 
									
										
										
										
											2001-10-17 04:16:15 +00:00
										 |  |  | "The functions in this module allow compression and decompression using the\n" | 
					
						
							|  |  |  | "zlib library, which is based on GNU zip.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "adler32(string[, start]) -- Compute an Adler-32 checksum.\n" | 
					
						
							|  |  |  | "compress(string[, level]) -- Compress string, with compression level in 1-9.\n" | 
					
						
							| 
									
										
										
										
											1997-06-03 22:21:03 +00:00
										 |  |  | "compressobj([level]) -- Return a compressor object.\n" | 
					
						
							| 
									
										
										
										
											2001-10-17 04:16:15 +00:00
										 |  |  | "crc32(string[, start]) -- Compute a CRC-32 checksum.\n" | 
					
						
							| 
									
										
										
										
											1999-12-20 22:13:38 +00:00
										 |  |  | "decompress(string,[wbits],[bufsize]) -- Decompresses a compressed string.\n" | 
					
						
							| 
									
										
										
										
											2001-10-17 04:16:15 +00:00
										 |  |  | "decompressobj([wbits]) -- Return a decompressor object.\n" | 
					
						
							|  |  |  | "\n" | 
					
						
							|  |  |  | "'wbits' is window buffer size.\n" | 
					
						
							|  |  |  | "Compressor objects support compress() and flush() methods; decompressor\n" | 
					
						
							|  |  |  | "objects support decompress() and flush()."; | 
					
						
							| 
									
										
										
										
											1997-06-03 22:21:03 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											1998-12-04 18:50:17 +00:00
										 |  |  | DL_EXPORT(void) | 
					
						
							| 
									
										
										
										
											2000-07-21 06:00:07 +00:00
										 |  |  | PyInit_zlib(void) | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     PyObject *m, *d, *ver; | 
					
						
							|  |  |  |     Comptype.ob_type = &PyType_Type; | 
					
						
							|  |  |  |     Decomptype.ob_type = &PyType_Type; | 
					
						
							|  |  |  |     m = Py_InitModule4("zlib", zlib_methods, | 
					
						
							|  |  |  | 		       zlib_module_documentation, | 
					
						
							|  |  |  | 		       (PyObject*)NULL,PYTHON_API_VERSION); | 
					
						
							|  |  |  |     d = PyModule_GetDict(m); | 
					
						
							|  |  |  |     ZlibError = PyErr_NewException("zlib.error", NULL, NULL); | 
					
						
							|  |  |  |     if (ZlibError != NULL) | 
					
						
							|  |  |  | 	PyDict_SetItemString(d, "error", ZlibError); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     PyModule_AddIntConstant(m, "MAX_WBITS", MAX_WBITS); | 
					
						
							|  |  |  |     PyModule_AddIntConstant(m, "DEFLATED", DEFLATED); | 
					
						
							|  |  |  |     PyModule_AddIntConstant(m, "DEF_MEM_LEVEL", DEF_MEM_LEVEL); | 
					
						
							|  |  |  |     PyModule_AddIntConstant(m, "Z_BEST_SPEED", Z_BEST_SPEED); | 
					
						
							|  |  |  |     PyModule_AddIntConstant(m, "Z_BEST_COMPRESSION", Z_BEST_COMPRESSION); | 
					
						
							|  |  |  |     PyModule_AddIntConstant(m, "Z_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION); | 
					
						
							|  |  |  |     PyModule_AddIntConstant(m, "Z_FILTERED", Z_FILTERED); | 
					
						
							|  |  |  |     PyModule_AddIntConstant(m, "Z_HUFFMAN_ONLY", Z_HUFFMAN_ONLY); | 
					
						
							|  |  |  |     PyModule_AddIntConstant(m, "Z_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY); | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     PyModule_AddIntConstant(m, "Z_FINISH", Z_FINISH); | 
					
						
							|  |  |  |     PyModule_AddIntConstant(m, "Z_NO_FLUSH", Z_NO_FLUSH); | 
					
						
							|  |  |  |     PyModule_AddIntConstant(m, "Z_SYNC_FLUSH", Z_SYNC_FLUSH); | 
					
						
							|  |  |  |     PyModule_AddIntConstant(m, "Z_FULL_FLUSH", Z_FULL_FLUSH); | 
					
						
							| 
									
										
										
										
											2001-10-17 03:57:20 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     ver = PyString_FromString(ZLIB_VERSION); | 
					
						
							|  |  |  |     if (ver != NULL) { | 
					
						
							|  |  |  | 	PyDict_SetItemString(d, "ZLIB_VERSION", ver); | 
					
						
							|  |  |  | 	Py_DECREF(ver); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2001-09-07 16:27:31 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | #ifdef WITH_THREAD
 | 
					
						
							| 
									
										
										
										
											2001-10-16 21:19:45 +00:00
										 |  |  |     zlib_lock = PyThread_allocate_lock(); | 
					
						
							| 
									
										
										
										
											2002-03-11 09:20:47 +00:00
										 |  |  | #endif /* WITH_THREAD */
 | 
					
						
							| 
									
										
										
										
											1997-04-29 15:38:09 +00:00
										 |  |  | } |