Coverage Report

Created: 2024-07-23 06:29

/src/libarchive/libarchive/archive_read_support_format_xar.c
Line
Count
Source (jump to first uncovered line)
1
/*-
2
 * Copyright (c) 2009 Michihiro NAKAJIMA
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
#include "archive_platform.h"
26
27
#ifdef HAVE_ERRNO_H
28
#include <errno.h>
29
#endif
30
#ifdef HAVE_STDLIB_H
31
#include <stdlib.h>
32
#endif
33
#if HAVE_LIBXML_XMLREADER_H
34
#include <libxml/xmlreader.h>
35
#elif HAVE_BSDXML_H
36
#include <bsdxml.h>
37
#elif HAVE_EXPAT_H
38
#include <expat.h>
39
#endif
40
#ifdef HAVE_BZLIB_H
41
#include <bzlib.h>
42
#endif
43
#if HAVE_LZMA_H
44
#include <lzma.h>
45
#endif
46
#ifdef HAVE_ZLIB_H
47
#include <zlib.h>
48
#endif
49
50
#include "archive.h"
51
#include "archive_digest_private.h"
52
#include "archive_endian.h"
53
#include "archive_entry.h"
54
#include "archive_entry_locale.h"
55
#include "archive_private.h"
56
#include "archive_read_private.h"
57
58
#if (!defined(HAVE_LIBXML_XMLREADER_H) && \
59
     !defined(HAVE_BSDXML_H) && !defined(HAVE_EXPAT_H)) ||\
60
  !defined(HAVE_ZLIB_H) || \
61
  !defined(ARCHIVE_HAS_MD5) || !defined(ARCHIVE_HAS_SHA1)
62
/*
63
 * xar needs several external libraries.
64
 *   o libxml2 or expat --- XML parser
65
 *   o openssl or MD5/SHA1 hash function
66
 *   o zlib
67
 *   o bzlib2 (option)
68
 *   o liblzma (option)
69
 */
70
int
71
archive_read_support_format_xar(struct archive *_a)
72
{
73
  struct archive_read *a = (struct archive_read *)_a;
74
  archive_check_magic(_a, ARCHIVE_READ_MAGIC,
75
      ARCHIVE_STATE_NEW, "archive_read_support_format_xar");
76
77
  archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
78
      "Xar not supported on this platform");
79
  return (ARCHIVE_WARN);
80
}
81
82
#else /* Support xar format */
83
84
/* #define DEBUG 1 */
85
/* #define DEBUG_PRINT_TOC 1 */
86
#if DEBUG_PRINT_TOC
87
#define PRINT_TOC(d, outbytes)  do {        \
88
  unsigned char *x = (unsigned char *)(uintptr_t)d; \
89
  unsigned char c = x[outbytes-1];      \
90
  x[outbytes - 1] = 0;          \
91
  fprintf(stderr, "%s", x);       \
92
  fprintf(stderr, "%c", c);       \
93
  x[outbytes - 1] = c;          \
94
} while (0)
95
#else
96
#define PRINT_TOC(d, outbytes)
97
#endif
98
99
232
#define HEADER_MAGIC  0x78617221
100
242
#define HEADER_SIZE 28
101
2
#define HEADER_VERSION  1
102
1.17k
#define CKSUM_NONE  0
103
0
#define CKSUM_SHA1  1
104
697
#define CKSUM_MD5 2
105
106
2
#define MD5_SIZE  16
107
0
#define SHA1_SIZE 20
108
#define MAX_SUM_SIZE  20
109
110
enum enctype {
111
  NONE,
112
  GZIP,
113
  BZIP2,
114
  LZMA,
115
  XZ,
116
};
117
118
struct chksumval {
119
  int      alg;
120
  size_t       len;
121
  unsigned char    val[MAX_SUM_SIZE];
122
};
123
124
struct chksumwork {
125
  int      alg;
126
#ifdef ARCHIVE_HAS_MD5
127
  archive_md5_ctx    md5ctx;
128
#endif
129
#ifdef ARCHIVE_HAS_SHA1
130
  archive_sha1_ctx   sha1ctx;
131
#endif
132
};
133
134
struct xattr {
135
  struct xattr    *next;
136
  struct archive_string  name;
137
  uint64_t     id;
138
  uint64_t     length;
139
  uint64_t     offset;
140
  uint64_t     size;
141
  enum enctype     encoding;
142
  struct chksumval   a_sum;
143
  struct chksumval   e_sum;
144
  struct archive_string  fstype;
145
};
146
147
struct xar_file {
148
  struct xar_file   *next;
149
  struct xar_file   *hdnext;
150
  struct xar_file   *parent;
151
  int      subdirs;
152
153
  unsigned int     has;
154
0
#define HAS_DATA    0x00001
155
0
#define HAS_PATHNAME    0x00002
156
0
#define HAS_SYMLINK   0x00004
157
0
#define HAS_TIME    0x00008
158
0
#define HAS_UID     0x00010
159
0
#define HAS_GID     0x00020
160
0
#define HAS_MODE    0x00040
161
0
#define HAS_TYPE    0x00080
162
0
#define HAS_DEV     0x00100
163
0
#define HAS_DEVMAJOR    0x00200
164
0
#define HAS_DEVMINOR    0x00400
165
0
#define HAS_INO     0x00800
166
0
#define HAS_FFLAGS    0x01000
167
0
#define HAS_XATTR   0x02000
168
0
#define HAS_ACL     0x04000
169
0
#define HAS_CTIME   0x08000
170
0
#define HAS_MTIME   0x10000
171
0
#define HAS_ATIME   0x20000
172
173
  uint64_t     id;
174
  uint64_t     length;
175
  uint64_t     offset;
176
  uint64_t     size;
177
  enum enctype     encoding;
178
  struct chksumval   a_sum;
179
  struct chksumval   e_sum;
180
  struct archive_string  pathname;
181
  struct archive_string  symlink;
182
  time_t       ctime;
183
  time_t       mtime;
184
  time_t       atime;
185
  struct archive_string  uname;
186
  int64_t      uid;
187
  struct archive_string  gname;
188
  int64_t      gid;
189
  mode_t       mode;
190
  dev_t      dev;
191
  dev_t      devmajor;
192
  dev_t      devminor;
193
  int64_t      ino64;
194
  struct archive_string  fflags_text;
195
  unsigned int     link;
196
  unsigned int     nlink;
197
  struct archive_string  hardlink;
198
  struct xattr    *xattr_list;
199
};
200
201
struct hdlink {
202
  struct hdlink    *next;
203
204
  unsigned int     id;
205
  int      cnt;
206
  struct xar_file    *files;
207
};
208
209
struct heap_queue {
210
  struct xar_file   **files;
211
  int      allocated;
212
  int      used;
213
};
214
215
enum xmlstatus {
216
  INIT,
217
  XAR,
218
  TOC,
219
  TOC_CREATION_TIME,
220
  TOC_CHECKSUM,
221
  TOC_CHECKSUM_OFFSET,
222
  TOC_CHECKSUM_SIZE,
223
  TOC_FILE,
224
  FILE_DATA,
225
  FILE_DATA_LENGTH,
226
  FILE_DATA_OFFSET,
227
  FILE_DATA_SIZE,
228
  FILE_DATA_ENCODING,
229
  FILE_DATA_A_CHECKSUM,
230
  FILE_DATA_E_CHECKSUM,
231
  FILE_DATA_CONTENT,
232
  FILE_EA,
233
  FILE_EA_LENGTH,
234
  FILE_EA_OFFSET,
235
  FILE_EA_SIZE,
236
  FILE_EA_ENCODING,
237
  FILE_EA_A_CHECKSUM,
238
  FILE_EA_E_CHECKSUM,
239
  FILE_EA_NAME,
240
  FILE_EA_FSTYPE,
241
  FILE_CTIME,
242
  FILE_MTIME,
243
  FILE_ATIME,
244
  FILE_GROUP,
245
  FILE_GID,
246
  FILE_USER,
247
  FILE_UID,
248
  FILE_MODE,
249
  FILE_DEVICE,
250
  FILE_DEVICE_MAJOR,
251
  FILE_DEVICE_MINOR,
252
  FILE_DEVICENO,
253
  FILE_INODE,
254
  FILE_LINK,
255
  FILE_TYPE,
256
  FILE_NAME,
257
  FILE_ACL,
258
  FILE_ACL_DEFAULT,
259
  FILE_ACL_ACCESS,
260
  FILE_ACL_APPLEEXTENDED,
261
  /* BSD file flags. */
262
  FILE_FLAGS,
263
  FILE_FLAGS_USER_NODUMP,
264
  FILE_FLAGS_USER_IMMUTABLE,
265
  FILE_FLAGS_USER_APPEND,
266
  FILE_FLAGS_USER_OPAQUE,
267
  FILE_FLAGS_USER_NOUNLINK,
268
  FILE_FLAGS_SYS_ARCHIVED,
269
  FILE_FLAGS_SYS_IMMUTABLE,
270
  FILE_FLAGS_SYS_APPEND,
271
  FILE_FLAGS_SYS_NOUNLINK,
272
  FILE_FLAGS_SYS_SNAPSHOT,
273
  /* Linux file flags. */
274
  FILE_EXT2,
275
  FILE_EXT2_SecureDeletion,
276
  FILE_EXT2_Undelete,
277
  FILE_EXT2_Compress,
278
  FILE_EXT2_Synchronous,
279
  FILE_EXT2_Immutable,
280
  FILE_EXT2_AppendOnly,
281
  FILE_EXT2_NoDump,
282
  FILE_EXT2_NoAtime,
283
  FILE_EXT2_CompDirty,
284
  FILE_EXT2_CompBlock,
285
  FILE_EXT2_NoCompBlock,
286
  FILE_EXT2_CompError,
287
  FILE_EXT2_BTree,
288
  FILE_EXT2_HashIndexed,
289
  FILE_EXT2_iMagic,
290
  FILE_EXT2_Journaled,
291
  FILE_EXT2_NoTail,
292
  FILE_EXT2_DirSync,
293
  FILE_EXT2_TopDir,
294
  FILE_EXT2_Reserved,
295
  UNKNOWN,
296
};
297
298
struct unknown_tag {
299
  struct unknown_tag  *next;
300
  struct archive_string  name;
301
};
302
303
struct xar {
304
  uint64_t     offset; /* Current position in the file. */
305
  int64_t      total;
306
  uint64_t     h_base;
307
  int      end_of_file;
308
0
#define OUTBUFF_SIZE  (1024 * 64)
309
  unsigned char   *outbuff;
310
311
  enum xmlstatus     xmlsts;
312
  enum xmlstatus     xmlsts_unknown;
313
  struct unknown_tag  *unknowntags;
314
  int      base64text;
315
316
  /*
317
   * TOC
318
   */
319
  uint64_t     toc_remaining;
320
  uint64_t     toc_total;
321
  uint64_t     toc_chksum_offset;
322
  uint64_t     toc_chksum_size;
323
324
  /*
325
   * For Decoding data.
326
   */
327
  enum enctype     rd_encoding;
328
  z_stream     stream;
329
  int      stream_valid;
330
#if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
331
  bz_stream    bzstream;
332
  int      bzstream_valid;
333
#endif
334
#if HAVE_LZMA_H && HAVE_LIBLZMA
335
  lzma_stream    lzstream;
336
  int      lzstream_valid;
337
#endif
338
  /*
339
   * For Checksum data.
340
   */
341
  struct chksumwork  a_sumwrk;
342
  struct chksumwork  e_sumwrk;
343
344
  struct xar_file   *file;  /* current reading file. */
345
  struct xattr    *xattr; /* current reading extended attribute. */
346
  struct heap_queue  file_queue;
347
  struct xar_file   *hdlink_orgs;
348
  struct hdlink   *hdlink_list;
349
350
  int      entry_init;
351
  uint64_t     entry_total;
352
  uint64_t     entry_remaining;
353
  size_t       entry_unconsumed;
354
  uint64_t     entry_size;
355
  enum enctype     entry_encoding;
356
  struct chksumval   entry_a_sum;
357
  struct chksumval   entry_e_sum;
358
359
  struct archive_string_conv *sconv;
360
};
361
362
struct xmlattr {
363
  struct xmlattr  *next;
364
  char    *name;
365
  char    *value;
366
};
367
368
struct xmlattr_list {
369
  struct xmlattr  *first;
370
  struct xmlattr  **last;
371
};
372
373
static int  xar_bid(struct archive_read *, int);
374
static int  xar_read_header(struct archive_read *,
375
        struct archive_entry *);
376
static int  xar_read_data(struct archive_read *,
377
        const void **, size_t *, int64_t *);
378
static int  xar_read_data_skip(struct archive_read *);
379
static int  xar_cleanup(struct archive_read *);
380
static int  move_reading_point(struct archive_read *, uint64_t);
381
static int  rd_contents_init(struct archive_read *,
382
        enum enctype, int, int);
383
static int  rd_contents(struct archive_read *, const void **,
384
        size_t *, size_t *, uint64_t);
385
static uint64_t atol10(const char *, size_t);
386
static int64_t  atol8(const char *, size_t);
387
static size_t atohex(unsigned char *, size_t, const char *, size_t);
388
static time_t parse_time(const char *p, size_t n);
389
static int  heap_add_entry(struct archive_read *a,
390
    struct heap_queue *, struct xar_file *);
391
static struct xar_file *heap_get_entry(struct heap_queue *);
392
static int  add_link(struct archive_read *,
393
    struct xar *, struct xar_file *);
394
static void checksum_init(struct archive_read *, int, int);
395
static void checksum_update(struct archive_read *, const void *,
396
        size_t, const void *, size_t);
397
static int  checksum_final(struct archive_read *, const void *,
398
        size_t, const void *, size_t);
399
static void checksum_cleanup(struct archive_read *);
400
static int  decompression_init(struct archive_read *, enum enctype);
401
static int  decompress(struct archive_read *, const void **,
402
        size_t *, const void *, size_t *);
403
static int  decompression_cleanup(struct archive_read *);
404
static void xmlattr_cleanup(struct xmlattr_list *);
405
static int  file_new(struct archive_read *,
406
    struct xar *, struct xmlattr_list *);
407
static void file_free(struct xar_file *);
408
static int  xattr_new(struct archive_read *,
409
    struct xar *, struct xmlattr_list *);
410
static void xattr_free(struct xattr *);
411
static int  getencoding(struct xmlattr_list *);
412
static int  getsumalgorithm(struct xmlattr_list *);
413
static int  unknowntag_start(struct archive_read *,
414
    struct xar *, const char *);
415
static void unknowntag_end(struct xar *, const char *);
416
static int  xml_start(struct archive_read *,
417
    const char *, struct xmlattr_list *);
418
static void xml_end(void *, const char *);
419
static void xml_data(void *, const char *, size_t);
420
static int  xml_parse_file_flags(struct xar *, const char *);
421
static int  xml_parse_file_ext2(struct xar *, const char *);
422
#if defined(HAVE_LIBXML_XMLREADER_H)
423
static int  xml2_xmlattr_setup(struct archive_read *,
424
    struct xmlattr_list *, xmlTextReaderPtr);
425
static int  xml2_read_cb(void *, char *, int);
426
static int  xml2_close_cb(void *);
427
static void xml2_error_hdr(void *, const char *, xmlParserSeverities,
428
        xmlTextReaderLocatorPtr);
429
static int  xml2_read_toc(struct archive_read *);
430
#elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
431
struct expat_userData {
432
  int state;
433
  struct archive_read *archive;
434
};
435
static int  expat_xmlattr_setup(struct archive_read *,
436
    struct xmlattr_list *, const XML_Char **);
