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