Work on support for variable sets.
[pspp] / utilities / pspp-dump-sav.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include <ctype.h>
20 #include <errno.h>
21 #include <float.h>
22 #include <getopt.h>
23 #include <inttypes.h>
24 #include <limits.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27
28 #include "data/val-type.h"
29 #include "libpspp/cast.h"
30 #include "libpspp/compiler.h"
31 #include "libpspp/float-format.h"
32 #include "libpspp/integer-format.h"
33 #include "libpspp/misc.h"
34
35 #include "gl/error.h"
36 #include "gl/minmax.h"
37 #include "gl/progname.h"
38 #include "gl/version-etc.h"
39 #include "gl/xalloc.h"
40 #include "gl/xsize.h"
41
42 #define ID_MAX_LEN 64
43
44 enum compression
45   {
46     COMP_NONE,
47     COMP_SIMPLE,
48     COMP_ZLIB
49   };
50
51 struct sfm_reader
52   {
53     const char *file_name;
54     FILE *file;
55
56     int n_variable_records, n_variables;
57
58     int *var_widths;
59     size_t n_var_widths, allocated_var_widths;
60
61     enum integer_format integer_format;
62     enum float_format float_format;
63
64     enum compression compression;
65     double bias;
66   };
67
68 static void read_header (struct sfm_reader *);
69 static void read_variable_record (struct sfm_reader *);
70 static void read_value_label_record (struct sfm_reader *);
71 static void read_document_record (struct sfm_reader *);
72 static void read_extension_record (struct sfm_reader *);
73 static void read_machine_integer_info (struct sfm_reader *,
74                                        size_t size, size_t count);
75 static void read_machine_float_info (struct sfm_reader *,
76                                      size_t size, size_t count);
77 static void read_extra_product_info (struct sfm_reader *,
78                                      size_t size, size_t count);
79 static void read_variable_sets (struct sfm_reader *, size_t size, size_t count);
80 static void read_mrsets (struct sfm_reader *, size_t size, size_t count);
81 static void read_display_parameters (struct sfm_reader *,
82                                      size_t size, size_t count);
83 static void read_long_var_name_map (struct sfm_reader *r,
84                                     size_t size, size_t count);
85 static void read_long_string_map (struct sfm_reader *r,
86                                   size_t size, size_t count);
87 static void read_datafile_attributes (struct sfm_reader *r,
88                                       size_t size, size_t count);
89 static void read_variable_attributes (struct sfm_reader *r,
90                                       size_t size, size_t count);
91 static void read_ncases64 (struct sfm_reader *, size_t size, size_t count);
92 static void read_character_encoding (struct sfm_reader *r,
93                                        size_t size, size_t count);
94 static void read_long_string_value_labels (struct sfm_reader *r,
95                                            size_t size, size_t count);
96 static void read_long_string_missing_values (struct sfm_reader *r,
97                                              size_t size, size_t count);
98 static void read_unknown_extension (struct sfm_reader *,
99                                     size_t size, size_t count);
100 static void read_simple_compressed_data (struct sfm_reader *, int max_cases);
101 static void read_zlib_compressed_data (struct sfm_reader *);
102
103 static struct text_record *open_text_record (
104   struct sfm_reader *, size_t size, size_t count);
105 static void close_text_record (struct text_record *);
106 static bool read_variable_to_value_pair (struct text_record *,
107                                          char **key, char **value);
108 static char *text_tokenize (struct text_record *, int delimiter);
109 static bool text_match (struct text_record *text, int c);
110 static const char *text_parse_counted_string (struct text_record *);
111 static size_t text_pos (const struct text_record *);
112 static const char *text_get_all (const struct text_record *);
113
114 static void usage (void);
115 static void sys_warn (struct sfm_reader *, const char *, ...)
116      PRINTF_FORMAT (2, 3);
117 static void sys_error (struct sfm_reader *, const char *, ...)
118      PRINTF_FORMAT (2, 3)
119      NO_RETURN;
120
121 static void read_bytes (struct sfm_reader *, void *, size_t);
122 static bool try_read_bytes (struct sfm_reader *, void *, size_t);
123 static int read_int (struct sfm_reader *);
124 static int64_t read_int64 (struct sfm_reader *);
125 static double read_float (struct sfm_reader *);
126 static void read_string (struct sfm_reader *, char *, size_t);
127 static void skip_bytes (struct sfm_reader *, size_t);
128 static void trim_spaces (char *);
129
130 static void print_string (const char *s, size_t len);
131
132 int
133 main (int argc, char *argv[])
134 {
135   int max_cases = 0;
136   struct sfm_reader r;
137   int i;
138
139   set_program_name (argv[0]);
140
141   for (;;)
142     {
143       static const struct option long_options[] =
144         {
145           { "data",    optional_argument, NULL, 'd' },
146           { "help",    no_argument,       NULL, 'h' },
147           { "version", no_argument,       NULL, 'v' },
148           { NULL,      0,                 NULL, 0 },
149         };
150
151       int c;
152
153       c = getopt_long (argc, argv, "d::hv", long_options, NULL);
154       if (c == -1)
155         break;
156
157       switch (c)
158         {
159         case 'd':
160           max_cases = optarg ? atoi (optarg) : INT_MAX;
161           break;
162
163         case 'v':
164           version_etc (stdout, "pspp-dump-sav", PACKAGE_NAME, PACKAGE_VERSION,
165                        "Ben Pfaff", "John Darrington", NULL_SENTINEL);
166           exit (EXIT_SUCCESS);
167
168         case 'h':
169           usage ();
170           exit (EXIT_SUCCESS);
171
172         default:
173           exit (EXIT_FAILURE);
174         }
175     }
176
177   if (optind == argc)
178     error (1, 0, "at least one non-option argument is required; "
179            "use --help for help");
180
181   for (i = optind; i < argc; i++)
182     {
183       int rec_type;
184
185       r.file_name = argv[i];
186       r.file = fopen (r.file_name, "rb");
187       if (r.file == NULL)
188         error (EXIT_FAILURE, errno, "error opening `%s'", r.file_name);
189       r.n_variable_records = 0;
190       r.n_variables = 0;
191       r.n_var_widths = 0;
192       r.allocated_var_widths = 0;
193       r.var_widths = 0;
194       r.compression = COMP_NONE;
195
196       if (argc - optind > 1)
197         printf ("Reading \"%s\":\n", r.file_name);
198
199       read_header (&r);
200       while ((rec_type = read_int (&r)) != 999)
201         {
202           switch (rec_type)
203             {
204             case 2:
205               read_variable_record (&r);
206               break;
207
208             case 3:
209               read_value_label_record (&r);
210               break;
211
212             case 4:
213               sys_error (&r, "Misplaced type 4 record.");
214
215             case 6:
216               read_document_record (&r);
217               break;
218
219             case 7:
220               read_extension_record (&r);
221               break;
222
223             default:
224               sys_error (&r, "Unrecognized record type %d.", rec_type);
225             }
226         }
227       printf ("%08llx: end-of-dictionary record "
228               "(first byte of data at %08llx)\n",
229               (long long int) ftello (r.file),
230               (long long int) ftello (r.file) + 4);
231
232       if (r.compression == COMP_SIMPLE)
233         {
234           if (max_cases > 0)
235             read_simple_compressed_data (&r, max_cases);
236         }
237       else if (r.compression == COMP_ZLIB)
238         read_zlib_compressed_data (&r);
239
240       free (r.var_widths);
241
242       fclose (r.file);
243     }
244
245   return 0;
246 }
247
248 static void
249 read_header (struct sfm_reader *r)
250 {
251   char rec_type[5];
252   char eye_catcher[61];
253   uint8_t raw_layout_code[4];
254   int32_t layout_code;
255   int32_t compressed;
256   int32_t weight_index;
257   int32_t ncases;
258   uint8_t raw_bias[8];
259   char creation_date[10];
260   char creation_time[9];
261   char file_label[65];
262   bool zmagic;
263
264   read_string (r, rec_type, sizeof rec_type);
265   read_string (r, eye_catcher, sizeof eye_catcher);
266
267   if (!strcmp ("$FL2", rec_type))
268     zmagic = false;
269   else if (!strcmp ("$FL3", rec_type))
270     zmagic = true;
271   else
272     sys_error (r, "This is not an SPSS system file.");
273
274   /* Identify integer format. */
275   read_bytes (r, raw_layout_code, sizeof raw_layout_code);
276   if ((!integer_identify (2, raw_layout_code, sizeof raw_layout_code,
277                           &r->integer_format)
278        && !integer_identify (3, raw_layout_code, sizeof raw_layout_code,
279                              &r->integer_format))
280       || (r->integer_format != INTEGER_MSB_FIRST
281           && r->integer_format != INTEGER_LSB_FIRST))
282     sys_error (r, "This is not an SPSS system file.");
283   layout_code = integer_get (r->integer_format,
284                              raw_layout_code, sizeof raw_layout_code);
285
286   read_int (r);                 /* Nominal case size (not actually useful). */
287   compressed = read_int (r);
288   weight_index = read_int (r);
289   ncases = read_int (r);
290
291   if (!zmagic)
292     {
293       if (compressed == 0)
294         r->compression = COMP_NONE;
295       else if (compressed == 1)
296         r->compression = COMP_SIMPLE;
297       else if (compressed != 0)
298         sys_error (r, "SAV file header has invalid compression value "
299                    "%"PRId32".", compressed);
300     }
301   else
302     {
303       if (compressed == 2)
304         r->compression = COMP_ZLIB;
305       else
306         sys_error (r, "ZSAV file header has invalid compression value "
307                    "%"PRId32".", compressed);
308     }
309
310   /* Identify floating-point format and obtain compression bias. */
311   read_bytes (r, raw_bias, sizeof raw_bias);
312   if (float_identify (100.0, raw_bias, sizeof raw_bias, &r->float_format) == 0)
313     {
314       sys_warn (r, "Compression bias is not the usual value of 100, or system "
315                 "file uses unrecognized floating-point format.");
316       if (r->integer_format == INTEGER_MSB_FIRST)
317         r->float_format = FLOAT_IEEE_DOUBLE_BE;
318       else
319         r->float_format = FLOAT_IEEE_DOUBLE_LE;
320     }
321   r->bias = float_get_double (r->float_format, raw_bias);
322
323   read_string (r, creation_date, sizeof creation_date);
324   read_string (r, creation_time, sizeof creation_time);
325   read_string (r, file_label, sizeof file_label);
326   trim_spaces (file_label);
327   skip_bytes (r, 3);
328
329   printf ("File header record:\n");
330   printf ("\t%17s: %s\n", "Product name", eye_catcher);
331   printf ("\t%17s: %"PRId32"\n", "Layout code", layout_code);
332   printf ("\t%17s: %"PRId32" (%s)\n", "Compressed",
333           compressed,
334           r->compression == COMP_NONE ? "no compression"
335           : r->compression == COMP_SIMPLE ? "simple compression"
336           : r->compression == COMP_ZLIB ? "ZLIB compression"
337           : "<error>");
338   printf ("\t%17s: %"PRId32"\n", "Weight index", weight_index);
339   printf ("\t%17s: %"PRId32"\n", "Number of cases", ncases);
340   printf ("\t%17s: %.*g\n", "Compression bias", DBL_DIG + 1, r->bias);
341   printf ("\t%17s: %s\n", "Creation date", creation_date);
342   printf ("\t%17s: %s\n", "Creation time", creation_time);
343   printf ("\t%17s: \"%s\"\n", "File label", file_label);
344 }
345
346 static const char *
347 format_name (int format)
348 {
349   switch ((format >> 16) & 0xff)
350     {
351     case 1: return "A";
352     case 2: return "AHEX";
353     case 3: return "COMMA";
354     case 4: return "DOLLAR";
355     case 5: return "F";
356     case 6: return "IB";
357     case 7: return "PIBHEX";
358     case 8: return "P";
359     case 9: return "PIB";
360     case 10: return "PK";
361     case 11: return "RB";
362     case 12: return "RBHEX";
363     case 15: return "Z";
364     case 16: return "N";
365     case 17: return "E";
366     case 20: return "DATE";
367     case 21: return "TIME";
368     case 22: return "DATETIME";
369     case 23: return "ADATE";
370     case 24: return "JDATE";
371     case 25: return "DTIME";
372     case 26: return "WKDAY";
373     case 27: return "MONTH";
374     case 28: return "MOYR";
375     case 29: return "QYR";
376     case 30: return "WKYR";
377     case 31: return "PCT";
378     case 32: return "DOT";
379     case 33: return "CCA";
380     case 34: return "CCB";
381     case 35: return "CCC";
382     case 36: return "CCD";
383     case 37: return "CCE";
384     case 38: return "EDATE";
385     case 39: return "SDATE";
386     case 40: return "MTIME";
387     case 41: return "YMDHMS";
388     default: return "invalid";
389     }
390 }
391
392 /* Reads a variable (type 2) record from R and adds the
393    corresponding variable to DICT.
394    Also skips past additional variable records for long string
395    variables. */
396 static void
397 read_variable_record (struct sfm_reader *r)
398 {
399   int width;
400   int has_variable_label;
401   int missing_value_code;
402   int print_format;
403   int write_format;
404   char name[9];
405
406   printf ("%08llx: variable record #%d\n",
407           (long long int) ftello (r->file), ++r->n_variable_records);
408
409   width = read_int (r);
410   has_variable_label = read_int (r);
411   missing_value_code = read_int (r);
412   print_format = read_int (r);
413   write_format = read_int (r);
414   read_string (r, name, sizeof name);
415   name[strcspn (name, " ")] = '\0';
416
417   if (width >= 0)
418     r->n_variables++;
419
420   if (r->n_var_widths >= r->allocated_var_widths)
421     r->var_widths = x2nrealloc (r->var_widths, &r->allocated_var_widths,
422                                 sizeof *r->var_widths);
423   r->var_widths[r->n_var_widths++] = width;
424
425   printf ("\tWidth: %d (%s)\n",
426           width,
427           width > 0 ? "string"
428           : width == 0 ? "numeric"
429           : "long string continuation record");
430   printf ("\tVariable label: %d\n", has_variable_label);
431   printf ("\tMissing values code: %d (%s)\n", missing_value_code,
432           (missing_value_code == 0 ? "no missing values"
433            : missing_value_code == 1 ? "one missing value"
434            : missing_value_code == 2 ? "two missing values"
435            : missing_value_code == 3 ? "three missing values"
436            : missing_value_code == -2 ? "one missing value range"
437            : missing_value_code == -3 ? "one missing value, one range"
438            : "bad value"));
439   printf ("\tPrint format: %06x (%s%d.%d)\n",
440           print_format, format_name (print_format),
441           (print_format >> 8) & 0xff, print_format & 0xff);
442   printf ("\tWrite format: %06x (%s%d.%d)\n",
443           write_format, format_name (write_format),
444           (write_format >> 8) & 0xff, write_format & 0xff);
445   printf ("\tName: %s\n", name);
446
447   /* Get variable label, if any. */
448   if (has_variable_label != 0 && has_variable_label != 1)
449     sys_error (r, "Variable label indicator field is not 0 or 1.");
450   if (has_variable_label == 1)
451     {
452       long long int offset = ftello (r->file);
453       enum { MAX_LABEL_LEN = 65536 };
454
455       size_t len = read_int (r);
456       size_t read_len = MIN (MAX_LABEL_LEN, len);
457       char *label = xmalloc (read_len + 1);
458       read_string (r, label, read_len + 1);
459       printf("\t%08llx Variable label: \"%s\"\n", offset, label);
460       free (label);
461
462       /* Skip label padding up to multiple of 4 bytes. */
463       skip_bytes (r, ROUND_UP (len, 4) - len);
464     }
465
466   /* Set missing values. */
467   if (missing_value_code != 0)
468     {
469       int i;
470
471       printf ("\t%08llx Missing values:", (long long int) ftello (r->file));
472       if (!width)
473         {
474           if (missing_value_code < -3 || missing_value_code > 3
475               || missing_value_code == -1)
476             sys_error (r, "Numeric missing value indicator field is not "
477                        "-3, -2, 0, 1, 2, or 3.");
478           if (missing_value_code < 0)
479             {
480               double low = read_float (r);
481               double high = read_float (r);
482               printf (" %.*g...%.*g", DBL_DIG + 1, low, DBL_DIG + 1, high);
483               missing_value_code = -missing_value_code - 2;
484             }
485           for (i = 0; i < missing_value_code; i++)
486             printf (" %.*g", DBL_DIG + 1, read_float (r));
487         }
488       else if (width > 0)
489         {
490           if (missing_value_code < 1 || missing_value_code > 3)
491             sys_error (r, "String missing value indicator field is not "
492                        "0, 1, 2, or 3.");
493           for (i = 0; i < missing_value_code; i++)
494             {
495               char string[9];
496               read_string (r, string, sizeof string);
497               printf (" \"%s\"", string);
498             }
499         }
500       putchar ('\n');
501     }
502 }
503
504 static void
505 print_untyped_value (struct sfm_reader *r, char raw_value[8])
506 {
507   int n_printable;
508   double value;
509
510   value = float_get_double (r->float_format, raw_value);
511   for (n_printable = 0; n_printable < 8; n_printable++)
512     if (!isprint (raw_value[n_printable]))
513       break;
514
515   printf ("%.*g/\"%.*s\"", DBL_DIG + 1, value, n_printable, raw_value);
516 }
517
518 /* Reads value labels from sysfile R and inserts them into the
519    associated dictionary. */
520 static void
521 read_value_label_record (struct sfm_reader *r)
522 {
523   int n_labels, n_vars;
524   int i;
525
526   printf ("%08llx: value labels record\n", (long long int) ftello (r->file));
527
528   /* Read number of labels. */
529   n_labels = read_int (r);
530   for (i = 0; i < n_labels; i++)
531     {
532       char raw_value[8];
533       unsigned char label_len;
534       size_t padded_len;
535       char label[256];
536
537       read_bytes (r, raw_value, sizeof raw_value);
538
539       /* Read label length. */
540       read_bytes (r, &label_len, sizeof label_len);
541       padded_len = ROUND_UP (label_len + 1, 8);
542
543       /* Read label, padding. */
544       read_bytes (r, label, padded_len - 1);
545       label[label_len] = 0;
546
547       printf ("\t");
548       print_untyped_value (r, raw_value);
549       printf (": \"%s\"\n", label);
550     }
551
552   /* Now, read the type 4 record that has the list of variables
553      to which the value labels are to be applied. */
554
555   /* Read record type of type 4 record. */
556   if (read_int (r) != 4)
557     sys_error (r, "Variable index record (type 4) does not immediately "
558                "follow value label record (type 3) as it should.");
559
560   /* Read number of variables associated with value label from type 4
561      record. */
562   printf ("\t%08llx: apply to variables", (long long int) ftello (r->file));
563   n_vars = read_int (r);
564   for (i = 0; i < n_vars; i++)
565     printf (" #%d", read_int (r));
566   putchar ('\n');
567 }
568
569 static void
570 read_document_record (struct sfm_reader *r)
571 {
572   int n_lines;
573   int i;
574
575   printf ("%08llx: document record\n", (long long int) ftello (r->file));
576   n_lines = read_int (r);
577   printf ("\t%d lines of documents\n", n_lines);
578
579   for (i = 0; i < n_lines; i++)
580     {
581       char line[81];
582       printf ("\t%08llx: ", (long long int) ftello (r->file));
583       read_string (r, line, sizeof line);
584       trim_spaces (line);
585       printf ("line %d: \"%s\"\n", i, line);
586     }
587 }
588
589 static void
590 read_extension_record (struct sfm_reader *r)
591 {
592   long long int offset = ftello (r->file);
593   int subtype = read_int (r);
594   size_t size = read_int (r);
595   size_t count = read_int (r);
596   size_t bytes = size * count;
597
598   printf ("%08llx: Record 7, subtype %d, size=%zu, count=%zu\n",
599           offset, subtype, size, count);
600
601   switch (subtype)
602     {
603     case 3:
604       read_machine_integer_info (r, size, count);
605       return;
606
607     case 4:
608       read_machine_float_info (r, size, count);
609       return;
610
611     case 5:
612       read_variable_sets (r, size, count);
613       return;
614
615     case 6:
616       /* DATE variable information.  We don't use it yet, but we
617          should. */
618       break;
619
620     case 7:
621     case 19:
622       read_mrsets (r, size, count);
623       return;
624
625     case 10:
626       read_extra_product_info (r, size, count);
627       return;
628
629     case 11:
630       read_display_parameters (r, size, count);
631       return;
632
633     case 13:
634       read_long_var_name_map (r, size, count);
635       return;
636
637     case 14:
638       read_long_string_map (r, size, count);
639       return;
640
641     case 16:
642       read_ncases64 (r, size, count);
643       return;
644
645     case 17:
646       read_datafile_attributes (r, size, count);
647       return;
648
649     case 18:
650       read_variable_attributes (r, size, count);
651       return;
652
653     case 20:
654       read_character_encoding (r, size, count);
655       return;
656
657     case 21:
658       read_long_string_value_labels (r, size, count);
659       return;
660
661     case 22:
662       read_long_string_missing_values (r, size, count);
663       return;
664
665     default:
666       sys_warn (r, "Unrecognized record type 7, subtype %d.", subtype);
667       read_unknown_extension (r, size, count);
668       return;
669     }
670
671   skip_bytes (r, bytes);
672 }
673
674 static void
675 read_machine_integer_info (struct sfm_reader *r, size_t size, size_t count)
676 {
677   long long int offset = ftello (r->file);
678   int version_major = read_int (r);
679   int version_minor = read_int (r);
680   int version_revision = read_int (r);
681   int machine_code = read_int (r);
682   int float_representation = read_int (r);
683   int compression_code = read_int (r);
684   int integer_representation = read_int (r);
685   int character_code = read_int (r);
686
687   printf ("%08llx: machine integer info\n", offset);
688   if (size != 4 || count != 8)
689     sys_error (r, "Bad size (%zu) or count (%zu) field on record type 7, "
690                "subtype 3.", size, count);
691
692   printf ("\tVersion: %d.%d.%d\n",
693           version_major, version_minor, version_revision);
694   printf ("\tMachine code: %d\n", machine_code);
695   printf ("\tFloating point representation: %d (%s)\n",
696           float_representation,
697           float_representation == 1 ? "IEEE 754"
698           : float_representation == 2 ? "IBM 370"
699           : float_representation == 3 ? "DEC VAX"
700           : "unknown");
701   printf ("\tCompression code: %d\n", compression_code);
702   printf ("\tEndianness: %d (%s)\n", integer_representation,
703           integer_representation == 1 ? "big"
704           : integer_representation == 2 ? "little" : "unknown");
705   printf ("\tCharacter code: %d\n", character_code);
706 }
707
708 /* Read record type 7, subtype 4. */
709 static void
710 read_machine_float_info (struct sfm_reader *r, size_t size, size_t count)
711 {
712   long long int offset = ftello (r->file);
713   double sysmis = read_float (r);
714   double highest = read_float (r);
715   double lowest = read_float (r);
716
717   printf ("%08llx: machine float info\n", offset);
718   if (size != 8 || count != 3)
719     sys_error (r, "Bad size (%zu) or count (%zu) on extension 4.",
720                size, count);
721
722   printf ("\tsysmis: %.*g (%a)\n", DBL_DIG + 1, sysmis, sysmis);
723   if (sysmis != SYSMIS)
724     sys_warn (r, "File specifies unexpected value %.*g (%a) as %s.",
725               DBL_DIG + 1, sysmis, sysmis, "SYSMIS");
726
727   printf ("\thighest: %.*g (%a)\n", DBL_DIG + 1, highest, highest);
728   if (highest != HIGHEST)
729     sys_warn (r, "File specifies unexpected value %.*g (%a) as %s.",
730               DBL_DIG + 1, highest, highest, "HIGHEST");
731
732   printf ("\tlowest: %.*g (%a)\n", DBL_DIG + 1, lowest, lowest);
733   if (lowest != LOWEST && lowest != SYSMIS)
734     sys_warn (r, "File specifies unexpected value %.*g (%a) as %s.",
735               DBL_DIG + 1, lowest, lowest, "LOWEST");
736 }
737
738 /* Read record type 7, subtype 5. */
739 static void
740 read_variable_sets (struct sfm_reader *r, size_t size, size_t count)
741 {
742   printf ("%08llx: variable sets\n", (long long int) ftello (r->file));
743   struct text_record *text = open_text_record (r, size, count);
744   for (;;)
745     {
746       while (text_match (text, '\n'))
747         continue;
748
749       const char *set = text_tokenize (text, '=');
750       if (!set)
751         break;
752
753       /* Always present even for an empty set. */
754       text_match (text, ' ');
755
756       char *variables = text_tokenize (text, '\n');
757       if (!variables)
758         printf ("\tset \"%s\" is empty\n", set);
759       else
760         {
761           size_t length = strlen (variables);
762           if (variables[length - 1] == '\r')
763             variables[length - 1] = '\0';
764           printf ("\tset \"%s\" contains \"%s\"\n", set, variables);
765         }
766     }
767   close_text_record (text);
768 }
769
770 static void
771 read_extra_product_info (struct sfm_reader *r,
772                          size_t size, size_t count)
773 {
774   struct text_record *text;
775   const char *s;
776
777   printf ("%08llx: extra product info\n", (long long int) ftello (r->file));
778   text = open_text_record (r, size, count);
779   s = text_get_all (text);
780   print_string (s, strlen (s));
781   close_text_record (text);
782 }
783
784 /* Read record type 7, subtype 7. */
785 static void
786 read_mrsets (struct sfm_reader *r, size_t size, size_t count)
787 {
788   struct text_record *text;
789
790   printf ("%08llx: multiple response sets\n",
791           (long long int) ftello (r->file));
792   text = open_text_record (r, size, count);
793   for (;;)
794     {
795       const char *name;
796       enum { MRSET_MC, MRSET_MD } type;
797       bool cat_label_from_counted_values = false;
798       bool label_from_var_label = false;
799       const char *counted;
800       const char *label;
801       const char *variables;
802
803       while (text_match (text, '\n'))
804         continue;
805
806       name = text_tokenize (text, '=');
807       if (name == NULL)
808         break;
809
810       if (text_match (text, 'C'))
811         {
812           type = MRSET_MC;
813           counted = NULL;
814           if (!text_match (text, ' '))
815             {
816               sys_warn (r, "missing space following 'C' at offset %zu "
817                         "in mrsets record", text_pos (text));
818               break;
819             }
820         }
821       else if (text_match (text, 'D'))
822         {
823           type = MRSET_MD;
824         }
825       else if (text_match (text, 'E'))
826         {
827           char *number;
828
829           type = MRSET_MD;
830           cat_label_from_counted_values = true;
831
832           if (!text_match (text, ' '))
833             {
834               sys_warn (r, "Missing space following `%c' at offset %zu "
835                         "in MRSETS record", 'E', text_pos (text));
836               break;
837             }
838
839           number = text_tokenize (text, ' ');
840           if (!number)
841             sys_warn (r, "Missing label source value "
842                       "following `E' at offset %zu in MRSETS record",
843                       text_pos (text));
844           else if (!strcmp (number, "11"))
845             label_from_var_label = true;
846           else if (strcmp (number, "1"))
847             sys_warn (r, "Unexpected label source value `%s' "
848                       "following `E' at offset %zu in MRSETS record",
849                       number, text_pos (text));
850
851         }
852       else
853         {
854           sys_warn (r, "missing `C', `D', or `E' at offset %zu "
855                     "in mrsets record", text_pos (text));
856           break;
857         }
858
859       if (type == MRSET_MD)
860         {
861           counted = text_parse_counted_string (text);
862           if (counted == NULL)
863             break;
864         }
865
866       label = text_parse_counted_string (text);
867       if (label == NULL)
868         break;
869
870       variables = text_tokenize (text, '\n');
871
872       printf ("\t\"%s\": multiple %s set",
873               name, type == MRSET_MC ? "category" : "dichotomy");
874       if (counted != NULL)
875         printf (", counted value \"%s\"", counted);
876       if (cat_label_from_counted_values)
877         printf (", category labels from counted values");
878       if (label[0] != '\0')
879         printf (", label \"%s\"", label);
880       if (label_from_var_label)
881         printf (", label from variable label");
882       if (variables != NULL)
883         printf(", variables \"%s\"\n", variables);
884       else
885         printf(", no variables\n");
886     }
887   close_text_record (text);
888 }
889
890 /* Read record type 7, subtype 11. */
891 static void
892 read_display_parameters (struct sfm_reader *r, size_t size, size_t count)
893 {
894   size_t n_vars;
895   bool includes_width;
896   size_t i;
897
898   printf ("%08llx: variable display parameters\n",
899           (long long int) ftello (r->file));
900   if (size != 4)
901     {
902       sys_warn (r, "Bad size %zu on extension 11.", size);
903       skip_bytes (r, size * count);
904       return;
905     }
906
907   n_vars = r->n_variables;
908   if (count == 3 * n_vars)
909     includes_width = true;
910   else if (count == 2 * n_vars)
911     includes_width = false;
912   else
913     {
914       sys_warn (r, "Extension 11 has bad count %zu (for %zu variables.",
915                 count, n_vars);
916       skip_bytes (r, size * count);
917       return;
918     }
919
920   for (i = 0; i < n_vars; ++i)
921     {
922       int measure = read_int (r);
923       int width = includes_width ? read_int (r) : 0;
924       int align = read_int (r);
925
926       printf ("\tVar #%zu: measure=%d (%s)", i, measure,
927               (measure == 1 ? "nominal"
928                : measure == 2 ? "ordinal"
929                : measure == 3 ? "scale"
930                : "invalid"));
931       if (includes_width)
932         printf (", width=%d", width);
933       printf (", align=%d (%s)\n", align,
934               (align == 0 ? "left"
935                : align == 1 ? "right"
936                : align == 2 ? "centre"
937                : "invalid"));
938     }
939 }
940
941 /* Reads record type 7, subtype 13, which gives the long name
942    that corresponds to each short name.  */
943 static void
944 read_long_var_name_map (struct sfm_reader *r, size_t size, size_t count)
945 {
946   struct text_record *text;
947   char *var;
948   char *long_name;
949
950   printf ("%08llx: long variable names (short => long)\n",
951           (long long int) ftello (r->file));
952   text = open_text_record (r, size, count);
953   while (read_variable_to_value_pair (text, &var, &long_name))
954     printf ("\t%s => %s\n", var, long_name);
955   close_text_record (text);
956 }
957
958 /* Reads record type 7, subtype 14, which gives the real length
959    of each very long string.  Rearranges DICT accordingly. */
960 static void
961 read_long_string_map (struct sfm_reader *r, size_t size, size_t count)
962 {
963   struct text_record *text;
964   char *var;
965   char *length_s;
966
967   printf ("%08llx: very long strings (variable => length)\n",
968           (long long int) ftello (r->file));
969   text = open_text_record (r, size, count);
970   while (read_variable_to_value_pair (text, &var, &length_s))
971     printf ("\t%s => %d\n", var, atoi (length_s));
972   close_text_record (text);
973 }
974
975 static bool
976 read_attributes (struct sfm_reader *r, struct text_record *text,
977                  const char *variable)
978 {
979   const char *key;
980   int index;
981
982   for (;;)
983     {
984       key = text_tokenize (text, '(');
985       if (key == NULL)
986         return true;
987
988       for (index = 1; ; index++)
989         {
990           /* Parse the value. */
991           const char *value = text_tokenize (text, '\n');
992           if (value == NULL)
993             {
994               sys_warn (r, "%s: Error parsing attribute value %s[%d]",
995                         variable, key, index);
996               return false;
997             }
998           if (strlen (value) < 2
999               || value[0] != '\'' || value[strlen (value) - 1] != '\'')
1000             sys_warn (r, "%s: Attribute value %s[%d] is not quoted: %s",
1001                       variable, key, index, value);
1002           else
1003             printf ("\t%s: %s[%d] = \"%.*s\"\n",
1004                     variable, key, index, (int) strlen (value) - 2, value + 1);
1005
1006           /* Was this the last value for this attribute? */
1007           if (text_match (text, ')'))
1008             break;
1009         }
1010
1011       if (text_match (text, '/'))
1012         return true;
1013     }
1014 }
1015
1016 /* Read extended number of cases record. */
1017 static void
1018 read_ncases64 (struct sfm_reader *r, size_t size, size_t count)
1019 {
1020   int64_t unknown, ncases64;
1021
1022   if (size != 8)
1023     {
1024       sys_warn (r, "Bad size %zu for extended number of cases.", size);
1025       skip_bytes (r, size * count);
1026       return;
1027     }
1028   if (count != 2)
1029     {
1030       sys_warn (r, "Bad count %zu for extended number of cases.", size);
1031       skip_bytes (r, size * count);
1032       return;
1033     }
1034   unknown = read_int64 (r);
1035   ncases64 = read_int64 (r);
1036   printf ("%08llx: extended number of cases: "
1037           "unknown=%"PRId64", ncases64=%"PRId64"\n",
1038           (long long int) ftello (r->file), unknown, ncases64);
1039 }
1040
1041 static void
1042 read_datafile_attributes (struct sfm_reader *r, size_t size, size_t count)
1043 {
1044   struct text_record *text;
1045
1046   printf ("%08llx: datafile attributes\n", (long long int) ftello (r->file));
1047   text = open_text_record (r, size, count);
1048   read_attributes (r, text, "datafile");
1049   close_text_record (text);
1050 }
1051
1052 static void
1053 read_character_encoding (struct sfm_reader *r, size_t size, size_t count)
1054 {
1055   long long int posn =  ftello (r->file);
1056   char *encoding = xcalloc (size, count + 1);
1057   read_string (r, encoding, count + 1);
1058
1059   printf ("%08llx: Character Encoding: %s\n", posn, encoding);
1060
1061   free (encoding);
1062 }
1063
1064 static void
1065 read_long_string_value_labels (struct sfm_reader *r, size_t size, size_t count)
1066 {
1067   long long int start = ftello (r->file);
1068
1069   printf ("%08llx: long string value labels\n", start);
1070   while (ftello (r->file) - start < size * count)
1071     {
1072       long long posn = ftello (r->file);
1073       char var_name[ID_MAX_LEN + 1];
1074       int var_name_len;
1075       int n_values;
1076       int width;
1077       int i;
1078
1079       /* Read variable name. */
1080       var_name_len = read_int (r);
1081       if (var_name_len > ID_MAX_LEN)
1082         sys_error (r, "Variable name length in long string value label "
1083                    "record (%d) exceeds %d-byte limit.",
1084                    var_name_len, ID_MAX_LEN);
1085       read_string (r, var_name, var_name_len + 1);
1086
1087       /* Read width, number of values. */
1088       width = read_int (r);
1089       n_values = read_int (r);
1090
1091       printf ("\t%08llx: %s, width %d, %d values\n",
1092               posn, var_name, width, n_values);
1093
1094       /* Read values. */
1095       for (i = 0; i < n_values; i++)
1096         {
1097           char *value;
1098           int value_length;
1099
1100           char *label;
1101           int label_length;
1102
1103           posn = ftello (r->file);
1104
1105           /* Read value. */
1106           value_length = read_int (r);
1107           value = xmalloc (value_length + 1);
1108           read_string (r, value, value_length + 1);
1109
1110           /* Read label. */
1111           label_length = read_int (r);
1112           label = xmalloc (label_length + 1);
1113           read_string (r, label, label_length + 1);
1114
1115           printf ("\t\t%08llx: \"%s\" (%d bytes) => \"%s\" (%d bytes)\n",
1116                   posn, value, value_length, label, label_length);
1117
1118           free (value);
1119           free (label);
1120         }
1121     }
1122 }
1123
1124 static void
1125 read_long_string_missing_values (struct sfm_reader *r,
1126                                  size_t size, size_t count)
1127 {
1128   long long int start = ftello (r->file);
1129
1130   printf ("%08llx: long string missing values\n", start);
1131   while (ftello (r->file) - start < size * count)
1132     {
1133       long long posn = ftello (r->file);
1134
1135       /* Read variable name. */
1136       int var_name_len = read_int (r);
1137       if (var_name_len > ID_MAX_LEN)
1138         sys_error (r, "Variable name length in long string value label "
1139                    "record (%d) exceeds %d-byte limit.",
1140                    var_name_len, ID_MAX_LEN);
1141       char var_name[ID_MAX_LEN + 1];
1142       read_string (r, var_name, var_name_len + 1);
1143
1144       /* Read number of values. */
1145       uint8_t n_missing_values;
1146       read_bytes (r, &n_missing_values, 1);
1147
1148       /* Read value length. */
1149       int value_length = read_int (r);
1150
1151       printf ("\t%08llx: %s, %d missing values, each %d bytes:",
1152               posn, var_name, n_missing_values, value_length);
1153
1154       /* Read values. */
1155       for (int i = 0; i < n_missing_values; i++)
1156         {
1157           posn = ftello (r->file);
1158
1159           /* Read value. */
1160           char *value = xmalloc (value_length + 1);
1161           read_string (r, value, value_length + 1);
1162
1163           printf (" \"%s\"", value);
1164
1165           free (value);
1166         }
1167       printf ("\n");
1168     }
1169 }
1170
1171 static void
1172 hex_dump (size_t offset, const void *buffer_, size_t buffer_size)
1173 {
1174   const uint8_t *buffer = buffer_;
1175
1176   while (buffer_size > 0)
1177     {
1178       size_t n = MIN (buffer_size, 16);
1179       size_t i;
1180
1181       printf ("%04zx", offset);
1182       for (i = 0; i < 16; i++)
1183         {
1184           if (i < n)
1185             printf ("%c%02x", i == 8 ? '-' : ' ', buffer[i]);
1186           else
1187             printf ("   ");
1188         }
1189
1190       printf (" |");
1191       for (i = 0; i < 16; i++)
1192         {
1193           unsigned char c = i < n ? buffer[i] : ' ';
1194           putchar (isprint (c) ? c : '.');
1195         }
1196       printf ("|\n");
1197
1198       offset += n;
1199       buffer += n;
1200       buffer_size -= n;
1201     }
1202 }
1203
1204 /* Reads and prints any type 7 record that we don't understand. */
1205 static void
1206 read_unknown_extension (struct sfm_reader *r, size_t size, size_t count)
1207 {
1208   unsigned char *buffer;
1209   size_t i;
1210
1211   if (size == 0 || count > 65536 / size)
1212     skip_bytes (r, size * count);
1213   else if (size != 1)
1214     {
1215       buffer = xmalloc (size);
1216       for (i = 0; i < count; i++)
1217         {
1218           read_bytes (r, buffer, size);
1219           hex_dump (i * size, buffer, size);
1220         }
1221       free (buffer);
1222     }
1223   else
1224     {
1225       buffer = xmalloc (count);
1226       read_bytes (r, buffer, count);
1227       print_string (CHAR_CAST (char *, buffer), count);
1228       free (buffer);
1229     }
1230 }
1231
1232 static void
1233 read_variable_attributes (struct sfm_reader *r, size_t size, size_t count)
1234 {
1235   struct text_record *text;
1236
1237   printf ("%08llx: variable attributes\n", (long long int) ftello (r->file));
1238   text = open_text_record (r, size, count);
1239   for (;;)
1240     {
1241       const char *variable = text_tokenize (text, ':');
1242       if (variable == NULL || !read_attributes (r, text, variable))
1243         break;
1244     }
1245   close_text_record (text);
1246 }
1247
1248 static void
1249 read_simple_compressed_data (struct sfm_reader *r, int max_cases)
1250 {
1251   enum { N_OPCODES = 8 };
1252   uint8_t opcodes[N_OPCODES];
1253   long long int opcode_ofs;
1254   int opcode_idx;
1255   int case_num;
1256   int i;
1257
1258   read_int (r);
1259   printf ("\n%08llx: compressed data:\n", (long long int) ftello (r->file));
1260
1261   opcode_idx = N_OPCODES;
1262   opcode_ofs = 0;
1263   case_num = 0;
1264   for (case_num = 0; case_num < max_cases; case_num++)
1265     {
1266       printf ("%08llx: case %d's uncompressible data begins\n",
1267               (long long int) ftello (r->file), case_num);
1268       for (i = 0; i < r->n_var_widths;)
1269         {
1270           int width = r->var_widths[i];
1271           char raw_value[8];
1272           int opcode;
1273
1274           if (opcode_idx >= N_OPCODES)
1275             {
1276               opcode_ofs = ftello (r->file);
1277               if (i == 0)
1278                 {
1279                   if (!try_read_bytes (r, opcodes, 8))
1280                     return;
1281                 }
1282               else
1283                 read_bytes (r, opcodes, 8);
1284               opcode_idx = 0;
1285             }
1286           opcode = opcodes[opcode_idx];
1287           printf ("%08llx: variable %d: opcode %d: ",
1288                   opcode_ofs + opcode_idx, i, opcode);
1289
1290           switch (opcode)
1291             {
1292             default:
1293               printf ("%.*g", DBL_DIG + 1, opcode - r->bias);
1294               if (width != 0)
1295                 printf (", but this is a string variable (width=%d)", width);
1296               printf ("\n");
1297               i++;
1298               break;
1299
1300             case 0:
1301               printf ("ignored padding\n");
1302               break;
1303
1304             case 252:
1305               printf ("end of data\n");
1306               return;
1307
1308             case 253:
1309               read_bytes (r, raw_value, 8);
1310               printf ("uncompressible data: ");
1311               print_untyped_value (r, raw_value);
1312               printf ("\n");
1313               i++;
1314               break;
1315
1316             case 254:
1317               printf ("spaces");
1318               if (width == 0)
1319                 printf (", but this is a numeric variable");
1320               printf ("\n");
1321               i++;
1322               break;
1323
1324             case 255:
1325               printf ("SYSMIS");
1326               if (width != 0)
1327                 printf (", but this is a string variable (width=%d)", width);
1328               printf ("\n");
1329               i++;
1330               break;
1331             }
1332
1333           opcode_idx++;
1334         }
1335     }
1336 }
1337
1338 static void
1339 read_zlib_compressed_data (struct sfm_reader *r)
1340 {
1341   long long int ofs;
1342   long long int this_ofs, next_ofs, next_len;
1343   long long int bias, zero;
1344   long long int expected_uncmp_ofs, expected_cmp_ofs;
1345   unsigned int block_size, n_blocks;
1346   unsigned int i;
1347
1348   read_int (r);
1349   ofs = ftello (r->file);
1350   printf ("\n%08llx: ZLIB compressed data header:\n", ofs);
1351
1352   this_ofs = read_int64 (r);
1353   next_ofs = read_int64 (r);
1354   next_len = read_int64 (r);
1355
1356   printf ("\tzheader_ofs: 0x%llx\n", this_ofs);
1357   if (this_ofs != ofs)
1358     printf ("\t\t(Expected 0x%llx.)\n", ofs);
1359   printf ("\tztrailer_ofs: 0x%llx\n", next_ofs);
1360   printf ("\tztrailer_len: %lld\n", next_len);
1361   if (next_len < 24 || next_len % 24)
1362     printf ("\t\t(Trailer length is not a positive multiple of 24.)\n");
1363
1364   printf ("\n%08llx: 0x%llx bytes of ZLIB compressed data\n",
1365           ofs + 8 * 3, next_ofs - (ofs + 8 * 3));
1366
1367   skip_bytes (r, next_ofs - (ofs + 8 * 3));
1368
1369   printf ("\n%08llx: ZLIB trailer fixed header:\n", next_ofs);
1370   bias = read_int64 (r);
1371   zero = read_int64 (r);
1372   block_size = read_int (r);
1373   n_blocks = read_int (r);
1374   printf ("\tbias: %lld\n", bias);
1375   printf ("\tzero: 0x%llx\n", zero);
1376   if (zero != 0)
1377     printf ("\t\t(Expected 0.)\n");
1378   printf ("\tblock_size: 0x%x\n", block_size);
1379   if (block_size != 0x3ff000)
1380     printf ("\t\t(Expected 0x3ff000.)\n");
1381   printf ("\tn_blocks: %u\n", n_blocks);
1382   if (n_blocks != next_len / 24 - 1)
1383     printf ("\t\t(Expected %llu.)\n", next_len / 24 - 1);
1384
1385   expected_uncmp_ofs = ofs;
1386   expected_cmp_ofs = ofs + 24;
1387   for (i = 0; i < n_blocks; i++)
1388     {
1389       long long int blockinfo_ofs = ftello (r->file);
1390       unsigned long long int uncompressed_ofs = read_int64 (r);
1391       unsigned long long int compressed_ofs = read_int64 (r);
1392       unsigned int uncompressed_size = read_int (r);
1393       unsigned int compressed_size = read_int (r);
1394
1395       printf ("\n%08llx: ZLIB block descriptor %d\n", blockinfo_ofs, i + 1);
1396
1397       printf ("\tuncompressed_ofs: 0x%llx\n", uncompressed_ofs);
1398       if (uncompressed_ofs != expected_uncmp_ofs)
1399         printf ("\t\t(Expected 0x%llx.)\n", ofs);
1400
1401       printf ("\tcompressed_ofs: 0x%llx\n", compressed_ofs);
1402       if (compressed_ofs != expected_cmp_ofs)
1403         printf ("\t\t(Expected 0x%llx.)\n", ofs + 24);
1404
1405       printf ("\tuncompressed_size: 0x%x\n", uncompressed_size);
1406       if (i < n_blocks - 1 && uncompressed_size != block_size)
1407         printf ("\t\t(Expected 0x%x.)\n", block_size);
1408
1409       printf ("\tcompressed_size: 0x%x\n", compressed_size);
1410       if (i == n_blocks - 1 && compressed_ofs + compressed_size != next_ofs)
1411         printf ("\t\t(This was expected to be 0x%llx.)\n",
1412                 next_ofs - compressed_size);
1413
1414       expected_uncmp_ofs += uncompressed_size;
1415       expected_cmp_ofs += compressed_size;
1416     }
1417 }
1418 \f
1419 /* Helpers for reading records that consist of structured text
1420    strings. */
1421
1422 /* State. */
1423 struct text_record
1424   {
1425     struct sfm_reader *reader;  /* Reader. */
1426     char *buffer;               /* Record contents. */
1427     size_t size;                /* Size of buffer. */
1428     size_t pos;                 /* Current position in buffer. */
1429   };
1430
1431 /* Reads SIZE * COUNT bytes into a text record for R,
1432    and returns the new text record. */
1433 static struct text_record *
1434 open_text_record (struct sfm_reader *r, size_t size, size_t count)
1435 {
1436   struct text_record *text = xmalloc (sizeof *text);
1437
1438   if (size_overflow_p (xsum (1, xtimes (size, count))))
1439     sys_error (r, "Extension record too large.");
1440
1441   size_t n_bytes = size * count;
1442   char *buffer = xmalloc (n_bytes + 1);
1443   read_bytes (r, buffer, n_bytes);
1444   buffer[n_bytes] = '\0';
1445   text->reader = r;
1446   text->buffer = buffer;
1447   text->size = n_bytes;
1448   text->pos = 0;
1449   return text;
1450 }
1451
1452 /* Closes TEXT and frees its storage.
1453    Not really needed, because the pool will free the text record anyway,
1454    but can be used to free it earlier. */
1455 static void
1456 close_text_record (struct text_record *text)
1457 {
1458   free (text->buffer);
1459   free (text);
1460 }
1461
1462 static char *
1463 text_tokenize (struct text_record *text, int delimiter)
1464 {
1465   size_t start = text->pos;
1466   while (text->pos < text->size
1467          && text->buffer[text->pos] != delimiter
1468          && text->buffer[text->pos] != '\0')
1469     text->pos++;
1470   if (start == text->pos)
1471     return NULL;
1472   text->buffer[text->pos++] = '\0';
1473   return &text->buffer[start];
1474 }
1475
1476 static bool
1477 text_match (struct text_record *text, int c)
1478 {
1479   if (text->pos < text->size && text->buffer[text->pos] == c)
1480     {
1481       text->pos++;
1482       return true;
1483     }
1484   else
1485     return false;
1486 }
1487
1488 /* Reads a integer value expressed in decimal, then a space, then a string that
1489    consists of exactly as many bytes as specified by the integer, then a space,
1490    from TEXT.  Returns the string, null-terminated, as a subset of TEXT's
1491    buffer (so the caller should not free the string). */
1492 static const char *
1493 text_parse_counted_string (struct text_record *text)
1494 {
1495   size_t start;
1496   size_t n;
1497   char *s;
1498
1499   start = text->pos;
1500   n = 0;
1501   while (isdigit ((unsigned char) text->buffer[text->pos]))
1502     n = (n * 10) + (text->buffer[text->pos++] - '0');
1503   if (start == text->pos)
1504     {
1505       sys_error (text->reader, "expecting digit at offset %zu in record",
1506                  text->pos);
1507       return NULL;
1508     }
1509
1510   if (!text_match (text, ' '))
1511     {
1512       sys_error (text->reader, "expecting space at offset %zu in record",
1513                  text->pos);
1514       return NULL;
1515     }
1516
1517   if (text->pos + n > text->size)
1518     {
1519       sys_error (text->reader, "%zu-byte string starting at offset %zu "
1520                  "exceeds record length %zu", n, text->pos, text->size);
1521       return NULL;
1522     }
1523
1524   s = &text->buffer[text->pos];
1525   if (s[n] != ' ')
1526     {
1527       sys_error (text->reader, "expecting space at offset %zu following "
1528                  "%zu-byte string", text->pos + n, n);
1529       return NULL;
1530     }
1531   s[n] = '\0';
1532   text->pos += n + 1;
1533   return s;
1534 }
1535
1536 /* Reads a variable=value pair from TEXT.
1537    Looks up the variable in DICT and stores it into *VAR.
1538    Stores a null-terminated value into *VALUE. */
1539 static bool
1540 read_variable_to_value_pair (struct text_record *text,
1541                              char **key, char **value)
1542 {
1543   *key = text_tokenize (text, '=');
1544   *value = text_tokenize (text, '\t');
1545   if (!*key || !*value)
1546     return false;
1547
1548   while (text->pos < text->size
1549          && (text->buffer[text->pos] == '\t'
1550              || text->buffer[text->pos] == '\0'))
1551     text->pos++;
1552   return true;
1553 }
1554
1555 /* Returns the current byte offset inside the TEXT's string. */
1556 static size_t
1557 text_pos (const struct text_record *text)
1558 {
1559   return text->pos;
1560 }
1561
1562 static const char *
1563 text_get_all (const struct text_record *text)
1564 {
1565   return text->buffer;
1566 }
1567 \f
1568 static void
1569 usage (void)
1570 {
1571   printf ("\
1572 %s, a utility for dissecting system files.\n\
1573 Usage: %s [OPTION]... SYSFILE...\n\
1574 where each SYSFILE is the name of a system file.\n\
1575 \n\
1576 Options:\n\
1577   --data[=MAXCASES]   print (up to MAXCASES cases of) compressed data\n\
1578   --help              display this help and exit\n\
1579   --version           output version information and exit\n",
1580           program_name, program_name);
1581 }
1582
1583 /* Displays a corruption message. */
1584 static void
1585 sys_msg (struct sfm_reader *r, const char *format, va_list args)
1586 {
1587   printf ("\"%s\" near offset 0x%llx: ",
1588           r->file_name, (long long int) ftello (r->file));
1589   vprintf (format, args);
1590   putchar ('\n');
1591 }
1592
1593 /* Displays a warning for the current file position. */
1594 static void
1595 sys_warn (struct sfm_reader *r, const char *format, ...)
1596 {
1597   va_list args;
1598
1599   va_start (args, format);
1600   sys_msg (r, format, args);
1601   va_end (args);
1602 }
1603
1604 /* Displays an error for the current file position,
1605    marks it as in an error state,
1606    and aborts reading it using longjmp. */
1607 static void
1608 sys_error (struct sfm_reader *r, const char *format, ...)
1609 {
1610   va_list args;
1611
1612   va_start (args, format);
1613   sys_msg (r, format, args);
1614   va_end (args);
1615
1616   exit (EXIT_FAILURE);
1617 }
1618 \f
1619 /* Reads BYTE_CNT bytes into BUF.
1620    Returns true if exactly BYTE_CNT bytes are successfully read.
1621    Aborts if an I/O error or a partial read occurs.
1622    If EOF_IS_OK, then an immediate end-of-file causes false to be
1623    returned; otherwise, immediate end-of-file causes an abort
1624    too. */
1625 static inline bool
1626 read_bytes_internal (struct sfm_reader *r, bool eof_is_ok,
1627                      void *buf, size_t n_bytes)
1628 {
1629   size_t bytes_read = fread (buf, 1, n_bytes, r->file);
1630   if (bytes_read == n_bytes)
1631     return true;
1632   else if (ferror (r->file))
1633     sys_error (r, "System error: %s.", strerror (errno));
1634   else if (!eof_is_ok || bytes_read != 0)
1635     sys_error (r, "Unexpected end of file.");
1636   else
1637     return false;
1638 }
1639
1640 /* Reads BYTE_CNT into BUF.
1641    Aborts upon I/O error or if end-of-file is encountered. */
1642 static void
1643 read_bytes (struct sfm_reader *r, void *buf, size_t n_bytes)
1644 {
1645   read_bytes_internal (r, false, buf, n_bytes);
1646 }
1647
1648 /* Reads BYTE_CNT bytes into BUF.
1649    Returns true if exactly BYTE_CNT bytes are successfully read.
1650    Returns false if an immediate end-of-file is encountered.
1651    Aborts if an I/O error or a partial read occurs. */
1652 static bool
1653 try_read_bytes (struct sfm_reader *r, void *buf, size_t n_bytes)
1654 {
1655   return read_bytes_internal (r, true, buf, n_bytes);
1656 }
1657
1658 /* Reads a 32-bit signed integer from R and returns its value in
1659    host format. */
1660 static int
1661 read_int (struct sfm_reader *r)
1662 {
1663   uint8_t integer[4];
1664   read_bytes (r, integer, sizeof integer);
1665   return integer_get (r->integer_format, integer, sizeof integer);
1666 }
1667
1668 /* Reads a 64-bit signed integer from R and returns its value in
1669    host format. */
1670 static int64_t
1671 read_int64 (struct sfm_reader *r)
1672 {
1673   uint8_t integer[8];
1674   read_bytes (r, integer, sizeof integer);
1675   return integer_get (r->integer_format, integer, sizeof integer);
1676 }
1677
1678 /* Reads a 64-bit floating-point number from R and returns its
1679    value in host format. */
1680 static double
1681 read_float (struct sfm_reader *r)
1682 {
1683   uint8_t number[8];
1684   read_bytes (r, number, sizeof number);
1685   return float_get_double (r->float_format, number);
1686 }
1687
1688 /* Reads exactly SIZE - 1 bytes into BUFFER
1689    and stores a null byte into BUFFER[SIZE - 1]. */
1690 static void
1691 read_string (struct sfm_reader *r, char *buffer, size_t size)
1692 {
1693   assert (size > 0);
1694   read_bytes (r, buffer, size - 1);
1695   buffer[size - 1] = '\0';
1696 }
1697
1698 /* Skips BYTES bytes forward in R. */
1699 static void
1700 skip_bytes (struct sfm_reader *r, size_t bytes)
1701 {
1702   while (bytes > 0)
1703     {
1704       char buffer[1024];
1705       size_t chunk = MIN (sizeof buffer, bytes);
1706       read_bytes (r, buffer, chunk);
1707       bytes -= chunk;
1708     }
1709 }
1710
1711 static void
1712 trim_spaces (char *s)
1713 {
1714   char *end = strchr (s, '\0');
1715   while (end > s && end[-1] == ' ')
1716     end--;
1717   *end = '\0';
1718 }
1719
1720 static void
1721 print_string (const char *s, size_t len)
1722 {
1723   if (memchr (s, 0, len) == 0)
1724     {
1725       size_t i;
1726
1727       for (i = 0; i < len; i++)
1728         {
1729           unsigned char c = s[i];
1730
1731           if (c == '\\')
1732             printf ("\\\\");
1733           else if (c == '\n' || isprint (c))
1734             putchar (c);
1735           else
1736             printf ("\\%02x", c);
1737         }
1738       putchar ('\n');
1739     }
1740   else
1741     hex_dump (0, s, len);
1742 }