2 * Copyright (c) 2009 Michihiro NAKAJIMA
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
25 #include "archive_platform.h"
26 __FBSDID("$FreeBSD$");
34 #if HAVE_LIBXML_XMLREADER_H
35 #include <libxml/xmlreader.h>
52 #include "archive_digest_private.h"
53 #include "archive_endian.h"
54 #include "archive_entry.h"
55 #include "archive_entry_locale.h"
56 #include "archive_private.h"
57 #include "archive_read_private.h"
59 #if (!defined(HAVE_LIBXML_XMLREADER_H) && \
60 !defined(HAVE_BSDXML_H) && !defined(HAVE_EXPAT_H)) ||\
61 !defined(HAVE_ZLIB_H) || \
62 !defined(ARCHIVE_HAS_MD5) || !defined(ARCHIVE_HAS_SHA1)
64 * xar needs several external libraries.
65 * o libxml2 or expat --- XML parser
66 * o openssl or MD5/SHA1 hash function
72 archive_read_support_format_xar(struct archive *_a)
74 struct archive_read *a = (struct archive_read *)_a;
75 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
76 ARCHIVE_STATE_NEW, "archive_read_support_format_xar");
78 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
79 "Xar not supported on this platform");
80 return (ARCHIVE_WARN);
83 #else /* Support xar format */
86 /* #define DEBUG_PRINT_TOC 1 */
88 #define PRINT_TOC(d, outbytes) do { \
89 unsigned char *x = (unsigned char *)(uintptr_t)d; \
90 unsigned char c = x[outbytes-1]; \
91 x[outbytes - 1] = 0; \
92 fprintf(stderr, "%s", x); \
93 fprintf(stderr, "%c", c); \
94 x[outbytes - 1] = c; \
97 #define PRINT_TOC(d, outbytes)
100 #define HEADER_MAGIC 0x78617221
101 #define HEADER_SIZE 28
102 #define HEADER_VERSION 1
109 #define MAX_SUM_SIZE 20
122 unsigned char val[MAX_SUM_SIZE];
127 #ifdef ARCHIVE_HAS_MD5
128 archive_md5_ctx md5ctx;
130 #ifdef ARCHIVE_HAS_SHA1
131 archive_sha1_ctx sha1ctx;
137 struct archive_string name;
142 enum enctype encoding;
143 struct chksumval a_sum;
144 struct chksumval e_sum;
145 struct archive_string fstype;
149 struct xar_file *next;
150 struct xar_file *hdnext;
151 struct xar_file *parent;
155 #define HAS_DATA 0x00001
156 #define HAS_PATHNAME 0x00002
157 #define HAS_SYMLINK 0x00004
158 #define HAS_TIME 0x00008
159 #define HAS_UID 0x00010
160 #define HAS_GID 0x00020
161 #define HAS_MODE 0x00040
162 #define HAS_TYPE 0x00080
163 #define HAS_DEV 0x00100
164 #define HAS_DEVMAJOR 0x00200
165 #define HAS_DEVMINOR 0x00400
166 #define HAS_INO 0x00800
167 #define HAS_FFLAGS 0x01000
168 #define HAS_XATTR 0x02000
169 #define HAS_ACL 0x04000
175 enum enctype encoding;
176 struct chksumval a_sum;
177 struct chksumval e_sum;
178 struct archive_string pathname;
179 struct archive_string symlink;
183 struct archive_string uname;
185 struct archive_string gname;
192 struct archive_string fflags_text;
195 struct archive_string hardlink;
196 struct xattr *xattr_list;
204 struct xar_file *files;
208 struct xar_file **files;
227 FILE_DATA_A_CHECKSUM,
228 FILE_DATA_E_CHECKSUM,
258 FILE_ACL_APPLEEXTENDED,
259 /* BSD file flags. */
261 FILE_FLAGS_USER_NODUMP,
262 FILE_FLAGS_USER_IMMUTABLE,
263 FILE_FLAGS_USER_APPEND,
264 FILE_FLAGS_USER_OPAQUE,
265 FILE_FLAGS_USER_NOUNLINK,
266 FILE_FLAGS_SYS_ARCHIVED,
267 FILE_FLAGS_SYS_IMMUTABLE,
268 FILE_FLAGS_SYS_APPEND,
269 FILE_FLAGS_SYS_NOUNLINK,
270 FILE_FLAGS_SYS_SNAPSHOT,
271 /* Linux file flags. */
273 FILE_EXT2_SecureDeletion,
276 FILE_EXT2_Synchronous,
278 FILE_EXT2_AppendOnly,
283 FILE_EXT2_NoCompBlock,
286 FILE_EXT2_HashIndexed,
297 struct unknown_tag *next;
298 struct archive_string name;
302 uint64_t offset; /* Current position in the file. */
306 #define OUTBUFF_SIZE (1024 * 64)
307 unsigned char *outbuff;
309 enum xmlstatus xmlsts;
310 enum xmlstatus xmlsts_unknown;
311 struct unknown_tag *unknowntags;
317 uint64_t toc_remaining;
319 uint64_t toc_chksum_offset;
320 uint64_t toc_chksum_size;
325 enum enctype rd_encoding;
328 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
332 #if HAVE_LZMA_H && HAVE_LIBLZMA
333 lzma_stream lzstream;
339 struct chksumwork a_sumwrk;
340 struct chksumwork e_sumwrk;
342 struct xar_file *file; /* current reading file. */
343 struct xattr *xattr; /* current reading extended attribute. */
344 struct heap_queue file_queue;
345 struct xar_file *hdlink_orgs;
346 struct hdlink *hdlink_list;
349 uint64_t entry_total;
350 uint64_t entry_remaining;
351 size_t entry_unconsumed;
353 enum enctype entry_encoding;
354 struct chksumval entry_a_sum;
355 struct chksumval entry_e_sum;
357 struct archive_string_conv *sconv;
361 struct xmlattr *next;
366 struct xmlattr_list {
367 struct xmlattr *first;
368 struct xmlattr **last;
371 static int xar_bid(struct archive_read *, int);
372 static int xar_read_header(struct archive_read *,
373 struct archive_entry *);
374 static int xar_read_data(struct archive_read *,
375 const void **, size_t *, int64_t *);
376 static int xar_read_data_skip(struct archive_read *);
377 static int xar_cleanup(struct archive_read *);
378 static int move_reading_point(struct archive_read *, uint64_t);
379 static int rd_contents_init(struct archive_read *,
380 enum enctype, int, int);
381 static int rd_contents(struct archive_read *, const void **,
382 size_t *, size_t *, uint64_t);
383 static uint64_t atol10(const char *, size_t);
384 static int64_t atol8(const char *, size_t);
385 static size_t atohex(unsigned char *, size_t, const char *, size_t);
386 static time_t parse_time(const char *p, size_t n);
387 static int heap_add_entry(struct archive_read *a,
388 struct heap_queue *, struct xar_file *);
389 static struct xar_file *heap_get_entry(struct heap_queue *);
390 static int add_link(struct archive_read *,
391 struct xar *, struct xar_file *);
392 static void checksum_init(struct archive_read *, int, int);
393 static void checksum_update(struct archive_read *, const void *,
394 size_t, const void *, size_t);
395 static int checksum_final(struct archive_read *, const void *,
396 size_t, const void *, size_t);
397 static void checksum_cleanup(struct archive_read *);
398 static int decompression_init(struct archive_read *, enum enctype);
399 static int decompress(struct archive_read *, const void **,
400 size_t *, const void *, size_t *);
401 static int decompression_cleanup(struct archive_read *);
402 static void xmlattr_cleanup(struct xmlattr_list *);
403 static int file_new(struct archive_read *,
404 struct xar *, struct xmlattr_list *);
405 static void file_free(struct xar_file *);
406 static int xattr_new(struct archive_read *,
407 struct xar *, struct xmlattr_list *);
408 static void xattr_free(struct xattr *);
409 static int getencoding(struct xmlattr_list *);
410 static int getsumalgorithm(struct xmlattr_list *);
411 static int unknowntag_start(struct archive_read *,
412 struct xar *, const char *);
413 static void unknowntag_end(struct xar *, const char *);
414 static int xml_start(struct archive_read *,
415 const char *, struct xmlattr_list *);
416 static void xml_end(void *, const char *);
417 static void xml_data(void *, const char *, int);
418 static int xml_parse_file_flags(struct xar *, const char *);
419 static int xml_parse_file_ext2(struct xar *, const char *);
420 #if defined(HAVE_LIBXML_XMLREADER_H)
421 static int xml2_xmlattr_setup(struct archive_read *,
422 struct xmlattr_list *, xmlTextReaderPtr);
423 static int xml2_read_cb(void *, char *, int);
424 static int xml2_close_cb(void *);
425 static void xml2_error_hdr(void *, const char *, xmlParserSeverities,
426 xmlTextReaderLocatorPtr);
427 static int xml2_read_toc(struct archive_read *);
428 #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
429 struct expat_userData {
431 struct archive_read *archive;
433 static int expat_xmlattr_setup(struct archive_read *,
434 struct xmlattr_list *, const XML_Char **);
435 static void expat_start_cb(void *, const XML_Char *, const XML_Char **);
436 static void expat_end_cb(void *, const XML_Char *);
437 static void expat_data_cb(void *, const XML_Char *, int);
438 static int expat_read_toc(struct archive_read *);
442 archive_read_support_format_xar(struct archive *_a)
445 struct archive_read *a = (struct archive_read *)_a;
448 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
449 ARCHIVE_STATE_NEW, "archive_read_support_format_xar");
451 xar = (struct xar *)calloc(1, sizeof(*xar));
453 archive_set_error(&a->archive, ENOMEM,
454 "Can't allocate xar data");
455 return (ARCHIVE_FATAL);
458 r = __archive_read_register_format(a,
476 xar_bid(struct archive_read *a, int best_bid)
478 const unsigned char *b;
481 (void)best_bid; /* UNUSED */
483 b = __archive_read_ahead(a, HEADER_SIZE, NULL);
491 if (archive_be32dec(b) != HEADER_MAGIC)
497 if (archive_be16dec(b+4) != HEADER_SIZE)
501 * Verify header version
503 if (archive_be16dec(b+6) != HEADER_VERSION)
507 * Verify type of checksum
509 switch (archive_be32dec(b+24)) {
523 read_toc(struct archive_read *a)
526 struct xar_file *file;
527 const unsigned char *b;
528 uint64_t toc_compressed_size;
529 uint64_t toc_uncompressed_size;
530 uint32_t toc_chksum_alg;
534 xar = (struct xar *)(a->format->data);
539 b = __archive_read_ahead(a, HEADER_SIZE, &bytes);
542 if (bytes < HEADER_SIZE) {
543 archive_set_error(&a->archive,
544 ARCHIVE_ERRNO_FILE_FORMAT,
545 "Truncated archive header");
546 return (ARCHIVE_FATAL);
549 if (archive_be32dec(b) != HEADER_MAGIC) {
550 archive_set_error(&a->archive,
551 ARCHIVE_ERRNO_FILE_FORMAT,
552 "Invalid header magic");
553 return (ARCHIVE_FATAL);
555 if (archive_be16dec(b+6) != HEADER_VERSION) {
556 archive_set_error(&a->archive,
557 ARCHIVE_ERRNO_FILE_FORMAT,
558 "Unsupported header version(%d)",
559 archive_be16dec(b+6));
560 return (ARCHIVE_FATAL);
562 toc_compressed_size = archive_be64dec(b+8);
563 xar->toc_remaining = toc_compressed_size;
564 toc_uncompressed_size = archive_be64dec(b+16);
565 toc_chksum_alg = archive_be32dec(b+24);
566 __archive_read_consume(a, HEADER_SIZE);
567 xar->offset += HEADER_SIZE;
571 * Read TOC(Table of Contents).
573 /* Initialize reading contents. */
574 r = move_reading_point(a, HEADER_SIZE);
577 r = rd_contents_init(a, GZIP, toc_chksum_alg, CKSUM_NONE);
581 #ifdef HAVE_LIBXML_XMLREADER_H
582 r = xml2_read_toc(a);
583 #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
584 r = expat_read_toc(a);
589 /* Set 'The HEAP' base. */
590 xar->h_base = xar->offset;
591 if (xar->toc_total != toc_uncompressed_size) {
592 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
593 "TOC uncompressed size error");
594 return (ARCHIVE_FATAL);
600 if (toc_chksum_alg != CKSUM_NONE) {
601 r = move_reading_point(a, xar->toc_chksum_offset);
604 b = __archive_read_ahead(a,
605 (size_t)xar->toc_chksum_size, &bytes);
608 if ((uint64_t)bytes < xar->toc_chksum_size) {
609 archive_set_error(&a->archive,
610 ARCHIVE_ERRNO_FILE_FORMAT,
611 "Truncated archive file");
612 return (ARCHIVE_FATAL);
614 r = checksum_final(a, b,
615 (size_t)xar->toc_chksum_size, NULL, 0);
616 __archive_read_consume(a, xar->toc_chksum_size);
617 xar->offset += xar->toc_chksum_size;
619 return (ARCHIVE_FATAL);
623 * Connect hardlinked files.
625 for (file = xar->hdlink_orgs; file != NULL; file = file->hdnext) {
626 struct hdlink **hdlink;
628 for (hdlink = &(xar->hdlink_list); *hdlink != NULL;
629 hdlink = &((*hdlink)->next)) {
630 if ((*hdlink)->id == file->id) {
631 struct hdlink *hltmp;
633 int nlink = (*hdlink)->cnt + 1;
636 for (f2 = (*hdlink)->files; f2 != NULL;
640 &(f2->hardlink), &(file->pathname));
642 /* Remove resolved files from hdlist_list. */
644 *hdlink = hltmp->next;
650 a->archive.archive_format = ARCHIVE_FORMAT_XAR;
651 a->archive.archive_format_name = "xar";
657 xar_read_header(struct archive_read *a, struct archive_entry *entry)
660 struct xar_file *file;
664 xar = (struct xar *)(a->format->data);
667 if (xar->offset == 0) {
668 /* Create a character conversion object. */
669 if (xar->sconv == NULL) {
670 xar->sconv = archive_string_conversion_from_charset(
671 &(a->archive), "UTF-8", 1);
672 if (xar->sconv == NULL)
673 return (ARCHIVE_FATAL);
683 file = xar->file = heap_get_entry(&(xar->file_queue));
685 xar->end_of_file = 1;
686 return (ARCHIVE_EOF);
688 if ((file->mode & AE_IFMT) != AE_IFDIR)
690 if (file->has != (HAS_PATHNAME | HAS_TYPE))
693 * If a file type is a directory and it does not have
694 * any metadata, do not export.
698 archive_entry_set_atime(entry, file->atime, 0);
699 archive_entry_set_ctime(entry, file->ctime, 0);
700 archive_entry_set_mtime(entry, file->mtime, 0);
701 archive_entry_set_gid(entry, file->gid);
702 if (file->gname.length > 0 &&
703 archive_entry_copy_gname_l(entry, file->gname.s,
704 archive_strlen(&(file->gname)), xar->sconv) != 0) {
705 if (errno == ENOMEM) {
706 archive_set_error(&a->archive, ENOMEM,
707 "Can't allocate memory for Gname");
708 return (ARCHIVE_FATAL);
710 archive_set_error(&a->archive,
711 ARCHIVE_ERRNO_FILE_FORMAT,
712 "Gname cannot be converted from %s to current locale.",
713 archive_string_conversion_charset_name(xar->sconv));
716 archive_entry_set_uid(entry, file->uid);
717 if (file->uname.length > 0 &&
718 archive_entry_copy_uname_l(entry, file->uname.s,
719 archive_strlen(&(file->uname)), xar->sconv) != 0) {
720 if (errno == ENOMEM) {
721 archive_set_error(&a->archive, ENOMEM,
722 "Can't allocate memory for Uname");
723 return (ARCHIVE_FATAL);
725 archive_set_error(&a->archive,
726 ARCHIVE_ERRNO_FILE_FORMAT,
727 "Uname cannot be converted from %s to current locale.",
728 archive_string_conversion_charset_name(xar->sconv));
731 archive_entry_set_mode(entry, file->mode);
732 if (archive_entry_copy_pathname_l(entry, file->pathname.s,
733 archive_strlen(&(file->pathname)), xar->sconv) != 0) {
734 if (errno == ENOMEM) {
735 archive_set_error(&a->archive, ENOMEM,
736 "Can't allocate memory for Pathname");
737 return (ARCHIVE_FATAL);
739 archive_set_error(&a->archive,
740 ARCHIVE_ERRNO_FILE_FORMAT,
741 "Pathname cannot be converted from %s to current locale.",
742 archive_string_conversion_charset_name(xar->sconv));
747 if (file->symlink.length > 0 &&
748 archive_entry_copy_symlink_l(entry, file->symlink.s,
749 archive_strlen(&(file->symlink)), xar->sconv) != 0) {
750 if (errno == ENOMEM) {
751 archive_set_error(&a->archive, ENOMEM,
752 "Can't allocate memory for Linkname");
753 return (ARCHIVE_FATAL);
755 archive_set_error(&a->archive,
756 ARCHIVE_ERRNO_FILE_FORMAT,
757 "Linkname cannot be converted from %s to current locale.",
758 archive_string_conversion_charset_name(xar->sconv));
761 /* Set proper nlink. */
762 if ((file->mode & AE_IFMT) == AE_IFDIR)
763 archive_entry_set_nlink(entry, file->subdirs + 2);
765 archive_entry_set_nlink(entry, file->nlink);
766 archive_entry_set_size(entry, file->size);
767 if (archive_strlen(&(file->hardlink)) > 0)
768 archive_entry_set_hardlink(entry, file->hardlink.s);
769 archive_entry_set_ino64(entry, file->ino64);
770 if (file->has & HAS_DEV)
771 archive_entry_set_dev(entry, file->dev);
772 if (file->has & HAS_DEVMAJOR)
773 archive_entry_set_devmajor(entry, file->devmajor);
774 if (file->has & HAS_DEVMINOR)
775 archive_entry_set_devminor(entry, file->devminor);
776 if (archive_strlen(&(file->fflags_text)) > 0)
777 archive_entry_copy_fflags_text(entry, file->fflags_text.s);
780 xar->entry_total = 0;
781 xar->entry_remaining = file->length;
782 xar->entry_size = file->size;
783 xar->entry_encoding = file->encoding;
784 xar->entry_a_sum = file->a_sum;
785 xar->entry_e_sum = file->e_sum;
787 * Read extended attributes.
789 xattr = file->xattr_list;
790 while (xattr != NULL) {
792 size_t outbytes, used;
794 r = move_reading_point(a, xattr->offset);
797 r = rd_contents_init(a, xattr->encoding,
798 xattr->a_sum.alg, xattr->e_sum.alg);
802 r = rd_contents(a, &d, &outbytes, &used, xattr->length);
805 if (outbytes != xattr->size) {
806 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
807 "Decompressed size error");
811 r = checksum_final(a,
812 xattr->a_sum.val, xattr->a_sum.len,
813 xattr->e_sum.val, xattr->e_sum.len);
816 archive_entry_xattr_add_entry(entry,
817 xattr->name.s, d, outbytes);
820 if (r != ARCHIVE_OK) {
825 if (xar->entry_remaining > 0)
826 /* Move reading point to the beginning of current
828 r = move_reading_point(a, file->offset);
837 xar_read_data(struct archive_read *a,
838 const void **buff, size_t *size, int64_t *offset)
844 xar = (struct xar *)(a->format->data);
846 if (xar->entry_unconsumed) {
847 __archive_read_consume(a, xar->entry_unconsumed);
848 xar->entry_unconsumed = 0;
851 if (xar->end_of_file || xar->entry_remaining <= 0) {
853 goto abort_read_data;
856 if (xar->entry_init) {
857 r = rd_contents_init(a, xar->entry_encoding,
858 xar->entry_a_sum.alg, xar->entry_e_sum.alg);
859 if (r != ARCHIVE_OK) {
860 xar->entry_remaining = 0;
867 r = rd_contents(a, buff, size, &used, xar->entry_remaining);
869 goto abort_read_data;
871 *offset = xar->entry_total;
872 xar->entry_total += *size;
875 xar->entry_remaining -= used;
876 xar->entry_unconsumed = used;
878 if (xar->entry_remaining == 0) {
879 if (xar->entry_total != xar->entry_size) {
880 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
881 "Decompressed size error");
883 goto abort_read_data;
885 r = checksum_final(a,
886 xar->entry_a_sum.val, xar->entry_a_sum.len,
887 xar->entry_e_sum.val, xar->entry_e_sum.len);
889 goto abort_read_data;
896 *offset = xar->total;
901 xar_read_data_skip(struct archive_read *a)
904 int64_t bytes_skipped;
906 xar = (struct xar *)(a->format->data);
907 if (xar->end_of_file)
908 return (ARCHIVE_EOF);
909 bytes_skipped = __archive_read_consume(a, xar->entry_remaining +
910 xar->entry_unconsumed);
911 if (bytes_skipped < 0)
912 return (ARCHIVE_FATAL);
913 xar->offset += bytes_skipped;
914 xar->entry_unconsumed = 0;
919 xar_cleanup(struct archive_read *a)
922 struct hdlink *hdlink;
926 xar = (struct xar *)(a->format->data);
928 r = decompression_cleanup(a);
929 hdlink = xar->hdlink_list;
930 while (hdlink != NULL) {
931 struct hdlink *next = hdlink->next;
936 for (i = 0; i < xar->file_queue.used; i++)
937 file_free(xar->file_queue.files[i]);
938 free(xar->file_queue.files);
939 while (xar->unknowntags != NULL) {
940 struct unknown_tag *tag;
942 tag = xar->unknowntags;
943 xar->unknowntags = tag->next;
944 archive_string_free(&(tag->name));
949 a->format->data = NULL;
954 move_reading_point(struct archive_read *a, uint64_t offset)
958 xar = (struct xar *)(a->format->data);
959 if (xar->offset - xar->h_base != offset) {
960 /* Seek forward to the start of file contents. */
963 step = offset - (xar->offset - xar->h_base);
965 step = __archive_read_consume(a, step);
970 int64_t pos = __archive_read_seek(a, offset, SEEK_SET);
971 if (pos == ARCHIVE_FAILED) {
972 archive_set_error(&(a->archive),
975 return (ARCHIVE_FAILED);
984 rd_contents_init(struct archive_read *a, enum enctype encoding,
985 int a_sum_alg, int e_sum_alg)
989 /* Init decompress library. */
990 if ((r = decompression_init(a, encoding)) != ARCHIVE_OK)
992 /* Init checksum library. */
993 checksum_init(a, a_sum_alg, e_sum_alg);
998 rd_contents(struct archive_read *a, const void **buff, size_t *size,
999 size_t *used, uint64_t remaining)
1001 const unsigned char *b;
1004 /* Get whatever bytes are immediately available. */
1005 b = __archive_read_ahead(a, 1, &bytes);
1007 return ((int)bytes);
1009 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1010 "Truncated archive file");
1011 return (ARCHIVE_FATAL);
1013 if ((uint64_t)bytes > remaining)
1014 bytes = (ssize_t)remaining;
1017 * Decompress contents of file.
1020 if (decompress(a, buff, size, b, used) != ARCHIVE_OK)
1021 return (ARCHIVE_FATAL);
1024 * Update checksum of a compressed data and a extracted data.
1026 checksum_update(a, b, *used, *buff, *size);
1028 return (ARCHIVE_OK);
1032 * Note that this implementation does not (and should not!) obey
1033 * locale settings; you cannot simply substitute strtol here, since
1034 * it does obey locale.
1038 atol10(const char *p, size_t char_cnt)
1045 while (digit >= 0 && digit < 10 && char_cnt-- > 0) {
1046 l = (l * 10) + digit;
1053 atol8(const char *p, size_t char_cnt)
1059 while (char_cnt-- > 0) {
1060 if (*p >= '0' && *p <= '7')
1072 atohex(unsigned char *b, size_t bsize, const char *p, size_t psize)
1074 size_t fbsize = bsize;
1076 while (bsize && psize > 1) {
1079 if (p[0] >= 'a' && p[0] <= 'z')
1080 x = (p[0] - 'a' + 0x0a) << 4;
1081 else if (p[0] >= 'A' && p[0] <= 'Z')
1082 x = (p[0] - 'A' + 0x0a) << 4;
1083 else if (p[0] >= '0' && p[0] <= '9')
1084 x = (p[0] - '0') << 4;
1087 if (p[1] >= 'a' && p[1] <= 'z')
1088 x |= p[1] - 'a' + 0x0a;
1089 else if (p[1] >= 'A' && p[1] <= 'Z')
1090 x |= p[1] - 'A' + 0x0a;
1091 else if (p[1] >= '0' && p[1] <= '9')
1101 return (fbsize - bsize);
1105 time_from_tm(struct tm *t)
1108 /* Use platform timegm() if available. */
1110 #elif HAVE__MKGMTIME64
1111 return (_mkgmtime64(t));
1113 /* Else use direct calculation using POSIX assumptions. */
1114 /* First, fix up tm_yday based on the year/month/day. */
1116 /* Then we can compute timegm() from first principles. */
1120 + t->tm_yday * 86400
1121 + (t->tm_year - 70) * 31536000
1122 + ((t->tm_year - 69) / 4) * 86400
1123 - ((t->tm_year - 1) / 100) * 86400
1124 + ((t->tm_year + 299) / 400) * 86400);
1129 parse_time(const char *p, size_t n)
1135 memset(&tm, 0, sizeof(tm));
1138 data = atol10(p, 4);
1141 tm.tm_year = (int)data - 1900;
1145 data = atol10(p, 2);
1146 if (data < 1 || data > 12)
1148 tm.tm_mon = (int)data -1;
1152 data = atol10(p, 2);
1153 if (data < 1 || data > 31)
1155 tm.tm_mday = (int)data;
1159 data = atol10(p, 2);
1160 if (data < 0 || data > 23)
1162 tm.tm_hour = (int)data;
1166 data = atol10(p, 2);
1167 if (data < 0 || data > 59)
1169 tm.tm_min = (int)data;
1173 data = atol10(p, 2);
1174 if (data < 0 || data > 60)
1176 tm.tm_sec = (int)data;
1183 t = time_from_tm(&tm);
1189 heap_add_entry(struct archive_read *a,
1190 struct heap_queue *heap, struct xar_file *file)
1192 uint64_t file_id, parent_id;
1195 /* Expand our pending files list as necessary. */
1196 if (heap->used >= heap->allocated) {
1197 struct xar_file **new_pending_files;
1198 int new_size = heap->allocated * 2;
1200 if (heap->allocated < 1024)
1202 /* Overflow might keep us from growing the list. */
1203 if (new_size <= heap->allocated) {
1204 archive_set_error(&a->archive,
1205 ENOMEM, "Out of memory");
1206 return (ARCHIVE_FATAL);
1208 new_pending_files = (struct xar_file **)
1209 malloc(new_size * sizeof(new_pending_files[0]));
1210 if (new_pending_files == NULL) {
1211 archive_set_error(&a->archive,
1212 ENOMEM, "Out of memory");
1213 return (ARCHIVE_FATAL);
1215 memcpy(new_pending_files, heap->files,
1216 heap->allocated * sizeof(new_pending_files[0]));
1217 if (heap->files != NULL)
1219 heap->files = new_pending_files;
1220 heap->allocated = new_size;
1226 * Start with hole at end, walk it up tree to find insertion point.
1228 hole = heap->used++;
1230 parent = (hole - 1)/2;
1231 parent_id = heap->files[parent]->id;
1232 if (file_id >= parent_id) {
1233 heap->files[hole] = file;
1234 return (ARCHIVE_OK);
1236 /* Move parent into hole <==> move hole up tree. */
1237 heap->files[hole] = heap->files[parent];
1240 heap->files[0] = file;
1242 return (ARCHIVE_OK);
1245 static struct xar_file *
1246 heap_get_entry(struct heap_queue *heap)
1248 uint64_t a_id, b_id, c_id;
1250 struct xar_file *r, *tmp;
1256 * The first file in the list is the earliest; we'll return this.
1261 * Move the last item in the heap to the root of the tree
1263 heap->files[0] = heap->files[--(heap->used)];
1266 * Rebalance the heap.
1268 a = 0; /* Starting element and its heap key */
1269 a_id = heap->files[a]->id;
1271 b = a + a + 1; /* First child */
1272 if (b >= heap->used)
1274 b_id = heap->files[b]->id;
1275 c = b + 1; /* Use second child if it is smaller. */
1276 if (c < heap->used) {
1277 c_id = heap->files[c]->id;
1285 tmp = heap->files[a];
1286 heap->files[a] = heap->files[b];
1287 heap->files[b] = tmp;
1293 add_link(struct archive_read *a, struct xar *xar, struct xar_file *file)
1295 struct hdlink *hdlink;
1297 for (hdlink = xar->hdlink_list; hdlink != NULL; hdlink = hdlink->next) {
1298 if (hdlink->id == file->link) {
1299 file->hdnext = hdlink->files;
1301 hdlink->files = file;
1302 return (ARCHIVE_OK);
1305 hdlink = malloc(sizeof(*hdlink));
1306 if (hdlink == NULL) {
1307 archive_set_error(&a->archive, ENOMEM, "Out of memory");
1308 return (ARCHIVE_FATAL);
1310 file->hdnext = NULL;
1311 hdlink->id = file->link;
1313 hdlink->files = file;
1314 hdlink->next = xar->hdlink_list;
1315 xar->hdlink_list = hdlink;
1316 return (ARCHIVE_OK);
1320 _checksum_init(struct chksumwork *sumwrk, int sum_alg)
1322 sumwrk->alg = sum_alg;
1327 archive_sha1_init(&(sumwrk->sha1ctx));
1330 archive_md5_init(&(sumwrk->md5ctx));
1336 _checksum_update(struct chksumwork *sumwrk, const void *buff, size_t size)
1339 switch (sumwrk->alg) {
1343 archive_sha1_update(&(sumwrk->sha1ctx), buff, size);
1346 archive_md5_update(&(sumwrk->md5ctx), buff, size);
1352 _checksum_final(struct chksumwork *sumwrk, const void *val, size_t len)
1354 unsigned char sum[MAX_SUM_SIZE];
1357 switch (sumwrk->alg) {
1361 archive_sha1_final(&(sumwrk->sha1ctx), sum);
1362 if (len != SHA1_SIZE ||
1363 memcmp(val, sum, SHA1_SIZE) != 0)
1367 archive_md5_final(&(sumwrk->md5ctx), sum);
1368 if (len != MD5_SIZE ||
1369 memcmp(val, sum, MD5_SIZE) != 0)
1377 checksum_init(struct archive_read *a, int a_sum_alg, int e_sum_alg)
1381 xar = (struct xar *)(a->format->data);
1382 _checksum_init(&(xar->a_sumwrk), a_sum_alg);
1383 _checksum_init(&(xar->e_sumwrk), e_sum_alg);
1387 checksum_update(struct archive_read *a, const void *abuff, size_t asize,
1388 const void *ebuff, size_t esize)
1392 xar = (struct xar *)(a->format->data);
1393 _checksum_update(&(xar->a_sumwrk), abuff, asize);
1394 _checksum_update(&(xar->e_sumwrk), ebuff, esize);
1398 checksum_final(struct archive_read *a, const void *a_sum_val,
1399 size_t a_sum_len, const void *e_sum_val, size_t e_sum_len)
1404 xar = (struct xar *)(a->format->data);
1405 r = _checksum_final(&(xar->a_sumwrk), a_sum_val, a_sum_len);
1406 if (r == ARCHIVE_OK)
1407 r = _checksum_final(&(xar->e_sumwrk), e_sum_val, e_sum_len);
1408 if (r != ARCHIVE_OK)
1409 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
1415 decompression_init(struct archive_read *a, enum enctype encoding)
1421 xar = (struct xar *)(a->format->data);
1422 xar->rd_encoding = encoding;
1427 if (xar->stream_valid)
1428 r = inflateReset(&(xar->stream));
1430 r = inflateInit(&(xar->stream));
1432 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1433 "Couldn't initialize zlib stream.");
1434 return (ARCHIVE_FATAL);
1436 xar->stream_valid = 1;
1437 xar->stream.total_in = 0;
1438 xar->stream.total_out = 0;
1440 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1442 if (xar->bzstream_valid) {
1443 BZ2_bzDecompressEnd(&(xar->bzstream));
1444 xar->bzstream_valid = 0;
1446 r = BZ2_bzDecompressInit(&(xar->bzstream), 0, 0);
1447 if (r == BZ_MEM_ERROR)
1448 r = BZ2_bzDecompressInit(&(xar->bzstream), 0, 1);
1450 int err = ARCHIVE_ERRNO_MISC;
1453 case BZ_PARAM_ERROR:
1454 detail = "invalid setup parameter";
1458 detail = "out of memory";
1460 case BZ_CONFIG_ERROR:
1461 detail = "mis-compiled library";
1464 archive_set_error(&a->archive, err,
1465 "Internal error initializing decompressor: %s",
1466 detail == NULL ? "??" : detail);
1467 xar->bzstream_valid = 0;
1468 return (ARCHIVE_FATAL);
1470 xar->bzstream_valid = 1;
1471 xar->bzstream.total_in_lo32 = 0;
1472 xar->bzstream.total_in_hi32 = 0;
1473 xar->bzstream.total_out_lo32 = 0;
1474 xar->bzstream.total_out_hi32 = 0;
1477 #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1478 #if LZMA_VERSION_MAJOR >= 5
1479 /* Effectively disable the limiter. */
1480 #define LZMA_MEMLIMIT UINT64_MAX
1482 /* NOTE: This needs to check memory size which running system has. */
1483 #define LZMA_MEMLIMIT (1U << 30)
1487 if (xar->lzstream_valid) {
1488 lzma_end(&(xar->lzstream));
1489 xar->lzstream_valid = 0;
1491 if (xar->entry_encoding == XZ)
1492 r = lzma_stream_decoder(&(xar->lzstream),
1493 LZMA_MEMLIMIT,/* memlimit */
1496 r = lzma_alone_decoder(&(xar->lzstream),
1497 LZMA_MEMLIMIT);/* memlimit */
1500 case LZMA_MEM_ERROR:
1501 archive_set_error(&a->archive,
1503 "Internal error initializing "
1504 "compression library: "
1505 "Cannot allocate memory");
1507 case LZMA_OPTIONS_ERROR:
1508 archive_set_error(&a->archive,
1510 "Internal error initializing "
1511 "compression library: "
1512 "Invalid or unsupported options");
1515 archive_set_error(&a->archive,
1517 "Internal error initializing "
1521 return (ARCHIVE_FATAL);
1523 xar->lzstream_valid = 1;
1524 xar->lzstream.total_in = 0;
1525 xar->lzstream.total_out = 0;
1529 * Unsupported compression.
1532 #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR)
1535 #if !defined(HAVE_LZMA_H) || !defined(HAVE_LIBLZMA)
1539 switch (xar->entry_encoding) {
1540 case BZIP2: detail = "bzip2"; break;
1541 case LZMA: detail = "lzma"; break;
1542 case XZ: detail = "xz"; break;
1543 default: detail = "??"; break;
1545 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1546 "%s compression not supported on this platform",
1548 return (ARCHIVE_FAILED);
1550 return (ARCHIVE_OK);
1554 decompress(struct archive_read *a, const void **buff, size_t *outbytes,
1555 const void *b, size_t *used)
1559 size_t avail_in, avail_out;
1562 xar = (struct xar *)(a->format->data);
1564 outbuff = (void *)(uintptr_t)*buff;
1565 if (outbuff == NULL) {
1566 if (xar->outbuff == NULL) {
1567 xar->outbuff = malloc(OUTBUFF_SIZE);
1568 if (xar->outbuff == NULL) {
1569 archive_set_error(&a->archive, ENOMEM,
1570 "Couldn't allocate memory for out buffer");
1571 return (ARCHIVE_FATAL);
1574 outbuff = xar->outbuff;
1576 avail_out = OUTBUFF_SIZE;
1578 avail_out = *outbytes;
1579 switch (xar->rd_encoding) {
1581 xar->stream.next_in = (Bytef *)(uintptr_t)b;
1582 xar->stream.avail_in = avail_in;
1583 xar->stream.next_out = (unsigned char *)outbuff;
1584 xar->stream.avail_out = avail_out;
1585 r = inflate(&(xar->stream), 0);
1587 case Z_OK: /* Decompressor made some progress.*/
1588 case Z_STREAM_END: /* Found end of stream. */
1591 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1592 "File decompression failed (%d)", r);
1593 return (ARCHIVE_FATAL);
1595 *used = avail_in - xar->stream.avail_in;
1596 *outbytes = avail_out - xar->stream.avail_out;
1598 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1600 xar->bzstream.next_in = (char *)(uintptr_t)b;
1601 xar->bzstream.avail_in = avail_in;
1602 xar->bzstream.next_out = (char *)outbuff;
1603 xar->bzstream.avail_out = avail_out;
1604 r = BZ2_bzDecompress(&(xar->bzstream));
1606 case BZ_STREAM_END: /* Found end of stream. */
1607 switch (BZ2_bzDecompressEnd(&(xar->bzstream))) {
1611 archive_set_error(&(a->archive),
1613 "Failed to clean up decompressor");
1614 return (ARCHIVE_FATAL);
1616 xar->bzstream_valid = 0;
1618 case BZ_OK: /* Decompressor made some progress. */
1621 archive_set_error(&(a->archive),
1623 "bzip decompression failed");
1624 return (ARCHIVE_FATAL);
1626 *used = avail_in - xar->bzstream.avail_in;
1627 *outbytes = avail_out - xar->bzstream.avail_out;
1630 #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1633 xar->lzstream.next_in = b;
1634 xar->lzstream.avail_in = avail_in;
1635 xar->lzstream.next_out = (unsigned char *)outbuff;
1636 xar->lzstream.avail_out = avail_out;
1637 r = lzma_code(&(xar->lzstream), LZMA_RUN);
1639 case LZMA_STREAM_END: /* Found end of stream. */
1640 lzma_end(&(xar->lzstream));
1641 xar->lzstream_valid = 0;
1643 case LZMA_OK: /* Decompressor made some progress. */
1646 archive_set_error(&(a->archive),
1648 "%s decompression failed(%d)",
1649 (xar->entry_encoding == XZ)?"xz":"lzma",
1651 return (ARCHIVE_FATAL);
1653 *used = avail_in - xar->lzstream.avail_in;
1654 *outbytes = avail_out - xar->lzstream.avail_out;
1657 #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR)
1660 #if !defined(HAVE_LZMA_H) || !defined(HAVE_LIBLZMA)
1666 if (outbuff == xar->outbuff) {
1669 *outbytes = avail_in;
1671 if (avail_out > avail_in)
1672 avail_out = avail_in;
1673 memcpy(outbuff, b, avail_out);
1675 *outbytes = avail_out;
1679 return (ARCHIVE_OK);
1683 decompression_cleanup(struct archive_read *a)
1688 xar = (struct xar *)(a->format->data);
1690 if (xar->stream_valid) {
1691 if (inflateEnd(&(xar->stream)) != Z_OK) {
1692 archive_set_error(&a->archive,
1694 "Failed to clean up zlib decompressor");
1698 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1699 if (xar->bzstream_valid) {
1700 if (BZ2_bzDecompressEnd(&(xar->bzstream)) != BZ_OK) {
1701 archive_set_error(&a->archive,
1703 "Failed to clean up bzip2 decompressor");
1708 #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1709 if (xar->lzstream_valid)
1710 lzma_end(&(xar->lzstream));
1711 #elif defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1712 if (xar->lzstream_valid) {
1713 if (lzmadec_end(&(xar->lzstream)) != LZMADEC_OK) {
1714 archive_set_error(&a->archive,
1716 "Failed to clean up lzmadec decompressor");
1725 checksum_cleanup(struct archive_read *a) {
1728 xar = (struct xar *)(a->format->data);
1730 _checksum_final(&(xar->a_sumwrk), NULL, 0);
1731 _checksum_final(&(xar->e_sumwrk), NULL, 0);
1735 xmlattr_cleanup(struct xmlattr_list *list)
1737 struct xmlattr *attr, *next;
1740 while (attr != NULL) {
1748 list->last = &(list->first);
1752 file_new(struct archive_read *a, struct xar *xar, struct xmlattr_list *list)
1754 struct xar_file *file;
1755 struct xmlattr *attr;
1757 file = calloc(1, sizeof(*file));
1759 archive_set_error(&a->archive, ENOMEM, "Out of memory");
1760 return (ARCHIVE_FATAL);
1762 file->parent = xar->file;
1763 file->mode = 0777 | AE_IFREG;
1764 file->atime = time(NULL);
1765 file->mtime = time(NULL);
1768 for (attr = list->first; attr != NULL; attr = attr->next) {
1769 if (strcmp(attr->name, "id") == 0)
1770 file->id = atol10(attr->value, strlen(attr->value));
1773 if (heap_add_entry(a, &(xar->file_queue), file) != ARCHIVE_OK)
1774 return (ARCHIVE_FATAL);
1775 return (ARCHIVE_OK);
1779 file_free(struct xar_file *file)
1781 struct xattr *xattr;
1783 archive_string_free(&(file->pathname));
1784 archive_string_free(&(file->symlink));
1785 archive_string_free(&(file->uname));
1786 archive_string_free(&(file->gname));
1787 archive_string_free(&(file->hardlink));
1788 xattr = file->xattr_list;
1789 while (xattr != NULL) {
1801 xattr_new(struct archive_read *a, struct xar *xar, struct xmlattr_list *list)
1803 struct xattr *xattr, **nx;
1804 struct xmlattr *attr;
1806 xattr = calloc(1, sizeof(*xattr));
1807 if (xattr == NULL) {
1808 archive_set_error(&a->archive, ENOMEM, "Out of memory");
1809 return (ARCHIVE_FATAL);
1812 for (attr = list->first; attr != NULL; attr = attr->next) {
1813 if (strcmp(attr->name, "id") == 0)
1814 xattr->id = atol10(attr->value, strlen(attr->value));
1816 /* Chain to xattr list. */
1817 for (nx = &(xar->file->xattr_list);
1818 *nx != NULL; nx = &((*nx)->next)) {
1819 if (xattr->id < (*nx)->id)
1825 return (ARCHIVE_OK);
1829 xattr_free(struct xattr *xattr)
1831 archive_string_free(&(xattr->name));
1836 getencoding(struct xmlattr_list *list)
1838 struct xmlattr *attr;
1839 enum enctype encoding = NONE;
1841 for (attr = list->first; attr != NULL; attr = attr->next) {
1842 if (strcmp(attr->name, "style") == 0) {
1843 if (strcmp(attr->value, "application/octet-stream") == 0)
1845 else if (strcmp(attr->value, "application/x-gzip") == 0)
1847 else if (strcmp(attr->value, "application/x-bzip2") == 0)
1849 else if (strcmp(attr->value, "application/x-lzma") == 0)
1851 else if (strcmp(attr->value, "application/x-xz") == 0)
1859 getsumalgorithm(struct xmlattr_list *list)
1861 struct xmlattr *attr;
1862 int alg = CKSUM_NONE;
1864 for (attr = list->first; attr != NULL; attr = attr->next) {
1865 if (strcmp(attr->name, "style") == 0) {
1866 const char *v = attr->value;
1867 if ((v[0] == 'S' || v[0] == 's') &&
1868 (v[1] == 'H' || v[1] == 'h') &&
1869 (v[2] == 'A' || v[2] == 'a') &&
1870 v[3] == '1' && v[4] == '\0')
1872 if ((v[0] == 'M' || v[0] == 'm') &&
1873 (v[1] == 'D' || v[1] == 'd') &&
1874 v[2] == '5' && v[3] == '\0')
1882 unknowntag_start(struct archive_read *a, struct xar *xar, const char *name)
1884 struct unknown_tag *tag;
1886 tag = malloc(sizeof(*tag));
1888 archive_set_error(&a->archive, ENOMEM, "Out of memory");
1889 return (ARCHIVE_FATAL);
1891 tag->next = xar->unknowntags;
1892 archive_string_init(&(tag->name));
1893 archive_strcpy(&(tag->name), name);
1894 if (xar->unknowntags == NULL) {
1896 fprintf(stderr, "UNKNOWNTAG_START:%s\n", name);
1898 xar->xmlsts_unknown = xar->xmlsts;
1899 xar->xmlsts = UNKNOWN;
1901 xar->unknowntags = tag;
1902 return (ARCHIVE_OK);
1906 unknowntag_end(struct xar *xar, const char *name)
1908 struct unknown_tag *tag;
1910 tag = xar->unknowntags;
1911 if (tag == NULL || name == NULL)
1913 if (strcmp(tag->name.s, name) == 0) {
1914 xar->unknowntags = tag->next;
1915 archive_string_free(&(tag->name));
1917 if (xar->unknowntags == NULL) {
1919 fprintf(stderr, "UNKNOWNTAG_END:%s\n", name);
1921 xar->xmlsts = xar->xmlsts_unknown;
1927 xml_start(struct archive_read *a, const char *name, struct xmlattr_list *list)
1930 struct xmlattr *attr;
1932 xar = (struct xar *)(a->format->data);
1935 fprintf(stderr, "xml_sta:[%s]\n", name);
1936 for (attr = list->first; attr != NULL; attr = attr->next)
1937 fprintf(stderr, " attr:\"%s\"=\"%s\"\n",
1938 attr->name, attr->value);
1940 xar->base64text = 0;
1941 switch (xar->xmlsts) {
1943 if (strcmp(name, "xar") == 0)
1946 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
1947 return (ARCHIVE_FATAL);
1950 if (strcmp(name, "toc") == 0)
1953 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
1954 return (ARCHIVE_FATAL);
1957 if (strcmp(name, "creation-time") == 0)
1958 xar->xmlsts = TOC_CREATION_TIME;
1959 else if (strcmp(name, "checksum") == 0)
1960 xar->xmlsts = TOC_CHECKSUM;
1961 else if (strcmp(name, "file") == 0) {
1962 if (file_new(a, xar, list) != ARCHIVE_OK)
1963 return (ARCHIVE_FATAL);
1964 xar->xmlsts = TOC_FILE;
1967 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
1968 return (ARCHIVE_FATAL);
1971 if (strcmp(name, "offset") == 0)
1972 xar->xmlsts = TOC_CHECKSUM_OFFSET;
1973 else if (strcmp(name, "size") == 0)
1974 xar->xmlsts = TOC_CHECKSUM_SIZE;
1976 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
1977 return (ARCHIVE_FATAL);
1980 if (strcmp(name, "file") == 0) {
1981 if (file_new(a, xar, list) != ARCHIVE_OK)
1982 return (ARCHIVE_FATAL);
1984 else if (strcmp(name, "data") == 0)
1985 xar->xmlsts = FILE_DATA;
1986 else if (strcmp(name, "ea") == 0) {
1987 if (xattr_new(a, xar, list) != ARCHIVE_OK)
1988 return (ARCHIVE_FATAL);
1989 xar->xmlsts = FILE_EA;
1991 else if (strcmp(name, "ctime") == 0)
1992 xar->xmlsts = FILE_CTIME;
1993 else if (strcmp(name, "mtime") == 0)
1994 xar->xmlsts = FILE_MTIME;
1995 else if (strcmp(name, "atime") == 0)
1996 xar->xmlsts = FILE_ATIME;
1997 else if (strcmp(name, "group") == 0)
1998 xar->xmlsts = FILE_GROUP;
1999 else if (strcmp(name, "gid") == 0)
2000 xar->xmlsts = FILE_GID;
2001 else if (strcmp(name, "user") == 0)
2002 xar->xmlsts = FILE_USER;
2003 else if (strcmp(name, "uid") == 0)
2004 xar->xmlsts = FILE_UID;
2005 else if (strcmp(name, "mode") == 0)
2006 xar->xmlsts = FILE_MODE;
2007 else if (strcmp(name, "device") == 0)
2008 xar->xmlsts = FILE_DEVICE;
2009 else if (strcmp(name, "deviceno") == 0)
2010 xar->xmlsts = FILE_DEVICENO;
2011 else if (strcmp(name, "inode") == 0)
2012 xar->xmlsts = FILE_INODE;
2013 else if (strcmp(name, "link") == 0)
2014 xar->xmlsts = FILE_LINK;
2015 else if (strcmp(name, "type") == 0) {
2016 xar->xmlsts = FILE_TYPE;
2017 for (attr = list->first; attr != NULL;
2018 attr = attr->next) {
2019 if (strcmp(attr->name, "link") != 0)
2021 if (strcmp(attr->value, "original") == 0) {
2022 xar->file->hdnext = xar->hdlink_orgs;
2023 xar->hdlink_orgs = xar->file;
2025 xar->file->link = (unsigned)atol10(attr->value,
2026 strlen(attr->value));
2027 if (xar->file->link > 0)
2028 if (add_link(a, xar, xar->file) != ARCHIVE_OK) {
2029 return (ARCHIVE_FATAL);
2034 else if (strcmp(name, "name") == 0) {
2035 xar->xmlsts = FILE_NAME;
2036 for (attr = list->first; attr != NULL;
2037 attr = attr->next) {
2038 if (strcmp(attr->name, "enctype") == 0 &&
2039 strcmp(attr->value, "base64") == 0)
2040 xar->base64text = 1;
2043 else if (strcmp(name, "acl") == 0)
2044 xar->xmlsts = FILE_ACL;
2045 else if (strcmp(name, "flags") == 0)
2046 xar->xmlsts = FILE_FLAGS;
2047 else if (strcmp(name, "ext2") == 0)
2048 xar->xmlsts = FILE_EXT2;
2050 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2051 return (ARCHIVE_FATAL);
2054 if (strcmp(name, "length") == 0)
2055 xar->xmlsts = FILE_DATA_LENGTH;
2056 else if (strcmp(name, "offset") == 0)
2057 xar->xmlsts = FILE_DATA_OFFSET;
2058 else if (strcmp(name, "size") == 0)
2059 xar->xmlsts = FILE_DATA_SIZE;
2060 else if (strcmp(name, "encoding") == 0) {
2061 xar->xmlsts = FILE_DATA_ENCODING;
2062 xar->file->encoding = getencoding(list);
2064 else if (strcmp(name, "archived-checksum") == 0) {
2065 xar->xmlsts = FILE_DATA_A_CHECKSUM;
2066 xar->file->a_sum.alg = getsumalgorithm(list);
2068 else if (strcmp(name, "extracted-checksum") == 0) {
2069 xar->xmlsts = FILE_DATA_E_CHECKSUM;
2070 xar->file->e_sum.alg = getsumalgorithm(list);
2072 else if (strcmp(name, "content") == 0)
2073 xar->xmlsts = FILE_DATA_CONTENT;
2075 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2076 return (ARCHIVE_FATAL);
2079 if (strcmp(name, "major") == 0)
2080 xar->xmlsts = FILE_DEVICE_MAJOR;
2081 else if (strcmp(name, "minor") == 0)
2082 xar->xmlsts = FILE_DEVICE_MINOR;
2084 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2085 return (ARCHIVE_FATAL);
2087 case FILE_DATA_CONTENT:
2088 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2089 return (ARCHIVE_FATAL);
2092 if (strcmp(name, "length") == 0)
2093 xar->xmlsts = FILE_EA_LENGTH;
2094 else if (strcmp(name, "offset") == 0)
2095 xar->xmlsts = FILE_EA_OFFSET;
2096 else if (strcmp(name, "size") == 0)
2097 xar->xmlsts = FILE_EA_SIZE;
2098 else if (strcmp(name, "encoding") == 0) {
2099 xar->xmlsts = FILE_EA_ENCODING;
2100 xar->xattr->encoding = getencoding(list);
2101 } else if (strcmp(name, "archived-checksum") == 0)
2102 xar->xmlsts = FILE_EA_A_CHECKSUM;
2103 else if (strcmp(name, "extracted-checksum") == 0)
2104 xar->xmlsts = FILE_EA_E_CHECKSUM;
2105 else if (strcmp(name, "name") == 0)
2106 xar->xmlsts = FILE_EA_NAME;
2107 else if (strcmp(name, "fstype") == 0)
2108 xar->xmlsts = FILE_EA_FSTYPE;
2110 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2111 return (ARCHIVE_FATAL);
2114 if (strcmp(name, "appleextended") == 0)
2115 xar->xmlsts = FILE_ACL_APPLEEXTENDED;
2116 else if (strcmp(name, "default") == 0)
2117 xar->xmlsts = FILE_ACL_DEFAULT;
2118 else if (strcmp(name, "access") == 0)
2119 xar->xmlsts = FILE_ACL_ACCESS;
2121 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2122 return (ARCHIVE_FATAL);
2125 if (!xml_parse_file_flags(xar, name))
2126 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2127 return (ARCHIVE_FATAL);
2130 if (!xml_parse_file_ext2(xar, name))
2131 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2132 return (ARCHIVE_FATAL);
2134 case TOC_CREATION_TIME:
2135 case TOC_CHECKSUM_OFFSET:
2136 case TOC_CHECKSUM_SIZE:
2137 case FILE_DATA_LENGTH:
2138 case FILE_DATA_OFFSET:
2139 case FILE_DATA_SIZE:
2140 case FILE_DATA_ENCODING:
2141 case FILE_DATA_A_CHECKSUM:
2142 case FILE_DATA_E_CHECKSUM:
2143 case FILE_EA_LENGTH:
2144 case FILE_EA_OFFSET:
2146 case FILE_EA_ENCODING:
2147 case FILE_EA_A_CHECKSUM:
2148 case FILE_EA_E_CHECKSUM:
2150 case FILE_EA_FSTYPE:
2159 case FILE_DEVICE_MAJOR:
2160 case FILE_DEVICE_MINOR:
2166 case FILE_ACL_DEFAULT:
2167 case FILE_ACL_ACCESS:
2168 case FILE_ACL_APPLEEXTENDED:
2169 case FILE_FLAGS_USER_NODUMP:
2170 case FILE_FLAGS_USER_IMMUTABLE:
2171 case FILE_FLAGS_USER_APPEND:
2172 case FILE_FLAGS_USER_OPAQUE:
2173 case FILE_FLAGS_USER_NOUNLINK:
2174 case FILE_FLAGS_SYS_ARCHIVED:
2175 case FILE_FLAGS_SYS_IMMUTABLE:
2176 case FILE_FLAGS_SYS_APPEND:
2177 case FILE_FLAGS_SYS_NOUNLINK:
2178 case FILE_FLAGS_SYS_SNAPSHOT:
2179 case FILE_EXT2_SecureDeletion:
2180 case FILE_EXT2_Undelete:
2181 case FILE_EXT2_Compress:
2182 case FILE_EXT2_Synchronous:
2183 case FILE_EXT2_Immutable:
2184 case FILE_EXT2_AppendOnly:
2185 case FILE_EXT2_NoDump:
2186 case FILE_EXT2_NoAtime:
2187 case FILE_EXT2_CompDirty:
2188 case FILE_EXT2_CompBlock:
2189 case FILE_EXT2_NoCompBlock:
2190 case FILE_EXT2_CompError:
2191 case FILE_EXT2_BTree:
2192 case FILE_EXT2_HashIndexed:
2193 case FILE_EXT2_iMagic:
2194 case FILE_EXT2_Journaled:
2195 case FILE_EXT2_NoTail:
2196 case FILE_EXT2_DirSync:
2197 case FILE_EXT2_TopDir:
2198 case FILE_EXT2_Reserved:
2200 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2201 return (ARCHIVE_FATAL);
2204 return (ARCHIVE_OK);
2208 xml_end(void *userData, const char *name)
2210 struct archive_read *a;
2213 a = (struct archive_read *)userData;
2214 xar = (struct xar *)(a->format->data);
2217 fprintf(stderr, "xml_end:[%s]\n", name);
2219 switch (xar->xmlsts) {
2223 if (strcmp(name, "xar") == 0)
2227 if (strcmp(name, "toc") == 0)
2230 case TOC_CREATION_TIME:
2231 if (strcmp(name, "creation-time") == 0)
2235 if (strcmp(name, "checksum") == 0)
2238 case TOC_CHECKSUM_OFFSET:
2239 if (strcmp(name, "offset") == 0)
2240 xar->xmlsts = TOC_CHECKSUM;
2242 case TOC_CHECKSUM_SIZE:
2243 if (strcmp(name, "size") == 0)
2244 xar->xmlsts = TOC_CHECKSUM;
2247 if (strcmp(name, "file") == 0) {
2248 if (xar->file->parent != NULL &&
2249 ((xar->file->mode & AE_IFMT) == AE_IFDIR))
2250 xar->file->parent->subdirs++;
2251 xar->file = xar->file->parent;
2252 if (xar->file == NULL)
2257 if (strcmp(name, "data") == 0)
2258 xar->xmlsts = TOC_FILE;
2260 case FILE_DATA_LENGTH:
2261 if (strcmp(name, "length") == 0)
2262 xar->xmlsts = FILE_DATA;
2264 case FILE_DATA_OFFSET:
2265 if (strcmp(name, "offset") == 0)
2266 xar->xmlsts = FILE_DATA;
2268 case FILE_DATA_SIZE:
2269 if (strcmp(name, "size") == 0)
2270 xar->xmlsts = FILE_DATA;
2272 case FILE_DATA_ENCODING:
2273 if (strcmp(name, "encoding") == 0)
2274 xar->xmlsts = FILE_DATA;
2276 case FILE_DATA_A_CHECKSUM:
2277 if (strcmp(name, "archived-checksum") == 0)
2278 xar->xmlsts = FILE_DATA;
2280 case FILE_DATA_E_CHECKSUM:
2281 if (strcmp(name, "extracted-checksum") == 0)
2282 xar->xmlsts = FILE_DATA;
2284 case FILE_DATA_CONTENT:
2285 if (strcmp(name, "content") == 0)
2286 xar->xmlsts = FILE_DATA;
2289 if (strcmp(name, "ea") == 0) {
2290 xar->xmlsts = TOC_FILE;
2294 case FILE_EA_LENGTH:
2295 if (strcmp(name, "length") == 0)
2296 xar->xmlsts = FILE_EA;
2298 case FILE_EA_OFFSET:
2299 if (strcmp(name, "offset") == 0)
2300 xar->xmlsts = FILE_EA;
2303 if (strcmp(name, "size") == 0)
2304 xar->xmlsts = FILE_EA;
2306 case FILE_EA_ENCODING:
2307 if (strcmp(name, "encoding") == 0)
2308 xar->xmlsts = FILE_EA;
2310 case FILE_EA_A_CHECKSUM:
2311 if (strcmp(name, "archived-checksum") == 0)
2312 xar->xmlsts = FILE_EA;
2314 case FILE_EA_E_CHECKSUM:
2315 if (strcmp(name, "extracted-checksum") == 0)
2316 xar->xmlsts = FILE_EA;
2319 if (strcmp(name, "name") == 0)
2320 xar->xmlsts = FILE_EA;
2322 case FILE_EA_FSTYPE:
2323 if (strcmp(name, "fstype") == 0)
2324 xar->xmlsts = FILE_EA;
2327 if (strcmp(name, "ctime") == 0)
2328 xar->xmlsts = TOC_FILE;
2331 if (strcmp(name, "mtime") == 0)
2332 xar->xmlsts = TOC_FILE;
2335 if (strcmp(name, "atime") == 0)
2336 xar->xmlsts = TOC_FILE;
2339 if (strcmp(name, "group") == 0)
2340 xar->xmlsts = TOC_FILE;
2343 if (strcmp(name, "gid") == 0)
2344 xar->xmlsts = TOC_FILE;
2347 if (strcmp(name, "user") == 0)
2348 xar->xmlsts = TOC_FILE;
2351 if (strcmp(name, "uid") == 0)
2352 xar->xmlsts = TOC_FILE;
2355 if (strcmp(name, "mode") == 0)
2356 xar->xmlsts = TOC_FILE;
2359 if (strcmp(name, "device") == 0)
2360 xar->xmlsts = TOC_FILE;
2362 case FILE_DEVICE_MAJOR:
2363 if (strcmp(name, "major") == 0)
2364 xar->xmlsts = FILE_DEVICE;
2366 case FILE_DEVICE_MINOR:
2367 if (strcmp(name, "minor") == 0)
2368 xar->xmlsts = FILE_DEVICE;
2371 if (strcmp(name, "deviceno") == 0)
2372 xar->xmlsts = TOC_FILE;
2375 if (strcmp(name, "inode") == 0)
2376 xar->xmlsts = TOC_FILE;
2379 if (strcmp(name, "link") == 0)
2380 xar->xmlsts = TOC_FILE;
2383 if (strcmp(name, "type") == 0)
2384 xar->xmlsts = TOC_FILE;
2387 if (strcmp(name, "name") == 0)
2388 xar->xmlsts = TOC_FILE;
2391 if (strcmp(name, "acl") == 0)
2392 xar->xmlsts = TOC_FILE;
2394 case FILE_ACL_DEFAULT:
2395 if (strcmp(name, "default") == 0)
2396 xar->xmlsts = FILE_ACL;
2398 case FILE_ACL_ACCESS:
2399 if (strcmp(name, "access") == 0)
2400 xar->xmlsts = FILE_ACL;
2402 case FILE_ACL_APPLEEXTENDED:
2403 if (strcmp(name, "appleextended") == 0)
2404 xar->xmlsts = FILE_ACL;
2407 if (strcmp(name, "flags") == 0)
2408 xar->xmlsts = TOC_FILE;
2410 case FILE_FLAGS_USER_NODUMP:
2411 if (strcmp(name, "UserNoDump") == 0)
2412 xar->xmlsts = FILE_FLAGS;
2414 case FILE_FLAGS_USER_IMMUTABLE:
2415 if (strcmp(name, "UserImmutable") == 0)
2416 xar->xmlsts = FILE_FLAGS;
2418 case FILE_FLAGS_USER_APPEND:
2419 if (strcmp(name, "UserAppend") == 0)
2420 xar->xmlsts = FILE_FLAGS;
2422 case FILE_FLAGS_USER_OPAQUE:
2423 if (strcmp(name, "UserOpaque") == 0)
2424 xar->xmlsts = FILE_FLAGS;
2426 case FILE_FLAGS_USER_NOUNLINK:
2427 if (strcmp(name, "UserNoUnlink") == 0)
2428 xar->xmlsts = FILE_FLAGS;
2430 case FILE_FLAGS_SYS_ARCHIVED:
2431 if (strcmp(name, "SystemArchived") == 0)
2432 xar->xmlsts = FILE_FLAGS;
2434 case FILE_FLAGS_SYS_IMMUTABLE:
2435 if (strcmp(name, "SystemImmutable") == 0)
2436 xar->xmlsts = FILE_FLAGS;
2438 case FILE_FLAGS_SYS_APPEND:
2439 if (strcmp(name, "SystemAppend") == 0)
2440 xar->xmlsts = FILE_FLAGS;
2442 case FILE_FLAGS_SYS_NOUNLINK:
2443 if (strcmp(name, "SystemNoUnlink") == 0)
2444 xar->xmlsts = FILE_FLAGS;
2446 case FILE_FLAGS_SYS_SNAPSHOT:
2447 if (strcmp(name, "SystemSnapshot") == 0)
2448 xar->xmlsts = FILE_FLAGS;
2451 if (strcmp(name, "ext2") == 0)
2452 xar->xmlsts = TOC_FILE;
2454 case FILE_EXT2_SecureDeletion:
2455 if (strcmp(name, "SecureDeletion") == 0)
2456 xar->xmlsts = FILE_EXT2;
2458 case FILE_EXT2_Undelete:
2459 if (strcmp(name, "Undelete") == 0)
2460 xar->xmlsts = FILE_EXT2;
2462 case FILE_EXT2_Compress:
2463 if (strcmp(name, "Compress") == 0)
2464 xar->xmlsts = FILE_EXT2;
2466 case FILE_EXT2_Synchronous:
2467 if (strcmp(name, "Synchronous") == 0)
2468 xar->xmlsts = FILE_EXT2;
2470 case FILE_EXT2_Immutable:
2471 if (strcmp(name, "Immutable") == 0)
2472 xar->xmlsts = FILE_EXT2;
2474 case FILE_EXT2_AppendOnly:
2475 if (strcmp(name, "AppendOnly") == 0)
2476 xar->xmlsts = FILE_EXT2;
2478 case FILE_EXT2_NoDump:
2479 if (strcmp(name, "NoDump") == 0)
2480 xar->xmlsts = FILE_EXT2;
2482 case FILE_EXT2_NoAtime:
2483 if (strcmp(name, "NoAtime") == 0)
2484 xar->xmlsts = FILE_EXT2;
2486 case FILE_EXT2_CompDirty:
2487 if (strcmp(name, "CompDirty") == 0)
2488 xar->xmlsts = FILE_EXT2;
2490 case FILE_EXT2_CompBlock:
2491 if (strcmp(name, "CompBlock") == 0)
2492 xar->xmlsts = FILE_EXT2;
2494 case FILE_EXT2_NoCompBlock:
2495 if (strcmp(name, "NoCompBlock") == 0)
2496 xar->xmlsts = FILE_EXT2;
2498 case FILE_EXT2_CompError:
2499 if (strcmp(name, "CompError") == 0)
2500 xar->xmlsts = FILE_EXT2;
2502 case FILE_EXT2_BTree:
2503 if (strcmp(name, "BTree") == 0)
2504 xar->xmlsts = FILE_EXT2;
2506 case FILE_EXT2_HashIndexed:
2507 if (strcmp(name, "HashIndexed") == 0)
2508 xar->xmlsts = FILE_EXT2;
2510 case FILE_EXT2_iMagic:
2511 if (strcmp(name, "iMagic") == 0)
2512 xar->xmlsts = FILE_EXT2;
2514 case FILE_EXT2_Journaled:
2515 if (strcmp(name, "Journaled") == 0)
2516 xar->xmlsts = FILE_EXT2;
2518 case FILE_EXT2_NoTail:
2519 if (strcmp(name, "NoTail") == 0)
2520 xar->xmlsts = FILE_EXT2;
2522 case FILE_EXT2_DirSync:
2523 if (strcmp(name, "DirSync") == 0)
2524 xar->xmlsts = FILE_EXT2;
2526 case FILE_EXT2_TopDir:
2527 if (strcmp(name, "TopDir") == 0)
2528 xar->xmlsts = FILE_EXT2;
2530 case FILE_EXT2_Reserved:
2531 if (strcmp(name, "Reserved") == 0)
2532 xar->xmlsts = FILE_EXT2;
2535 unknowntag_end(xar, name);
2540 static const int base64[256] = {
2541 -1, -1, -1, -1, -1, -1, -1, -1,
2542 -1, -1, -1, -1, -1, -1, -1, -1, /* 00 - 0F */
2543 -1, -1, -1, -1, -1, -1, -1, -1,
2544 -1, -1, -1, -1, -1, -1, -1, -1, /* 10 - 1F */
2545 -1, -1, -1, -1, -1, -1, -1, -1,
2546 -1, -1, -1, 62, -1, -1, -1, 63, /* 20 - 2F */
2547 52, 53, 54, 55, 56, 57, 58, 59,
2548 60, 61, -1, -1, -1, -1, -1, -1, /* 30 - 3F */
2549 -1, 0, 1, 2, 3, 4, 5, 6,
2550 7, 8, 9, 10, 11, 12, 13, 14, /* 40 - 4F */
2551 15, 16, 17, 18, 19, 20, 21, 22,
2552 23, 24, 25, -1, -1, -1, -1, -1, /* 50 - 5F */
2553 -1, 26, 27, 28, 29, 30, 31, 32,
2554 33, 34, 35, 36, 37, 38, 39, 40, /* 60 - 6F */
2555 41, 42, 43, 44, 45, 46, 47, 48,
2556 49, 50, 51, -1, -1, -1, -1, -1, /* 70 - 7F */
2557 -1, -1, -1, -1, -1, -1, -1, -1,
2558 -1, -1, -1, -1, -1, -1, -1, -1, /* 80 - 8F */
2559 -1, -1, -1, -1, -1, -1, -1, -1,
2560 -1, -1, -1, -1, -1, -1, -1, -1, /* 90 - 9F */
2561 -1, -1, -1, -1, -1, -1, -1, -1,
2562 -1, -1, -1, -1, -1, -1, -1, -1, /* A0 - AF */
2563 -1, -1, -1, -1, -1, -1, -1, -1,
2564 -1, -1, -1, -1, -1, -1, -1, -1, /* B0 - BF */
2565 -1, -1, -1, -1, -1, -1, -1, -1,
2566 -1, -1, -1, -1, -1, -1, -1, -1, /* C0 - CF */
2567 -1, -1, -1, -1, -1, -1, -1, -1,
2568 -1, -1, -1, -1, -1, -1, -1, -1, /* D0 - DF */
2569 -1, -1, -1, -1, -1, -1, -1, -1,
2570 -1, -1, -1, -1, -1, -1, -1, -1, /* E0 - EF */
2571 -1, -1, -1, -1, -1, -1, -1, -1,
2572 -1, -1, -1, -1, -1, -1, -1, -1, /* F0 - FF */
2576 strappend_base64(struct xar *xar,
2577 struct archive_string *as, const char *s, size_t l)
2579 unsigned char buff[256];
2581 const unsigned char *b;
2584 (void)xar; /* UNUSED */
2587 b = (const unsigned char *)s;
2592 if (base64[b[0]] < 0 || base64[b[1]] < 0)
2594 n = base64[*b++] << 18;
2595 n |= base64[*b++] << 12;
2603 n |= base64[*b++] << 6;
2604 *out++ = (n >> 8) & 0xFF;
2616 if (len+3 >= sizeof(buff)) {
2617 archive_strncat(as, (const char *)buff, len);
2623 archive_strncat(as, (const char *)buff, len);
2627 xml_data(void *userData, const char *s, int len)
2629 struct archive_read *a;
2632 a = (struct archive_read *)userData;
2633 xar = (struct xar *)(a->format->data);
2638 if (len > (int)(sizeof(buff)-1))
2639 len = (int)(sizeof(buff)-1);
2640 strncpy(buff, s, len);
2642 fprintf(stderr, "\tlen=%d:\"%s\"\n", len, buff);
2645 switch (xar->xmlsts) {
2646 case TOC_CHECKSUM_OFFSET:
2647 xar->toc_chksum_offset = atol10(s, len);
2649 case TOC_CHECKSUM_SIZE:
2650 xar->toc_chksum_size = atol10(s, len);
2655 if (xar->file == NULL)
2658 switch (xar->xmlsts) {
2660 if (xar->file->parent != NULL) {
2661 archive_string_concat(&(xar->file->pathname),
2662 &(xar->file->parent->pathname));
2663 archive_strappend_char(&(xar->file->pathname), '/');
2665 xar->file->has |= HAS_PATHNAME;
2666 if (xar->base64text) {
2667 strappend_base64(xar,
2668 &(xar->file->pathname), s, len);
2670 archive_strncat(&(xar->file->pathname), s, len);
2673 xar->file->has |= HAS_SYMLINK;
2674 archive_strncpy(&(xar->file->symlink), s, len);
2677 if (strncmp("file", s, len) == 0 ||
2678 strncmp("hardlink", s, len) == 0)
2680 (xar->file->mode & ~AE_IFMT) | AE_IFREG;
2681 if (strncmp("directory", s, len) == 0)
2683 (xar->file->mode & ~AE_IFMT) | AE_IFDIR;
2684 if (strncmp("symlink", s, len) == 0)
2686 (xar->file->mode & ~AE_IFMT) | AE_IFLNK;
2687 if (strncmp("character special", s, len) == 0)
2689 (xar->file->mode & ~AE_IFMT) | AE_IFCHR;
2690 if (strncmp("block special", s, len) == 0)
2692 (xar->file->mode & ~AE_IFMT) | AE_IFBLK;
2693 if (strncmp("socket", s, len) == 0)
2695 (xar->file->mode & ~AE_IFMT) | AE_IFSOCK;
2696 if (strncmp("fifo", s, len) == 0)
2698 (xar->file->mode & ~AE_IFMT) | AE_IFIFO;
2699 xar->file->has |= HAS_TYPE;
2702 xar->file->has |= HAS_INO;
2703 xar->file->ino64 = atol10(s, len);
2705 case FILE_DEVICE_MAJOR:
2706 xar->file->has |= HAS_DEVMAJOR;
2707 xar->file->devmajor = (dev_t)atol10(s, len);
2709 case FILE_DEVICE_MINOR:
2710 xar->file->has |= HAS_DEVMINOR;
2711 xar->file->devminor = (dev_t)atol10(s, len);
2714 xar->file->has |= HAS_DEV;
2715 xar->file->dev = (dev_t)atol10(s, len);
2718 xar->file->has |= HAS_MODE;
2720 (xar->file->mode & AE_IFMT) |
2721 ((mode_t)(atol8(s, len)) & ~AE_IFMT);
2724 xar->file->has |= HAS_GID;
2725 archive_strncpy(&(xar->file->gname), s, len);
2728 xar->file->has |= HAS_GID;
2729 xar->file->gid = atol10(s, len);
2732 xar->file->has |= HAS_UID;
2733 archive_strncpy(&(xar->file->uname), s, len);
2736 xar->file->has |= HAS_UID;
2737 xar->file->uid = atol10(s, len);
2740 xar->file->has |= HAS_TIME;
2741 xar->file->ctime = parse_time(s, len);
2744 xar->file->has |= HAS_TIME;
2745 xar->file->mtime = parse_time(s, len);
2748 xar->file->has |= HAS_TIME;
2749 xar->file->atime = parse_time(s, len);
2751 case FILE_DATA_LENGTH:
2752 xar->file->has |= HAS_DATA;
2753 xar->file->length = atol10(s, len);
2755 case FILE_DATA_OFFSET:
2756 xar->file->has |= HAS_DATA;
2757 xar->file->offset = atol10(s, len);
2759 case FILE_DATA_SIZE:
2760 xar->file->has |= HAS_DATA;
2761 xar->file->size = atol10(s, len);
2763 case FILE_DATA_A_CHECKSUM:
2764 xar->file->a_sum.len = atohex(xar->file->a_sum.val,
2765 sizeof(xar->file->a_sum.val), s, len);
2767 case FILE_DATA_E_CHECKSUM:
2768 xar->file->e_sum.len = atohex(xar->file->e_sum.val,
2769 sizeof(xar->file->e_sum.val), s, len);
2771 case FILE_EA_LENGTH:
2772 xar->file->has |= HAS_XATTR;
2773 xar->xattr->length = atol10(s, len);
2775 case FILE_EA_OFFSET:
2776 xar->file->has |= HAS_XATTR;
2777 xar->xattr->offset = atol10(s, len);
2780 xar->file->has |= HAS_XATTR;
2781 xar->xattr->size = atol10(s, len);
2783 case FILE_EA_A_CHECKSUM:
2784 xar->file->has |= HAS_XATTR;
2785 xar->xattr->a_sum.len = atohex(xar->xattr->a_sum.val,
2786 sizeof(xar->xattr->a_sum.val), s, len);
2788 case FILE_EA_E_CHECKSUM:
2789 xar->file->has |= HAS_XATTR;
2790 xar->xattr->e_sum.len = atohex(xar->xattr->e_sum.val,
2791 sizeof(xar->xattr->e_sum.val), s, len);
2794 xar->file->has |= HAS_XATTR;
2795 archive_strncpy(&(xar->xattr->name), s, len);
2797 case FILE_EA_FSTYPE:
2798 xar->file->has |= HAS_XATTR;
2799 archive_strncpy(&(xar->xattr->fstype), s, len);
2802 case FILE_ACL_DEFAULT:
2803 case FILE_ACL_ACCESS:
2804 case FILE_ACL_APPLEEXTENDED:
2805 xar->file->has |= HAS_ACL;
2811 case TOC_CREATION_TIME:
2813 case TOC_CHECKSUM_OFFSET:
2814 case TOC_CHECKSUM_SIZE:
2817 case FILE_DATA_ENCODING:
2818 case FILE_DATA_CONTENT:
2821 case FILE_EA_ENCODING:
2824 case FILE_FLAGS_USER_NODUMP:
2825 case FILE_FLAGS_USER_IMMUTABLE:
2826 case FILE_FLAGS_USER_APPEND:
2827 case FILE_FLAGS_USER_OPAQUE:
2828 case FILE_FLAGS_USER_NOUNLINK:
2829 case FILE_FLAGS_SYS_ARCHIVED:
2830 case FILE_FLAGS_SYS_IMMUTABLE:
2831 case FILE_FLAGS_SYS_APPEND:
2832 case FILE_FLAGS_SYS_NOUNLINK:
2833 case FILE_FLAGS_SYS_SNAPSHOT:
2835 case FILE_EXT2_SecureDeletion:
2836 case FILE_EXT2_Undelete:
2837 case FILE_EXT2_Compress:
2838 case FILE_EXT2_Synchronous:
2839 case FILE_EXT2_Immutable:
2840 case FILE_EXT2_AppendOnly:
2841 case FILE_EXT2_NoDump:
2842 case FILE_EXT2_NoAtime:
2843 case FILE_EXT2_CompDirty:
2844 case FILE_EXT2_CompBlock:
2845 case FILE_EXT2_NoCompBlock:
2846 case FILE_EXT2_CompError:
2847 case FILE_EXT2_BTree:
2848 case FILE_EXT2_HashIndexed:
2849 case FILE_EXT2_iMagic:
2850 case FILE_EXT2_Journaled:
2851 case FILE_EXT2_NoTail:
2852 case FILE_EXT2_DirSync:
2853 case FILE_EXT2_TopDir:
2854 case FILE_EXT2_Reserved:
2864 xml_parse_file_flags(struct xar *xar, const char *name)
2866 const char *flag = NULL;
2868 if (strcmp(name, "UserNoDump") == 0) {
2869 xar->xmlsts = FILE_FLAGS_USER_NODUMP;
2872 else if (strcmp(name, "UserImmutable") == 0) {
2873 xar->xmlsts = FILE_FLAGS_USER_IMMUTABLE;
2874 flag = "uimmutable";
2876 else if (strcmp(name, "UserAppend") == 0) {
2877 xar->xmlsts = FILE_FLAGS_USER_APPEND;
2880 else if (strcmp(name, "UserOpaque") == 0) {
2881 xar->xmlsts = FILE_FLAGS_USER_OPAQUE;
2884 else if (strcmp(name, "UserNoUnlink") == 0) {
2885 xar->xmlsts = FILE_FLAGS_USER_NOUNLINK;
2888 else if (strcmp(name, "SystemArchived") == 0) {
2889 xar->xmlsts = FILE_FLAGS_SYS_ARCHIVED;
2892 else if (strcmp(name, "SystemImmutable") == 0) {
2893 xar->xmlsts = FILE_FLAGS_SYS_IMMUTABLE;
2894 flag = "simmutable";
2896 else if (strcmp(name, "SystemAppend") == 0) {
2897 xar->xmlsts = FILE_FLAGS_SYS_APPEND;
2900 else if (strcmp(name, "SystemNoUnlink") == 0) {
2901 xar->xmlsts = FILE_FLAGS_SYS_NOUNLINK;
2904 else if (strcmp(name, "SystemSnapshot") == 0) {
2905 xar->xmlsts = FILE_FLAGS_SYS_SNAPSHOT;
2911 xar->file->has |= HAS_FFLAGS;
2912 if (archive_strlen(&(xar->file->fflags_text)) > 0)
2913 archive_strappend_char(&(xar->file->fflags_text), ',');
2914 archive_strcat(&(xar->file->fflags_text), flag);
2922 xml_parse_file_ext2(struct xar *xar, const char *name)
2924 const char *flag = NULL;
2926 if (strcmp(name, "SecureDeletion") == 0) {
2927 xar->xmlsts = FILE_EXT2_SecureDeletion;
2928 flag = "securedeletion";
2930 else if (strcmp(name, "Undelete") == 0) {
2931 xar->xmlsts = FILE_EXT2_Undelete;
2934 else if (strcmp(name, "Compress") == 0) {
2935 xar->xmlsts = FILE_EXT2_Compress;
2938 else if (strcmp(name, "Synchronous") == 0) {
2939 xar->xmlsts = FILE_EXT2_Synchronous;
2942 else if (strcmp(name, "Immutable") == 0) {
2943 xar->xmlsts = FILE_EXT2_Immutable;
2944 flag = "simmutable";
2946 else if (strcmp(name, "AppendOnly") == 0) {
2947 xar->xmlsts = FILE_EXT2_AppendOnly;
2950 else if (strcmp(name, "NoDump") == 0) {
2951 xar->xmlsts = FILE_EXT2_NoDump;
2954 else if (strcmp(name, "NoAtime") == 0) {
2955 xar->xmlsts = FILE_EXT2_NoAtime;
2958 else if (strcmp(name, "CompDirty") == 0) {
2959 xar->xmlsts = FILE_EXT2_CompDirty;
2962 else if (strcmp(name, "CompBlock") == 0) {
2963 xar->xmlsts = FILE_EXT2_CompBlock;
2966 else if (strcmp(name, "NoCompBlock") == 0) {
2967 xar->xmlsts = FILE_EXT2_NoCompBlock;
2968 flag = "nocomprblk";
2970 else if (strcmp(name, "CompError") == 0) {
2971 xar->xmlsts = FILE_EXT2_CompError;
2974 else if (strcmp(name, "BTree") == 0) {
2975 xar->xmlsts = FILE_EXT2_BTree;
2978 else if (strcmp(name, "HashIndexed") == 0) {
2979 xar->xmlsts = FILE_EXT2_HashIndexed;
2982 else if (strcmp(name, "iMagic") == 0) {
2983 xar->xmlsts = FILE_EXT2_iMagic;
2986 else if (strcmp(name, "Journaled") == 0) {
2987 xar->xmlsts = FILE_EXT2_Journaled;
2990 else if (strcmp(name, "NoTail") == 0) {
2991 xar->xmlsts = FILE_EXT2_NoTail;
2994 else if (strcmp(name, "DirSync") == 0) {
2995 xar->xmlsts = FILE_EXT2_DirSync;
2998 else if (strcmp(name, "TopDir") == 0) {
2999 xar->xmlsts = FILE_EXT2_TopDir;
3002 else if (strcmp(name, "Reserved") == 0) {
3003 xar->xmlsts = FILE_EXT2_Reserved;
3009 if (archive_strlen(&(xar->file->fflags_text)) > 0)
3010 archive_strappend_char(&(xar->file->fflags_text), ',');
3011 archive_strcat(&(xar->file->fflags_text), flag);
3015 #ifdef HAVE_LIBXML_XMLREADER_H
3018 xml2_xmlattr_setup(struct archive_read *a,
3019 struct xmlattr_list *list, xmlTextReaderPtr reader)
3021 struct xmlattr *attr;
3025 list->last = &(list->first);
3026 r = xmlTextReaderMoveToFirstAttribute(reader);
3028 attr = malloc(sizeof*(attr));
3030 archive_set_error(&a->archive, ENOMEM, "Out of memory");
3031 return (ARCHIVE_FATAL);
3033 attr->name = strdup(
3034 (const char *)xmlTextReaderConstLocalName(reader));
3035 if (attr->name == NULL) {
3037 archive_set_error(&a->archive, ENOMEM, "Out of memory");
3038 return (ARCHIVE_FATAL);
3040 attr->value = strdup(
3041 (const char *)xmlTextReaderConstValue(reader));
3042 if (attr->value == NULL) {
3045 archive_set_error(&a->archive, ENOMEM, "Out of memory");
3046 return (ARCHIVE_FATAL);
3050 list->last = &(attr->next);
3051 r = xmlTextReaderMoveToNextAttribute(reader);
3057 xml2_read_cb(void *context, char *buffer, int len)
3059 struct archive_read *a;
3066 a = (struct archive_read *)context;
3067 xar = (struct xar *)(a->format->data);
3069 if (xar->toc_remaining <= 0)
3073 r = rd_contents(a, &d, &outbytes, &used, xar->toc_remaining);
3074 if (r != ARCHIVE_OK)
3076 __archive_read_consume(a, used);
3077 xar->toc_remaining -= used;
3078 xar->offset += used;
3079 xar->toc_total += outbytes;
3080 PRINT_TOC(buffer, len);
3082 return ((int)outbytes);
3086 xml2_close_cb(void *context)
3089 (void)context; /* UNUSED */
3094 xml2_error_hdr(void *arg, const char *msg, xmlParserSeverities severity,
3095 xmlTextReaderLocatorPtr locator)
3097 struct archive_read *a;
3099 (void)locator; /* UNUSED */
3100 a = (struct archive_read *)arg;
3102 case XML_PARSER_SEVERITY_VALIDITY_WARNING:
3103 case XML_PARSER_SEVERITY_WARNING:
3104 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3105 "XML Parsing error: %s", msg);
3107 case XML_PARSER_SEVERITY_VALIDITY_ERROR:
3108 case XML_PARSER_SEVERITY_ERROR:
3109 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3110 "XML Parsing error: %s", msg);
3116 xml2_read_toc(struct archive_read *a)
3118 xmlTextReaderPtr reader;
3119 struct xmlattr_list list;
3122 reader = xmlReaderForIO(xml2_read_cb, xml2_close_cb, a, NULL, NULL, 0);
3123 if (reader == NULL) {
3124 archive_set_error(&a->archive, ENOMEM,
3125 "Couldn't allocate memory for xml parser");
3126 return (ARCHIVE_FATAL);
3128 xmlTextReaderSetErrorHandler(reader, xml2_error_hdr, a);
3130 while ((r = xmlTextReaderRead(reader)) == 1) {
3131 const char *name, *value;
3134 type = xmlTextReaderNodeType(reader);
3135 name = (const char *)xmlTextReaderConstLocalName(reader);
3137 case XML_READER_TYPE_ELEMENT:
3138 empty = xmlTextReaderIsEmptyElement(reader);
3139 r = xml2_xmlattr_setup(a, &list, reader);
3140 if (r == ARCHIVE_OK)
3141 r = xml_start(a, name, &list);
3142 xmlattr_cleanup(&list);
3143 if (r != ARCHIVE_OK)
3148 case XML_READER_TYPE_END_ELEMENT:
3151 case XML_READER_TYPE_TEXT:
3152 value = (const char *)xmlTextReaderConstValue(reader);
3153 xml_data(a, value, strlen(value));
3155 case XML_READER_TYPE_SIGNIFICANT_WHITESPACE:
3162 xmlFreeTextReader(reader);
3165 return ((r == 0)?ARCHIVE_OK:ARCHIVE_FATAL);
3168 #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
3171 expat_xmlattr_setup(struct archive_read *a,
3172 struct xmlattr_list *list, const XML_Char **atts)
3174 struct xmlattr *attr;
3178 list->last = &(list->first);
3180 return (ARCHIVE_OK);
3181 while (atts[0] != NULL && atts[1] != NULL) {
3182 attr = malloc(sizeof*(attr));
3183 name = strdup(atts[0]);
3184 value = strdup(atts[1]);
3185 if (attr == NULL || name == NULL || value == NULL) {
3186 archive_set_error(&a->archive, ENOMEM, "Out of memory");
3190 return (ARCHIVE_FATAL);
3193 attr->value = value;
3196 list->last = &(attr->next);
3199 return (ARCHIVE_OK);
3203 expat_start_cb(void *userData, const XML_Char *name, const XML_Char **atts)
3205 struct expat_userData *ud = (struct expat_userData *)userData;
3206 struct archive_read *a = ud->archive;
3207 struct xmlattr_list list;
3210 r = expat_xmlattr_setup(a, &list, atts);
3211 if (r == ARCHIVE_OK)
3212 r = xml_start(a, (const char *)name, &list);
3213 xmlattr_cleanup(&list);
3218 expat_end_cb(void *userData, const XML_Char *name)
3220 struct expat_userData *ud = (struct expat_userData *)userData;
3222 xml_end(ud->archive, (const char *)name);
3226 expat_data_cb(void *userData, const XML_Char *s, int len)
3228 struct expat_userData *ud = (struct expat_userData *)userData;
3230 xml_data(ud->archive, s, len);
3234 expat_read_toc(struct archive_read *a)
3238 struct expat_userData ud;
3240 ud.state = ARCHIVE_OK;
3243 xar = (struct xar *)(a->format->data);
3245 /* Initialize XML Parser library. */
3246 parser = XML_ParserCreate(NULL);
3247 if (parser == NULL) {
3248 archive_set_error(&a->archive, ENOMEM,
3249 "Couldn't allocate memory for xml parser");
3250 return (ARCHIVE_FATAL);
3252 XML_SetUserData(parser, &ud);
3253 XML_SetElementHandler(parser, expat_start_cb, expat_end_cb);
3254 XML_SetCharacterDataHandler(parser, expat_data_cb);
3257 while (xar->toc_remaining && ud.state == ARCHIVE_OK) {
3265 r = rd_contents(a, &d, &outbytes, &used, xar->toc_remaining);
3266 if (r != ARCHIVE_OK)
3268 xar->toc_remaining -= used;
3269 xar->offset += used;
3270 xar->toc_total += outbytes;
3271 PRINT_TOC(d, outbytes);
3273 xr = XML_Parse(parser, d, outbytes, xar->toc_remaining == 0);
3274 __archive_read_consume(a, used);
3275 if (xr == XML_STATUS_ERROR) {
3276 XML_ParserFree(parser);
3277 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3278 "XML Parsing failed");
3279 return (ARCHIVE_FATAL);
3282 XML_ParserFree(parser);
3285 #endif /* defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H) */
3287 #endif /* Support xar format */