Debian Codegraph @

mz_strm.c

raw symbols SCIP
1 /* mz_strm.c -- Stream interface
2 part of the minizip-ng project
3
4 Copyright (C) Nathan Moinvaziri
5 https://github.com/zlib-ng/minizip-ng
6
7 This program is distributed under the terms of the same license as zlib.
8 See the accompanying LICENSE file for the full text of the license.
9 */
10
11 #include "mz.h"
12 #include "mz_strm.h"
13
14 /***************************************************************************/
15
16 #define MZ_STREAM_FIND_SIZE (1024)
17
18 /***************************************************************************/
19
20 int32_t mz_stream_open(void *stream, const char *path, int32_t mode) {
21 mz_stream *strm = (mz_stream *)stream;
22 if (!strm || !strm->vtbl || !strm->vtbl->open)
23 return MZ_STREAM_ERROR;
24 return strm->vtbl->open(strm, path, mode);
25 }
26
27 int32_t mz_stream_is_open(void *stream) {
28 mz_stream *strm = (mz_stream *)stream;
29 if (!strm || !strm->vtbl || !strm->vtbl->is_open)
30 return MZ_STREAM_ERROR;
31 return strm->vtbl->is_open(strm);
32 }
33
34 int32_t mz_stream_read(void *stream, void *buf, int32_t size) {
35 mz_stream *strm = (mz_stream *)stream;
36 if (!strm || !strm->vtbl || !strm->vtbl->read)
37 return MZ_PARAM_ERROR;
38 if (mz_stream_is_open(stream) != MZ_OK)
39 return MZ_STREAM_ERROR;
40 return strm->vtbl->read(strm, buf, size);
41 }
42
43 static int32_t mz_stream_read_value(void *stream, uint64_t *value, int32_t len) {
44 uint8_t buf[8];
45 int32_t n = 0;
46 int32_t i = 0;
47
48 *value = 0;
49 if (mz_stream_read(stream, buf, len) == len) {
50 for (n = 0; n < len; n += 1, i += 8)
51 *value += ((uint64_t)buf[n]) << i;
52 } else if (mz_stream_error(stream))
53 return MZ_STREAM_ERROR;
54 else
55 return MZ_END_OF_STREAM;
56
57 return MZ_OK;
58 }
59
60 int32_t mz_stream_read_uint8(void *stream, uint8_t *value) {
61 int32_t err = MZ_OK;
62 uint64_t value64 = 0;
63
64 *value = 0;
65 err = mz_stream_read_value(stream, &value64, sizeof(uint8_t));
66 if (err == MZ_OK)
67 *value = (uint8_t)value64;
68 return err;
69 }
70
71 int32_t mz_stream_read_uint16(void *stream, uint16_t *value) {
72 int32_t err = MZ_OK;
73 uint64_t value64 = 0;
74
75 *value = 0;
76 err = mz_stream_read_value(stream, &value64, sizeof(uint16_t));
77 if (err == MZ_OK)
78 *value = (uint16_t)value64;
79 return err;
80 }
81
82 int32_t mz_stream_read_uint32(void *stream, uint32_t *value) {
83 int32_t err = MZ_OK;
84 uint64_t value64 = 0;
85
86 *value = 0;
87 err = mz_stream_read_value(stream, &value64, sizeof(uint32_t));
88 if (err == MZ_OK)
89 *value = (uint32_t)value64;
90 return err;
91 }
92
93 int32_t mz_stream_read_int64(void *stream, int64_t *value) {
94 return mz_stream_read_value(stream, (uint64_t *)value, sizeof(uint64_t));
95 }
96
97 int32_t mz_stream_read_uint64(void *stream, uint64_t *value) {
98 return mz_stream_read_value(stream, value, sizeof(uint64_t));
99 }
100
101 int32_t mz_stream_write(void *stream, const void *buf, int32_t size) {
102 mz_stream *strm = (mz_stream *)stream;
103 if (size == 0)
104 return size;
105 if (!strm || !strm->vtbl || !strm->vtbl->write)
106 return MZ_PARAM_ERROR;
107 if (mz_stream_is_open(stream) != MZ_OK)
108 return MZ_STREAM_ERROR;
109 return strm->vtbl->write(strm, buf, size);
110 }
111
112 static int32_t mz_stream_write_value(void *stream, uint64_t value, int32_t len) {
113 uint8_t buf[8];
114 int32_t n = 0;
115
116 for (n = 0; n < len; n += 1) {
117 buf[n] = (uint8_t)(value & 0xff);
118 value >>= 8;
119 }
120
121 if (value != 0) {
122 /* Data overflow - hack for ZIP64 (X Roche) */
123 for (n = 0; n < len; n += 1)
124 buf[n] = 0xff;
125 }
126
127 if (mz_stream_write(stream, buf, len) != len)
128 return MZ_STREAM_ERROR;
129
130 return MZ_OK;
131 }
132
133 int32_t mz_stream_write_uint8(void *stream, uint8_t value) {
134 return mz_stream_write_value(stream, value, sizeof(uint8_t));
135 }
136
137 int32_t mz_stream_write_uint16(void *stream, uint16_t value) {
138 return mz_stream_write_value(stream, value, sizeof(uint16_t));
139 }
140
141 int32_t mz_stream_write_uint32(void *stream, uint32_t value) {
142 return mz_stream_write_value(stream, value, sizeof(uint32_t));
143 }
144
145 int32_t mz_stream_write_int64(void *stream, int64_t value) {
146 return mz_stream_write_value(stream, (uint64_t)value, sizeof(uint64_t));
147 }
148
149 int32_t mz_stream_write_uint64(void *stream, uint64_t value) {
150 return mz_stream_write_value(stream, value, sizeof(uint64_t));
151 }
152
153 int32_t mz_stream_copy(void *target, void *source, int32_t len) {
154 return mz_stream_copy_stream(target, NULL, source, NULL, len);
155 }
156
157 int32_t mz_stream_copy_to_end(void *target, void *source) {
158 return mz_stream_copy_stream_to_end(target, NULL, source, NULL);
159 }
160
161 int32_t mz_stream_copy_stream(void *target, mz_stream_write_cb write_cb, void *source, mz_stream_read_cb read_cb,
162 int32_t len) {
163 uint8_t buf[16384];
164 int32_t bytes_to_copy = 0;
165 int32_t read = 0;
166 int32_t written = 0;
167
168 if (!write_cb)
169 write_cb = mz_stream_write;
170 if (!read_cb)
171 read_cb = mz_stream_read;
172
173 while (len > 0) {
174 bytes_to_copy = len;
175 if (bytes_to_copy > (int32_t)sizeof(buf))
176 bytes_to_copy = sizeof(buf);
177 read = read_cb(source, buf, bytes_to_copy);
178 if (read <= 0)
179 return MZ_STREAM_ERROR;
180 written = write_cb(target, buf, read);
181 if (written != read)
182 return MZ_STREAM_ERROR;
183 len -= read;
184 }
185
186 return MZ_OK;
187 }
188
189 int32_t mz_stream_copy_stream_to_end(void *target, mz_stream_write_cb write_cb, void *source,
190 mz_stream_read_cb read_cb) {
191 uint8_t buf[16384];
192 int32_t read = 0;
193 int32_t written = 0;
194
195 if (!write_cb)
196 write_cb = mz_stream_write;
197 if (!read_cb)
198 read_cb = mz_stream_read;
199
200 read = read_cb(source, buf, sizeof(buf));
201 while (read > 0) {
202 written = write_cb(target, buf, read);
203 if (written != read)
204 return MZ_STREAM_ERROR;
205 read = read_cb(source, buf, sizeof(buf));
206 }
207
208 if (read < 0)
209 return MZ_STREAM_ERROR;
210
211 return MZ_OK;
212 }
213
214 int64_t mz_stream_tell(void *stream) {
215 mz_stream *strm = (mz_stream *)stream;
216 if (!strm || !strm->vtbl || !strm->vtbl->tell)
217 return MZ_PARAM_ERROR;
218 if (mz_stream_is_open(stream) != MZ_OK)
219 return MZ_STREAM_ERROR;
220 return strm->vtbl->tell(strm);
221 }
222
223 int32_t mz_stream_seek(void *stream, int64_t offset, int32_t origin) {
224 mz_stream *strm = (mz_stream *)stream;
225 if (!strm || !strm->vtbl || !strm->vtbl->seek)
226 return MZ_PARAM_ERROR;
227 if (mz_stream_is_open(stream) != MZ_OK)
228 return MZ_STREAM_ERROR;
229 if (origin == MZ_SEEK_SET && offset < 0)
230 return MZ_SEEK_ERROR;
231 return strm->vtbl->seek(strm, offset, origin);
232 }
233
234 int32_t mz_stream_find(void *stream, const void *find, int32_t find_size, int64_t max_seek, int64_t *position) {
235 uint8_t buf[MZ_STREAM_FIND_SIZE];
236 int32_t buf_pos = 0;
237 int32_t read_size = sizeof(buf);
238 int32_t read = 0;
239 int64_t read_pos = 0;
240 int64_t start_pos = 0;
241 int64_t disk_pos = 0;
242 int32_t i = 0;
243 uint8_t first = 1;
244 int32_t err = MZ_OK;
245
246 if (!stream || !find || !position)
247 return MZ_PARAM_ERROR;
248 if (find_size < 0 || find_size >= (int32_t)sizeof(buf))
249 return MZ_PARAM_ERROR;
250
251 *position = -1;
252
253 start_pos = mz_stream_tell(stream);
254
255 while (read_pos < max_seek) {
256 if (read_size > (int32_t)(max_seek - read_pos - buf_pos) &&
257 (max_seek - read_pos - buf_pos) < (int64_t)sizeof(buf)) {
258 read_size = (int32_t)(max_seek - read_pos - buf_pos);
259 }
260
261 read = mz_stream_read(stream, buf + buf_pos, read_size);
262 if ((read <= 0) || (read + buf_pos < find_size))
263 break;
264
265 for (i = 0; i <= read + buf_pos - find_size; i += 1) {
266 if (memcmp(&buf[i], find, find_size) != 0)
267 continue;
268
269 disk_pos = mz_stream_tell(stream);
270
271 /* Seek to position on disk where the data was found */
272 err = mz_stream_seek(stream, disk_pos - ((int64_t)read + buf_pos - i), MZ_SEEK_SET);
273 if (err != MZ_OK)
274 return MZ_EXIST_ERROR;
275
276 *position = start_pos + read_pos + i;
277 return MZ_OK;
278 }
279
280 if (first) {
281 read -= find_size;
282 read_size -= find_size;
283 buf_pos = find_size;
284 first = 0;
285 }
286
287 memmove(buf, buf + read, find_size);
288 read_pos += read;
289 }
290
291 return MZ_EXIST_ERROR;
292 }
293
294 int32_t mz_stream_find_reverse(void *stream, const void *find, int32_t find_size, int64_t max_seek, int64_t *position) {
295 uint8_t buf[MZ_STREAM_FIND_SIZE];
296 int32_t buf_pos = 0;
297 int32_t read_size = MZ_STREAM_FIND_SIZE;
298 int64_t read_pos = 0;
299 int32_t read = 0;
300 int64_t start_pos = 0;
301 int64_t disk_pos = 0;
302 uint8_t first = 1;
303 int32_t i = 0;
304 int32_t err = MZ_OK;
305
306 if (!stream || !find || !position)
307 return MZ_PARAM_ERROR;
308 if (find_size < 0 || find_size >= (int32_t)sizeof(buf))
309 return MZ_PARAM_ERROR;
310
311 *position = -1;
312
313 start_pos = mz_stream_tell(stream);
314
315 while (read_pos < max_seek) {
316 if (read_size > (int32_t)(max_seek - read_pos) && (max_seek - read_pos) < (int64_t)sizeof(buf))
317 read_size = (int32_t)(max_seek - read_pos);
318
319 if (mz_stream_seek(stream, start_pos - (read_pos + read_size), MZ_SEEK_SET) != MZ_OK)
320 break;
321 read = mz_stream_read(stream, buf, read_size);
322 if ((read <= 0) || (read + buf_pos < find_size))
323 break;
324 if (read + buf_pos < MZ_STREAM_FIND_SIZE)
325 memmove(buf + MZ_STREAM_FIND_SIZE - (read + buf_pos), buf, read);
326
327 for (i = find_size; i <= (read + buf_pos); i += 1) {
328 if (memcmp(&buf[MZ_STREAM_FIND_SIZE - i], find, find_size) != 0)
329 continue;
330
331 disk_pos = mz_stream_tell(stream);
332
333 /* Seek to position on disk where the data was found */
334 err = mz_stream_seek(stream, disk_pos + buf_pos - i, MZ_SEEK_SET);
335 if (err != MZ_OK)
336 return MZ_EXIST_ERROR;
337
338 *position = start_pos - (read_pos - buf_pos + i);
339 return MZ_OK;
340 }
341
342 if (first) {
343 read -= find_size;
344 read_size -= find_size;
345 buf_pos = find_size;
346 first = 0;
347 }
348
349 if (read == 0)
350 break;
351
352 memmove(buf + read_size, buf, find_size);
353 read_pos += read;
354 }
355
356 return MZ_EXIST_ERROR;
357 }
358
359 int32_t mz_stream_close(void *stream) {
360 mz_stream *strm = (mz_stream *)stream;
361 if (!strm || !strm->vtbl || !strm->vtbl->close)
362 return MZ_PARAM_ERROR;
363 if (mz_stream_is_open(stream) != MZ_OK)
364 return MZ_STREAM_ERROR;
365 return strm->vtbl->close(strm);
366 }
367
368 int32_t mz_stream_error(void *stream) {
369 mz_stream *strm = (mz_stream *)stream;
370 if (!strm || !strm->vtbl || !strm->vtbl->error)
371 return MZ_PARAM_ERROR;
372 return strm->vtbl->error(strm);
373 }
374
375 int32_t mz_stream_set_base(void *stream, void *base) {
376 mz_stream *strm = (mz_stream *)stream;
377 strm->base = (mz_stream *)base;
378 return MZ_OK;
379 }
380
381 void *mz_stream_get_interface(void *stream) {
382 mz_stream *strm = (mz_stream *)stream;
383 if (!strm || !strm->vtbl)
384 return NULL;
385 return (void *)strm->vtbl;
386 }
387
388 int32_t mz_stream_get_prop_int64(void *stream, int32_t prop, int64_t *value) {
389 mz_stream *strm = (mz_stream *)stream;
390 if (!strm || !strm->vtbl || !strm->vtbl->get_prop_int64)
391 return MZ_PARAM_ERROR;
392 return strm->vtbl->get_prop_int64(stream, prop, value);
393 }
394
395 int32_t mz_stream_set_prop_int64(void *stream, int32_t prop, int64_t value) {
396 mz_stream *strm = (mz_stream *)stream;
397 if (!strm || !strm->vtbl || !strm->vtbl->set_prop_int64)
398 return MZ_PARAM_ERROR;
399 return strm->vtbl->set_prop_int64(stream, prop, value);
400 }
401
402 void *mz_stream_create(mz_stream_vtbl *vtbl) {
403 if (!vtbl || !vtbl->create)
404 return NULL;
405 return vtbl->create();
406 }
407
408 void mz_stream_delete(void **stream) {
409 mz_stream *strm = NULL;
410 if (!stream)
411 return;
412 strm = (mz_stream *)*stream;
413 if (strm && strm->vtbl && strm->vtbl->destroy)
414 strm->vtbl->destroy(stream);
415 *stream = NULL;
416 }
417
418 /***************************************************************************/
419
420 typedef struct mz_stream_raw_s {
421 mz_stream stream;
422 int64_t total_in;
423 int64_t total_out;
424 int64_t max_total_in;
425 } mz_stream_raw;
426
427 /***************************************************************************/
428
429 int32_t mz_stream_raw_open(void *stream, const char *path, int32_t mode) {
430 MZ_UNUSED(stream);
431 MZ_UNUSED(path);
432 MZ_UNUSED(mode);
433
434 return MZ_OK;
435 }
436
437 int32_t mz_stream_raw_is_open(void *stream) {
438 mz_stream_raw *raw = (mz_stream_raw *)stream;
439 return mz_stream_is_open(raw->stream.base);
440 }
441
442 int32_t mz_stream_raw_read(void *stream, void *buf, int32_t size) {
443 mz_stream_raw *raw = (mz_stream_raw *)stream;
444 int32_t bytes_to_read = size;
445 int32_t read = 0;
446
447 if (raw->max_total_in > 0) {
448 if ((int64_t)bytes_to_read > (raw->max_total_in - raw->total_in))
449 bytes_to_read = (int32_t)(raw->max_total_in - raw->total_in);
450 }
451
452 read = mz_stream_read(raw->stream.base, buf, bytes_to_read);
453
454 if (read > 0) {
455 raw->total_in += read;
456 raw->total_out += read;
457 }
458
459 return read;
460 }
461
462 int32_t mz_stream_raw_write(void *stream, const void *buf, int32_t size) {
463 mz_stream_raw *raw = (mz_stream_raw *)stream;
464 int32_t written = 0;
465
466 written = mz_stream_write(raw->stream.base, buf, size);
467
468 if (written > 0) {
469 raw->total_out += written;
470 raw->total_in += written;
471 }
472
473 return written;
474 }
475
476 int64_t mz_stream_raw_tell(void *stream) {
477 mz_stream_raw *raw = (mz_stream_raw *)stream;
478 return mz_stream_tell(raw->stream.base);
479 }
480
481 int32_t mz_stream_raw_seek(void *stream, int64_t offset, int32_t origin) {
482 mz_stream_raw *raw = (mz_stream_raw *)stream;
483 return mz_stream_seek(raw->stream.base, offset, origin);
484 }
485
486 int32_t mz_stream_raw_close(void *stream) {
487 MZ_UNUSED(stream);
488 return MZ_OK;
489 }
490
491 int32_t mz_stream_raw_error(void *stream) {
492 mz_stream_raw *raw = (mz_stream_raw *)stream;
493 return mz_stream_error(raw->stream.base);
494 }
495
496 int32_t mz_stream_raw_get_prop_int64(void *stream, int32_t prop, int64_t *value) {
497 mz_stream_raw *raw = (mz_stream_raw *)stream;
498 switch (prop) {
499 case MZ_STREAM_PROP_TOTAL_IN:
500 *value = raw->total_in;
501 return MZ_OK;
502 case MZ_STREAM_PROP_TOTAL_OUT:
503 *value = raw->total_out;
504 return MZ_OK;
505 }
506 return MZ_EXIST_ERROR;
507 }
508
509 int32_t mz_stream_raw_set_prop_int64(void *stream, int32_t prop, int64_t value) {
510 mz_stream_raw *raw = (mz_stream_raw *)stream;
511 switch (prop) {
512 case MZ_STREAM_PROP_TOTAL_IN_MAX:
513 raw->max_total_in = value;
514 return MZ_OK;
515 }
516 return MZ_EXIST_ERROR;
517 }
518
519 /***************************************************************************/
520
521 static mz_stream_vtbl mz_stream_raw_vtbl = {
522 mz_stream_raw_open, mz_stream_raw_is_open, mz_stream_raw_read, mz_stream_raw_write,
523 mz_stream_raw_tell, mz_stream_raw_seek, mz_stream_raw_close, mz_stream_raw_error,
524 mz_stream_raw_create, mz_stream_raw_delete, mz_stream_raw_get_prop_int64, mz_stream_raw_set_prop_int64};
525
526 /***************************************************************************/
527
528 void *mz_stream_raw_create(void) {
529 mz_stream_raw *raw = (mz_stream_raw *)calloc(1, sizeof(mz_stream_raw));
530 if (raw)
531 raw->stream.vtbl = &mz_stream_raw_vtbl;
532 return raw;
533 }
534
535 void mz_stream_raw_delete(void **stream) {
536 mz_stream_raw *raw = NULL;
537 if (!stream)
538 return;
539 raw = (mz_stream_raw *)*stream;
540 free(raw);
541 *stream = NULL;
542 }