Coverage Report

Created: 2026-03-12 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/CMake/Utilities/cmlibarchive/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
1.44M
#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
29.0k
{
93
29.0k
  struct archive_read *a;
94
95
29.0k
  a = calloc(1, sizeof(*a));
96
29.0k
  if (a == NULL)
97
0
    return (NULL);
98
29.0k
  a->archive.magic = ARCHIVE_READ_MAGIC;
99
100
29.0k
  a->archive.state = ARCHIVE_STATE_NEW;
101
29.0k
  a->entry = archive_entry_new2(&a->archive);
102
29.0k
  a->archive.vtable = &archive_read_vtable;
103
104
29.0k
  a->passphrases.last = &a->passphrases.first;
105
106
29.0k
  return (&a->archive);
107
29.0k
}
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
29.0k
{
116
29.0k
  struct archive_read *a = (struct archive_read *)_a;
117
118
29.0k
  if (ARCHIVE_OK != __archive_check_magic(_a, ARCHIVE_READ_MAGIC,
119
29.0k
    ARCHIVE_STATE_ANY, "archive_read_extract_set_skip_file"))
120
0
    return;
121
29.0k
  a->skip_file_set = 1;
122
29.0k
  a->skip_file_dev = d;
123
29.0k
  a->skip_file_ino = i;
124
29.0k
}
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
70.5k
{
164
70.5k
  ssize_t r;
165
70.5k
  r = (self->archive->client.reader)(&self->archive->archive,
166
70.5k
      self->data, buff);
167
70.5k
  return (r);
168
70.5k
}
169
170
static int64_t
171
client_skip_proxy(struct archive_read_filter *self, int64_t request)
172
420
{
173
420
  if (request < 0)
174
0
    __archive_errx(1, "Negative skip requested.");
175
420
  if (request == 0)
176
0
    return 0;
177
178
420
  if (self->archive->client.skipper != NULL) {
179
420
    int64_t total = 0;
180
420
    for (;;) {
181
420
      int64_t get, ask = request;
182
420
      get = (self->archive->client.skipper)
183
420
        (&self->archive->archive, self->data, ask);
184
420
      total += get;
185
420
      if (get == 0 || get == request)
186
420
        return (total);
187
0
      if (get > request)
188
0
        return ARCHIVE_FATAL;
189
0
      request -= get;
190
0
    }
191
420
  } 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
420
}
213
214
static int64_t
215
client_seek_proxy(struct archive_read_filter *self, int64_t offset, int whence)
216
54.0k
{
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
54.0k
  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
54.0k
  return (self->archive->client.seeker)(&self->archive->archive,
228
54.0k
      self->data, offset, whence);
229
54.0k
}
230
231
static int
232
read_client_close_proxy(struct archive_read *a)
233
29.0k
{
234
29.0k
  int r = ARCHIVE_OK, r2;
235
29.0k
  unsigned int i;
236
237
29.0k
  if (a->client.closer == NULL)
238
0
    return (r);
239
58.1k
  for (i = 0; i < a->client.nodes; i++)
240
29.0k
  {
241
29.0k
    r2 = (a->client.closer)
242
29.0k
      ((struct archive *)a, a->client.dataset[i].data);
243
29.0k
    if (r > r2)
244
0
      r = r2;
245
29.0k
  }
246
29.0k
  return (r);
247
29.0k
}
248
249
static int
250
client_close_proxy(struct archive_read_filter *self)
251
29.0k
{
252
29.0k
  return read_client_close_proxy(self->archive);
253
29.0k
}
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
63.9k
{
268
63.9k
  int r1 = ARCHIVE_OK, r2 = ARCHIVE_OK;
269
63.9k
  void *data2 = NULL;
270
271
  /* Don't do anything if already in the specified data node */
272
63.9k
  if (self->archive->client.cursor == iindex)
273
63.9k
    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
63.9k
}
294
295
int
296
archive_read_set_open_callback(struct archive *_a,
297
    archive_open_callback *client_opener)
298
29.0k
{
299
29.0k
  struct archive_read *a = (struct archive_read *)_a;
300
29.0k
  archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
301
29.0k
      "archive_read_set_open_callback");
302
29.0k
  a->client.opener = client_opener;
303
29.0k
  return ARCHIVE_OK;
304
29.0k
}
305
306
int
307
archive_read_set_read_callback(struct archive *_a,
308
    archive_read_callback *client_reader)
309
29.0k
{
310
29.0k
  struct archive_read *a = (struct archive_read *)_a;
311
29.0k
  archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
312
29.0k
      "archive_read_set_read_callback");
313
29.0k
  a->client.reader = client_reader;
314
29.0k
  return ARCHIVE_OK;
315
29.0k
}
316
317
int
318
archive_read_set_skip_callback(struct archive *_a,
319
    archive_skip_callback *client_skipper)
320
29.0k
{
321
29.0k
  struct archive_read *a = (struct archive_read *)_a;
322
29.0k
  archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
323
29.0k
      "archive_read_set_skip_callback");
324
29.0k
  a->client.skipper = client_skipper;
325
29.0k
  return ARCHIVE_OK;
326
29.0k
}
327
328
int
329
archive_read_set_seek_callback(struct archive *_a,
330
    archive_seek_callback *client_seeker)
331
29.0k
{
332
29.0k
  struct archive_read *a = (struct archive_read *)_a;
333
29.0k
  archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
334
29.0k
      "archive_read_set_seek_callback");
335
29.0k
  a->client.seeker = client_seeker;
336
29.0k
  return ARCHIVE_OK;
337
29.0k
}
338
339
int
340
archive_read_set_close_callback(struct archive *_a,
341
    archive_close_callback *client_closer)
342
29.0k
{
343
29.0k
  struct archive_read *a = (struct archive_read *)_a;
344
29.0k
  archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
345
29.0k
      "archive_read_set_close_callback");
346
29.0k
  a->client.closer = client_closer;
347
29.0k
  return ARCHIVE_OK;
348
29.0k
}
349
350
int
351
archive_read_set_switch_callback(struct archive *_a,
352
    archive_switch_callback *client_switcher)
