work on docs
[pspp] / utilities / pspp-dump-sav.c
index aeb64866504c59a857778661dbcb9d252ad6b258..36cb285380f564c93edbc19be87d4f6383ba43bb 100644 (file)
@@ -37,6 +37,7 @@
 #include "gl/progname.h"
 #include "gl/version-etc.h"
 #include "gl/xalloc.h"
+#include "gl/xsize.h"
 
 #define ID_MAX_LEN 64
 
@@ -99,7 +100,7 @@ static void read_simple_compressed_data (struct sfm_reader *, int max_cases);
 static void read_zlib_compressed_data (struct sfm_reader *);
 
 static struct text_record *open_text_record (
-  struct sfm_reader *, size_t size);
+  struct sfm_reader *, size_t size, size_t count);
 static void close_text_record (struct text_record *);
 static bool read_variable_to_value_pair (struct text_record *,
                                          char **key, char **value);
@@ -235,6 +236,8 @@ main (int argc, char *argv[])
       else if (r.compression == COMP_ZLIB)
         read_zlib_compressed_data (&r);
 
+      free (r.var_widths);
+
       fclose (r.file);
     }
 
@@ -400,7 +403,7 @@ read_variable_record (struct sfm_reader *r)
   char name[9];
 
   printf ("%08llx: variable record #%d\n",
-          (long long int) ftello (r->file), r->n_variable_records++);
+          (long long int) ftello (r->file), ++r->n_variable_records);
 
   width = read_int (r);
   has_variable_label = read_int (r);
