Blob Blame History Raw
From 0c7af9e6cb05b436505e7f46ef49dcb6f791f30a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Petr=20P=C3=ADsa=C5=99?= <ppisar@redhat.com>
Date: Fri, 17 Feb 2017 11:00:21 +0100
Subject: [PATCH] Adapt to zlib-1.2.11
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This is a fix ported from Compress-Raw-Zlib-2.072 that restores
compatibility with zlib-1.2.11.

CPAN RT#119762

Signed-off-by: Petr Písař <ppisar@redhat.com>
---
 Zlib.xs    | 220 +++++++++++++++++++++++++++++++++++++++++++++++--------------
 t/02zlib.t |  11 +++-
 2 files changed, 178 insertions(+), 53 deletions(-)

diff --git a/Zlib.xs b/Zlib.xs
index d379f78..83d1423 100644
--- a/Zlib.xs
+++ b/Zlib.xs
@@ -74,6 +74,10 @@
 #  define AT_LEAST_ZLIB_1_2_8
 #endif
 
+#if  defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1290
+#  define AT_LEAST_ZLIB_1_2_9
+#endif
+
 #ifdef USE_PPPORT_H
 #  define NEED_sv_2pvbyte
 #  define NEED_sv_2pv_nolen
@@ -134,12 +138,13 @@ typedef struct di_stream {
     uLong    dict_adler ;
     int      last_error ;
     bool     zip_mode ;
-#define SETP_BYTE
+/* #define SETP_BYTE */
 #ifdef SETP_BYTE
+    /* SETP_BYTE only works with zlib up to 1.2.8 */
     bool     deflateParams_out_valid ;
     Bytef    deflateParams_out_byte;
 #else
-#define deflateParams_BUFFER_SIZE       0x4000
+#define deflateParams_BUFFER_SIZE       0x40000
     uLong    deflateParams_out_length;
     Bytef*   deflateParams_out_buffer;
 #endif
@@ -636,6 +641,103 @@ char * string ;
     return sv ;
 }
 
+#if 0
+int
+flushToBuffer(di_stream* s, int flush)
+{
+    dTHX;
+    int ret ;
+    z_stream * strm = &s->stream;
+
+    Bytef* output = s->deflateParams_out_buffer ;
+
+    strm->next_in = NULL;
+    strm->avail_in = 0;
+    
+    uLong total_output = 0;
+    uLong have = 0;
+
+    do 
+    {
+        if (output)
+            output = (unsigned char *)saferealloc(output, total_output + s->bufsize);
+        else
+            output = (unsigned char *)safemalloc(s->bufsize);
+
+        strm->next_out  = output + total_output;
+        strm->avail_out = s->bufsize;
+
+        ret = deflate(strm, flush);    /* no bad return value */
+        //assert(ret != Z_STREAM_ERROR);  /* state not clobbered */
+        if(ret == Z_STREAM_ERROR)
+        {
+            safefree(output);
+            return ret;
+        }
+        have = s->bufsize - strm->avail_out;
+        total_output += have;
+
+        //fprintf(stderr, "FLUSH %s %d, return %d\n", flush_flags[flush], have, ret);
+
+    } while (strm->avail_out == 0);
+
+    s->deflateParams_out_buffer = output;
+    s->deflateParams_out_length = total_output; 
+
+    return Z_OK;
+}
+#endif
+
+#ifndef SETP_BYTE
+int
+flushParams(di_stream* s)
+{
+    dTHX;
+    int ret ;
+    z_stream * strm = &s->stream;
+
+    strm->next_in = NULL;
+    strm->avail_in = 0;
+    
+    Bytef* output = s->deflateParams_out_buffer ;
+    uLong total_output = s->deflateParams_out_length;
+
+    uLong have = 0;
+
+    do 
+    {
+        if (output)
+            output = (unsigned char *)saferealloc(output, total_output + s->bufsize);
+        else
+            output = (unsigned char *)safemalloc(s->bufsize);
+
+        strm->next_out  = output + total_output;
+        strm->avail_out = s->bufsize;
+
+        ret = deflateParams(&(s->stream), s->Level, s->Strategy);
+        /* fprintf(stderr, "deflateParams %d %s %lu\n", ret,
+            GetErrorString(ret),  s->bufsize - strm->avail_out); */
+
+        if (ret == Z_STREAM_ERROR) 
+            break;
+
+        have = s->bufsize - strm->avail_out;
+        total_output += have;
+
+
+    } while (ret == Z_BUF_ERROR) ;
+
+    if(ret == Z_STREAM_ERROR)
+        safefree(output);
+    else 
+    {
+        s->deflateParams_out_buffer = output;
+        s->deflateParams_out_length = total_output; 
+    }
+
+    return ret;
+}
+#endif /* ! SETP_BYTE */
 
 #include "constants.h"
 