353
29.0k
{
354
29.0k
  struct archive_read *a = (struct archive_read *)_a;
355
29.0k
  archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
356
29.0k
      "archive_read_set_switch_callback");
357
29.0k
  a->client.switcher = client_switcher;
358
29.0k
  return ARCHIVE_OK;
359
29.0k
}
360
361
int
362
archive_read_set_callback_data(struct archive *_a, void *client_data)
363
0
{
364
0
  return archive_read_set_callback_data2(_a, client_data, 0);
365
0
}
366
367
int
368
archive_read_set_callback_data2(struct archive *_a, void *client_data,
369
    unsigned int iindex)
370
0
{
371
0
  struct archive_read *a = (struct archive_read *)_a;
372
0
  archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
373
0
      "archive_read_set_callback_data2");
374
375
0
  if (a->client.nodes == 0)
376
0
  {
377
0
    a->client.dataset = (struct archive_read_data_node *)
378
0
        calloc(1, sizeof(*a->client.dataset));
379
0
    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
0
    a->client.nodes = 1;
386
0
  }
387
388
0
  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
0
  a->client.dataset[iindex].data = client_data;
395
0
  a->client.dataset[iindex].begin_position = -1;
396
0
  a->client.dataset[iindex].total_size = -1;
397
0
  return ARCHIVE_OK;
398
0
}
399
400
int
401
archive_read_add_callback_data(struct archive *_a, void *client_data,
402
    unsigned int iindex)
403
29.0k
{
404
29.0k
  struct archive_read *a = (struct archive_read *)_a;
405
29.0k
  void *p;
406
29.0k
  unsigned int i;
407
408
29.0k
  archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
409
29.0k
      "archive_read_add_callback_data");
410
29.0k
  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
29.0k
  p = realloc(a->client.dataset, sizeof(*a->client.dataset)
416
29.0k
    * (++(a->client.nodes)));
417
29.0k
  if (p == NULL) {
418
0
    archive_set_error(&a->archive, ENOMEM,
419
0
      "No memory.");
420
0
    return ARCHIVE_FATAL;
421
0
  }
422
29.0k
  a->client.dataset = (struct archive_read_data_node *)p;
423
29.0k
  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
29.0k
  a->client.dataset[iindex].data = client_data;
429
29.0k
  a->client.dataset[iindex].begin_position = -1;
430
29.0k
  a->client.dataset[iindex].total_size = -1;
431
29.0k
  return ARCHIVE_OK;
432
29.0k
}
433
434
int
435
archive_read_append_callback_data(struct archive *_a, void *client_data)
436
29.0k
{
437
29.0k
  struct archive_read *a = (struct archive_read *)_a;
438
29.0k
  return archive_read_add_callback_data(_a, client_data, a->client.nodes);
439
29.0k
}
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
29.0k
{
456
29.0k
  struct archive_read *a = (struct archive_read *)_a;
457
29.0k
  struct archive_read_filter *filter, *tmp;
458
29.0k
  int slot, e = ARCHIVE_OK;
459
460
29.0k
  archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
461
29.0k
      "archive_read_open");
462
29.0k
  archive_clear_error(&a->archive);
463
464
29.0k
  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
29.0k
  if (a->client.opener != NULL) {
473
29.0k
    e = (a->client.opener)(&a->archive, a->client.dataset[0].data);
474
29.0k
    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
29.0k
  }
480
481
29.0k
  filter = calloc(1, sizeof(*filter));
482
29.0k
  if (filter == NULL)
483
0
    return (ARCHIVE_FATAL);
484
29.0k
  filter->bidder = NULL;
485
29.0k
  filter->upstream = NULL;
486
29.0k
  filter->archive = a;
487
29.0k
  filter->data = a->client.dataset[0].data;
488
29.0k
  filter->vtable = &none_reader_vtable;
489
29.0k
  filter->name = "none";
490
29.0k
  filter->code = ARCHIVE_FILTER_NONE;
491
29.0k
  filter->can_skip = 1;
492
29.0k
  filter->can_seek = 1;
493
494
29.0k
  a->client.dataset[0].begin_position = 0;
495
29.0k
  if (!a->filter || !a->bypass_filter_bidding)
496
29.0k
  {
497
29.0k
    a->filter = filter;
498
    /* Build out the input pipeline. */
499
29.0k
    e = choose_filters(a);
500
29.0k
    if (e < ARCHIVE_WARN) {
501
13.5k
      a->archive.state = ARCHIVE_STATE_FATAL;
502
13.5k
      return (ARCHIVE_FATAL);
503
13.5k
    }
504
29.0k
  }
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
15.4k
  if (!a->format)
515
15.4k
  {
516
15.4k
    slot = choose_format(a);
517
15.4k
    if (slot < 0) {
518
4.58k
      close_filters(a);
519
4.58k
      a->archive.state = ARCHIVE_STATE_FATAL;
520
4.58k
      return (ARCHIVE_FATAL);
521
4.58k
    }
522
10.9k
    a->format = &(a->formats[slot]);
523
10.9k
  }
524
525
10.9k
  a->archive.state = ARCHIVE_STATE_HEADER;
526
527
  /* Ensure libarchive starts from the first node in a multivolume set */
528
10.9k
  client_switch_proxy(a->filter, 0);
529
10.9k
  return (e);
