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 int decompression_init(struct archive_read *, enum enctype);
398 static int decompress(struct archive_read *, const void **,
399 size_t *, const void *, size_t *);
400 static int decompression_cleanup(struct archive_read *);
401 static void xmlattr_cleanup(struct xmlattr_list *);
402 static int file_new(struct archive_read *,
403 struct xar *, struct xmlattr_list *);
404 static void file_free(struct xar_file *);
405 static int xattr_new(struct archive_read *,
406 struct xar *, struct xmlattr_list *);
407 static void xattr_free(struct xattr *);
408 static int getencoding(struct xmlattr_list *);
409 static int getsumalgorithm(struct xmlattr_list *);
410 static int unknowntag_start(struct archive_read *,
411 struct xar *, const char *);
412 static void unknowntag_end(struct xar *, const char *);
413 static int xml_start(struct archive_read *,
414 const char *, struct xmlattr_list *);
415 static void xml_end(void *, const char *);
416 static void xml_data(void *, const char *, int);
417 static int xml_parse_file_flags(struct xar *, const char *);
418 static int xml_parse_file_ext2(struct xar *, const char *);
419 #if defined(HAVE_LIBXML_XMLREADER_H)
420 static int xml2_xmlattr_setup(struct archive_read *,
421 struct xmlattr_list *, xmlTextReaderPtr);
422 static int xml2_read_cb(void *, char *, int);
423 static int xml2_close_cb(void *);
424 static void xml2_error_hdr(void *, const char *, xmlParserSeverities,
425 xmlTextReaderLocatorPtr);
426 static int xml2_read_toc(struct archive_read *);
427 #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
428 struct expat_userData {
430 struct archive_read *archive;
432 static int expat_xmlattr_setup(struct archive_read *,
433 struct xmlattr_list *, const XML_Char **);
434 static void expat_start_cb(void *, const XML_Char *, const XML_Char **);
435 static void expat_end_cb(void *, const XML_Char *);
436 static void expat_data_cb(void *, const XML_Char *, int);
437 static int expat_read_toc(struct archive_read *);
441 archive_read_support_format_xar(struct archive *_a)
444 struct archive_read *a = (struct archive_read *)_a;
447 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
448 ARCHIVE_STATE_NEW, "archive_read_support_format_xar");
450 xar = (struct xar *)calloc(1, sizeof(*xar));
452 archive_set_error(&a->archive, ENOMEM,
453 "Can't allocate xar data");
454 return (ARCHIVE_FATAL);
457 r = __archive_read_register_format(a,
475 xar_bid(struct archive_read *a, int best_bid)
477 const unsigned char *b;
480 (void)best_bid; /* UNUSED */
482 b = __archive_read_ahead(a, HEADER_SIZE, NULL);
490 if (archive_be32dec(b) != HEADER_MAGIC)
496 if (archive_be16dec(b+4) != HEADER_SIZE)
500 * Verify header version
502 if (archive_be16dec(b+6) != HEADER_VERSION)
506 * Verify type of checksum
508 switch (archive_be32dec(b+24)) {
522 read_toc(struct archive_read *a)
525 struct xar_file *file;
526 const unsigned char *b;
527 uint64_t toc_compressed_size;
528 uint64_t toc_uncompressed_size;
529 uint32_t toc_chksum_alg;
533 xar = (struct xar *)(a->format->data);
538 b = __archive_read_ahead(a, HEADER_SIZE, &bytes);
541 if (bytes < HEADER_SIZE) {
542 archive_set_error(&a->archive,
543 ARCHIVE_ERRNO_FILE_FORMAT,
544 "Truncated archive header");
545 return (ARCHIVE_FATAL);
548 if (archive_be32dec(b) != HEADER_MAGIC) {
549 archive_set_error(&a->archive,
550 ARCHIVE_ERRNO_FILE_FORMAT,
551 "Invalid header magic");
552 return (ARCHIVE_FATAL);
554 if (archive_be16dec(b+6) != HEADER_VERSION) {
555 archive_set_error(&a->archive,
556 ARCHIVE_ERRNO_FILE_FORMAT,
557 "Unsupported header version(%d)",
558 archive_be16dec(b+6));
559 return (ARCHIVE_FATAL);
561 toc_compressed_size = archive_be64dec(b+8);
562 xar->toc_remaining = toc_compressed_size;
563 toc_uncompressed_size = archive_be64dec(b+16);
564 toc_chksum_alg = archive_be32dec(b+24);
565 __archive_read_consume(a, HEADER_SIZE);
566 xar->offset += HEADER_SIZE;
570 * Read TOC(Table of Contents).
572 /* Initialize reading contents. */
573 r = move_reading_point(a, HEADER_SIZE);
576 r = rd_contents_init(a, GZIP, toc_chksum_alg, CKSUM_NONE);
580 #ifdef HAVE_LIBXML_XMLREADER_H
581 r = xml2_read_toc(a);
582 #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
583 r = expat_read_toc(a);
588 /* Set 'The HEAP' base. */
589 xar->h_base = xar->offset;
590 if (xar->toc_total != toc_uncompressed_size) {
591 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
592 "TOC uncompressed size error");
593 return (ARCHIVE_FATAL);
599 if (toc_chksum_alg != CKSUM_NONE) {
600 r = move_reading_point(a, xar->toc_chksum_offset);
603 b = __archive_read_ahead(a,
604 (size_t)xar->toc_chksum_size, &bytes);
607 if ((uint64_t)bytes < xar->toc_chksum_size) {
608 archive_set_error(&a->archive,
609 ARCHIVE_ERRNO_FILE_FORMAT,
610 "Truncated archive file");
611 return (ARCHIVE_FATAL);
613 r = checksum_final(a, b,
614 (size_t)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 free(xar->file_queue.files);
937 while (xar->unknowntags != NULL) {
938 struct unknown_tag *tag;
940 tag = xar->unknowntags;
941 xar->unknowntags = tag->next;
942 archive_string_free(&(tag->name));
947 a->format->data = NULL;
952 move_reading_point(struct archive_read *a, uint64_t offset)
956 xar = (struct xar *)(a->format->data);
957 if (xar->offset - xar->h_base != offset) {
958 /* Seek forward to the start of file contents. */
961 step = offset - (xar->offset - xar->h_base);
963 step = __archive_read_consume(a, step);
968 int64_t pos = __archive_read_seek(a, offset, SEEK_SET);
969 if (pos == ARCHIVE_FAILED) {
970 archive_set_error(&(a->archive),
973 return (ARCHIVE_FAILED);
982 rd_contents_init(struct archive_read *a, enum enctype encoding,
983 int a_sum_alg, int e_sum_alg)
987 /* Init decompress library. */
988 if ((r = decompression_init(a, encoding)) != ARCHIVE_OK)
990 /* Init checksum library. */
991 checksum_init(a, a_sum_alg, e_sum_alg);
996 rd_contents(struct archive_read *a, const void **buff, size_t *size,
997 size_t *used, uint64_t remaining)
999 const unsigned char *b;
1002 /* Get whatever bytes are immediately available. */
1003 b = __archive_read_ahead(a, 1, &bytes);
1005 return ((int)bytes);
1007 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1008 "Truncated archive file");
1009 return (ARCHIVE_FATAL);
1011 if ((uint64_t)bytes > remaining)
1012 bytes = (ssize_t)remaining;
1015 * Decompress contents of file.
1018 if (decompress(a, buff, size, b, used) != ARCHIVE_OK)
1019 return (ARCHIVE_FATAL);
1022 * Update checksum of a compressed data and a extracted data.
1024 checksum_update(a, b, *used, *buff, *size);
1026 return (ARCHIVE_OK);
1030 * Note that this implementation does not (and should not!) obey
1031 * locale settings; you cannot simply substitute strtol here, since
1032 * it does obey locale.
1036 atol10(const char *p, size_t char_cnt)
1043 while (digit >= 0 && digit < 10 && char_cnt-- > 0) {
1044 l = (l * 10) + digit;
1051 atol8(const char *p, size_t char_cnt)
1057 while (char_cnt-- > 0) {
1058 if (*p >= '0' && *p <= '7')
1070 atohex(unsigned char *b, size_t bsize, const char *p, size_t psize)
1072 size_t fbsize = bsize;
1074 while (bsize && psize > 1) {
1077 if (p[0] >= 'a' && p[0] <= 'z')
1078 x = (p[0] - 'a' + 0x0a) << 4;
1079 else if (p[0] >= 'A' && p[0] <= 'Z')
1080 x = (p[0] - 'A' + 0x0a) << 4;
1081 else if (p[0] >= '0' && p[0] <= '9')
1082 x = (p[0] - '0') << 4;
1085 if (p[1] >= 'a' && p[1] <= 'z')
1086 x |= p[1] - 'a' + 0x0a;
1087 else if (p[1] >= 'A' && p[1] <= 'Z')
1088 x |= p[1] - 'A' + 0x0a;
1089 else if (p[1] >= '0' && p[1] <= '9')
1099 return (fbsize - bsize);
1103 time_from_tm(struct tm *t)
1106 /* Use platform timegm() if available. */
1108 #elif HAVE__MKGMTIME64
1109 return (_mkgmtime64(t));
1111 /* Else use direct calculation using POSIX assumptions. */
1112 /* First, fix up tm_yday based on the year/month/day. */
1114 /* Then we can compute timegm() from first principles. */
1118 + t->tm_yday * 86400
1119 + (t->tm_year - 70) * 31536000
1120 + ((t->tm_year - 69) / 4) * 86400
1121 - ((t->tm_year - 1) / 100) * 86400
1122 + ((t->tm_year + 299) / 400) * 86400);
1127 parse_time(const char *p, size_t n)
1133 memset(&tm, 0, sizeof(tm));
1136 data = atol10(p, 4);
1139 tm.tm_year = (int)data - 1900;
1143 data = atol10(p, 2);
1144 if (data < 1 || data > 12)
1146 tm.tm_mon = (int)data -1;
1150 data = atol10(p, 2);
1151 if (data < 1 || data > 31)
1153 tm.tm_mday = (int)data;
1157 data = atol10(p, 2);
1158 if (data < 0 || data > 23)
1160 tm.tm_hour = (int)data;
1164 data = atol10(p, 2);
1165 if (data < 0 || data > 59)
1167 tm.tm_min = (int)data;
1171 data = atol10(p, 2);
1172 if (data < 0 || data > 60)
1174 tm.tm_sec = (int)data;
1181 t = time_from_tm(&tm);
1187 heap_add_entry(struct archive_read *a,
1188 struct heap_queue *heap, struct xar_file *file)
1190 uint64_t file_id, parent_id;
1193 /* Expand our pending files list as necessary. */
1194 if (heap->used >= heap->allocated) {
1195 struct xar_file **new_pending_files;
1196 int new_size = heap->allocated * 2;
1198 if (heap->allocated < 1024)
1200 /* Overflow might keep us from growing the list. */
1201 if (new_size <= heap->allocated) {
1202 archive_set_error(&a->archive,
1203 ENOMEM, "Out of memory");
1204 return (ARCHIVE_FATAL);
1206 new_pending_files = (struct xar_file **)
1207 malloc(new_size * sizeof(new_pending_files[0]));
1208 if (new_pending_files == NULL) {
1209 archive_set_error(&a->archive,
1210 ENOMEM, "Out of memory");
1211 return (ARCHIVE_FATAL);
1213 memcpy(new_pending_files, heap->files,
1214 heap->allocated * sizeof(new_pending_files[0]));
1215 if (heap->files != NULL)
1217 heap->files = new_pending_files;
1218 heap->allocated = new_size;
1224 * Start with hole at end, walk it up tree to find insertion point.
1226 hole = heap->used++;
1228 parent = (hole - 1)/2;
1229 parent_id = heap->files[parent]->id;
1230 if (file_id >= parent_id) {
1231 heap->files[hole] = file;
1232 return (ARCHIVE_OK);
1234 /* Move parent into hole <==> move hole up tree. */
1235 heap->files[hole] = heap->files[parent];
1238 heap->files[0] = file;
1240 return (ARCHIVE_OK);
1243 static struct xar_file *
1244 heap_get_entry(struct heap_queue *heap)
1246 uint64_t a_id, b_id, c_id;
1248 struct xar_file *r, *tmp;
1254 * The first file in the list is the earliest; we'll return this.
1259 * Move the last item in the heap to the root of the tree
1261 heap->files[0] = heap->files[--(heap->used)];
1264 * Rebalance the heap.
1266 a = 0; /* Starting element and its heap key */
1267 a_id = heap->files[a]->id;
1269 b = a + a + 1; /* First child */
1270 if (b >= heap->used)
1272 b_id = heap->files[b]->id;
1273 c = b + 1; /* Use second child if it is smaller. */
1274 if (c < heap->used) {
1275 c_id = heap->files[c]->id;
1283 tmp = heap->files[a];
1284 heap->files[a] = heap->files[b];
1285 heap->files[b] = tmp;
1291 add_link(struct archive_read *a, struct xar *xar, struct xar_file *file)
1293 struct hdlink *hdlink;
1295 for (hdlink = xar->hdlink_list; hdlink != NULL; hdlink = hdlink->next) {
1296 if (hdlink->id == file->link) {
1297 file->hdnext = hdlink->files;
1299 hdlink->files = file;
1300 return (ARCHIVE_OK);
1303 hdlink = malloc(sizeof(*hdlink));
1304 if (hdlink == NULL) {
1305 archive_set_error(&a->archive, ENOMEM, "Out of memory");
1306 return (ARCHIVE_FATAL);
1308 file->hdnext = NULL;
1309 hdlink->id = file->link;
1311 hdlink->files = file;
1312 hdlink->next = xar->hdlink_list;
1313 xar->hdlink_list = hdlink;
1314 return (ARCHIVE_OK);
1318 _checksum_init(struct chksumwork *sumwrk, int sum_alg)
1320 sumwrk->alg = sum_alg;
1325 archive_sha1_init(&(sumwrk->sha1ctx));
1328 archive_md5_init(&(sumwrk->md5ctx));
1334 _checksum_update(struct chksumwork *sumwrk, const void *buff, size_t size)
1337 switch (sumwrk->alg) {
1341 archive_sha1_update(&(sumwrk->sha1ctx), buff, size);
1344 archive_md5_update(&(sumwrk->md5ctx), buff, size);
1350 _checksum_final(struct chksumwork *sumwrk, const void *val, size_t len)
1352 unsigned char sum[MAX_SUM_SIZE];
1355 switch (sumwrk->alg) {
1359 archive_sha1_final(&(sumwrk->sha1ctx), sum);
1360 if (len != SHA1_SIZE ||
1361 memcmp(val, sum, SHA1_SIZE) != 0)
1365 archive_md5_final(&(sumwrk->md5ctx), sum);
1366 if (len != MD5_SIZE ||
1367 memcmp(val, sum, MD5_SIZE) != 0)
1375 checksum_init(struct archive_read *a, int a_sum_alg, int e_sum_alg)
1379 xar = (struct xar *)(a->format->data);
1380 _checksum_init(&(xar->a_sumwrk), a_sum_alg);
1381 _checksum_init(&(xar->e_sumwrk), e_sum_alg);
1385 checksum_update(struct archive_read *a, const void *abuff, size_t asize,
1386 const void *ebuff, size_t esize)
1390 xar = (struct xar *)(a->format->data);
1391 _checksum_update(&(xar->a_sumwrk), abuff, asize);
1392 _checksum_update(&(xar->e_sumwrk), ebuff, esize);
1396 checksum_final(struct archive_read *a, const void *a_sum_val,
1397 size_t a_sum_len, const void *e_sum_val, size_t e_sum_len)
1402 xar = (struct xar *)(a->format->data);
1403 r = _checksum_final(&(xar->a_sumwrk), a_sum_val, a_sum_len);
1404 if (r == ARCHIVE_OK)
1405 r = _checksum_final(&(xar->e_sumwrk), e_sum_val, e_sum_len);
1406 if (r != ARCHIVE_OK)
1407 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
1413 decompression_init(struct archive_read *a, enum enctype encoding)
1419 xar = (struct xar *)(a->format->data);
1420 xar->rd_encoding = encoding;
1425 if (xar->stream_valid)
1426 r = inflateReset(&(xar->stream));
1428 r = inflateInit(&(xar->stream));
1430 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1431 "Couldn't initialize zlib stream.");
1432 return (ARCHIVE_FATAL);
1434 xar->stream_valid = 1;
1435 xar->stream.total_in = 0;
1436 xar->stream.total_out = 0;
1438 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1440 if (xar->bzstream_valid) {
1441 BZ2_bzDecompressEnd(&(xar->bzstream));
1442 xar->bzstream_valid = 0;
1444 r = BZ2_bzDecompressInit(&(xar->bzstream), 0, 0);
1445 if (r == BZ_MEM_ERROR)
1446 r = BZ2_bzDecompressInit(&(xar->bzstream), 0, 1);
1448 int err = ARCHIVE_ERRNO_MISC;
1451 case BZ_PARAM_ERROR:
1452 detail = "invalid setup parameter";
1456 detail = "out of memory";
1458 case BZ_CONFIG_ERROR:
1459 detail = "mis-compiled library";
1462 archive_set_error(&a->archive, err,
1463 "Internal error initializing decompressor: %s",
1464 detail == NULL ? "??" : detail);
1465 xar->bzstream_valid = 0;
1466 return (ARCHIVE_FATAL);
1468 xar->bzstream_valid = 1;
1469 xar->bzstream.total_in_lo32 = 0;
1470 xar->bzstream.total_in_hi32 = 0;
1471 xar->bzstream.total_out_lo32 = 0;
1472 xar->bzstream.total_out_hi32 = 0;
1475 #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1476 #if LZMA_VERSION_MAJOR >= 5
1477 /* Effectively disable the limiter. */
1478 #define LZMA_MEMLIMIT UINT64_MAX
1480 /* NOTE: This needs to check memory size which running system has. */
1481 #define LZMA_MEMLIMIT (1U << 30)
1485 if (xar->lzstream_valid) {
1486 lzma_end(&(xar->lzstream));
1487 xar->lzstream_valid = 0;
1489 if (xar->entry_encoding == XZ)
1490 r = lzma_stream_decoder(&(xar->lzstream),
1491 LZMA_MEMLIMIT,/* memlimit */
1494 r = lzma_alone_decoder(&(xar->lzstream),
1495 LZMA_MEMLIMIT);/* memlimit */
1498 case LZMA_MEM_ERROR:
1499 archive_set_error(&a->archive,
1501 "Internal error initializing "
1502 "compression library: "
1503 "Cannot allocate memory");
1505 case LZMA_OPTIONS_ERROR:
1506 archive_set_error(&a->archive,
1508 "Internal error initializing "
1509 "compression library: "
1510 "Invalid or unsupported options");
1513 archive_set_error(&a->archive,
1515 "Internal error initializing "
1519 return (ARCHIVE_FATAL);
1521 xar->lzstream_valid = 1;
1522 xar->lzstream.total_in = 0;
1523 xar->lzstream.total_out = 0;
1527 * Unsupported compression.
1530 #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR)
1533 #if !defined(HAVE_LZMA_H) || !defined(HAVE_LIBLZMA)
1537 switch (xar->entry_encoding) {
1538 case BZIP2: detail = "bzip2"; break;
1539 case LZMA: detail = "lzma"; break;
1540 case XZ: detail = "xz"; break;
1541 default: detail = "??"; break;
1543 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1544 "%s compression not supported on this platform",
1546 return (ARCHIVE_FAILED);
1548 return (ARCHIVE_OK);
1552 decompress(struct archive_read *a, const void **buff, size_t *outbytes,
1553 const void *b, size_t *used)
1557 size_t avail_in, avail_out;
1560 xar = (struct xar *)(a->format->data);
1562 outbuff = (void *)(uintptr_t)*buff;
1563 if (outbuff == NULL) {
1564 if (xar->outbuff == NULL) {
1565 xar->outbuff = malloc(OUTBUFF_SIZE);
1566 if (xar->outbuff == NULL) {
1567 archive_set_error(&a->archive, ENOMEM,
1568 "Couldn't allocate memory for out buffer");
1569 return (ARCHIVE_FATAL);
1572 outbuff = xar->outbuff;
1574 avail_out = OUTBUFF_SIZE;
1576 avail_out = *outbytes;
1577 switch (xar->rd_encoding) {
1579 xar->stream.next_in = (Bytef *)(uintptr_t)b;
1580 xar->stream.avail_in = avail_in;
1581 xar->stream.next_out = (unsigned char *)outbuff;
1582 xar->stream.avail_out = avail_out;
1583 r = inflate(&(xar->stream), 0);
1585 case Z_OK: /* Decompressor made some progress.*/
1586 case Z_STREAM_END: /* Found end of stream. */
1589 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1590 "File decompression failed (%d)", r);
1591 return (ARCHIVE_FATAL);
1593 *used = avail_in - xar->stream.avail_in;
1594 *outbytes = avail_out - xar->stream.avail_out;
1596 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1598 xar->bzstream.next_in = (char *)(uintptr_t)b;
1599 xar->bzstream.avail_in = avail_in;
1600 xar->bzstream.next_out = (char *)outbuff;
1601 xar->bzstream.avail_out = avail_out;
1602 r = BZ2_bzDecompress(&(xar->bzstream));
1604 case BZ_STREAM_END: /* Found end of stream. */
1605 switch (BZ2_bzDecompressEnd(&(xar->bzstream))) {
1609 archive_set_error(&(a->archive),
1611 "Failed to clean up decompressor");
1612 return (ARCHIVE_FATAL);
1614 xar->bzstream_valid = 0;
1616 case BZ_OK: /* Decompressor made some progress. */
1619 archive_set_error(&(a->archive),
1621 "bzip decompression failed");
1622 return (ARCHIVE_FATAL);
1624 *used = avail_in - xar->bzstream.avail_in;
1625 *outbytes = avail_out - xar->bzstream.avail_out;
1628 #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1631 xar->lzstream.next_in = b;
1632 xar->lzstream.avail_in = avail_in;
1633 xar->lzstream.next_out = (unsigned char *)outbuff;
1634 xar->lzstream.avail_out = avail_out;
1635 r = lzma_code(&(xar->lzstream), LZMA_RUN);
1637 case LZMA_STREAM_END: /* Found end of stream. */
1638 lzma_end(&(xar->lzstream));
1639 xar->lzstream_valid = 0;
1641 case LZMA_OK: /* Decompressor made some progress. */
1644 archive_set_error(&(a->archive),
1646 "%s decompression failed(%d)",
1647 (xar->entry_encoding == XZ)?"xz":"lzma",
1649 return (ARCHIVE_FATAL);
1651 *used = avail_in - xar->lzstream.avail_in;
1652 *outbytes = avail_out - xar->lzstream.avail_out;
1655 #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR)
1658 #if !defined(HAVE_LZMA_H) || !defined(HAVE_LIBLZMA)
1664 if (outbuff == xar->outbuff) {
1667 *outbytes = avail_in;
1669 if (avail_out > avail_in)
1670 avail_out = avail_in;
1671 memcpy(outbuff, b, avail_out);
1673 *outbytes = avail_out;
1677 return (ARCHIVE_OK);
1681 decompression_cleanup(struct archive_read *a)
1686 xar = (struct xar *)(a->format->data);
1688 if (xar->stream_valid) {
1689 if (inflateEnd(&(xar->stream)) != Z_OK) {
1690 archive_set_error(&a->archive,
1692 "Failed to clean up zlib decompressor");
1696 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1697 if (xar->bzstream_valid) {
1698 if (BZ2_bzDecompressEnd(&(xar->bzstream)) != BZ_OK) {
1699 archive_set_error(&a->archive,
1701 "Failed to clean up bzip2 decompressor");
1706 #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1707 if (xar->lzstream_valid)
1708 lzma_end(&(xar->lzstream));
1709 #elif defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1710 if (xar->lzstream_valid) {
1711 if (lzmadec_end(&(xar->lzstream)) != LZMADEC_OK) {
1712 archive_set_error(&a->archive,
1714 "Failed to clean up lzmadec decompressor");
1723 xmlattr_cleanup(struct xmlattr_list *list)
1725 struct xmlattr *attr, *next;
1728 while (attr != NULL) {
1736 list->last = &(list->first);
1740 file_new(struct archive_read *a, struct xar *xar, struct xmlattr_list *list)
1742 struct xar_file *file;
1743 struct xmlattr *attr;
1745 file = calloc(1, sizeof(*file));
1747 archive_set_error(&a->archive, ENOMEM, "Out of memory");
1748 return (ARCHIVE_FATAL);
1750 file->parent = xar->file;
1751 file->mode = 0777 | AE_IFREG;
1752 file->atime = time(NULL);
1753 file->mtime = time(NULL);
1756 for (attr = list->first; attr != NULL; attr = attr->next) {
1757 if (strcmp(attr->name, "id") == 0)
1758 file->id = atol10(attr->value, strlen(attr->value));
1761 if (heap_add_entry(a, &(xar->file_queue), file) != ARCHIVE_OK)
1762 return (ARCHIVE_FATAL);
1763 return (ARCHIVE_OK);
1767 file_free(struct xar_file *file)
1769 struct xattr *xattr;
1771 archive_string_free(&(file->pathname));
1772 archive_string_free(&(file->symlink));
1773 archive_string_free(&(file->uname));
1774 archive_string_free(&(file->gname));
1775 archive_string_free(&(file->hardlink));
1776 xattr = file->xattr_list;
1777 while (xattr != NULL) {
1789 xattr_new(struct archive_read *a, struct xar *xar, struct xmlattr_list *list)
1791 struct xattr *xattr, **nx;
1792 struct xmlattr *attr;
1794 xattr = calloc(1, sizeof(*xattr));
1795 if (xattr == NULL) {
1796 archive_set_error(&a->archive, ENOMEM, "Out of memory");
1797 return (ARCHIVE_FATAL);
1800 for (attr = list->first; attr != NULL; attr = attr->next) {
1801 if (strcmp(attr->name, "id") == 0)
1802 xattr->id = atol10(attr->value, strlen(attr->value));
1804 /* Chain to xattr list. */
1805 for (nx = &(xar->file->xattr_list);
1806 *nx != NULL; nx = &((*nx)->next)) {
1807 if (xattr->id < (*nx)->id)
1813 return (ARCHIVE_OK);
1817 xattr_free(struct xattr *xattr)
1819 archive_string_free(&(xattr->name));
1824 getencoding(struct xmlattr_list *list)
1826 struct xmlattr *attr;
1827 enum enctype encoding = NONE;
1829 for (attr = list->first; attr != NULL; attr = attr->next) {
1830 if (strcmp(attr->name, "style") == 0) {
1831 if (strcmp(attr->value, "application/octet-stream") == 0)
1833 else if (strcmp(attr->value, "application/x-gzip") == 0)
1835 else if (strcmp(attr->value, "application/x-bzip2") == 0)
1837 else if (strcmp(attr->value, "application/x-lzma") == 0)
1839 else if (strcmp(attr->value, "application/x-xz") == 0)
1847 getsumalgorithm(struct xmlattr_list *list)
1849 struct xmlattr *attr;
1850 int alg = CKSUM_NONE;
1852 for (attr = list->first; attr != NULL; attr = attr->next) {
1853 if (strcmp(attr->name, "style") == 0) {
1854 const char *v = attr->value;
1855 if ((v[0] == 'S' || v[0] == 's') &&
1856 (v[1] == 'H' || v[1] == 'h') &&
1857 (v[2] == 'A' || v[2] == 'a') &&
1858 v[3] == '1' && v[4] == '\0')
1860 if ((v[0] == 'M' || v[0] == 'm') &&
1861 (v[1] == 'D' || v[1] == 'd') &&
1862 v[2] == '5' && v[3] == '\0')
1870 unknowntag_start(struct archive_read *a, struct xar *xar, const char *name)
1872 struct unknown_tag *tag;
1874 tag = malloc(sizeof(*tag));
1876 archive_set_error(&a->archive, ENOMEM, "Out of memory");
1877 return (ARCHIVE_FATAL);
1879 tag->next = xar->unknowntags;
1880 archive_string_init(&(tag->name));
1881 archive_strcpy(&(tag->name), name);
1882 if (xar->unknowntags == NULL) {
1884 fprintf(stderr, "UNKNOWNTAG_START:%s\n", name);
1886 xar->xmlsts_unknown = xar->xmlsts;
1887 xar->xmlsts = UNKNOWN;
1889 xar->unknowntags = tag;
1890 return (ARCHIVE_OK);
1894 unknowntag_end(struct xar *xar, const char *name)
1896 struct unknown_tag *tag;
1898 tag = xar->unknowntags;
1899 if (tag == NULL || name == NULL)
1901 if (strcmp(tag->name.s, name) == 0) {
1902 xar->unknowntags = tag->next;
1903 archive_string_free(&(tag->name));
1905 if (xar->unknowntags == NULL) {
1907 fprintf(stderr, "UNKNOWNTAG_END:%s\n", name);
1909 xar->xmlsts = xar->xmlsts_unknown;
1915 xml_start(struct archive_read *a, const char *name, struct xmlattr_list *list)
1918 struct xmlattr *attr;
1920 xar = (struct xar *)(a->format->data);
1923 fprintf(stderr, "xml_sta:[%s]\n", name);
1924 for (attr = list->first; attr != NULL; attr = attr->next)
1925 fprintf(stderr, " attr:\"%s\"=\"%s\"\n",
1926 attr->name, attr->value);
1928 xar->base64text = 0;
1929 switch (xar->xmlsts) {
1931 if (strcmp(name, "xar") == 0)
1934 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
1935 return (ARCHIVE_FATAL);
1938 if (strcmp(name, "toc") == 0)
1941 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
1942 return (ARCHIVE_FATAL);
1945 if (strcmp(name, "creation-time") == 0)
1946 xar->xmlsts = TOC_CREATION_TIME;
1947 else if (strcmp(name, "checksum") == 0)
1948 xar->xmlsts = TOC_CHECKSUM;
1949 else if (strcmp(name, "file") == 0) {
1950 if (file_new(a, xar, list) != ARCHIVE_OK)
1951 return (ARCHIVE_FATAL);
1952 xar->xmlsts = TOC_FILE;
1955 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
1956 return (ARCHIVE_FATAL);
1959 if (strcmp(name, "offset") == 0)
1960 xar->xmlsts = TOC_CHECKSUM_OFFSET;
1961 else if (strcmp(name, "size") == 0)
1962 xar->xmlsts = TOC_CHECKSUM_SIZE;
1964 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
1965 return (ARCHIVE_FATAL);
1968 if (strcmp(name, "file") == 0) {
1969 if (file_new(a, xar, list) != ARCHIVE_OK)
1970 return (ARCHIVE_FATAL);
1972 else if (strcmp(name, "data") == 0)
1973 xar->xmlsts = FILE_DATA;
1974 else if (strcmp(name, "ea") == 0) {
1975 if (xattr_new(a, xar, list) != ARCHIVE_OK)
1976 return (ARCHIVE_FATAL);
1977 xar->xmlsts = FILE_EA;
1979 else if (strcmp(name, "ctime") == 0)
1980 xar->xmlsts = FILE_CTIME;
1981 else if (strcmp(name, "mtime") == 0)
1982 xar->xmlsts = FILE_MTIME;
1983 else if (strcmp(name, "atime") == 0)
1984 xar->xmlsts = FILE_ATIME;
1985 else if (strcmp(name, "group") == 0)
1986 xar->xmlsts = FILE_GROUP;
1987 else if (strcmp(name, "gid") == 0)
1988 xar->xmlsts = FILE_GID;
1989 else if (strcmp(name, "user") == 0)
1990 xar->xmlsts = FILE_USER;
1991 else if (strcmp(name, "uid") == 0)
1992 xar->xmlsts = FILE_UID;
1993 else if (strcmp(name, "mode") == 0)
1994 xar->xmlsts = FILE_MODE;
1995 else if (strcmp(name, "device") == 0)
1996 xar->xmlsts = FILE_DEVICE;
1997 else if (strcmp(name, "deviceno") == 0)
1998 xar->xmlsts = FILE_DEVICENO;
1999 else if (strcmp(name, "inode") == 0)
2000 xar->xmlsts = FILE_INODE;
2001 else if (strcmp(name, "link") == 0)
2002 xar->xmlsts = FILE_LINK;
2003 else if (strcmp(name, "type") == 0) {
2004 xar->xmlsts = FILE_TYPE;
2005 for (attr = list->first; attr != NULL;
2006 attr = attr->next) {
2007 if (strcmp(attr->name, "link") != 0)
2009 if (strcmp(attr->value, "original") == 0) {
2010 xar->file->hdnext = xar->hdlink_orgs;
2011 xar->hdlink_orgs = xar->file;
2013 xar->file->link = (unsigned)atol10(attr->value,
2014 strlen(attr->value));
2015 if (xar->file->link > 0)
2016 if (add_link(a, xar, xar->file) != ARCHIVE_OK) {
2017 return (ARCHIVE_FATAL);
2022 else if (strcmp(name, "name") == 0) {
2023 xar->xmlsts = FILE_NAME;
2024 for (attr = list->first; attr != NULL;
2025 attr = attr->next) {
2026 if (strcmp(attr->name, "enctype") == 0 &&
2027 strcmp(attr->value, "base64") == 0)
2028 xar->base64text = 1;
2031 else if (strcmp(name, "acl") == 0)
2032 xar->xmlsts = FILE_ACL;
2033 else if (strcmp(name, "flags") == 0)
2034 xar->xmlsts = FILE_FLAGS;
2035 else if (strcmp(name, "ext2") == 0)
2036 xar->xmlsts = FILE_EXT2;
2038 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2039 return (ARCHIVE_FATAL);
2042 if (strcmp(name, "length") == 0)
2043 xar->xmlsts = FILE_DATA_LENGTH;
2044 else if (strcmp(name, "offset") == 0)
2045 xar->xmlsts = FILE_DATA_OFFSET;
2046 else if (strcmp(name, "size") == 0)
2047 xar->xmlsts = FILE_DATA_SIZE;
2048 else if (strcmp(name, "encoding") == 0) {
2049 xar->xmlsts = FILE_DATA_ENCODING;
2050 xar->file->encoding = getencoding(list);
2052 else if (strcmp(name, "archived-checksum") == 0) {
2053 xar->xmlsts = FILE_DATA_A_CHECKSUM;
2054 xar->file->a_sum.alg = getsumalgorithm(list);
2056 else if (strcmp(name, "extracted-checksum") == 0) {
2057 xar->xmlsts = FILE_DATA_E_CHECKSUM;
2058 xar->file->e_sum.alg = getsumalgorithm(list);
2060 else if (strcmp(name, "content") == 0)
2061 xar->xmlsts = FILE_DATA_CONTENT;
2063 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2064 return (ARCHIVE_FATAL);
2067 if (strcmp(name, "major") == 0)
2068 xar->xmlsts = FILE_DEVICE_MAJOR;
2069 else if (strcmp(name, "minor") == 0)
2070 xar->xmlsts = FILE_DEVICE_MINOR;
2072 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2073 return (ARCHIVE_FATAL);
2075 case FILE_DATA_CONTENT:
2076 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2077 return (ARCHIVE_FATAL);
2080 if (strcmp(name, "length") == 0)
2081 xar->xmlsts = FILE_EA_LENGTH;
2082 else if (strcmp(name, "offset") == 0)
2083 xar->xmlsts = FILE_EA_OFFSET;
2084 else if (strcmp(name, "size") == 0)
2085 xar->xmlsts = FILE_EA_SIZE;
2086 else if (strcmp(name, "encoding") == 0) {
2087 xar->xmlsts = FILE_EA_ENCODING;
2088 xar->xattr->encoding = getencoding(list);
2089 } else if (strcmp(name, "archived-checksum") == 0)
2090 xar->xmlsts = FILE_EA_A_CHECKSUM;
2091 else if (strcmp(name, "extracted-checksum") == 0)
2092 xar->xmlsts = FILE_EA_E_CHECKSUM;
2093 else if (strcmp(name, "name") == 0)
2094 xar->xmlsts = FILE_EA_NAME;
2095 else if (strcmp(name, "fstype") == 0)
2096 xar->xmlsts = FILE_EA_FSTYPE;
2098 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2099 return (ARCHIVE_FATAL);
2102 if (strcmp(name, "appleextended") == 0)
2103 xar->xmlsts = FILE_ACL_APPLEEXTENDED;
2104 else if (strcmp(name, "default") == 0)
2105 xar->xmlsts = FILE_ACL_DEFAULT;
2106 else if (strcmp(name, "access") == 0)
2107 xar->xmlsts = FILE_ACL_ACCESS;
2109 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2110 return (ARCHIVE_FATAL);
2113 if (!xml_parse_file_flags(xar, name))
2114 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2115 return (ARCHIVE_FATAL);
2118 if (!xml_parse_file_ext2(xar, name))
2119 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2120 return (ARCHIVE_FATAL);
2122 case TOC_CREATION_TIME:
2123 case TOC_CHECKSUM_OFFSET:
2124 case TOC_CHECKSUM_SIZE:
2125 case FILE_DATA_LENGTH:
2126 case FILE_DATA_OFFSET:
2127 case FILE_DATA_SIZE:
2128 case FILE_DATA_ENCODING:
2129 case FILE_DATA_A_CHECKSUM:
2130 case FILE_DATA_E_CHECKSUM:
2131 case FILE_EA_LENGTH:
2132 case FILE_EA_OFFSET:
2134 case FILE_EA_ENCODING:
2135 case FILE_EA_A_CHECKSUM:
2136 case FILE_EA_E_CHECKSUM:
2138 case FILE_EA_FSTYPE:
2147 case FILE_DEVICE_MAJOR:
2148 case FILE_DEVICE_MINOR:
2154 case FILE_ACL_DEFAULT:
2155 case FILE_ACL_ACCESS:
2156 case FILE_ACL_APPLEEXTENDED:
2157 case FILE_FLAGS_USER_NODUMP:
2158 case FILE_FLAGS_USER_IMMUTABLE:
2159 case FILE_FLAGS_USER_APPEND:
2160 case FILE_FLAGS_USER_OPAQUE:
2161 case FILE_FLAGS_USER_NOUNLINK:
2162 case FILE_FLAGS_SYS_ARCHIVED:
2163 case FILE_FLAGS_SYS_IMMUTABLE:
2164 case FILE_FLAGS_SYS_APPEND:
2165 case FILE_FLAGS_SYS_NOUNLINK:
2166 case FILE_FLAGS_SYS_SNAPSHOT:
2167 case FILE_EXT2_SecureDeletion:
2168 case FILE_EXT2_Undelete:
2169 case FILE_EXT2_Compress:
2170 case FILE_EXT2_Synchronous:
2171 case FILE_EXT2_Immutable:
2172 case FILE_EXT2_AppendOnly:
2173 case FILE_EXT2_NoDump:
2174 case FILE_EXT2_NoAtime:
2175 case FILE_EXT2_CompDirty:
2176 case FILE_EXT2_CompBlock:
2177 case FILE_EXT2_NoCompBlock:
2178 case FILE_EXT2_CompError:
2179 case FILE_EXT2_BTree:
2180 case FILE_EXT2_HashIndexed:
2181 case FILE_EXT2_iMagic:
2182 case FILE_EXT2_Journaled:
2183 case FILE_EXT2_NoTail:
2184 case FILE_EXT2_DirSync:
2185 case FILE_EXT2_TopDir:
2186 case FILE_EXT2_Reserved:
2188 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2189 return (ARCHIVE_FATAL);
2192 return (ARCHIVE_OK);
2196 xml_end(void *userData, const char *name)
2198 struct archive_read *a;
2201 a = (struct archive_read *)userData;
2202 xar = (struct xar *)(a->format->data);
2205 fprintf(stderr, "xml_end:[%s]\n", name);
2207 switch (xar->xmlsts) {
2211 if (strcmp(name, "xar") == 0)
2215 if (strcmp(name, "toc") == 0)
2218 case TOC_CREATION_TIME:
2219 if (strcmp(name, "creation-time") == 0)
2223 if (strcmp(name, "checksum") == 0)
2226 case TOC_CHECKSUM_OFFSET:
2227 if (strcmp(name, "offset") == 0)
2228 xar->xmlsts = TOC_CHECKSUM;
2230 case TOC_CHECKSUM_SIZE:
2231 if (strcmp(name, "size") == 0)
2232 xar->xmlsts = TOC_CHECKSUM;
2235 if (strcmp(name, "file") == 0) {
2236 if (xar->file->parent != NULL &&
2237 ((xar->file->mode & AE_IFMT) == AE_IFDIR))
2238 xar->file->parent->subdirs++;
2239 xar->file = xar->file->parent;
2240 if (xar->file == NULL)
2245 if (strcmp(name, "data") == 0)
2246 xar->xmlsts = TOC_FILE;
2248 case FILE_DATA_LENGTH:
2249 if (strcmp(name, "length") == 0)
2250 xar->xmlsts = FILE_DATA;
2252 case FILE_DATA_OFFSET:
2253 if (strcmp(name, "offset") == 0)
2254 xar->xmlsts = FILE_DATA;
2256 case FILE_DATA_SIZE:
2257 if (strcmp(name, "size") == 0)
2258 xar->xmlsts = FILE_DATA;
2260 case FILE_DATA_ENCODING:
2261 if (strcmp(name, "encoding") == 0)
2262 xar->xmlsts = FILE_DATA;
2264 case FILE_DATA_A_CHECKSUM:
2265 if (strcmp(name, "archived-checksum") == 0)
2266 xar->xmlsts = FILE_DATA;
2268 case FILE_DATA_E_CHECKSUM:
2269 if (strcmp(name, "extracted-checksum") == 0)
2270 xar->xmlsts = FILE_DATA;
2272 case FILE_DATA_CONTENT:
2273 if (strcmp(name, "content") == 0)
2274 xar->xmlsts = FILE_DATA;
2277 if (strcmp(name, "ea") == 0) {
2278 xar->xmlsts = TOC_FILE;
2282 case FILE_EA_LENGTH:
2283 if (strcmp(name, "length") == 0)
2284 xar->xmlsts = FILE_EA;
2286 case FILE_EA_OFFSET:
2287 if (strcmp(name, "offset") == 0)
2288 xar->xmlsts = FILE_EA;
2291 if (strcmp(name, "size") == 0)
2292 xar->xmlsts = FILE_EA;
2294 case FILE_EA_ENCODING:
2295 if (strcmp(name, "encoding") == 0)
2296 xar->xmlsts = FILE_EA;
2298 case FILE_EA_A_CHECKSUM:
2299 if (strcmp(name, "archived-checksum") == 0)
2300 xar->xmlsts = FILE_EA;
2302 case FILE_EA_E_CHECKSUM:
2303 if (strcmp(name, "extracted-checksum") == 0)
2304 xar->xmlsts = FILE_EA;
2307 if (strcmp(name, "name") == 0)
2308 xar->xmlsts = FILE_EA;
2310 case FILE_EA_FSTYPE:
2311 if (strcmp(name, "fstype") == 0)
2312 xar->xmlsts = FILE_EA;
2315 if (strcmp(name, "ctime") == 0)
2316 xar->xmlsts = TOC_FILE;
2319 if (strcmp(name, "mtime") == 0)
2320 xar->xmlsts = TOC_FILE;
2323 if (strcmp(name, "atime") == 0)
2324 xar->xmlsts = TOC_FILE;
2327 if (strcmp(name, "group") == 0)
2328 xar->xmlsts = TOC_FILE;
2331 if (strcmp(name, "gid") == 0)
2332 xar->xmlsts = TOC_FILE;
2335 if (strcmp(name, "user") == 0)
2336 xar->xmlsts = TOC_FILE;
2339 if (strcmp(name, "uid") == 0)
2340 xar->xmlsts = TOC_FILE;
2343 if (strcmp(name, "mode") == 0)
2344 xar->xmlsts = TOC_FILE;
2347 if (strcmp(name, "device") == 0)
2348 xar->xmlsts = TOC_FILE;
2350 case FILE_DEVICE_MAJOR:
2351 if (strcmp(name, "major") == 0)
2352 xar->xmlsts = FILE_DEVICE;
2354 case FILE_DEVICE_MINOR:
2355 if (strcmp(name, "minor") == 0)
2356 xar->xmlsts = FILE_DEVICE;
2359 if (strcmp(name, "deviceno") == 0)
2360 xar->xmlsts = TOC_FILE;
2363 if (strcmp(name, "inode") == 0)
2364 xar->xmlsts = TOC_FILE;
2367 if (strcmp(name, "link") == 0)
2368 xar->xmlsts = TOC_FILE;
2371 if (strcmp(name, "type") == 0)
2372 xar->xmlsts = TOC_FILE;
2375 if (strcmp(name, "name") == 0)
2376 xar->xmlsts = TOC_FILE;
2379 if (strcmp(name, "acl") == 0)
2380 xar->xmlsts = TOC_FILE;
2382 case FILE_ACL_DEFAULT:
2383 if (strcmp(name, "default") == 0)
2384 xar->xmlsts = FILE_ACL;
2386 case FILE_ACL_ACCESS:
2387 if (strcmp(name, "access") == 0)
2388 xar->xmlsts = FILE_ACL;
2390 case FILE_ACL_APPLEEXTENDED:
2391 if (strcmp(name, "appleextended") == 0)
2392 xar->xmlsts = FILE_ACL;
2395 if (strcmp(name, "flags") == 0)
2396 xar->xmlsts = TOC_FILE;
2398 case FILE_FLAGS_USER_NODUMP:
2399 if (strcmp(name, "UserNoDump") == 0)
2400 xar->xmlsts = FILE_FLAGS;
2402 case FILE_FLAGS_USER_IMMUTABLE:
2403 if (strcmp(name, "UserImmutable") == 0)
2404 xar->xmlsts = FILE_FLAGS;
2406 case FILE_FLAGS_USER_APPEND:
2407 if (strcmp(name, "UserAppend") == 0)
2408 xar->xmlsts = FILE_FLAGS;
2410 case FILE_FLAGS_USER_OPAQUE:
2411 if (strcmp(name, "UserOpaque") == 0)
2412 xar->xmlsts = FILE_FLAGS;
2414 case FILE_FLAGS_USER_NOUNLINK:
2415 if (strcmp(name, "UserNoUnlink") == 0)
2416 xar->xmlsts = FILE_FLAGS;
2418 case FILE_FLAGS_SYS_ARCHIVED:
2419 if (strcmp(name, "SystemArchived") == 0)
2420 xar->xmlsts = FILE_FLAGS;
2422 case FILE_FLAGS_SYS_IMMUTABLE:
2423 if (strcmp(name, "SystemImmutable") == 0)
2424 xar->xmlsts = FILE_FLAGS;
2426 case FILE_FLAGS_SYS_APPEND:
2427 if (strcmp(name, "SystemAppend") == 0)
2428 xar->xmlsts = FILE_FLAGS;
2430 case FILE_FLAGS_SYS_NOUNLINK:
2431 if (strcmp(name, "SystemNoUnlink") == 0)
2432 xar->xmlsts = FILE_FLAGS;
2434 case FILE_FLAGS_SYS_SNAPSHOT:
2435 if (strcmp(name, "SystemSnapshot") == 0)
2436 xar->xmlsts = FILE_FLAGS;
2439 if (strcmp(name, "ext2") == 0)
2440 xar->xmlsts = TOC_FILE;
2442 case FILE_EXT2_SecureDeletion:
2443 if (strcmp(name, "SecureDeletion") == 0)
2444 xar->xmlsts = FILE_EXT2;
2446 case FILE_EXT2_Undelete:
2447 if (strcmp(name, "Undelete") == 0)
2448 xar->xmlsts = FILE_EXT2;
2450 case FILE_EXT2_Compress:
2451 if (strcmp(name, "Compress") == 0)
2452 xar->xmlsts = FILE_EXT2;
2454 case FILE_EXT2_Synchronous:
2455 if (strcmp(name, "Synchronous") == 0)
2456 xar->xmlsts = FILE_EXT2;
2458 case FILE_EXT2_Immutable:
2459 if (strcmp(name, "Immutable") == 0)
2460 xar->xmlsts = FILE_EXT2;
2462 case FILE_EXT2_AppendOnly:
2463 if (strcmp(name, "AppendOnly") == 0)
2464 xar->xmlsts = FILE_EXT2;
2466 case FILE_EXT2_NoDump:
2467 if (strcmp(name, "NoDump") == 0)
2468 xar->xmlsts = FILE_EXT2;
2470 case FILE_EXT2_NoAtime:
2471 if (strcmp(name, "NoAtime") == 0)
2472 xar->xmlsts = FILE_EXT2;
2474 case FILE_EXT2_CompDirty:
2475 if (strcmp(name, "CompDirty") == 0)
2476 xar->xmlsts = FILE_EXT2;
2478 case FILE_EXT2_CompBlock:
2479 if (strcmp(name, "CompBlock") == 0)
2480 xar->xmlsts = FILE_EXT2;
2482 case FILE_EXT2_NoCompBlock:
2483 if (strcmp(name, "NoCompBlock") == 0)
2484 xar->xmlsts = FILE_EXT2;
2486 case FILE_EXT2_CompError:
2487 if (strcmp(name, "CompError") == 0)
2488 xar->xmlsts = FILE_EXT2;
2490 case FILE_EXT2_BTree:
2491 if (strcmp(name, "BTree") == 0)
2492 xar->xmlsts = FILE_EXT2;
2494 case FILE_EXT2_HashIndexed:
2495 if (strcmp(name, "HashIndexed") == 0)
2496 xar->xmlsts = FILE_EXT2;
2498 case FILE_EXT2_iMagic:
2499 if (strcmp(name, "iMagic") == 0)
2500 xar->xmlsts = FILE_EXT2;
2502 case FILE_EXT2_Journaled:
2503 if (strcmp(name, "Journaled") == 0)
2504 xar->xmlsts = FILE_EXT2;
2506 case FILE_EXT2_NoTail:
2507 if (strcmp(name, "NoTail") == 0)
2508 xar->xmlsts = FILE_EXT2;
2510 case FILE_EXT2_DirSync:
2511 if (strcmp(name, "DirSync") == 0)
2512 xar->xmlsts = FILE_EXT2;
2514 case FILE_EXT2_TopDir:
2515 if (strcmp(name, "TopDir") == 0)
2516 xar->xmlsts = FILE_EXT2;
2518 case FILE_EXT2_Reserved:
2519 if (strcmp(name, "Reserved") == 0)
2520 xar->xmlsts = FILE_EXT2;
2523 unknowntag_end(xar, name);
2528 static const int base64[256] = {
2529 -1, -1, -1, -1, -1, -1, -1, -1,
2530 -1, -1, -1, -1, -1, -1, -1, -1, /* 00 - 0F */
2531 -1, -1, -1, -1, -1, -1, -1, -1,
2532 -1, -1, -1, -1, -1, -1, -1, -1, /* 10 - 1F */
2533 -1, -1, -1, -1, -1, -1, -1, -1,
2534 -1, -1, -1, 62, -1, -1, -1, 63, /* 20 - 2F */
2535 52, 53, 54, 55, 56, 57, 58, 59,
2536 60, 61, -1, -1, -1, -1, -1, -1, /* 30 - 3F */
2537 -1, 0, 1, 2, 3, 4, 5, 6,
2538 7, 8, 9, 10, 11, 12, 13, 14, /* 40 - 4F */
2539 15, 16, 17, 18, 19, 20, 21, 22,
2540 23, 24, 25, -1, -1, -1, -1, -1, /* 50 - 5F */
2541 -1, 26, 27, 28, 29, 30, 31, 32,
2542 33, 34, 35, 36, 37, 38, 39, 40, /* 60 - 6F */
2543 41, 42, 43, 44, 45, 46, 47, 48,
2544 49, 50, 51, -1, -1, -1, -1, -1, /* 70 - 7F */
2545 -1, -1, -1, -1, -1, -1, -1, -1,
2546 -1, -1, -1, -1, -1, -1, -1, -1, /* 80 - 8F */
2547 -1, -1, -1, -1, -1, -1, -1, -1,
2548 -1, -1, -1, -1, -1, -1, -1, -1, /* 90 - 9F */
2549 -1, -1, -1, -1, -1, -1, -1, -1,
2550 -1, -1, -1, -1, -1, -1, -1, -1, /* A0 - AF */
2551 -1, -1, -1, -1, -1, -1, -1, -1,
2552 -1, -1, -1, -1, -1, -1, -1, -1, /* B0 - BF */
2553 -1, -1, -1, -1, -1, -1, -1, -1,
2554 -1, -1, -1, -1, -1, -1, -1, -1, /* C0 - CF */
2555 -1, -1, -1, -1, -1, -1, -1, -1,
2556 -1, -1, -1, -1, -1, -1, -1, -1, /* D0 - DF */
2557 -1, -1, -1, -1, -1, -1, -1, -1,
2558 -1, -1, -1, -1, -1, -1, -1, -1, /* E0 - EF */
2559 -1, -1, -1, -1, -1, -1, -1, -1,
2560 -1, -1, -1, -1, -1, -1, -1, -1, /* F0 - FF */
2564 strappend_base64(struct xar *xar,
2565 struct archive_string *as, const char *s, size_t l)
2567 unsigned char buff[256];
2569 const unsigned char *b;
2572 (void)xar; /* UNUSED */
2575 b = (const unsigned char *)s;
2580 if (base64[b[0]] < 0 || base64[b[1]] < 0)
2582 n = base64[*b++] << 18;
2583 n |= base64[*b++] << 12;
2591 n |= base64[*b++] << 6;
2592 *out++ = (n >> 8) & 0xFF;
2604 if (len+3 >= sizeof(buff)) {
2605 archive_strncat(as, (const char *)buff, len);
2611 archive_strncat(as, (const char *)buff, len);
2615 xml_data(void *userData, const char *s, int len)
2617 struct archive_read *a;
2620 a = (struct archive_read *)userData;
2621 xar = (struct xar *)(a->format->data);
2626 if (len > (int)(sizeof(buff)-1))
2627 len = (int)(sizeof(buff)-1);
2628 strncpy(buff, s, len);
2630 fprintf(stderr, "\tlen=%d:\"%s\"\n", len, buff);
2633 switch (xar->xmlsts) {
2634 case TOC_CHECKSUM_OFFSET:
2635 xar->toc_chksum_offset = atol10(s, len);
2637 case TOC_CHECKSUM_SIZE:
2638 xar->toc_chksum_size = atol10(s, len);
2643 if (xar->file == NULL)
2646 switch (xar->xmlsts) {
2648 if (xar->file->parent != NULL) {
2649 archive_string_concat(&(xar->file->pathname),
2650 &(xar->file->parent->pathname));
2651 archive_strappend_char(&(xar->file->pathname), '/');
2653 xar->file->has |= HAS_PATHNAME;
2654 if (xar->base64text) {
2655 strappend_base64(xar,
2656 &(xar->file->pathname), s, len);
2658 archive_strncat(&(xar->file->pathname), s, len);
2661 xar->file->has |= HAS_SYMLINK;
2662 archive_strncpy(&(xar->file->symlink), s, len);
2665 if (strncmp("file", s, len) == 0 ||
2666 strncmp("hardlink", s, len) == 0)
2668 (xar->file->mode & ~AE_IFMT) | AE_IFREG;
2669 if (strncmp("directory", s, len) == 0)
2671 (xar->file->mode & ~AE_IFMT) | AE_IFDIR;
2672 if (strncmp("symlink", s, len) == 0)
2674 (xar->file->mode & ~AE_IFMT) | AE_IFLNK;
2675 if (strncmp("character special", s, len) == 0)
2677 (xar->file->mode & ~AE_IFMT) | AE_IFCHR;
2678 if (strncmp("block special", s, len) == 0)
2680 (xar->file->mode & ~AE_IFMT) | AE_IFBLK;
2681 if (strncmp("socket", s, len) == 0)
2683 (xar->file->mode & ~AE_IFMT) | AE_IFSOCK;
2684 if (strncmp("fifo", s, len) == 0)
2686 (xar->file->mode & ~AE_IFMT) | AE_IFIFO;
2687 xar->file->has |= HAS_TYPE;
2690 xar->file->has |= HAS_INO;
2691 xar->file->ino64 = atol10(s, len);
2693 case FILE_DEVICE_MAJOR:
2694 xar->file->has |= HAS_DEVMAJOR;
2695 xar->file->devmajor = (dev_t)atol10(s, len);
2697 case FILE_DEVICE_MINOR:
2698 xar->file->has |= HAS_DEVMINOR;
2699 xar->file->devminor = (dev_t)atol10(s, len);
2702 xar->file->has |= HAS_DEV;
2703 xar->file->dev = (dev_t)atol10(s, len);
2706 xar->file->has |= HAS_MODE;
2708 (xar->file->mode & AE_IFMT) |
2709 ((mode_t)(atol8(s, len)) & ~AE_IFMT);
2712 xar->file->has |= HAS_GID;
2713 archive_strncpy(&(xar->file->gname), s, len);
2716 xar->file->has |= HAS_GID;
2717 xar->file->gid = atol10(s, len);
2720 xar->file->has |= HAS_UID;
2721 archive_strncpy(&(xar->file->uname), s, len);
2724 xar->file->has |= HAS_UID;
2725 xar->file->uid = atol10(s, len);
2728 xar->file->has |= HAS_TIME;
2729 xar->file->ctime = parse_time(s, len);
2732 xar->file->has |= HAS_TIME;
2733 xar->file->mtime = parse_time(s, len);
2736 xar->file->has |= HAS_TIME;
2737 xar->file->atime = parse_time(s, len);
2739 case FILE_DATA_LENGTH:
2740 xar->file->has |= HAS_DATA;
2741 xar->file->length = atol10(s, len);
2743 case FILE_DATA_OFFSET:
2744 xar->file->has |= HAS_DATA;
2745 xar->file->offset = atol10(s, len);
2747 case FILE_DATA_SIZE:
2748 xar->file->has |= HAS_DATA;
2749 xar->file->size = atol10(s, len);
2751 case FILE_DATA_A_CHECKSUM:
2752 xar->file->a_sum.len = atohex(xar->file->a_sum.val,
2753 sizeof(xar->file->a_sum.val), s, len);
2755 case FILE_DATA_E_CHECKSUM:
2756 xar->file->e_sum.len = atohex(xar->file->e_sum.val,
2757 sizeof(xar->file->e_sum.val), s, len);
2759 case FILE_EA_LENGTH:
2760 xar->file->has |= HAS_XATTR;
2761 xar->xattr->length = atol10(s, len);
2763 case FILE_EA_OFFSET:
2764 xar->file->has |= HAS_XATTR;
2765 xar->xattr->offset = atol10(s, len);
2768 xar->file->has |= HAS_XATTR;
2769 xar->xattr->size = atol10(s, len);
2771 case FILE_EA_A_CHECKSUM:
2772 xar->file->has |= HAS_XATTR;
2773 xar->xattr->a_sum.len = atohex(xar->xattr->a_sum.val,
2774 sizeof(xar->xattr->a_sum.val), s, len);
2776 case FILE_EA_E_CHECKSUM:
2777 xar->file->has |= HAS_XATTR;
2778 xar->xattr->e_sum.len = atohex(xar->xattr->e_sum.val,
2779 sizeof(xar->xattr->e_sum.val), s, len);
2782 xar->file->has |= HAS_XATTR;
2783 archive_strncpy(&(xar->xattr->name), s, len);
2785 case FILE_EA_FSTYPE:
2786 xar->file->has |= HAS_XATTR;
2787 archive_strncpy(&(xar->xattr->fstype), s, len);
2790 case FILE_ACL_DEFAULT:
2791 case FILE_ACL_ACCESS:
2792 case FILE_ACL_APPLEEXTENDED:
2793 xar->file->has |= HAS_ACL;
2799 case TOC_CREATION_TIME:
2801 case TOC_CHECKSUM_OFFSET:
2802 case TOC_CHECKSUM_SIZE:
2805 case FILE_DATA_ENCODING:
2806 case FILE_DATA_CONTENT:
2809 case FILE_EA_ENCODING:
2812 case FILE_FLAGS_USER_NODUMP:
2813 case FILE_FLAGS_USER_IMMUTABLE:
2814 case FILE_FLAGS_USER_APPEND:
2815 case FILE_FLAGS_USER_OPAQUE:
2816 case FILE_FLAGS_USER_NOUNLINK:
2817 case FILE_FLAGS_SYS_ARCHIVED:
2818 case FILE_FLAGS_SYS_IMMUTABLE:
2819 case FILE_FLAGS_SYS_APPEND:
2820 case FILE_FLAGS_SYS_NOUNLINK:
2821 case FILE_FLAGS_SYS_SNAPSHOT:
2823 case FILE_EXT2_SecureDeletion:
2824 case FILE_EXT2_Undelete:
2825 case FILE_EXT2_Compress:
2826 case FILE_EXT2_Synchronous:
2827 case FILE_EXT2_Immutable:
2828 case FILE_EXT2_AppendOnly:
2829 case FILE_EXT2_NoDump:
2830 case FILE_EXT2_NoAtime:
2831 case FILE_EXT2_CompDirty:
2832 case FILE_EXT2_CompBlock:
2833 case FILE_EXT2_NoCompBlock:
2834 case FILE_EXT2_CompError:
2835 case FILE_EXT2_BTree:
2836 case FILE_EXT2_HashIndexed:
2837 case FILE_EXT2_iMagic:
2838 case FILE_EXT2_Journaled:
2839 case FILE_EXT2_NoTail:
2840 case FILE_EXT2_DirSync:
2841 case FILE_EXT2_TopDir:
2842 case FILE_EXT2_Reserved:
2852 xml_parse_file_flags(struct xar *xar, const char *name)
2854 const char *flag = NULL;
2856 if (strcmp(name, "UserNoDump") == 0) {
2857 xar->xmlsts = FILE_FLAGS_USER_NODUMP;
2860 else if (strcmp(name, "UserImmutable") == 0) {
2861 xar->xmlsts = FILE_FLAGS_USER_IMMUTABLE;
2862 flag = "uimmutable";
2864 else if (strcmp(name, "UserAppend") == 0) {
2865 xar->xmlsts = FILE_FLAGS_USER_APPEND;
2868 else if (strcmp(name, "UserOpaque") == 0) {
2869 xar->xmlsts = FILE_FLAGS_USER_OPAQUE;
2872 else if (strcmp(name, "UserNoUnlink") == 0) {
2873 xar->xmlsts = FILE_FLAGS_USER_NOUNLINK;
2876 else if (strcmp(name, "SystemArchived") == 0) {
2877 xar->xmlsts = FILE_FLAGS_SYS_ARCHIVED;
2880 else if (strcmp(name, "SystemImmutable") == 0) {
2881 xar->xmlsts = FILE_FLAGS_SYS_IMMUTABLE;
2882 flag = "simmutable";
2884 else if (strcmp(name, "SystemAppend") == 0) {
2885 xar->xmlsts = FILE_FLAGS_SYS_APPEND;
2888 else if (strcmp(name, "SystemNoUnlink") == 0) {
2889 xar->xmlsts = FILE_FLAGS_SYS_NOUNLINK;
2892 else if (strcmp(name, "SystemSnapshot") == 0) {
2893 xar->xmlsts = FILE_FLAGS_SYS_SNAPSHOT;
2899 xar->file->has |= HAS_FFLAGS;
2900 if (archive_strlen(&(xar->file->fflags_text)) > 0)
2901 archive_strappend_char(&(xar->file->fflags_text), ',');
2902 archive_strcat(&(xar->file->fflags_text), flag);
2910 xml_parse_file_ext2(struct xar *xar, const char *name)
2912 const char *flag = NULL;
2914 if (strcmp(name, "SecureDeletion") == 0) {
2915 xar->xmlsts = FILE_EXT2_SecureDeletion;
2916 flag = "securedeletion";
2918 else if (strcmp(name, "Undelete") == 0) {
2919 xar->xmlsts = FILE_EXT2_Undelete;
2922 else if (strcmp(name, "Compress") == 0) {
2923 xar->xmlsts = FILE_EXT2_Compress;
2926 else if (strcmp(name, "Synchronous") == 0) {
2927 xar->xmlsts = FILE_EXT2_Synchronous;
2930 else if (strcmp(name, "Immutable") == 0) {
2931 xar->xmlsts = FILE_EXT2_Immutable;
2932 flag = "simmutable";
2934 else if (strcmp(name, "AppendOnly") == 0) {
2935 xar->xmlsts = FILE_EXT2_AppendOnly;
2938 else if (strcmp(name, "NoDump") == 0) {
2939 xar->xmlsts = FILE_EXT2_NoDump;
2942 else if (strcmp(name, "NoAtime") == 0) {
2943 xar->xmlsts = FILE_EXT2_NoAtime;
2946 else if (strcmp(name, "CompDirty") == 0) {
2947 xar->xmlsts = FILE_EXT2_CompDirty;
2950 else if (strcmp(name, "CompBlock") == 0) {
2951 xar->xmlsts = FILE_EXT2_CompBlock;
2954 else if (strcmp(name, "NoCompBlock") == 0) {
2955 xar->xmlsts = FILE_EXT2_NoCompBlock;
2956 flag = "nocomprblk";
2958 else if (strcmp(name, "CompError") == 0) {
2959 xar->xmlsts = FILE_EXT2_CompError;
2962 else if (strcmp(name, "BTree") == 0) {
2963 xar->xmlsts = FILE_EXT2_BTree;
2966 else if (strcmp(name, "HashIndexed") == 0) {
2967 xar->xmlsts = FILE_EXT2_HashIndexed;
2970 else if (strcmp(name, "iMagic") == 0) {
2971 xar->xmlsts = FILE_EXT2_iMagic;
2974 else if (strcmp(name, "Journaled") == 0) {
2975 xar->xmlsts = FILE_EXT2_Journaled;
2978 else if (strcmp(name, "NoTail") == 0) {
2979 xar->xmlsts = FILE_EXT2_NoTail;
2982 else if (strcmp(name, "DirSync") == 0) {
2983 xar->xmlsts = FILE_EXT2_DirSync;
2986 else if (strcmp(name, "TopDir") == 0) {
2987 xar->xmlsts = FILE_EXT2_TopDir;
2990 else if (strcmp(name, "Reserved") == 0) {
2991 xar->xmlsts = FILE_EXT2_Reserved;
2997 if (archive_strlen(&(xar->file->fflags_text)) > 0)
2998 archive_strappend_char(&(xar->file->fflags_text), ',');
2999 archive_strcat(&(xar->file->fflags_text), flag);
3003 #ifdef HAVE_LIBXML_XMLREADER_H
3006 xml2_xmlattr_setup(struct archive_read *a,
3007 struct xmlattr_list *list, xmlTextReaderPtr reader)
3009 struct xmlattr *attr;
3013 list->last = &(list->first);
3014 r = xmlTextReaderMoveToFirstAttribute(reader);
3016 attr = malloc(sizeof*(attr));
3018 archive_set_error(&a->archive, ENOMEM, "Out of memory");
3019 return (ARCHIVE_FATAL);
3021 attr->name = strdup(
3022 (const char *)xmlTextReaderConstLocalName(reader));
3023 if (attr->name == NULL) {
3025 archive_set_error(&a->archive, ENOMEM, "Out of memory");
3026 return (ARCHIVE_FATAL);
3028 attr->value = strdup(
3029 (const char *)xmlTextReaderConstValue(reader));
3030 if (attr->value == NULL) {
3033 archive_set_error(&a->archive, ENOMEM, "Out of memory");
3034 return (ARCHIVE_FATAL);
3038 list->last = &(attr->next);
3039 r = xmlTextReaderMoveToNextAttribute(reader);
3045 xml2_read_cb(void *context, char *buffer, int len)
3047 struct archive_read *a;
3054 a = (struct archive_read *)context;
3055 xar = (struct xar *)(a->format->data);
3057 if (xar->toc_remaining <= 0)
3061 r = rd_contents(a, &d, &outbytes, &used, xar->toc_remaining);
3062 if (r != ARCHIVE_OK)
3064 __archive_read_consume(a, used);
3065 xar->toc_remaining -= used;
3066 xar->offset += used;
3067 xar->toc_total += outbytes;
3068 PRINT_TOC(buffer, len);
3070 return ((int)outbytes);
3074 xml2_close_cb(void *context)
3077 (void)context; /* UNUSED */
3082 xml2_error_hdr(void *arg, const char *msg, xmlParserSeverities severity,
3083 xmlTextReaderLocatorPtr locator)
3085 struct archive_read *a;
3087 (void)locator; /* UNUSED */
3088 a = (struct archive_read *)arg;
3090 case XML_PARSER_SEVERITY_VALIDITY_WARNING:
3091 case XML_PARSER_SEVERITY_WARNING:
3092 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3093 "XML Parsing error: %s", msg);
3095 case XML_PARSER_SEVERITY_VALIDITY_ERROR:
3096 case XML_PARSER_SEVERITY_ERROR:
3097 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3098 "XML Parsing error: %s", msg);
3104 xml2_read_toc(struct archive_read *a)
3106 xmlTextReaderPtr reader;
3107 struct xmlattr_list list;
3110 reader = xmlReaderForIO(xml2_read_cb, xml2_close_cb, a, NULL, NULL, 0);
3111 if (reader == NULL) {
3112 archive_set_error(&a->archive, ENOMEM,
3113 "Couldn't allocate memory for xml parser");
3114 return (ARCHIVE_FATAL);
3116 xmlTextReaderSetErrorHandler(reader, xml2_error_hdr, a);
3118 while ((r = xmlTextReaderRead(reader)) == 1) {
3119 const char *name, *value;
3122 type = xmlTextReaderNodeType(reader);
3123 name = (const char *)xmlTextReaderConstLocalName(reader);
3125 case XML_READER_TYPE_ELEMENT:
3126 empty = xmlTextReaderIsEmptyElement(reader);
3127 r = xml2_xmlattr_setup(a, &list, reader);
3128 if (r == ARCHIVE_OK)
3129 r = xml_start(a, name, &list);
3130 xmlattr_cleanup(&list);
3131 if (r != ARCHIVE_OK)
3136 case XML_READER_TYPE_END_ELEMENT:
3139 case XML_READER_TYPE_TEXT:
3140 value = (const char *)xmlTextReaderConstValue(reader);
3141 xml_data(a, value, strlen(value));
3143 case XML_READER_TYPE_SIGNIFICANT_WHITESPACE:
3150 xmlFreeTextReader(reader);
3153 return ((r == 0)?ARCHIVE_OK:ARCHIVE_FATAL);
3156 #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
3159 expat_xmlattr_setup(struct archive_read *a,
3160 struct xmlattr_list *list, const XML_Char **atts)
3162 struct xmlattr *attr;
3166 list->last = &(list->first);
3168 return (ARCHIVE_OK);
3169 while (atts[0] != NULL && atts[1] != NULL) {
3170 attr = malloc(sizeof*(attr));
3171 name = strdup(atts[0]);
3172 value = strdup(atts[1]);
3173 if (attr == NULL || name == NULL || value == NULL) {
3174 archive_set_error(&a->archive, ENOMEM, "Out of memory");
3178 return (ARCHIVE_FATAL);
3181 attr->value = value;
3184 list->last = &(attr->next);
3187 return (ARCHIVE_OK);
3191 expat_start_cb(void *userData, const XML_Char *name, const XML_Char **atts)
3193 struct expat_userData *ud = (struct expat_userData *)userData;
3194 struct archive_read *a = ud->archive;
3195 struct xmlattr_list list;
3198 r = expat_xmlattr_setup(a, &list, atts);
3199 if (r == ARCHIVE_OK)
3200 r = xml_start(a, (const char *)name, &list);
3201 xmlattr_cleanup(&list);
3206 expat_end_cb(void *userData, const XML_Char *name)
3208 struct expat_userData *ud = (struct expat_userData *)userData;
3210 xml_end(ud->archive, (const char *)name);
3214 expat_data_cb(void *userData, const XML_Char *s, int len)
3216 struct expat_userData *ud = (struct expat_userData *)userData;
3218 xml_data(ud->archive, s, len);
3222 expat_read_toc(struct archive_read *a)
3226 struct expat_userData ud;
3228 ud.state = ARCHIVE_OK;
3231 xar = (struct xar *)(a->format->data);
3233 /* Initialize XML Parser library. */
3234 parser = XML_ParserCreate(NULL);
3235 if (parser == NULL) {
3236 archive_set_error(&a->archive, ENOMEM,
3237 "Couldn't allocate memory for xml parser");
3238 return (ARCHIVE_FATAL);
3240 XML_SetUserData(parser, &ud);
3241 XML_SetElementHandler(parser, expat_start_cb, expat_end_cb);
3242 XML_SetCharacterDataHandler(parser, expat_data_cb);
3245 while (xar->toc_remaining && ud.state == ARCHIVE_OK) {
3253 r = rd_contents(a, &d, &outbytes, &used, xar->toc_remaining);
3254 if (r != ARCHIVE_OK)
3256 xar->toc_remaining -= used;
3257 xar->offset += used;
3258 xar->toc_total += outbytes;
3259 PRINT_TOC(d, outbytes);
3261 xr = XML_Parse(parser, d, outbytes, xar->toc_remaining == 0);
3262 __archive_read_consume(a, used);
3263 if (xr == XML_STATUS_ERROR) {
3264 XML_ParserFree(parser);
3265 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3266 "XML Parsing failed");
3267 return (ARCHIVE_FATAL);
3270 XML_ParserFree(parser);
3273 #endif /* defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H) */
3275 #endif /* Support xar format */