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