zip-reader: New function zip_member_read_all().
authorBen Pfaff <blp@cs.stanford.edu>
Mon, 26 Nov 2018 00:20:29 +0000 (16:20 -0800)
committerBen Pfaff <blp@cs.stanford.edu>
Sun, 10 Feb 2019 00:00:47 +0000 (16:00 -0800)
This will have its first user in an upcoming commit.

src/libpspp/zip-reader.c
src/libpspp/zip-reader.h

index 52d2b029e785a4c02d3084c6d8785768a56bacfd..d46af3415f14f7cd425d2dee921a20b8d23bf1bb 100644 (file)
@@ -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
index 225c5fecc5ad257e3635f7bcfec5b8c82d0d3f10..7293aba3185ff18383d08bf14b5e951327daaa3e 100644 (file)
@@ -43,6 +43,12 @@ struct zip_member *zip_member_open (struct zip_reader *zr, const char *member);
    Returns the number of bytes read, or -1 on error */
 int zip_member_read (struct zip_member *zm, void *buf, size_t n);
 
+/* 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 *zip_member_read_all (struct zip_reader *, const char *member_name,
+                           void **datap, size_t *np) WARN_UNUSED_RESULT;
+
 void zip_member_finish (struct zip_member *zm);