13 #include "u8-mbtouc.h"
15 static const char *filename;
24 #define STR(x) XSTR(x)
25 #define WHERE __FILE__":" STR(__LINE__)
37 memcpy(&x, &data[pos], 4);
42 static unsigned long long int
46 memcpy(&x, &data[pos], 8);
55 x = (data[pos] << 24) | (data[pos + 1] << 16) | (data[pos + 2] << 8) | data[pos + 3];
64 memcpy(&x, &data[pos], 2);
73 memcpy(&x, &data[pos], 8);
78 static double __attribute__((unused))
82 memcpy(&x, &data[pos], 4);
106 match_u32_assert(uint32_t x, const char *where)
108 unsigned int y = get_u32();
111 fprintf(stderr, "%s: 0x%x: expected i%u, got i%u\n", where, pos - 4, x, y);
115 #define match_u32_assert(x) match_u32_assert(x, WHERE)
118 match_u16_assert(uint16_t x, const char *where)
120 unsigned int y = get_u16();
123 fprintf(stderr, "%s: 0x%x: expected u16:%u, got u16:%u\n", where, pos - 2, x, y);
127 #define match_u16_assert(x) match_u16_assert(x, WHERE)
129 static bool __attribute__((unused))
130 match_u64(uint64_t x)
138 static void __attribute__((unused))
139 match_u64_assert(uint64_t x, const char *where)
141 unsigned long long int y = get_u64();
144 fprintf(stderr, "%s: 0x%x: expected u64:%lu, got u64:%llu\n", where, pos - 8, x, y);
148 #define match_u64_assert(x) match_u64_assert(x, WHERE)
150 static bool __attribute__((unused))
151 match_be32(uint32_t x)
160 match_be32_assert(uint32_t x, const char *where)
162 unsigned int y = get_be32();
165 fprintf(stderr, "%s: 0x%x: expected be%u, got be%u\n", where, pos - 4, x, y);
169 #define match_be32_assert(x) match_be32_assert(x, WHERE)
172 match_byte(uint8_t b)
174 if (pos < n && data[pos] == b)
184 match_byte_assert(uint8_t b, const char *where)
188 fprintf(stderr, "%s: 0x%x: expected %02x, got %02x\n", where, pos, b, data[pos]);
192 #define match_byte_assert(b) match_byte_assert(b, WHERE)
195 match_bytes(int start, const int *bytes, size_t n_bytes)
197 for (size_t i = 0; i < n_bytes; i++)
198 if (bytes[i] >= 0 && data[start + i] != bytes[i])
204 xmemdup0(const void *p, size_t n)
206 char *s = malloc(n + 1);
217 match_byte_assert(1);
221 static bool __attribute__((unused))
224 return (p >= ' ' && p < 127) || p == '\r' || p == '\n' || p == '\t';
228 count_zeros(const uint8_t *p)
236 static bool __attribute__((unused))
237 all_utf8(const char *p_, size_t len)
239 const uint8_t *p = (const uint8_t *) p_;
240 for (size_t ofs = 0, mblen; ofs < len; ofs += mblen)
244 mblen = u8_mbtouc (&uc, p + ofs, len - ofs);
245 if ((uc < 32 && uc != '\n') || uc == 127 || uc == 0xfffd)
254 int len = data[pos++];
255 char *s = xmemdup0(&data[pos], len);
263 int len = data[pos] + data[pos + 1] * 256;
264 char *s = xmemdup0(&data[pos + 2], len);
270 get_string(const char *where)
273 /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
274 /*&& all_ascii(&data[pos + 4], data[pos])*/)
276 int len = data[pos] + data[pos + 1] * 256;
277 char *s = malloc(len + 1);
279 memcpy(s, &data[pos + 4], len);
286 fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
290 #define get_string() get_string(WHERE)
293 get_string_be(const char *where)
296 /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
297 /*&& all_ascii(&data[pos + 4], data[pos])*/)
299 int len = data[pos + 2] * 256 + data[pos + 3];
300 char *s = malloc(len + 1);
302 memcpy(s, &data[pos + 4], len);
309 fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
313 #define get_string_be() get_string_be(WHERE)
322 static void __attribute__((unused))
323 hex_dump(FILE *stream, int ofs, int n)
326 for (int i = 0; i < n; i++)
328 int c = data[ofs + i];
329 n_ascii += is_ascii(c);
330 fprintf(stream, " %02x", c);
335 for (int i = 0; i < n; i++)
337 int c = data[ofs + i];
338 putc(c >= 32 && c < 127 ? c : '.', stream);
344 static void __attribute__((unused))
345 char_dump(FILE *stream, int ofs, int n)
347 for (int i = 0; i < n; i++)
349 int c = data[ofs + i];
350 putc(c >= 32 && c < 127 ? c : '.', stream);
357 compare_int(const void *a_, const void *b_)
361 return *a < *b ? -1 : *a > *b;
366 format_name (int format, char *buf)
371 case 2: return "AHEX";
372 case 3: return "COMMA";
373 case 4: return "DOLLAR";
374 case 5: case 40: return "F";
376 case 7: return "PIBHEX";
378 case 9: return "PIB";
379 case 10: return "PK";
380 case 11: return "RB";
381 case 12: return "RBHEX";
385 case 20: return "DATE";
386 case 21: return "TIME";
387 case 22: return "DATETIME";
388 case 23: return "ADATE";
389 case 24: return "JDATE";
390 case 25: return "DTIME";
391 case 26: return "WKDAY";
392 case 27: return "MONTH";
393 case 28: return "MOYR";
394 case 29: return "QYR";
395 case 30: return "WKYR";
396 case 31: return "PCT";
397 case 32: return "DOT";
398 case 33: return "CCA";
399 case 34: return "CCB";
400 case 35: return "CCC";
401 case 36: return "CCD";
402 case 37: return "CCE";
403 case 38: return "EDATE";
404 case 39: return "SDATE";
405 default: sprintf(buf, "(%d)", format); return buf;
411 main(int argc, char *argv[])
413 bool print_offsets = false;
416 int c = getopt (argc, argv, "o");
423 print_offsets = true;
430 if (argc - optind != 1)
432 fprintf (stderr, "usage: %s FILE.bin", argv[0]);
436 const char *filename = argv[optind];
437 int fd = open(filename, O_RDONLY);
440 fprintf (stderr, "%s: open failed (%s)", filename, strerror (errno));
457 if (read(fd, data, n) != n)
464 setvbuf (stdout, NULL, _IOLBF, 0);