forked from psgroove/psgroove
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathraw2payload.c
56 lines (45 loc) · 1.04 KB
/
raw2payload.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
/* raw2payload. Convert our raw PPC code into a fancy C header */
#include <stdio.h>
#include <string.h>
static const char header[] =
"#ifndef __%s__\n" \
"#define __%s__\n\n" \
"const uint8_t PROGMEM %s[] = {\n";
static const char footer[] = "};\n\n#endif\n";
int main(int argc, char **argv)
{
char buf[256];
FILE *fi, *fo;
int r, k;
if (argc < 4) {
fprintf(stderr, "Usage: %s <raw> <c header> <array name>\n", argv[0]);
return 1;
}
fi = fopen(argv[1], "rb");
if (fi == NULL) {
perror(argv[1]);
return 1;
}
fo = fopen(argv[2], "w");
if (fo == NULL) {
perror(argv[2]);
return 1;
}
fprintf(fo, header, argv[3], argv[3], argv[3]);
while ((r = fread(buf, 1, 256, fi)) > 0) {
for (k = 0; k < r; k++) {
fprintf(fo, "0x%.2x, ", buf[k] & 0xff);
if (k && !((k + 1) & 15))
fprintf(fo, "\n");
}
}
r = strlen(footer);
if (fwrite(footer, 1, r, fo) != r) {
perror("fwrite");
return 1;
}
fclose(fi);
fclose(fo);
fprintf(stdout, "Header generated.\n");
return 0;
}