530
15.4k
}
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
43.6k
#define MAX_NUMBER_FILTERS 25
540
541
static int
542
choose_filters(struct archive_read *a)
543
29.0k
{
544
29.0k
  int number_bidders, i, bid, best_bid, number_filters;
545
29.0k
  struct archive_read_filter_bidder *bidder, *best_bidder;
546
29.0k
  struct archive_read_filter *filter;
547
29.0k
  ssize_t avail;
548
29.0k
  int r;
549
550
43.6k
  for (number_filters = 0; number_filters < MAX_NUMBER_FILTERS; ++number_filters) {
551
43.6k
    number_bidders = sizeof(a->bidders) / sizeof(a->bidders[0]);
552
553
43.6k
    best_bid = 0;
554
43.6k
    best_bidder = NULL;
555
556
43.6k
    bidder = a->bidders;
557
742k
    for (i = 0; i < number_bidders; i++, bidder++) {
558
698k
      if (bidder->vtable == NULL)
559
131k
        continue;
560
567k
      bid = (bidder->vtable->bid)(bidder, a->filter);
561
567k
      if (bid > best_bid) {
562
14.6k
        best_bid = bid;
563
14.6k
        best_bidder = bidder;
564
14.6k
      }
565
567k
    }
566
567
    /* If no bidder, we're done. */
568
43.6k
    if (best_bidder == NULL) {
569
      /* Verify the filter by asking it for some data. */
570
29.0k
      __archive_read_filter_ahead(a->filter, 1, &avail);
571
29.0k
      if (avail < 0) {
572
13.5k
        __archive_read_free_filters(a);
573
13.5k
        return (ARCHIVE_FATAL);
574
13.5k
      }
575
15.4k
      return (ARCHIVE_OK);
576
29.0k
    }
577
578
14.6k
    filter = calloc(1, sizeof(*filter));
579
14.6k
    if (filter == NULL)
580
0
      return (ARCHIVE_FATAL);
581
14.6k
    filter->bidder = best_bidder;
582
14.6k
    filter->archive = a;
583
14.6k
    filter->upstream = a->filter;
584
14.6k
    a->filter = filter;
585
14.6k
    r = (best_bidder->vtable->init)(a->filter);
586
14.6k
    if (r != ARCHIVE_OK) {
587
6
      __archive_read_free_filters(a);
588
6
      return (ARCHIVE_FATAL);
589
6
    }
590
14.6k
  }
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
29.0k
}
595
596
int
597
__archive_read_header(struct archive_read *a, struct archive_entry *entry)
598
0
{
599
0
  if (!a->filter->vtable->read_header)
600
0
    return (ARCHIVE_OK);
601
0
  return a->filter->vtable->read_header(a->filter, entry);
602
0
}
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
11.7k
{
610
11.7k
  struct archive_read *a = (struct archive_read *)_a;
611
11.7k
  int r1 = ARCHIVE_OK, r2;
612
613
11.7k
  archive_check_magic(_a, ARCHIVE_READ_MAGIC,
614
11.7k
      ARCHIVE_STATE_HEADER | ARCHIVE_STATE_DATA,
615
11.7k
      "archive_read_next_header");
616
617
11.7k
  archive_entry_clear(entry);
618
11.7k
  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
11.7k
  if (a->archive.state == ARCHIVE_STATE_DATA) {
625
806
    r1 = archive_read_data_skip(&a->archive);
626
806
    if (r1 == ARCHIVE_EOF)
627
0
      archive_set_error(&a->archive, EIO,
628
0
          "Premature end-of-file.");
629
806
    if (r1 == ARCHIVE_EOF || r1 == ARCHIVE_FATAL) {
630
244
      a->archive.state = ARCHIVE_STATE_FATAL;
631
244
      return (ARCHIVE_FATAL);
632
244
    }
633
806
  }
634
635
  /* Record start-of-header offset in uncompressed stream. */
636
11.4k
  a->header_position = a->filter->position;
637
638
11.4k
  ++_a->file_count;
639
11.4k
  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
11.4k
  switch (r2) {
647
808
  case ARCHIVE_EOF:
648
808
    a->archive.state = ARCHIVE_STATE_EOF;
649
808
    --_a->file_count;/* Revert a file counter. */
650
808
    break;
651
4.65k
  case ARCHIVE_OK:
652
4.65k
    a->archive.state = ARCHIVE_STATE_DATA;
653
4.65k
    break;
654
2.44k
  case ARCHIVE_WARN:
655
2.44k
    a->archive.state = ARCHIVE_STATE_DATA;
656
2.44k
    break;
657
0
  case ARCHIVE_RETRY:
658
0
    break;
659
3.55k
  case ARCHIVE_FATAL:
660
3.55k
    a->archive.state = ARCHIVE_STATE_FATAL;
661
3.55k
    break;
662
11.4k
  }
663
664
11.4k
  __archive_reset_read_data(&a->archive);
665
666
11.4k
  a->data_start_node = a->client.cursor;
667
  /* EOF always wins; otherwise return the worst error. */
668
11.4k
  return (r2 < r1 || r2 == ARCHIVE_EOF) ? r2 : r1;
669
11.4k
}
670
671
static int
672
_archive_read_next_header(struct archive *_a, struct archive_entry **entryp)
673
11.7k
{
674
11.7k
  int ret;
675
11.7k
  struct archive_read *a = (struct archive_read *)_a;
676
11.7k
  *entryp = NULL;
677
11.7k
  ret = _archive_read_next_header2(_a, a->entry);
678
11.7k
  *entryp = a->entry;
679
11.7k
  return ret;
680
11.7k
}
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
15.4k
{
689
15.4k
  int slots;
690
15.4k
  int i;
691
15.4k
  int bid, best_bid;
692
15.4k
  int best_bid_slot;
693
694
15.4k
  slots = sizeof(a->formats) / sizeof(a->formats[0]);
695
15.4k
  best_bid = -1;
696
15.4k
  best_bid_slot = -1;
697
698
  /* Set up a->format for convenience of bidders. */
699
15.4k
  a->format = &(a->formats[0]);
700
263k
  for (i = 0; i < slots; i++, a->format++) {
701
247k
    if (a->format->bid) {
702
216k
      bid = (a->format->bid)(a, best_bid);
703
216k
      if (bid == ARCHIVE_FATAL)
704
0
        return (ARCHIVE_FATAL);
705
216k
      if (a->filter->position != 0)
706
564
        __archive_read_seek(a, 0, SEEK_SET);
707
216k
      if ((bid > best_bid) || (best_bid_slot < 0)) {
708
38.4k
        best_bid = bid;
709
38.4k
        best_bid_slot = i;
710
38.4k
      }
711
216k
    }
712
247k
  }
713
714
  /*
715
   * There were no bidders; this is a serious programmer error
716
   * and demands a quick and definitive abort.
717
   */
718
15.4k
  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
15.4k
  if (best_bid < 1) {
729
4.58k
    archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
730
4.58k
        "Unrecognized archive format");
731
4.58k
    return (ARCHIVE_FATAL);
732
4.58k
  }
