/src/libarchive/libarchive/archive_read.c
Line | Count | Source |
1 | | /*- |
2 | | * Copyright (c) 2003-2011 Tim Kientzle |
3 | | * All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that the following conditions |
7 | | * are met: |
8 | | * 1. Redistributions of source code must retain the above copyright |
9 | | * notice, this list of conditions and the following disclaimer. |
10 | | * 2. Redistributions in binary form must reproduce the above copyright |
11 | | * notice, this list of conditions and the following disclaimer in the |
12 | | * documentation and/or other materials provided with the distribution. |
13 | | * |
14 | | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR |
15 | | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
16 | | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
17 | | * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, |
18 | | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
19 | | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
20 | | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
21 | | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
23 | | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 | | */ |
25 | | |
26 | | /* |
27 | | * This file contains the "essential" portions of the read API, that |
28 | | * is, stuff that will probably always be used by any client that |
29 | | * actually needs to read an archive. Optional pieces have been, as |
30 | | * far as possible, separated out into separate files to avoid |
31 | | * needlessly bloating statically-linked clients. |
32 | | */ |
33 | | |
34 | | #include "archive_platform.h" |
35 | | |
36 | | #ifdef HAVE_ERRNO_H |
37 | | #include <errno.h> |
38 | | #endif |
39 | | #include <stdio.h> |
40 | | #ifdef HAVE_STDLIB_H |
41 | | #include <stdlib.h> |
42 | | #endif |
43 | | #ifdef HAVE_STRING_H |
44 | | #include <string.h> |
45 | | #endif |
46 | | #ifdef HAVE_UNISTD_H |
47 | | #include <unistd.h> |
48 | | #endif |
49 | | |
50 | | #include "archive.h" |
51 | | #include "archive_entry.h" |
52 | | #include "archive_private.h" |
53 | | #include "archive_read_private.h" |
54 | | |
55 | 11.0M | #define minimum(a, b) (a < b ? a : b) |
56 | | |
57 | | static int choose_filters(struct archive_read *); |
58 | | static int choose_format(struct archive_read *); |
59 | | static int close_filters(struct archive_read *); |
60 | | static int64_t _archive_filter_bytes(struct archive *, int); |
61 | | static int _archive_filter_code(struct archive *, int); |
62 | | static const char *_archive_filter_name(struct archive *, int); |
63 | | static int _archive_filter_count(struct archive *); |
64 | | static int _archive_read_close(struct archive *); |
65 | | static int _archive_read_data_block(struct archive *, |
66 | | const void **, size_t *, int64_t *); |
67 | | static int _archive_read_free(struct archive *); |
68 | | static int _archive_read_next_header(struct archive *, |
69 | | struct archive_entry **); |
70 | | static int _archive_read_next_header2(struct archive *, |
71 | | struct archive_entry *); |
72 | | static int64_t advance_file_pointer(struct archive_read_filter *, int64_t); |
73 | | |
74 | | static const struct archive_vtable |
75 | | archive_read_vtable = { |
76 | | .archive_filter_bytes = _archive_filter_bytes, |
77 | | .archive_filter_code = _archive_filter_code, |
78 | | .archive_filter_name = _archive_filter_name, |
79 | | .archive_filter_count = _archive_filter_count, |
80 | | .archive_read_data_block = _archive_read_data_block, |
81 | | .archive_read_next_header = _archive_read_next_header, |
82 | | .archive_read_next_header2 = _archive_read_next_header2, |
83 | | .archive_free = _archive_read_free, |
84 | | .archive_close = _archive_read_close, |
85 | | }; |
86 | | |
87 | | /* |
88 | | * Allocate, initialize and return a struct archive object. |
89 | | */ |
90 | | struct archive * |
91 | | archive_read_new(void) |
92 | 347 | { |
93 | 347 | struct archive_read *a; |
94 | | |
95 | 347 | a = calloc(1, sizeof(*a)); |
96 | 347 | if (a == NULL) |
97 | 0 | return (NULL); |
98 | 347 | a->archive.magic = ARCHIVE_READ_MAGIC; |
99 | | |
100 | 347 | a->archive.state = ARCHIVE_STATE_NEW; |
101 | 347 | a->entry = archive_entry_new2(&a->archive); |
102 | 347 | a->archive.vtable = &archive_read_vtable; |
103 | | |
104 | 347 | a->passphrases.last = &a->passphrases.first; |
105 | | |
106 | 347 | return (&a->archive); |
107 | 347 | } |
108 | | |
109 | | /* |
110 | | * Record the do-not-extract-to file. This belongs in archive_read_extract.c. |
111 | | */ |
112 | | void |
113 | | archive_read_extract_set_skip_file(struct archive *_a, la_int64_t d, |
114 | | la_int64_t i) |
115 | 0 | { |
116 | 0 | struct archive_read *a = (struct archive_read *)_a; |
117 | |
|
118 | 0 | if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_MAGIC, |
119 | 0 | ARCHIVE_STATE_ANY, "archive_read_extract_set_skip_file")) |
120 | 0 | return; |
121 | 0 | a->skip_file_set = 1; |
122 | 0 | a->skip_file_dev = d; |
123 | 0 | a->skip_file_ino = i; |
124 | 0 | } |
125 | | |
126 | | /* |
127 | | * Open the archive |
128 | | */ |
129 | | int |
130 | | archive_read_open(struct archive *a, void *client_data, |
131 | | archive_open_callback *client_opener, archive_read_callback *client_reader, |
132 | | archive_close_callback *client_closer) |
133 | 0 | { |
134 | | /* Old archive_read_open() is just a thin shell around |
135 | | * archive_read_open1. */ |
136 | 0 | archive_read_set_open_callback(a, client_opener); |
137 | 0 | archive_read_set_read_callback(a, client_reader); |
138 | 0 | archive_read_set_close_callback(a, client_closer); |
139 | 0 | archive_read_set_callback_data(a, client_data); |
140 | 0 | return archive_read_open1(a); |
141 | 0 | } |
142 | | |
143 | | |
144 | | int |
145 | | archive_read_open2(struct archive *a, void *client_data, |
146 | | archive_open_callback *client_opener, |
147 | | archive_read_callback *client_reader, |
148 | | archive_skip_callback *client_skipper, |
149 | | archive_close_callback *client_closer) |
150 | 0 | { |
151 | | /* Old archive_read_open2() is just a thin shell around |
152 | | * archive_read_open1. */ |
153 | 0 | archive_read_set_callback_data(a, client_data); |
154 | 0 | archive_read_set_open_callback(a, client_opener); |
155 | 0 | archive_read_set_read_callback(a, client_reader); |
156 | 0 | archive_read_set_skip_callback(a, client_skipper); |
157 | 0 | archive_read_set_close_callback(a, client_closer); |
158 | 0 | return archive_read_open1(a); |
159 | 0 | } |
160 | | |
161 | | static ssize_t |
162 | | client_read_proxy(struct archive_read_filter *self, const void **buff) |
163 | 1.08k | { |
164 | 1.08k | ssize_t r; |
165 | 1.08k | r = (self->archive->client.reader)(&self->archive->archive, |
166 | 1.08k | self->data, buff); |
167 | 1.08k | return (r); |
168 | 1.08k | } |
169 | | |
170 | | static int64_t |
171 | | client_skip_proxy(struct archive_read_filter *self, int64_t request) |
172 | 5 | { |
173 | 5 | if (request < 0) |
174 | 0 | __archive_errx(1, "Negative skip requested."); |
175 | 5 | if (request == 0) |
176 | 0 | return 0; |
177 | | |
178 | 5 | if (self->archive->client.skipper != NULL) { |
179 | 5 | int64_t total = 0; |
180 | 5 | for (;;) { |
181 | 5 | int64_t get, ask = request; |
182 | 5 | get = (self->archive->client.skipper) |
183 | 5 | (&self->archive->archive, self->data, ask); |
184 | 5 | total += get; |
185 | 5 | if (get == 0 || get == request) |
186 | 5 | return (total); |
187 | 0 | if (get > request) |
188 | 0 | return ARCHIVE_FATAL; |
189 | 0 | request -= get; |
190 | 0 | } |
191 | 5 | } else if (self->archive->client.seeker != NULL |
192 | 0 | && request > 64 * 1024) { |
193 | | /* If the client provided a seeker but not a skipper, |
194 | | * we can use the seeker to skip forward. |
195 | | * |
196 | | * Note: This isn't always a good idea. The client |
197 | | * skipper is allowed to skip by less than requested |
198 | | * if it needs to maintain block alignment. The |
199 | | * seeker is not allowed to play such games, so using |
200 | | * the seeker here may be a performance loss compared |
201 | | * to just reading and discarding. That's why we |
202 | | * only do this for skips of over 64k. |
203 | | */ |
204 | 0 | int64_t before = self->position; |
205 | 0 | int64_t after = (self->archive->client.seeker) |
206 | 0 | (&self->archive->archive, self->data, request, SEEK_CUR); |
207 | 0 | if (after != before + request) |
208 | 0 | return ARCHIVE_FATAL; |
209 | 0 | return after - before; |
210 | 0 | } |
211 | 0 | return 0; |
212 | 5 | } |
213 | | |
214 | | static int64_t |
215 | | client_seek_proxy(struct archive_read_filter *self, int64_t offset, int whence) |
216 | 1.45k | { |
217 | | /* DO NOT use the skipper here! If we transparently handled |
218 | | * forward seek here by using the skipper, that will break |
219 | | * other libarchive code that assumes a successful forward |
220 | | * seek means it can also seek backwards. |
221 | | */ |
222 | 1.45k | if (self->archive->client.seeker == NULL) { |
223 | 0 | archive_set_error(&self->archive->archive, ARCHIVE_ERRNO_MISC, |
224 | 0 | "Current client reader does not support seeking a device"); |
225 | 0 | return (ARCHIVE_FAILED); |
226 | 0 | } |
227 | 1.45k | return (self->archive->client.seeker)(&self->archive->archive, |
228 | 1.45k | self->data, offset, whence); |
229 | 1.45k | } |
230 | | |
231 | | static int |
232 | | read_client_close_proxy(struct archive_read *a) |
233 | 347 | { |
234 | 347 | int r = ARCHIVE_OK, r2; |
235 | 347 | unsigned int i; |
236 | | |
237 | 347 | if (a->client.closer == NULL) |
238 | 0 | return (r); |
239 | 694 | for (i = 0; i < a->client.nodes; i++) |
240 | 347 | { |
241 | 347 | r2 = (a->client.closer) |
242 | 347 | ((struct archive *)a, a->client.dataset[i].data); |
243 | 347 | if (r > r2) |
244 | 0 | r = r2; |
245 | 347 | } |
246 | 347 | return (r); |
247 | 347 | } |
248 | | |
249 | | static int |
250 | | client_close_proxy(struct archive_read_filter *self) |
251 | 347 | { |
252 | 347 | return read_client_close_proxy(self->archive); |
253 | 347 | } |
254 | | |
255 | | static int |
256 | | client_open_proxy(struct archive_read_filter *self) |
257 | 0 | { |
258 | 0 | int r = ARCHIVE_OK; |
259 | 0 | if (self->archive->client.opener != NULL) |
260 | 0 | r = (self->archive->client.opener)( |
261 | 0 | (struct archive *)self->archive, self->data); |
262 | 0 | return (r); |
263 | 0 | } |
264 | | |
265 | | static int |
266 | | client_switch_proxy(struct archive_read_filter *self, unsigned int iindex) |
267 | 1.49k | { |
268 | 1.49k | int r1 = ARCHIVE_OK, r2 = ARCHIVE_OK; |
269 | 1.49k | void *data2 = NULL; |
270 | | |
271 | | /* Don't do anything if already in the specified data node */ |
272 | 1.49k | if (self->archive->client.cursor == iindex) |
273 | 1.49k | return (ARCHIVE_OK); |
274 | | |
275 | 0 | self->archive->client.cursor = iindex; |
276 | 0 | data2 = self->archive->client.dataset[self->archive->client.cursor].data; |
277 | 0 | if (self->archive->client.switcher != NULL) |
278 | 0 | { |
279 | 0 | r1 = r2 = (self->archive->client.switcher) |
280 | 0 | ((struct archive *)self->archive, self->data, data2); |
281 | 0 | self->data = data2; |
282 | 0 | } |
283 | 0 | else |
284 | 0 | { |
285 | | /* Attempt to call close and open instead */ |
286 | 0 | if (self->archive->client.closer != NULL) |
287 | 0 | r1 = (self->archive->client.closer) |
288 | 0 | ((struct archive *)self->archive, self->data); |
289 | 0 | self->data = data2; |
290 | 0 | r2 = client_open_proxy(self); |
291 | 0 | } |
292 | 0 | return (r1 < r2) ? r1 : r2; |
293 | 1.49k | } |
294 | | |
295 | | int |
296 | | archive_read_set_open_callback(struct archive *_a, |
297 | | archive_open_callback *client_opener) |
298 | 347 | { |
299 | 347 | struct archive_read *a = (struct archive_read *)_a; |
300 | 347 | archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, |
301 | 347 | "archive_read_set_open_callback"); |
302 | 347 | a->client.opener = client_opener; |
303 | 347 | return ARCHIVE_OK; |
304 | 347 | } |
305 | | |
306 | | int |
307 | | archive_read_set_read_callback(struct archive *_a, |
308 | | archive_read_callback *client_reader) |
309 | 347 | { |
310 | 347 | struct archive_read *a = (struct archive_read *)_a; |
311 | 347 | archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, |
312 | 347 | "archive_read_set_read_callback"); |
313 | 347 | a->client.reader = client_reader; |
314 | 347 | return ARCHIVE_OK; |
315 | 347 | } |
316 | | |
317 | | int |
318 | | archive_read_set_skip_callback(struct archive *_a, |
319 | | archive_skip_callback *client_skipper) |
320 | 347 | { |
321 | 347 | struct archive_read *a = (struct archive_read *)_a; |
322 | 347 | archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, |
323 | 347 | "archive_read_set_skip_callback"); |
324 | 347 | a->client.skipper = client_skipper; |
325 | 347 | return ARCHIVE_OK; |
326 | 347 | } |
327 | | |
328 | | int |
329 | | archive_read_set_seek_callback(struct archive *_a, |
330 | | archive_seek_callback *client_seeker) |
331 | 347 | { |
332 | 347 | struct archive_read *a = (struct archive_read *)_a; |
333 | 347 | archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, |
334 | 347 | "archive_read_set_seek_callback"); |
335 | 347 | a->client.seeker = client_seeker; |
336 | 347 | return ARCHIVE_OK; |
337 | 347 | } |
338 | | |
339 | | int |
340 | | archive_read_set_close_callback(struct archive *_a, |
341 | | archive_close_callback *client_closer) |
342 | 347 | { |
343 | 347 | struct archive_read *a = (struct archive_read *)_a; |
344 | 347 | archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, |
345 | 347 | "archive_read_set_close_callback"); |
346 | 347 | a->client.closer = client_closer; |
347 | 347 | return ARCHIVE_OK; |
348 | 347 | } |
349 | | |
350 | | int |
351 | | archive_read_set_switch_callback(struct archive *_a, |
352 | | archive_switch_callback *client_switcher) |
353 | 0 | { |
354 | 0 | struct archive_read *a = (struct archive_read *)_a; |
355 | 0 | archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, |
356 | 0 | "archive_read_set_switch_callback"); |
357 | 0 | a->client.switcher = client_switcher; |
358 | 0 | return ARCHIVE_OK; |
359 | 0 | } |
360 | | |
361 | | int |
362 | | archive_read_set_callback_data(struct archive *_a, void *client_data) |
363 | 347 | { |
364 | 347 | return archive_read_set_callback_data2(_a, client_data, 0); |
365 | 347 | } |
366 | | |
367 | | int |
368 | | archive_read_set_callback_data2(struct archive *_a, void *client_data, |
369 | | unsigned int iindex) |
370 | 347 | { |
371 | 347 | struct archive_read *a = (struct archive_read *)_a; |
372 | 347 | archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, |
373 | 347 | "archive_read_set_callback_data2"); |
374 | | |
375 | 347 | if (a->client.nodes == 0) |
376 | 347 | { |
377 | 347 | a->client.dataset = (struct archive_read_data_node *) |
378 | 347 | calloc(1, sizeof(*a->client.dataset)); |
379 | 347 | if (a->client.dataset == NULL) |
380 | 0 | { |
381 | 0 | archive_set_error(&a->archive, ENOMEM, |
382 | 0 | "No memory."); |
383 | 0 | return ARCHIVE_FATAL; |
384 | 0 | } |
385 | 347 | a->client.nodes = 1; |
386 | 347 | } |
387 | | |
388 | 347 | if (iindex > a->client.nodes - 1) |
389 | 0 | { |
390 | 0 | archive_set_error(&a->archive, EINVAL, |
391 | 0 | "Invalid index specified."); |
392 | 0 | return ARCHIVE_FATAL; |
393 | 0 | } |
394 | 347 | a->client.dataset[iindex].data = client_data; |
395 | 347 | a->client.dataset[iindex].begin_position = -1; |
396 | 347 | a->client.dataset[iindex].total_size = -1; |
397 | 347 | return ARCHIVE_OK; |
398 | 347 | } |
399 | | |
400 | | int |
401 | | archive_read_add_callback_data(struct archive *_a, void *client_data, |
402 | | unsigned int iindex) |
403 | 0 | { |
404 | 0 | struct archive_read *a = (struct archive_read *)_a; |
405 | 0 | void *p; |
406 | 0 | unsigned int i; |
407 | |
|
408 | 0 | archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, |
409 | 0 | "archive_read_add_callback_data"); |
410 | 0 | if (iindex > a->client.nodes) { |
411 | 0 | archive_set_error(&a->archive, EINVAL, |
412 | 0 | "Invalid index specified."); |
413 | 0 | return ARCHIVE_FATAL; |
414 | 0 | } |
415 | 0 | p = realloc(a->client.dataset, sizeof(*a->client.dataset) |
416 | 0 | * (++(a->client.nodes))); |
417 | 0 | if (p == NULL) { |
418 | 0 | archive_set_error(&a->archive, ENOMEM, |
419 | 0 | "No memory."); |
420 | 0 | return ARCHIVE_FATAL; |
421 | 0 | } |
422 | 0 | a->client.dataset = (struct archive_read_data_node *)p; |
423 | 0 | for (i = a->client.nodes - 1; i > iindex; i--) { |
424 | 0 | a->client.dataset[i].data = a->client.dataset[i-1].data; |
425 | 0 | a->client.dataset[i].begin_position = -1; |
426 | 0 | a->client.dataset[i].total_size = -1; |
427 | 0 | } |
428 | 0 | a->client.dataset[iindex].data = client_data; |
429 | 0 | a->client.dataset[iindex].begin_position = -1; |
430 | 0 | a->client.dataset[iindex].total_size = -1; |
431 | 0 | return ARCHIVE_OK; |
432 | 0 | } |
433 | | |
434 | | int |
435 | | archive_read_append_callback_data(struct archive *_a, void *client_data) |
436 | 0 | { |
437 | 0 | struct archive_read *a = (struct archive_read *)_a; |
438 | 0 | return archive_read_add_callback_data(_a, client_data, a->client.nodes); |
439 | 0 | } |
440 | | |
441 | | int |
442 | | archive_read_prepend_callback_data(struct archive *_a, void *client_data) |
443 | 0 | { |
444 | 0 | return archive_read_add_callback_data(_a, client_data, 0); |
445 | 0 | } |
446 | | |
447 | | static const struct archive_read_filter_vtable |
448 | | none_reader_vtable = { |
449 | | .read = client_read_proxy, |
450 | | .close = client_close_proxy, |
451 | | }; |
452 | | |
453 | | int |
454 | | archive_read_open1(struct archive *_a) |
455 | 347 | { |
456 | 347 | struct archive_read *a = (struct archive_read *)_a; |
457 | 347 | struct archive_read_filter *filter, *tmp; |
458 | 347 | int slot, e = ARCHIVE_OK; |
459 | | |
460 | 347 | archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, |
461 | 347 | "archive_read_open"); |
462 | 347 | archive_clear_error(&a->archive); |
463 | | |
464 | 347 | if (a->client.reader == NULL) { |
465 | 0 | archive_set_error(&a->archive, EINVAL, |
466 | 0 | "No reader function provided to archive_read_open"); |
467 | 0 | a->archive.state = ARCHIVE_STATE_FATAL; |
468 | 0 | return (ARCHIVE_FATAL); |
469 | 0 | } |
470 | | |
471 | | /* Open data source. */ |
472 | 347 | if (a->client.opener != NULL) { |
473 | 347 | e = (a->client.opener)(&a->archive, a->client.dataset[0].data); |
474 | 347 | if (e != 0) { |
475 | | /* If the open failed, call the closer to clean up. */ |
476 | 0 | read_client_close_proxy(a); |
477 | 0 | return (e); |
478 | 0 | } |
479 | 347 | } |
480 | | |
481 | 347 | filter = calloc(1, sizeof(*filter)); |
482 | 347 | if (filter == NULL) |
483 | 0 | return (ARCHIVE_FATAL); |
484 | 347 | filter->bidder = NULL; |
485 | 347 | filter->upstream = NULL; |
486 | 347 | filter->archive = a; |
487 | 347 | filter->data = a->client.dataset[0].data; |
488 | 347 | filter->vtable = &none_reader_vtable; |
489 | 347 | filter->name = "none"; |
490 | 347 | filter->code = ARCHIVE_FILTER_NONE; |
491 | 347 | filter->can_skip = 1; |
492 | 347 | filter->can_seek = 1; |
493 | | |
494 | 347 | a->client.dataset[0].begin_position = 0; |
495 | 347 | if (!a->filter || !a->bypass_filter_bidding) |
496 | 347 | { |
497 | 347 | a->filter = filter; |
498 | | /* Build out the input pipeline. */ |
499 | 347 | e = choose_filters(a); |
500 | 347 | if (e < ARCHIVE_WARN) { |
501 | 9 | a->archive.state = ARCHIVE_STATE_FATAL; |
502 | 9 | return (ARCHIVE_FATAL); |
503 | 9 | } |
504 | 347 | } |
505 | 0 | else |
506 | 0 | { |
507 | | /* Need to add "NONE" type filter at the end of the filter chain */ |
508 | 0 | tmp = a->filter; |
509 | 0 | while (tmp->upstream) |
510 | 0 | tmp = tmp->upstream; |
511 | 0 | tmp->upstream = filter; |
512 | 0 | } |
513 | | |
514 | 338 | if (!a->format) |
515 | 338 | { |
516 | 338 | slot = choose_format(a); |
517 | 338 | if (slot < 0) { |
518 | 0 | close_filters(a); |
519 | 0 | a->archive.state = ARCHIVE_STATE_FATAL; |
520 | 0 | return (ARCHIVE_FATAL); |
521 | 0 | } |
522 | 338 | a->format = &(a->formats[slot]); |
523 | 338 | } |
524 | | |
525 | 338 | a->archive.state = ARCHIVE_STATE_HEADER; |
526 | | |
527 | | /* Ensure libarchive starts from the first node in a multivolume set */ |
528 | 338 | client_switch_proxy(a->filter, 0); |
529 | 338 | return (e); |
530 | 338 | } |
531 | | |
532 | | /* |
533 | | * Allow each registered stream transform to bid on whether |
534 | | * it wants to handle this stream. Repeat until we've finished |
535 | | * building the pipeline. |
536 | | */ |
537 | | |
538 | | /* We won't build a filter pipeline with more stages than this. */ |
539 | 406 | #define MAX_NUMBER_FILTERS 25 |
540 | | |
541 | | static int |
542 | | choose_filters(struct archive_read *a) |
543 | 347 | { |
544 | 347 | int number_bidders, i, bid, best_bid, number_filters; |
545 | 347 | struct archive_read_filter_bidder *bidder, *best_bidder; |
546 | 347 | struct archive_read_filter *filter; |
547 | 347 | ssize_t avail; |
548 | 347 | int r; |
549 | | |
550 | 406 | for (number_filters = 0; number_filters < MAX_NUMBER_FILTERS; ++number_filters) { |
551 | 406 | number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]); |
552 | | |
553 | 406 | best_bid = 0; |
554 | 406 | best_bidder = NULL; |
555 | | |
556 | 406 | bidder = a->bidders; |
557 | 6.90k | for (i = 0; i < number_bidders; i++, bidder++) { |
558 | 6.49k | if (bidder->vtable == NULL) |
559 | 1.21k | continue; |
560 | 5.27k | bid = (bidder->vtable->bid)(bidder, a->filter); |
561 | 5.27k | if (bid > best_bid) { |
562 | 59 | best_bid = bid; |
563 | 59 | best_bidder = bidder; |
564 | 59 | } |
565 | 5.27k | } |
566 | | |
567 | | /* If no bidder, we're done. */ |
568 | 406 | if (best_bidder == NULL) { |
569 | | /* Verify the filter by asking it for some data. */ |
570 | 347 | __archive_read_filter_ahead(a->filter, 1, &avail); |
571 | 347 | if (avail < 0) { |
572 | 9 | __archive_read_free_filters(a); |
573 | 9 | return (ARCHIVE_FATAL); |
574 | 9 | } |
575 | 338 | return (ARCHIVE_OK); |
576 | 347 | } |
577 | | |
578 | 59 | filter = calloc(1, sizeof(*filter)); |
579 | 59 | if (filter == NULL) |
580 | 0 | return (ARCHIVE_FATAL); |
581 | 59 | filter->bidder = best_bidder; |
582 | 59 | filter->archive = a; |
583 | 59 | filter->upstream = a->filter; |
584 | 59 | a->filter = filter; |
585 | 59 | r = (best_bidder->vtable->init)(a->filter); |
586 | 59 | if (r != ARCHIVE_OK) { |
587 | 0 | __archive_read_free_filters(a); |
588 | 0 | return (ARCHIVE_FATAL); |
589 | 0 | } |
590 | 59 | } |
591 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, |
592 | 0 | "Input requires too many filters for decoding"); |
593 | 0 | return (ARCHIVE_FATAL); |
594 | 347 | } |
595 | | |
596 | | int |
597 | | __archive_read_header(struct archive_read *a, struct archive_entry *entry) |
598 | 32 | { |
599 | 32 | if (!a->filter->vtable->read_header) |
600 | 24 | return (ARCHIVE_OK); |
601 | 8 | return a->filter->vtable->read_header(a->filter, entry); |
602 | 32 | } |
603 | | |
604 | | /* |
605 | | * Read header of next entry. |
606 | | */ |
607 | | static int |
608 | | _archive_read_next_header2(struct archive *_a, struct archive_entry *entry) |
609 | 505k | { |
610 | 505k | struct archive_read *a = (struct archive_read *)_a; |
611 | 505k | int r1 = ARCHIVE_OK, r2; |
612 | | |
613 | 505k | archive_check_magic(_a, ARCHIVE_READ_MAGIC, |
614 | 505k | ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA, |
615 | 505k | "archive_read_next_header"); |
616 | | |
617 | 505k | archive_entry_clear(entry); |
618 | 505k | archive_clear_error(&a->archive); |
619 | | |
620 | | /* |
621 | | * If client didn't consume entire data, skip any remainder |
622 | | * (This is especially important for GNU incremental directories.) |
623 | | */ |
624 | 505k | if (a->archive.state == ARCHIVE_STATE_DATA) { |
625 | 36.4k | r1 = archive_read_data_skip(&a->archive); |
626 | 36.4k | if (r1 == ARCHIVE_EOF) |
627 | 0 | archive_set_error(&a->archive, EIO, |
628 | 0 | "Premature end-of-file."); |
629 | 36.4k | if (r1 == ARCHIVE_EOF || r1 == ARCHIVE_FATAL) { |
630 | 6 | a->archive.state = ARCHIVE_STATE_FATAL; |
631 | 6 | return (ARCHIVE_FATAL); |
632 | 6 | } |
633 | 36.4k | } |
634 | | |
635 | | /* Record start-of-header offset in uncompressed stream. */ |
636 | 505k | a->header_position = a->filter->position; |
637 | | |
638 | 505k | ++_a->file_count; |
639 | 505k | r2 = (a->format->read_header)(a, entry); |
640 | | |
641 | | /* |
642 | | * EOF and FATAL are persistent at this layer. By |
643 | | * modifying the state, we guarantee that future calls to |
644 | | * read a header or read data will fail. |
645 | | */ |
646 | 505k | switch (r2) { |
647 | 44 | case ARCHIVE_EOF: |
648 | 44 | a->archive.state = ARCHIVE_STATE_EOF; |
649 | 44 | --_a->file_count;/* Revert a file counter. */ |
650 | 44 | break; |
651 | 32.3k | case ARCHIVE_OK: |
652 | 32.3k | a->archive.state = ARCHIVE_STATE_DATA; |
653 | 32.3k | break; |
654 | 4.18k | case ARCHIVE_WARN: |
655 | 4.18k | a->archive.state = ARCHIVE_STATE_DATA; |
656 | 4.18k | break; |
657 | 469k | case ARCHIVE_RETRY: |
658 | 469k | break; |
659 | 200 | case ARCHIVE_FATAL: |
660 | 200 | a->archive.state = ARCHIVE_STATE_FATAL; |
661 | 200 | break; |
662 | 505k | } |
663 | | |
664 | 505k | __archive_reset_read_data(&a->archive); |
665 | | |
666 | 505k | a->data_start_node = a->client.cursor; |
667 | | /* EOF always wins; otherwise return the worst error. */ |
668 | 505k | return (r2 < r1 || r2 == ARCHIVE_EOF) ? r2 : r1; |
669 | 505k | } |
670 | | |
671 | | static int |
672 | | _archive_read_next_header(struct archive *_a, struct archive_entry **entryp) |
673 | 505k | { |
674 | 505k | int ret; |
675 | 505k | struct archive_read *a = (struct archive_read *)_a; |
676 | 505k | *entryp = NULL; |
677 | 505k | ret = _archive_read_next_header2(_a, a->entry); |
678 | 505k | *entryp = a->entry; |
679 | 505k | return ret; |
680 | 505k | } |
681 | | |
682 | | /* |
683 | | * Allow each registered format to bid on whether it wants to handle |
684 | | * the next entry. Return index of winning bidder. |
685 | | */ |
686 | | static int |
687 | | choose_format(struct archive_read *a) |
688 | 338 | { |
689 | 338 | int slots; |
690 | 338 | int i; |
691 | 338 | int bid, best_bid; |
692 | 338 | int best_bid_slot; |
693 | | |
694 | 338 | slots = sizeof(a->formats) / sizeof(a->formats[0]); |
695 | 338 | best_bid = -1; |
696 | 338 | best_bid_slot = -1; |
697 | | |
698 | | /* Set up a->format for convenience of bidders. */ |
699 | 338 | a->format = &(a->formats[0]); |
700 | 5.74k | for (i = 0; i < slots; i++, a->format++) { |
701 | 5.40k | if (a->format->bid) { |
702 | 5.40k | bid = (a->format->bid)(a, best_bid); |
703 | 5.40k | if (bid == ARCHIVE_FATAL) |
704 | 0 | return (ARCHIVE_FATAL); |
705 | 5.40k | if (a->filter->position != 0) |
706 | 215 | __archive_read_seek(a, 0, SEEK_SET); |
707 | 5.40k | if ((bid > best_bid) || (best_bid_slot < 0)) { |
708 | 991 | best_bid = bid; |
709 | 991 | best_bid_slot = i; |
710 | 991 | } |
711 | 5.40k | } |
712 | 5.40k | } |
713 | | |
714 | | /* |
715 | | * There were no bidders; this is a serious programmer error |
716 | | * and demands a quick and definitive abort. |
717 | | */ |
718 | 338 | if (best_bid_slot < 0) { |
719 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, |
720 | 0 | "No formats registered"); |
721 | 0 | return (ARCHIVE_FATAL); |
722 | 0 | } |
723 | | |
724 | | /* |
725 | | * There were bidders, but no non-zero bids; this means we |
726 | | * can't support this stream. |
727 | | */ |
728 | 338 | if (best_bid < 1) { |
729 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, |
730 | 0 | "Unrecognized archive format"); |
731 | 0 | return (ARCHIVE_FATAL); |
732 | 0 | } |
733 | | |
734 | 338 | return (best_bid_slot); |
735 | 338 | } |
736 | | |
737 | | /* |
738 | | * Return the file offset (within the uncompressed data stream) where |
739 | | * the last header started. |
740 | | */ |
741 | | la_int64_t |
742 | | archive_read_header_position(struct archive *_a) |
743 | 0 | { |
744 | 0 | struct archive_read *a = (struct archive_read *)_a; |
745 | 0 | archive_check_magic(_a, ARCHIVE_READ_MAGIC, |
746 | 0 | ARCHIVE_STATE_ANY, "archive_read_header_position"); |
747 | 0 | return (a->header_position); |
748 | 0 | } |
749 | | |
750 | | /* |
751 | | * Returns 1 if the archive contains at least one encrypted entry. |
752 | | * If the archive format not support encryption at all |
753 | | * ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED is returned. |
754 | | * If for any other reason (e.g. not enough data read so far) |
755 | | * we cannot say whether there are encrypted entries, then |
756 | | * ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW is returned. |
757 | | * In general, this function will return values below zero when the |
758 | | * reader is uncertain or totally incapable of encryption support. |
759 | | * When this function returns 0 you can be sure that the reader |
760 | | * supports encryption detection but no encrypted entries have |
761 | | * been found yet. |
762 | | * |
763 | | * NOTE: If the metadata/header of an archive is also encrypted, you |
764 | | * cannot rely on the number of encrypted entries. That is why this |
765 | | * function does not return the number of encrypted entries but# |
766 | | * just shows that there are some. |
767 | | */ |
768 | | int |
769 | | archive_read_has_encrypted_entries(struct archive *_a) |
770 | 338 | { |
771 | 338 | struct archive_read *a = (struct archive_read *)_a; |
772 | 338 | int format_supports_encryption = archive_read_format_capabilities(_a) |
773 | 338 | & (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA | ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA); |
774 | | |
775 | 338 | if (!_a || !format_supports_encryption) { |
776 | | /* Format in general doesn't support encryption */ |
777 | 128 | return ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED; |
778 | 128 | } |
779 | | |
780 | | /* A reader potentially has read enough data now. */ |
781 | 210 | if (a->format && a->format->has_encrypted_entries) { |
782 | 210 | return (a->format->has_encrypted_entries)(a); |
783 | 210 | } |
784 | | |
785 | | /* For any other reason we cannot say how many entries are there. */ |
786 | 0 | return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW; |
787 | 210 | } |
788 | | |
789 | | /* |
790 | | * Returns a bitmask of capabilities that are supported by the archive format reader. |
791 | | * If the reader has no special capabilities, ARCHIVE_READ_FORMAT_CAPS_NONE is returned. |
792 | | */ |
793 | | int |
794 | | archive_read_format_capabilities(struct archive *_a) |
795 | 676 | { |
796 | 676 | struct archive_read *a = (struct archive_read *)_a; |
797 | 676 | if (a && a->format && a->format->format_capabilties) { |
798 | 420 | return (a->format->format_capabilties)(a); |
799 | 420 | } |
800 | 256 | return ARCHIVE_READ_FORMAT_CAPS_NONE; |
801 | 676 | } |
802 | | |
803 | | /* |
804 | | * Read data from an archive entry, using a read(2)-style interface. |
805 | | * This is a convenience routine that just calls |
806 | | * archive_read_data_block and copies the results into the client |
807 | | * buffer, filling any gaps with zero bytes. Clients using this |
808 | | * API can be completely ignorant of sparse-file issues; sparse files |
809 | | * will simply be padded with nulls. |
810 | | * |
811 | | * DO NOT intermingle calls to this function and archive_read_data_block |
812 | | * to read a single entry body. |
813 | | */ |
814 | | la_ssize_t |
815 | | archive_read_data(struct archive *_a, void *buff, size_t s) |
816 | 57.9k | { |
817 | 57.9k | struct archive *a = (struct archive *)_a; |
818 | 57.9k | char *dest; |
819 | 57.9k | const void *read_buf; |
820 | 57.9k | size_t bytes_read; |
821 | 57.9k | size_t len; |
822 | 57.9k | int r; |
823 | | |
824 | 57.9k | bytes_read = 0; |
825 | 57.9k | dest = (char *)buff; |
826 | | |
827 | 86.0k | while (s > 0) { |
828 | 70.5k | if (a->read_data_offset == a->read_data_output_offset && |
829 | 70.5k | a->read_data_remaining == 0) { |
830 | 55.2k | read_buf = a->read_data_block; |
831 | 55.2k | a->read_data_is_posix_read = 1; |
832 | 55.2k | a->read_data_requested = s; |
833 | 55.2k | r = archive_read_data_block(a, &read_buf, |
834 | 55.2k | &a->read_data_remaining, &a->read_data_offset); |
835 | 55.2k | a->read_data_block = read_buf; |
836 | 55.2k | if (r == ARCHIVE_EOF && |
837 | 27.5k | a->read_data_offset == a->read_data_output_offset && |
838 | 27.5k | a->read_data_remaining == 0) |
839 | 27.5k | return (bytes_read); |
840 | | /* |
841 | | * Error codes are all negative, so the status |
842 | | * return here cannot be confused with a valid |
843 | | * byte count. (ARCHIVE_OK is zero.) |
844 | | */ |
845 | 27.7k | if (r < ARCHIVE_OK) |
846 | 14.9k | return (r); |
847 | 27.7k | } |
848 | | |
849 | 28.0k | if (a->read_data_offset < a->read_data_output_offset) { |
850 | 9 | archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT, |
851 | 9 | "Encountered out-of-order sparse blocks"); |
852 | 9 | return (ARCHIVE_RETRY); |
853 | 9 | } |
854 | | |
855 | | /* Compute the amount of zero padding needed. */ |
856 | 28.0k | if (a->read_data_output_offset + (int64_t)s < |
857 | 28.0k | a->read_data_offset) { |
858 | 0 | len = s; |
859 | 28.0k | } else if (a->read_data_output_offset < |
860 | 28.0k | a->read_data_offset) { |
861 | 0 | len = (size_t)(a->read_data_offset - |
862 | 0 | a->read_data_output_offset); |
863 | 0 | } else |
864 | 28.0k | len = 0; |
865 | | |
866 | | /* Add zeroes. */ |
867 | 28.0k | memset(dest, 0, len); |
868 | 28.0k | s -= len; |
869 | 28.0k | a->read_data_output_offset += len; |
870 | 28.0k | dest += len; |
871 | 28.0k | bytes_read += len; |
872 | | |
873 | | /* Copy data if there is any space left. */ |
874 | 28.0k | if (s > 0) { |
875 | 28.0k | len = a->read_data_remaining; |
876 | 28.0k | if (len > s) |
877 | 15.2k | len = s; |
878 | 28.0k | if (len) { |
879 | 25.8k | memcpy(dest, a->read_data_block, len); |
880 | 25.8k | s -= len; |
881 | 25.8k | a->read_data_block += len; |
882 | 25.8k | a->read_data_remaining -= len; |
883 | 25.8k | a->read_data_output_offset += len; |
884 | 25.8k | a->read_data_offset += len; |
885 | 25.8k | dest += len; |
886 | 25.8k | bytes_read += len; |
887 | 25.8k | } |
888 | 28.0k | } |
889 | 28.0k | } |
890 | 15.4k | a->read_data_is_posix_read = 0; |
891 | 15.4k | a->read_data_requested = 0; |
892 | 15.4k | return (bytes_read); |
893 | 57.9k | } |
894 | | |
895 | | /* |
896 | | * Reset the read_data_* variables, used for starting a new entry. |
897 | | */ |
898 | | void __archive_reset_read_data(struct archive * a) |
899 | 505k | { |
900 | 505k | a->read_data_output_offset = 0; |
901 | 505k | a->read_data_remaining = 0; |
902 | 505k | a->read_data_is_posix_read = 0; |
903 | 505k | a->read_data_requested = 0; |
904 | | |
905 | | /* extra resets, from rar.c */ |
906 | 505k | a->read_data_block = NULL; |
907 | 505k | a->read_data_offset = 0; |
908 | 505k | } |
909 | | |
910 | | /* |
911 | | * Skip over all remaining data in this entry. |
912 | | */ |
913 | | int |
914 | | archive_read_data_skip(struct archive *_a) |
915 | 36.4k | { |
916 | 36.4k | struct archive_read *a = (struct archive_read *)_a; |
917 | 36.4k | int r; |
918 | 36.4k | const void *buff; |
919 | 36.4k | size_t size; |
920 | 36.4k | int64_t offset; |
921 | | |
922 | 36.4k | archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA, |
923 | 36.4k | "archive_read_data_skip"); |
924 | | |
925 | 36.4k | if (a->format->read_data_skip != NULL) |
926 | 36.4k | r = (a->format->read_data_skip)(a); |
927 | 0 | else { |
928 | 0 | while ((r = archive_read_data_block(&a->archive, |
929 | 0 | &buff, &size, &offset)) |
930 | 0 | == ARCHIVE_OK) |
931 | 0 | ; |
932 | 0 | } |
933 | | |
934 | 36.4k | if (r == ARCHIVE_EOF) |
935 | 0 | r = ARCHIVE_OK; |
936 | | |
937 | 36.4k | a->archive.state = ARCHIVE_STATE_HEADER; |
938 | 36.4k | return (r); |
939 | 36.4k | } |
940 | | |
941 | | la_int64_t |
942 | | archive_seek_data(struct archive *_a, int64_t offset, int whence) |
943 | 338 | { |
944 | 338 | struct archive_read *a = (struct archive_read *)_a; |
945 | 338 | archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA, |
946 | 338 | "archive_seek_data_block"); |
947 | | |
948 | 88 | if (a->format->seek_data == NULL) { |
949 | 43 | archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER, |
950 | 43 | "Internal error: " |
951 | 43 | "No format_seek_data_block function registered"); |
952 | 43 | return (ARCHIVE_FATAL); |
953 | 43 | } |
954 | | |
955 | 45 | return (a->format->seek_data)(a, offset, whence); |
956 | 88 | } |
957 | | |
958 | | /* |
959 | | * Read the next block of entry data from the archive. |
960 | | * This is a zero-copy interface; the client receives a pointer, |
961 | | * size, and file offset of the next available block of data. |
962 | | * |
963 | | * Returns ARCHIVE_OK if the operation is successful, ARCHIVE_EOF if |
964 | | * the end of entry is encountered. |
965 | | */ |
966 | | static int |
967 | | _archive_read_data_block(struct archive *_a, |
968 | | const void **buff, size_t *size, int64_t *offset) |
969 | 55.2k | { |
970 | 55.2k | struct archive_read *a = (struct archive_read *)_a; |
971 | 55.2k | archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA, |
972 | 55.2k | "archive_read_data_block"); |
973 | | |
974 | 55.2k | if (a->format->read_data == NULL) { |
975 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER, |
976 | 0 | "Internal error: " |
977 | 0 | "No format->read_data function registered"); |
978 | 0 | return (ARCHIVE_FATAL); |
979 | 0 | } |
980 | | |
981 | 55.2k | return (a->format->read_data)(a, buff, size, offset); |
982 | 55.2k | } |
983 | | |
984 | | static int |
985 | | close_filters(struct archive_read *a) |
986 | 444 | { |
987 | 444 | struct archive_read_filter *f = a->filter; |
988 | 444 | int r = ARCHIVE_OK; |
989 | | /* Close each filter in the pipeline. */ |
990 | 938 | while (f != NULL) { |
991 | 494 | struct archive_read_filter *t = f->upstream; |
992 | 494 | if (!f->closed && f->vtable != NULL) { |
993 | 406 | int r1 = (f->vtable->close)(f); |
994 | 406 | f->closed = 1; |
995 | 406 | if (r1 < r) |
996 | 0 | r = r1; |
997 | 406 | } |
998 | 494 | free(f->buffer); |
999 | 494 | f->buffer = NULL; |
1000 | 494 | f = t; |
1001 | 494 | } |
1002 | 444 | return r; |
1003 | 444 | } |
1004 | | |
1005 | | void |
1006 | | __archive_read_free_filters(struct archive_read *a) |
1007 | 356 | { |
1008 | | /* Make sure filters are closed and their buffers are freed */ |
1009 | 356 | close_filters(a); |
1010 | | |
1011 | 762 | while (a->filter != NULL) { |
1012 | 406 | struct archive_read_filter *t = a->filter->upstream; |
1013 | 406 | free(a->filter); |
1014 | 406 | a->filter = t; |
1015 | 406 | } |
1016 | 356 | } |
1017 | | |
1018 | | /* |
1019 | | * return the count of # of filters in use |
1020 | | */ |
1021 | | static int |
1022 | | _archive_filter_count(struct archive *_a) |
1023 | 0 | { |
1024 | 0 | struct archive_read *a = (struct archive_read *)_a; |
1025 | 0 | struct archive_read_filter *p = a->filter; |
1026 | 0 | int count = 0; |
1027 | 0 | while(p) { |
1028 | 0 | count++; |
1029 | 0 | p = p->upstream; |
1030 | 0 | } |
1031 | 0 | return count; |
1032 | 0 | } |
1033 | | |
1034 | | /* |
1035 | | * Close the file and all I/O. |
1036 | | */ |
1037 | | static int |
1038 | | _archive_read_close(struct archive *_a) |
1039 | 88 | { |
1040 | 88 | struct archive_read *a = (struct archive_read *)_a; |
1041 | 88 | int r = ARCHIVE_OK, r1 = ARCHIVE_OK; |
1042 | | |
1043 | 88 | archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC, |
1044 | 88 | ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close"); |
1045 | 88 | if (a->archive.state == ARCHIVE_STATE_CLOSED) |
1046 | 0 | return (ARCHIVE_OK); |
1047 | 88 | archive_clear_error(&a->archive); |
1048 | 88 | a->archive.state = ARCHIVE_STATE_CLOSED; |
1049 | | |
1050 | | /* TODO: Clean up the formatters. */ |
1051 | | |
1052 | | /* Release the filter objects. */ |
1053 | 88 | r1 = close_filters(a); |
1054 | 88 | if (r1 < r) |
1055 | 0 | r = r1; |
1056 | | |
1057 | 88 | return (r); |
1058 | 88 | } |
1059 | | |
1060 | | /* |
1061 | | * Release memory and other resources. |
1062 | | */ |
1063 | | static int |
1064 | | _archive_read_free(struct archive *_a) |
1065 | 347 | { |
1066 | 347 | struct archive_read *a = (struct archive_read *)_a; |
1067 | 347 | struct archive_read_passphrase *p; |
1068 | 347 | int i, n; |
1069 | 347 | int slots; |
1070 | 347 | int r = ARCHIVE_OK; |
1071 | | |
1072 | 347 | if (_a == NULL) |
1073 | 0 | return (ARCHIVE_OK); |
1074 | 347 | archive_check_magic(_a, ARCHIVE_READ_MAGIC, |
1075 | 347 | ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free"); |
1076 | 347 | if (a->archive.state != ARCHIVE_STATE_CLOSED |
1077 | 347 | && a->archive.state != ARCHIVE_STATE_FATAL) |
1078 | 88 | r = archive_read_close(&a->archive); |
1079 | | |
1080 | | /* Call cleanup functions registered by optional components. */ |
1081 | 347 | if (a->cleanup_archive_extract != NULL) |
1082 | 0 | r = (a->cleanup_archive_extract)(a); |
1083 | | |
1084 | | /* Cleanup format-specific data. */ |
1085 | 347 | slots = sizeof(a->formats) / sizeof(a->formats[0]); |
1086 | 5.89k | for (i = 0; i < slots; i++) { |
1087 | 5.55k | a->format = &(a->formats[i]); |
1088 | 5.55k | if (a->formats[i].cleanup) |
1089 | 5.20k | (a->formats[i].cleanup)(a); |
1090 | 5.55k | } |
1091 | | |
1092 | | /* Free the filters */ |
1093 | 347 | __archive_read_free_filters(a); |
1094 | | |
1095 | | /* Release the bidder objects. */ |
1096 | 347 | n = sizeof(a->bidders)/sizeof(a->bidders[0]); |
1097 | 5.89k | for (i = 0; i < n; i++) { |
1098 | 5.55k | if (a->bidders[i].vtable == NULL || |
1099 | 4.51k | a->bidders[i].vtable->free == NULL) |
1100 | 5.55k | continue; |
1101 | 0 | (a->bidders[i].vtable->free)(&a->bidders[i]); |
1102 | 0 | } |
1103 | | |
1104 | | /* Release passphrase list. */ |
1105 | 347 | p = a->passphrases.first; |
1106 | 694 | while (p != NULL) { |
1107 | 347 | struct archive_read_passphrase *np = p->next; |
1108 | | |
1109 | | /* A passphrase should be cleaned. */ |
1110 | 347 | memset(p->passphrase, 0, strlen(p->passphrase)); |
1111 | 347 | free(p->passphrase); |
1112 | 347 | free(p); |
1113 | 347 | p = np; |
1114 | 347 | } |
1115 | | |
1116 | 347 | archive_string_free(&a->archive.error_string); |
1117 | 347 | archive_entry_free(a->entry); |
1118 | 347 | a->archive.magic = 0; |
1119 | 347 | __archive_clean(&a->archive); |
1120 | 347 | free(a->client.dataset); |
1121 | 347 | free(a); |
1122 | 347 | return (r); |
1123 | 347 | } |
1124 | | |
1125 | | static struct archive_read_filter * |
1126 | | get_filter(struct archive *_a, int n) |
1127 | 1 | { |
1128 | 1 | struct archive_read *a = (struct archive_read *)_a; |
1129 | 1 | struct archive_read_filter *f = a->filter; |
1130 | | /* We use n == -1 for 'the last filter', which is always the |
1131 | | * client proxy. */ |
1132 | 1 | if (n == -1 && f != NULL) { |
1133 | 0 | struct archive_read_filter *last = f; |
1134 | 0 | f = f->upstream; |
1135 | 0 | while (f != NULL) { |
1136 | 0 | last = f; |
1137 | 0 | f = f->upstream; |
1138 | 0 | } |
1139 | 0 | return (last); |
1140 | 0 | } |
1141 | 1 | if (n < 0) |
1142 | 0 | return NULL; |
1143 | 1 | while (n > 0 && f != NULL) { |
1144 | 0 | f = f->upstream; |
1145 | 0 | --n; |
1146 | 0 | } |
1147 | 1 | return (f); |
1148 | 1 | } |
1149 | | |
1150 | | static int |
1151 | | _archive_filter_code(struct archive *_a, int n) |
1152 | 0 | { |
1153 | 0 | struct archive_read_filter *f = get_filter(_a, n); |
1154 | 0 | return f == NULL ? -1 : f->code; |
1155 | 0 | } |
1156 | | |
1157 | | static const char * |
1158 | | _archive_filter_name(struct archive *_a, int n) |
1159 | 0 | { |
1160 | 0 | struct archive_read_filter *f = get_filter(_a, n); |
1161 | 0 | return f != NULL ? f->name : NULL; |
1162 | 0 | } |
1163 | | |
1164 | | static int64_t |
1165 | | _archive_filter_bytes(struct archive *_a, int n) |
1166 | 1 | { |
1167 | 1 | struct archive_read_filter *f = get_filter(_a, n); |
1168 | 1 | return f == NULL ? -1 : f->position; |
1169 | 1 | } |
1170 | | |
1171 | | /* |
1172 | | * Used internally by read format handlers to register their bid and |
1173 | | * initialization functions. |
1174 | | */ |
1175 | | int |
1176 | | __archive_read_register_format(struct archive_read *a, |
1177 | | void *format_data, |
1178 | | const char *name, |
1179 | | int (*bid)(struct archive_read *, int), |
1180 | | int (*options)(struct archive_read *, const char *, const char *), |
1181 | | int (*read_header)(struct archive_read *, struct archive_entry *), |
1182 | | int (*read_data)(struct archive_read *, const void **, size_t *, int64_t *), |
1183 | | int (*read_data_skip)(struct archive_read *), |
1184 | | int64_t (*seek_data)(struct archive_read *, int64_t, int), |
1185 | | int (*cleanup)(struct archive_read *), |
1186 | | int (*format_capabilities)(struct archive_read *), |
1187 | | int (*has_encrypted_entries)(struct archive_read *)) |
1188 | 6.24k | { |
1189 | 6.24k | int i, number_slots; |
1190 | | |
1191 | 6.24k | archive_check_magic(&a->archive, |
1192 | 6.24k | ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW, |
1193 | 6.24k | "__archive_read_register_format"); |
1194 | | |
1195 | 6.24k | number_slots = sizeof(a->formats) / sizeof(a->formats[0]); |
1196 | | |
1197 | 50.3k | for (i = 0; i < number_slots; i++) { |
1198 | 50.3k | if (a->formats[i].bid == bid) |
1199 | 694 | return (ARCHIVE_WARN); /* We've already installed */ |
1200 | 49.6k | if (a->formats[i].bid == NULL) { |
1201 | 5.55k | a->formats[i].bid = bid; |
1202 | 5.55k | a->formats[i].options = options; |
1203 | 5.55k | a->formats[i].read_header = read_header; |
1204 | 5.55k | a->formats[i].read_data = read_data; |
1205 | 5.55k | a->formats[i].read_data_skip = read_data_skip; |
1206 | 5.55k | a->formats[i].seek_data = seek_data; |
1207 | 5.55k | a->formats[i].cleanup = cleanup; |
1208 | 5.55k | a->formats[i].data = format_data; |
1209 | 5.55k | a->formats[i].name = name; |
1210 | 5.55k | a->formats[i].format_capabilties = format_capabilities; |
1211 | 5.55k | a->formats[i].has_encrypted_entries = has_encrypted_entries; |
1212 | 5.55k | return (ARCHIVE_OK); |
1213 | 5.55k | } |
1214 | 49.6k | } |
1215 | | |
1216 | 0 | archive_set_error(&a->archive, ENOMEM, |
1217 | 0 | "Not enough slots for format registration"); |
1218 | 0 | return (ARCHIVE_FATAL); |
1219 | 6.24k | } |
1220 | | |
1221 | | /* |
1222 | | * Used internally by decompression routines to register their bid and |
1223 | | * initialization functions. |
1224 | | */ |
1225 | | int |
1226 | | __archive_read_register_bidder(struct archive_read *a, |
1227 | | void *bidder_data, |
1228 | | const char *name, |
1229 | | const struct archive_read_filter_bidder_vtable *vtable) |
1230 | 4.51k | { |
1231 | 4.51k | struct archive_read_filter_bidder *bidder; |
1232 | 4.51k | int i, number_slots; |
1233 | | |
1234 | 4.51k | archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC, |
1235 | 4.51k | ARCHIVE_STATE_NEW, "__archive_read_register_bidder"); |
1236 | | |
1237 | 4.51k | number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]); |
1238 | | |
1239 | 31.5k | for (i = 0; i < number_slots; i++) { |
1240 | 31.5k | if (a->bidders[i].vtable != NULL) |
1241 | 27.0k | continue; |
1242 | 4.51k | memset(a->bidders + i, 0, sizeof(a->bidders[0])); |
1243 | 4.51k | bidder = (a->bidders + i); |
1244 | 4.51k | bidder->data = bidder_data; |
1245 | 4.51k | bidder->name = name; |
1246 | 4.51k | bidder->vtable = vtable; |
1247 | 4.51k | if (bidder->vtable->bid == NULL || bidder->vtable->init == NULL) { |
1248 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER, |
1249 | 0 | "Internal error: " |
1250 | 0 | "no bid/init for filter bidder"); |
1251 | 0 | return (ARCHIVE_FATAL); |
1252 | 0 | } |
1253 | | |
1254 | 4.51k | return (ARCHIVE_OK); |
1255 | 4.51k | } |
1256 | | |
1257 | 0 | archive_set_error(&a->archive, ENOMEM, |
1258 | 0 | "Not enough slots for filter registration"); |
1259 | 0 | return (ARCHIVE_FATAL); |
1260 | 4.51k | } |
1261 | | |
1262 | | /* |
1263 | | * The next section implements the peek/consume internal I/O |
1264 | | * system used by archive readers. This system allows simple |
1265 | | * read-ahead for consumers while preserving zero-copy operation |
1266 | | * most of the time. |
1267 | | * |
1268 | | * The two key operations: |
1269 | | * * The read-ahead function returns a pointer to a block of data |
1270 | | * that satisfies a minimum request. |
1271 | | * * The consume function advances the file pointer. |
1272 | | * |
1273 | | * In the ideal case, filters generate blocks of data |
1274 | | * and __archive_read_ahead() just returns pointers directly into |
1275 | | * those blocks. Then __archive_read_consume() just bumps those |
1276 | | * pointers. Only if your request would span blocks does the I/O |
1277 | | * layer use a copy buffer to provide you with a contiguous block of |
1278 | | * data. |
1279 | | * |
1280 | | * A couple of useful idioms: |
1281 | | * * "I just want some data." Ask for 1 byte and pay attention to |
1282 | | * the "number of bytes available" from __archive_read_ahead(). |
1283 | | * Consume whatever you actually use. |
1284 | | * * "I want to output a large block of data." As above, ask for 1 byte, |
1285 | | * emit all that's available (up to whatever limit you have), consume |
1286 | | * it all, then repeat until you're done. This effectively means that |
1287 | | * you're passing along the blocks that came from your provider. |
1288 | | * * "I want to peek ahead by a large amount." Ask for 4k or so, then |
1289 | | * double and repeat until you get an error or have enough. Note |
1290 | | * that the I/O layer will likely end up expanding its copy buffer |
1291 | | * to fit your request, so use this technique cautiously. This |
1292 | | * technique is used, for example, by some of the format tasting |
1293 | | * code that has uncertain look-ahead needs. |
1294 | | */ |
1295 | | |
1296 | | /* |
1297 | | * Looks ahead in the input stream: |
1298 | | * * If 'avail' pointer is provided, that returns number of bytes available |
1299 | | * in the current buffer, which may be much larger than requested. |
1300 | | * * If end-of-file, *avail gets set to zero. |
1301 | | * * If error, *avail gets error code. |
1302 | | * * If request can be met, returns pointer to data. |
1303 | | * * If minimum request cannot be met, returns NULL. |
1304 | | * |
1305 | | * Note: If you just want "some data", ask for 1 byte and pay attention |
1306 | | * to *avail, which will have the actual amount available. If you |
1307 | | * know exactly how many bytes you need, just ask for that and treat |
1308 | | * a NULL return as an error. |
1309 | | * |
1310 | | * Important: This does NOT move the file pointer. See |
1311 | | * __archive_read_consume() below. |
1312 | | */ |
1313 | | const void * |
1314 | | __archive_read_ahead(struct archive_read *a, size_t min, ssize_t *avail) |
1315 | 11.3M | { |
1316 | 11.3M | return (__archive_read_filter_ahead(a->filter, min, avail)); |
1317 | 11.3M | } |
1318 | | |
1319 | | const void * |
1320 | | __archive_read_filter_ahead(struct archive_read_filter *filter, |
1321 | | size_t min, ssize_t *avail) |
1322 | 11.4M | { |
1323 | 11.4M | ssize_t bytes_read; |
1324 | 11.4M | size_t tocopy; |
1325 | | |
1326 | 11.4M | if (filter->fatal) { |
1327 | 263 | if (avail) |
1328 | 263 | *avail = ARCHIVE_FATAL; |
1329 | 263 | return (NULL); |
1330 | 263 | } |
1331 | | |
1332 | | /* |
1333 | | * Keep pulling more data until we can satisfy the request. |
1334 | | */ |
1335 | 11.4M | for (;;) { |
1336 | | |
1337 | | /* |
1338 | | * If we can satisfy from the copy buffer (and the |
1339 | | * copy buffer isn't empty), we're done. In particular, |
1340 | | * note that min == 0 is a perfectly well-defined |
1341 | | * request. |
1342 | | */ |
1343 | 11.4M | if (filter->avail >= min && filter->avail > 0) { |
1344 | 2.73k | if (avail != NULL) |
1345 | 2.51k | *avail = filter->avail; |
1346 | 2.73k | return (filter->next); |
1347 | 2.73k | } |
1348 | | |
1349 | | /* |
1350 | | * We can satisfy directly from client buffer if everything |
1351 | | * currently in the copy buffer is still in the client buffer. |
1352 | | */ |
1353 | 11.4M | if (filter->client_total >= filter->client_avail + filter->avail |
1354 | 11.4M | && filter->client_avail + filter->avail >= min) { |
1355 | | /* "Roll back" to client buffer. */ |
1356 | 11.3M | filter->client_avail += filter->avail; |
1357 | 11.3M | filter->client_next -= filter->avail; |
1358 | | /* Copy buffer is now empty. */ |
1359 | 11.3M | filter->avail = 0; |
1360 | 11.3M | filter->next = filter->buffer; |
1361 | | /* Return data from client buffer. */ |
1362 | 11.3M | if (avail != NULL) |
1363 | 11.2M | *avail = filter->client_avail; |
1364 | 11.3M | return (filter->client_next); |
1365 | 11.3M | } |
1366 | | |
1367 | | /* Move data forward in copy buffer if necessary. */ |
1368 | 6.28k | if (filter->next > filter->buffer && |
1369 | 19 | filter->next + min > filter->buffer + filter->buffer_size) { |
1370 | 11 | if (filter->avail > 0) |
1371 | 9 | memmove(filter->buffer, filter->next, |
1372 | 9 | filter->avail); |
1373 | 11 | filter->next = filter->buffer; |
1374 | 11 | } |
1375 | | |
1376 | | /* If we've used up the client data, get more. */ |
1377 | 6.28k | if (filter->client_avail <= 0) { |
1378 | 5.81k | if (filter->end_of_file) { |
1379 | 362 | if (avail != NULL) |
1380 | 296 | *avail = filter->avail; |
1381 | 362 | return (NULL); |
1382 | 362 | } |
1383 | 5.45k | bytes_read = (filter->vtable->read)(filter, |
1384 | 5.45k | &filter->client_buff); |
1385 | 5.45k | if (bytes_read < 0) { /* Read error. */ |
1386 | 13 | filter->client_total = filter->client_avail = 0; |
1387 | 13 | filter->client_next = |
1388 | 13 | filter->client_buff = NULL; |
1389 | 13 | filter->fatal = 1; |
1390 | 13 | if (avail != NULL) |
1391 | 13 | *avail = ARCHIVE_FATAL; |
1392 | 13 | return (NULL); |
1393 | 13 | } |
1394 | 5.43k | if (bytes_read == 0) { |
1395 | | /* Check for another client object first */ |
1396 | 272 | if (filter->archive->client.cursor != |
1397 | 272 | filter->archive->client.nodes - 1) { |
1398 | 0 | if (client_switch_proxy(filter, |
1399 | 0 | filter->archive->client.cursor + 1) |
1400 | 0 | == ARCHIVE_OK) |
1401 | 0 | continue; |
1402 | 0 | } |
1403 | | /* Premature end-of-file. */ |
1404 | 272 | filter->client_total = filter->client_avail = 0; |
1405 | 272 | filter->client_next = |
1406 | 272 | filter->client_buff = NULL; |
1407 | 272 | filter->end_of_file = 1; |
1408 | | /* Return whatever we do have. */ |
1409 | 272 | if (avail != NULL) |
1410 | 246 | *avail = filter->avail; |
1411 | 272 | return (NULL); |
1412 | 272 | } |
1413 | 5.16k | filter->client_total = bytes_read; |
1414 | 5.16k | filter->client_avail = filter->client_total; |
1415 | 5.16k | filter->client_next = filter->client_buff; |
1416 | 5.16k | } else { |
1417 | | /* |
1418 | | * We can't satisfy the request from the copy |
1419 | | * buffer or the existing client data, so we |
1420 | | * need to copy more client data over to the |
1421 | | * copy buffer. |
1422 | | */ |
1423 | | |
1424 | | /* Ensure the buffer is big enough. */ |
1425 | 474 | if (min > filter->buffer_size) { |
1426 | 189 | size_t s, t; |
1427 | 189 | char *p; |
1428 | | |
1429 | | /* Double the buffer; watch for overflow. */ |
1430 | 189 | s = t = filter->buffer_size; |
1431 | 189 | if (s == 0) |
1432 | 180 | s = min; |
1433 | 218 | while (s < min) { |
1434 | 29 | t *= 2; |
1435 | 29 | if (t <= s) { /* Integer overflow! */ |
1436 | 0 | archive_set_error( |
1437 | 0 | &filter->archive->archive, |
1438 | 0 | ENOMEM, |
1439 | 0 | "Unable to allocate copy" |
1440 | 0 | " buffer"); |
1441 | 0 | filter->fatal = 1; |
1442 | 0 | if (avail != NULL) |
1443 | 0 | *avail = ARCHIVE_FATAL; |
1444 | 0 | return (NULL); |
1445 | 0 | } |
1446 | 29 | s = t; |
1447 | 29 | } |
1448 | | /* Now s >= min, so allocate a new buffer. */ |
1449 | 189 | p = malloc(s); |
1450 | 189 | if (p == NULL) { |
1451 | 1 | archive_set_error( |
1452 | 1 | &filter->archive->archive, |
1453 | 1 | ENOMEM, |
1454 | 1 | "Unable to allocate copy buffer"); |
1455 | 1 | filter->fatal = 1; |
1456 | 1 | if (avail != NULL) |
1457 | 1 | *avail = ARCHIVE_FATAL; |
1458 | 1 | return (NULL); |
1459 | 1 | } |
1460 | | /* Move data into newly-enlarged buffer. */ |
1461 | 188 | if (filter->avail > 0) |
1462 | 8 | memmove(p, filter->next, filter->avail); |
1463 | 188 | free(filter->buffer); |
1464 | 188 | filter->next = filter->buffer = p; |
1465 | 188 | filter->buffer_size = s; |
1466 | 188 | } |
1467 | | |
1468 | | /* We can add client data to copy buffer. */ |
1469 | | /* First estimate: copy to fill rest of buffer. */ |
1470 | 473 | tocopy = (filter->buffer + filter->buffer_size) |
1471 | 473 | - (filter->next + filter->avail); |
1472 | | /* Don't waste time buffering more than we need to. */ |
1473 | 473 | if (tocopy + filter->avail > min) |
1474 | 280 | tocopy = min - filter->avail; |
1475 | | /* Don't copy more than is available. */ |
1476 | 473 | if (tocopy > filter->client_avail) |
1477 | 429 | tocopy = filter->client_avail; |
1478 | | |
1479 | 473 | memcpy(filter->next + filter->avail, |
1480 | 473 | filter->client_next, tocopy); |
1481 | | /* Remove this data from client buffer. */ |
1482 | 473 | filter->client_next += tocopy; |
1483 | 473 | filter->client_avail -= tocopy; |
1484 | | /* add it to copy buffer. */ |
1485 | 473 | filter->avail += tocopy; |
1486 | 473 | } |
1487 | 6.28k | } |
1488 | 11.4M | } |
1489 | | |
1490 | | /* |
1491 | | * Move the file pointer forward. |
1492 | | */ |
1493 | | int64_t |
1494 | | __archive_read_consume(struct archive_read *a, int64_t request) |
1495 | 11.1M | { |
1496 | 11.1M | return (__archive_read_filter_consume(a->filter, request)); |
1497 | 11.1M | } |
1498 | | |
1499 | | int64_t |
1500 | | __archive_read_filter_consume(struct archive_read_filter * filter, |
1501 | | int64_t request) |
1502 | 11.1M | { |
1503 | 11.1M | int64_t skipped; |
1504 | | |
1505 | 11.1M | if (request < 0) |
1506 | 0 | return ARCHIVE_FATAL; |
1507 | 11.1M | if (request == 0) |
1508 | 106k | return 0; |
1509 | | |
1510 | 11.0M | skipped = advance_file_pointer(filter, request); |
1511 | 11.0M | if (skipped == request) |
1512 | 11.0M | return (skipped); |
1513 | | /* We hit EOF before we satisfied the skip request. */ |
1514 | 5 | if (skipped < 0) /* Map error code to 0 for error message below. */ |
1515 | 0 | skipped = 0; |
1516 | 5 | archive_set_error(&filter->archive->archive, |
1517 | 5 | ARCHIVE_ERRNO_MISC, |
1518 | 5 | "Truncated input file (needed %jd bytes, only %jd available)", |
1519 | 5 | (intmax_t)request, (intmax_t)skipped); |
1520 | 5 | return (ARCHIVE_FATAL); |
1521 | 11.0M | } |
1522 | | |
1523 | | /* |
1524 | | * Advance the file pointer by the amount requested. |
1525 | | * Returns the amount actually advanced, which may be less than the |
1526 | | * request if EOF is encountered first. |
1527 | | * Returns a negative value if there's an I/O error. |
1528 | | */ |
1529 | | static int64_t |
1530 | | advance_file_pointer(struct archive_read_filter *filter, int64_t request) |
1531 | 11.0M | { |
1532 | 11.0M | int64_t bytes_skipped, total_bytes_skipped = 0; |
1533 | 11.0M | ssize_t bytes_read; |
1534 | 11.0M | size_t min; |
1535 | | |
1536 | 11.0M | if (filter->fatal) |
1537 | 0 | return (-1); |
1538 | | |
1539 | | /* Use up the copy buffer first. */ |
1540 | 11.0M | if (filter->avail > 0) { |
1541 | 2.22k | min = (size_t)minimum(request, (int64_t)filter->avail); |
1542 | 2.22k | filter->next += min; |
1543 | 2.22k | filter->avail -= min; |
1544 | 2.22k | request -= min; |
1545 | 2.22k | filter->position += min; |
1546 | 2.22k | total_bytes_skipped += min; |
1547 | 2.22k | } |
1548 | | |
1549 | | /* Then use up the client buffer. */ |
1550 | 11.0M | if (filter->client_avail > 0) { |
1551 | 11.0M | min = (size_t)minimum(request, (int64_t)filter->client_avail); |
1552 | 11.0M | filter->client_next += min; |
1553 | 11.0M | filter->client_avail -= min; |
1554 | 11.0M | request -= min; |
1555 | 11.0M | filter->position += min; |
1556 | 11.0M | total_bytes_skipped += min; |
1557 | 11.0M | } |
1558 | 11.0M | if (request == 0) |
1559 | 11.0M | return (total_bytes_skipped); |
1560 | | |
1561 | | /* If there's an optimized skip function, use it. */ |
1562 | 5 | if (filter->can_skip != 0) { |
1563 | 5 | bytes_skipped = client_skip_proxy(filter, request); |
1564 | 5 | if (bytes_skipped < 0) { /* error */ |
1565 | 0 | filter->fatal = 1; |
1566 | 0 | return (bytes_skipped); |
1567 | 0 | } |
1568 | 5 | filter->position += bytes_skipped; |
1569 | 5 | total_bytes_skipped += bytes_skipped; |
1570 | 5 | request -= bytes_skipped; |
1571 | 5 | if (request == 0) |
1572 | 0 | return (total_bytes_skipped); |
1573 | 5 | } |
1574 | | |
1575 | | /* Use ordinary reads as necessary to complete the request. */ |
1576 | 5 | for (;;) { |
1577 | 5 | bytes_read = (filter->vtable->read)(filter, &filter->client_buff); |
1578 | 5 | if (bytes_read < 0) { |
1579 | 0 | filter->client_buff = NULL; |
1580 | 0 | filter->fatal = 1; |
1581 | 0 | return (bytes_read); |
1582 | 0 | } |
1583 | | |
1584 | 5 | if (bytes_read == 0) { |
1585 | 5 | if (filter->archive->client.cursor != |
1586 | 5 | filter->archive->client.nodes - 1) { |
1587 | 0 | if (client_switch_proxy(filter, |
1588 | 0 | filter->archive->client.cursor + 1) |
1589 | 0 | == ARCHIVE_OK) |
1590 | 0 | continue; |
1591 | 0 | } |
1592 | 5 | filter->client_buff = NULL; |
1593 | 5 | filter->end_of_file = 1; |
1594 | 5 | return (total_bytes_skipped); |
1595 | 5 | } |
1596 | | |
1597 | 0 | if (bytes_read >= request) { |
1598 | 0 | filter->client_next = |
1599 | 0 | ((const char *)filter->client_buff) + request; |
1600 | 0 | filter->client_avail = (size_t)(bytes_read - request); |
1601 | 0 | filter->client_total = bytes_read; |
1602 | 0 | total_bytes_skipped += request; |
1603 | 0 | filter->position += request; |
1604 | 0 | return (total_bytes_skipped); |
1605 | 0 | } |
1606 | | |
1607 | 0 | filter->position += bytes_read; |
1608 | 0 | total_bytes_skipped += bytes_read; |
1609 | 0 | request -= bytes_read; |
1610 | 0 | } |
1611 | 5 | } |
1612 | | |
1613 | | /** |
1614 | | * Returns ARCHIVE_FAILED if seeking isn't supported. |
1615 | | */ |
1616 | | int64_t |
1617 | | __archive_read_seek(struct archive_read *a, int64_t offset, int whence) |
1618 | 743 | { |
1619 | 743 | return __archive_read_filter_seek(a->filter, offset, whence); |
1620 | 743 | } |
1621 | | |
1622 | | int64_t |
1623 | | __archive_read_filter_seek(struct archive_read_filter *filter, int64_t offset, |
1624 | | int whence) |
1625 | 743 | { |
1626 | 743 | struct archive_read_client *client; |
1627 | 743 | int64_t r; |
1628 | 743 | unsigned int cursor; |
1629 | | |
1630 | 743 | if (filter->closed || filter->fatal) |
1631 | 0 | return (ARCHIVE_FATAL); |
1632 | 743 | if (filter->can_seek == 0) |
1633 | 13 | return (ARCHIVE_FAILED); |
1634 | | |
1635 | 730 | client = &(filter->archive->client); |
1636 | 730 | switch (whence) { |
1637 | 0 | case SEEK_CUR: |
1638 | | /* Adjust the offset and use SEEK_SET instead */ |
1639 | 0 | offset += filter->position; |
1640 | 0 | __LA_FALLTHROUGH; |
1641 | 300 | case SEEK_SET: |
1642 | 300 | cursor = 0; |
1643 | 300 | while (1) |
1644 | 300 | { |
1645 | 300 | if (client->dataset[cursor].begin_position < 0 || |
1646 | 300 | client->dataset[cursor].total_size < 0 || |
1647 | 268 | client->dataset[cursor].begin_position + |
1648 | 268 | client->dataset[cursor].total_size - 1 > offset || |
1649 | 3 | cursor + 1 >= client->nodes) |
1650 | 300 | break; |
1651 | 0 | r = client->dataset[cursor].begin_position + |
1652 | 0 | client->dataset[cursor].total_size; |
1653 | 0 | client->dataset[++cursor].begin_position = r; |
1654 | 0 | } |
1655 | 300 | while (1) { |
1656 | 300 | r = client_switch_proxy(filter, cursor); |
1657 | 300 | if (r != ARCHIVE_OK) |
1658 | 0 | return r; |
1659 | 300 | if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0) |
1660 | 0 | return r; |
1661 | 300 | client->dataset[cursor].total_size = r; |
1662 | 300 | if (client->dataset[cursor].begin_position + |
1663 | 300 | client->dataset[cursor].total_size - 1 > offset || |
1664 | 5 | cursor + 1 >= client->nodes) |
1665 | 300 | break; |
1666 | 0 | r = client->dataset[cursor].begin_position + |
1667 | 0 | client->dataset[cursor].total_size; |
1668 | 0 | client->dataset[++cursor].begin_position = r; |
1669 | 0 | } |
1670 | 300 | offset -= client->dataset[cursor].begin_position; |
1671 | 300 | if (offset < 0 |
1672 | 300 | || offset > client->dataset[cursor].total_size) |
1673 | 5 | return ARCHIVE_FATAL; |
1674 | 295 | if ((r = client_seek_proxy(filter, offset, SEEK_SET)) < 0) |
1675 | 0 | return r; |
1676 | 295 | break; |
1677 | | |
1678 | 430 | case SEEK_END: |
1679 | 430 | cursor = 0; |
1680 | 430 | while (1) { |
1681 | 430 | if (client->dataset[cursor].begin_position < 0 || |
1682 | 430 | client->dataset[cursor].total_size < 0 || |
1683 | 219 | cursor + 1 >= client->nodes) |
1684 | 430 | break; |
1685 | 0 | r = client->dataset[cursor].begin_position + |
1686 | 0 | client->dataset[cursor].total_size; |
1687 | 0 | client->dataset[++cursor].begin_position = r; |
1688 | 0 | } |
1689 | 430 | while (1) { |
1690 | 430 | r = client_switch_proxy(filter, cursor); |
1691 | 430 | if (r != ARCHIVE_OK) |
1692 | 0 | return r; |
1693 | 430 | if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0) |
1694 | 0 | return r; |
1695 | 430 | client->dataset[cursor].total_size = r; |
1696 | 430 | r = client->dataset[cursor].begin_position + |
1697 | 430 | client->dataset[cursor].total_size; |
1698 | 430 | if (cursor + 1 >= client->nodes) |
1699 | 430 | break; |
1700 | 0 | client->dataset[++cursor].begin_position = r; |
1701 | 0 | } |
1702 | 430 | while (1) { |
1703 | 430 | if (r + offset >= |
1704 | 430 | client->dataset[cursor].begin_position) |
1705 | 430 | break; |
1706 | 0 | offset += client->dataset[cursor].total_size; |
1707 | 0 | if (cursor == 0) |
1708 | 0 | break; |
1709 | 0 | cursor--; |
1710 | 0 | r = client->dataset[cursor].begin_position + |
1711 | 0 | client->dataset[cursor].total_size; |
1712 | 0 | } |
1713 | 430 | offset = (r + offset) - client->dataset[cursor].begin_position; |
1714 | 430 | if ((r = client_switch_proxy(filter, cursor)) != ARCHIVE_OK) |
1715 | 0 | return r; |
1716 | 430 | r = client_seek_proxy(filter, offset, SEEK_SET); |
1717 | 430 | if (r < ARCHIVE_OK) |
1718 | 0 | return r; |
1719 | 430 | break; |
1720 | | |
1721 | 430 | default: |
1722 | 0 | return (ARCHIVE_FATAL); |
1723 | 730 | } |
1724 | 725 | r += client->dataset[cursor].begin_position; |
1725 | | |
1726 | 725 | if (r >= 0) { |
1727 | | /* |
1728 | | * Ouch. Clearing the buffer like this hurts, especially |
1729 | | * at bid time. A lot of our efficiency at bid time comes |
1730 | | * from having bidders reuse the data we've already read. |
1731 | | * |
1732 | | * TODO: If the seek request is in data we already |
1733 | | * have, then don't call the seek callback. |
1734 | | * |
1735 | | * TODO: Zip seeks to end-of-file at bid time. If |
1736 | | * other formats also start doing this, we may need to |
1737 | | * find a way for clients to fudge the seek offset to |
1738 | | * a block boundary. |
1739 | | * |
1740 | | * Hmmm... If whence was SEEK_END, we know the file |
1741 | | * size is (r - offset). Can we use that to simplify |
1742 | | * the TODO items above? |
1743 | | */ |
1744 | 725 | filter->avail = filter->client_avail = 0; |
1745 | 725 | filter->next = filter->buffer; |
1746 | 725 | filter->position = r; |
1747 | 725 | filter->end_of_file = 0; |
1748 | 725 | } |
1749 | 725 | return r; |
1750 | 730 | } |