2021-07-25 10:22:56 +01:00
|
|
|
// Copyright (c) Cesanta Software Limited
|
|
|
|
// All rights reserved.
|
|
|
|
|
|
|
|
// This program is used to pack arbitrary data into a C binary. It takes
|
|
|
|
// a list of files as an input, and produces a .c data file that contains
|
|
|
|
// contents of all these files as a collection of byte arrays.
|
|
|
|
//
|
|
|
|
// Usage:
|
|
|
|
// 1. Compile this file:
|
|
|
|
// cc -o pack pack.c
|
|
|
|
//
|
|
|
|
// 2. Convert list of files into single .c:
|
|
|
|
// ./pack file1.data file2.data > fs.c
|
|
|
|
//
|
|
|
|
// 3. In your application code, you can access files using this function:
|
2021-07-26 09:15:17 +01:00
|
|
|
// const char *mg_unpack(const char *file_name, size_t *size);
|
2021-07-25 10:22:56 +01:00
|
|
|
//
|
|
|
|
// 4. Build your app with fs.c:
|
|
|
|
// cc -o my_app my_app.c fs.c
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2021-07-30 13:19:20 +01:00
|
|
|
#include <sys/stat.h>
|
2021-07-25 10:22:56 +01:00
|
|
|
|
|
|
|
static const char *code =
|
Make private functions static and add missing prototypes.
Fixes:
mongoose/mongoose.c:180:8: warning: no previous prototype for ‘mg_dns_parse_name’ [-Wmissing-prototypes]
180 | size_t mg_dns_parse_name(const uint8_t *s, size_t n, size_t ofs, char *dst,
| ^~~~~~~~~~~~~~~~~
mongoose/mongoose.c:306:6: warning: no previous prototype for ‘mg_dns_send’ [-Wmissing-prototypes]
306 | void mg_dns_send(struct mg_connection *c, const struct mg_str *name,
| ^~~~~~~~~~~
mongoose/mongoose.c:925:6: warning: no previous prototype for ‘mg_http_parse_headers’ [-Wmissing-prototypes]
925 | void mg_http_parse_headers(const char *s, const char *end,
| ^~~~~~~~~~~~~~~~~~~~~
mongoose/mongoose.c:1125:7: warning: no previous prototype for ‘mg_http_etag’ [-Wmissing-prototypes]
1125 | char *mg_http_etag(char *buf, size_t len, size_t size, time_t mtime) {
| ^~~~~~~~~~~~
mongoose/mongoose.c:2578:6: warning: no previous prototype for ‘mg_sha1_transform’ [-Wmissing-prototypes]
2578 | void mg_sha1_transform(uint32_t state[5], const unsigned char buffer[64]) {
| ^~~~~~~~~~~~~~~~~
mongoose/mongoose.c:2976:8: warning: no previous prototype for ‘mg_open_listener’ [-Wmissing-prototypes]
2976 | SOCKET mg_open_listener(const char *url, struct mg_addr *addr) {
| ^~~~~~~~~~~~~~~~
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
2021-10-12 18:26:20 -06:00
|
|
|
"const char *mg_unlist(size_t no);\n"
|
2021-07-30 13:19:20 +01:00
|
|
|
"const char *mg_unlist(size_t no) {\n"
|
|
|
|
" return packed_files[no].name;\n"
|
|
|
|
"}\n"
|
Make private functions static and add missing prototypes.
Fixes:
mongoose/mongoose.c:180:8: warning: no previous prototype for ‘mg_dns_parse_name’ [-Wmissing-prototypes]
180 | size_t mg_dns_parse_name(const uint8_t *s, size_t n, size_t ofs, char *dst,
| ^~~~~~~~~~~~~~~~~
mongoose/mongoose.c:306:6: warning: no previous prototype for ‘mg_dns_send’ [-Wmissing-prototypes]
306 | void mg_dns_send(struct mg_connection *c, const struct mg_str *name,
| ^~~~~~~~~~~
mongoose/mongoose.c:925:6: warning: no previous prototype for ‘mg_http_parse_headers’ [-Wmissing-prototypes]
925 | void mg_http_parse_headers(const char *s, const char *end,
| ^~~~~~~~~~~~~~~~~~~~~
mongoose/mongoose.c:1125:7: warning: no previous prototype for ‘mg_http_etag’ [-Wmissing-prototypes]
1125 | char *mg_http_etag(char *buf, size_t len, size_t size, time_t mtime) {
| ^~~~~~~~~~~~
mongoose/mongoose.c:2578:6: warning: no previous prototype for ‘mg_sha1_transform’ [-Wmissing-prototypes]
2578 | void mg_sha1_transform(uint32_t state[5], const unsigned char buffer[64]) {
| ^~~~~~~~~~~~~~~~~
mongoose/mongoose.c:2976:8: warning: no previous prototype for ‘mg_open_listener’ [-Wmissing-prototypes]
2976 | SOCKET mg_open_listener(const char *url, struct mg_addr *addr) {
| ^~~~~~~~~~~~~~~~
Signed-off-by: James Hilliard <james.hilliard1@gmail.com>
2021-10-12 18:26:20 -06:00
|
|
|
"const char *mg_unpack(const char *name, size_t *size, time_t *mtime);\n"
|
2021-07-30 13:19:20 +01:00
|
|
|
"const char *mg_unpack(const char *name, size_t *size, time_t *mtime) {\n"
|
2021-07-25 10:22:56 +01:00
|
|
|
" const struct packed_file *p;\n"
|
2021-07-26 09:15:17 +01:00
|
|
|
" for (p = packed_files; p->name != NULL; p++) {\n"
|
2021-07-25 10:22:56 +01:00
|
|
|
" if (strcmp(p->name, name) != 0) continue;\n"
|
|
|
|
" if (size != NULL) *size = p->size - 1;\n"
|
2021-07-30 13:19:20 +01:00
|
|
|
" if (mtime != NULL) *mtime = p->mtime;\n"
|
2021-07-25 10:22:56 +01:00
|
|
|
" return (const char *) p->data;\n"
|
|
|
|
" }\n"
|
|
|
|
" return NULL;\n"
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
int i, j, ch;
|
|
|
|
|
|
|
|
printf("%s", "#include <stddef.h>\n");
|
|
|
|
printf("%s", "#include <string.h>\n");
|
2021-07-30 13:19:20 +01:00
|
|
|
printf("%s", "#include <time.h>\n");
|
2021-07-25 10:22:56 +01:00
|
|
|
printf("%s", "\n");
|
|
|
|
|
|
|
|
for (i = 1; i < argc; i++) {
|
2021-07-25 21:08:02 +01:00
|
|
|
char ascii[12];
|
2021-07-30 13:19:20 +01:00
|
|
|
FILE *fp = fopen(argv[i], "rb");
|
|
|
|
if (fp == NULL) printf("Cannot open %s\n", argv[i]), exit(EXIT_FAILURE);
|
2021-07-25 10:22:56 +01:00
|
|
|
|
2021-07-25 21:08:02 +01:00
|
|
|
printf("static const unsigned char v%d[] = {\n", i);
|
2021-07-25 10:22:56 +01:00
|
|
|
for (j = 0; (ch = fgetc(fp)) != EOF; j++) {
|
2021-07-25 21:08:02 +01:00
|
|
|
if (j == (int) sizeof(ascii)) {
|
|
|
|
printf(" // %.*s\n", j, ascii);
|
|
|
|
j = 0;
|
|
|
|
}
|
2021-07-26 09:05:34 +01:00
|
|
|
ascii[j] = (char) ((ch >= ' ' && ch <= '~' && ch != '\\') ? ch : '.');
|
2021-07-25 10:22:56 +01:00
|
|
|
printf(" %3u,", ch);
|
|
|
|
}
|
|
|
|
// Append zero byte at the end, to make text files appear in memory
|
|
|
|
// as nul-terminated strings.
|
2021-07-25 21:08:02 +01:00
|
|
|
// printf(" 0 // %.*s\n", (int) sizeof(ascii), ascii);
|
|
|
|
printf(" 0 // %.*s\n};\n", j, ascii);
|
2021-07-25 10:22:56 +01:00
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2021-07-26 09:15:17 +01:00
|
|
|
printf("%s", "\nstatic const struct packed_file {\n");
|
2021-07-25 10:22:56 +01:00
|
|
|
printf("%s", " const char *name;\n");
|
|
|
|
printf("%s", " const unsigned char *data;\n");
|
|
|
|
printf("%s", " size_t size;\n");
|
2021-07-30 13:19:20 +01:00
|
|
|
printf("%s", " time_t mtime;\n");
|
2021-07-26 09:15:17 +01:00
|
|
|
printf("%s", "} packed_files[] = {\n");
|
2021-07-25 10:22:56 +01:00
|
|
|
|
|
|
|
for (i = 1; i < argc; i++) {
|
2021-07-30 13:19:20 +01:00
|
|
|
struct stat st;
|
|
|
|
stat(argv[i], &st);
|
2021-07-30 17:41:23 +01:00
|
|
|
printf(" {\"/%s\", v%d, sizeof(v%d), %lu},\n", argv[i], i, i, st.st_mtime);
|
2021-07-25 10:22:56 +01:00
|
|
|
}
|
2021-07-30 13:19:20 +01:00
|
|
|
printf("%s", " {NULL, NULL, 0, 0}\n");
|
2021-07-25 10:22:56 +01:00
|
|
|
printf("%s", "};\n\n");
|
|
|
|
printf("%s", code);
|
|
|
|
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|