/src/libzip/lib/zip_buffer.c
Line | Count | Source |
1 | | /* |
2 | | zip_buffer.c -- bounds checked access to memory buffer |
3 | | Copyright (C) 2014-2025 Dieter Baron and Thomas Klausner |
4 | | |
5 | | This file is part of libzip, a library to manipulate ZIP archives. |
6 | | The authors can be contacted at <info@libzip.org> |
7 | | |
8 | | Redistribution and use in source and binary forms, with or without |
9 | | modification, are permitted provided that the following conditions |
10 | | are met: |
11 | | 1. Redistributions of source code must retain the above copyright |
12 | | notice, this list of conditions and the following disclaimer. |
13 | | 2. Redistributions in binary form must reproduce the above copyright |
14 | | notice, this list of conditions and the following disclaimer in |
15 | | the documentation and/or other materials provided with the |
16 | | distribution. |
17 | | 3. The names of the authors may not be used to endorse or promote |
18 | | products derived from this software without specific prior |
19 | | written permission. |
20 | | |
21 | | THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS |
22 | | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
23 | | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
24 | | ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY |
25 | | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
26 | | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
27 | | GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
28 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
29 | | IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
30 | | OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
31 | | IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
32 | | */ |
33 | | |
34 | | #include "zipint.h" |
35 | | |
36 | | #include <stdlib.h> |
37 | | #include <string.h> |
38 | | |
39 | 0 | zip_uint8_t *_zip_buffer_data(zip_buffer_t *buffer) { |
40 | 0 | return buffer->data; |
41 | 0 | } |
42 | | |
43 | | |
44 | 0 | void _zip_buffer_free(zip_buffer_t *buffer) { |
45 | 0 | if (buffer == NULL) { |
46 | 0 | return; |
47 | 0 | } |
48 | | |
49 | 0 | if (buffer->free_data) { |
50 | 0 | free(buffer->data); |
51 | 0 | } |
52 | |
|
53 | 0 | free(buffer); |
54 | 0 | } |
55 | | |
56 | | |
57 | 0 | bool _zip_buffer_eof(zip_buffer_t *buffer) { |
58 | 0 | return buffer->ok && buffer->offset == buffer->size; |
59 | 0 | } |
60 | | |
61 | | |
62 | 0 | zip_uint8_t *_zip_buffer_get(zip_buffer_t *buffer, zip_uint64_t length) { |
63 | 0 | zip_uint8_t *data; |
64 | |
|
65 | 0 | data = _zip_buffer_peek(buffer, length); |
66 | |
|
67 | 0 | if (data != NULL) { |
68 | 0 | buffer->offset += length; |
69 | 0 | } |
70 | |
|
71 | 0 | return data; |
72 | 0 | } |
73 | | |
74 | | |
75 | 0 | zip_uint16_t _zip_buffer_get_16(zip_buffer_t *buffer) { |
76 | 0 | zip_uint8_t *data = _zip_buffer_get(buffer, 2); |
77 | |
|
78 | 0 | if (data == NULL) { |
79 | 0 | return 0; |
80 | 0 | } |
81 | | |
82 | 0 | return (zip_uint16_t)(data[0] + (data[1] << 8)); |
83 | 0 | } |
84 | | |
85 | | |
86 | 0 | zip_uint32_t _zip_buffer_get_32(zip_buffer_t *buffer) { |
87 | 0 | zip_uint8_t *data = _zip_buffer_get(buffer, 4); |
88 | |
|
89 | 0 | if (data == NULL) { |
90 | 0 | return 0; |
91 | 0 | } |
92 | | |
93 | 0 | return ((((((zip_uint32_t)data[3] << 8) + data[2]) << 8) + data[1]) << 8) + data[0]; |
94 | 0 | } |
95 | | |
96 | | |
97 | 0 | zip_uint64_t _zip_buffer_get_64(zip_buffer_t *buffer) { |
98 | 0 | zip_uint8_t *data = _zip_buffer_get(buffer, 8); |
99 | |
|
100 | 0 | if (data == NULL) { |
101 | 0 | return 0; |
102 | 0 | } |
103 | | |
104 | 0 | return ((zip_uint64_t)data[7] << 56) + ((zip_uint64_t)data[6] << 48) + ((zip_uint64_t)data[5] << 40) + ((zip_uint64_t)data[4] << 32) + ((zip_uint64_t)data[3] << 24) + ((zip_uint64_t)data[2] << 16) + ((zip_uint64_t)data[1] << 8) + (zip_uint64_t)data[0]; |
105 | 0 | } |
106 | | |
107 | | |
108 | 0 | zip_uint8_t _zip_buffer_get_8(zip_buffer_t *buffer) { |
109 | 0 | zip_uint8_t *data = _zip_buffer_get(buffer, 1); |
110 | |
|
111 | 0 | if (data == NULL) { |
112 | 0 | return 0; |
113 | 0 | } |
114 | | |
115 | 0 | return data[0]; |
116 | 0 | } |
117 | | |
118 | | |
119 | 0 | zip_uint64_t _zip_buffer_left(zip_buffer_t *buffer) { |
120 | 0 | return buffer->ok ? buffer->size - buffer->offset : 0; |
121 | 0 | } |
122 | | |
123 | | |
124 | 0 | zip_uint64_t _zip_buffer_read(zip_buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length) { |
125 | 0 | zip_uint64_t copied; |
126 | |
|
127 | 0 | if (_zip_buffer_left(buffer) < length) { |
128 | 0 | length = _zip_buffer_left(buffer); |
129 | 0 | } |
130 | |
|
131 | 0 | copied = 0; |
132 | 0 | while (copied < length) { |
133 | 0 | size_t n = ZIP_MIN(length - copied, SIZE_MAX); |
134 | 0 | (void)memcpy_s(data + copied, n, _zip_buffer_get(buffer, n), n); |
135 | 0 | copied += n; |
136 | 0 | } |
137 | |
|
138 | 0 | return copied; |
139 | 0 | } |
140 | | |
141 | | |
142 | 0 | zip_buffer_t *_zip_buffer_new(zip_uint8_t *data, zip_uint64_t size) { |
143 | 0 | bool free_data = (data == NULL); |
144 | 0 | zip_buffer_t *buffer; |
145 | |
|
146 | | #if ZIP_UINT64_MAX > SIZE_MAX |
147 | | if (size > SIZE_MAX) { |
148 | | return NULL; |
149 | | } |
150 | | #endif |
151 | |
|
152 | 0 | if (data == NULL) { |
153 | 0 | if ((data = (zip_uint8_t *)malloc((size_t)size)) == NULL) { |
154 | 0 | return NULL; |
155 | 0 | } |
156 | 0 | } |
157 | | |
158 | 0 | if ((buffer = (zip_buffer_t *)malloc(sizeof(*buffer))) == NULL) { |
159 | 0 | if (free_data) { |
160 | 0 | free(data); |
161 | 0 | } |
162 | 0 | return NULL; |
163 | 0 | } |
164 | | |
165 | 0 | buffer->ok = true; |
166 | 0 | buffer->data = data; |
167 | 0 | buffer->size = size; |
168 | 0 | buffer->offset = 0; |
169 | 0 | buffer->free_data = free_data; |
170 | |
|
171 | 0 | return buffer; |
172 | 0 | } |
173 | | |
174 | | |
175 | 0 | zip_buffer_t *_zip_buffer_new_from_source(zip_source_t *src, zip_uint64_t size, zip_uint8_t *buf, zip_error_t *error) { |
176 | 0 | zip_buffer_t *buffer; |
177 | |
|
178 | 0 | if ((buffer = _zip_buffer_new(buf, size)) == NULL) { |
179 | 0 | zip_error_set(error, ZIP_ER_MEMORY, 0); |
180 | 0 | return NULL; |
181 | 0 | } |
182 | | |
183 | 0 | if (_zip_read(src, buffer->data, size, error) < 0) { |
184 | 0 | _zip_buffer_free(buffer); |
185 | 0 | return NULL; |
186 | 0 | } |
187 | | |
188 | 0 | return buffer; |
189 | 0 | } |
190 | | |
191 | | |
192 | 0 | zip_uint64_t _zip_buffer_offset(zip_buffer_t *buffer) { |
193 | 0 | return buffer->ok ? buffer->offset : 0; |
194 | 0 | } |
195 | | |
196 | | |
197 | 0 | bool _zip_buffer_ok(zip_buffer_t *buffer) { |
198 | 0 | return buffer->ok; |
199 | 0 | } |
200 | | |
201 | | |
202 | 0 | zip_uint8_t *_zip_buffer_peek(zip_buffer_t *buffer, zip_uint64_t length) { |
203 | 0 | zip_uint8_t *data; |
204 | |
|
205 | 0 | if (!buffer->ok || buffer->offset + length < length || buffer->offset + length > buffer->size) { |
206 | 0 | buffer->ok = false; |
207 | 0 | return NULL; |
208 | 0 | } |
209 | | |
210 | 0 | data = buffer->data + buffer->offset; |
211 | 0 | return data; |
212 | 0 | } |
213 | | |
214 | 0 | int _zip_buffer_put(zip_buffer_t *buffer, const void *src, size_t length) { |
215 | 0 | zip_uint8_t *dst = _zip_buffer_get(buffer, length); |
216 | |
|
217 | 0 | if (dst == NULL) { |
218 | 0 | return -1; |
219 | 0 | } |
220 | | |
221 | 0 | (void)memcpy_s(dst, length, src, length); |
222 | 0 | return 0; |
223 | 0 | } |
224 | | |
225 | | |
226 | 0 | int _zip_buffer_put_16(zip_buffer_t *buffer, zip_uint16_t i) { |
227 | 0 | zip_uint8_t *data = _zip_buffer_get(buffer, 2); |
228 | |
|
229 | 0 | if (data == NULL) { |
230 | 0 | return -1; |
231 | 0 | } |
232 | | |
233 | 0 | data[0] = (zip_uint8_t)(i & 0xff); |
234 | 0 | data[1] = (zip_uint8_t)((i >> 8) & 0xff); |
235 | |
|
236 | 0 | return 0; |
237 | 0 | } |
238 | | |
239 | | |
240 | 0 | int _zip_buffer_put_32(zip_buffer_t *buffer, zip_uint32_t i) { |
241 | 0 | zip_uint8_t *data = _zip_buffer_get(buffer, 4); |
242 | |
|
243 | 0 | if (data == NULL) { |
244 | 0 | return -1; |
245 | 0 | } |
246 | | |
247 | 0 | data[0] = (zip_uint8_t)(i & 0xff); |
248 | 0 | data[1] = (zip_uint8_t)((i >> 8) & 0xff); |
249 | 0 | data[2] = (zip_uint8_t)((i >> 16) & 0xff); |
250 | 0 | data[3] = (zip_uint8_t)((i >> 24) & 0xff); |
251 | |
|
252 | 0 | return 0; |
253 | 0 | } |
254 | | |
255 | | |
256 | 0 | int _zip_buffer_put_64(zip_buffer_t *buffer, zip_uint64_t i) { |
257 | 0 | zip_uint8_t *data = _zip_buffer_get(buffer, 8); |
258 | |
|
259 | 0 | if (data == NULL) { |
260 | 0 | return -1; |
261 | 0 | } |
262 | | |
263 | 0 | data[0] = (zip_uint8_t)(i & 0xff); |
264 | 0 | data[1] = (zip_uint8_t)((i >> 8) & 0xff); |
265 | 0 | data[2] = (zip_uint8_t)((i >> 16) & 0xff); |
266 | 0 | data[3] = (zip_uint8_t)((i >> 24) & 0xff); |
267 | 0 | data[4] = (zip_uint8_t)((i >> 32) & 0xff); |
268 | 0 | data[5] = (zip_uint8_t)((i >> 40) & 0xff); |
269 | 0 | data[6] = (zip_uint8_t)((i >> 48) & 0xff); |
270 | 0 | data[7] = (zip_uint8_t)((i >> 56) & 0xff); |
271 | |
|
272 | 0 | return 0; |
273 | 0 | } |
274 | | |
275 | | |
276 | 0 | int _zip_buffer_put_8(zip_buffer_t *buffer, zip_uint8_t i) { |
277 | 0 | zip_uint8_t *data = _zip_buffer_get(buffer, 1); |
278 | |
|
279 | 0 | if (data == NULL) { |
280 | 0 | return -1; |
281 | 0 | } |
282 | | |
283 | 0 | data[0] = i; |
284 | |
|
285 | 0 | return 0; |
286 | 0 | } |
287 | | |
288 | | |
289 | 0 | int _zip_buffer_set_offset(zip_buffer_t *buffer, zip_uint64_t offset) { |
290 | 0 | if (offset > buffer->size) { |
291 | 0 | buffer->ok = false; |
292 | 0 | return -1; |
293 | 0 | } |
294 | | |
295 | 0 | buffer->ok = true; |
296 | 0 | buffer->offset = offset; |
297 | |
|
298 | 0 | return 0; |
299 | 0 | } |
300 | | |
301 | | |
302 | 0 | int _zip_buffer_skip(zip_buffer_t *buffer, zip_uint64_t length) { |
303 | 0 | zip_uint64_t offset = buffer->offset + length; |
304 | |
|
305 | 0 | if (offset < buffer->offset) { |
306 | 0 | buffer->ok = false; |
307 | 0 | return -1; |
308 | 0 | } |
309 | 0 | return _zip_buffer_set_offset(buffer, offset); |
310 | 0 | } |
311 | | |
312 | 0 | zip_uint64_t _zip_buffer_size(zip_buffer_t *buffer) { |
313 | 0 | return buffer->size; |
314 | 0 | } |