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
170 #define HAS_CTIME 0x08000
171 #define HAS_MTIME 0x10000
172 #define HAS_ATIME 0x20000
178 enum enctype encoding;
179 struct chksumval a_sum;
180 struct chksumval e_sum;
181 struct archive_string pathname;
182 struct archive_string symlink;
186 struct archive_string uname;
188 struct archive_string gname;
195 struct archive_string fflags_text;
198 struct archive_string hardlink;
199 struct xattr *xattr_list;
207 struct xar_file *files;
211 struct xar_file **files;
230 FILE_DATA_A_CHECKSUM,
231 FILE_DATA_E_CHECKSUM,
261 FILE_ACL_APPLEEXTENDED,
262 /* BSD file flags. */
264 FILE_FLAGS_USER_NODUMP,
265 FILE_FLAGS_USER_IMMUTABLE,
266 FILE_FLAGS_USER_APPEND,
267 FILE_FLAGS_USER_OPAQUE,
268 FILE_FLAGS_USER_NOUNLINK,
269 FILE_FLAGS_SYS_ARCHIVED,
270 FILE_FLAGS_SYS_IMMUTABLE,
271 FILE_FLAGS_SYS_APPEND,
272 FILE_FLAGS_SYS_NOUNLINK,
273 FILE_FLAGS_SYS_SNAPSHOT,
274 /* Linux file flags. */
276 FILE_EXT2_SecureDeletion,
279 FILE_EXT2_Synchronous,
281 FILE_EXT2_AppendOnly,
286 FILE_EXT2_NoCompBlock,
289 FILE_EXT2_HashIndexed,
300 struct unknown_tag *next;
301 struct archive_string name;
305 uint64_t offset; /* Current position in the file. */
309 #define OUTBUFF_SIZE (1024 * 64)
310 unsigned char *outbuff;
312 enum xmlstatus xmlsts;
313 enum xmlstatus xmlsts_unknown;
314 struct unknown_tag *unknowntags;
320 uint64_t toc_remaining;
322 uint64_t toc_chksum_offset;
323 uint64_t toc_chksum_size;
328 enum enctype rd_encoding;
331 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
335 #if HAVE_LZMA_H && HAVE_LIBLZMA
336 lzma_stream lzstream;
342 struct chksumwork a_sumwrk;
343 struct chksumwork e_sumwrk;
345 struct xar_file *file; /* current reading file. */
346 struct xattr *xattr; /* current reading extended attribute. */
347 struct heap_queue file_queue;
348 struct xar_file *hdlink_orgs;
349 struct hdlink *hdlink_list;
352 uint64_t entry_total;
353 uint64_t entry_remaining;
354 size_t entry_unconsumed;
356 enum enctype entry_encoding;
357 struct chksumval entry_a_sum;
358 struct chksumval entry_e_sum;
360 struct archive_string_conv *sconv;
364 struct xmlattr *next;
369 struct xmlattr_list {
370 struct xmlattr *first;
371 struct xmlattr **last;
374 static int xar_bid(struct archive_read *, int);
375 static int xar_read_header(struct archive_read *,
376 struct archive_entry *);
377 static int xar_read_data(struct archive_read *,
378 const void **, size_t *, int64_t *);
379 static int xar_read_data_skip(struct archive_read *);
380 static int xar_cleanup(struct archive_read *);
381 static int move_reading_point(struct archive_read *, uint64_t);
382 static int rd_contents_init(struct archive_read *,
383 enum enctype, int, int);
384 static int rd_contents(struct archive_read *, const void **,
385 size_t *, size_t *, uint64_t);
386 static uint64_t atol10(const char *, size_t);
387 static int64_t atol8(const char *, size_t);
388 static size_t atohex(unsigned char *, size_t, const char *, size_t);
389 static time_t parse_time(const char *p, size_t n);
390 static int heap_add_entry(struct archive_read *a,
391 struct heap_queue *, struct xar_file *);
392 static struct xar_file *heap_get_entry(struct heap_queue *);
393 static int add_link(struct archive_read *,
394 struct xar *, struct xar_file *);
395 static void checksum_init(struct archive_read *, int, int);
396 static void checksum_update(struct archive_read *, const void *,
397 size_t, const void *, size_t);
398 static int checksum_final(struct archive_read *, const void *,
399 size_t, const void *, size_t);
400 static void checksum_cleanup(struct archive_read *);
401 static int decompression_init(struct archive_read *, enum enctype);
402 static int decompress(struct archive_read *, const void **,
403 size_t *, const void *, size_t *);
404 static int decompression_cleanup(struct archive_read *);
405 static void xmlattr_cleanup(struct xmlattr_list *);
406 static int file_new(struct archive_read *,
407 struct xar *, struct xmlattr_list *);
408 static void file_free(struct xar_file *);
409 static int xattr_new(struct archive_read *,
410 struct xar *, struct xmlattr_list *);
411 static void xattr_free(struct xattr *);
412 static int getencoding(struct xmlattr_list *);
413 static int getsumalgorithm(struct xmlattr_list *);
414 static int unknowntag_start(struct archive_read *,
415 struct xar *, const char *);
416 static void unknowntag_end(struct xar *, const char *);
417 static int xml_start(struct archive_read *,
418 const char *, struct xmlattr_list *);
419 static void xml_end(void *, const char *);
420 static void xml_data(void *, const char *, int);
421 static int xml_parse_file_flags(struct xar *, const char *);
422 static int xml_parse_file_ext2(struct xar *, const char *);
423 #if defined(HAVE_LIBXML_XMLREADER_H)
424 static int xml2_xmlattr_setup(struct archive_read *,
425 struct xmlattr_list *, xmlTextReaderPtr);
426 static int xml2_read_cb(void *, char *, int);
427 static int xml2_close_cb(void *);
428 static void xml2_error_hdr(void *, const char *, xmlParserSeverities,
429 xmlTextReaderLocatorPtr);
430 static int xml2_read_toc(struct archive_read *);
431 #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
432 struct expat_userData {
434 struct archive_read *archive;
436 static int expat_xmlattr_setup(struct archive_read *,
437 struct xmlattr_list *, const XML_Char **);
438 static void expat_start_cb(void *, const XML_Char *, const XML_Char **);
439 static void expat_end_cb(void *, const XML_Char *);
440 static void expat_data_cb(void *, const XML_Char *, int);
441 static int expat_read_toc(struct archive_read *);
445 archive_read_support_format_xar(struct archive *_a)
448 struct archive_read *a = (struct archive_read *)_a;
451 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
452 ARCHIVE_STATE_NEW, "archive_read_support_format_xar");
454 xar = (struct xar *)calloc(1, sizeof(*xar));
456 archive_set_error(&a->archive, ENOMEM,
457 "Can't allocate xar data");
458 return (ARCHIVE_FATAL);
461 r = __archive_read_register_format(a,
479 xar_bid(struct archive_read *a, int best_bid)
481 const unsigned char *b;
484 (void)best_bid; /* UNUSED */
486 b = __archive_read_ahead(a, HEADER_SIZE, NULL);
494 if (archive_be32dec(b) != HEADER_MAGIC)
500 if (archive_be16dec(b+4) != HEADER_SIZE)
504 * Verify header version
506 if (archive_be16dec(b+6) != HEADER_VERSION)
510 * Verify type of checksum
512 switch (archive_be32dec(b+24)) {
526 read_toc(struct archive_read *a)
529 struct xar_file *file;
530 const unsigned char *b;
531 uint64_t toc_compressed_size;
532 uint64_t toc_uncompressed_size;
533 uint32_t toc_chksum_alg;
537 xar = (struct xar *)(a->format->data);
542 b = __archive_read_ahead(a, HEADER_SIZE, &bytes);
545 if (bytes < HEADER_SIZE) {
546 archive_set_error(&a->archive,
547 ARCHIVE_ERRNO_FILE_FORMAT,
548 "Truncated archive header");
549 return (ARCHIVE_FATAL);
552 if (archive_be32dec(b) != HEADER_MAGIC) {
553 archive_set_error(&a->archive,
554 ARCHIVE_ERRNO_FILE_FORMAT,
555 "Invalid header magic");
556 return (ARCHIVE_FATAL);
558 if (archive_be16dec(b+6) != HEADER_VERSION) {
559 archive_set_error(&a->archive,
560 ARCHIVE_ERRNO_FILE_FORMAT,
561 "Unsupported header version(%d)",
562 archive_be16dec(b+6));
563 return (ARCHIVE_FATAL);
565 toc_compressed_size = archive_be64dec(b+8);
566 xar->toc_remaining = toc_compressed_size;
567 toc_uncompressed_size = archive_be64dec(b+16);
568 toc_chksum_alg = archive_be32dec(b+24);
569 __archive_read_consume(a, HEADER_SIZE);
570 xar->offset += HEADER_SIZE;
574 * Read TOC(Table of Contents).
576 /* Initialize reading contents. */
577 r = move_reading_point(a, HEADER_SIZE);
580 r = rd_contents_init(a, GZIP, toc_chksum_alg, CKSUM_NONE);
584 #ifdef HAVE_LIBXML_XMLREADER_H
585 r = xml2_read_toc(a);
586 #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
587 r = expat_read_toc(a);
592 /* Set 'The HEAP' base. */
593 xar->h_base = xar->offset;
594 if (xar->toc_total != toc_uncompressed_size) {
595 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
596 "TOC uncompressed size error");
597 return (ARCHIVE_FATAL);
603 if (toc_chksum_alg != CKSUM_NONE) {
604 r = move_reading_point(a, xar->toc_chksum_offset);
607 b = __archive_read_ahead(a,
608 (size_t)xar->toc_chksum_size, &bytes);
611 if ((uint64_t)bytes < xar->toc_chksum_size) {
612 archive_set_error(&a->archive,
613 ARCHIVE_ERRNO_FILE_FORMAT,
614 "Truncated archive file");
615 return (ARCHIVE_FATAL);
617 r = checksum_final(a, b,
618 (size_t)xar->toc_chksum_size, NULL, 0);
619 __archive_read_consume(a, xar->toc_chksum_size);
620 xar->offset += xar->toc_chksum_size;
622 return (ARCHIVE_FATAL);
626 * Connect hardlinked files.
628 for (file = xar->hdlink_orgs; file != NULL; file = file->hdnext) {
629 struct hdlink **hdlink;
631 for (hdlink = &(xar->hdlink_list); *hdlink != NULL;
632 hdlink = &((*hdlink)->next)) {
633 if ((*hdlink)->id == file->id) {
634 struct hdlink *hltmp;
636 int nlink = (*hdlink)->cnt + 1;
639 for (f2 = (*hdlink)->files; f2 != NULL;
643 &(f2->hardlink), &(file->pathname));
645 /* Remove resolved files from hdlist_list. */
647 *hdlink = hltmp->next;
653 a->archive.archive_format = ARCHIVE_FORMAT_XAR;
654 a->archive.archive_format_name = "xar";
660 xar_read_header(struct archive_read *a, struct archive_entry *entry)
663 struct xar_file *file;
667 xar = (struct xar *)(a->format->data);
670 if (xar->offset == 0) {
671 /* Create a character conversion object. */
672 if (xar->sconv == NULL) {
673 xar->sconv = archive_string_conversion_from_charset(
674 &(a->archive), "UTF-8", 1);
675 if (xar->sconv == NULL)
676 return (ARCHIVE_FATAL);
686 file = xar->file = heap_get_entry(&(xar->file_queue));
688 xar->end_of_file = 1;
689 return (ARCHIVE_EOF);
691 if ((file->mode & AE_IFMT) != AE_IFDIR)
693 if (file->has != (HAS_PATHNAME | HAS_TYPE))
696 * If a file type is a directory and it does not have
697 * any metadata, do not export.
701 if (file->has & HAS_ATIME) {
702 archive_entry_set_atime(entry, file->atime, 0);
704 if (file->has & HAS_CTIME) {
705 archive_entry_set_ctime(entry, file->ctime, 0);
707 if (file->has & HAS_MTIME) {
708 archive_entry_set_mtime(entry, file->mtime, 0);
710 archive_entry_set_gid(entry, file->gid);
711 if (file->gname.length > 0 &&
712 archive_entry_copy_gname_l(entry, file->gname.s,
713 archive_strlen(&(file->gname)), xar->sconv) != 0) {
714 if (errno == ENOMEM) {
715 archive_set_error(&a->archive, ENOMEM,
716 "Can't allocate memory for Gname");
717 return (ARCHIVE_FATAL);
719 archive_set_error(&a->archive,
720 ARCHIVE_ERRNO_FILE_FORMAT,
721 "Gname cannot be converted from %s to current locale.",
722 archive_string_conversion_charset_name(xar->sconv));
725 archive_entry_set_uid(entry, file->uid);
726 if (file->uname.length > 0 &&
727 archive_entry_copy_uname_l(entry, file->uname.s,
728 archive_strlen(&(file->uname)), xar->sconv) != 0) {
729 if (errno == ENOMEM) {
730 archive_set_error(&a->archive, ENOMEM,
731 "Can't allocate memory for Uname");
732 return (ARCHIVE_FATAL);
734 archive_set_error(&a->archive,
735 ARCHIVE_ERRNO_FILE_FORMAT,
736 "Uname cannot be converted from %s to current locale.",
737 archive_string_conversion_charset_name(xar->sconv));
740 archive_entry_set_mode(entry, file->mode);
741 if (archive_entry_copy_pathname_l(entry, file->pathname.s,
742 archive_strlen(&(file->pathname)), xar->sconv) != 0) {
743 if (errno == ENOMEM) {
744 archive_set_error(&a->archive, ENOMEM,
745 "Can't allocate memory for Pathname");
746 return (ARCHIVE_FATAL);
748 archive_set_error(&a->archive,
749 ARCHIVE_ERRNO_FILE_FORMAT,
750 "Pathname cannot be converted from %s to current locale.",
751 archive_string_conversion_charset_name(xar->sconv));
756 if (file->symlink.length > 0 &&
757 archive_entry_copy_symlink_l(entry, file->symlink.s,
758 archive_strlen(&(file->symlink)), xar->sconv) != 0) {
759 if (errno == ENOMEM) {
760 archive_set_error(&a->archive, ENOMEM,
761 "Can't allocate memory for Linkname");
762 return (ARCHIVE_FATAL);
764 archive_set_error(&a->archive,
765 ARCHIVE_ERRNO_FILE_FORMAT,
766 "Linkname cannot be converted from %s to current locale.",
767 archive_string_conversion_charset_name(xar->sconv));
770 /* Set proper nlink. */
771 if ((file->mode & AE_IFMT) == AE_IFDIR)
772 archive_entry_set_nlink(entry, file->subdirs + 2);
774 archive_entry_set_nlink(entry, file->nlink);
775 archive_entry_set_size(entry, file->size);
776 if (archive_strlen(&(file->hardlink)) > 0)
777 archive_entry_set_hardlink(entry, file->hardlink.s);
778 archive_entry_set_ino64(entry, file->ino64);
779 if (file->has & HAS_DEV)
780 archive_entry_set_dev(entry, file->dev);
781 if (file->has & HAS_DEVMAJOR)
782 archive_entry_set_devmajor(entry, file->devmajor);
783 if (file->has & HAS_DEVMINOR)
784 archive_entry_set_devminor(entry, file->devminor);
785 if (archive_strlen(&(file->fflags_text)) > 0)
786 archive_entry_copy_fflags_text(entry, file->fflags_text.s);
789 xar->entry_total = 0;
790 xar->entry_remaining = file->length;
791 xar->entry_size = file->size;
792 xar->entry_encoding = file->encoding;
793 xar->entry_a_sum = file->a_sum;
794 xar->entry_e_sum = file->e_sum;
796 * Read extended attributes.
798 xattr = file->xattr_list;
799 while (xattr != NULL) {
801 size_t outbytes, used;
803 r = move_reading_point(a, xattr->offset);
806 r = rd_contents_init(a, xattr->encoding,
807 xattr->a_sum.alg, xattr->e_sum.alg);
811 r = rd_contents(a, &d, &outbytes, &used, xattr->length);
814 if (outbytes != xattr->size) {
815 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
816 "Decompressed size error");
820 r = checksum_final(a,
821 xattr->a_sum.val, xattr->a_sum.len,
822 xattr->e_sum.val, xattr->e_sum.len);
825 archive_entry_xattr_add_entry(entry,
826 xattr->name.s, d, outbytes);
829 if (r != ARCHIVE_OK) {
834 if (xar->entry_remaining > 0)
835 /* Move reading point to the beginning of current
837 r = move_reading_point(a, file->offset);
846 xar_read_data(struct archive_read *a,
847 const void **buff, size_t *size, int64_t *offset)
853 xar = (struct xar *)(a->format->data);
855 if (xar->entry_unconsumed) {
856 __archive_read_consume(a, xar->entry_unconsumed);
857 xar->entry_unconsumed = 0;
860 if (xar->end_of_file || xar->entry_remaining <= 0) {
862 goto abort_read_data;
865 if (xar->entry_init) {
866 r = rd_contents_init(a, xar->entry_encoding,
867 xar->entry_a_sum.alg, xar->entry_e_sum.alg);
868 if (r != ARCHIVE_OK) {
869 xar->entry_remaining = 0;
876 r = rd_contents(a, buff, size, &used, xar->entry_remaining);
878 goto abort_read_data;
880 *offset = xar->entry_total;
881 xar->entry_total += *size;
884 xar->entry_remaining -= used;
885 xar->entry_unconsumed = used;
887 if (xar->entry_remaining == 0) {
888 if (xar->entry_total != xar->entry_size) {
889 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
890 "Decompressed size error");
892 goto abort_read_data;
894 r = checksum_final(a,
895 xar->entry_a_sum.val, xar->entry_a_sum.len,
896 xar->entry_e_sum.val, xar->entry_e_sum.len);
898 goto abort_read_data;
905 *offset = xar->total;
910 xar_read_data_skip(struct archive_read *a)
913 int64_t bytes_skipped;
915 xar = (struct xar *)(a->format->data);
916 if (xar->end_of_file)
917 return (ARCHIVE_EOF);
918 bytes_skipped = __archive_read_consume(a, xar->entry_remaining +
919 xar->entry_unconsumed);
920 if (bytes_skipped < 0)
921 return (ARCHIVE_FATAL);
922 xar->offset += bytes_skipped;
923 xar->entry_unconsumed = 0;
928 xar_cleanup(struct archive_read *a)
931 struct hdlink *hdlink;
935 xar = (struct xar *)(a->format->data);
937 r = decompression_cleanup(a);
938 hdlink = xar->hdlink_list;
939 while (hdlink != NULL) {
940 struct hdlink *next = hdlink->next;
945 for (i = 0; i < xar->file_queue.used; i++)
946 file_free(xar->file_queue.files[i]);
947 free(xar->file_queue.files);
948 while (xar->unknowntags != NULL) {
949 struct unknown_tag *tag;
951 tag = xar->unknowntags;
952 xar->unknowntags = tag->next;
953 archive_string_free(&(tag->name));
958 a->format->data = NULL;
963 move_reading_point(struct archive_read *a, uint64_t offset)
967 xar = (struct xar *)(a->format->data);
968 if (xar->offset - xar->h_base != offset) {
969 /* Seek forward to the start of file contents. */
972 step = offset - (xar->offset - xar->h_base);
974 step = __archive_read_consume(a, step);
979 int64_t pos = __archive_read_seek(a, xar->h_base + offset, SEEK_SET);
980 if (pos == ARCHIVE_FAILED) {
981 archive_set_error(&(a->archive),
984 return (ARCHIVE_FAILED);
993 rd_contents_init(struct archive_read *a, enum enctype encoding,
994 int a_sum_alg, int e_sum_alg)
998 /* Init decompress library. */
999 if ((r = decompression_init(a, encoding)) != ARCHIVE_OK)
1001 /* Init checksum library. */
1002 checksum_init(a, a_sum_alg, e_sum_alg);
1003 return (ARCHIVE_OK);
1007 rd_contents(struct archive_read *a, const void **buff, size_t *size,
1008 size_t *used, uint64_t remaining)
1010 const unsigned char *b;
1013 /* Get whatever bytes are immediately available. */
1014 b = __archive_read_ahead(a, 1, &bytes);
1016 return ((int)bytes);
1018 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1019 "Truncated archive file");
1020 return (ARCHIVE_FATAL);
1022 if ((uint64_t)bytes > remaining)
1023 bytes = (ssize_t)remaining;
1026 * Decompress contents of file.
1029 if (decompress(a, buff, size, b, used) != ARCHIVE_OK)
1030 return (ARCHIVE_FATAL);
1033 * Update checksum of a compressed data and a extracted data.
1035 checksum_update(a, b, *used, *buff, *size);
1037 return (ARCHIVE_OK);
1041 * Note that this implementation does not (and should not!) obey
1042 * locale settings; you cannot simply substitute strtol here, since
1043 * it does obey locale.
1047 atol10(const char *p, size_t char_cnt)
1057 while (digit >= 0 && digit < 10 && char_cnt-- > 0) {
1058 l = (l * 10) + digit;
1065 atol8(const char *p, size_t char_cnt)
1074 while (char_cnt-- > 0) {
1075 if (*p >= '0' && *p <= '7')
1087 atohex(unsigned char *b, size_t bsize, const char *p, size_t psize)
1089 size_t fbsize = bsize;
1091 while (bsize && psize > 1) {
1094 if (p[0] >= 'a' && p[0] <= 'z')
1095 x = (p[0] - 'a' + 0x0a) << 4;
1096 else if (p[0] >= 'A' && p[0] <= 'Z')
1097 x = (p[0] - 'A' + 0x0a) << 4;
1098 else if (p[0] >= '0' && p[0] <= '9')
1099 x = (p[0] - '0') << 4;
1102 if (p[1] >= 'a' && p[1] <= 'z')
1103 x |= p[1] - 'a' + 0x0a;
1104 else if (p[1] >= 'A' && p[1] <= 'Z')
1105 x |= p[1] - 'A' + 0x0a;
1106 else if (p[1] >= '0' && p[1] <= '9')
1116 return (fbsize - bsize);
1120 time_from_tm(struct tm *t)
1123 /* Use platform timegm() if available. */
1125 #elif HAVE__MKGMTIME64
1126 return (_mkgmtime64(t));
1128 /* Else use direct calculation using POSIX assumptions. */
1129 /* First, fix up tm_yday based on the year/month/day. */
1131 /* Then we can compute timegm() from first principles. */
1135 + t->tm_yday * 86400
1136 + (t->tm_year - 70) * 31536000
1137 + ((t->tm_year - 69) / 4) * 86400
1138 - ((t->tm_year - 1) / 100) * 86400
1139 + ((t->tm_year + 299) / 400) * 86400);
1144 parse_time(const char *p, size_t n)
1150 memset(&tm, 0, sizeof(tm));
1153 data = atol10(p, 4);
1156 tm.tm_year = (int)data - 1900;
1160 data = atol10(p, 2);
1161 if (data < 1 || data > 12)
1163 tm.tm_mon = (int)data -1;
1167 data = atol10(p, 2);
1168 if (data < 1 || data > 31)
1170 tm.tm_mday = (int)data;
1174 data = atol10(p, 2);
1175 if (data < 0 || data > 23)
1177 tm.tm_hour = (int)data;
1181 data = atol10(p, 2);
1182 if (data < 0 || data > 59)
1184 tm.tm_min = (int)data;
1188 data = atol10(p, 2);
1189 if (data < 0 || data > 60)
1191 tm.tm_sec = (int)data;
1198 t = time_from_tm(&tm);
1204 heap_add_entry(struct archive_read *a,
1205 struct heap_queue *heap, struct xar_file *file)
1207 uint64_t file_id, parent_id;
1210 /* Expand our pending files list as necessary. */
1211 if (heap->used >= heap->allocated) {
1212 struct xar_file **new_pending_files;
1213 int new_size = heap->allocated * 2;
1215 if (heap->allocated < 1024)
1217 /* Overflow might keep us from growing the list. */
1218 if (new_size <= heap->allocated) {
1219 archive_set_error(&a->archive,
1220 ENOMEM, "Out of memory");
1221 return (ARCHIVE_FATAL);
1223 new_pending_files = (struct xar_file **)
1224 malloc(new_size * sizeof(new_pending_files[0]));
1225 if (new_pending_files == NULL) {
1226 archive_set_error(&a->archive,
1227 ENOMEM, "Out of memory");
1228 return (ARCHIVE_FATAL);
1230 memcpy(new_pending_files, heap->files,
1231 heap->allocated * sizeof(new_pending_files[0]));
1233 heap->files = new_pending_files;
1234 heap->allocated = new_size;
1240 * Start with hole at end, walk it up tree to find insertion point.
1242 hole = heap->used++;
1244 parent = (hole - 1)/2;
1245 parent_id = heap->files[parent]->id;
1246 if (file_id >= parent_id) {
1247 heap->files[hole] = file;
1248 return (ARCHIVE_OK);
1250 /* Move parent into hole <==> move hole up tree. */
1251 heap->files[hole] = heap->files[parent];
1254 heap->files[0] = file;
1256 return (ARCHIVE_OK);
1259 static struct xar_file *
1260 heap_get_entry(struct heap_queue *heap)
1262 uint64_t a_id, b_id, c_id;
1264 struct xar_file *r, *tmp;
1270 * The first file in the list is the earliest; we'll return this.
1275 * Move the last item in the heap to the root of the tree
1277 heap->files[0] = heap->files[--(heap->used)];
1280 * Rebalance the heap.
1282 a = 0; /* Starting element and its heap key */
1283 a_id = heap->files[a]->id;
1285 b = a + a + 1; /* First child */
1286 if (b >= heap->used)
1288 b_id = heap->files[b]->id;
1289 c = b + 1; /* Use second child if it is smaller. */
1290 if (c < heap->used) {
1291 c_id = heap->files[c]->id;
1299 tmp = heap->files[a];
1300 heap->files[a] = heap->files[b];
1301 heap->files[b] = tmp;
1307 add_link(struct archive_read *a, struct xar *xar, struct xar_file *file)
1309 struct hdlink *hdlink;
1311 for (hdlink = xar->hdlink_list; hdlink != NULL; hdlink = hdlink->next) {
1312 if (hdlink->id == file->link) {
1313 file->hdnext = hdlink->files;
1315 hdlink->files = file;
1316 return (ARCHIVE_OK);
1319 hdlink = malloc(sizeof(*hdlink));
1320 if (hdlink == NULL) {
1321 archive_set_error(&a->archive, ENOMEM, "Out of memory");
1322 return (ARCHIVE_FATAL);
1324 file->hdnext = NULL;
1325 hdlink->id = file->link;
1327 hdlink->files = file;
1328 hdlink->next = xar->hdlink_list;
1329 xar->hdlink_list = hdlink;
1330 return (ARCHIVE_OK);
1334 _checksum_init(struct chksumwork *sumwrk, int sum_alg)
1336 sumwrk->alg = sum_alg;
1341 archive_sha1_init(&(sumwrk->sha1ctx));
1344 archive_md5_init(&(sumwrk->md5ctx));
1350 _checksum_update(struct chksumwork *sumwrk, const void *buff, size_t size)
1353 switch (sumwrk->alg) {
1357 archive_sha1_update(&(sumwrk->sha1ctx), buff, size);
1360 archive_md5_update(&(sumwrk->md5ctx), buff, size);
1366 _checksum_final(struct chksumwork *sumwrk, const void *val, size_t len)
1368 unsigned char sum[MAX_SUM_SIZE];
1371 switch (sumwrk->alg) {
1375 archive_sha1_final(&(sumwrk->sha1ctx), sum);
1376 if (len != SHA1_SIZE ||
1377 memcmp(val, sum, SHA1_SIZE) != 0)
1381 archive_md5_final(&(sumwrk->md5ctx), sum);
1382 if (len != MD5_SIZE ||
1383 memcmp(val, sum, MD5_SIZE) != 0)
1391 checksum_init(struct archive_read *a, int a_sum_alg, int e_sum_alg)
1395 xar = (struct xar *)(a->format->data);
1396 _checksum_init(&(xar->a_sumwrk), a_sum_alg);
1397 _checksum_init(&(xar->e_sumwrk), e_sum_alg);
1401 checksum_update(struct archive_read *a, const void *abuff, size_t asize,
1402 const void *ebuff, size_t esize)
1406 xar = (struct xar *)(a->format->data);
1407 _checksum_update(&(xar->a_sumwrk), abuff, asize);
1408 _checksum_update(&(xar->e_sumwrk), ebuff, esize);
1412 checksum_final(struct archive_read *a, const void *a_sum_val,
1413 size_t a_sum_len, const void *e_sum_val, size_t e_sum_len)
1418 xar = (struct xar *)(a->format->data);
1419 r = _checksum_final(&(xar->a_sumwrk), a_sum_val, a_sum_len);
1420 if (r == ARCHIVE_OK)
1421 r = _checksum_final(&(xar->e_sumwrk), e_sum_val, e_sum_len);
1422 if (r != ARCHIVE_OK)
1423 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
1429 decompression_init(struct archive_read *a, enum enctype encoding)
1435 xar = (struct xar *)(a->format->data);
1436 xar->rd_encoding = encoding;
1441 if (xar->stream_valid)
1442 r = inflateReset(&(xar->stream));
1444 r = inflateInit(&(xar->stream));
1446 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1447 "Couldn't initialize zlib stream.");
1448 return (ARCHIVE_FATAL);
1450 xar->stream_valid = 1;
1451 xar->stream.total_in = 0;
1452 xar->stream.total_out = 0;
1454 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1456 if (xar->bzstream_valid) {
1457 BZ2_bzDecompressEnd(&(xar->bzstream));
1458 xar->bzstream_valid = 0;
1460 r = BZ2_bzDecompressInit(&(xar->bzstream), 0, 0);
1461 if (r == BZ_MEM_ERROR)
1462 r = BZ2_bzDecompressInit(&(xar->bzstream), 0, 1);
1464 int err = ARCHIVE_ERRNO_MISC;
1467 case BZ_PARAM_ERROR:
1468 detail = "invalid setup parameter";
1472 detail = "out of memory";
1474 case BZ_CONFIG_ERROR:
1475 detail = "mis-compiled library";
1478 archive_set_error(&a->archive, err,
1479 "Internal error initializing decompressor: %s",
1480 detail == NULL ? "??" : detail);
1481 xar->bzstream_valid = 0;
1482 return (ARCHIVE_FATAL);
1484 xar->bzstream_valid = 1;
1485 xar->bzstream.total_in_lo32 = 0;
1486 xar->bzstream.total_in_hi32 = 0;
1487 xar->bzstream.total_out_lo32 = 0;
1488 xar->bzstream.total_out_hi32 = 0;
1491 #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1492 #if LZMA_VERSION_MAJOR >= 5
1493 /* Effectively disable the limiter. */
1494 #define LZMA_MEMLIMIT UINT64_MAX
1496 /* NOTE: This needs to check memory size which running system has. */
1497 #define LZMA_MEMLIMIT (1U << 30)
1501 if (xar->lzstream_valid) {
1502 lzma_end(&(xar->lzstream));
1503 xar->lzstream_valid = 0;
1505 if (xar->entry_encoding == XZ)
1506 r = lzma_stream_decoder(&(xar->lzstream),
1507 LZMA_MEMLIMIT,/* memlimit */
1510 r = lzma_alone_decoder(&(xar->lzstream),
1511 LZMA_MEMLIMIT);/* memlimit */
1514 case LZMA_MEM_ERROR:
1515 archive_set_error(&a->archive,
1517 "Internal error initializing "
1518 "compression library: "
1519 "Cannot allocate memory");
1521 case LZMA_OPTIONS_ERROR:
1522 archive_set_error(&a->archive,
1524 "Internal error initializing "
1525 "compression library: "
1526 "Invalid or unsupported options");
1529 archive_set_error(&a->archive,
1531 "Internal error initializing "
1535 return (ARCHIVE_FATAL);
1537 xar->lzstream_valid = 1;
1538 xar->lzstream.total_in = 0;
1539 xar->lzstream.total_out = 0;
1543 * Unsupported compression.
1546 #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR)
1549 #if !defined(HAVE_LZMA_H) || !defined(HAVE_LIBLZMA)
1553 switch (xar->entry_encoding) {
1554 case BZIP2: detail = "bzip2"; break;
1555 case LZMA: detail = "lzma"; break;
1556 case XZ: detail = "xz"; break;
1557 default: detail = "??"; break;
1559 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1560 "%s compression not supported on this platform",
1562 return (ARCHIVE_FAILED);
1564 return (ARCHIVE_OK);
1568 decompress(struct archive_read *a, const void **buff, size_t *outbytes,
1569 const void *b, size_t *used)
1573 size_t avail_in, avail_out;
1576 xar = (struct xar *)(a->format->data);
1578 outbuff = (void *)(uintptr_t)*buff;
1579 if (outbuff == NULL) {
1580 if (xar->outbuff == NULL) {
1581 xar->outbuff = malloc(OUTBUFF_SIZE);
1582 if (xar->outbuff == NULL) {
1583 archive_set_error(&a->archive, ENOMEM,
1584 "Couldn't allocate memory for out buffer");
1585 return (ARCHIVE_FATAL);
1588 outbuff = xar->outbuff;
1590 avail_out = OUTBUFF_SIZE;
1592 avail_out = *outbytes;
1593 switch (xar->rd_encoding) {
1595 xar->stream.next_in = (Bytef *)(uintptr_t)b;
1596 xar->stream.avail_in = avail_in;
1597 xar->stream.next_out = (unsigned char *)outbuff;
1598 xar->stream.avail_out = avail_out;
1599 r = inflate(&(xar->stream), 0);
1601 case Z_OK: /* Decompressor made some progress.*/
1602 case Z_STREAM_END: /* Found end of stream. */
1605 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1606 "File decompression failed (%d)", r);
1607 return (ARCHIVE_FATAL);
1609 *used = avail_in - xar->stream.avail_in;
1610 *outbytes = avail_out - xar->stream.avail_out;
1612 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1614 xar->bzstream.next_in = (char *)(uintptr_t)b;
1615 xar->bzstream.avail_in = avail_in;
1616 xar->bzstream.next_out = (char *)outbuff;
1617 xar->bzstream.avail_out = avail_out;
1618 r = BZ2_bzDecompress(&(xar->bzstream));
1620 case BZ_STREAM_END: /* Found end of stream. */
1621 switch (BZ2_bzDecompressEnd(&(xar->bzstream))) {
1625 archive_set_error(&(a->archive),
1627 "Failed to clean up decompressor");
1628 return (ARCHIVE_FATAL);
1630 xar->bzstream_valid = 0;
1632 case BZ_OK: /* Decompressor made some progress. */
1635 archive_set_error(&(a->archive),
1637 "bzip decompression failed");
1638 return (ARCHIVE_FATAL);
1640 *used = avail_in - xar->bzstream.avail_in;
1641 *outbytes = avail_out - xar->bzstream.avail_out;
1644 #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1647 xar->lzstream.next_in = b;
1648 xar->lzstream.avail_in = avail_in;
1649 xar->lzstream.next_out = (unsigned char *)outbuff;
1650 xar->lzstream.avail_out = avail_out;
1651 r = lzma_code(&(xar->lzstream), LZMA_RUN);
1653 case LZMA_STREAM_END: /* Found end of stream. */
1654 lzma_end(&(xar->lzstream));
1655 xar->lzstream_valid = 0;
1657 case LZMA_OK: /* Decompressor made some progress. */
1660 archive_set_error(&(a->archive),
1662 "%s decompression failed(%d)",
1663 (xar->entry_encoding == XZ)?"xz":"lzma",
1665 return (ARCHIVE_FATAL);
1667 *used = avail_in - xar->lzstream.avail_in;
1668 *outbytes = avail_out - xar->lzstream.avail_out;
1671 #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR)
1674 #if !defined(HAVE_LZMA_H) || !defined(HAVE_LIBLZMA)
1680 if (outbuff == xar->outbuff) {
1683 *outbytes = avail_in;
1685 if (avail_out > avail_in)
1686 avail_out = avail_in;
1687 memcpy(outbuff, b, avail_out);
1689 *outbytes = avail_out;
1693 return (ARCHIVE_OK);
1697 decompression_cleanup(struct archive_read *a)
1702 xar = (struct xar *)(a->format->data);
1704 if (xar->stream_valid) {
1705 if (inflateEnd(&(xar->stream)) != Z_OK) {
1706 archive_set_error(&a->archive,
1708 "Failed to clean up zlib decompressor");
1712 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1713 if (xar->bzstream_valid) {
1714 if (BZ2_bzDecompressEnd(&(xar->bzstream)) != BZ_OK) {
1715 archive_set_error(&a->archive,
1717 "Failed to clean up bzip2 decompressor");
1722 #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1723 if (xar->lzstream_valid)
1724 lzma_end(&(xar->lzstream));
1725 #elif defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1726 if (xar->lzstream_valid) {
1727 if (lzmadec_end(&(xar->lzstream)) != LZMADEC_OK) {
1728 archive_set_error(&a->archive,
1730 "Failed to clean up lzmadec decompressor");
1739 checksum_cleanup(struct archive_read *a) {
1742 xar = (struct xar *)(a->format->data);
1744 _checksum_final(&(xar->a_sumwrk), NULL, 0);
1745 _checksum_final(&(xar->e_sumwrk), NULL, 0);
1749 xmlattr_cleanup(struct xmlattr_list *list)
1751 struct xmlattr *attr, *next;
1754 while (attr != NULL) {
1762 list->last = &(list->first);
1766 file_new(struct archive_read *a, struct xar *xar, struct xmlattr_list *list)
1768 struct xar_file *file;
1769 struct xmlattr *attr;
1771 file = calloc(1, sizeof(*file));
1773 archive_set_error(&a->archive, ENOMEM, "Out of memory");
1774 return (ARCHIVE_FATAL);
1776 file->parent = xar->file;
1777 file->mode = 0777 | AE_IFREG;
1782 for (attr = list->first; attr != NULL; attr = attr->next) {
1783 if (strcmp(attr->name, "id") == 0)
1784 file->id = atol10(attr->value, strlen(attr->value));
1787 if (heap_add_entry(a, &(xar->file_queue), file) != ARCHIVE_OK)
1788 return (ARCHIVE_FATAL);
1789 return (ARCHIVE_OK);
1793 file_free(struct xar_file *file)
1795 struct xattr *xattr;
1797 archive_string_free(&(file->pathname));
1798 archive_string_free(&(file->symlink));
1799 archive_string_free(&(file->uname));
1800 archive_string_free(&(file->gname));
1801 archive_string_free(&(file->hardlink));
1802 xattr = file->xattr_list;
1803 while (xattr != NULL) {
1815 xattr_new(struct archive_read *a, struct xar *xar, struct xmlattr_list *list)
1817 struct xattr *xattr, **nx;
1818 struct xmlattr *attr;
1820 xattr = calloc(1, sizeof(*xattr));
1821 if (xattr == NULL) {
1822 archive_set_error(&a->archive, ENOMEM, "Out of memory");
1823 return (ARCHIVE_FATAL);
1826 for (attr = list->first; attr != NULL; attr = attr->next) {
1827 if (strcmp(attr->name, "id") == 0)
1828 xattr->id = atol10(attr->value, strlen(attr->value));
1830 /* Chain to xattr list. */
1831 for (nx = &(xar->file->xattr_list);
1832 *nx != NULL; nx = &((*nx)->next)) {
1833 if (xattr->id < (*nx)->id)
1839 return (ARCHIVE_OK);
1843 xattr_free(struct xattr *xattr)
1845 archive_string_free(&(xattr->name));
1850 getencoding(struct xmlattr_list *list)
1852 struct xmlattr *attr;
1853 enum enctype encoding = NONE;
1855 for (attr = list->first; attr != NULL; attr = attr->next) {
1856 if (strcmp(attr->name, "style") == 0) {
1857 if (strcmp(attr->value, "application/octet-stream") == 0)
1859 else if (strcmp(attr->value, "application/x-gzip") == 0)
1861 else if (strcmp(attr->value, "application/x-bzip2") == 0)
1863 else if (strcmp(attr->value, "application/x-lzma") == 0)
1865 else if (strcmp(attr->value, "application/x-xz") == 0)
1873 getsumalgorithm(struct xmlattr_list *list)
1875 struct xmlattr *attr;
1876 int alg = CKSUM_NONE;
1878 for (attr = list->first; attr != NULL; attr = attr->next) {
1879 if (strcmp(attr->name, "style") == 0) {
1880 const char *v = attr->value;
1881 if ((v[0] == 'S' || v[0] == 's') &&
1882 (v[1] == 'H' || v[1] == 'h') &&
1883 (v[2] == 'A' || v[2] == 'a') &&
1884 v[3] == '1' && v[4] == '\0')
1886 if ((v[0] == 'M' || v[0] == 'm') &&
1887 (v[1] == 'D' || v[1] == 'd') &&
1888 v[2] == '5' && v[3] == '\0')
1896 unknowntag_start(struct archive_read *a, struct xar *xar, const char *name)
1898 struct unknown_tag *tag;
1900 tag = malloc(sizeof(*tag));
1902 archive_set_error(&a->archive, ENOMEM, "Out of memory");
1903 return (ARCHIVE_FATAL);
1905 tag->next = xar->unknowntags;
1906 archive_string_init(&(tag->name));
1907 archive_strcpy(&(tag->name), name);
1908 if (xar->unknowntags == NULL) {
1910 fprintf(stderr, "UNKNOWNTAG_START:%s\n", name);
1912 xar->xmlsts_unknown = xar->xmlsts;
1913 xar->xmlsts = UNKNOWN;
1915 xar->unknowntags = tag;
1916 return (ARCHIVE_OK);
1920 unknowntag_end(struct xar *xar, const char *name)
1922 struct unknown_tag *tag;
1924 tag = xar->unknowntags;
1925 if (tag == NULL || name == NULL)
1927 if (strcmp(tag->name.s, name) == 0) {
1928 xar->unknowntags = tag->next;
1929 archive_string_free(&(tag->name));
1931 if (xar->unknowntags == NULL) {
1933 fprintf(stderr, "UNKNOWNTAG_END:%s\n", name);
1935 xar->xmlsts = xar->xmlsts_unknown;
1941 xml_start(struct archive_read *a, const char *name, struct xmlattr_list *list)
1944 struct xmlattr *attr;
1946 xar = (struct xar *)(a->format->data);
1949 fprintf(stderr, "xml_sta:[%s]\n", name);
1950 for (attr = list->first; attr != NULL; attr = attr->next)
1951 fprintf(stderr, " attr:\"%s\"=\"%s\"\n",
1952 attr->name, attr->value);
1954 xar->base64text = 0;
1955 switch (xar->xmlsts) {
1957 if (strcmp(name, "xar") == 0)
1960 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
1961 return (ARCHIVE_FATAL);
1964 if (strcmp(name, "toc") == 0)
1967 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
1968 return (ARCHIVE_FATAL);
1971 if (strcmp(name, "creation-time") == 0)
1972 xar->xmlsts = TOC_CREATION_TIME;
1973 else if (strcmp(name, "checksum") == 0)
1974 xar->xmlsts = TOC_CHECKSUM;
1975 else if (strcmp(name, "file") == 0) {
1976 if (file_new(a, xar, list) != ARCHIVE_OK)
1977 return (ARCHIVE_FATAL);
1978 xar->xmlsts = TOC_FILE;
1981 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
1982 return (ARCHIVE_FATAL);
1985 if (strcmp(name, "offset") == 0)
1986 xar->xmlsts = TOC_CHECKSUM_OFFSET;
1987 else if (strcmp(name, "size") == 0)
1988 xar->xmlsts = TOC_CHECKSUM_SIZE;
1990 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
1991 return (ARCHIVE_FATAL);
1994 if (strcmp(name, "file") == 0) {
1995 if (file_new(a, xar, list) != ARCHIVE_OK)
1996 return (ARCHIVE_FATAL);
1998 else if (strcmp(name, "data") == 0)
1999 xar->xmlsts = FILE_DATA;
2000 else if (strcmp(name, "ea") == 0) {
2001 if (xattr_new(a, xar, list) != ARCHIVE_OK)
2002 return (ARCHIVE_FATAL);
2003 xar->xmlsts = FILE_EA;
2005 else if (strcmp(name, "ctime") == 0)
2006 xar->xmlsts = FILE_CTIME;
2007 else if (strcmp(name, "mtime") == 0)
2008 xar->xmlsts = FILE_MTIME;
2009 else if (strcmp(name, "atime") == 0)
2010 xar->xmlsts = FILE_ATIME;
2011 else if (strcmp(name, "group") == 0)
2012 xar->xmlsts = FILE_GROUP;
2013 else if (strcmp(name, "gid") == 0)
2014 xar->xmlsts = FILE_GID;
2015 else if (strcmp(name, "user") == 0)
2016 xar->xmlsts = FILE_USER;
2017 else if (strcmp(name, "uid") == 0)
2018 xar->xmlsts = FILE_UID;
2019 else if (strcmp(name, "mode") == 0)
2020 xar->xmlsts = FILE_MODE;
2021 else if (strcmp(name, "device") == 0)
2022 xar->xmlsts = FILE_DEVICE;
2023 else if (strcmp(name, "deviceno") == 0)
2024 xar->xmlsts = FILE_DEVICENO;
2025 else if (strcmp(name, "inode") == 0)
2026 xar->xmlsts = FILE_INODE;
2027 else if (strcmp(name, "link") == 0)
2028 xar->xmlsts = FILE_LINK;
2029 else if (strcmp(name, "type") == 0) {
2030 xar->xmlsts = FILE_TYPE;
2031 for (attr = list->first; attr != NULL;
2032 attr = attr->next) {
2033 if (strcmp(attr->name, "link") != 0)
2035 if (strcmp(attr->value, "original") == 0) {
2036 xar->file->hdnext = xar->hdlink_orgs;
2037 xar->hdlink_orgs = xar->file;
2039 xar->file->link = (unsigned)atol10(attr->value,
2040 strlen(attr->value));
2041 if (xar->file->link > 0)
2042 if (add_link(a, xar, xar->file) != ARCHIVE_OK) {
2043 return (ARCHIVE_FATAL);
2048 else if (strcmp(name, "name") == 0) {
2049 xar->xmlsts = FILE_NAME;
2050 for (attr = list->first; attr != NULL;
2051 attr = attr->next) {
2052 if (strcmp(attr->name, "enctype") == 0 &&
2053 strcmp(attr->value, "base64") == 0)
2054 xar->base64text = 1;
2057 else if (strcmp(name, "acl") == 0)
2058 xar->xmlsts = FILE_ACL;
2059 else if (strcmp(name, "flags") == 0)
2060 xar->xmlsts = FILE_FLAGS;
2061 else if (strcmp(name, "ext2") == 0)
2062 xar->xmlsts = FILE_EXT2;
2064 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2065 return (ARCHIVE_FATAL);
2068 if (strcmp(name, "length") == 0)
2069 xar->xmlsts = FILE_DATA_LENGTH;
2070 else if (strcmp(name, "offset") == 0)
2071 xar->xmlsts = FILE_DATA_OFFSET;
2072 else if (strcmp(name, "size") == 0)
2073 xar->xmlsts = FILE_DATA_SIZE;
2074 else if (strcmp(name, "encoding") == 0) {
2075 xar->xmlsts = FILE_DATA_ENCODING;
2076 xar->file->encoding = getencoding(list);
2078 else if (strcmp(name, "archived-checksum") == 0) {
2079 xar->xmlsts = FILE_DATA_A_CHECKSUM;
2080 xar->file->a_sum.alg = getsumalgorithm(list);
2082 else if (strcmp(name, "extracted-checksum") == 0) {
2083 xar->xmlsts = FILE_DATA_E_CHECKSUM;
2084 xar->file->e_sum.alg = getsumalgorithm(list);
2086 else if (strcmp(name, "content") == 0)
2087 xar->xmlsts = FILE_DATA_CONTENT;
2089 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2090 return (ARCHIVE_FATAL);
2093 if (strcmp(name, "major") == 0)
2094 xar->xmlsts = FILE_DEVICE_MAJOR;
2095 else if (strcmp(name, "minor") == 0)
2096 xar->xmlsts = FILE_DEVICE_MINOR;
2098 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2099 return (ARCHIVE_FATAL);
2101 case FILE_DATA_CONTENT:
2102 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2103 return (ARCHIVE_FATAL);
2106 if (strcmp(name, "length") == 0)
2107 xar->xmlsts = FILE_EA_LENGTH;
2108 else if (strcmp(name, "offset") == 0)
2109 xar->xmlsts = FILE_EA_OFFSET;
2110 else if (strcmp(name, "size") == 0)
2111 xar->xmlsts = FILE_EA_SIZE;
2112 else if (strcmp(name, "encoding") == 0) {
2113 xar->xmlsts = FILE_EA_ENCODING;
2114 xar->xattr->encoding = getencoding(list);
2115 } else if (strcmp(name, "archived-checksum") == 0)
2116 xar->xmlsts = FILE_EA_A_CHECKSUM;
2117 else if (strcmp(name, "extracted-checksum") == 0)
2118 xar->xmlsts = FILE_EA_E_CHECKSUM;
2119 else if (strcmp(name, "name") == 0)
2120 xar->xmlsts = FILE_EA_NAME;
2121 else if (strcmp(name, "fstype") == 0)
2122 xar->xmlsts = FILE_EA_FSTYPE;
2124 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2125 return (ARCHIVE_FATAL);
2128 if (strcmp(name, "appleextended") == 0)
2129 xar->xmlsts = FILE_ACL_APPLEEXTENDED;
2130 else if (strcmp(name, "default") == 0)
2131 xar->xmlsts = FILE_ACL_DEFAULT;
2132 else if (strcmp(name, "access") == 0)
2133 xar->xmlsts = FILE_ACL_ACCESS;
2135 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2136 return (ARCHIVE_FATAL);
2139 if (!xml_parse_file_flags(xar, name))
2140 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2141 return (ARCHIVE_FATAL);
2144 if (!xml_parse_file_ext2(xar, name))
2145 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2146 return (ARCHIVE_FATAL);
2148 case TOC_CREATION_TIME:
2149 case TOC_CHECKSUM_OFFSET:
2150 case TOC_CHECKSUM_SIZE:
2151 case FILE_DATA_LENGTH:
2152 case FILE_DATA_OFFSET:
2153 case FILE_DATA_SIZE:
2154 case FILE_DATA_ENCODING:
2155 case FILE_DATA_A_CHECKSUM:
2156 case FILE_DATA_E_CHECKSUM:
2157 case FILE_EA_LENGTH:
2158 case FILE_EA_OFFSET:
2160 case FILE_EA_ENCODING:
2161 case FILE_EA_A_CHECKSUM:
2162 case FILE_EA_E_CHECKSUM:
2164 case FILE_EA_FSTYPE:
2173 case FILE_DEVICE_MAJOR:
2174 case FILE_DEVICE_MINOR:
2180 case FILE_ACL_DEFAULT:
2181 case FILE_ACL_ACCESS:
2182 case FILE_ACL_APPLEEXTENDED:
2183 case FILE_FLAGS_USER_NODUMP:
2184 case FILE_FLAGS_USER_IMMUTABLE:
2185 case FILE_FLAGS_USER_APPEND:
2186 case FILE_FLAGS_USER_OPAQUE:
2187 case FILE_FLAGS_USER_NOUNLINK:
2188 case FILE_FLAGS_SYS_ARCHIVED:
2189 case FILE_FLAGS_SYS_IMMUTABLE:
2190 case FILE_FLAGS_SYS_APPEND:
2191 case FILE_FLAGS_SYS_NOUNLINK:
2192 case FILE_FLAGS_SYS_SNAPSHOT:
2193 case FILE_EXT2_SecureDeletion:
2194 case FILE_EXT2_Undelete:
2195 case FILE_EXT2_Compress:
2196 case FILE_EXT2_Synchronous:
2197 case FILE_EXT2_Immutable:
2198 case FILE_EXT2_AppendOnly:
2199 case FILE_EXT2_NoDump:
2200 case FILE_EXT2_NoAtime:
2201 case FILE_EXT2_CompDirty:
2202 case FILE_EXT2_CompBlock:
2203 case FILE_EXT2_NoCompBlock:
2204 case FILE_EXT2_CompError:
2205 case FILE_EXT2_BTree:
2206 case FILE_EXT2_HashIndexed:
2207 case FILE_EXT2_iMagic:
2208 case FILE_EXT2_Journaled:
2209 case FILE_EXT2_NoTail:
2210 case FILE_EXT2_DirSync:
2211 case FILE_EXT2_TopDir:
2212 case FILE_EXT2_Reserved:
2214 if (unknowntag_start(a, xar, name) != ARCHIVE_OK)
2215 return (ARCHIVE_FATAL);
2218 return (ARCHIVE_OK);
2222 xml_end(void *userData, const char *name)
2224 struct archive_read *a;
2227 a = (struct archive_read *)userData;
2228 xar = (struct xar *)(a->format->data);
2231 fprintf(stderr, "xml_end:[%s]\n", name);
2233 switch (xar->xmlsts) {
2237 if (strcmp(name, "xar") == 0)
2241 if (strcmp(name, "toc") == 0)
2244 case TOC_CREATION_TIME:
2245 if (strcmp(name, "creation-time") == 0)
2249 if (strcmp(name, "checksum") == 0)
2252 case TOC_CHECKSUM_OFFSET:
2253 if (strcmp(name, "offset") == 0)
2254 xar->xmlsts = TOC_CHECKSUM;
2256 case TOC_CHECKSUM_SIZE:
2257 if (strcmp(name, "size") == 0)
2258 xar->xmlsts = TOC_CHECKSUM;
2261 if (strcmp(name, "file") == 0) {
2262 if (xar->file->parent != NULL &&
2263 ((xar->file->mode & AE_IFMT) == AE_IFDIR))
2264 xar->file->parent->subdirs++;
2265 xar->file = xar->file->parent;
2266 if (xar->file == NULL)
2271 if (strcmp(name, "data") == 0)
2272 xar->xmlsts = TOC_FILE;
2274 case FILE_DATA_LENGTH:
2275 if (strcmp(name, "length") == 0)
2276 xar->xmlsts = FILE_DATA;
2278 case FILE_DATA_OFFSET:
2279 if (strcmp(name, "offset") == 0)
2280 xar->xmlsts = FILE_DATA;
2282 case FILE_DATA_SIZE:
2283 if (strcmp(name, "size") == 0)
2284 xar->xmlsts = FILE_DATA;
2286 case FILE_DATA_ENCODING:
2287 if (strcmp(name, "encoding") == 0)
2288 xar->xmlsts = FILE_DATA;
2290 case FILE_DATA_A_CHECKSUM:
2291 if (strcmp(name, "archived-checksum") == 0)
2292 xar->xmlsts = FILE_DATA;
2294 case FILE_DATA_E_CHECKSUM:
2295 if (strcmp(name, "extracted-checksum") == 0)
2296 xar->xmlsts = FILE_DATA;
2298 case FILE_DATA_CONTENT:
2299 if (strcmp(name, "content") == 0)
2300 xar->xmlsts = FILE_DATA;
2303 if (strcmp(name, "ea") == 0) {
2304 xar->xmlsts = TOC_FILE;
2308 case FILE_EA_LENGTH:
2309 if (strcmp(name, "length") == 0)
2310 xar->xmlsts = FILE_EA;
2312 case FILE_EA_OFFSET:
2313 if (strcmp(name, "offset") == 0)
2314 xar->xmlsts = FILE_EA;
2317 if (strcmp(name, "size") == 0)
2318 xar->xmlsts = FILE_EA;
2320 case FILE_EA_ENCODING:
2321 if (strcmp(name, "encoding") == 0)
2322 xar->xmlsts = FILE_EA;
2324 case FILE_EA_A_CHECKSUM:
2325 if (strcmp(name, "archived-checksum") == 0)
2326 xar->xmlsts = FILE_EA;
2328 case FILE_EA_E_CHECKSUM:
2329 if (strcmp(name, "extracted-checksum") == 0)
2330 xar->xmlsts = FILE_EA;
2333 if (strcmp(name, "name") == 0)
2334 xar->xmlsts = FILE_EA;
2336 case FILE_EA_FSTYPE:
2337 if (strcmp(name, "fstype") == 0)
2338 xar->xmlsts = FILE_EA;
2341 if (strcmp(name, "ctime") == 0)
2342 xar->xmlsts = TOC_FILE;
2345 if (strcmp(name, "mtime") == 0)
2346 xar->xmlsts = TOC_FILE;
2349 if (strcmp(name, "atime") == 0)
2350 xar->xmlsts = TOC_FILE;
2353 if (strcmp(name, "group") == 0)
2354 xar->xmlsts = TOC_FILE;
2357 if (strcmp(name, "gid") == 0)
2358 xar->xmlsts = TOC_FILE;
2361 if (strcmp(name, "user") == 0)
2362 xar->xmlsts = TOC_FILE;
2365 if (strcmp(name, "uid") == 0)
2366 xar->xmlsts = TOC_FILE;
2369 if (strcmp(name, "mode") == 0)
2370 xar->xmlsts = TOC_FILE;
2373 if (strcmp(name, "device") == 0)
2374 xar->xmlsts = TOC_FILE;
2376 case FILE_DEVICE_MAJOR:
2377 if (strcmp(name, "major") == 0)
2378 xar->xmlsts = FILE_DEVICE;
2380 case FILE_DEVICE_MINOR:
2381 if (strcmp(name, "minor") == 0)
2382 xar->xmlsts = FILE_DEVICE;
2385 if (strcmp(name, "deviceno") == 0)
2386 xar->xmlsts = TOC_FILE;
2389 if (strcmp(name, "inode") == 0)
2390 xar->xmlsts = TOC_FILE;
2393 if (strcmp(name, "link") == 0)
2394 xar->xmlsts = TOC_FILE;
2397 if (strcmp(name, "type") == 0)
2398 xar->xmlsts = TOC_FILE;
2401 if (strcmp(name, "name") == 0)
2402 xar->xmlsts = TOC_FILE;
2405 if (strcmp(name, "acl") == 0)
2406 xar->xmlsts = TOC_FILE;
2408 case FILE_ACL_DEFAULT:
2409 if (strcmp(name, "default") == 0)
2410 xar->xmlsts = FILE_ACL;
2412 case FILE_ACL_ACCESS:
2413 if (strcmp(name, "access") == 0)
2414 xar->xmlsts = FILE_ACL;
2416 case FILE_ACL_APPLEEXTENDED:
2417 if (strcmp(name, "appleextended") == 0)
2418 xar->xmlsts = FILE_ACL;
2421 if (strcmp(name, "flags") == 0)
2422 xar->xmlsts = TOC_FILE;
2424 case FILE_FLAGS_USER_NODUMP:
2425 if (strcmp(name, "UserNoDump") == 0)
2426 xar->xmlsts = FILE_FLAGS;
2428 case FILE_FLAGS_USER_IMMUTABLE:
2429 if (strcmp(name, "UserImmutable") == 0)
2430 xar->xmlsts = FILE_FLAGS;
2432 case FILE_FLAGS_USER_APPEND:
2433 if (strcmp(name, "UserAppend") == 0)
2434 xar->xmlsts = FILE_FLAGS;
2436 case FILE_FLAGS_USER_OPAQUE:
2437 if (strcmp(name, "UserOpaque") == 0)
2438 xar->xmlsts = FILE_FLAGS;
2440 case FILE_FLAGS_USER_NOUNLINK:
2441 if (strcmp(name, "UserNoUnlink") == 0)
2442 xar->xmlsts = FILE_FLAGS;
2444 case FILE_FLAGS_SYS_ARCHIVED:
2445 if (strcmp(name, "SystemArchived") == 0)
2446 xar->xmlsts = FILE_FLAGS;
2448 case FILE_FLAGS_SYS_IMMUTABLE:
2449 if (strcmp(name, "SystemImmutable") == 0)
2450 xar->xmlsts = FILE_FLAGS;
2452 case FILE_FLAGS_SYS_APPEND:
2453 if (strcmp(name, "SystemAppend") == 0)
2454 xar->xmlsts = FILE_FLAGS;
2456 case FILE_FLAGS_SYS_NOUNLINK:
2457 if (strcmp(name, "SystemNoUnlink") == 0)
2458 xar->xmlsts = FILE_FLAGS;
2460 case FILE_FLAGS_SYS_SNAPSHOT:
2461 if (strcmp(name, "SystemSnapshot") == 0)
2462 xar->xmlsts = FILE_FLAGS;
2465 if (strcmp(name, "ext2") == 0)
2466 xar->xmlsts = TOC_FILE;
2468 case FILE_EXT2_SecureDeletion:
2469 if (strcmp(name, "SecureDeletion") == 0)
2470 xar->xmlsts = FILE_EXT2;
2472 case FILE_EXT2_Undelete:
2473 if (strcmp(name, "Undelete") == 0)
2474 xar->xmlsts = FILE_EXT2;
2476 case FILE_EXT2_Compress:
2477 if (strcmp(name, "Compress") == 0)
2478 xar->xmlsts = FILE_EXT2;
2480 case FILE_EXT2_Synchronous:
2481 if (strcmp(name, "Synchronous") == 0)
2482 xar->xmlsts = FILE_EXT2;
2484 case FILE_EXT2_Immutable:
2485 if (strcmp(name, "Immutable") == 0)
2486 xar->xmlsts = FILE_EXT2;
2488 case FILE_EXT2_AppendOnly:
2489 if (strcmp(name, "AppendOnly") == 0)
2490 xar->xmlsts = FILE_EXT2;
2492 case FILE_EXT2_NoDump:
2493 if (strcmp(name, "NoDump") == 0)
2494 xar->xmlsts = FILE_EXT2;
2496 case FILE_EXT2_NoAtime:
2497 if (strcmp(name, "NoAtime") == 0)
2498 xar->xmlsts = FILE_EXT2;
2500 case FILE_EXT2_CompDirty:
2501 if (strcmp(name, "CompDirty") == 0)
2502 xar->xmlsts = FILE_EXT2;
2504 case FILE_EXT2_CompBlock:
2505 if (strcmp(name, "CompBlock") == 0)
2506 xar->xmlsts = FILE_EXT2;
2508 case FILE_EXT2_NoCompBlock:
2509 if (strcmp(name, "NoCompBlock") == 0)
2510 xar->xmlsts = FILE_EXT2;
2512 case FILE_EXT2_CompError:
2513 if (strcmp(name, "CompError") == 0)
2514 xar->xmlsts = FILE_EXT2;
2516 case FILE_EXT2_BTree:
2517 if (strcmp(name, "BTree") == 0)
2518 xar->xmlsts = FILE_EXT2;
2520 case FILE_EXT2_HashIndexed:
2521 if (strcmp(name, "HashIndexed") == 0)
2522 xar->xmlsts = FILE_EXT2;
2524 case FILE_EXT2_iMagic:
2525 if (strcmp(name, "iMagic") == 0)
2526 xar->xmlsts = FILE_EXT2;
2528 case FILE_EXT2_Journaled:
2529 if (strcmp(name, "Journaled") == 0)
2530 xar->xmlsts = FILE_EXT2;
2532 case FILE_EXT2_NoTail:
2533 if (strcmp(name, "NoTail") == 0)
2534 xar->xmlsts = FILE_EXT2;
2536 case FILE_EXT2_DirSync:
2537 if (strcmp(name, "DirSync") == 0)
2538 xar->xmlsts = FILE_EXT2;
2540 case FILE_EXT2_TopDir:
2541 if (strcmp(name, "TopDir") == 0)
2542 xar->xmlsts = FILE_EXT2;
2544 case FILE_EXT2_Reserved:
2545 if (strcmp(name, "Reserved") == 0)
2546 xar->xmlsts = FILE_EXT2;
2549 unknowntag_end(xar, name);
2554 static const int base64[256] = {
2555 -1, -1, -1, -1, -1, -1, -1, -1,
2556 -1, -1, -1, -1, -1, -1, -1, -1, /* 00 - 0F */
2557 -1, -1, -1, -1, -1, -1, -1, -1,
2558 -1, -1, -1, -1, -1, -1, -1, -1, /* 10 - 1F */
2559 -1, -1, -1, -1, -1, -1, -1, -1,
2560 -1, -1, -1, 62, -1, -1, -1, 63, /* 20 - 2F */
2561 52, 53, 54, 55, 56, 57, 58, 59,
2562 60, 61, -1, -1, -1, -1, -1, -1, /* 30 - 3F */
2563 -1, 0, 1, 2, 3, 4, 5, 6,
2564 7, 8, 9, 10, 11, 12, 13, 14, /* 40 - 4F */
2565 15, 16, 17, 18, 19, 20, 21, 22,
2566 23, 24, 25, -1, -1, -1, -1, -1, /* 50 - 5F */
2567 -1, 26, 27, 28, 29, 30, 31, 32,
2568 33, 34, 35, 36, 37, 38, 39, 40, /* 60 - 6F */
2569 41, 42, 43, 44, 45, 46, 47, 48,
2570 49, 50, 51, -1, -1, -1, -1, -1, /* 70 - 7F */
2571 -1, -1, -1, -1, -1, -1, -1, -1,
2572 -1, -1, -1, -1, -1, -1, -1, -1, /* 80 - 8F */
2573 -1, -1, -1, -1, -1, -1, -1, -1,
2574 -1, -1, -1, -1, -1, -1, -1, -1, /* 90 - 9F */
2575 -1, -1, -1, -1, -1, -1, -1, -1,
2576 -1, -1, -1, -1, -1, -1, -1, -1, /* A0 - AF */
2577 -1, -1, -1, -1, -1, -1, -1, -1,
2578 -1, -1, -1, -1, -1, -1, -1, -1, /* B0 - BF */
2579 -1, -1, -1, -1, -1, -1, -1, -1,
2580 -1, -1, -1, -1, -1, -1, -1, -1, /* C0 - CF */
2581 -1, -1, -1, -1, -1, -1, -1, -1,
2582 -1, -1, -1, -1, -1, -1, -1, -1, /* D0 - DF */
2583 -1, -1, -1, -1, -1, -1, -1, -1,
2584 -1, -1, -1, -1, -1, -1, -1, -1, /* E0 - EF */
2585 -1, -1, -1, -1, -1, -1, -1, -1,
2586 -1, -1, -1, -1, -1, -1, -1, -1, /* F0 - FF */
2590 strappend_base64(struct xar *xar,
2591 struct archive_string *as, const char *s, size_t l)
2593 unsigned char buff[256];
2595 const unsigned char *b;
2598 (void)xar; /* UNUSED */
2601 b = (const unsigned char *)s;
2606 if (base64[b[0]] < 0 || base64[b[1]] < 0)
2608 n = base64[*b++] << 18;
2609 n |= base64[*b++] << 12;
2617 n |= base64[*b++] << 6;
2618 *out++ = (n >> 8) & 0xFF;
2630 if (len+3 >= sizeof(buff)) {
2631 archive_strncat(as, (const char *)buff, len);
2637 archive_strncat(as, (const char *)buff, len);
2641 is_string(const char *known, const char *data, size_t len)
2643 if (strlen(known) != len)
2645 return memcmp(data, known, len);
2649 xml_data(void *userData, const char *s, int len)
2651 struct archive_read *a;
2654 a = (struct archive_read *)userData;
2655 xar = (struct xar *)(a->format->data);
2660 if (len > (int)(sizeof(buff)-1))
2661 len = (int)(sizeof(buff)-1);
2662 strncpy(buff, s, len);
2664 fprintf(stderr, "\tlen=%d:\"%s\"\n", len, buff);
2667 switch (xar->xmlsts) {
2668 case TOC_CHECKSUM_OFFSET:
2669 xar->toc_chksum_offset = atol10(s, len);
2671 case TOC_CHECKSUM_SIZE:
2672 xar->toc_chksum_size = atol10(s, len);
2677 if (xar->file == NULL)
2680 switch (xar->xmlsts) {
2682 if (xar->file->parent != NULL) {
2683 archive_string_concat(&(xar->file->pathname),
2684 &(xar->file->parent->pathname));
2685 archive_strappend_char(&(xar->file->pathname), '/');
2687 xar->file->has |= HAS_PATHNAME;
2688 if (xar->base64text) {
2689 strappend_base64(xar,
2690 &(xar->file->pathname), s, len);
2692 archive_strncat(&(xar->file->pathname), s, len);
2695 xar->file->has |= HAS_SYMLINK;
2696 archive_strncpy(&(xar->file->symlink), s, len);
2699 if (is_string("file", s, len) == 0 ||
2700 is_string("hardlink", s, len) == 0)
2702 (xar->file->mode & ~AE_IFMT) | AE_IFREG;
2703 if (is_string("directory", s, len) == 0)
2705 (xar->file->mode & ~AE_IFMT) | AE_IFDIR;
2706 if (is_string("symlink", s, len) == 0)
2708 (xar->file->mode & ~AE_IFMT) | AE_IFLNK;
2709 if (is_string("character special", s, len) == 0)
2711 (xar->file->mode & ~AE_IFMT) | AE_IFCHR;
2712 if (is_string("block special", s, len) == 0)
2714 (xar->file->mode & ~AE_IFMT) | AE_IFBLK;
2715 if (is_string("socket", s, len) == 0)
2717 (xar->file->mode & ~AE_IFMT) | AE_IFSOCK;
2718 if (is_string("fifo", s, len) == 0)
2720 (xar->file->mode & ~AE_IFMT) | AE_IFIFO;
2721 xar->file->has |= HAS_TYPE;
2724 xar->file->has |= HAS_INO;
2725 xar->file->ino64 = atol10(s, len);
2727 case FILE_DEVICE_MAJOR:
2728 xar->file->has |= HAS_DEVMAJOR;
2729 xar->file->devmajor = (dev_t)atol10(s, len);
2731 case FILE_DEVICE_MINOR:
2732 xar->file->has |= HAS_DEVMINOR;
2733 xar->file->devminor = (dev_t)atol10(s, len);
2736 xar->file->has |= HAS_DEV;
2737 xar->file->dev = (dev_t)atol10(s, len);
2740 xar->file->has |= HAS_MODE;
2742 (xar->file->mode & AE_IFMT) |
2743 ((mode_t)(atol8(s, len)) & ~AE_IFMT);
2746 xar->file->has |= HAS_GID;
2747 archive_strncpy(&(xar->file->gname), s, len);
2750 xar->file->has |= HAS_GID;
2751 xar->file->gid = atol10(s, len);
2754 xar->file->has |= HAS_UID;
2755 archive_strncpy(&(xar->file->uname), s, len);
2758 xar->file->has |= HAS_UID;
2759 xar->file->uid = atol10(s, len);
2762 xar->file->has |= HAS_TIME | HAS_CTIME;
2763 xar->file->ctime = parse_time(s, len);
2766 xar->file->has |= HAS_TIME | HAS_MTIME;
2767 xar->file->mtime = parse_time(s, len);
2770 xar->file->has |= HAS_TIME | HAS_ATIME;
2771 xar->file->atime = parse_time(s, len);
2773 case FILE_DATA_LENGTH:
2774 xar->file->has |= HAS_DATA;
2775 xar->file->length = atol10(s, len);
2777 case FILE_DATA_OFFSET:
2778 xar->file->has |= HAS_DATA;
2779 xar->file->offset = atol10(s, len);
2781 case FILE_DATA_SIZE:
2782 xar->file->has |= HAS_DATA;
2783 xar->file->size = atol10(s, len);
2785 case FILE_DATA_A_CHECKSUM:
2786 xar->file->a_sum.len = atohex(xar->file->a_sum.val,
2787 sizeof(xar->file->a_sum.val), s, len);
2789 case FILE_DATA_E_CHECKSUM:
2790 xar->file->e_sum.len = atohex(xar->file->e_sum.val,
2791 sizeof(xar->file->e_sum.val), s, len);
2793 case FILE_EA_LENGTH:
2794 xar->file->has |= HAS_XATTR;
2795 xar->xattr->length = atol10(s, len);
2797 case FILE_EA_OFFSET:
2798 xar->file->has |= HAS_XATTR;
2799 xar->xattr->offset = atol10(s, len);
2802 xar->file->has |= HAS_XATTR;
2803 xar->xattr->size = atol10(s, len);
2805 case FILE_EA_A_CHECKSUM:
2806 xar->file->has |= HAS_XATTR;
2807 xar->xattr->a_sum.len = atohex(xar->xattr->a_sum.val,
2808 sizeof(xar->xattr->a_sum.val), s, len);
2810 case FILE_EA_E_CHECKSUM:
2811 xar->file->has |= HAS_XATTR;
2812 xar->xattr->e_sum.len = atohex(xar->xattr->e_sum.val,
2813 sizeof(xar->xattr->e_sum.val), s, len);
2816 xar->file->has |= HAS_XATTR;
2817 archive_strncpy(&(xar->xattr->name), s, len);
2819 case FILE_EA_FSTYPE:
2820 xar->file->has |= HAS_XATTR;
2821 archive_strncpy(&(xar->xattr->fstype), s, len);
2824 case FILE_ACL_DEFAULT:
2825 case FILE_ACL_ACCESS:
2826 case FILE_ACL_APPLEEXTENDED:
2827 xar->file->has |= HAS_ACL;
2833 case TOC_CREATION_TIME:
2835 case TOC_CHECKSUM_OFFSET:
2836 case TOC_CHECKSUM_SIZE:
2839 case FILE_DATA_ENCODING:
2840 case FILE_DATA_CONTENT:
2843 case FILE_EA_ENCODING:
2846 case FILE_FLAGS_USER_NODUMP:
2847 case FILE_FLAGS_USER_IMMUTABLE:
2848 case FILE_FLAGS_USER_APPEND:
2849 case FILE_FLAGS_USER_OPAQUE:
2850 case FILE_FLAGS_USER_NOUNLINK:
2851 case FILE_FLAGS_SYS_ARCHIVED:
2852 case FILE_FLAGS_SYS_IMMUTABLE:
2853 case FILE_FLAGS_SYS_APPEND:
2854 case FILE_FLAGS_SYS_NOUNLINK:
2855 case FILE_FLAGS_SYS_SNAPSHOT:
2857 case FILE_EXT2_SecureDeletion:
2858 case FILE_EXT2_Undelete:
2859 case FILE_EXT2_Compress:
2860 case FILE_EXT2_Synchronous:
2861 case FILE_EXT2_Immutable:
2862 case FILE_EXT2_AppendOnly:
2863 case FILE_EXT2_NoDump:
2864 case FILE_EXT2_NoAtime:
2865 case FILE_EXT2_CompDirty:
2866 case FILE_EXT2_CompBlock:
2867 case FILE_EXT2_NoCompBlock:
2868 case FILE_EXT2_CompError:
2869 case FILE_EXT2_BTree:
2870 case FILE_EXT2_HashIndexed:
2871 case FILE_EXT2_iMagic:
2872 case FILE_EXT2_Journaled:
2873 case FILE_EXT2_NoTail:
2874 case FILE_EXT2_DirSync:
2875 case FILE_EXT2_TopDir:
2876 case FILE_EXT2_Reserved:
2886 xml_parse_file_flags(struct xar *xar, const char *name)
2888 const char *flag = NULL;
2890 if (strcmp(name, "UserNoDump") == 0) {
2891 xar->xmlsts = FILE_FLAGS_USER_NODUMP;
2894 else if (strcmp(name, "UserImmutable") == 0) {
2895 xar->xmlsts = FILE_FLAGS_USER_IMMUTABLE;
2896 flag = "uimmutable";
2898 else if (strcmp(name, "UserAppend") == 0) {
2899 xar->xmlsts = FILE_FLAGS_USER_APPEND;
2902 else if (strcmp(name, "UserOpaque") == 0) {
2903 xar->xmlsts = FILE_FLAGS_USER_OPAQUE;
2906 else if (strcmp(name, "UserNoUnlink") == 0) {
2907 xar->xmlsts = FILE_FLAGS_USER_NOUNLINK;
2910 else if (strcmp(name, "SystemArchived") == 0) {
2911 xar->xmlsts = FILE_FLAGS_SYS_ARCHIVED;
2914 else if (strcmp(name, "SystemImmutable") == 0) {
2915 xar->xmlsts = FILE_FLAGS_SYS_IMMUTABLE;
2916 flag = "simmutable";
2918 else if (strcmp(name, "SystemAppend") == 0) {
2919 xar->xmlsts = FILE_FLAGS_SYS_APPEND;
2922 else if (strcmp(name, "SystemNoUnlink") == 0) {
2923 xar->xmlsts = FILE_FLAGS_SYS_NOUNLINK;
2926 else if (strcmp(name, "SystemSnapshot") == 0) {
2927 xar->xmlsts = FILE_FLAGS_SYS_SNAPSHOT;
2933 xar->file->has |= HAS_FFLAGS;
2934 if (archive_strlen(&(xar->file->fflags_text)) > 0)
2935 archive_strappend_char(&(xar->file->fflags_text), ',');
2936 archive_strcat(&(xar->file->fflags_text), flag);
2944 xml_parse_file_ext2(struct xar *xar, const char *name)
2946 const char *flag = NULL;
2948 if (strcmp(name, "SecureDeletion") == 0) {
2949 xar->xmlsts = FILE_EXT2_SecureDeletion;
2950 flag = "securedeletion";
2952 else if (strcmp(name, "Undelete") == 0) {
2953 xar->xmlsts = FILE_EXT2_Undelete;
2956 else if (strcmp(name, "Compress") == 0) {
2957 xar->xmlsts = FILE_EXT2_Compress;
2960 else if (strcmp(name, "Synchronous") == 0) {
2961 xar->xmlsts = FILE_EXT2_Synchronous;
2964 else if (strcmp(name, "Immutable") == 0) {
2965 xar->xmlsts = FILE_EXT2_Immutable;
2966 flag = "simmutable";
2968 else if (strcmp(name, "AppendOnly") == 0) {
2969 xar->xmlsts = FILE_EXT2_AppendOnly;
2972 else if (strcmp(name, "NoDump") == 0) {
2973 xar->xmlsts = FILE_EXT2_NoDump;
2976 else if (strcmp(name, "NoAtime") == 0) {
2977 xar->xmlsts = FILE_EXT2_NoAtime;
2980 else if (strcmp(name, "CompDirty") == 0) {
2981 xar->xmlsts = FILE_EXT2_CompDirty;
2984 else if (strcmp(name, "CompBlock") == 0) {
2985 xar->xmlsts = FILE_EXT2_CompBlock;
2988 else if (strcmp(name, "NoCompBlock") == 0) {
2989 xar->xmlsts = FILE_EXT2_NoCompBlock;
2990 flag = "nocomprblk";
2992 else if (strcmp(name, "CompError") == 0) {
2993 xar->xmlsts = FILE_EXT2_CompError;
2996 else if (strcmp(name, "BTree") == 0) {
2997 xar->xmlsts = FILE_EXT2_BTree;
3000 else if (strcmp(name, "HashIndexed") == 0) {
3001 xar->xmlsts = FILE_EXT2_HashIndexed;
3004 else if (strcmp(name, "iMagic") == 0) {
3005 xar->xmlsts = FILE_EXT2_iMagic;
3008 else if (strcmp(name, "Journaled") == 0) {
3009 xar->xmlsts = FILE_EXT2_Journaled;
3012 else if (strcmp(name, "NoTail") == 0) {
3013 xar->xmlsts = FILE_EXT2_NoTail;
3016 else if (strcmp(name, "DirSync") == 0) {
3017 xar->xmlsts = FILE_EXT2_DirSync;
3020 else if (strcmp(name, "TopDir") == 0) {
3021 xar->xmlsts = FILE_EXT2_TopDir;
3024 else if (strcmp(name, "Reserved") == 0) {
3025 xar->xmlsts = FILE_EXT2_Reserved;
3031 if (archive_strlen(&(xar->file->fflags_text)) > 0)
3032 archive_strappend_char(&(xar->file->fflags_text), ',');
3033 archive_strcat(&(xar->file->fflags_text), flag);
3037 #ifdef HAVE_LIBXML_XMLREADER_H
3040 xml2_xmlattr_setup(struct archive_read *a,
3041 struct xmlattr_list *list, xmlTextReaderPtr reader)
3043 struct xmlattr *attr;
3047 list->last = &(list->first);
3048 r = xmlTextReaderMoveToFirstAttribute(reader);
3050 attr = malloc(sizeof*(attr));
3052 archive_set_error(&a->archive, ENOMEM, "Out of memory");
3053 return (ARCHIVE_FATAL);
3055 attr->name = strdup(
3056 (const char *)xmlTextReaderConstLocalName(reader));
3057 if (attr->name == NULL) {
3059 archive_set_error(&a->archive, ENOMEM, "Out of memory");
3060 return (ARCHIVE_FATAL);
3062 attr->value = strdup(
3063 (const char *)xmlTextReaderConstValue(reader));
3064 if (attr->value == NULL) {
3067 archive_set_error(&a->archive, ENOMEM, "Out of memory");
3068 return (ARCHIVE_FATAL);
3072 list->last = &(attr->next);
3073 r = xmlTextReaderMoveToNextAttribute(reader);
3079 xml2_read_cb(void *context, char *buffer, int len)
3081 struct archive_read *a;
3088 a = (struct archive_read *)context;
3089 xar = (struct xar *)(a->format->data);
3091 if (xar->toc_remaining <= 0)
3095 r = rd_contents(a, &d, &outbytes, &used, xar->toc_remaining);
3096 if (r != ARCHIVE_OK)
3098 __archive_read_consume(a, used);
3099 xar->toc_remaining -= used;
3100 xar->offset += used;
3101 xar->toc_total += outbytes;
3102 PRINT_TOC(buffer, len);
3104 return ((int)outbytes);
3108 xml2_close_cb(void *context)
3111 (void)context; /* UNUSED */
3116 xml2_error_hdr(void *arg, const char *msg, xmlParserSeverities severity,
3117 xmlTextReaderLocatorPtr locator)
3119 struct archive_read *a;
3121 (void)locator; /* UNUSED */
3122 a = (struct archive_read *)arg;
3124 case XML_PARSER_SEVERITY_VALIDITY_WARNING:
3125 case XML_PARSER_SEVERITY_WARNING:
3126 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3127 "XML Parsing error: %s", msg);
3129 case XML_PARSER_SEVERITY_VALIDITY_ERROR:
3130 case XML_PARSER_SEVERITY_ERROR:
3131 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3132 "XML Parsing error: %s", msg);
3138 xml2_read_toc(struct archive_read *a)
3140 xmlTextReaderPtr reader;
3141 struct xmlattr_list list;
3144 reader = xmlReaderForIO(xml2_read_cb, xml2_close_cb, a, NULL, NULL, 0);
3145 if (reader == NULL) {
3146 archive_set_error(&a->archive, ENOMEM,
3147 "Couldn't allocate memory for xml parser");
3148 return (ARCHIVE_FATAL);
3150 xmlTextReaderSetErrorHandler(reader, xml2_error_hdr, a);
3152 while ((r = xmlTextReaderRead(reader)) == 1) {
3153 const char *name, *value;
3156 type = xmlTextReaderNodeType(reader);
3157 name = (const char *)xmlTextReaderConstLocalName(reader);
3159 case XML_READER_TYPE_ELEMENT:
3160 empty = xmlTextReaderIsEmptyElement(reader);
3161 r = xml2_xmlattr_setup(a, &list, reader);
3162 if (r == ARCHIVE_OK)
3163 r = xml_start(a, name, &list);
3164 xmlattr_cleanup(&list);
3165 if (r != ARCHIVE_OK)
3170 case XML_READER_TYPE_END_ELEMENT:
3173 case XML_READER_TYPE_TEXT:
3174 value = (const char *)xmlTextReaderConstValue(reader);
3175 xml_data(a, value, strlen(value));
3177 case XML_READER_TYPE_SIGNIFICANT_WHITESPACE:
3184 xmlFreeTextReader(reader);
3187 return ((r == 0)?ARCHIVE_OK:ARCHIVE_FATAL);
3190 #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
3193 expat_xmlattr_setup(struct archive_read *a,
3194 struct xmlattr_list *list, const XML_Char **atts)
3196 struct xmlattr *attr;
3200 list->last = &(list->first);
3202 return (ARCHIVE_OK);
3203 while (atts[0] != NULL && atts[1] != NULL) {
3204 attr = malloc(sizeof*(attr));
3205 name = strdup(atts[0]);
3206 value = strdup(atts[1]);
3207 if (attr == NULL || name == NULL || value == NULL) {
3208 archive_set_error(&a->archive, ENOMEM, "Out of memory");
3212 return (ARCHIVE_FATAL);
3215 attr->value = value;
3218 list->last = &(attr->next);
3221 return (ARCHIVE_OK);
3225 expat_start_cb(void *userData, const XML_Char *name, const XML_Char **atts)
3227 struct expat_userData *ud = (struct expat_userData *)userData;
3228 struct archive_read *a = ud->archive;
3229 struct xmlattr_list list;
3232 r = expat_xmlattr_setup(a, &list, atts);
3233 if (r == ARCHIVE_OK)
3234 r = xml_start(a, (const char *)name, &list);
3235 xmlattr_cleanup(&list);
3240 expat_end_cb(void *userData, const XML_Char *name)
3242 struct expat_userData *ud = (struct expat_userData *)userData;
3244 xml_end(ud->archive, (const char *)name);
3248 expat_data_cb(void *userData, const XML_Char *s, int len)
3250 struct expat_userData *ud = (struct expat_userData *)userData;
3252 xml_data(ud->archive, s, len);
3256 expat_read_toc(struct archive_read *a)
3260 struct expat_userData ud;
3262 ud.state = ARCHIVE_OK;
3265 xar = (struct xar *)(a->format->data);
3267 /* Initialize XML Parser library. */
3268 parser = XML_ParserCreate(NULL);
3269 if (parser == NULL) {
3270 archive_set_error(&a->archive, ENOMEM,
3271 "Couldn't allocate memory for xml parser");
3272 return (ARCHIVE_FATAL);
3274 XML_SetUserData(parser, &ud);
3275 XML_SetElementHandler(parser, expat_start_cb, expat_end_cb);
3276 XML_SetCharacterDataHandler(parser, expat_data_cb);
3279 while (xar->toc_remaining && ud.state == ARCHIVE_OK) {
3287 r = rd_contents(a, &d, &outbytes, &used, xar->toc_remaining);
3288 if (r != ARCHIVE_OK)
3290 xar->toc_remaining -= used;
3291 xar->offset += used;
3292 xar->toc_total += outbytes;
3293 PRINT_TOC(d, outbytes);
3295 xr = XML_Parse(parser, d, outbytes, xar->toc_remaining == 0);
3296 __archive_read_consume(a, used);
3297 if (xr == XML_STATUS_ERROR) {
3298 XML_ParserFree(parser);
3299 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3300 "XML Parsing failed");
3301 return (ARCHIVE_FATAL);
3304 XML_ParserFree(parser);
3307 #endif /* defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H) */
3309 #endif /* Support xar format */