733
734
10.9k
  return (best_bid_slot);
735
15.4k
}
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
0
{
771
0
  struct archive_read *a = (struct archive_read *)_a;
772
0
  int format_supports_encryption = archive_read_format_capabilities(_a)
773
0
      & (ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_DATA | ARCHIVE_READ_FORMAT_CAPS_ENCRYPT_METADATA);
774
775
0
  if (!_a || !format_supports_encryption) {
776
    /* Format in general doesn't support encryption */
777
0
    return ARCHIVE_READ_FORMAT_ENCRYPTION_UNSUPPORTED;
778
0
  }
779
780
  /* A reader potentially has read enough data now. */
781
0
  if (a->format && a->format->has_encrypted_entries) {
782
0
    return (a->format->has_encrypted_entries)(a);
783
0
  }
784
785
  /* For any other reason we cannot say how many entries are there. */
786
0
  return ARCHIVE_READ_FORMAT_ENCRYPTION_DONT_KNOW;
787
0
}
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
0
{
796
0
  struct archive_read *a = (struct archive_read *)_a;
797
0
  if (a && a->format && a->format->format_capabilties) {
798
0
    return (a->format->format_capabilties)(a);
799
0
  }
800
0
  return ARCHIVE_READ_FORMAT_CAPS_NONE;
801
0
}
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
0
{
817
0
  struct archive *a = (struct archive *)_a;
818
0
  char  *dest;
819
0
  const void *read_buf;
820
0
  size_t   bytes_read;
821
0
  size_t   len;
822
0
  int  r;
823
824
0
  bytes_read = 0;
825
0
  dest = (char *)buff;
826
827
0
  while (s > 0) {
828
0
    if (a->read_data_offset == a->read_data_output_offset &&
829
0
        a->read_data_remaining == 0) {
830
0
      read_buf = a->read_data_block;
831
0
      a->read_data_is_posix_read = 1;
832
0
      a->read_data_requested = s;
833
0
      r = archive_read_data_block(a, &read_buf,
834
0
          &a->read_data_remaining, &a->read_data_offset);
835
0
      a->read_data_block = read_buf;
836
0
      if (r == ARCHIVE_EOF &&
837
0
          a->read_data_offset == a->read_data_output_offset &&
838
0
          a->read_data_remaining == 0)
839
0
        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
0
      if (r < ARCHIVE_OK)
846
0
        return (r);
847
0
    }
848
849
0
    if (a->read_data_offset < a->read_data_output_offset) {
850
0
      archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
851
0
          "Encountered out-of-order sparse blocks");
852
0
      return (ARCHIVE_RETRY);
853
0
    }
854
855
    /* Compute the amount of zero padding needed. */
856
0
    if (a->read_data_output_offset + (int64_t)s <
857
0
        a->read_data_offset) {
858
0
      len = s;
859
0
    } else if (a->read_data_output_offset <
860
0
        a->read_data_offset) {
861
0
      len = (size_t)(a->read_data_offset -
862
0
          a->read_data_output_offset);
863
0
    } else
864
0
      len = 0;
865
866
    /* Add zeroes. */
867
0
    memset(dest, 0, len);
868
0
    s -= len;
869
0
    a->read_data_output_offset += len;
870
0
    dest += len;
871
0
    bytes_read += len;
872
873
    /* Copy data if there is any space left. */
874
0
    if (s > 0) {
875
0
      len = a->read_data_remaining;
876
0
      if (len > s)
877
0
        len = s;
878
0
      if (len) {
879
0
        memcpy(dest, a->read_data_block, len);
880
0
        s -= len;
881
0
        a->read_data_block += len;
882
0
        a->read_data_remaining -= len;
883
0
        a->read_data_output_offset += len;
884
0
        a->read_data_offset += len;
885
0
        dest += len;
886
0
        bytes_read += len;
887
0
      }
888
0
    }
889
0
  }
890
0
  a->read_data_is_posix_read = 0;
891
0
  a->read_data_requested = 0;
892
0
  return (bytes_read);
893
0
}
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
11.4k
{
900
11.4k
  a->read_data_output_offset = 0;
901
11.4k
  a->read_data_remaining = 0;
902
11.4k
  a->read_data_is_posix_read = 0;
903
11.4k
  a->read_data_requested = 0;
904
905
   /* extra resets, from rar.c */
906
11.4k
   a->read_data_block = NULL;
907
11.4k
   a->read_data_offset = 0;
908
11.4k
}
909
910
/*
911
 * Skip over all remaining data in this entry.
912
 */
913
int
914
archive_read_data_skip(struct archive *_a)
915
806
{
916
806
  struct archive_read *a = (struct archive_read *)_a;
917
806
  int r;
918
806
  const void *buff;
919
806
  size_t size;
920
806
  int64_t offset;
921
922
806
  archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
923
806
      "archive_read_data_skip");
924
925
806
  if (a->format->read_data_skip != NULL)
926
806
    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
806
  if (r == ARCHIVE_EOF)
935
0
    r = ARCHIVE_OK;
936
937
806
  a->archive.state = ARCHIVE_STATE_HEADER;
938
806
  return (r);
939
806
}
940
941
la_int64_t
942
archive_seek_data(struct archive *_a, int64_t offset, int whence)
943
0
{
944
0
  struct archive_read *a = (struct archive_read *)_a;
945
0
  archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
946
0
      "archive_seek_data_block");
947
948
0
  if (a->format->seek_data == NULL) {
949
0
    archive_set_error(&a->archive, ARCHIVE_ERRNO_PROGRAMMER,
950
0
        "Internal error: "
951
0
        "No format_seek_data_block function registered");
952
0
    return (ARCHIVE_FATAL);
953
0
  }
954
955
0
  return (a->format->seek_data)(a, offset, whence);
