diff libinterp/dldfcn/gzip.cc @ 22922:426325aa8ee9 stable

Fix gzip for certain types of gzip files (bug #49760). * gzip.cc (deflate): Follow example code from zlib and quit loop based on feof rather than on return status of fread.
author Rik <rik@octave.org>
date Sun, 18 Dec 2016 07:09:49 -0800
parents 93ea313301f9
children b150be19734d e9a0469dedd9
line wrap: on
line diff
--- a/libinterp/dldfcn/gzip.cc	Fri Dec 16 10:13:14 2016 -0500
+++ b/libinterp/dldfcn/gzip.cc	Sun Dec 18 07:09:49 2016 -0800
@@ -348,14 +348,18 @@
         unsigned char buf_in[buf_len];
         unsigned char buf_out[buf_len];
 
-        while ((strm->avail_in = std::fread (buf_in, sizeof (buf_in[0]),
-                                             buf_len, source.fp)) != 0)
+        int flush;
+
+        do
           {
+            strm->avail_in = std::fread (buf_in, sizeof (buf_in[0]),
+                                         buf_len, source.fp);
+
             if (std::ferror (source.fp))
               throw std::runtime_error ("failed to read source file");
 
             strm->next_in = buf_in;
-            const int flush = std::feof (source.fp) ? Z_FINISH : Z_NO_FLUSH;
+            flush = std::feof (source.fp) ? Z_FINISH : Z_NO_FLUSH;
 
             // If deflate returns Z_OK and with zero avail_out, it must be
             // called again after making room in the output buffer because
@@ -376,8 +380,12 @@
             while (strm->avail_out == 0);
 
             if (strm->avail_in != 0)
-              throw std::runtime_error ("failed to wrote file");
-          }
+              throw std::runtime_error ("failed to write file");
+
+          } while (flush != Z_FINISH);
+
+          if (status != Z_STREAM_END)
+            throw std::runtime_error ("failed to write file");
       }
 
       void