}
static size_t
-find(const char *target, size_t target_len)
+try_find(const char *target, size_t target_len)
{
const uint8_t *pos = (const uint8_t *) memmem (data, n, target, target_len);
+ return pos ? pos - data : 0;
+}
+
+static size_t
+try_find_tail(const char *target, size_t target_len)
+{
+ size_t pos = try_find(target, target_len);
+ return pos ? pos + target_len : 0;
+}
+
+static size_t
+find(const char *target, size_t target_len)
+{
+ size_t pos = try_find(target, target_len);
+ if (!pos)
+ {
+ fprintf (stderr, "not found\n");
+ exit(1);
+ }
+ return pos;
+}
+
+static size_t
+find_tail(const char *target, size_t target_len)
+{
+ size_t pos = try_find_tail(target, target_len);
if (!pos)
{
fprintf (stderr, "not found\n");
exit(1);
}
- return pos - data;
+ return pos;
}
size_t pos;
printf ("nested %d bytes", subn);
pos += subn;
printf ("; \"%s\", substitutions:", get_string());
- fprintf (stderr, "substitutions:");
for (;;)
{
int n_subst = get_u32();
if (!n_subst)
break;
printf (" %d", n_subst);
- fprintf (stderr, " %d", n_subst);
total_subs *= n_subst;
}
- putc ('\n', stderr);
for (int i = 0; i < total_subs; i++)
{
}
else if (!strcmp(argv[1], "dimensions"))
{
- const char dimensions[] = "-,,,.\0";
- start = find(dimensions, sizeof dimensions - 1) + sizeof dimensions - 1;
+ {
+ const char dimensions[] = "-,,,.\0";
+ start = try_find_tail(dimensions, sizeof dimensions - 1);
+ }
+
+ if (!start)
+ {
+ const char dimensions[] = "-,,, .\0";
+ start = find_tail(dimensions, sizeof dimensions - 1);
+ }
+
pos = start;
dump_dims ();
return 0;