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
|
#ifdef _MSC_VER
#define _CRT_SECURE_NO_WARNINGS
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
int main (int argc, char *argv[])
{
char* input_name;
FILE* input_file;
char* output_name;
FILE* output_file;
char* variable_name;
if (4 != argc)
{
puts("Usage: bin2h [input] [output] [variable]");
return EXIT_FAILURE;
}
input_name = argv[1];
output_name = argv[2];
variable_name = argv[3];
input_file = fopen(input_name, "rb");
if (NULL == input_file)
{
printf("Could not open input file '%s': %s\n", input_name, strerror(errno));
return EXIT_FAILURE;
}
output_file = fopen(output_name, "w");
if (NULL == output_file)
{
printf("Could not open output file '%s': %s\n", output_name, strerror(errno));
return EXIT_FAILURE;
}
if (fprintf(output_file, "static const unsigned char %s[] = {", variable_name) < 0)
{
printf("Could not write to output file '%s': %s\n", output_name, strerror(ferror(output_file)));
return EXIT_FAILURE;
}
while (0 == feof(input_file))
{
unsigned char buffer[4096];
size_t i, count = fread(buffer, 1, sizeof(buffer), input_file);
if (sizeof(buffer) != count)
{
if (0 == feof(input_file) || 0 != ferror(input_file))
{
printf("Could not read from input file '%s': %s\n", input_name, strerror(ferror(input_file)));
return EXIT_FAILURE;
}
}
for (i = 0; i < count; ++i)
{
if ((i & 15) == 0)
{
if (fprintf(output_file, "\n ") < 0)
{
printf("Could not write to output file '%s': %s\n", output_name, strerror(ferror(output_file)));
return EXIT_FAILURE;
}
}
if (fprintf(output_file, "0x%2.2x, ", buffer[i]) < 0)
{
printf("Could not write to output file '%s': %s\n", output_name, strerror(ferror(output_file)));
return EXIT_FAILURE;
}
}
}
if (fprintf(output_file, "\n};\n") < 0)
{
printf("Could not write to output file '%s': %s\n", output_name, strerror(ferror(output_file)));
return EXIT_FAILURE;
}
if (fclose(output_file) < 0)
{
printf("Could not close output file '%s': %s\n", output_name, strerror(ferror(output_file)));
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|