956
0
}
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
5.57k
{
970
5.57k
  struct archive_read *a = (struct archive_read *)_a;
971
5.57k
  archive_check_magic(_a, ARCHIVE_READ_MAGIC, ARCHIVE_STATE_DATA,
972
5.57k
      "archive_read_data_block");
973
974
5.57k
  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
5.57k
  return (a->format->read_data)(a, buff, size, offset);
982
5.57k
}
983
984
static int
985
close_filters(struct archive_read *a)
986
76.3k
{
987
76.3k
  struct archive_read_filter *f = a->filter;
988
76.3k
  int r = ARCHIVE_OK;
989
  /* Close each filter in the pipeline. */
990
141k
  while (f != NULL) {
991
65.5k
    struct archive_read_filter *t = f->upstream;
992
65.5k
    if (!f->closed && f->vtable != NULL) {
993
43.6k
      int r1 = (f->vtable->close)(f);
994
43.6k
      f->closed = 1;
995
43.6k
      if (r1 < r)
996
16
        r = r1;
997
43.6k
    }
998
65.5k
    free(f->buffer);
999
65.5k
    f->buffer = NULL;
1000
65.5k
    f = t;
1001
65.5k
  }
1002
76.3k
  return r;
1003
76.3k
}
1004
1005
void
1006
__archive_read_free_filters(struct archive_read *a)
1007
42.6k
{
1008
  /* Make sure filters are closed and their buffers are freed */
1009
42.6k
  close_filters(a);
1010
1011
86.3k
  while (a->filter != NULL) {
1012
43.6k
    struct archive_read_filter *t = a->filter->upstream;
1013
43.6k
    free(a->filter);
1014
43.6k
    a->filter = t;
1015
43.6k
  }
1016
42.6k
}
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
29.0k
{
1040
29.0k
  struct archive_read *a = (struct archive_read *)_a;
1041
29.0k
  int r = ARCHIVE_OK, r1 = ARCHIVE_OK;
1042
1043
29.0k
  archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
1044
29.0k
      ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_close");
1045
29.0k
  if (a->archive.state == ARCHIVE_STATE_CLOSED)
1046
0
    return (ARCHIVE_OK);
1047
29.0k
  archive_clear_error(&a->archive);
1048
29.0k
  a->archive.state = ARCHIVE_STATE_CLOSED;
1049
1050
  /* TODO: Clean up the formatters. */
1051
1052
  /* Release the filter objects. */
1053
29.0k
  r1 = close_filters(a);
1054
29.0k
  if (r1 < r)
1055
0
    r = r1;
1056
1057
29.0k
  return (r);
1058
29.0k
}
1059
1060
/*
1061
 * Release memory and other resources.
1062
 */
1063
static int
1064
_archive_read_free(struct archive *_a)
1065
29.0k
{
1066
29.0k
  struct archive_read *a = (struct archive_read *)_a;
1067
29.0k
  struct archive_read_passphrase *p;
1068
29.0k
  int i, n;
1069
29.0k
  int slots;
1070
29.0k
  int r = ARCHIVE_OK;
1071
1072
29.0k
  if (_a == NULL)
1073
0
    return (ARCHIVE_OK);
1074
29.0k
  archive_check_magic(_a, ARCHIVE_READ_MAGIC,
1075
29.0k
      ARCHIVE_STATE_ANY | ARCHIVE_STATE_FATAL, "archive_read_free");
1076
29.0k
  if (a->archive.state != ARCHIVE_STATE_CLOSED
1077
0
      && a->archive.state != ARCHIVE_STATE_FATAL)
1078
0
    r = archive_read_close(&a->archive);
1079
1080
  /* Call cleanup functions registered by optional components. */
1081
29.0k
  if (a->cleanup_archive_extract != NULL)
1082
0
    r = (a->cleanup_archive_extract)(a);
1083
1084
  /* Cleanup format-specific data. */
1085
29.0k
  slots = sizeof(a->formats) / sizeof(a->formats[0]);
1086
494k
  for (i = 0; i < slots; i++) {
1087
465k
    a->format = &(a->formats[i]);
1088
465k
    if (a->formats[i].cleanup)
1089
378k
      (a->formats[i].cleanup)(a);
1090
465k
  }
1091
1092
  /* Free the filters */
1093
29.0k
  __archive_read_free_filters(a);
1094
1095
  /* Release the bidder objects. */
1096
29.0k
  n = sizeof(a->bidders)/sizeof(a->bidders[0]);
1097
494k
  for (i = 0; i < n; i++) {
1098
465k
    if (a->bidders[i].vtable == NULL ||
1099
378k
        a->bidders[i].vtable->free == NULL)
1100
465k
      continue;
1101
0
    (a->bidders[i].vtable->free)(&a->bidders[i]);
1102
0
  }
1103
1104
  /* Release passphrase list. */
1105
29.0k
  p = a->passphrases.first;
1106
29.0k
  while (p != NULL) {
1107
0
    struct archive_read_passphrase *np = p->next;
1108
1109
    /* A passphrase should be cleaned. */
1110
0
    memset(p->passphrase, 0, strlen(p->passphrase));
1111
0
    free(p->passphrase);
1112
0
    free(p);
1113
0
    p = np;
1114
0
  }
1115
1116
29.0k
  archive_string_free(&a->archive.error_string);
1117
29.0k
  archive_entry_free(a->entry);
1118
29.0k
  a->archive.magic = 0;
1119
29.0k
  __archive_clean(&a->archive);
1120
29.0k
  free(a->client.dataset);
1121
29.0k
  free(a);
1122
29.0k
  return (r);
1123
29.0k
}
1124
1125
static struct archive_read_filter *
1126
get_filter(struct archive *_a, int n)
1127
72
{
1128
72
  struct archive_read *a = (struct archive_read *)_a;
1129
72
  struct archive_read_filter *f = a->filter;
1130
  /* We use n == -1 for 'the last filter', which is always the
1131
   * client proxy. */
1132
72
  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
72
  if (n < 0)
1142
0
    return NULL;
1143
72
  while (n > 0 && f != NULL) {
1144
0
    f = f->upstream;
1145
0
    --n;
1146
0
  }
1147
72
  return (f);
1148
72
}
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
72
{
1167
72
  struct archive_read_filter *f = get_filter(_a, n);
1168
72
  return f == NULL ? -1 : f->position;
1169
72
}
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
407k
{
1189
407k
  int i, number_slots;
1190
1191
407k
  archive_check_magic(&a->archive,
1192
407k
      ARCHIVE_READ_MAGIC, ARCHIVE_STATE_NEW,
1193
407k
      "__archive_read_register_format");
1194
1195
407k
  number_slots = sizeof(a->formats) / sizeof(a->formats[0]);
1196
1197
3.05M
  for (i = 0; i < number_slots; i++) {
1198
3.05M
    if (a->formats[i].bid == bid)
1199
0
      return (ARCHIVE_WARN); /* We've already installed */
1200
3.05M
    if (a->formats[i].bid == NULL) {
1201
407k
      a->formats[i].bid = bid;
1202
407k
      a->formats[i].options = options;
1203
407k
      a->formats[i].read_header = read_header;
1204
407k
      a->formats[i].read_data = read_data;
1205
407k
      a->formats[i].read_data_skip = read_data_skip;
1206
407k
      a->formats[i].seek_data = seek_data;
1207
407k
      a->formats[i].cleanup = cleanup;
1208
407k
      a->formats[i].data = format_data;
1209
407k
      a->formats[i].name = name;
1210
407k
      a->formats[i].format_capabilties = format_capabilities;
1211
407k
      a->formats[i].has_encrypted_entries = has_encrypted_entries;
1212
407k
      return (ARCHIVE_OK);
1213
407k
    }
1214
3.05M
  }
1215
1216
0
  archive_set_error(&a->archive, ENOMEM,
1217
0
      "Not enough slots for format registration");
1218
0
  return (ARCHIVE_FATAL);
1219
407k
}
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
378k
{
1231
378k
  struct archive_read_filter_bidder *bidder;
1232
378k
  int i, number_slots;
1233
1234
378k
  archive_check_magic(&a->archive, ARCHIVE_READ_MAGIC,
1235
378k
      ARCHIVE_STATE_NEW, "__archive_read_register_bidder");
1236
1237
378k
  number_slots = sizeof(a->bidders) / sizeof(a->bidders[0]);
1238
1239
2.64M
  for (i = 0; i < number_slots; i++) {
1240
2.64M
    if (a->bidders[i].vtable != NULL)
1241
2.26M
      continue;
1242
378k
    memset(a->bidders + i, 0, sizeof(a->bidders[0]));
1243
378k
    bidder = (a->bidders + i);
1244
378k
    bidder->data = bidder_data;
1245
378k
    bidder->name = name;
1246
378k
    bidder->vtable = vtable;
1247
378k
    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
378k
    return (ARCHIVE_OK);
1255
378k
  }
1256
1257
0
  archive_set_error(&a->archive, ENOMEM,
1258
0
      "Not enough slots for filter registration");
1259
0
  return (ARCHIVE_FATAL);
1260
378k
}
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
1.80M
{
1316
1.80M
  return (__archive_read_filter_ahead(a->filter, min, avail));
1317
1.80M
}
1318
1319
const void *
1320
__archive_read_filter_ahead(struct archive_read_filter *filter,
1321
    size_t min, ssize_t *avail)
