po: auto update
[gnupg.git] / g10 / compress.c
index 6e412e9..e7a6f2b 100644 (file)
@@ -15,7 +15,7 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, see <http://www.gnu.org/licenses/>.
+ * along with this program; if not, see <https://www.gnu.org/licenses/>.
  */
 
 /* Note that the code in compress-bz2.c is nearly identical to the
@@ -29,7 +29,6 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
-#include <assert.h>
 #include <errno.h>
 #ifdef HAVE_ZIP
 # include <zlib.h>
@@ -39,7 +38,7 @@
 #endif
 
 #include "gpg.h"
-#include "util.h"
+#include "../common/util.h"
 #include "packet.h"
 #include "filter.h"
 #include "main.h"
@@ -164,7 +163,8 @@ do_uncompress( compress_filter_context_t *zfx, z_stream *zs,
               IOBUF a, size_t *ret_len )
 {
     int zrc;
-    int rc=0;
+    int rc = 0;
+    int leave = 0;
     size_t n;
     int nread, count;
     int refill = !zs->avail_in;
@@ -182,13 +182,14 @@ do_uncompress( compress_filter_context_t *zfx, z_stream *zs,
            nread = iobuf_read( a, zfx->inbuf + n, count );
            if( nread == -1 ) nread = 0;
            n += nread;
-           /* If we use the undocumented feature to suppress
-            * the zlib header, we have to give inflate an
-            * extra dummy byte to read */
-           if( nread < count && zfx->algo == 1 ) {
-               *(zfx->inbuf + n) = 0xFF; /* is it really needed ? */
-               zfx->algo1hack = 1;
+           /* Algo 1 has no zlib header which requires us to give
+            * inflate an extra dummy byte to read. To be on the safe
+            * side we allow for up to 4 ff bytes.  */
+           if( nread < count && zfx->algo == 1 && zfx->algo1hack < 4) {
+               *(zfx->inbuf + n) = 0xFF;
+               zfx->algo1hack++;
                n++;
+                leave = 1;
            }
            zs->avail_in = n;
        }
@@ -208,7 +209,8 @@ do_uncompress( compress_filter_context_t *zfx, z_stream *zs,
            else
                log_fatal("zlib inflate problem: rc=%d\n", zrc );
        }
-    } while( zs->avail_out && zrc != Z_STREAM_END && zrc != Z_BUF_ERROR );
+    } while (zs->avail_out && zrc != Z_STREAM_END && zrc != Z_BUF_ERROR
+             && !leave);
 
     *ret_len = zfx->outbufsize - zs->avail_out;
     if( DBG_FILTER )
@@ -285,7 +287,7 @@ compress_filter( void *opaque, int control,
           zfx->release (zfx);
     }
     else if( control == IOBUFCTRL_DESC )
-       *(char**)buf = "compress_filter";
+        mem2str (buf, "compress_filter", *ret_len);
     return rc;
 }
 #endif /*HAVE_ZIP*/
@@ -293,6 +295,10 @@ compress_filter( void *opaque, int control,
 static void
 release_context (compress_filter_context_t *ctx)
 {
+  xfree(ctx->inbuf);
+  ctx->inbuf = NULL;
+  xfree(ctx->outbuf);
+  ctx->outbuf = NULL;
   xfree (ctx);
 }
 
@@ -303,15 +309,19 @@ int
 handle_compressed (ctrl_t ctrl, void *procctx, PKT_compressed *cd,
                   int (*callback)(IOBUF, void *), void *passthru )
 {
-    compress_filter_context_t *cfx;
     int rc;
 
     if(check_compress_algo(cd->algorithm))
-      return G10ERR_COMPR_ALGO;
-    cfx = xmalloc_clear (sizeof *cfx);
-    cfx->release = release_context;
-    cfx->algo = cd->algorithm;
-    push_compress_filter(cd->buf,cfx,cd->algorithm);
+      return GPG_ERR_COMPR_ALGO;
+    if(cd->algorithm) {
+        compress_filter_context_t *cfx;
+
+        cfx = xmalloc_clear (sizeof *cfx);
+        cfx->release = release_context;
+        cfx->algo = cd->algorithm;
+        if (push_compress_filter(cd->buf, cfx, cd->algorithm))
+          xfree (cfx);
+    }
     if( callback )
        rc = callback(cd->buf, passthru );
     else
@@ -320,16 +330,20 @@ handle_compressed (ctrl_t ctrl, void *procctx, PKT_compressed *cd,
     return rc;
 }
 
-void
+gpg_error_t
 push_compress_filter(IOBUF out,compress_filter_context_t *zfx,int algo)
 {
-  push_compress_filter2(out,zfx,algo,0);
+  return push_compress_filter2(out,zfx,algo,0);
 }
 
-void
+
+/* Push a compress filter and return 0 if that succeeded.  */
+gpg_error_t
 push_compress_filter2(IOBUF out,compress_filter_context_t *zfx,
                      int algo,int rel)
 {
+  gpg_error_t err = gpg_error (GPG_ERR_FALSE);
+
   if(algo>=0)
     zfx->algo=algo;
   else
@@ -344,16 +358,20 @@ push_compress_filter2(IOBUF out,compress_filter_context_t *zfx,
     case COMPRESS_ALGO_ZIP:
     case COMPRESS_ALGO_ZLIB:
       iobuf_push_filter2(out,compress_filter,zfx,rel);
+      err = 0;
       break;
 #endif
 
 #ifdef HAVE_BZIP2
     case COMPRESS_ALGO_BZIP2:
       iobuf_push_filter2(out,compress_filter_bz2,zfx,rel);
+      err = 0;
       break;
 #endif
 
     default:
       BUG();
     }
+
+  return err;
 }