-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbce.c
More file actions
131 lines (111 loc) · 3.88 KB
/
Copy pathbce.c
File metadata and controls
131 lines (111 loc) · 3.88 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
131
/*
bce v1.3
https://github.com/wr7/bce
bce is software for converting binary files to C headers. This is useful for
embedding binary files into your C project.
This software is licensed under the MIT No Attribution License (SPDX MIT-0).
Copyright © 2024 wr7
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to
deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
sell copies of the Software, and to permit persons to whom the Software is
furnished to do so.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
IN THE SOFTWARE.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <errno.h>
static void print_usage(void) {
fprintf(stderr,
"Converts a file to a C byte array. \n"
"This is a non drop-in replacement for `xxd -i`.\n"
"\n"
"Usage\n"
"bce <input_file> <output_file>\n"
"`-` can be used in place of <input_file> or <output_file>\n"
"to read from stdin or to output to stdout\n"
"\n"
"Ouput\n"
"bce has very different output compared to `xxd -i`.\n"
"Say the user runs `bce foo.bin foo.h`. \n"
"If foo.bin contains only two null bytes, bce will only output:\n"
" {0,0,};\n"
"\n"
"Unlike xxd, the user must create the variable storing the data themselves,\n"
"and they must find the length themselves. \n"
"\n"
"This can be done with the following C code:\n"
"\n"
"const char FOO[] =\n"
"#include \"foo.h\"\n"
"\n"
"const size_t FOO_LEN = sizeof(FOO)/sizeof(FOO[0]);\n"
);
}
static void check_write_error(void) {
if(errno != 0) {
perror("bce: failed to write to output file");
exit(errno);
}
}
int main(int argc, char **args) {
// Check commandline arguments //
if(argc == 2) {
if(strcmp(args[1], "--help") == 0 || strcmp(args[1], "-h") == 0) {
print_usage();
return 0;
}
}
if(argc != 3) {
fprintf(stderr, "bce: Invalid usage, see `bce --help` for more information.\n");
return 1;
}
// Open input and output files //
const char *const input_file_name = args[1];
FILE *input_file;
if(strcmp(input_file_name, "-") == 0) {
input_file = stdin;
} else {
input_file = fopen(input_file_name, "rb");
}
if(input_file == NULL) {
perror("bce: Failed to open input file");
exit(errno);
}
const char *const output_file_name = args[2];
FILE *output_file;
if(strcmp(output_file_name, "-") == 0) {
output_file = stdout;
} else {
output_file = fopen(output_file_name, "w");
}
if(output_file == NULL) {
perror("bce: Failed to open output file");
exit(errno);
}
// Read and write to input/output files //
errno = 0;
fprintf(output_file, "{");
check_write_error();
for(int byte = getc(input_file); byte != EOF; byte = getc(input_file)) {
fprintf(output_file, "%u,", byte);
check_write_error();
}
if(errno != 0) {
perror("bce: Failed to read from input file");
exit(errno);
}
fprintf(output_file, "};");
check_write_error();
fclose(output_file);
fclose(input_file);
}