cairo: Add missing new-lines at the end of messages written to stderr.
[pspp] / src / libpspp / zip-reader.c
index 52d2b029e785a4c02d3084c6d8785768a56bacfd..679bdc69ebca1aee79beceae7767f1bb92f2695d 100644 (file)
@@ -220,11 +220,11 @@ zip_member_read (struct zip_member *zm, void *buf, size_t bytes)
 
   ds_clear (zm->errmsgs);
 
-  if ( bytes > zm->bytes_unread)
+  if (bytes > zm->bytes_unread)
     bytes = zm->bytes_unread;
 
   bytes_read  = zm->decompressor->read (zm, buf, bytes);
-  if ( bytes_read < 0)
+  if (bytes_read < 0)
     return bytes_read;
 
   zm->bytes_unread -= bytes_read;
@@ -232,6 +232,39 @@ 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);
+    }
+
+  *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
@@ -248,7 +281,7 @@ zip_header_read_next (FILE *file, const char *file_name,
   uint32_t eattr;
   uint16_t comp_type;
 
-  if ( ! check_magic (file, file_name, MAGIC_SOCD, errs))
+  if (! check_magic (file, file_name, MAGIC_SOCD, errs))
     return false;
 
   if (! get_u16 (file, &v)) return false;
@@ -287,7 +320,7 @@ zip_reader_create (const char *file_name, struct string *errs)
 
   struct zip_reader *zr = xzalloc (sizeof *zr);
   zr->errs = errs;
-  if ( zr->errs)
+  if (zr->errs)
     ds_init_empty (zr->errs);
 
   FILE *file = fopen (file_name, "rb");
@@ -299,14 +332,14 @@ zip_reader_create (const char *file_name, struct string *errs)
       return NULL;
     }
 
-  if ( ! check_magic (file, file_name, MAGIC_LHDR, zr->errs))
+  if (! check_magic (file, file_name, MAGIC_LHDR, zr->errs))
     {
       fclose (file);
       free (zr);
       return NULL;
     }
 
-  if ( ! find_eocd (file, &offset))
+  if (! find_eocd (file, &offset))
     {
       ds_put_format (zr->errs, _("%s: cannot find central directory"),
                      file_name);
@@ -315,7 +348,7 @@ zip_reader_create (const char *file_name, struct string *errs)
       return NULL;
     }
 
-  if ( 0 != fseeko (file, offset, SEEK_SET))
+  if (0 != fseeko (file, offset, SEEK_SET))
     {
       ds_put_format (zr->errs, _("%s: seek failed (%s)"),
                      file_name, strerror (errno));
@@ -325,7 +358,7 @@ zip_reader_create (const char *file_name, struct string *errs)
     }
 
 
-  if ( ! check_magic (file, file_name, MAGIC_EOCD, zr->errs))
+  if (! check_magic (file, file_name, MAGIC_EOCD, zr->errs))
     {
       fclose (file);
       free (zr);
@@ -346,7 +379,7 @@ zip_reader_create (const char *file_name, struct string *errs)
       return NULL;
     }
 
