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