437
static void expat_start_cb(void *, const XML_Char *, const XML_Char **);
438
static void expat_end_cb(void *, const XML_Char *);
439
static void expat_data_cb(void *, const XML_Char *, int);
440
static int  expat_read_toc(struct archive_read *);
441
#endif
442
443
int
444
archive_read_support_format_xar(struct archive *_a)
445
238
{
446
238
  struct xar *xar;
447
238
  struct archive_read *a = (struct archive_read *)_a;
448
238
  int r;
449
450
238
  archive_check_magic(_a, ARCHIVE_READ_MAGIC,
451
238
      ARCHIVE_STATE_NEW, "archive_read_support_format_xar");
452
453
238
  xar = (struct xar *)calloc(1, sizeof(*xar));
454
238
  if (xar == NULL) {
455
0
    archive_set_error(&a->archive, ENOMEM,
456
0
        "Can't allocate xar data");
457
0
    return (ARCHIVE_FATAL);
458
0
  }
459
460
  /* initialize xar->file_queue */
461
238
  xar->file_queue.allocated = 0;
462
238
  xar->file_queue.used = 0;
463
238
  xar->file_queue.files = NULL;
464
465
238
  r = __archive_read_register_format(a,
466
238
      xar,
467
238
      "xar",
468
238
      xar_bid,
469
238
      NULL,
470
238
      xar_read_header,
471
238
      xar_read_data,
472
238
      xar_read_data_skip,
473
238
      NULL,
474
238
      xar_cleanup,
475
238
      NULL,
476
238
      NULL);
477
238
  if (r != ARCHIVE_OK)
478
0
    free(xar);
479
238
  return (r);
480
238
}
481
482
static int
483
xar_bid(struct archive_read *a, int best_bid)
484
236
{
485
236
  const unsigned char *b;
486
236
  int bid;
487
488
236
  (void)best_bid; /* UNUSED */
489
490
236
  b = __archive_read_ahead(a, HEADER_SIZE, NULL);
491
236
  if (b == NULL)
492
5
    return (-1);
493
494
231
  bid = 0;
495
  /*
496
   * Verify magic code
497
   */
498
231
  if (archive_be32dec(b) != HEADER_MAGIC)
499
230
    return (0);
500
1
  bid += 32;
501
  /*
502
   * Verify header size
503
   */
504
1
  if (archive_be16dec(b+4) != HEADER_SIZE)
505
0
    return (0);
506
1
  bid += 16;
507
  /*
508
   * Verify header version
509
   */
510
1
  if (archive_be16dec(b+6) != HEADER_VERSION)
511
0
    return (0);
512
1
  bid += 16;
513
  /*
514
   * Verify type of checksum
515
   */
516
1
  switch (archive_be32dec(b+24)) {
517
0
  case CKSUM_NONE:
518
0
  case CKSUM_SHA1:
519
1
  case CKSUM_MD5:
520
1
    bid += 32;
521
1
    break;
522
0
  default:
523
0
    return (0);
524
1
  }
525
526
1
  return (bid);
527
1
}
528
529
static int
530
read_toc(struct archive_read *a)
531
1
{
532
1
  struct xar *xar;
533
1
  struct xar_file *file;
534
1
  const unsigned char *b;
535
1
  uint64_t toc_compressed_size;
536
1
  uint64_t toc_uncompressed_size;
537
1
  uint32_t toc_chksum_alg;
538
1
  ssize_t bytes;
539
1
  int r;
540
541
1
  xar = (struct xar *)(a->format->data);
542
543
  /*
544
   * Read xar header.
545
   */
546
1
  b = __archive_read_ahead(a, HEADER_SIZE, &bytes);
547
1
  if (bytes < 0)
548
0
    return ((int)bytes);
549
1
  if (bytes < HEADER_SIZE) {
550
0
    archive_set_error(&a->archive,
551
0
        ARCHIVE_ERRNO_FILE_FORMAT,
552
0
        "Truncated archive header");
553
0
    return (ARCHIVE_FATAL);
554
0
  }
555
556
1
  if (archive_be32dec(b) != HEADER_MAGIC) {
557
0
    archive_set_error(&a->archive,
558
0
        ARCHIVE_ERRNO_FILE_FORMAT,
559
0
        "Invalid header magic");
560
0
    return (ARCHIVE_FATAL);
561
0
  }
562
1
  if (archive_be16dec(b+6) != HEADER_VERSION) {
563
0
    archive_set_error(&a->archive,
564
0
        ARCHIVE_ERRNO_FILE_FORMAT,
565
0
        "Unsupported header version(%d)",
566
0
        archive_be16dec(b+6));
567
0
    return (ARCHIVE_FATAL);
568
0
  }
569
1
  toc_compressed_size = archive_be64dec(b+8);
570
1
  xar->toc_remaining = toc_compressed_size;
571
1
  toc_uncompressed_size = archive_be64dec(b+16);
572
1
  toc_chksum_alg = archive_be32dec(b+24);
573
1
  __archive_read_consume(a, HEADER_SIZE);
574
1
  xar->offset += HEADER_SIZE;
575
1
  xar->toc_total = 0;
576
577
  /*
578
   * Read TOC(Table of Contents).
579
   */
580
  /* Initialize reading contents. */
581
1
  r = move_reading_point(a, HEADER_SIZE);
582
1
  if (r != ARCHIVE_OK)
583
0
    return (r);
584
1
  r = rd_contents_init(a, GZIP, toc_chksum_alg, CKSUM_NONE);
585
1
  if (r != ARCHIVE_OK)
586
0
    return (r);
587
588
1
#ifdef HAVE_LIBXML_XMLREADER_H
589
1
  r = xml2_read_toc(a);
590
#elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
591
  r = expat_read_toc(a);
592
#endif
593
1
  if (r != ARCHIVE_OK)
594
1
    return (r);
595
596
  /* Set 'The HEAP' base. */
597
0
  xar->h_base = xar->offset;
598
0
  if (xar->toc_total != toc_uncompressed_size) {
599
0
    archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
600
0
        "TOC uncompressed size error");
601
0
    return (ARCHIVE_FATAL);
602
0
  }
603
604
  /*
605
   * Checksum TOC
606
   */
607
0
  if (toc_chksum_alg != CKSUM_NONE) {
608
0
    r = move_reading_point(a, xar->toc_chksum_offset);
609
0
    if (r != ARCHIVE_OK)
610
0
      return (r);
611
0
    b = __archive_read_ahead(a,
612
0
      (size_t)xar->toc_chksum_size, &bytes);
613
0
    if (bytes < 0)
614
0
      return ((int)bytes);
615
0
    if ((uint64_t)bytes < xar->toc_chksum_size) {
616
0
      archive_set_error(&a->archive,
617
0
          ARCHIVE_ERRNO_FILE_FORMAT,
618
0
          "Truncated archive file");
619
0
      return (ARCHIVE_FATAL);
620
0
    }
621
0
    r = checksum_final(a, b,
622
0
      (size_t)xar->toc_chksum_size, NULL, 0);
623
0
    __archive_read_consume(a, xar->toc_chksum_size);
624
0
    xar->offset += xar->toc_chksum_size;
625
#ifndef DONT_FAIL_ON_CRC_ERROR
626
    if (r != ARCHIVE_OK)
627
      return (ARCHIVE_FATAL);
628
#endif
629
0
  }
630
631
  /*
632
   * Connect hardlinked files.
633
   */
634
0
  for (file = xar->hdlink_orgs; file != NULL; file = file->hdnext) {
635
0
    struct hdlink **hdlink;
636
637
0
    for (hdlink = &(xar->hdlink_list); *hdlink != NULL;
638
0
        hdlink = &((*hdlink)->next)) {
639
0
      if ((*hdlink)->id == file->id) {
640
0
        struct hdlink *hltmp;
641
0
        struct xar_file *f2;
642
0
        int nlink = (*hdlink)->cnt + 1;
643
644
0
        file->nlink = nlink;
645
0
        for (f2 = (*hdlink)->files; f2 != NULL;
646
0
            f2 = f2->hdnext) {
647
0
          f2->nlink = nlink;
648
0
          archive_string_copy(
649
0
              &(f2->hardlink), &(file->pathname));
650
0
        }
651
        /* Remove resolved files from hdlist_list. */
652
0
        hltmp = *hdlink;
653
0
        *hdlink = hltmp->next;
654
0
        free(hltmp);
655
0
        break;
656
0
      }
657
0
    }
658
0
  }
659
0
  a->archive.archive_format = ARCHIVE_FORMAT_XAR;
660
0
  a->archive.archive_format_name = "xar";
661
662
0
  return (ARCHIVE_OK);
663
0
}
664
665
static int
666
xar_read_header(struct archive_read *a, struct archive_entry *entry)
667
1
{
668
1
  struct xar *xar;
669
1
  struct xar_file *file;
670
1
  struct xattr *xattr;
671
1
  int r;
672
673
1
  xar = (struct xar *)(a->format->data);
674
1
  r = ARCHIVE_OK;
675
676
1
  if (xar->offset == 0) {
677
    /* Create a character conversion object. */
678
1
    if (xar->sconv == NULL) {
679
1
      xar->sconv = archive_string_conversion_from_charset(
680
1
          &(a->archive), "UTF-8", 1);
681
1
      if (xar->sconv == NULL)
682
0
        return (ARCHIVE_FATAL);
683
1
    }
684
685
    /* Read TOC. */
686
1
    r = read_toc(a);
687
1
    if (r != ARCHIVE_OK)
688
1
      return (r);
689
1
  }
690
691
0
  for (;;) {
692
0
    file = xar->file = heap_get_entry(&(xar->file_queue));
693
0
    if (file == NULL) {
694
0
      xar->end_of_file = 1;
695
0
      return (ARCHIVE_EOF);
696
0
    }
697
0
    if ((file->mode & AE_IFMT) != AE_IFDIR)
698
0
      break;
699
0
    if (file->has != (HAS_PATHNAME | HAS_TYPE))
700
0
      break;
701
    /*
702
     * If a file type is a directory and it does not have
703
     * any metadata, do not export.
704
     */
705
0
    file_free(file);
706
0
  }
707
0
        if (file->has & HAS_ATIME) {
708
0
          archive_entry_set_atime(entry, file->atime, 0);
709
0
        }
710
0
        if (file->has & HAS_CTIME) {
711
0
          archive_entry_set_ctime(entry, file->ctime, 0);
712
0
        }
713
0
        if (file->has & HAS_MTIME) {
714
0
          archive_entry_set_mtime(entry, file->mtime, 0);
715
0
        }
716
0
  archive_entry_set_gid(entry, file->gid);
717
0
  if (file->gname.length > 0 &&
718
0
      archive_entry_copy_gname_l(entry, file->gname.s,
719
0
    archive_strlen(&(file->gname)), xar->sconv) != 0) {
720
0
    if (errno == ENOMEM) {
721
0
      archive_set_error(&a->archive, ENOMEM,
722
0
          "Can't allocate memory for Gname");
723
0
      return (ARCHIVE_FATAL);
724
0
    }
725
0
    archive_set_error(&a->archive,
726
0
        ARCHIVE_ERRNO_FILE_FORMAT,
727
0
        "Gname cannot be converted from %s to current locale.",
728
0
        archive_string_conversion_charset_name(xar->sconv));
729
0
    r = ARCHIVE_WARN;
730
0
  }
731
0
  archive_entry_set_uid(entry, file->uid);
732
0
  if (file->uname.length > 0 &&
733
0
      archive_entry_copy_uname_l(entry, file->uname.s,
734
0
    archive_strlen(&(file->uname)), xar->sconv) != 0) {
735
0
    if (errno == ENOMEM) {
736
0
      archive_set_error(&a->archive, ENOMEM,
737
0
          "Can't allocate memory for Uname");
738
0
      return (ARCHIVE_FATAL);
739
0
    }
740
0
    archive_set_error(&a->archive,
741
0
        ARCHIVE_ERRNO_FILE_FORMAT,
742
0
        "Uname cannot be converted from %s to current locale.",
743
0
        archive_string_conversion_charset_name(xar->sconv));
744
0
    r = ARCHIVE_WARN;
745
0
  }
746
0
  archive_entry_set_mode(entry, file->mode);
747
0
  if (archive_entry_copy_pathname_l(entry, file->pathname.s,
748
0
      archive_strlen(&(file->pathname)), xar->sconv) != 0) {
749
0
    if (errno == ENOMEM) {
750
0
      archive_set_error(&a->archive, ENOMEM,
751
0
          "Can't allocate memory for Pathname");
752
0
      return (ARCHIVE_FATAL);
753
0
    }
754
0
    archive_set_error(&a->archive,
755
0
        ARCHIVE_ERRNO_FILE_FORMAT,
756
0
        "Pathname cannot be converted from %s to current locale.",
757
0
        archive_string_conversion_charset_name(xar->sconv));
758
0
    r = ARCHIVE_WARN;
759
0
  }
760
761
762
0
  if (file->symlink.length > 0 &&
763
0
      archive_entry_copy_symlink_l(entry, file->symlink.s,
764
0
    archive_strlen(&(file->symlink)), xar->sconv) != 0) {
765
0
    if (errno == ENOMEM) {
766
0
      archive_set_error(&a->archive, ENOMEM,
767
0
          "Can't allocate memory for Linkname");
768
0
      return (ARCHIVE_FATAL);
769
0
    }
770
0
    archive_set_error(&a->archive,
771
0
        ARCHIVE_ERRNO_FILE_FORMAT,
772
0
        "Linkname cannot be converted from %s to current locale.",
773
0
        archive_string_conversion_charset_name(xar->sconv));
774
0
    r = ARCHIVE_WARN;
775
0
  }
776
  /* Set proper nlink. */
777
0
  if ((file->mode & AE_IFMT) == AE_IFDIR)
778
0
    archive_entry_set_nlink(entry, file->subdirs + 2);
779
0
  else
780
0
    archive_entry_set_nlink(entry, file->nlink);
781
0
  archive_entry_set_size(entry, file->size);
782
0
  if (archive_strlen(&(file->hardlink)) > 0)
783
0
    archive_entry_set_hardlink(entry, file->hardlink.s);
784
0
  archive_entry_set_ino64(entry, file->ino64);
785
0
  if (file->has & HAS_DEV)
786
0
    archive_entry_set_dev(entry, file->dev);
787
0
  if (file->has & HAS_DEVMAJOR)
788
0
    archive_entry_set_devmajor(entry, file->devmajor);
789
0
  if (file->has & HAS_DEVMINOR)
790
0
    archive_entry_set_devminor(entry, file->devminor);
791
0
  if (archive_strlen(&(file->fflags_text)) > 0)
792
0
    archive_entry_copy_fflags_text(entry, file->fflags_text.s);
793
794
0
  xar->entry_init = 1;
795
0
  xar->entry_total = 0;
796
0
  xar->entry_remaining = file->length;
797
0
  xar->entry_size = file->size;
798
0
  xar->entry_encoding = file->encoding;
799
0
  xar->entry_a_sum = file->a_sum;
800
0
  xar->entry_e_sum = file->e_sum;
801
  /*
802
   * Read extended attributes.
803
   */
804
0
  xattr = file->xattr_list;
805
0
  while (xattr != NULL) {
806
0
    const void *d;
807
0
    size_t outbytes = 0;
808
0
    size_t used = 0;
809
810
0
    r = move_reading_point(a, xattr->offset);
811
0
    if (r != ARCHIVE_OK)
812
0
      break;
813
0
    r = rd_contents_init(a, xattr->encoding,
814
0
        xattr->a_sum.alg, xattr->e_sum.alg);
815
0
    if (r != ARCHIVE_OK)
816
0
      break;
817
0
    d = NULL;
818
0
    r = rd_contents(a, &d, &outbytes, &used, xattr->length);
819
0
    if (r != ARCHIVE_OK)
820
0
      break;
821
0
    if (outbytes != xattr->size) {
822
0
      archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
823
0
          "Decompressed size error");
824
0
      r = ARCHIVE_FATAL;
825
0
      break;
826
0
    }
827
0
    r = checksum_final(a,
828
0
        xattr->a_sum.val, xattr->a_sum.len,
829
0
        xattr->e_sum.val, xattr->e_sum.len);
830
0
    if (r != ARCHIVE_OK) {
831
#ifndef DONT_FAIL_ON_CRC_ERROR
832
      archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
833
          "Xattr checksum error");
834
      r = ARCHIVE_WARN;
835
      break;
836
#endif
837
0
    }
838
0
    if (xattr->name.s == NULL) {
839
0
      archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
840
0
          "Xattr name error");
841
0
      r = ARCHIVE_WARN;
842
0
      break;
843
0
    }
844
0
    archive_entry_xattr_add_entry(entry,
845
0
        xattr->name.s, d, outbytes);
846
0
    xattr = xattr->next;
847
0
  }
848
0
  if (r != ARCHIVE_OK) {
849
0
    file_free(file);
850
0
    return (r);
851
0
  }
852
853
0
  if (xar->entry_remaining > 0)
854
    /* Move reading point to the beginning of current
855
     * file contents. */
856
0
    r = move_reading_point(a, file->offset);
857
0
  else
858
0
    r = ARCHIVE_OK;
859
860
0
  file_free(file);
861
0
  return (r);
862
0
}
863
864
static int
865
xar_read_data(struct archive_read *a,
866
    const void **buff, size_t *size, int64_t *offset)
867
0
{
868
0
  struct xar *xar;
869
0
  size_t used = 0;
870
0
  int r;
871
872
0
  xar = (struct xar *)(a->format->data);
873
874
0
  if (xar->entry_unconsumed) {
875
0
    __archive_read_consume(a, xar->entry_unconsumed);
876
0
    xar->entry_unconsumed = 0;
877
0
  }
878
879
0
  if (xar->end_of_file || xar->entry_remaining <= 0) {
880
0
    r = ARCHIVE_EOF;
881
0
    goto abort_read_data;
882
0
  }
883
884
0
  if (xar->entry_init) {
885
0
    r = rd_contents_init(a, xar->entry_encoding,
886
0
        xar->entry_a_sum.alg, xar->entry_e_sum.alg);
887
0
    if (r != ARCHIVE_OK) {
888
0
      xar->entry_remaining = 0;
889
0
      return (r);
890
0
    }
891
0
    xar->entry_init = 0;
892
0
  }
893
894
0
  *buff = NULL;
895
0
  r = rd_contents(a, buff, size, &used, xar->entry_remaining);
896
0
  if (r != ARCHIVE_OK)
897
0
    goto abort_read_data;
898
899
0
  *offset = xar->entry_total;
900
0
  xar->entry_total += *size;
901
0
  xar->total += *size;
902
0
  xar->offset += used;
903
0
  xar->entry_remaining -= used;
904
0
  xar->entry_unconsumed = used;
905
906
0
  if (xar->entry_remaining == 0) {
907
0
    if (xar->entry_total != xar->entry_size) {
908
0
      archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
909
0
          "Decompressed size error");
910
0
      r = ARCHIVE_FATAL;
911
0
      goto abort_read_data;
912
0
    }
913
0
    r = checksum_final(a,
914
0
        xar->entry_a_sum.val, xar->entry_a_sum.len,
915
0
        xar->entry_e_sum.val, xar->entry_e_sum.len);
916
0
    if (r != ARCHIVE_OK)
917
0
      goto abort_read_data;
918
0
  }
919
920
0
  return (ARCHIVE_OK);
921
0
abort_read_data:
922
0
  *buff = NULL;
923
0
  *size = 0;
924
0
  *offset = xar->total;
925
0
  return (r);
