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>
54 #include "archive_crypto_private.h"
55 #include "archive_endian.h"
56 #include "archive_entry.h"
57 #include "archive_entry_locale.h"
58 #include "archive_private.h"
59 #include "archive_read_private.h"
61 #if (!defined(HAVE_LIBXML_XMLREADER_H) && \
62 !defined(HAVE_BSDXML_H) && !defined(HAVE_EXPAT_H)) ||\
63 !defined(HAVE_ZLIB_H) || \
64 !defined(ARCHIVE_HAS_MD5) || !defined(ARCHIVE_HAS_SHA1)
66 * xar needs several external libraries.
67 * o libxml2 or expat --- XML parser
68 * o openssl or MD5/SHA1 hash function
74 archive_read_support_format_xar(struct archive *_a)
76 struct archive_read *a = (struct archive_read *)_a;
77 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
78 ARCHIVE_STATE_NEW, "archive_read_support_format_xar");
80 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
81 "Xar not supported on this platform");
82 return (ARCHIVE_WARN);
85 #else /* Support xar format */
88 /* #define DEBUG_PRINT_TOC 1 */
90 #define PRINT_TOC(d, outbytes) do { \
91 unsigned char *x = (unsigned char *)(uintptr_t)d; \
92 unsigned char c = x[outbytes-1]; \
93 x[outbytes - 1] = 0; \
94 fprintf(stderr, "%s", x); \
95 fprintf(stderr, "%c", c); \
96 x[outbytes - 1] = c; \
99 #define PRINT_TOC(d, outbytes)
102 #define HEADER_MAGIC 0x78617221
103 #define HEADER_SIZE 28
104 #define HEADER_VERSION 1
111 #define MAX_SUM_SIZE 20
124 unsigned char val[MAX_SUM_SIZE];
129 #ifdef ARCHIVE_HAS_MD5
130 archive_md5_ctx md5ctx;
132 #ifdef ARCHIVE_HAS_SHA1
133 archive_sha1_ctx sha1ctx;
139 struct archive_string name;
144 enum enctype encoding;
145 struct chksumval a_sum;
146 struct chksumval e_sum;
147 struct archive_string fstype;
151 struct xar_file *next;
152 struct xar_file *hdnext;
153 struct xar_file *parent;
157 #define HAS_DATA 0x00001
158 #define HAS_PATHNAME 0x00002
159 #define HAS_SYMLINK 0x00004
160 #define HAS_TIME 0x00008
161 #define HAS_UID 0x00010
162 #define HAS_GID 0x00020
163 #define HAS_MODE 0x00040
164 #define HAS_TYPE 0x00080
165 #define HAS_DEV 0x00100
166 #define HAS_DEVMAJOR 0x00200
167 #define HAS_DEVMINOR 0x00400
168 #define HAS_INO 0x00800
169 #define HAS_FFLAGS 0x01000
170 #define HAS_XATTR 0x02000
171 #define HAS_ACL 0x04000
177 enum enctype encoding;
178 struct chksumval a_sum;
179 struct chksumval e_sum;
180 struct archive_string pathname;
181 struct archive_string symlink;
185 struct archive_string uname;
187 struct archive_string gname;
194 struct archive_string fflags_text;
197 struct archive_string hardlink;
198 struct xattr *xattr_list;
206 struct xar_file *files;
210 struct xar_file **files;
229 FILE_DATA_A_CHECKSUM,
230 FILE_DATA_E_CHECKSUM,
260 FILE_ACL_APPLEEXTENDED,
261 /* BSD file flags. */
263 FILE_FLAGS_USER_NODUMP,
264 FILE_FLAGS_USER_IMMUTABLE,
265 FILE_FLAGS_USER_APPEND,
266 FILE_FLAGS_USER_OPAQUE,
267 FILE_FLAGS_USER_NOUNLINK,
268 FILE_FLAGS_SYS_ARCHIVED,
269 FILE_FLAGS_SYS_IMMUTABLE,
270 FILE_FLAGS_SYS_APPEND,
271 FILE_FLAGS_SYS_NOUNLINK,
272 FILE_FLAGS_SYS_SNAPSHOT,
273 /* Linux file flags. */
275 FILE_EXT2_SecureDeletion,
278 FILE_EXT2_Synchronous,
280 FILE_EXT2_AppendOnly,
285 FILE_EXT2_NoCompBlock,
288 FILE_EXT2_HashIndexed,
299 struct unknown_tag *next;
300 struct archive_string name;
304 uint64_t offset; /* Current position in the file. */
308 #define OUTBUFF_SIZE (1024 * 64)
309 unsigned char *outbuff;
311 enum xmlstatus xmlsts;
312 enum xmlstatus xmlsts_unknown;
313 struct unknown_tag *unknowntags;
319 uint64_t toc_remaining;
321 uint64_t toc_chksum_offset;
322 uint64_t toc_chksum_size;
327 enum enctype rd_encoding;
330 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
334 #if HAVE_LZMA_H && HAVE_LIBLZMA
335 lzma_stream lzstream;
337 #elif HAVE_LZMADEC_H && HAVE_LIBLZMADEC
338 lzmadec_stream lzstream;
344 struct chksumwork a_sumwrk;
345 struct chksumwork e_sumwrk;
347 struct xar_file *file; /* current reading file. */
348 struct xattr *xattr; /* current reading extended attribute. */
349 struct heap_queue file_queue;
350 struct xar_file *hdlink_orgs;
351 struct hdlink *hdlink_list;
354 uint64_t entry_total;
355 uint64_t entry_remaining;
356 size_t entry_unconsumed;
358 enum enctype entry_encoding;
359 struct chksumval entry_a_sum;
360 struct chksumval entry_e_sum;
362 struct archive_string_conv *sconv;
366 struct xmlattr *next;
371 struct xmlattr_list {
372 struct xmlattr *first;
373 struct xmlattr **last;
376 static int xar_bid(struct archive_read *, int);
377 static int xar_read_header(struct archive_read *,
378 struct archive_entry *);
379 static int xar_read_data(struct archive_read *,
380 const void **, size_t *, int64_t *);
381 static int xar_read_data_skip(struct archive_read *);
382 static int xar_cleanup(struct archive_read *);
383 static int move_reading_point(struct archive_read *, uint64_t);
384 static int rd_contents_init(struct archive_read *,
385 enum enctype, int, int);
386 static int rd_contents(struct archive_read *, const void **,
387 size_t *, size_t *, uint64_t);
388 static uint64_t atol10(const char *, size_t);
389 static int64_t atol8(const char *, size_t);
390 static size_t atohex(unsigned char *, size_t, const char *, size_t);
391 static time_t parse_time(const char *p, size_t n);
392 static int heap_add_entry(struct archive_read *a,
393 struct heap_queue *, struct xar_file *);
394 static struct xar_file *heap_get_entry(struct heap_queue *);
395 static int add_link(struct archive_read *,
396 struct xar *, struct xar_file *);
397 static void checksum_init(struct archive_read *, int, int);
398 static void checksum_update(struct archive_read *, const void *,
399 size_t, const void *, size_t);
400 static int checksum_final(struct archive_read *, const void *,
401 size_t, const void *, size_t);
402 static int decompression_init(struct archive_read *, enum enctype);
403 static int decompress(struct archive_read *, const void **,
404 size_t *, const void *, size_t *);
405 static int decompression_cleanup(struct archive_read *);
406 static void xmlattr_cleanup(struct xmlattr_list *);
407 static int file_new(struct archive_read *,
408 struct xar *, struct xmlattr_list *);
409 static void file_free(struct xar_file *);
410 static int xattr_new(struct archive_read *,
411 struct xar *, struct xmlattr_list *);
412 static void xattr_free(struct xattr *);
413 static int getencoding(struct xmlattr_list *);
414 static int getsumalgorithm(struct xmlattr_list *);
415 static int unknowntag_start(struct archive_read *,
416 struct xar *, const char *);
417 static void unknowntag_end(struct xar *, const char *);
418 static int xml_start(struct archive_read *,
419 const char *, struct xmlattr_list *);
420 static void xml_end(void *, const char *);
421 static void xml_data(void *, const char *, int);
422 static int xml_parse_file_flags(struct xar *, const char *);
423 static int xml_parse_file_ext2(struct xar *, const char *);
424 #if defined(HAVE_LIBXML_XMLREADER_H)
425 static int xml2_xmlattr_setup(struct archive_read *,
426 struct xmlattr_list *, xmlTextReaderPtr);
427 static int xml2_read_cb(void *, char *, int);
428 static int xml2_close_cb(void *);
429 static void xml2_error_hdr(void *, const char *, xmlParserSeverities,
430 xmlTextReaderLocatorPtr);
431 static int xml2_read_toc(struct archive_read *);
432 #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
433 struct expat_userData {
435 struct archive_read *archive;
437 static int expat_xmlattr_setup(struct archive_read *,
438 struct xmlattr_list *, const XML_Char **);
439 static void expat_start_cb(void *, const XML_Char *, const XML_Char **);
440 static void expat_end_cb(void *, const XML_Char *);
441 static void expat_data_cb(void *, const XML_Char *, int);
442 static int expat_read_toc(struct archive_read *);
446 archive_read_support_format_xar(struct archive *_a)
449 struct archive_read *a = (struct archive_read *)_a;
452 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
453 ARCHIVE_STATE_NEW, "archive_read_support_format_xar");
455 xar = (struct xar *)calloc(1, sizeof(*xar));
457 archive_set_error(&a->archive, ENOMEM,
458 "Can't allocate xar data");
459 return (ARCHIVE_FATAL);
462 r = __archive_read_register_format(a,
477 xar_bid(struct archive_read *a, int best_bid)
479 const unsigned char *b;
482 (void)best_bid; /* UNUSED */
484 b = __archive_read_ahead(a, HEADER_SIZE, NULL);
492 if (archive_be32dec(b) != HEADER_MAGIC)
498 if (archive_be16dec(b+4) != HEADER_SIZE)
502 * Verify header version
504 if (archive_be16dec(b+6) != HEADER_VERSION)
508 * Verify type of checksum
510 switch (archive_be32dec(b+24)) {
524 read_toc(struct archive_read *a)
527 struct xar_file *file;
528 const unsigned char *b;
529 uint64_t toc_compressed_size;
530 uint64_t toc_uncompressed_size;
531 uint32_t toc_chksum_alg;
535 xar = (struct xar *)(a->format->data);
540 b = __archive_read_ahead(a, HEADER_SIZE, &bytes);
543 if (bytes < HEADER_SIZE) {
544 archive_set_error(&a->archive,
545 ARCHIVE_ERRNO_FILE_FORMAT,
546 "Truncated archive header");
547 return (ARCHIVE_FATAL);
550 if (archive_be32dec(b) != HEADER_MAGIC) {
551 archive_set_error(&a->archive,
552 ARCHIVE_ERRNO_FILE_FORMAT,
553 "Invalid header magic");
554 return (ARCHIVE_FATAL);
556 if (archive_be16dec(b+6) != HEADER_VERSION) {
557 archive_set_error(&a->archive,
558 ARCHIVE_ERRNO_FILE_FORMAT,
559 "Unsupported header version(%d)",
560 archive_be16dec(b+6));
561 return (ARCHIVE_FATAL);
563 toc_compressed_size = archive_be64dec(b+8);
564 xar->toc_remaining = toc_compressed_size;
565 toc_uncompressed_size = archive_be64dec(b+16);
566 toc_chksum_alg = archive_be32dec(b+24);
567 __archive_read_consume(a, HEADER_SIZE);
568 xar->offset += HEADER_SIZE;
572 * Read TOC(Table of Contents).
574 /* Initialize reading contents. */
575 r = move_reading_point(a, HEADER_SIZE);
578 r = rd_contents_init(a, GZIP, toc_chksum_alg, CKSUM_NONE);
582 #ifdef HAVE_LIBXML_XMLREADER_H
583 r = xml2_read_toc(a);
584 #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
585 r = expat_read_toc(a);
590 /* Set 'The HEAP' base. */
591 xar->h_base = xar->offset;
592 if (xar->toc_total != toc_uncompressed_size) {
593 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
594 "TOC uncompressed size error");
595 return (ARCHIVE_FATAL);
601 if (toc_chksum_alg != CKSUM_NONE) {
602 r = move_reading_point(a, xar->toc_chksum_offset);
605 b = __archive_read_ahead(a, 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, xar->toc_chksum_size, NULL, 0);
615 __archive_read_consume(a, xar->toc_chksum_size);
616 xar->offset += xar->toc_chksum_size;
618 return (ARCHIVE_FATAL);
622 * Connect hardlinked files.
624 for (file = xar->hdlink_orgs; file != NULL; file = file->hdnext) {
625 struct hdlink **hdlink;
627 for (hdlink = &(xar->hdlink_list); *hdlink != NULL;
628 hdlink = &((*hdlink)->next)) {
629 if ((*hdlink)->id == file->id) {
630 struct hdlink *hltmp;
632 int nlink = (*hdlink)->cnt + 1;
635 for (f2 = (*hdlink)->files; f2 != NULL;
639 &(f2->hardlink), &(file->pathname));
641 /* Remove resolved files from hdlist_list. */
643 *hdlink = hltmp->next;
649 a->archive.archive_format = ARCHIVE_FORMAT_XAR;
650 a->archive.archive_format_name = "xar";
656 xar_read_header(struct archive_read *a, struct archive_entry *entry)
659 struct xar_file *file;
663 xar = (struct xar *)(a->format->data);
666 if (xar->offset == 0) {
667 /* Create a character conversion object. */
668 if (xar->sconv == NULL) {
669 xar->sconv = archive_string_conversion_from_charset(
670 &(a->archive), "UTF-8", 1);
671 if (xar->sconv == NULL)
672 return (ARCHIVE_FATAL);
682 file = xar->file = heap_get_entry(&(xar->file_queue));
684 xar->end_of_file = 1;
685 return (ARCHIVE_EOF);
687 if ((file->mode & AE_IFMT) != AE_IFDIR)
689 if (file->has != (HAS_PATHNAME | HAS_TYPE))
692 * If a file type is a directory and it does not have
693 * any metadata, do not export.
697 archive_entry_set_atime(entry, file->atime, 0);
698 archive_entry_set_ctime(entry, file->ctime, 0);
699 archive_entry_set_mtime(entry, file->mtime, 0);
700 archive_entry_set_gid(entry, file->gid);
701 if (file->gname.length > 0 &&
702 archive_entry_copy_gname_l(entry, file->gname.s,
703 archive_strlen(&(file->gname)), xar->sconv) != 0) {
704 if (errno == ENOMEM) {
705 archive_set_error(&a->archive, ENOMEM,
706 "Can't allocate memory for Gname");
707 return (ARCHIVE_FATAL);
709 archive_set_error(&a->archive,
710 ARCHIVE_ERRNO_FILE_FORMAT,
711 "Gname cannot be converted from %s to current locale.",
712 archive_string_conversion_charset_name(xar->sconv));
715 archive_entry_set_uid(entry, file->uid);
716 if (file->uname.length > 0 &&
717 archive_entry_copy_uname_l(entry, file->uname.s,
718 archive_strlen(&(file->uname)), xar->sconv) != 0) {
719 if (errno == ENOMEM) {
720 archive_set_error(&a->archive, ENOMEM,
721 "Can't allocate memory for Uname");
722 return (ARCHIVE_FATAL);
724 archive_set_error(&a->archive,
725 ARCHIVE_ERRNO_FILE_FORMAT,
726 "Uname cannot be converted from %s to current locale.",
727 archive_string_conversion_charset_name(xar->sconv));
730 archive_entry_set_mode(entry, file->mode);
731 if (archive_entry_copy_pathname_l(entry, file->pathname.s,
732 archive_strlen(&(file->pathname)), xar->sconv) != 0) {
733 if (errno == ENOMEM) {
734 archive_set_error(&a->archive, ENOMEM,
735 "Can't allocate memory for Pathname");
736 return (ARCHIVE_FATAL);
738 archive_set_error(&a->archive,
739 ARCHIVE_ERRNO_FILE_FORMAT,
740 "Pathname cannot be converted from %s to current locale.",
741 archive_string_conversion_charset_name(xar->sconv));
746 if (file->symlink.length > 0 &&
747 archive_entry_copy_symlink_l(entry, file->symlink.s,
748 archive_strlen(&(file->symlink)), xar->sconv) != 0) {
749 if (errno == ENOMEM) {
750 archive_set_error(&a->archive, ENOMEM,
751 "Can't allocate memory for Linkname");
752 return (ARCHIVE_FATAL);
754 archive_set_error(&a->archive,
755 ARCHIVE_ERRNO_FILE_FORMAT,
756 "Linkname cannot be converted from %s to current locale.",
757 archive_string_conversion_charset_name(xar->sconv));
760 /* Set proper nlink. */
761 if ((file->mode & AE_IFMT) == AE_IFDIR)
762 archive_entry_set_nlink(entry, file->subdirs + 2);
764 archive_entry_set_nlink(entry, file->nlink);
765 archive_entry_set_size(entry, file->size);
766 if (archive_strlen(&(file->hardlink)) > 0)
767 archive_entry_set_hardlink(entry, file->hardlink.s);
768 archive_entry_set_ino64(entry, file->ino64);
769 if (file->has & HAS_DEV)
770 archive_entry_set_dev(entry, file->dev);
771 if (file->has & HAS_DEVMAJOR)
772 archive_entry_set_devmajor(entry, file->devmajor);
773 if (file->has & HAS_DEVMINOR)
774 archive_entry_set_devminor(entry, file->devminor);
775 if (archive_strlen(&(file->fflags_text)) > 0)
776 archive_entry_copy_fflags_text(entry, file->fflags_text.s);
779 xar->entry_total = 0;
780 xar->entry_remaining = file->length;
781 xar->entry_size = file->size;
782 xar->entry_encoding = file->encoding;
783 xar->entry_a_sum = file->a_sum;
784 xar->entry_e_sum = file->e_sum;
786 * Read extended attributes.
788 xattr = file->xattr_list;
789 while (xattr != NULL) {
791 size_t outbytes, used;
793 r = move_reading_point(a, xattr->offset);
796 r = rd_contents_init(a, xattr->encoding,
797 xattr->a_sum.alg, xattr->e_sum.alg);
801 r = rd_contents(a, &d, &outbytes, &used, xattr->length);
804 if (outbytes != xattr->size) {
805 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
806 "Decompressed size error");
810 r = checksum_final(a,
811 xattr->a_sum.val, xattr->a_sum.len,
812 xattr->e_sum.val, xattr->e_sum.len);
815 archive_entry_xattr_add_entry(entry,
816 xattr->name.s, d, outbytes);
819 if (r != ARCHIVE_OK) {
824 if (xar->entry_remaining > 0)
825 /* Move reading point to the beginning of current
827 r = move_reading_point(a, file->offset);
836 xar_read_data(struct archive_read *a,
837 const void **buff, size_t *size, int64_t *offset)
843 xar = (struct xar *)(a->format->data);
845 if (xar->entry_unconsumed) {
846 __archive_read_consume(a, xar->entry_unconsumed);
847 xar->entry_unconsumed = 0;
850 if (xar->end_of_file || xar->entry_remaining <= 0) {
852 goto abort_read_data;
855 if (xar->entry_init) {
856 r = rd_contents_init(a, xar->entry_encoding,
857 xar->entry_a_sum.alg, xar->entry_e_sum.alg);
858 if (r != ARCHIVE_OK) {
859 xar->entry_remaining = 0;
866 r = rd_contents(a, buff, size, &used, xar->entry_remaining);
868 goto abort_read_data;
870 *offset = xar->entry_total;
871 xar->entry_total += *size;
874 xar->entry_remaining -= used;
875 xar->entry_unconsumed = used;
877 if (xar->entry_remaining == 0) {
878 if (xar->entry_total != xar->entry_size) {
879 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
880 "Decompressed size error");
882 goto abort_read_data;
884 r = checksum_final(a,
885 xar->entry_a_sum.val, xar->entry_a_sum.len,
886 xar->entry_e_sum.val, xar->entry_e_sum.len);
888 goto abort_read_data;
895 *offset = xar->total;
900 xar_read_data_skip(struct archive_read *a)
903 int64_t bytes_skipped;
905 xar = (struct xar *)(a->format->data);
906 if (xar->end_of_file)
907 return (ARCHIVE_EOF);
908 bytes_skipped = __archive_read_consume(a, xar->entry_remaining +
909 xar->entry_unconsumed);
910 if (bytes_skipped < 0)
911 return (ARCHIVE_FATAL);
912 xar->offset += bytes_skipped;
913 xar->entry_unconsumed = 0;
918 xar_cleanup(struct archive_read *a)
921 struct hdlink *hdlink;
925 xar = (struct xar *)(a->format->data);
926 r = decompression_cleanup(a);
927 hdlink = xar->hdlink_list;
928 while (hdlink != NULL) {
929 struct hdlink *next = hdlink->next;
934 for (i = 0; i < xar->file_queue.used; i++)
935 file_free(xar->file_queue.files[i]);
936 while (xar->unknowntags != NULL) {
937 struct unknown_tag *tag;
939 tag = xar->unknowntags;
940 xar->unknowntags = tag->next;
941 archive_string_free(&(tag->name));
946 a->format->data = NULL;
951 move_reading_point(struct archive_read *a, uint64_t offset)
955 xar = (struct xar *)(a->format->data);
956 if (xar->offset - xar->h_base != offset) {
957 /* Seek forward to the start of file contents. */
960 step = offset - (xar->offset - xar->h_base);
962 step = __archive_read_consume(a, step);
967 archive_set_error(&(a->archive),
970 return (ARCHIVE_FAILED);
977 rd_contents_init(struct archive_read *a, enum enctype encoding,
978 int a_sum_alg, int e_sum_alg)
982 /* Init decompress library. */
983 if ((r = decompression_init(a, encoding)) != ARCHIVE_OK)
985 /* Init checksum library. */
986 checksum_init(a, a_sum_alg, e_sum_alg);
991 rd_contents(struct archive_read *a, const void **buff, size_t *size,
992 size_t *used, uint64_t remaining)
994 const unsigned char *b;
997 /* Get whatever bytes are immediately available. */
998 b = __archive_read_ahead(a, 1, &bytes);
1000 return ((int)bytes);
1002 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1003 "Truncated archive file");
1004 return (ARCHIVE_FATAL);
1006 if ((uint64_t)bytes > remaining)
1007 bytes = (ssize_t)remaining;
1010 * Decompress contents of file.
1013 if (decompress(a, buff, size, b, used) != ARCHIVE_OK)
1014 return (ARCHIVE_FATAL);
1017 * Update checksum of a compressed data and a extracted data.
1019 checksum_update(a, b, *used, *buff, *size);
1021 return (ARCHIVE_OK);
1025 * Note that this implementation does not (and should not!) obey
1026 * locale settings; you cannot simply substitute strtol here, since
1027 * it does obey locale.
1031 atol10(const char *p, size_t char_cnt)
1038 while (digit >= 0 && digit < 10 && char_cnt-- > 0) {
1039 l = (l * 10) + digit;
1046 atol8(const char *p, size_t char_cnt)
1052 while (char_cnt-- > 0) {
1053 if (*p >= '0' && *p <= '7')
1065 atohex(unsigned char *b, size_t bsize, const char *p, size_t psize)
1067 size_t fbsize = bsize;
1069 while (bsize && psize > 1) {
1072 if (p[0] >= 'a' && p[0] <= 'z')
1073 x = (p[0] - 'a' + 0x0a) << 4;
1074 else if (p[0] >= 'A' && p[0] <= 'Z')
1075 x = (p[0] - 'A' + 0x0a) << 4;
1076 else if (p[0] >= '0' && p[0] <= '9')
1077 x = (p[0] - '0') << 4;
1080 if (p[1] >= 'a' && p[1] <= 'z')
1081 x |= p[1] - 'a' + 0x0a;
1082 else if (p[1] >= 'A' && p[1] <= 'Z')
1083 x |= p[1] - 'A' + 0x0a;
1084 else if (p[1] >= '0' && p[1] <= '9')
1094 return (fbsize - bsize);
1098 time_from_tm(struct tm *t)
1101 /* Use platform timegm() if available. */
1103 #elif HAVE__MKGMTIME64
1104 return (_mkgmtime64(t));
1106 /* Else use direct calculation using POSIX assumptions. */
1107 /* First, fix up tm_yday based on the year/month/day. */
1109 /* Then we can compute timegm() from first principles. */
1110 return (t->tm_sec + t->tm_min * 60 + t->tm_hour * 3600
1111 + t->tm_yday * 86400 + (t->tm_year - 70) * 31536000
1112 + ((t->tm_year - 69) / 4) * 86400 -
1113 ((t->tm_year - 1) / 100) * 86400
1114 + ((t->tm_year + 299) / 400) * 86400);
1119 parse_time(const char *p, size_t n)
1125 memset(&tm, 0, sizeof(tm));
1128 data = atol10(p, 4);
1131 tm.tm_year = (int)data - 1900;
1135 data = atol10(p, 2);
1136 if (data < 1 || data > 12)
1138 tm.tm_mon = (int)data -1;
1142 data = atol10(p, 2);
1143 if (data < 1 || data > 31)
1145 tm.tm_mday = (int)data;
1149 data = atol10(p, 2);
1150 if (data < 0 || data > 23)
1152 tm.tm_hour = (int)data;
1156 data = atol10(p, 2);
1157 if (data < 0 || data > 59)
1159 tm.tm_min = (int)data;
1163 data = atol10(p, 2);
1164 if (data < 0 || data > 60)
1166 tm.tm_sec = (int)data;
1173 t = time_from_tm(&tm);
1179 heap_add_entry(struct archive_read *a,
1180 struct heap_queue *heap, struct xar_file *file)
1182 uint64_t file_id, parent_id;
1185 /* Expand our pending files list as necessary. */
1186 if (heap->used >= heap->allocated) {
1187 struct xar_file **new_pending_files;
1188 int new_size = heap->allocated * 2;
1190 if (heap->allocated < 1024)
1192 /* Overflow might keep us from growing the list. */
1193 if (new_size <= heap->allocated) {
1194 archive_set_error(&a->archive,
1195 ENOMEM, "Out of memory");
1196 return (ARCHIVE_FATAL);
1198 new_pending_files = (struct xar_file **)
1199 malloc(new_size * sizeof(new_pending_files[0]));
1200 if (new_pending_files == NULL) {
1201 archive_set_error(&a->archive,
1202 ENOMEM, "Out of memory");
1203 return (ARCHIVE_FATAL);
1205 memcpy(new_pending_files, heap->files,
1206 heap->allocated * sizeof(new_pending_files[0]));
1207 if (heap->files != NULL)
1209 heap->files = new_pending_files;
1210 heap->allocated = new_size;
1216 * Start with hole at end, walk it up tree to find insertion point.
1218 hole = heap->used++;
1220 parent = (hole - 1)/2;
1221 parent_id = heap->files[parent]->id;
1222 if (file_id >= parent_id) {
1223 heap->files[hole] = file;
1224 return (ARCHIVE_OK);
1226 /* Move parent into hole <==> move hole up tree. */
1227 heap->files[hole] = heap->files[parent];
1230 heap->files[0] = file;
1232 return (ARCHIVE_OK);
1235 static struct xar_file *
1236 heap_get_entry(struct heap_queue *heap)
1238 uint64_t a_id, b_id, c_id;
1240 struct xar_file *r, *tmp;
1246 * The first file in the list is the earliest; we'll return this.
1251 * Move the last item in the heap to the root of the tree
1253 heap->files[0] = heap->files[--(heap->used)];
1256 * Rebalance the heap.
1258 a = 0; /* Starting element and its heap key */
1259 a_id = heap->files[a]->id;
1261 b = a + a + 1; /* First child */
1262 if (b >= heap->used)
1264 b_id = heap->files[b]->id;
1265 c = b + 1; /* Use second child if it is smaller. */
1266 if (c < heap->used) {
1267 c_id = heap->files[c]->id;
1275 tmp = heap->files[a];
1276 heap->files[a] = heap->files[b];
1277 heap->files[b] = tmp;
1283 add_link(struct archive_read *a, struct xar *xar, struct xar_file *file)
1285 struct hdlink *hdlink;
1287 for (hdlink = xar->hdlink_list; hdlink != NULL; hdlink = hdlink->next) {
1288 if (hdlink->id == file->link) {
1289 file->hdnext = hdlink->files;
1291 hdlink->files = file;
1292 return (ARCHIVE_OK);
1295 hdlink = malloc(sizeof(*hdlink));
1296 if (hdlink == NULL) {
1297 archive_set_error(&a->archive, ENOMEM, "Out of memory");
1298 return (ARCHIVE_FATAL);
1300 file->hdnext = NULL;
1301 hdlink->id = file->link;
1303 hdlink->files = file;
1304 hdlink->next = xar->hdlink_list;
1305 xar->hdlink_list = hdlink;
1306 return (ARCHIVE_OK);
1310 _checksum_init(struct chksumwork *sumwrk, int sum_alg)
1312 sumwrk->alg = sum_alg;
1317 archive_sha1_init(&(sumwrk->sha1ctx));
1320 archive_md5_init(&(sumwrk->md5ctx));
1326 _checksum_update(struct chksumwork *sumwrk, const void *buff, size_t size)
1329 switch (sumwrk->alg) {
1333 archive_sha1_update(&(sumwrk->sha1ctx), buff, size);
1336 archive_md5_update(&(sumwrk->md5ctx), buff, size);
1342 _checksum_final(struct chksumwork *sumwrk, const void *val, size_t len)
1344 unsigned char sum[MAX_SUM_SIZE];
1347 switch (sumwrk->alg) {
1351 archive_sha1_final(&(sumwrk->sha1ctx), sum);
1352 if (len != SHA1_SIZE ||
1353 memcmp(val, sum, SHA1_SIZE) != 0)
1357 archive_md5_final(&(sumwrk->md5ctx), sum);
1358 if (len != MD5_SIZE ||
1359 memcmp(val, sum, MD5_SIZE) != 0)
1367 checksum_init(struct archive_read *a, int a_sum_alg, int e_sum_alg)
1371 xar = (struct xar *)(a->format->data);
1372 _checksum_init(&(xar->a_sumwrk), a_sum_alg);
1373 _checksum_init(&(xar->e_sumwrk), e_sum_alg);
1377 checksum_update(struct archive_read *a, const void *abuff, size_t asize,
1378 const void *ebuff, size_t esize)
1382 xar = (struct xar *)(a->format->data);
1383 _checksum_update(&(xar->a_sumwrk), abuff, asize);
1384 _checksum_update(&(xar->e_sumwrk), ebuff, esize);
1388 checksum_final(struct archive_read *a, const void *a_sum_val,
1389 size_t a_sum_len, const void *e_sum_val, size_t e_sum_len)
1394 xar = (struct xar *)(a->format->data);
1395 r = _checksum_final(&(xar->a_sumwrk), a_sum_val, a_sum_len);
1396 if (r == ARCHIVE_OK)
1397 r = _checksum_final(&(xar->e_sumwrk), e_sum_val, e_sum_len);
1398 if (r != ARCHIVE_OK)
1399 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
1405 decompression_init(struct archive_read *a, enum enctype encoding)
1411 xar = (struct xar *)(a->format->data);
1412 xar->rd_encoding = encoding;
1417 if (xar->stream_valid)
1418 r = inflateReset(&(xar->stream));
1420 r = inflateInit(&(xar->stream));
1422 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1423 "Couldn't initialize zlib stream.");
1424 return (ARCHIVE_FATAL);
1426 xar->stream_valid = 1;
1427 xar->stream.total_in = 0;
1428 xar->stream.total_out = 0;
1430 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1432 if (xar->bzstream_valid) {
1433 BZ2_bzDecompressEnd(&(xar->bzstream));
1434 xar->bzstream_valid = 0;
1436 r = BZ2_bzDecompressInit(&(xar->bzstream), 0, 0);
1437 if (r == BZ_MEM_ERROR)
1438 r = BZ2_bzDecompressInit(&(xar->bzstream), 0, 1);
1440 int err = ARCHIVE_ERRNO_MISC;
1443 case BZ_PARAM_ERROR:
1444 detail = "invalid setup parameter";
1448 detail = "out of memory";
1450 case BZ_CONFIG_ERROR:
1451 detail = "mis-compiled library";
1454 archive_set_error(&a->archive, err,
1455 "Internal error initializing decompressor: %s",
1456 detail == NULL ? "??" : detail);
1457 xar->bzstream_valid = 0;
1458 return (ARCHIVE_FATAL);
1460 xar->bzstream_valid = 1;
1461 xar->bzstream.total_in_lo32 = 0;
1462 xar->bzstream.total_in_hi32 = 0;
1463 xar->bzstream.total_out_lo32 = 0;
1464 xar->bzstream.total_out_hi32 = 0;
1467 #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1468 #if LZMA_VERSION_MAJOR >= 5
1469 /* Effectively disable the limiter. */
1470 #define LZMA_MEMLIMIT UINT64_MAX
1472 /* NOTE: This needs to check memory size which running system has. */
1473 #define LZMA_MEMLIMIT (1U << 30)
1477 if (xar->lzstream_valid) {
1478 lzma_end(&(xar->lzstream));
1479 xar->lzstream_valid = 0;
1481 if (xar->entry_encoding == XZ)
1482 r = lzma_stream_decoder(&(xar->lzstream),
1483 LZMA_MEMLIMIT,/* memlimit */
1486 r = lzma_alone_decoder(&(xar->lzstream),
1487 LZMA_MEMLIMIT);/* memlimit */
1490 case LZMA_MEM_ERROR:
1491 archive_set_error(&a->archive,
1493 "Internal error initializing "
1494 "compression library: "
1495 "Cannot allocate memory");
1497 case LZMA_OPTIONS_ERROR:
1498 archive_set_error(&a->archive,
1500 "Internal error initializing "
1501 "compression library: "
1502 "Invalid or unsupported options");
1505 archive_set_error(&a->archive,
1507 "Internal error initializing "
1511 return (ARCHIVE_FATAL);
1513 xar->lzstream_valid = 1;
1514 xar->lzstream.total_in = 0;
1515 xar->lzstream.total_out = 0;
1517 #elif defined(HAVE_LZMADEC_H) && defined(HAVE_LIBLZMADEC)
1519 if (xar->lzstream_valid)
1520 lzmadec_end(&(xar->lzstream));
1521 r = lzmadec_init(&(xar->lzstream));
1522 if (r != LZMADEC_OK) {
1524 case LZMADEC_HEADER_ERROR:
1525 archive_set_error(&a->archive,
1527 "Internal error initializing "
1528 "compression library: "
1531 case LZMADEC_MEM_ERROR:
1532 archive_set_error(&a->archive,
1534 "Internal error initializing "
1535 "compression library: "
1539 return (ARCHIVE_FATAL);
1541 xar->lzstream_valid = 1;
1542 xar->lzstream.total_in = 0;
1543 xar->lzstream.total_out = 0;
1547 * Unsupported compression.
1550 #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR)
1553 #if !defined(HAVE_LZMA_H) || !defined(HAVE_LIBLZMA)
1554 #if !defined(HAVE_LZMADEC_H) || !defined(HAVE_LIBLZMADEC)
1559 switch (xar->entry_encoding) {
1560 case BZIP2: detail = "bzip2"; break;
1561 case LZMA: detail = "lzma"; break;
1562 case XZ: detail = "xz"; break;
1563 default: detail = "??"; break;
1565 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1566 "%s compression not supported on this platform",
1568 return (ARCHIVE_FAILED);
1570 return (ARCHIVE_OK);
1574 decompress(struct archive_read *a, const void **buff, size_t *outbytes,
1575 const void *b, size_t *used)
1579 size_t avail_in, avail_out;
1582 xar = (struct xar *)(a->format->data);
1584 outbuff = (void *)(uintptr_t)*buff;
1585 if (outbuff == NULL) {
1586 if (xar->outbuff == NULL) {
1587 xar->outbuff = malloc(OUTBUFF_SIZE);
1588 if (xar->outbuff == NULL) {
1589 archive_set_error(&a->archive, ENOMEM,
1590 "Couldn't allocate memory for out buffer");
1591 return (ARCHIVE_FATAL);
1594 outbuff = xar->outbuff;
1596 avail_out = OUTBUFF_SIZE;
1598 avail_out = *outbytes;
1599 switch (xar->rd_encoding) {
1601 xar->stream.next_in = (Bytef *)(uintptr_t)b;
1602 xar->stream.avail_in = avail_in;
1603 xar->stream.next_out = (unsigned char *)outbuff;
1604 xar->stream.avail_out = avail_out;
1605 r = inflate(&(xar->stream), 0);
1607 case Z_OK: /* Decompressor made some progress.*/
1608 case Z_STREAM_END: /* Found end of stream. */
1611 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1612 "File decompression failed (%d)", r);
1613 return (ARCHIVE_FATAL);
1615 *used = avail_in - xar->stream.avail_in;
1616 *outbytes = avail_out - xar->stream.avail_out;
1618 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1620 xar->bzstream.next_in = (char *)(uintptr_t)b;
1621 xar->bzstream.avail_in = avail_in;
1622 xar->bzstream.next_out = (char *)outbuff;
1623 xar->bzstream.avail_out = avail_out;
1624 r = BZ2_bzDecompress(&(xar->bzstream));
1626 case BZ_STREAM_END: /* Found end of stream. */
1627 switch (BZ2_bzDecompressEnd(&(xar->bzstream))) {
1631 archive_set_error(&(a->archive),
1633 "Failed to clean up decompressor");
1634 return (ARCHIVE_FATAL);
1636 xar->bzstream_valid = 0;
1638 case BZ_OK: /* Decompressor made some progress. */
1641 archive_set_error(&(a->archive),
1643 "bzip decompression failed");
1644 return (ARCHIVE_FATAL);
1646 *used = avail_in - xar->bzstream.avail_in;
1647 *outbytes = avail_out - xar->bzstream.avail_out;
1650 #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1653 xar->lzstream.next_in = b;
1654 xar->lzstream.avail_in = avail_in;
1655 xar->lzstream.next_out = (unsigned char *)outbuff;
1656 xar->lzstream.avail_out = avail_out;
1657 r = lzma_code(&(xar->lzstream), LZMA_RUN);
1659 case LZMA_STREAM_END: /* Found end of stream. */
1660 lzma_end(&(xar->lzstream));
1661 xar->lzstream_valid = 0;
1663 case LZMA_OK: /* Decompressor made some progress. */
1666 archive_set_error(&(a->archive),
1668 "%s decompression failed(%d)",
1669 (xar->entry_encoding == XZ)?"xz":"lzma",
1671 return (ARCHIVE_FATAL);
1673 *used = avail_in - xar->lzstream.avail_in;
1674 *outbytes = avail_out - xar->lzstream.avail_out;
1676 #elif defined(HAVE_LZMADEC_H) && defined(HAVE_LIBLZMADEC)
1678 xar->lzstream.next_in = (unsigned char *)(uintptr_t)b;
1679 xar->lzstream.avail_in = avail_in;
1680 xar->lzstream.next_out = (unsigned char *)outbuff;
1681 xar->lzstream.avail_out = avail_out;
1682 r = lzmadec_decode(&(xar->lzstream), 0);
1684 case LZMADEC_STREAM_END: /* Found end of stream. */
1685 switch (lzmadec_end(&(xar->lzstream))) {
1689 archive_set_error(&(a->archive),
1691 "Failed to clean up lzmadec decompressor");
1692 return (ARCHIVE_FATAL);
1694 xar->lzstream_valid = 0;
1696 case LZMADEC_OK: /* Decompressor made some progress. */
1699 archive_set_error(&(a->archive),
1701 "lzmadec decompression failed(%d)",
1703 return (ARCHIVE_FATAL);
1705 *used = avail_in - xar->lzstream.avail_in;
1706 *outbytes = avail_out - xar->lzstream.avail_out;
1709 #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR)
1712 #if !defined(HAVE_LZMA_H) || !defined(HAVE_LIBLZMA)
1713 #if !defined(HAVE_LZMADEC_H) || !defined(HAVE_LIBLZMADEC)
1720 if (outbuff == xar->outbuff) {
1723 *outbytes = avail_in;
1725 if (avail_out > avail_in)
1726 avail_out = avail_in;
1727 memcpy(outbuff, b, avail_out);
1729 *outbytes = avail_out;
1733 return (ARCHIVE_OK);
1737 decompression_cleanup(struct archive_read *a)
1742 xar = (struct xar *)(a->format->data);
1744 if (xar->stream_valid) {
1745 if (inflateEnd(&(xar->stream)) != Z_OK) {
1746 archive_set_error(&a->archive,
1748 "Failed to clean up zlib decompressor");
1752 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1753 if (xar->bzstream_valid) {
1754 if (BZ2_bzDecompressEnd(&(xar->bzstream)) != BZ_OK) {
1755 archive_set_error(&a->archive,
1757 "Failed to clean up bzip2 decompressor");
1762 #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1763 if (xar->lzstream_valid)
1764 lzma_end(&(xar->lzstream));
1765 #elif defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1766 if (xar->lzstream_valid) {
1767 if (lzmadec_end(&(xar->lzstream)) != LZMADEC_OK) {
1768 archive_set_error(&a->archive,
1770 "Failed to clean up lzmadec decompressor");
1779 xmlattr_cleanup(struct xmlattr_list *list)
1781 struct xmlattr *attr, *next;
1784 while (attr != NULL) {
1792 list->last = &(list->first);
1796 file_new(struct archive_read *a, struct xar *xar, struct xmlattr_list *list)
1798 struct xar_file *file;
1799 struct xmlattr *attr;
1801 file = calloc(1, sizeof(*file));
1803 archive_set_error(&a->archive, ENOMEM, "Out of memory");
1804 return (ARCHIVE_FATAL);
1806 file->parent = xar->file;
1807 file->mode = 0777 | AE_IFREG;
1808 file->atime = time(NULL);
1809 file->mtime = time(NULL);
1812 for (attr = list->first; attr != NULL; attr = attr->next) {
1813 if (strcmp(attr->name, "id") == 0)
1814 file->id = atol10(attr->value, strlen(attr->value));
1817 if (heap_add_entry(a, &(xar->file_queue), file) != ARCHIVE_OK)
1818 return (ARCHIVE_FATAL);
1819 return (ARCHIVE_OK);
1823 file_free(struct xar_file *file)
1825 struct xattr *xattr;
1827 archive_string_free(&(file->pathname));
1828 archive_string_free(&(file->symlink));
1829 archive_string_free(&(file->uname));
1830 archive_string_free(&(file->gname));
1831 archive_string_free(&(file->hardlink));
1832 xattr = file->xattr_list;
1833 while (xattr != NULL) {
1845 xattr_new(struct archive_read *a, struct xar *xar, struct xmlattr_list *list)
1847 struct xattr *xattr, **nx;
1848 struct xmlattr *attr;
1850 xattr = calloc(1, sizeof(*xattr));
1851 if (xattr == NULL) {
1852 archive_set_error(&a->archive, ENOMEM, "Out of memory");
1853 return (ARCHIVE_FATAL);
1856 for (attr = list->first; attr != NULL; attr = attr->next) {
1857 if (strcmp(attr->name, "id") == 0)
1858 xattr->id = atol10(attr->value, strlen(attr->value));
1860 /* Chain to xattr list. */
1861 for (nx = &(xar->file->xattr_list);
1862 *nx != NULL; nx = &((*nx)->next)) {
1863 if (xattr->id < (*nx)->id)
1869 return (ARCHIVE_OK);
1873 xattr_free(struct xattr *xattr)
1875 archive_string_free(&(xattr->name));
1880 getencoding(struct xmlattr_list *list)
1882 struct xmlattr *attr;
1883 enum enctype encoding = NONE;
1885 for (attr = list->first; attr != NULL; attr = attr->next) {
1886 if (strcmp(attr->name, "style") == 0) {
1887 if (strcmp(attr->value, "application/octet-stream") == 0)
1889 else if (strcmp(attr->value, "application/x-gzip") == 0)
1891 else if (strcmp(attr->value, "application/x-bzip2") == 0)
1893 else if (strcmp(attr->value, "application/x-lzma") == 0)
1895 else if (strcmp(attr->value, "application/x-xz") == 0)
1903 getsumalgorithm(struct xmlattr_list *list)
1905 struct xmlattr *attr;
1906 int alg = CKSUM_NONE;
1908 for (attr = list->first; attr != NULL; attr = attr->next) {
1909 if (strcmp(attr->name, "style") == 0) {
1910 const char *v = attr->value;
1911 if ((v[0] == 'S' || v[0] == 's') &&
1912 (v[1] == 'H' || v[1] == 'h') &&
1913 (v[2] == 'A' || v[2] == 'a') &&
1914 v[3] == '1' && v[4] == '\0')
1916 if ((v[0] == 'M' || v[0] == 'm') &&
1917 (v[1] == 'D' || v[1] == 'd') &&
1918 v[2] == '5' && v[3] == '\0')
1926 unknowntag_start(struct archive_read *a, struct xar *xar, const char *name)
1928 struct unknown_tag *tag;
1931 fprintf(stderr, "unknowntag_start:%s\n", name);
1933 tag = malloc(sizeof(*tag));
1935 archive_set_error(&a->archive, ENOMEM, "Out of memory");
1936 return (ARCHIVE_FATAL);
1938 tag->next = xar->unknowntags;
1939 archive_string_init(&(tag->name));
1940 archive_strcpy(&(tag->name), name);
1941 if (xar->unknowntags == NULL) {
1942 xar->xmlsts_unknown = xar->xmlsts;
1943 xar->xmlsts = UNKNOWN;
1945 xar->unknowntags = tag;
1946 return (ARCHIVE_OK);
1950 unknowntag_end(struct xar *xar, const char *name)
1952 struct unknown_tag *tag;
1955 fprintf(stderr, "unknowntag_end:%s\n", name);
1957 tag = xar->unknowntags;
1958 if (tag == NULL || name == NULL)
1960 if (strcmp(tag->name.s, name) == 0) {
1961 xar->unknowntags = tag->next;
1962 archive_string_free(&(tag->name));
1964 if (xar->unknowntags == NULL)
1965 xar->xmlsts = xar->xmlsts_unknown;
1970 xml_start(struct archive_read *a, const char *name, struct xmlattr_list *list)
1973 struct xmlattr *attr;
1975 xar = (struct xar *)(a->format->data);
1978 fprintf(stderr, "xml_sta:[%s]\n", name);
1979 for (attr = list->first; attr != NULL; attr = attr->next)
1980 fprintf(stderr, " attr:\"%s\"=\"%s\"\n",
1981 attr->name, attr->value);
1983 xar->base64text = 0;
1984 switch (xar->xmlsts) {
1986 if (strcmp(name, "xar") == 0)
1989 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
1990 return (ARCHIVE_FATAL);
1993 if (strcmp(name, "toc") == 0)
1996 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
1997 return (ARCHIVE_FATAL);
2000 if (strcmp(name, "creation-time") == 0)
2001 xar->xmlsts = TOC_CREATION_TIME;
2002 else if (strcmp(name, "checksum") == 0)
2003 xar->xmlsts = TOC_CHECKSUM;
2004 else if (strcmp(name, "file") == 0) {
2005 if (file_new(a, xar, list) != ARCHIVE_OK)
2006 return (ARCHIVE_FATAL);
2007 xar->xmlsts = TOC_FILE;
2010 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2011 return (ARCHIVE_FATAL);
2014 if (strcmp(name, "offset") == 0)
2015 xar->xmlsts = TOC_CHECKSUM_OFFSET;
2016 else if (strcmp(name, "size") == 0)
2017 xar->xmlsts = TOC_CHECKSUM_SIZE;
2019 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2020 return (ARCHIVE_FATAL);
2023 if (strcmp(name, "file") == 0) {
2024 if (file_new(a, xar, list) != ARCHIVE_OK)
2025 return (ARCHIVE_FATAL);
2027 else if (strcmp(name, "data") == 0)
2028 xar->xmlsts = FILE_DATA;
2029 else if (strcmp(name, "ea") == 0) {
2030 if (xattr_new(a, xar, list) != ARCHIVE_OK)
2031 return (ARCHIVE_FATAL);
2032 xar->xmlsts = FILE_EA;
2034 else if (strcmp(name, "ctime") == 0)
2035 xar->xmlsts = FILE_CTIME;
2036 else if (strcmp(name, "mtime") == 0)
2037 xar->xmlsts = FILE_MTIME;
2038 else if (strcmp(name, "atime") == 0)
2039 xar->xmlsts = FILE_ATIME;
2040 else if (strcmp(name, "group") == 0)
2041 xar->xmlsts = FILE_GROUP;
2042 else if (strcmp(name, "gid") == 0)
2043 xar->xmlsts = FILE_GID;
2044 else if (strcmp(name, "user") == 0)
2045 xar->xmlsts = FILE_USER;
2046 else if (strcmp(name, "uid") == 0)
2047 xar->xmlsts = FILE_UID;
2048 else if (strcmp(name, "mode") == 0)
2049 xar->xmlsts = FILE_MODE;
2050 else if (strcmp(name, "device") == 0)
2051 xar->xmlsts = FILE_DEVICE;
2052 else if (strcmp(name, "deviceno") == 0)
2053 xar->xmlsts = FILE_DEVICENO;
2054 else if (strcmp(name, "inode") == 0)
2055 xar->xmlsts = FILE_INODE;
2056 else if (strcmp(name, "link") == 0)
2057 xar->xmlsts = FILE_LINK;
2058 else if (strcmp(name, "type") == 0) {
2059 xar->xmlsts = FILE_TYPE;
2060 for (attr = list->first; attr != NULL;
2061 attr = attr->next) {
2062 if (strcmp(attr->name, "link") != 0)
2064 if (strcmp(attr->value, "original") == 0) {
2065 xar->file->hdnext = xar->hdlink_orgs;
2066 xar->hdlink_orgs = xar->file;
2068 xar->file->link = 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);
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;
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;
2093 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2094 return (ARCHIVE_FATAL);
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);
2107 else if (strcmp(name, "archived-checksum") == 0) {
2108 xar->xmlsts = FILE_DATA_A_CHECKSUM;
2109 xar->file->a_sum.alg = getsumalgorithm(list);
2111 else if (strcmp(name, "extracted-checksum") == 0) {
2112 xar->xmlsts = FILE_DATA_E_CHECKSUM;
2113 xar->file->e_sum.alg = getsumalgorithm(list);
2115 else if (strcmp(name, "content") == 0)
2116 xar->xmlsts = FILE_DATA_CONTENT;
2118 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2119 return (ARCHIVE_FATAL);
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;
2127 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2128 return (ARCHIVE_FATAL);
2130 case FILE_DATA_CONTENT:
2131 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2132 return (ARCHIVE_FATAL);
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;
2153 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2154 return (ARCHIVE_FATAL);
2157 if (strcmp(name, "appleextended") == 0)
2158 xar->xmlsts = FILE_ACL_APPLEEXTENDED;
2159 if (strcmp(name, "default") == 0)
2160 xar->xmlsts = FILE_ACL_DEFAULT;
2161 else if (strcmp(name, "access") == 0)
2162 xar->xmlsts = FILE_ACL_ACCESS;
2164 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2165 return (ARCHIVE_FATAL);
2168 if (!xml_parse_file_flags(xar, name))
2169 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2170 return (ARCHIVE_FATAL);
2173 if (!xml_parse_file_ext2(xar, name))
2174 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2175 return (ARCHIVE_FATAL);
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:
2189 case FILE_EA_ENCODING:
2190 case FILE_EA_A_CHECKSUM:
2191 case FILE_EA_E_CHECKSUM:
2193 case FILE_EA_FSTYPE:
2202 case FILE_DEVICE_MAJOR:
2203 case FILE_DEVICE_MINOR:
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:
2243 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2244 return (ARCHIVE_FATAL);
2247 return (ARCHIVE_OK);
2251 xml_end(void *userData, const char *name)
2253 struct archive_read *a;
2256 a = (struct archive_read *)userData;
2257 xar = (struct xar *)(a->format->data);
2260 fprintf(stderr, "xml_end:[%s]\n", name);
2262 switch (xar->xmlsts) {
2266 if (strcmp(name, "xar") == 0)
2270 if (strcmp(name, "toc") == 0)
2273 case TOC_CREATION_TIME:
2274 if (strcmp(name, "creation-time") == 0)
2278 if (strcmp(name, "checksum") == 0)
2281 case TOC_CHECKSUM_OFFSET:
2282 if (strcmp(name, "offset") == 0)
2283 xar->xmlsts = TOC_CHECKSUM;
2285 case TOC_CHECKSUM_SIZE:
2286 if (strcmp(name, "size") == 0)
2287 xar->xmlsts = TOC_CHECKSUM;
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)
2300 if (strcmp(name, "data") == 0)
2301 xar->xmlsts = TOC_FILE;
2303 case FILE_DATA_LENGTH:
2304 if (strcmp(name, "length") == 0)
2305 xar->xmlsts = FILE_DATA;
2307 case FILE_DATA_OFFSET:
2308 if (strcmp(name, "offset") == 0)
2309 xar->xmlsts = FILE_DATA;
2311 case FILE_DATA_SIZE:
2312 if (strcmp(name, "size") == 0)
2313 xar->xmlsts = FILE_DATA;
2315 case FILE_DATA_ENCODING:
2316 if (strcmp(name, "encoding") == 0)
2317 xar->xmlsts = FILE_DATA;
2319 case FILE_DATA_A_CHECKSUM:
2320 if (strcmp(name, "archived-checksum") == 0)
2321 xar->xmlsts = FILE_DATA;
2323 case FILE_DATA_E_CHECKSUM:
2324 if (strcmp(name, "extracted-checksum") == 0)
2325 xar->xmlsts = FILE_DATA;
2327 case FILE_DATA_CONTENT:
2328 if (strcmp(name, "content") == 0)
2329 xar->xmlsts = FILE_DATA;
2332 if (strcmp(name, "ea") == 0) {
2333 xar->xmlsts = TOC_FILE;
2337 case FILE_EA_LENGTH:
2338 if (strcmp(name, "length") == 0)
2339 xar->xmlsts = FILE_EA;
2341 case FILE_EA_OFFSET:
2342 if (strcmp(name, "offset") == 0)
2343 xar->xmlsts = FILE_EA;
2346 if (strcmp(name, "size") == 0)
2347 xar->xmlsts = FILE_EA;
2349 case FILE_EA_ENCODING:
2350 if (strcmp(name, "encoding") == 0)
2351 xar->xmlsts = FILE_EA;
2353 case FILE_EA_A_CHECKSUM:
2354 if (strcmp(name, "archived-checksum") == 0)
2355 xar->xmlsts = FILE_EA;
2357 case FILE_EA_E_CHECKSUM:
2358 if (strcmp(name, "extracted-checksum") == 0)
2359 xar->xmlsts = FILE_EA;
2362 if (strcmp(name, "name") == 0)
2363 xar->xmlsts = FILE_EA;
2365 case FILE_EA_FSTYPE:
2366 if (strcmp(name, "fstype") == 0)
2367 xar->xmlsts = FILE_EA;
2370 if (strcmp(name, "ctime") == 0)
2371 xar->xmlsts = TOC_FILE;
2374 if (strcmp(name, "mtime") == 0)
2375 xar->xmlsts = TOC_FILE;
2378 if (strcmp(name, "atime") == 0)
2379 xar->xmlsts = TOC_FILE;
2382 if (strcmp(name, "group") == 0)
2383 xar->xmlsts = TOC_FILE;
2386 if (strcmp(name, "gid") == 0)
2387 xar->xmlsts = TOC_FILE;
2390 if (strcmp(name, "user") == 0)
2391 xar->xmlsts = TOC_FILE;
2394 if (strcmp(name, "uid") == 0)
2395 xar->xmlsts = TOC_FILE;
2398 if (strcmp(name, "mode") == 0)
2399 xar->xmlsts = TOC_FILE;
2402 if (strcmp(name, "device") == 0)
2403 xar->xmlsts = TOC_FILE;
2405 case FILE_DEVICE_MAJOR:
2406 if (strcmp(name, "major") == 0)
2407 xar->xmlsts = FILE_DEVICE;
2409 case FILE_DEVICE_MINOR:
2410 if (strcmp(name, "minor") == 0)
2411 xar->xmlsts = FILE_DEVICE;
2414 if (strcmp(name, "deviceno") == 0)
2415 xar->xmlsts = TOC_FILE;
2418 if (strcmp(name, "inode") == 0)
2419 xar->xmlsts = TOC_FILE;
2422 if (strcmp(name, "link") == 0)
2423 xar->xmlsts = TOC_FILE;
2426 if (strcmp(name, "type") == 0)
2427 xar->xmlsts = TOC_FILE;
2430 if (strcmp(name, "name") == 0)
2431 xar->xmlsts = TOC_FILE;
2434 if (strcmp(name, "acl") == 0)
2435 xar->xmlsts = TOC_FILE;
2437 case FILE_ACL_DEFAULT:
2438 if (strcmp(name, "default") == 0)
2439 xar->xmlsts = FILE_ACL;
2441 case FILE_ACL_ACCESS:
2442 if (strcmp(name, "access") == 0)
2443 xar->xmlsts = FILE_ACL;
2445 case FILE_ACL_APPLEEXTENDED:
2446 if (strcmp(name, "appleextended") == 0)
2447 xar->xmlsts = FILE_ACL;
2450 if (strcmp(name, "flags") == 0)
2451 xar->xmlsts = TOC_FILE;
2453 case FILE_FLAGS_USER_NODUMP:
2454 if (strcmp(name, "UserNoDump") == 0)
2455 xar->xmlsts = FILE_FLAGS;
2457 case FILE_FLAGS_USER_IMMUTABLE:
2458 if (strcmp(name, "UserImmutable") == 0)
2459 xar->xmlsts = FILE_FLAGS;
2461 case FILE_FLAGS_USER_APPEND:
2462 if (strcmp(name, "UserAppend") == 0)
2463 xar->xmlsts = FILE_FLAGS;
2465 case FILE_FLAGS_USER_OPAQUE:
2466 if (strcmp(name, "UserOpaque") == 0)
2467 xar->xmlsts = FILE_FLAGS;
2469 case FILE_FLAGS_USER_NOUNLINK:
2470 if (strcmp(name, "UserNoUnlink") == 0)
2471 xar->xmlsts = FILE_FLAGS;
2473 case FILE_FLAGS_SYS_ARCHIVED:
2474 if (strcmp(name, "SystemArchived") == 0)
2475 xar->xmlsts = FILE_FLAGS;
2477 case FILE_FLAGS_SYS_IMMUTABLE:
2478 if (strcmp(name, "SystemImmutable") == 0)
2479 xar->xmlsts = FILE_FLAGS;
2481 case FILE_FLAGS_SYS_APPEND:
2482 if (strcmp(name, "SystemAppend") == 0)
2483 xar->xmlsts = FILE_FLAGS;
2485 case FILE_FLAGS_SYS_NOUNLINK:
2486 if (strcmp(name, "SystemNoUnlink") == 0)
2487 xar->xmlsts = FILE_FLAGS;
2489 case FILE_FLAGS_SYS_SNAPSHOT:
2490 if (strcmp(name, "SystemSnapshot") == 0)
2491 xar->xmlsts = FILE_FLAGS;
2494 if (strcmp(name, "ext2") == 0)
2495 xar->xmlsts = TOC_FILE;
2497 case FILE_EXT2_SecureDeletion:
2498 if (strcmp(name, "SecureDeletion") == 0)
2499 xar->xmlsts = FILE_EXT2;
2501 case FILE_EXT2_Undelete:
2502 if (strcmp(name, "Undelete") == 0)
2503 xar->xmlsts = FILE_EXT2;
2505 case FILE_EXT2_Compress:
2506 if (strcmp(name, "Compress") == 0)
2507 xar->xmlsts = FILE_EXT2;
2509 case FILE_EXT2_Synchronous:
2510 if (strcmp(name, "Synchronous") == 0)
2511 xar->xmlsts = FILE_EXT2;
2513 case FILE_EXT2_Immutable:
2514 if (strcmp(name, "Immutable") == 0)
2515 xar->xmlsts = FILE_EXT2;
2517 case FILE_EXT2_AppendOnly:
2518 if (strcmp(name, "AppendOnly") == 0)
2519 xar->xmlsts = FILE_EXT2;
2521 case FILE_EXT2_NoDump:
2522 if (strcmp(name, "NoDump") == 0)
2523 xar->xmlsts = FILE_EXT2;
2525 case FILE_EXT2_NoAtime:
2526 if (strcmp(name, "NoAtime") == 0)
2527 xar->xmlsts = FILE_EXT2;
2529 case FILE_EXT2_CompDirty:
2530 if (strcmp(name, "CompDirty") == 0)
2531 xar->xmlsts = FILE_EXT2;
2533 case FILE_EXT2_CompBlock:
2534 if (strcmp(name, "CompBlock") == 0)
2535 xar->xmlsts = FILE_EXT2;
2537 case FILE_EXT2_NoCompBlock:
2538 if (strcmp(name, "NoCompBlock") == 0)
2539 xar->xmlsts = FILE_EXT2;
2541 case FILE_EXT2_CompError:
2542 if (strcmp(name, "CompError") == 0)
2543 xar->xmlsts = FILE_EXT2;
2545 case FILE_EXT2_BTree:
2546 if (strcmp(name, "BTree") == 0)
2547 xar->xmlsts = FILE_EXT2;
2549 case FILE_EXT2_HashIndexed:
2550 if (strcmp(name, "HashIndexed") == 0)
2551 xar->xmlsts = FILE_EXT2;
2553 case FILE_EXT2_iMagic:
2554 if (strcmp(name, "iMagic") == 0)
2555 xar->xmlsts = FILE_EXT2;
2557 case FILE_EXT2_Journaled:
2558 if (strcmp(name, "Journaled") == 0)
2559 xar->xmlsts = FILE_EXT2;
2561 case FILE_EXT2_NoTail:
2562 if (strcmp(name, "NoTail") == 0)
2563 xar->xmlsts = FILE_EXT2;
2565 case FILE_EXT2_DirSync:
2566 if (strcmp(name, "DirSync") == 0)
2567 xar->xmlsts = FILE_EXT2;
2569 case FILE_EXT2_TopDir:
2570 if (strcmp(name, "TopDir") == 0)
2571 xar->xmlsts = FILE_EXT2;
2573 case FILE_EXT2_Reserved:
2574 if (strcmp(name, "Reserved") == 0)
2575 xar->xmlsts = FILE_EXT2;
2578 unknowntag_end(xar, name);
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 */
2619 strappend_base64(struct xar *xar,
2620 struct archive_string *as, const char *s, size_t l)
2622 unsigned char buff[256];
2624 const unsigned char *b;
2629 b = (const unsigned char *)s;
2634 if (base64[b[0]] < 0 || base64[b[1]] < 0)
2636 n = base64[*b++] << 18;
2637 n |= base64[*b++] << 12;
2645 n |= base64[*b++] << 6;
2646 *out++ = (n >> 8) & 0xFF;
2658 if (len+3 >= sizeof(buff)) {
2659 archive_strncat(as, (const char *)buff, len);
2665 archive_strncat(as, (const char *)buff, len);
2669 xml_data(void *userData, const char *s, int len)
2671 struct archive_read *a;
2674 a = (struct archive_read *)userData;
2675 xar = (struct xar *)(a->format->data);
2680 if (len > sizeof(buff)-1)
2681 len = sizeof(buff)-1;
2682 memcpy(buff, s, len);
2684 fprintf(stderr, "\tlen=%d:\"%s\"\n", len, buff);
2687 switch (xar->xmlsts) {
2688 case TOC_CHECKSUM_OFFSET:
2689 xar->toc_chksum_offset = atol10(s, len);
2691 case TOC_CHECKSUM_SIZE:
2692 xar->toc_chksum_size = atol10(s, len);
2697 if (xar->file == NULL)
2700 switch (xar->xmlsts) {
2702 if (xar->file->parent != NULL) {
2703 archive_string_concat(&(xar->file->pathname),
2704 &(xar->file->parent->pathname));
2705 archive_strappend_char(&(xar->file->pathname), '/');
2707 xar->file->has |= HAS_PATHNAME;
2708 if (xar->base64text) {
2709 strappend_base64(xar,
2710 &(xar->file->pathname), s, len);
2712 archive_strncat(&(xar->file->pathname), s, len);
2715 xar->file->has |= HAS_SYMLINK;
2716 archive_strncpy(&(xar->file->symlink), s, len);
2719 if (strncmp("file", s, len) == 0 ||
2720 strncmp("hardlink", s, len) == 0)
2722 (xar->file->mode & ~AE_IFMT) | AE_IFREG;
2723 if (strncmp("directory", s, len) == 0)
2725 (xar->file->mode & ~AE_IFMT) | AE_IFDIR;
2726 if (strncmp("symlink", s, len) == 0)
2728 (xar->file->mode & ~AE_IFMT) | AE_IFLNK;
2729 if (strncmp("character special", s, len) == 0)
2731 (xar->file->mode & ~AE_IFMT) | AE_IFCHR;
2732 if (strncmp("block special", s, len) == 0)
2734 (xar->file->mode & ~AE_IFMT) | AE_IFBLK;
2735 if (strncmp("socket", s, len) == 0)
2737 (xar->file->mode & ~AE_IFMT) | AE_IFSOCK;
2738 if (strncmp("fifo", s, len) == 0)
2740 (xar->file->mode & ~AE_IFMT) | AE_IFIFO;
2741 xar->file->has |= HAS_TYPE;
2744 xar->file->has |= HAS_INO;
2745 xar->file->ino64 = atol10(s, len);
2747 case FILE_DEVICE_MAJOR:
2748 xar->file->has |= HAS_DEVMAJOR;
2749 xar->file->devmajor = (dev_t)atol10(s, len);
2751 case FILE_DEVICE_MINOR:
2752 xar->file->has |= HAS_DEVMINOR;
2753 xar->file->devminor = (dev_t)atol10(s, len);
2756 xar->file->has |= HAS_DEV;
2757 xar->file->dev = (dev_t)atol10(s, len);
2760 xar->file->has |= HAS_MODE;
2762 (xar->file->mode & AE_IFMT) |
2763 (atol8(s, len) & ~AE_IFMT);
2766 xar->file->has |= HAS_GID;
2767 archive_strncpy(&(xar->file->gname), s, len);
2770 xar->file->has |= HAS_GID;
2771 xar->file->gid = atol10(s, len);
2774 xar->file->has |= HAS_UID;
2775 archive_strncpy(&(xar->file->uname), s, len);
2778 xar->file->has |= HAS_UID;
2779 xar->file->uid = atol10(s, len);
2782 xar->file->has |= HAS_TIME;
2783 xar->file->ctime = parse_time(s, len);
2786 xar->file->has |= HAS_TIME;
2787 xar->file->mtime = parse_time(s, len);
2790 xar->file->has |= HAS_TIME;
2791 xar->file->atime = parse_time(s, len);
2793 case FILE_DATA_LENGTH:
2794 xar->file->has |= HAS_DATA;
2795 xar->file->length = atol10(s, len);
2797 case FILE_DATA_OFFSET:
2798 xar->file->has |= HAS_DATA;
2799 xar->file->offset = atol10(s, len);
2801 case FILE_DATA_SIZE:
2802 xar->file->has |= HAS_DATA;
2803 xar->file->size = atol10(s, len);
2805 case FILE_DATA_A_CHECKSUM:
2806 xar->file->a_sum.len = atohex(xar->file->a_sum.val,
2807 sizeof(xar->file->a_sum.val), s, len);
2809 case FILE_DATA_E_CHECKSUM:
2810 xar->file->e_sum.len = atohex(xar->file->e_sum.val,
2811 sizeof(xar->file->e_sum.val), s, len);
2813 case FILE_EA_LENGTH:
2814 xar->file->has |= HAS_XATTR;
2815 xar->xattr->length = atol10(s, len);
2817 case FILE_EA_OFFSET:
2818 xar->file->has |= HAS_XATTR;
2819 xar->xattr->offset = atol10(s, len);
2822 xar->file->has |= HAS_XATTR;
2823 xar->xattr->size = atol10(s, len);
2825 case FILE_EA_A_CHECKSUM:
2826 xar->file->has |= HAS_XATTR;
2827 xar->xattr->a_sum.len = atohex(xar->xattr->a_sum.val,
2828 sizeof(xar->xattr->a_sum.val), s, len);
2830 case FILE_EA_E_CHECKSUM:
2831 xar->file->has |= HAS_XATTR;
2832 xar->xattr->e_sum.len = atohex(xar->xattr->e_sum.val,
2833 sizeof(xar->xattr->e_sum.val), s, len);
2836 xar->file->has |= HAS_XATTR;
2837 archive_strncpy(&(xar->xattr->name), s, len);
2839 case FILE_EA_FSTYPE:
2840 xar->file->has |= HAS_XATTR;
2841 archive_strncpy(&(xar->xattr->fstype), s, len);
2844 case FILE_ACL_DEFAULT:
2845 case FILE_ACL_ACCESS:
2846 case FILE_ACL_APPLEEXTENDED:
2847 xar->file->has |= HAS_ACL;
2853 case TOC_CREATION_TIME:
2855 case TOC_CHECKSUM_OFFSET:
2856 case TOC_CHECKSUM_SIZE:
2859 case FILE_DATA_ENCODING:
2860 case FILE_DATA_CONTENT:
2863 case FILE_EA_ENCODING:
2866 case FILE_FLAGS_USER_NODUMP:
2867 case FILE_FLAGS_USER_IMMUTABLE:
2868 case FILE_FLAGS_USER_APPEND:
2869 case FILE_FLAGS_USER_OPAQUE:
2870 case FILE_FLAGS_USER_NOUNLINK:
2871 case FILE_FLAGS_SYS_ARCHIVED:
2872 case FILE_FLAGS_SYS_IMMUTABLE:
2873 case FILE_FLAGS_SYS_APPEND:
2874 case FILE_FLAGS_SYS_NOUNLINK:
2875 case FILE_FLAGS_SYS_SNAPSHOT:
2877 case FILE_EXT2_SecureDeletion:
2878 case FILE_EXT2_Undelete:
2879 case FILE_EXT2_Compress:
2880 case FILE_EXT2_Synchronous:
2881 case FILE_EXT2_Immutable:
2882 case FILE_EXT2_AppendOnly:
2883 case FILE_EXT2_NoDump:
2884 case FILE_EXT2_NoAtime:
2885 case FILE_EXT2_CompDirty:
2886 case FILE_EXT2_CompBlock:
2887 case FILE_EXT2_NoCompBlock:
2888 case FILE_EXT2_CompError:
2889 case FILE_EXT2_BTree:
2890 case FILE_EXT2_HashIndexed:
2891 case FILE_EXT2_iMagic:
2892 case FILE_EXT2_Journaled:
2893 case FILE_EXT2_NoTail:
2894 case FILE_EXT2_DirSync:
2895 case FILE_EXT2_TopDir:
2896 case FILE_EXT2_Reserved:
2906 xml_parse_file_flags(struct xar *xar, const char *name)
2908 const char *flag = NULL;
2910 if (strcmp(name, "UserNoDump") == 0) {
2911 xar->xmlsts = FILE_FLAGS_USER_NODUMP;
2914 else if (strcmp(name, "UserImmutable") == 0) {
2915 xar->xmlsts = FILE_FLAGS_USER_IMMUTABLE;
2916 flag = "uimmutable";
2918 else if (strcmp(name, "UserAppend") == 0) {
2919 xar->xmlsts = FILE_FLAGS_USER_APPEND;
2922 else if (strcmp(name, "UserOpaque") == 0) {
2923 xar->xmlsts = FILE_FLAGS_USER_OPAQUE;
2926 else if (strcmp(name, "UserNoUnlink") == 0) {
2927 xar->xmlsts = FILE_FLAGS_USER_NOUNLINK;
2930 else if (strcmp(name, "SystemArchived") == 0) {
2931 xar->xmlsts = FILE_FLAGS_SYS_ARCHIVED;
2934 else if (strcmp(name, "SystemImmutable") == 0) {
2935 xar->xmlsts = FILE_FLAGS_SYS_IMMUTABLE;
2936 flag = "simmutable";
2938 else if (strcmp(name, "SystemAppend") == 0) {
2939 xar->xmlsts = FILE_FLAGS_SYS_APPEND;
2942 else if (strcmp(name, "SystemNoUnlink") == 0) {
2943 xar->xmlsts = FILE_FLAGS_SYS_NOUNLINK;
2946 else if (strcmp(name, "SystemSnapshot") == 0) {
2947 xar->xmlsts = FILE_FLAGS_SYS_SNAPSHOT;
2953 xar->file->has |= HAS_FFLAGS;
2954 if (archive_strlen(&(xar->file->fflags_text)) > 0)
2955 archive_strappend_char(&(xar->file->fflags_text), ',');
2956 archive_strcat(&(xar->file->fflags_text), flag);
2964 xml_parse_file_ext2(struct xar *xar, const char *name)
2966 const char *flag = NULL;
2968 if (strcmp(name, "SecureDeletion") == 0) {
2969 xar->xmlsts = FILE_EXT2_SecureDeletion;
2970 flag = "securedeletion";
2972 else if (strcmp(name, "Undelete") == 0) {
2973 xar->xmlsts = FILE_EXT2_Undelete;
2976 else if (strcmp(name, "Compress") == 0) {
2977 xar->xmlsts = FILE_EXT2_Compress;
2980 else if (strcmp(name, "Synchronous") == 0) {
2981 xar->xmlsts = FILE_EXT2_Synchronous;
2984 else if (strcmp(name, "Immutable") == 0) {
2985 xar->xmlsts = FILE_EXT2_Immutable;
2986 flag = "simmutable";
2988 else if (strcmp(name, "AppendOnly") == 0) {
2989 xar->xmlsts = FILE_EXT2_AppendOnly;
2992 else if (strcmp(name, "NoDump") == 0) {
2993 xar->xmlsts = FILE_EXT2_NoDump;
2996 else if (strcmp(name, "NoAtime") == 0) {
2997 xar->xmlsts = FILE_EXT2_NoAtime;
3000 else if (strcmp(name, "CompDirty") == 0) {
3001 xar->xmlsts = FILE_EXT2_CompDirty;
3004 else if (strcmp(name, "CompBlock") == 0) {
3005 xar->xmlsts = FILE_EXT2_CompBlock;
3008 else if (strcmp(name, "NoCompBlock") == 0) {
3009 xar->xmlsts = FILE_EXT2_NoCompBlock;
3010 flag = "nocomprblk";
3012 else if (strcmp(name, "CompError") == 0) {
3013 xar->xmlsts = FILE_EXT2_CompError;
3016 else if (strcmp(name, "BTree") == 0) {
3017 xar->xmlsts = FILE_EXT2_BTree;
3020 else if (strcmp(name, "HashIndexed") == 0) {
3021 xar->xmlsts = FILE_EXT2_HashIndexed;
3024 else if (strcmp(name, "iMagic") == 0) {
3025 xar->xmlsts = FILE_EXT2_iMagic;
3028 else if (strcmp(name, "Journaled") == 0) {
3029 xar->xmlsts = FILE_EXT2_Journaled;
3032 else if (strcmp(name, "NoTail") == 0) {
3033 xar->xmlsts = FILE_EXT2_NoTail;
3036 else if (strcmp(name, "DirSync") == 0) {
3037 xar->xmlsts = FILE_EXT2_DirSync;
3040 else if (strcmp(name, "TopDir") == 0) {
3041 xar->xmlsts = FILE_EXT2_TopDir;
3044 else if (strcmp(name, "Reserved") == 0) {
3045 xar->xmlsts = FILE_EXT2_Reserved;
3051 if (archive_strlen(&(xar->file->fflags_text)) > 0)
3052 archive_strappend_char(&(xar->file->fflags_text), ',');
3053 archive_strcat(&(xar->file->fflags_text), flag);
3057 #ifdef HAVE_LIBXML_XMLREADER_H
3060 xml2_xmlattr_setup(struct archive_read *a,
3061 struct xmlattr_list *list, xmlTextReaderPtr reader)
3063 struct xmlattr *attr;
3067 list->last = &(list->first);
3068 r = xmlTextReaderMoveToFirstAttribute(reader);
3070 attr = malloc(sizeof*(attr));
3072 archive_set_error(&a->archive, ENOMEM, "Out of memory");
3073 return (ARCHIVE_FATAL);
3075 attr->name = strdup(
3076 (const char *)xmlTextReaderConstLocalName(reader));
3077 if (attr->name == NULL) {
3078 archive_set_error(&a->archive, ENOMEM, "Out of memory");
3079 return (ARCHIVE_FATAL);
3081 attr->value = strdup(
3082 (const char *)xmlTextReaderConstValue(reader));
3083 if (attr->value == NULL) {
3084 archive_set_error(&a->archive, ENOMEM, "Out of memory");
3085 return (ARCHIVE_FATAL);
3089 list->last = &(attr->next);
3090 r = xmlTextReaderMoveToNextAttribute(reader);
3096 xml2_read_cb(void *context, char *buffer, int len)
3098 struct archive_read *a;
3105 a = (struct archive_read *)context;
3106 xar = (struct xar *)(a->format->data);
3108 if (xar->toc_remaining <= 0)
3112 r = rd_contents(a, &d, &outbytes, &used, xar->toc_remaining);
3113 if (r != ARCHIVE_OK)
3115 __archive_read_consume(a, used);
3116 xar->toc_remaining -= used;
3117 xar->offset += used;
3118 xar->toc_total += outbytes;
3119 PRINT_TOC(buffer, len);
3121 return ((int)outbytes);
3125 xml2_close_cb(void *context)
3128 (void)context; /* UNUSED */
3133 xml2_error_hdr(void *arg, const char *msg, xmlParserSeverities severity,
3134 xmlTextReaderLocatorPtr locator)
3136 struct archive_read *a;
3138 (void)locator; /* UNUSED */
3139 a = (struct archive_read *)arg;
3141 case XML_PARSER_SEVERITY_VALIDITY_WARNING:
3142 case XML_PARSER_SEVERITY_WARNING:
3143 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3144 "XML Parsing error: %s", msg);
3146 case XML_PARSER_SEVERITY_VALIDITY_ERROR:
3147 case XML_PARSER_SEVERITY_ERROR:
3148 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3149 "XML Parsing error: %s", msg);
3155 xml2_read_toc(struct archive_read *a)
3157 xmlTextReaderPtr reader;
3158 struct xmlattr_list list;
3161 reader = xmlReaderForIO(xml2_read_cb, xml2_close_cb, a, NULL, NULL, 0);
3162 if (reader == NULL) {
3163 archive_set_error(&a->archive, ENOMEM,
3164 "Couldn't allocate memory for xml parser");
3165 return (ARCHIVE_FATAL);
3167 xmlTextReaderSetErrorHandler(reader, xml2_error_hdr, a);
3169 while ((r = xmlTextReaderRead(reader)) == 1) {
3170 const char *name, *value;
3173 type = xmlTextReaderNodeType(reader);
3174 name = (const char *)xmlTextReaderConstLocalName(reader);
3176 case XML_READER_TYPE_ELEMENT:
3177 empty = xmlTextReaderIsEmptyElement(reader);
3178 r = xml2_xmlattr_setup(a, &list, reader);
3179 if (r != ARCHIVE_OK)
3181 r = xml_start(a, name, &list);
3182 xmlattr_cleanup(&list);
3183 if (r != ARCHIVE_OK)
3188 case XML_READER_TYPE_END_ELEMENT:
3191 case XML_READER_TYPE_TEXT:
3192 value = (const char *)xmlTextReaderConstValue(reader);
3193 xml_data(a, value, strlen(value));
3195 case XML_READER_TYPE_SIGNIFICANT_WHITESPACE:
3202 xmlFreeTextReader(reader);
3205 return ((r == 0)?ARCHIVE_OK:ARCHIVE_FATAL);
3208 #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
3211 expat_xmlattr_setup(struct archive_read *a,
3212 struct xmlattr_list *list, const XML_Char **atts)
3214 struct xmlattr *attr;
3218 list->last = &(list->first);
3220 return (ARCHIVE_OK);
3221 while (atts[0] != NULL && atts[1] != NULL) {
3222 attr = malloc(sizeof*(attr));
3223 name = strdup(atts[0]);
3224 value = strdup(atts[1]);
3225 if (attr == NULL || name == NULL || value == NULL) {
3226 archive_set_error(&a->archive, ENOMEM, "Out of memory");
3227 return (ARCHIVE_FATAL);
3230 attr->value = value;
3233 list->last = &(attr->next);
3236 return (ARCHIVE_OK);
3240 expat_start_cb(void *userData, const XML_Char *name, const XML_Char **atts)
3242 struct expat_userData *ud = (struct expat_userData *)userData;
3243 struct archive_read *a = ud->archive;
3244 struct xmlattr_list list;
3247 r = expat_xmlattr_setup(a, &list, atts);
3248 if (r == ARCHIVE_OK)
3249 r = xml_start(a, (const char *)name, &list);
3250 xmlattr_cleanup(&list);
3255 expat_end_cb(void *userData, const XML_Char *name)
3257 struct expat_userData *ud = (struct expat_userData *)userData;
3259 xml_end(ud->archive, (const char *)name);
3263 expat_data_cb(void *userData, const XML_Char *s, int len)
3265 struct expat_userData *ud = (struct expat_userData *)userData;
3267 xml_data(ud->archive, s, len);
3271 expat_read_toc(struct archive_read *a)
3275 struct expat_userData ud;
3277 ud.state = ARCHIVE_OK;
3280 xar = (struct xar *)(a->format->data);
3282 /* Initialize XML Parser library. */
3283 parser = XML_ParserCreate(NULL);
3284 if (parser == NULL) {
3285 archive_set_error(&a->archive, ENOMEM,
3286 "Couldn't allocate memory for xml parser");
3287 return (ARCHIVE_FATAL);
3289 XML_SetUserData(parser, &ud);
3290 XML_SetElementHandler(parser, expat_start_cb, expat_end_cb);
3291 XML_SetCharacterDataHandler(parser, expat_data_cb);
3294 while (xar->toc_remaining && ud.state == ARCHIVE_OK) {
3302 r = rd_contents(a, &d, &outbytes, &used, xar->toc_remaining);
3303 if (r != ARCHIVE_OK)
3305 xar->toc_remaining -= used;
3306 xar->offset += used;
3307 xar->toc_total += outbytes;
3308 PRINT_TOC(d, outbytes);
3310 xr = XML_Parse(parser, d, outbytes, xar->toc_remaining == 0);
3311 __archive_read_consume(a, used);
3312 if (xr == XML_STATUS_ERROR) {
3313 XML_ParserFree(parser);
3314 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3315 "XML Parsing failed");
3316 return (ARCHIVE_FATAL);
3319 XML_ParserFree(parser);
3322 #endif /* defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H) */
3324 #endif /* Support xar format */