diff options
Diffstat (limited to 'util.c')
-rw-r--r-- | util.c | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -1,6 +1,7 @@ | |||
1 | #include "util.h" | 1 | #include "util.h" |
2 | #include <string.h> | 2 | #include <string.h> |
3 | #include <tox/tox.h> | 3 | #include <tox/tox.h> |
4 | #include <stdio.h> | ||
4 | 5 | ||
5 | void writechecksum(uint8_t *address) | 6 | void writechecksum(uint8_t *address) |
6 | { | 7 | { |
@@ -100,3 +101,42 @@ int parse_local_port_forward(char *string, int *local_port, char **hostname, int | |||
100 | 101 | ||
101 | return 0; | 102 | return 0; |
102 | } | 103 | } |
104 | |||
105 | void* file_raw(char *path, uint32_t *size) | ||
106 | { | ||
107 | FILE *file; | ||
108 | char *data; | ||
109 | int len; | ||
110 | |||
111 | file = fopen(path, "rb"); | ||
112 | if(!file) { | ||
113 | fprintf(stderr, "File not found (%s)\n", path); | ||
114 | return NULL; | ||
115 | } | ||
116 | |||
117 | fseek(file, 0, SEEK_END); | ||
118 | len = ftell(file); | ||
119 | data = malloc(len); | ||
120 | if(!data) { | ||
121 | fclose(file); | ||
122 | return NULL; | ||
123 | } | ||
124 | |||
125 | fseek(file, 0, SEEK_SET); | ||
126 | |||
127 | if(fread(data, len, 1, file) != 1) { | ||
128 | fprintf(stderr, "Read error (%s)\n", path); | ||
129 | fclose(file); | ||
130 | free(data); | ||
131 | return NULL; | ||
132 | } | ||
133 | |||
134 | fclose(file); | ||
135 | |||
136 | fprintf(stderr, "Read %u bytes (%s)\n", len, path); | ||
137 | |||
138 | if(size) { | ||
139 | *size = len; | ||
140 | } | ||
141 | return data; | ||
142 | } | ||