Coverage Report

Created: 2025-11-27 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/minizip-ng/mz_strm_mem.c
Line
Count
Source
1
/* mz_strm_mem.c -- Stream for memory access
2
   part of the minizip-ng project
3
4
   This interface is designed to access memory rather than files.
5
   We do use a region of memory to put data in to and take it out of.
6
7
   Based on Unzip ioapi.c version 0.22, May 19th, 2003
8
9
   Copyright (C) Nathan Moinvaziri
10
     https://github.com/zlib-ng/minizip-ng
11
   Copyright (C) 2003 Justin Fletcher
12
   Copyright (C) 1998-2003 Gilles Vollant
13
     https://www.winimage.com/zLibDll/minizip.html
14
15
   This program is distributed under the terms of the same license as zlib.
16
   See the accompanying LICENSE file for the full text of the license.
17
*/
18
19
#include "mz.h"
20
#include "mz_strm.h"
21
#include "mz_strm_mem.h"
22
23
/***************************************************************************/
24
25
static mz_stream_vtbl mz_stream_mem_vtbl = {mz_stream_mem_open,
26
                                            mz_stream_mem_is_open,
27
                                            mz_stream_mem_read,
28
                                            mz_stream_mem_write,
29
                                            mz_stream_mem_tell,
30
                                            mz_stream_mem_seek,
31
                                            mz_stream_mem_close,
32
                                            mz_stream_mem_error,
33
                                            mz_stream_mem_create,
34
                                            mz_stream_mem_delete,
35
                                            NULL,
36
                                            NULL};
