/src/libzip/lib/zip_source_buffer.c
Line  | Count  | Source  | 
1  |  | /*  | 
2  |  |   zip_source_buffer.c -- create zip data source from buffer  | 
3  |  |   Copyright (C) 1999-2023 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  |  | #ifndef WRITE_FRAGMENT_SIZE  | 
40  | 0  | #define WRITE_FRAGMENT_SIZE (64 * 1024)  | 
41  |  | #endif  | 
42  |  |  | 
43  |  | struct buffer { | 
44  |  |     zip_buffer_fragment_t *fragments; /* fragments */  | 
45  |  |     zip_uint64_t *fragment_offsets;   /* offset of each fragment from start of buffer, nfragments+1 entries */  | 
46  |  |     zip_uint64_t nfragments;          /* number of allocated fragments */  | 
47  |  |     zip_uint64_t fragments_capacity;  /* size of fragments (number of pointers) */  | 
48  |  |  | 
49  |  |     zip_uint64_t first_owned_fragment; /* first fragment to free data from */  | 
50  |  |  | 
51  |  |     zip_uint64_t shared_fragments; /* number of shared fragments */  | 
52  |  |     struct buffer *shared_buffer;  /* buffer fragments are shared with */  | 
53  |  |  | 
54  |  |     zip_uint64_t size;             /* size of buffer */  | 
55  |  |     zip_uint64_t offset;           /* current offset in buffer */  | 
56  |  |     zip_uint64_t current_fragment; /* fragment current offset is in */  | 
57  |  | };  | 
58  |  |  | 
59  |  | typedef struct buffer buffer_t;  | 
60  |  |  | 
61  |  | struct read_data { | 
62  |  |     zip_error_t error;  | 
63  |  |     time_t mtime;  | 
64  |  |     zip_file_attributes_t attributes;  | 
65  |  |     buffer_t *in;  | 
66  |  |     buffer_t *out;  | 
67  |  | };  | 
68  |  |  | 
69  | 0  | #define buffer_capacity(buffer) ((buffer)->fragment_offsets[(buffer)->nfragments])  | 
70  |  | #define buffer_size(buffer) ((buffer)->size)  | 
71  |  |  | 
72  |  | static buffer_t *buffer_clone(buffer_t *buffer, zip_uint64_t length, zip_error_t *error);  | 
73  |  | static zip_uint64_t buffer_find_fragment(const buffer_t *buffer, zip_uint64_t offset);  | 
74  |  | static void buffer_free(buffer_t *buffer);  | 
75  |  | static bool buffer_grow_fragments(buffer_t *buffer, zip_uint64_t capacity, zip_error_t *error);  | 
76  |  | static buffer_t *buffer_new(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int free_data, zip_error_t *error);  | 
77  |  | static zip_int64_t buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length);  | 
78  |  | static int buffer_seek(buffer_t *buffer, void *data, zip_uint64_t len, zip_error_t *error);  | 
79  |  | static zip_int64_t buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip_error_t *);  | 
80  |  |  | 
81  |  | static zip_int64_t read_data(void *, void *, zip_uint64_t, zip_source_cmd_t);  | 
82  |  |  | 
83  |  | zip_source_t *zip_source_buffer_with_attributes_create(const void *data, zip_uint64_t len, int freep, zip_file_attributes_t *attributes, zip_error_t *error);  | 
84  |  | zip_source_t *zip_source_buffer_fragment_with_attributes_create(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int freep, zip_file_attributes_t *attributes, zip_error_t *error);  | 
85  |  |  | 
86  |  |  | 
87  |  | ZIP_EXTERN zip_source_t *  | 
88  | 939  | zip_source_buffer(zip_t *za, const void *data, zip_uint64_t len, int freep) { | 
89  | 939  |     if (za == NULL)  | 
90  | 0  |         return NULL;  | 
91  |  |  | 
92  | 939  |     return zip_source_buffer_with_attributes_create(data, len, freep, NULL, &za->error);  | 
93  | 939  | }  | 
94  |  |  | 
95  |  |  | 
96  |  | ZIP_EXTERN zip_source_t *  | 
97  | 4.80k  | zip_source_buffer_create(const void *data, zip_uint64_t len, int freep, zip_error_t *error) { | 
98  | 4.80k  |     return zip_source_buffer_with_attributes_create(data, len, freep, NULL, error);  | 
99  | 4.80k  | }  | 
100  |  |  | 
101  |  |  | 
102  |  | zip_source_t *  | 
103  | 12.0k  | zip_source_buffer_with_attributes_create(const void *data, zip_uint64_t len, int freep, zip_file_attributes_t *attributes, zip_error_t *error) { | 
104  | 12.0k  |     zip_buffer_fragment_t fragment;  | 
105  |  |  | 
106  | 12.0k  |     if (data == NULL) { | 
107  | 6.33k  |         if (len > 0) { | 
108  | 0  |             zip_error_set(error, ZIP_ER_INVAL, 0);  | 
109  | 0  |             return NULL;  | 
110  | 0  |         }  | 
111  |  |  | 
112  | 6.33k  |         return zip_source_buffer_fragment_with_attributes_create(NULL, 0, freep, attributes, error);  | 
113  | 6.33k  |     }  | 
114  |  |  | 
115  | 5.74k  |     fragment.data = (zip_uint8_t *)data;  | 
116  | 5.74k  |     fragment.length = len;  | 
117  |  |  | 
118  | 5.74k  |     return zip_source_buffer_fragment_with_attributes_create(&fragment, 1, freep, attributes, error);  | 
119  | 12.0k  | }  | 
120  |  |  | 
121  |  |  | 
122  |  | ZIP_EXTERN zip_source_t *  | 
123  | 0  | zip_source_buffer_fragment(zip_t *za, const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int freep) { | 
124  | 0  |     if (za == NULL) { | 
125  | 0  |         return NULL;  | 
126  | 0  |     }  | 
127  |  |  | 
128  | 0  |     return zip_source_buffer_fragment_with_attributes_create(fragments, nfragments, freep, NULL, &za->error);  | 
129  | 0  | }  | 
130  |  |  | 
131  |  |  | 
132  |  | ZIP_EXTERN zip_source_t *  | 
133  | 0  | zip_source_buffer_fragment_create(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int freep, zip_error_t *error) { | 
134  | 0  |     return zip_source_buffer_fragment_with_attributes_create(fragments, nfragments, freep, NULL, error);  | 
135  | 0  | }  | 
136  |  |  | 
137  |  | zip_source_t *  | 
138  | 12.0k  | zip_source_buffer_fragment_with_attributes_create(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int freep, zip_file_attributes_t *attributes, zip_error_t *error) { | 
139  | 12.0k  |     struct read_data *ctx;  | 
140  | 12.0k  |     zip_source_t *zs;  | 
141  | 12.0k  |     buffer_t *buffer;  | 
142  |  |  | 
143  | 12.0k  |     if (fragments == NULL && nfragments > 0) { | 
144  | 0  |         zip_error_set(error, ZIP_ER_INVAL, 0);  | 
145  | 0  |         return NULL;  | 
146  | 0  |     }  | 
147  |  |  | 
148  | 12.0k  |     if ((buffer = buffer_new(fragments, nfragments, freep, error)) == NULL) { | 
149  | 0  |         return NULL;  | 
150  | 0  |     }  | 
151  |  |  | 
152  | 12.0k  |     if ((ctx = (struct read_data *)malloc(sizeof(*ctx))) == NULL) { | 
153  | 0  |         zip_error_set(error, ZIP_ER_MEMORY, 0);  | 
154  | 0  |         buffer_free(buffer);  | 
155  | 0  |         return NULL;  | 
156  | 0  |     }  | 
157  |  |  | 
158  | 12.0k  |     ctx->in = buffer;  | 
159  | 12.0k  |     ctx->out = NULL;  | 
160  | 12.0k  |     ctx->mtime = time(NULL);  | 
161  | 12.0k  |     if (attributes) { | 
162  | 6.33k  |         (void)memcpy_s(&ctx->attributes, sizeof(ctx->attributes), attributes, sizeof(ctx->attributes));  | 
163  | 6.33k  |     }  | 
164  | 5.74k  |     else { | 
165  | 5.74k  |         zip_file_attributes_init(&ctx->attributes);  | 
166  | 5.74k  |     }  | 
167  | 12.0k  |     zip_error_init(&ctx->error);  | 
168  |  |  | 
169  | 12.0k  |     if ((zs = zip_source_function_create(read_data, ctx, error)) == NULL) { | 
170  | 0  |         buffer_free(ctx->in);  | 
171  | 0  |         free(ctx);  | 
172  | 0  |         return NULL;  | 
173  | 0  |     }  | 
174  |  |  | 
175  | 12.0k  |     return zs;  | 
176  | 12.0k  | }  | 
177  |  |  | 
178  |  |  | 
179  |  | zip_source_t *  | 
180  | 0  | zip_source_buffer_with_attributes(zip_t *za, const void *data, zip_uint64_t len, int freep, zip_file_attributes_t *attributes) { | 
181  | 0  |     return zip_source_buffer_with_attributes_create(data, len, freep, attributes, &za->error);  | 
182  | 0  | }  | 
183  |  |  | 
184  |  | static zip_int64_t  | 
185  | 279k  | read_data(void *state, void *data, zip_uint64_t len, zip_source_cmd_t cmd) { | 
186  | 279k  |     struct read_data *ctx = (struct read_data *)state;  | 
187  |  |  | 
188  | 279k  |     switch (cmd) { | 
189  | 0  |     case ZIP_SOURCE_BEGIN_WRITE:  | 
190  | 0  |         if ((ctx->out = buffer_new(NULL, 0, 0, &ctx->error)) == NULL) { | 
191  | 0  |             return -1;  | 
192  | 0  |         }  | 
193  | 0  |         ctx->out->offset = 0;  | 
194  | 0  |         ctx->out->current_fragment = 0;  | 
195  | 0  |         return 0;  | 
196  |  |  | 
197  | 0  |     case ZIP_SOURCE_BEGIN_WRITE_CLONING:  | 
198  | 0  |         if ((ctx->out = buffer_clone(ctx->in, len, &ctx->error)) == NULL) { | 
199  | 0  |             return -1;  | 
200  | 0  |         }  | 
201  | 0  |         ctx->out->offset = len;  | 
202  | 0  |         ctx->out->current_fragment = ctx->out->nfragments;  | 
203  | 0  |         return 0;  | 
204  |  |  | 
205  | 11.0k  |     case ZIP_SOURCE_CLOSE:  | 
206  | 11.0k  |         return 0;  | 
207  |  |  | 
208  | 0  |     case ZIP_SOURCE_COMMIT_WRITE:  | 
209  | 0  |         buffer_free(ctx->in);  | 
210  | 0  |         ctx->in = ctx->out;  | 
211  | 0  |         ctx->out = NULL;  | 
212  | 0  |         return 0;  | 
213  |  |  | 
214  | 5.07k  |     case ZIP_SOURCE_ERROR:  | 
215  | 5.07k  |         return zip_error_to_data(&ctx->error, data, len);  | 
216  |  |  | 
217  | 12.0k  |     case ZIP_SOURCE_FREE:  | 
218  | 12.0k  |         buffer_free(ctx->in);  | 
219  | 12.0k  |         buffer_free(ctx->out);  | 
220  | 12.0k  |         free(ctx);  | 
221  | 12.0k  |         return 0;  | 
222  |  |  | 
223  | 19.2k  |     case ZIP_SOURCE_GET_FILE_ATTRIBUTES: { | 
224  | 19.2k  |         if (len < sizeof(ctx->attributes)) { | 
225  | 0  |             zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);  | 
226  | 0  |             return -1;  | 
227  | 0  |         }  | 
228  |  |  | 
229  | 19.2k  |         (void)memcpy_s(data, sizeof(ctx->attributes), &ctx->attributes, sizeof(ctx->attributes));  | 
230  |  |  | 
231  | 19.2k  |         return sizeof(ctx->attributes);  | 
232  | 19.2k  |     }  | 
233  |  |  | 
234  | 11.0k  |     case ZIP_SOURCE_OPEN:  | 
235  | 11.0k  |         ctx->in->offset = 0;  | 
236  | 11.0k  |         ctx->in->current_fragment = 0;  | 
237  | 11.0k  |         return 0;  | 
238  |  |  | 
239  | 93.9k  |     case ZIP_SOURCE_READ:  | 
240  | 93.9k  |         if (len > ZIP_INT64_MAX) { | 
241  | 0  |             zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);  | 
242  | 0  |             return -1;  | 
243  | 0  |         }  | 
244  | 93.9k  |         return buffer_read(ctx->in, data, len);  | 
245  |  |  | 
246  | 0  |     case ZIP_SOURCE_REMOVE: { | 
247  | 0  |         buffer_t *empty = buffer_new(NULL, 0, 0, &ctx->error);  | 
248  | 0  |         if (empty == NULL) { | 
249  | 0  |             return -1;  | 
250  | 0  |         }  | 
251  |  |  | 
252  | 0  |         buffer_free(ctx->in);  | 
253  | 0  |         ctx->in = empty;  | 
254  | 0  |         return 0;  | 
255  | 0  |     }  | 
256  |  |  | 
257  | 0  |     case ZIP_SOURCE_ROLLBACK_WRITE:  | 
258  | 0  |         buffer_free(ctx->out);  | 
259  | 0  |         ctx->out = NULL;  | 
260  | 0  |         return 0;  | 
261  |  |  | 
262  | 70.4k  |     case ZIP_SOURCE_SEEK:  | 
263  | 70.4k  |         return buffer_seek(ctx->in, data, len, &ctx->error);  | 
264  |  |  | 
265  | 0  |     case ZIP_SOURCE_SEEK_WRITE:  | 
266  | 0  |         return buffer_seek(ctx->out, data, len, &ctx->error);  | 
267  |  |  | 
268  | 39.2k  |     case ZIP_SOURCE_STAT: { | 
269  | 39.2k  |         zip_stat_t *st;  | 
270  |  |  | 
271  | 39.2k  |         if (len < sizeof(*st)) { | 
272  | 0  |             zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);  | 
273  | 0  |             return -1;  | 
274  | 0  |         }  | 
275  |  |  | 
276  | 39.2k  |         st = (zip_stat_t *)data;  | 
277  |  |  | 
278  | 39.2k  |         zip_stat_init(st);  | 
279  | 39.2k  |         st->mtime = ctx->mtime;  | 
280  | 39.2k  |         st->size = ctx->in->size;  | 
281  | 39.2k  |         st->comp_size = st->size;  | 
282  | 39.2k  |         st->comp_method = ZIP_CM_STORE;  | 
283  | 39.2k  |         st->encryption_method = ZIP_EM_NONE;  | 
284  | 39.2k  |         st->valid = ZIP_STAT_MTIME | ZIP_STAT_SIZE | ZIP_STAT_COMP_SIZE | ZIP_STAT_COMP_METHOD | ZIP_STAT_ENCRYPTION_METHOD;  | 
285  |  |  | 
286  | 39.2k  |         return sizeof(*st);  | 
287  | 39.2k  |     }  | 
288  |  |  | 
289  | 12.0k  |     case ZIP_SOURCE_SUPPORTS:  | 
290  | 12.0k  |         return zip_source_make_command_bitmap(ZIP_SOURCE_GET_FILE_ATTRIBUTES, ZIP_SOURCE_OPEN, ZIP_SOURCE_READ, ZIP_SOURCE_CLOSE, ZIP_SOURCE_STAT, ZIP_SOURCE_ERROR, ZIP_SOURCE_FREE, ZIP_SOURCE_SEEK, ZIP_SOURCE_TELL, ZIP_SOURCE_BEGIN_WRITE, ZIP_SOURCE_BEGIN_WRITE_CLONING, ZIP_SOURCE_COMMIT_WRITE, ZIP_SOURCE_REMOVE, ZIP_SOURCE_ROLLBACK_WRITE, ZIP_SOURCE_SEEK_WRITE, ZIP_SOURCE_TELL_WRITE, ZIP_SOURCE_WRITE, ZIP_SOURCE_SUPPORTS_REOPEN, -1);  | 
291  |  |  | 
292  | 4.90k  |     case ZIP_SOURCE_TELL:  | 
293  | 4.90k  |         if (ctx->in->offset > ZIP_INT64_MAX) { | 
294  | 0  |             zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW);  | 
295  | 0  |             return -1;  | 
296  | 0  |         }  | 
297  | 4.90k  |         return (zip_int64_t)ctx->in->offset;  | 
298  |  |  | 
299  |  |  | 
300  | 0  |     case ZIP_SOURCE_TELL_WRITE:  | 
301  | 0  |         if (ctx->out->offset > ZIP_INT64_MAX) { | 
302  | 0  |             zip_error_set(&ctx->error, ZIP_ER_TELL, EOVERFLOW);  | 
303  | 0  |             return -1;  | 
304  | 0  |         }  | 
305  | 0  |         return (zip_int64_t)ctx->out->offset;  | 
306  |  |  | 
307  | 0  |     case ZIP_SOURCE_WRITE:  | 
308  | 0  |         if (len > ZIP_INT64_MAX) { | 
309  | 0  |             zip_error_set(&ctx->error, ZIP_ER_INVAL, 0);  | 
310  | 0  |             return -1;  | 
311  | 0  |         }  | 
312  | 0  |         return buffer_write(ctx->out, data, len, &ctx->error);  | 
313  |  |  | 
314  | 0  |     default:  | 
315  | 0  |         zip_error_set(&ctx->error, ZIP_ER_OPNOTSUPP, 0);  | 
316  | 0  |         return -1;  | 
317  | 279k  |     }  | 
318  | 279k  | }  | 
319  |  |  | 
320  |  |  | 
321  |  | static buffer_t *  | 
322  | 0  | buffer_clone(buffer_t *buffer, zip_uint64_t offset, zip_error_t *error) { | 
323  | 0  |     zip_uint64_t fragment, fragment_offset, waste;  | 
324  | 0  |     buffer_t *clone;  | 
325  |  | 
  | 
326  | 0  |     if (offset == 0) { | 
327  | 0  |         return buffer_new(NULL, 0, 1, error);  | 
328  | 0  |     }  | 
329  |  |  | 
330  | 0  |     if (offset > buffer->size) { | 
331  | 0  |         zip_error_set(error, ZIP_ER_INVAL, 0);  | 
332  | 0  |         return NULL;  | 
333  | 0  |     }  | 
334  | 0  |     if (buffer->shared_buffer != NULL) { | 
335  | 0  |         zip_error_set(error, ZIP_ER_INUSE, 0);  | 
336  | 0  |         return NULL;  | 
337  | 0  |     }  | 
338  |  |  | 
339  | 0  |     fragment = buffer_find_fragment(buffer, offset);  | 
340  | 0  |     fragment_offset = offset - buffer->fragment_offsets[fragment];  | 
341  |  | 
  | 
342  | 0  |     if (fragment_offset == 0) { | 
343  |  |         /* We can't be at beginning of fragment zero if offset > 0. */  | 
344  | 0  |         fragment--;  | 
345  | 0  |         fragment_offset = buffer->fragments[fragment].length;  | 
346  | 0  |     }  | 
347  |  |  | 
348  |  |     /* TODO: This should also consider the length of the fully shared fragments */  | 
349  | 0  |     waste = buffer->fragments[fragment].length - fragment_offset;  | 
350  | 0  |     if (waste > offset) { | 
351  | 0  |         zip_error_set(error, ZIP_ER_OPNOTSUPP, 0);  | 
352  | 0  |         return NULL;  | 
353  | 0  |     }  | 
354  |  |  | 
355  | 0  |     if ((clone = buffer_new(buffer->fragments, fragment + 1, 0, error)) == NULL) { | 
356  | 0  |         return NULL;  | 
357  | 0  |     }  | 
358  |  |  | 
359  | 0  | #ifndef __clang_analyzer__  | 
360  |  |     /* clone->fragments can't be null, since it was created with at least one fragment */  | 
361  | 0  |     clone->fragments[fragment].length = fragment_offset;  | 
362  | 0  | #endif  | 
363  | 0  |     clone->fragment_offsets[clone->nfragments] = offset;  | 
364  | 0  |     clone->size = offset;  | 
365  |  | 
  | 
366  | 0  |     clone->first_owned_fragment = ZIP_MIN(buffer->first_owned_fragment, clone->nfragments);  | 
367  |  | 
  | 
368  | 0  |     buffer->shared_buffer = clone;  | 
369  | 0  |     clone->shared_buffer = buffer;  | 
370  | 0  |     buffer->shared_fragments = fragment + 1;  | 
371  | 0  |     clone->shared_fragments = fragment + 1;  | 
372  |  |       | 
373  | 0  |     return clone;  | 
374  | 0  | }  | 
375  |  |  | 
376  |  |  | 
377  |  | static zip_uint64_t  | 
378  | 65.3k  | buffer_find_fragment(const buffer_t *buffer, zip_uint64_t offset) { | 
379  | 65.3k  |     zip_uint64_t low, high, mid;  | 
380  |  |  | 
381  | 65.3k  |     if (buffer->nfragments == 0) { | 
382  | 1.66k  |         return 0;  | 
383  | 1.66k  |     }  | 
384  |  |  | 
385  | 63.6k  |     low = 0;  | 
386  | 63.6k  |     high = buffer->nfragments - 1;  | 
387  |  |  | 
388  | 63.6k  |     while (low < high) { | 
389  | 0  |         mid = (high - low) / 2 + low;  | 
390  | 0  |         if (buffer->fragment_offsets[mid] > offset) { | 
391  | 0  |             high = mid - 1;  | 
392  | 0  |         }  | 
393  | 0  |         else if (mid == buffer->nfragments || buffer->fragment_offsets[mid + 1] > offset) { | 
394  | 0  |             return mid;  | 
395  | 0  |         }  | 
396  | 0  |         else { | 
397  | 0  |             low = mid + 1;  | 
398  | 0  |         }  | 
399  | 0  |     }  | 
400  |  |  | 
401  | 63.6k  |     return low;  | 
402  | 63.6k  | }  | 
403  |  |  | 
404  |  |  | 
405  |  | static void  | 
406  | 24.1k  | buffer_free(buffer_t *buffer) { | 
407  | 24.1k  |     zip_uint64_t i;  | 
408  |  |  | 
409  | 24.1k  |     if (buffer == NULL) { | 
410  | 12.0k  |         return;  | 
411  | 12.0k  |     }  | 
412  |  |  | 
413  | 12.0k  |     if (buffer->shared_buffer != NULL) { | 
414  | 0  |         buffer->shared_buffer->shared_buffer = NULL;  | 
415  | 0  |         buffer->shared_buffer->shared_fragments = 0;  | 
416  |  | 
  | 
417  | 0  |         buffer->first_owned_fragment = ZIP_MAX(buffer->first_owned_fragment, buffer->shared_fragments);  | 
418  | 0  |     }  | 
419  |  |  | 
420  | 12.0k  |     for (i = buffer->first_owned_fragment; i < buffer->nfragments; i++) { | 
421  | 0  |         free(buffer->fragments[i].data);  | 
422  | 0  |     }  | 
423  | 12.0k  |     free(buffer->fragments);  | 
424  | 12.0k  |     free(buffer->fragment_offsets);  | 
425  | 12.0k  |     free(buffer);  | 
426  | 12.0k  | }  | 
427  |  |  | 
428  |  |  | 
429  |  | static bool  | 
430  | 5.74k  | buffer_grow_fragments(buffer_t *buffer, zip_uint64_t capacity, zip_error_t *error) { | 
431  | 5.74k  |     zip_uint64_t additional_fragments;  | 
432  | 5.74k  |     zip_uint64_t offset_capacity = buffer->fragments_capacity + 1;  | 
433  |  |  | 
434  | 5.74k  |     if (capacity <= buffer->fragments_capacity) { | 
435  | 0  |         return true;  | 
436  | 0  |     }  | 
437  |  |  | 
438  | 5.74k  |     additional_fragments = capacity - buffer->fragments_capacity;  | 
439  |  |  | 
440  | 5.74k  |     if (!ZIP_REALLOC(buffer->fragments, buffer->fragments_capacity, additional_fragments, error)) { | 
441  | 0  |         return false;  | 
442  | 0  |     }  | 
443  |  |     /* The size of both buffer->fragments and buffer->fragment_offsets is stored in buffer->fragments_capacity, so use a temporary capacity variable here for reallocating buffer->fragment_offsets. */  | 
444  | 5.74k  |     if (!ZIP_REALLOC(buffer->fragment_offsets, offset_capacity, additional_fragments, error)) { | 
445  | 0  |         buffer->fragments_capacity -= additional_fragments;  | 
446  | 0  |         return false;  | 
447  | 0  |     }  | 
448  |  |  | 
449  | 5.74k  |     return true;  | 
450  | 5.74k  | }  | 
451  |  |  | 
452  |  |  | 
453  |  | static buffer_t *  | 
454  | 12.0k  | buffer_new(const zip_buffer_fragment_t *fragments, zip_uint64_t nfragments, int free_data, zip_error_t *error) { | 
455  | 12.0k  |     buffer_t *buffer;  | 
456  |  |  | 
457  | 12.0k  |     if ((buffer = malloc(sizeof(*buffer))) == NULL) { | 
458  | 0  |         return NULL;  | 
459  | 0  |     }  | 
460  |  |  | 
461  | 12.0k  |     buffer->offset = 0;  | 
462  | 12.0k  |     buffer->first_owned_fragment = 0;  | 
463  | 12.0k  |     buffer->size = 0;  | 
464  | 12.0k  |     buffer->fragments = NULL;  | 
465  | 12.0k  |     buffer->fragment_offsets = NULL;  | 
466  | 12.0k  |     buffer->nfragments = 0;  | 
467  | 12.0k  |     buffer->fragments_capacity = 0;  | 
468  | 12.0k  |     buffer->shared_buffer = NULL;  | 
469  | 12.0k  |     buffer->shared_fragments = 0;  | 
470  |  |  | 
471  | 12.0k  |     if (nfragments == 0) { | 
472  | 6.33k  |         if ((buffer->fragment_offsets = malloc(sizeof(buffer->fragment_offsets[0]))) == NULL) { | 
473  | 0  |             free(buffer);  | 
474  | 0  |             zip_error_set(error, ZIP_ER_MEMORY, 0);  | 
475  | 0  |             return NULL;  | 
476  | 0  |         }  | 
477  | 6.33k  |         buffer->fragment_offsets[0] = 0;  | 
478  | 6.33k  |     }  | 
479  | 5.74k  |     else { | 
480  | 5.74k  |         zip_uint64_t i, j, offset;  | 
481  |  |  | 
482  | 5.74k  |         if (!buffer_grow_fragments(buffer, nfragments, NULL)) { | 
483  | 0  |             zip_error_set(error, ZIP_ER_MEMORY, 0);  | 
484  | 0  |             buffer_free(buffer);  | 
485  | 0  |             return NULL;  | 
486  | 0  |         }  | 
487  |  |  | 
488  | 5.74k  |         offset = 0;  | 
489  | 11.4k  |         for (i = 0, j = 0; i < nfragments; i++) { | 
490  | 5.74k  |             if (fragments[i].length == 0) { | 
491  | 0  |                 continue;  | 
492  | 0  |             }  | 
493  | 5.74k  |             if (fragments[i].data == NULL) { | 
494  | 0  |                 zip_error_set(error, ZIP_ER_INVAL, 0);  | 
495  | 0  |                 buffer_free(buffer);  | 
496  | 0  |                 return NULL;  | 
497  | 0  |             }  | 
498  | 5.74k  |             buffer->fragments[j].data = fragments[i].data;  | 
499  | 5.74k  |             buffer->fragments[j].length = fragments[i].length;  | 
500  | 5.74k  |             buffer->fragment_offsets[i] = offset;  | 
501  |  |             /* TODO: overflow */  | 
502  | 5.74k  |             offset += fragments[i].length;  | 
503  | 5.74k  |             j++;  | 
504  | 5.74k  |         }  | 
505  | 5.74k  |         buffer->nfragments = j;  | 
506  | 5.74k  |         buffer->first_owned_fragment = free_data ? 0 : buffer->nfragments;  | 
507  | 5.74k  |         buffer->fragment_offsets[buffer->nfragments] = offset;  | 
508  | 5.74k  |         buffer->size = offset;  | 
509  | 5.74k  |     }  | 
510  |  |  | 
511  | 12.0k  |     return buffer;  | 
512  | 12.0k  | }  | 
513  |  |  | 
514  |  | static zip_int64_t  | 
515  | 93.9k  | buffer_read(buffer_t *buffer, zip_uint8_t *data, zip_uint64_t length) { | 
516  | 93.9k  |     zip_uint64_t n, i, fragment_offset;  | 
517  |  |  | 
518  | 93.9k  |     length = ZIP_MIN(length, buffer->size - buffer->offset);  | 
519  |  |  | 
520  | 93.9k  |     if (length == 0) { | 
521  | 10.4k  |         return 0;  | 
522  | 10.4k  |     }  | 
523  | 83.5k  |     if (length > ZIP_INT64_MAX) { | 
524  | 0  |         return -1;  | 
525  | 0  |     }  | 
526  |  |  | 
527  | 83.5k  |     i = buffer->current_fragment;  | 
528  | 83.5k  |     fragment_offset = buffer->offset - buffer->fragment_offsets[i];  | 
529  | 83.5k  |     n = 0;  | 
530  | 167k  |     while (n < length) { | 
531  | 83.5k  |         zip_uint64_t left = ZIP_MIN(length - n, buffer->fragments[i].length - fragment_offset);  | 
532  |  | #if ZIP_UINT64_MAX > SIZE_MAX  | 
533  |  |         left = ZIP_MIN(left, SIZE_MAX);  | 
534  |  | #endif  | 
535  |  |  | 
536  | 83.5k  |         (void)memcpy_s(data + n, (size_t)left, buffer->fragments[i].data + fragment_offset, (size_t)left);  | 
537  |  |  | 
538  | 83.5k  |         if (left == buffer->fragments[i].length - fragment_offset) { | 
539  | 9.89k  |             i++;  | 
540  | 9.89k  |         }  | 
541  | 83.5k  |         n += left;  | 
542  | 83.5k  |         fragment_offset = 0;  | 
543  | 83.5k  |     }  | 
544  |  |  | 
545  | 83.5k  |     buffer->offset += n;  | 
546  | 83.5k  |     buffer->current_fragment = i;  | 
547  | 83.5k  |     return (zip_int64_t)n;  | 
548  | 83.5k  | }  | 
549  |  |  | 
550  |  |  | 
551  |  | static int  | 
552  | 70.4k  | buffer_seek(buffer_t *buffer, void *data, zip_uint64_t len, zip_error_t *error) { | 
553  | 70.4k  |     zip_int64_t new_offset = zip_source_seek_compute_offset(buffer->offset, buffer->size, data, len, error);  | 
554  |  |  | 
555  | 70.4k  |     if (new_offset < 0) { | 
556  | 5.07k  |         return -1;  | 
557  | 5.07k  |     }  | 
558  |  |  | 
559  | 65.3k  |     buffer->offset = (zip_uint64_t)new_offset;  | 
560  | 65.3k  |     buffer->current_fragment = buffer_find_fragment(buffer, buffer->offset);  | 
561  | 65.3k  |     return 0;  | 
562  | 70.4k  | }  | 
563  |  |  | 
564  |  |  | 
565  |  | static zip_int64_t  | 
566  | 0  | buffer_write(buffer_t *buffer, const zip_uint8_t *data, zip_uint64_t length, zip_error_t *error) { | 
567  | 0  |     zip_uint64_t copied, i, fragment_offset, capacity;  | 
568  |  | 
  | 
569  | 0  |     if (buffer->offset + length + WRITE_FRAGMENT_SIZE - 1 < length) { | 
570  | 0  |         zip_error_set(error, ZIP_ER_INVAL, 0);  | 
571  | 0  |         return -1;  | 
572  | 0  |     }  | 
573  |  |  | 
574  |  |     /* grow buffer if needed */  | 
575  | 0  |     capacity = buffer_capacity(buffer);  | 
576  | 0  |     if (buffer->offset + length > capacity) { | 
577  | 0  |         zip_uint64_t needed_fragments = buffer->nfragments + (length - (capacity - buffer->offset) + WRITE_FRAGMENT_SIZE - 1) / WRITE_FRAGMENT_SIZE;  | 
578  |  | 
  | 
579  | 0  |         if (needed_fragments > buffer->fragments_capacity) { | 
580  | 0  |             zip_uint64_t new_capacity = buffer->fragments_capacity;  | 
581  |  | 
  | 
582  | 0  |             if (new_capacity == 0) { | 
583  | 0  |                 new_capacity = 16;  | 
584  | 0  |             }  | 
585  | 0  |             while (new_capacity < needed_fragments) { | 
586  | 0  |                 new_capacity *= 2;  | 
587  | 0  |             }  | 
588  |  | 
  | 
589  | 0  |             if (!buffer_grow_fragments(buffer, new_capacity, error)) { | 
590  | 0  |                 zip_error_set(error, ZIP_ER_MEMORY, 0);  | 
591  | 0  |                 return -1;  | 
592  | 0  |             }  | 
593  | 0  |         }  | 
594  |  |  | 
595  | 0  |         while (buffer->nfragments < needed_fragments) { | 
596  | 0  |             if ((buffer->fragments[buffer->nfragments].data = malloc(WRITE_FRAGMENT_SIZE)) == NULL) { | 
597  | 0  |                 zip_error_set(error, ZIP_ER_MEMORY, 0);  | 
598  | 0  |                 return -1;  | 
599  | 0  |             }  | 
600  | 0  |             buffer->fragments[buffer->nfragments].length = WRITE_FRAGMENT_SIZE;  | 
601  | 0  |             buffer->nfragments++;  | 
602  | 0  |             capacity += WRITE_FRAGMENT_SIZE;  | 
603  | 0  |             buffer->fragment_offsets[buffer->nfragments] = capacity;  | 
604  | 0  |         }  | 
605  | 0  |     }  | 
606  |  |  | 
607  | 0  |     i = buffer->current_fragment;  | 
608  | 0  |     fragment_offset = buffer->offset - buffer->fragment_offsets[i];  | 
609  | 0  |     copied = 0;  | 
610  | 0  |     while (copied < length) { | 
611  | 0  |         zip_uint64_t n = ZIP_MIN(ZIP_MIN(length - copied, buffer->fragments[i].length - fragment_offset), SIZE_MAX);  | 
612  |  | #if ZIP_UINT64_MAX > SIZE_MAX  | 
613  |  |         n = ZIP_MIN(n, SIZE_MAX);  | 
614  |  | #endif  | 
615  |  | 
  | 
616  | 0  |         (void)memcpy_s(buffer->fragments[i].data + fragment_offset, (size_t)n, data + copied, (size_t)n);  | 
617  |  | 
  | 
618  | 0  |         if (n == buffer->fragments[i].length - fragment_offset) { | 
619  | 0  |             i++;  | 
620  | 0  |             fragment_offset = 0;  | 
621  | 0  |         }  | 
622  | 0  |         else { | 
623  | 0  |             fragment_offset += n;  | 
624  | 0  |         }  | 
625  | 0  |         copied += n;  | 
626  | 0  |     }  | 
627  |  | 
  | 
628  | 0  |     buffer->offset += copied;  | 
629  | 0  |     buffer->current_fragment = i;  | 
630  | 0  |     if (buffer->offset > buffer->size) { | 
631  | 0  |         buffer->size = buffer->offset;  | 
632  | 0  |     }  | 
633  |  | 
  | 
634  | 0  |     return (zip_int64_t)copied;  | 
635  | 0  | }  |