zip-reader: Fix memory leak on error path in zip_reader_create().
[pspp] / src / libpspp / zip-reader.c
index 7c1e342aa76e51bb513afa1e72cf01d9bf41e7d0..0f8c26332868f83699e460474e6c635ec5288d04 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2011 Free Software Foundation, Inc.
+   Copyright (C) 2011, 2013, 2014 Free Software Foundation, Inc.
 
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
@@ -16,7 +16,7 @@
 
 #include <config.h>
 
-
+#include <inttypes.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <errno.h>
 #include <xalloc.h>
 #include <libpspp/assertion.h>
+#include <libpspp/compiler.h>
 
 #include <byteswap.h>
 #include <crc.h>
 
-#include "inflate.h"
-
 #include "str.h"
 
 #include "zip-reader.h"
 #define _(msgid) gettext (msgid)
 #define N_(msgid) (msgid)
 
-
-static bool find_eocd (FILE *fp, off_t *off);
-
-static int
-stored_read (struct zip_member *zm, void *buf, size_t n)
+struct zip_member
 {
-  return fread (buf, 1, n, zm->fp);
-}
+  FILE *fp;                   /* The stream from which the data is read */
+  uint32_t offset;            /* Starting offset in file. */
+  uint32_t comp_size;         /* Length of member file data, in bytes. */
+  uint32_t ucomp_size;        /* Uncompressed length of member file data, in bytes. */
+  uint32_t expected_crc;      /* CRC-32 of member file data.. */
+  char *name;                 /* Name of member file. */
+  uint32_t crc;
+  const struct decompressor *decompressor;
 
-static bool
-stored_init (struct zip_member *zm UNUSED)
-{
-  return true;
-}
+  size_t bytes_unread;       /* Number of bytes left in the member available for reading */
+  int ref_cnt;
+  struct string *errmsgs;    /* A string to hold error messages.
+                               This string is NOT owned by this object. */
+  void *aux;
+};
 
-static void
-stored_finish (struct zip_member *zm UNUSED)
+struct decompressor
 {
-  /* Nothing required */
-}
-
+  bool (*init) (struct zip_member *);
+  int  (*read) (struct zip_member *, void *, size_t);
+  void (*finish) (struct zip_member *);
+};
+static const struct decompressor stored_decompressor;
+static const struct decompressor inflate_decompressor;
 
-static struct decompressor decompressors[n_COMPRESSION] = 
-  {
-    {stored_init, stored_read, stored_finish},
-#if HAVE_ZLIB_H
-    {inflate_init, inflate_read, inflate_finish}
-#endif
-  };
+static bool find_eocd (FILE *fp, off_t *off);
 
-static enum compression
-comp_code (struct zip_member *zm, uint16_t c)
+static const struct decompressor *
+get_decompressor (struct zip_member *zm, uint16_t c)
 {
-  enum compression which;
+  assert (zm->errmsgs);
   switch (c)
     {
     case 0:
-      which = COMPRESSION_STORED;
-      break;
-#if HAVE_ZLIB_H
+      return &stored_decompressor;
+
     case 8:
-      which = COMPRESSION_INFLATE;
-      break;
-#endif
+      return &inflate_decompressor;
+
     default:
-      ds_put_format (zm->errs, _("Unsupported compression type (%d)"), c);
-      which = n_COMPRESSION;
-      break;
+      ds_put_format (zm->errmsgs, _("Unsupported compression type (%d)"), c);
+      return NULL;
     }
-  return which;
 }
 
 
