1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2008, 2009, 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
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.
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.
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/>. */
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"
35 #include "gl/minmax.h"
36 #include "gl/progname.h"
37 #include "gl/version-etc.h"
38 #include "gl/xalloc.h"
51 const char *file_name;
54 int n_variable_records, n_variables;
57 size_t n_var_widths, allocated_var_widths;
59 enum integer_format integer_format;
60 enum float_format float_format;
62 enum compression compression;
66 static void read_header (struct sfm_reader *);
67 static void read_variable_record (struct sfm_reader *);
68 static void read_value_label_record (struct sfm_reader *);
69 static void read_document_record (struct sfm_reader *);
70 static void read_extension_record (struct sfm_reader *);
71 static void read_machine_integer_info (struct sfm_reader *,
72 size_t size, size_t count);
73 static void read_machine_float_info (struct sfm_reader *,
74 size_t size, size_t count);
75 static void read_extra_product_info (struct sfm_reader *,
76 size_t size, size_t count);
77 static void read_mrsets (struct sfm_reader *, size_t size, size_t count);
78 static void read_display_parameters (struct sfm_reader *,
79 size_t size, size_t count);
80 static void read_long_var_name_map (struct sfm_reader *r,
81 size_t size, size_t count);
82 static void read_long_string_map (struct sfm_reader *r,
83 size_t size, size_t count);
84 static void read_datafile_attributes (struct sfm_reader *r,
85 size_t size, size_t count);
86 static void read_variable_attributes (struct sfm_reader *r,
87 size_t size, size_t count);
88 static void read_ncases64 (struct sfm_reader *, size_t size, size_t count);
89 static void read_character_encoding (struct sfm_reader *r,
90 size_t size, size_t count);
91 static void read_long_string_value_labels (struct sfm_reader *r,
92 size_t size, size_t count);
93 static void read_long_string_missing_values (struct sfm_reader *r,
94 size_t size, size_t count);
95 static void read_unknown_extension (struct sfm_reader *,
96 size_t size, size_t count);
97 static void read_simple_compressed_data (struct sfm_reader *, int max_cases);
98 static void read_zlib_compressed_data (struct sfm_reader *);
100 static struct text_record *open_text_record (
101 struct sfm_reader *, size_t size);
102 static void close_text_record (struct text_record *);
103 static bool read_variable_to_value_pair (struct text_record *,
104 char **key, char **value);
105 static char *text_tokenize (struct text_record *, int delimiter);
106 static bool text_match (struct text_record *text, int c);
107 static const char *text_parse_counted_string (struct text_record *);
108 static size_t text_pos (const struct text_record *);
109 static const char *text_get_all (const struct text_record *);
111 static void usage (void);
112 static void sys_warn (struct sfm_reader *, const char *, ...)
113 PRINTF_FORMAT (2, 3);
114 static void sys_error (struct sfm_reader *, const char *, ...)
118 static void read_bytes (struct sfm_reader *, void *, size_t);
119 static bool try_read_bytes (struct sfm_reader *, void *, size_t);
120 static int read_int (struct sfm_reader *);
121 static int64_t read_int64 (struct sfm_reader *);
122 static double read_float (struct sfm_reader *);
123 static void read_string (struct sfm_reader *, char *, size_t);
124 static void skip_bytes (struct sfm_reader *, size_t);
125 static void trim_spaces (char *);
127 static void print_string (const char *s, size_t len);
130 main (int argc, char *argv[])
136 set_program_name (argv[0]);
140 static const struct option long_options[] =
142 { "data", optional_argument, NULL, 'd' },
143 { "help", no_argument, NULL, 'h' },
144 { "version", no_argument, NULL, 'v' },
145 { NULL, 0, NULL, 0 },
150 c = getopt_long (argc, argv, "d::hv", long_options, NULL);
157 max_cases = optarg ? atoi (optarg) : INT_MAX;
161 version_etc (stdout, "pspp-dump-sav", PACKAGE_NAME, PACKAGE_VERSION,
162 "Ben Pfaff", "John Darrington", NULL_SENTINEL);
175 error (1, 0, "at least one non-option argument is required; "
176 "use --help for help");
178 for (i = optind; i < argc; i++)
182 r.file_name = argv[i];
183 r.file = fopen (r.file_name, "rb");
185 error (EXIT_FAILURE, errno, "error opening `%s'", r.file_name);
186 r.n_variable_records = 0;
189 r.allocated_var_widths = 0;
191 r.compression = COMP_NONE;
193 if (argc - optind > 1)
194 printf ("Reading \"%s\":\n", r.file_name);
197 while ((rec_type = read_int (&r)) != 999)
202 read_variable_record (&r);
206 read_value_label_record (&r);
210 sys_error (&r, "Misplaced type 4 record.");
213 read_document_record (&r);
217 read_extension_record (&r);
221 sys_error (&r, "Unrecognized record type %d.", rec_type);
224 printf ("%08llx: end-of-dictionary record "
225 "(first byte of data at %08llx)\n",
226 (long long int) ftello (r.file),
227 (long long int) ftello (r.file) + 4);
229 if (r.compression == COMP_SIMPLE)
232 read_simple_compressed_data (&r, max_cases);
234 else if (r.compression == COMP_ZLIB)
235 read_zlib_compressed_data (&r);
244 read_header (struct sfm_reader *r)
247 char eye_catcher[61];
248 uint8_t raw_layout_code[4];
251 int32_t weight_index;
254 char creation_date[10];
255 char creation_time[9];
259 read_string (r, rec_type, sizeof rec_type);
260 read_string (r, eye_catcher, sizeof eye_catcher);
262 if (!strcmp ("$FL2", rec_type))
264 else if (!strcmp ("$FL3", rec_type))
267 sys_error (r, "This is not an SPSS system file.");
269 /* Identify integer format. */
270 read_bytes (r, raw_layout_code, sizeof raw_layout_code);
271 if ((!integer_identify (2, raw_layout_code, sizeof raw_layout_code,
273 && !integer_identify (3, raw_layout_code, sizeof raw_layout_code,
275 || (r->integer_format != INTEGER_MSB_FIRST
276 && r->integer_format != INTEGER_LSB_FIRST))
277 sys_error (r, "This is not an SPSS system file.");
278 layout_code = integer_get (r->integer_format,
279 raw_layout_code, sizeof raw_layout_code);
281 read_int (r); /* Nominal case size (not actually useful). */
282 compressed = read_int (r);
283 weight_index = read_int (r);
284 ncases = read_int (r);
289 r->compression = COMP_NONE;
290 else if (compressed == 1)
291 r->compression = COMP_SIMPLE;
292 else if (compressed != 0)
293 sys_error (r, "SAV file header has invalid compression value "
294 "%"PRId32".", compressed);
299 r->compression = COMP_ZLIB;
301 sys_error (r, "ZSAV file header has invalid compression value "
302 "%"PRId32".", compressed);
305 /* Identify floating-point format and obtain compression bias. */
306 read_bytes (r, raw_bias, sizeof raw_bias);
307 if (float_identify (100.0, raw_bias, sizeof raw_bias, &r->float_format) == 0)
309 sys_warn (r, "Compression bias is not the usual value of 100, or system "
310 "file uses unrecognized floating-point format.");
311 if (r->integer_format == INTEGER_MSB_FIRST)
312 r->float_format = FLOAT_IEEE_DOUBLE_BE;
314 r->float_format = FLOAT_IEEE_DOUBLE_LE;
316 r->bias = float_get_double (r->float_format, raw_bias);
318 read_string (r, creation_date, sizeof creation_date);
319 read_string (r, creation_time, sizeof creation_time);
320 read_string (r, file_label, sizeof file_label);
321 trim_spaces (file_label);
324 printf ("File header record:\n");
325 printf ("\t%17s: %s\n", "Product name", eye_catcher);
326 printf ("\t%17s: %"PRId32"\n", "Layout code", layout_code);
327 printf ("\t%17s: %"PRId32" (%s)\n", "Compressed",
329 r->compression == COMP_NONE ? "no compression"
330 : r->compression == COMP_SIMPLE ? "simple compression"
331 : r->compression == COMP_ZLIB ? "ZLIB compression"
333 printf ("\t%17s: %"PRId32"\n", "Weight index", weight_index);
334 printf ("\t%17s: %"PRId32"\n", "Number of cases", ncases);
335 printf ("\t%17s: %g\n", "Compression bias", r->bias);
336 printf ("\t%17s: %s\n", "Creation date", creation_date);
337 printf ("\t%17s: %s\n", "Creation time", creation_time);
338 printf ("\t%17s: \"%s\"\n", "File label", file_label);
342 format_name (int format)
344 switch ((format >> 16) & 0xff)
347 case 2: return "AHEX";
348 case 3: return "COMMA";
349 case 4: return "DOLLAR";
352 case 7: return "PIBHEX";
354 case 9: return "PIB";
355 case 10: return "PK";
356 case 11: return "RB";
357 case 12: return "RBHEX";
361 case 20: return "DATE";
362 case 21: return "TIME";
363 case 22: return "DATETIME";
364 case 23: return "ADATE";
365 case 24: return "JDATE";
366 case 25: return "DTIME";
367 case 26: return "WKDAY";
368 case 27: return "MONTH";
369 case 28: return "MOYR";
370 case 29: return "QYR";
371 case 30: return "WKYR";
372 case 31: return "PCT";
373 case 32: return "DOT";
374 case 33: return "CCA";
375 case 34: return "CCB";
376 case 35: return "CCC";
377 case 36: return "CCD";
378 case 37: return "CCE";
379 case 38: return "EDATE";
380 case 39: return "SDATE";
381 default: return "invalid";
385 /* Reads a variable (type 2) record from R and adds the
386 corresponding variable to DICT.
387 Also skips past additional variable records for long string
390 read_variable_record (struct sfm_reader *r)
393 int has_variable_label;
394 int missing_value_code;
399 printf ("%08llx: variable record #%d\n",
400 (long long int) ftello (r->file), r->n_variable_records++);
402 width = read_int (r);
403 has_variable_label = read_int (r);
404 missing_value_code = read_int (r);
405 print_format = read_int (r);
406 write_format = read_int (r);
407 read_string (r, name, sizeof name);
408 name[strcspn (name, " ")] = '\0';
413 if (r->n_var_widths >= r->allocated_var_widths)
414 r->var_widths = x2nrealloc (r->var_widths, &r->allocated_var_widths,
415 sizeof *r->var_widths);
416 r->var_widths[r->n_var_widths++] = width;
418 printf ("\tWidth: %d (%s)\n",
421 : width == 0 ? "numeric"
422 : "long string continuation record");
423 printf ("\tVariable label: %d\n", has_variable_label);
424 printf ("\tMissing values code: %d (%s)\n", missing_value_code,
425 (missing_value_code == 0 ? "no missing values"
426 : missing_value_code == 1 ? "one missing value"
427 : missing_value_code == 2 ? "two missing values"
428 : missing_value_code == 3 ? "three missing values"
429 : missing_value_code == -2 ? "one missing value range"
430 : missing_value_code == -3 ? "one missing value, one range"
432 printf ("\tPrint format: %06x (%s%d.%d)\n",
433 print_format, format_name (print_format),
434 (print_format >> 8) & 0xff, print_format & 0xff);
435 printf ("\tWrite format: %06x (%s%d.%d)\n",
436 write_format, format_name (write_format),
437 (write_format >> 8) & 0xff, write_format & 0xff);
438 printf ("\tName: %s\n", name);
440 /* Get variable label, if any. */
441 if (has_variable_label != 0 && has_variable_label != 1)
442 sys_error (r, "Variable label indicator field is not 0 or 1.");
443 if (has_variable_label == 1)
445 long long int offset = ftello (r->file);
446 size_t len, read_len;
451 /* Read up to 255 bytes of label. */
452 read_len = MIN (sizeof label - 1, len);
453 read_string (r, label, read_len + 1);
454 printf("\t%08llx Variable label: \"%s\"\n", offset, label);
456 /* Skip unread label bytes. */
457 skip_bytes (r, len - read_len);
459 /* Skip label padding up to multiple of 4 bytes. */
460 skip_bytes (r, ROUND_UP (len, 4) - len);
463 /* Set missing values. */
464 if (missing_value_code != 0)
468 printf ("\t%08llx Missing values:", (long long int) ftello (r->file));
471 if (missing_value_code < -3 || missing_value_code > 3
472 || missing_value_code == -1)
473 sys_error (r, "Numeric missing value indicator field is not "
474 "-3, -2, 0, 1, 2, or 3.");
475 if (missing_value_code < 0)
477 double low = read_float (r);
478 double high = read_float (r);
479 printf (" %g...%g", low, high);
480 missing_value_code = -missing_value_code - 2;
482 for (i = 0; i < missing_value_code; i++)
483 printf (" %g", read_float (r));
487 if (missing_value_code < 1 || missing_value_code > 3)
488 sys_error (r, "String missing value indicator field is not "
490 for (i = 0; i < missing_value_code; i++)
493 read_string (r, string, sizeof string);
494 printf (" \"%s\"", string);
502 print_untyped_value (struct sfm_reader *r, char raw_value[8])
507 value = float_get_double (r->float_format, raw_value);
508 for (n_printable = 0; n_printable < 8; n_printable++)
509 if (!isprint (raw_value[n_printable]))
512 printf ("%g/\"%.*s\"", value, n_printable, raw_value);
515 /* Reads value labels from sysfile R and inserts them into the
516 associated dictionary. */
518 read_value_label_record (struct sfm_reader *r)
520 int label_cnt, var_cnt;
523 printf ("%08llx: value labels record\n", (long long int) ftello (r->file));
525 /* Read number of labels. */
526 label_cnt = read_int (r);
527 for (i = 0; i < label_cnt; i++)
530 unsigned char label_len;
534 read_bytes (r, raw_value, sizeof raw_value);
536 /* Read label length. */
537 read_bytes (r, &label_len, sizeof label_len);
538 padded_len = ROUND_UP (label_len + 1, 8);
540 /* Read label, padding. */
541 read_bytes (r, label, padded_len - 1);
542 label[label_len] = 0;
545 print_untyped_value (r, raw_value);
546 printf (": \"%s\"\n", label);
549 /* Now, read the type 4 record that has the list of variables
550 to which the value labels are to be applied. */
552 /* Read record type of type 4 record. */
553 if (read_int (r) != 4)
554 sys_error (r, "Variable index record (type 4) does not immediately "
555 "follow value label record (type 3) as it should.");
557 /* Read number of variables associated with value label from type 4
559 printf ("\t%08llx: apply to variables", (long long int) ftello (r->file));
560 var_cnt = read_int (r);
561 for (i = 0; i < var_cnt; i++)
562 printf (" #%d", read_int (r));
567 read_document_record (struct sfm_reader *r)
572 printf ("%08llx: document record\n", (long long int) ftello (r->file));
573 n_lines = read_int (r);
574 printf ("\t%d lines of documents\n", n_lines);
576 for (i = 0; i < n_lines; i++)
579 printf ("\t%08llx: ", (long long int) ftello (r->file));
580 read_string (r, line, sizeof line);
582 printf ("line %d: \"%s\"\n", i, line);
587 read_extension_record (struct sfm_reader *r)
589 long long int offset = ftello (r->file);
590 int subtype = read_int (r);
591 size_t size = read_int (r);
592 size_t count = read_int (r);
593 size_t bytes = size * count;
595 printf ("%08llx: Record 7, subtype %d, size=%zu, count=%zu\n",
596 offset, subtype, size, count);
601 read_machine_integer_info (r, size, count);
605 read_machine_float_info (r, size, count);
609 /* Variable sets information. We don't use these yet.
610 They only apply to GUIs; see VARSETS on the APPLY
611 DICTIONARY command in SPSS documentation. */
615 /* DATE variable information. We don't use it yet, but we
621 read_mrsets (r, size, count);
625 read_extra_product_info (r, size, count);
629 read_display_parameters (r, size, count);
633 read_long_var_name_map (r, size, count);
637 read_long_string_map (r, size, count);
641 read_ncases64 (r, size, count);
645 read_datafile_attributes (r, size, count);
649 read_variable_attributes (r, size, count);
653 read_character_encoding (r, size, count);
657 read_long_string_value_labels (r, size, count);
661 read_long_string_missing_values (r, size, count);
665 sys_warn (r, "Unrecognized record type 7, subtype %d.", subtype);
666 read_unknown_extension (r, size, count);
670 skip_bytes (r, bytes);
674 read_machine_integer_info (struct sfm_reader *r, size_t size, size_t count)
676 long long int offset = ftello (r->file);
677 int version_major = read_int (r);
678 int version_minor = read_int (r);
679 int version_revision = read_int (r);
680 int machine_code = read_int (r);
681 int float_representation = read_int (r);
682 int compression_code = read_int (r);
683 int integer_representation = read_int (r);
684 int character_code = read_int (r);
686 printf ("%08llx: machine integer info\n", offset);
687 if (size != 4 || count != 8)
688 sys_error (r, "Bad size (%zu) or count (%zu) field on record type 7, "
689 "subtype 3.", size, count);
691 printf ("\tVersion: %d.%d.%d\n",
692 version_major, version_minor, version_revision);
693 printf ("\tMachine code: %d\n", machine_code);
694 printf ("\tFloating point representation: %d (%s)\n",
695 float_representation,
696 float_representation == 1 ? "IEEE 754"
697 : float_representation == 2 ? "IBM 370"
698 : float_representation == 3 ? "DEC VAX"
700 printf ("\tCompression code: %d\n", compression_code);
701 printf ("\tEndianness: %d (%s)\n", integer_representation,
702 integer_representation == 1 ? "big"
703 : integer_representation == 2 ? "little" : "unknown");
704 printf ("\tCharacter code: %d\n", character_code);
707 /* Read record type 7, subtype 4. */
709 read_machine_float_info (struct sfm_reader *r, size_t size, size_t count)
711 long long int offset = ftello (r->file);
712 double sysmis = read_float (r);
713 double highest = read_float (r);
714 double lowest = read_float (r);
716 printf ("%08llx: machine float info\n", offset);
717 if (size != 8 || count != 3)
718 sys_error (r, "Bad size (%zu) or count (%zu) on extension 4.",
721 printf ("\tsysmis: %g (%a)\n", sysmis, sysmis);
722 if (sysmis != SYSMIS)
723 sys_warn (r, "File specifies unexpected value %g (%a) as %s.",
724 sysmis, sysmis, "SYSMIS");
726 printf ("\thighest: %g (%a)\n", highest, highest);
727 if (highest != HIGHEST)
728 sys_warn (r, "File specifies unexpected value %g (%a) as %s.",
729 highest, highest, "HIGHEST");
731 printf ("\tlowest: %g (%a)\n", lowest, lowest);
732 if (lowest != LOWEST && lowest != SYSMIS)
733 sys_warn (r, "File specifies unexpected value %g (%a) as %s.",
734 lowest, lowest, "LOWEST");
738 read_extra_product_info (struct sfm_reader *r,
739 size_t size, size_t count)
741 struct text_record *text;
744 printf ("%08llx: extra product info\n", (long long int) ftello (r->file));
745 text = open_text_record (r, size * count);
746 s = text_get_all (text);
747 print_string (s, strlen (s));
748 close_text_record (text);
751 /* Read record type 7, subtype 7. */
753 read_mrsets (struct sfm_reader *r, size_t size, size_t count)
755 struct text_record *text;
757 printf ("%08llx: multiple response sets\n",
758 (long long int) ftello (r->file));
759 text = open_text_record (r, size * count);
763 enum { MRSET_MC, MRSET_MD } type;
764 bool cat_label_from_counted_values = false;
765 bool label_from_var_label = false;
768 const char *variables;
770 name = text_tokenize (text, '=');
774 if (text_match (text, 'C'))
778 if (!text_match (text, ' '))
780 sys_warn (r, "missing space following 'C' at offset %zu "
781 "in mrsets record", text_pos (text));
785 else if (text_match (text, 'D'))
789 else if (text_match (text, 'E'))
794 cat_label_from_counted_values = true;
796 if (!text_match (text, ' '))
798 sys_warn (r, "Missing space following `%c' at offset %zu "
799 "in MRSETS record", 'E', text_pos (text));
803 number = text_tokenize (text, ' ');
804 if (!strcmp (number, "11"))
805 label_from_var_label = true;
806 else if (strcmp (number, "1"))
807 sys_warn (r, "Unexpected label source value `%s' "
808 "following `E' at offset %zu in MRSETS record",
809 number, text_pos (text));
814 sys_warn (r, "missing `C', `D', or `E' at offset %zu "
815 "in mrsets record", text_pos (text));
819 if (type == MRSET_MD)
821 counted = text_parse_counted_string (text);
826 label = text_parse_counted_string (text);
830 variables = text_tokenize (text, '\n');
831 if (variables == NULL)
833 sys_warn (r, "missing variable names following label "
834 "at offset %zu in mrsets record", text_pos (text));
838 printf ("\t\"%s\": multiple %s set",
839 name, type == MRSET_MC ? "category" : "dichotomy");
841 printf (", counted value \"%s\"", counted);
842 if (cat_label_from_counted_values)
843 printf (", category labels from counted values");
844 if (label[0] != '\0')
845 printf (", label \"%s\"", label);
846 if (label_from_var_label)
847 printf (", label from variable label");
848 printf(", variables \"%s\"\n", variables);
850 close_text_record (text);
853 /* Read record type 7, subtype 11. */
855 read_display_parameters (struct sfm_reader *r, size_t size, size_t count)
861 printf ("%08llx: variable display parameters\n",
862 (long long int) ftello (r->file));
865 sys_warn (r, "Bad size %zu on extension 11.", size);
866 skip_bytes (r, size * count);
870 n_vars = r->n_variables;
871 if (count == 3 * n_vars)
872 includes_width = true;
873 else if (count == 2 * n_vars)
874 includes_width = false;
877 sys_warn (r, "Extension 11 has bad count %zu (for %zu variables.",
879 skip_bytes (r, size * count);
883 for (i = 0; i < n_vars; ++i)
885 int measure = read_int (r);
886 int width = includes_width ? read_int (r) : 0;
887 int align = read_int (r);
889 printf ("\tVar #%zu: measure=%d (%s)", i, measure,
890 (measure == 1 ? "nominal"
891 : measure == 2 ? "ordinal"
892 : measure == 3 ? "scale"
895 printf (", width=%d", width);
896 printf (", align=%d (%s)\n", align,
898 : align == 1 ? "right"
899 : align == 2 ? "centre"
904 /* Reads record type 7, subtype 13, which gives the long name
905 that corresponds to each short name. */
907 read_long_var_name_map (struct sfm_reader *r, size_t size, size_t count)
909 struct text_record *text;
913 printf ("%08llx: long variable names (short => long)\n",
914 (long long int) ftello (r->file));
915 text = open_text_record (r, size * count);
916 while (read_variable_to_value_pair (text, &var, &long_name))
917 printf ("\t%s => %s\n", var, long_name);
918 close_text_record (text);
921 /* Reads record type 7, subtype 14, which gives the real length
922 of each very long string. Rearranges DICT accordingly. */
924 read_long_string_map (struct sfm_reader *r, size_t size, size_t count)
926 struct text_record *text;
930 printf ("%08llx: very long strings (variable => length)\n",
931 (long long int) ftello (r->file));
932 text = open_text_record (r, size * count);
933 while (read_variable_to_value_pair (text, &var, &length_s))
934 printf ("\t%s => %d\n", var, atoi (length_s));
935 close_text_record (text);
939 read_attributes (struct sfm_reader *r, struct text_record *text,
940 const char *variable)
947 key = text_tokenize (text, '(');
951 for (index = 1; ; index++)
953 /* Parse the value. */
954 const char *value = text_tokenize (text, '\n');
957 sys_warn (r, "%s: Error parsing attribute value %s[%d]",
958 variable, key, index);
961 if (strlen (value) < 2
962 || value[0] != '\'' || value[strlen (value) - 1] != '\'')
963 sys_warn (r, "%s: Attribute value %s[%d] is not quoted: %s",
964 variable, key, index, value);
966 printf ("\t%s: %s[%d] = \"%.*s\"\n",
967 variable, key, index, (int) strlen (value) - 2, value + 1);
969 /* Was this the last value for this attribute? */
970 if (text_match (text, ')'))
974 if (text_match (text, '/'))
979 /* Read extended number of cases record. */
981 read_ncases64 (struct sfm_reader *r, size_t size, size_t count)
983 int64_t unknown, ncases64;
987 sys_warn (r, "Bad size %zu for extended number of cases.", size);
988 skip_bytes (r, size * count);
993 sys_warn (r, "Bad count %zu for extended number of cases.", size);
994 skip_bytes (r, size * count);
997 unknown = read_int64 (r);
998 ncases64 = read_int64 (r);
999 printf ("%08llx: extended number of cases: "
1000 "unknown=%"PRId64", ncases64=%"PRId64"\n",
1001 (long long int) ftello (r->file), unknown, ncases64);
1005 read_datafile_attributes (struct sfm_reader *r, size_t size, size_t count)
1007 struct text_record *text;
1009 printf ("%08llx: datafile attributes\n", (long long int) ftello (r->file));
1010 text = open_text_record (r, size * count);
1011 read_attributes (r, text, "datafile");
1012 close_text_record (text);
1016 read_character_encoding (struct sfm_reader *r, size_t size, size_t count)
1018 long long int posn = ftello (r->file);
1019 char *encoding = xcalloc (size, count + 1);
1020 read_string (r, encoding, count + 1);
1022 printf ("%08llx: Character Encoding: %s\n", posn, encoding);
1026 read_long_string_value_labels (struct sfm_reader *r, size_t size, size_t count)
1028 long long int start = ftello (r->file);
1030 printf ("%08llx: long string value labels\n", start);
1031 while (ftello (r->file) - start < size * count)
1033 long long posn = ftello (r->file);
1034 char var_name[ID_MAX_LEN + 1];
1040 /* Read variable name. */
1041 var_name_len = read_int (r);
1042 if (var_name_len > ID_MAX_LEN)
1043 sys_error (r, "Variable name length in long string value label "
1044 "record (%d) exceeds %d-byte limit.",
1045 var_name_len, ID_MAX_LEN);
1046 read_string (r, var_name, var_name_len + 1);
1048 /* Read width, number of values. */
1049 width = read_int (r);
1050 n_values = read_int (r);
1052 printf ("\t%08llx: %s, width %d, %d values\n",
1053 posn, var_name, width, n_values);
1056 for (i = 0; i < n_values; i++)
1064 posn = ftello (r->file);
1067 value_length = read_int (r);
1068 value = xmalloc (value_length + 1);
1069 read_string (r, value, value_length + 1);
1072 label_length = read_int (r);
1073 label = xmalloc (label_length + 1);
1074 read_string (r, label, label_length + 1);
1076 printf ("\t\t%08llx: \"%s\" (%d bytes) => \"%s\" (%d bytes)\n",
1077 posn, value, value_length, label, label_length);
1086 read_long_string_missing_values (struct sfm_reader *r,
1087 size_t size, size_t count)
1089 long long int start = ftello (r->file);
1091 printf ("%08llx: long string missing values\n", start);
1092 while (ftello (r->file) - start < size * count)
1094 long long posn = ftello (r->file);
1095 char var_name[ID_MAX_LEN + 1];
1096 uint8_t n_missing_values;
1100 /* Read variable name. */
1101 var_name_len = read_int (r);
1102 if (var_name_len > ID_MAX_LEN)
1103 sys_error (r, "Variable name length in long string value label "
1104 "record (%d) exceeds %d-byte limit.",
1105 var_name_len, ID_MAX_LEN);
1106 read_string (r, var_name, var_name_len + 1);
1108 /* Read number of values. */
1109 read_bytes (r, &n_missing_values, 1);
1111 printf ("\t%08llx: %s, %d missing values:",
1112 posn, var_name, n_missing_values);
1115 for (i = 0; i < n_missing_values; i++)
1120 posn = ftello (r->file);
1123 value_length = read_int (r);
1124 value = xmalloc (value_length + 1);
1125 read_string (r, value, value_length + 1);
1127 printf (" \"%s\"", value);
1136 hex_dump (size_t offset, const void *buffer_, size_t buffer_size)
1138 const uint8_t *buffer = buffer_;
1140 while (buffer_size > 0)
1142 size_t n = MIN (buffer_size, 16);
1145 printf ("%04zx", offset);
1146 for (i = 0; i < 16; i++)
1149 printf ("%c%02x", i == 8 ? '-' : ' ', buffer[i]);
1155 for (i = 0; i < 16; i++)
1157 unsigned char c = i < n ? buffer[i] : ' ';
1158 putchar (isprint (c) ? c : '.');
1168 /* Reads and prints any type 7 record that we don't understand. */
1170 read_unknown_extension (struct sfm_reader *r, size_t size, size_t count)
1172 unsigned char *buffer;
1175 if (size == 0 || count > 65536 / size)
1176 skip_bytes (r, size * count);
1179 buffer = xmalloc (size);
1180 for (i = 0; i < count; i++)
1182 read_bytes (r, buffer, size);
1183 hex_dump (i * size, buffer, size);
1189 buffer = xmalloc (count);
1190 read_bytes (r, buffer, count);
1191 print_string (CHAR_CAST (char *, buffer), count);
1197 read_variable_attributes (struct sfm_reader *r, size_t size, size_t count)
1199 struct text_record *text;
1201 printf ("%08llx: variable attributes\n", (long long int) ftello (r->file));
1202 text = open_text_record (r, size * count);
1205 const char *variable = text_tokenize (text, ':');
1206 if (variable == NULL || !read_attributes (r, text, variable))
1209 close_text_record (text);
1213 read_simple_compressed_data (struct sfm_reader *r, int max_cases)
1215 enum { N_OPCODES = 8 };
1216 uint8_t opcodes[N_OPCODES];
1217 long long int opcode_ofs;
1223 printf ("\n%08llx: compressed data:\n", (long long int) ftello (r->file));
1225 opcode_idx = N_OPCODES;
1228 for (case_num = 0; case_num < max_cases; case_num++)
1230 printf ("%08llx: case %d's uncompressible data begins\n",
1231 (long long int) ftello (r->file), case_num);
1232 for (i = 0; i < r->n_var_widths; )
1234 int width = r->var_widths[i];
1238 if (opcode_idx >= N_OPCODES)
1240 opcode_ofs = ftello (r->file);
1243 if (!try_read_bytes (r, opcodes, 8))
1247 read_bytes (r, opcodes, 8);
1250 opcode = opcodes[opcode_idx];
1251 printf ("%08llx: variable %d: opcode %d: ",
1252 opcode_ofs + opcode_idx, i, opcode);
1257 printf ("%g", opcode - r->bias);
1259 printf (", but this is a string variable (width=%d)", width);
1265 printf ("ignored padding\n");
1269 printf ("end of data\n");
1273 read_bytes (r, raw_value, 8);
1274 printf ("uncompressible data: ");
1275 print_untyped_value (r, raw_value);
1283 printf (", but this is a numeric variable");
1291 printf (", but this is a string variable (width=%d)", width);
1303 read_zlib_compressed_data (struct sfm_reader *r)
1306 long long int this_ofs, next_ofs, next_len;
1307 long long int bias, zero;
1308 long long int expected_uncmp_ofs, expected_cmp_ofs;
1309 unsigned int block_size, n_blocks;
1313 ofs = ftello (r->file);
1314 printf ("\n%08llx: ZLIB compressed data header:\n", ofs);
1316 this_ofs = read_int64 (r);
1317 next_ofs = read_int64 (r);
1318 next_len = read_int64 (r);
1320 printf ("\tzheader_ofs: 0x%llx\n", this_ofs);
1321 if (this_ofs != ofs)
1322 printf ("\t\t(Expected 0x%llx.)\n", ofs);
1323 printf ("\tztrailer_ofs: 0x%llx\n", next_ofs);
1324 printf ("\tztrailer_len: %lld\n", next_len);
1325 if (next_len < 24 || next_len % 24)
1326 printf ("\t\t(Trailer length is not a positive multiple of 24.)\n");
1328 printf ("\n%08llx: 0x%llx bytes of ZLIB compressed data\n",
1329 ofs + 8 * 3, next_ofs - (ofs + 8 * 3));
1331 skip_bytes (r, next_ofs - (ofs + 8 * 3));
1333 printf ("\n%08llx: ZLIB trailer fixed header:\n", next_ofs);
1334 bias = read_int64 (r);
1335 zero = read_int64 (r);
1336 block_size = read_int (r);
1337 n_blocks = read_int (r);
1338 printf ("\tbias: %lld\n", bias);
1339 printf ("\tzero: 0x%llx\n", zero);
1341 printf ("\t\t(Expected 0.)\n");
1342 printf ("\tblock_size: 0x%x\n", block_size);
1343 if (block_size != 0x3ff000)
1344 printf ("\t\t(Expected 0x3ff000.)\n");
1345 printf ("\tn_blocks: %u\n", n_blocks);
1346 if (n_blocks != next_len / 24 - 1)
1347 printf ("\t\t(Expected %llu.)\n", next_len / 24 - 1);
1349 expected_uncmp_ofs = ofs;
1350 expected_cmp_ofs = ofs + 24;
1351 for (i = 0; i < n_blocks; i++)
1353 long long int blockinfo_ofs = ftello (r->file);
1354 unsigned long long int uncompressed_ofs = read_int64 (r);
1355 unsigned long long int compressed_ofs = read_int64 (r);
1356 unsigned int uncompressed_size = read_int (r);
1357 unsigned int compressed_size = read_int (r);
1359 printf ("\n%08llx: ZLIB block descriptor %d\n", blockinfo_ofs, i + 1);
1361 printf ("\tuncompressed_ofs: 0x%llx\n", uncompressed_ofs);
1362 if (uncompressed_ofs != expected_uncmp_ofs)
1363 printf ("\t\t(Expected 0x%llx.)\n", ofs);
1365 printf ("\tcompressed_ofs: 0x%llx\n", compressed_ofs);
1366 if (compressed_ofs != expected_cmp_ofs)
1367 printf ("\t\t(Expected 0x%llx.)\n", ofs + 24);
1369 printf ("\tuncompressed_size: 0x%x\n", uncompressed_size);
1370 if (i < n_blocks - 1 && uncompressed_size != block_size)
1371 printf ("\t\t(Expected 0x%x.)\n", block_size);
1373 printf ("\tcompressed_size: 0x%x\n", compressed_size);
1374 if (i == n_blocks - 1 && compressed_ofs + compressed_size != next_ofs)
1375 printf ("\t\t(This was expected to be 0x%llx.)\n",
1376 next_ofs - compressed_size);
1378 expected_uncmp_ofs += uncompressed_size;
1379 expected_cmp_ofs += compressed_size;
1383 /* Helpers for reading records that consist of structured text
1389 struct sfm_reader *reader; /* Reader. */
1390 char *buffer; /* Record contents. */
1391 size_t size; /* Size of buffer. */
1392 size_t pos; /* Current position in buffer. */
1395 /* Reads SIZE bytes into a text record for R,
1396 and returns the new text record. */
1397 static struct text_record *
1398 open_text_record (struct sfm_reader *r, size_t size)
1400 struct text_record *text = xmalloc (sizeof *text);
1401 char *buffer = xmalloc (size + 1);
1402 read_bytes (r, buffer, size);
1403 buffer[size] = '\0';
1405 text->buffer = buffer;
1411 /* Closes TEXT and frees its storage.
1412 Not really needed, because the pool will free the text record anyway,
1413 but can be used to free it earlier. */
1415 close_text_record (struct text_record *text)
1417 free (text->buffer);
1422 text_tokenize (struct text_record *text, int delimiter)
1424 size_t start = text->pos;
1425 while (text->pos < text->size
1426 && text->buffer[text->pos] != delimiter
1427 && text->buffer[text->pos] != '\0')
1429 if (start == text->pos)
1431 text->buffer[text->pos++] = '\0';
1432 return &text->buffer[start];
1436 text_match (struct text_record *text, int c)
1438 if (text->pos < text->size && text->buffer[text->pos] == c)
1447 /* Reads a integer value expressed in decimal, then a space, then a string that
1448 consists of exactly as many bytes as specified by the integer, then a space,
1449 from TEXT. Returns the string, null-terminated, as a subset of TEXT's
1450 buffer (so the caller should not free the string). */
1452 text_parse_counted_string (struct text_record *text)
1460 while (isdigit ((unsigned char) text->buffer[text->pos]))
1461 n = (n * 10) + (text->buffer[text->pos++] - '0');
1462 if (start == text->pos)
1464 sys_error (text->reader, "expecting digit at offset %zu in record",
1469 if (!text_match (text, ' '))
1471 sys_error (text->reader, "expecting space at offset %zu in record",
1476 if (text->pos + n > text->size)
1478 sys_error (text->reader, "%zu-byte string starting at offset %zu "
1479 "exceeds record length %zu", n, text->pos, text->size);
1483 s = &text->buffer[text->pos];
1486 sys_error (text->reader, "expecting space at offset %zu following "
1487 "%zu-byte string", text->pos + n, n);
1495 /* Reads a variable=value pair from TEXT.
1496 Looks up the variable in DICT and stores it into *VAR.
1497 Stores a null-terminated value into *VALUE. */
1499 read_variable_to_value_pair (struct text_record *text,
1500 char **key, char **value)
1502 *key = text_tokenize (text, '=');
1503 *value = text_tokenize (text, '\t');
1504 if (!*key || !*value)
1507 while (text->pos < text->size
1508 && (text->buffer[text->pos] == '\t'
1509 || text->buffer[text->pos] == '\0'))
1514 /* Returns the current byte offset inside the TEXT's string. */
1516 text_pos (const struct text_record *text)
1522 text_get_all (const struct text_record *text)
1524 return text->buffer;
1531 %s, a utility for dissecting system files.\n\
1532 Usage: %s [OPTION]... SYSFILE...\n\
1533 where each SYSFILE is the name of a system file.\n\
1536 --data[=MAXCASES] print (up to MAXCASES cases of) compressed data\n\
1537 --help display this help and exit\n\
1538 --version output version information and exit\n",
1539 program_name, program_name);
1542 /* Displays a corruption message. */
1544 sys_msg (struct sfm_reader *r, const char *format, va_list args)
1546 printf ("\"%s\" near offset 0x%llx: ",
1547 r->file_name, (long long int) ftello (r->file));
1548 vprintf (format, args);
1552 /* Displays a warning for the current file position. */
1554 sys_warn (struct sfm_reader *r, const char *format, ...)
1558 va_start (args, format);
1559 sys_msg (r, format, args);
1563 /* Displays an error for the current file position,
1564 marks it as in an error state,
1565 and aborts reading it using longjmp. */
1567 sys_error (struct sfm_reader *r, const char *format, ...)
1571 va_start (args, format);
1572 sys_msg (r, format, args);
1575 exit (EXIT_FAILURE);
1578 /* Reads BYTE_CNT bytes into BUF.
1579 Returns true if exactly BYTE_CNT bytes are successfully read.
1580 Aborts if an I/O error or a partial read occurs.
1581 If EOF_IS_OK, then an immediate end-of-file causes false to be
1582 returned; otherwise, immediate end-of-file causes an abort
1585 read_bytes_internal (struct sfm_reader *r, bool eof_is_ok,
1586 void *buf, size_t byte_cnt)
1588 size_t bytes_read = fread (buf, 1, byte_cnt, r->file);
1589 if (bytes_read == byte_cnt)
1591 else if (ferror (r->file))
1592 sys_error (r, "System error: %s.", strerror (errno));
1593 else if (!eof_is_ok || bytes_read != 0)
1594 sys_error (r, "Unexpected end of file.");
1599 /* Reads BYTE_CNT into BUF.
1600 Aborts upon I/O error or if end-of-file is encountered. */
1602 read_bytes (struct sfm_reader *r, void *buf, size_t byte_cnt)
1604 read_bytes_internal (r, false, buf, byte_cnt);
1607 /* Reads BYTE_CNT bytes into BUF.
1608 Returns true if exactly BYTE_CNT bytes are successfully read.
1609 Returns false if an immediate end-of-file is encountered.
1610 Aborts if an I/O error or a partial read occurs. */
1612 try_read_bytes (struct sfm_reader *r, void *buf, size_t byte_cnt)
1614 return read_bytes_internal (r, true, buf, byte_cnt);
1617 /* Reads a 32-bit signed integer from R and returns its value in
1620 read_int (struct sfm_reader *r)
1623 read_bytes (r, integer, sizeof integer);
1624 return integer_get (r->integer_format, integer, sizeof integer);
1627 /* Reads a 64-bit signed integer from R and returns its value in
1630 read_int64 (struct sfm_reader *r)
1633 read_bytes (r, integer, sizeof integer);
1634 return integer_get (r->integer_format, integer, sizeof integer);
1637 /* Reads a 64-bit floating-point number from R and returns its
1638 value in host format. */
1640 read_float (struct sfm_reader *r)
1643 read_bytes (r, number, sizeof number);
1644 return float_get_double (r->float_format, number);
1647 /* Reads exactly SIZE - 1 bytes into BUFFER
1648 and stores a null byte into BUFFER[SIZE - 1]. */
1650 read_string (struct sfm_reader *r, char *buffer, size_t size)
1653 read_bytes (r, buffer, size - 1);
1654 buffer[size - 1] = '\0';
1657 /* Skips BYTES bytes forward in R. */
1659 skip_bytes (struct sfm_reader *r, size_t bytes)
1664 size_t chunk = MIN (sizeof buffer, bytes);
1665 read_bytes (r, buffer, chunk);
1671 trim_spaces (char *s)
1673 char *end = strchr (s, '\0');
1674 while (end > s && end[-1] == ' ')
1680 print_string (const char *s, size_t len)
1682 if (memchr (s, 0, len) == 0)
1686 for (i = 0; i < len; i++)
1688 unsigned char c = s[i];
1692 else if (c == '\n' || isprint (c))
1695 printf ("\\%02x", c);
1700 hex_dump (0, s, len);