926
0
}
927
928
static int
929
xar_read_data_skip(struct archive_read *a)
930
0
{
931
0
  struct xar *xar;
932
0
  int64_t bytes_skipped;
933
934
0
  xar = (struct xar *)(a->format->data);
935
0
  if (xar->end_of_file)
936
0
    return (ARCHIVE_EOF);
937
0
  bytes_skipped = __archive_read_consume(a, xar->entry_remaining +
938
0
    xar->entry_unconsumed);
939
0
  if (bytes_skipped < 0)
940
0
    return (ARCHIVE_FATAL);
941
0
  xar->offset += bytes_skipped;
942
0
  xar->entry_unconsumed = 0;
943
0
  return (ARCHIVE_OK);
944
0
}
945
946
static int
947
xar_cleanup(struct archive_read *a)
948
238
{
949
238
  struct xar *xar;
950
238
  struct hdlink *hdlink;
951
238
  int i;
952
238
  int r;
953
954
238
  xar = (struct xar *)(a->format->data);
955
238
  checksum_cleanup(a);
956
238
  r = decompression_cleanup(a);
957
238
  hdlink = xar->hdlink_list;
958
238
  while (hdlink != NULL) {
959
0
    struct hdlink *next = hdlink->next;
960
961
0
    free(hdlink);
962
0
    hdlink = next;
963
0
  }
964
238
  for (i = 0; i < xar->file_queue.used; i++)
965
0
    file_free(xar->file_queue.files[i]);
966
238
  free(xar->file_queue.files);
967
238
  while (xar->unknowntags != NULL) {
968
0
    struct unknown_tag *tag;
969
970
0
    tag = xar->unknowntags;
971
0
    xar->unknowntags = tag->next;
972
0
    archive_string_free(&(tag->name));
973
0
    free(tag);
974
0
  }
975
238
  free(xar->outbuff);
976
238
  free(xar);
977
238
  a->format->data = NULL;
978
238
  return (r);
979
238
}
980
981
static int
982
move_reading_point(struct archive_read *a, uint64_t offset)
983
1
{
984
1
  struct xar *xar;
985
986
1
  xar = (struct xar *)(a->format->data);
987
1
  if (xar->offset - xar->h_base != offset) {
988
    /* Seek forward to the start of file contents. */
989
0
    int64_t step;
990
991
0
    step = offset - (xar->offset - xar->h_base);
992
0
    if (step > 0) {
993
0
      step = __archive_read_consume(a, step);
994
0
      if (step < 0)
995
0
        return ((int)step);
996
0
      xar->offset += step;
997
0
    } else {
998
0
      int64_t pos = __archive_read_seek(a, xar->h_base + offset, SEEK_SET);
999
0
      if (pos == ARCHIVE_FAILED) {
1000
0
        archive_set_error(&(a->archive),
1001
0
            ARCHIVE_ERRNO_MISC,
1002
0
            "Cannot seek.");
1003
0
        return (ARCHIVE_FAILED);
1004
0
      }
1005
0
      xar->offset = pos;
1006
0
    }
1007
0
  }
1008
1
  return (ARCHIVE_OK);
1009
1
}
1010
1011
static int
1012
rd_contents_init(struct archive_read *a, enum enctype encoding,
1013
    int a_sum_alg, int e_sum_alg)
1014
1
{
1015
1
  int r;
1016
1017
  /* Init decompress library. */
1018
1
  if ((r = decompression_init(a, encoding)) != ARCHIVE_OK)
1019
0
    return (r);
1020
  /* Init checksum library. */
1021
1
  checksum_init(a, a_sum_alg, e_sum_alg);
1022
1
  return (ARCHIVE_OK);
1023
1
}
1024
1025
static int
1026
rd_contents(struct archive_read *a, const void **buff, size_t *size,
1027
    size_t *used, uint64_t remaining)
1028
695
{
1029
695
  const unsigned char *b;
1030
695
  ssize_t bytes;
1031
1032
  /* Get whatever bytes are immediately available. */
1033
695
  b = __archive_read_ahead(a, 1, &bytes);
1034
695
  if (bytes < 0)
1035
0
    return ((int)bytes);
1036
695
  if (bytes == 0) {
1037
1
    archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1038
1
        "Truncated archive file");
1039
1
    return (ARCHIVE_FATAL);
1040
1
  }
1041
694
  if ((uint64_t)bytes > remaining)
1042
0
    bytes = (ssize_t)remaining;
1043
1044
  /*
1045
   * Decompress contents of file.
1046
   */
1047
694
  *used = bytes;
1048
694
  if (decompress(a, buff, size, b, used) != ARCHIVE_OK)
1049
0
    return (ARCHIVE_FATAL);
1050
1051
  /*
1052
   * Update checksum of a compressed data and a extracted data.
1053
   */
1054
694
  checksum_update(a, b, *used, *buff, *size);
1055
1056
694
  return (ARCHIVE_OK);
1057
694
}
1058
1059
/*
1060
 * Note that this implementation does not (and should not!) obey
1061
 * locale settings; you cannot simply substitute strtol here, since
1062
 * it does obey locale.
1063
 */
1064
1065
static uint64_t
1066
atol10(const char *p, size_t char_cnt)
1067
0
{
1068
0
  uint64_t l;
1069
0
  int digit;
1070
1071
0
  if (char_cnt == 0)
1072
0
    return (0);
1073
1074
0
  l = 0;
1075
0
  digit = *p - '0';
1076
0
  while (digit >= 0 && digit < 10  && char_cnt-- > 0) {
1077
0
    l = (l * 10) + digit;
1078
0
    digit = *++p - '0';
1079
0
  }
1080
0
  return (l);
1081
0
}
1082
1083
static int64_t
1084
atol8(const char *p, size_t char_cnt)
1085
0
{
1086
0
  int64_t l;
1087
0
  int digit;
1088
1089
0
  if (char_cnt == 0)
1090
0
    return (0);
1091
1092
0
  l = 0;
1093
0
  while (char_cnt-- > 0) {
1094
0
    if (*p >= '0' && *p <= '7')
1095
0
      digit = *p - '0';
1096
0
    else
1097
0
      break;
1098
0
    p++;
1099
0
    l <<= 3;
1100
0
    l |= digit;
1101
0
  }
1102
0
  return (l);
1103
0
}
1104
1105
static size_t
1106
atohex(unsigned char *b, size_t bsize, const char *p, size_t psize)
1107
0
{
1108
0
  size_t fbsize = bsize;
1109
1110
0
  while (bsize && psize > 1) {
1111
0
    unsigned char x;
1112
1113
0
    if (p[0] >= 'a' && p[0] <= 'z')
1114
0
      x = (p[0] - 'a' + 0x0a) << 4;
1115
0
    else if (p[0] >= 'A' && p[0] <= 'Z')
1116
0
      x = (p[0] - 'A' + 0x0a) << 4;
1117
0
    else if (p[0] >= '0' && p[0] <= '9')
1118
0
      x = (p[0] - '0') << 4;
1119
0
    else
1120
0
      return (-1);
1121
0
    if (p[1] >= 'a' && p[1] <= 'z')
1122
0
      x |= p[1] - 'a' + 0x0a;
1123
0
    else if (p[1] >= 'A' && p[1] <= 'Z')
1124
0
      x |= p[1] - 'A' + 0x0a;
1125
0
    else if (p[1] >= '0' && p[1] <= '9')
1126
0
      x |= p[1] - '0';
1127
0
    else
1128
0
      return (-1);
1129
1130
0
    *b++ = x;
1131
0
    bsize--;
1132
0
    p += 2;
1133
0
    psize -= 2;
1134
0
  }
1135
0
  return (fbsize - bsize);
1136
0
}
1137
1138
static time_t
1139
time_from_tm(struct tm *t)
1140
0
{
1141
#if HAVE__MKGMTIME
1142
        return _mkgmtime(t);
1143
#elif HAVE_TIMEGM
1144
        /* Use platform timegm() if available. */
1145
0
        return (timegm(t));
1146
#else
1147
        /* Else use direct calculation using POSIX assumptions. */
1148
        /* First, fix up tm_yday based on the year/month/day. */
1149
        mktime(t);
1150
        /* Then we can compute timegm() from first principles. */
1151
        return (t->tm_sec
1152
            + t->tm_min * 60
1153
            + t->tm_hour * 3600
1154
            + t->tm_yday * 86400
1155
            + (t->tm_year - 70) * 31536000
1156
            + ((t->tm_year - 69) / 4) * 86400
1157
            - ((t->tm_year - 1) / 100) * 86400
1158
            + ((t->tm_year + 299) / 400) * 86400);
1159
#endif
1160
0
}
1161
1162
static time_t
1163
parse_time(const char *p, size_t n)
1164
0
{
1165
0
  struct tm tm;
1166
0
  time_t t = 0;
1167
0
  int64_t data;
1168
1169
0
  memset(&tm, 0, sizeof(tm));
1170
0
  if (n != 20)
1171
0
    return (t);
1172
0
  data = atol10(p, 4);
1173
0
  if (data < 1900)
1174
0
    return (t);
1175
0
  tm.tm_year = (int)data - 1900;
1176
0
  p += 4;
1177
0
  if (*p++ != '-')
1178
0
    return (t);
1179
0
  data = atol10(p, 2);
1180
0
  if (data < 1 || data > 12)
1181
0
    return (t);
1182
0
  tm.tm_mon = (int)data -1;
1183
0
  p += 2;
1184
0
  if (*p++ != '-')
1185
0
    return (t);
1186
0
  data = atol10(p, 2);
1187
0
  if (data < 1 || data > 31)
1188
0
    return (t);
1189
0
  tm.tm_mday = (int)data;
1190
0
  p += 2;
1191
0
  if (*p++ != 'T')
1192
0
    return (t);
1193
0
  data = atol10(p, 2);
1194
0
  if (data < 0 || data > 23)
1195
0
    return (t);
1196
0
  tm.tm_hour = (int)data;
1197
0
  p += 2;
1198
0
  if (*p++ != ':')
1199
0
    return (t);
1200
0
  data = atol10(p, 2);
1201
0
  if (data < 0 || data > 59)
1202
0
    return (t);
1203
0
  tm.tm_min = (int)data;
1204
0
  p += 2;
1205
0
  if (*p++ != ':')
1206
0
    return (t);
1207
0
  data = atol10(p, 2);
1208
0
  if (data < 0 || data > 60)
1209
0
    return (t);
1210
0
  tm.tm_sec = (int)data;
1211
#if 0
1212
  p += 2;
1213
  if (*p != 'Z')
1214
    return (t);
1215
#endif
1216
1217
0
  t = time_from_tm(&tm);
1218
1219
0
  return (t);
1220
0
}
1221
1222
static int
1223
heap_add_entry(struct archive_read *a,
1224
    struct heap_queue *heap, struct xar_file *file)
1225
0
{
1226
0
  uint64_t file_id, parent_id;
1227
0
  int hole, parent;
1228
1229
  /* Expand our pending files list as necessary. */
1230
0
  if (heap->used >= heap->allocated) {
1231
0
    struct xar_file **new_pending_files;
1232
0
    int new_size;
1233
1234
0
    if (heap->allocated < 1024)
1235
0
      new_size = 1024;
1236
0
    else
1237
0
      new_size = heap->allocated * 2;
1238
    /* Overflow might keep us from growing the list. */
1239
0
    if (new_size <= heap->allocated) {
1240
0
      archive_set_error(&a->archive,
1241
0
          ENOMEM, "Out of memory");
1242
0
      return (ARCHIVE_FATAL);
1243
0
    }
1244
0
    new_pending_files = (struct xar_file **)
1245
0
        calloc(new_size, sizeof(new_pending_files[0]));
1246
0
    if (new_pending_files == NULL) {
1247
0
      archive_set_error(&a->archive,
1248
0
          ENOMEM, "Out of memory");
1249
0
      return (ARCHIVE_FATAL);
1250
0
    }
1251
0
    if (heap->allocated) {
1252
0
      memcpy(new_pending_files, heap->files,
1253
0
          heap->allocated * sizeof(new_pending_files[0]));
1254
0
      free(heap->files);
1255
0
    }
1256
0
    heap->files = new_pending_files;
1257
0
    heap->allocated = new_size;
1258
0
  }
1259
1260
0
  file_id = file->id;
1261
1262
  /*
1263
   * Start with hole at end, walk it up tree to find insertion point.
1264
   */
1265
0
  hole = heap->used++;
1266
0
  while (hole > 0) {
1267
0
    parent = (hole - 1)/2;
1268
0
    parent_id = heap->files[parent]->id;
1269
0
    if (file_id >= parent_id) {
1270
0
      heap->files[hole] = file;
1271
0
      return (ARCHIVE_OK);
1272
0
    }
1273
    /* Move parent into hole <==> move hole up tree. */
1274
0
    heap->files[hole] = heap->files[parent];
1275
0
    hole = parent;
1276
0
  }
1277
0
  heap->files[0] = file;
1278
1279
0
  return (ARCHIVE_OK);
1280
0
}
1281
1282
static struct xar_file *
1283
heap_get_entry(struct heap_queue *heap)
1284
0
{
1285
0
  uint64_t a_id, b_id, c_id;
1286
0
  int a, b, c;
1287
0
  struct xar_file *r, *tmp;
1288
1289
0
  if (heap->used < 1)
1290
0
    return (NULL);
1291
1292
  /*
1293
   * The first file in the list is the earliest; we'll return this.
1294
   */
1295
0
  r = heap->files[0];
1296
1297
  /*
1298
   * Move the last item in the heap to the root of the tree
1299
   */
1300
0
  heap->files[0] = heap->files[--(heap->used)];
1301
1302
  /*
1303
   * Rebalance the heap.
1304
   */
1305
0
  a = 0; /* Starting element and its heap key */
1306
0
  a_id = heap->files[a]->id;
1307
0
  for (;;) {
1308
0
    b = a + a + 1; /* First child */
1309
0
    if (b >= heap->used)
1310
0
      return (r);
1311
0
    b_id = heap->files[b]->id;
1312
0
    c = b + 1; /* Use second child if it is smaller. */
1313
0
    if (c < heap->used) {
1314
0
      c_id = heap->files[c]->id;
1315
0
      if (c_id < b_id) {
1316
0
        b = c;
1317
0
        b_id = c_id;
1318
0
      }
1319
0
    }
1320
0
    if (a_id <= b_id)
1321
0
      return (r);
1322
0
    tmp = heap->files[a];
1323
0
    heap->files[a] = heap->files[b];
1324
0
    heap->files[b] = tmp;
1325
0
    a = b;
1326
0
  }
1327
0
}
1328
1329
static int
1330
add_link(struct archive_read *a, struct xar *xar, struct xar_file *file)
1331
0
{
1332
0
  struct hdlink *hdlink;
1333
1334
0
  for (hdlink = xar->hdlink_list; hdlink != NULL; hdlink = hdlink->next) {
1335
0
    if (hdlink->id == file->link) {
1336
0
      file->hdnext = hdlink->files;
1337
0
      hdlink->cnt++;
1338
0
      hdlink->files = file;
1339
0
      return (ARCHIVE_OK);
1340
0
    }
1341
0
  }
1342
0
  hdlink = malloc(sizeof(*hdlink));
1343
0
  if (hdlink == NULL) {
1344
0
    archive_set_error(&a->archive, ENOMEM, "Out of memory");
1345
0
    return (ARCHIVE_FATAL);
1346
0
  }
1347
0
  file->hdnext = NULL;
1348
0
  hdlink->id = file->link;
1349
0
  hdlink->cnt = 1;
1350
0
  hdlink->files = file;
1351
0
  hdlink->next = xar->hdlink_list;
1352
0
  xar->hdlink_list = hdlink;
1353
0
  return (ARCHIVE_OK);
1354
0
}
1355
1356
static void
1357
_checksum_init(struct chksumwork *sumwrk, int sum_alg)
1358
2
{
1359
2
  sumwrk->alg = sum_alg;
1360
2
  switch (sum_alg) {
1361
1
  case CKSUM_NONE:
1362
1
    break;
1363
0
  case CKSUM_SHA1:
1364
0
    archive_sha1_init(&(sumwrk->sha1ctx));
1365
0
    break;
1366
1
  case CKSUM_MD5:
1367
1
    archive_md5_init(&(sumwrk->md5ctx));
1368
1
    break;
1369
2
  }
1370
2
}
1371
1372
static void
1373
_checksum_update(struct chksumwork *sumwrk, const void *buff, size_t size)
1374
1.38k
{
1375
1376
1.38k
  switch (sumwrk->alg) {
1377
694
  case CKSUM_NONE:
1378
694
    break;
1379
0
  case CKSUM_SHA1:
1380
0
    archive_sha1_update(&(sumwrk->sha1ctx), buff, size);
1381
0
    break;
1382
694
  case CKSUM_MD5:
1383
694
    archive_md5_update(&(sumwrk->md5ctx), buff, size);
1384
694
    break;
1385
1.38k
  }
1386
1.38k
}
1387
1388
static int
1389
_checksum_final(struct chksumwork *sumwrk, const void *val, size_t len)
1390
476
{
1391
476
  unsigned char sum[MAX_SUM_SIZE];
1392
476
  int r = ARCHIVE_OK;
1393
1394
476
  switch (sumwrk->alg) {
1395
475
  case CKSUM_NONE:
1396
475
    break;
1397
0
  case CKSUM_SHA1:
1398
0
    archive_sha1_final(&(sumwrk->sha1ctx), sum);
1399
0
    if (len != SHA1_SIZE ||
1400
0
        memcmp(val, sum, SHA1_SIZE) != 0)
1401
0
      r = ARCHIVE_FAILED;
1402
0
    break;
1403
1
  case CKSUM_MD5:
1404
1
    archive_md5_final(&(sumwrk->md5ctx), sum);
1405
1
    if (len != MD5_SIZE ||
1406
1
        memcmp(val, sum, MD5_SIZE) != 0)
1407
1
      r = ARCHIVE_FAILED;
1408
1
    break;
1409
476
  }
1410
476
  return (r);
1411
476
}
1412
1413
static void
1414
checksum_init(struct archive_read *a, int a_sum_alg, int e_sum_alg)
1415
1
{
1416
1
  struct xar *xar;
1417
1418
1
  xar = (struct xar *)(a->format->data);
1419
1
  _checksum_init(&(xar->a_sumwrk), a_sum_alg);
1420
1
  _checksum_init(&(xar->e_sumwrk), e_sum_alg);
1421
1
}
1422
1423
static void
1424
checksum_update(struct archive_read *a, const void *abuff, size_t asize,
1425
    const void *ebuff, size_t esize)
