-
Notifications
You must be signed in to change notification settings - Fork 0
/
zmgfs.c
302 lines (247 loc) · 8.17 KB
/
zmgfs.c
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
//
// Created by rainm on 17-6-9.
//
#include <string.h>
#include <stdio.h>
#include <zlib.h>
#include <assert.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <stdlib.h>
#include "zmgfs.h"
void init_zmg_header(struct zmg_header *header) {
strcpy(header->magic, "zmg");
header->version = ZMG_VERSION;
}
int zip_buffer_to_file(const char *buffer, size_t size, FILE *dest, size_t *outsize, int level) {
*outsize = 0;
int ret, flush;
unsigned have;
z_stream strm;
unsigned char out[CHUNK_SIZE] = {0};
/* allocate deflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
ret = deflateInit(&strm, level);
if (ret != Z_OK)
return ret;
size_t pos = 0;
/* compress until end of file */
do {
int blk = (int) ((size - pos) > CHUNK_SIZE ? CHUNK_SIZE : (size - pos));
if (blk == 0) {
break;
}
strm.avail_in = (uInt) blk;
flush = pos + blk < size ? Z_NO_FLUSH : Z_FINISH;
strm.next_in = (Bytef *) (buffer + pos);
/* run deflate() on input until output buffer not full, finish
compression if all of source has been read in */
do {
strm.avail_out = CHUNK_SIZE;
strm.next_out = out;
ret = deflate(&strm, flush); /* no bad return value */
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
have = CHUNK_SIZE - strm.avail_out;
if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
(void) deflateEnd(&strm);
return Z_ERRNO;
}
*outsize += have;
} while (strm.avail_out == 0);
assert(strm.avail_in == 0); /* all input will be used */
pos += blk;
/* done when last data in file processed */
} while (flush != Z_FINISH);
assert(ret == Z_STREAM_END); /* stream will be complete */
/* clean up and return */
(void) deflateEnd(&strm);
return Z_OK;
}
int unzip_buffer_to_buffer(const char *input, size_t input_size, char *output, size_t *output_size) {
int ret;
unsigned have;
z_stream strm;
unsigned char out[CHUNK_SIZE];
/* allocate inflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit(&strm);
if (ret != Z_OK) {
return ret;
}
size_t pos = 0;
size_t outpos = 0;
/* decompress until deflate stream ends or end of file */
do {
int blk = (int) ((input_size - pos) > CHUNK_SIZE ? CHUNK_SIZE : (input_size - pos));
if (blk == 0) {
break;
}
strm.avail_in = (uInt) blk;
strm.next_in = (Bytef *) (input + pos);
/* run inflate() on input until output input not full */
do {
strm.avail_out = CHUNK_SIZE;
strm.next_out = out;
ret = inflate(&strm, Z_NO_FLUSH);
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR; /* and fall through */
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void) inflateEnd(&strm);
return ret;
}
have = CHUNK_SIZE - strm.avail_out;
memcpy(&output[outpos], out, have);
outpos += have;
} while (strm.avail_out == 0);
pos += blk;
/* done when inflate() says it's done */
} while (ret != Z_STREAM_END);
/* clean up and return */
(void) inflateEnd(&strm);
return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
}
int unzip_buffer_to_file(const char *buffer, size_t size, FILE *dest) {
int ret;
unsigned have;
z_stream strm;
unsigned char out[CHUNK_SIZE];
/* allocate inflate state */
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit(&strm);
if (ret != Z_OK) {
return ret;
}
size_t pos = 0;
/* decompress until deflate stream ends or end of file */
do {
int blk = (int) ((size - pos) > CHUNK_SIZE ? CHUNK_SIZE : (size - pos));
if (blk == 0) {
break;
}
strm.avail_in = (uInt) blk;
strm.next_in = (Bytef *) (buffer + pos);
/* run inflate() on input until output buffer not full */
do {
strm.avail_out = CHUNK_SIZE;
strm.next_out = out;
ret = inflate(&strm, Z_NO_FLUSH);
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
switch (ret) {
case Z_NEED_DICT:
ret = Z_DATA_ERROR; /* and fall through */
case Z_DATA_ERROR:
case Z_MEM_ERROR:
(void) inflateEnd(&strm);
return ret;
}
have = CHUNK_SIZE - strm.avail_out;
if (fwrite(out, 1, have, dest) != have || ferror(dest)) {
(void) inflateEnd(&strm);
return Z_ERRNO;
}
} while (strm.avail_out == 0);
pos += blk;
/* done when inflate() says it's done */
} while (ret != Z_STREAM_END);
/* clean up and return */
(void) inflateEnd(&strm);
return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
}
int zip_file_to_file(const char *filename, FILE *dest, size_t *outsize) {
int _fd = open(filename, O_RDONLY);
struct stat st;
fstat(_fd, &st);
size_t _size = (size_t) st.st_size;
char *_buf = mmap(NULL, _size, PROT_READ, MAP_PRIVATE, _fd, 0);
int ret = zip_buffer_to_file(_buf, _size, dest, outsize, Z_DEFAULT_COMPRESSION);
munmap(_buf, _size);
close(_fd);
return ret;
}
const char *last_name_of(const char *path) {
int len = (int) strlen(path);
int i = len - 1;
while (i >= 0) {
if (path[i] == '/') {
return &(path[i + 1]);
}
i--;
}
return path;
}
struct zmg_dir_entry *find_dir_entry_from_dir(const char *name, struct zmg_dir_entry *dentry) {
struct zmg_dir_entry *entries = (struct zmg_dir_entry *) (((char *) dentry) + dentry->off_data);
for (int i = 0; i < dentry->n_dirs; i++) {
if (strcmp(entries[i].name, name) == 0) {
return &entries[i];
}
}
return NULL;
}
struct zmg_dir_entry *find_dir_entry_at(const char *path, struct zmg_dir_entry *root) {
if (path[0] == '/') {
path++;
}
if (path[0] == '\0') {
return root;
}
char *pathname = strdup(path);
struct zmg_dir_entry *dentry = root;
char *restpath = pathname;
char *dirname = restpath;
restpath = strchr(pathname, '/');
while (restpath != NULL) {
*restpath = '\0';
restpath++;
dentry = find_dir_entry_from_dir(dirname, dentry);
if (dentry == NULL) {
goto out;
}
dirname = restpath;
restpath = strchr(restpath, '/');
}
dentry = find_dir_entry_from_dir(dirname, dentry);
out:
free(pathname);
return dentry;
}
struct zmg_file_entry *find_file_entry_with_filename_at(const char *name, struct zmg_dir_entry *dentry) {
struct zmg_dir_entry *entries = (struct zmg_dir_entry *) (((char *) dentry) + dentry->off_data);
struct zmg_file_entry *fentry = (struct zmg_file_entry *) ((char *) (entries + dentry->n_dirs));
int nfiles = dentry->n_files;
while (nfiles-- > 0) {
if (strcmp(fentry->name, name) == 0) {
return fentry;
}
fentry = (struct zmg_file_entry *) (((char *) fentry) + fentry->off_data + fentry->data_size);
}
return NULL;
}
struct zmg_file_entry *find_file_entry_at(const char *path, struct zmg_dir_entry *direntry) {
char filepath[256];
strcpy(filepath, path);
char *filename = (char *) last_name_of(filepath);
filename[-1] = '\0';
struct zmg_dir_entry *dentry = find_dir_entry_at(filepath, direntry);
if (dentry != NULL) {
struct zmg_file_entry *fentry = find_file_entry_with_filename_at(filename, dentry);
if (fentry != NULL) {
return fentry;
}
}
return NULL;
}