cordova-plugin-mserviceinterface-2.0 1.0.26 → 1.0.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +28 -28
- package/plugin.xml +154 -153
- package/src/android/CallbackProgressDialog.java +53 -53
- package/src/android/FileProvider.java +7 -7
- package/src/android/backgroundNotification.java +120 -120
- package/src/android/doubleogradle.gradle +5 -5
- package/src/android/mInterface.java +2206 -2087
- package/src/android/mInterfaceReceiver.java +59 -59
- package/src/android/mInterfaceService.java +752 -749
- package/src/android/mInterfaceUtil.java +1686 -1686
- package/src/android/res/xml/network_security_config.xml +8 -8
- package/src/android/res/xml/opener_paths.xml +16 -16
- package/src/ios/InternetConnection.h +64 -64
- package/src/ios/InternetConnection.m +242 -242
- package/src/ios/SSZipArchive.h +146 -146
- package/src/ios/SSZipArchive.m +1180 -1180
- package/src/ios/SSZipCommon.h +66 -66
- package/src/ios/UICKeyChainStore.h +64 -64
- package/src/ios/UICKeyChainStore.m +434 -434
- package/src/ios/XMLConverter.h +61 -61
- package/src/ios/XMLConverter.m +243 -243
- package/src/ios/ZipArchive.h +19 -19
- package/src/ios/aes/aes.h +270 -270
- package/src/ios/aes/aes_ni.c +687 -687
- package/src/ios/aes/aes_ni.h +59 -59
- package/src/ios/aes/aescrypt.c +301 -301
- package/src/ios/aes/aeskey.c +554 -554
- package/src/ios/aes/aesopt.h +776 -776
- package/src/ios/aes/aestab.c +418 -418
- package/src/ios/aes/aestab.h +173 -173
- package/src/ios/aes/brg_endian.h +127 -127
- package/src/ios/aes/brg_types.h +191 -191
- package/src/ios/aes/fileenc.c +145 -145
- package/src/ios/aes/fileenc.h +121 -121
- package/src/ios/aes/hmac.c +209 -209
- package/src/ios/aes/hmac.h +119 -119
- package/src/ios/aes/prng.c +155 -155
- package/src/ios/aes/prng.h +82 -82
- package/src/ios/aes/pwd2key.c +181 -181
- package/src/ios/aes/pwd2key.h +45 -45
- package/src/ios/aes/sha1.c +283 -283
- package/src/ios/aes/sha1.h +72 -72
- package/src/ios/mInterface.h +1 -0
- package/src/ios/mInterface.m +15 -3
- package/src/ios/minizip/crypt.c +146 -146
- package/src/ios/minizip/crypt.h +66 -66
- package/src/ios/minizip/ioapi.c +353 -353
- package/src/ios/minizip/ioapi.h +154 -154
- package/src/ios/minizip/ioapi_buf.c +452 -452
- package/src/ios/minizip/ioapi_buf.h +52 -52
- package/src/ios/minizip/ioapi_mem.c +167 -167
- package/src/ios/minizip/ioapi_mem.h +52 -52
- package/src/ios/minizip/minishared.c +292 -292
- package/src/ios/minizip/minishared.h +50 -50
- package/src/ios/minizip/unzip.c +2007 -2007
- package/src/ios/minizip/unzip.h +254 -254
- package/src/ios/minizip/zip.c +2007 -2007
- package/src/ios/minizip/zip.h +215 -215
- package/www/mInterface.js +3 -3
|
@@ -1,452 +1,452 @@
|
|
|
1
|
-
/* ioapi_buf.c -- IO base function header for compress/uncompress .zip
|
|
2
|
-
files using zlib + zip or unzip API
|
|
3
|
-
|
|
4
|
-
This version of ioapi is designed to buffer IO.
|
|
5
|
-
|
|
6
|
-
Copyright (C) 2012-2017 Nathan Moinvaziri
|
|
7
|
-
https://github.com/nmoinvaz/minizip
|
|
8
|
-
|
|
9
|
-
This program is distributed under the terms of the same license as zlib.
|
|
10
|
-
See the accompanying LICENSE file for the full text of the license.
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
#include <stdio.h>
|
|
15
|
-
#include <stdlib.h>
|
|
16
|
-
#include <string.h>
|
|
17
|
-
#include <stdarg.h>
|
|
18
|
-
|
|
19
|
-
#include "zlib.h"
|
|
20
|
-
#include "ioapi.h"
|
|
21
|
-
|
|
22
|
-
#include "ioapi_buf.h"
|
|
23
|
-
|
|
24
|
-
#ifndef IOBUF_BUFFERSIZE
|
|
25
|
-
# define IOBUF_BUFFERSIZE (UINT16_MAX)
|
|
26
|
-
#endif
|
|
27
|
-
|
|
28
|
-
#if defined(_WIN32)
|
|
29
|
-
# include <conio.h>
|
|
30
|
-
# define PRINTF _cprintf
|
|
31
|
-
# define VPRINTF _vcprintf
|
|
32
|
-
#else
|
|
33
|
-
# define PRINTF printf
|
|
34
|
-
# define VPRINTF vprintf
|
|
35
|
-
#endif
|
|
36
|
-
|
|
37
|
-
//#define IOBUF_VERBOSE
|
|
38
|
-
|
|
39
|
-
typedef struct ourstream_s {
|
|
40
|
-
char readbuf[IOBUF_BUFFERSIZE];
|
|
41
|
-
uint32_t readbuf_len;
|
|
42
|
-
uint32_t readbuf_pos;
|
|
43
|
-
uint32_t readbuf_hits;
|
|
44
|
-
uint32_t readbuf_misses;
|
|
45
|
-
char writebuf[IOBUF_BUFFERSIZE];
|
|
46
|
-
uint32_t writebuf_len;
|
|
47
|
-
uint32_t writebuf_pos;
|
|
48
|
-
uint32_t writebuf_hits;
|
|
49
|
-
uint32_t writebuf_misses;
|
|
50
|
-
uint64_t position;
|
|
51
|
-
voidpf stream;
|
|
52
|
-
} ourstream_t;
|
|
53
|
-
|
|
54
|
-
#if defined(IOBUF_VERBOSE)
|
|
55
|
-
# define print_buf(o,s,f,...) print_buf_internal(o,s,f,__VA_ARGS__);
|
|
56
|
-
#else
|
|
57
|
-
# define print_buf(o,s,f,...)
|
|
58
|
-
#endif
|
|
59
|
-
|
|
60
|
-
void print_buf_internal(ZIP_UNUSED voidpf opaque, voidpf stream, char *format, ...)
|
|
61
|
-
{
|
|
62
|
-
ourstream_t *streamio = (ourstream_t *)stream;
|
|
63
|
-
va_list arglist;
|
|
64
|
-
PRINTF("Buf stream %p - ", streamio);
|
|
65
|
-
va_start(arglist, format);
|
|
66
|
-
VPRINTF(format, arglist);
|
|
67
|
-
va_end(arglist);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
voidpf fopen_buf_internal_func(ZIP_UNUSED voidpf opaque, voidpf stream, ZIP_UNUSED uint32_t number_disk, ZIP_UNUSED int mode)
|
|
71
|
-
{
|
|
72
|
-
ourstream_t *streamio = NULL;
|
|
73
|
-
if (stream == NULL)
|
|
74
|
-
return NULL;
|
|
75
|
-
streamio = (ourstream_t *)malloc(sizeof(ourstream_t));
|
|
76
|
-
if (streamio == NULL)
|
|
77
|
-
return NULL;
|
|
78
|
-
memset(streamio, 0, sizeof(ourstream_t));
|
|
79
|
-
streamio->stream = stream;
|
|
80
|
-
print_buf(opaque, streamio, "open [num %d mode %d]\n", number_disk, mode);
|
|
81
|
-
return streamio;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
voidpf ZCALLBACK fopen_buf_func(voidpf opaque, const char *filename, int mode)
|
|
85
|
-
{
|
|
86
|
-
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
87
|
-
voidpf stream = bufio->filefunc.zopen_file(bufio->filefunc.opaque, filename, mode);
|
|
88
|
-
return fopen_buf_internal_func(opaque, stream, 0, mode);
|
|
89
|
-
}
|
|
90
|
-
|
|
91
|
-
voidpf ZCALLBACK fopen64_buf_func(voidpf opaque, const void *filename, int mode)
|
|
92
|
-
{
|
|
93
|
-
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
94
|
-
voidpf stream = bufio->filefunc64.zopen64_file(bufio->filefunc64.opaque, filename, mode);
|
|
95
|
-
return fopen_buf_internal_func(opaque, stream, 0, mode);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
voidpf ZCALLBACK fopendisk_buf_func(voidpf opaque, voidpf stream_cd, uint32_t number_disk, int mode)
|
|
99
|
-
{
|
|
100
|
-
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
101
|
-
ourstream_t *streamio = (ourstream_t *)stream_cd;
|
|
102
|
-
voidpf *stream = bufio->filefunc.zopendisk_file(bufio->filefunc.opaque, streamio->stream, number_disk, mode);
|
|
103
|
-
return fopen_buf_internal_func(opaque, stream, number_disk, mode);
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
voidpf ZCALLBACK fopendisk64_buf_func(voidpf opaque, voidpf stream_cd, uint32_t number_disk, int mode)
|
|
107
|
-
{
|
|
108
|
-
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
109
|
-
ourstream_t *streamio = (ourstream_t *)stream_cd;
|
|
110
|
-
voidpf stream = bufio->filefunc64.zopendisk64_file(bufio->filefunc64.opaque, streamio->stream, number_disk, mode);
|
|
111
|
-
return fopen_buf_internal_func(opaque, stream, number_disk, mode);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
long fflush_buf(voidpf opaque, voidpf stream)
|
|
115
|
-
{
|
|
116
|
-
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
117
|
-
ourstream_t *streamio = (ourstream_t *)stream;
|
|
118
|
-
uint32_t total_bytes_to_write = 0;
|
|
119
|
-
uint32_t bytes_to_write = streamio->writebuf_len;
|
|
120
|
-
uint32_t bytes_left_to_write = streamio->writebuf_len;
|
|
121
|
-
long bytes_written = 0;
|
|
122
|
-
|
|
123
|
-
while (bytes_left_to_write > 0)
|
|
124
|
-
{
|
|
125
|
-
if (bufio->filefunc64.zwrite_file != NULL)
|
|
126
|
-
bytes_written = bufio->filefunc64.zwrite_file(bufio->filefunc64.opaque, streamio->stream, streamio->writebuf + (bytes_to_write - bytes_left_to_write), bytes_left_to_write);
|
|
127
|
-
else
|
|
128
|
-
bytes_written = bufio->filefunc.zwrite_file(bufio->filefunc.opaque, streamio->stream, streamio->writebuf + (bytes_to_write - bytes_left_to_write), bytes_left_to_write);
|
|
129
|
-
|
|
130
|
-
streamio->writebuf_misses += 1;
|
|
131
|
-
|
|
132
|
-
print_buf(opaque, stream, "write flush [%d:%d len %d]\n", bytes_to_write, bytes_left_to_write, streamio->writebuf_len);
|
|
133
|
-
|
|
134
|
-
if (bytes_written < 0)
|
|
135
|
-
return bytes_written;
|
|
136
|
-
|
|
137
|
-
total_bytes_to_write += bytes_written;
|
|
138
|
-
bytes_left_to_write -= bytes_written;
|
|
139
|
-
streamio->position += bytes_written;
|
|
140
|
-
}
|
|
141
|
-
streamio->writebuf_len = 0;
|
|
142
|
-
streamio->writebuf_pos = 0;
|
|
143
|
-
return total_bytes_to_write;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
uint32_t ZCALLBACK fread_buf_func(voidpf opaque, voidpf stream, void *buf, uint32_t size)
|
|
147
|
-
{
|
|
148
|
-
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
149
|
-
ourstream_t *streamio = (ourstream_t *)stream;
|
|
150
|
-
uint32_t buf_len = 0;
|
|
151
|
-
uint32_t bytes_to_read = 0;
|
|
152
|
-
uint32_t bytes_to_copy = 0;
|
|
153
|
-
uint32_t bytes_left_to_read = size;
|
|
154
|
-
uint32_t bytes_read = 0;
|
|
155
|
-
|
|
156
|
-
print_buf(opaque, stream, "read [size %ld pos %lld]\n", size, streamio->position);
|
|
157
|
-
|
|
158
|
-
if (streamio->writebuf_len > 0)
|
|
159
|
-
{
|
|
160
|
-
print_buf(opaque, stream, "switch from write to read, not yet supported [%lld]\n", streamio->position);
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
while (bytes_left_to_read > 0)
|
|
164
|
-
{
|
|
165
|
-
if ((streamio->readbuf_len == 0) || (streamio->readbuf_pos == streamio->readbuf_len))
|
|
166
|
-
{
|
|
167
|
-
if (streamio->readbuf_len == IOBUF_BUFFERSIZE)
|
|
168
|
-
{
|
|
169
|
-
streamio->readbuf_pos = 0;
|
|
170
|
-
streamio->readbuf_len = 0;
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
bytes_to_read = IOBUF_BUFFERSIZE - (streamio->readbuf_len - streamio->readbuf_pos);
|
|
174
|
-
|
|
175
|
-
if (bufio->filefunc64.zread_file != NULL)
|
|
176
|
-
bytes_read = bufio->filefunc64.zread_file(bufio->filefunc64.opaque, streamio->stream, streamio->readbuf + streamio->readbuf_pos, bytes_to_read);
|
|
177
|
-
else
|
|
178
|
-
bytes_read = bufio->filefunc.zread_file(bufio->filefunc.opaque, streamio->stream, streamio->readbuf + streamio->readbuf_pos, bytes_to_read);
|
|
179
|
-
|
|
180
|
-
streamio->readbuf_misses += 1;
|
|
181
|
-
streamio->readbuf_len += bytes_read;
|
|
182
|
-
streamio->position += bytes_read;
|
|
183
|
-
|
|
184
|
-
print_buf(opaque, stream, "filled [read %d/%d buf %d:%d pos %lld]\n", bytes_read, bytes_to_read, streamio->readbuf_pos, streamio->readbuf_len, streamio->position);
|
|
185
|
-
|
|
186
|
-
if (bytes_read == 0)
|
|
187
|
-
break;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
if ((streamio->readbuf_len - streamio->readbuf_pos) > 0)
|
|
191
|
-
{
|
|
192
|
-
bytes_to_copy = streamio->readbuf_len - streamio->readbuf_pos;
|
|
193
|
-
if (bytes_to_copy > bytes_left_to_read)
|
|
194
|
-
bytes_to_copy = bytes_left_to_read;
|
|
195
|
-
|
|
196
|
-
memcpy((char *)buf + buf_len, streamio->readbuf + streamio->readbuf_pos, bytes_to_copy);
|
|
197
|
-
|
|
198
|
-
buf_len += bytes_to_copy;
|
|
199
|
-
bytes_left_to_read -= bytes_to_copy;
|
|
200
|
-
|
|
201
|
-
streamio->readbuf_hits += 1;
|
|
202
|
-
streamio->readbuf_pos += bytes_to_copy;
|
|
203
|
-
|
|
204
|
-
print_buf(opaque, stream, "emptied [copied %d remaining %d buf %d:%d pos %lld]\n", bytes_to_copy, bytes_left_to_read, streamio->readbuf_pos, streamio->readbuf_len, streamio->position);
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
return size - bytes_left_to_read;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
uint32_t ZCALLBACK fwrite_buf_func(voidpf opaque, voidpf stream, const void *buf, uint32_t size)
|
|
212
|
-
{
|
|
213
|
-
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
214
|
-
ourstream_t *streamio = (ourstream_t *)stream;
|
|
215
|
-
uint32_t bytes_to_write = size;
|
|
216
|
-
uint32_t bytes_left_to_write = size;
|
|
217
|
-
uint32_t bytes_to_copy = 0;
|
|
218
|
-
uint32_t bytes_used = 0;
|
|
219
|
-
int64_t ret = 0;
|
|
220
|
-
|
|
221
|
-
print_buf(opaque, stream, "write [size %ld len %d pos %lld]\n", size, streamio->writebuf_len, streamio->position);
|
|
222
|
-
|
|
223
|
-
if (streamio->readbuf_len > 0)
|
|
224
|
-
{
|
|
225
|
-
streamio->position -= streamio->readbuf_len;
|
|
226
|
-
streamio->position += streamio->readbuf_pos;
|
|
227
|
-
|
|
228
|
-
streamio->readbuf_len = 0;
|
|
229
|
-
streamio->readbuf_pos = 0;
|
|
230
|
-
|
|
231
|
-
print_buf(opaque, stream, "switch from read to write [%lld]\n", streamio->position);
|
|
232
|
-
|
|
233
|
-
if (bufio->filefunc64.zseek64_file != NULL)
|
|
234
|
-
ret = bufio->filefunc64.zseek64_file(bufio->filefunc64.opaque, streamio->stream, streamio->position, ZLIB_FILEFUNC_SEEK_SET);
|
|
235
|
-
else
|
|
236
|
-
ret = bufio->filefunc.zseek_file(bufio->filefunc.opaque, streamio->stream, (uint32_t)streamio->position, ZLIB_FILEFUNC_SEEK_SET);
|
|
237
|
-
|
|
238
|
-
if (ret != 0)
|
|
239
|
-
return (uint32_t)-1;
|
|
240
|
-
}
|
|
241
|
-
|
|
242
|
-
while (bytes_left_to_write > 0)
|
|
243
|
-
{
|
|
244
|
-
bytes_used = streamio->writebuf_len;
|
|
245
|
-
if (bytes_used > streamio->writebuf_pos)
|
|
246
|
-
bytes_used = streamio->writebuf_pos;
|
|
247
|
-
bytes_to_copy = (uint32_t)(IOBUF_BUFFERSIZE - bytes_used);
|
|
248
|
-
if (bytes_to_copy > bytes_left_to_write)
|
|
249
|
-
bytes_to_copy = bytes_left_to_write;
|
|
250
|
-
|
|
251
|
-
if (bytes_to_copy == 0)
|
|
252
|
-
{
|
|
253
|
-
if (fflush_buf(opaque, stream) <= 0)
|
|
254
|
-
return 0;
|
|
255
|
-
|
|
256
|
-
continue;
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
memcpy(streamio->writebuf + streamio->writebuf_pos, (char *)buf + (bytes_to_write - bytes_left_to_write), bytes_to_copy);
|
|
260
|
-
|
|
261
|
-
print_buf(opaque, stream, "write copy [remaining %d write %d:%d len %d]\n", bytes_to_copy, bytes_to_write, bytes_left_to_write, streamio->writebuf_len);
|
|
262
|
-
|
|
263
|
-
bytes_left_to_write -= bytes_to_copy;
|
|
264
|
-
|
|
265
|
-
streamio->writebuf_pos += bytes_to_copy;
|
|
266
|
-
streamio->writebuf_hits += 1;
|
|
267
|
-
if (streamio->writebuf_pos > streamio->writebuf_len)
|
|
268
|
-
streamio->writebuf_len += streamio->writebuf_pos - streamio->writebuf_len;
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
return size - bytes_left_to_write;
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
uint64_t ftell_buf_internal_func(ZIP_UNUSED voidpf opaque, voidpf stream, uint64_t position)
|
|
275
|
-
{
|
|
276
|
-
ourstream_t *streamio = (ourstream_t *)stream;
|
|
277
|
-
streamio->position = position;
|
|
278
|
-
print_buf(opaque, stream, "tell [pos %llu readpos %d writepos %d err %d]\n", streamio->position, streamio->readbuf_pos, streamio->writebuf_pos, errno);
|
|
279
|
-
if (streamio->readbuf_len > 0)
|
|
280
|
-
position -= (streamio->readbuf_len - streamio->readbuf_pos);
|
|
281
|
-
if (streamio->writebuf_len > 0)
|
|
282
|
-
position += streamio->writebuf_pos;
|
|
283
|
-
return position;
|
|
284
|
-
}
|
|
285
|
-
|
|
286
|
-
long ZCALLBACK ftell_buf_func(voidpf opaque, voidpf stream)
|
|
287
|
-
{
|
|
288
|
-
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
289
|
-
ourstream_t *streamio = (ourstream_t *)stream;
|
|
290
|
-
uint64_t position = bufio->filefunc.ztell_file(bufio->filefunc.opaque, streamio->stream);
|
|
291
|
-
return (long)ftell_buf_internal_func(opaque, stream, position);
|
|
292
|
-
}
|
|
293
|
-
|
|
294
|
-
uint64_t ZCALLBACK ftell64_buf_func(voidpf opaque, voidpf stream)
|
|
295
|
-
{
|
|
296
|
-
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
297
|
-
ourstream_t *streamio = (ourstream_t *)stream;
|
|
298
|
-
uint64_t position = bufio->filefunc64.ztell64_file(bufio->filefunc64.opaque, streamio->stream);
|
|
299
|
-
return ftell_buf_internal_func(opaque, stream, position);
|
|
300
|
-
}
|
|
301
|
-
|
|
302
|
-
int fseek_buf_internal_func(voidpf opaque, voidpf stream, uint64_t offset, int origin)
|
|
303
|
-
{
|
|
304
|
-
ourstream_t *streamio = (ourstream_t *)stream;
|
|
305
|
-
|
|
306
|
-
print_buf(opaque, stream, "seek [origin %d offset %llu pos %lld]\n", origin, offset, streamio->position);
|
|
307
|
-
|
|
308
|
-
switch (origin)
|
|
309
|
-
{
|
|
310
|
-
case ZLIB_FILEFUNC_SEEK_SET:
|
|
311
|
-
|
|
312
|
-
if (streamio->writebuf_len > 0)
|
|
313
|
-
{
|
|
314
|
-
if ((offset >= streamio->position) && (offset <= streamio->position + streamio->writebuf_len))
|
|
315
|
-
{
|
|
316
|
-
streamio->writebuf_pos = (uint32_t)(offset - streamio->position);
|
|
317
|
-
return 0;
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
if ((streamio->readbuf_len > 0) && (offset < streamio->position) && (offset >= streamio->position - streamio->readbuf_len))
|
|
321
|
-
{
|
|
322
|
-
streamio->readbuf_pos = (uint32_t)(offset - (streamio->position - streamio->readbuf_len));
|
|
323
|
-
return 0;
|
|
324
|
-
}
|
|
325
|
-
if (fflush_buf(opaque, stream) < 0)
|
|
326
|
-
return -1;
|
|
327
|
-
streamio->position = offset;
|
|
328
|
-
break;
|
|
329
|
-
|
|
330
|
-
case ZLIB_FILEFUNC_SEEK_CUR:
|
|
331
|
-
|
|
332
|
-
if (streamio->readbuf_len > 0)
|
|
333
|
-
{
|
|
334
|
-
if (offset <= (streamio->readbuf_len - streamio->readbuf_pos))
|
|
335
|
-
{
|
|
336
|
-
streamio->readbuf_pos += (uint32_t)offset;
|
|
337
|
-
return 0;
|
|
338
|
-
}
|
|
339
|
-
offset -= (streamio->readbuf_len - streamio->readbuf_pos);
|
|
340
|
-
streamio->position += offset;
|
|
341
|
-
}
|
|
342
|
-
if (streamio->writebuf_len > 0)
|
|
343
|
-
{
|
|
344
|
-
if (offset <= (streamio->writebuf_len - streamio->writebuf_pos))
|
|
345
|
-
{
|
|
346
|
-
streamio->writebuf_pos += (uint32_t)offset;
|
|
347
|
-
return 0;
|
|
348
|
-
}
|
|
349
|
-
//offset -= (streamio->writebuf_len - streamio->writebuf_pos);
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
if (fflush_buf(opaque, stream) < 0)
|
|
353
|
-
return -1;
|
|
354
|
-
|
|
355
|
-
break;
|
|
356
|
-
|
|
357
|
-
case ZLIB_FILEFUNC_SEEK_END:
|
|
358
|
-
|
|
359
|
-
if (streamio->writebuf_len > 0)
|
|
360
|
-
{
|
|
361
|
-
streamio->writebuf_pos = streamio->writebuf_len;
|
|
362
|
-
return 0;
|
|
363
|
-
}
|
|
364
|
-
break;
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
streamio->readbuf_len = 0;
|
|
368
|
-
streamio->readbuf_pos = 0;
|
|
369
|
-
streamio->writebuf_len = 0;
|
|
370
|
-
streamio->writebuf_pos = 0;
|
|
371
|
-
return 1;
|
|
372
|
-
}
|
|
373
|
-
|
|
374
|
-
long ZCALLBACK fseek_buf_func(voidpf opaque, voidpf stream, uint32_t offset, int origin)
|
|
375
|
-
{
|
|
376
|
-
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
377
|
-
ourstream_t *streamio = (ourstream_t *)stream;
|
|
378
|
-
long ret = -1;
|
|
379
|
-
if (bufio->filefunc.zseek_file == NULL)
|
|
380
|
-
return ret;
|
|
381
|
-
ret = fseek_buf_internal_func(opaque, stream, offset, origin);
|
|
382
|
-
if (ret == 1)
|
|
383
|
-
ret = bufio->filefunc.zseek_file(bufio->filefunc.opaque, streamio->stream, offset, origin);
|
|
384
|
-
return ret;
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
long ZCALLBACK fseek64_buf_func(voidpf opaque, voidpf stream, uint64_t offset, int origin)
|
|
388
|
-
{
|
|
389
|
-
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
390
|
-
ourstream_t *streamio = (ourstream_t *)stream;
|
|
391
|
-
long ret = -1;
|
|
392
|
-
if (bufio->filefunc64.zseek64_file == NULL)
|
|
393
|
-
return ret;
|
|
394
|
-
ret = fseek_buf_internal_func(opaque, stream, offset, origin);
|
|
395
|
-
if (ret == 1)
|
|
396
|
-
ret = bufio->filefunc64.zseek64_file(bufio->filefunc64.opaque, streamio->stream, offset, origin);
|
|
397
|
-
return ret;
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
int ZCALLBACK fclose_buf_func(voidpf opaque, voidpf stream)
|
|
401
|
-
{
|
|
402
|
-
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
403
|
-
ourstream_t *streamio = (ourstream_t *)stream;
|
|
404
|
-
int ret = 0;
|
|
405
|
-
fflush_buf(opaque, stream);
|
|
406
|
-
print_buf(opaque, stream, "close\n");
|
|
407
|
-
if (streamio->readbuf_hits + streamio->readbuf_misses > 0)
|
|
408
|
-
print_buf(opaque, stream, "read efficency %.02f%%\n", (streamio->readbuf_hits / ((float)streamio->readbuf_hits + streamio->readbuf_misses)) * 100);
|
|
409
|
-
if (streamio->writebuf_hits + streamio->writebuf_misses > 0)
|
|
410
|
-
print_buf(opaque, stream, "write efficency %.02f%%\n", (streamio->writebuf_hits / ((float)streamio->writebuf_hits + streamio->writebuf_misses)) * 100);
|
|
411
|
-
if (bufio->filefunc64.zclose_file != NULL)
|
|
412
|
-
ret = bufio->filefunc64.zclose_file(bufio->filefunc64.opaque, streamio->stream);
|
|
413
|
-
else
|
|
414
|
-
ret = bufio->filefunc.zclose_file(bufio->filefunc.opaque, streamio->stream);
|
|
415
|
-
free(streamio);
|
|
416
|
-
return ret;
|
|
417
|
-
}
|
|
418
|
-
|
|
419
|
-
int ZCALLBACK ferror_buf_func(voidpf opaque, voidpf stream)
|
|
420
|
-
{
|
|
421
|
-
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
422
|
-
ourstream_t *streamio = (ourstream_t *)stream;
|
|
423
|
-
if (bufio->filefunc64.zerror_file != NULL)
|
|
424
|
-
return bufio->filefunc64.zerror_file(bufio->filefunc64.opaque, streamio->stream);
|
|
425
|
-
return bufio->filefunc.zerror_file(bufio->filefunc.opaque, streamio->stream);
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
void fill_buffer_filefunc(zlib_filefunc_def *pzlib_filefunc_def, ourbuffer_t *ourbuf)
|
|
429
|
-
{
|
|
430
|
-
pzlib_filefunc_def->zopen_file = fopen_buf_func;
|
|
431
|
-
pzlib_filefunc_def->zopendisk_file = fopendisk_buf_func;
|
|
432
|
-
pzlib_filefunc_def->zread_file = fread_buf_func;
|
|
433
|
-
pzlib_filefunc_def->zwrite_file = fwrite_buf_func;
|
|
434
|
-
pzlib_filefunc_def->ztell_file = ftell_buf_func;
|
|
435
|
-
pzlib_filefunc_def->zseek_file = fseek_buf_func;
|
|
436
|
-
pzlib_filefunc_def->zclose_file = fclose_buf_func;
|
|
437
|
-
pzlib_filefunc_def->zerror_file = ferror_buf_func;
|
|
438
|
-
pzlib_filefunc_def->opaque = ourbuf;
|
|
439
|
-
}
|
|
440
|
-
|
|
441
|
-
void fill_buffer_filefunc64(zlib_filefunc64_def *pzlib_filefunc_def, ourbuffer_t *ourbuf)
|
|
442
|
-
{
|
|
443
|
-
pzlib_filefunc_def->zopen64_file = fopen64_buf_func;
|
|
444
|
-
pzlib_filefunc_def->zopendisk64_file = fopendisk64_buf_func;
|
|
445
|
-
pzlib_filefunc_def->zread_file = fread_buf_func;
|
|
446
|
-
pzlib_filefunc_def->zwrite_file = fwrite_buf_func;
|
|
447
|
-
pzlib_filefunc_def->ztell64_file = ftell64_buf_func;
|
|
448
|
-
pzlib_filefunc_def->zseek64_file = fseek64_buf_func;
|
|
449
|
-
pzlib_filefunc_def->zclose_file = fclose_buf_func;
|
|
450
|
-
pzlib_filefunc_def->zerror_file = ferror_buf_func;
|
|
451
|
-
pzlib_filefunc_def->opaque = ourbuf;
|
|
452
|
-
}
|
|
1
|
+
/* ioapi_buf.c -- IO base function header for compress/uncompress .zip
|
|
2
|
+
files using zlib + zip or unzip API
|
|
3
|
+
|
|
4
|
+
This version of ioapi is designed to buffer IO.
|
|
5
|
+
|
|
6
|
+
Copyright (C) 2012-2017 Nathan Moinvaziri
|
|
7
|
+
https://github.com/nmoinvaz/minizip
|
|
8
|
+
|
|
9
|
+
This program is distributed under the terms of the same license as zlib.
|
|
10
|
+
See the accompanying LICENSE file for the full text of the license.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
#include <stdio.h>
|
|
15
|
+
#include <stdlib.h>
|
|
16
|
+
#include <string.h>
|
|
17
|
+
#include <stdarg.h>
|
|
18
|
+
|
|
19
|
+
#include "zlib.h"
|
|
20
|
+
#include "ioapi.h"
|
|
21
|
+
|
|
22
|
+
#include "ioapi_buf.h"
|
|
23
|
+
|
|
24
|
+
#ifndef IOBUF_BUFFERSIZE
|
|
25
|
+
# define IOBUF_BUFFERSIZE (UINT16_MAX)
|
|
26
|
+
#endif
|
|
27
|
+
|
|
28
|
+
#if defined(_WIN32)
|
|
29
|
+
# include <conio.h>
|
|
30
|
+
# define PRINTF _cprintf
|
|
31
|
+
# define VPRINTF _vcprintf
|
|
32
|
+
#else
|
|
33
|
+
# define PRINTF printf
|
|
34
|
+
# define VPRINTF vprintf
|
|
35
|
+
#endif
|
|
36
|
+
|
|
37
|
+
//#define IOBUF_VERBOSE
|
|
38
|
+
|
|
39
|
+
typedef struct ourstream_s {
|
|
40
|
+
char readbuf[IOBUF_BUFFERSIZE];
|
|
41
|
+
uint32_t readbuf_len;
|
|
42
|
+
uint32_t readbuf_pos;
|
|
43
|
+
uint32_t readbuf_hits;
|
|
44
|
+
uint32_t readbuf_misses;
|
|
45
|
+
char writebuf[IOBUF_BUFFERSIZE];
|
|
46
|
+
uint32_t writebuf_len;
|
|
47
|
+
uint32_t writebuf_pos;
|
|
48
|
+
uint32_t writebuf_hits;
|
|
49
|
+
uint32_t writebuf_misses;
|
|
50
|
+
uint64_t position;
|
|
51
|
+
voidpf stream;
|
|
52
|
+
} ourstream_t;
|
|
53
|
+
|
|
54
|
+
#if defined(IOBUF_VERBOSE)
|
|
55
|
+
# define print_buf(o,s,f,...) print_buf_internal(o,s,f,__VA_ARGS__);
|
|
56
|
+
#else
|
|
57
|
+
# define print_buf(o,s,f,...)
|
|
58
|
+
#endif
|
|
59
|
+
|
|
60
|
+
void print_buf_internal(ZIP_UNUSED voidpf opaque, voidpf stream, char *format, ...)
|
|
61
|
+
{
|
|
62
|
+
ourstream_t *streamio = (ourstream_t *)stream;
|
|
63
|
+
va_list arglist;
|
|
64
|
+
PRINTF("Buf stream %p - ", streamio);
|
|
65
|
+
va_start(arglist, format);
|
|
66
|
+
VPRINTF(format, arglist);
|
|
67
|
+
va_end(arglist);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
voidpf fopen_buf_internal_func(ZIP_UNUSED voidpf opaque, voidpf stream, ZIP_UNUSED uint32_t number_disk, ZIP_UNUSED int mode)
|
|
71
|
+
{
|
|
72
|
+
ourstream_t *streamio = NULL;
|
|
73
|
+
if (stream == NULL)
|
|
74
|
+
return NULL;
|
|
75
|
+
streamio = (ourstream_t *)malloc(sizeof(ourstream_t));
|
|
76
|
+
if (streamio == NULL)
|
|
77
|
+
return NULL;
|
|
78
|
+
memset(streamio, 0, sizeof(ourstream_t));
|
|
79
|
+
streamio->stream = stream;
|
|
80
|
+
print_buf(opaque, streamio, "open [num %d mode %d]\n", number_disk, mode);
|
|
81
|
+
return streamio;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
voidpf ZCALLBACK fopen_buf_func(voidpf opaque, const char *filename, int mode)
|
|
85
|
+
{
|
|
86
|
+
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
87
|
+
voidpf stream = bufio->filefunc.zopen_file(bufio->filefunc.opaque, filename, mode);
|
|
88
|
+
return fopen_buf_internal_func(opaque, stream, 0, mode);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
voidpf ZCALLBACK fopen64_buf_func(voidpf opaque, const void *filename, int mode)
|
|
92
|
+
{
|
|
93
|
+
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
94
|
+
voidpf stream = bufio->filefunc64.zopen64_file(bufio->filefunc64.opaque, filename, mode);
|
|
95
|
+
return fopen_buf_internal_func(opaque, stream, 0, mode);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
voidpf ZCALLBACK fopendisk_buf_func(voidpf opaque, voidpf stream_cd, uint32_t number_disk, int mode)
|
|
99
|
+
{
|
|
100
|
+
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
101
|
+
ourstream_t *streamio = (ourstream_t *)stream_cd;
|
|
102
|
+
voidpf *stream = bufio->filefunc.zopendisk_file(bufio->filefunc.opaque, streamio->stream, number_disk, mode);
|
|
103
|
+
return fopen_buf_internal_func(opaque, stream, number_disk, mode);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
voidpf ZCALLBACK fopendisk64_buf_func(voidpf opaque, voidpf stream_cd, uint32_t number_disk, int mode)
|
|
107
|
+
{
|
|
108
|
+
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
109
|
+
ourstream_t *streamio = (ourstream_t *)stream_cd;
|
|
110
|
+
voidpf stream = bufio->filefunc64.zopendisk64_file(bufio->filefunc64.opaque, streamio->stream, number_disk, mode);
|
|
111
|
+
return fopen_buf_internal_func(opaque, stream, number_disk, mode);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
long fflush_buf(voidpf opaque, voidpf stream)
|
|
115
|
+
{
|
|
116
|
+
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
117
|
+
ourstream_t *streamio = (ourstream_t *)stream;
|
|
118
|
+
uint32_t total_bytes_to_write = 0;
|
|
119
|
+
uint32_t bytes_to_write = streamio->writebuf_len;
|
|
120
|
+
uint32_t bytes_left_to_write = streamio->writebuf_len;
|
|
121
|
+
long bytes_written = 0;
|
|
122
|
+
|
|
123
|
+
while (bytes_left_to_write > 0)
|
|
124
|
+
{
|
|
125
|
+
if (bufio->filefunc64.zwrite_file != NULL)
|
|
126
|
+
bytes_written = bufio->filefunc64.zwrite_file(bufio->filefunc64.opaque, streamio->stream, streamio->writebuf + (bytes_to_write - bytes_left_to_write), bytes_left_to_write);
|
|
127
|
+
else
|
|
128
|
+
bytes_written = bufio->filefunc.zwrite_file(bufio->filefunc.opaque, streamio->stream, streamio->writebuf + (bytes_to_write - bytes_left_to_write), bytes_left_to_write);
|
|
129
|
+
|
|
130
|
+
streamio->writebuf_misses += 1;
|
|
131
|
+
|
|
132
|
+
print_buf(opaque, stream, "write flush [%d:%d len %d]\n", bytes_to_write, bytes_left_to_write, streamio->writebuf_len);
|
|
133
|
+
|
|
134
|
+
if (bytes_written < 0)
|
|
135
|
+
return bytes_written;
|
|
136
|
+
|
|
137
|
+
total_bytes_to_write += bytes_written;
|
|
138
|
+
bytes_left_to_write -= bytes_written;
|
|
139
|
+
streamio->position += bytes_written;
|
|
140
|
+
}
|
|
141
|
+
streamio->writebuf_len = 0;
|
|
142
|
+
streamio->writebuf_pos = 0;
|
|
143
|
+
return total_bytes_to_write;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
uint32_t ZCALLBACK fread_buf_func(voidpf opaque, voidpf stream, void *buf, uint32_t size)
|
|
147
|
+
{
|
|
148
|
+
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
149
|
+
ourstream_t *streamio = (ourstream_t *)stream;
|
|
150
|
+
uint32_t buf_len = 0;
|
|
151
|
+
uint32_t bytes_to_read = 0;
|
|
152
|
+
uint32_t bytes_to_copy = 0;
|
|
153
|
+
uint32_t bytes_left_to_read = size;
|
|
154
|
+
uint32_t bytes_read = 0;
|
|
155
|
+
|
|
156
|
+
print_buf(opaque, stream, "read [size %ld pos %lld]\n", size, streamio->position);
|
|
157
|
+
|
|
158
|
+
if (streamio->writebuf_len > 0)
|
|
159
|
+
{
|
|
160
|
+
print_buf(opaque, stream, "switch from write to read, not yet supported [%lld]\n", streamio->position);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
while (bytes_left_to_read > 0)
|
|
164
|
+
{
|
|
165
|
+
if ((streamio->readbuf_len == 0) || (streamio->readbuf_pos == streamio->readbuf_len))
|
|
166
|
+
{
|
|
167
|
+
if (streamio->readbuf_len == IOBUF_BUFFERSIZE)
|
|
168
|
+
{
|
|
169
|
+
streamio->readbuf_pos = 0;
|
|
170
|
+
streamio->readbuf_len = 0;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
bytes_to_read = IOBUF_BUFFERSIZE - (streamio->readbuf_len - streamio->readbuf_pos);
|
|
174
|
+
|
|
175
|
+
if (bufio->filefunc64.zread_file != NULL)
|
|
176
|
+
bytes_read = bufio->filefunc64.zread_file(bufio->filefunc64.opaque, streamio->stream, streamio->readbuf + streamio->readbuf_pos, bytes_to_read);
|
|
177
|
+
else
|
|
178
|
+
bytes_read = bufio->filefunc.zread_file(bufio->filefunc.opaque, streamio->stream, streamio->readbuf + streamio->readbuf_pos, bytes_to_read);
|
|
179
|
+
|
|
180
|
+
streamio->readbuf_misses += 1;
|
|
181
|
+
streamio->readbuf_len += bytes_read;
|
|
182
|
+
streamio->position += bytes_read;
|
|
183
|
+
|
|
184
|
+
print_buf(opaque, stream, "filled [read %d/%d buf %d:%d pos %lld]\n", bytes_read, bytes_to_read, streamio->readbuf_pos, streamio->readbuf_len, streamio->position);
|
|
185
|
+
|
|
186
|
+
if (bytes_read == 0)
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if ((streamio->readbuf_len - streamio->readbuf_pos) > 0)
|
|
191
|
+
{
|
|
192
|
+
bytes_to_copy = streamio->readbuf_len - streamio->readbuf_pos;
|
|
193
|
+
if (bytes_to_copy > bytes_left_to_read)
|
|
194
|
+
bytes_to_copy = bytes_left_to_read;
|
|
195
|
+
|
|
196
|
+
memcpy((char *)buf + buf_len, streamio->readbuf + streamio->readbuf_pos, bytes_to_copy);
|
|
197
|
+
|
|
198
|
+
buf_len += bytes_to_copy;
|
|
199
|
+
bytes_left_to_read -= bytes_to_copy;
|
|
200
|
+
|
|
201
|
+
streamio->readbuf_hits += 1;
|
|
202
|
+
streamio->readbuf_pos += bytes_to_copy;
|
|
203
|
+
|
|
204
|
+
print_buf(opaque, stream, "emptied [copied %d remaining %d buf %d:%d pos %lld]\n", bytes_to_copy, bytes_left_to_read, streamio->readbuf_pos, streamio->readbuf_len, streamio->position);
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return size - bytes_left_to_read;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
uint32_t ZCALLBACK fwrite_buf_func(voidpf opaque, voidpf stream, const void *buf, uint32_t size)
|
|
212
|
+
{
|
|
213
|
+
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
214
|
+
ourstream_t *streamio = (ourstream_t *)stream;
|
|
215
|
+
uint32_t bytes_to_write = size;
|
|
216
|
+
uint32_t bytes_left_to_write = size;
|
|
217
|
+
uint32_t bytes_to_copy = 0;
|
|
218
|
+
uint32_t bytes_used = 0;
|
|
219
|
+
int64_t ret = 0;
|
|
220
|
+
|
|
221
|
+
print_buf(opaque, stream, "write [size %ld len %d pos %lld]\n", size, streamio->writebuf_len, streamio->position);
|
|
222
|
+
|
|
223
|
+
if (streamio->readbuf_len > 0)
|
|
224
|
+
{
|
|
225
|
+
streamio->position -= streamio->readbuf_len;
|
|
226
|
+
streamio->position += streamio->readbuf_pos;
|
|
227
|
+
|
|
228
|
+
streamio->readbuf_len = 0;
|
|
229
|
+
streamio->readbuf_pos = 0;
|
|
230
|
+
|
|
231
|
+
print_buf(opaque, stream, "switch from read to write [%lld]\n", streamio->position);
|
|
232
|
+
|
|
233
|
+
if (bufio->filefunc64.zseek64_file != NULL)
|
|
234
|
+
ret = bufio->filefunc64.zseek64_file(bufio->filefunc64.opaque, streamio->stream, streamio->position, ZLIB_FILEFUNC_SEEK_SET);
|
|
235
|
+
else
|
|
236
|
+
ret = bufio->filefunc.zseek_file(bufio->filefunc.opaque, streamio->stream, (uint32_t)streamio->position, ZLIB_FILEFUNC_SEEK_SET);
|
|
237
|
+
|
|
238
|
+
if (ret != 0)
|
|
239
|
+
return (uint32_t)-1;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
while (bytes_left_to_write > 0)
|
|
243
|
+
{
|
|
244
|
+
bytes_used = streamio->writebuf_len;
|
|
245
|
+
if (bytes_used > streamio->writebuf_pos)
|
|
246
|
+
bytes_used = streamio->writebuf_pos;
|
|
247
|
+
bytes_to_copy = (uint32_t)(IOBUF_BUFFERSIZE - bytes_used);
|
|
248
|
+
if (bytes_to_copy > bytes_left_to_write)
|
|
249
|
+
bytes_to_copy = bytes_left_to_write;
|
|
250
|
+
|
|
251
|
+
if (bytes_to_copy == 0)
|
|
252
|
+
{
|
|
253
|
+
if (fflush_buf(opaque, stream) <= 0)
|
|
254
|
+
return 0;
|
|
255
|
+
|
|
256
|
+
continue;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
memcpy(streamio->writebuf + streamio->writebuf_pos, (char *)buf + (bytes_to_write - bytes_left_to_write), bytes_to_copy);
|
|
260
|
+
|
|
261
|
+
print_buf(opaque, stream, "write copy [remaining %d write %d:%d len %d]\n", bytes_to_copy, bytes_to_write, bytes_left_to_write, streamio->writebuf_len);
|
|
262
|
+
|
|
263
|
+
bytes_left_to_write -= bytes_to_copy;
|
|
264
|
+
|
|
265
|
+
streamio->writebuf_pos += bytes_to_copy;
|
|
266
|
+
streamio->writebuf_hits += 1;
|
|
267
|
+
if (streamio->writebuf_pos > streamio->writebuf_len)
|
|
268
|
+
streamio->writebuf_len += streamio->writebuf_pos - streamio->writebuf_len;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
return size - bytes_left_to_write;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
uint64_t ftell_buf_internal_func(ZIP_UNUSED voidpf opaque, voidpf stream, uint64_t position)
|
|
275
|
+
{
|
|
276
|
+
ourstream_t *streamio = (ourstream_t *)stream;
|
|
277
|
+
streamio->position = position;
|
|
278
|
+
print_buf(opaque, stream, "tell [pos %llu readpos %d writepos %d err %d]\n", streamio->position, streamio->readbuf_pos, streamio->writebuf_pos, errno);
|
|
279
|
+
if (streamio->readbuf_len > 0)
|
|
280
|
+
position -= (streamio->readbuf_len - streamio->readbuf_pos);
|
|
281
|
+
if (streamio->writebuf_len > 0)
|
|
282
|
+
position += streamio->writebuf_pos;
|
|
283
|
+
return position;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
long ZCALLBACK ftell_buf_func(voidpf opaque, voidpf stream)
|
|
287
|
+
{
|
|
288
|
+
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
289
|
+
ourstream_t *streamio = (ourstream_t *)stream;
|
|
290
|
+
uint64_t position = bufio->filefunc.ztell_file(bufio->filefunc.opaque, streamio->stream);
|
|
291
|
+
return (long)ftell_buf_internal_func(opaque, stream, position);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
uint64_t ZCALLBACK ftell64_buf_func(voidpf opaque, voidpf stream)
|
|
295
|
+
{
|
|
296
|
+
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
297
|
+
ourstream_t *streamio = (ourstream_t *)stream;
|
|
298
|
+
uint64_t position = bufio->filefunc64.ztell64_file(bufio->filefunc64.opaque, streamio->stream);
|
|
299
|
+
return ftell_buf_internal_func(opaque, stream, position);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
int fseek_buf_internal_func(voidpf opaque, voidpf stream, uint64_t offset, int origin)
|
|
303
|
+
{
|
|
304
|
+
ourstream_t *streamio = (ourstream_t *)stream;
|
|
305
|
+
|
|
306
|
+
print_buf(opaque, stream, "seek [origin %d offset %llu pos %lld]\n", origin, offset, streamio->position);
|
|
307
|
+
|
|
308
|
+
switch (origin)
|
|
309
|
+
{
|
|
310
|
+
case ZLIB_FILEFUNC_SEEK_SET:
|
|
311
|
+
|
|
312
|
+
if (streamio->writebuf_len > 0)
|
|
313
|
+
{
|
|
314
|
+
if ((offset >= streamio->position) && (offset <= streamio->position + streamio->writebuf_len))
|
|
315
|
+
{
|
|
316
|
+
streamio->writebuf_pos = (uint32_t)(offset - streamio->position);
|
|
317
|
+
return 0;
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
if ((streamio->readbuf_len > 0) && (offset < streamio->position) && (offset >= streamio->position - streamio->readbuf_len))
|
|
321
|
+
{
|
|
322
|
+
streamio->readbuf_pos = (uint32_t)(offset - (streamio->position - streamio->readbuf_len));
|
|
323
|
+
return 0;
|
|
324
|
+
}
|
|
325
|
+
if (fflush_buf(opaque, stream) < 0)
|
|
326
|
+
return -1;
|
|
327
|
+
streamio->position = offset;
|
|
328
|
+
break;
|
|
329
|
+
|
|
330
|
+
case ZLIB_FILEFUNC_SEEK_CUR:
|
|
331
|
+
|
|
332
|
+
if (streamio->readbuf_len > 0)
|
|
333
|
+
{
|
|
334
|
+
if (offset <= (streamio->readbuf_len - streamio->readbuf_pos))
|
|
335
|
+
{
|
|
336
|
+
streamio->readbuf_pos += (uint32_t)offset;
|
|
337
|
+
return 0;
|
|
338
|
+
}
|
|
339
|
+
offset -= (streamio->readbuf_len - streamio->readbuf_pos);
|
|
340
|
+
streamio->position += offset;
|
|
341
|
+
}
|
|
342
|
+
if (streamio->writebuf_len > 0)
|
|
343
|
+
{
|
|
344
|
+
if (offset <= (streamio->writebuf_len - streamio->writebuf_pos))
|
|
345
|
+
{
|
|
346
|
+
streamio->writebuf_pos += (uint32_t)offset;
|
|
347
|
+
return 0;
|
|
348
|
+
}
|
|
349
|
+
//offset -= (streamio->writebuf_len - streamio->writebuf_pos);
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (fflush_buf(opaque, stream) < 0)
|
|
353
|
+
return -1;
|
|
354
|
+
|
|
355
|
+
break;
|
|
356
|
+
|
|
357
|
+
case ZLIB_FILEFUNC_SEEK_END:
|
|
358
|
+
|
|
359
|
+
if (streamio->writebuf_len > 0)
|
|
360
|
+
{
|
|
361
|
+
streamio->writebuf_pos = streamio->writebuf_len;
|
|
362
|
+
return 0;
|
|
363
|
+
}
|
|
364
|
+
break;
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
streamio->readbuf_len = 0;
|
|
368
|
+
streamio->readbuf_pos = 0;
|
|
369
|
+
streamio->writebuf_len = 0;
|
|
370
|
+
streamio->writebuf_pos = 0;
|
|
371
|
+
return 1;
|
|
372
|
+
}
|
|
373
|
+
|
|
374
|
+
long ZCALLBACK fseek_buf_func(voidpf opaque, voidpf stream, uint32_t offset, int origin)
|
|
375
|
+
{
|
|
376
|
+
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
377
|
+
ourstream_t *streamio = (ourstream_t *)stream;
|
|
378
|
+
long ret = -1;
|
|
379
|
+
if (bufio->filefunc.zseek_file == NULL)
|
|
380
|
+
return ret;
|
|
381
|
+
ret = fseek_buf_internal_func(opaque, stream, offset, origin);
|
|
382
|
+
if (ret == 1)
|
|
383
|
+
ret = bufio->filefunc.zseek_file(bufio->filefunc.opaque, streamio->stream, offset, origin);
|
|
384
|
+
return ret;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
long ZCALLBACK fseek64_buf_func(voidpf opaque, voidpf stream, uint64_t offset, int origin)
|
|
388
|
+
{
|
|
389
|
+
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
390
|
+
ourstream_t *streamio = (ourstream_t *)stream;
|
|
391
|
+
long ret = -1;
|
|
392
|
+
if (bufio->filefunc64.zseek64_file == NULL)
|
|
393
|
+
return ret;
|
|
394
|
+
ret = fseek_buf_internal_func(opaque, stream, offset, origin);
|
|
395
|
+
if (ret == 1)
|
|
396
|
+
ret = bufio->filefunc64.zseek64_file(bufio->filefunc64.opaque, streamio->stream, offset, origin);
|
|
397
|
+
return ret;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
int ZCALLBACK fclose_buf_func(voidpf opaque, voidpf stream)
|
|
401
|
+
{
|
|
402
|
+
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
403
|
+
ourstream_t *streamio = (ourstream_t *)stream;
|
|
404
|
+
int ret = 0;
|
|
405
|
+
fflush_buf(opaque, stream);
|
|
406
|
+
print_buf(opaque, stream, "close\n");
|
|
407
|
+
if (streamio->readbuf_hits + streamio->readbuf_misses > 0)
|
|
408
|
+
print_buf(opaque, stream, "read efficency %.02f%%\n", (streamio->readbuf_hits / ((float)streamio->readbuf_hits + streamio->readbuf_misses)) * 100);
|
|
409
|
+
if (streamio->writebuf_hits + streamio->writebuf_misses > 0)
|
|
410
|
+
print_buf(opaque, stream, "write efficency %.02f%%\n", (streamio->writebuf_hits / ((float)streamio->writebuf_hits + streamio->writebuf_misses)) * 100);
|
|
411
|
+
if (bufio->filefunc64.zclose_file != NULL)
|
|
412
|
+
ret = bufio->filefunc64.zclose_file(bufio->filefunc64.opaque, streamio->stream);
|
|
413
|
+
else
|
|
414
|
+
ret = bufio->filefunc.zclose_file(bufio->filefunc.opaque, streamio->stream);
|
|
415
|
+
free(streamio);
|
|
416
|
+
return ret;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
int ZCALLBACK ferror_buf_func(voidpf opaque, voidpf stream)
|
|
420
|
+
{
|
|
421
|
+
ourbuffer_t *bufio = (ourbuffer_t *)opaque;
|
|
422
|
+
ourstream_t *streamio = (ourstream_t *)stream;
|
|
423
|
+
if (bufio->filefunc64.zerror_file != NULL)
|
|
424
|
+
return bufio->filefunc64.zerror_file(bufio->filefunc64.opaque, streamio->stream);
|
|
425
|
+
return bufio->filefunc.zerror_file(bufio->filefunc.opaque, streamio->stream);
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
void fill_buffer_filefunc(zlib_filefunc_def *pzlib_filefunc_def, ourbuffer_t *ourbuf)
|
|
429
|
+
{
|
|
430
|
+
pzlib_filefunc_def->zopen_file = fopen_buf_func;
|
|
431
|
+
pzlib_filefunc_def->zopendisk_file = fopendisk_buf_func;
|
|
432
|
+
pzlib_filefunc_def->zread_file = fread_buf_func;
|
|
433
|
+
pzlib_filefunc_def->zwrite_file = fwrite_buf_func;
|
|
434
|
+
pzlib_filefunc_def->ztell_file = ftell_buf_func;
|
|
435
|
+
pzlib_filefunc_def->zseek_file = fseek_buf_func;
|
|
436
|
+
pzlib_filefunc_def->zclose_file = fclose_buf_func;
|
|
437
|
+
pzlib_filefunc_def->zerror_file = ferror_buf_func;
|
|
438
|
+
pzlib_filefunc_def->opaque = ourbuf;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
void fill_buffer_filefunc64(zlib_filefunc64_def *pzlib_filefunc_def, ourbuffer_t *ourbuf)
|
|
442
|
+
{
|
|
443
|
+
pzlib_filefunc_def->zopen64_file = fopen64_buf_func;
|
|
444
|
+
pzlib_filefunc_def->zopendisk64_file = fopendisk64_buf_func;
|
|
445
|
+
pzlib_filefunc_def->zread_file = fread_buf_func;
|
|
446
|
+
pzlib_filefunc_def->zwrite_file = fwrite_buf_func;
|
|
447
|
+
pzlib_filefunc_def->ztell64_file = ftell64_buf_func;
|
|
448
|
+
pzlib_filefunc_def->zseek64_file = fseek64_buf_func;
|
|
449
|
+
pzlib_filefunc_def->zclose_file = fclose_buf_func;
|
|
450
|
+
pzlib_filefunc_def->zerror_file = ferror_buf_func;
|
|
451
|
+
pzlib_filefunc_def->opaque = ourbuf;
|
|
452
|
+
}
|