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