1426
694
{
1427
694
  struct xar *xar;
1428
1429
694
  xar = (struct xar *)(a->format->data);
1430
694
  _checksum_update(&(xar->a_sumwrk), abuff, asize);
1431
694
  _checksum_update(&(xar->e_sumwrk), ebuff, esize);
1432
694
}
1433
1434
static int
1435
checksum_final(struct archive_read *a, const void *a_sum_val,
1436
    size_t a_sum_len, const void *e_sum_val, size_t e_sum_len)
1437
0
{
1438
0
  struct xar *xar;
1439
0
  int r;
1440
1441
0
  xar = (struct xar *)(a->format->data);
1442
0
  r = _checksum_final(&(xar->a_sumwrk), a_sum_val, a_sum_len);
1443
0
  if (r == ARCHIVE_OK)
1444
0
    r = _checksum_final(&(xar->e_sumwrk), e_sum_val, e_sum_len);
1445
0
  if (r != ARCHIVE_OK)
1446
0
    archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
1447
0
        "Sumcheck error");
1448
0
  return (r);
1449
0
}
1450
1451
static int
1452
decompression_init(struct archive_read *a, enum enctype encoding)
1453
1
{
1454
1
  struct xar *xar;
1455
1
  const char *detail;
1456
1
  int r;
1457
1458
1
  xar = (struct xar *)(a->format->data);
1459
1
  xar->rd_encoding = encoding;
1460
1
  switch (encoding) {
1461
0
  case NONE:
1462
0
    break;
1463
1
  case GZIP:
1464
1
    if (xar->stream_valid)
1465
0
      r = inflateReset(&(xar->stream));
1466
1
    else
1467
1
      r = inflateInit(&(xar->stream));
1468
1
    if (r != Z_OK) {
1469
0
      archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1470
0
          "Couldn't initialize zlib stream.");
1471
0
      return (ARCHIVE_FATAL);
1472
0
    }
1473
1
    xar->stream_valid = 1;
1474
1
    xar->stream.total_in = 0;
1475
1
    xar->stream.total_out = 0;
1476
1
    break;
1477
0
#if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1478
0
  case BZIP2:
1479
0
    if (xar->bzstream_valid) {
1480
0
      BZ2_bzDecompressEnd(&(xar->bzstream));
1481
0
      xar->bzstream_valid = 0;
1482
0
    }
1483
0
    r = BZ2_bzDecompressInit(&(xar->bzstream), 0, 0);
1484
0
    if (r == BZ_MEM_ERROR)
1485
0
      r = BZ2_bzDecompressInit(&(xar->bzstream), 0, 1);
1486
0
    if (r != BZ_OK) {
1487
0
      int err = ARCHIVE_ERRNO_MISC;
1488
0
      detail = NULL;
1489
0
      switch (r) {
1490
0
      case BZ_PARAM_ERROR:
1491
0
        detail = "invalid setup parameter";
1492
0
        break;
1493
0
      case BZ_MEM_ERROR:
1494
0
        err = ENOMEM;
1495
0
        detail = "out of memory";
1496
0
        break;
1497
0
      case BZ_CONFIG_ERROR:
1498
0
        detail = "mis-compiled library";
1499
0
        break;
1500
0
      }
1501
0
      archive_set_error(&a->archive, err,
1502
0
          "Internal error initializing decompressor: %s",
1503
0
          detail == NULL ? "??" : detail);
1504
0
      xar->bzstream_valid = 0;
1505
0
      return (ARCHIVE_FATAL);
1506
0
    }
1507
0
    xar->bzstream_valid = 1;
1508
0
    xar->bzstream.total_in_lo32 = 0;
1509
0
    xar->bzstream.total_in_hi32 = 0;
1510
0
    xar->bzstream.total_out_lo32 = 0;
1511
0
    xar->bzstream.total_out_hi32 = 0;
1512
0
    break;
1513
0
#endif
1514
0
#if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1515
0
#if LZMA_VERSION_MAJOR >= 5
1516
/* Effectively disable the limiter. */
1517
0
#define LZMA_MEMLIMIT   UINT64_MAX
1518
#else
1519
/* NOTE: This needs to check memory size which running system has. */
1520
#define LZMA_MEMLIMIT   (1U << 30)
1521
#endif
1522
0
  case XZ:
1523
0
  case LZMA:
1524
0
    if (xar->lzstream_valid) {
1525
0
      lzma_end(&(xar->lzstream));
1526
0
      xar->lzstream_valid = 0;
1527
0
    }
1528
0
    if (xar->entry_encoding == XZ)
1529
0
      r = lzma_stream_decoder(&(xar->lzstream),
1530
0
          LZMA_MEMLIMIT,/* memlimit */
1531
0
          LZMA_CONCATENATED);
1532
0
    else
1533
0
      r = lzma_alone_decoder(&(xar->lzstream),
1534
0
          LZMA_MEMLIMIT);/* memlimit */
1535
0
    if (r != LZMA_OK) {
1536
0
      switch (r) {
1537
0
      case LZMA_MEM_ERROR:
1538
0
        archive_set_error(&a->archive,
1539
0
            ENOMEM,
1540
0
            "Internal error initializing "
1541
0
            "compression library: "
1542
0
            "Cannot allocate memory");
1543
0
        break;
1544
0
      case LZMA_OPTIONS_ERROR:
1545
0
        archive_set_error(&a->archive,
1546
0
            ARCHIVE_ERRNO_MISC,
1547
0
            "Internal error initializing "
1548
0
            "compression library: "
1549
0
            "Invalid or unsupported options");
1550
0
        break;
1551
0
      default:
1552
0
        archive_set_error(&a->archive,
1553
0
            ARCHIVE_ERRNO_MISC,
1554
0
            "Internal error initializing "
1555
0
            "lzma library");
1556
0
        break;
1557
0
      }
1558
0
      return (ARCHIVE_FATAL);
1559
0
    }
1560
0
    xar->lzstream_valid = 1;
1561
0
    xar->lzstream.total_in = 0;
1562
0
    xar->lzstream.total_out = 0;
1563
0
    break;
1564
0
#endif
1565
  /*
1566
   * Unsupported compression.
1567
   */
1568
0
  default:
1569
#if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR)
1570
  case BZIP2:
1571
#endif
1572
#if !defined(HAVE_LZMA_H) || !defined(HAVE_LIBLZMA)
1573
  case LZMA:
1574
  case XZ:
1575
#endif
1576
0
    switch (xar->entry_encoding) {
1577
0
    case BZIP2: detail = "bzip2"; break;
1578
0
    case LZMA: detail = "lzma"; break;
1579
0
    case XZ: detail = "xz"; break;
1580
0
    default: detail = "??"; break;
1581
0
    }
1582
0
    archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1583
0
        "%s compression not supported on this platform",
1584
0
        detail);
1585
0
    return (ARCHIVE_FAILED);
1586
1
  }
1587
1
  return (ARCHIVE_OK);
1588
1
}
1589
1590
static int
1591
decompress(struct archive_read *a, const void **buff, size_t *outbytes,
1592
    const void *b, size_t *used)
1593
694
{
1594
694
  struct xar *xar;
1595
694
  void *outbuff;
1596
694
  size_t avail_in, avail_out;
1597
694
  int r;
1598
1599
694
  xar = (struct xar *)(a->format->data);
1600
694
  avail_in = *used;
1601
694
  outbuff = (void *)(uintptr_t)*buff;
1602
694
  if (outbuff == NULL) {
1603
0
    if (xar->outbuff == NULL) {
1604
0
      xar->outbuff = malloc(OUTBUFF_SIZE);
1605
0
      if (xar->outbuff == NULL) {
1606
0
        archive_set_error(&a->archive, ENOMEM,
1607
0
            "Couldn't allocate memory for out buffer");
1608
0
        return (ARCHIVE_FATAL);
1609
0
      }
1610
0
    }
1611
0
    outbuff = xar->outbuff;
1612
0
    *buff = outbuff;
1613
0
    avail_out = OUTBUFF_SIZE;
1614
0
  } else
1615
694
    avail_out = *outbytes;
1616
694
  switch (xar->rd_encoding) {
1617
694
  case GZIP:
1618
694
    xar->stream.next_in = (Bytef *)(uintptr_t)b;
1619
694
    xar->stream.avail_in = (uInt)avail_in;
1620
694
    xar->stream.next_out = (unsigned char *)outbuff;
1621
694
    xar->stream.avail_out = (uInt)avail_out;
1622
694
    r = inflate(&(xar->stream), 0);
1623
694
    switch (r) {
1624
694
    case Z_OK: /* Decompressor made some progress.*/
1625
694
    case Z_STREAM_END: /* Found end of stream. */
1626
694
      break;
1627
0
    default:
1628
0
      archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1629
0
          "File decompression failed (%d)", r);
1630
0
      return (ARCHIVE_FATAL);
1631
694
    }
1632
694
    *used = avail_in - xar->stream.avail_in;
1633
694
    *outbytes = avail_out - xar->stream.avail_out;
1634
694
    break;
1635
0
#if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1636
0
  case BZIP2:
1637
0
    xar->bzstream.next_in = (char *)(uintptr_t)b;
1638
0
    xar->bzstream.avail_in = (unsigned int)avail_in;
1639
0
    xar->bzstream.next_out = (char *)outbuff;
1640
0
    xar->bzstream.avail_out = (unsigned int)avail_out;
1641
0
    r = BZ2_bzDecompress(&(xar->bzstream));
1642
0
    switch (r) {
1643
0
    case BZ_STREAM_END: /* Found end of stream. */
1644
0
      switch (BZ2_bzDecompressEnd(&(xar->bzstream))) {
1645
0
      case BZ_OK:
1646
0
        break;
1647
0
      default:
1648
0
        archive_set_error(&(a->archive),
1649
0
            ARCHIVE_ERRNO_MISC,
1650
0
            "Failed to clean up decompressor");
1651
0
        return (ARCHIVE_FATAL);
1652
0
      }
1653
0
      xar->bzstream_valid = 0;
1654
      /* FALLTHROUGH */
1655
0
    case BZ_OK: /* Decompressor made some progress. */
1656
0
      break;
1657
0
    default:
1658
0
      archive_set_error(&(a->archive),
1659
0
          ARCHIVE_ERRNO_MISC,
1660
0
          "bzip decompression failed");
1661
0
      return (ARCHIVE_FATAL);
1662
0
    }
1663
0
    *used = avail_in - xar->bzstream.avail_in;
1664
0
    *outbytes = avail_out - xar->bzstream.avail_out;
1665
0
    break;
1666
0
#endif
1667
0
#if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1668
0
  case LZMA:
1669
0
  case XZ:
1670
0
    xar->lzstream.next_in = b;
1671
0
    xar->lzstream.avail_in = avail_in;
1672
0
    xar->lzstream.next_out = (unsigned char *)outbuff;
1673
0
    xar->lzstream.avail_out = avail_out;
1674
0
    r = lzma_code(&(xar->lzstream), LZMA_RUN);
1675
0
    switch (r) {
1676
0
    case LZMA_STREAM_END: /* Found end of stream. */
1677
0
      lzma_end(&(xar->lzstream));
1678
0
      xar->lzstream_valid = 0;
1679
      /* FALLTHROUGH */
1680
0
    case LZMA_OK: /* Decompressor made some progress. */
1681
0
      break;
1682
0
    default:
1683
0
      archive_set_error(&(a->archive),
1684
0
          ARCHIVE_ERRNO_MISC,
1685
0
          "%s decompression failed(%d)",
1686
0
          (xar->entry_encoding == XZ)?"xz":"lzma",
1687
0
          r);
1688
0
      return (ARCHIVE_FATAL);
1689
0
    }
1690
0
    *used = avail_in - xar->lzstream.avail_in;
1691
0
    *outbytes = avail_out - xar->lzstream.avail_out;
1692
0
    break;
1693
0
#endif
1694
#if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR)
1695
  case BZIP2:
1696
#endif
1697
#if !defined(HAVE_LZMA_H) || !defined(HAVE_LIBLZMA)
1698
  case LZMA:
1699
  case XZ:
1700
#endif
1701
0
  case NONE:
1702
0
  default:
1703
0
    if (outbuff == xar->outbuff) {
1704
0
      *buff = b;
1705
0
      *used = avail_in;
1706
0
      *outbytes = avail_in;
1707
0
    } else {
1708
0
      if (avail_out > avail_in)
1709
0
        avail_out = avail_in;
1710
0
      memcpy(outbuff, b, avail_out);
1711
0
      *used = avail_out;
1712
0
      *outbytes = avail_out;
1713
0
    }
1714
0
    break;
1715
694
  }
1716
694
  return (ARCHIVE_OK);
