13 msg(int level
, const char *fmt
, ...)
18 if (level
> verbosity
)
22 ret
= vfprintf(stderr
, fmt
, ap
);
30 void *result
= malloc(size
);
32 fprintf(stderr
, "Failed to malloc %zi bytes\n", size
);
39 xstrdup(const char *s
)
41 char *result
= strdup(s
);
43 fprintf(stderr
, "Failed to strdup %zi bytes\n", strlen(s
));
50 binary_print(const char *s
, ssize_t len
)
54 for (i
= 0; i
< len
; i
++) {
58 printf("0x%02X", (int)s
[i
]);
63 xfwrite(const void *ptr
, size_t size
, FILE *stream
)
65 if (fwrite(ptr
, size
, 1, stream
) != 1) {
72 write_int(uint64_t value
, size_t len
, FILE *to
)
77 for (i
= 0; i
< len
; i
++)
78 buf
[i
] = ((value
>> (8 * i
)) & 0xff);
79 xfwrite(buf
, len
, to
);
83 write_binary_string(const char *string
, size_t len
, FILE *to
)
85 xfwrite(string
, len
, to
);
89 write_string(const char *string
, FILE *to
)
91 xfwrite(string
, strlen(string
) + 1, to
);
95 read_int(char **from
, size_t len
, const char *max
)
100 if (*from
+ len
> max
) {
101 fprintf(stderr
, "Attempt to read beyond end of file, corrupt file?\n");
105 for (i
= 0; i
< len
; i
++)
106 result
+= (((*from
)[i
] & 0xff) << (8 * i
));
112 read_binary_string(char **from
, size_t len
, const char *max
)
116 if (*from
+ len
> max
) {
117 fprintf(stderr
, "Attempt to read beyond end of file, corrupt file?\n");
121 result
= xmalloc(len
);
122 strncpy(result
, *from
, len
);
128 read_string(char **from
, const char *max
)
130 return read_binary_string(from
, strlen(*from
) + 1, max
);