track axes
[pspp] / src / libpspp / zip-reader.c
index 7b5aabadfbf870c9e54899984f33adb3d0edba91..00249e0bae41be64128720410c0079c7e819a230 100644 (file)
@@ -1,5 +1,5 @@
 /* PSPP - a program for statistical analysis.
-   Copyright (C) 2011, 2013, 2014 Free Software Foundation, Inc.
+   Copyright (C) 2011, 2013, 2014, 2021 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
 
 #include <config.h>
 
+#include "libpspp/zip-reader.h"
+#include "libpspp/zip-private.h"
+
+#include <errno.h>
 #include <inttypes.h>
 #include <stdbool.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 
-#include <errno.h>
-#include <xalloc.h>
-#include <libpspp/assertion.h>
-#include <libpspp/compiler.h>
-
-#include "str.h"
+#include "libpspp/assertion.h"
+#include "libpspp/cast.h"
+#include "libpspp/compiler.h"
+#include "libpspp/integer-format.h"
+#include "libpspp/str.h"
 
-#include "integer-format.h"
-#include "zip-reader.h"
-#include "zip-private.h"
+#include "gl/xalloc.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
@@ -81,6 +82,7 @@ get_decompressor (uint16_t c)
 
 struct zip_reader
 {
+  int ref_cnt;
   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. */
@@ -116,17 +118,28 @@ zip_member_finish (struct zip_member *zm)
     }
 }
 
+struct zip_reader *
+zip_reader_ref (const struct zip_reader *zr_)
+{
+  struct zip_reader *zr = CONST_CAST (struct zip_reader *, zr_);
+  assert (zr->ref_cnt > 0);
+  zr->ref_cnt++;
+  return zr;
+}
+
 /* Destroy the zip reader */
 void
-zip_reader_destroy (struct zip_reader *zr)
+zip_reader_unref (struct zip_reader *zr)
 {
-  int i;
   if (zr == NULL)
     return;
+  assert (zr->ref_cnt > 0);
+  if (--zr->ref_cnt)
+    return;
 
   free (zr->file_name);
 
-  for (i = 0; i < zr->n_entries; ++i)
+  for (int i = 0; i < zr->n_entries; ++i)
     {
       struct zip_entry *ze = &zr->entries[i];
       free (ze->name);
@@ -365,7 +378,8 @@ zip_reader_create (const char *file_name, struct zip_reader **zrp)
       return NULL;
     }
 
-  struct zip_reader *zr = xzalloc (sizeof *zr);
+  struct zip_reader *zr = XZALLOC (struct zip_reader);
+  zr->ref_cnt = 1;
   zr->file_name = xstrdup (file_name);
   zr->entries = xcalloc (n_members, sizeof *zr->entries);
   for (int i = 0; i < n_members; i++)
@@ -375,7 +389,7 @@ zip_reader_create (const char *file_name, struct zip_reader **zrp)
       if (error)
         {
           fclose (file);
-          zip_reader_destroy (zr);
+          zip_reader_unref (zr);
           return error;
         }
       zr->n_entries++;
@@ -563,7 +577,7 @@ probe_magic (FILE *fp, uint32_t magic, off_t start, off_t stop, off_t *off)
 
   if (0 > fseeko (fp, start, SEEK_SET))
     {
-      return -1;
+      return false;
     }
 
   for (i = 0; i < 4 ; ++i)
@@ -654,7 +668,7 @@ static char *
 inflate_init (struct zip_member *zm)
 {
   int r;
-  struct inflator *inf = xzalloc (sizeof *inf);
+  struct inflator *inf = XZALLOC (struct inflator);
 
   uint16_t flg = 0 ;
   uint16_t cmf = 0x8; /* Always 8 for inflate */