1717
694
}
1718
1719
static int
1720
decompression_cleanup(struct archive_read *a)
1721
238
{
1722
238
  struct xar *xar;
1723
238
  int r;
1724
1725
238
  xar = (struct xar *)(a->format->data);
1726
238
  r = ARCHIVE_OK;
1727
238
  if (xar->stream_valid) {
1728
1
    if (inflateEnd(&(xar->stream)) != Z_OK) {
1729
0
      archive_set_error(&a->archive,
1730
0
          ARCHIVE_ERRNO_MISC,
1731
0
          "Failed to clean up zlib decompressor");
1732
0
      r = ARCHIVE_FATAL;
1733
0
    }
1734
1
  }
1735
238
#if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1736
238
  if (xar->bzstream_valid) {
1737
0
    if (BZ2_bzDecompressEnd(&(xar->bzstream)) != BZ_OK) {
1738
0
      archive_set_error(&a->archive,
1739
0
          ARCHIVE_ERRNO_MISC,
1740
0
          "Failed to clean up bzip2 decompressor");
1741
0
      r = ARCHIVE_FATAL;
1742
0
    }
1743
0
  }
1744
238
#endif
1745
238
#if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1746
238
  if (xar->lzstream_valid)
1747
0
    lzma_end(&(xar->lzstream));
1748
#elif defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1749
  if (xar->lzstream_valid) {
1750
    if (lzmadec_end(&(xar->lzstream)) != LZMADEC_OK) {
1751
      archive_set_error(&a->archive,
1752
          ARCHIVE_ERRNO_MISC,
1753
          "Failed to clean up lzmadec decompressor");
1754
      r = ARCHIVE_FATAL;
1755
    }
1756
  }
1757
#endif
1758
238
  return (r);
1759
238
}
1760
1761
static void
1762
238
checksum_cleanup(struct archive_read *a) {
1763
238
  struct xar *xar;
1764
1765
238
  xar = (struct xar *)(a->format->data);
1766
1767
238
  _checksum_final(&(xar->a_sumwrk), NULL, 0);
1768
238
  _checksum_final(&(xar->e_sumwrk), NULL, 0);
1769
238
}
1770
1771
static void
1772
xmlattr_cleanup(struct xmlattr_list *list)
1773
0
{
1774
0
  struct xmlattr *attr, *next;
1775
1776
0
  attr = list->first;
1777
0
  while (attr != NULL) {
1778
0
    next = attr->next;
1779
0
    free(attr->name);
1780
0
    free(attr->value);
1781
0
    free(attr);
1782
0
    attr = next;
1783
0
  }
1784
0
  list->first = NULL;
1785
0
  list->last = &(list->first);
1786
0
}
1787
1788
static int
1789
file_new(struct archive_read *a, struct xar *xar, struct xmlattr_list *list)
1790
0
{
1791
0
  struct xar_file *file;
1792
0
  struct xmlattr *attr;
1793
1794
0
  file = calloc(1, sizeof(*file));
1795
0
  if (file == NULL) {
1796
0
    archive_set_error(&a->archive, ENOMEM, "Out of memory");
1797
0
    return (ARCHIVE_FATAL);
1798
0
  }
1799
0
  file->parent = xar->file;
1800
0
  file->mode = 0777 | AE_IFREG;
1801
0
  file->atime =  0;
1802
0
  file->mtime = 0;
1803
0
  xar->file = file;
1804
0
  xar->xattr = NULL;
1805
0
  for (attr = list->first; attr != NULL; attr = attr->next) {
1806
0
    if (strcmp(attr->name, "id") == 0)
1807
0
      file->id = atol10(attr->value, strlen(attr->value));
1808
0
  }
1809
0
  file->nlink = 1;
1810
0
  if (heap_add_entry(a, &(xar->file_queue), file) != ARCHIVE_OK)
1811
0
    return (ARCHIVE_FATAL);
1812
0
  return (ARCHIVE_OK);
1813
0
}
1814
1815
static void
1816
file_free(struct xar_file *file)
1817
0
{
1818
0
  struct xattr *xattr;
1819
1820
0
  archive_string_free(&(file->pathname));
1821
0
  archive_string_free(&(file->symlink));
1822
0
  archive_string_free(&(file->uname));
1823
0
  archive_string_free(&(file->gname));
1824
0
  archive_string_free(&(file->hardlink));
1825
0
  xattr = file->xattr_list;
1826
0
  while (xattr != NULL) {
1827
0
    struct xattr *next;
1828
1829
0
    next = xattr->next;
1830
0
    xattr_free(xattr);
1831
0
    xattr = next;
1832
0
  }
1833
1834
0
  free(file);
1835
0
}
1836
1837
static int
1838
xattr_new(struct archive_read *a, struct xar *xar, struct xmlattr_list *list)
1839
0
{
1840
0
  struct xattr *xattr, **nx;
1841
0
  struct xmlattr *attr;
1842
1843
0
  xattr = calloc(1, sizeof(*xattr));
1844
0
  if (xattr == NULL) {
1845
0
    archive_set_error(&a->archive, ENOMEM, "Out of memory");
1846
0
    return (ARCHIVE_FATAL);
1847
0
  }
1848
0
  xar->xattr = xattr;
1849
0
  for (attr = list->first; attr != NULL; attr = attr->next) {
1850
0
    if (strcmp(attr->name, "id") == 0)
1851
0
      xattr->id = atol10(attr->value, strlen(attr->value));
1852
0
  }
1853
  /* Chain to xattr list. */
1854
0
  for (nx = &(xar->file->xattr_list);
1855
0
      *nx != NULL; nx = &((*nx)->next)) {
1856
0
    if (xattr->id < (*nx)->id)
1857
0
      break;
1858
0
  }
1859
0
  xattr->next = *nx;
1860
0
  *nx = xattr;
1861
1862
0
  return (ARCHIVE_OK);
1863
0
}
1864
1865
static void
1866
xattr_free(struct xattr *xattr)
1867
0
{
1868
0
  archive_string_free(&(xattr->name));
1869
0
  free(xattr);
1870
0
}
1871
1872
static int
1873
getencoding(struct xmlattr_list *list)
1874
0
{
1875
0
  struct xmlattr *attr;
1876
0
  enum enctype encoding = NONE;
1877
1878
0
  for (attr = list->first; attr != NULL; attr = attr->next) {
1879
0
    if (strcmp(attr->name, "style") == 0) {
1880
0
      if (strcmp(attr->value, "application/octet-stream") == 0)
1881
0
        encoding = NONE;
1882
0
      else if (strcmp(attr->value, "application/x-gzip") == 0)
1883
0
        encoding = GZIP;
1884
0
      else if (strcmp(attr->value, "application/x-bzip2") == 0)
1885
0
        encoding = BZIP2;
1886
0
      else if (strcmp(attr->value, "application/x-lzma") == 0)
1887
0
        encoding = LZMA;
1888
0
      else if (strcmp(attr->value, "application/x-xz") == 0)
1889
0
        encoding = XZ;
1890
0
    }
1891
0
  }
1892
0
  return (encoding);
1893
0
}
1894
1895
static int
1896
getsumalgorithm(struct xmlattr_list *list)
1897
0
{
1898
0
  struct xmlattr *attr;
1899
0
  int alg = CKSUM_NONE;
1900
1901
0
  for (attr = list->first; attr != NULL; attr = attr->next) {
1902
0
    if (strcmp(attr->name, "style") == 0) {
1903
0
      const char *v = attr->value;
1904
0
      if ((v[0] == 'S' || v[0] == 's') &&
1905
0
          (v[1] == 'H' || v[1] == 'h') &&
1906
0
          (v[2] == 'A' || v[2] == 'a') &&
1907
0
          v[3] == '1' && v[4] == '\0')
1908
0
        alg = CKSUM_SHA1;
1909
0
      if ((v[0] == 'M' || v[0] == 'm') &&
1910
0
          (v[1] == 'D' || v[1] == 'd') &&
1911
0
          v[2] == '5' && v[3] == '\0')
1912
0
        alg = CKSUM_MD5;
1913
0
    }
1914
0
  }
1915
0
  return (alg);
1916
0
}
1917
1918
static int
1919
unknowntag_start(struct archive_read *a, struct xar *xar, const char *name)
1920
0
{
1921
0
  struct unknown_tag *tag;
1922
1923
0
  tag = malloc(sizeof(*tag));
1924
0
  if (tag == NULL) {
1925
0
    archive_set_error(&a->archive, ENOMEM, "Out of memory");
1926
0
    return (ARCHIVE_FATAL);
1927
0
  }
1928
0
  tag->next = xar->unknowntags;
1929
0
  archive_string_init(&(tag->name));
1930
0
  archive_strcpy(&(tag->name), name);
1931
0
  if (xar->unknowntags == NULL) {
1932
#if DEBUG
1933
    fprintf(stderr, "UNKNOWNTAG_START:%s\n", name);
1934
#endif
1935
0
    xar->xmlsts_unknown = xar->xmlsts;
1936
0
    xar->xmlsts = UNKNOWN;
1937
0
  }
1938
0
  xar->unknowntags = tag;
1939
0
  return (ARCHIVE_OK);
1940
0
}
1941
1942
static void
1943
unknowntag_end(struct xar *xar, const char *name)
1944
0
{
1945
0
  struct unknown_tag *tag;
1946
1947
0
  tag = xar->unknowntags;
1948
0
  if (tag == NULL || name == NULL)
1949
0
    return;
1950
0
  if (strcmp(tag->name.s, name) == 0) {
1951
0
    xar->unknowntags = tag->next;
1952
0
    archive_string_free(&(tag->name));
1953
0
    free(tag);
1954
0
    if (xar->unknowntags == NULL) {
1955
#if DEBUG
1956
      fprintf(stderr, "UNKNOWNTAG_END:%s\n", name);
1957
#endif
1958
0
      xar->xmlsts = xar->xmlsts_unknown;
1959
0
    }
1960
0
  }
1961
0
}
1962
1963
static int
1964
xml_start(struct archive_read *a, const char *name, struct xmlattr_list *list)
1965
0
{
1966
0
  struct xar *xar;
1967
0
  struct xmlattr *attr;
1968
1969
0
  xar = (struct xar *)(a->format->data);
1970
1971
#if DEBUG
1972
  fprintf(stderr, "xml_sta:[%s]\n", name);
1973
  for (attr = list->first; attr != NULL; attr = attr->next)
1974
    fprintf(stderr, "    attr:\"%s\"=\"%s\"\n",
1975
        attr->name, attr->value);
1976
#endif
1977
0
  xar->base64text = 0;
1978
0
  switch (xar->xmlsts) {
1979
0
  case INIT:
1980
0
    if (strcmp(name, "xar") == 0)
1981
0
      xar->xmlsts = XAR;
1982
0
    else
1983
0
      if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
1984
0
        return (ARCHIVE_FATAL);
1985
0
    break;
1986
0
  case XAR:
1987
0
    if (strcmp(name, "toc") == 0)
1988
0
      xar->xmlsts = TOC;
1989
0
    else
1990
0
      if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
1991
0
        return (ARCHIVE_FATAL);
1992
0
    break;
1993
0
  case TOC:
1994
0
    if (strcmp(name, "creation-time") == 0)
1995
0
      xar->xmlsts = TOC_CREATION_TIME;
1996
0
    else if (strcmp(name, "checksum") == 0)
1997
0
      xar->xmlsts = TOC_CHECKSUM;
1998
0
    else if (strcmp(name, "file") == 0) {
1999
0
      if (file_new(a, xar, list) != ARCHIVE_OK)
2000
0
        return (ARCHIVE_FATAL);
2001
0
      xar->xmlsts = TOC_FILE;
2002
0
    }
2003
0
    else
2004
0
      if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2005
0
        return (ARCHIVE_FATAL);
2006
0
    break;
2007
0
  case TOC_CHECKSUM:
2008
0
    if (strcmp(name, "offset") == 0)
2009
0
      xar->xmlsts = TOC_CHECKSUM_OFFSET;
2010
0
    else if (strcmp(name, "size") == 0)
2011
0
      xar->xmlsts = TOC_CHECKSUM_SIZE;
2012
0
    else
2013
0
      if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2014
0
        return (ARCHIVE_FATAL);
2015
0
    break;
2016
0
  case TOC_FILE:
2017
0
    if (strcmp(name, "file") == 0) {
2018
0
      if (file_new(a, xar, list) != ARCHIVE_OK)
2019
0
        return (ARCHIVE_FATAL);
2020
0
    }
2021
0
    else if (strcmp(name, "data") == 0)
2022
0
      xar->xmlsts = FILE_DATA;
2023
0
    else if (strcmp(name, "ea") == 0) {
2024
0
      if (xattr_new(a, xar, list) != ARCHIVE_OK)
2025
0
        return (ARCHIVE_FATAL);
2026
0
      xar->xmlsts = FILE_EA;
2027
0
    }
2028
0
    else if (strcmp(name, "ctime") == 0)
2029
0
      xar->xmlsts = FILE_CTIME;
2030
0
    else if (strcmp(name, "mtime") == 0)
2031
0
      xar->xmlsts = FILE_MTIME;
2032
0
    else if (strcmp(name, "atime") == 0)
2033
0
      xar->xmlsts = FILE_ATIME;
2034
0
    else if (strcmp(name, "group") == 0)
2035
0
      xar->xmlsts = FILE_GROUP;
2036
0
    else if (strcmp(name, "gid") == 0)
2037
0
      xar->xmlsts = FILE_GID;
2038
0
    else if (strcmp(name, "user") == 0)
2039
0
      xar->xmlsts = FILE_USER;
2040
0
    else if (strcmp(name, "uid") == 0)
2041
0
      xar->xmlsts = FILE_UID;
2042
0
    else if (strcmp(name, "mode") == 0)
2043
0
      xar->xmlsts = FILE_MODE;
2044
0
    else if (strcmp(name, "device") == 0)
2045
0
      xar->xmlsts = FILE_DEVICE;
2046
0
    else if (strcmp(name, "deviceno") == 0)
2047
0
      xar->xmlsts = FILE_DEVICENO;
2048
0
    else if (strcmp(name, "inode") == 0)
2049
0
      xar->xmlsts = FILE_INODE;
2050
0
    else if (strcmp(name, "link") == 0)
2051
0
      xar->xmlsts = FILE_LINK;
2052
0
    else if (strcmp(name, "type") == 0) {
2053
0
      xar->xmlsts = FILE_TYPE;
2054
0
      for (attr = list->first; attr != NULL;
2055
0
          attr = attr->next) {
2056
0
        if (strcmp(attr->name, "link") != 0)
2057
0
          continue;
2058
0
        if (xar->file->hdnext != NULL || xar->file->link != 0 ||
2059
0
            xar->file == xar->hdlink_orgs) {
2060
0
          archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2061
0
              "File with multiple link attributes");
2062
0
          return (ARCHIVE_FATAL);
2063
0
        }
2064
0
        if (strcmp(attr->value, "original") == 0) {
2065
0
          xar->file->hdnext = xar->hdlink_orgs;
2066
0
          xar->hdlink_orgs = xar->file;
2067
0
        } else {
2068
0
          xar->file->link = (unsigned)atol10(attr->value,
2069
0
              strlen(attr->value));
2070
0
          if (xar->file->link > 0)
2071
0
            if (add_link(a, xar, xar->file) != ARCHIVE_OK) {
2072
0
              return (ARCHIVE_FATAL);
2073
0
            };
2074
0
        }
2075
0
      }
2076
0
    }
2077
0
    else if (strcmp(name, "name") == 0) {
2078
0
      xar->xmlsts = FILE_NAME;
2079
0
      for (attr = list->first; attr != NULL;
2080
0
          attr = attr->next) {
2081
0
        if (strcmp(attr->name, "enctype") == 0 &&
2082
0
            strcmp(attr->value, "base64") == 0)
2083
0
          xar->base64text = 1;
2084
0
      }
2085
0
    }
2086
0
    else if (strcmp(name, "acl") == 0)
2087
0
      xar->xmlsts = FILE_ACL;
2088
0
    else if (strcmp(name, "flags") == 0)
2089
0
      xar->xmlsts = FILE_FLAGS;
2090
0
    else if (strcmp(name, "ext2") == 0)
2091
0
      xar->xmlsts = FILE_EXT2;
2092
0
    else
2093
0
      if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2094
0
        return (ARCHIVE_FATAL);
2095
0
    break;
2096
0
  case FILE_DATA:
2097
0
    if (strcmp(name, "length") == 0)
2098
0
      xar->xmlsts = FILE_DATA_LENGTH;
2099
0
    else if (strcmp(name, "offset") == 0)
2100
0
      xar->xmlsts = FILE_DATA_OFFSET;
2101
0
    else if (strcmp(name, "size") == 0)
2102
0
      xar->xmlsts = FILE_DATA_SIZE;
2103
0
    else if (strcmp(name, "encoding") == 0) {
2104
0
      xar->xmlsts = FILE_DATA_ENCODING;
2105
0
      xar->file->encoding = getencoding(list);
2106
0
    }
2107
0
    else if (strcmp(name, "archived-checksum") == 0) {
2108
0
      xar->xmlsts = FILE_DATA_A_CHECKSUM;
2109
0
      xar->file->a_sum.alg = getsumalgorithm(list);
2110
0
    }
2111
0
    else if (strcmp(name, "extracted-checksum") == 0) {
2112
0
      xar->xmlsts = FILE_DATA_E_CHECKSUM;
2113
0
      xar->file->e_sum.alg = getsumalgorithm(list);
2114
0
    }
2115
0
    else if (strcmp(name, "content") == 0)
2116
0
      xar->xmlsts = FILE_DATA_CONTENT;
2117
0
    else
2118
0
      if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2119
0
        return (ARCHIVE_FATAL);
2120
0
    break;
2121
0
  case FILE_DEVICE:
2122
0
    if (strcmp(name, "major") == 0)
2123
0
      xar->xmlsts = FILE_DEVICE_MAJOR;
2124
0
    else if (strcmp(name, "minor") == 0)
2125
0
      xar->xmlsts = FILE_DEVICE_MINOR;
2126
0
    else
2127
0
      if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2128
0
        return (ARCHIVE_FATAL);
2129
0
    break;
2130
0
  case FILE_DATA_CONTENT:
2131
0
    if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2132
0
      return (ARCHIVE_FATAL);
2133
0
    break;
2134
0
  case FILE_EA:
2135
0
    if (strcmp(name, "length") == 0)
2136
0
      xar->xmlsts = FILE_EA_LENGTH;
2137
0
    else if (strcmp(name, "offset") == 0)
2138
0
      xar->xmlsts = FILE_EA_OFFSET;
2139
0
    else if (strcmp(name, "size") == 0)
2140
0
      xar->xmlsts = FILE_EA_SIZE;
2141
0
    else if (strcmp(name, "encoding") == 0) {
2142
0
      xar->xmlsts = FILE_EA_ENCODING;
2143
0
      xar->xattr->encoding = getencoding(list);
2144
0
    } else if (strcmp(name, "archived-checksum") == 0)
2145
0
      xar->xmlsts = FILE_EA_A_CHECKSUM;
2146
0
    else if (strcmp(name, "extracted-checksum") == 0)
2147
0
      xar->xmlsts = FILE_EA_E_CHECKSUM;
2148
0
    else if (strcmp(name, "name") == 0)
2149
0
      xar->xmlsts = FILE_EA_NAME;
2150
0
    else if (strcmp(name, "fstype") == 0)
2151
0
      xar->xmlsts = FILE_EA_FSTYPE;
2152
0
    else
2153
0
      if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2154
0
        return (ARCHIVE_FATAL);
2155
0
    break;
2156
0
  case FILE_ACL:
2157
0
    if (strcmp(name, "appleextended") == 0)
2158
0
      xar->xmlsts = FILE_ACL_APPLEEXTENDED;
2159
0
    else if (strcmp(name, "default") == 0)
2160
0
      xar->xmlsts = FILE_ACL_DEFAULT;
2161
0
    else if (strcmp(name, "access") == 0)
2162
0
      xar->xmlsts = FILE_ACL_ACCESS;
2163
0
    else
2164
0
      if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2165
0
        return (ARCHIVE_FATAL);
2166
0
    break;
2167
0
  case FILE_FLAGS:
2168
0
    if (!xml_parse_file_flags(xar, name))
2169
0
      if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2170
0
        return (ARCHIVE_FATAL);
2171
0
    break;
2172
0
  case FILE_EXT2:
2173
0
    if (!xml_parse_file_ext2(xar, name))
2174
0
      if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2175
0
        return (ARCHIVE_FATAL);
2176
0
    break;
2177
0
  case TOC_CREATION_TIME:
2178
0
  case TOC_CHECKSUM_OFFSET:
2179
0
  case TOC_CHECKSUM_SIZE:
2180
0
  case FILE_DATA_LENGTH:
2181
0
  case FILE_DATA_OFFSET:
2182
0
  case FILE_DATA_SIZE:
2183
0
  case FILE_DATA_ENCODING:
2184
0
  case FILE_DATA_A_CHECKSUM:
2185
0
  case FILE_DATA_E_CHECKSUM:
2186
0
  case FILE_EA_LENGTH:
2187
0
  case FILE_EA_OFFSET:
2188
0
  case FILE_EA_SIZE:
2189
0
  case FILE_EA_ENCODING:
2190
0
  case FILE_EA_A_CHECKSUM:
2191
0
  case FILE_EA_E_CHECKSUM:
2192
0
  case FILE_EA_NAME:
2193
0
  case FILE_EA_FSTYPE:
2194
0
  case FILE_CTIME:
2195
0
  case FILE_MTIME:
2196
0
  case FILE_ATIME:
2197
0
  case FILE_GROUP:
2198
0
  case FILE_GID:
2199
0
  case FILE_USER:
2200
0
  case FILE_UID:
2201
0
  case FILE_INODE:
2202
0
  case FILE_DEVICE_MAJOR:
2203
0
  case FILE_DEVICE_MINOR:
2204
0
  case FILE_DEVICENO:
2205
0
  case FILE_MODE:
2206
0
  case FILE_TYPE:
2207
0
  case FILE_LINK:
2208
0
  case FILE_NAME:
2209
0
  case FILE_ACL_DEFAULT:
2210
0
  case FILE_ACL_ACCESS:
2211
0
  case FILE_ACL_APPLEEXTENDED:
2212
0
  case FILE_FLAGS_USER_NODUMP:
2213
0
  case FILE_FLAGS_USER_IMMUTABLE:
2214
0
  case FILE_FLAGS_USER_APPEND:
2215
0
  case FILE_FLAGS_USER_OPAQUE:
2216
0
  case FILE_FLAGS_USER_NOUNLINK:
2217
0
  case FILE_FLAGS_SYS_ARCHIVED:
2218
0
  case FILE_FLAGS_SYS_IMMUTABLE:
2219
0
  case FILE_FLAGS_SYS_APPEND:
2220
0
  case FILE_FLAGS_SYS_NOUNLINK:
2221
0
  case FILE_FLAGS_SYS_SNAPSHOT:
2222
0
  case FILE_EXT2_SecureDeletion:
2223
0
  case FILE_EXT2_Undelete:
2224
0
  case FILE_EXT2_Compress:
2225
0
  case FILE_EXT2_Synchronous:
2226
0
  case FILE_EXT2_Immutable:
2227
0
  case FILE_EXT2_AppendOnly:
2228
0
  case FILE_EXT2_NoDump:
2229
0
  case FILE_EXT2_NoAtime:
2230
0
  case FILE_EXT2_CompDirty:
2231
0
  case FILE_EXT2_CompBlock:
2232
0
  case FILE_EXT2_NoCompBlock:
2233
0
  case FILE_EXT2_CompError:
2234
0
  case FILE_EXT2_BTree:
2235
0
  case FILE_EXT2_HashIndexed:
2236
0
  case FILE_EXT2_iMagic:
2237
0
  case FILE_EXT2_Journaled:
2238
0
  case FILE_EXT2_NoTail:
2239
0
  case FILE_EXT2_DirSync:
2240
0
  case FILE_EXT2_TopDir:
2241
0
  case FILE_EXT2_Reserved:
2242
0
  case UNKNOWN:
2243
0
    if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2244
0
      return (ARCHIVE_FATAL);
2245
0
    break;
2246
0
  }
