/
blob.c
60 lines (50 loc) · 922 Bytes
/
blob.c
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
/* This file is part of punani-strike
* Copyright (c) 2011 Gianni Tedesco
* Released under the terms of GPLv3
*/
#include <punani/punani.h>
#include <punani/blob.h>
uint8_t *blob_from_file(const char *fn, size_t *size)
{
FILE *f = NULL;
uint8_t *b = NULL;
int sz;
f = fopen(fn, "rb");
if ( NULL == f )
goto err;
fseek(f, 0, SEEK_END);
sz = ftell(f);
fseek(f, 0, SEEK_SET);
if ( sz < 0 )
goto err;
b = malloc(sz);
if ( NULL == b )
goto err;
if ( fread(b, sz, 1, f) <= 0 )
goto err;
*size = sz;
fclose(f);
return b;
err:
fprintf(stderr, "%s: %s\n", fn, strerror(errno));
if ( f )
fclose(f);
free(b);
*size = 0;
return NULL;
}
int blob_to_file(const uint8_t *b, size_t sz, const char *fn)
{
FILE *f;
int ret;
f = fopen(fn, "wb");
if ( NULL == f )
return 0;
ret = (fwrite(b, sz, 1, f) == 1);
fclose(f);
return ret;
}
void blob_free(uint8_t *blob, size_t sz)
{
free(blob);
}