Alexey Borzenkov avatar Alexey Borzenkov committed 159b8f4

Fix ancient bug with zlib deflating
---

Comments (0)

Files changed (1)

libImaging/ZipEncode.c

 	context->z_stream.zalloc = (alloc_func)0;
 	context->z_stream.zfree = (free_func)0;
 	context->z_stream.opaque = (voidpf)0;
+	context->z_stream.next_in = 0;
+	context->z_stream.avail_in = 0;
 
 	err = deflateInit2(&context->z_stream,
 			   /* compression level */
     /* Setup the destination buffer */
     context->z_stream.next_out = buf;
     context->z_stream.avail_out = bytes;
+    if (context->z_stream.next_in && context->z_stream.avail_in > 0) {
+	/* We have some data from previous round, deflate it first */
+	err = deflate(&context->z_stream, Z_NO_FLUSH);
+
+	if (err < 0) {
+	    /* Something went wrong inside the compression library */
+	    if (err == Z_DATA_ERROR)
+		state->errcode = IMAGING_CODEC_BROKEN;
+	    else if (err == Z_MEM_ERROR)
+		state->errcode = IMAGING_CODEC_MEMORY;
+	    else
+		state->errcode = IMAGING_CODEC_CONFIG;
+	    free(context->paeth);
+	    free(context->average);
+	    free(context->up);
+	    free(context->prior);
+	    free(context->previous);
+	    deflateEnd(&context->z_stream);
+	    return -1;
+	}
+    }
 
     ImagingSectionEnter(&cookie);
     for (;;) {
 		context->z_stream.next_in = context->output;
 		context->z_stream.avail_in = state->bytes+1;
 
-		/* err = deflate(&context->z_stream, Z_NO_FLUSH); */
-
-                /* FIXME: temporary workaround for problem with recent
-                   versions of zlib -- 990709/fl */
-
-		err = deflate(&context->z_stream, Z_SYNC_FLUSH);
+		err = deflate(&context->z_stream, Z_NO_FLUSH);
 
 		if (err < 0) {
 		    /* Something went wrong inside the compression library */
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.