1322
2.44M
{
1323
2.44M
  ssize_t bytes_read;
1324
2.44M
  size_t tocopy;
1325
1326
2.44M
  if (filter->fatal) {
1327
180k
    if (avail)
1328
178k
      *avail = ARCHIVE_FATAL;
1329
180k
    return (NULL);
1330
180k
  }
1331
1332
  /*
1333
   * Keep pulling more data until we can satisfy the request.
1334
   */
1335
2.32M
  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
2.32M
    if (filter->avail >= min && filter->avail > 0) {
1344
1.53M
      if (avail != NULL)
1345
1.44M
        *avail = filter->avail;
1346
1.53M
      return (filter->next);
1347
1.53M
    }
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
796k
    if (filter->client_total >= filter->client_avail + filter->avail
1354
629k
        && filter->client_avail + filter->avail >= min) {
1355
      /* "Roll back" to client buffer. */
1356
511k
      filter->client_avail += filter->avail;
1357
511k
      filter->client_next -= filter->avail;
1358
      /* Copy buffer is now empty. */
1359
511k
      filter->avail = 0;
1360
511k
      filter->next = filter->buffer;
1361
      /* Return data from client buffer. */
1362
511k
      if (avail != NULL)
1363
426k
        *avail = filter->client_avail;
1364
511k
      return (filter->client_next);
1365
511k
    }
1366
1367
    /* Move data forward in copy buffer if necessary. */
1368
284k
    if (filter->next > filter->buffer &&
1369
73.1k
        filter->next + min > filter->buffer + filter->buffer_size) {
1370
798
      if (filter->avail > 0)
1371
774
        memmove(filter->buffer, filter->next,
1372
774
            filter->avail);
1373
798
      filter->next = filter->buffer;
1374
798
    }
1375
1376
    /* If we've used up the client data, get more. */
1377
284k
    if (filter->client_avail <= 0) {
1378
261k
      if (filter->end_of_file) {
1379
175k
        if (avail != NULL)
1380
159k
          *avail = filter->avail;
1381
175k
        return (NULL);
1382
175k
      }
1383
86.8k
      bytes_read = (filter->vtable->read)(filter,
1384
86.8k
          &filter->client_buff);
1385
86.8k
      if (bytes_read < 0) {   /* Read error. */
1386
13.8k
        filter->client_total = filter->client_avail = 0;
1387
13.8k
        filter->client_next =
1388
13.8k
            filter->client_buff = NULL;
1389
13.8k
        filter->fatal = 1;
1390
13.8k
        if (avail != NULL)
1391
13.8k
          *avail = ARCHIVE_FATAL;
1392
13.8k
        return (NULL);
1393
13.8k
      }
1394
72.9k
      if (bytes_read == 0) {
1395
        /* Check for another client object first */
1396
27.5k
        if (filter->archive->client.cursor !=
1397
27.5k
              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
27.5k
        filter->client_total = filter->client_avail = 0;
1405
27.5k
        filter->client_next =
1406
27.5k
            filter->client_buff = NULL;
1407
27.5k
        filter->end_of_file = 1;
1408
        /* Return whatever we do have. */
1409
27.5k
        if (avail != NULL)
1410
16.4k
          *avail = filter->avail;
1411
27.5k
        return (NULL);
1412
27.5k
      }
1413
45.3k
      filter->client_total = bytes_read;
1414
45.3k
      filter->client_avail = filter->client_total;
1415
45.3k
      filter->client_next = filter->client_buff;
1416
45.3k
    } 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
22.6k
      if (min > filter->buffer_size) {
1426
19.0k
        size_t s, t;
1427
19.0k
        char *p;
1428
1429
        /* Double the buffer; watch for overflow. */
1430
19.0k
        s = t = filter->buffer_size;
1431
19.0k
        if (s == 0)
1432
16.9k
          s = min;
1433
38.2k
        while (s < min) {
1434
19.2k
          t *= 2;
1435
19.2k
          if (t <= s) { /* Integer overflow! */
1436
64
            archive_set_error(
1437
64
                &filter->archive->archive,
1438
64
                ENOMEM,
1439
64
                "Unable to allocate copy"
1440
64
                " buffer");
1441
64
            filter->fatal = 1;
1442
64
            if (avail != NULL)
1443
0
              *avail = ARCHIVE_FATAL;
1444
64
            return (NULL);
1445
64
          }
1446
19.2k
          s = t;
1447
19.2k
        }
1448
        /* Now s >= min, so allocate a new buffer. */
1449
18.9k
        p = malloc(s);
1450
18.9k
        if (p == NULL) {
1451
50
          archive_set_error(
1452
50
            &filter->archive->archive,
1453
50
            ENOMEM,
1454
50
              "Unable to allocate copy buffer");
1455
50
          filter->fatal = 1;
1456
50
          if (avail != NULL)
1457
0
            *avail = ARCHIVE_FATAL;
1458
50
          return (NULL);
1459
50
        }
1460
        /* Move data into newly-enlarged buffer. */
1461
18.9k
        if (filter->avail > 0)
1462
330
          memmove(p, filter->next, filter->avail);
1463
18.9k
        free(filter->buffer);
1464
18.9k
        filter->next = filter->buffer = p;
1465
18.9k
        filter->buffer_size = s;
1466
18.9k
      }
1467
1468
      /* We can add client data to copy buffer. */
1469
      /* First estimate: copy to fill rest of buffer. */
1470
22.5k
      tocopy = (filter->buffer + filter->buffer_size)
1471
22.5k
          - (filter->next + filter->avail);
1472
      /* Don't waste time buffering more than we need to. */
1473
22.5k
      if (tocopy + filter->avail > min)
1474
2.30k
        tocopy = min - filter->avail;
1475
      /* Don't copy more than is available. */
1476
22.5k
      if (tocopy > filter->client_avail)
1477
21.4k
        tocopy = filter->client_avail;
1478
1479
22.5k
      memcpy(filter->next + filter->avail,
1480
22.5k
          filter->client_next, tocopy);
1481
      /* Remove this data from client buffer. */
1482
22.5k
      filter->client_next += tocopy;
1483
22.5k
      filter->client_avail -= tocopy;
1484
      /* add it to copy buffer. */
1485
22.5k
      filter->avail += tocopy;
1486
22.5k
    }
1487
284k
  }
