-
Notifications
You must be signed in to change notification settings - Fork 1
/
binary_embed.cpp
executable file
·61 lines (45 loc) · 1.07 KB
/
binary_embed.cpp
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
/***
binary_embed.cpp
Transform a binary file into a C source file that can be compiled into an executable.
Copyright (c) 2014-2022, C. M. Street
***/
#include "libcodehappy.h"
#define BIN_ERROR 1
u32 flength(const char* pname) {
FILE *f;
u32 ret;
f = fopen(pname, "rb");
ret = filelen(f);
fclose(f);
return ret;
}
int binary_to_c_src(const char* infile, const char* outfile) {
int32_t flen;
FILE* f;
FILE* fout;
unsigned char* cbuf;
int e;
int cline;
flen = flength(infile);
if (flen <= 0)
return(BIN_ERROR);
cbuf = new [unsigned char] flen;
if (NULL == cbuf)
return(BIN_ERROR);
f = fopen(infile, "rb");
fread(cbuf, sizeof(unsigned char), flen, f);
fclose(f);
fout = fopen(outfile, "w");
fprintf(fout, "const unsigned char __data[%d] = {\n\t", flen);
for (cline = 0, e = 0; e < flen; ++e) {
fprintf(fout, "0x%X, ", cbuf[e]);
++cline;
if ((cline & 0xf) == 0)
fprintf(fout, "\n\t");
}
fprintf(fout, "\n};\n");
fclose(fout);
delete [] cbuf;
return(0);
}
/* end binary_embed.cpp */