14 all_ascii(const uint8_t *p, size_t n)
16 for (size_t i = 0; i < n; i++)
17 if (p[i] < 32 || p[i] > 126)
23 try_find(const char *target, size_t target_len)
25 const uint8_t *pos = (const uint8_t *) memmem (data, n, target, target_len);
26 return pos ? pos - data : 0;
30 try_find_tail(const char *target, size_t target_len)
32 size_t pos = try_find(target, target_len);
33 return pos ? pos + target_len : 0;
37 find(const char *target, size_t target_len)
39 size_t pos = try_find(target, target_len);
42 fprintf (stderr, "not found\n");
49 find_tail(const char *target, size_t target_len)
51 size_t pos = try_find_tail(target, target_len);
54 fprintf (stderr, "not found\n");
63 #define STR(x) XSTR(x)
64 #define WHERE __FILE__":" STR(__LINE__)
70 memcpy(&x, &data[pos], 4);
79 memcpy(&x, &data[pos], 8);
94 match_u32_assert(uint32_t x, const char *where)
96 unsigned int y = get_u32();
99 fprintf(stderr, "%s: 0x%x: expected i%u, got i%u\n", where, pos - 4, x, y);
103 #define match_u32_assert(x) match_u32_assert(x, WHERE)
106 match_byte(uint8_t b)
108 if (pos < n && data[pos] == b)
118 match_byte_assert(uint8_t b, const char *where)
122 fprintf(stderr, "%s: 0x%x: expected %02x, got %02x\n", where, pos, b, data[pos]);
126 #define match_byte_assert(b) match_byte_assert(b, WHERE)
129 dump_raw(FILE *stream, int start, int end, const char *separator)
131 for (size_t i = start; i < end; )
138 && i + 4 + data[i] + data[i + 1] * 256 <= end
139 && all_ascii(&data[i + 4], data[i] + data[i + 1] * 256))
141 fprintf(stream, "%s\"", separator);
142 fwrite(&data[i + 4], 1, data[i] + data[i + 1] * 256, stream);
143 fputs("\" ", stream);
145 i += 4 + data[i] + data[i + 1] * 256;
147 else if (i + 12 <= end
154 memcpy (&d, &data[i + 4], 8);
155 fprintf (stream, "F40.%d(%.*f)%s", data[i], data[i], d, separator);
158 else if (i + 12 <= end
165 memcpy (&d, &data[i + 4], 8);
166 fprintf (stream, "PCT40.%d(%.*f)%s", data[i], data[i], d, separator);
169 else if (i + 4 <= end
170 && (data[i] && data[i] != 88 && data[i] != 0x41)
175 fprintf (stream, "i%d ", data[i]);
180 fprintf(stream, "%02x ", data[i]);
189 get_string(const char *where)
192 /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
193 /*&& all_ascii(&data[pos + 4], data[pos])*/)
195 int len = data[pos] + data[pos + 1] * 256;
196 char *s = malloc(len + 1);
198 memcpy(s, &data[pos + 4], len);
205 fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
209 #define get_string() get_string(WHERE)
212 dump_nested_string(void)
216 match_byte_assert (0);
217 match_byte_assert (0);
218 int outer_end = pos + get_u32();
219 int inner_end = pos + get_u32();
220 if (pos != inner_end)
223 if (match_byte(0x31))
226 match_byte_assert(0x58);
227 if (pos != inner_end)
229 fprintf(stderr, "inner end discrepancy\n");
233 match_byte_assert(0x58);
234 match_byte_assert(0x58);
235 if (pos != outer_end)
237 fprintf(stderr, "outer end discrepancy\n");
245 dump_value_31(FILE *stream)
247 if (match_byte (0x31))
253 /* Only "a" observed as a sample value (although it appears 44 times in the corpus). */
257 match_u32_assert (0);
259 int outer_end = pos + get_u32();
260 int inner_end = pos + get_u32();
262 if (match_byte(0x31))
264 /* Appears to be a template string, e.g. '^1 cells (^2) expf < 5. Min exp = ^3...'.
265 Probably doesn't actually appear in output because many examples look unpolished,
266 e.g. 'partial list cases value ^1 shown upper...' */
270 match_byte_assert(0x58);
271 if (pos != inner_end)
273 fprintf(stderr, "inner end discrepancy\n");
277 if (match_byte(0x31))
279 /* Only one example in the corpus. */
283 match_byte_assert(1);
284 get_string(); /* foreground */
285 get_string(); /* background */
286 get_string(); /* font */
287 match_byte_assert(12); /* size? */
290 match_byte_assert(0x58);
291 match_byte_assert(0x58);
292 if (pos != outer_end)
294 fprintf(stderr, "outer end discrepancy\n");
298 else if (match_u32 (1))
300 fprintf(stream, "(footnote %d) ", get_u32());
301 dump_nested_string();
303 else if (match_u32 (2))
305 fprintf(stream, "(special 2)");
306 match_byte_assert(0);
307 match_byte_assert(0);
310 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
315 fprintf(stream, "(special 3)");
316 match_byte_assert(0);
317 match_byte_assert(0);
318 match_byte_assert(1);
319 match_byte_assert(0);
321 dump_nested_string(); /* Our corpus doesn't contain any examples with strings though. */
325 match_byte_assert (0x58);
329 format_to_string (int type)
335 case 2: return "AHEX";
336 case 3: return "COMMA";
337 case 4: return "DOLLAR";
338 case 5: case 40: return "F";
340 case 7: return "PIBHEX";
342 case 9: return "PIB";
343 case 10: return "PK";
344 case 11: return "RB";
345 case 12: return "RBHEX";
349 case 20: return "DATE";
350 case 21: return "TIME";
351 case 22: return "DATETIME";
352 case 23: return "ADATE";
353 case 24: return "JDATE";
354 case 25: return "DTIME";
355 case 26: return "WKDAY";
356 case 27: return "MONTH";
357 case 28: return "MOYR";
358 case 29: return "QYR";
359 case 30: return "WKYR";
360 case 31: return "PCT";
361 case 32: return "DOT";
362 case 33: return "CCA";
363 case 34: return "CCB";
364 case 35: return "CCC";
365 case 36: return "CCD";
366 case 37: return "CCE";
367 case 38: return "EDATE";
368 case 39: return "SDATE";
371 sprintf(tmp, "<%d>", type);
377 dump_value__(FILE *stream, int level, bool match1)
384 for (int i = 0; i <= level; i++)
385 fprintf (stream, " ");
389 char *text = get_string();
390 dump_value_31(stream);
391 char *identifier = get_string();
392 char *text_eng = get_string();
393 fprintf (stream, "<string c=\"%s\"", text_eng);
395 fprintf (stream, " identifier=\"%s\"", identifier);
396 if (strcmp(text_eng, text))
397 fprintf (stream, " local=\"%s\"", text);
398 fprintf (stream, "/>\n");
400 match_byte_assert(1);
404 else if (match_byte (5))
406 dump_value_31(stream);
407 char *name = get_string ();
408 char *label = get_string ();
409 fprintf (stream, "<variable name=\"%s\"", name);
411 fprintf (stream, " label=\"%s\"", label);
412 fprintf (stream, "/>\n");
413 if (!match_byte(1) && !match_byte(2))
414 match_byte_assert(3);
416 else if (match_byte (2))
422 match_byte_assert (0x58);
424 value = get_double ();
426 vallab = get_string ();
427 fprintf (stream, "<numeric-datum value=\"%.*g\" format=\"%s%d.%d\"",
428 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
430 fprintf (stream, " variable=\"%s\"", var);
432 fprintf (stream, " label=\"%s\"/>\n", vallab);
433 fprintf (stream, "/>\n");
434 if (!match_byte (1) && !match_byte(2))
435 match_byte_assert (3);
437 else if (match_byte (4))
440 char *var, *vallab, *value;
442 match_byte_assert (0x58);
444 vallab = get_string ();
446 if (!match_byte(1) && !match_byte(2))
447 match_byte_assert (3);
448 value = get_string ();
449 fprintf (stream, "<string-datum value=\"%s\" format=\"%s%d.%d\"",
450 value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
452 fprintf (stream, " variable=\"%s\"", var);
454 fprintf (stream, " label=\"%s\"/>\n", vallab);
455 fprintf (stream, "/>\n");
457 else if (match_byte (1))
462 dump_value_31(stream);
464 value = get_double ();
465 fprintf (stream, "<number value=\"%.*g\" format=\"%s%d.%d\"/>\n",
466 DBL_DIG, value, format_to_string(format >> 16), (format >> 8) & 0xff, format & 0xff);
472 dump_value_31(stream);
474 char *base = get_string();
476 fprintf (stream, "<template format=\"%s\">\n", base);
477 for (int i = 0; i < x; i++)
484 for (int j = 0; j <= level + 1; j++)
485 fprintf (stream, " ");
486 fprintf (stream, "<substitution index=\"%d\">\n", i + 1);
487 for (int j = 0; j < y; j++)
488 dump_value__ (stream, level + 2, false);
489 for (int j = 0; j <= level + 1; j++)
490 fprintf (stream, " ");
491 fprintf (stream, "</substitution>\n");
493 for (int j = 0; j <= level; j++)
494 fprintf (stream, " ");
495 fprintf (stream, "</template>\n");
500 compare_int(const void *a_, const void *b_)
504 return *a < *b ? -1 : *a > *b;
508 check_permutation(int *a, int n, const char *name)
511 memcpy(b, a, n * sizeof *a);
512 qsort(b, n, sizeof *b, compare_int);
513 for (int i = 0; i < n; i++)
516 fprintf(stderr, "bad %s permutation:", name);
517 for (int i = 0; i < n; i++)
518 fprintf(stderr, " %d", a[i]);
525 dump_category(int level, int *indexes, int *n_indexes)
527 dump_value__ (stdout, level, true);
534 else if (match_byte (1))
538 match_u32_assert (1);
541 else if (!match_u32(2))
542 match_u32_assert (0);
544 int indx = get_u32();
545 int n_categories = get_u32();
548 if (n_categories != 0)
550 fprintf(stderr, "index not -1 but subcategories\n");
553 indexes[(*n_indexes)++] = indx;
555 if (n_categories > 0)
556 printf (", %d subcategories:", n_categories);
558 printf (", index %d", indx);
560 for (int i = 0; i < n_categories; i++)
561 dump_category (level + 1, indexes, n_indexes);
568 printf("next dim\n");
569 dump_value__ (stdout, 0, false);
571 /* This byte is usually 0x02 but 0x00 and 0x75 (!) have also been spotted. */
574 if (!match_byte(0) && !match_byte(1))
575 match_byte_assert(2);
579 match_byte_assert(1);
581 match_byte_assert(1);
582 match_byte_assert(1);
583 static int dim_indx = 0;
584 match_u32_assert(dim_indx++);
585 n_categories = get_u32();
586 printf("%d nested categories\n", n_categories);
590 for (int i = 0; i < n_categories; i++)
591 dump_category (0, indexes, &n_indexes);
592 check_permutation(indexes, n_indexes, "categories");
600 printf ("%u dimensions\n", n_dims);
601 for (int i = 0; i < n_dims; i++)
611 /* The first three numbers add to the number of dimensions. */
614 match_u32_assert(n_dims - t);
616 /* The next n_dims numbers are a permutation of the dimension numbers. */
618 for (int i = 0; i < n_dims; i++)
620 check_permutation(a, n_dims, "dimensions");
623 printf ("%d data values, starting at %08x\n", x, pos);
624 for (int i = 0; i < x; i++)
626 printf("%08x, index %d:\n", pos, get_u32());
628 dump_value__(stdout, 0, false);
638 dump_value__(stdout, 0, true); putchar('\n');
639 printf("subtype:\n");
640 dump_value__(stdout, 0, true); putchar('\n');
641 match_byte_assert(0x31);
642 printf("text_eng:\n");
643 dump_value__(stdout, 0, true); putchar('\n');
645 match_byte_assert(0x58);
646 if (match_byte(0x31))
648 printf("caption:\n");
649 dump_value__(stdout, 0, false); putchar('\n');
652 match_byte_assert(0x58);
655 int n_footnotes = get_u32();
656 if (n_footnotes >= 20)
658 fprintf(stderr, "%08x: %d footnotes\n", pos - 4, n_footnotes);
662 printf("------\n%d footnotes\n", n_footnotes);
663 if (n_footnotes < 20)
665 for (int i = 0; i < n_footnotes; i++)
667 printf("footnote %d:\n", i);
668 dump_value__(stdout, 0, false);
669 if (match_byte (0x31))
671 /* Custom footnote marker string. */
672 match_byte_assert(3);
674 match_byte_assert(0x58);
679 match_byte_assert (0x58);
680 printf("(%d)\n", get_u32());
686 find_dimensions(void)
689 const char dimensions[] = "-,,,.\0";
690 int x = try_find_tail(dimensions, sizeof dimensions - 1);
695 const char dimensions[] = "-,,, .\0";
696 return find_tail(dimensions, sizeof dimensions - 1);
702 printf("fonts: offset=%08x\n", pos);
704 for (int i = 1; i <= 8; i++)
706 printf("%08x: font %d, ", pos, i);
707 match_byte_assert(i);
708 match_byte_assert(0x31);
709 printf("%s, ", get_string());
710 match_byte_assert(0);
711 match_byte_assert(0);
712 if (!match_byte(0x40) && !match_byte(0x20) && !match_byte(0x80) && !match_byte(0x10))
713 match_byte_assert(0x50);
714 if (!match_byte(0x41))
715 match_byte_assert(0x51);
718 match_byte_assert(0);
720 /* OK, this seems really unlikely to be totally correct, but it matches my corpus... */
721 if (!match_u32(0) && !match_u32(2))
722 match_u32_assert(0xfaad);
724 if (!match_u32(0) && !match_u32(1) && !match_u32(2))
726 printf ("%s, ", get_string());
727 printf ("%s, ", get_string());
730 match_byte_assert(0);
732 /* These seem unlikely to be correct too. */
737 match_u32_assert(11);
743 if (!match_u32(-1) && !match_u32(8))
744 match_u32_assert(24);
745 if (!match_u32(-1) && !match_u32(2))
749 /* Who knows? Ranges from -1 to 8 with no obvious pattern. */
753 match_u32_assert(240);
756 match_u32_assert(18);
763 match_u32_assert(142);
767 int count = get_u32();
770 char *encoding = get_string();
771 printf("encoding=%s\n", encoding);
774 match_u32_assert(UINT32_MAX);
776 match_byte_assert(1);
777 match_byte_assert(0);
779 match_byte_assert(1);
780 if (!match_byte(0x99) && !match_byte(0x98))
781 match_byte_assert(0x97);
782 match_byte_assert(7);
783 match_byte_assert(0);
784 match_byte_assert(0);
786 match_byte_assert(',');
789 match_byte_assert(',');
790 if (!match_byte('.'))
791 match_byte_assert(' ');
794 for (int i = 0; i < 5; i++)
797 if (pos != find_dimensions())
798 fprintf (stderr, "%08x / %08x\n", pos, find_dimensions());
802 main(int argc, char *argv[])
807 if (isatty(STDIN_FILENO))
809 fprintf(stderr, "redirect stdin from a .bin file\n");
812 if (fstat(STDIN_FILENO, &s))
824 if (read(STDIN_FILENO, data, n) != n)
832 if (!strcmp(argv[1], "title0"))
835 if (match_byte (0x03)
836 || (match_byte (0x05) && match_byte (0x58)))
837 printf ("%s\n", get_string());
839 printf ("<unknown>\n");
842 else if (!strcmp(argv[1], "title"))
847 else if (!strcmp(argv[1], "titleraw"))
849 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
851 n = find(fonts, sizeof fonts - 1);
853 else if (!strcmp(argv[1], "fonts"))
855 const char fonts[] = "\x01\x31\x09\0\0\0SansSerif";
856 const char styles[] = "\xf0\0\0\0";
857 start = find(fonts, sizeof fonts - 1);
858 n = find(styles, sizeof styles - 1);
860 else if (!strcmp(argv[1], "styles"))
862 const char styles[] = "\xf0\0\0\0";
863 const char dimensions[] = "-,,,.\0";
864 start = find(styles, sizeof styles - 1);
865 n = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
867 else if (!strcmp(argv[1], "dimensions") || !strcmp(argv[1], "all"))
870 match_byte_assert(1);
871 match_byte_assert(0);
873 match_byte_assert(1);
875 match_byte_assert(1);
876 match_byte_assert(0);
877 match_byte_assert(0);
879 match_byte_assert(1);
881 match_byte_assert(0);
882 match_byte_assert(0);
883 match_byte_assert(0);
887 printf("\n\ndata:\n");
892 fprintf (stderr, "%x / %x\n", pos, n);
899 fprintf (stderr, "unknown section %s\n", argv[1]);
906 dump_raw(stdout, start, n, "\n");