Add optional zip cmd to pack.c

This commit is contained in:
Sergey Lyubka 2022-02-12 14:28:02 +00:00
parent 4f802ece52
commit 3e8e2ab513
4 changed files with 43 additions and 22 deletions

View File

@ -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

View File

@ -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,

View File

@ -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,

View File

@ -18,12 +18,13 @@
// 4. Build your app with fs.c:
// cc -o my_app my_app.c fs.c
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
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 <stddef.h>\n");
printf("%s", "#include <string.h>\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);