forked from zlib-ng/minizip-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mz_strm_mem.c
284 lines (229 loc) · 6.61 KB
/
mz_strm_mem.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
/* mz_strm_mem.c -- Stream for memory access
Version 2.3.8, July 14, 2018
part of the MiniZip project
This interface is designed to access memory rather than files.
We do use a region of memory to put data in to and take it out of.
Based on Unzip ioapi.c version 0.22, May 19th, 2003
Copyright (C) 2010-2018 Nathan Moinvaziri
https://github.com/nmoinvaz/minizip
Copyright (C) 2003 Justin Fletcher
Copyright (C) 1998-2003 Gilles Vollant
http://www.winimage.com/zLibDll/minizip.html
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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "mz.h"
#include "mz_strm.h"
#include "mz_strm_mem.h"
/***************************************************************************/
static mz_stream_vtbl mz_stream_mem_vtbl = {
mz_stream_mem_open,
mz_stream_mem_is_open,
mz_stream_mem_read,
mz_stream_mem_write,
mz_stream_mem_tell,
mz_stream_mem_seek,
mz_stream_mem_close,
mz_stream_mem_error,
mz_stream_mem_create,
mz_stream_mem_delete,
NULL,
NULL
};
/***************************************************************************/
typedef struct mz_stream_mem_s {
mz_stream stream;
int32_t mode;
char *buffer; // Memory buffer pointer
int32_t size; // Size of the memory buffer
int32_t limit; // Furthest we've written
int32_t position; // Current position in the memory
int32_t grow_size; // Size to grow when full
} mz_stream_mem;
/***************************************************************************/
static void mz_stream_mem_set_size(void *stream, int32_t size)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
int32_t new_size = size;
char *new_buf = NULL;
new_buf = (char *)MZ_ALLOC(new_size);
if (mem->buffer)
{
memcpy(new_buf, mem->buffer, mem->size);
MZ_FREE(mem->buffer);
}
mem->buffer = new_buf;
mem->size = new_size;
}
int32_t mz_stream_mem_open(void *stream, const char *path, int32_t mode)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
MZ_UNUSED(path);
mem->mode = mode;
mem->limit = 0;
mem->position = 0;
if (mem->mode & MZ_OPEN_MODE_CREATE)
mz_stream_mem_set_size(stream, mem->grow_size);
else
mem->limit = mem->size;
return MZ_OK;
}
int32_t mz_stream_mem_is_open(void *stream)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
if (mem->buffer == NULL)
return MZ_STREAM_ERROR;
return MZ_OK;
}
int32_t mz_stream_mem_read(void *stream, void *buf, int32_t size)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
if (size > mem->size - mem->position)
size = mem->size - mem->position;
if (mem->position + size > mem->limit)
return 0;
memcpy(buf, mem->buffer + mem->position, size);
mem->position += size;
return size;
}
int32_t mz_stream_mem_write(void *stream, const void *buf, int32_t size)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
int32_t new_size = 0;
if (size == 0)
return size;
if (size > mem->size - mem->position)
{
if (mem->mode & MZ_OPEN_MODE_CREATE)
{
new_size = mem->size;
if (size < mem->grow_size)
new_size += mem->grow_size;
else
new_size += size;
mz_stream_mem_set_size(stream, new_size);
}
else
{
size = mem->size - mem->position;
}
}
memcpy(mem->buffer + mem->position, buf, size);
mem->position += size;
if (mem->position > mem->limit)
mem->limit = mem->position;
return size;
}
int64_t mz_stream_mem_tell(void *stream)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
return mem->position;
}
int32_t mz_stream_mem_seek(void *stream, int64_t offset, int32_t origin)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
int64_t new_pos = 0;
switch (origin)
{
case MZ_SEEK_CUR:
new_pos = mem->position + offset;
break;
case MZ_SEEK_END:
new_pos = mem->limit + offset;
break;
case MZ_SEEK_SET:
new_pos = offset;
break;
default:
return MZ_STREAM_ERROR;
}
if (new_pos > mem->size)
{
if ((mem->mode & MZ_OPEN_MODE_CREATE) == 0)
return MZ_STREAM_ERROR;
mz_stream_mem_set_size(stream, (int32_t)new_pos);
}
mem->position = (uint32_t)new_pos;
return MZ_OK;
}
int32_t mz_stream_mem_close(void *stream)
{
MZ_UNUSED(stream);
// We never return errors
return MZ_OK;
}
int32_t mz_stream_mem_error(void *stream)
{
MZ_UNUSED(stream);
// We never return errors
return MZ_OK;
}
void mz_stream_mem_set_buffer(void *stream, void *buf, int32_t size)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
mem->buffer = buf;
mem->size = size;
mem->limit = size;
}
int32_t mz_stream_mem_get_buffer(void *stream, const void **buf)
{
return mz_stream_mem_get_buffer_at(stream, 0, buf);
}
int32_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, const void **buf)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
if (buf == NULL || position < 0 || mem->size < position || mem->buffer == NULL)
return MZ_STREAM_ERROR;
*buf = mem->buffer + position;
return MZ_OK;
}
void mz_stream_mem_get_buffer_length(void *stream, int32_t *length)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
*length = mem->limit;
}
void mz_stream_mem_set_buffer_limit(void *stream, int32_t limit)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
mem->limit = limit;
}
void mz_stream_mem_set_grow_size(void *stream, int32_t grow_size)
{
mz_stream_mem *mem = (mz_stream_mem *)stream;
mem->grow_size = grow_size;
}
void *mz_stream_mem_create(void **stream)
{
mz_stream_mem *mem = NULL;
mem = (mz_stream_mem *)MZ_ALLOC(sizeof(mz_stream_mem));
if (mem != NULL)
{
memset(mem, 0, sizeof(mz_stream_mem));
mem->stream.vtbl = &mz_stream_mem_vtbl;
mem->grow_size = 4096;
}
if (stream != NULL)
*stream = mem;
return mem;
}
void mz_stream_mem_delete(void **stream)
{
mz_stream_mem *mem = NULL;
if (stream == NULL)
return;
mem = (mz_stream_mem *)*stream;
if (mem != NULL)
{
if ((mem->mode & MZ_OPEN_MODE_CREATE) && (mem->buffer != NULL))
MZ_FREE(mem->buffer);
MZ_FREE(mem);
}
*stream = NULL;
}
void *mz_stream_mem_get_interface(void)
{
return (void *)&mz_stream_mem_vtbl;
}