2 a2728733 2021-07-18 op * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
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.
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.
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.
22 a2728733 2021-07-18 op * Usage: pagebundler -f file -v varname > outfile
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>
31 a2728733 2021-07-18 op const char *file;
32 a2728733 2021-07-18 op const char *varname;
35 a2728733 2021-07-18 op main(int argc, char **argv)
37 a2728733 2021-07-18 op size_t len, r, i;
40 a2728733 2021-07-18 op uint8_t buf[64];
42 a2728733 2021-07-18 op while ((ch = getopt(argc, argv, "f:v:")) != -1) {
45 a2728733 2021-07-18 op file = optarg;
48 a2728733 2021-07-18 op varname = optarg;
51 a2728733 2021-07-18 op fprintf(stderr, "%s: wrong usage\n",
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]);
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));
68 95a8c791 2021-08-26 op printf("const uint8_t %s[] = {\n", varname);
73 a2728733 2021-07-18 op r = fread(buf, 1, sizeof(buf), f);
80 a2728733 2021-07-18 op for (i = 0; i < r; ++i) {
81 a2728733 2021-07-18 op printf("0x%x, ", buf[i]);
85 a2728733 2021-07-18 op if (r != sizeof(buf))
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
97 2aeddf9c 2021-07-18 op printf("\t0x0\n");
100 a2728733 2021-07-18 op printf("}; /* %s */\n", varname);
102 a2728733 2021-07-18 op printf("size_t %s_len = %zu;\n", varname, len);