forked from zlib-ng/minizip-ng
-
Notifications
You must be signed in to change notification settings - Fork 2
/
mz_strm_buf.c
378 lines (294 loc) · 12.8 KB
/
mz_strm_buf.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
/* mz_strm_buf.c -- Stream for buffering reads/writes
part of the minizip-ng project
This version of ioapi is designed to buffer IO.
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"
#include "mz_strm_buf.h"
/***************************************************************************/
static mz_stream_vtbl mz_stream_buffered_vtbl = {
mz_stream_buffered_open,
mz_stream_buffered_is_open,
mz_stream_buffered_read,
mz_stream_buffered_write,
mz_stream_buffered_tell,
mz_stream_buffered_seek,
mz_stream_buffered_close,
mz_stream_buffered_error,
mz_stream_buffered_create,
mz_stream_buffered_delete,
NULL,
NULL
};
/***************************************************************************/
typedef struct mz_stream_buffered_s {
mz_stream stream;
int32_t error;
char readbuf[INT16_MAX];
int32_t readbuf_len;
int32_t readbuf_pos;
int32_t readbuf_hits;
int32_t readbuf_misses;
char writebuf[INT16_MAX];
int32_t writebuf_len;
int32_t writebuf_pos;
int32_t writebuf_hits;
int32_t writebuf_misses;
int64_t position;
} mz_stream_buffered;
/***************************************************************************/
#if 0
# define mz_stream_buffered_print printf
#else
# define mz_stream_buffered_print(fmt,...)
#endif
/***************************************************************************/
static int32_t mz_stream_buffered_reset(void *stream) {
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
buffered->readbuf_len = 0;
buffered->readbuf_pos = 0;
buffered->writebuf_len = 0;
buffered->writebuf_pos = 0;
buffered->position = 0;
return MZ_OK;
}
int32_t mz_stream_buffered_open(void *stream, const char *path, int32_t mode) {
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
mz_stream_buffered_print("Buffered - Open (mode %" PRId32 ")\n", mode);
mz_stream_buffered_reset(buffered);
return mz_stream_open(buffered->stream.base, path, mode);
}
int32_t mz_stream_buffered_is_open(void *stream) {
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
return mz_stream_is_open(buffered->stream.base);
}
static int32_t mz_stream_buffered_flush(void *stream, int32_t *written) {
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
int32_t total_bytes_written = 0;
int32_t bytes_to_write = buffered->writebuf_len;
int32_t bytes_left_to_write = buffered->writebuf_len;
int32_t bytes_written = 0;
*written = 0;
while (bytes_left_to_write > 0) {
bytes_written = mz_stream_write(buffered->stream.base,
buffered->writebuf + (bytes_to_write - bytes_left_to_write), bytes_left_to_write);
if (bytes_written != bytes_left_to_write)
return MZ_WRITE_ERROR;
buffered->writebuf_misses += 1;
mz_stream_buffered_print("Buffered - Write flush (%" PRId32 ":%" PRId32 " len %" PRId32 ")\n",
bytes_to_write, bytes_left_to_write, buffered->writebuf_len);
total_bytes_written += bytes_written;
bytes_left_to_write -= bytes_written;
buffered->position += bytes_written;
}
buffered->writebuf_len = 0;
buffered->writebuf_pos = 0;
*written = total_bytes_written;
return MZ_OK;
}
int32_t mz_stream_buffered_read(void *stream, void *buf, int32_t size) {
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
int32_t buf_len = 0;
int32_t bytes_to_read = 0;
int32_t bytes_to_copy = 0;
int32_t bytes_left_to_read = size;
int32_t bytes_read = 0;
int32_t bytes_flushed = 0;
mz_stream_buffered_print("Buffered - Read (size %" PRId32 " pos %" PRId64 ")\n", size, buffered->position);
if (buffered->writebuf_len > 0) {
int64_t position = buffered->position + buffered->writebuf_pos
mz_stream_buffered_print("Buffered - Switch from write to read, flushing (pos %" PRId64 ")\n", position);
mz_stream_buffered_flush(stream, &bytes_flushed);
mz_stream_buffered_seek(stream, position, MZ_SEEK_SET);
}
while (bytes_left_to_read > 0) {
if ((buffered->readbuf_len == 0) || (buffered->readbuf_pos == buffered->readbuf_len)) {
if (buffered->readbuf_len == sizeof(buffered->readbuf)) {
buffered->readbuf_pos = 0;
buffered->readbuf_len = 0;
}
bytes_to_read = (int32_t)sizeof(buffered->readbuf) - (buffered->readbuf_len - buffered->readbuf_pos);
bytes_read = mz_stream_read(buffered->stream.base, buffered->readbuf + buffered->readbuf_pos, bytes_to_read);
if (bytes_read < 0)
return bytes_read;
buffered->readbuf_misses += 1;
buffered->readbuf_len += bytes_read;
buffered->position += bytes_read;
mz_stream_buffered_print("Buffered - Filled (read %" PRId32 "/%" PRId32 " buf %" PRId32 ":%" PRId32 " pos %" PRId64 ")\n",
bytes_read, bytes_to_read, buffered->readbuf_pos, buffered->readbuf_len, buffered->position);
if (bytes_read == 0)
break;
}
if ((buffered->readbuf_len - buffered->readbuf_pos) > 0) {
bytes_to_copy = buffered->readbuf_len - buffered->readbuf_pos;
if (bytes_to_copy > bytes_left_to_read)
bytes_to_copy = bytes_left_to_read;
memcpy((char *)buf + buf_len, buffered->readbuf + buffered->readbuf_pos, bytes_to_copy);
buf_len += bytes_to_copy;
bytes_left_to_read -= bytes_to_copy;
buffered->readbuf_hits += 1;
buffered->readbuf_pos += bytes_to_copy;
mz_stream_buffered_print("Buffered - Emptied (copied %" PRId32 " remaining %" PRId32 " buf %" PRId32 ":%" PRId32 " pos %" PRId64 ")\n",
bytes_to_copy, bytes_left_to_read, buffered->readbuf_pos, buffered->readbuf_len, buffered->position);
}
}
return size - bytes_left_to_read;
}
int32_t mz_stream_buffered_write(void *stream, const void *buf, int32_t size) {
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
int32_t bytes_to_write = size;
int32_t bytes_left_to_write = size;
int32_t bytes_to_copy = 0;
int32_t bytes_used = 0;
int32_t bytes_flushed = 0;
int32_t err = MZ_OK;
mz_stream_buffered_print("Buffered - Write (size %" PRId32 " len %" PRId32 " pos %" PRId64 ")\n",
size, buffered->writebuf_len, buffered->position);
if (buffered->readbuf_len > 0) {
buffered->position -= buffered->readbuf_len;
buffered->position += buffered->readbuf_pos;
buffered->readbuf_len = 0;
buffered->readbuf_pos = 0;
mz_stream_buffered_print("Buffered - Switch from read to write (pos %" PRId64 ")\n", buffered->position);
err = mz_stream_seek(buffered->stream.base, buffered->position, MZ_SEEK_SET);
if (err != MZ_OK)
return err;
}
while (bytes_left_to_write > 0) {
bytes_used = buffered->writebuf_len;
if (bytes_used > buffered->writebuf_pos)
bytes_used = buffered->writebuf_pos;
bytes_to_copy = (int32_t)sizeof(buffered->writebuf) - bytes_used;
if (bytes_to_copy > bytes_left_to_write)
bytes_to_copy = bytes_left_to_write;
if (bytes_to_copy == 0) {
err = mz_stream_buffered_flush(stream, &bytes_flushed);
if (err != MZ_OK)
return err;
if (bytes_flushed == 0)
return 0;
continue;
}
memcpy(buffered->writebuf + buffered->writebuf_pos,
(const char *)buf + (bytes_to_write - bytes_left_to_write), bytes_to_copy);
mz_stream_buffered_print("Buffered - Write copy (remaining %" PRId32 " write %" PRId32 ":%" PRId32 " len %" PRId32 ")\n",
bytes_to_copy, bytes_to_write, bytes_left_to_write, buffered->writebuf_len);
bytes_left_to_write -= bytes_to_copy;
buffered->writebuf_pos += bytes_to_copy;
buffered->writebuf_hits += 1;
if (buffered->writebuf_pos > buffered->writebuf_len)
buffered->writebuf_len += buffered->writebuf_pos - buffered->writebuf_len;
}
return size - bytes_left_to_write;
}
int64_t mz_stream_buffered_tell(void *stream) {
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
int64_t position = mz_stream_tell(buffered->stream.base);
buffered->position = position;
mz_stream_buffered_print("Buffered - Tell (pos %" PRId64 " readpos %" PRId32 " writepos %" PRId32 ")\n",
buffered->position, buffered->readbuf_pos, buffered->writebuf_pos);
if (buffered->readbuf_len > 0)
position -= ((int64_t)buffered->readbuf_len - buffered->readbuf_pos);
if (buffered->writebuf_len > 0)
position += buffered->writebuf_pos;
return position;
}
int32_t mz_stream_buffered_seek(void *stream, int64_t offset, int32_t origin) {
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
int32_t bytes_flushed = 0;
int32_t err = MZ_OK;
mz_stream_buffered_print("Buffered - Seek (origin %" PRId32 " offset %" PRId64 " pos %" PRId64 ")\n",
origin, offset, buffered->position);
switch (origin) {
case MZ_SEEK_SET:
if ((buffered->readbuf_len > 0) && (offset < buffered->position) &&
(offset >= buffered->position - buffered->readbuf_len)) {
buffered->readbuf_pos = (int32_t)(offset - (buffered->position - buffered->readbuf_len));
return MZ_OK;
}
if (buffered->writebuf_len > 0) {
if ((offset >= buffered->position) && (offset <= buffered->position + buffered->writebuf_len)) {
buffered->writebuf_pos = (int32_t)(offset - buffered->position);
return MZ_OK;
}
}
err = mz_stream_buffered_flush(stream, &bytes_flushed);
if (err != MZ_OK)
return err;
buffered->position = offset;
break;
case MZ_SEEK_CUR:
if (buffered->readbuf_len > 0) {
if (offset <= ((int64_t)buffered->readbuf_len - buffered->readbuf_pos)) {
buffered->readbuf_pos += (uint32_t)offset;
return MZ_OK;
}
offset -= ((int64_t)buffered->readbuf_len - buffered->readbuf_pos);
buffered->position += offset;
}
if (buffered->writebuf_len > 0) {
if (offset <= ((int64_t)buffered->writebuf_len - buffered->writebuf_pos)) {
buffered->writebuf_pos += (uint32_t)offset;
return MZ_OK;
}
/* offset -= (buffered->writebuf_len - buffered->writebuf_pos); */
}
err = mz_stream_buffered_flush(stream, &bytes_flushed);
if (err != MZ_OK)
return err;
break;
case MZ_SEEK_END:
if (buffered->writebuf_len > 0) {
buffered->writebuf_pos = buffered->writebuf_len;
return MZ_OK;
}
break;
}
buffered->readbuf_len = 0;
buffered->readbuf_pos = 0;
buffered->writebuf_len = 0;
buffered->writebuf_pos = 0;
return mz_stream_seek(buffered->stream.base, offset, origin);
}
int32_t mz_stream_buffered_close(void *stream) {
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
int32_t bytes_flushed = 0;
mz_stream_buffered_flush(stream, &bytes_flushed);
mz_stream_buffered_print("Buffered - Close (flushed %" PRId32 ")\n", bytes_flushed);
if (buffered->readbuf_hits + buffered->readbuf_misses > 0) {
mz_stream_buffered_print("Buffered - Read efficiency %.02f%%\n",
(buffered->readbuf_hits / ((float)buffered->readbuf_hits + buffered->readbuf_misses)) * 100);
}
if (buffered->writebuf_hits + buffered->writebuf_misses > 0) {
mz_stream_buffered_print("Buffered - Write efficiency %.02f%%\n",
(buffered->writebuf_hits / ((float)buffered->writebuf_hits + buffered->writebuf_misses)) * 100);
}
mz_stream_buffered_reset(buffered);
return mz_stream_close(buffered->stream.base);
}
int32_t mz_stream_buffered_error(void *stream) {
mz_stream_buffered *buffered = (mz_stream_buffered *)stream;
return mz_stream_error(buffered->stream.base);
}
void *mz_stream_buffered_create(void) {
mz_stream_buffered *buffered = (mz_stream_buffered *)calloc(1, sizeof(mz_stream_buffered));
if (buffered)
buffered->stream.vtbl = &mz_stream_buffered_vtbl;
return buffered;
}
void mz_stream_buffered_delete(void **stream) {
mz_stream_buffered *buffered = NULL;
if (!stream)
return;
buffered = (mz_stream_buffered *)*stream;
if (buffered)
free(buffered);
*stream = NULL;
}
void *mz_stream_buffered_get_interface(void) {
return (void *)&mz_stream_buffered_vtbl;
}