Coverage Report

Created: 2026-09-14 06:43

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