-  if ( 0 != fseeko (file, central_dir_start, SEEK_SET))
+  if (0 != fseeko (file, central_dir_start, SEEK_SET))
     {
       ds_put_format (zr->errs, _("%s: seek failed (%s)"),
                      file_name, strerror (errno));
@@ -374,7 +407,7 @@ zip_reader_create (const char *file_name, 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)
     {
@@ -391,12 +424,19 @@ 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)
 {
   struct zip_entry *ze = zip_entry_find (zr, member);
-  if ( ze == NULL)
+  if (ze == NULL)
     {
       ds_put_format (zr->errs, _("%s: unknown member \"%s\""),
                      zr->file_name, member);
@@ -423,14 +463,14 @@ zip_member_open (struct zip_reader *zr, const char *member)
   zm->errmsgs = zr->errs;
   zm->aux = NULL;
 
-  if ( 0 != fseeko (zm->fp, zm->offset, SEEK_SET))
+  if (0 != fseeko (zm->fp, zm->offset, SEEK_SET))
     {
       ds_put_format (zr->errs, _("%s: seek failed (%s)"),
                      ze->name, strerror (errno));
       goto error;
     }
 
-  if ( ! check_magic (zm->fp, zr->file_name, MAGIC_LHDR, zr->errs))
+  if (! check_magic (zm->fp, zr->file_name, MAGIC_LHDR, zr->errs))
     goto error;
 
   uint16_t v, nlen, extra_len;
@@ -460,7 +500,7 @@ zip_member_open (struct zip_reader *zr, const char *member)
   if (strcmp (name, ze->name) != 0)
     {
       ds_put_format (zm->errmsgs,
-                    _("%s: name mismatch betwen central directory (%s) "
+                    _("%s: name mismatch between central directory (%s) "
                        "and local file header (%s)"),
                      zm->file_name, ze->name, name);
       free (name);
@@ -470,7 +510,7 @@ zip_member_open (struct zip_reader *zr, const char *member)
 
   skip_bytes (zm->fp, extra_len);
 
-  if (!zm->decompressor->init (zm) )
+  if (!zm->decompressor->init (zm))
     goto error;
 
   return zm;
@@ -503,7 +543,7 @@ find_eocd (FILE *fp, off_t *off)
      because that is the minimum length of the EndOfCentralDirectory
      record.
    */
-  if ( 0 > fseeko (fp, -22, SEEK_END))
+  if (0 > fseeko (fp, -22, SEEK_END))
     {
       return false;
     }
@@ -513,12 +553,12 @@ find_eocd (FILE *fp, off_t *off)
     {
       found = probe_magic (fp, magic, start, stop, off);
       /* FIXME: For extra confidence lookup the directory start record here*/
-      if ( start == 0)
+      if (start == 0)
        break;
       stop = start + sizeof (magic);
       start >>= 1;
     }
-  while (!found );
+  while (!found);
 
   return found;
 }
@@ -537,7 +577,7 @@ probe_magic (FILE *fp, uint32_t magic, off_t start, off_t stop, off_t *off)
   unsigned char seq[4];
   unsigned char byte;
 
-  if ( 0 > fseeko (fp, start, SEEK_SET))
+  if (0 > fseeko (fp, start, SEEK_SET))
     {
       return -1;
     }
@@ -552,18 +592,18 @@ probe_magic (FILE *fp, uint32_t magic, off_t start, off_t stop, off_t *off)
       if (1 != fread (&byte, 1, 1, fp))
        break;
 
-      if ( byte == seq[state])
+      if (byte == seq[state])
        state++;
       else
        state = 0;
 
-      if ( state == 4)
+      if (state == 4)
        {
          *off = ftello (fp) - 4;
          return true;
        }
       start++;
-      if ( start >= stop)
+      if (start >= stop)
        break;
     }
   while (!feof (fp));
@@ -637,7 +677,7 @@ inflate_init (struct zip_member *zm)
   cmf |= cinfo << 4;     /* Put cinfo into the high nibble */
 
   /* make these into a 16 bit word */
-  inf->cmf_flg = (cmf << 8 ) | flg;
+  inf->cmf_flg = (cmf << 8) | flg;
 
   /* Set the check bits */
   inf->cmf_flg += 31 - (inf->cmf_flg % 31);
@@ -650,7 +690,7 @@ inflate_init (struct zip_member *zm)
   inf->zss.opaque = Z_NULL;
   r = inflateInit (&inf->zss);
 
-  if ( Z_OK != r)
+  if (Z_OK != r)
     {
       ds_put_format (zm->errmsgs,
                      _("%s: cannot initialize inflator (%s)"),
@@ -675,7 +715,7 @@ inflate_read (struct zip_member *zm, void *buf, size_t n)
       int bytes_to_read;
       int pad = 0;
 
-      if ( inf->state == 0)
+      if (inf->state == 0)
        {
          inf->ucomp[1] = inf->cmf_flg ;
          inf->ucomp[0] = inf->cmf_flg >> 8 ;
@@ -703,7 +743,7 @@ inflate_read (struct zip_member *zm, void *buf, size_t n)
   inf->zss.next_out = buf;
 
   r = inflate (&inf->zss, Z_NO_FLUSH);
-  if ( Z_OK == r)
+  if (Z_OK == r)
     {
       return n - inf->zss.avail_out;
     }