14 #include "u8-mbtouc.h"
16 static const char *filename;
25 #define STR(x) XSTR(x)
26 #define WHERE __FILE__":" STR(__LINE__)
38 memcpy(&x, &data[pos], 4);
43 static unsigned long long int
47 memcpy(&x, &data[pos], 8);
56 x = (data[pos] << 24) | (data[pos + 1] << 16) | (data[pos + 2] << 8) | data[pos + 3];
65 memcpy(&x, &data[pos], 2);
74 memcpy(&x, &data[pos], 8);
79 static double __attribute__((unused))
83 memcpy(&x, &data[pos], 4);
107 match_u32_assert(uint32_t x, const char *where)
109 unsigned int y = get_u32();
112 fprintf(stderr, "%s: 0x%x: expected i%u, got i%u\n", where, pos - 4, x, y);
116 #define match_u32_assert(x) match_u32_assert(x, WHERE)
119 match_u16_assert(uint16_t x, const char *where)
121 unsigned int y = get_u16();
124 fprintf(stderr, "%s: 0x%x: expected u16:%u, got u16:%u\n", where, pos - 2, x, y);
128 #define match_u16_assert(x) match_u16_assert(x, WHERE)
130 static bool __attribute__((unused))
131 match_u64(uint64_t x)
139 static void __attribute__((unused))
140 match_u64_assert(uint64_t x, const char *where)
142 unsigned long long int y = get_u64();
145 fprintf(stderr, "%s: 0x%x: expected u64:%lu, got u64:%llu\n", where, pos - 8, x, y);
149 #define match_u64_assert(x) match_u64_assert(x, WHERE)
151 static bool __attribute__((unused))
152 match_be32(uint32_t x)
161 match_be32_assert(uint32_t x, const char *where)
163 unsigned int y = get_be32();
166 fprintf(stderr, "%s: 0x%x: expected be%u, got be%u\n", where, pos - 4, x, y);
170 #define match_be32_assert(x) match_be32_assert(x, WHERE)
173 match_byte(uint8_t b)
175 if (pos < n && data[pos] == b)
185 match_byte_assert(uint8_t b, const char *where)
189 fprintf(stderr, "%s: 0x%x: expected %02x, got %02x\n", where, pos, b, data[pos]);
193 #define match_byte_assert(b) match_byte_assert(b, WHERE)
196 match_bytes(int start, const int *bytes, size_t n_bytes)
198 for (size_t i = 0; i < n_bytes; i++)
199 if (bytes[i] >= 0 && data[start + i] != bytes[i])
205 xmemdup0(const void *p, size_t n)
207 char *s = malloc(n + 1);
218 match_byte_assert(1);
222 static bool __attribute__((unused))
225 return (p >= ' ' && p < 127) || p == '\r' || p == '\n' || p == '\t';
229 count_zeros(const uint8_t *p)
237 static bool __attribute__((unused))
238 all_utf8(const char *p_, size_t len)
240 const uint8_t *p = (const uint8_t *) p_;
241 for (size_t ofs = 0, mblen; ofs < len; ofs += mblen)
245 mblen = u8_mbtouc (&uc, p + ofs, len - ofs);
246 if ((uc < 32 && uc != '\n') || uc == 127 || uc == 0xfffd)
255 int len = data[pos++];
256 char *s = xmemdup0(&data[pos], len);
262 match_string1_assert(const char *exp, const char *where)
265 char *act = get_string1();
266 if (strcmp(act, exp))
268 fprintf(stderr, "%s: 0x%x: expected \"%s\", got \"%s\"\n",
269 where, start, exp, act);
273 #define match_string1_assert(x) match_string1_assert(x, WHERE)
278 int len = data[pos] + data[pos + 1] * 256;
279 char *s = xmemdup0(&data[pos + 2], len);
285 match_string2_assert(const char *exp, const char *where)
288 char *act = get_string2();
289 if (strcmp(act, exp))
291 fprintf(stderr, "%s: 0x%x: expected \"%s\", got \"%s\"\n",
292 where, start, exp, act);
296 #define match_string2_assert(x) match_string2_assert(x, WHERE)
299 get_string(const char *where)
302 /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
303 /*&& all_ascii(&data[pos + 4], data[pos])*/)
305 int len = data[pos] + data[pos + 1] * 256;
306 char *s = malloc(len + 1);
308 memcpy(s, &data[pos + 4], len);
315 fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
319 #define get_string() get_string(WHERE)
322 get_string_be(const char *where)
325 /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
326 /*&& all_ascii(&data[pos + 4], data[pos])*/)
328 int len = data[pos + 2] * 256 + data[pos + 3];
329 char *s = malloc(len + 1);
331 memcpy(s, &data[pos + 4], len);
338 fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
342 #define get_string_be() get_string_be(WHERE)
351 static void __attribute__((unused))
352 hex_dump(FILE *stream, int ofs, int n)
355 for (int i = 0; i < n; i++)
357 int c = data[ofs + i];
358 n_ascii += is_ascii(c);
359 fprintf(stream, " %02x", c);
364 for (int i = 0; i < n; i++)
366 int c = data[ofs + i];
367 putc(c >= 32 && c < 127 ? c : '.', stream);
373 static void __attribute__((unused))
374 char_dump(FILE *stream, int ofs, int n)
376 for (int i = 0; i < n; i++)
378 int c = data[ofs + i];
379 putc(c >= 32 && c < 127 ? c : '.', stream);
386 compare_int(const void *a_, const void *b_)
390 return *a < *b ? -1 : *a > *b;
395 format_name (int format, char *buf)
400 case 2: return "AHEX";
401 case 3: return "COMMA";
402 case 4: return "DOLLAR";
403 case 5: case 40: return "F";
405 case 7: return "PIBHEX";
407 case 9: return "PIB";
408 case 10: return "PK";
409 case 11: return "RB";
410 case 12: return "RBHEX";
414 case 20: return "DATE";
415 case 21: return "TIME";
416 case 22: return "DATETIME";
417 case 23: return "ADATE";
418 case 24: return "JDATE";
419 case 25: return "DTIME";
420 case 26: return "WKDAY";
421 case 27: return "MONTH";
422 case 28: return "MOYR";
423 case 29: return "QYR";
424 case 30: return "WKYR";
425 case 31: return "PCT";
426 case 32: return "DOT";
427 case 33: return "CCA";
428 case 34: return "CCB";
429 case 35: return "CCC";
430 case 36: return "CCD";
431 case 37: return "CCE";
432 case 38: return "EDATE";
433 case 39: return "SDATE";
434 default: sprintf(buf, "(%d)", format); return buf;
439 parse_heading(const char *name)
441 match_u16_assert(0xffff);
443 match_string2_assert(name);
447 match_zeros_assert(int count)
449 for (int i = 0; i < count; i++)
453 "%#x: expected %d zeros here but offset %d is %#"PRIx8"\n",
454 pos, count, i, data[pos + i]);
461 parse_DspString(void)
463 match_byte_assert(1);
464 match_byte_assert(2);
465 match_byte_assert(40);
467 match_byte_assert(5);
468 match_byte_assert(0);
469 match_byte_assert(1);
470 printf ("DspString(\"%s\")\n", get_string1());
474 main(int argc, char *argv[])
476 bool print_offsets = false;
479 int c = getopt (argc, argv, "o");
486 print_offsets = true;
493 if (argc - optind != 1)
495 fprintf (stderr, "usage: %s FILE.bin", argv[0]);
499 const char *filename = argv[optind];
500 int fd = open(filename, O_RDONLY);
503 fprintf (stderr, "%s: open failed (%s)", filename, strerror (errno));
520 if (read(fd, data, n) != n)
527 setvbuf (stdout, NULL, _IOLBF, 0);
529 match_byte_assert(4);
531 match_string1_assert("SPSS Output Document");
533 match_byte_assert(0x63);
535 parse_heading("NavRoot");
536 match_byte_assert(2);
537 match_zeros_assert(32);
539 parse_heading("DspSimpleText");
540 match_zeros_assert(10);
542 parse_heading("DspString");
545 parse_heading("NavTreeViewItem");
548 printf ("%#x: end of successful parse\n", pos);