13 void *result
= malloc(size
);
15 fprintf(stderr
, "Failed to malloc %zi bytes\n", size
);
22 xstrdup(const char *s
)
24 char *result
= strdup(s
);
26 fprintf(stderr
, "Failed to strdup %zi bytes\n", strlen(s
));
33 binary_print(const char *s
, ssize_t len
)
37 for (i
= 0; i
< len
; i
++) {
41 printf("0x%02X", (int)s
[i
]);
46 xfwrite(const void *ptr
, size_t size
, FILE *stream
)
48 if (fwrite(ptr
, size
, 1, stream
) != 1) {
55 write_int(uint64_t value
, size_t len
, FILE *to
)
60 for (i
= 0; i
< len
; i
++)
61 buf
[i
] = ((value
>> (8 * i
)) & 0xff);
62 xfwrite(buf
, len
, to
);
66 write_binary_string(const char *string
, size_t len
, FILE *to
)
68 xfwrite(string
, len
, to
);
72 write_string(const char *string
, FILE *to
)
74 xfwrite(string
, strlen(string
) + 1, to
);
78 read_int(char **from
, size_t len
, const char *max
)
83 if (*from
+ len
> max
) {
84 fprintf(stderr
, "Attempt to read beyond end of file, corrupt file?\n");
88 for (i
= 0; i
< len
; i
++)
89 result
+= (((*from
)[i
] & 0xff) << (8 * i
));
95 read_binary_string(char **from
, size_t len
, const char *max
)
99 if (*from
+ len
> max
) {
100 fprintf(stderr
, "Attempt to read beyond end of file, corrupt file?\n");
104 result
= xmalloc(len
);
105 strncpy(result
, *from
, len
);
111 read_string(char **from
, const char *max
)
113 return read_binary_string(from
, strlen(*from
) + 1, max
);