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 ("%08llx: end-of-dictionary record "
169 "(first byte of data at %08llx)\n",
170 (long long int) ftello (r.file),
171 (long long int) ftello (r.file) + 4);
174 read_compressed_data (&r);
183 read_header (struct sfm_reader *r)
186 char eye_catcher[61];
187 uint8_t raw_layout_code[4];
189 int32_t nominal_case_size;
191 int32_t weight_index;
194 char creation_date[10];
195 char creation_time[9];
198 read_string (r, rec_type, sizeof rec_type);
199 read_string (r, eye_catcher, sizeof eye_catcher);
201 if (strcmp ("$FL2", rec_type) != 0)
202 sys_error (r, _("This is not an SPSS system file."));
204 /* Identify integer format. */
205 read_bytes (r, raw_layout_code, sizeof raw_layout_code);
206 if ((!integer_identify (2, raw_layout_code, sizeof raw_layout_code,
208 && !integer_identify (3, raw_layout_code, sizeof raw_layout_code,
210 || (r->integer_format != INTEGER_MSB_FIRST
211 && r->integer_format != INTEGER_LSB_FIRST))
212 sys_error (r, _("This is not an SPSS system file."));
213 layout_code = integer_get (r->integer_format,
214 raw_layout_code, sizeof raw_layout_code);
216 nominal_case_size = read_int (r);
217 compressed = read_int (r);
218 weight_index = read_int (r);
219 ncases = read_int (r);
221 r->compressed = compressed != 0;
223 /* Identify floating-point format and obtain compression bias. */
224 read_bytes (r, raw_bias, sizeof raw_bias);
225 if (float_identify (100.0, raw_bias, sizeof raw_bias, &r->float_format) == 0)
227 sys_warn (r, _("Compression bias is not the usual "
228 "value of 100, or system file uses unrecognized "
229 "floating-point format."));
230 if (r->integer_format == INTEGER_MSB_FIRST)
231 r->float_format = FLOAT_IEEE_DOUBLE_BE;
233 r->float_format = FLOAT_IEEE_DOUBLE_LE;
235 r->bias = float_get_double (r->float_format, raw_bias);
237 read_string (r, creation_date, sizeof creation_date);
238 read_string (r, creation_time, sizeof creation_time);
239 read_string (r, file_label, sizeof file_label);
240 trim_spaces (file_label);
243 printf ("File header record:\n");
244 printf ("\t%17s: %s\n", "Product name", eye_catcher);
245 printf ("\t%17s: %"PRId32"\n", "Layout code", layout_code);
246 printf ("\t%17s: %"PRId32"\n", "Compressed", compressed);
247 printf ("\t%17s: %"PRId32"\n", "Weight index", weight_index);
248 printf ("\t%17s: %"PRId32"\n", "Number of cases", ncases);
249 printf ("\t%17s: %g\n", "Compression bias", r->bias);
250 printf ("\t%17s: %s\n", "Creation date", creation_date);
251 printf ("\t%17s: %s\n", "Creation time", creation_time);
252 printf ("\t%17s: \"%s\"\n", "File label", file_label);
256 format_name (int format)
258 switch ((format >> 16) & 0xff)
261 case 2: return "AHEX";
262 case 3: return "COMMA";
263 case 4: return "DOLLAR";
266 case 7: return "PIBHEX";
268 case 9: return "PIB";
269 case 10: return "PK";
270 case 11: return "RB";
271 case 12: return "RBHEX";
275 case 20: return "DATE";
276 case 21: return "TIME";
277 case 22: return "DATETIME";
278 case 23: return "ADATE";
279 case 24: return "JDATE";
280 case 25: return "DTIME";
281 case 26: return "WKDAY";
282 case 27: return "MONTH";
283 case 28: return "MOYR";
284 case 29: return "QYR";
285 case 30: return "WKYR";
286 case 31: return "PCT";
287 case 32: return "DOT";
288 case 33: return "CCA";
289 case 34: return "CCB";
290 case 35: return "CCC";
291 case 36: return "CCD";
292 case 37: return "CCE";
293 case 38: return "EDATE";
294 case 39: return "SDATE";
295 default: return "invalid";
299 /* Reads a variable (type 2) record from R and adds the
300 corresponding variable to DICT.
301 Also skips past additional variable records for long string
304 read_variable_record (struct sfm_reader *r)
307 int has_variable_label;
308 int missing_value_code;
313 printf ("%08llx: variable record #%d\n",
314 (long long int) ftello (r->file), r->n_variable_records++);
316 width = read_int (r);
317 has_variable_label = read_int (r);
318 missing_value_code = read_int (r);
319 print_format = read_int (r);
320 write_format = read_int (r);
321 read_string (r, name, sizeof name);
322 name[strcspn (name, " ")] = '\0';
327 if (r->n_var_widths >= r->allocated_var_widths)
328 r->var_widths = x2nrealloc (r->var_widths, &r->allocated_var_widths,
329 sizeof *r->var_widths);
330 r->var_widths[r->n_var_widths++] = width;
332 printf ("\tWidth: %d (%s)\n",
335 : width == 0 ? "numeric"
336 : "long string continuation record");
337 printf ("\tVariable label: %d\n", has_variable_label);
338 printf ("\tMissing values code: %d (%s)\n", missing_value_code,
339 (missing_value_code == 0 ? "no missing values"
340 : missing_value_code == 1 ? "one missing value"
341 : missing_value_code == 2 ? "two missing values"
342 : missing_value_code == 3 ? "three missing values"
343 : missing_value_code == -2 ? "one missing value range"
344 : missing_value_code == -3 ? "one missing value, one range"
346 printf ("\tPrint format: %06x (%s%d.%d)\n",
347 print_format, format_name (print_format),
348 (print_format >> 8) & 0xff, print_format & 0xff);
349 printf ("\tWrite format: %06x (%s%d.%d)\n",
350 write_format, format_name (write_format),
351 (write_format >> 8) & 0xff, write_format & 0xff);
352 printf ("\tName: %s\n", name);
354 /* Get variable label, if any. */
355 if (has_variable_label != 0 && has_variable_label != 1)
356 sys_error (r, _("Variable label indicator field is not 0 or 1."));
357 if (has_variable_label == 1)
359 long long int offset = ftello (r->file);
364 if (len >= sizeof label)
365 sys_error (r, _("Variable %s has label of invalid length %zu."),
367 read_string (r, label, len + 1);
368 printf("\t%08llx Variable label: \"%s\"\n", offset, label);
370 skip_bytes (r, ROUND_UP (len, 4) - len);
373 /* Set missing values. */
374 if (missing_value_code != 0)
378 printf ("\t%08llx Missing values:", (long long int) ftello (r->file));
381 if (missing_value_code < -3 || missing_value_code > 3
382 || missing_value_code == -1)
383 sys_error (r, _("Numeric missing value indicator field is not "
384 "-3, -2, 0, 1, 2, or 3."));
385 if (missing_value_code < 0)
387 double low = read_float (r);
388 double high = read_float (r);
389 printf (" %g...%g", low, high);
390 missing_value_code = -missing_value_code - 2;
392 for (i = 0; i < missing_value_code; i++)
393 printf (" %g", read_float (r));
397 if (missing_value_code < 1 || missing_value_code > 3)
398 sys_error (r, _("String missing value indicator field is not "
400 for (i = 0; i < missing_value_code; i++)
403 read_string (r, string, sizeof string);
404 printf (" \"%s\"", string);
412 print_untyped_value (struct sfm_reader *r, char raw_value[8])
417 value = float_get_double (r->float_format, raw_value);
418 for (n_printable = 0; n_printable < sizeof raw_value; n_printable++)
419 if (!isprint (raw_value[n_printable]))
422 printf ("%g/\"%.*s\"", value, n_printable, raw_value);
425 /* Reads value labels from sysfile R and inserts them into the
426 associated dictionary. */
428 read_value_label_record (struct sfm_reader *r)
430 int label_cnt, var_cnt;
433 printf ("%08llx: value labels record\n", (long long int) ftello (r->file));
435 /* Read number of labels. */
436 label_cnt = read_int (r);
437 for (i = 0; i < label_cnt; i++)
440 unsigned char label_len;
444 read_bytes (r, raw_value, sizeof raw_value);
446 /* Read label length. */
447 read_bytes (r, &label_len, sizeof label_len);
448 padded_len = ROUND_UP (label_len + 1, 8);
450 /* Read label, padding. */
451 read_bytes (r, label, padded_len - 1);
452 label[label_len] = 0;
455 print_untyped_value (r, raw_value);
456 printf (": \"%s\"\n", label);
459 /* Now, read the type 4 record that has the list of variables
460 to which the value labels are to be applied. */
462 /* Read record type of type 4 record. */
463 if (read_int (r) != 4)
464 sys_error (r, _("Variable index record (type 4) does not immediately "
465 "follow value label record (type 3) as it should."));
467 /* Read number of variables associated with value label from type 4
469 printf ("\t%08llx: apply to variables", (long long int) ftello (r->file));
470 var_cnt = read_int (r);
471 for (i = 0; i < var_cnt; i++)
472 printf (" #%d", read_int (r));
477 read_document_record (struct sfm_reader *r)
482 printf ("%08llx: document record\n", (long long int) ftello (r->file));
483 n_lines = read_int (r);
484 printf ("\t%d lines of documents\n", n_lines);
486 for (i = 0; i < n_lines; i++)
489 printf ("\t%08llx: ", (long long int) ftello (r->file));
490 read_string (r, line, sizeof line);
492 printf ("line %d: \"%s\"\n", i, line);
497 read_extension_record (struct sfm_reader *r)
499 long long int offset = ftello (r->file);
500 int subtype = read_int (r);
501 size_t size = read_int (r);
502 size_t count = read_int (r);
503 size_t bytes = size * count;
505 printf ("%08llx: Record 7, subtype %d, size=%zu, count=%zu\n",
506 offset, subtype, size, count);
511 read_machine_integer_info (r, size, count);
515 read_machine_float_info (r, size, count);
519 /* Variable sets information. We don't use these yet.
520 They only apply to GUIs; see VARSETS on the APPLY
521 DICTIONARY command in SPSS documentation. */
525 /* DATE variable information. We don't use it yet, but we
531 read_mrsets (r, size, count);
535 read_display_parameters (r, size, count);
539 read_long_var_name_map (r, size, count);
543 read_long_string_map (r, size, count);
547 read_ncases64 (r, size, count);
551 read_datafile_attributes (r, size, count);
555 read_variable_attributes (r, size, count);
559 read_character_encoding (r, size, count);
563 read_long_string_value_labels (r, size, count);
567 sys_warn (r, _("Unrecognized record type 7, subtype %d."), subtype);
568 read_unknown_extension (r, size, count);
572 skip_bytes (r, bytes);
576 read_machine_integer_info (struct sfm_reader *r, size_t size, size_t count)
578 long long int offset = ftello (r->file);
579 int version_major = read_int (r);
580 int version_minor = read_int (r);
581 int version_revision = read_int (r);
582 int machine_code = read_int (r);
583 int float_representation = read_int (r);
584 int compression_code = read_int (r);
585 int integer_representation = read_int (r);
586 int character_code = read_int (r);
588 printf ("%08llx: machine integer info\n", offset);
589 if (size != 4 || count != 8)
590 sys_error (r, _("Bad size (%zu) or count (%zu) field on record type 7, "
594 printf ("\tVersion: %d.%d.%d\n",
595 version_major, version_minor, version_revision);
596 printf ("\tMachine code: %d\n", machine_code);
597 printf ("\tFloating point representation: %d (%s)\n",
598 float_representation,
599 float_representation == 1 ? "IEEE 754"
600 : float_representation == 2 ? "IBM 370"
601 : float_representation == 3 ? "DEC VAX"
603 printf ("\tCompression code: %d\n", compression_code);
604 printf ("\tEndianness: %d (%s)\n", integer_representation,
605 integer_representation == 1 ? "big"
606 : integer_representation == 2 ? "little" : "unknown");
607 printf ("\tCharacter code: %d\n", character_code);
610 /* Read record type 7, subtype 4. */
612 read_machine_float_info (struct sfm_reader *r, size_t size, size_t count)
614 long long int offset = ftello (r->file);
615 double sysmis = read_float (r);
616 double highest = read_float (r);
617 double lowest = read_float (r);
619 printf ("%08llx: machine float info\n", offset);
620 if (size != 8 || count != 3)
621 sys_error (r, _("Bad size (%zu) or count (%zu) on extension 4."),
624 printf ("\tsysmis: %g\n", sysmis);
625 if (sysmis != SYSMIS)
626 sys_warn (r, _("File specifies unexpected value %g as %s."),
629 printf ("\thighest: %g\n", highest);
630 if (highest != HIGHEST)
631 sys_warn (r, _("File specifies unexpected value %g as %s."),
634 printf ("\tlowest: %g\n", lowest);
635 if (lowest != LOWEST)
636 sys_warn (r, _("File specifies unexpected value %g as %s."),
640 /* Read record type 7, subtype 7. */
642 read_mrsets (struct sfm_reader *r, size_t size, size_t count)
644 struct text_record *text;
646 printf ("%08llx: multiple response sets\n",
647 (long long int) ftello (r->file));
648 text = open_text_record (r, size * count);
652 enum { MRSET_MC, MRSET_MD } type;
653 bool cat_label_from_counted_values = false;
654 bool label_from_var_label = false;
657 const char *variables;
659 name = text_tokenize (text, '=');
663 if (text_match (text, 'C'))
667 if (!text_match (text, ' '))
669 sys_warn (r, "missing space following 'C' at offset %zu "
670 "in mrsets record", text_pos (text));
674 else if (text_match (text, 'D'))
678 else if (text_match (text, 'E'))
683 cat_label_from_counted_values = true;
685 if (!text_match (text, ' '))
687 sys_warn (r, _("Missing space following 'E' at offset %zu "
688 "in MRSETS record"), text_pos (text));
692 number = text_tokenize (text, ' ');
693 if (!strcmp (number, "11"))
694 label_from_var_label = true;
695 else if (strcmp (number, "1"))
696 sys_warn (r, _("Unexpected label source value \"%s\" "
697 "following 'E' at offset %zu in MRSETS record"),
698 number, text_pos (text));
703 sys_warn (r, "missing 'C', 'D', or 'E' at offset %zu "
704 "in mrsets record", text_pos (text));
708 if (type == MRSET_MD)
710 counted = text_parse_counted_string (text);
715 label = text_parse_counted_string (text);
719 variables = text_tokenize (text, '\n');
720 if (variables == NULL)
722 sys_warn (r, "missing variable names following label "
723 "at offset %zu in mrsets record", text_pos (text));
727 printf ("\t\"%s\": multiple %s set",
728 name, type == MRSET_MC ? "category" : "dichotomy");
730 printf (", counted value \"%s\"", counted);
731 if (cat_label_from_counted_values)
732 printf (", category labels from counted values");
733 if (label[0] != '\0')
734 printf (", label \"%s\"", label);
735 if (label_from_var_label)
736 printf (", label from variable label");
737 printf(", variables \"%s\"\n", variables);
739 close_text_record (text);
742 /* Read record type 7, subtype 11. */
744 read_display_parameters (struct sfm_reader *r, size_t size, size_t count)
750 printf ("%08llx: variable display parameters\n",
751 (long long int) ftello (r->file));
754 sys_warn (r, _("Bad size %zu on extension 11."), size);
755 skip_bytes (r, size * count);
759 n_vars = r->n_variables;
760 if (count == 3 * n_vars)
761 includes_width = true;
762 else if (count == 2 * n_vars)
763 includes_width = false;
766 sys_warn (r, _("Extension 11 has bad count %zu (for %zu variables)."),
768 skip_bytes (r, size * count);
772 for (i = 0; i < n_vars; ++i)
774 int measure = read_int (r);
775 int width = includes_width ? read_int (r) : 0;
776 int align = read_int (r);
778 printf ("\tVar #%zu: measure=%d (%s)", i, measure,
779 (measure == 1 ? "nominal"
780 : measure == 2 ? "ordinal"
781 : measure == 3 ? "scale"
784 printf (", width=%d", width);
785 printf (", align=%d (%s)\n", align,
787 : align == 1 ? "right"
788 : align == 2 ? "centre"
793 /* Reads record type 7, subtype 13, which gives the long name
794 that corresponds to each short name. */
796 read_long_var_name_map (struct sfm_reader *r, size_t size, size_t count)
798 struct text_record *text;
802 printf ("%08llx: long variable names (short => long)\n",
803 (long long int) ftello (r->file));
804 text = open_text_record (r, size * count);
805 while (read_variable_to_value_pair (text, &var, &long_name))
806 printf ("\t%s => %s\n", var, long_name);
807 close_text_record (text);
810 /* Reads record type 7, subtype 14, which gives the real length
811 of each very long string. Rearranges DICT accordingly. */
813 read_long_string_map (struct sfm_reader *r, size_t size, size_t count)
815 struct text_record *text;
819 printf ("%08llx: very long strings (variable => length)\n",
820 (long long int) ftello (r->file));
821 text = open_text_record (r, size * count);
822 while (read_variable_to_value_pair (text, &var, &length_s))
823 printf ("\t%s => %d\n", var, atoi (length_s));
824 close_text_record (text);
828 read_attributes (struct sfm_reader *r, struct text_record *text,
829 const char *variable)
836 key = text_tokenize (text, '(');
840 for (index = 1; ; index++)
842 /* Parse the value. */
843 const char *value = text_tokenize (text, '\n');
846 sys_warn (r, _("%s: Error parsing attribute value %s[%d]"),
847 variable, key, index);
850 if (strlen (value) < 2
851 || value[0] != '\'' || value[strlen (value) - 1] != '\'')
852 sys_warn (r, _("%s: Attribute value %s[%d] is not quoted: %s"),
853 variable, key, index, value);
855 printf ("\t%s: %s[%d] = \"%.*s\"\n",
856 variable, key, index, (int) strlen (value) - 2, value + 1);
858 /* Was this the last value for this attribute? */
859 if (text_match (text, ')'))
863 if (text_match (text, '/'))
868 /* Read extended number of cases record. */
870 read_ncases64 (struct sfm_reader *r, size_t size, size_t count)
872 int64_t unknown, ncases64;
876 sys_warn (r, _("Bad size %zu for extended number of cases."), size);
877 skip_bytes (r, size * count);
882 sys_warn (r, _("Bad count %zu for extended number of cases."), size);
883 skip_bytes (r, size * count);
886 unknown = read_int64 (r);
887 ncases64 = read_int64 (r);
888 printf ("%08llx: extended number of cases: "
889 "unknown=%"PRId64", ncases64=%"PRId64"\n",
890 (long long int) ftello (r->file), unknown, ncases64);
894 read_datafile_attributes (struct sfm_reader *r, size_t size, size_t count)
896 struct text_record *text;
898 printf ("%08llx: datafile attributes\n", (long long int) ftello (r->file));
899 text = open_text_record (r, size * count);
900 read_attributes (r, text, "datafile");
901 close_text_record (text);
905 read_character_encoding (struct sfm_reader *r, size_t size, size_t count)
907 long long int posn = ftello (r->file);
908 char *encoding = xcalloc (size, count + 1);
909 read_string (r, encoding, count + 1);
911 printf ("%08llx: Character Encoding: %s\n", posn, encoding);
915 read_long_string_value_labels (struct sfm_reader *r, size_t size, size_t count)
917 long long int start = ftello (r->file);
919 printf ("%08llx: long string value labels\n", start);
920 while (ftello (r->file) - start < size * count)
922 long long posn = ftello (r->file);
923 char var_name[VAR_NAME_LEN + 1];
929 /* Read variable name. */
930 var_name_len = read_int (r);
931 if (var_name_len > VAR_NAME_LEN)
932 sys_error (r, _("Variable name length in long string value label "
933 "record (%d) exceeds %d-byte limit."),
934 var_name_len, VAR_NAME_LEN);
935 read_string (r, var_name, var_name_len + 1);
937 /* Read width, number of values. */
938 width = read_int (r);
939 n_values = read_int (r);
941 printf ("\t%08llx: %s, width %d, %d values\n",
942 posn, var_name, width, n_values);
945 for (i = 0; i < n_values; i++)
953 posn = ftello (r->file);
956 value_length = read_int (r);
957 value = xmalloc (value_length + 1);
958 read_string (r, value, value_length + 1);
961 label_length = read_int (r);
962 label = xmalloc (label_length + 1);
963 read_string (r, label, label_length + 1);
965 printf ("\t\t%08llx: \"%s\" (%d bytes) => \"%s\" (%d bytes)\n",
966 posn, value, value_length, label, label_length);
975 hex_dump (size_t offset, const void *buffer_, size_t buffer_size)
977 const uint8_t *buffer = buffer_;
979 while (buffer_size > 0)
981 size_t n = MIN (buffer_size, 16);
984 printf ("%04zx", offset);
985 for (i = 0; i < 16; i++)
988 printf ("%c%02x", i == 8 ? '-' : ' ', buffer[i]);
994 for (i = 0; i < 16; i++)
996 unsigned char c = i < n ? buffer[i] : ' ';
997 putchar (isprint (c) ? c : '.');
1007 /* Reads and prints any type 7 record that we don't understand. */
1009 read_unknown_extension (struct sfm_reader *r, size_t size, size_t count)
1011 unsigned char *buffer;
1014 if (size == 0 || count > 65536 / size)
1015 skip_bytes (r, size * count);
1018 buffer = xmalloc (size);
1019 for (i = 0; i < count; i++)
1021 read_bytes (r, buffer, size);
1022 hex_dump (i * size, buffer, size);
1028 buffer = xmalloc (count);
1029 read_bytes (r, buffer, count);
1030 if (memchr (buffer, 0, count) == 0)
1031 for (i = 0; i < count; i++)
1033 unsigned char c = buffer[i];
1037 else if (c == '\n' || isprint (c))
1040 printf ("\\%02x", c);
1043 hex_dump (0, buffer, count);
1049 read_variable_attributes (struct sfm_reader *r, size_t size, size_t count)
1051 struct text_record *text;
1053 printf ("%08llx: variable attributes\n", (long long int) ftello (r->file));
1054 text = open_text_record (r, size * count);
1057 const char *variable = text_tokenize (text, ':');
1058 if (variable == NULL || !read_attributes (r, text, variable))
1061 close_text_record (text);
1065 read_compressed_data (struct sfm_reader *r)
1067 enum { N_OPCODES = 8 };
1068 uint8_t opcodes[N_OPCODES];
1069 long long int opcode_ofs;
1075 printf ("\n%08llx: compressed data:\n", (long long int) ftello (r->file));
1077 opcode_idx = N_OPCODES;
1079 for (case_num = 0; ; case_num++)
1081 printf ("%08llx: case %d's uncompressible data begins\n",
1082 (long long int) ftello (r->file), case_num);
1083 for (i = 0; i < r->n_var_widths; i++)
1085 int width = r->var_widths[i];
1089 if (opcode_idx >= N_OPCODES)
1091 opcode_ofs = ftello (r->file);
1092 read_bytes (r, opcodes, 8);
1095 opcode = opcodes[opcode_idx];
1096 printf ("%08llx: variable %d: opcode %d: ",
1097 opcode_ofs + opcode_idx, i, opcode);
1102 printf ("%g", opcode - r->bias);
1104 printf (", but this is a string variable (width=%d)", width);
1109 printf ("end of data\n");
1113 read_bytes (r, raw_value, 8);
1114 printf ("uncompressible data: ");
1115 print_untyped_value (r, raw_value);
1122 printf (", but this is a numeric variable");
1129 printf (", but this is a string variable (width=%d)", width);
1140 /* Helpers for reading records that consist of structured text
1146 struct sfm_reader *reader; /* Reader. */
1147 char *buffer; /* Record contents. */
1148 size_t size; /* Size of buffer. */
1149 size_t pos; /* Current position in buffer. */
1152 /* Reads SIZE bytes into a text record for R,
1153 and returns the new text record. */
1154 static struct text_record *
1155 open_text_record (struct sfm_reader *r, size_t size)
1157 struct text_record *text = xmalloc (sizeof *text);
1158 char *buffer = xmalloc (size + 1);
1159 read_bytes (r, buffer, size);
1160 buffer[size] = '\0';
1162 text->buffer = buffer;
1168 /* Closes TEXT and frees its storage.
1169 Not really needed, because the pool will free the text record anyway,
1170 but can be used to free it earlier. */
1172 close_text_record (struct text_record *text)
1174 free (text->buffer);
1179 text_tokenize (struct text_record *text, int delimiter)
1181 size_t start = text->pos;
1182 while (text->pos < text->size
1183 && text->buffer[text->pos] != delimiter
1184 && text->buffer[text->pos] != '\0')
1186 if (text->pos == text->size)
1188 text->buffer[text->pos++] = '\0';
1189 return &text->buffer[start];
1193 text_match (struct text_record *text, int c)
1195 if (text->pos < text->size && text->buffer[text->pos] == c)
1204 /* Reads a integer value expressed in decimal, then a space, then a string that
1205 consists of exactly as many bytes as specified by the integer, then a space,
1206 from TEXT. Returns the string, null-terminated, as a subset of TEXT's
1207 buffer (so the caller should not free the string). */
1209 text_parse_counted_string (struct text_record *text)
1217 while (isdigit ((unsigned char) text->buffer[text->pos]))
1218 n = (n * 10) + (text->buffer[text->pos++] - '0');
1219 if (start == text->pos)
1221 sys_error (text->reader, "expecting digit at offset %zu in record",
1226 if (!text_match (text, ' '))
1228 sys_error (text->reader, "expecting space at offset %zu in record",
1233 if (text->pos + n > text->size)
1235 sys_error (text->reader, "%zu-byte string starting at offset %zu "
1236 "exceeds record length %zu", n, text->pos, text->size);
1240 s = &text->buffer[text->pos];
1243 sys_error (text->reader, "expecting space at offset %zu following "
1244 "%zu-byte string", text->pos + n, n);
1252 /* Reads a variable=value pair from TEXT.
1253 Looks up the variable in DICT and stores it into *VAR.
1254 Stores a null-terminated value into *VALUE. */
1256 read_variable_to_value_pair (struct text_record *text,
1257 char **key, char **value)
1259 *key = text_tokenize (text, '=');
1260 *value = text_tokenize (text, '\t');
1261 if (!*key || !*value)
1264 while (text->pos < text->size
1265 && (text->buffer[text->pos] == '\t'
1266 || text->buffer[text->pos] == '\0'))
1271 /* Returns the current byte offset inside the TEXT's string. */
1273 text_pos (const struct text_record *text)
1279 usage (int exit_code)
1281 printf ("usage: %s SYSFILE...\n"
1282 "where each SYSFILE is the name of a system file\n",
1287 /* Displays a corruption message. */
1289 sys_msg (struct sfm_reader *r, const char *format, va_list args)
1291 printf ("\"%s\" near offset 0x%llx: ",
1292 r->file_name, (long long int) ftello (r->file));
1293 vprintf (format, args);
1297 /* Displays a warning for the current file position. */
1299 sys_warn (struct sfm_reader *r, const char *format, ...)
1303 va_start (args, format);
1304 sys_msg (r, format, args);
1308 /* Displays an error for the current file position,
1309 marks it as in an error state,
1310 and aborts reading it using longjmp. */
1312 sys_error (struct sfm_reader *r, const char *format, ...)
1316 va_start (args, format);
1317 sys_msg (r, format, args);
1320 exit (EXIT_FAILURE);
1323 /* Reads BYTE_CNT bytes into BUF.
1324 Returns true if exactly BYTE_CNT bytes are successfully read.
1325 Aborts if an I/O error or a partial read occurs.
1326 If EOF_IS_OK, then an immediate end-of-file causes false to be
1327 returned; otherwise, immediate end-of-file causes an abort
1330 read_bytes_internal (struct sfm_reader *r, bool eof_is_ok,
1331 void *buf, size_t byte_cnt)
1333 size_t bytes_read = fread (buf, 1, byte_cnt, r->file);
1334 if (bytes_read == byte_cnt)
1336 else if (ferror (r->file))
1337 sys_error (r, _("System error: %s."), strerror (errno));
1338 else if (!eof_is_ok || bytes_read != 0)
1339 sys_error (r, _("Unexpected end of file."));
1344 /* Reads BYTE_CNT into BUF.
1345 Aborts upon I/O error or if end-of-file is encountered. */
1347 read_bytes (struct sfm_reader *r, void *buf, size_t byte_cnt)
1349 read_bytes_internal (r, false, buf, byte_cnt);
1352 /* Reads a 32-bit signed integer from R and returns its value in
1355 read_int (struct sfm_reader *r)
1358 read_bytes (r, integer, sizeof integer);
1359 return integer_get (r->integer_format, integer, sizeof integer);
1362 /* Reads a 64-bit signed integer from R and returns its value in
1365 read_int64 (struct sfm_reader *r)
1368 read_bytes (r, integer, sizeof integer);
1369 return integer_get (r->integer_format, integer, sizeof integer);
1372 /* Reads a 64-bit floating-point number from R and returns its
1373 value in host format. */
1375 read_float (struct sfm_reader *r)
1378 read_bytes (r, number, sizeof number);
1379 return float_get_double (r->float_format, number);
1382 /* Reads exactly SIZE - 1 bytes into BUFFER
1383 and stores a null byte into BUFFER[SIZE - 1]. */
1385 read_string (struct sfm_reader *r, char *buffer, size_t size)
1388 read_bytes (r, buffer, size - 1);
1389 buffer[size - 1] = '\0';
1392 /* Skips BYTES bytes forward in R. */
1394 skip_bytes (struct sfm_reader *r, size_t bytes)
1399 size_t chunk = MIN (sizeof buffer, bytes);
1400 read_bytes (r, buffer, chunk);
1406 trim_spaces (char *s)
1408 char *end = strchr (s, '\0');
1409 while (end > s && end[-1] == ' ')