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