2247
0
  return (ARCHIVE_OK);
2248
0
}
2249
2250
static void
2251
xml_end(void *userData, const char *name)
2252
0
{
2253
0
  struct archive_read *a;
2254
0
  struct xar *xar;
2255
2256
0
  a = (struct archive_read *)userData;
2257
0
  xar = (struct xar *)(a->format->data);
2258
2259
#if DEBUG
2260
  fprintf(stderr, "xml_end:[%s]\n", name);
2261
#endif
2262
0
  switch (xar->xmlsts) {
2263
0
  case INIT:
2264
0
    break;
2265
0
  case XAR:
2266
0
    if (strcmp(name, "xar") == 0)
2267
0
      xar->xmlsts = INIT;
2268
0
    break;
2269
0
  case TOC:
2270
0
    if (strcmp(name, "toc") == 0)
2271
0
      xar->xmlsts = XAR;
2272
0
    break;
2273
0
  case TOC_CREATION_TIME:
2274
0
    if (strcmp(name, "creation-time") == 0)
2275
0
      xar->xmlsts = TOC;
2276
0
    break;
2277
0
  case TOC_CHECKSUM:
2278
0
    if (strcmp(name, "checksum") == 0)
2279
0
      xar->xmlsts = TOC;
2280
0
    break;
2281
0
  case TOC_CHECKSUM_OFFSET:
2282
0
    if (strcmp(name, "offset") == 0)
2283
0
      xar->xmlsts = TOC_CHECKSUM;
2284
0
    break;
2285
0
  case TOC_CHECKSUM_SIZE:
2286
0
    if (strcmp(name, "size") == 0)
2287
0
      xar->xmlsts = TOC_CHECKSUM;
2288
0
    break;
2289
0
  case TOC_FILE:
2290
0
    if (strcmp(name, "file") == 0) {
2291
0
      if (xar->file->parent != NULL &&
2292
0
          ((xar->file->mode & AE_IFMT) == AE_IFDIR))
2293
0
        xar->file->parent->subdirs++;
2294
0
      xar->file = xar->file->parent;
2295
0
      if (xar->file == NULL)
2296
0
        xar->xmlsts = TOC;
2297
0
    }
2298
0
    break;
2299
0
  case FILE_DATA:
2300
0
    if (strcmp(name, "data") == 0)
2301
0
      xar->xmlsts = TOC_FILE;
2302
0
    break;
2303
0
  case FILE_DATA_LENGTH:
2304
0
    if (strcmp(name, "length") == 0)
2305
0
      xar->xmlsts = FILE_DATA;
2306
0
    break;
2307
0
  case FILE_DATA_OFFSET:
2308
0
    if (strcmp(name, "offset") == 0)
2309
0
      xar->xmlsts = FILE_DATA;
2310
0
    break;
2311
0
  case FILE_DATA_SIZE:
2312
0
    if (strcmp(name, "size") == 0)
2313
0
      xar->xmlsts = FILE_DATA;
2314
0
    break;
2315
0
  case FILE_DATA_ENCODING:
2316
0
    if (strcmp(name, "encoding") == 0)
2317
0
      xar->xmlsts = FILE_DATA;
2318
0
    break;
2319
0
  case FILE_DATA_A_CHECKSUM:
2320
0
    if (strcmp(name, "archived-checksum") == 0)
2321
0
      xar->xmlsts = FILE_DATA;
2322
0
    break;
2323
0
  case FILE_DATA_E_CHECKSUM:
2324
0
    if (strcmp(name, "extracted-checksum") == 0)
2325
0
      xar->xmlsts = FILE_DATA;
2326
0
    break;
2327
0
  case FILE_DATA_CONTENT:
2328
0
    if (strcmp(name, "content") == 0)
2329
0
      xar->xmlsts = FILE_DATA;
2330
0
    break;
2331
0
  case FILE_EA:
2332
0
    if (strcmp(name, "ea") == 0) {
2333
0
      xar->xmlsts = TOC_FILE;
2334
0
      xar->xattr = NULL;
2335
0
    }
2336
0
    break;
2337
0
  case FILE_EA_LENGTH:
2338
0
    if (strcmp(name, "length") == 0)
2339
0
      xar->xmlsts = FILE_EA;
2340
0
    break;
2341
0
  case FILE_EA_OFFSET:
2342
0
    if (strcmp(name, "offset") == 0)
2343
0
      xar->xmlsts = FILE_EA;
2344
0
    break;
2345
0
  case FILE_EA_SIZE:
2346
0
    if (strcmp(name, "size") == 0)
2347
0
      xar->xmlsts = FILE_EA;
2348
0
    break;
2349
0
  case FILE_EA_ENCODING:
2350
0
    if (strcmp(name, "encoding") == 0)
2351
0
      xar->xmlsts = FILE_EA;
2352
0
    break;
2353
0
  case FILE_EA_A_CHECKSUM:
2354
0
    if (strcmp(name, "archived-checksum") == 0)
2355
0
      xar->xmlsts = FILE_EA;
2356
0
    break;
2357
0
  case FILE_EA_E_CHECKSUM:
2358
0
    if (strcmp(name, "extracted-checksum") == 0)
2359
0
      xar->xmlsts = FILE_EA;
2360
0
    break;
2361
0
  case FILE_EA_NAME:
2362
0
    if (strcmp(name, "name") == 0)
2363
0
      xar->xmlsts = FILE_EA;
2364
0
    break;
2365
0
  case FILE_EA_FSTYPE:
2366
0
    if (strcmp(name, "fstype") == 0)
2367
0
      xar->xmlsts = FILE_EA;
2368
0
    break;
2369
0
  case FILE_CTIME:
2370
0
    if (strcmp(name, "ctime") == 0)
2371
0
      xar->xmlsts = TOC_FILE;
2372
0
    break;
2373
0
  case FILE_MTIME:
2374
0
    if (strcmp(name, "mtime") == 0)
2375
0
      xar->xmlsts = TOC_FILE;
2376
0
    break;
2377
0
  case FILE_ATIME:
2378
0
    if (strcmp(name, "atime") == 0)
2379
0
      xar->xmlsts = TOC_FILE;
2380
0
    break;
2381
0
  case FILE_GROUP:
2382
0
    if (strcmp(name, "group") == 0)
2383
0
      xar->xmlsts = TOC_FILE;
2384
0
    break;
2385
0
  case FILE_GID:
2386
0
    if (strcmp(name, "gid") == 0)
2387
0
      xar->xmlsts = TOC_FILE;
2388
0
    break;
2389
0
  case FILE_USER:
2390
0
    if (strcmp(name, "user") == 0)
2391
0
      xar->xmlsts = TOC_FILE;
2392
0
    break;
2393
0
  case FILE_UID:
2394
0
    if (strcmp(name, "uid") == 0)
2395
0
      xar->xmlsts = TOC_FILE;
2396
0
    break;
2397
0
  case FILE_MODE:
2398
0
    if (strcmp(name, "mode") == 0)
2399
0
      xar->xmlsts = TOC_FILE;
2400
0
    break;
2401
0
  case FILE_DEVICE:
2402
0
    if (strcmp(name, "device") == 0)
2403
0
      xar->xmlsts = TOC_FILE;
2404
0
    break;
2405
0
  case FILE_DEVICE_MAJOR:
2406
0
    if (strcmp(name, "major") == 0)
2407
0
      xar->xmlsts = FILE_DEVICE;
2408
0
    break;
2409
0
  case FILE_DEVICE_MINOR:
2410
0
    if (strcmp(name, "minor") == 0)
2411
0
      xar->xmlsts = FILE_DEVICE;
2412
0
    break;
2413
0
  case FILE_DEVICENO:
2414
0
    if (strcmp(name, "deviceno") == 0)
2415
0
      xar->xmlsts = TOC_FILE;
2416
0
    break;
2417
0
  case FILE_INODE:
2418
0
    if (strcmp(name, "inode") == 0)
2419
0
      xar->xmlsts = TOC_FILE;
2420
0
    break;
2421
0
  case FILE_LINK:
2422
0
    if (strcmp(name, "link") == 0)
2423
0
      xar->xmlsts = TOC_FILE;
2424
0
    break;
2425
0
  case FILE_TYPE:
2426
0
    if (strcmp(name, "type") == 0)
2427
0
      xar->xmlsts = TOC_FILE;
2428
0
    break;
2429
0
  case FILE_NAME:
2430
0
    if (strcmp(name, "name") == 0)
2431
0
      xar->xmlsts = TOC_FILE;
2432
0
    break;
2433
0
  case FILE_ACL:
2434
0
    if (strcmp(name, "acl") == 0)
2435
0
      xar->xmlsts = TOC_FILE;
2436
0
    break;
2437
0
  case FILE_ACL_DEFAULT:
2438
0
    if (strcmp(name, "default") == 0)
2439
0
      xar->xmlsts = FILE_ACL;
2440
0
    break;
2441
0
  case FILE_ACL_ACCESS:
2442
0
    if (strcmp(name, "access") == 0)
2443
0
      xar->xmlsts = FILE_ACL;
2444
0
    break;
2445
0
  case FILE_ACL_APPLEEXTENDED:
2446
0
    if (strcmp(name, "appleextended") == 0)
2447
0
      xar->xmlsts = FILE_ACL;
2448
0
    break;
2449
0
  case FILE_FLAGS:
2450
0
    if (strcmp(name, "flags") == 0)
2451
0
      xar->xmlsts = TOC_FILE;
2452
0
    break;
2453
0
  case FILE_FLAGS_USER_NODUMP:
2454
0
    if (strcmp(name, "UserNoDump") == 0)
2455
0
      xar->xmlsts = FILE_FLAGS;
2456
0
    break;
2457
0
  case FILE_FLAGS_USER_IMMUTABLE:
2458
0
    if (strcmp(name, "UserImmutable") == 0)
2459
0
      xar->xmlsts = FILE_FLAGS;
2460
0
    break;
2461
0
  case FILE_FLAGS_USER_APPEND:
2462
0
    if (strcmp(name, "UserAppend") == 0)
2463
0
      xar->xmlsts = FILE_FLAGS;
2464
0
    break;
2465
0
  case FILE_FLAGS_USER_OPAQUE:
2466
0
    if (strcmp(name, "UserOpaque") == 0)
2467
0
      xar->xmlsts = FILE_FLAGS;
2468
0
    break;
2469
0
  case FILE_FLAGS_USER_NOUNLINK:
2470
0
    if (strcmp(name, "UserNoUnlink") == 0)
2471
0
      xar->xmlsts = FILE_FLAGS;
2472
0
    break;
2473
0
  case FILE_FLAGS_SYS_ARCHIVED:
2474
0
    if (strcmp(name, "SystemArchived") == 0)
2475
0
      xar->xmlsts = FILE_FLAGS;
2476
0
    break;
2477
0
  case FILE_FLAGS_SYS_IMMUTABLE:
2478
0
    if (strcmp(name, "SystemImmutable") == 0)
2479
0
      xar->xmlsts = FILE_FLAGS;
2480
0
    break;
2481
0
  case FILE_FLAGS_SYS_APPEND:
2482
0
    if (strcmp(name, "SystemAppend") == 0)
2483
0
      xar->xmlsts = FILE_FLAGS;
2484
0
    break;
2485
0
  case FILE_FLAGS_SYS_NOUNLINK:
2486
0
    if (strcmp(name, "SystemNoUnlink") == 0)
2487
0
      xar->xmlsts = FILE_FLAGS;
2488
0
    break;
2489
0
  case FILE_FLAGS_SYS_SNAPSHOT:
2490
0
    if (strcmp(name, "SystemSnapshot") == 0)
2491
0
      xar->xmlsts = FILE_FLAGS;
2492
0
    break;
2493
0
  case FILE_EXT2:
2494
0
    if (strcmp(name, "ext2") == 0)
2495
0
      xar->xmlsts = TOC_FILE;
2496
0
    break;
2497
0
  case FILE_EXT2_SecureDeletion:
2498
0
    if (strcmp(name, "SecureDeletion") == 0)
2499
0
      xar->xmlsts = FILE_EXT2;
2500
0
    break;
2501
0
  case FILE_EXT2_Undelete:
2502
0
    if (strcmp(name, "Undelete") == 0)
2503
0
      xar->xmlsts = FILE_EXT2;
2504
0
    break;
2505
0
  case FILE_EXT2_Compress:
2506
0
    if (strcmp(name, "Compress") == 0)
2507
0
      xar->xmlsts = FILE_EXT2;
2508
0
    break;
2509
0
  case FILE_EXT2_Synchronous:
2510
0
    if (strcmp(name, "Synchronous") == 0)
2511
0
      xar->xmlsts = FILE_EXT2;
2512
0
    break;
2513
0
  case FILE_EXT2_Immutable:
2514
0
    if (strcmp(name, "Immutable") == 0)
2515
0
      xar->xmlsts = FILE_EXT2;
2516
0
    break;
2517
0
  case FILE_EXT2_AppendOnly:
2518
0
    if (strcmp(name, "AppendOnly") == 0)
2519
0
      xar->xmlsts = FILE_EXT2;
2520
0
    break;
2521
0
  case FILE_EXT2_NoDump:
2522
0
    if (strcmp(name, "NoDump") == 0)
2523
0
      xar->xmlsts = FILE_EXT2;
2524
0
    break;
2525
0
  case FILE_EXT2_NoAtime:
2526
0
    if (strcmp(name, "NoAtime") == 0)
2527
0
      xar->xmlsts = FILE_EXT2;
2528
0
    break;
2529
0
  case FILE_EXT2_CompDirty:
2530
0
    if (strcmp(name, "CompDirty") == 0)
2531
0
      xar->xmlsts = FILE_EXT2;
2532
0
    break;
2533
0
  case FILE_EXT2_CompBlock:
2534
0
    if (strcmp(name, "CompBlock") == 0)
2535
0
      xar->xmlsts = FILE_EXT2;
2536
0
    break;
2537
0
  case FILE_EXT2_NoCompBlock:
2538
0
    if (strcmp(name, "NoCompBlock") == 0)
2539
0
      xar->xmlsts = FILE_EXT2;
2540
0
    break;
2541
0
  case FILE_EXT2_CompError:
2542
0
    if (strcmp(name, "CompError") == 0)
2543
0
      xar->xmlsts = FILE_EXT2;
2544
0
    break;
2545
0
  case FILE_EXT2_BTree:
2546
0
    if (strcmp(name, "BTree") == 0)
2547
0
      xar->xmlsts = FILE_EXT2;
2548
0
    break;
2549
0
  case FILE_EXT2_HashIndexed:
2550
0
    if (strcmp(name, "HashIndexed") == 0)
2551
0
      xar->xmlsts = FILE_EXT2;
2552
0
    break;
2553
0
  case FILE_EXT2_iMagic:
2554
0
    if (strcmp(name, "iMagic") == 0)
2555
0
      xar->xmlsts = FILE_EXT2;
2556
0
    break;
2557
0
  case FILE_EXT2_Journaled:
2558
0
    if (strcmp(name, "Journaled") == 0)
2559
0
      xar->xmlsts = FILE_EXT2;
2560
0
    break;
2561
0
  case FILE_EXT2_NoTail:
2562
0
    if (strcmp(name, "NoTail") == 0)
2563
0
      xar->xmlsts = FILE_EXT2;
2564
0
    break;
2565
0
  case FILE_EXT2_DirSync:
2566
0
    if (strcmp(name, "DirSync") == 0)
2567
0
      xar->xmlsts = FILE_EXT2;
2568
0
    break;
2569
0
  case FILE_EXT2_TopDir:
2570
0
    if (strcmp(name, "TopDir") == 0)
2571
0
      xar->xmlsts = FILE_EXT2;
2572
0
    break;
2573
0
  case FILE_EXT2_Reserved:
2574
0
    if (strcmp(name, "Reserved") == 0)
2575
0
      xar->xmlsts = FILE_EXT2;
2576
0
    break;
2577
0
  case UNKNOWN:
2578
0
    unknowntag_end(xar, name);
2579
0
    break;
2580
0
  }
2581
0
}
2582
2583
static const int base64[256] = {
2584
  -1, -1, -1, -1, -1, -1, -1, -1,
2585
  -1, -1, -1, -1, -1, -1, -1, -1, /* 00 - 0F */
2586
  -1, -1, -1, -1, -1, -1, -1, -1,
2587
  -1, -1, -1, -1, -1, -1, -1, -1, /* 10 - 1F */
2588
  -1, -1, -1, -1, -1, -1, -1, -1,
2589
  -1, -1, -1, 62, -1, -1, -1, 63, /* 20 - 2F */
2590
  52, 53, 54, 55, 56, 57, 58, 59,
2591
  60, 61, -1, -1, -1, -1, -1, -1, /* 30 - 3F */
2592
  -1,  0,  1,  2,  3,  4,  5,  6,
2593
   7,  8,  9, 10, 11, 12, 13, 14, /* 40 - 4F */
2594
  15, 16, 17, 18, 19, 20, 21, 22,
2595
  23, 24, 25, -1, -1, -1, -1, -1, /* 50 - 5F */
2596
  -1, 26, 27, 28, 29, 30, 31, 32,
2597
  33, 34, 35, 36, 37, 38, 39, 40, /* 60 - 6F */
2598
  41, 42, 43, 44, 45, 46, 47, 48,
2599
  49, 50, 51, -1, -1, -1, -1, -1, /* 70 - 7F */
2600
  -1, -1, -1, -1, -1, -1, -1, -1,
2601
  -1, -1, -1, -1, -1, -1, -1, -1, /* 80 - 8F */
2602
  -1, -1, -1, -1, -1, -1, -1, -1,
2603
  -1, -1, -1, -1, -1, -1, -1, -1, /* 90 - 9F */
2604
  -1, -1, -1, -1, -1, -1, -1, -1,
2605
  -1, -1, -1, -1, -1, -1, -1, -1, /* A0 - AF */
2606
  -1, -1, -1, -1, -1, -1, -1, -1,
2607
  -1, -1, -1, -1, -1, -1, -1, -1, /* B0 - BF */
2608
  -1, -1, -1, -1, -1, -1, -1, -1,
2609
  -1, -1, -1, -1, -1, -1, -1, -1, /* C0 - CF */
2610
  -1, -1, -1, -1, -1, -1, -1, -1,
2611
  -1, -1, -1, -1, -1, -1, -1, -1, /* D0 - DF */
2612
  -1, -1, -1, -1, -1, -1, -1, -1,
2613
  -1, -1, -1, -1, -1, -1, -1, -1, /* E0 - EF */
2614
  -1, -1, -1, -1, -1, -1, -1, -1,
2615
  -1, -1, -1, -1, -1, -1, -1, -1, /* F0 - FF */
2616
};
2617
2618
static void
2619
strappend_base64(struct xar *xar,
2620
    struct archive_string *as, const char *s, size_t l)
