Coverage Report

Created: 2026-10-10 07:18

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