37
38
/***************************************************************************/
39
40
typedef struct mz_stream_mem_s {
41
    mz_stream stream;
42
    int32_t mode;
43
    uint8_t *buffer;   /* Memory buffer pointer */
44
    int32_t size;      /* Size of the memory buffer */
45
    int32_t limit;     /* Furthest we've written */
46
    int32_t position;  /* Current position in the memory */
47
    int32_t grow_size; /* Size to grow when full */
48
} mz_stream_mem;
49
50
/***************************************************************************/
51
52
11.2k
static int32_t mz_stream_mem_set_size(void *stream, int32_t size) {
53
11.2k
    mz_stream_mem *mem = (mz_stream_mem *)stream;
54
11.2k
    int32_t new_size = size;
55
11.2k
    uint8_t *new_buf = NULL;
56
57
11.2k
    new_buf = (uint8_t *)malloc((uint32_t)new_size);
58
11.2k
    if (!new_buf)
59
0
        return MZ_BUF_ERROR;
60
61
11.2k
    if (mem->buffer) {
62
2.81k
        memcpy(new_buf, mem->buffer, mem->size);
63
2.81k
        free(mem->buffer);
64
2.81k
    }
65
66
11.2k
    mem->buffer = new_buf;
67
11.2k
    mem->size = new_size;
68
11.2k
    return MZ_OK;
69
11.2k
}
70
71
8.45k
int32_t mz_stream_mem_open(void *stream, const char *path, int32_t mode) {
72
8.45k
    mz_stream_mem *mem = (mz_stream_mem *)stream;
73
8.45k
    int32_t err = MZ_OK;
74
75
8.45k
    MZ_UNUSED(path);
76
77
8.45k
    mem->mode = mode;
78
8.45k
    mem->limit = 0;
79
8.45k
    mem->position = 0;
80
81
8.45k
    if (mem->mode & MZ_OPEN_MODE_CREATE)
82
8.45k
        err = mz_stream_mem_set_size(stream, mem->grow_size);
83
0
    else
84
0
        mem->limit = mem->size;
85
86
8.45k
    return err;
87
8.45k
}
88
89
16.0M
int32_t mz_stream_mem_is_open(void *stream) {
90
16.0M
    mz_stream_mem *mem = (mz_stream_mem *)stream;
91
16.0M
    if (!mem->buffer)
92
4.06k
        return MZ_OPEN_ERROR;
93
16.0M
    return MZ_OK;
94
16.0M
}
95
96
8.29M
int32_t mz_stream_mem_read(void *stream, void *buf, int32_t size) {
97
8.29M
    mz_stream_mem *mem = (mz_stream_mem *)stream;
98
99
8.29M
    if (size > mem->size - mem->position)
100
37.3k
        size = mem->size - mem->position;
101
8.29M
    if (mem->position + size > mem->limit)
102
4.58k
        size = mem->limit - mem->position;
103
104
8.29M
    if (size <= 0)
105
26.8k
        return 0;
106
107
8.27M
    memcpy(buf, mem->buffer + mem->position, size);
108
8.27M
    mem->position += size;
109
110
8.27M
    return size;
111
8.29M
}
112
113
4.38M
int32_t mz_stream_mem_write(void *stream, const void *buf, int32_t size) {
114
4.38M
    mz_stream_mem *mem = (mz_stream_mem *)stream;
115
4.38M
    int32_t new_size = 0;
116
4.38M
    int32_t err = MZ_OK;
117
118
4.38M
    if (!size)
119
0
        return size;
120
121
4.38M
    if (size > mem->size - mem->position) {
122
2.79k
        if (mem->mode & MZ_OPEN_MODE_CREATE) {
123
2.79k
            new_size = mem->size;
124
2.79k
            if (size < mem->grow_size)
125
1.65k
                new_size += mem->grow_size;
126
1.13k
            else
127
1.13k
                new_size += size;
128
129
2.79k
            err = mz_stream_mem_set_size(stream, new_size);
130
2.79k
            if (err != MZ_OK)
131
0
                return err;
132
2.79k
        } else {
133
0
            size = mem->size - mem->position;
134
0
        }
135
2.79k
    }
136
137
4.38M
    memcpy(mem->buffer + mem->position, buf, size);
138
139
4.38M
    mem->position += size;
140
4.38M
    if (mem->position > mem->limit)
141
2.29M
        mem->limit = mem->position;
142
143
4.38M
    return size;
144
4.38M
}
145
146
1.89M
int64_t mz_stream_mem_tell(void *stream) {
147
1.89M
    mz_stream_mem *mem = (mz_stream_mem *)stream;
148
1.89M
    return mem->position;
149
1.89M
}
150
151
1.45M
int32_t mz_stream_mem_seek(void *stream, int64_t offset, int32_t origin) {
152
1.45M
    mz_stream_mem *mem = (mz_stream_mem *)stream;
153
1.45M
    int64_t new_pos = 0;
154
1.45M
    int32_t err = MZ_OK;
155
156
1.45M
    switch (origin) {
157
208k
    case MZ_SEEK_CUR:
158
208k
        new_pos = mem->position + offset;
159
208k
        break;
160
4.47k
    case MZ_SEEK_END:
161
4.47k
        new_pos = mem->limit + offset;
162
4.47k
        break;
163
1.24M
    case MZ_SEEK_SET:
164
1.24M
        new_pos = offset;
165
1.24M
        break;
166
0
    default:
167
0
        return MZ_SEEK_ERROR;
168
1.45M
    }
169
170
    // While `malloc` accepts a size_t, mz_stream_mem_s.size only supports an int32_t
171
1.45M
    if (new_pos < 0 || new_pos > INT32_MAX) {
172
26.1k
        return MZ_SEEK_ERROR;
173
26.1k
    }
174
175
1.42M
    if (new_pos > mem->size) {
176
17.6k
        if ((mem->mode & MZ_OPEN_MODE_CREATE) == 0)
177
17.5k
            return MZ_SEEK_ERROR;
178
179
18
        err = mz_stream_mem_set_size(stream, (int32_t)new_pos);
180
18
        if (err != MZ_OK)
181
0
            return err;
182
18
    }
183
184
1.41M
    mem->position = (int32_t)new_pos;
185
1.41M
    return MZ_OK;
186
1.42M
}
187
188
7.42k
int32_t mz_stream_mem_close(void *stream) {
189
7.42k
    MZ_UNUSED(stream);
190
191
    /* We never return errors */
192
7.42k
    return MZ_OK;
193
7.42k
}
194
195
17.1k
int32_t mz_stream_mem_error(void *stream) {
196
17.1k
    MZ_UNUSED(stream);
197
198
    /* We never return errors */
199
17.1k
    return MZ_OK;
200
17.1k
}
201
202
20.4k
void mz_stream_mem_set_buffer(void *stream, void *buf, int32_t size) {
203
20.4k
    mz_stream_mem *mem = (mz_stream_mem *)stream;
204
20.4k
    mem->buffer = (uint8_t *)buf;
205
20.4k
    mem->size = size;
206
20.4k
    mem->limit = size;
207
20.4k
}
208
209
437k
int32_t mz_stream_mem_get_buffer(void *stream, const void **buf) {
210
437k
    return mz_stream_mem_get_buffer_at(stream, 0, buf);
211
437k
}
212
213
1.75M
int32_t mz_stream_mem_get_buffer_at(void *stream, int64_t position, const void **buf) {
214
1.75M
    mz_stream_mem *mem = (mz_stream_mem *)stream;
215
1.75M
    if (!buf || position < 0 || !mem->buffer || mem->size < position)
216
0
        return MZ_SEEK_ERROR;
217
1.75M
    *buf = mem->buffer + position;
218
1.75M
    return MZ_OK;
219
1.75M
}
220
221
0
int32_t mz_stream_mem_get_buffer_at_current(void *stream, const void **buf) {
222
0
    mz_stream_mem *mem = (mz_stream_mem *)stream;
223
0
    return mz_stream_mem_get_buffer_at(stream, mem->position, buf);
224
0
}
225
226
0
void mz_stream_mem_get_buffer_length(void *stream, int32_t *length) {
227
0
    mz_stream_mem *mem = (mz_stream_mem *)stream;
228
0
    *length = mem->limit;
229
0
}
230
231
890
void mz_stream_mem_set_buffer_limit(void *stream, int32_t limit) {
232
890
    mz_stream_mem *mem = (mz_stream_mem *)stream;
233
890
    mem->limit = limit;
234
890
}
235
236
0
void mz_stream_mem_set_grow_size(void *stream, int32_t grow_size) {
237
0
    mz_stream_mem *mem = (mz_stream_mem *)stream;
238
0
    mem->grow_size = grow_size;
239
0
}
240
241
31.9k
void *mz_stream_mem_create(void) {
242
31.9k
    mz_stream_mem *mem = (mz_stream_mem *)calloc(1, sizeof(mz_stream_mem));
243
31.9k
    if (mem) {
244
31.9k
        mem->stream.vtbl = &mz_stream_mem_vtbl;
245
31.9k
        mem->grow_size = 4096;
246
31.9k
    }
247
31.9k
    return mem;
248
31.9k
}
249
250
31.9k
void mz_stream_mem_delete(void **stream) {
251
31.9k
    mz_stream_mem *mem = NULL;
252
31.9k
    if (!stream)
253
0
        return;
254
31.9k
    mem = (mz_stream_mem *)*stream;
255
31.9k
    if (mem) {
256
31.9k
        if ((mem->mode & MZ_OPEN_MODE_CREATE) && (mem->buffer))
257
8.45k
            free(mem->buffer);
258
31.9k
        free(mem);
259
31.9k
    }
260
31.9k
    *stream = NULL;
261
31.9k
}
262
263
0
void *mz_stream_mem_get_interface(void) {
264
0
    return (void *)&mz_stream_mem_vtbl;
265
0
}