2 f2f795df 2024-02-06 op * Copyright (c) 2021, 2022 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 34b4388f 2022-02-08 op * Usage: pagebundler file > outfile
25 f3f98c70 2022-02-08 op #include <ctype.h>
26 a2728733 2021-07-18 op #include <errno.h>
27 34b4388f 2022-02-08 op #include <limits.h>
28 de6548b9 2021-07-18 op #include <stdint.h>
29 a2728733 2021-07-18 op #include <stdio.h>
30 34b4388f 2022-02-08 op #include <stdlib.h>
31 a2728733 2021-07-18 op #include <string.h>
32 de6548b9 2021-07-18 op #include <unistd.h>
35 34b4388f 2022-02-08 op setfname(const char *fname, char *buf, size_t siz)
37 34b4388f 2022-02-08 op const char *c, *d;
40 34b4388f 2022-02-08 op if ((c = strrchr(fname, '/')) != NULL)
45 34b4388f 2022-02-08 op if ((d = strrchr(fname, '.')) == NULL || c > d)
46 34b4388f 2022-02-08 op d = strchr(fname, '\0');
49 34b4388f 2022-02-08 op if (len >= siz) {
50 34b4388f 2022-02-08 op fprintf(stderr, "file name too long: %s\n", fname);
54 34b4388f 2022-02-08 op memcpy(buf, c, len);
55 34b4388f 2022-02-08 op buf[len] = '\0';
61 f3f98c70 2022-02-08 op return isprint(c) && c != '\\' && c != '\'' && c != '\n';
65 a2728733 2021-07-18 op main(int argc, char **argv)
67 47eaa0e0 2024-06-09 op size_t r, i, n;
69 afda2dda 2022-02-08 op uint8_t buf[BUFSIZ];
70 34b4388f 2022-02-08 op char varname[PATH_MAX];
72 34b4388f 2022-02-08 op if (argc != 2) {
73 34b4388f 2022-02-08 op fprintf(stderr, "usage: %s file\n", *argv);
77 34b4388f 2022-02-08 op setfname(argv[1], varname, sizeof(varname));
79 34b4388f 2022-02-08 op if ((f = fopen(argv[1], "r")) == NULL) {
80 a2728733 2021-07-18 op fprintf(stderr, "%s: can't open %s: %s",
81 34b4388f 2022-02-08 op argv[0], argv[1], strerror(errno));
85 f3f98c70 2022-02-08 op printf("const uint8_t %s[] = {", varname);
89 a2728733 2021-07-18 op r = fread(buf, 1, sizeof(buf), f);
91 f3f98c70 2022-02-08 op for (i = 0; i < r; ++i, ++n) {
92 f3f98c70 2022-02-08 op if (n % 12 == 0)
93 f3f98c70 2022-02-08 op printf("\n\t");
97 f3f98c70 2022-02-08 op if (validc(buf[i]))
98 f3f98c70 2022-02-08 op printf("'%c',", buf[i]);
99 f3f98c70 2022-02-08 op else if (buf[i] == '\n')
100 f3f98c70 2022-02-08 op printf("'\\n',");
102 f3f98c70 2022-02-08 op printf("0x%x,", buf[i]);
104 a2728733 2021-07-18 op printf("\n");
106 a2728733 2021-07-18 op if (r != sizeof(buf))
110 47eaa0e0 2024-06-09 op printf("\t0x0\n");
111 a2728733 2021-07-18 op printf("}; /* %s */\n", varname);