-
-
Notifications
You must be signed in to change notification settings - Fork 434
/
mz_strm.c
543 lines (438 loc) · 15.5 KB
/
mz_strm.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
/* mz_strm.c -- Stream interface
part of the minizip-ng project
Copyright (C) Nathan Moinvaziri
https://github.com/zlib-ng/minizip-ng
This program is distributed under the terms of the same license as zlib.
See the accompanying LICENSE file for the full text of the license.
*/
#include "mz.h"
#include "mz_strm.h"
/***************************************************************************/
#define MZ_STREAM_FIND_SIZE (1024)
/***************************************************************************/
int32_t mz_stream_open(void *stream, const char *path, int32_t mode) {
mz_stream *strm = (mz_stream *)stream;
if (!strm || !strm->vtbl || !strm->vtbl->open)
return MZ_STREAM_ERROR;
return strm->vtbl->open(strm, path, mode);
}
int32_t mz_stream_is_open(void *stream) {
mz_stream *strm = (mz_stream *)stream;
if (!strm || !strm->vtbl || !strm->vtbl->is_open)
return MZ_STREAM_ERROR;
return strm->vtbl->is_open(strm);
}
int32_t mz_stream_read(void *stream, void *buf, int32_t size) {
mz_stream *strm = (mz_stream *)stream;
if (!strm || !strm->vtbl || !strm->vtbl->read)
return MZ_PARAM_ERROR;
if (mz_stream_is_open(stream) != MZ_OK)
return MZ_STREAM_ERROR;
return strm->vtbl->read(strm, buf, size);
}
static int32_t mz_stream_read_value(void *stream, uint64_t *value, int32_t len) {
uint8_t buf[8];
int32_t n = 0;
int32_t i = 0;
*value = 0;
if (mz_stream_read(stream, buf, len) == len) {
for (n = 0; n < len; n += 1, i += 8)
*value += ((uint64_t)buf[n]) << i;
} else if (mz_stream_error(stream))
return MZ_STREAM_ERROR;
else
return MZ_END_OF_STREAM;
return MZ_OK;
}
int32_t mz_stream_read_uint8(void *stream, uint8_t *value) {
int32_t err = MZ_OK;
uint64_t value64 = 0;
*value = 0;
err = mz_stream_read_value(stream, &value64, sizeof(uint8_t));
if (err == MZ_OK)
*value = (uint8_t)value64;
return err;
}
int32_t mz_stream_read_uint16(void *stream, uint16_t *value) {
int32_t err = MZ_OK;
uint64_t value64 = 0;
*value = 0;
err = mz_stream_read_value(stream, &value64, sizeof(uint16_t));
if (err == MZ_OK)
*value = (uint16_t)value64;
return err;
}
int32_t mz_stream_read_uint32(void *stream, uint32_t *value) {
int32_t err = MZ_OK;
uint64_t value64 = 0;
*value = 0;
err = mz_stream_read_value(stream, &value64, sizeof(uint32_t));
if (err == MZ_OK)
*value = (uint32_t)value64;
return err;
}
int32_t mz_stream_read_int64(void *stream, int64_t *value) {
return mz_stream_read_value(stream, (uint64_t *)value, sizeof(uint64_t));
}
int32_t mz_stream_read_uint64(void *stream, uint64_t *value) {
return mz_stream_read_value(stream, value, sizeof(uint64_t));
}
int32_t mz_stream_write(void *stream, const void *buf, int32_t size) {
mz_stream *strm = (mz_stream *)stream;
if (size == 0)
return size;
if (!strm || !strm->vtbl || !strm->vtbl->write)
return MZ_PARAM_ERROR;
if (mz_stream_is_open(stream) != MZ_OK)
return MZ_STREAM_ERROR;
return strm->vtbl->write(strm, buf, size);
}
static int32_t mz_stream_write_value(void *stream, uint64_t value, int32_t len) {
uint8_t buf[8];
int32_t n = 0;
for (n = 0; n < len; n += 1) {
buf[n] = (uint8_t)(value & 0xff);
value >>= 8;
}
if (value != 0) {
/* Data overflow - hack for ZIP64 (X Roche) */
for (n = 0; n < len; n += 1)
buf[n] = 0xff;
}
if (mz_stream_write(stream, buf, len) != len)
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_write_uint8(void *stream, uint8_t value) {
return mz_stream_write_value(stream, value, sizeof(uint8_t));
}
int32_t mz_stream_write_uint16(void *stream, uint16_t value) {
return mz_stream_write_value(stream, value, sizeof(uint16_t));
}
int32_t mz_stream_write_uint32(void *stream, uint32_t value) {
return mz_stream_write_value(stream, value, sizeof(uint32_t));
}
int32_t mz_stream_write_int64(void *stream, int64_t value) {
return mz_stream_write_value(stream, (uint64_t)value, sizeof(uint64_t));
}
int32_t mz_stream_write_uint64(void *stream, uint64_t value) {
return mz_stream_write_value(stream, value, sizeof(uint64_t));
}
int32_t mz_stream_copy(void *target, void *source, int32_t len) {
return mz_stream_copy_stream(target, NULL, source, NULL, len);
}
int32_t mz_stream_copy_to_end(void *target, void *source) {
return mz_stream_copy_stream_to_end(target, NULL, source, NULL);
}
int32_t mz_stream_copy_stream(void *target, mz_stream_write_cb write_cb, void *source, mz_stream_read_cb read_cb,
int32_t len) {
uint8_t buf[16384];
int32_t bytes_to_copy = 0;
int32_t read = 0;
int32_t written = 0;
if (!write_cb)
write_cb = mz_stream_write;
if (!read_cb)
read_cb = mz_stream_read;
while (len > 0) {
bytes_to_copy = len;
if (bytes_to_copy > (int32_t)sizeof(buf))
bytes_to_copy = sizeof(buf);
read = read_cb(source, buf, bytes_to_copy);
if (read <= 0)
return MZ_STREAM_ERROR;
written = write_cb(target, buf, read);
if (written != read)
return MZ_STREAM_ERROR;
len -= read;
}
return MZ_OK;
}
int32_t mz_stream_copy_stream_to_end(void *target, mz_stream_write_cb write_cb, void *source,
mz_stream_read_cb read_cb) {
uint8_t buf[16384];
int32_t read = 0;
int32_t written = 0;
if (!write_cb)
write_cb = mz_stream_write;
if (!read_cb)
read_cb = mz_stream_read;
read = read_cb(source, buf, sizeof(buf));
while (read > 0) {
written = write_cb(target, buf, read);
if (written != read)
return MZ_STREAM_ERROR;
read = read_cb(source, buf, sizeof(buf));
}
if (read < 0)
return MZ_STREAM_ERROR;
return MZ_OK;
}
int64_t mz_stream_tell(void *stream) {
mz_stream *strm = (mz_stream *)stream;
if (!strm || !strm->vtbl || !strm->vtbl->tell)
return MZ_PARAM_ERROR;
if (mz_stream_is_open(stream) != MZ_OK)
return MZ_STREAM_ERROR;
return strm->vtbl->tell(strm);
}
int32_t mz_stream_seek(void *stream, int64_t offset, int32_t origin) {
mz_stream *strm = (mz_stream *)stream;
if (!strm || !strm->vtbl || !strm->vtbl->seek)
return MZ_PARAM_ERROR;
if (mz_stream_is_open(stream) != MZ_OK)
return MZ_STREAM_ERROR;
if (origin == MZ_SEEK_SET && offset < 0)
return MZ_SEEK_ERROR;
return strm->vtbl->seek(strm, offset, origin);
}
int32_t mz_stream_find(void *stream, const void *find, int32_t find_size, int64_t max_seek, int64_t *position) {
uint8_t buf[MZ_STREAM_FIND_SIZE];
int32_t buf_pos = 0;
int32_t read_size = sizeof(buf);
int32_t read = 0;
int64_t read_pos = 0;
int64_t start_pos = 0;
int64_t disk_pos = 0;
int32_t i = 0;
uint8_t first = 1;
int32_t err = MZ_OK;
if (!stream || !find || !position)
return MZ_PARAM_ERROR;
if (find_size < 0 || find_size >= (int32_t)sizeof(buf))
return MZ_PARAM_ERROR;
*position = -1;
start_pos = mz_stream_tell(stream);
while (read_pos < max_seek) {
if (read_size > (int32_t)(max_seek - read_pos - buf_pos) &&
(max_seek - read_pos - buf_pos) < (int64_t)sizeof(buf)) {
read_size = (int32_t)(max_seek - read_pos - buf_pos);
}
read = mz_stream_read(stream, buf + buf_pos, read_size);
if ((read <= 0) || (read + buf_pos < find_size))
break;
for (i = 0; i <= read + buf_pos - find_size; i += 1) {
if (memcmp(&buf[i], find, find_size) != 0)
continue;
disk_pos = mz_stream_tell(stream);
/* Seek to position on disk where the data was found */
err = mz_stream_seek(stream, disk_pos - ((int64_t)read + buf_pos - i), MZ_SEEK_SET);
if (err != MZ_OK)
return MZ_EXIST_ERROR;
*position = start_pos + read_pos + i;
return MZ_OK;
}
if (first) {
read -= find_size;
read_size -= find_size;
buf_pos = find_size;
first = 0;
}
memmove(buf, buf + read, find_size);
read_pos += read;
}
return MZ_EXIST_ERROR;
}
int32_t mz_stream_find_reverse(void *stream, const void *find, int32_t find_size, int64_t max_seek, int64_t *position) {
uint8_t buf[MZ_STREAM_FIND_SIZE];
int32_t buf_pos = 0;
int32_t read_size = MZ_STREAM_FIND_SIZE;
int64_t read_pos = 0;
int32_t read = 0;
int64_t start_pos = 0;
int64_t disk_pos = 0;
uint8_t first = 1;
int32_t i = 0;
int32_t err = MZ_OK;
if (!stream || !find || !position)
return MZ_PARAM_ERROR;
if (find_size < 0 || find_size >= (int32_t)sizeof(buf))
return MZ_PARAM_ERROR;
*position = -1;
start_pos = mz_stream_tell(stream);
while (read_pos < max_seek) {
if (read_size > (int32_t)(max_seek - read_pos) && (max_seek - read_pos) < (int64_t)sizeof(buf))
read_size = (int32_t)(max_seek - read_pos);
if (mz_stream_seek(stream, start_pos - (read_pos + read_size), MZ_SEEK_SET) != MZ_OK)
break;
read = mz_stream_read(stream, buf, read_size);
if ((read <= 0) || (read + buf_pos < find_size))
break;
if (read + buf_pos < MZ_STREAM_FIND_SIZE)
memmove(buf + MZ_STREAM_FIND_SIZE - (read + buf_pos), buf, read);
for (i = find_size; i <= (read + buf_pos); i += 1) {
if (memcmp(&buf[MZ_STREAM_FIND_SIZE - i], find, find_size) != 0)
continue;
disk_pos = mz_stream_tell(stream);
/* Seek to position on disk where the data was found */
err = mz_stream_seek(stream, disk_pos + buf_pos - i, MZ_SEEK_SET);
if (err != MZ_OK)
return MZ_EXIST_ERROR;
*position = start_pos - (read_pos - buf_pos + i);
return MZ_OK;
}
if (first) {
read -= find_size;
read_size -= find_size;
buf_pos = find_size;
first = 0;
}
if (read == 0)
break;
memmove(buf + read_size, buf, find_size);
read_pos += read;
}
return MZ_EXIST_ERROR;
}
int32_t mz_stream_close(void *stream) {
mz_stream *strm = (mz_stream *)stream;
if (!strm || !strm->vtbl || !strm->vtbl->close)
return MZ_PARAM_ERROR;
if (mz_stream_is_open(stream) != MZ_OK)
return MZ_STREAM_ERROR;
return strm->vtbl->close(strm);
}
int32_t mz_stream_error(void *stream) {
mz_stream *strm = (mz_stream *)stream;
if (!strm || !strm->vtbl || !strm->vtbl->error)
return MZ_PARAM_ERROR;
return strm->vtbl->error(strm);
}
int32_t mz_stream_set_base(void *stream, void *base) {
mz_stream *strm = (mz_stream *)stream;
strm->base = (mz_stream *)base;
return MZ_OK;
}
void *mz_stream_get_interface(void *stream) {
mz_stream *strm = (mz_stream *)stream;
if (!strm || !strm->vtbl)
return NULL;
return (void *)strm->vtbl;
}
int32_t mz_stream_get_prop_int64(void *stream, int32_t prop, int64_t *value) {
mz_stream *strm = (mz_stream *)stream;
if (!strm || !strm->vtbl || !strm->vtbl->get_prop_int64)
return MZ_PARAM_ERROR;
return strm->vtbl->get_prop_int64(stream, prop, value);
}
int32_t mz_stream_set_prop_int64(void *stream, int32_t prop, int64_t value) {
mz_stream *strm = (mz_stream *)stream;
if (!strm || !strm->vtbl || !strm->vtbl->set_prop_int64)
return MZ_PARAM_ERROR;
return strm->vtbl->set_prop_int64(stream, prop, value);
}
void *mz_stream_create(mz_stream_vtbl *vtbl) {
if (!vtbl || !vtbl->create)
return NULL;
return vtbl->create();
}
void mz_stream_delete(void **stream) {
mz_stream *strm = NULL;
if (!stream)
return;
strm = (mz_stream *)*stream;
if (strm && strm->vtbl && strm->vtbl->destroy)
strm->vtbl->destroy(stream);
*stream = NULL;
}
/***************************************************************************/
typedef struct mz_stream_raw_s {
mz_stream stream;
int64_t total_in;
int64_t total_out;
int64_t max_total_in;
} mz_stream_raw;
/***************************************************************************/
int32_t mz_stream_raw_open(void *stream, const char *path, int32_t mode) {
MZ_UNUSED(stream);
MZ_UNUSED(path);
MZ_UNUSED(mode);
return MZ_OK;
}
int32_t mz_stream_raw_is_open(void *stream) {
mz_stream_raw *raw = (mz_stream_raw *)stream;
return mz_stream_is_open(raw->stream.base);
}
int32_t mz_stream_raw_read(void *stream, void *buf, int32_t size) {
mz_stream_raw *raw = (mz_stream_raw *)stream;
int32_t bytes_to_read = size;
int32_t read = 0;
if (raw->max_total_in > 0) {
if ((int64_t)bytes_to_read > (raw->max_total_in - raw->total_in))
bytes_to_read = (int32_t)(raw->max_total_in - raw->total_in);
}
read = mz_stream_read(raw->stream.base, buf, bytes_to_read);
if (read > 0) {
raw->total_in += read;
raw->total_out += read;
}
return read;
}
int32_t mz_stream_raw_write(void *stream, const void *buf, int32_t size) {
mz_stream_raw *raw = (mz_stream_raw *)stream;
int32_t written = 0;
written = mz_stream_write(raw->stream.base, buf, size);
if (written > 0) {
raw->total_out += written;
raw->total_in += written;
}
return written;
}
int64_t mz_stream_raw_tell(void *stream) {
mz_stream_raw *raw = (mz_stream_raw *)stream;
return mz_stream_tell(raw->stream.base);
}
int32_t mz_stream_raw_seek(void *stream, int64_t offset, int32_t origin) {
mz_stream_raw *raw = (mz_stream_raw *)stream;
return mz_stream_seek(raw->stream.base, offset, origin);
}
int32_t mz_stream_raw_close(void *stream) {
MZ_UNUSED(stream);
return MZ_OK;
}
int32_t mz_stream_raw_error(void *stream) {
mz_stream_raw *raw = (mz_stream_raw *)stream;
return mz_stream_error(raw->stream.base);
}
int32_t mz_stream_raw_get_prop_int64(void *stream, int32_t prop, int64_t *value) {
mz_stream_raw *raw = (mz_stream_raw *)stream;
switch (prop) {
case MZ_STREAM_PROP_TOTAL_IN:
*value = raw->total_in;
return MZ_OK;
case MZ_STREAM_PROP_TOTAL_OUT:
*value = raw->total_out;
return MZ_OK;
}
return MZ_EXIST_ERROR;
}
int32_t mz_stream_raw_set_prop_int64(void *stream, int32_t prop, int64_t value) {
mz_stream_raw *raw = (mz_stream_raw *)stream;
switch (prop) {
case MZ_STREAM_PROP_TOTAL_IN_MAX:
raw->max_total_in = value;
return MZ_OK;
}
return MZ_EXIST_ERROR;
}
/***************************************************************************/
static mz_stream_vtbl mz_stream_raw_vtbl = {
mz_stream_raw_open, mz_stream_raw_is_open, mz_stream_raw_read, mz_stream_raw_write,
mz_stream_raw_tell, mz_stream_raw_seek, mz_stream_raw_close, mz_stream_raw_error,
mz_stream_raw_create, mz_stream_raw_delete, mz_stream_raw_get_prop_int64, mz_stream_raw_set_prop_int64};
/***************************************************************************/
void *mz_stream_raw_create(void) {
mz_stream_raw *raw = (mz_stream_raw *)calloc(1, sizeof(mz_stream_raw));
if (raw)
raw->stream.vtbl = &mz_stream_raw_vtbl;
return raw;
}
void mz_stream_raw_delete(void **stream) {
mz_stream_raw *raw = NULL;
if (!stream)
return;
raw = (mz_stream_raw *)*stream;
if (raw)
free(raw);
*stream = NULL;
}