@@ -991,20 +1093,24 @@ deflate (s, buf, output)
     /* Check for saved output from deflateParams */
     if (s->deflateParams_out_length) {
         uLong plen = s->deflateParams_out_length ;
-        /* printf("Copy %d bytes saved data\n", plen);*/
+        /* printf("Copy %lu bytes saved data\n", plen); */
         if (s->stream.avail_out < plen) {
-            /*printf("GROW from %d to %d\n", s->stream.avail_out,
-                        SvLEN(output) + plen - s->stream.avail_out); */
-            Sv_Grow(output, SvLEN(output) + plen - s->stream.avail_out) ;
+            /* printf("GROW from %d to %lu\n", s->stream.avail_out,
+                        SvLEN(output) + plen - s->stream.avail_out);  */
+             s->stream.next_out = (Bytef*) Sv_Grow(output, SvLEN(output) + plen - s->stream.avail_out) ;
+             s->stream.next_out += cur_length;
         }
         
-        Copy(s->stream.next_out, s->deflateParams_out_buffer, plen, Bytef) ;	
-        cur_length = cur_length + plen;
+        Copy(s->deflateParams_out_buffer, s->stream.next_out, plen, Bytef) ;	
+        cur_length += plen;
         SvCUR_set(output, cur_length);
-	s->stream.next_out += plen ;
-	s->stream.avail_out = SvLEN(output) - cur_length ;
-	increment = s->stream.avail_out;
-	s->deflateParams_out_length = 0;
+        s->stream.next_out += plen ;
+        s->stream.avail_out = SvLEN(output) - cur_length ;
+        increment = s->stream.avail_out;
+
+        s->deflateParams_out_length = 0;
+        Safefree(s->deflateParams_out_buffer);
+        s->deflateParams_out_buffer = NULL;
     }
 #endif
     RETVAL = Z_OK ;
@@ -1027,6 +1133,12 @@ deflate (s, buf, output)
         }
 
         RETVAL = deflate(&(s->stream), Z_NO_FLUSH);
