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);
360 size_t len, read_len;
365 /* Read up to 255 bytes of label. */
366 read_len = MIN (sizeof label - 1, len);
367 read_string (r, label, read_len + 1);
368 printf("\t%08llx Variable label: \"%s\"\n", offset, label);
370 /* Skip unread label bytes. */
371 skip_bytes (r, len - read_len);
373 /* Skip label padding up to multiple of 4 bytes. */
374 skip_bytes (r, ROUND_UP (len, 4) - len);
377 /* Set missing values. */
378 if (missing_value_code != 0)
382 printf ("\t%08llx Missing values:", (long long int) ftello (r->file));
385 if (missing_value_code < -3 || missing_value_code > 3
386 || missing_value_code == -1)
387 sys_error (r, _("Numeric missing value indicator field is not "
388 "-3, -2, 0, 1, 2, or 3."));
389 if (missing_value_code < 0)
391 double low = read_float (r);
392 double high = read_float (r);
393 printf (" %g...%g", low, high);
394 missing_value_code = -missing_value_code - 2;
396 for (i = 0; i < missing_value_code; i++)
397 printf (" %g", read_float (r));
401 if (missing_value_code < 1 || missing_value_code > 3)
402 sys_error (r, _("String missing value indicator field is not "
404 for (i = 0; i < missing_value_code; i++)
407 read_string (r, string, sizeof string);
408 printf (" \"%s\"", string);
416 print_untyped_value (struct sfm_reader *r, char raw_value[8])
421 value = float_get_double (r->float_format, raw_value);
422 for (n_printable = 0; n_printable < sizeof raw_value; n_printable++)
423 if (!isprint (raw_value[n_printable]))
426 printf ("%g/\"%.*s\"", value, n_printable, raw_value);
429 /* Reads value labels from sysfile R and inserts them into the
430 associated dictionary. */
432 read_value_label_record (struct sfm_reader *r)
434 int label_cnt, var_cnt;
437 printf ("%08llx: value labels record\n", (long long int) ftello (r->file));
439 /* Read number of labels. */
440 label_cnt = read_int (r);
441 for (i = 0; i < label_cnt; i++)
444 unsigned char label_len;
448 read_bytes (r, raw_value, sizeof raw_value);
450 /* Read label length. */
451 read_bytes (r, &label_len, sizeof label_len);
452 padded_len = ROUND_UP (label_len + 1, 8);
454 /* Read label, padding. */
455 read_bytes (r, label, padded_len - 1);
456 label[label_len] = 0;
459 print_untyped_value (r, raw_value);
460 printf (": \"%s\"\n", label);
463 /* Now, read the type 4 record that has the list of variables
464 to which the value labels are to be applied. */
466 /* Read record type of type 4 record. */
467 if (read_int (r) != 4)
468 sys_error (r, _("Variable index record (type 4) does not immediately "
469 "follow value label record (type 3) as it should."));
471 /* Read number of variables associated with value label from type 4
473 printf ("\t%08llx: apply to variables", (long long int) ftello (r->file));
474 var_cnt = read_int (r);
475 for (i = 0; i < var_cnt; i++)
476 printf (" #%d", read_int (r));
481 read_document_record (struct sfm_reader *r)
486 printf ("%08llx: document record\n", (long long int) ftello (r->file));
487 n_lines = read_int (r);
488 printf ("\t%d lines of documents\n", n_lines);
490 for (i = 0; i < n_lines; i++)
493 printf ("\t%08llx: ", (long long int) ftello (r->file));
494 read_string (r, line, sizeof line);
496 printf ("line %d: \"%s\"\n", i, line);
501 read_extension_record (struct sfm_reader *r)
503 long long int offset = ftello (r->file);
504 int subtype = read_int (r);
505 size_t size = read_int (r);
506 size_t count = read_int (r);
507 size_t bytes = size * count;
509 printf ("%08llx: Record 7, subtype %d, size=%zu, count=%zu\n",
510 offset, subtype, size, count);
515 read_machine_integer_info (r, size, count);
519 read_machine_float_info (r, size, count);
523 /* Variable sets information. We don't use these yet.
524 They only apply to GUIs; see VARSETS on the APPLY
525 DICTIONARY command in SPSS documentation. */
529 /* DATE variable information. We don't use it yet, but we
535 read_mrsets (r, size, count);
539 read_display_parameters (r, size, count);
543 read_long_var_name_map (r, size, count);
547 read_long_string_map (r, size, count);
551 read_ncases64 (r, size, count);
555 read_datafile_attributes (r, size, count);
559 read_variable_attributes (r, size, count);
563 read_character_encoding (r, size, count);
567 read_long_string_value_labels (r, size, count);
571 sys_warn (r, _("Unrecognized record type 7, subtype %d."), subtype);
572 read_unknown_extension (r, size, count);
576 skip_bytes (r, bytes);
580 read_machine_integer_info (struct sfm_reader *r, size_t size, size_t count)
582 long long int offset = ftello (r->file);
583 int version_major = read_int (r);
584 int version_minor = read_int (r);
585 int version_revision = read_int (r);
586 int machine_code = read_int (r);
587 int float_representation = read_int (r);
588 int compression_code = read_int (r);
589 int integer_representation = read_int (r);
590 int character_code = read_int (r);
592 printf ("%08llx: machine integer info\n", offset);
593 if (size != 4 || count != 8)
594 sys_error (r, _("Bad size (%zu) or count (%zu) field on record type 7, "
598 printf ("\tVersion: %d.%d.%d\n",
599 version_major, version_minor, version_revision);
600 printf ("\tMachine code: %d\n", machine_code);
601 printf ("\tFloating point representation: %d (%s)\n",
602 float_representation,
603 float_representation == 1 ? "IEEE 754"
604 : float_representation == 2 ? "IBM 370"
605 : float_representation == 3 ? "DEC VAX"
607 printf ("\tCompression code: %d\n", compression_code);
608 printf ("\tEndianness: %d (%s)\n", integer_representation,
609 integer_representation == 1 ? "big"
610 : integer_representation == 2 ? "little" : "unknown");
611 printf ("\tCharacter code: %d\n", character_code);
614 /* Read record type 7, subtype 4. */
616 read_machine_float_info (struct sfm_reader *r, size_t size, size_t count)
618 long long int offset = ftello (r->file);
619 double sysmis = read_float (r);
620 double highest = read_float (r);
621 double lowest = read_float (r);
623 printf ("%08llx: machine float info\n", offset);
624 if (size != 8 || count != 3)
625 sys_error (r, _("Bad size (%zu) or count (%zu) on extension 4."),
628 printf ("\tsysmis: %g\n", sysmis);
629 if (sysmis != SYSMIS)
630 sys_warn (r, _("File specifies unexpected value %g as %s."),
633 printf ("\thighest: %g\n", highest);
634 if (highest != HIGHEST)
635 sys_warn (r, _("File specifies unexpected value %g as %s."),
638 printf ("\tlowest: %g\n", lowest);
639 if (lowest != LOWEST)
640 sys_warn (r, _("File specifies unexpected value %g as %s."),
644 /* Read record type 7, subtype 7. */
646 read_mrsets (struct sfm_reader *r, size_t size, size_t count)
648 struct text_record *text;
650 printf ("%08llx: multiple response sets\n",
651 (long long int) ftello (r->file));
652 text = open_text_record (r, size * count);
656 enum { MRSET_MC, MRSET_MD } type;
657 bool cat_label_from_counted_values = false;
658 bool label_from_var_label = false;
661 const char *variables;
663 name = text_tokenize (text, '=');
667 if (text_match (text, 'C'))
671 if (!text_match (text, ' '))
673 sys_warn (r, "missing space following 'C' at offset %zu "
674 "in mrsets record", text_pos (text));
678 else if (text_match (text, 'D'))
682 else if (text_match (text, 'E'))
687 cat_label_from_counted_values = true;
689 if (!text_match (text, ' '))
691 sys_warn (r, _("Missing space following 'E' at offset %zu "
692 "in MRSETS record"), text_pos (text));
696 number = text_tokenize (text, ' ');
697 if (!strcmp (number, "11"))
698 label_from_var_label = true;
699 else if (strcmp (number, "1"))
700 sys_warn (r, _("Unexpected label source value \"%s\" "
701 "following 'E' at offset %zu in MRSETS record"),
702 number, text_pos (text));
707 sys_warn (r, "missing 'C', 'D', or 'E' at offset %zu "
708 "in mrsets record", text_pos (text));
712 if (type == MRSET_MD)
714 counted = text_parse_counted_string (text);
719 label = text_parse_counted_string (text);
723 variables = text_tokenize (text, '\n');
724 if (variables == NULL)
726 sys_warn (r, "missing variable names following label "
727 "at offset %zu in mrsets record", text_pos (text));
731 printf ("\t\"%s\": multiple %s set",
732 name, type == MRSET_MC ? "category" : "dichotomy");
734 printf (", counted value \"%s\"", counted);
735 if (cat_label_from_counted_values)
736 printf (", category labels from counted values");
737 if (label[0] != '\0')
738 printf (", label \"%s\"", label);
739 if (label_from_var_label)
740 printf (", label from variable label");
741 printf(", variables \"%s\"\n", variables);
743 close_text_record (text);
746 /* Read record type 7, subtype 11. */
748 read_display_parameters (struct sfm_reader *r, size_t size, size_t count)
754 printf ("%08llx: variable display parameters\n",
755 (long long int) ftello (r->file));
758 sys_warn (r, _("Bad size %zu on extension 11."), size);
759 skip_bytes (r, size * count);
763 n_vars = r->n_variables;
764 if (count == 3 * n_vars)
765 includes_width = true;
766 else if (count == 2 * n_vars)
767 includes_width = false;
770 sys_warn (r, _("Extension 11 has bad count %zu (for %zu variables)."),
772 skip_bytes (r, size * count);
776 for (i = 0; i < n_vars; ++i)
778 int measure = read_int (r);
779 int width = includes_width ? read_int (r) : 0;
780 int align = read_int (r);
782 printf ("\tVar #%zu: measure=%d (%s)", i, measure,
783 (measure == 1 ? "nominal"
784 : measure == 2 ? "ordinal"
785 : measure == 3 ? "scale"
788 printf (", width=%d", width);
789 printf (", align=%d (%s)\n", align,
791 : align == 1 ? "right"
792 : align == 2 ? "centre"
797 /* Reads record type 7, subtype 13, which gives the long name
798 that corresponds to each short name. */
800 read_long_var_name_map (struct sfm_reader *r, size_t size, size_t count)
802 struct text_record *text;
806 printf ("%08llx: long variable names (short => long)\n",
807 (long long int) ftello (r->file));
808 text = open_text_record (r, size * count);
809 while (read_variable_to_value_pair (text, &var, &long_name))
810 printf ("\t%s => %s\n", var, long_name);
811 close_text_record (text);
814 /* Reads record type 7, subtype 14, which gives the real length
815 of each very long string. Rearranges DICT accordingly. */
817 read_long_string_map (struct sfm_reader *r, size_t size, size_t count)
819 struct text_record *text;
823 printf ("%08llx: very long strings (variable => length)\n",
824 (long long int) ftello (r->file));
825 text = open_text_record (r, size * count);
826 while (read_variable_to_value_pair (text, &var, &length_s))
827 printf ("\t%s => %d\n", var, atoi (length_s));
828 close_text_record (text);
832 read_attributes (struct sfm_reader *r, struct text_record *text,
833 const char *variable)
840 key = text_tokenize (text, '(');
844 for (index = 1; ; index++)
846 /* Parse the value. */
847 const char *value = text_tokenize (text, '\n');
850 sys_warn (r, _("%s: Error parsing attribute value %s[%d]"),
851 variable, key, index);
854 if (strlen (value) < 2
855 || value[0] != '\'' || value[strlen (value) - 1] != '\'')
856 sys_warn (r, _("%s: Attribute value %s[%d] is not quoted: %s"),
857 variable, key, index, value);
859 printf ("\t%s: %s[%d] = \"%.*s\"\n",
860 variable, key, index, (int) strlen (value) - 2, value + 1);
862 /* Was this the last value for this attribute? */
863 if (text_match (text, ')'))
867 if (text_match (text, '/'))
872 /* Read extended number of cases record. */
874 read_ncases64 (struct sfm_reader *r, size_t size, size_t count)
876 int64_t unknown, ncases64;
880 sys_warn (r, _("Bad size %zu for extended number of cases."), size);
881 skip_bytes (r, size * count);
886 sys_warn (r, _("Bad count %zu for extended number of cases."), size);
887 skip_bytes (r, size * count);
890 unknown = read_int64 (r);
891 ncases64 = read_int64 (r);
892 printf ("%08llx: extended number of cases: "
893 "unknown=%"PRId64", ncases64=%"PRId64"\n",
894 (long long int) ftello (r->file), unknown, ncases64);
898 read_datafile_attributes (struct sfm_reader *r, size_t size, size_t count)
900 struct text_record *text;
902 printf ("%08llx: datafile attributes\n", (long long int) ftello (r->file));
903 text = open_text_record (r, size * count);
904 read_attributes (r, text, "datafile");
905 close_text_record (text);
909 read_character_encoding (struct sfm_reader *r, size_t size, size_t count)
911 long long int posn = ftello (r->file);
912 char *encoding = xcalloc (size, count + 1);
913 read_string (r, encoding, count + 1);
915 printf ("%08llx: Character Encoding: %s\n", posn, encoding);
919 read_long_string_value_labels (struct sfm_reader *r, size_t size, size_t count)
921 long long int start = ftello (r->file);
923 printf ("%08llx: long string value labels\n", start);
924 while (ftello (r->file) - start < size * count)
926 long long posn = ftello (r->file);
927 char var_name[VAR_NAME_LEN + 1];
933 /* Read variable name. */
934 var_name_len = read_int (r);
935 if (var_name_len > VAR_NAME_LEN)
936 sys_error (r, _("Variable name length in long string value label "
937 "record (%d) exceeds %d-byte limit."),
938 var_name_len, VAR_NAME_LEN);
939 read_string (r, var_name, var_name_len + 1);
941 /* Read width, number of values. */
942 width = read_int (r);
943 n_values = read_int (r);
945 printf ("\t%08llx: %s, width %d, %d values\n",
946 posn, var_name, width, n_values);
949 for (i = 0; i < n_values; i++)
957 posn = ftello (r->file);
960 value_length = read_int (r);
961 value = xmalloc (value_length + 1);
962 read_string (r, value, value_length + 1);
965 label_length = read_int (r);
966 label = xmalloc (label_length + 1);
967 read_string (r, label, label_length + 1);
969 printf ("\t\t%08llx: \"%s\" (%d bytes) => \"%s\" (%d bytes)\n",
970 posn, value, value_length, label, label_length);
979 hex_dump (size_t offset, const void *buffer_, size_t buffer_size)
981 const uint8_t *buffer = buffer_;
983 while (buffer_size > 0)
985 size_t n = MIN (buffer_size, 16);
988 printf ("%04zx", offset);
989 for (i = 0; i < 16; i++)
992 printf ("%c%02x", i == 8 ? '-' : ' ', buffer[i]);
998 for (i = 0; i < 16; i++)
1000 unsigned char c = i < n ? buffer[i] : ' ';
1001 putchar (isprint (c) ? c : '.');
1011 /* Reads and prints any type 7 record that we don't understand. */
1013 read_unknown_extension (struct sfm_reader *r, size_t size, size_t count)
1015 unsigned char *buffer;
1018 if (size == 0 || count > 65536 / size)
1019 skip_bytes (r, size * count);
1022 buffer = xmalloc (size);
1023 for (i = 0; i < count; i++)
1025 read_bytes (r, buffer, size);
1026 hex_dump (i * size, buffer, size);
1032 buffer = xmalloc (count);
1033 read_bytes (r, buffer, count);
1034 if (memchr (buffer, 0, count) == 0)
1035 for (i = 0; i < count; i++)
1037 unsigned char c = buffer[i];
1041 else if (c == '\n' || isprint (c))
1044 printf ("\\%02x", c);
1047 hex_dump (0, buffer, count);
1053 read_variable_attributes (struct sfm_reader *r, size_t size, size_t count)
1055 struct text_record *text;
1057 printf ("%08llx: variable attributes\n", (long long int) ftello (r->file));
1058 text = open_text_record (r, size * count);
1061 const char *variable = text_tokenize (text, ':');
1062 if (variable == NULL || !read_attributes (r, text, variable))
1065 close_text_record (text);
1069 read_compressed_data (struct sfm_reader *r)
1071 enum { N_OPCODES = 8 };
1072 uint8_t opcodes[N_OPCODES];
1073 long long int opcode_ofs;
1079 printf ("\n%08llx: compressed data:\n", (long long int) ftello (r->file));
1081 opcode_idx = N_OPCODES;
1083 for (case_num = 0; ; case_num++)
1085 printf ("%08llx: case %d's uncompressible data begins\n",
1086 (long long int) ftello (r->file), case_num);
1087 for (i = 0; i < r->n_var_widths; )
1089 int width = r->var_widths[i];
1093 if (opcode_idx >= N_OPCODES)
1095 opcode_ofs = ftello (r->file);
1096 read_bytes (r, opcodes, 8);
1099 opcode = opcodes[opcode_idx];
1100 printf ("%08llx: variable %d: opcode %d: ",
1101 opcode_ofs + opcode_idx, i, opcode);
1106 printf ("%g", opcode - r->bias);
1108 printf (", but this is a string variable (width=%d)", width);
1114 printf ("ignored padding\n");
1118 printf ("end of data\n");
1122 read_bytes (r, raw_value, 8);
1123 printf ("uncompressible data: ");
1124 print_untyped_value (r, raw_value);
1132 printf (", but this is a numeric variable");
1140 printf (", but this is a string variable (width=%d)", width);
1151 /* Helpers for reading records that consist of structured text
1157 struct sfm_reader *reader; /* Reader. */
1158 char *buffer; /* Record contents. */
1159 size_t size; /* Size of buffer. */
1160 size_t pos; /* Current position in buffer. */
1163 /* Reads SIZE bytes into a text record for R,
1164 and returns the new text record. */
1165 static struct text_record *
1166 open_text_record (struct sfm_reader *r, size_t size)
1168 struct text_record *text = xmalloc (sizeof *text);
1169 char *buffer = xmalloc (size + 1);
1170 read_bytes (r, buffer, size);
1171 buffer[size] = '\0';
1173 text->buffer = buffer;
1179 /* Closes TEXT and frees its storage.
1180 Not really needed, because the pool will free the text record anyway,
1181 but can be used to free it earlier. */
1183 close_text_record (struct text_record *text)
1185 free (text->buffer);
1190 text_tokenize (struct text_record *text, int delimiter)
1192 size_t start = text->pos;
1193 while (text->pos < text->size
1194 && text->buffer[text->pos] != delimiter
1195 && text->buffer[text->pos] != '\0')
1197 if (text->pos == text->size)
1199 text->buffer[text->pos++] = '\0';
1200 return &text->buffer[start];
1204 text_match (struct text_record *text, int c)
1206 if (text->pos < text->size && text->buffer[text->pos] == c)
1215 /* Reads a integer value expressed in decimal, then a space, then a string that
1216 consists of exactly as many bytes as specified by the integer, then a space,
1217 from TEXT. Returns the string, null-terminated, as a subset of TEXT's
1218 buffer (so the caller should not free the string). */
1220 text_parse_counted_string (struct text_record *text)
1228 while (isdigit ((unsigned char) text->buffer[text->pos]))
1229 n = (n * 10) + (text->buffer[text->pos++] - '0');
1230 if (start == text->pos)
1232 sys_error (text->reader, "expecting digit at offset %zu in record",
1237 if (!text_match (text, ' '))
1239 sys_error (text->reader, "expecting space at offset %zu in record",
1244 if (text->pos + n > text->size)
1246 sys_error (text->reader, "%zu-byte string starting at offset %zu "
1247 "exceeds record length %zu", n, text->pos, text->size);
1251 s = &text->buffer[text->pos];
1254 sys_error (text->reader, "expecting space at offset %zu following "
1255 "%zu-byte string", text->pos + n, n);
1263 /* Reads a variable=value pair from TEXT.
1264 Looks up the variable in DICT and stores it into *VAR.
1265 Stores a null-terminated value into *VALUE. */
1267 read_variable_to_value_pair (struct text_record *text,
1268 char **key, char **value)
1270 *key = text_tokenize (text, '=');
1271 *value = text_tokenize (text, '\t');
1272 if (!*key || !*value)
1275 while (text->pos < text->size
1276 && (text->buffer[text->pos] == '\t'
1277 || text->buffer[text->pos] == '\0'))
1282 /* Returns the current byte offset inside the TEXT's string. */
1284 text_pos (const struct text_record *text)
1290 usage (int exit_code)
1292 printf ("usage: %s SYSFILE...\n"
1293 "where each SYSFILE is the name of a system file\n",
1298 /* Displays a corruption message. */
1300 sys_msg (struct sfm_reader *r, const char *format, va_list args)
1302 printf ("\"%s\" near offset 0x%llx: ",
1303 r->file_name, (long long int) ftello (r->file));
1304 vprintf (format, args);
1308 /* Displays a warning for the current file position. */
1310 sys_warn (struct sfm_reader *r, const char *format, ...)
1314 va_start (args, format);
1315 sys_msg (r, format, args);
1319 /* Displays an error for the current file position,
1320 marks it as in an error state,
1321 and aborts reading it using longjmp. */
1323 sys_error (struct sfm_reader *r, const char *format, ...)
1327 va_start (args, format);
1328 sys_msg (r, format, args);
1331 exit (EXIT_FAILURE);
1334 /* Reads BYTE_CNT bytes into BUF.
1335 Returns true if exactly BYTE_CNT bytes are successfully read.
1336 Aborts if an I/O error or a partial read occurs.
1337 If EOF_IS_OK, then an immediate end-of-file causes false to be
1338 returned; otherwise, immediate end-of-file causes an abort
1341 read_bytes_internal (struct sfm_reader *r, bool eof_is_ok,
1342 void *buf, size_t byte_cnt)
1344 size_t bytes_read = fread (buf, 1, byte_cnt, r->file);
1345 if (bytes_read == byte_cnt)
1347 else if (ferror (r->file))
1348 sys_error (r, _("System error: %s."), strerror (errno));
1349 else if (!eof_is_ok || bytes_read != 0)
1350 sys_error (r, _("Unexpected end of file."));
1355 /* Reads BYTE_CNT into BUF.
1356 Aborts upon I/O error or if end-of-file is encountered. */
1358 read_bytes (struct sfm_reader *r, void *buf, size_t byte_cnt)
1360 read_bytes_internal (r, false, buf, byte_cnt);
1363 /* Reads a 32-bit signed integer from R and returns its value in
1366 read_int (struct sfm_reader *r)
1369 read_bytes (r, integer, sizeof integer);
1370 return integer_get (r->integer_format, integer, sizeof integer);
1373 /* Reads a 64-bit signed integer from R and returns its value in
1376 read_int64 (struct sfm_reader *r)
1379 read_bytes (r, integer, sizeof integer);
1380 return integer_get (r->integer_format, integer, sizeof integer);
1383 /* Reads a 64-bit floating-point number from R and returns its
1384 value in host format. */
1386 read_float (struct sfm_reader *r)
1389 read_bytes (r, number, sizeof number);
1390 return float_get_double (r->float_format, number);
1393 /* Reads exactly SIZE - 1 bytes into BUFFER
1394 and stores a null byte into BUFFER[SIZE - 1]. */
1396 read_string (struct sfm_reader *r, char *buffer, size_t size)
1399 read_bytes (r, buffer, size - 1);
1400 buffer[size - 1] = '\0';
1403 /* Skips BYTES bytes forward in R. */
1405 skip_bytes (struct sfm_reader *r, size_t bytes)
1410 size_t chunk = MIN (sizeof buffer, bytes);
1411 read_bytes (r, buffer, chunk);
1417 trim_spaces (char *s)
1419 char *end = strchr (s, '\0');
1420 while (end > s && end[-1] == ' ')