forked from cesanta/mongoose
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpack.c
More file actions
131 lines (119 loc) · 4.02 KB
/
Copy pathpack.c
File metadata and controls
131 lines (119 loc) · 4.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// 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:
// const char *mg_unpack(const char *file_name, size_t *size);
//
// 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>
#if defined(_WIN32)
#undef popen
#undef pclose
#undef snprintf
#define snprintf _snprintf
#define popen(a, b) _popen((a), (b))
#define pclose(a) _pclose(a)
#endif
static const char *code =
"const char *mg_unpack(const char *path, size_t *size, time_t *mtime);\n"
"const char *mg_unlist(size_t no);\n"
"const char *mg_unlist(size_t no) {\n"
" return packed_files[no].name;\n"
"}\n"
"const char *mg_unpack(const char *name, size_t *size, time_t *mtime) {\n"
" const struct packed_file *p;\n"
" for (p = packed_files; p->name != NULL; p++) {\n"
" if (strcmp(p->name, name) != 0) continue;\n"
" if (size != NULL) *size = p->size - 1;\n"
" if (mtime != NULL) *mtime = p->mtime;\n"
" return (const char *) p->data;\n"
" }\n"
" return NULL;\n"
"}\n";
int main(int argc, char *argv[]) {
int i, j, ch;
const char *zip_cmd = NULL, *strip_prefix = "";
printf("%s", "#include <stddef.h>\n");
printf("%s", "#include <string.h>\n");
printf("%s", "#include <time.h>\n");
printf("%s", "\n");
for (i = 1; i < argc; i++) {
if (strcmp(argv[i], "-z") == 0 && i + 1 < argc) {
zip_cmd = argv[++i];
} else if (strcmp(argv[i], "-s") == 0) {
strip_prefix = argv[++i];
} else if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) {
fprintf(stderr, "Usage: %s [-z ZIP_CMD] [-s STRIP_PREFIX] files...\n",
argv[0]);
exit(EXIT_FAILURE);
} else {
char ascii[12], cmd[2048];
FILE *fp;
if (zip_cmd == NULL) {
fp = fopen(argv[i], "rb");
} else {
snprintf(cmd, sizeof(cmd), "%s %s", zip_cmd, argv[i]);
fp = popen(cmd, "r");
}
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);
}
}
printf("%s", "\nstatic const struct packed_file {\n");
printf("%s", " const char *name;\n");
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;
const char *name = argv[i];
size_t n = strlen(strip_prefix);
if (strcmp(argv[i], "-z") == 0 || strcmp(argv[i], "-s") == 0) {
i++;
continue;
}
stat(argv[i], &st);
if (strncmp(name, strip_prefix, n) == 0) name += n;
printf(" {\"/%s\", v%d, sizeof(v%d), %lu, %d},\n", name, i, i, st.st_mtime,
zip_cmd == NULL ? 0 : 1);
}
printf("%s", " {NULL, NULL, 0, 0, 0}\n");
printf("%s", "};\n\n");
printf("%s", code);
return EXIT_SUCCESS;
}