1488
2.26M
}
1489
1490
/*
1491
 * Move the file pointer forward.
1492
 */
1493
int64_t
1494
__archive_read_consume(struct archive_read *a, int64_t request)
1495
1.44M
{
1496
1.44M
  return (__archive_read_filter_consume(a->filter, request));
1497
1.44M
}
1498
1499
int64_t
1500
__archive_read_filter_consume(struct archive_read_filter * filter,
1501
    int64_t request)
1502
1.46M
{
1503
1.46M
  int64_t skipped;
1504
1505
1.46M
  if (request < 0)
1506
0
    return ARCHIVE_FATAL;
1507
1.46M
  if (request == 0)
1508
22.0k
    return 0;
1509
1510
1.44M
  skipped = advance_file_pointer(filter, request);
1511
1.44M
  if (skipped == request)
1512
1.44M
    return (skipped);
1513
  /* We hit EOF before we satisfied the skip request. */
1514
420
  if (skipped < 0)  /* Map error code to 0 for error message below. */
1515
0
    skipped = 0;
1516
420
  archive_set_error(&filter->archive->archive,
1517
420
      ARCHIVE_ERRNO_MISC,
1518
420
      "Truncated input file (needed %jd bytes, only %jd available)",
1519
420
      (intmax_t)request, (intmax_t)skipped);
1520
420
  return (ARCHIVE_FATAL);
1521
1.44M
}
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
1.44M
{
1532
1.44M
  int64_t bytes_skipped, total_bytes_skipped = 0;
1533
1.44M
  ssize_t bytes_read;
1534
1.44M
  size_t min;
1535
1536
1.44M
  if (filter->fatal)
1537
0
    return (-1);
1538
1539
  /* Use up the copy buffer first. */
1540
1.44M
  if (filter->avail > 0) {
1541
1.37M
    min = (size_t)minimum(request, (int64_t)filter->avail);
1542
1.37M
    filter->next += min;
1543
1.37M
    filter->avail -= min;
1544
1.37M
    request -= min;
1545
1.37M
    filter->position += min;
1546
1.37M
    total_bytes_skipped += min;
1547
1.37M
  }
1548
1549
  /* Then use up the client buffer. */
1550
1.44M
  if (filter->client_avail > 0) {
1551
66.5k
    min = (size_t)minimum(request, (int64_t)filter->client_avail);
1552
66.5k
    filter->client_next += min;
1553
66.5k
    filter->client_avail -= min;
1554
66.5k
    request -= min;
1555
66.5k
    filter->position += min;
1556
66.5k
    total_bytes_skipped += min;
1557
66.5k
  }
1558
1.44M
  if (request == 0)
1559
1.44M
    return (total_bytes_skipped);
1560
1561
  /* If there's an optimized skip function, use it. */
1562
420
  if (filter->can_skip != 0) {
1563
420
    bytes_skipped = client_skip_proxy(filter, request);
1564
420
    if (bytes_skipped < 0) { /* error */
1565
0
      filter->fatal = 1;
1566
0
      return (bytes_skipped);
1567
0
    }
1568
420
    filter->position += bytes_skipped;
1569
420
    total_bytes_skipped += bytes_skipped;
1570
420
    request -= bytes_skipped;
1571
420
    if (request == 0)
1572
0
      return (total_bytes_skipped);
1573
420
  }
1574
1575
  /* Use ordinary reads as necessary to complete the request. */
1576
424
  for (;;) {
1577
424
    bytes_read = (filter->vtable->read)(filter, &filter->client_buff);
1578
424
    if (bytes_read < 0) {
1579
0
      filter->client_buff = NULL;
1580
0
      filter->fatal = 1;
1581
0
      return (bytes_read);
1582
0
    }
1583
1584
424
    if (bytes_read == 0) {
1585
420
      if (filter->archive->client.cursor !=
1586
420
            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
420
      filter->client_buff = NULL;
1593
420
      filter->end_of_file = 1;
1594
420
      return (total_bytes_skipped);
1595
420
    }
1596
1597
4
    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
4
    filter->position += bytes_read;
1608
4
    total_bytes_skipped += bytes_read;
1609
4
    request -= bytes_read;
1610
4
  }
1611
420
}
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
29.0k
{
1619
29.0k
  return __archive_read_filter_seek(a->filter, offset, whence);
1620
29.0k
}
1621
1622
int64_t
1623
__archive_read_filter_seek(struct archive_read_filter *filter, int64_t offset,
1624
    int whence)
1625
29.0k
{
1626
29.0k
  struct archive_read_client *client;
1627
29.0k
  int64_t r;
1628
29.0k
  unsigned int cursor;
1629
1630
29.0k
  if (filter->closed || filter->fatal)
1631
1.09k
    return (ARCHIVE_FATAL);
1632
27.9k
  if (filter->can_seek == 0)
1633
690
    return (ARCHIVE_FAILED);
1634
1635
27.2k
  client = &(filter->archive->client);
1636
27.2k
  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
1.40k
  case SEEK_SET:
1642
1.40k
    cursor = 0;
1643
1.40k
    while (1)
1644
1.40k
    {
1645
1.40k
      if (client->dataset[cursor].begin_position < 0 ||
1646
1.40k
          client->dataset[cursor].total_size < 0 ||
1647
826
          client->dataset[cursor].begin_position +
1648
826
            client->dataset[cursor].total_size - 1 > offset ||
1649
100
          cursor + 1 >= client->nodes)
1650
1.40k
        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
1.40k
    while (1) {
1656
1.40k
      r = client_switch_proxy(filter, cursor);
1657
1.40k
      if (r != ARCHIVE_OK)
1658
0
        return r;
1659
1.40k
      if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
1660
0
        return r;
1661
1.40k
      client->dataset[cursor].total_size = r;
1662
1.40k
      if (client->dataset[cursor].begin_position +
1663
1.40k
          client->dataset[cursor].total_size - 1 > offset ||
1664
206
          cursor + 1 >= client->nodes)
1665
1.40k
        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
1.40k
    offset -= client->dataset[cursor].begin_position;
1671
1.40k
    if (offset < 0
1672
1.21k
        || offset > client->dataset[cursor].total_size)
1673
360
      return ARCHIVE_FATAL;
1674
1.04k
    if ((r = client_seek_proxy(filter, offset, SEEK_SET)) < 0)
1675
0
      return r;
1676
1.04k
    break;
1677
1678
25.8k
  case SEEK_END:
1679
25.8k
    cursor = 0;
1680
25.8k
    while (1) {
1681
25.8k
      if (client->dataset[cursor].begin_position < 0 ||
1682
25.8k
          client->dataset[cursor].total_size < 0 ||
1683
13.3k
          cursor + 1 >= client->nodes)
1684
25.8k
        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
25.8k
    while (1) {
1690
25.8k
      r = client_switch_proxy(filter, cursor);
1691
25.8k
      if (r != ARCHIVE_OK)
1692
0
        return r;
1693
25.8k
      if ((r = client_seek_proxy(filter, 0, SEEK_END)) < 0)
1694
0
        return r;
1695
25.8k
      client->dataset[cursor].total_size = r;
1696
25.8k
      r = client->dataset[cursor].begin_position +
1697
25.8k
        client->dataset[cursor].total_size;
1698
25.8k
      if (cursor + 1 >= client->nodes)
1699
25.8k
        break;
1700
0
      client->dataset[++cursor].begin_position = r;
1701
0
    }
1702
25.8k
    while (1) {
1703
25.8k
      if (r + offset >=
1704
25.8k
          client->dataset[cursor].begin_position)
1705
25.8k
        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
25.8k
    offset = (r + offset) - client->dataset[cursor].begin_position;
1714
25.8k
    if ((r = client_switch_proxy(filter, cursor)) != ARCHIVE_OK)
1715
0
      return r;
1716
25.8k
    r = client_seek_proxy(filter, offset, SEEK_SET);
1717
25.8k
    if (r < ARCHIVE_OK)
1718
0
      return r;
1719
25.8k
    break;
1720
1721
25.8k
  default:
1722
0
    return (ARCHIVE_FATAL);
1723
27.2k
  }
1724
26.8k
  r += client->dataset[cursor].begin_position;
1725
1726
26.8k
  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
26.8k
    filter->avail = filter->client_avail = 0;
1745
26.8k
    filter->next = filter->buffer;
1746
26.8k
    filter->position = r;
1747
26.8k
    filter->end_of_file = 0;
1748
26.8k
  }
1749
26.8k
  return r;
1750
27.2k
}