1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
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"
32 #include "gl/minmax.h"
33 #include "gl/progname.h"
34 #include "gl/xalloc.h"
40 const char *file_name;
43 int n_variable_records, n_variables;
46 size_t n_var_widths, allocated_var_widths;
48 enum integer_format integer_format;
49 enum float_format float_format;
55 static void read_header (struct sfm_reader *);
56 static void read_variable_record (struct sfm_reader *);
57 static void read_value_label_record (struct sfm_reader *);
58 static void read_document_record (struct sfm_reader *);
59 static void read_extension_record (struct sfm_reader *);
60 static void read_machine_integer_info (struct sfm_reader *,
61 size_t size, size_t count);
62 static void read_machine_float_info (struct sfm_reader *,
63 size_t size, size_t count);
64 static void read_mrsets (struct sfm_reader *, size_t size, size_t count);
65 static void read_display_parameters (struct sfm_reader *,
66 size_t size, size_t count);
67 static void read_long_var_name_map (struct sfm_reader *r,
68 size_t size, size_t count);
69 static void read_long_string_map (struct sfm_reader *r,
70 size_t size, size_t count);
71 static void read_datafile_attributes (struct sfm_reader *r,
72 size_t size, size_t count);
73 static void read_variable_attributes (struct sfm_reader *r,
74 size_t size, size_t count);
75 static void read_ncases64 (struct sfm_reader *, size_t size, size_t count);
76 static void read_character_encoding (struct sfm_reader *r,
77 size_t size, size_t count);
78 static void read_long_string_value_labels (struct sfm_reader *r,
79 size_t size, size_t count);
80 static void read_unknown_extension (struct sfm_reader *,
81 size_t size, size_t count);
82 static void read_compressed_data (struct sfm_reader *);
84 static struct text_record *open_text_record (
85 struct sfm_reader *, size_t size);
86 static void close_text_record (struct text_record *);
87 static bool read_variable_to_value_pair (struct text_record *,
88 char **key, char **value);
89 static char *text_tokenize (struct text_record *, int delimiter);
90 static bool text_match (struct text_record *text, int c);
91 static const char *text_parse_counted_string (struct text_record *);
92 static size_t text_pos (const struct text_record *);
94 static void usage (int exit_code);
95 static void sys_warn (struct sfm_reader *, const char *, ...)
97 static void sys_error (struct sfm_reader *, const char *, ...)
101 static void read_bytes (struct sfm_reader *, void *, size_t);
102 static bool try_read_bytes (struct sfm_reader *, void *, size_t);
103 static int read_int (struct sfm_reader *);
104 static int64_t read_int64 (struct sfm_reader *);
105 static double read_float (struct sfm_reader *);
106 static void read_string (struct sfm_reader *, char *, size_t);
107 static void skip_bytes (struct sfm_reader *, size_t);
108 static void trim_spaces (char *);
111 main (int argc, char *argv[])
116 set_program_name (argv[0]);
118 usage (EXIT_FAILURE);
120 for (i = 1; i < argc; i++)
124 r.file_name = argv[i];
125 r.file = fopen (r.file_name, "rb");
127 error (EXIT_FAILURE, errno, "error opening `%s'", r.file_name);
128 r.n_variable_records = 0;
131 r.allocated_var_widths = 0;
133 r.compressed = false;
136 printf ("Reading \"%s\":\n", r.file_name);
139 while ((rec_type = read_int (&r)) != 999)
144 read_variable_record (&r);
148 read_value_label_record (&r);
152 sys_error (&r, "Misplaced type 4 record.");
155 read_document_record (&r);
159 read_extension_record (&r);
163 sys_error (&r, "Unrecognized record type %d.", rec_type);
166 printf ("%08llx: end-of-dictionary record "
167 "(first byte of data at %08llx)\n",
168 (long long int) ftello (r.file),
169 (long long int) ftello (r.file) + 4);
172 read_compressed_data (&r);
181 read_header (struct sfm_reader *r)
184 char eye_catcher[61];
185 uint8_t raw_layout_code[4];
187 int32_t nominal_case_size;
189 int32_t weight_index;
192 char creation_date[10];
193 char creation_time[9];
196 read_string (r, rec_type, sizeof rec_type);
197 read_string (r, eye_catcher, sizeof eye_catcher);
199 if (strcmp ("$FL2", rec_type) != 0)
200 sys_error (r, "This is not an SPSS system file.");
202 /* Identify integer format. */
203 read_bytes (r, raw_layout_code, sizeof raw_layout_code);
204 if ((!integer_identify (2, raw_layout_code, sizeof raw_layout_code,
206 && !integer_identify (3, raw_layout_code, sizeof raw_layout_code,
208 || (r->integer_format != INTEGER_MSB_FIRST
209 && r->integer_format != INTEGER_LSB_FIRST))
210 sys_error (r, "This is not an SPSS system file.");
211 layout_code = integer_get (r->integer_format,
212 raw_layout_code, sizeof raw_layout_code);
214 nominal_case_size = read_int (r);
215 compressed = read_int (r);
216 weight_index = read_int (r);
217 ncases = read_int (r);
219 r->compressed = compressed != 0;
221 /* Identify floating-point format and obtain compression bias. */
222 read_bytes (r, raw_bias, sizeof raw_bias);
223 if (float_identify (100.0, raw_bias, sizeof raw_bias, &r->float_format) == 0)
225 sys_warn (r, "Compression bias is not the usual value of 100, or system "
226 "file uses unrecognized floating-point format.");
227 if (r->integer_format == INTEGER_MSB_FIRST)
228 r->float_format = FLOAT_IEEE_DOUBLE_BE;
230 r->float_format = FLOAT_IEEE_DOUBLE_LE;
232 r->bias = float_get_double (r->float_format, raw_bias);
234 read_string (r, creation_date, sizeof creation_date);
235 read_string (r, creation_time, sizeof creation_time);
236 read_string (r, file_label, sizeof file_label);
237 trim_spaces (file_label);
240 printf ("File header record:\n");
241 printf ("\t%17s: %s\n", "Product name", eye_catcher);
242 printf ("\t%17s: %"PRId32"\n", "Layout code", layout_code);
243 printf ("\t%17s: %"PRId32"\n", "Compressed", compressed);
244 printf ("\t%17s: %"PRId32"\n", "Weight index", weight_index);
245 printf ("\t%17s: %"PRId32"\n", "Number of cases", ncases);
246 printf ("\t%17s: %g\n", "Compression bias", r->bias);
247 printf ("\t%17s: %s\n", "Creation date", creation_date);
248 printf ("\t%17s: %s\n", "Creation time", creation_time);
249 printf ("\t%17s: \"%s\"\n", "File label", file_label);
253 format_name (int format)
255 switch ((format >> 16) & 0xff)
258 case 2: return "AHEX";
259 case 3: return "COMMA";
260 case 4: return "DOLLAR";
263 case 7: return "PIBHEX";
265 case 9: return "PIB";
266 case 10: return "PK";
267 case 11: return "RB";
268 case 12: return "RBHEX";
272 case 20: return "DATE";
273 case 21: return "TIME";
274 case 22: return "DATETIME";
275 case 23: return "ADATE";
276 case 24: return "JDATE";
277 case 25: return "DTIME";
278 case 26: return "WKDAY";
279 case 27: return "MONTH";
280 case 28: return "MOYR";
281 case 29: return "QYR";
282 case 30: return "WKYR";
283 case 31: return "PCT";
284 case 32: return "DOT";
285 case 33: return "CCA";
286 case 34: return "CCB";
287 case 35: return "CCC";
288 case 36: return "CCD";
289 case 37: return "CCE";
290 case 38: return "EDATE";
291 case 39: return "SDATE";
292 default: return "invalid";
296 /* Reads a variable (type 2) record from R and adds the
297 corresponding variable to DICT.
298 Also skips past additional variable records for long string
301 read_variable_record (struct sfm_reader *r)
304 int has_variable_label;
305 int missing_value_code;
310 printf ("%08llx: variable record #%d\n",
311 (long long int) ftello (r->file), r->n_variable_records++);
313 width = read_int (r);
314 has_variable_label = read_int (r);
315 missing_value_code = read_int (r);
316 print_format = read_int (r);
317 write_format = read_int (r);
318 read_string (r, name, sizeof name);
319 name[strcspn (name, " ")] = '\0';
324 if (r->n_var_widths >= r->allocated_var_widths)
325 r->var_widths = x2nrealloc (r->var_widths, &r->allocated_var_widths,
326 sizeof *r->var_widths);
327 r->var_widths[r->n_var_widths++] = width;
329 printf ("\tWidth: %d (%s)\n",
332 : width == 0 ? "numeric"
333 : "long string continuation record");
334 printf ("\tVariable label: %d\n", has_variable_label);
335 printf ("\tMissing values code: %d (%s)\n", missing_value_code,
336 (missing_value_code == 0 ? "no missing values"
337 : missing_value_code == 1 ? "one missing value"
338 : missing_value_code == 2 ? "two missing values"
339 : missing_value_code == 3 ? "three missing values"
340 : missing_value_code == -2 ? "one missing value range"
341 : missing_value_code == -3 ? "one missing value, one range"
343 printf ("\tPrint format: %06x (%s%d.%d)\n",
344 print_format, format_name (print_format),
345 (print_format >> 8) & 0xff, print_format & 0xff);
346 printf ("\tWrite format: %06x (%s%d.%d)\n",
347 write_format, format_name (write_format),
348 (write_format >> 8) & 0xff, write_format & 0xff);
349 printf ("\tName: %s\n", name);
351 /* Get variable label, if any. */
352 if (has_variable_label != 0 && has_variable_label != 1)
353 sys_error (r, "Variable label indicator field is not 0 or 1.");
354 if (has_variable_label == 1)
356 long long int offset = ftello (r->file);
357 size_t len, read_len;
362 /* Read up to 255 bytes of label. */
363 read_len = MIN (sizeof label - 1, len);
364 read_string (r, label, read_len + 1);
365 printf("\t%08llx Variable label: \"%s\"\n", offset, label);
367 /* Skip unread label bytes. */
368 skip_bytes (r, len - read_len);
370 /* Skip label padding up to multiple of 4 bytes. */
371 skip_bytes (r, ROUND_UP (len, 4) - len);
374 /* Set missing values. */
375 if (missing_value_code != 0)
379 printf ("\t%08llx Missing values:", (long long int) ftello (r->file));
382 if (missing_value_code < -3 || missing_value_code > 3
383 || missing_value_code == -1)
384 sys_error (r, "Numeric missing value indicator field is not "
385 "-3, -2, 0, 1, 2, or 3.");
386 if (missing_value_code < 0)
388 double low = read_float (r);
389 double high = read_float (r);
390 printf (" %g...%g", low, high);
391 missing_value_code = -missing_value_code - 2;
393 for (i = 0; i < missing_value_code; i++)
394 printf (" %g", read_float (r));
398 if (missing_value_code < 1 || missing_value_code > 3)
399 sys_error (r, "String missing value indicator field is not "
401 for (i = 0; i < missing_value_code; i++)
404 read_string (r, string, sizeof string);
405 printf (" \"%s\"", string);
413 print_untyped_value (struct sfm_reader *r, char raw_value[8])
418 value = float_get_double (r->float_format, raw_value);
419 for (n_printable = 0; n_printable < sizeof raw_value; n_printable++)
420 if (!isprint (raw_value[n_printable]))
423 printf ("%g/\"%.*s\"", value, n_printable, raw_value);
426 /* Reads value labels from sysfile R and inserts them into the
427 associated dictionary. */
429 read_value_label_record (struct sfm_reader *r)
431 int label_cnt, var_cnt;
434 printf ("%08llx: value labels record\n", (long long int) ftello (r->file));
436 /* Read number of labels. */
437 label_cnt = read_int (r);
438 for (i = 0; i < label_cnt; i++)
441 unsigned char label_len;
445 read_bytes (r, raw_value, sizeof raw_value);
447 /* Read label length. */
448 read_bytes (r, &label_len, sizeof label_len);
449 padded_len = ROUND_UP (label_len + 1, 8);
451 /* Read label, padding. */
452 read_bytes (r, label, padded_len - 1);
453 label[label_len] = 0;
456 print_untyped_value (r, raw_value);
457 printf (": \"%s\"\n", label);
460 /* Now, read the type 4 record that has the list of variables
461 to which the value labels are to be applied. */
463 /* Read record type of type 4 record. */
464 if (read_int (r) != 4)
465 sys_error (r, "Variable index record (type 4) does not immediately "
466 "follow value label record (type 3) as it should.");
468 /* Read number of variables associated with value label from type 4
470 printf ("\t%08llx: apply to variables", (long long int) ftello (r->file));
471 var_cnt = read_int (r);
472 for (i = 0; i < var_cnt; i++)
473 printf (" #%d", read_int (r));
478 read_document_record (struct sfm_reader *r)
483 printf ("%08llx: document record\n", (long long int) ftello (r->file));
484 n_lines = read_int (r);
485 printf ("\t%d lines of documents\n", n_lines);
487 for (i = 0; i < n_lines; i++)
490 printf ("\t%08llx: ", (long long int) ftello (r->file));
491 read_string (r, line, sizeof line);
493 printf ("line %d: \"%s\"\n", i, line);
498 read_extension_record (struct sfm_reader *r)
500 long long int offset = ftello (r->file);
501 int subtype = read_int (r);
502 size_t size = read_int (r);
503 size_t count = read_int (r);
504 size_t bytes = size * count;
506 printf ("%08llx: Record 7, subtype %d, size=%zu, count=%zu\n",
507 offset, subtype, size, count);
512 read_machine_integer_info (r, size, count);
516 read_machine_float_info (r, size, count);
520 /* Variable sets information. We don't use these yet.
521 They only apply to GUIs; see VARSETS on the APPLY
522 DICTIONARY command in SPSS documentation. */
526 /* DATE variable information. We don't use it yet, but we
532 read_mrsets (r, size, count);
536 read_display_parameters (r, size, count);
540 read_long_var_name_map (r, size, count);
544 read_long_string_map (r, size, count);
548 read_ncases64 (r, size, count);
552 read_datafile_attributes (r, size, count);
556 read_variable_attributes (r, size, count);
560 read_character_encoding (r, size, count);
564 read_long_string_value_labels (r, size, count);
568 sys_warn (r, "Unrecognized record type 7, subtype %d.", subtype);
569 read_unknown_extension (r, size, count);
573 skip_bytes (r, bytes);
577 read_machine_integer_info (struct sfm_reader *r, size_t size, size_t count)
579 long long int offset = ftello (r->file);
580 int version_major = read_int (r);
581 int version_minor = read_int (r);
582 int version_revision = read_int (r);
583 int machine_code = read_int (r);
584 int float_representation = read_int (r);
585 int compression_code = read_int (r);
586 int integer_representation = read_int (r);
587 int character_code = read_int (r);
589 printf ("%08llx: machine integer info\n", offset);
590 if (size != 4 || count != 8)
591 sys_error (r, "Bad size (%zu) or count (%zu) field on record type 7, "
592 "subtype 3.", size, count);
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 `%c' at offset %zu "
688 "in MRSETS record", 'E', 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[ID_MAX_LEN + 1];
929 /* Read variable name. */
930 var_name_len = read_int (r);
931 if (var_name_len > ID_MAX_LEN)
932 sys_error (r, "Variable name length in long string value label "
933 "record (%d) exceeds %d-byte limit.",
934 var_name_len, ID_MAX_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;
1080 for (case_num = 0; ; case_num++)
1082 printf ("%08llx: case %d's uncompressible data begins\n",
1083 (long long int) ftello (r->file), case_num);
1084 for (i = 0; i < r->n_var_widths; )
1086 int width = r->var_widths[i];
1090 if (opcode_idx >= N_OPCODES)
1092 opcode_ofs = ftello (r->file);
1095 if (!try_read_bytes (r, opcodes, 8))
1099 read_bytes (r, opcodes, 8);
1102 opcode = opcodes[opcode_idx];
1103 printf ("%08llx: variable %d: opcode %d: ",
1104 opcode_ofs + opcode_idx, i, opcode);
1109 printf ("%g", opcode - r->bias);
1111 printf (", but this is a string variable (width=%d)", width);
1117 printf ("ignored padding\n");
1121 printf ("end of data\n");
1125 read_bytes (r, raw_value, 8);
1126 printf ("uncompressible data: ");
1127 print_untyped_value (r, raw_value);
1135 printf (", but this is a numeric variable");
1143 printf (", but this is a string variable (width=%d)", width);
1154 /* Helpers for reading records that consist of structured text
1160 struct sfm_reader *reader; /* Reader. */
1161 char *buffer; /* Record contents. */
1162 size_t size; /* Size of buffer. */
1163 size_t pos; /* Current position in buffer. */
1166 /* Reads SIZE bytes into a text record for R,
1167 and returns the new text record. */
1168 static struct text_record *
1169 open_text_record (struct sfm_reader *r, size_t size)
1171 struct text_record *text = xmalloc (sizeof *text);
1172 char *buffer = xmalloc (size + 1);
1173 read_bytes (r, buffer, size);
1174 buffer[size] = '\0';
1176 text->buffer = buffer;
1182 /* Closes TEXT and frees its storage.
1183 Not really needed, because the pool will free the text record anyway,
1184 but can be used to free it earlier. */
1186 close_text_record (struct text_record *text)
1188 free (text->buffer);
1193 text_tokenize (struct text_record *text, int delimiter)
1195 size_t start = text->pos;
1196 while (text->pos < text->size
1197 && text->buffer[text->pos] != delimiter
1198 && text->buffer[text->pos] != '\0')
1200 if (start == text->pos)
1202 text->buffer[text->pos++] = '\0';
1203 return &text->buffer[start];
1207 text_match (struct text_record *text, int c)
1209 if (text->pos < text->size && text->buffer[text->pos] == c)
1218 /* Reads a integer value expressed in decimal, then a space, then a string that
1219 consists of exactly as many bytes as specified by the integer, then a space,
1220 from TEXT. Returns the string, null-terminated, as a subset of TEXT's
1221 buffer (so the caller should not free the string). */
1223 text_parse_counted_string (struct text_record *text)
1231 while (isdigit ((unsigned char) text->buffer[text->pos]))
1232 n = (n * 10) + (text->buffer[text->pos++] - '0');
1233 if (start == text->pos)
1235 sys_error (text->reader, "expecting digit at offset %zu in record",
1240 if (!text_match (text, ' '))
1242 sys_error (text->reader, "expecting space at offset %zu in record",
1247 if (text->pos + n > text->size)
1249 sys_error (text->reader, "%zu-byte string starting at offset %zu "
1250 "exceeds record length %zu", n, text->pos, text->size);
1254 s = &text->buffer[text->pos];
1257 sys_error (text->reader, "expecting space at offset %zu following "
1258 "%zu-byte string", text->pos + n, n);
1266 /* Reads a variable=value pair from TEXT.
1267 Looks up the variable in DICT and stores it into *VAR.
1268 Stores a null-terminated value into *VALUE. */
1270 read_variable_to_value_pair (struct text_record *text,
1271 char **key, char **value)
1273 *key = text_tokenize (text, '=');
1274 *value = text_tokenize (text, '\t');
1275 if (!*key || !*value)
1278 while (text->pos < text->size
1279 && (text->buffer[text->pos] == '\t'
1280 || text->buffer[text->pos] == '\0'))
1285 /* Returns the current byte offset inside the TEXT's string. */
1287 text_pos (const struct text_record *text)
1293 usage (int exit_code)
1295 printf ("usage: %s SYSFILE...\n"
1296 "where each SYSFILE is the name of a system file\n",
1301 /* Displays a corruption message. */
1303 sys_msg (struct sfm_reader *r, const char *format, va_list args)
1305 printf ("\"%s\" near offset 0x%llx: ",
1306 r->file_name, (long long int) ftello (r->file));
1307 vprintf (format, args);
1311 /* Displays a warning for the current file position. */
1313 sys_warn (struct sfm_reader *r, const char *format, ...)
1317 va_start (args, format);
1318 sys_msg (r, format, args);
1322 /* Displays an error for the current file position,
1323 marks it as in an error state,
1324 and aborts reading it using longjmp. */
1326 sys_error (struct sfm_reader *r, const char *format, ...)
1330 va_start (args, format);
1331 sys_msg (r, format, args);
1334 exit (EXIT_FAILURE);
1337 /* Reads BYTE_CNT bytes into BUF.
1338 Returns true if exactly BYTE_CNT bytes are successfully read.
1339 Aborts if an I/O error or a partial read occurs.
1340 If EOF_IS_OK, then an immediate end-of-file causes false to be
1341 returned; otherwise, immediate end-of-file causes an abort
1344 read_bytes_internal (struct sfm_reader *r, bool eof_is_ok,
1345 void *buf, size_t byte_cnt)
1347 size_t bytes_read = fread (buf, 1, byte_cnt, r->file);
1348 if (bytes_read == byte_cnt)
1350 else if (ferror (r->file))
1351 sys_error (r, "System error: %s.", strerror (errno));
1352 else if (!eof_is_ok || bytes_read != 0)
1353 sys_error (r, "Unexpected end of file.");
1358 /* Reads BYTE_CNT into BUF.
1359 Aborts upon I/O error or if end-of-file is encountered. */
1361 read_bytes (struct sfm_reader *r, void *buf, size_t byte_cnt)
1363 read_bytes_internal (r, false, buf, byte_cnt);
1366 /* Reads BYTE_CNT bytes into BUF.
1367 Returns true if exactly BYTE_CNT bytes are successfully read.
1368 Returns false if an immediate end-of-file is encountered.
1369 Aborts if an I/O error or a partial read occurs. */
1371 try_read_bytes (struct sfm_reader *r, void *buf, size_t byte_cnt)
1373 return read_bytes_internal (r, true, buf, byte_cnt);
1376 /* Reads a 32-bit signed integer from R and returns its value in
1379 read_int (struct sfm_reader *r)
1382 read_bytes (r, integer, sizeof integer);
1383 return integer_get (r->integer_format, integer, sizeof integer);
1386 /* Reads a 64-bit signed integer from R and returns its value in
1389 read_int64 (struct sfm_reader *r)
1392 read_bytes (r, integer, sizeof integer);
1393 return integer_get (r->integer_format, integer, sizeof integer);
1396 /* Reads a 64-bit floating-point number from R and returns its
1397 value in host format. */
1399 read_float (struct sfm_reader *r)
1402 read_bytes (r, number, sizeof number);
1403 return float_get_double (r->float_format, number);
1406 /* Reads exactly SIZE - 1 bytes into BUFFER
1407 and stores a null byte into BUFFER[SIZE - 1]. */
1409 read_string (struct sfm_reader *r, char *buffer, size_t size)
1412 read_bytes (r, buffer, size - 1);
1413 buffer[size - 1] = '\0';
1416 /* Skips BYTES bytes forward in R. */
1418 skip_bytes (struct sfm_reader *r, size_t bytes)
1423 size_t chunk = MIN (sizeof buffer, bytes);
1424 read_bytes (r, buffer, chunk);
1430 trim_spaces (char *s)
1432 char *end = strchr (s, '\0');
1433 while (end > s && end[-1] == ' ')