From 3e8e2ab513f2fce6aef38dd9b35b6b1ce63a4dbc Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Sat, 12 Feb 2022 14:28:02 +0000 Subject: [PATCH] Add optional zip cmd to pack.c --- Makefile | 4 ++-- mongoose.c | 2 +- src/http.c | 2 +- test/pack.c | 57 ++++++++++++++++++++++++++++++++++++----------------- 4 files changed, 43 insertions(+), 22 deletions(-) diff --git a/Makefile b/Makefile index b3be6b96..5cf73209 100644 --- a/Makefile +++ b/Makefile @@ -33,9 +33,9 @@ all: mg_prefix unamalgamated unpacked test test++ arm examples vc98 vc2017 mingw examples: @for X in $(EXAMPLES); do test -f $$X/Makefile || continue; $(MAKE) -C $$X example || exit 1; done -test/packed_fs.c: Makefile src/fs.h src/ssi.h test/fuzz.c test/data/a.txt +test/packed_fs.c: $(CC) $(CFLAGS) test/pack.c -o pack - ./pack $? > $@ + ./pack Makefile src/ssi.h test/fuzz.c test/data/a.txt -z 'gzip -c' test/data/range.txt > $@ # Check that all external (exported) symbols have "mg_" prefix mg_prefix: mongoose.c mongoose.h diff --git a/mongoose.c b/mongoose.c index c93ab987..9fb40de0 100644 --- a/mongoose.c +++ b/mongoose.c @@ -1869,7 +1869,7 @@ void mg_http_delete_chunk(struct mg_connection *c, struct mg_http_message *hm) { ce = &ch.ptr[ch.len]; if (ce < end) memmove((void *) ch.ptr, ce, (size_t) (end - ce)); c->recv.len -= ch.len; - if (c->pfn_data == NULL) c->pfn_data = (char *) c->pfn_data - ch.len; + if (c->pfn_data != NULL) c->pfn_data = (char *) c->pfn_data - ch.len; } int mg_http_upload(struct mg_connection *c, struct mg_http_message *hm, diff --git a/src/http.c b/src/http.c index 39899a86..a11e7d6e 100644 --- a/src/http.c +++ b/src/http.c @@ -875,7 +875,7 @@ void mg_http_delete_chunk(struct mg_connection *c, struct mg_http_message *hm) { ce = &ch.ptr[ch.len]; if (ce < end) memmove((void *) ch.ptr, ce, (size_t) (end - ce)); c->recv.len -= ch.len; - if (c->pfn_data == NULL) c->pfn_data = (char *) c->pfn_data - ch.len; + if (c->pfn_data != NULL) c->pfn_data = (char *) c->pfn_data - ch.len; } int mg_http_upload(struct mg_connection *c, struct mg_http_message *hm, diff --git a/test/pack.c b/test/pack.c index a296a13f..d48d28e3 100644 --- a/test/pack.c +++ b/test/pack.c @@ -18,12 +18,13 @@ // 4. Build your app with fs.c: // cc -o my_app my_app.c fs.c +#include #include #include +#include #include static const char *code = - "const char *mg_unlist(size_t no);\n" "const char *mg_unlist(size_t no) {\n" " return packed_files[no].name;\n" "}\n" @@ -41,6 +42,7 @@ static const char *code = int main(int argc, char *argv[]) { int i, j, ch; + const char *zip_cmd = NULL; printf("%s", "#include \n"); printf("%s", "#include \n"); @@ -48,24 +50,40 @@ int main(int argc, char *argv[]) { printf("%s", "\n"); for (i = 1; i < argc; i++) { - char ascii[12]; - FILE *fp = fopen(argv[i], "rb"); - if (fp == NULL) printf("Cannot open %s\n", argv[i]), exit(EXIT_FAILURE); + if (strcmp(argv[i], "-z") == 0 && i + 1 < argc) { + zip_cmd = argv[++i]; + } else { + char ascii[12], cmd[2048]; + FILE *fp; - printf("static const unsigned char v%d[] = {\n", i); - for (j = 0; (ch = fgetc(fp)) != EOF; j++) { - if (j == (int) sizeof(ascii)) { - printf(" // %.*s\n", j, ascii); - j = 0; + if (zip_cmd == NULL) { + fp = fopen(argv[i], "rb"); + } else { + snprintf(cmd, sizeof(cmd), "%s %s", zip_cmd, argv[i]); + fp = popen(cmd, "r"); } - ascii[j] = (char) ((ch >= ' ' && ch <= '~' && ch != '\\') ? ch : '.'); - printf(" %3u,", ch); + if (fp == NULL) { + fprintf(stderr, "Cannot open [%s]: %s\n", zip_cmd ? cmd : argv[i], + strerror(errno)); + exit(EXIT_FAILURE); + } + + printf("static const unsigned char v%d[] = {\n", i); + for (j = 0; (ch = fgetc(fp)) != EOF; j++) { + if (j == (int) sizeof(ascii)) { + printf(" // %.*s\n", j, ascii); + j = 0; + } + ascii[j] = (char) ((ch >= ' ' && ch <= '~' && ch != '\\') ? ch : '.'); + printf(" %3u,", ch); + } + // Append zero byte at the end, to make text files appear in memory + // as nul-terminated strings. + // printf(" 0 // %.*s\n", (int) sizeof(ascii), ascii); + printf(" 0 // %.*s\n};\n", j, ascii); + if (zip_cmd == NULL) fclose(fp); + if (zip_cmd != NULL) pclose(fp); } - // Append zero byte at the end, to make text files appear in memory - // as nul-terminated strings. - // printf(" 0 // %.*s\n", (int) sizeof(ascii), ascii); - printf(" 0 // %.*s\n};\n", j, ascii); - fclose(fp); } printf("%s", "\nstatic const struct packed_file {\n"); @@ -73,14 +91,17 @@ int main(int argc, char *argv[]) { printf("%s", " const unsigned char *data;\n"); printf("%s", " size_t size;\n"); printf("%s", " time_t mtime;\n"); + printf("%s", " int zipped;\n"); printf("%s", "} packed_files[] = {\n"); for (i = 1; i < argc; i++) { struct stat st; + if (strcmp(argv[i], "-z") == 0 && ++i + 1 < argc) continue; stat(argv[i], &st); - printf(" {\"/%s\", v%d, sizeof(v%d), %lu},\n", argv[i], i, i, st.st_mtime); + printf(" {\"/%s\", v%d, sizeof(v%d), %lu, %d},\n", argv[i], i, i, + st.st_mtime, zip_cmd == NULL ? 0 : 1); } - printf("%s", " {NULL, NULL, 0, 0}\n"); + printf("%s", " {NULL, NULL, 0, 0, 0}\n"); printf("%s", "};\n\n"); printf("%s", code);