1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
struct blob blob_new(void) {
return (struct blob) {.data = NULL, .size = 0};
}
void blob_append(struct blob *const blob, void const *const data, size_t const size) {
char *new = realloc(blob->data, blob->size + size);
if (new == NULL) {
abort();
}
memcpy(new + blob->size, data, size);
blob->data = new;
blob->size += size;
}
void blob_free(struct blob *const blob) {
free(blob->data);
blob->data = NULL;
blob->size = 0;
}
struct blob load_file(char const *const name) {
const size_t START_SIZE = 8192;
FILE *file = fopen(name, "rb");
if (file == NULL) {
return (struct blob) {.data = NULL};
}
char *data = malloc(START_SIZE);
size_t allocated = START_SIZE;
size_t used = 0;
while (1) {
size_t read = fread(data + used, 1, allocated - used, file);
if (read != allocated - used) {
used += read;
break;
}
used += read;
allocated *= 2;
void *const newdata = realloc(data, allocated);
if (newdata == NULL) {
goto realloc_error;
}
data = newdata;
}
if (used == 0) {
free(data);
fclose(file);
return (struct blob) {.data = NULL, .size = 0};
}
void *const newdata = realloc(data, used);
if (newdata == NULL) {
goto realloc_error;
}
fclose(file);
return (struct blob) {.data = newdata, .size = used};
realloc_error:
free(data);
fclose(file);
return (struct blob) {.data = NULL};
}
|