From 86d07c5bd8cbd0ddae3b5fe42cf0410295296748 Mon Sep 17 00:00:00 2001 From: Tor Andersson Date: Thu, 16 Mar 2017 11:50:41 +0100 Subject: Replace fontdump, bin2hex and cquote with one tool: hexdump. Still need specialty tools for namedump and cmapdump. --- scripts/bin2hex.c | 99 --------------------------------------- scripts/cquote.c | 134 ----------------------------------------------------- scripts/fontdump.c | 121 ----------------------------------------------- scripts/hexdump.c | 122 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 122 insertions(+), 354 deletions(-) delete mode 100644 scripts/bin2hex.c delete mode 100644 scripts/cquote.c delete mode 100644 scripts/fontdump.c create mode 100644 scripts/hexdump.c (limited to 'scripts') diff --git a/scripts/bin2hex.c b/scripts/bin2hex.c deleted file mode 100644 index f7b9d8cf..00000000 --- a/scripts/bin2hex.c +++ /dev/null @@ -1,99 +0,0 @@ -/* bin2hex.c -- Turn the contents of a file into an array of unsigned chars */ - -#include -#include - -/* We never want to build memento versions of the cquote util */ -#undef MEMENTO - -static void -clean(char *p) -{ - while (*p) - { - if ((*p == '/') || (*p == '.') || (*p == '\\') || (*p == '-')) - *p = '_'; - p ++; - } -} - -int -main(int argc, char **argv) -{ - FILE *fi, *fo; - char name[256]; - char *realname; - int i, j, c; - - if (argc < 3) - { - fprintf(stderr, "usage: bin2hex output.h lots of text files\n"); - return 1; - } - - fo = fopen(argv[1], "wb"); - if (!fo) - { - fprintf(stderr, "cquote: could not open output file '%s'\n", argv[1]); - return 1; - } - - fprintf(fo, "/* This is an automatically generated file. Do not edit. */\n"); - - for (i = 2; i < argc; i++) - { - realname = strrchr(argv[i], '/'); - if (!realname) - realname = strrchr(argv[i], '\\'); - if (realname) - realname ++; - else - realname = argv[i]; - - if (strlen(realname) > (sizeof name - 1)) - { - fprintf(stderr, "bin2hex: file name too long\n"); - if (fclose(fo)) - { - fprintf(stderr, "bin2hex: could not close output file '%s'\n", argv[1]); - return 1; - } - return 1; - } - - strcpy(name, realname); - clean(name); - - fi = fopen(argv[i], "rb"); - - j = 0; - while ((c = fgetc(fi)) != EOF) - { - if (j != 0) - { - fputc(',', fo); - fputc(j%8 == 0 ? '\n' : ' ', fo); - } - - fprintf(fo, "0x%02x", c); - j++; - } - - fputc('\n', fo); - - if (fclose(fi)) - { - fprintf(stderr, "bin2hex: could not close input file '%s'\n", argv[i]); - return 1; - } - - } - - if (fclose(fo)) - { - fprintf(stderr, "bin2hex: could not close output file '%s'\n", argv[1]); - return 1; - } - - return 0; -} diff --git a/scripts/cquote.c b/scripts/cquote.c deleted file mode 100644 index 4544d2e1..00000000 --- a/scripts/cquote.c +++ /dev/null @@ -1,134 +0,0 @@ -/* cquote.c -- Turn the contents of a file into a quoted string */ - -#include -#include - -/* We never want to build memento versions of the cquote util */ -#undef MEMENTO - -static void -clean(char *p) -{ - while (*p) - { - if ((*p == '/') || (*p == '.') || (*p == '\\') || (*p == '-')) - *p = '_'; - p ++; - } -} - -int -main(int argc, char **argv) -{ - FILE *fi, *fo; - char name[256]; - char *realname; - int i, c; - int bol = 1; - - if (argc < 3) - { - fprintf(stderr, "usage: cquote output.c lots of text files\n"); - return 1; - } - - fo = fopen(argv[1], "wb"); - if (!fo) - { - fprintf(stderr, "cquote: could not open output file '%s'\n", argv[1]); - return 1; - } - - fprintf(fo, "/* This is an automatically generated file. Do not edit. */\n"); - - for (i = 2; i < argc; i++) - { - realname = strrchr(argv[i], '/'); - if (!realname) - realname = strrchr(argv[i], '\\'); - if (realname) - realname ++; - else - realname = argv[i]; - - if (strlen(realname) > (sizeof name - 1)) - { - fprintf(stderr, "cquote: file name too long\n"); - if (fclose(fo)) - { - fprintf(stderr, "cquote: could not close output file '%s'\n", argv[1]); - return 1; - } - return 1; - } - - strcpy(name, realname); - clean(name); - - fi = fopen(argv[i], "rb"); - - fprintf(fo, "\n/* %s */\n\n", name); - - c = fgetc(fi); - while (c != EOF) - { - int eol = 0; - - if (bol) - { - fputc('\"', fo); - bol = 0; - } - - switch (c) - { - case '\"': - fprintf(fo, "\\\""); - break; - - case '\\': - fprintf(fo, "\\\\"); - break; - - case '\r': - case '\n': - eol = 1; - break; - - default: - fputc(c, fo); - break; - } - - if (eol) - { - fprintf(fo, "\\n\"\n"); - while ((c = fgetc(fi)) == '\r' || c == '\n') - ; - bol = 1; - } - else - { - c = fgetc(fi); - } - } - - if (!bol) - fprintf(fo, "\\n\"\n"); - - if (fclose(fi)) - { - fprintf(stderr, "cquote: could not close input file '%s'\n", argv[i]); - return 1; - } - - } - - if (fclose(fo)) - { - fprintf(stderr, "cquote: could not close output file '%s'\n", argv[1]); - return 1; - } - - return 0; -} diff --git a/scripts/fontdump.c b/scripts/fontdump.c deleted file mode 100644 index 4669b6ee..00000000 --- a/scripts/fontdump.c +++ /dev/null @@ -1,121 +0,0 @@ -/* fontdump.c -- an "xxd -i" workalike for dumping binary fonts as source code */ - -#include -#include -#include - -static int -hexdump(FILE *fo, FILE *fi) -{ - int c, n; - - n = 0; - c = fgetc(fi); - while (c != -1) - { - n += fprintf(fo, "%d,", c); - if (n > 72) { - fprintf(fo, "\n"); - n = 0; - } - c = fgetc(fi); - } - - return n; -} - -int -main(int argc, char **argv) -{ - FILE *fo; - FILE *fi; - char fontname[256]; - char *basename; - char *p; - int i, size; - - if (argc < 3) - { - fprintf(stderr, "usage: fontdump output.c input.dat\n"); - return 1; - } - - fo = fopen(argv[1], "wb"); - if (!fo) - { - fprintf(stderr, "fontdump: could not open output file '%s'\n", argv[1]); - return 1; - } - - fprintf(fo, "#ifndef __STRICT_ANSI__\n"); - fprintf(fo, "#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__)\n"); - fprintf(fo, "#if !defined(__ICC) && !defined(__ANDROID__)\n"); - fprintf(fo, "#define HAVE_INCBIN\n"); - fprintf(fo, "#endif\n"); - fprintf(fo, "#endif\n"); - fprintf(fo, "#endif\n"); - - for (i = 2; i < argc; i++) - { - fi = fopen(argv[i], "rb"); - if (!fi) - { - fclose(fo); - fprintf(stderr, "fontdump: could not open input file '%s'\n", argv[i]); - return 1; - } - - basename = strrchr(argv[i], '/'); - if (!basename) - basename = strrchr(argv[i], '\\'); - if (basename) - basename++; - else - basename = argv[i]; - - if (strlen(basename) >= sizeof(fontname)) - { - fclose(fi); - fclose(fo); - fprintf(stderr, "fontdump: filename '%s' too long\n", basename); - return 1; - } - - strcpy(fontname, basename); - for (p = fontname; *p; ++p) - { - if (*p == '/' || *p == '.' || *p == '\\' || *p == '-') - *p = '_'; - } - - fseek(fi, 0, SEEK_END); - size = ftell(fi); - fseek(fi, 0, SEEK_SET); - - fprintf(fo, "\n#ifdef HAVE_INCBIN\n"); - fprintf(fo, "const int fz_font_%s_size = %d;\n", fontname, size); - fprintf(fo, "asm(\".section .rodata\");\n"); - fprintf(fo, "asm(\".global fz_font_%s\");\n", fontname); - fprintf(fo, "asm(\".type fz_font_%s STT_OBJECT\");\n", fontname); - fprintf(fo, "asm(\".size fz_font_%s, %d\");\n", fontname, size); - fprintf(fo, "asm(\".balign 64\");\n"); - fprintf(fo, "asm(\"fz_font_%s:\");\n", fontname); - fprintf(fo, "asm(\".incbin \\\"%s\\\"\");\n", argv[i]); - fprintf(fo, "#else\n"); - fprintf(fo, "const int fz_font_%s_size = %d;\n", fontname, size); - fprintf(fo, "const char fz_font_%s[] = {\n", fontname); - hexdump(fo, fi); - fprintf(fo, "};\n"); - fprintf(fo, "#endif\n"); - - fclose(fi); - } - - if (fclose(fo)) - { - fprintf(stderr, "fontdump: could not close output file '%s'\n", argv[1]); - return 1; - } - - return 0; -} diff --git a/scripts/hexdump.c b/scripts/hexdump.c new file mode 100644 index 00000000..728afc33 --- /dev/null +++ b/scripts/hexdump.c @@ -0,0 +1,122 @@ +/* hexdump.c -- an "xxd -i" workalike for dumping binary files as source code */ + +#include +#include +#include + +static int +hexdump(FILE *fo, FILE *fi) +{ + int c, n; + + n = 0; + c = fgetc(fi); + while (c != -1) + { + n += fprintf(fo, "%d,", c); + if (n > 72) { + fprintf(fo, "\n"); + n = 0; + } + c = fgetc(fi); + } + + return n; +} + +int +main(int argc, char **argv) +{ + FILE *fo; + FILE *fi; + char filename[256]; + char *basename; + char *p; + int i, size; + + if (argc < 3) + { + fprintf(stderr, "usage: hexdump output.c input.dat\n"); + return 1; + } + + fo = fopen(argv[1], "wb"); + if (!fo) + { + fprintf(stderr, "hexdump: could not open output file '%s'\n", argv[1]); + return 1; + } + + fprintf(fo, "#ifndef __STRICT_ANSI__\n"); + fprintf(fo, "#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__)\n"); + fprintf(fo, "#if !defined(__ICC) && !defined(__ANDROID__)\n"); + fprintf(fo, "#define HAVE_INCBIN\n"); + fprintf(fo, "#endif\n"); + fprintf(fo, "#endif\n"); + fprintf(fo, "#endif\n"); + + for (i = 2; i < argc; i++) + { + fi = fopen(argv[i], "rb"); + if (!fi) + { + fclose(fo); + fprintf(stderr, "hexdump: could not open input file '%s'\n", argv[i]); + return 1; + } + + basename = strrchr(argv[i], '/'); + if (!basename) + basename = strrchr(argv[i], '\\'); + if (basename) + basename++; + else + basename = argv[i]; + + if (strlen(basename) >= sizeof(filename)) + { + fclose(fi); + fclose(fo); + fprintf(stderr, "hexdump: filename '%s' too long\n", basename); + return 1; + } + + strcpy(filename, argv[i]); + for (p = filename; *p; ++p) + { + if (*p == '/' || *p == '.' || *p == '\\' || *p == '-') + *p = '_'; + } + + fseek(fi, 0, SEEK_END); + size = ftell(fi); + fseek(fi, 0, SEEK_SET); + + fprintf(fo, "\n#ifdef HAVE_INCBIN\n"); + fprintf(fo, "const int fz_%s_size = %d;\n", filename, size); + fprintf(fo, "extern const char fz_%s[];\n", filename); + fprintf(fo, "asm(\".section .rodata\");\n"); + fprintf(fo, "asm(\".global fz_%s\");\n", filename); + fprintf(fo, "asm(\".type fz_%s STT_OBJECT\");\n", filename); + fprintf(fo, "asm(\".size fz_%s, %d\");\n", filename, size); + fprintf(fo, "asm(\".balign 64\");\n"); + fprintf(fo, "asm(\"fz_%s:\");\n", filename); + fprintf(fo, "asm(\".incbin \\\"%s\\\"\");\n", argv[i]); + fprintf(fo, "#else\n"); + fprintf(fo, "const int fz_%s_size = %d;\n", filename, size); + fprintf(fo, "const char fz_%s[] = {\n", filename); + hexdump(fo, fi); + fprintf(fo, "0};\n"); /* zero-terminate so we can hexdump text files into C strings */ + fprintf(fo, "#endif\n"); + + fclose(fi); + } + + if (fclose(fo)) + { + fprintf(stderr, "hexdump: could not close output file '%s'\n", argv[1]); + return 1; + } + + return 0; +} -- cgit v1.2.3