+static char *
+get_string_be(const char *where)
+{
+ if (1
+ /*data[pos + 1] == 0 && data[pos + 2] == 0 && data[pos + 3] == 0*/
+ /*&& all_ascii(&data[pos + 4], data[pos])*/)
+ {
+ int len = data[pos + 2] * 256 + data[pos + 3];
+ char *s = malloc(len + 1);
+
+ memcpy(s, &data[pos + 4], len);
+ s[len] = 0;
+ pos += 4 + len;
+ return s;
+ }
+ else
+ {
+ fprintf(stderr, "%s: 0x%x: expected string\n", where, pos);
+ exit(1);
+ }
+}
+#define get_string_be() get_string_be(WHERE)
+
+static int
+get_end(void)
+{
+ int len = get_u32();
+ return pos + len;
+}
+
+static void __attribute__((unused))
+hex_dump(FILE *stream, int ofs, int n)
+{
+ for (int i = 0; i < n; i++)
+ {
+ int c = data[ofs + i];
+#if 1
+ if (i && !(i % 16))
+ putc('-', stream);
+ else
+ putc(' ', stream);
+#endif
+ fprintf(stream, "%02x", c);
+ }
+ for (int i = 0; i < n; i++)
+ {
+ int c = data[ofs + i];
+ putc(c >= 32 && c < 127 ? c : '.', stream);
+ }
+ putc('\n', stream);
+}
+
+static char *
+dump_counted_string(void)
+{
+ int inner_end = get_end();
+ if (pos == inner_end)
+ return NULL;
+
+ if (match_u32(5))
+ {
+ match_u32_assert(0);
+ match_byte_assert(0x58);
+ }
+ else
+ match_u32_assert(0);
+
+ char *s = NULL;
+ if (match_byte(0x31))
+ s = get_string();
+ else
+ match_byte_assert(0x58);
+ if (pos != inner_end)
+ {
+ fprintf(stderr, "inner end discrepancy\n");
+ exit(1);
+ }
+ return s;
+}
+
+static void
+dump_style(FILE *stream)
+{
+ if (match_byte(0x58))
+ return;
+
+ match_byte_assert(0x31);
+ if (get_bool())
+ printf (" bold=\"yes\"");
+ if (get_bool())
+ printf (" italic=\"yes\"");
+ if (get_bool())
+ printf (" underline=\"yes\"");
+ if (!get_bool())
+ printf (" show=\"no\"");
+ char *fg = get_string(); /* foreground */
+ char *bg = get_string(); /* background */
+ char *font = get_string(); /* font */
+ int size = get_byte() * (72. / 96.);
+ fprintf(stream, " fgcolor=\"%s\" bgcolor=\"%s\" font=\"%s\" size=\"%dpt\"",
+ fg, bg, font, size);
+}
+
+static void
+dump_style2(FILE *stream)
+{
+ if (match_byte(0x58))
+ return;
+
+ match_byte_assert(0x31);
+ uint32_t halign = get_u32();
+ printf (" halign=\"%s\"",
+ halign == 0 ? "center"
+ : halign == 2 ? "left"
+ : halign == 4 ? "right"
+ : halign == 6 ? "decimal"
+ : halign == 0xffffffad ? "mixed"
+ : "<error>");
+ int valign = get_u32();
+ printf (" valign=\"%s\"",
+ valign == 0 ? "center"
+ : valign == 1 ? "top"
+ : valign == 3 ? "bottom"
+ : "<error>");
+ printf (" offset=\"%gpt\"", get_double());
+ int l = get_u16();
+ int r = get_u16();
+ int t = get_u16();
+ int b = get_u16();
+ printf (" margins=\"%d %d %d %d\"", l, r, t, b);
+}
+
+static char *
+dump_nested_string(FILE *stream)
+{
+ char *s = NULL;
+
+ match_byte_assert (0);
+ match_byte_assert (0);
+ int outer_end = get_end();
+ s = dump_counted_string();
+ if (s)
+ fprintf(stream, " \"%s\"", s);
+ dump_style(stream);
+ match_byte_assert(0x58);
+ if (pos != outer_end)
+ {
+ fprintf(stderr, "outer end discrepancy\n");
+ exit(1);
+ }
+
+ return s;
+}
+