2 * Copyright (c) 2021, 2022 Omar Polo <op@omarpolo.com>
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.
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.
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.
22 * Usage: pagebundler file > outfile
35 setfname(const char *fname, char *buf, size_t siz)
40 if ((c = strrchr(fname, '/')) != NULL)
45 if ((d = strrchr(fname, '.')) == NULL || c > d)
46 d = strchr(fname, '\0');
50 fprintf(stderr, "file name too long: %s\n", fname);
61 return isprint(c) && c != '\\' && c != '\'' && c != '\n';
65 main(int argc, char **argv)
70 char varname[PATH_MAX];
73 fprintf(stderr, "usage: %s file\n", *argv);
77 setfname(argv[1], varname, sizeof(varname));
79 if ((f = fopen(argv[1], "r")) == NULL) {
80 fprintf(stderr, "%s: can't open %s: %s",
81 argv[0], argv[1], strerror(errno));
85 printf("const uint8_t %s[] = {", varname);
89 r = fread(buf, 1, sizeof(buf), f);
91 for (i = 0; i < r; ++i, ++n) {
98 printf("'%c',", buf[i]);
99 else if (buf[i] == '\n')
102 printf("0x%x,", buf[i]);
106 if (r != sizeof(buf))
111 printf("}; /* %s */\n", varname);