2621
0
{
2622
0
  unsigned char buff[256];
2623
0
  unsigned char *out;
2624
0
  const unsigned char *b;
2625
0
  size_t len;
2626
2627
0
  (void)xar; /* UNUSED */
2628
0
  len = 0;
2629
0
  out = buff;
2630
0
  b = (const unsigned char *)s;
2631
0
  while (l > 0) {
2632
0
    int n = 0;
2633
2634
0
    if (base64[b[0]] < 0 || base64[b[1]] < 0)
2635
0
      break;
2636
0
    n = base64[*b++] << 18;
2637
0
    n |= base64[*b++] << 12;
2638
0
    *out++ = n >> 16;
2639
0
    len++;
2640
0
    l -= 2;
2641
2642
0
    if (l > 0) {
2643
0
      if (base64[*b] < 0)
2644
0
        break;
2645
0
      n |= base64[*b++] << 6;
2646
0
      *out++ = (n >> 8) & 0xFF;
2647
0
      len++;
2648
0
      --l;
2649
0
    }
2650
0
    if (l > 0) {
2651
0
      if (base64[*b] < 0)
2652
0
        break;
2653
0
      n |= base64[*b++];
2654
0
      *out++ = n & 0xFF;
2655
0
      len++;
2656
0
      --l;
2657
0
    }
2658
0
    if (len+3 >= sizeof(buff)) {
2659
0
      archive_strncat(as, (const char *)buff, len);
2660
0
      len = 0;
2661
0
      out = buff;
2662
0
    }
2663
0
  }
2664
0
  if (len > 0)
2665
0
    archive_strncat(as, (const char *)buff, len);
2666
0
}
2667
2668
static int
2669
is_string(const char *known, const char *data, size_t len)
2670
0
{
2671
0
  if (strlen(known) != len)
2672
0
    return -1;
2673
0
  return memcmp(data, known, len);
2674
0
}
2675
2676
static void
2677
xml_data(void *userData, const char *s, size_t len)
2678
0
{
2679
0
  struct archive_read *a;
2680
0
  struct xar *xar;
2681
2682
0
  a = (struct archive_read *)userData;
2683
0
  xar = (struct xar *)(a->format->data);
2684
2685
#if DEBUG
2686
  {
2687
    char buff[1024];
2688
    if (len > (int)(sizeof(buff)-1))
2689
      len = (int)(sizeof(buff)-1);
2690
    strncpy(buff, s, len);
2691
    buff[len] = 0;
2692
    fprintf(stderr, "\tlen=%d:\"%s\"\n", len, buff);
2693
  }
2694
#endif
2695
0
  switch (xar->xmlsts) {
2696
0
  case TOC_CHECKSUM_OFFSET:
2697
0
    xar->toc_chksum_offset = atol10(s, len);
2698
0
    break;
2699
0
  case TOC_CHECKSUM_SIZE:
2700
0
    xar->toc_chksum_size = atol10(s, len);
2701
0
    break;
2702
0
  default:
2703
0
    break;
2704
0
  }
2705
0
  if (xar->file == NULL)
2706
0
    return;
2707
2708
0
  switch (xar->xmlsts) {
2709
0
  case FILE_NAME:
2710
0
    if (xar->file->has & HAS_PATHNAME)
2711
0
      break;
2712
2713
0
    if (xar->file->parent != NULL) {
2714
0
      archive_string_concat(&(xar->file->pathname),
2715
0
          &(xar->file->parent->pathname));
2716
0
      archive_strappend_char(&(xar->file->pathname), '/');
2717
0
    }
2718
0
    xar->file->has |= HAS_PATHNAME;
2719
0
    if (xar->base64text) {
2720
0
      strappend_base64(xar,
2721
0
          &(xar->file->pathname), s, len);
2722
0
    } else
2723
0
      archive_strncat(&(xar->file->pathname), s, len);
2724
0
    break;
2725
0
  case FILE_LINK:
2726
0
    xar->file->has |= HAS_SYMLINK;
2727
0
    archive_strncpy(&(xar->file->symlink), s, len);
2728
0
    break;
2729
0
  case FILE_TYPE:
2730
0
    if (is_string("file", s, len) == 0 ||
2731
0
        is_string("hardlink", s, len) == 0)
2732
0
      xar->file->mode =
2733
0
          (xar->file->mode & ~AE_IFMT) | AE_IFREG;
2734
0
    if (is_string("directory", s, len) == 0)
2735
0
      xar->file->mode =
2736
0
          (xar->file->mode & ~AE_IFMT) | AE_IFDIR;
2737
0
    if (is_string("symlink", s, len) == 0)
2738
0
      xar->file->mode =
2739
0
          (xar->file->mode & ~AE_IFMT) | AE_IFLNK;
2740
0
    if (is_string("character special", s, len) == 0)
2741
0
      xar->file->mode =
2742
0
          (xar->file->mode & ~AE_IFMT) | AE_IFCHR;
2743
0
    if (is_string("block special", s, len) == 0)
2744
0
      xar->file->mode =
2745
0
          (xar->file->mode & ~AE_IFMT) | AE_IFBLK;
2746
0
    if (is_string("socket", s, len) == 0)
2747
0
      xar->file->mode =
2748
0
          (xar->file->mode & ~AE_IFMT) | AE_IFSOCK;
2749
0
    if (is_string("fifo", s, len) == 0)
2750
0
      xar->file->mode =
2751
0
          (xar->file->mode & ~AE_IFMT) | AE_IFIFO;
2752
0
    xar->file->has |= HAS_TYPE;
2753
0
    break;
2754
0
  case FILE_INODE:
2755
0
    xar->file->has |= HAS_INO;
2756
0
    xar->file->ino64 = atol10(s, len);
2757
0
    break;
2758
0
  case FILE_DEVICE_MAJOR:
2759
0
    xar->file->has |= HAS_DEVMAJOR;
2760
0
    xar->file->devmajor = (dev_t)atol10(s, len);
2761
0
    break;
2762
0
  case FILE_DEVICE_MINOR:
2763
0
    xar->file->has |= HAS_DEVMINOR;
2764
0
    xar->file->devminor = (dev_t)atol10(s, len);
2765
0
    break;
2766
0
  case FILE_DEVICENO:
2767
0
    xar->file->has |= HAS_DEV;
2768
0
    xar->file->dev = (dev_t)atol10(s, len);
2769
0
    break;
2770
0
  case FILE_MODE:
2771
0
    xar->file->has |= HAS_MODE;
2772
0
    xar->file->mode =
2773
0
        (xar->file->mode & AE_IFMT) |
2774
0
        ((mode_t)(atol8(s, len)) & ~AE_IFMT);
2775
0
    break;
2776
0
  case FILE_GROUP:
2777
0
    xar->file->has |= HAS_GID;
2778
0
    archive_strncpy(&(xar->file->gname), s, len);
2779
0
    break;
2780
0
  case FILE_GID:
2781
0
    xar->file->has |= HAS_GID;
2782
0
    xar->file->gid = atol10(s, len);
2783
0
    break;
2784
0
  case FILE_USER:
2785
0
    xar->file->has |= HAS_UID;
2786
0
    archive_strncpy(&(xar->file->uname), s, len);
2787
0
    break;
2788
0
  case FILE_UID:
2789
0
    xar->file->has |= HAS_UID;
2790
0
    xar->file->uid = atol10(s, len);
2791
0
    break;
2792
0
  case FILE_CTIME:
2793
0
    xar->file->has |= HAS_TIME | HAS_CTIME;
2794
0
    xar->file->ctime = parse_time(s, len);
2795
0
    break;
2796
0
  case FILE_MTIME:
2797
0
    xar->file->has |= HAS_TIME | HAS_MTIME;
2798
0
    xar->file->mtime = parse_time(s, len);
2799
0
    break;
2800
0
  case FILE_ATIME:
2801
0
    xar->file->has |= HAS_TIME | HAS_ATIME;
2802
0
    xar->file->atime = parse_time(s, len);
2803
0
    break;
2804
0
  case FILE_DATA_LENGTH:
2805
0
    xar->file->has |= HAS_DATA;
2806
0
    xar->file->length = atol10(s, len);
2807
0
    break;
2808
0
  case FILE_DATA_OFFSET:
2809
0
    xar->file->has |= HAS_DATA;
2810
0
    xar->file->offset = atol10(s, len);
2811
0
    break;
2812
0
  case FILE_DATA_SIZE:
2813
0
    xar->file->has |= HAS_DATA;
2814
0
    xar->file->size = atol10(s, len);
2815
0
    break;
2816
0
  case FILE_DATA_A_CHECKSUM:
2817
0
    xar->file->a_sum.len = atohex(xar->file->a_sum.val,
2818
0
        sizeof(xar->file->a_sum.val), s, len);
2819
0
    break;
2820
0
  case FILE_DATA_E_CHECKSUM:
2821
0
    xar->file->e_sum.len = atohex(xar->file->e_sum.val,
2822
0
        sizeof(xar->file->e_sum.val), s, len);
2823
0
    break;
2824
0
  case FILE_EA_LENGTH:
2825
0
    xar->file->has |= HAS_XATTR;
2826
0
    xar->xattr->length = atol10(s, len);
2827
0
    break;
2828
0
  case FILE_EA_OFFSET:
2829
0
    xar->file->has |= HAS_XATTR;
2830
0
    xar->xattr->offset = atol10(s, len);
2831
0
    break;
2832
0
  case FILE_EA_SIZE:
2833
0
    xar->file->has |= HAS_XATTR;
2834
0
    xar->xattr->size = atol10(s, len);
2835
0
    break;
2836
0
  case FILE_EA_A_CHECKSUM:
2837
0
    xar->file->has |= HAS_XATTR;
2838
0
    xar->xattr->a_sum.len = atohex(xar->xattr->a_sum.val,
2839
0
        sizeof(xar->xattr->a_sum.val), s, len);
2840
0
    break;
2841
0
  case FILE_EA_E_CHECKSUM:
2842
0
    xar->file->has |= HAS_XATTR;
2843
0
    xar->xattr->e_sum.len = atohex(xar->xattr->e_sum.val,
2844
0
        sizeof(xar->xattr->e_sum.val), s, len);
2845
0
    break;
2846
0
  case FILE_EA_NAME:
2847
0
    xar->file->has |= HAS_XATTR;
2848
0
    archive_strncpy(&(xar->xattr->name), s, len);
2849
0
    break;
2850
0
  case FILE_EA_FSTYPE:
2851
0
    xar->file->has |= HAS_XATTR;
2852
0
    archive_strncpy(&(xar->xattr->fstype), s, len);
2853
0
    break;
2854
0
    break;
2855
0
  case FILE_ACL_DEFAULT:
2856
0
  case FILE_ACL_ACCESS:
2857
0
  case FILE_ACL_APPLEEXTENDED:
2858
0
    xar->file->has |= HAS_ACL;
2859
    /* TODO */
2860
0
    break;
2861
0
  case INIT:
2862
0
  case XAR:
2863
0
  case TOC:
2864
0
  case TOC_CREATION_TIME:
2865
0
  case TOC_CHECKSUM:
2866
0
  case TOC_CHECKSUM_OFFSET:
2867
0
  case TOC_CHECKSUM_SIZE:
2868
0
  case TOC_FILE:
2869
0
  case FILE_DATA:
2870
0
  case FILE_DATA_ENCODING:
2871
0
  case FILE_DATA_CONTENT:
2872
0
  case FILE_DEVICE:
2873
0
  case FILE_EA:
2874
0
  case FILE_EA_ENCODING:
2875
0
  case FILE_ACL:
2876
0
  case FILE_FLAGS:
2877
0
  case FILE_FLAGS_USER_NODUMP:
2878
0
  case FILE_FLAGS_USER_IMMUTABLE:
2879
0
  case FILE_FLAGS_USER_APPEND:
2880
0
  case FILE_FLAGS_USER_OPAQUE:
2881
0
  case FILE_FLAGS_USER_NOUNLINK:
2882
0
  case FILE_FLAGS_SYS_ARCHIVED:
2883
0
  case FILE_FLAGS_SYS_IMMUTABLE:
2884
0
  case FILE_FLAGS_SYS_APPEND:
2885
0
  case FILE_FLAGS_SYS_NOUNLINK:
2886
0
  case FILE_FLAGS_SYS_SNAPSHOT:
2887
0
  case FILE_EXT2:
2888
0
  case FILE_EXT2_SecureDeletion:
2889
0
  case FILE_EXT2_Undelete:
2890
0
  case FILE_EXT2_Compress:
2891
0
  case FILE_EXT2_Synchronous:
2892
0
  case FILE_EXT2_Immutable:
2893
0
  case FILE_EXT2_AppendOnly:
2894
0
  case FILE_EXT2_NoDump:
2895
0
  case FILE_EXT2_NoAtime:
2896
0
  case FILE_EXT2_CompDirty:
2897
0
  case FILE_EXT2_CompBlock:
2898
0
  case FILE_EXT2_NoCompBlock:
2899
0
  case FILE_EXT2_CompError:
2900
0
  case FILE_EXT2_BTree:
2901
0
  case FILE_EXT2_HashIndexed:
2902
0
  case FILE_EXT2_iMagic:
2903
0
  case FILE_EXT2_Journaled:
2904
0
  case FILE_EXT2_NoTail:
2905
0
  case FILE_EXT2_DirSync:
2906
0
  case FILE_EXT2_TopDir:
2907
0
  case FILE_EXT2_Reserved:
2908
0
  case UNKNOWN:
2909
0
    break;
2910
0
  }
2911
0
}
2912
2913
/*
2914
 * BSD file flags.
2915
 */