@@ -106,7 +100,7 @@ struct zip_reader
 void
 zip_member_finish (struct zip_member *zm)
 {
-  ds_clear (zm->errs);
+  ds_clear (zm->errmsgs);
   /*  Probably not useful, because we would have to read right to the end of the member
   if (zm->expected_crc != zm->crc)
     {
@@ -123,7 +117,7 @@ void
 zip_reader_destroy (struct zip_reader *zr)
 {
   int i;
-  if (zr == NULL) 
+  if (zr == NULL)
     return;
 
   fclose (zr->fr);
@@ -152,37 +146,50 @@ skip_bytes (FILE *f, size_t n)
   fseeko (f, n, SEEK_CUR);
 }
 
+static bool get_bytes (FILE *f, void *x, size_t n) WARN_UNUSED_RESULT;
+
+
 /* Read N bytes from F, storing the result in X */
-static void
+static bool
 get_bytes (FILE *f, void *x, size_t n)
 {
-  fread (x, 1, n, f);
+  return (n == fread (x, 1, n, f));
 }
 
+static bool get_u32 (FILE *f, uint32_t *v) WARN_UNUSED_RESULT;
+
+
 /* Read a 32 bit value from F */
-static void
+static bool
 get_u32 (FILE *f, uint32_t *v)
 {
   uint32_t x;
-  get_bytes (f, &x, sizeof x);
+  if (!get_bytes (f, &x, sizeof x))
+    return false;
 #ifdef WORDS_BIGENDIAN
   *v = bswap_32 (x);
 #else
   *v = x;
 #endif
+  return true;
 }
 
+static bool get_u16 (FILE *f, uint16_t *v) WARN_UNUSED_RESULT;
+
+
 /* Read a 16 bit value from F */
-static void
+static bool
 get_u16 (FILE *f, uint16_t *v)
 {
   uint16_t x;
-  get_bytes (f, &x, sizeof x);
+  if (!get_bytes (f, &x, sizeof x))
+    return false;
 #ifdef WORDS_BIGENDIAN
   *v = bswap_16 (x);
 #else
   *v = x;
 #endif
+  return true;
 }
 
 
@@ -193,12 +200,12 @@ check_magic (FILE *f, uint32_t expected, struct string *err)
 {
   uint32_t magic;
 
-  get_u32 (f, &magic);
+  if (! get_u32 (f, &magic)) return false;
 
   if ((expected != magic))
     {
       ds_put_format (err,
-                    _("Corrupt file at 0x%llx: Expected %"PRIx32"; got %"PRIx32), 
+                    _("Corrupt file at 0x%llx: Expected %"PRIx32"; got %"PRIx32),
                     (long long int) ftello (f) - sizeof (uint32_t), expected, magic);
 
       return false;
@@ -214,12 +221,12 @@ zip_member_read (struct zip_member *zm, void *buf, size_t bytes)
 {
   int bytes_read = 0;
 
-  ds_clear (zm->errs);
+  ds_clear (zm->errmsgs);
 
   if ( bytes > zm->bytes_unread)
     bytes = zm->bytes_unread;
 
-  bytes_read  = decompressors[zm->compression].read (zm, buf, bytes);
+  bytes_read  = zm->decompressor->read (zm, buf, bytes);
   if ( bytes_read < 0)
     return bytes_read;
 
@@ -234,7 +241,7 @@ zip_member_read (struct zip_member *zm, void *buf, size_t bytes)
 /*
   Read a local file header from ZR and add it to ZR's internal array.
   Returns a pointer to the member read.  This pointer belongs to ZR.
-  If the caller wishes to control it, she should ref it with 
+  If the caller wishes to control it, she should ref it with
   zip_member_ref.
 */
 static struct zip_member *
@@ -244,47 +251,49 @@ zip_header_read_next (struct zip_reader *zr)
 
   uint16_t v, nlen, extralen;
   uint16_t gp, time, date;
-  
+
   uint16_t clen, diskstart, iattr;
   uint32_t eattr;
   uint16_t comp_type;
 
   ds_clear (zr->errs);
+  zm->errmsgs = zr->errs;
 
   if ( ! check_magic (zr->fr, MAGIC_SOCD, zr->errs))
     return NULL;
 
-  get_u16 (zr->fr, &v);
+  if (! get_u16 (zr->fr, &v)) return NULL;
 
-  get_u16 (zr->fr, &v);
-  get_u16 (zr->fr, &gp);
-  get_u16 (zr->fr, &comp_type);
+  if (! get_u16 (zr->fr, &v)) return NULL;
+  if (! get_u16 (zr->fr, &gp)) return NULL;
+  if (! get_u16 (zr->fr, &comp_type)) return NULL;
 
-  zm->compression = comp_code (zm, comp_type);
+  zm->decompressor = get_decompressor (zm, comp_type);
+  if (! zm->decompressor) return NULL;
 
-  get_u16 (zr->fr, &time);
-  get_u16 (zr->fr, &date);
-  get_u32 (zr->fr, &zm->expected_crc);
-  get_u32 (zr->fr, &zm->comp_size);
-  get_u32 (zr->fr, &zm->ucomp_size);
-  get_u16 (zr->fr, &nlen);
-  get_u16 (zr->fr, &extralen);
-  get_u16 (zr->fr, &clen);
-  get_u16 (zr->fr, &diskstart);
-  get_u16 (zr->fr, &iattr);
-  get_u32 (zr->fr, &eattr);
-  get_u32 (zr->fr, &zm->offset);
+  if (! get_u16 (zr->fr, &time)) return NULL;
+  if (! get_u16 (zr->fr, &date)) return NULL;
+  if (! get_u32 (zr->fr, &zm->expected_crc)) return NULL;
+  if (! get_u32 (zr->fr, &zm->comp_size)) return NULL;
+  if (! get_u32 (zr->fr, &zm->ucomp_size)) return NULL;
+  if (! get_u16 (zr->fr, &nlen)) return NULL;
+  if (! get_u16 (zr->fr, &extralen)) return NULL;
+  if (! get_u16 (zr->fr, &clen)) return NULL;
+  if (! get_u16 (zr->fr, &diskstart)) return NULL;
+  if (! get_u16 (zr->fr, &iattr)) return NULL;
+  if (! get_u32 (zr->fr, &eattr)) return NULL;
+  if (! get_u32 (zr->fr, &zm->offset)) return NULL;
 
   zm->name = xzalloc (nlen + 1);
-  get_bytes (zr->fr, zm->name, nlen);
+  if (! get_bytes (zr->fr, zm->name, nlen)) return NULL;
 
   skip_bytes (zr->fr, extralen);
-  
+
   zr->members[zr->nm++] = zm;
 
-  zm->fp = fopen (zr->filename, "r");
+  zm->fp = fopen (zr->filename, "rb");
   zm->ref_cnt = 1;
-  zm->errs = zr->errs;
+
 
   return zm;
 }
@@ -305,7 +314,7 @@ zip_reader_create (const char *filename, struct string *errs)
 
   zr->nm = 0;
 
-  zr->fr = fopen (filename, "r");
+  zr->fr = fopen (filename, "rb");
   if (NULL == zr->fr)
     {
       ds_put_cstr (zr->errs, strerror (errno));
@@ -344,15 +353,20 @@ zip_reader_create (const char *filename, struct string *errs)
       free (zr);
       return NULL;
     }
-  
-  get_u16 (zr->fr, &disknum);
-  get_u16 (zr->fr, &disknum);
 
-  get_u16 (zr->fr, &zr->n_members);
-  get_u16 (zr->fr, &total_members);
+  if (! get_u16 (zr->fr, &disknum)
+      || ! get_u16 (zr->fr, &disknum)
 
-  get_u32 (zr->fr, &central_dir_length);
-  get_u32 (zr->fr, &central_dir_start);
+      || ! get_u16 (zr->fr, &zr->n_members)
+      || ! get_u16 (zr->fr, &total_members)
+
+      || ! get_u32 (zr->fr, &central_dir_length)
+      || ! get_u32 (zr->fr, &central_dir_start))
+    {
+      fclose (zr->fr);
+      free (zr);
+      return NULL;
+    }
 
   if ( 0 != fseeko (zr->fr, central_dir_start, SEEK_SET))
     {
@@ -366,7 +380,7 @@ zip_reader_create (const char *filename, struct string *errs)
   zr->members = xcalloc (zr->n_members, sizeof (*zr->members));
   memset (zr->members, 0, zr->n_members * sizeof (*zr->members));
 
-  zr->filename = strdup (filename);
+  zr->filename = xstrdup (filename);
 
   return zr;
 }
@@ -380,7 +394,7 @@ zip_member_open (struct zip_reader *zr, const char *member)
   uint16_t v, nlen, extra_len;
   uint16_t gp, comp_type, time, date;
   uint32_t ucomp_size, comp_size;
-  
+
   uint32_t crc;
   bool new_member = false;
   char *name = NULL;
@@ -405,14 +419,14 @@ zip_member_open (struct zip_reader *zr, const char *member)
     else
       zm = NULL;
   }
-  
+
   if ( zm == NULL)
     return NULL;
 
   if ( 0 != fseeko (zm->fp, zm->offset, SEEK_SET))
     {
       const char *mm = strerror (errno);
-      ds_put_format (zm->errs, _("Failed to seek to start of member `%s': %s"), zm->name, mm);
+      ds_put_format (zm->errmsgs, _("Failed to seek to start of member `%s': %s"), zm->name, mm);
       return NULL;
     }
 
@@ -421,28 +435,29 @@ zip_member_open (struct zip_reader *zr, const char *member)
       return NULL;
     }
 
-  get_u16 (zm->fp, &v);
-  get_u16 (zm->fp, &gp);
-  get_u16 (zm->fp, &comp_type);
-  zm->compression = comp_code (zm, comp_type);
-  get_u16 (zm->fp, &time);
-  get_u16 (zm->fp, &date);
-  get_u32 (zm->fp, &crc);
-  get_u32 (zm->fp, &comp_size);
+  if (! get_u16 (zm->fp, &v)) return NULL;
+  if (! get_u16 (zm->fp, &gp)) return NULL;
+  if (! get_u16 (zm->fp, &comp_type)) return NULL;
+  zm->decompressor = get_decompressor (zm, comp_type);
+  if (! zm->decompressor) return NULL;
+  if (! get_u16 (zm->fp, &time)) return NULL;
+  if (! get_u16 (zm->fp, &date)) return NULL;
+  if (! get_u32 (zm->fp, &crc)) return NULL;
+  if (! get_u32 (zm->fp, &comp_size)) return NULL;
 
-  get_u32 (zm->fp, &ucomp_size);
-  get_u16 (zm->fp, &nlen);
-  get_u16 (zm->fp, &extra_len);
+  if (! get_u32 (zm->fp, &ucomp_size)) return NULL;
+  if (! get_u16 (zm->fp, &nlen)) return NULL;
+  if (! get_u16 (zm->fp, &extra_len)) return NULL;
 
   name = xzalloc (nlen + 1);
 
-  get_bytes (zm->fp, name, nlen);
+  if (! get_bytes (zm->fp, name, nlen)) return NULL;
 
   skip_bytes (zm->fp, extra_len);
 
   if (strcmp (name, zm->name) != 0)
     {
-      ds_put_format (zm->errs,
+      ds_put_format (zm->errmsgs,
                     _("Name mismatch in zip archive. Central directory says `%s'; local file header says `%s'"),
                     zm->name, name);
       free (name);
@@ -453,11 +468,11 @@ zip_member_open (struct zip_reader *zr, const char *member)
   free (name);
 
   zm->bytes_unread = zm->ucomp_size;
-  
+
   if ( !new_member)
-    decompressors[zm->compression].finish (zm);
+    zm->decompressor->finish (zm);
 
-  if (!decompressors[zm->compression].init (zm) )
+  if (!zm->decompressor->init (zm) )
     return NULL;
 
   return zm;
@@ -480,7 +495,7 @@ zip_member_unref (struct zip_member *zm)
 
   if (--zm->ref_cnt == 0)
     {
-      decompressors[zm->compression].finish (zm);
+      zm->decompressor->finish (zm);
       if (zm->fp)
        fclose (zm->fp);
       free (zm->name);
@@ -505,7 +520,7 @@ find_eocd (FILE *fp, off_t *off)
   const uint32_t magic = MAGIC_EOCD;
   bool found = false;
 
-  /* The magic cannot be more than 22 bytes from the end of the file, 
+  /* The magic cannot be more than 22 bytes from the end of the file,
      because that is the minimum length of the EndOfCentralDirectory
      record.
    */
@@ -515,7 +530,7 @@ find_eocd (FILE *fp, off_t *off)
     }
   start = ftello (fp);
   stop = start + sizeof (magic);
-  do 
+  do
     {
       found = probe_magic (fp, magic, start, stop, off);
       /* FIXME: For extra confidence lookup the directory start record here*/
@@ -555,13 +570,14 @@ probe_magic (FILE *fp, uint32_t magic, off_t start, off_t stop, off_t *off)
 
   do
     {
-      fread (&byte, 1, 1, fp);
+      if (1 != fread (&byte, 1, 1, fp))
+       break;
 
       if ( byte == seq[state])
        state++;
       else
        state = 0;
-      
+
       if ( state == 4)
        {
          *off = ftello (fp) - 4;
@@ -575,4 +591,147 @@ probe_magic (FILE *fp, uint32_t magic, off_t start, off_t stop, off_t *off)
 
   return false;
 }
+\f
+/* Null decompressor. */
+
+static int
+stored_read (struct zip_member *zm, void *buf, size_t n)
+{
+  return fread (buf, 1, n, zm->fp);
+}
+
+static bool
+stored_init (struct zip_member *zm UNUSED)
+{
+  return true;
+}
+
+static void
+stored_finish (struct zip_member *zm UNUSED)
+{
+  /* Nothing required */
+}
+
+static const struct decompressor stored_decompressor =
+  {stored_init, stored_read, stored_finish};
+\f
+/* Inflate decompressor. */
+
+#undef crc32
+#include <zlib.h>
+
+#define UCOMPSIZE 4096
+
+struct inflator
+{
+  z_stream zss;
+  int state;
+  unsigned char ucomp[UCOMPSIZE];
+  size_t bytes_uncomp;
+  size_t ucomp_bytes_read;
+
+  /* Two bitfields as defined by RFC1950 */
+  uint16_t cmf_flg ;
+};
+
+static void
+inflate_finish (struct zip_member *zm)
+{
+  struct inflator *inf = zm->aux;
+
+  inflateEnd (&inf->zss);
+
+  free (inf);
+}
+
+static bool
+inflate_init (struct zip_member *zm)
+{
+  int r;
+  struct inflator *inf = xzalloc (sizeof *inf);
+
+  uint16_t flg = 0 ;
+  uint16_t cmf = 0x8; /* Always 8 for inflate */
+
+  const uint16_t cinfo = 7;  /* log_2(Window size) - 8 */
+
+  cmf |= cinfo << 4;     /* Put cinfo into the high nibble */
+
+  /* make these into a 16 bit word */
+  inf->cmf_flg = (cmf << 8 ) | flg;
+
+  /* Set the check bits */
+  inf->cmf_flg += 31 - (inf->cmf_flg % 31);
+  assert (inf->cmf_flg % 31 == 0);
+
+  inf->zss.next_in = Z_NULL;
+  inf->zss.avail_in = 0;
+  inf->zss.zalloc = Z_NULL;
+  inf->zss.zfree  = Z_NULL;
+  inf->zss.opaque = Z_NULL;
+  r = inflateInit (&inf->zss);
+
+  if ( Z_OK != r)
+    {
+      ds_put_format (zm->errmsgs, _("Cannot initialize inflator: %s"), zError (r));
+      return false;
+    }
+
+  zm->aux = inf;
+
+  return true;
+}
+
+static int
+inflate_read (struct zip_member *zm, void *buf, size_t n)
+{
+  int r;
+  struct inflator *inf = zm->aux;
+
+  if (inf->zss.avail_in == 0)
+    {
+      int bytes_read;
+      int bytes_to_read;
+      int pad = 0;
+
+      if ( inf->state == 0)
+       {
+         inf->ucomp[1] = inf->cmf_flg ;
+         inf->ucomp[0] = inf->cmf_flg >> 8 ;
+
+         pad = 2;
+         inf->state++;
+       }
+
+      bytes_to_read = zm->comp_size - inf->ucomp_bytes_read;
+
+      if (bytes_to_read == 0)
+       return 0;
+
+      if (bytes_to_read > UCOMPSIZE)
+       bytes_to_read = UCOMPSIZE;
+
+      bytes_read = fread (inf->ucomp + pad, 1, bytes_to_read - pad, zm->fp);
+
+      inf->ucomp_bytes_read += bytes_read;
+
+      inf->zss.avail_in = bytes_read + pad;
+      inf->zss.next_in = inf->ucomp;
+    }
+  inf->zss.avail_out = n;
+  inf->zss.next_out = buf;
+
+  r = inflate (&inf->zss, Z_NO_FLUSH);
+  if ( Z_OK == r)
+    {
+      return n - inf->zss.avail_out;
+    }
+
+  ds_put_format (zm->errmsgs, _("Error inflating: %s"), zError (r));
+
+  return -1;
+}
+
+static const struct decompressor inflate_decompressor =
+  {inflate_init, inflate_read, inflate_finish};