+        if (RETVAL != Z_STREAM_ERROR) {
+            int done = increment -  s->stream.avail_out ;
+            /* printf("std DEFLATEr returned %d '%s'  avail in %d, out %d wrote %d\n", RETVAL,
+            GetErrorString(RETVAL), s->stream.avail_in,
+s->stream.avail_out, done); */
+        }
     
         if (trace) {
             printf("DEFLATE returned %d %s, avail in %d, out %d\n", RETVAL,
@@ -1080,7 +1192,6 @@ flush(s, output, f=Z_FINISH)
   CODE:
     bufinc = s->bufsize;
   
-    s->stream.avail_in = 0; /* should be zero already anyway */
   
     /* retrieve the output buffer */
     output = deRef_l(output, "flush") ;
@@ -1108,20 +1219,24 @@ flush(s, output, f=Z_FINISH)
     /* Check for saved output from deflateParams */
     if (s->deflateParams_out_length) {
         uLong plen = s->deflateParams_out_length ;
-        /* printf("Copy %d bytes saved data\n", plen); */
+        /* printf("Copy %lu bytes saved data\n", plen); */
         if (s->stream.avail_out < plen) {
-            /* printf("GROW from %d to %d\n", s->stream.avail_out, 
+            /* printf("GROW from %d to %lu\n", s->stream.avail_out, 
                         SvLEN(output) + plen - s->stream.avail_out); */
-            Sv_Grow(output, SvLEN(output) + plen - s->stream.avail_out) ;
+            s->stream.next_out = (Bytef*) Sv_Grow(output, SvLEN(output) + plen - s->stream.avail_out) ;
+            s->stream.next_out += cur_length;
         }
         
-        Copy(s->stream.next_out, s->deflateParams_out_buffer, plen, Bytef) ;	
-        cur_length = cur_length + plen;
+        Copy(s->deflateParams_out_buffer, s->stream.next_out, plen, Bytef) ;	
+        cur_length += plen;
         SvCUR_set(output, cur_length);
-	s->stream.next_out += plen ;
-	s->stream.avail_out = SvLEN(output) - cur_length ;
-	increment = s->stream.avail_out;
-	s->deflateParams_out_length = 0;
+        s->stream.next_out += plen ;
+        s->stream.avail_out = SvLEN(output) - cur_length ;
+        increment = s->stream.avail_out;
+
+        s->deflateParams_out_length = 0;
+        Safefree(s->deflateParams_out_buffer);
+        s->deflateParams_out_buffer = NULL;
     }
 #endif
 
@@ -1145,9 +1260,15 @@ flush(s, output, f=Z_FINISH)
         }
 
         RETVAL = deflate(&(s->stream), f);
+        if (RETVAL != Z_STREAM_ERROR) {
+            int done = availableout -  s->stream.avail_out ;
+            /* printf("flush DEFLATEr returned %d '%s'  avail in %d, out %d wrote %d\n", RETVAL,
+            GetErrorString(RETVAL), s->stream.avail_in,
+s->stream.avail_out, done); */
+        }
     
         if (trace) {
-            printf("flush DEFLATE returned %d %s, avail in %d, out %d\n", RETVAL,
+            printf("flush DEFLATE returned %d '%s', avail in %d, out %d\n", RETVAL,
             GetErrorString(RETVAL), s->stream.avail_in, s->stream.avail_out); 
             DispStream(s, "AFTER");
         }
@@ -1184,41 +1305,38 @@ _deflateParams(s, flags, level, strategy, bufsize)
 	int	level
 	int	strategy
     	uLong	bufsize
+	bool changed = FALSE;
     CODE:
-	/* printf("_deflateParams(Flags %d Level %d Strategy %d Bufsize %d)\n", flags, level, strategy, bufsize); 
-	printf("Before -- Level %d, Strategy %d, Bufsize %d\n", s->Level, s->Strategy, s->bufsize); */
-	if (flags & 1)
-	    s->Level = level ;
-	if (flags & 2)
-	    s->Strategy = strategy ;
-        if (flags & 4) {
+        /* printf("_deflateParams(Flags %d Level %d Strategy %d Bufsize %d)\n", flags, level, strategy, bufsize); 
+        printf("Before -- Level %d, Strategy %d, Bufsize %d\n", s->Level, s->Strategy, s->bufsize); */
+        if (flags & 1 && level != s->Level) {
+            s->Level = level ;
+            changed = TRUE;
+        }
+        if (flags & 2 && strategy != s->Strategy) {
+            s->Strategy = strategy ;
+            changed = TRUE;
+        }
+        if (flags & 4)
             s->bufsize = bufsize; 
-	}
-	/* printf("After --  Level %d, Strategy %d, Bufsize %d\n", s->Level, s->Strategy, s->bufsize);*/
+        if (changed) {
 #ifdef SETP_BYTE
-        s->stream.avail_in = 0; 
-        s->stream.next_out = &(s->deflateParams_out_byte) ;
-        s->stream.avail_out = 1;
-	RETVAL = deflateParams(&(s->stream), s->Level, s->Strategy);
-	s->deflateParams_out_valid = 
-		(RETVAL == Z_OK && s->stream.avail_out == 0) ;
-	/* printf("RETVAL %d, avail out %d, byte %c\n", RETVAL, s->stream.avail_out, s->deflateParams_out_byte); */
+            s->stream.avail_in = 0; 
+            s->stream.next_out = &(s->deflateParams_out_byte) ;
+            s->stream.avail_out = 1;
+            RETVAL = deflateParams(&(s->stream), s->Level, s->Strategy);
+            s->deflateParams_out_valid = 
+            (RETVAL == Z_OK && s->stream.avail_out == 0) ;
 #else
-	/* printf("Level %d Strategy %d, Prev Len %d\n", 
+            /* printf("Level %d Strategy %d, Prev Len %d\n", 
                 s->Level, s->Strategy, s->deflateParams_out_length); */
-        s->stream.avail_in = 0; 
-        if (s->deflateParams_out_buffer == NULL)
-            s->deflateParams_out_buffer = safemalloc(deflateParams_BUFFER_SIZE);
-        s->stream.next_out = s->deflateParams_out_buffer ;
-        s->stream.avail_out = deflateParams_BUFFER_SIZE;
-
-	RETVAL = deflateParams(&(s->stream), s->Level, s->Strategy);
-	s->deflateParams_out_length = deflateParams_BUFFER_SIZE - s->stream.avail_out;
-	/* printf("RETVAL %d, length out %d, avail %d\n", 
-                    RETVAL, s->deflateParams_out_length, s->stream.avail_out ); */
+            RETVAL = flushParams(s);
 #endif
+        }
+        else
+            RETVAL = Z_OK;
     OUTPUT:
-	RETVAL
+        RETVAL
 
 
 int
diff --git a/t/02zlib.t b/t/02zlib.t
index 2c9aad6..5d024a9 100644
--- a/t/02zlib.t
+++ b/t/02zlib.t
@@ -27,7 +27,7 @@ BEGIN
         $count = 232 ;
     }
     elsif ($] >= 5.006) {
-        $count = 317 ;
+        $count = 320 ;
     }
     else {
         $count = 275 ;
@@ -559,6 +559,13 @@ SKIP:
     is $x->get_Level(),    Z_BEST_SPEED;
     is $x->get_Strategy(), Z_HUFFMAN_ONLY;
      
+    # change both Level & Strategy again without any calls to deflate 
+    $status = $x->deflateParams(-Level => Z_DEFAULT_COMPRESSION, -Strategy => Z_DEFAULT_STRATEGY, -Bufsize => 1234) ;
+    cmp_ok $status, '==', Z_OK ;
+    
+    is $x->get_Level(),    Z_DEFAULT_COMPRESSION;
+    is $x->get_Strategy(), Z_DEFAULT_STRATEGY;
+     
     $status = $x->deflate($goodbye, $Answer) ;
     cmp_ok $status, '==', Z_OK ;
     $input .= $goodbye;
@@ -568,7 +575,7 @@ SKIP:
     cmp_ok $status, '==', Z_OK ;
     
     is $x->get_Level(),    Z_NO_COMPRESSION;
-    is $x->get_Strategy(), Z_HUFFMAN_ONLY;
+    is $x->get_Strategy(), Z_DEFAULT_STRATEGY;
      
     $status = $x->deflate($goodbye, $Answer) ;
     cmp_ok $status, '==', Z_OK ;
-- 
2.7.4