Blob


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