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