Blame


1 a2728733 2021-07-18 op /*
2 a2728733 2021-07-18 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
3 a2728733 2021-07-18 op *
4 a2728733 2021-07-18 op * Permission to use, copy, modify, and distribute this software for any
5 a2728733 2021-07-18 op * purpose with or without fee is hereby granted, provided that the above
6 a2728733 2021-07-18 op * copyright notice and this permission notice appear in all copies.
7 a2728733 2021-07-18 op *
8 a2728733 2021-07-18 op * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 a2728733 2021-07-18 op * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 a2728733 2021-07-18 op * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 a2728733 2021-07-18 op * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 a2728733 2021-07-18 op * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 a2728733 2021-07-18 op * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 a2728733 2021-07-18 op * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 a2728733 2021-07-18 op */
16 a2728733 2021-07-18 op
17 a2728733 2021-07-18 op /*
18 a2728733 2021-07-18 op * pagebundler converts the given file into a valid C program that can
19 a2728733 2021-07-18 op * be compiled. The generated code provides a variable that holds the
20 ed84beb9 2021-07-18 op * content of the original file and a _len variable with the size.
21 a2728733 2021-07-18 op *
22 a2728733 2021-07-18 op * Usage: pagebundler -f file -v varname > outfile
23 a2728733 2021-07-18 op */
24 a2728733 2021-07-18 op
25 a2728733 2021-07-18 op #include <errno.h>
26 de6548b9 2021-07-18 op #include <stdint.h>
27 a2728733 2021-07-18 op #include <stdio.h>
28 a2728733 2021-07-18 op #include <string.h>
29 de6548b9 2021-07-18 op #include <unistd.h>
30 a2728733 2021-07-18 op
31 a2728733 2021-07-18 op const char *file;
32 a2728733 2021-07-18 op const char *varname;
33 a2728733 2021-07-18 op
34 a2728733 2021-07-18 op int
35 a2728733 2021-07-18 op main(int argc, char **argv)
36 a2728733 2021-07-18 op {
37 a2728733 2021-07-18 op size_t len, r, i;
38 2aeddf9c 2021-07-18 op int ch, did;
39 a2728733 2021-07-18 op FILE *f;
40 a2728733 2021-07-18 op uint8_t buf[64];
41 a2728733 2021-07-18 op
42 a2728733 2021-07-18 op while ((ch = getopt(argc, argv, "f:v:")) != -1) {
43 a2728733 2021-07-18 op switch (ch) {
44 a2728733 2021-07-18 op case 'f':
45 a2728733 2021-07-18 op file = optarg;
46 a2728733 2021-07-18 op break;
47 a2728733 2021-07-18 op case 'v':
48 a2728733 2021-07-18 op varname = optarg;
49 a2728733 2021-07-18 op break;
50 a2728733 2021-07-18 op default:
51 a2728733 2021-07-18 op fprintf(stderr, "%s: wrong usage\n",
52 a2728733 2021-07-18 op argv[0]);
53 a2728733 2021-07-18 op return 1;
54 a2728733 2021-07-18 op }
55 a2728733 2021-07-18 op }
56 a2728733 2021-07-18 op
57 a2728733 2021-07-18 op if (file == NULL || varname == NULL) {
58 a2728733 2021-07-18 op fprintf(stderr, "%s: wrong usage\n", argv[0]);
59 a2728733 2021-07-18 op return 1;
60 a2728733 2021-07-18 op }
61 a2728733 2021-07-18 op
62 a2728733 2021-07-18 op if ((f = fopen(file, "r")) == NULL) {
63 a2728733 2021-07-18 op fprintf(stderr, "%s: can't open %s: %s",
64 a2728733 2021-07-18 op argv[0], file, strerror(errno));
65 ed84beb9 2021-07-18 op return 1;
66 a2728733 2021-07-18 op }
67 a2728733 2021-07-18 op
68 a2728733 2021-07-18 op printf("const uint8_t %s[] = {\n", varname);
69 a2728733 2021-07-18 op
70 2aeddf9c 2021-07-18 op did = 0;
71 a2728733 2021-07-18 op len = 0;
72 a2728733 2021-07-18 op for (;;) {
73 a2728733 2021-07-18 op r = fread(buf, 1, sizeof(buf), f);
74 a2728733 2021-07-18 op len += r;
75 a2728733 2021-07-18 op
76 2aeddf9c 2021-07-18 op if (r != 0)
77 2aeddf9c 2021-07-18 op did = 1;
78 2aeddf9c 2021-07-18 op
79 a2728733 2021-07-18 op printf("\t");
80 a2728733 2021-07-18 op for (i = 0; i < r; ++i) {
81 a2728733 2021-07-18 op printf("0x%x, ", buf[i]);
82 a2728733 2021-07-18 op }
83 a2728733 2021-07-18 op printf("\n");
84 a2728733 2021-07-18 op
85 a2728733 2021-07-18 op if (r != sizeof(buf))
86 a2728733 2021-07-18 op break;
87 a2728733 2021-07-18 op }
88 a2728733 2021-07-18 op
89 2aeddf9c 2021-07-18 op if (!did) {
90 2aeddf9c 2021-07-18 op /*
91 2aeddf9c 2021-07-18 op * if nothing was emitted, add a NUL byte. This was
92 2aeddf9c 2021-07-18 op * still produce an exact copy of the file because
93 2aeddf9c 2021-07-18 op * `len' doesn't count this NUL byte. It prevents the
94 2aeddf9c 2021-07-18 op * "use of GNU empty initializer extension" warning
95 2aeddf9c 2021-07-18 op * when bundling pages/about_empty.gmi
96 2aeddf9c 2021-07-18 op */
97 2aeddf9c 2021-07-18 op printf("\t0x0\n");
98 2aeddf9c 2021-07-18 op }
99 2aeddf9c 2021-07-18 op
100 a2728733 2021-07-18 op printf("}; /* %s */\n", varname);
101 a2728733 2021-07-18 op
102 a2728733 2021-07-18 op printf("size_t %s_len = %zu;\n", varname, len);
103 a2728733 2021-07-18 op
104 a2728733 2021-07-18 op fclose(f);
105 a2728733 2021-07-18 op return 0;
106 a2728733 2021-07-18 op }