forked from gwilymk/bin2c
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbin2c.c
More file actions
130 lines (107 loc) · 3.25 KB
/
bin2c.c
File metadata and controls
130 lines (107 loc) · 3.25 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* This is bin2c program, which allows you to convert binary file to
* C language array, for use as embedded resource, for instance you can
* embed graphics or audio file directly into your program.
* This is public domain software, use it on your own risk.
* Contact Serge Fukanchik at fuxx@mail.ru if you have any questions.
*
* Some modifications were made by Gwilym Kuiper (kuiper.gwilym@gmail.com)
* I have decided not to change the licence.
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#ifdef USE_BZ2
#include <bzlib.h>
#endif
int
main(int argc, char *argv[])
{
char *buf;
char *ident;
unsigned int i, file_size, need_comma;
FILE *f_input, *f_output;
bool is_appending = false;
#ifdef USE_BZ2
char *bz2_buf;
unsigned int uncompressed_size, bz2_size;
#endif
if (argc < 4) {
fprintf(stderr, "Usage: %s binary_file output_file array_name\n\n"
"Command line arguments: \n"
" -a|--append appends to output file instead of overwriting\n",
argv[0]);
return -1;
}
if(argc > 4) {
if(strcmp(argv[4], "-a") == 0 || strcmp(argv[4], "--append") == 0) {
is_appending = true;
}
else {
fprintf(stderr, "Invalid command line argument: %s\n", argv[4]);
return -1;
}
}
f_input = fopen(argv[1], "rb");
if (f_input == NULL) {
fprintf(stderr, "%s: can't open %s for reading\n", argv[0], argv[1]);
return -1;
}
// Get the file length
fseek(f_input, 0, SEEK_END);
file_size = ftell(f_input);
fseek(f_input, 0, SEEK_SET);
buf = (char *) malloc(file_size);
assert(buf);
fread(buf, file_size, 1, f_input);
fclose(f_input);
#ifdef USE_BZ2
// allocate for bz2.
bz2_size =
(file_size + file_size / 100 + 1) + 600; // as per the documentation
bz2_buf = (char *) malloc(bz2_size);
assert(bz2_buf);
// compress the data
int status =
BZ2_bzBuffToBuffCompress(bz2_buf, &bz2_size, buf, file_size, 9, 1, 0);
if (status != BZ_OK) {
fprintf(stderr, "Failed to compress data: error %i\n", status);
return -1;
}
// and be very lazy
free(buf);
uncompressed_size = file_size;
file_size = bz2_size;
buf = bz2_buf;
#endif
if(is_appending)
f_output = fopen(argv[2], "a+");
else
f_output = fopen(argv[2], "w");
if (f_output == NULL) {
fprintf(stderr, "%s: can't open %s for writing\n", argv[0], argv[2]);
return -1;
}
ident = argv[3];
need_comma = 0;
fprintf(f_output, "const char %s[%i] = {", ident, file_size);
for (i = 0; i < file_size; ++i) {
if (need_comma)
fprintf(f_output, ", ");
else
need_comma = 1;
if ((i % 11) == 0)
fprintf(f_output, "\n\t");
fprintf(f_output, "0x%.2x", buf[i] & 0xff);
}
fprintf(f_output, "\n};\n\n");
fprintf(f_output, "const unsigned int %s_length = %i;\n", ident, file_size);
#ifdef USE_BZ2
fprintf(f_output, "const unsigned int %s_length_uncompressed = %i;\n", ident,
uncompressed_size);
#endif
fclose(f_output);
return 0;
}