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$");
35 #if HAVE_LIBXML_XMLREADER_H
36 #include <libxml/xmlreader.h>
55 #include "archive_endian.h"
56 #include "archive_entry.h"
57 #include "archive_hash.h"
58 #include "archive_private.h"
59 #include "archive_read_private.h"
61 #if (!defined(HAVE_LIBXML_XMLREADER_H) && \
62 !defined(HAVE_BSDXML_H) && !defined(HAVE_EXPAT_H)) ||\
63 !defined(HAVE_ZLIB_H) || \
64 !defined(ARCHIVE_HAS_MD5) || !defined(ARCHIVE_HAS_SHA1)
66 * xar needs several external libraries.
67 * o libxml2 or expat --- XML parser
68 * o openssl or MD5/SHA1 hash function
74 archive_read_support_format_xar(struct archive *_a)
76 struct archive_read *a = (struct archive_read *)_a;
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 unsigned char buff[1024*32];
308 enum xmlstatus xmlsts;
309 enum xmlstatus xmlsts_unknown;
310 struct unknown_tag *unknowntags;
316 uint64_t toc_remaining;
318 uint64_t toc_chksum_offset;
319 uint64_t toc_chksum_size;
324 enum enctype rd_encoding;
327 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
331 #if HAVE_LZMA_H && HAVE_LIBLZMA
332 lzma_stream lzstream;
334 #elif HAVE_LZMADEC_H && HAVE_LIBLZMADEC
335 lzmadec_stream lzstream;
341 struct chksumwork a_sumwrk;
342 struct chksumwork e_sumwrk;
344 struct xar_file *file; /* current reading file. */
345 struct xattr *xattr; /* current reading extended attribute. */
346 struct heap_queue file_queue;
347 struct xar_file *hdlink_orgs;
348 struct hdlink *hdlink_list;
351 uint64_t entry_total;
352 uint64_t entry_remaining;
354 enum enctype entry_encoding;
355 struct chksumval entry_a_sum;
356 struct chksumval entry_e_sum;
360 struct xmlattr *next;
365 struct xmlattr_list {
366 struct xmlattr *first;
367 struct xmlattr **last;
370 static int xar_bid(struct archive_read *);
371 static int xar_read_header(struct archive_read *,
372 struct archive_entry *);
373 static int xar_read_data(struct archive_read *,
374 const void **, size_t *, off_t *);
375 static int xar_read_data_skip(struct archive_read *);
376 static int xar_cleanup(struct archive_read *);
377 static int move_reading_point(struct archive_read *, uint64_t);
378 static int rd_contents_init(struct archive_read *,
379 enum enctype, int, int);
380 static int rd_contents(struct archive_read *, const void **,
381 size_t *, size_t *, uint64_t);
382 static uint64_t atol10(const char *, size_t);
383 static int64_t atol8(const char *, size_t);
384 static size_t atohex(unsigned char *, size_t, const char *, size_t);
385 static time_t parse_time(const char *p, size_t n);
386 static void heap_add_entry(struct heap_queue *, struct xar_file *);
387 static struct xar_file *heap_get_entry(struct heap_queue *);
388 static void add_link(struct xar *, struct xar_file *);
389 static void checksum_init(struct archive_read *, int, int);
390 static void checksum_update(struct archive_read *, const void *,
391 size_t, const void *, size_t);
392 static int checksum_final(struct archive_read *, const void *,
393 size_t, const void *, size_t);
394 static int decompression_init(struct archive_read *, enum enctype);
395 static int decompress(struct archive_read *, const void **,
396 size_t *, const void *, size_t *);
397 static int decompression_cleanup(struct archive_read *);
398 static void xmlattr_cleanup(struct xmlattr_list *);
399 static void file_new(struct xar *, struct xmlattr_list *);
400 static void file_free(struct xar_file *);
401 static void xattr_new(struct xar *, struct xmlattr_list *);
402 static void xattr_free(struct xattr *);
403 static int getencoding(struct xmlattr_list *);
404 static int getsumalgorithm(struct xmlattr_list *);
405 static void unknowntag_start(struct xar *, const char *);
406 static void unknowntag_end(struct xar *, const char *);
407 static void xml_start(void *, const char *, struct xmlattr_list *);
408 static void xml_end(void *, const char *);
409 static void xml_data(void *, const char *, int);
410 static int xml_parse_file_flags(struct xar *, const char *);
411 static int xml_parse_file_ext2(struct xar *, const char *);
412 #if defined(HAVE_LIBXML_XMLREADER_H)
413 static int xml2_xmlattr_setup(struct xmlattr_list *, xmlTextReaderPtr);
414 static int xml2_read_cb(void *, char *, int);
415 static int xml2_close_cb(void *);
416 static void xml2_error_hdr(void *, const char *, xmlParserSeverities,
417 xmlTextReaderLocatorPtr);
418 static int xml2_read_toc(struct archive_read *);
419 #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
420 static void expat_xmlattr_setup(struct xmlattr_list *, const XML_Char **);
421 static void expat_start_cb(void *, const XML_Char *, const XML_Char **);
422 static void expat_end_cb(void *, const XML_Char *);
423 static void expat_data_cb(void *, const XML_Char *, int);
424 static int expat_read_toc(struct archive_read *);
428 archive_read_support_format_xar(struct archive *_a)
431 struct archive_read *a = (struct archive_read *)_a;
434 xar = (struct xar *)calloc(1, sizeof(*xar));
436 archive_set_error(&a->archive, ENOMEM,
437 "Can't allocate xar data");
438 return (ARCHIVE_FATAL);
441 r = __archive_read_register_format(a,
456 xar_bid(struct archive_read *a)
458 const unsigned char *b;
461 b = __archive_read_ahead(a, HEADER_SIZE, NULL);
469 if (archive_be32dec(b) != HEADER_MAGIC)
475 if (archive_be16dec(b+4) != HEADER_SIZE)
479 * Verify header version
481 if (archive_be16dec(b+6) != HEADER_VERSION)
485 * Verify type of checksum
487 switch (archive_be32dec(b+24)) {
501 read_toc(struct archive_read *a)
504 struct xar_file *file;
505 const unsigned char *b;
506 uint64_t toc_compressed_size;
507 uint64_t toc_uncompressed_size;
508 uint32_t toc_chksum_alg;
512 xar = (struct xar *)(a->format->data);
517 b = __archive_read_ahead(a, HEADER_SIZE, &bytes);
520 if (bytes < HEADER_SIZE) {
521 archive_set_error(&a->archive,
522 ARCHIVE_ERRNO_FILE_FORMAT,
523 "Truncated archive header");
524 return (ARCHIVE_FATAL);
527 if (archive_be32dec(b) != HEADER_MAGIC) {
528 archive_set_error(&a->archive,
529 ARCHIVE_ERRNO_FILE_FORMAT,
530 "Invalid header magic");
531 return (ARCHIVE_FATAL);
533 if (archive_be16dec(b+6) != HEADER_VERSION) {
534 archive_set_error(&a->archive,
535 ARCHIVE_ERRNO_FILE_FORMAT,
536 "Unsupported header version(%d)",
537 archive_be16dec(b+6));
538 return (ARCHIVE_FATAL);
540 toc_compressed_size = archive_be64dec(b+8);
541 xar->toc_remaining = toc_compressed_size;
542 toc_uncompressed_size = archive_be64dec(b+16);
543 toc_chksum_alg = archive_be32dec(b+24);
544 __archive_read_consume(a, HEADER_SIZE);
545 xar->offset += HEADER_SIZE;
549 * Read TOC(Table of Contents).
551 /* Initialize reading contents. */
552 r = move_reading_point(a, HEADER_SIZE);
555 r = rd_contents_init(a, GZIP, toc_chksum_alg, CKSUM_NONE);
559 #ifdef HAVE_LIBXML_XMLREADER_H
560 r = xml2_read_toc(a);
561 #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
562 r = expat_read_toc(a);
567 /* Set 'The HEAP' base. */
568 xar->h_base = xar->offset;
569 if (xar->toc_total != toc_uncompressed_size) {
570 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
571 "TOC uncompressed size error");
572 return (ARCHIVE_FATAL);
578 if (toc_chksum_alg != CKSUM_NONE) {
579 r = move_reading_point(a, xar->toc_chksum_offset);
582 b = __archive_read_ahead(a, xar->toc_chksum_size, &bytes);
585 if ((uint64_t)bytes < xar->toc_chksum_size) {
586 archive_set_error(&a->archive,
587 ARCHIVE_ERRNO_FILE_FORMAT,
588 "Truncated archive file");
589 return (ARCHIVE_FATAL);
591 r = checksum_final(a, b, xar->toc_chksum_size, NULL, 0);
592 __archive_read_consume(a, xar->toc_chksum_size);
593 xar->offset += xar->toc_chksum_size;
595 return (ARCHIVE_FATAL);
599 * Connect hardlinked files.
601 for (file = xar->hdlink_orgs; file != NULL; file = file->hdnext) {
602 struct hdlink **hdlink;
604 for (hdlink = &(xar->hdlink_list); *hdlink != NULL;
605 hdlink = &((*hdlink)->next)) {
606 if ((*hdlink)->id == file->id) {
607 struct hdlink *hltmp;
609 int nlink = (*hdlink)->cnt + 1;
612 for (f2 = (*hdlink)->files; f2 != NULL;
616 &(f2->hardlink), &(file->pathname));
618 /* Remove resolved files from hdlist_list. */
620 *hdlink = hltmp->next;
626 a->archive.archive_format = ARCHIVE_FORMAT_XAR;
627 a->archive.archive_format_name = "xar";
633 xar_read_header(struct archive_read *a, struct archive_entry *entry)
636 struct xar_file *file;
640 xar = (struct xar *)(a->format->data);
642 if (xar->offset == 0) {
650 file = xar->file = heap_get_entry(&(xar->file_queue));
652 xar->end_of_file = 1;
653 return (ARCHIVE_EOF);
655 if ((file->mode & AE_IFMT) != AE_IFDIR)
657 if (file->has != (HAS_PATHNAME | HAS_TYPE))
660 * If a file type is a directory and it does not have
661 * any metadata, do not export.
665 archive_entry_set_atime(entry, file->atime, 0);
666 archive_entry_set_ctime(entry, file->ctime, 0);
667 archive_entry_set_mtime(entry, file->mtime, 0);
668 archive_entry_set_gid(entry, file->gid);
669 if (file->gname.length > 0)
670 archive_entry_update_gname_utf8(entry, file->gname.s);
671 archive_entry_set_uid(entry, file->uid);
672 if (file->uname.length > 0)
673 archive_entry_update_uname_utf8(entry, file->uname.s);
674 archive_entry_set_mode(entry, file->mode);
675 archive_entry_update_pathname_utf8(entry, file->pathname.s);
676 if (file->symlink.length > 0)
677 archive_entry_update_symlink_utf8(entry, file->symlink.s);
678 /* Set proper nlink. */
679 if ((file->mode & AE_IFMT) == AE_IFDIR)
680 archive_entry_set_nlink(entry, file->subdirs + 2);
682 archive_entry_set_nlink(entry, file->nlink);
683 archive_entry_set_size(entry, file->size);
684 if (archive_strlen(&(file->hardlink)) > 0)
685 archive_entry_update_hardlink_utf8(entry,
687 archive_entry_set_ino64(entry, file->ino64);
688 if (file->has & HAS_DEV)
689 archive_entry_set_dev(entry, file->dev);
690 if (file->has & HAS_DEVMAJOR)
691 archive_entry_set_devmajor(entry, file->devmajor);
692 if (file->has & HAS_DEVMINOR)
693 archive_entry_set_devminor(entry, file->devminor);
694 if (archive_strlen(&(file->fflags_text)) > 0)
695 archive_entry_copy_fflags_text(entry, file->fflags_text.s);
698 xar->entry_total = 0;
699 xar->entry_remaining = file->length;
700 xar->entry_size = file->size;
701 xar->entry_encoding = file->encoding;
702 xar->entry_a_sum = file->a_sum;
703 xar->entry_e_sum = file->e_sum;
705 * Read extended attributes.
708 xattr = file->xattr_list;
709 while (xattr != NULL) {
711 size_t outbytes, used;
713 r = move_reading_point(a, xattr->offset);
716 r = rd_contents_init(a, xattr->encoding,
717 xattr->a_sum.alg, xattr->e_sum.alg);
721 r = rd_contents(a, &d, &outbytes, &used, xattr->length);
724 if (outbytes != xattr->size) {
725 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
726 "Decompressed size error");
730 r = checksum_final(a,
731 xattr->a_sum.val, xattr->a_sum.len,
732 xattr->e_sum.val, xattr->e_sum.len);
735 archive_entry_xattr_add_entry(entry,
736 xattr->name.s, d, outbytes);
739 if (r != ARCHIVE_OK) {
744 if (xar->entry_remaining > 0)
745 /* Move reading point to the beginning of current
747 r = move_reading_point(a, file->offset);
756 xar_read_data(struct archive_read *a,
757 const void **buff, size_t *size, off_t *offset)
763 xar = (struct xar *)(a->format->data);
764 if (xar->end_of_file || xar->entry_remaining <= 0) {
766 goto abort_read_data;
769 if (xar->entry_init) {
770 r = rd_contents_init(a, xar->entry_encoding,
771 xar->entry_a_sum.alg, xar->entry_e_sum.alg);
772 if (r != ARCHIVE_OK) {
773 xar->entry_remaining = 0;
780 r = rd_contents(a, buff, size, &used, xar->entry_remaining);
782 goto abort_read_data;
784 *offset = xar->entry_total;
785 xar->entry_total += *size;
788 xar->entry_remaining -= used;
789 __archive_read_consume(a, used);
791 if (xar->entry_remaining == 0) {
792 if (xar->entry_total != xar->entry_size) {
793 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
794 "Decompressed size error");
796 goto abort_read_data;
798 r = checksum_final(a,
799 xar->entry_a_sum.val, xar->entry_a_sum.len,
800 xar->entry_e_sum.val, xar->entry_e_sum.len);
802 goto abort_read_data;
809 *offset = xar->total;
814 xar_read_data_skip(struct archive_read *a)
817 int64_t bytes_skipped;
819 xar = (struct xar *)(a->format->data);
820 if (xar->end_of_file)
821 return (ARCHIVE_EOF);
822 bytes_skipped = __archive_read_skip(a, xar->entry_remaining);
823 if (bytes_skipped < 0)
824 return (ARCHIVE_FATAL);
825 xar->offset += bytes_skipped;
830 xar_cleanup(struct archive_read *a)
833 struct hdlink *hdlink;
837 xar = (struct xar *)(a->format->data);
838 r = decompression_cleanup(a);
839 hdlink = xar->hdlink_list;
840 while (hdlink != NULL) {
841 struct hdlink *next = hdlink->next;
846 for (i = 0; i < xar->file_queue.used; i++)
847 file_free(xar->file_queue.files[i]);
848 while (xar->unknowntags != NULL) {
849 struct unknown_tag *tag;
851 tag = xar->unknowntags;
852 xar->unknowntags = tag->next;
853 archive_string_free(&(tag->name));
857 a->format->data = NULL;
862 move_reading_point(struct archive_read *a, uint64_t offset)
866 xar = (struct xar *)(a->format->data);
867 if (xar->offset - xar->h_base != offset) {
868 /* Seek forward to the start of file contents. */
871 step = offset - (xar->offset - xar->h_base);
873 step = __archive_read_skip(a, step);
878 archive_set_error(&(a->archive),
881 return (ARCHIVE_FAILED);
888 rd_contents_init(struct archive_read *a, enum enctype encoding,
889 int a_sum_alg, int e_sum_alg)
893 /* Init decompress library. */
894 if ((r = decompression_init(a, encoding)) != ARCHIVE_OK)
896 /* Init checksum library. */
897 checksum_init(a, a_sum_alg, e_sum_alg);
902 rd_contents(struct archive_read *a, const void **buff, size_t *size,
903 size_t *used, uint64_t remaining)
905 const unsigned char *b;
908 /* Get whatever bytes are immediately available. */
909 b = __archive_read_ahead(a, 1, &bytes);
913 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
914 "Truncated archive file");
915 return (ARCHIVE_FATAL);
917 if ((uint64_t)bytes > remaining)
918 bytes = (ssize_t)remaining;
921 * Decompress contents of file.
924 if (decompress(a, buff, size, b, used) != ARCHIVE_OK)
925 return (ARCHIVE_FATAL);
928 * Update checksum of a compressed data and a extracted data.
930 checksum_update(a, b, *used, *buff, *size);
936 * Note that this implementation does not (and should not!) obey
937 * locale settings; you cannot simply substitute strtol here, since
938 * it does obey locale.
942 atol10(const char *p, size_t char_cnt)
949 while (digit >= 0 && digit < 10 && char_cnt-- > 0) {
950 l = (l * 10) + digit;
957 atol8(const char *p, size_t char_cnt)
963 while (char_cnt-- > 0) {
964 if (*p >= '0' && *p <= '7')
976 atohex(unsigned char *b, size_t bsize, const char *p, size_t psize)
978 size_t fbsize = bsize;
980 while (bsize && psize > 1) {
983 if (p[0] >= 'a' && p[0] <= 'z')
984 x = (p[0] - 'a' + 0x0a) << 4;
985 else if (p[0] >= 'A' && p[0] <= 'Z')
986 x = (p[0] - 'A' + 0x0a) << 4;
987 else if (p[0] >= '0' && p[0] <= '9')
988 x = (p[0] - '0') << 4;
991 if (p[1] >= 'a' && p[1] <= 'z')
992 x |= p[1] - 'a' + 0x0a;
993 else if (p[1] >= 'A' && p[1] <= 'Z')
994 x |= p[1] - 'A' + 0x0a;
995 else if (p[1] >= '0' && p[1] <= '9')
1005 return (fbsize - bsize);
1009 time_from_tm(struct tm *t)
1012 /* Use platform timegm() if available. */
1015 /* Else use direct calculation using POSIX assumptions. */
1016 /* First, fix up tm_yday based on the year/month/day. */
1018 /* Then we can compute timegm() from first principles. */
1019 return (t->tm_sec + t->tm_min * 60 + t->tm_hour * 3600
1020 + t->tm_yday * 86400 + (t->tm_year - 70) * 31536000
1021 + ((t->tm_year - 69) / 4) * 86400 -
1022 ((t->tm_year - 1) / 100) * 86400
1023 + ((t->tm_year + 299) / 400) * 86400);
1028 parse_time(const char *p, size_t n)
1034 memset(&tm, 0, sizeof(tm));
1037 data = atol10(p, 4);
1040 tm.tm_year = (int)data - 1900;
1044 data = atol10(p, 2);
1045 if (data < 1 || data > 12)
1047 tm.tm_mon = (int)data -1;
1051 data = atol10(p, 2);
1052 if (data < 1 || data > 31)
1054 tm.tm_mday = (int)data;
1058 data = atol10(p, 2);
1059 if (data < 0 || data > 23)
1061 tm.tm_hour = (int)data;
1065 data = atol10(p, 2);
1066 if (data < 0 || data > 59)
1068 tm.tm_min = (int)data;
1072 data = atol10(p, 2);
1073 if (data < 0 || data > 60)
1075 tm.tm_sec = (int)data;
1082 t = time_from_tm(&tm);
1088 heap_add_entry(struct heap_queue *heap, struct xar_file *file)
1090 uint64_t file_id, parent_id;
1093 /* Expand our pending files list as necessary. */
1094 if (heap->used >= heap->allocated) {
1095 struct xar_file **new_pending_files;
1096 int new_size = heap->allocated * 2;
1098 if (heap->allocated < 1024)
1100 /* Overflow might keep us from growing the list. */
1101 if (new_size <= heap->allocated)
1102 __archive_errx(1, "Out of memory");
1103 new_pending_files = (struct xar_file **)
1104 malloc(new_size * sizeof(new_pending_files[0]));
1105 if (new_pending_files == NULL)
1106 __archive_errx(1, "Out of memory");
1107 memcpy(new_pending_files, heap->files,
1108 heap->allocated * sizeof(new_pending_files[0]));
1109 if (heap->files != NULL)
1111 heap->files = new_pending_files;
1112 heap->allocated = new_size;
1118 * Start with hole at end, walk it up tree to find insertion point.
1120 hole = heap->used++;
1122 parent = (hole - 1)/2;
1123 parent_id = heap->files[parent]->id;
1124 if (file_id >= parent_id) {
1125 heap->files[hole] = file;
1128 // Move parent into hole <==> move hole up tree.
1129 heap->files[hole] = heap->files[parent];
1132 heap->files[0] = file;
1135 static struct xar_file *
1136 heap_get_entry(struct heap_queue *heap)
1138 uint64_t a_id, b_id, c_id;
1140 struct xar_file *r, *tmp;
1146 * The first file in the list is the earliest; we'll return this.
1151 * Move the last item in the heap to the root of the tree
1153 heap->files[0] = heap->files[--(heap->used)];
1156 * Rebalance the heap.
1158 a = 0; // Starting element and its heap key
1159 a_id = heap->files[a]->id;
1161 b = a + a + 1; // First child
1162 if (b >= heap->used)
1164 b_id = heap->files[b]->id;
1165 c = b + 1; // Use second child if it is smaller.
1166 if (c < heap->used) {
1167 c_id = heap->files[c]->id;
1175 tmp = heap->files[a];
1176 heap->files[a] = heap->files[b];
1177 heap->files[b] = tmp;
1183 add_link(struct xar *xar, struct xar_file *file)
1185 struct hdlink *hdlink;
1187 for (hdlink = xar->hdlink_list; hdlink != NULL; hdlink = hdlink->next) {
1188 if (hdlink->id == file->link) {
1189 file->hdnext = hdlink->files;
1191 hdlink->files = file;
1195 hdlink = malloc(sizeof(*hdlink));
1197 __archive_errx(1, "No memory for add_link()");
1198 file->hdnext = NULL;
1199 hdlink->id = file->link;
1201 hdlink->files = file;
1202 hdlink->next = xar->hdlink_list;
1203 xar->hdlink_list = hdlink;
1207 _checksum_init(struct chksumwork *sumwrk, int sum_alg)
1209 sumwrk->alg = sum_alg;
1214 archive_sha1_init(&(sumwrk->sha1ctx));
1217 archive_md5_init(&(sumwrk->md5ctx));
1223 _checksum_update(struct chksumwork *sumwrk, const void *buff, size_t size)
1226 switch (sumwrk->alg) {
1230 archive_sha1_update(&(sumwrk->sha1ctx), buff, size);
1233 archive_md5_update(&(sumwrk->md5ctx), buff, size);
1239 _checksum_final(struct chksumwork *sumwrk, const void *val, size_t len)
1241 unsigned char sum[MAX_SUM_SIZE];
1244 switch (sumwrk->alg) {
1248 archive_sha1_final(&(sumwrk->sha1ctx), sum);
1249 if (len != SHA1_SIZE ||
1250 memcmp(val, sum, SHA1_SIZE) != 0)
1254 archive_md5_final(&(sumwrk->md5ctx), sum);
1255 if (len != MD5_SIZE ||
1256 memcmp(val, sum, MD5_SIZE) != 0)
1264 checksum_init(struct archive_read *a, int a_sum_alg, int e_sum_alg)
1268 xar = (struct xar *)(a->format->data);
1269 _checksum_init(&(xar->a_sumwrk), a_sum_alg);
1270 _checksum_init(&(xar->e_sumwrk), e_sum_alg);
1274 checksum_update(struct archive_read *a, const void *abuff, size_t asize,
1275 const void *ebuff, size_t esize)
1279 xar = (struct xar *)(a->format->data);
1280 _checksum_update(&(xar->a_sumwrk), abuff, asize);
1281 _checksum_update(&(xar->e_sumwrk), ebuff, esize);
1285 checksum_final(struct archive_read *a, const void *a_sum_val,
1286 size_t a_sum_len, const void *e_sum_val, size_t e_sum_len)
1291 xar = (struct xar *)(a->format->data);
1292 r = _checksum_final(&(xar->a_sumwrk), a_sum_val, a_sum_len);
1293 if (r == ARCHIVE_OK)
1294 r = _checksum_final(&(xar->e_sumwrk), e_sum_val, e_sum_len);
1295 if (r != ARCHIVE_OK)
1296 archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC,
1302 decompression_init(struct archive_read *a, enum enctype encoding)
1308 xar = (struct xar *)(a->format->data);
1309 xar->rd_encoding = encoding;
1314 if (xar->stream_valid)
1315 r = inflateReset(&(xar->stream));
1317 r = inflateInit(&(xar->stream));
1319 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1320 "Couldn't initialize zlib stream.");
1321 return (ARCHIVE_FATAL);
1323 xar->stream_valid = 1;
1324 xar->stream.total_in = 0;
1325 xar->stream.total_out = 0;
1327 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1329 if (xar->bzstream_valid) {
1330 BZ2_bzDecompressEnd(&(xar->bzstream));
1331 xar->bzstream_valid = 0;
1333 r = BZ2_bzDecompressInit(&(xar->bzstream), 0, 0);
1334 if (r == BZ_MEM_ERROR)
1335 r = BZ2_bzDecompressInit(&(xar->bzstream), 0, 1);
1337 int err = ARCHIVE_ERRNO_MISC;
1340 case BZ_PARAM_ERROR:
1341 detail = "invalid setup parameter";
1345 detail = "out of memory";
1347 case BZ_CONFIG_ERROR:
1348 detail = "mis-compiled library";
1351 archive_set_error(&a->archive, err,
1352 "Internal error initializing decompressor: %s",
1353 detail == NULL ? "??" : detail);
1354 xar->bzstream_valid = 0;
1355 return (ARCHIVE_FATAL);
1357 xar->bzstream_valid = 1;
1358 xar->bzstream.total_in_lo32 = 0;
1359 xar->bzstream.total_in_hi32 = 0;
1360 xar->bzstream.total_out_lo32 = 0;
1361 xar->bzstream.total_out_hi32 = 0;
1364 #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1367 if (xar->lzstream_valid) {
1368 lzma_end(&(xar->lzstream));
1369 xar->lzstream_valid = 0;
1371 if (xar->entry_encoding == XZ)
1372 r = lzma_stream_decoder(&(xar->lzstream),
1373 (1U << 30),/* memlimit */
1376 r = lzma_alone_decoder(&(xar->lzstream),
1377 (1U << 30));/* memlimit */
1380 case LZMA_MEM_ERROR:
1381 archive_set_error(&a->archive,
1383 "Internal error initializing "
1384 "compression library: "
1385 "Cannot allocate memory");
1387 case LZMA_OPTIONS_ERROR:
1388 archive_set_error(&a->archive,
1390 "Internal error initializing "
1391 "compression library: "
1392 "Invalid or unsupported options");
1395 archive_set_error(&a->archive,
1397 "Internal error initializing "
1401 return (ARCHIVE_FATAL);
1403 xar->lzstream_valid = 1;
1404 xar->lzstream.total_in = 0;
1405 xar->lzstream.total_out = 0;
1407 #elif defined(HAVE_LZMADEC_H) && defined(HAVE_LIBLZMADEC)
1409 if (xar->lzstream_valid)
1410 lzmadec_end(&(xar->lzstream));
1411 r = lzmadec_init(&(xar->lzstream));
1412 if (r != LZMADEC_OK) {
1414 case LZMADEC_HEADER_ERROR:
1415 archive_set_error(&a->archive,
1417 "Internal error initializing "
1418 "compression library: "
1421 case LZMADEC_MEM_ERROR:
1422 archive_set_error(&a->archive,
1424 "Internal error initializing "
1425 "compression library: "
1429 return (ARCHIVE_FATAL);
1431 xar->lzstream_valid = 1;
1432 xar->lzstream.total_in = 0;
1433 xar->lzstream.total_out = 0;
1437 * Unsupported compression.
1440 #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR)
1443 #if !defined(HAVE_LZMA_H) || !defined(HAVE_LIBLZMA)
1444 #if !defined(HAVE_LZMADEC_H) || !defined(HAVE_LIBLZMADEC)
1449 switch (xar->entry_encoding) {
1450 case BZIP2: detail = "bzip2"; break;
1451 case LZMA: detail = "lzma"; break;
1452 case XZ: detail = "xz"; break;
1453 default: detail = "??"; break;
1455 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1456 "%s compression not supported on this platform",
1458 return (ARCHIVE_FAILED);
1460 return (ARCHIVE_OK);
1464 decompress(struct archive_read *a, const void **buff, size_t *outbytes,
1465 const void *b, size_t *used)
1469 size_t avail_in, avail_out;
1472 xar = (struct xar *)(a->format->data);
1474 outbuff = (void *)(uintptr_t)*buff;
1475 if (outbuff == NULL) {
1476 outbuff = xar->buff;
1478 avail_out = sizeof(xar->buff);
1480 avail_out = *outbytes;
1481 switch (xar->rd_encoding) {
1483 xar->stream.next_in = (Bytef *)(uintptr_t)b;
1484 xar->stream.avail_in = avail_in;
1485 xar->stream.next_out = (unsigned char *)outbuff;
1486 xar->stream.avail_out = avail_out;
1487 r = inflate(&(xar->stream), 0);
1489 case Z_OK: /* Decompressor made some progress.*/
1490 case Z_STREAM_END: /* Found end of stream. */
1493 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1494 "File decompression failed (%d)", r);
1495 return (ARCHIVE_FATAL);
1497 *used = avail_in - xar->stream.avail_in;
1498 *outbytes = avail_out - xar->stream.avail_out;
1500 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1502 xar->bzstream.next_in = (char *)(uintptr_t)b;
1503 xar->bzstream.avail_in = avail_in;
1504 xar->bzstream.next_out = (char *)outbuff;
1505 xar->bzstream.avail_out = avail_out;
1506 r = BZ2_bzDecompress(&(xar->bzstream));
1508 case BZ_STREAM_END: /* Found end of stream. */
1509 switch (BZ2_bzDecompressEnd(&(xar->bzstream))) {
1513 archive_set_error(&(a->archive),
1515 "Failed to clean up decompressor");
1516 return (ARCHIVE_FATAL);
1518 xar->bzstream_valid = 0;
1520 case BZ_OK: /* Decompressor made some progress. */
1523 archive_set_error(&(a->archive),
1525 "bzip decompression failed");
1526 return (ARCHIVE_FATAL);
1528 *used = avail_in - xar->bzstream.avail_in;
1529 *outbytes = avail_out - xar->bzstream.avail_out;
1532 #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1535 xar->lzstream.next_in = b;
1536 xar->lzstream.avail_in = avail_in;
1537 xar->lzstream.next_out = (unsigned char *)outbuff;
1538 xar->lzstream.avail_out = avail_out;
1539 r = lzma_code(&(xar->lzstream), LZMA_RUN);
1541 case LZMA_STREAM_END: /* Found end of stream. */
1542 lzma_end(&(xar->lzstream));
1543 xar->lzstream_valid = 0;
1545 case LZMA_OK: /* Decompressor made some progress. */
1548 archive_set_error(&(a->archive),
1550 "%s decompression failed(%d)",
1551 (xar->entry_encoding == XZ)?"xz":"lzma",
1553 return (ARCHIVE_FATAL);
1555 *used = avail_in - xar->lzstream.avail_in;
1556 *outbytes = avail_out - xar->lzstream.avail_out;
1558 #elif defined(HAVE_LZMADEC_H) && defined(HAVE_LIBLZMADEC)
1560 xar->lzstream.next_in = (unsigned char *)(uintptr_t)b;
1561 xar->lzstream.avail_in = avail_in;
1562 xar->lzstream.next_out = (unsigned char *)outbuff;
1563 xar->lzstream.avail_out = avail_out;
1564 r = lzmadec_decode(&(xar->lzstream), 0);
1566 case LZMADEC_STREAM_END: /* Found end of stream. */
1567 switch (lzmadec_end(&(xar->lzstream))) {
1571 archive_set_error(&(a->archive),
1573 "Failed to clean up lzmadec decompressor");
1574 return (ARCHIVE_FATAL);
1576 xar->lzstream_valid = 0;
1578 case LZMADEC_OK: /* Decompressor made some progress. */
1581 archive_set_error(&(a->archive),
1583 "lzmadec decompression failed(%d)",
1585 return (ARCHIVE_FATAL);
1587 *used = avail_in - xar->lzstream.avail_in;
1588 *outbytes = avail_out - xar->lzstream.avail_out;
1591 #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR)
1594 #if !defined(HAVE_LZMA_H) || !defined(HAVE_LIBLZMA)
1595 #if !defined(HAVE_LZMADEC_H) || !defined(HAVE_LIBLZMADEC)
1602 if (outbuff == xar->buff) {
1605 *outbytes = avail_in;
1607 if (avail_out > avail_in)
1608 avail_out = avail_in;
1609 memcpy(outbuff, b, avail_out);
1611 *outbytes = avail_out;
1615 return (ARCHIVE_OK);
1619 decompression_cleanup(struct archive_read *a)
1624 xar = (struct xar *)(a->format->data);
1626 if (xar->stream_valid) {
1627 if (inflateEnd(&(xar->stream)) != Z_OK) {
1628 archive_set_error(&a->archive,
1630 "Failed to clean up zlib decompressor");
1634 #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR)
1635 if (xar->bzstream_valid) {
1636 if (BZ2_bzDecompressEnd(&(xar->bzstream)) != BZ_OK) {
1637 archive_set_error(&a->archive,
1639 "Failed to clean up bzip2 decompressor");
1644 #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1645 if (xar->lzstream_valid)
1646 lzma_end(&(xar->lzstream));
1647 #elif defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA)
1648 if (xar->lzstream_valid) {
1649 if (lzmadec_end(&(xar->lzstream)) != LZMADEC_OK) {
1650 archive_set_error(&a->archive,
1652 "Failed to clean up lzmadec decompressor");
1661 xmlattr_cleanup(struct xmlattr_list *list)
1663 struct xmlattr *attr, *next;
1666 while (attr != NULL) {
1674 list->last = &(list->first);
1678 file_new(struct xar *xar, struct xmlattr_list *list)
1680 struct xar_file *file;
1681 struct xmlattr *attr;
1683 file = calloc(1, sizeof(*file));
1685 __archive_errx(1, "Out of memory");
1686 file->parent = xar->file;
1687 file->mode = 0777 | AE_IFREG;
1688 file->atime = time(NULL);
1689 file->mtime = time(NULL);
1692 for (attr = list->first; attr != NULL; attr = attr->next) {
1693 if (strcmp(attr->name, "id") == 0)
1694 file->id = atol10(attr->value, strlen(attr->value));
1697 heap_add_entry(&(xar->file_queue), file);
1701 file_free(struct xar_file *file)
1703 struct xattr *xattr;
1705 archive_string_free(&(file->pathname));
1706 archive_string_free(&(file->symlink));
1707 archive_string_free(&(file->uname));
1708 archive_string_free(&(file->gname));
1709 archive_string_free(&(file->hardlink));
1710 xattr = file->xattr_list;
1711 while (xattr != NULL) {
1723 xattr_new(struct xar *xar, struct xmlattr_list *list)
1725 struct xattr *xattr, **nx;
1726 struct xmlattr *attr;
1728 xattr = calloc(1, sizeof(*xattr));
1730 __archive_errx(1, "Out of memory");
1732 for (attr = list->first; attr != NULL; attr = attr->next) {
1733 if (strcmp(attr->name, "id") == 0)
1734 xattr->id = atol10(attr->value, strlen(attr->value));
1736 /* Chain to xattr list. */
1737 for (nx = &(xar->file->xattr_list);
1738 *nx != NULL; nx = &((*nx)->next)) {
1739 if (xattr->id < (*nx)->id)
1747 xattr_free(struct xattr *xattr)
1749 archive_string_free(&(xattr->name));
1754 getencoding(struct xmlattr_list *list)
1756 struct xmlattr *attr;
1757 enum enctype encoding = NONE;
1759 for (attr = list->first; attr != NULL; attr = attr->next) {
1760 if (strcmp(attr->name, "style") == 0) {
1761 if (strcmp(attr->value, "application/octet-stream") == 0)
1763 else if (strcmp(attr->value, "application/x-gzip") == 0)
1765 else if (strcmp(attr->value, "application/x-bzip2") == 0)
1767 else if (strcmp(attr->value, "application/x-lzma") == 0)
1769 else if (strcmp(attr->value, "application/x-xz") == 0)
1777 getsumalgorithm(struct xmlattr_list *list)
1779 struct xmlattr *attr;
1780 int alg = CKSUM_NONE;
1782 for (attr = list->first; attr != NULL; attr = attr->next) {
1783 if (strcmp(attr->name, "style") == 0) {
1784 const char *v = attr->value;
1785 if ((v[0] == 'S' || v[0] == 's') &&
1786 (v[1] == 'H' || v[1] == 'h') &&
1787 (v[2] == 'A' || v[2] == 'a') &&
1788 v[3] == '1' && v[4] == '\0')
1790 if ((v[0] == 'M' || v[0] == 'm') &&
1791 (v[1] == 'D' || v[1] == 'd') &&
1792 v[2] == '5' && v[3] == '\0')
1800 unknowntag_start(struct xar *xar, const char *name)
1802 struct unknown_tag *tag;
1805 fprintf(stderr, "unknowntag_start:%s\n", name);
1807 tag = malloc(sizeof(*tag));
1809 __archive_errx(1, "Out of memory");
1810 tag->next = xar->unknowntags;
1811 archive_string_init(&(tag->name));
1812 archive_strcpy(&(tag->name), name);
1813 if (xar->unknowntags == NULL) {
1814 xar->xmlsts_unknown = xar->xmlsts;
1815 xar->xmlsts = UNKNOWN;
1817 xar->unknowntags = tag;
1821 unknowntag_end(struct xar *xar, const char *name)
1823 struct unknown_tag *tag;
1826 fprintf(stderr, "unknowntag_end:%s\n", name);
1828 tag = xar->unknowntags;
1829 if (tag == NULL || name == NULL)
1831 if (strcmp(tag->name.s, name) == 0) {
1832 xar->unknowntags = tag->next;
1833 archive_string_free(&(tag->name));
1835 if (xar->unknowntags == NULL)
1836 xar->xmlsts = xar->xmlsts_unknown;
1841 xml_start(void *userData, const char *name, struct xmlattr_list *list)
1843 struct archive_read *a;
1845 struct xmlattr *attr;
1847 a = (struct archive_read *)userData;
1848 xar = (struct xar *)(a->format->data);
1851 fprintf(stderr, "xml_sta:[%s]\n", name);
1852 for (attr = list->first; attr != NULL; attr = attr->next)
1853 fprintf(stderr, " attr:\"%s\"=\"%s\"\n",
1854 attr->name, attr->value);
1856 xar->base64text = 0;
1857 switch (xar->xmlsts) {
1859 if (strcmp(name, "xar") == 0)
1862 unknowntag_start(xar, name);
1865 if (strcmp(name, "toc") == 0)
1868 unknowntag_start(xar, name);
1871 if (strcmp(name, "creation-time") == 0)
1872 xar->xmlsts = TOC_CREATION_TIME;
1873 else if (strcmp(name, "checksum") == 0)
1874 xar->xmlsts = TOC_CHECKSUM;
1875 else if (strcmp(name, "file") == 0) {
1876 file_new(xar, list);
1877 xar->xmlsts = TOC_FILE;
1880 unknowntag_start(xar, name);
1883 if (strcmp(name, "offset") == 0)
1884 xar->xmlsts = TOC_CHECKSUM_OFFSET;
1885 else if (strcmp(name, "size") == 0)
1886 xar->xmlsts = TOC_CHECKSUM_SIZE;
1888 unknowntag_start(xar, name);
1891 if (strcmp(name, "file") == 0) {
1892 file_new(xar, list);
1894 else if (strcmp(name, "data") == 0)
1895 xar->xmlsts = FILE_DATA;
1896 else if (strcmp(name, "ea") == 0) {
1897 xattr_new(xar, list);
1898 xar->xmlsts = FILE_EA;
1900 else if (strcmp(name, "ctime") == 0)
1901 xar->xmlsts = FILE_CTIME;
1902 else if (strcmp(name, "mtime") == 0)
1903 xar->xmlsts = FILE_MTIME;
1904 else if (strcmp(name, "atime") == 0)
1905 xar->xmlsts = FILE_ATIME;
1906 else if (strcmp(name, "group") == 0)
1907 xar->xmlsts = FILE_GROUP;
1908 else if (strcmp(name, "gid") == 0)
1909 xar->xmlsts = FILE_GID;
1910 else if (strcmp(name, "user") == 0)
1911 xar->xmlsts = FILE_USER;
1912 else if (strcmp(name, "uid") == 0)
1913 xar->xmlsts = FILE_UID;
1914 else if (strcmp(name, "mode") == 0)
1915 xar->xmlsts = FILE_MODE;
1916 else if (strcmp(name, "device") == 0)
1917 xar->xmlsts = FILE_DEVICE;
1918 else if (strcmp(name, "deviceno") == 0)
1919 xar->xmlsts = FILE_DEVICENO;
1920 else if (strcmp(name, "inode") == 0)
1921 xar->xmlsts = FILE_INODE;
1922 else if (strcmp(name, "link") == 0)
1923 xar->xmlsts = FILE_LINK;
1924 else if (strcmp(name, "type") == 0) {
1925 xar->xmlsts = FILE_TYPE;
1926 for (attr = list->first; attr != NULL;
1927 attr = attr->next) {
1928 if (strcmp(attr->name, "link") != 0)
1930 if (strcmp(attr->value, "original") == 0) {
1931 xar->file->hdnext = xar->hdlink_orgs;
1932 xar->hdlink_orgs = xar->file;
1934 xar->file->link = atol10(attr->value,
1935 strlen(attr->value));
1936 if (xar->file->link > 0)
1937 add_link(xar, xar->file);
1941 else if (strcmp(name, "name") == 0) {
1942 xar->xmlsts = FILE_NAME;
1943 for (attr = list->first; attr != NULL;
1944 attr = attr->next) {
1945 if (strcmp(attr->name, "enctype") == 0 &&
1946 strcmp(attr->value, "base64") == 0)
1947 xar->base64text = 1;
1950 else if (strcmp(name, "acl") == 0)
1951 xar->xmlsts = FILE_ACL;
1952 else if (strcmp(name, "flags") == 0)
1953 xar->xmlsts = FILE_FLAGS;
1954 else if (strcmp(name, "ext2") == 0)
1955 xar->xmlsts = FILE_EXT2;
1957 unknowntag_start(xar, name);
1960 if (strcmp(name, "length") == 0)
1961 xar->xmlsts = FILE_DATA_LENGTH;
1962 else if (strcmp(name, "offset") == 0)
1963 xar->xmlsts = FILE_DATA_OFFSET;
1964 else if (strcmp(name, "size") == 0)
1965 xar->xmlsts = FILE_DATA_SIZE;
1966 else if (strcmp(name, "encoding") == 0) {
1967 xar->xmlsts = FILE_DATA_ENCODING;
1968 xar->file->encoding = getencoding(list);
1970 else if (strcmp(name, "archived-checksum") == 0) {
1971 xar->xmlsts = FILE_DATA_A_CHECKSUM;
1972 xar->file->a_sum.alg = getsumalgorithm(list);
1974 else if (strcmp(name, "extracted-checksum") == 0) {
1975 xar->xmlsts = FILE_DATA_E_CHECKSUM;
1976 xar->file->e_sum.alg = getsumalgorithm(list);
1978 else if (strcmp(name, "content") == 0)
1979 xar->xmlsts = FILE_DATA_CONTENT;
1981 unknowntag_start(xar, name);
1984 if (strcmp(name, "major") == 0)
1985 xar->xmlsts = FILE_DEVICE_MAJOR;
1986 else if (strcmp(name, "minor") == 0)
1987 xar->xmlsts = FILE_DEVICE_MINOR;
1989 unknowntag_start(xar, name);
1991 case FILE_DATA_CONTENT:
1992 unknowntag_start(xar, name);
1995 if (strcmp(name, "length") == 0)
1996 xar->xmlsts = FILE_EA_LENGTH;
1997 else if (strcmp(name, "offset") == 0)
1998 xar->xmlsts = FILE_EA_OFFSET;
1999 else if (strcmp(name, "size") == 0)
2000 xar->xmlsts = FILE_EA_SIZE;
2001 else if (strcmp(name, "encoding") == 0) {
2002 xar->xmlsts = FILE_EA_ENCODING;
2003 xar->xattr->encoding = getencoding(list);
2004 } else if (strcmp(name, "archived-checksum") == 0)
2005 xar->xmlsts = FILE_EA_A_CHECKSUM;
2006 else if (strcmp(name, "extracted-checksum") == 0)
2007 xar->xmlsts = FILE_EA_E_CHECKSUM;
2008 else if (strcmp(name, "name") == 0)
2009 xar->xmlsts = FILE_EA_NAME;
2010 else if (strcmp(name, "fstype") == 0)
2011 xar->xmlsts = FILE_EA_FSTYPE;
2013 unknowntag_start(xar, name);
2016 if (strcmp(name, "appleextended") == 0)
2017 xar->xmlsts = FILE_ACL_APPLEEXTENDED;
2018 if (strcmp(name, "default") == 0)
2019 xar->xmlsts = FILE_ACL_DEFAULT;
2020 else if (strcmp(name, "access") == 0)
2021 xar->xmlsts = FILE_ACL_ACCESS;
2023 unknowntag_start(xar, name);
2026 if (!xml_parse_file_flags(xar, name))
2027 unknowntag_start(xar, name);
2030 if (!xml_parse_file_ext2(xar, name))
2031 unknowntag_start(xar, name);
2033 case TOC_CREATION_TIME:
2034 case TOC_CHECKSUM_OFFSET:
2035 case TOC_CHECKSUM_SIZE:
2036 case FILE_DATA_LENGTH:
2037 case FILE_DATA_OFFSET:
2038 case FILE_DATA_SIZE:
2039 case FILE_DATA_ENCODING:
2040 case FILE_DATA_A_CHECKSUM:
2041 case FILE_DATA_E_CHECKSUM:
2042 case FILE_EA_LENGTH:
2043 case FILE_EA_OFFSET:
2045 case FILE_EA_ENCODING:
2046 case FILE_EA_A_CHECKSUM:
2047 case FILE_EA_E_CHECKSUM:
2049 case FILE_EA_FSTYPE:
2058 case FILE_DEVICE_MAJOR:
2059 case FILE_DEVICE_MINOR:
2065 case FILE_ACL_DEFAULT:
2066 case FILE_ACL_ACCESS:
2067 case FILE_ACL_APPLEEXTENDED:
2068 case FILE_FLAGS_USER_NODUMP:
2069 case FILE_FLAGS_USER_IMMUTABLE:
2070 case FILE_FLAGS_USER_APPEND:
2071 case FILE_FLAGS_USER_OPAQUE:
2072 case FILE_FLAGS_USER_NOUNLINK:
2073 case FILE_FLAGS_SYS_ARCHIVED:
2074 case FILE_FLAGS_SYS_IMMUTABLE:
2075 case FILE_FLAGS_SYS_APPEND:
2076 case FILE_FLAGS_SYS_NOUNLINK:
2077 case FILE_FLAGS_SYS_SNAPSHOT:
2078 case FILE_EXT2_SecureDeletion:
2079 case FILE_EXT2_Undelete:
2080 case FILE_EXT2_Compress:
2081 case FILE_EXT2_Synchronous:
2082 case FILE_EXT2_Immutable:
2083 case FILE_EXT2_AppendOnly:
2084 case FILE_EXT2_NoDump:
2085 case FILE_EXT2_NoAtime:
2086 case FILE_EXT2_CompDirty:
2087 case FILE_EXT2_CompBlock:
2088 case FILE_EXT2_NoCompBlock:
2089 case FILE_EXT2_CompError:
2090 case FILE_EXT2_BTree:
2091 case FILE_EXT2_HashIndexed:
2092 case FILE_EXT2_iMagic:
2093 case FILE_EXT2_Journaled:
2094 case FILE_EXT2_NoTail:
2095 case FILE_EXT2_DirSync:
2096 case FILE_EXT2_TopDir:
2097 case FILE_EXT2_Reserved:
2099 unknowntag_start(xar, name);
2105 xml_end(void *userData, const char *name)
2107 struct archive_read *a;
2110 a = (struct archive_read *)userData;
2111 xar = (struct xar *)(a->format->data);
2114 fprintf(stderr, "xml_end:[%s]\n", name);
2116 switch (xar->xmlsts) {
2120 if (strcmp(name, "xar") == 0)
2124 if (strcmp(name, "toc") == 0)
2127 case TOC_CREATION_TIME:
2128 if (strcmp(name, "creation-time") == 0)
2132 if (strcmp(name, "checksum") == 0)
2135 case TOC_CHECKSUM_OFFSET:
2136 if (strcmp(name, "offset") == 0)
2137 xar->xmlsts = TOC_CHECKSUM;
2139 case TOC_CHECKSUM_SIZE:
2140 if (strcmp(name, "size") == 0)
2141 xar->xmlsts = TOC_CHECKSUM;
2144 if (strcmp(name, "file") == 0) {
2145 if (xar->file->parent != NULL &&
2146 ((xar->file->mode & AE_IFMT) == AE_IFDIR))
2147 xar->file->parent->subdirs++;
2148 xar->file = xar->file->parent;
2149 if (xar->file == NULL)
2154 if (strcmp(name, "data") == 0)
2155 xar->xmlsts = TOC_FILE;
2157 case FILE_DATA_LENGTH:
2158 if (strcmp(name, "length") == 0)
2159 xar->xmlsts = FILE_DATA;
2161 case FILE_DATA_OFFSET:
2162 if (strcmp(name, "offset") == 0)
2163 xar->xmlsts = FILE_DATA;
2165 case FILE_DATA_SIZE:
2166 if (strcmp(name, "size") == 0)
2167 xar->xmlsts = FILE_DATA;
2169 case FILE_DATA_ENCODING:
2170 if (strcmp(name, "encoding") == 0)
2171 xar->xmlsts = FILE_DATA;
2173 case FILE_DATA_A_CHECKSUM:
2174 if (strcmp(name, "archived-checksum") == 0)
2175 xar->xmlsts = FILE_DATA;
2177 case FILE_DATA_E_CHECKSUM:
2178 if (strcmp(name, "extracted-checksum") == 0)
2179 xar->xmlsts = FILE_DATA;
2181 case FILE_DATA_CONTENT:
2182 if (strcmp(name, "content") == 0)
2183 xar->xmlsts = FILE_DATA;
2186 if (strcmp(name, "ea") == 0) {
2187 xar->xmlsts = TOC_FILE;
2191 case FILE_EA_LENGTH:
2192 if (strcmp(name, "length") == 0)
2193 xar->xmlsts = FILE_EA;
2195 case FILE_EA_OFFSET:
2196 if (strcmp(name, "offset") == 0)
2197 xar->xmlsts = FILE_EA;
2200 if (strcmp(name, "size") == 0)
2201 xar->xmlsts = FILE_EA;
2203 case FILE_EA_ENCODING:
2204 if (strcmp(name, "encoding") == 0)
2205 xar->xmlsts = FILE_EA;
2207 case FILE_EA_A_CHECKSUM:
2208 if (strcmp(name, "archived-checksum") == 0)
2209 xar->xmlsts = FILE_EA;
2211 case FILE_EA_E_CHECKSUM:
2212 if (strcmp(name, "extracted-checksum") == 0)
2213 xar->xmlsts = FILE_EA;
2216 if (strcmp(name, "name") == 0)
2217 xar->xmlsts = FILE_EA;
2219 case FILE_EA_FSTYPE:
2220 if (strcmp(name, "fstype") == 0)
2221 xar->xmlsts = FILE_EA;
2224 if (strcmp(name, "ctime") == 0)
2225 xar->xmlsts = TOC_FILE;
2228 if (strcmp(name, "mtime") == 0)
2229 xar->xmlsts = TOC_FILE;
2232 if (strcmp(name, "atime") == 0)
2233 xar->xmlsts = TOC_FILE;
2236 if (strcmp(name, "group") == 0)
2237 xar->xmlsts = TOC_FILE;
2240 if (strcmp(name, "gid") == 0)
2241 xar->xmlsts = TOC_FILE;
2244 if (strcmp(name, "user") == 0)
2245 xar->xmlsts = TOC_FILE;
2248 if (strcmp(name, "uid") == 0)
2249 xar->xmlsts = TOC_FILE;
2252 if (strcmp(name, "mode") == 0)
2253 xar->xmlsts = TOC_FILE;
2256 if (strcmp(name, "device") == 0)
2257 xar->xmlsts = TOC_FILE;
2259 case FILE_DEVICE_MAJOR:
2260 if (strcmp(name, "major") == 0)
2261 xar->xmlsts = FILE_DEVICE;
2263 case FILE_DEVICE_MINOR:
2264 if (strcmp(name, "minor") == 0)
2265 xar->xmlsts = FILE_DEVICE;
2268 if (strcmp(name, "deviceno") == 0)
2269 xar->xmlsts = TOC_FILE;
2272 if (strcmp(name, "inode") == 0)
2273 xar->xmlsts = TOC_FILE;
2276 if (strcmp(name, "link") == 0)
2277 xar->xmlsts = TOC_FILE;
2280 if (strcmp(name, "type") == 0)
2281 xar->xmlsts = TOC_FILE;
2284 if (strcmp(name, "name") == 0)
2285 xar->xmlsts = TOC_FILE;
2288 if (strcmp(name, "acl") == 0)
2289 xar->xmlsts = TOC_FILE;
2291 case FILE_ACL_DEFAULT:
2292 if (strcmp(name, "default") == 0)
2293 xar->xmlsts = FILE_ACL;
2295 case FILE_ACL_ACCESS:
2296 if (strcmp(name, "access") == 0)
2297 xar->xmlsts = FILE_ACL;
2299 case FILE_ACL_APPLEEXTENDED:
2300 if (strcmp(name, "appleextended") == 0)
2301 xar->xmlsts = FILE_ACL;
2304 if (strcmp(name, "flags") == 0)
2305 xar->xmlsts = TOC_FILE;
2307 case FILE_FLAGS_USER_NODUMP:
2308 if (strcmp(name, "UserNoDump") == 0)
2309 xar->xmlsts = FILE_FLAGS;
2311 case FILE_FLAGS_USER_IMMUTABLE:
2312 if (strcmp(name, "UserImmutable") == 0)
2313 xar->xmlsts = FILE_FLAGS;
2315 case FILE_FLAGS_USER_APPEND:
2316 if (strcmp(name, "UserAppend") == 0)
2317 xar->xmlsts = FILE_FLAGS;
2319 case FILE_FLAGS_USER_OPAQUE:
2320 if (strcmp(name, "UserOpaque") == 0)
2321 xar->xmlsts = FILE_FLAGS;
2323 case FILE_FLAGS_USER_NOUNLINK:
2324 if (strcmp(name, "UserNoUnlink") == 0)
2325 xar->xmlsts = FILE_FLAGS;
2327 case FILE_FLAGS_SYS_ARCHIVED:
2328 if (strcmp(name, "SystemArchived") == 0)
2329 xar->xmlsts = FILE_FLAGS;
2331 case FILE_FLAGS_SYS_IMMUTABLE:
2332 if (strcmp(name, "SystemImmutable") == 0)
2333 xar->xmlsts = FILE_FLAGS;
2335 case FILE_FLAGS_SYS_APPEND:
2336 if (strcmp(name, "SystemAppend") == 0)
2337 xar->xmlsts = FILE_FLAGS;
2339 case FILE_FLAGS_SYS_NOUNLINK:
2340 if (strcmp(name, "SystemNoUnlink") == 0)
2341 xar->xmlsts = FILE_FLAGS;
2343 case FILE_FLAGS_SYS_SNAPSHOT:
2344 if (strcmp(name, "SystemSnapshot") == 0)
2345 xar->xmlsts = FILE_FLAGS;
2348 if (strcmp(name, "ext2") == 0)
2349 xar->xmlsts = TOC_FILE;
2351 case FILE_EXT2_SecureDeletion:
2352 if (strcmp(name, "SecureDeletion") == 0)
2353 xar->xmlsts = FILE_EXT2;
2355 case FILE_EXT2_Undelete:
2356 if (strcmp(name, "Undelete") == 0)
2357 xar->xmlsts = FILE_EXT2;
2359 case FILE_EXT2_Compress:
2360 if (strcmp(name, "Compress") == 0)
2361 xar->xmlsts = FILE_EXT2;
2363 case FILE_EXT2_Synchronous:
2364 if (strcmp(name, "Synchronous") == 0)
2365 xar->xmlsts = FILE_EXT2;
2367 case FILE_EXT2_Immutable:
2368 if (strcmp(name, "Immutable") == 0)
2369 xar->xmlsts = FILE_EXT2;
2371 case FILE_EXT2_AppendOnly:
2372 if (strcmp(name, "AppendOnly") == 0)
2373 xar->xmlsts = FILE_EXT2;
2375 case FILE_EXT2_NoDump:
2376 if (strcmp(name, "NoDump") == 0)
2377 xar->xmlsts = FILE_EXT2;
2379 case FILE_EXT2_NoAtime:
2380 if (strcmp(name, "NoAtime") == 0)
2381 xar->xmlsts = FILE_EXT2;
2383 case FILE_EXT2_CompDirty:
2384 if (strcmp(name, "CompDirty") == 0)
2385 xar->xmlsts = FILE_EXT2;
2387 case FILE_EXT2_CompBlock:
2388 if (strcmp(name, "CompBlock") == 0)
2389 xar->xmlsts = FILE_EXT2;
2391 case FILE_EXT2_NoCompBlock:
2392 if (strcmp(name, "NoCompBlock") == 0)
2393 xar->xmlsts = FILE_EXT2;
2395 case FILE_EXT2_CompError:
2396 if (strcmp(name, "CompError") == 0)
2397 xar->xmlsts = FILE_EXT2;
2399 case FILE_EXT2_BTree:
2400 if (strcmp(name, "BTree") == 0)
2401 xar->xmlsts = FILE_EXT2;
2403 case FILE_EXT2_HashIndexed:
2404 if (strcmp(name, "HashIndexed") == 0)
2405 xar->xmlsts = FILE_EXT2;
2407 case FILE_EXT2_iMagic:
2408 if (strcmp(name, "iMagic") == 0)
2409 xar->xmlsts = FILE_EXT2;
2411 case FILE_EXT2_Journaled:
2412 if (strcmp(name, "Journaled") == 0)
2413 xar->xmlsts = FILE_EXT2;
2415 case FILE_EXT2_NoTail:
2416 if (strcmp(name, "NoTail") == 0)
2417 xar->xmlsts = FILE_EXT2;
2419 case FILE_EXT2_DirSync:
2420 if (strcmp(name, "DirSync") == 0)
2421 xar->xmlsts = FILE_EXT2;
2423 case FILE_EXT2_TopDir:
2424 if (strcmp(name, "TopDir") == 0)
2425 xar->xmlsts = FILE_EXT2;
2427 case FILE_EXT2_Reserved:
2428 if (strcmp(name, "Reserved") == 0)
2429 xar->xmlsts = FILE_EXT2;
2432 unknowntag_end(xar, name);
2437 static const int base64[256] = {
2438 -1, -1, -1, -1, -1, -1, -1, -1,
2439 -1, -1, -1, -1, -1, -1, -1, -1, /* 00 - 0F */
2440 -1, -1, -1, -1, -1, -1, -1, -1,
2441 -1, -1, -1, -1, -1, -1, -1, -1, /* 10 - 1F */
2442 -1, -1, -1, -1, -1, -1, -1, -1,
2443 -1, -1, -1, 62, -1, -1, -1, 63, /* 20 - 2F */
2444 52, 53, 54, 55, 56, 57, 58, 59,
2445 60, 61, -1, -1, -1, -1, -1, -1, /* 30 - 3F */
2446 -1, 0, 1, 2, 3, 4, 5, 6,
2447 7, 8, 9, 10, 11, 12, 13, 14, /* 40 - 4F */
2448 15, 16, 17, 18, 19, 20, 21, 22,
2449 23, 24, 25, -1, -1, -1, -1, -1, /* 50 - 5F */
2450 -1, 26, 27, 28, 29, 30, 31, 32,
2451 33, 34, 35, 36, 37, 38, 39, 40, /* 60 - 6F */
2452 41, 42, 43, 44, 45, 46, 47, 48,
2453 49, 50, 51, -1, -1, -1, -1, -1, /* 70 - 7F */
2454 -1, -1, -1, -1, -1, -1, -1, -1,
2455 -1, -1, -1, -1, -1, -1, -1, -1, /* 80 - 8F */
2456 -1, -1, -1, -1, -1, -1, -1, -1,
2457 -1, -1, -1, -1, -1, -1, -1, -1, /* 90 - 9F */
2458 -1, -1, -1, -1, -1, -1, -1, -1,
2459 -1, -1, -1, -1, -1, -1, -1, -1, /* A0 - AF */
2460 -1, -1, -1, -1, -1, -1, -1, -1,
2461 -1, -1, -1, -1, -1, -1, -1, -1, /* B0 - BF */
2462 -1, -1, -1, -1, -1, -1, -1, -1,
2463 -1, -1, -1, -1, -1, -1, -1, -1, /* C0 - CF */
2464 -1, -1, -1, -1, -1, -1, -1, -1,
2465 -1, -1, -1, -1, -1, -1, -1, -1, /* D0 - DF */
2466 -1, -1, -1, -1, -1, -1, -1, -1,
2467 -1, -1, -1, -1, -1, -1, -1, -1, /* E0 - EF */
2468 -1, -1, -1, -1, -1, -1, -1, -1,
2469 -1, -1, -1, -1, -1, -1, -1, -1, /* F0 - FF */
2473 strappend_base64(struct archive_string *as, const char *s, size_t l)
2475 unsigned char buff[256];
2477 const unsigned char *b;
2482 b = (const unsigned char *)s;
2487 if (base64[b[0]] < 0 || base64[b[1]] < 0)
2489 n = base64[*b++] << 18;
2490 n |= base64[*b++] << 12;
2498 n |= base64[*b++] << 6;
2499 *out++ = (n >> 8) & 0xFF;
2511 if (len+3 >= sizeof(buff)) {
2512 archive_strncat(as, (const char *)buff, len);
2518 archive_strncat(as, (const char *)buff, len);
2522 xml_data(void *userData, const char *s, int len)
2524 struct archive_read *a;
2527 a = (struct archive_read *)userData;
2528 xar = (struct xar *)(a->format->data);
2533 if (len > (int)sizeof(buff)-1)
2534 len = (int)sizeof(buff)-1;
2535 memcpy(buff, s, len);
2537 fprintf(stderr, "\tlen=%d:\"%s\"\n", len, buff);
2540 switch (xar->xmlsts) {
2541 case TOC_CHECKSUM_OFFSET:
2542 xar->toc_chksum_offset = atol10(s, len);
2544 case TOC_CHECKSUM_SIZE:
2545 xar->toc_chksum_size = atol10(s, len);
2550 if (xar->file == NULL)
2553 switch (xar->xmlsts) {
2555 if (xar->file->parent != NULL) {
2556 archive_string_concat(&(xar->file->pathname),
2557 &(xar->file->parent->pathname));
2558 archive_strappend_char(&(xar->file->pathname), '/');
2560 xar->file->has |= HAS_PATHNAME;
2561 if (xar->base64text)
2562 strappend_base64(&(xar->file->pathname), s, len);
2564 archive_strncat(&(xar->file->pathname), s, len);
2567 xar->file->has |= HAS_SYMLINK;
2568 archive_strncpy(&(xar->file->symlink), s, len);
2571 if (strncmp("file", s, len) == 0 ||
2572 strncmp("hardlink", s, len) == 0)
2574 (xar->file->mode & ~AE_IFMT) | AE_IFREG;
2575 if (strncmp("directory", s, len) == 0)
2577 (xar->file->mode & ~AE_IFMT) | AE_IFDIR;
2578 if (strncmp("symlink", s, len) == 0)
2580 (xar->file->mode & ~AE_IFMT) | AE_IFLNK;
2581 if (strncmp("character special", s, len) == 0)
2583 (xar->file->mode & ~AE_IFMT) | AE_IFCHR;
2584 if (strncmp("block special", s, len) == 0)
2586 (xar->file->mode & ~AE_IFMT) | AE_IFBLK;
2587 if (strncmp("socket", s, len) == 0)
2589 (xar->file->mode & ~AE_IFMT) | AE_IFSOCK;
2590 if (strncmp("fifo", s, len) == 0)
2592 (xar->file->mode & ~AE_IFMT) | AE_IFIFO;
2593 xar->file->has |= HAS_TYPE;
2596 xar->file->has |= HAS_INO;
2597 xar->file->ino64 = atol10(s, len);
2599 case FILE_DEVICE_MAJOR:
2600 xar->file->has |= HAS_DEVMAJOR;
2601 xar->file->devmajor = (dev_t)atol10(s, len);
2603 case FILE_DEVICE_MINOR:
2604 xar->file->has |= HAS_DEVMINOR;
2605 xar->file->devminor = (dev_t)atol10(s, len);
2608 xar->file->has |= HAS_DEV;
2609 xar->file->dev = (dev_t)atol10(s, len);
2612 xar->file->has |= HAS_MODE;
2614 (xar->file->mode & AE_IFMT) |
2615 (atol8(s, len) & ~AE_IFMT);
2618 xar->file->has |= HAS_GID;
2619 archive_strncpy(&(xar->file->gname), s, len);
2622 xar->file->has |= HAS_GID;
2623 xar->file->gid = atol10(s, len);
2626 xar->file->has |= HAS_UID;
2627 archive_strncpy(&(xar->file->uname), s, len);
2630 xar->file->has |= HAS_UID;
2631 xar->file->uid = atol10(s, len);
2634 xar->file->has |= HAS_TIME;
2635 xar->file->ctime = parse_time(s, len);
2638 xar->file->has |= HAS_TIME;
2639 xar->file->mtime = parse_time(s, len);
2642 xar->file->has |= HAS_TIME;
2643 xar->file->atime = parse_time(s, len);
2645 case FILE_DATA_LENGTH:
2646 xar->file->has |= HAS_DATA;
2647 xar->file->length = atol10(s, len);
2649 case FILE_DATA_OFFSET:
2650 xar->file->has |= HAS_DATA;
2651 xar->file->offset = atol10(s, len);
2653 case FILE_DATA_SIZE:
2654 xar->file->has |= HAS_DATA;
2655 xar->file->size = atol10(s, len);
2657 case FILE_DATA_A_CHECKSUM:
2658 xar->file->a_sum.len = atohex(xar->file->a_sum.val,
2659 sizeof(xar->file->a_sum.val), s, len);
2661 case FILE_DATA_E_CHECKSUM:
2662 xar->file->e_sum.len = atohex(xar->file->e_sum.val,
2663 sizeof(xar->file->e_sum.val), s, len);
2665 case FILE_EA_LENGTH:
2666 xar->file->has |= HAS_XATTR;
2667 xar->xattr->length = atol10(s, len);
2669 case FILE_EA_OFFSET:
2670 xar->file->has |= HAS_XATTR;
2671 xar->xattr->offset = atol10(s, len);
2674 xar->file->has |= HAS_XATTR;
2675 xar->xattr->size = atol10(s, len);
2677 case FILE_EA_A_CHECKSUM:
2678 xar->file->has |= HAS_XATTR;
2679 xar->xattr->a_sum.len = atohex(xar->xattr->a_sum.val,
2680 sizeof(xar->xattr->a_sum.val), s, len);
2682 case FILE_EA_E_CHECKSUM:
2683 xar->file->has |= HAS_XATTR;
2684 xar->xattr->e_sum.len = atohex(xar->xattr->e_sum.val,
2685 sizeof(xar->xattr->e_sum.val), s, len);
2688 xar->file->has |= HAS_XATTR;
2689 archive_strncpy(&(xar->xattr->name), s, len);
2691 case FILE_EA_FSTYPE:
2692 xar->file->has |= HAS_XATTR;
2693 archive_strncpy(&(xar->xattr->fstype), s, len);
2696 case FILE_ACL_DEFAULT:
2697 case FILE_ACL_ACCESS:
2698 case FILE_ACL_APPLEEXTENDED:
2699 xar->file->has |= HAS_ACL;
2705 case TOC_CREATION_TIME:
2707 case TOC_CHECKSUM_OFFSET:
2708 case TOC_CHECKSUM_SIZE:
2711 case FILE_DATA_ENCODING:
2712 case FILE_DATA_CONTENT:
2715 case FILE_EA_ENCODING:
2718 case FILE_FLAGS_USER_NODUMP:
2719 case FILE_FLAGS_USER_IMMUTABLE:
2720 case FILE_FLAGS_USER_APPEND:
2721 case FILE_FLAGS_USER_OPAQUE:
2722 case FILE_FLAGS_USER_NOUNLINK:
2723 case FILE_FLAGS_SYS_ARCHIVED:
2724 case FILE_FLAGS_SYS_IMMUTABLE:
2725 case FILE_FLAGS_SYS_APPEND:
2726 case FILE_FLAGS_SYS_NOUNLINK:
2727 case FILE_FLAGS_SYS_SNAPSHOT:
2729 case FILE_EXT2_SecureDeletion:
2730 case FILE_EXT2_Undelete:
2731 case FILE_EXT2_Compress:
2732 case FILE_EXT2_Synchronous:
2733 case FILE_EXT2_Immutable:
2734 case FILE_EXT2_AppendOnly:
2735 case FILE_EXT2_NoDump:
2736 case FILE_EXT2_NoAtime:
2737 case FILE_EXT2_CompDirty:
2738 case FILE_EXT2_CompBlock:
2739 case FILE_EXT2_NoCompBlock:
2740 case FILE_EXT2_CompError:
2741 case FILE_EXT2_BTree:
2742 case FILE_EXT2_HashIndexed:
2743 case FILE_EXT2_iMagic:
2744 case FILE_EXT2_Journaled:
2745 case FILE_EXT2_NoTail:
2746 case FILE_EXT2_DirSync:
2747 case FILE_EXT2_TopDir:
2748 case FILE_EXT2_Reserved:
2758 xml_parse_file_flags(struct xar *xar, const char *name)
2760 const char *flag = NULL;
2762 if (strcmp(name, "UserNoDump") == 0) {
2763 xar->xmlsts = FILE_FLAGS_USER_NODUMP;
2766 else if (strcmp(name, "UserImmutable") == 0) {
2767 xar->xmlsts = FILE_FLAGS_USER_IMMUTABLE;
2768 flag = "uimmutable";
2770 else if (strcmp(name, "UserAppend") == 0) {
2771 xar->xmlsts = FILE_FLAGS_USER_APPEND;
2774 else if (strcmp(name, "UserOpaque") == 0) {
2775 xar->xmlsts = FILE_FLAGS_USER_OPAQUE;
2778 else if (strcmp(name, "UserNoUnlink") == 0) {
2779 xar->xmlsts = FILE_FLAGS_USER_NOUNLINK;
2782 else if (strcmp(name, "SystemArchived") == 0) {
2783 xar->xmlsts = FILE_FLAGS_SYS_ARCHIVED;
2786 else if (strcmp(name, "SystemImmutable") == 0) {
2787 xar->xmlsts = FILE_FLAGS_SYS_IMMUTABLE;
2788 flag = "simmutable";
2790 else if (strcmp(name, "SystemAppend") == 0) {
2791 xar->xmlsts = FILE_FLAGS_SYS_APPEND;
2794 else if (strcmp(name, "SystemNoUnlink") == 0) {
2795 xar->xmlsts = FILE_FLAGS_SYS_NOUNLINK;
2798 else if (strcmp(name, "SystemSnapshot") == 0) {
2799 xar->xmlsts = FILE_FLAGS_SYS_SNAPSHOT;
2805 xar->file->has |= HAS_FFLAGS;
2806 if (archive_strlen(&(xar->file->fflags_text)) > 0)
2807 archive_strappend_char(&(xar->file->fflags_text), ',');
2808 archive_strcat(&(xar->file->fflags_text), flag);
2816 xml_parse_file_ext2(struct xar *xar, const char *name)
2818 const char *flag = NULL;
2820 if (strcmp(name, "SecureDeletion") == 0) {
2821 xar->xmlsts = FILE_EXT2_SecureDeletion;
2822 flag = "securedeletion";
2824 else if (strcmp(name, "Undelete") == 0) {
2825 xar->xmlsts = FILE_EXT2_Undelete;
2828 else if (strcmp(name, "Compress") == 0) {
2829 xar->xmlsts = FILE_EXT2_Compress;
2832 else if (strcmp(name, "Synchronous") == 0) {
2833 xar->xmlsts = FILE_EXT2_Synchronous;
2836 else if (strcmp(name, "Immutable") == 0) {
2837 xar->xmlsts = FILE_EXT2_Immutable;
2838 flag = "simmutable";
2840 else if (strcmp(name, "AppendOnly") == 0) {
2841 xar->xmlsts = FILE_EXT2_AppendOnly;
2844 else if (strcmp(name, "NoDump") == 0) {
2845 xar->xmlsts = FILE_EXT2_NoDump;
2848 else if (strcmp(name, "NoAtime") == 0) {
2849 xar->xmlsts = FILE_EXT2_NoAtime;
2852 else if (strcmp(name, "CompDirty") == 0) {
2853 xar->xmlsts = FILE_EXT2_CompDirty;
2856 else if (strcmp(name, "CompBlock") == 0) {
2857 xar->xmlsts = FILE_EXT2_CompBlock;
2860 else if (strcmp(name, "NoCompBlock") == 0) {
2861 xar->xmlsts = FILE_EXT2_NoCompBlock;
2862 flag = "nocomprblk";
2864 else if (strcmp(name, "CompError") == 0) {
2865 xar->xmlsts = FILE_EXT2_CompError;
2868 else if (strcmp(name, "BTree") == 0) {
2869 xar->xmlsts = FILE_EXT2_BTree;
2872 else if (strcmp(name, "HashIndexed") == 0) {
2873 xar->xmlsts = FILE_EXT2_HashIndexed;
2876 else if (strcmp(name, "iMagic") == 0) {
2877 xar->xmlsts = FILE_EXT2_iMagic;
2880 else if (strcmp(name, "Journaled") == 0) {
2881 xar->xmlsts = FILE_EXT2_Journaled;
2884 else if (strcmp(name, "NoTail") == 0) {
2885 xar->xmlsts = FILE_EXT2_NoTail;
2888 else if (strcmp(name, "DirSync") == 0) {
2889 xar->xmlsts = FILE_EXT2_DirSync;
2892 else if (strcmp(name, "TopDir") == 0) {
2893 xar->xmlsts = FILE_EXT2_TopDir;
2896 else if (strcmp(name, "Reserved") == 0) {
2897 xar->xmlsts = FILE_EXT2_Reserved;
2903 if (archive_strlen(&(xar->file->fflags_text)) > 0)
2904 archive_strappend_char(&(xar->file->fflags_text), ',');
2905 archive_strcat(&(xar->file->fflags_text), flag);
2909 #ifdef HAVE_LIBXML_XMLREADER_H
2912 xml2_xmlattr_setup(struct xmlattr_list *list, xmlTextReaderPtr reader)
2914 struct xmlattr *attr;
2918 list->last = &(list->first);
2919 r = xmlTextReaderMoveToFirstAttribute(reader);
2921 attr = malloc(sizeof*(attr));
2923 __archive_errx(1, "Out of memory");
2924 attr->name = strdup(
2925 (const char *)xmlTextReaderConstLocalName(reader));
2926 if (attr->name == NULL)
2927 __archive_errx(1, "Out of memory");
2928 attr->value = strdup(
2929 (const char *)xmlTextReaderConstValue(reader));
2930 if (attr->value == NULL)
2931 __archive_errx(1, "Out of memory");
2934 list->last = &(attr->next);
2935 r = xmlTextReaderMoveToNextAttribute(reader);
2941 xml2_read_cb(void *context, char *buffer, int len)
2943 struct archive_read *a;
2950 a = (struct archive_read *)context;
2951 xar = (struct xar *)(a->format->data);
2953 if (xar->toc_remaining <= 0)
2957 r = rd_contents(a, &d, &outbytes, &used, xar->toc_remaining);
2958 if (r != ARCHIVE_OK)
2960 __archive_read_consume(a, used);
2961 xar->toc_remaining -= used;
2962 xar->offset += used;
2963 xar->toc_total += outbytes;
2964 PRINT_TOC(buffer, len);
2966 return ((int)outbytes);
2970 xml2_close_cb(void *context)
2973 (void)context; /* UNUSED */
2978 xml2_error_hdr(void *arg, const char *msg, xmlParserSeverities severity,
2979 xmlTextReaderLocatorPtr locator)
2981 struct archive_read *a;
2983 (void)locator; /* UNUSED */
2984 a = (struct archive_read *)arg;
2986 case XML_PARSER_SEVERITY_VALIDITY_WARNING:
2987 case XML_PARSER_SEVERITY_WARNING:
2988 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2989 "XML Parsing error: %s", msg);
2991 case XML_PARSER_SEVERITY_VALIDITY_ERROR:
2992 case XML_PARSER_SEVERITY_ERROR:
2993 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
2994 "XML Parsing error: %s", msg);
3000 xml2_read_toc(struct archive_read *a)
3002 xmlTextReaderPtr reader;
3003 struct xmlattr_list list;
3006 reader = xmlReaderForIO(xml2_read_cb, xml2_close_cb, a, NULL, NULL, 0);
3007 if (reader == NULL) {
3008 archive_set_error(&a->archive, ENOMEM,
3009 "Couldn't allocate memory for xml parser");
3010 return (ARCHIVE_FATAL);
3012 xmlTextReaderSetErrorHandler(reader, xml2_error_hdr, a);
3014 while ((r = xmlTextReaderRead(reader)) == 1) {
3015 const char *name, *value;
3018 type = xmlTextReaderNodeType(reader);
3019 name = (const char *)xmlTextReaderConstLocalName(reader);
3021 case XML_READER_TYPE_ELEMENT:
3022 empty = xmlTextReaderIsEmptyElement(reader);
3023 r = xml2_xmlattr_setup(&list, reader);
3025 xml_start(a, name, &list);
3026 xmlattr_cleanup(&list);
3031 case XML_READER_TYPE_END_ELEMENT:
3034 case XML_READER_TYPE_TEXT:
3035 value = (const char *)xmlTextReaderConstValue(reader);
3036 xml_data(a, value, strlen(value));
3038 case XML_READER_TYPE_SIGNIFICANT_WHITESPACE:
3045 xmlFreeTextReader(reader);
3048 return ((r == 0)?ARCHIVE_OK:ARCHIVE_FATAL);
3051 #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H)
3054 expat_xmlattr_setup(struct xmlattr_list *list, const XML_Char **atts)
3056 struct xmlattr *attr;
3059 list->last = &(list->first);
3062 while (atts[0] != NULL && atts[1] != NULL) {
3063 attr = malloc(sizeof*(attr));
3065 __archive_errx(1, "Out of memory");
3066 attr->name = strdup(atts[0]);
3067 if (attr->name == NULL)
3068 __archive_errx(1, "Out of memory");
3069 attr->value = strdup(atts[1]);
3070 if (attr->value == NULL)
3071 __archive_errx(1, "Out of memory");
3074 list->last = &(attr->next);
3080 expat_start_cb(void *userData, const XML_Char *name, const XML_Char **atts)
3082 struct xmlattr_list list;
3084 expat_xmlattr_setup(&list, atts);
3085 xml_start(userData, (const char *)name, &list);
3086 xmlattr_cleanup(&list);
3090 expat_end_cb(void *userData, const XML_Char *name)
3092 xml_end(userData, (const char *)name);
3096 expat_data_cb(void *userData, const XML_Char *s, int len)
3098 xml_data(userData, s, len);
3102 expat_read_toc(struct archive_read *a)
3107 xar = (struct xar *)(a->format->data);
3109 /* Initialize XML Parser library. */
3110 parser = XML_ParserCreate(NULL);
3111 if (parser == NULL) {
3112 archive_set_error(&a->archive, ENOMEM,
3113 "Couldn't allocate memory for xml parser");
3114 return (ARCHIVE_FATAL);
3116 XML_SetUserData(parser, a);
3117 XML_SetElementHandler(parser, expat_start_cb, expat_end_cb);
3118 XML_SetCharacterDataHandler(parser, expat_data_cb);
3121 while (xar->toc_remaining) {
3129 r = rd_contents(a, &d, &outbytes, &used, xar->toc_remaining);
3130 if (r != ARCHIVE_OK)
3132 __archive_read_consume(a, used);
3133 xar->toc_remaining -= used;
3134 xar->offset += used;
3135 xar->toc_total += outbytes;
3136 PRINT_TOC(d, outbytes);
3138 xr = XML_Parse(parser, d, outbytes, xar->toc_remaining == 0);
3139 if (xr == XML_STATUS_ERROR) {
3140 XML_ParserFree(parser);
3141 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
3142 "XML Parsing failed");
3143 return (ARCHIVE_FATAL);
3146 XML_ParserFree(parser);
3147 return (ARCHIVE_OK);
3149 #endif /* defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H) */
3151 #endif /* Support xar format */