1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2008, 2009, 2010, 2011 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"
44 const char *file_name;
47 int n_variable_records, n_variables;
50 size_t n_var_widths, allocated_var_widths;
52 enum integer_format integer_format;
53 enum float_format float_format;
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_mrsets (struct sfm_reader *, size_t size, size_t count);
69 static void read_display_parameters (struct sfm_reader *,
70 size_t size, size_t count);
71 static void read_long_var_name_map (struct sfm_reader *r,
72 size_t size, size_t count);
73 static void read_long_string_map (struct sfm_reader *r,
74 size_t size, size_t count);
75 static void read_datafile_attributes (struct sfm_reader *r,
76 size_t size, size_t count);
77 static void read_variable_attributes (struct sfm_reader *r,
78 size_t size, size_t count);
79 static void read_ncases64 (struct sfm_reader *, size_t size, size_t count);
80 static void read_character_encoding (struct sfm_reader *r,
81 size_t size, size_t count);
82 static void read_long_string_value_labels (struct sfm_reader *r,
83 size_t size, size_t count);
84 static void read_unknown_extension (struct sfm_reader *,
85 size_t size, size_t count);
86 static void read_compressed_data (struct sfm_reader *, int max_cases);
88 static struct text_record *open_text_record (
89 struct sfm_reader *, size_t size);
90 static void close_text_record (struct text_record *);
91 static bool read_variable_to_value_pair (struct text_record *,
92 char **key, char **value);
93 static char *text_tokenize (struct text_record *, int delimiter);
94 static bool text_match (struct text_record *text, int c);
95 static const char *text_parse_counted_string (struct text_record *);
96 static size_t text_pos (const struct text_record *);
98 static void usage (void);
99 static void sys_warn (struct sfm_reader *, const char *, ...)
100 PRINTF_FORMAT (2, 3);
101 static void sys_error (struct sfm_reader *, const char *, ...)
105 static void read_bytes (struct sfm_reader *, void *, size_t);
106 static bool try_read_bytes (struct sfm_reader *, void *, size_t);
107 static int read_int (struct sfm_reader *);
108 static int64_t read_int64 (struct sfm_reader *);
109 static double read_float (struct sfm_reader *);
110 static void read_string (struct sfm_reader *, char *, size_t);
111 static void skip_bytes (struct sfm_reader *, size_t);
112 static void trim_spaces (char *);
115 main (int argc, char *argv[])
121 set_program_name (argv[0]);
125 static const struct option long_options[] =
127 { "data", optional_argument, NULL, 'd' },
128 { "help", no_argument, NULL, 'h' },
129 { "version", no_argument, NULL, 'v' },
130 { NULL, 0, NULL, 0 },
135 c = getopt_long (argc, argv, "d::hv", long_options, NULL);
142 max_cases = optarg ? atoi (optarg) : INT_MAX;
146 version_etc (stdout, "pspp-dump-sav", PACKAGE_NAME, PACKAGE_VERSION,
147 "Ben Pfaff", "John Darrington", NULL_SENTINEL);
160 error (1, 0, "at least one non-option argument is required; "
161 "use --help for help");
163 for (i = optind; i < argc; i++)
167 r.file_name = argv[i];
168 r.file = fopen (r.file_name, "rb");
170 error (EXIT_FAILURE, errno, "error opening `%s'", r.file_name);
171 r.n_variable_records = 0;
174 r.allocated_var_widths = 0;
176 r.compressed = false;
178 if (argc - optind > 1)
179 printf ("Reading \"%s\":\n", r.file_name);
182 while ((rec_type = read_int (&r)) != 999)
187 read_variable_record (&r);
191 read_value_label_record (&r);
195 sys_error (&r, "Misplaced type 4 record.");
198 read_document_record (&r);
202 read_extension_record (&r);
206 sys_error (&r, "Unrecognized record type %d.", rec_type);
209 printf ("%08llx: end-of-dictionary record "
210 "(first byte of data at %08llx)\n",
211 (long long int) ftello (r.file),
212 (long long int) ftello (r.file) + 4);
214 if (r.compressed && max_cases > 0)
215 read_compressed_data (&r, max_cases);
224 read_header (struct sfm_reader *r)
227 char eye_catcher[61];
228 uint8_t raw_layout_code[4];
230 int32_t nominal_case_size;
232 int32_t weight_index;
235 char creation_date[10];
236 char creation_time[9];
239 read_string (r, rec_type, sizeof rec_type);
240 read_string (r, eye_catcher, sizeof eye_catcher);
242 if (strcmp ("$FL2", rec_type) != 0)
243 sys_error (r, "This is not an SPSS system file.");
245 /* Identify integer format. */
246 read_bytes (r, raw_layout_code, sizeof raw_layout_code);
247 if ((!integer_identify (2, raw_layout_code, sizeof raw_layout_code,
249 && !integer_identify (3, raw_layout_code, sizeof raw_layout_code,
251 || (r->integer_format != INTEGER_MSB_FIRST
252 && r->integer_format != INTEGER_LSB_FIRST))
253 sys_error (r, "This is not an SPSS system file.");
254 layout_code = integer_get (r->integer_format,
255 raw_layout_code, sizeof raw_layout_code);
257 nominal_case_size = read_int (r);
258 compressed = read_int (r);
259 weight_index = read_int (r);
260 ncases = read_int (r);
262 r->compressed = compressed != 0;
264 /* Identify floating-point format and obtain compression bias. */
265 read_bytes (r, raw_bias, sizeof raw_bias);
266 if (float_identify (100.0, raw_bias, sizeof raw_bias, &r->float_format) == 0)
268 sys_warn (r, "Compression bias is not the usual value of 100, or system "
269 "file uses unrecognized floating-point format.");
270 if (r->integer_format == INTEGER_MSB_FIRST)
271 r->float_format = FLOAT_IEEE_DOUBLE_BE;
273 r->float_format = FLOAT_IEEE_DOUBLE_LE;
275 r->bias = float_get_double (r->float_format, raw_bias);
277 read_string (r, creation_date, sizeof creation_date);
278 read_string (r, creation_time, sizeof creation_time);
279 read_string (r, file_label, sizeof file_label);
280 trim_spaces (file_label);
283 printf ("File header record:\n");
284 printf ("\t%17s: %s\n", "Product name", eye_catcher);
285 printf ("\t%17s: %"PRId32"\n", "Layout code", layout_code);
286 printf ("\t%17s: %"PRId32"\n", "Compressed", compressed);
287 printf ("\t%17s: %"PRId32"\n", "Weight index", weight_index);
288 printf ("\t%17s: %"PRId32"\n", "Number of cases", ncases);
289 printf ("\t%17s: %g\n", "Compression bias", r->bias);
290 printf ("\t%17s: %s\n", "Creation date", creation_date);
291 printf ("\t%17s: %s\n", "Creation time", creation_time);
292 printf ("\t%17s: \"%s\"\n", "File label", file_label);
296 format_name (int format)
298 switch ((format >> 16) & 0xff)
301 case 2: return "AHEX";
302 case 3: return "COMMA";
303 case 4: return "DOLLAR";
306 case 7: return "PIBHEX";
308 case 9: return "PIB";
309 case 10: return "PK";
310 case 11: return "RB";
311 case 12: return "RBHEX";
315 case 20: return "DATE";
316 case 21: return "TIME";
317 case 22: return "DATETIME";
318 case 23: return "ADATE";
319 case 24: return "JDATE";
320 case 25: return "DTIME";
321 case 26: return "WKDAY";
322 case 27: return "MONTH";
323 case 28: return "MOYR";
324 case 29: return "QYR";
325 case 30: return "WKYR";
326 case 31: return "PCT";
327 case 32: return "DOT";
328 case 33: return "CCA";
329 case 34: return "CCB";
330 case 35: return "CCC";
331 case 36: return "CCD";
332 case 37: return "CCE";
333 case 38: return "EDATE";
334 case 39: return "SDATE";
335 default: return "invalid";
339 /* Reads a variable (type 2) record from R and adds the
340 corresponding variable to DICT.
341 Also skips past additional variable records for long string
344 read_variable_record (struct sfm_reader *r)
347 int has_variable_label;
348 int missing_value_code;
353 printf ("%08llx: variable record #%d\n",
354 (long long int) ftello (r->file), r->n_variable_records++);
356 width = read_int (r);
357 has_variable_label = read_int (r);
358 missing_value_code = read_int (r);
359 print_format = read_int (r);
360 write_format = read_int (r);
361 read_string (r, name, sizeof name);
362 name[strcspn (name, " ")] = '\0';
367 if (r->n_var_widths >= r->allocated_var_widths)
368 r->var_widths = x2nrealloc (r->var_widths, &r->allocated_var_widths,
369 sizeof *r->var_widths);
370 r->var_widths[r->n_var_widths++] = width;
372 printf ("\tWidth: %d (%s)\n",
375 : width == 0 ? "numeric"
376 : "long string continuation record");
377 printf ("\tVariable label: %d\n", has_variable_label);
378 printf ("\tMissing values code: %d (%s)\n", missing_value_code,
379 (missing_value_code == 0 ? "no missing values"
380 : missing_value_code == 1 ? "one missing value"
381 : missing_value_code == 2 ? "two missing values"
382 : missing_value_code == 3 ? "three missing values"
383 : missing_value_code == -2 ? "one missing value range"
384 : missing_value_code == -3 ? "one missing value, one range"
386 printf ("\tPrint format: %06x (%s%d.%d)\n",
387 print_format, format_name (print_format),
388 (print_format >> 8) & 0xff, print_format & 0xff);
389 printf ("\tWrite format: %06x (%s%d.%d)\n",
390 write_format, format_name (write_format),
391 (write_format >> 8) & 0xff, write_format & 0xff);
392 printf ("\tName: %s\n", name);
394 /* Get variable label, if any. */
395 if (has_variable_label != 0 && has_variable_label != 1)
396 sys_error (r, "Variable label indicator field is not 0 or 1.");
397 if (has_variable_label == 1)
399 long long int offset = ftello (r->file);
400 size_t len, read_len;
405 /* Read up to 255 bytes of label. */
406 read_len = MIN (sizeof label - 1, len);
407 read_string (r, label, read_len + 1);
408 printf("\t%08llx Variable label: \"%s\"\n", offset, label);
410 /* Skip unread label bytes. */
411 skip_bytes (r, len - read_len);
413 /* Skip label padding up to multiple of 4 bytes. */
414 skip_bytes (r, ROUND_UP (len, 4) - len);
417 /* Set missing values. */
418 if (missing_value_code != 0)
422 printf ("\t%08llx Missing values:", (long long int) ftello (r->file));
425 if (missing_value_code < -3 || missing_value_code > 3
426 || missing_value_code == -1)
427 sys_error (r, "Numeric missing value indicator field is not "
428 "-3, -2, 0, 1, 2, or 3.");
429 if (missing_value_code < 0)
431 double low = read_float (r);
432 double high = read_float (r);
433 printf (" %g...%g", low, high);
434 missing_value_code = -missing_value_code - 2;
436 for (i = 0; i < missing_value_code; i++)
437 printf (" %g", read_float (r));
441 if (missing_value_code < 1 || missing_value_code > 3)
442 sys_error (r, "String missing value indicator field is not "
444 for (i = 0; i < missing_value_code; i++)
447 read_string (r, string, sizeof string);
448 printf (" \"%s\"", string);
456 print_untyped_value (struct sfm_reader *r, char raw_value[8])
461 value = float_get_double (r->float_format, raw_value);
462 for (n_printable = 0; n_printable < sizeof raw_value; n_printable++)
463 if (!isprint (raw_value[n_printable]))
466 printf ("%g/\"%.*s\"", value, n_printable, raw_value);
469 /* Reads value labels from sysfile R and inserts them into the
470 associated dictionary. */
472 read_value_label_record (struct sfm_reader *r)
474 int label_cnt, var_cnt;
477 printf ("%08llx: value labels record\n", (long long int) ftello (r->file));
479 /* Read number of labels. */
480 label_cnt = read_int (r);
481 for (i = 0; i < label_cnt; i++)
484 unsigned char label_len;
488 read_bytes (r, raw_value, sizeof raw_value);
490 /* Read label length. */
491 read_bytes (r, &label_len, sizeof label_len);
492 padded_len = ROUND_UP (label_len + 1, 8);
494 /* Read label, padding. */
495 read_bytes (r, label, padded_len - 1);
496 label[label_len] = 0;
499 print_untyped_value (r, raw_value);
500 printf (": \"%s\"\n", label);
503 /* Now, read the type 4 record that has the list of variables
504 to which the value labels are to be applied. */
506 /* Read record type of type 4 record. */
507 if (read_int (r) != 4)
508 sys_error (r, "Variable index record (type 4) does not immediately "
509 "follow value label record (type 3) as it should.");
511 /* Read number of variables associated with value label from type 4
513 printf ("\t%08llx: apply to variables", (long long int) ftello (r->file));
514 var_cnt = read_int (r);
515 for (i = 0; i < var_cnt; i++)
516 printf (" #%d", read_int (r));
521 read_document_record (struct sfm_reader *r)
526 printf ("%08llx: document record\n", (long long int) ftello (r->file));
527 n_lines = read_int (r);
528 printf ("\t%d lines of documents\n", n_lines);
530 for (i = 0; i < n_lines; i++)
533 printf ("\t%08llx: ", (long long int) ftello (r->file));
534 read_string (r, line, sizeof line);
536 printf ("line %d: \"%s\"\n", i, line);
541 read_extension_record (struct sfm_reader *r)
543 long long int offset = ftello (r->file);
544 int subtype = read_int (r);
545 size_t size = read_int (r);
546 size_t count = read_int (r);
547 size_t bytes = size * count;
549 printf ("%08llx: Record 7, subtype %d, size=%zu, count=%zu\n",
550 offset, subtype, size, count);
555 read_machine_integer_info (r, size, count);
559 read_machine_float_info (r, size, count);
563 /* Variable sets information. We don't use these yet.
564 They only apply to GUIs; see VARSETS on the APPLY
565 DICTIONARY command in SPSS documentation. */
569 /* DATE variable information. We don't use it yet, but we
575 read_mrsets (r, size, count);
579 read_display_parameters (r, size, count);
583 read_long_var_name_map (r, size, count);
587 read_long_string_map (r, size, count);
591 read_ncases64 (r, size, count);
595 read_datafile_attributes (r, size, count);
599 read_variable_attributes (r, size, count);
603 read_character_encoding (r, size, count);
607 read_long_string_value_labels (r, size, count);
611 sys_warn (r, "Unrecognized record type 7, subtype %d.", subtype);
612 read_unknown_extension (r, size, count);
616 skip_bytes (r, bytes);
620 read_machine_integer_info (struct sfm_reader *r, size_t size, size_t count)
622 long long int offset = ftello (r->file);
623 int version_major = read_int (r);
624 int version_minor = read_int (r);
625 int version_revision = read_int (r);
626 int machine_code = read_int (r);
627 int float_representation = read_int (r);
628 int compression_code = read_int (r);
629 int integer_representation = read_int (r);
630 int character_code = read_int (r);
632 printf ("%08llx: machine integer info\n", offset);
633 if (size != 4 || count != 8)
634 sys_error (r, "Bad size (%zu) or count (%zu) field on record type 7, "
635 "subtype 3.", size, count);
637 printf ("\tVersion: %d.%d.%d\n",
638 version_major, version_minor, version_revision);
639 printf ("\tMachine code: %d\n", machine_code);
640 printf ("\tFloating point representation: %d (%s)\n",
641 float_representation,
642 float_representation == 1 ? "IEEE 754"
643 : float_representation == 2 ? "IBM 370"
644 : float_representation == 3 ? "DEC VAX"
646 printf ("\tCompression code: %d\n", compression_code);
647 printf ("\tEndianness: %d (%s)\n", integer_representation,
648 integer_representation == 1 ? "big"
649 : integer_representation == 2 ? "little" : "unknown");
650 printf ("\tCharacter code: %d\n", character_code);
653 /* Read record type 7, subtype 4. */
655 read_machine_float_info (struct sfm_reader *r, size_t size, size_t count)
657 long long int offset = ftello (r->file);
658 double sysmis = read_float (r);
659 double highest = read_float (r);
660 double lowest = read_float (r);
662 printf ("%08llx: machine float info\n", offset);
663 if (size != 8 || count != 3)
664 sys_error (r, "Bad size (%zu) or count (%zu) on extension 4.",
667 printf ("\tsysmis: %g\n", sysmis);
668 if (sysmis != SYSMIS)
669 sys_warn (r, "File specifies unexpected value %g as %s.",
672 printf ("\thighest: %g\n", highest);
673 if (highest != HIGHEST)
674 sys_warn (r, "File specifies unexpected value %g as %s.",
677 printf ("\tlowest: %g\n", lowest);
678 if (lowest != LOWEST)
679 sys_warn (r, "File specifies unexpected value %g as %s.",
683 /* Read record type 7, subtype 7. */
685 read_mrsets (struct sfm_reader *r, size_t size, size_t count)
687 struct text_record *text;
689 printf ("%08llx: multiple response sets\n",
690 (long long int) ftello (r->file));
691 text = open_text_record (r, size * count);
695 enum { MRSET_MC, MRSET_MD } type;
696 bool cat_label_from_counted_values = false;
697 bool label_from_var_label = false;
700 const char *variables;
702 name = text_tokenize (text, '=');
706 if (text_match (text, 'C'))
710 if (!text_match (text, ' '))
712 sys_warn (r, "missing space following 'C' at offset %zu "
713 "in mrsets record", text_pos (text));
717 else if (text_match (text, 'D'))
721 else if (text_match (text, 'E'))
726 cat_label_from_counted_values = true;
728 if (!text_match (text, ' '))
730 sys_warn (r, "Missing space following `%c' at offset %zu "
731 "in MRSETS record", 'E', text_pos (text));
735 number = text_tokenize (text, ' ');
736 if (!strcmp (number, "11"))
737 label_from_var_label = true;
738 else if (strcmp (number, "1"))
739 sys_warn (r, "Unexpected label source value `%s' "
740 "following `E' at offset %zu in MRSETS record",
741 number, text_pos (text));
746 sys_warn (r, "missing `C', `D', or `E' at offset %zu "
747 "in mrsets record", text_pos (text));
751 if (type == MRSET_MD)
753 counted = text_parse_counted_string (text);
758 label = text_parse_counted_string (text);
762 variables = text_tokenize (text, '\n');
763 if (variables == NULL)
765 sys_warn (r, "missing variable names following label "
766 "at offset %zu in mrsets record", text_pos (text));
770 printf ("\t\"%s\": multiple %s set",
771 name, type == MRSET_MC ? "category" : "dichotomy");
773 printf (", counted value \"%s\"", counted);
774 if (cat_label_from_counted_values)
775 printf (", category labels from counted values");
776 if (label[0] != '\0')
777 printf (", label \"%s\"", label);
778 if (label_from_var_label)
779 printf (", label from variable label");
780 printf(", variables \"%s\"\n", variables);
782 close_text_record (text);
785 /* Read record type 7, subtype 11. */
787 read_display_parameters (struct sfm_reader *r, size_t size, size_t count)
793 printf ("%08llx: variable display parameters\n",
794 (long long int) ftello (r->file));
797 sys_warn (r, "Bad size %zu on extension 11.", size);
798 skip_bytes (r, size * count);
802 n_vars = r->n_variables;
803 if (count == 3 * n_vars)
804 includes_width = true;
805 else if (count == 2 * n_vars)
806 includes_width = false;
809 sys_warn (r, "Extension 11 has bad count %zu (for %zu variables.",
811 skip_bytes (r, size * count);
815 for (i = 0; i < n_vars; ++i)
817 int measure = read_int (r);
818 int width = includes_width ? read_int (r) : 0;
819 int align = read_int (r);
821 printf ("\tVar #%zu: measure=%d (%s)", i, measure,
822 (measure == 1 ? "nominal"
823 : measure == 2 ? "ordinal"
824 : measure == 3 ? "scale"
827 printf (", width=%d", width);
828 printf (", align=%d (%s)\n", align,
830 : align == 1 ? "right"
831 : align == 2 ? "centre"
836 /* Reads record type 7, subtype 13, which gives the long name
837 that corresponds to each short name. */
839 read_long_var_name_map (struct sfm_reader *r, size_t size, size_t count)
841 struct text_record *text;
845 printf ("%08llx: long variable names (short => long)\n",
846 (long long int) ftello (r->file));
847 text = open_text_record (r, size * count);
848 while (read_variable_to_value_pair (text, &var, &long_name))
849 printf ("\t%s => %s\n", var, long_name);
850 close_text_record (text);
853 /* Reads record type 7, subtype 14, which gives the real length
854 of each very long string. Rearranges DICT accordingly. */
856 read_long_string_map (struct sfm_reader *r, size_t size, size_t count)
858 struct text_record *text;
862 printf ("%08llx: very long strings (variable => length)\n",
863 (long long int) ftello (r->file));
864 text = open_text_record (r, size * count);
865 while (read_variable_to_value_pair (text, &var, &length_s))
866 printf ("\t%s => %d\n", var, atoi (length_s));
867 close_text_record (text);
871 read_attributes (struct sfm_reader *r, struct text_record *text,
872 const char *variable)
879 key = text_tokenize (text, '(');
883 for (index = 1; ; index++)
885 /* Parse the value. */
886 const char *value = text_tokenize (text, '\n');
889 sys_warn (r, "%s: Error parsing attribute value %s[%d]",
890 variable, key, index);
893 if (strlen (value) < 2
894 || value[0] != '\'' || value[strlen (value) - 1] != '\'')
895 sys_warn (r, "%s: Attribute value %s[%d] is not quoted: %s",
896 variable, key, index, value);
898 printf ("\t%s: %s[%d] = \"%.*s\"\n",
899 variable, key, index, (int) strlen (value) - 2, value + 1);
901 /* Was this the last value for this attribute? */
902 if (text_match (text, ')'))
906 if (text_match (text, '/'))
911 /* Read extended number of cases record. */
913 read_ncases64 (struct sfm_reader *r, size_t size, size_t count)
915 int64_t unknown, ncases64;
919 sys_warn (r, "Bad size %zu for extended number of cases.", size);
920 skip_bytes (r, size * count);
925 sys_warn (r, "Bad count %zu for extended number of cases.", size);
926 skip_bytes (r, size * count);
929 unknown = read_int64 (r);
930 ncases64 = read_int64 (r);
931 printf ("%08llx: extended number of cases: "
932 "unknown=%"PRId64", ncases64=%"PRId64"\n",
933 (long long int) ftello (r->file), unknown, ncases64);
937 read_datafile_attributes (struct sfm_reader *r, size_t size, size_t count)
939 struct text_record *text;
941 printf ("%08llx: datafile attributes\n", (long long int) ftello (r->file));
942 text = open_text_record (r, size * count);
943 read_attributes (r, text, "datafile");
944 close_text_record (text);
948 read_character_encoding (struct sfm_reader *r, size_t size, size_t count)
950 long long int posn = ftello (r->file);
951 char *encoding = xcalloc (size, count + 1);
952 read_string (r, encoding, count + 1);
954 printf ("%08llx: Character Encoding: %s\n", posn, encoding);
958 read_long_string_value_labels (struct sfm_reader *r, size_t size, size_t count)
960 long long int start = ftello (r->file);
962 printf ("%08llx: long string value labels\n", start);
963 while (ftello (r->file) - start < size * count)
965 long long posn = ftello (r->file);
966 char var_name[ID_MAX_LEN + 1];
972 /* Read variable name. */
973 var_name_len = read_int (r);
974 if (var_name_len > ID_MAX_LEN)
975 sys_error (r, "Variable name length in long string value label "
976 "record (%d) exceeds %d-byte limit.",
977 var_name_len, ID_MAX_LEN);
978 read_string (r, var_name, var_name_len + 1);
980 /* Read width, number of values. */
981 width = read_int (r);
982 n_values = read_int (r);
984 printf ("\t%08llx: %s, width %d, %d values\n",
985 posn, var_name, width, n_values);
988 for (i = 0; i < n_values; i++)
996 posn = ftello (r->file);
999 value_length = read_int (r);
1000 value = xmalloc (value_length + 1);
1001 read_string (r, value, value_length + 1);
1004 label_length = read_int (r);
1005 label = xmalloc (label_length + 1);
1006 read_string (r, label, label_length + 1);
1008 printf ("\t\t%08llx: \"%s\" (%d bytes) => \"%s\" (%d bytes)\n",
1009 posn, value, value_length, label, label_length);
1018 hex_dump (size_t offset, const void *buffer_, size_t buffer_size)
1020 const uint8_t *buffer = buffer_;
1022 while (buffer_size > 0)
1024 size_t n = MIN (buffer_size, 16);
1027 printf ("%04zx", offset);
1028 for (i = 0; i < 16; i++)
1031 printf ("%c%02x", i == 8 ? '-' : ' ', buffer[i]);
1037 for (i = 0; i < 16; i++)
1039 unsigned char c = i < n ? buffer[i] : ' ';
1040 putchar (isprint (c) ? c : '.');
1050 /* Reads and prints any type 7 record that we don't understand. */
1052 read_unknown_extension (struct sfm_reader *r, size_t size, size_t count)
1054 unsigned char *buffer;
1057 if (size == 0 || count > 65536 / size)
1058 skip_bytes (r, size * count);
1061 buffer = xmalloc (size);
1062 for (i = 0; i < count; i++)
1064 read_bytes (r, buffer, size);
1065 hex_dump (i * size, buffer, size);
1071 buffer = xmalloc (count);
1072 read_bytes (r, buffer, count);
1073 if (memchr (buffer, 0, count) == 0)
1074 for (i = 0; i < count; i++)
1076 unsigned char c = buffer[i];
1080 else if (c == '\n' || isprint (c))
1083 printf ("\\%02x", c);
1086 hex_dump (0, buffer, count);
1092 read_variable_attributes (struct sfm_reader *r, size_t size, size_t count)
1094 struct text_record *text;
1096 printf ("%08llx: variable attributes\n", (long long int) ftello (r->file));
1097 text = open_text_record (r, size * count);
1100 const char *variable = text_tokenize (text, ':');
1101 if (variable == NULL || !read_attributes (r, text, variable))
1104 close_text_record (text);
1108 read_compressed_data (struct sfm_reader *r, int max_cases)
1110 enum { N_OPCODES = 8 };
1111 uint8_t opcodes[N_OPCODES];
1112 long long int opcode_ofs;
1118 printf ("\n%08llx: compressed data:\n", (long long int) ftello (r->file));
1120 opcode_idx = N_OPCODES;
1123 for (case_num = 0; case_num < max_cases; case_num++)
1125 printf ("%08llx: case %d's uncompressible data begins\n",
1126 (long long int) ftello (r->file), case_num);
1127 for (i = 0; i < r->n_var_widths; )
1129 int width = r->var_widths[i];
1133 if (opcode_idx >= N_OPCODES)
1135 opcode_ofs = ftello (r->file);
1138 if (!try_read_bytes (r, opcodes, 8))
1142 read_bytes (r, opcodes, 8);
1145 opcode = opcodes[opcode_idx];
1146 printf ("%08llx: variable %d: opcode %d: ",
1147 opcode_ofs + opcode_idx, i, opcode);
1152 printf ("%g", opcode - r->bias);
1154 printf (", but this is a string variable (width=%d)", width);
1160 printf ("ignored padding\n");
1164 printf ("end of data\n");
1168 read_bytes (r, raw_value, 8);
1169 printf ("uncompressible data: ");
1170 print_untyped_value (r, raw_value);
1178 printf (", but this is a numeric variable");
1186 printf (", but this is a string variable (width=%d)", width);
1197 /* Helpers for reading records that consist of structured text
1203 struct sfm_reader *reader; /* Reader. */
1204 char *buffer; /* Record contents. */
1205 size_t size; /* Size of buffer. */
1206 size_t pos; /* Current position in buffer. */
1209 /* Reads SIZE bytes into a text record for R,
1210 and returns the new text record. */
1211 static struct text_record *
1212 open_text_record (struct sfm_reader *r, size_t size)
1214 struct text_record *text = xmalloc (sizeof *text);
1215 char *buffer = xmalloc (size + 1);
1216 read_bytes (r, buffer, size);
1217 buffer[size] = '\0';
1219 text->buffer = buffer;
1225 /* Closes TEXT and frees its storage.
1226 Not really needed, because the pool will free the text record anyway,
1227 but can be used to free it earlier. */
1229 close_text_record (struct text_record *text)
1231 free (text->buffer);
1236 text_tokenize (struct text_record *text, int delimiter)
1238 size_t start = text->pos;
1239 while (text->pos < text->size
1240 && text->buffer[text->pos] != delimiter
1241 && text->buffer[text->pos] != '\0')
1243 if (start == text->pos)
1245 text->buffer[text->pos++] = '\0';
1246 return &text->buffer[start];
1250 text_match (struct text_record *text, int c)
1252 if (text->pos < text->size && text->buffer[text->pos] == c)
1261 /* Reads a integer value expressed in decimal, then a space, then a string that
1262 consists of exactly as many bytes as specified by the integer, then a space,
1263 from TEXT. Returns the string, null-terminated, as a subset of TEXT's
1264 buffer (so the caller should not free the string). */
1266 text_parse_counted_string (struct text_record *text)
1274 while (isdigit ((unsigned char) text->buffer[text->pos]))
1275 n = (n * 10) + (text->buffer[text->pos++] - '0');
1276 if (start == text->pos)
1278 sys_error (text->reader, "expecting digit at offset %zu in record",
1283 if (!text_match (text, ' '))
1285 sys_error (text->reader, "expecting space at offset %zu in record",
1290 if (text->pos + n > text->size)
1292 sys_error (text->reader, "%zu-byte string starting at offset %zu "
1293 "exceeds record length %zu", n, text->pos, text->size);
1297 s = &text->buffer[text->pos];
1300 sys_error (text->reader, "expecting space at offset %zu following "
1301 "%zu-byte string", text->pos + n, n);
1309 /* Reads a variable=value pair from TEXT.
1310 Looks up the variable in DICT and stores it into *VAR.
1311 Stores a null-terminated value into *VALUE. */
1313 read_variable_to_value_pair (struct text_record *text,
1314 char **key, char **value)
1316 *key = text_tokenize (text, '=');
1317 *value = text_tokenize (text, '\t');
1318 if (!*key || !*value)
1321 while (text->pos < text->size
1322 && (text->buffer[text->pos] == '\t'
1323 || text->buffer[text->pos] == '\0'))
1328 /* Returns the current byte offset inside the TEXT's string. */
1330 text_pos (const struct text_record *text)
1339 %s, a utility for dissecting system files.\n\
1340 Usage: %s [OPTION]... SYSFILE...\n\
1341 where each SYSFILE is the name of a system file.\n\
1344 --data[=MAXCASES] print (up to MAXCASES cases of) compressed data\n\
1345 --help display this help and exit\n\
1346 --version output version information and exit\n",
1347 program_name, program_name);
1350 /* Displays a corruption message. */
1352 sys_msg (struct sfm_reader *r, const char *format, va_list args)
1354 printf ("\"%s\" near offset 0x%llx: ",
1355 r->file_name, (long long int) ftello (r->file));
1356 vprintf (format, args);
1360 /* Displays a warning for the current file position. */
1362 sys_warn (struct sfm_reader *r, const char *format, ...)
1366 va_start (args, format);
1367 sys_msg (r, format, args);
1371 /* Displays an error for the current file position,
1372 marks it as in an error state,
1373 and aborts reading it using longjmp. */
1375 sys_error (struct sfm_reader *r, const char *format, ...)
1379 va_start (args, format);
1380 sys_msg (r, format, args);
1383 exit (EXIT_FAILURE);
1386 /* Reads BYTE_CNT bytes into BUF.
1387 Returns true if exactly BYTE_CNT bytes are successfully read.
1388 Aborts if an I/O error or a partial read occurs.
1389 If EOF_IS_OK, then an immediate end-of-file causes false to be
1390 returned; otherwise, immediate end-of-file causes an abort
1393 read_bytes_internal (struct sfm_reader *r, bool eof_is_ok,
1394 void *buf, size_t byte_cnt)
1396 size_t bytes_read = fread (buf, 1, byte_cnt, r->file);
1397 if (bytes_read == byte_cnt)
1399 else if (ferror (r->file))
1400 sys_error (r, "System error: %s.", strerror (errno));
1401 else if (!eof_is_ok || bytes_read != 0)
1402 sys_error (r, "Unexpected end of file.");
1407 /* Reads BYTE_CNT into BUF.
1408 Aborts upon I/O error or if end-of-file is encountered. */
1410 read_bytes (struct sfm_reader *r, void *buf, size_t byte_cnt)
1412 read_bytes_internal (r, false, buf, byte_cnt);
1415 /* Reads BYTE_CNT bytes into BUF.
1416 Returns true if exactly BYTE_CNT bytes are successfully read.
1417 Returns false if an immediate end-of-file is encountered.
1418 Aborts if an I/O error or a partial read occurs. */
1420 try_read_bytes (struct sfm_reader *r, void *buf, size_t byte_cnt)
1422 return read_bytes_internal (r, true, buf, byte_cnt);
1425 /* Reads a 32-bit signed integer from R and returns its value in
1428 read_int (struct sfm_reader *r)
1431 read_bytes (r, integer, sizeof integer);
1432 return integer_get (r->integer_format, integer, sizeof integer);
1435 /* Reads a 64-bit signed integer from R and returns its value in
1438 read_int64 (struct sfm_reader *r)
1441 read_bytes (r, integer, sizeof integer);
1442 return integer_get (r->integer_format, integer, sizeof integer);
1445 /* Reads a 64-bit floating-point number from R and returns its
1446 value in host format. */
1448 read_float (struct sfm_reader *r)
1451 read_bytes (r, number, sizeof number);
1452 return float_get_double (r->float_format, number);
1455 /* Reads exactly SIZE - 1 bytes into BUFFER
1456 and stores a null byte into BUFFER[SIZE - 1]. */
1458 read_string (struct sfm_reader *r, char *buffer, size_t size)
1461 read_bytes (r, buffer, size - 1);
1462 buffer[size - 1] = '\0';
1465 /* Skips BYTES bytes forward in R. */
1467 skip_bytes (struct sfm_reader *r, size_t bytes)
1472 size_t chunk = MIN (sizeof buffer, bytes);
1473 read_bytes (r, buffer, chunk);
1479 trim_spaces (char *s)
1481 char *end = strchr (s, '\0');
1482 while (end > s && end[-1] == ' ')