pack.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright (c) Cesanta Software Limited
  2. // All rights reserved.
  3. // This program is used to pack arbitrary data into a C binary. It takes
  4. // a list of files as an input, and produces a .c data file that contains
  5. // contents of all these files as a collection of byte arrays.
  6. //
  7. // Usage:
  8. // 1. Compile this file:
  9. // cc -o pack pack.c
  10. //
  11. // 2. Convert list of files into single .c:
  12. // ./pack file1.data file2.data > fs.c
  13. //
  14. // 3. In your application code, you can access files using this function:
  15. // const char *mg_unpack(const char *file_name, size_t *size);
  16. //
  17. // 4. Build your app with fs.c:
  18. // cc -o my_app my_app.c fs.c
  19. #include <errno.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/stat.h>
  24. static const char *code =
  25. "static int scmp(const char *a, const char *b) {\n"
  26. " while (*a && (*a == *b)) a++, b++;\n"
  27. " return *(const unsigned char *) a - *(const unsigned char *) b;\n"
  28. "}\n"
  29. "const char *mg_unlist(size_t no);\n"
  30. "const char *mg_unlist(size_t no) {\n"
  31. " return packed_files[no].name;\n"
  32. "}\n"
  33. "const char *mg_unpack(const char *path, size_t *size, time_t *mtime);\n"
  34. "const char *mg_unpack(const char *name, size_t *size, time_t *mtime) {\n"
  35. " const struct packed_file *p;\n"
  36. " for (p = packed_files; p->name != NULL; p++) {\n"
  37. " if (scmp(p->name, name) != 0) continue;\n"
  38. " if (size != NULL) *size = p->size - 1;\n"
  39. " if (mtime != NULL) *mtime = p->mtime;\n"
  40. " return (const char *) p->data;\n"
  41. " }\n"
  42. " return NULL;\n"
  43. "}\n";
  44. int main(int argc, char *argv[]) {
  45. int i, j, ch;
  46. const char *strip_prefix = "";
  47. printf("%s", "#include <stddef.h>\n");
  48. printf("%s", "#include <string.h>\n");
  49. printf("%s", "#include <time.h>\n");
  50. printf("%s", "\n");
  51. for (i = 1; i < argc; i++) {
  52. if (strcmp(argv[i], "-s") == 0) {
  53. strip_prefix = argv[++i];
  54. } else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
  55. fprintf(stderr, "Usage: %s[-s STRIP_PREFIX] files...\n", argv[0]);
  56. exit(EXIT_FAILURE);
  57. } else {
  58. char ascii[12];
  59. FILE *fp = fopen(argv[i], "rb");
  60. if (fp == NULL) {
  61. fprintf(stderr, "Cannot open [%s]: %s\n", argv[i], strerror(errno));
  62. exit(EXIT_FAILURE);
  63. }
  64. printf("static const unsigned char v%d[] = {\n", i);
  65. for (j = 0; (ch = fgetc(fp)) != EOF; j++) {
  66. if (j == (int) sizeof(ascii)) {
  67. printf(" // %.*s\n", j, ascii);
  68. j = 0;
  69. }
  70. ascii[j] = (char) ((ch >= ' ' && ch <= '~' && ch != '\\') ? ch : '.');
  71. printf(" %3u,", ch);
  72. }
  73. // Append zero byte at the end, to make text files appear in memory
  74. // as nul-terminated strings.
  75. // printf(" 0 // %.*s\n", (int) sizeof(ascii), ascii);
  76. printf(" 0 // %.*s\n};\n", j, ascii);
  77. fclose(fp);
  78. }
  79. }
  80. printf("%s", "\nstatic const struct packed_file {\n");
  81. printf("%s", " const char *name;\n");
  82. printf("%s", " const unsigned char *data;\n");
  83. printf("%s", " size_t size;\n");
  84. printf("%s", " time_t mtime;\n");
  85. printf("%s", "} packed_files[] = {\n");
  86. for (i = 1; i < argc; i++) {
  87. struct stat st;
  88. const char *name = argv[i];
  89. size_t n = strlen(strip_prefix);
  90. if (strcmp(argv[i], "-s") == 0) {
  91. i++;
  92. continue;
  93. }
  94. stat(argv[i], &st);
  95. if (strncmp(name, strip_prefix, n) == 0) name += n;
  96. printf(" {\"/%s\", v%d, sizeof(v%d), %lu},\n", name, i, i, st.st_mtime);
  97. }
  98. printf("%s", " {NULL, NULL, 0, 0}\n");
  99. printf("%s", "};\n\n");
  100. printf("%s", code);
  101. return EXIT_SUCCESS;
  102. }