+#include <assert.h>
#include <float.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>
+#include "u8-mbtouc.h"
static uint8_t *data;
static size_t n, pos;
}
#define match_u32_assert(x) match_u32_assert(x, WHERE)
+static bool
+match_byte(uint8_t b)
+{
+ if (pos < n && data[pos] == b)
+ {
+ pos++;
+ return true;
+ }
+ else
+ return false;
+}
+
+static void
+match_byte_assert(uint8_t b, const char *where)
+{
+ if (!match_byte(b))
+ {
+ fprintf(stderr, "%s: 0x%x: expected %02x, got %02x\n", where, pos, b, data[pos]);
+ exit(1);
+ }
+}
+#define match_byte_assert(b) match_byte_assert(b, WHERE)
+
static bool
all_ascii(const uint8_t *p)
{
return true;
}
+static bool
+all_utf8(const uint8_t *p)
+{
+ size_t len = strlen ((char *) p);
+ for (size_t ofs = 0, mblen; ofs < len; ofs += mblen)
+ {
+ ucs4_t uc;
+
+ mblen = u8_mbtouc (&uc, p + ofs, len - ofs);
+ if (uc < 32 || uc == 127 || uc == 0xfffd)
+ return false;
+ }
+ return true;
+}
+
static char *
get_fixed_string(int len, const char *where)
{
- if (pos + len > n || !memchr(&data[pos], 0, len) || !all_ascii(&data[pos]))
+ if (pos + len > n || !memchr(&data[pos], 0, len) || !all_utf8(&data[pos]))
{
fprintf(stderr, "%s: 0x%x: bad fixed-width string\n", where, pos);
exit(1);
}
}
-int
-main(int argc, char **argv)
+static void
+dump_source(int end, int count, int n_series, const char *name)
{
- size_t start;
- struct stat s;
-
- if (isatty(STDIN_FILENO))
- {
- fprintf(stderr, "redirect stdin from a .bin file\n");
- exit(1);
- }
- if (fstat(STDIN_FILENO, &s))
- {
- perror("fstat");
- exit(1);
- }
- n = s.st_size;
- data = malloc(n);
- if (!data)
- {
- perror("malloc");
- exit(1);
- }
- if (read(STDIN_FILENO, data, n) != n)
- {
- perror("read");
- exit(1);
- }
-
- pos = 0;
- match_u32_assert(0x1b000);
- match_u32_assert(s.st_size);
-
- printf ("%08x:", pos);
- int count = get_u32(), n_series = get_u32();
- printf (" %d series, %d observations per series\n", n_series, count);
- printf ("%08x\n\n", pos);
-
const union
{
uint8_t b[8];
double d;
}
sysmis = {.b = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xef, 0xff}};
- pos = 0x58;
+ int n_sysmis = 0;
for (int i = 0; i < n_series; i++)
{
- printf ("%08x:", pos);
- printf (" %s\n", get_fixed_string(288));
- printf ("%08x:", pos);
+ printf (" series %d: \"%s\", %d values:\n ",
+ i, get_fixed_string(288), count);
for (int i = 0; i < count; i++)
{
double d = get_double();
if (d == sysmis.d)
- printf (" .");
+ {
+ printf (" .");
+ n_sysmis++;
+ }
else
printf (" %.*g", DBL_DIG, d);
}
printf ("\n");
}
- printf ("%08x:", pos);
- printf (" %d", get_u32());
- printf (", \"%s\"\n", get_string());
+ if (pos >= end)
+ return;
- printf ("\n%08x:", pos);
+ match_u32_assert(1);
+ char *name2 = get_string();
+ assert(!strcmp(name, name2));
+
+ printf ("\n %08x:", pos);
int n_more_series = get_u32();
+ if (n_series != n_more_series)
+ printf("different series counts: %d %d\n", n_series, n_more_series);
+ assert(n_more_series <= n_series);
printf (" %d series to come\n", n_more_series);
+ int max1 = -1;
+ int ofs = pos;
for (int i = 0; i < n_more_series; i++)
{
printf ("%08x:", pos);
{
int x = get_u32();
int y = get_u32();
- printf (" (%d,%d)", x, y);
+ printf (" (%d, %d)", x, y);
+ if (y > max1)
+ max1 = y;
}
printf ("\n");
}
printf ("\n%08x:", pos);
int n_strings = get_u32();
+ assert(n_strings == max1 + 1);
printf (" %d strings\n", n_strings);
+
+ char **strings = malloc(n_strings * sizeof *strings);
for (int i = 0; i < n_strings; i++)
{
- int x = get_u32();
+ int frequency = get_u32();
char *s = get_string();
- printf ("%d: \"%s\" (%d)\n", i, s, x);
+ printf ("%d: \"%s\" (%d)\n", i, s, frequency);
+ strings[i] = s;
+ }
+ printf ("\n");
+
+ assert (pos == end);
+ pos = ofs;
+ printf("Strings:\n");
+ for (int i = 0; i < n_more_series; i++)
+ {
+ printf (" \"%s\"\n", get_string());
+ int n_pairs = get_u32();
+ for (int j = 0; j < n_pairs; j++)
+ {
+ int x = get_u32();
+ //assert (x == j);
+ int y = get_u32();
+ printf (" %d: \"%s\"\n", x, strings[y]);
+ }
+ printf ("\n");
+ }
+ pos = end;
+}
+
+int
+main(int argc, char **argv)
+{
+ size_t start;
+ struct stat s;
+
+ if (isatty(STDIN_FILENO))
+ {
+ fprintf(stderr, "redirect stdin from a .bin file\n");
+ exit(1);
+ }
+ if (fstat(STDIN_FILENO, &s))
+ {
+ perror("fstat");
+ exit(1);
+ }
+ n = s.st_size;
+ data = malloc(n);
+ if (!data)
+ {
+ perror("malloc");
+ exit(1);
+ }
+ if (read(STDIN_FILENO, data, n) != n)
+ {
+ perror("read");
+ exit(1);
+ }
+
+ pos = 0;
+ match_byte_assert(0);
+ int version = data[pos];
+ if (!match_byte(0xaf))
+ match_byte_assert(0xb0);
+ int n_sources = data[pos++];
+ match_byte_assert(0);
+
+ match_u32_assert(s.st_size);
+ printf ("%d sources\n", n_sources);
+
+ struct source
+ {
+ int offset, count, n_series;
+ char *name;
+ }
+ sources[n_sources];
+ for (int i = 0; i < n_sources; i++)
+ {
+ int count = get_u32();
+ int n_series = get_u32();
+ int offset = get_u32();
+ char *name = get_fixed_string(version == 0xb0 ? 64 : 28);
+ int dunno = version == 0xb0 ? get_u32() : 0;
+ printf ("source %d: %d series, %d observations per series, offset %08x, \"%s\", %x\n",
+ i, n_series, count, offset, name, dunno);
+ sources[i].offset = offset;
+ sources[i].count = count;
+ sources[i].n_series = n_series;
+ sources[i].name = name;
+ }
+
+ for (int i = 0; i < n_sources; i++)
+ {
+ if (pos != sources[i].offset)
+ {
+ fprintf (stderr, "pos=0x%x expected=0x%x reading source %d\n", pos, sources[i].offset, i);
+ exit(1);
+ }
+ dump_source(i + 1 >= n_sources ? n : sources[i + 1].offset, sources[i].count, sources[i].n_series, sources[i].name);
}
+ assert(pos == n);
- dump_raw (stdout, pos, n, "\n");
- putchar('\n');
return 0;
}