Bump to 1.10.1
[platform/upstream/libzip.git] / lib / zip_buffer.c
1 /*
2  zip_buffer.c -- bounds checked access to memory buffer
3  Copyright (C) 2014-2021 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 <stdlib.h>
35 #include <string.h>
36
37 #include "zipint.h"
38
39 zip_uint8_t *
40 _zip_buffer_data(zip_buffer_t *buffer) {
41     return buffer->data;
42 }
43
44
45 void
46 _zip_buffer_free(zip_buffer_t *buffer) {
47     if (buffer == NULL) {
48         return;
49     }
50
51     if (buffer->free_data) {
52         free(buffer->data);
53     }
54
55     free(buffer);
56 }
57
58
59 bool
60 _zip_buffer_eof(zip_buffer_t *buffer) {
61     return buffer->ok && buffer->offset == buffer->size;
62 }
63
64
65 zip_uint8_t *
66 _zip_buffer_get(zip_buffer_t *buffer, zip_uint64_t length) {
67     zip_uint8_t *data;
68
69     data = _zip_buffer_peek(buffer, length);
70
71     if (data != NULL) {
72         buffer->offset += length;
73     }
74
75     return data;
76 }
77
78
79 zip_uint16_t
80 _zip_buffer_get_16(zip_buffer_t *buffer) {
81     zip_uint8_t *data = _zip_buffer_get(buffer, 2);
82
83     if (data == NULL) {
84         return 0;
85     }
86
87     return (zip_uint16_t)(data[0] + (data[1] << 8));
88 }
89
90
91 zip_uint32_t
92 _zip_buffer_get_32(zip_buffer_t *buffer) {
93     zip_uint8_t *data = _zip_buffer_get(buffer, 4);
94
95     if (data == NULL) {
96         return 0;
97     }
98
99     return ((((((zip_uint32_t)data[3] << 8) + data[2]) << 8) + data[1]) << 8) + data[0];
100 }
101
102
103 zip_uint64_t
104 _zip_buffer_get_64(zip_buffer_t *buffer) {
105     zip_uint8_t *data = _zip_buffer_get(buffer, 8);
106
107     if (data == NULL) {
108         return 0;
109     }
110
111     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];
112 }
113
114
115 zip_uint8_t
116 _zip_buffer_get_8(zip_buffer_t *buffer) {
117     zip_uint8_t *data = _zip_buffer_get(buffer, 1);
118
119     if (data == NULL) {
120         return 0;
121     }
122
123     return data[0];
124 }
125
126
127 zip_uint64_t
128 _zip_buffer_left(zip_buffer_t *buffer) {
129     return buffer->ok ? buffer->size - buffer->offset : 0;
130 }
131
132
133 zip_uint64_t
134 _zip_buffer_read(zip_buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length) {
135     zip_uint64_t copied;
136
137     if (_zip_buffer_left(buffer) < length) {
138         length = _zip_buffer_left(buffer);
139     }
140
141     copied = 0;
142     while (copied < length) {
143         size_t n = ZIP_MIN(length - copied, SIZE_MAX);
144         (void)memcpy_s(data + copied, n, _zip_buffer_get(buffer, n), n);
145         copied += n;
146     }
147
148     return copied;
149 }
150
151
152 zip_buffer_t *
153 _zip_buffer_new(zip_uint8_t *data, zip_uint64_t size) {
154     bool free_data = (data == NULL);
155     zip_buffer_t *buffer;
156
157 #if ZIP_UINT64_MAX > SIZE_MAX
158     if (size > SIZE_MAX) {
159         return NULL;
160     }
161 #endif
162
163     if (data == NULL) {
164         if ((data = (zip_uint8_t *)malloc((size_t)size)) == NULL) {
165             return NULL;
166         }
167     }
168
169     if ((buffer = (zip_buffer_t *)malloc(sizeof(*buffer))) == NULL) {
170         if (free_data) {
171             free(data);
172         }
173         return NULL;
174     }
175
176     buffer->ok = true;
177     buffer->data = data;
178     buffer->size = size;
179     buffer->offset = 0;
180     buffer->free_data = free_data;
181
182     return buffer;
183 }
184
185
186 zip_buffer_t *
187 _zip_buffer_new_from_source(zip_source_t *src, zip_uint64_t size, zip_uint8_t *buf, zip_error_t *error) {
188     zip_buffer_t *buffer;
189
190     if ((buffer = _zip_buffer_new(buf, size)) == NULL) {
191         zip_error_set(error, ZIP_ER_MEMORY, 0);
192         return NULL;
193     }
194
195     if (_zip_read(src, buffer->data, size, error) < 0) {
196         _zip_buffer_free(buffer);
197         return NULL;
198     }
199
200     return buffer;
201 }
202
203
204 zip_uint64_t
205 _zip_buffer_offset(zip_buffer_t *buffer) {
206     return buffer->ok ? buffer->offset : 0;
207 }
208
209
210 bool
211 _zip_buffer_ok(zip_buffer_t *buffer) {
212     return buffer->ok;
213 }
214
215
216 zip_uint8_t *
217 _zip_buffer_peek(zip_buffer_t *buffer, zip_uint64_t length) {
218     zip_uint8_t *data;
219
220     if (!buffer->ok || buffer->offset + length < length || buffer->offset + length > buffer->size) {
221         buffer->ok = false;
222         return NULL;
223     }
224
225     data = buffer->data + buffer->offset;
226     return data;
227 }
228
229 int
230 _zip_buffer_put(zip_buffer_t *buffer, const void *src, size_t length) {
231     zip_uint8_t *dst = _zip_buffer_get(buffer, length);
232
233     if (dst == NULL) {
234         return -1;
235     }
236
237     (void)memcpy_s(dst, length, src, length);
238     return 0;
239 }
240
241
242 int
243 _zip_buffer_put_16(zip_buffer_t *buffer, zip_uint16_t i) {
244     zip_uint8_t *data = _zip_buffer_get(buffer, 2);
245
246     if (data == NULL) {
247         return -1;
248     }
249
250     data[0] = (zip_uint8_t)(i & 0xff);
251     data[1] = (zip_uint8_t)((i >> 8) & 0xff);
252
253     return 0;
254 }
255
256
257 int
258 _zip_buffer_put_32(zip_buffer_t *buffer, zip_uint32_t i) {
259     zip_uint8_t *data = _zip_buffer_get(buffer, 4);
260
261     if (data == NULL) {
262         return -1;
263     }
264
265     data[0] = (zip_uint8_t)(i & 0xff);
266     data[1] = (zip_uint8_t)((i >> 8) & 0xff);
267     data[2] = (zip_uint8_t)((i >> 16) & 0xff);
268     data[3] = (zip_uint8_t)((i >> 24) & 0xff);
269
270     return 0;
271 }
272
273
274 int
275 _zip_buffer_put_64(zip_buffer_t *buffer, zip_uint64_t i) {
276     zip_uint8_t *data = _zip_buffer_get(buffer, 8);
277
278     if (data == NULL) {
279         return -1;
280     }
281
282     data[0] = (zip_uint8_t)(i & 0xff);
283     data[1] = (zip_uint8_t)((i >> 8) & 0xff);
284     data[2] = (zip_uint8_t)((i >> 16) & 0xff);
285     data[3] = (zip_uint8_t)((i >> 24) & 0xff);
286     data[4] = (zip_uint8_t)((i >> 32) & 0xff);
287     data[5] = (zip_uint8_t)((i >> 40) & 0xff);
288     data[6] = (zip_uint8_t)((i >> 48) & 0xff);
289     data[7] = (zip_uint8_t)((i >> 56) & 0xff);
290
291     return 0;
292 }
293
294
295 int
296 _zip_buffer_put_8(zip_buffer_t *buffer, zip_uint8_t i) {
297     zip_uint8_t *data = _zip_buffer_get(buffer, 1);
298
299     if (data == NULL) {
300         return -1;
301     }
302
303     data[0] = i;
304
305     return 0;
306 }
307
308
309 int
310 _zip_buffer_set_offset(zip_buffer_t *buffer, zip_uint64_t offset) {
311     if (offset > buffer->size) {
312         buffer->ok = false;
313         return -1;
314     }
315
316     buffer->ok = true;
317     buffer->offset = offset;
318
319     return 0;
320 }
321
322
323 int
324 _zip_buffer_skip(zip_buffer_t *buffer, zip_uint64_t length) {
325     zip_uint64_t offset = buffer->offset + length;
326
327     if (offset < buffer->offset) {
328         buffer->ok = false;
329         return -1;
330     }
331     return _zip_buffer_set_offset(buffer, offset);
332 }
333
334 zip_uint64_t
335 _zip_buffer_size(zip_buffer_t *buffer) {
336     return buffer->size;
337 }