@@ -516,14 +519,14 @@ print_untyped_value (struct sfm_reader *r, char raw_value[8])
 static void
 read_value_label_record (struct sfm_reader *r)
 {
-  int label_cnt, var_cnt;
+  int n_labels, n_vars;
   int i;
 
   printf ("%08llx: value labels record\n", (long long int) ftello (r->file));
 
   /* Read number of labels. */
-  label_cnt = read_int (r);
-  for (i = 0; i < label_cnt; i++)
+  n_labels = read_int (r);
+  for (i = 0; i < n_labels; i++)
     {
       char raw_value[8];
       unsigned char label_len;
@@ -556,8 +559,8 @@ read_value_label_record (struct sfm_reader *r)
   /* Read number of variables associated with value label from type 4
      record. */
   printf ("\t%08llx: apply to variables", (long long int) ftello (r->file));
-  var_cnt = read_int (r);
-  for (i = 0; i < var_cnt; i++)
+  n_vars = read_int (r);
+  for (i = 0; i < n_vars; i++)
     printf (" #%d", read_int (r));
   putchar ('\n');
 }
@@ -735,7 +738,7 @@ read_extra_product_info (struct sfm_reader *r,
   const char *s;
 
   printf ("%08llx: extra product info\n", (long long int) ftello (r->file));
-  text = open_text_record (r, size * count);
+  text = open_text_record (r, size, count);
   s = text_get_all (text);
   print_string (s, strlen (s));
   close_text_record (text);
@@ -749,7 +752,7 @@ read_mrsets (struct sfm_reader *r, size_t size, size_t count)
 
   printf ("%08llx: multiple response sets\n",
           (long long int) ftello (r->file));
-  text = open_text_record (r, size * count);
+  text = open_text_record (r, size, count);
   for (;;)
     {
       const char *name;
@@ -909,7 +912,7 @@ read_long_var_name_map (struct sfm_reader *r, size_t size, size_t count)
 
   printf ("%08llx: long variable names (short => long)\n",
           (long long int) ftello (r->file));
-  text = open_text_record (r, size * count);
+  text = open_text_record (r, size, count);
   while (read_variable_to_value_pair (text, &var, &long_name))
     printf ("\t%s => %s\n", var, long_name);
   close_text_record (text);
@@ -926,7 +929,7 @@ read_long_string_map (struct sfm_reader *r, size_t size, size_t count)
 
   printf ("%08llx: very long strings (variable => length)\n",
           (long long int) ftello (r->file));
-  text = open_text_record (r, size * count);
+  text = open_text_record (r, size, count);
   while (read_variable_to_value_pair (text, &var, &length_s))
     printf ("\t%s => %d\n", var, atoi (length_s));
   close_text_record (text);
@@ -1004,7 +1007,7 @@ read_datafile_attributes (struct sfm_reader *r, size_t size, size_t count)
   struct text_record *text;
 
   printf ("%08llx: datafile attributes\n", (long long int) ftello (r->file));
-  text = open_text_record (r, size * count);
+  text = open_text_record (r, size, count);
   read_attributes (r, text, "datafile");
   close_text_record (text);
 }
@@ -1017,6 +1020,8 @@ read_character_encoding (struct sfm_reader *r, size_t size, size_t count)
   read_string (r, encoding, count + 1);
 
   printf ("%08llx: Character Encoding: %s\n", posn, encoding);
+
+  free (encoding);
 }
 
 static void
@@ -1196,7 +1201,7 @@ read_variable_attributes (struct sfm_reader *r, size_t size, size_t count)
   struct text_record *text;
 
   printf ("%08llx: variable attributes\n", (long long int) ftello (r->file));
-  text = open_text_record (r, size * count);
+  text = open_text_record (r, size, count);
   for (;;)
     {
       const char *variable = text_tokenize (text, ':');
@@ -1226,7 +1231,7 @@ read_simple_compressed_data (struct sfm_reader *r, int max_cases)
     {
       printf ("%08llx: case %d's uncompressible data begins\n",
               (long long int) ftello (r->file), case_num);
-      for (i = 0; i < r->n_var_widths; )
+      for (i = 0; i < r->n_var_widths;)
         {
           int width = r->var_widths[i];
           char raw_value[8];
@@ -1389,18 +1394,23 @@ struct text_record
     size_t pos;                 /* Current position in buffer. */
   };
 
-/* Reads SIZE bytes into a text record for R,
+/* Reads SIZE * COUNT bytes into a text record for R,
    and returns the new text record. */
 static struct text_record *
-open_text_record (struct sfm_reader *r, size_t size)
+open_text_record (struct sfm_reader *r, size_t size, size_t count)
 {
   struct text_record *text = xmalloc (sizeof *text);
-  char *buffer = xmalloc (size + 1);
-  read_bytes (r, buffer, size);
-  buffer[size] = '\0';
+
+  if (size_overflow_p (xsum (1, xtimes (size, count))))
+    sys_error (r, "Extension record too large.");
+
+  size_t n_bytes = size * count;
+  char *buffer = xmalloc (n_bytes + 1);
+  read_bytes (r, buffer, n_bytes);
+  buffer[n_bytes] = '\0';
   text->reader = r;
   text->buffer = buffer;
-  text->size = size;
+  text->size = n_bytes;
   text->pos = 0;
   return text;
 }
@@ -1580,10 +1590,10 @@ sys_error (struct sfm_reader *r, const char *format, ...)
    too. */
 static inline bool
 read_bytes_internal (struct sfm_reader *r, bool eof_is_ok,
-                     void *buf, size_t byte_cnt)
+                     void *buf, size_t n_bytes)
 {
-  size_t bytes_read = fread (buf, 1, byte_cnt, r->file);
-  if (bytes_read == byte_cnt)
+  size_t bytes_read = fread (buf, 1, n_bytes, r->file);
+  if (bytes_read == n_bytes)
     return true;
   else if (ferror (r->file))
     sys_error (r, "System error: %s.", strerror (errno));
@@ -1596,9 +1606,9 @@ read_bytes_internal (struct sfm_reader *r, bool eof_is_ok,
 /* Reads BYTE_CNT into BUF.
    Aborts upon I/O error or if end-of-file is encountered. */
 static void
-read_bytes (struct sfm_reader *r, void *buf, size_t byte_cnt)
+read_bytes (struct sfm_reader *r, void *buf, size_t n_bytes)
 {
-  read_bytes_internal (r, false, buf, byte_cnt);
+  read_bytes_internal (r, false, buf, n_bytes);
 }
 
 /* Reads BYTE_CNT bytes into BUF.
@@ -1606,9 +1616,9 @@ read_bytes (struct sfm_reader *r, void *buf, size_t byte_cnt)
    Returns false if an immediate end-of-file is encountered.
    Aborts if an I/O error or a partial read occurs. */
 static bool
-try_read_bytes (struct sfm_reader *r, void *buf, size_t byte_cnt)
+try_read_bytes (struct sfm_reader *r, void *buf, size_t n_bytes)
 {
-  return read_bytes_internal (r, true, buf, byte_cnt);
+  return read_bytes_internal (r, true, buf, n_bytes);
 }
 
 /* Reads a 32-bit signed integer from R and returns its value in