2916
static int
2917
xml_parse_file_flags(struct xar *xar, const char *name)
2918
0
{
2919
0
  const char *flag = NULL;
2920
2921
0
  if (strcmp(name, "UserNoDump") == 0) {
2922
0
    xar->xmlsts = FILE_FLAGS_USER_NODUMP;
2923
0
    flag = "nodump";
2924
0
  }
2925
0
  else if (strcmp(name, "UserImmutable") == 0) {
2926
0
    xar->xmlsts = FILE_FLAGS_USER_IMMUTABLE;
2927
0
    flag = "uimmutable";
2928
0
  }
2929
0
  else if (strcmp(name, "UserAppend") == 0) {
2930
0
    xar->xmlsts = FILE_FLAGS_USER_APPEND;
2931
0
    flag = "uappend";
2932
0
  }
2933
0
  else if (strcmp(name, "UserOpaque") == 0) {
2934
0
    xar->xmlsts = FILE_FLAGS_USER_OPAQUE;
2935
0
    flag = "opaque";
2936
0
  }
2937
0
  else if (strcmp(name, "UserNoUnlink") == 0) {
2938
0
    xar->xmlsts = FILE_FLAGS_USER_NOUNLINK;
2939
0
    flag = "nouunlink";
2940
0
  }
2941
0
  else if (strcmp(name, "SystemArchived") == 0) {
2942
0
    xar->xmlsts = FILE_FLAGS_SYS_ARCHIVED;
2943
0
    flag = "archived";
2944
0
  }
2945
0
  else if (strcmp(name, "SystemImmutable") == 0) {
2946
0
    xar->xmlsts = FILE_FLAGS_SYS_IMMUTABLE;
2947
0
    flag = "simmutable";
2948
0
  }
2949
0
  else if (strcmp(name, "SystemAppend") == 0) {
2950
0
    xar->xmlsts = FILE_FLAGS_SYS_APPEND;
2951
0
    flag = "sappend";
2952
0
  }
2953
0
  else if (strcmp(name, "SystemNoUnlink") == 0) {
2954
0
    xar->xmlsts = FILE_FLAGS_SYS_NOUNLINK;
2955
0
    flag = "nosunlink";
2956
0
  }
2957
0
  else if (strcmp(name, "SystemSnapshot") == 0) {
2958
0
    xar->xmlsts = FILE_FLAGS_SYS_SNAPSHOT;
2959
0
    flag = "snapshot";
2960
0
  }
2961
2962
0
  if (flag == NULL)
2963
0
    return (0);
2964
0
  xar->file->has |= HAS_FFLAGS;
2965
0
  if (archive_strlen(&(xar->file->fflags_text)) > 0)
2966
0
    archive_strappend_char(&(xar->file->fflags_text), ',');
2967
0
  archive_strcat(&(xar->file->fflags_text), flag);
2968
0
  return (1);
2969
0
}
2970
2971
/*
2972
 * Linux file flags.
2973
 */
2974
static int
2975
xml_parse_file_ext2(struct xar *xar, const char *name)
2976
0
{
2977
0
  const char *flag = NULL;
2978
2979
0
  if (strcmp(name, "SecureDeletion") == 0) {
2980
0
    xar->xmlsts = FILE_EXT2_SecureDeletion;
2981
0
    flag = "securedeletion";
2982
0
  }
2983
0
  else if (strcmp(name, "Undelete") == 0) {
2984
0
    xar->xmlsts = FILE_EXT2_Undelete;
2985
0
    flag = "nouunlink";
2986
0
  }
2987
0
  else if (strcmp(name, "Compress") == 0) {
2988
0
    xar->xmlsts = FILE_EXT2_Compress;
2989
0
    flag = "compress";
2990
0
  }
2991
0
  else if (strcmp(name, "Synchronous") == 0) {
2992
0
    xar->xmlsts = FILE_EXT2_Synchronous;
2993
0
    flag = "sync";
2994
0
  }
2995
0
  else if (strcmp(name, "Immutable") == 0) {
2996
0
    xar->xmlsts = FILE_EXT2_Immutable;
2997
0
    flag = "simmutable";
2998
0
  }
2999
0
  else if (strcmp(name, "AppendOnly") == 0) {
3000
0
    xar->xmlsts = FILE_EXT2_AppendOnly;
3001
0
    flag = "sappend";
3002
0
  }
3003
0
  else if (strcmp(name, "NoDump") == 0) {
3004
0
    xar->xmlsts = FILE_EXT2_NoDump;
3005
0
    flag = "nodump";
3006
0
  }
3007
0
  else if (strcmp(name, "NoAtime") == 0) {
3008
0
    xar->xmlsts = FILE_EXT2_NoAtime;
3009
0
    flag = "noatime";
3010
0
  }
3011
0
  else if (strcmp(name, "CompDirty") == 0) {
3012
0
    xar->xmlsts = FILE_EXT2_CompDirty;
3013
0
    flag = "compdirty";
3014
0
  }
3015
0
  else if (strcmp(name, "CompBlock") == 0) {
3016
0
    xar->xmlsts = FILE_EXT2_CompBlock;
3017
0
    flag = "comprblk";
3018
0
  }
3019
0
  else if (strcmp(name, "NoCompBlock") == 0) {
3020
0
    xar->xmlsts = FILE_EXT2_NoCompBlock;
3021
0
    flag = "nocomprblk";
3022
0
  }
3023
0
  else if (strcmp(name, "CompError") == 0) {
3024
0
    xar->xmlsts = FILE_EXT2_CompError;
3025
0
    flag = "comperr";
3026
0
  }
3027
0
  else if (strcmp(name, "BTree") == 0) {
3028
0
    xar->xmlsts = FILE_EXT2_BTree;
3029
0
    flag = "btree";
3030
0
  }
3031
0
  else if (strcmp(name, "HashIndexed") == 0) {
3032
0
    xar->xmlsts = FILE_EXT2_HashIndexed;
3033
0
    flag = "hashidx";
3034
0
  }
3035
0
  else if (strcmp(name, "iMagic") == 0) {
3036
0
    xar->xmlsts = FILE_EXT2_iMagic;
3037
0
    flag = "imagic";
3038
0
  }
3039
0
  else if (strcmp(name, "Journaled") == 0) {
3040
0
    xar->xmlsts = FILE_EXT2_Journaled;
3041
0
    flag = "journal";
3042
0
  }
3043
0
  else if (strcmp(name, "NoTail") == 0) {
3044
0
    xar->xmlsts = FILE_EXT2_NoTail;
3045
0
    flag = "notail";
3046
0
  }
3047
0
  else if (strcmp(name, "DirSync") == 0) {
3048
0
    xar->xmlsts = FILE_EXT2_DirSync;
3049
0
    flag = "dirsync";
3050
0
  }
3051
0
  else if (strcmp(name, "TopDir") == 0) {
3052
0
    xar->xmlsts = FILE_EXT2_TopDir;
3053
0
    flag = "topdir";
3054
0
  }
3055
0
  else if (strcmp(name, "Reserved") == 0) {
3056
0
    xar->xmlsts = FILE_EXT2_Reserved;
3057
0
    flag = "reserved";
3058
0
  }
3059
3060
0
  if (flag == NULL)
3061
0
    return (0);
3062
0
  if (archive_strlen(&(xar->file->fflags_text)) > 0)
3063
0
    archive_strappend_char(&(xar->file->fflags_text), ',');
3064
0
  archive_strcat(&(xar->file->fflags_text), flag);
3065
0
  return (1);
3066
0
}
3067
3068
#ifdef HAVE_LIBXML_XMLREADER_H
3069
3070
static int
3071
xml2_xmlattr_setup(struct archive_read *a,
3072
    struct xmlattr_list *list, xmlTextReaderPtr reader)
3073
0
{
3074
0
  struct xmlattr *attr;
3075
0
  int r;
3076
3077
0
  list->first = NULL;
3078
0
  list->last = &(list->first);
3079
0
  r = xmlTextReaderMoveToFirstAttribute(reader);
3080
0
  while (r == 1) {
3081
0
    attr = malloc(sizeof*(attr));
3082
0
    if (attr == NULL) {
3083
0
      archive_set_error(&a->archive, ENOMEM, "Out of memory");
3084
0
      return (ARCHIVE_FATAL);
3085
0
    }
3086
0
    attr->name = strdup(
3087
0
        (const char *)xmlTextReaderConstLocalName(reader));
3088
0
    if (attr->name == NULL) {
3089
0
      free(attr);
3090
0
      archive_set_error(&a->archive, ENOMEM, "Out of memory");
3091
0
      return (ARCHIVE_FATAL);
3092
0
    }
3093
0
    attr->value = strdup(
3094
0
        (const char *)xmlTextReaderConstValue(reader));
3095
0
    if (attr->value == NULL) {
3096
0
      free(attr->name);
3097
0
      free(attr);
3098
0
      archive_set_error(&a->archive, ENOMEM, "Out of memory");
3099
0
      return (ARCHIVE_FATAL);
3100
0
    }
3101
0
    attr->next = NULL;
3102
0
    *list->last = attr;
3103
0
    list->last = &(attr->next);
3104
0
    r = xmlTextReaderMoveToNextAttribute(reader);
3105
0
  }
3106
0
  return (r);
3107
0
}
3108
3109
static int
3110
xml2_read_cb(void *context, char *buffer, int len)
3111
695
{
3112
695
  struct archive_read *a;
3113
695
  struct xar *xar;
3114
695
  const void *d;
3115
695
  size_t outbytes;
3116
695
  size_t used = 0;
3117
695
  int r;
3118
3119
695
  a = (struct archive_read *)context;
3120
695
  xar = (struct xar *)(a->format->data);
3121
3122
695
  if (xar->toc_remaining <= 0)
3123
0
    return (0);
3124
695
  d = buffer;
3125
695
  outbytes = len;
3126
695
  r = rd_contents(a, &d, &outbytes, &used, xar->toc_remaining);
3127
695
  if (r != ARCHIVE_OK)
3128
1
    return (r);
3129
694
  __archive_read_consume(a, used);
3130
694
  xar->toc_remaining -= used;
3131
694
  xar->offset += used;
3132
694
  xar->toc_total += outbytes;
3133
694
  PRINT_TOC(buffer, len);
3134
3135
694
  return ((int)outbytes);
3136
695
}
3137
3138
static int
3139
xml2_close_cb(void *context)
3140
1
{
3141
3142
1
  (void)context; /* UNUSED */
3143
1
  return (0);
3144
1
}
3145
3146
static void
3147
xml2_error_hdr(void *arg, const char *msg, xmlParserSeverities severity,
3148
    xmlTextReaderLocatorPtr locator)
3149
1
{
3150
1
  struct archive_read *a;
3151
3152
1
  (void)locator; /* UNUSED */
3153
1
  a = (struct archive_read *)arg;
3154
1
  switch (severity) {
3155
0
  case XML_PARSER_SEVERITY_VALIDITY_WARNING:
3156
0
  case XML_PARSER_SEVERITY_WARNING:
3157
0
    archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3158
0
        "XML Parsing error: %s", msg);
3159
0
    break;
3160
0
  case XML_PARSER_SEVERITY_VALIDITY_ERROR:
3161
1
  case XML_PARSER_SEVERITY_ERROR:
3162
1
    archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3163
1
        "XML Parsing error: %s", msg);
3164
1
    break;
3165
1
  }
3166
1
}
3167
3168
static int
3169
xml2_read_toc(struct archive_read *a)
3170
1
{
3171
1
  xmlTextReaderPtr reader;
3172
1
  struct xmlattr_list list;
3173
1
  int r;
3174
3175
1
  reader = xmlReaderForIO(xml2_read_cb, xml2_close_cb, a, NULL, NULL, 0);
3176
1
  if (reader == NULL) {
3177
0
    archive_set_error(&a->archive, ENOMEM,
3178
0
        "Couldn't allocate memory for xml parser");
3179
0
    return (ARCHIVE_FATAL);
3180
0
  }
3181
1
  xmlTextReaderSetErrorHandler(reader, xml2_error_hdr, a);
3182
3183
1
  while ((r = xmlTextReaderRead(reader)) == 1) {
3184
0
    const char *name, *value;
3185
0
    int type, empty;
3186
3187
0
    type = xmlTextReaderNodeType(reader);
3188
0
    name = (const char *)xmlTextReaderConstLocalName(reader);
3189
0
    switch (type) {
3190
0
    case XML_READER_TYPE_ELEMENT:
3191
0
      empty = xmlTextReaderIsEmptyElement(reader);
3192
0
      r = xml2_xmlattr_setup(a, &list, reader);
3193
0
      if (r == ARCHIVE_OK)
3194
0
        r = xml_start(a, name, &list);
3195
0
      xmlattr_cleanup(&list);
3196
0
      if (r != ARCHIVE_OK) {
3197
0
        xmlFreeTextReader(reader);
3198
0
        xmlCleanupParser();
3199
0
        return (r);
3200
0
      }
3201
0
      if (empty)
3202
0
        xml_end(a, name);
3203
0
      break;
3204
0
    case XML_READER_TYPE_END_ELEMENT:
3205
0
      xml_end(a, name);
3206
0
      break;
3207
0
    case XML_READER_TYPE_TEXT:
3208
0
      value = (const char *)xmlTextReaderConstValue(reader);
3209
0
      xml_data(a, value, strlen(value));
3210
0
      break;
3211
0
    case XML_READER_TYPE_SIGNIFICANT_WHITESPACE:
3212
0
    default:
3213
0
      break;
3214
0
    }
3215
0
    if (r < 0)
3216
0
      break;
3217
0
  }
3218
1
  xmlFreeTextReader(reader);
3219
1
  xmlCleanupParser();
3220
3221
1
  return ((r == 0)?ARCHIVE_OK:ARCHIVE_FATAL);
3222
1
}
3223
3224
#elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
3225
3226
static int
3227
expat_xmlattr_setup(struct archive_read *a,
3228
    struct xmlattr_list *list, const XML_Char **atts)
3229
{
3230
  struct xmlattr *attr;
3231
  char *name, *value;
3232
3233
  list->first = NULL;
3234
  list->last = &(list->first);
3235
  if (atts == NULL)
3236
    return (ARCHIVE_OK);
3237
  while (atts[0] != NULL && atts[1] != NULL) {
3238
    attr = malloc(sizeof*(attr));
3239
    name = strdup(atts[0]);
3240
    value = strdup(atts[1]);
3241
    if (attr == NULL || name == NULL || value == NULL) {
3242
      archive_set_error(&a->archive, ENOMEM, "Out of memory");
3243
      free(attr);
3244
      free(name);
3245
      free(value);
3246
      return (ARCHIVE_FATAL);
3247
    }
3248
    attr->name = name;
3249
    attr->value = value;
3250
    attr->next = NULL;
3251
    *list->last = attr;
3252
    list->last = &(attr->next);
3253
    atts += 2;
3254
  }
3255
  return (ARCHIVE_OK);
3256
}
3257
3258
static void
3259
expat_start_cb(void *userData, const XML_Char *name, const XML_Char **atts)
3260
{
3261
  struct expat_userData *ud = (struct expat_userData *)userData;
3262
  struct archive_read *a = ud->archive;
3263
  struct xmlattr_list list;
3264
  int r;
3265
3266
  if (ud->state != ARCHIVE_OK)
3267
    return;
3268
3269
  r = expat_xmlattr_setup(a, &list, atts);
3270
  if (r == ARCHIVE_OK)
3271
    r = xml_start(a, (const char *)name, &list);
3272
  xmlattr_cleanup(&list);
3273
  ud->state = r;
3274
}
3275
3276
static void
3277
expat_end_cb(void *userData, const XML_Char *name)
3278
{
3279
  struct expat_userData *ud = (struct expat_userData *)userData;
3280
3281
  xml_end(ud->archive, (const char *)name);
3282
}
3283
3284
static void
3285
expat_data_cb(void *userData, const XML_Char *s, int len)
3286
{
3287
  struct expat_userData *ud = (struct expat_userData *)userData;
3288
3289
  xml_data(ud->archive, s, (size_t)len);
3290
}
3291
3292
static int
3293
expat_read_toc(struct archive_read *a)
3294
{
3295
  struct xar *xar;
3296
  XML_Parser parser;
3297
  struct expat_userData ud;
3298
3299
  ud.state = ARCHIVE_OK;
3300
  ud.archive = a;
3301
3302
  xar = (struct xar *)(a->format->data);
3303
3304
  /* Initialize XML Parser library. */
3305
  parser = XML_ParserCreate(NULL);
3306
  if (parser == NULL) {
3307
    archive_set_error(&a->archive, ENOMEM,
3308
        "Couldn't allocate memory for xml parser");
3309
    return (ARCHIVE_FATAL);
3310
  }
3311
  XML_SetUserData(parser, &ud);
3312
  XML_SetElementHandler(parser, expat_start_cb, expat_end_cb);
3313
  XML_SetCharacterDataHandler(parser, expat_data_cb);
3314
  xar->xmlsts = INIT;
3315
3316
  while (xar->toc_remaining && ud.state == ARCHIVE_OK) {
3317
    enum XML_Status xr;
3318
    const void *d;
3319
    size_t outbytes;
3320
    size_t used;
3321
    int r;
3322
3323
    d = NULL;
3324
    r = rd_contents(a, &d, &outbytes, &used, xar->toc_remaining);
3325
    if (r != ARCHIVE_OK) {
3326
      XML_ParserFree(parser);
3327
      return (r);
3328
    }
3329
    xar->toc_remaining -= used;
3330
    xar->offset += used;
3331
    xar->toc_total += outbytes;
3332
    PRINT_TOC(d, outbytes);
3333
3334
    xr = XML_Parse(parser, d, (int)outbytes, xar->toc_remaining == 0);
3335
    __archive_read_consume(a, used);
3336
    if (xr == XML_STATUS_ERROR) {
3337
      XML_ParserFree(parser);
3338
      archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3339
          "XML Parsing failed");
3340
      return (ARCHIVE_FATAL);
3341
    }
3342
  }
3343
  XML_ParserFree(parser);
3344
  return (ud.state);
3345
}
3346
#endif /* defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H) */
3347
3348
#endif /* Support xar format */