1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2008, 2009, 2010 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/>. */
25 #include <data/val-type.h>
26 #include <libpspp/compiler.h>
27 #include <libpspp/float-format.h>
28 #include <libpspp/integer-format.h>
29 #include <libpspp/misc.h>
37 #define _(msgid) gettext (msgid)
39 #define VAR_NAME_LEN 64
43 const char *file_name;
46 int n_variable_records, n_variables;
49 size_t n_var_widths, allocated_var_widths;
51 enum integer_format integer_format;
52 enum float_format float_format;
58 static void read_header (struct sfm_reader *);
59 static void read_variable_record (struct sfm_reader *);
60 static void read_value_label_record (struct sfm_reader *);
61 static void read_document_record (struct sfm_reader *);
62 static void read_extension_record (struct sfm_reader *);
63 static void read_machine_integer_info (struct sfm_reader *,
64 size_t size, size_t count);
65 static void read_machine_float_info (struct sfm_reader *,
66 size_t size, size_t count);
67 static void read_mrsets (struct sfm_reader *, size_t size, size_t count);
68 static void read_display_parameters (struct sfm_reader *,
69 size_t size, size_t count);
70 static void read_long_var_name_map (struct sfm_reader *r,
71 size_t size, size_t count);
72 static void read_long_string_map (struct sfm_reader *r,
73 size_t size, size_t count);
74 static void read_datafile_attributes (struct sfm_reader *r,
75 size_t size, size_t count);
76 static void read_variable_attributes (struct sfm_reader *r,
77 size_t size, size_t count);
78 static void read_ncases64 (struct sfm_reader *, size_t size, size_t count);
79 static void read_character_encoding (struct sfm_reader *r,
80 size_t size, size_t count);
81 static void read_long_string_value_labels (struct sfm_reader *r,
82 size_t size, size_t count);
83 static void read_unknown_extension (struct sfm_reader *,
84 size_t size, size_t count);
85 static void read_compressed_data (struct sfm_reader *);
87 static struct text_record *open_text_record (
88 struct sfm_reader *, size_t size);
89 static void close_text_record (struct text_record *);
90 static bool read_variable_to_value_pair (struct text_record *,
91 char **key, char **value);
92 static char *text_tokenize (struct text_record *, int delimiter);
93 static bool text_match (struct text_record *text, int c);
94 static const char *text_parse_counted_string (struct text_record *);
95 static size_t text_pos (const struct text_record *);
97 static void usage (int exit_code);
98 static void sys_warn (struct sfm_reader *, const char *, ...)
100 static void sys_error (struct sfm_reader *, const char *, ...)
104 static void read_bytes (struct sfm_reader *, void *, size_t);
105 static int read_int (struct sfm_reader *);
106 static int64_t read_int64 (struct sfm_reader *);
107 static double read_float (struct sfm_reader *);
108 static void read_string (struct sfm_reader *, char *, size_t);
109 static void skip_bytes (struct sfm_reader *, size_t);
110 static void trim_spaces (char *);
113 main (int argc, char *argv[])
118 set_program_name (argv[0]);
120 usage (EXIT_FAILURE);
122 for (i = 1; i < argc; i++)
126 r.file_name = argv[i];
127 r.file = fopen (r.file_name, "rb");
129 error (EXIT_FAILURE, errno, "error opening \"%s\"", r.file_name);
130 r.n_variable_records = 0;
133 r.allocated_var_widths = 0;
135 r.compressed = false;
138 printf ("Reading \"%s\":\n", r.file_name);
141 while ((rec_type = read_int (&r)) != 999)
146 read_variable_record (&r);
150 read_value_label_record (&r);
154 sys_error (&r, _("Misplaced type 4 record."));
157 read_document_record (&r);
161 read_extension_record (&r);
165 sys_error (&r, _("Unrecognized record type %d."), rec_type);
168 printf ("%08lx: end-of-dictionary record "
169 "(first byte of data at %08lx)\n",
170 ftell (r.file), ftell (r.file) + 4);
173 read_compressed_data (&r);
182 read_header (struct sfm_reader *r)
185 char eye_catcher[61];
186 uint8_t raw_layout_code[4];
188 int32_t nominal_case_size;
190 int32_t weight_index;
193 char creation_date[10];
194 char creation_time[9];
197 read_string (r, rec_type, sizeof rec_type);
198 read_string (r, eye_catcher, sizeof eye_catcher);
200 if (strcmp ("$FL2", rec_type) != 0)
201 sys_error (r, _("This is not an SPSS system file."));
203 /* Identify integer format. */
204 read_bytes (r, raw_layout_code, sizeof raw_layout_code);
205 if ((!integer_identify (2, raw_layout_code, sizeof raw_layout_code,
207 && !integer_identify (3, raw_layout_code, sizeof raw_layout_code,
209 || (r->integer_format != INTEGER_MSB_FIRST
210 && r->integer_format != INTEGER_LSB_FIRST))
211 sys_error (r, _("This is not an SPSS system file."));
212 layout_code = integer_get (r->integer_format,
213 raw_layout_code, sizeof raw_layout_code);
215 nominal_case_size = read_int (r);
216 compressed = read_int (r);
217 weight_index = read_int (r);
218 ncases = read_int (r);
220 r->compressed = compressed != 0;
222 /* Identify floating-point format and obtain compression bias. */
223 read_bytes (r, raw_bias, sizeof raw_bias);
224 if (float_identify (100.0, raw_bias, sizeof raw_bias, &r->float_format) == 0)
226 sys_warn (r, _("Compression bias is not the usual "
227 "value of 100, or system file uses unrecognized "
228 "floating-point format."));
229 if (r->integer_format == INTEGER_MSB_FIRST)
230 r->float_format = FLOAT_IEEE_DOUBLE_BE;
232 r->float_format = FLOAT_IEEE_DOUBLE_LE;
234 r->bias = float_get_double (r->float_format, raw_bias);
236 read_string (r, creation_date, sizeof creation_date);
237 read_string (r, creation_time, sizeof creation_time);
238 read_string (r, file_label, sizeof file_label);
239 trim_spaces (file_label);
242 printf ("File header record:\n");
243 printf ("\t%17s: %s\n", "Product name", eye_catcher);
244 printf ("\t%17s: %"PRId32"\n", "Layout code", layout_code);
245 printf ("\t%17s: %"PRId32"\n", "Compressed", compressed);
246 printf ("\t%17s: %"PRId32"\n", "Weight index", weight_index);
247 printf ("\t%17s: %"PRId32"\n", "Number of cases", ncases);
248 printf ("\t%17s: %g\n", "Compression bias", r->bias);
249 printf ("\t%17s: %s\n", "Creation date", creation_date);
250 printf ("\t%17s: %s\n", "Creation time", creation_time);
251 printf ("\t%17s: \"%s\"\n", "File label", file_label);
255 format_name (int format)
257 switch ((format >> 16) & 0xff)
260 case 2: return "AHEX";
261 case 3: return "COMMA";
262 case 4: return "DOLLAR";
265 case 7: return "PIBHEX";
267 case 9: return "PIB";
268 case 10: return "PK";
269 case 11: return "RB";
270 case 12: return "RBHEX";
274 case 20: return "DATE";
275 case 21: return "TIME";
276 case 22: return "DATETIME";
277 case 23: return "ADATE";
278 case 24: return "JDATE";
279 case 25: return "DTIME";
280 case 26: return "WKDAY";
281 case 27: return "MONTH";
282 case 28: return "MOYR";
283 case 29: return "QYR";
284 case 30: return "WKYR";
285 case 31: return "PCT";
286 case 32: return "DOT";
287 case 33: return "CCA";
288 case 34: return "CCB";
289 case 35: return "CCC";
290 case 36: return "CCD";
291 case 37: return "CCE";
292 case 38: return "EDATE";
293 case 39: return "SDATE";
294 default: return "invalid";
298 /* Reads a variable (type 2) record from R and adds the
299 corresponding variable to DICT.
300 Also skips past additional variable records for long string
303 read_variable_record (struct sfm_reader *r)
306 int has_variable_label;
307 int missing_value_code;
312 printf ("%08lx: variable record #%d\n",
313 ftell (r->file), r->n_variable_records++);
315 width = read_int (r);
316 has_variable_label = read_int (r);
317 missing_value_code = read_int (r);
318 print_format = read_int (r);
319 write_format = read_int (r);
320 read_string (r, name, sizeof name);
321 name[strcspn (name, " ")] = '\0';
326 if (r->n_var_widths >= r->allocated_var_widths)
327 r->var_widths = x2nrealloc (r->var_widths, &r->allocated_var_widths,
328 sizeof *r->var_widths);
329 r->var_widths[r->n_var_widths++] = width;
331 printf ("\tWidth: %d (%s)\n",
334 : width == 0 ? "numeric"
335 : "long string continuation record");
336 printf ("\tVariable label: %d\n", has_variable_label);
337 printf ("\tMissing values code: %d (%s)\n", missing_value_code,
338 (missing_value_code == 0 ? "no missing values"
339 : missing_value_code == 1 ? "one missing value"
340 : missing_value_code == 2 ? "two missing values"
341 : missing_value_code == 3 ? "three missing values"
342 : missing_value_code == -2 ? "one missing value range"
343 : missing_value_code == -3 ? "one missing value, one range"
345 printf ("\tPrint format: %06x (%s%d.%d)\n",
346 print_format, format_name (print_format),
347 (print_format >> 8) & 0xff, print_format & 0xff);
348 printf ("\tWrite format: %06x (%s%d.%d)\n",
349 write_format, format_name (write_format),
350 (write_format >> 8) & 0xff, write_format & 0xff);
351 printf ("\tName: %s\n", name);
353 /* Get variable label, if any. */
354 if (has_variable_label != 0 && has_variable_label != 1)
355 sys_error (r, _("Variable label indicator field is not 0 or 1."));
356 if (has_variable_label == 1)
358 long int offset = ftell (r->file);
363 if (len >= sizeof label)
364 sys_error (r, _("Variable %s has label of invalid length %zu."),
366 read_string (r, label, len + 1);
367 printf("\t%08lx Variable label: \"%s\"\n", offset, label);
369 skip_bytes (r, ROUND_UP (len, 4) - len);
372 /* Set missing values. */
373 if (missing_value_code != 0)
377 printf ("\t%08lx Missing values:", ftell (r->file));
380 if (missing_value_code < -3 || missing_value_code > 3
381 || missing_value_code == -1)
382 sys_error (r, _("Numeric missing value indicator field is not "
383 "-3, -2, 0, 1, 2, or 3."));
384 if (missing_value_code < 0)
386 double low = read_float (r);
387 double high = read_float (r);
388 printf (" %g...%g", low, high);
389 missing_value_code = -missing_value_code - 2;
391 for (i = 0; i < missing_value_code; i++)
392 printf (" %g", read_float (r));
396 if (missing_value_code < 1 || missing_value_code > 3)
397 sys_error (r, _("String missing value indicator field is not "
399 for (i = 0; i < missing_value_code; i++)
402 read_string (r, string, sizeof string);
403 printf (" \"%s\"", string);
411 print_untyped_value (struct sfm_reader *r, char raw_value[8])
416 value = float_get_double (r->float_format, raw_value);
417 for (n_printable = 0; n_printable < sizeof raw_value; n_printable++)
418 if (!isprint (raw_value[n_printable]))
421 printf ("%g/\"%.*s\"", value, n_printable, raw_value);
424 /* Reads value labels from sysfile R and inserts them into the
425 associated dictionary. */
427 read_value_label_record (struct sfm_reader *r)
429 int label_cnt, var_cnt;
432 printf ("%08lx: value labels record\n", ftell (r->file));
434 /* Read number of labels. */
435 label_cnt = read_int (r);
436 for (i = 0; i < label_cnt; i++)
439 unsigned char label_len;
443 read_bytes (r, raw_value, sizeof raw_value);
445 /* Read label length. */
446 read_bytes (r, &label_len, sizeof label_len);
447 padded_len = ROUND_UP (label_len + 1, 8);
449 /* Read label, padding. */
450 read_bytes (r, label, padded_len - 1);
451 label[label_len] = 0;
454 print_untyped_value (r, raw_value);
455 printf (": \"%s\"\n", label);
458 /* Now, read the type 4 record that has the list of variables
459 to which the value labels are to be applied. */
461 /* Read record type of type 4 record. */
462 if (read_int (r) != 4)
463 sys_error (r, _("Variable index record (type 4) does not immediately "
464 "follow value label record (type 3) as it should."));
466 /* Read number of variables associated with value label from type 4
468 printf ("\t%08lx: apply to variables", ftell (r->file));
469 var_cnt = read_int (r);
470 for (i = 0; i < var_cnt; i++)
471 printf (" #%d", read_int (r));
476 read_document_record (struct sfm_reader *r)
481 printf ("%08lx: document record\n", ftell (r->file));
482 n_lines = read_int (r);
483 printf ("\t%d lines of documents\n", n_lines);
485 for (i = 0; i < n_lines; i++)
488 printf ("\t%08lx: ", ftell (r->file));
489 read_string (r, line, sizeof line);
491 printf ("line %d: \"%s\"\n", i, line);
496 read_extension_record (struct sfm_reader *r)
498 long int offset = ftell (r->file);
499 int subtype = read_int (r);
500 size_t size = read_int (r);
501 size_t count = read_int (r);
502 size_t bytes = size * count;
504 printf ("%08lx: Record 7, subtype %d, size=%zu, count=%zu\n",
505 offset, subtype, size, count);
510 read_machine_integer_info (r, size, count);
514 read_machine_float_info (r, size, count);
518 /* Variable sets information. We don't use these yet.
519 They only apply to GUIs; see VARSETS on the APPLY
520 DICTIONARY command in SPSS documentation. */
524 /* DATE variable information. We don't use it yet, but we
530 read_mrsets (r, size, count);
534 read_display_parameters (r, size, count);
538 read_long_var_name_map (r, size, count);
542 read_long_string_map (r, size, count);
546 read_ncases64 (r, size, count);
550 read_datafile_attributes (r, size, count);
554 read_variable_attributes (r, size, count);
558 read_character_encoding (r, size, count);
562 read_long_string_value_labels (r, size, count);
566 sys_warn (r, _("Unrecognized record type 7, subtype %d."), subtype);
567 read_unknown_extension (r, size, count);
571 skip_bytes (r, bytes);
575 read_machine_integer_info (struct sfm_reader *r, size_t size, size_t count)
577 long int offset = ftell (r->file);
578 int version_major = read_int (r);
579 int version_minor = read_int (r);
580 int version_revision = read_int (r);
581 int machine_code = read_int (r);
582 int float_representation = read_int (r);
583 int compression_code = read_int (r);
584 int integer_representation = read_int (r);
585 int character_code = read_int (r);
587 printf ("%08lx: machine integer info\n", offset);
588 if (size != 4 || count != 8)
589 sys_error (r, _("Bad size (%zu) or count (%zu) field on record type 7, "
593 printf ("\tVersion: %d.%d.%d\n",
594 version_major, version_minor, version_revision);
595 printf ("\tMachine code: %d\n", machine_code);
596 printf ("\tFloating point representation: %d (%s)\n",
597 float_representation,
598 float_representation == 1 ? "IEEE 754"
599 : float_representation == 2 ? "IBM 370"
600 : float_representation == 3 ? "DEC VAX"
602 printf ("\tCompression code: %d\n", compression_code);
603 printf ("\tEndianness: %d (%s)\n", integer_representation,
604 integer_representation == 1 ? "big"
605 : integer_representation == 2 ? "little" : "unknown");
606 printf ("\tCharacter code: %d\n", character_code);
609 /* Read record type 7, subtype 4. */
611 read_machine_float_info (struct sfm_reader *r, size_t size, size_t count)
613 long int offset = ftell (r->file);
614 double sysmis = read_float (r);
615 double highest = read_float (r);
616 double lowest = read_float (r);
618 printf ("%08lx: machine float info\n", offset);
619 if (size != 8 || count != 3)
620 sys_error (r, _("Bad size (%zu) or count (%zu) on extension 4."),
623 printf ("\tsysmis: %g\n", sysmis);
624 if (sysmis != SYSMIS)
625 sys_warn (r, _("File specifies unexpected value %g as %s."),
628 printf ("\thighest: %g\n", highest);
629 if (highest != HIGHEST)
630 sys_warn (r, _("File specifies unexpected value %g as %s."),
633 printf ("\tlowest: %g\n", lowest);
634 if (lowest != LOWEST)
635 sys_warn (r, _("File specifies unexpected value %g as %s."),
639 /* Read record type 7, subtype 7. */
641 read_mrsets (struct sfm_reader *r, size_t size, size_t count)
643 struct text_record *text;
645 printf ("%08lx: multiple response sets\n", ftell (r->file));
646 text = open_text_record (r, size * count);
650 enum { MRSET_MC, MRSET_MD } type;
651 bool cat_label_from_counted_values = false;
652 bool label_from_var_label = false;
655 const char *variables;
657 name = text_tokenize (text, '=');
661 if (text_match (text, 'C'))
665 if (!text_match (text, ' '))
667 sys_warn (r, "missing space following 'C' at offset %zu "
668 "in mrsets record", text_pos (text));
672 else if (text_match (text, 'D'))
676 else if (text_match (text, 'E'))
681 cat_label_from_counted_values = true;
683 if (!text_match (text, ' '))
685 sys_warn (r, _("Missing space following 'E' at offset %zu "
686 "in MRSETS record"), text_pos (text));
690 number = text_tokenize (text, ' ');
691 if (!strcmp (number, "11"))
692 label_from_var_label = true;
693 else if (strcmp (number, "1"))
694 sys_warn (r, _("Unexpected label source value \"%s\" "
695 "following 'E' at offset %zu in MRSETS record"),
696 number, text_pos (text));
701 sys_warn (r, "missing 'C', 'D', or 'E' at offset %zu "
702 "in mrsets record", text_pos (text));
706 if (type == MRSET_MD)
708 counted = text_parse_counted_string (text);
713 label = text_parse_counted_string (text);
717 variables = text_tokenize (text, '\n');
718 if (variables == NULL)
720 sys_warn (r, "missing variable names following label "
721 "at offset %zu in mrsets record", text_pos (text));
725 printf ("\t\"%s\": multiple %s set",
726 name, type == MRSET_MC ? "category" : "dichotomy");
728 printf (", counted value \"%s\"", counted);
729 if (cat_label_from_counted_values)
730 printf (", category labels from counted values");
731 if (label[0] != '\0')
732 printf (", label \"%s\"", label);
733 if (label_from_var_label)
734 printf (", label from variable label");
735 printf(", variables \"%s\"\n", variables);
737 close_text_record (text);
740 /* Read record type 7, subtype 11. */
742 read_display_parameters (struct sfm_reader *r, size_t size, size_t count)
748 printf ("%08lx: variable display parameters\n", ftell (r->file));
751 sys_warn (r, _("Bad size %zu on extension 11."), size);
752 skip_bytes (r, size * count);
756 n_vars = r->n_variables;
757 if (count == 3 * n_vars)
758 includes_width = true;
759 else if (count == 2 * n_vars)
760 includes_width = false;
763 sys_warn (r, _("Extension 11 has bad count %zu (for %zu variables)."),
765 skip_bytes (r, size * count);
769 for (i = 0; i < n_vars; ++i)
771 int measure = read_int (r);
772 int width = includes_width ? read_int (r) : 0;
773 int align = read_int (r);
775 printf ("\tVar #%zu: measure=%d (%s)", i, measure,
776 (measure == 1 ? "nominal"
777 : measure == 2 ? "ordinal"
778 : measure == 3 ? "scale"
781 printf (", width=%d", width);
782 printf (", align=%d (%s)\n", align,
784 : align == 1 ? "right"
785 : align == 2 ? "centre"
790 /* Reads record type 7, subtype 13, which gives the long name
791 that corresponds to each short name. */
793 read_long_var_name_map (struct sfm_reader *r, size_t size, size_t count)
795 struct text_record *text;
799 printf ("%08lx: long variable names (short => long)\n", ftell (r->file));
800 text = open_text_record (r, size * count);
801 while (read_variable_to_value_pair (text, &var, &long_name))
802 printf ("\t%s => %s\n", var, long_name);
803 close_text_record (text);
806 /* Reads record type 7, subtype 14, which gives the real length
807 of each very long string. Rearranges DICT accordingly. */
809 read_long_string_map (struct sfm_reader *r, size_t size, size_t count)
811 struct text_record *text;
815 printf ("%08lx: very long strings (variable => length)\n", ftell (r->file));
816 text = open_text_record (r, size * count);
817 while (read_variable_to_value_pair (text, &var, &length_s))
818 printf ("\t%s => %d\n", var, atoi (length_s));
819 close_text_record (text);
823 read_attributes (struct sfm_reader *r, struct text_record *text,
824 const char *variable)
831 key = text_tokenize (text, '(');
835 for (index = 1; ; index++)
837 /* Parse the value. */
838 const char *value = text_tokenize (text, '\n');
841 sys_warn (r, _("%s: Error parsing attribute value %s[%d]"),
842 variable, key, index);
845 if (strlen (value) < 2
846 || value[0] != '\'' || value[strlen (value) - 1] != '\'')
847 sys_warn (r, _("%s: Attribute value %s[%d] is not quoted: %s"),
848 variable, key, index, value);
850 printf ("\t%s: %s[%d] = \"%.*s\"\n",
851 variable, key, index, (int) strlen (value) - 2, value + 1);
853 /* Was this the last value for this attribute? */
854 if (text_match (text, ')'))
858 if (text_match (text, '/'))
863 /* Read extended number of cases record. */
865 read_ncases64 (struct sfm_reader *r, size_t size, size_t count)
867 int64_t unknown, ncases64;
871 sys_warn (r, _("Bad size %zu for extended number of cases."), size);
872 skip_bytes (r, size * count);
877 sys_warn (r, _("Bad count %zu for extended number of cases."), size);
878 skip_bytes (r, size * count);
881 unknown = read_int64 (r);
882 ncases64 = read_int64 (r);
883 printf ("%08lx: extended number of cases: "
884 "unknown=%"PRId64", ncases64=%"PRId64"\n",
885 ftell (r->file), unknown, ncases64);
889 read_datafile_attributes (struct sfm_reader *r, size_t size, size_t count)
891 struct text_record *text;
893 printf ("%08lx: datafile attributes\n", ftell (r->file));
894 text = open_text_record (r, size * count);
895 read_attributes (r, text, "datafile");
896 close_text_record (text);
900 read_character_encoding (struct sfm_reader *r, size_t size, size_t count)
902 const unsigned long int posn = ftell (r->file);
903 char *encoding = xcalloc (size, count + 1);
904 read_string (r, encoding, count + 1);
906 printf ("%08lx: Character Encoding: %s\n", posn, encoding);
910 read_long_string_value_labels (struct sfm_reader *r, size_t size, size_t count)
912 const long start = ftell (r->file);
914 printf ("%08lx: long string value labels\n", start);
915 while (ftell (r->file) - start < size * count)
917 long posn = ftell (r->file);
918 char var_name[VAR_NAME_LEN + 1];
924 /* Read variable name. */
925 var_name_len = read_int (r);
926 if (var_name_len > VAR_NAME_LEN)
927 sys_error (r, _("Variable name length in long string value label "
928 "record (%d) exceeds %d-byte limit."),
929 var_name_len, VAR_NAME_LEN);
930 read_string (r, var_name, var_name_len + 1);
932 /* Read width, number of values. */
933 width = read_int (r);
934 n_values = read_int (r);
936 printf ("\t%08lx: %s, width %d, %d values\n",
937 posn, var_name, width, n_values);
940 for (i = 0; i < n_values; i++)
948 posn = ftell (r->file);
951 value_length = read_int (r);
952 value = xmalloc (value_length + 1);
953 read_string (r, value, value_length + 1);
956 label_length = read_int (r);
957 label = xmalloc (label_length + 1);
958 read_string (r, label, label_length + 1);
960 printf ("\t\t%08lx: \"%s\" (%d bytes) => \"%s\" (%d bytes)\n",
961 posn, value, value_length, label, label_length);
970 hex_dump (size_t offset, const void *buffer_, size_t buffer_size)
972 const uint8_t *buffer = buffer_;
974 while (buffer_size > 0)
976 size_t n = MIN (buffer_size, 16);
979 printf ("%04zx", offset);
980 for (i = 0; i < 16; i++)
983 printf ("%c%02x", i == 8 ? '-' : ' ', buffer[i]);
989 for (i = 0; i < 16; i++)
991 unsigned char c = i < n ? buffer[i] : ' ';
992 putchar (isprint (c) ? c : '.');
1002 /* Reads and prints any type 7 record that we don't understand. */
1004 read_unknown_extension (struct sfm_reader *r, size_t size, size_t count)
1006 unsigned char *buffer;
1009 if (size == 0 || count > 65536 / size)
1010 skip_bytes (r, size * count);
1013 buffer = xmalloc (size);
1014 for (i = 0; i < count; i++)
1016 read_bytes (r, buffer, size);
1017 hex_dump (i * size, buffer, size);
1023 buffer = xmalloc (count);
1024 read_bytes (r, buffer, count);
1025 if (memchr (buffer, 0, count) == 0)
1026 for (i = 0; i < count; i++)
1028 unsigned char c = buffer[i];
1032 else if (c == '\n' || isprint (c))
1035 printf ("\\%02x", c);
1038 hex_dump (0, buffer, count);
1044 read_variable_attributes (struct sfm_reader *r, size_t size, size_t count)
1046 struct text_record *text;
1048 printf ("%08lx: variable attributes\n", ftell (r->file));
1049 text = open_text_record (r, size * count);
1052 const char *variable = text_tokenize (text, ':');
1053 if (variable == NULL || !read_attributes (r, text, variable))
1056 close_text_record (text);
1060 read_compressed_data (struct sfm_reader *r)
1062 enum { N_OPCODES = 8 };
1063 uint8_t opcodes[N_OPCODES];
1064 long int opcode_ofs;
1070 printf ("\n%08lx: compressed data:\n", ftell (r->file));
1072 opcode_idx = N_OPCODES;
1074 for (case_num = 0; ; case_num++)
1076 printf ("%08lx: case %d's uncompressible data begins\n",
1077 ftell (r->file), case_num);
1078 for (i = 0; i < r->n_var_widths; i++)
1080 int width = r->var_widths[i];
1084 if (opcode_idx >= N_OPCODES)
1086 opcode_ofs = ftell (r->file);
1087 read_bytes (r, opcodes, 8);
1090 opcode = opcodes[opcode_idx];
1091 printf ("%08lx: variable %d: opcode %d: ",
1092 opcode_ofs + opcode_idx, i, opcode);
1097 printf ("%g", opcode - r->bias);
1099 printf (", but this is a string variable (width=%d)", width);
1104 printf ("end of data\n");
1108 read_bytes (r, raw_value, 8);
1109 printf ("uncompressible data: ");
1110 print_untyped_value (r, raw_value);
1117 printf (", but this is a numeric variable");
1124 printf (", but this is a string variable (width=%d)", width);
1135 /* Helpers for reading records that consist of structured text
1141 struct sfm_reader *reader; /* Reader. */
1142 char *buffer; /* Record contents. */
1143 size_t size; /* Size of buffer. */
1144 size_t pos; /* Current position in buffer. */
1147 /* Reads SIZE bytes into a text record for R,
1148 and returns the new text record. */
1149 static struct text_record *
1150 open_text_record (struct sfm_reader *r, size_t size)
1152 struct text_record *text = xmalloc (sizeof *text);
1153 char *buffer = xmalloc (size + 1);
1154 read_bytes (r, buffer, size);
1155 buffer[size] = '\0';
1157 text->buffer = buffer;
1163 /* Closes TEXT and frees its storage.
1164 Not really needed, because the pool will free the text record anyway,
1165 but can be used to free it earlier. */
1167 close_text_record (struct text_record *text)
1169 free (text->buffer);
1174 text_tokenize (struct text_record *text, int delimiter)
1176 size_t start = text->pos;
1177 while (text->pos < text->size
1178 && text->buffer[text->pos] != delimiter
1179 && text->buffer[text->pos] != '\0')
1181 if (text->pos == text->size)
1183 text->buffer[text->pos++] = '\0';
1184 return &text->buffer[start];
1188 text_match (struct text_record *text, int c)
1190 if (text->pos < text->size && text->buffer[text->pos] == c)
1199 /* Reads a integer value expressed in decimal, then a space, then a string that
1200 consists of exactly as many bytes as specified by the integer, then a space,
1201 from TEXT. Returns the string, null-terminated, as a subset of TEXT's
1202 buffer (so the caller should not free the string). */
1204 text_parse_counted_string (struct text_record *text)
1212 while (isdigit ((unsigned char) text->buffer[text->pos]))
1213 n = (n * 10) + (text->buffer[text->pos++] - '0');
1214 if (start == text->pos)
1216 sys_error (text->reader, "expecting digit at offset %zu in record",
1221 if (!text_match (text, ' '))
1223 sys_error (text->reader, "expecting space at offset %zu in record",
1228 if (text->pos + n > text->size)
1230 sys_error (text->reader, "%zu-byte string starting at offset %zu "
1231 "exceeds record length %zu", n, text->pos, text->size);
1235 s = &text->buffer[text->pos];
1238 sys_error (text->reader, "expecting space at offset %zu following "
1239 "%zu-byte string", text->pos + n, n);
1247 /* Reads a variable=value pair from TEXT.
1248 Looks up the variable in DICT and stores it into *VAR.
1249 Stores a null-terminated value into *VALUE. */
1251 read_variable_to_value_pair (struct text_record *text,
1252 char **key, char **value)
1254 *key = text_tokenize (text, '=');
1255 *value = text_tokenize (text, '\t');
1256 if (!*key || !*value)
1259 while (text->pos < text->size
1260 && (text->buffer[text->pos] == '\t'
1261 || text->buffer[text->pos] == '\0'))
1266 /* Returns the current byte offset inside the TEXT's string. */
1268 text_pos (const struct text_record *text)
1274 usage (int exit_code)
1276 printf ("usage: %s SYSFILE...\n"
1277 "where each SYSFILE is the name of a system file\n",
1282 /* Displays a corruption message. */
1284 sys_msg (struct sfm_reader *r, const char *format, va_list args)
1286 printf ("\"%s\" near offset 0x%lx: ",
1287 r->file_name, (unsigned long) ftell (r->file));
1288 vprintf (format, args);
1292 /* Displays a warning for the current file position. */
1294 sys_warn (struct sfm_reader *r, const char *format, ...)
1298 va_start (args, format);
1299 sys_msg (r, format, args);
1303 /* Displays an error for the current file position,
1304 marks it as in an error state,
1305 and aborts reading it using longjmp. */
1307 sys_error (struct sfm_reader *r, const char *format, ...)
1311 va_start (args, format);
1312 sys_msg (r, format, args);
1315 exit (EXIT_FAILURE);
1318 /* Reads BYTE_CNT bytes into BUF.
1319 Returns true if exactly BYTE_CNT bytes are successfully read.
1320 Aborts if an I/O error or a partial read occurs.
1321 If EOF_IS_OK, then an immediate end-of-file causes false to be
1322 returned; otherwise, immediate end-of-file causes an abort
1325 read_bytes_internal (struct sfm_reader *r, bool eof_is_ok,
1326 void *buf, size_t byte_cnt)
1328 size_t bytes_read = fread (buf, 1, byte_cnt, r->file);
1329 if (bytes_read == byte_cnt)
1331 else if (ferror (r->file))
1332 sys_error (r, _("System error: %s."), strerror (errno));
1333 else if (!eof_is_ok || bytes_read != 0)
1334 sys_error (r, _("Unexpected end of file."));
1339 /* Reads BYTE_CNT into BUF.
1340 Aborts upon I/O error or if end-of-file is encountered. */
1342 read_bytes (struct sfm_reader *r, void *buf, size_t byte_cnt)
1344 read_bytes_internal (r, false, buf, byte_cnt);
1347 /* Reads a 32-bit signed integer from R and returns its value in
1350 read_int (struct sfm_reader *r)
1353 read_bytes (r, integer, sizeof integer);
1354 return integer_get (r->integer_format, integer, sizeof integer);
1357 /* Reads a 64-bit signed integer from R and returns its value in
1360 read_int64 (struct sfm_reader *r)
1363 read_bytes (r, integer, sizeof integer);
1364 return integer_get (r->integer_format, integer, sizeof integer);
1367 /* Reads a 64-bit floating-point number from R and returns its
1368 value in host format. */
1370 read_float (struct sfm_reader *r)
1373 read_bytes (r, number, sizeof number);
1374 return float_get_double (r->float_format, number);
1377 /* Reads exactly SIZE - 1 bytes into BUFFER
1378 and stores a null byte into BUFFER[SIZE - 1]. */
1380 read_string (struct sfm_reader *r, char *buffer, size_t size)
1383 read_bytes (r, buffer, size - 1);
1384 buffer[size - 1] = '\0';
1387 /* Skips BYTES bytes forward in R. */
1389 skip_bytes (struct sfm_reader *r, size_t bytes)
1394 size_t chunk = MIN (sizeof buffer, bytes);
1395 read_bytes (r, buffer, chunk);
1401 trim_spaces (char *s)
1403 char *end = strchr (s, '\0');
1404 while (end > s && end[-1] == ' ')