pivot-table: Make pivot_area_get_default_style() return a static copy.
[pspp] / src / libpspp / zip-reader.c
index ae8f5a57fb86ec84dc49d107f54e6e938e3a8958..6c7c4e41bccec78651c87c33312e84ac527b344f 100644 (file)
@@ -40,6 +40,8 @@
 
 struct zip_member
 {
+  char *file_name;             /* File name. */
+  char *member_name;          /* Member name. */
   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. */
@@ -81,11 +83,11 @@ get_decompressor (uint16_t c)
 
 struct zip_reader
 {
-  char *filename;                  /* The name of the file from which the data is read */
-  FILE *fr;                        /* The stream from which the meta data is read */
+  char *file_name;                  /* The name of the file from which the data is read */
   uint16_t n_entries;              /* Number of directory entries. */
   struct zip_entry *entries;       /* Directory entries. */
-  struct string *errs;
+  struct string *errs;             /* A string to hold error messages.  This
+                                      string is NOT owned by this object. */
 };
 
 struct zip_entry
@@ -101,6 +103,8 @@ zip_member_finish (struct zip_member *zm)
 {
   if (zm)
     {
+      free (zm->file_name);
+      free (zm->member_name);
       ds_clear (zm->errmsgs);
       zm->decompressor->finish (zm);
       fclose (zm->fp);
@@ -116,8 +120,7 @@ zip_reader_destroy (struct zip_reader *zr)
   if (zr == NULL)
     return;
 
-  fclose (zr->fr);
-  free (zr->filename);
+  free (zr->file_name);
 
   for (i = 0; i < zr->n_entries; ++i)
     {
@@ -186,7 +189,8 @@ get_u16 (FILE *f, uint16_t *v)
 /* Read 32 bit integer and compare it with EXPECTED.
    place an error string in ERR if necessary. */
 static bool
-check_magic (FILE *f, uint32_t expected, struct string *err)
+check_magic (FILE *f, const char *file_name,
+             uint32_t expected, struct string *err)
 {
   uint32_t magic;
 
@@ -195,8 +199,11 @@ check_magic (FILE *f, uint32_t expected, struct string *err)
   if ((expected != magic))
     {
       ds_put_format (err,
-                    _("Corrupt file at 0x%llx: Expected %"PRIx32"; got %"PRIx32),
-                    (long long int) ftello (f) - sizeof (uint32_t), expected, magic);
+                    _("%s: corrupt archive at 0x%llx: "
+                       "expected %#"PRIx32" but got %#"PRIx32),
+                     file_name,
+                    (long long int) ftello (f) - sizeof (uint32_t),
+                     expected, magic);
 
       return false;
     }
@@ -225,11 +232,46 @@ zip_member_read (struct zip_member *zm, void *buf, size_t bytes)
   return bytes_read;
 }
 
+/* Read all of ZM into memory, storing the data in *DATAP and its size in *NP.
+   Returns NULL if successful, otherwise an error string that the caller
+   must eventually free(). */
+char * WARN_UNUSED_RESULT
+zip_member_read_all (struct zip_reader *zr, const char *member_name,
+                     void **datap, size_t *np)
+{
+  struct zip_member *zm = zip_member_open (zr, member_name);
+  if (!zm)
+    {
+      *datap = NULL;
+      *np = 0;
+      return ds_steal_cstr (zr->errs);
+    }
 
-/* Read a central directory header from ZR and initializes ZE with it.
-   Returns true if successful, false otherwise. */
+  *datap = xmalloc (zm->ucomp_size);
+  *np = zm->ucomp_size;
+
+  uint8_t *data = *datap;
+  while (zm->bytes_unread)
+    if (zip_member_read (zm, data + (zm->ucomp_size - zm->bytes_unread),
+                         zm->bytes_unread) == -1)
+      {
+        zip_member_finish (zm);
+        free (*datap);
+        *datap = NULL;
+        *np = 0;
+        return ds_steal_cstr (zr->errs);
+      }
+
+  zip_member_finish (zm);
+  return NULL;
+}
+
+/* Read a central directory header from FILE and initializes ZE with it.
+   Returns true if successful, false otherwise.  On error, appends error
+   messages to ERRS. */
 static bool
-zip_header_read_next (struct zip_reader *zr, struct zip_entry *ze)
+zip_header_read_next (FILE *file, const char *file_name,
+                      struct zip_entry *ze, struct string *errs)
 {
   uint16_t v, nlen, extralen;
   uint16_t gp, time, date;
@@ -239,38 +281,38 @@ zip_header_read_next (struct zip_reader *zr, struct zip_entry *ze)
   uint32_t eattr;
   uint16_t comp_type;
 
-  if ( ! check_magic (zr->fr, MAGIC_SOCD, zr->errs))
+  if ( ! check_magic (file, file_name, MAGIC_SOCD, errs))
     return false;
 
-  if (! get_u16 (zr->fr, &v)) return false;
-  if (! get_u16 (zr->fr, &v)) return false;
-  if (! get_u16 (zr->fr, &gp)) return false;
-  if (! get_u16 (zr->fr, &comp_type)) return false;
-  if (! get_u16 (zr->fr, &time)) return false;
-  if (! get_u16 (zr->fr, &date)) return false;
-  if (! get_u32 (zr->fr, &expected_crc)) return false;
-  if (! get_u32 (zr->fr, &ze->comp_size)) return false;
-  if (! get_u32 (zr->fr, &ze->ucomp_size)) return false;
-  if (! get_u16 (zr->fr, &nlen)) return false;
-  if (! get_u16 (zr->fr, &extralen)) return false;
-  if (! get_u16 (zr->fr, &clen)) return false;
-  if (! get_u16 (zr->fr, &diskstart)) return false;
-  if (! get_u16 (zr->fr, &iattr)) return false;
-  if (! get_u32 (zr->fr, &eattr)) return false;
-  if (! get_u32 (zr->fr, &ze->offset)) return false;
+  if (! get_u16 (file, &v)) return false;
+  if (! get_u16 (file, &v)) return false;
+  if (! get_u16 (file, &gp)) return false;
+  if (! get_u16 (file, &comp_type)) return false;
+  if (! get_u16 (file, &time)) return false;
+  if (! get_u16 (file, &date)) return false;
+  if (! get_u32 (file, &expected_crc)) return false;
+  if (! get_u32 (file, &ze->comp_size)) return false;
+  if (! get_u32 (file, &ze->ucomp_size)) return false;
+  if (! get_u16 (file, &nlen)) return false;
+  if (! get_u16 (file, &extralen)) return false;
+  if (! get_u16 (file, &clen)) return false;
+  if (! get_u16 (file, &diskstart)) return false;
+  if (! get_u16 (file, &iattr)) return false;
+  if (! get_u32 (file, &eattr)) return false;
+  if (! get_u32 (file, &ze->offset)) return false;
 
   ze->name = xzalloc (nlen + 1);
-  if (! get_bytes (zr->fr, ze->name, nlen)) return false;
+  if (! get_bytes (file, ze->name, nlen)) return false;
 
-  skip_bytes (zr->fr, extralen);
+  skip_bytes (file, extralen);
 
   return true;
 }
 
 
-/* Create a reader from the zip called FILENAME */
+/* Create a reader from the zip called FILE_NAME */
 struct zip_reader *
-zip_reader_create (const char *filename, struct string *errs)
+zip_reader_create (const char *file_name, struct string *errs)
 {
   uint16_t disknum, n_members, total_members;
   off_t offset = 0;
@@ -281,76 +323,80 @@ zip_reader_create (const char *filename, struct string *errs)
   if ( zr->errs)
     ds_init_empty (zr->errs);
 
-  zr->fr = fopen (filename, "rb");
-  if (NULL == zr->fr)
+  FILE *file = fopen (file_name, "rb");
+  if (!file)
     {
-      ds_put_cstr (zr->errs, strerror (errno));
+      ds_put_format (zr->errs, _("%s: open failed (%s)"),
+                     file_name, strerror (errno));
       free (zr);
       return NULL;
     }
 
-  if ( ! check_magic (zr->fr, MAGIC_LHDR, zr->errs))
+  if ( ! check_magic (file, file_name, MAGIC_LHDR, zr->errs))
     {
-      fclose (zr->fr);
+      fclose (file);
       free (zr);
       return NULL;
     }
 
-  if ( ! find_eocd (zr->fr, &offset))
+  if ( ! find_eocd (file, &offset))
     {
-      ds_put_format (zr->errs, _("Cannot find central directory"));
-      fclose (zr->fr);
+      ds_put_format (zr->errs, _("%s: cannot find central directory"),
+                     file_name);
+      fclose (file);
       free (zr);
       return NULL;
     }
 
-  if ( 0 != fseeko (zr->fr, offset, SEEK_SET))
+  if ( 0 != fseeko (file, offset, SEEK_SET))
     {
-      const char *mm = strerror (errno);
-      ds_put_format (zr->errs, _("Failed to seek to end of central directory record: %s"), mm);
-      fclose (zr->fr);
+      ds_put_format (zr->errs, _("%s: seek failed (%s)"),
+                     file_name, strerror (errno));
+      fclose (file);
       free (zr);
       return NULL;
     }
 
 
-  if ( ! check_magic (zr->fr, MAGIC_EOCD, zr->errs))
+  if ( ! check_magic (file, file_name, MAGIC_EOCD, zr->errs))
     {
-      fclose (zr->fr);
+      fclose (file);
       free (zr);
       return NULL;
     }
 
-  if (! get_u16 (zr->fr, &disknum)
-      || ! get_u16 (zr->fr, &disknum)
+  if (! get_u16 (file, &disknum)
+      || ! get_u16 (file, &disknum)
 
-      || ! get_u16 (zr->fr, &n_members)
-      || ! get_u16 (zr->fr, &total_members)
+      || ! get_u16 (file, &n_members)
+      || ! get_u16 (file, &total_members)
 
-      || ! get_u32 (zr->fr, &central_dir_length)
-      || ! get_u32 (zr->fr, &central_dir_start))
+      || ! get_u32 (file, &central_dir_length)
+      || ! get_u32 (file, &central_dir_start))
     {
-      fclose (zr->fr);
+      fclose (file);
       free (zr);
       return NULL;
     }
 
-  if ( 0 != fseeko (zr->fr, central_dir_start, SEEK_SET))
+  if ( 0 != fseeko (file, central_dir_start, SEEK_SET))
     {
-      const char *mm = strerror (errno);
-      ds_put_format (zr->errs, _("Failed to seek to central directory: %s"), mm);
-      fclose (zr->fr);
+      ds_put_format (zr->errs, _("%s: seek failed (%s)"),
+                     file_name, strerror (errno));
+      fclose (file);
       free (zr);
       return NULL;
     }
 
-  zr->filename = xstrdup (filename);
+  zr->file_name = xstrdup (file_name);
 
   zr->entries = xcalloc (n_members, sizeof *zr->entries);
   for (int i = 0; i < n_members; i++)
     {
-      if (!zip_header_read_next (zr, &zr->entries[zr->n_entries]))
+      if (!zip_header_read_next (file, file_name,
+                                 &zr->entries[zr->n_entries], errs))
         {
+          fclose (file);
           zip_reader_destroy (zr);
           return NULL;
         }
@@ -361,7 +407,7 @@ zip_reader_create (const char *filename, struct string *errs)
 }
 
 static struct zip_entry *
-zip_entry_find (struct zip_reader *zr, const char *member)
+zip_entry_find (const struct zip_reader *zr, const char *member)
 {
   for (int i = 0; i < zr->n_entries; ++i)
     {
@@ -372,6 +418,19 @@ zip_entry_find (struct zip_reader *zr, const char *member)
   return NULL;
 }
 
+const char *
+zip_reader_get_member_name(const struct zip_reader *zr, size_t idx)
+{
+  return idx < zr->n_entries ? zr->entries[idx].name : NULL;
+}
+
+/* Returns true if ZR contains a member named MEMBER, false otherwise. */
+bool
+zip_reader_contains_member (const struct zip_reader *zr, const char *member)
+{
+  return zip_entry_find (zr, member) != NULL;
+}
+
 /* Return the member called MEMBER from the reader ZR  */
 struct zip_member *
 zip_member_open (struct zip_reader *zr, const char *member)
@@ -379,18 +438,22 @@ zip_member_open (struct zip_reader *zr, const char *member)
   struct zip_entry *ze = zip_entry_find (zr, member);
   if ( ze == NULL)
     {
-      ds_put_format (zr->errs, _("%s: unknown member"), member);
+      ds_put_format (zr->errs, _("%s: unknown member \"%s\""),
+                     zr->file_name, member);
       return NULL;
     }
 
-  FILE *fp = fopen (zr->filename, "rb");
+  FILE *fp = fopen (zr->file_name, "rb");
   if (!fp)
     {
-      ds_put_cstr (zr->errs, strerror (errno));
+      ds_put_format (zr->errs, _("%s: open failed (%s)"),
+                     zr->file_name, strerror (errno));
       return NULL;
     }
 
   struct zip_member *zm = xmalloc (sizeof *zm);
+  zm->file_name = xstrdup (zr->file_name);
+  zm->member_name = xstrdup (member);
   zm->fp = fp;
   zm->offset = ze->offset;
   zm->comp_size = ze->comp_size;
@@ -402,12 +465,12 @@ zip_member_open (struct zip_reader *zr, const char *member)
 
   if ( 0 != fseeko (zm->fp, zm->offset, SEEK_SET))
     {
-      ds_put_format (zr->errs, _("Failed to seek to start of member `%s': %s"),
+      ds_put_format (zr->errs, _("%s: seek failed (%s)"),
                      ze->name, strerror (errno));
       goto error;
     }
 
-  if ( ! check_magic (zm->fp, MAGIC_LHDR, zr->errs))
+  if ( ! check_magic (zm->fp, zr->file_name, MAGIC_LHDR, zr->errs))
     goto error;
 
   uint16_t v, nlen, extra_len;
@@ -437,9 +500,9 @@ zip_member_open (struct zip_reader *zr, const char *member)
   if (strcmp (name, ze->name) != 0)
     {
       ds_put_format (zm->errmsgs,
-                    _("Name mismatch in zip archive. Central directory "
-                       "says `%s'; local file header says `%s'"),
-                    ze->name, name);
+                    _("%s: name mismatch betwen central directory (%s) "
+                       "and local file header (%s)"),
+                     zm->file_name, ze->name, name);
       free (name);
       goto error;
     }
@@ -454,6 +517,8 @@ zip_member_open (struct zip_reader *zr, const char *member)
 
 error:
   fclose (zm->fp);
+  free (zm->file_name);
+  free (zm->member_name);
   free (zm);
   return NULL;
 }
@@ -627,7 +692,9 @@ inflate_init (struct zip_member *zm)
 
   if ( Z_OK != r)
     {
-      ds_put_format (zm->errmsgs, _("Cannot initialize inflator: %s"), zError (r));
+      ds_put_format (zm->errmsgs,
+                     _("%s: cannot initialize inflator (%s)"),
+                     zm->file_name, zError (r));
       return false;
     }
 
@@ -681,7 +748,8 @@ inflate_read (struct zip_member *zm, void *buf, size_t n)
       return n - inf->zss.avail_out;
     }
 
-  ds_put_format (zm->errmsgs, _("Error inflating: %s"), zError (r));
+  ds_put_format (zm->errmsgs, _("%s: error inflating \"%s\" (%s)"),
+                 zm->file_name, zm->member_name, zError (r));
 
   return -1;
 }