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