-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbin2src.c
81 lines (70 loc) · 2.12 KB
/
bin2src.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
A stupid C program that does what xxd does with
the source option just incase you don't have it installed
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct filebuffer {
char* data;
size_t length;
};
struct filebuffer filebuffer(const char* path) {
char* data;
size_t length;
FILE* f = fopen(path, "rb+");
fseek(f, 0, SEEK_END);
length = ftell(f);
fseek(f, 0, SEEK_SET);
data = malloc(length);
fread(data, 1, length, f);
fclose(f);
return (struct filebuffer){
.data = data,
.length = length
};
}
// don't do pretty printing... It's just data.
void spit_buffer(FILE* stream, char* variable_name, struct filebuffer buffer) {
fprintf(stream, "struct {\n");
fprintf(stream, "\tunsigned char buffer[%d];\n", buffer.length);
fprintf(stream, "\tsize_t length;\n");
fprintf(stream, "} %s = {\n", variable_name);
printf("\t{");
for (size_t i = 0; i < buffer.length; ++i) {
if ((i % 20) == 0) {
printf("\n\t\t");
}
printf("0x%X, ", buffer.data[i] & 0xFF);
}
printf("\n\t},\n\t%d", buffer.length);
fprintf(stream, "\n};\n\n");
}
int main(int argc, char** argv) {
if (argc < 2) {
fprintf(stderr, "feedme files\n");
return 1;
}
FILE* output = stdout;
fprintf(output, "// This file was generated. Please do not touch!\n");
fprintf(output, "/*\n\tORIGINAL FILES:\n");
for (int i = 1; i < argc; ++i) {
fprintf(output, "\t\t%s\n", argv[i]);
}
fprintf(output, "*/\n\n");
char temp[1024];
for (int i = 1; i < argc; ++i) {
char* filename = argv[i];
strncpy(temp, "FileData_", 1024);
strncpy(temp+9, filename, 1024-9);
int len = strlen(temp);
for (int j = 0; j < len; ++j) {
if (temp[j] == '/' || temp[j] == '\\' || temp[j] == ':' || temp[j] == ' ' || temp[j] == '.' || temp[j] == ',' || temp[j] == '-' || temp[j] == '+' || temp[j] == '=') {
temp[j] = '_';
}
}
spit_buffer(output, temp, filebuffer(filename));
}
fclose(output);
return 0;
}