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"
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_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);
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 *);
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 *, ...)
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 *);
117 static void print_string (const char *s, size_t len);
120 main (int argc, char *argv[])
126 set_program_name (argv[0]);
130 static const struct option long_options[] =
132 { "data", optional_argument, NULL, 'd' },
133 { "help", no_argument, NULL, 'h' },
134 { "version", no_argument, NULL, 'v' },
135 { NULL, 0, NULL, 0 },
140 c = getopt_long (argc, argv, "d::hv", long_options, NULL);
147 max_cases = optarg ? atoi (optarg) : INT_MAX;
151 version_etc (stdout, "pspp-dump-sav", PACKAGE_NAME, PACKAGE_VERSION,
152 "Ben Pfaff", "John Darrington", NULL_SENTINEL);
165 error (1, 0, "at least one non-option argument is required; "
166 "use --help for help");
168 for (i = optind; i < argc; i++)
172 r.file_name = argv[i];
173 r.file = fopen (r.file_name, "rb");
175 error (EXIT_FAILURE, errno, "error opening `%s'", r.file_name);
176 r.n_variable_records = 0;
179 r.allocated_var_widths = 0;
181 r.compressed = false;
183 if (argc - optind > 1)
184 printf ("Reading \"%s\":\n", r.file_name);
187 while ((rec_type = read_int (&r)) != 999)
192 read_variable_record (&r);
196 read_value_label_record (&r);
200 sys_error (&r, "Misplaced type 4 record.");
203 read_document_record (&r);
207 read_extension_record (&r);
211 sys_error (&r, "Unrecognized record type %d.", rec_type);
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);
219 if (r.compressed && max_cases > 0)
220 read_compressed_data (&r, max_cases);
229 read_header (struct sfm_reader *r)
232 char eye_catcher[61];
233 uint8_t raw_layout_code[4];
236 int32_t weight_index;
239 char creation_date[10];
240 char creation_time[9];
243 read_string (r, rec_type, sizeof rec_type);
244 read_string (r, eye_catcher, sizeof eye_catcher);
246 if (strcmp ("$FL2", rec_type) != 0)
247 sys_error (r, "This is not an SPSS system file.");
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,
253 && !integer_identify (3, raw_layout_code, sizeof raw_layout_code,
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);
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);
266 r->compressed = compressed != 0;
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)
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;
277 r->float_format = FLOAT_IEEE_DOUBLE_LE;
279 r->bias = float_get_double (r->float_format, raw_bias);
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);
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);
300 format_name (int format)
302 switch ((format >> 16) & 0xff)
305 case 2: return "AHEX";
306 case 3: return "COMMA";
307 case 4: return "DOLLAR";
310 case 7: return "PIBHEX";
312 case 9: return "PIB";
313 case 10: return "PK";
314 case 11: return "RB";
315 case 12: return "RBHEX";
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";
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
348 read_variable_record (struct sfm_reader *r)
351 int has_variable_label;
352 int missing_value_code;
357 printf ("%08llx: variable record #%d\n",
358 (long long int) ftello (r->file), r->n_variable_records++);
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';
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;
376 printf ("\tWidth: %d (%s)\n",
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"
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);
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)
403 long long int offset = ftello (r->file);
404 size_t len, read_len;
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);
414 /* Skip unread label bytes. */
415 skip_bytes (r, len - read_len);
417 /* Skip label padding up to multiple of 4 bytes. */
418 skip_bytes (r, ROUND_UP (len, 4) - len);
421 /* Set missing values. */
422 if (missing_value_code != 0)
426 printf ("\t%08llx Missing values:", (long long int) ftello (r->file));
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)
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;
440 for (i = 0; i < missing_value_code; i++)
441 printf (" %g", read_float (r));
445 if (missing_value_code < 1 || missing_value_code > 3)
446 sys_error (r, "String missing value indicator field is not "
448 for (i = 0; i < missing_value_code; i++)
451 read_string (r, string, sizeof string);
452 printf (" \"%s\"", string);
460 print_untyped_value (struct sfm_reader *r, char raw_value[8])
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]))
470 printf ("%g/\"%.*s\"", value, n_printable, raw_value);
473 /* Reads value labels from sysfile R and inserts them into the
474 associated dictionary. */
476 read_value_label_record (struct sfm_reader *r)
478 int label_cnt, var_cnt;
481 printf ("%08llx: value labels record\n", (long long int) ftello (r->file));
483 /* Read number of labels. */
484 label_cnt = read_int (r);
485 for (i = 0; i < label_cnt; i++)
488 unsigned char label_len;
492 read_bytes (r, raw_value, sizeof raw_value);
494 /* Read label length. */
495 read_bytes (r, &label_len, sizeof label_len);
496 padded_len = ROUND_UP (label_len + 1, 8);
498 /* Read label, padding. */
499 read_bytes (r, label, padded_len - 1);
500 label[label_len] = 0;
503 print_untyped_value (r, raw_value);
504 printf (": \"%s\"\n", label);
507 /* Now, read the type 4 record that has the list of variables
508 to which the value labels are to be applied. */
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.");
515 /* Read number of variables associated with value label from type 4
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));
525 read_document_record (struct sfm_reader *r)
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);
534 for (i = 0; i < n_lines; i++)
537 printf ("\t%08llx: ", (long long int) ftello (r->file));
538 read_string (r, line, sizeof line);
540 printf ("line %d: \"%s\"\n", i, line);
545 read_extension_record (struct sfm_reader *r)
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;
553 printf ("%08llx: Record 7, subtype %d, size=%zu, count=%zu\n",
554 offset, subtype, size, count);
559 read_machine_integer_info (r, size, count);
563 read_machine_float_info (r, size, count);
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. */
573 /* DATE variable information. We don't use it yet, but we
579 read_mrsets (r, size, count);
583 read_extra_product_info (r, size, count);
587 read_display_parameters (r, size, count);
591 read_long_var_name_map (r, size, count);
595 read_long_string_map (r, size, count);
599 read_ncases64 (r, size, count);
603 read_datafile_attributes (r, size, count);
607 read_variable_attributes (r, size, count);
611 read_character_encoding (r, size, count);
615 read_long_string_value_labels (r, size, count);
619 sys_warn (r, "Unrecognized record type 7, subtype %d.", subtype);
620 read_unknown_extension (r, size, count);
624 skip_bytes (r, bytes);
628 read_machine_integer_info (struct sfm_reader *r, size_t size, size_t count)
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);
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);
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"
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);
661 /* Read record type 7, subtype 4. */
663 read_machine_float_info (struct sfm_reader *r, size_t size, size_t count)
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);
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.",
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");
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");
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");
692 read_extra_product_info (struct sfm_reader *r,
693 size_t size, size_t count)
695 struct text_record *text;
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);
705 /* Read record type 7, subtype 7. */
707 read_mrsets (struct sfm_reader *r, size_t size, size_t count)
709 struct text_record *text;
711 printf ("%08llx: multiple response sets\n",
712 (long long int) ftello (r->file));
713 text = open_text_record (r, size * count);
717 enum { MRSET_MC, MRSET_MD } type;
718 bool cat_label_from_counted_values = false;
719 bool label_from_var_label = false;
722 const char *variables;
724 name = text_tokenize (text, '=');
728 if (text_match (text, 'C'))
732 if (!text_match (text, ' '))
734 sys_warn (r, "missing space following 'C' at offset %zu "
735 "in mrsets record", text_pos (text));
739 else if (text_match (text, 'D'))
743 else if (text_match (text, 'E'))
748 cat_label_from_counted_values = true;
750 if (!text_match (text, ' '))
752 sys_warn (r, "Missing space following `%c' at offset %zu "
753 "in MRSETS record", 'E', text_pos (text));
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));
768 sys_warn (r, "missing `C', `D', or `E' at offset %zu "
769 "in mrsets record", text_pos (text));
773 if (type == MRSET_MD)
775 counted = text_parse_counted_string (text);
780 label = text_parse_counted_string (text);
784 variables = text_tokenize (text, '\n');
785 if (variables == NULL)
787 sys_warn (r, "missing variable names following label "
788 "at offset %zu in mrsets record", text_pos (text));
792 printf ("\t\"%s\": multiple %s set",
793 name, type == MRSET_MC ? "category" : "dichotomy");
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);
804 close_text_record (text);
807 /* Read record type 7, subtype 11. */
809 read_display_parameters (struct sfm_reader *r, size_t size, size_t count)
815 printf ("%08llx: variable display parameters\n",
816 (long long int) ftello (r->file));
819 sys_warn (r, "Bad size %zu on extension 11.", size);
820 skip_bytes (r, size * count);
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;
831 sys_warn (r, "Extension 11 has bad count %zu (for %zu variables.",
833 skip_bytes (r, size * count);
837 for (i = 0; i < n_vars; ++i)
839 int measure = read_int (r);
840 int width = includes_width ? read_int (r) : 0;
841 int align = read_int (r);
843 printf ("\tVar #%zu: measure=%d (%s)", i, measure,
844 (measure == 1 ? "nominal"
845 : measure == 2 ? "ordinal"
846 : measure == 3 ? "scale"
849 printf (", width=%d", width);
850 printf (", align=%d (%s)\n", align,
852 : align == 1 ? "right"
853 : align == 2 ? "centre"
858 /* Reads record type 7, subtype 13, which gives the long name
859 that corresponds to each short name. */
861 read_long_var_name_map (struct sfm_reader *r, size_t size, size_t count)
863 struct text_record *text;
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);
875 /* Reads record type 7, subtype 14, which gives the real length
876 of each very long string. Rearranges DICT accordingly. */
878 read_long_string_map (struct sfm_reader *r, size_t size, size_t count)
880 struct text_record *text;
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);
893 read_attributes (struct sfm_reader *r, struct text_record *text,
894 const char *variable)
901 key = text_tokenize (text, '(');
905 for (index = 1; ; index++)
907 /* Parse the value. */
908 const char *value = text_tokenize (text, '\n');
911 sys_warn (r, "%s: Error parsing attribute value %s[%d]",
912 variable, key, index);
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);
920 printf ("\t%s: %s[%d] = \"%.*s\"\n",
921 variable, key, index, (int) strlen (value) - 2, value + 1);
923 /* Was this the last value for this attribute? */
924 if (text_match (text, ')'))
928 if (text_match (text, '/'))
933 /* Read extended number of cases record. */
935 read_ncases64 (struct sfm_reader *r, size_t size, size_t count)
937 int64_t unknown, ncases64;
941 sys_warn (r, "Bad size %zu for extended number of cases.", size);
942 skip_bytes (r, size * count);
947 sys_warn (r, "Bad count %zu for extended number of cases.", size);
948 skip_bytes (r, size * count);
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);
959 read_datafile_attributes (struct sfm_reader *r, size_t size, size_t count)
961 struct text_record *text;
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);
970 read_character_encoding (struct sfm_reader *r, size_t size, size_t count)
972 long long int posn = ftello (r->file);
973 char *encoding = xcalloc (size, count + 1);
974 read_string (r, encoding, count + 1);
976 printf ("%08llx: Character Encoding: %s\n", posn, encoding);
980 read_long_string_value_labels (struct sfm_reader *r, size_t size, size_t count)
982 long long int start = ftello (r->file);
984 printf ("%08llx: long string value labels\n", start);
985 while (ftello (r->file) - start < size * count)
987 long long posn = ftello (r->file);
988 char var_name[ID_MAX_LEN + 1];
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);
1002 /* Read width, number of values. */
1003 width = read_int (r);
1004 n_values = read_int (r);
1006 printf ("\t%08llx: %s, width %d, %d values\n",
1007 posn, var_name, width, n_values);
1010 for (i = 0; i < n_values; i++)
1018 posn = ftello (r->file);
1021 value_length = read_int (r);
1022 value = xmalloc (value_length + 1);
1023 read_string (r, value, value_length + 1);
1026 label_length = read_int (r);
1027 label = xmalloc (label_length + 1);
1028 read_string (r, label, label_length + 1);
1030 printf ("\t\t%08llx: \"%s\" (%d bytes) => \"%s\" (%d bytes)\n",
1031 posn, value, value_length, label, label_length);
1040 hex_dump (size_t offset, const void *buffer_, size_t buffer_size)
1042 const uint8_t *buffer = buffer_;
1044 while (buffer_size > 0)
1046 size_t n = MIN (buffer_size, 16);
1049 printf ("%04zx", offset);
1050 for (i = 0; i < 16; i++)
1053 printf ("%c%02x", i == 8 ? '-' : ' ', buffer[i]);
1059 for (i = 0; i < 16; i++)
1061 unsigned char c = i < n ? buffer[i] : ' ';
1062 putchar (isprint (c) ? c : '.');
1072 /* Reads and prints any type 7 record that we don't understand. */
1074 read_unknown_extension (struct sfm_reader *r, size_t size, size_t count)
1076 unsigned char *buffer;
1079 if (size == 0 || count > 65536 / size)
1080 skip_bytes (r, size * count);
1083 buffer = xmalloc (size);
1084 for (i = 0; i < count; i++)
1086 read_bytes (r, buffer, size);
1087 hex_dump (i * size, buffer, size);
1093 buffer = xmalloc (count);
1094 read_bytes (r, buffer, count);
1095 print_string (CHAR_CAST (char *, buffer), count);
1101 read_variable_attributes (struct sfm_reader *r, size_t size, size_t count)
1103 struct text_record *text;
1105 printf ("%08llx: variable attributes\n", (long long int) ftello (r->file));
1106 text = open_text_record (r, size * count);
1109 const char *variable = text_tokenize (text, ':');
1110 if (variable == NULL || !read_attributes (r, text, variable))
1113 close_text_record (text);
1117 read_compressed_data (struct sfm_reader *r, int max_cases)
1119 enum { N_OPCODES = 8 };
1120 uint8_t opcodes[N_OPCODES];
1121 long long int opcode_ofs;
1127 printf ("\n%08llx: compressed data:\n", (long long int) ftello (r->file));
1129 opcode_idx = N_OPCODES;
1132 for (case_num = 0; case_num < max_cases; case_num++)
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; )
1138 int width = r->var_widths[i];
1142 if (opcode_idx >= N_OPCODES)
1144 opcode_ofs = ftello (r->file);
1147 if (!try_read_bytes (r, opcodes, 8))
1151 read_bytes (r, opcodes, 8);
1154 opcode = opcodes[opcode_idx];
1155 printf ("%08llx: variable %d: opcode %d: ",
1156 opcode_ofs + opcode_idx, i, opcode);
1161 printf ("%g", opcode - r->bias);
1163 printf (", but this is a string variable (width=%d)", width);
1169 printf ("ignored padding\n");
1173 printf ("end of data\n");
1177 read_bytes (r, raw_value, 8);
1178 printf ("uncompressible data: ");
1179 print_untyped_value (r, raw_value);
1187 printf (", but this is a numeric variable");
1195 printf (", but this is a string variable (width=%d)", width);
1206 /* Helpers for reading records that consist of structured text
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. */
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)
1223 struct text_record *text = xmalloc (sizeof *text);
1224 char *buffer = xmalloc (size + 1);
1225 read_bytes (r, buffer, size);
1226 buffer[size] = '\0';
1228 text->buffer = buffer;
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. */
1238 close_text_record (struct text_record *text)
1240 free (text->buffer);
1245 text_tokenize (struct text_record *text, int delimiter)
1247 size_t start = text->pos;
1248 while (text->pos < text->size
1249 && text->buffer[text->pos] != delimiter
1250 && text->buffer[text->pos] != '\0')
1252 if (start == text->pos)
1254 text->buffer[text->pos++] = '\0';
1255 return &text->buffer[start];
1259 text_match (struct text_record *text, int c)
1261 if (text->pos < text->size && text->buffer[text->pos] == c)
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). */
1275 text_parse_counted_string (struct text_record *text)
1283 while (isdigit ((unsigned char) text->buffer[text->pos]))
1284 n = (n * 10) + (text->buffer[text->pos++] - '0');
1285 if (start == text->pos)
1287 sys_error (text->reader, "expecting digit at offset %zu in record",
1292 if (!text_match (text, ' '))
1294 sys_error (text->reader, "expecting space at offset %zu in record",
1299 if (text->pos + n > text->size)
1301 sys_error (text->reader, "%zu-byte string starting at offset %zu "
1302 "exceeds record length %zu", n, text->pos, text->size);
1306 s = &text->buffer[text->pos];
1309 sys_error (text->reader, "expecting space at offset %zu following "
1310 "%zu-byte string", text->pos + n, n);
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. */
1322 read_variable_to_value_pair (struct text_record *text,
1323 char **key, char **value)
1325 *key = text_tokenize (text, '=');
1326 *value = text_tokenize (text, '\t');
1327 if (!*key || !*value)
1330 while (text->pos < text->size
1331 && (text->buffer[text->pos] == '\t'
1332 || text->buffer[text->pos] == '\0'))
1337 /* Returns the current byte offset inside the TEXT's string. */
1339 text_pos (const struct text_record *text)
1345 text_get_all (const struct text_record *text)
1347 return text->buffer;
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\
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);
1365 /* Displays a corruption message. */
1367 sys_msg (struct sfm_reader *r, const char *format, va_list args)
1369 printf ("\"%s\" near offset 0x%llx: ",
1370 r->file_name, (long long int) ftello (r->file));
1371 vprintf (format, args);
1375 /* Displays a warning for the current file position. */
1377 sys_warn (struct sfm_reader *r, const char *format, ...)
1381 va_start (args, format);
1382 sys_msg (r, format, args);
1386 /* Displays an error for the current file position,
1387 marks it as in an error state,
1388 and aborts reading it using longjmp. */
1390 sys_error (struct sfm_reader *r, const char *format, ...)
1394 va_start (args, format);
1395 sys_msg (r, format, args);
1398 exit (EXIT_FAILURE);
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
1408 read_bytes_internal (struct sfm_reader *r, bool eof_is_ok,
1409 void *buf, size_t byte_cnt)
1411 size_t bytes_read = fread (buf, 1, byte_cnt, r->file);
1412 if (bytes_read == byte_cnt)
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.");
1422 /* Reads BYTE_CNT into BUF.
1423 Aborts upon I/O error or if end-of-file is encountered. */
1425 read_bytes (struct sfm_reader *r, void *buf, size_t byte_cnt)
1427 read_bytes_internal (r, false, buf, byte_cnt);
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. */
1435 try_read_bytes (struct sfm_reader *r, void *buf, size_t byte_cnt)
1437 return read_bytes_internal (r, true, buf, byte_cnt);
1440 /* Reads a 32-bit signed integer from R and returns its value in
1443 read_int (struct sfm_reader *r)
1446 read_bytes (r, integer, sizeof integer);
1447 return integer_get (r->integer_format, integer, sizeof integer);
1450 /* Reads a 64-bit signed integer from R and returns its value in
1453 read_int64 (struct sfm_reader *r)
1456 read_bytes (r, integer, sizeof integer);
1457 return integer_get (r->integer_format, integer, sizeof integer);
1460 /* Reads a 64-bit floating-point number from R and returns its
1461 value in host format. */
1463 read_float (struct sfm_reader *r)
1466 read_bytes (r, number, sizeof number);
1467 return float_get_double (r->float_format, number);
1470 /* Reads exactly SIZE - 1 bytes into BUFFER
1471 and stores a null byte into BUFFER[SIZE - 1]. */
1473 read_string (struct sfm_reader *r, char *buffer, size_t size)
1476 read_bytes (r, buffer, size - 1);
1477 buffer[size - 1] = '\0';
1480 /* Skips BYTES bytes forward in R. */
1482 skip_bytes (struct sfm_reader *r, size_t bytes)
1487 size_t chunk = MIN (sizeof buffer, bytes);
1488 read_bytes (r, buffer, chunk);
1494 trim_spaces (char *s)
1496 char *end = strchr (s, '\0');
1497 while (end > s && end[-1] == ' ')
1503 print_string (const char *s, size_t len)
1505 if (memchr (s, 0, len) == 0)
1509 for (i = 0; i < len; i++)
1511 unsigned char c = s[i];
1515 else if (c == '\n' || isprint (c))
1518 printf ("\\%02x", c);
1523 hex_dump (0, s, len);