/src/libarchive/libarchive/archive_read_support_format_xar.c
Line | Count | Source |
1 | | /*- |
2 | | * Copyright (c) 2009 Michihiro NAKAJIMA |
3 | | * All rights reserved. |
4 | | * |
5 | | * Redistribution and use in source and binary forms, with or without |
6 | | * modification, are permitted provided that the following conditions |
7 | | * are met: |
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. |
13 | | * |
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. |
24 | | */ |
25 | | #include "archive_platform.h" |
26 | | |
27 | | #ifdef HAVE_ERRNO_H |
28 | | #include <errno.h> |
29 | | #endif |
30 | | #ifdef HAVE_STDLIB_H |
31 | | #include <stdlib.h> |
32 | | #endif |
33 | | #if HAVE_LIBXML_XMLREADER_H |
34 | | #include <libxml/xmlreader.h> |
35 | | #elif HAVE_BSDXML_H |
36 | | #include <bsdxml.h> |
37 | | #elif HAVE_EXPAT_H |
38 | | #include <expat.h> |
39 | | #elif HAVE_XMLLITE_H |
40 | | #include <objidl.h> |
41 | | #include <initguid.h> |
42 | | #include <xmllite.h> |
43 | | #endif |
44 | | #ifdef HAVE_BZLIB_H |
45 | | #include <bzlib.h> |
46 | | #endif |
47 | | #if HAVE_LZMA_H |
48 | | #include <lzma.h> |
49 | | #endif |
50 | | #ifdef HAVE_ZLIB_H |
51 | | #include <zlib.h> |
52 | | #endif |
53 | | |
54 | | #include "archive.h" |
55 | | #include "archive_digest_private.h" |
56 | | #include "archive_endian.h" |
57 | | #include "archive_entry.h" |
58 | | #include "archive_entry_locale.h" |
59 | | #include "archive_private.h" |
60 | | #include "archive_read_private.h" |
61 | | |
62 | | #if (!defined(HAVE_LIBXML_XMLREADER_H) && \ |
63 | | !defined(HAVE_BSDXML_H) && !defined(HAVE_EXPAT_H) && \ |
64 | | !defined(HAVE_XMLLITE_H)) ||\ |
65 | | !defined(HAVE_ZLIB_H) || \ |
66 | | !defined(ARCHIVE_HAS_MD5) || !defined(ARCHIVE_HAS_SHA1) |
67 | | /* |
68 | | * xar needs several external libraries. |
69 | | * o libxml2, expat or (Windows only) xmllite --- XML parser |
70 | | * o openssl or MD5/SHA1 hash function |
71 | | * o zlib |
72 | | * o bzlib2 (option) |
73 | | * o liblzma (option) |
74 | | */ |
75 | | int |
76 | | archive_read_support_format_xar(struct archive *_a) |
77 | | { |
78 | | struct archive_read *a = (struct archive_read *)_a; |
79 | | archive_check_magic(_a, ARCHIVE_READ_MAGIC, |
80 | | ARCHIVE_STATE_NEW, "archive_read_support_format_xar"); |
81 | | |
82 | | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
83 | | "Xar not supported on this platform"); |
84 | | return (ARCHIVE_WARN); |
85 | | } |
86 | | |
87 | | #else /* Support xar format */ |
88 | | |
89 | | /* #define DEBUG 1 */ |
90 | | /* #define DEBUG_PRINT_TOC 1 */ |
91 | | #if DEBUG_PRINT_TOC |
92 | | #define PRINT_TOC(d, outbytes) do { \ |
93 | | unsigned char *x = (unsigned char *)(uintptr_t)d; \ |
94 | | unsigned char c = x[outbytes-1]; \ |
95 | | x[outbytes - 1] = 0; \ |
96 | | fprintf(stderr, "%s", x); \ |
97 | | fprintf(stderr, "%c", c); \ |
98 | | x[outbytes - 1] = c; \ |
99 | | } while (0) |
100 | | #else |
101 | | #define PRINT_TOC(d, outbytes) |
102 | | #endif |
103 | | |
104 | 2.54k | #define HEADER_MAGIC 0x78617221 |
105 | 3.11k | #define HEADER_SIZE 28 |
106 | 220 | #define HEADER_VERSION 1 |
107 | 18.5k | #define CKSUM_NONE 0 |
108 | 2.47k | #define CKSUM_SHA1 1 |
109 | 521 | #define CKSUM_MD5 2 |
110 | | |
111 | 12 | #define MD5_SIZE 16 |
112 | 192 | #define SHA1_SIZE 20 |
113 | | #define MAX_SUM_SIZE 20 |
114 | | |
115 | | enum enctype { |
116 | | NONE, |
117 | | GZIP, |
118 | | BZIP2, |
119 | | LZMA, |
120 | | XZ, |
121 | | }; |
122 | | |
123 | | struct chksumval { |
124 | | int alg; |
125 | | size_t len; |
126 | | unsigned char val[MAX_SUM_SIZE]; |
127 | | }; |
128 | | |
129 | | struct chksumwork { |
130 | | int alg; |
131 | | #ifdef ARCHIVE_HAS_MD5 |
132 | | archive_md5_ctx md5ctx; |
133 | | #endif |
134 | | #ifdef ARCHIVE_HAS_SHA1 |
135 | | archive_sha1_ctx sha1ctx; |
136 | | #endif |
137 | | }; |
138 | | |
139 | | struct xattr { |
140 | | struct xattr *next; |
141 | | struct archive_string name; |
142 | | uint64_t id; |
143 | | uint64_t length; |
144 | | uint64_t offset; |
145 | | uint64_t size; |
146 | | enum enctype encoding; |
147 | | struct chksumval a_sum; |
148 | | struct chksumval e_sum; |
149 | | struct archive_string fstype; |
150 | | }; |
151 | | |
152 | | struct xar_file { |
153 | | struct xar_file *next; |
154 | | struct xar_file *hdnext; |
155 | | struct xar_file *parent; |
156 | | int subdirs; |
157 | | |
158 | | unsigned int has; |
159 | 2.58k | #define HAS_DATA 0x00001 |
160 | 0 | #define HAS_PATHNAME 0x00002 |
161 | 0 | #define HAS_SYMLINK 0x00004 |
162 | 1.25k | #define HAS_TIME 0x00008 |
163 | 1.31k | #define HAS_UID 0x00010 |
164 | 1.69k | #define HAS_GID 0x00020 |
165 | 0 | #define HAS_MODE 0x00040 |
166 | 0 | #define HAS_TYPE 0x00080 |
167 | 0 | #define HAS_DEV 0x00100 |
168 | 0 | #define HAS_DEVMAJOR 0x00200 |
169 | 0 | #define HAS_DEVMINOR 0x00400 |
170 | 0 | #define HAS_INO 0x00800 |
171 | 0 | #define HAS_FFLAGS 0x01000 |
172 | 6.85k | #define HAS_XATTR 0x02000 |
173 | 0 | #define HAS_ACL 0x04000 |
174 | 87 | #define HAS_CTIME 0x08000 |
175 | 88 | #define HAS_MTIME 0x10000 |
176 | 1.08k | #define HAS_ATIME 0x20000 |
177 | | |
178 | | uint64_t id; |
179 | | uint64_t length; |
180 | | uint64_t offset; |
181 | | uint64_t size; |
182 | | enum enctype encoding; |
183 | | struct chksumval a_sum; |
184 | | struct chksumval e_sum; |
185 | | struct archive_string pathname; |
186 | | struct archive_string symlink; |
187 | | time_t ctime; |
188 | | time_t mtime; |
189 | | time_t atime; |
190 | | struct archive_string uname; |
191 | | int64_t uid; |
192 | | struct archive_string gname; |
193 | | int64_t gid; |
194 | | mode_t mode; |
195 | | dev_t dev; |
196 | | dev_t devmajor; |
197 | | dev_t devminor; |
198 | | int64_t ino64; |
199 | | struct archive_string fflags_text; |
200 | | unsigned int link; |
201 | | unsigned int nlink; |
202 | | struct archive_string hardlink; |
203 | | struct xattr *xattr_list; |
204 | | }; |
205 | | |
206 | | struct hdlink { |
207 | | struct hdlink *next; |
208 | | |
209 | | unsigned int id; |
210 | | int cnt; |
211 | | struct xar_file *files; |
212 | | }; |
213 | | |
214 | | struct heap_queue { |
215 | | struct xar_file **files; |
216 | | int allocated; |
217 | | int used; |
218 | | }; |
219 | | |
220 | | enum xmlstatus { |
221 | | INIT, |
222 | | XAR, |
223 | | TOC, |
224 | | TOC_CREATION_TIME, |
225 | | TOC_CHECKSUM, |
226 | | TOC_CHECKSUM_OFFSET, |
227 | | TOC_CHECKSUM_SIZE, |
228 | | TOC_FILE, |
229 | | FILE_DATA, |
230 | | FILE_DATA_LENGTH, |
231 | | FILE_DATA_OFFSET, |
232 | | FILE_DATA_SIZE, |
233 | | FILE_DATA_ENCODING, |
234 | | FILE_DATA_A_CHECKSUM, |
235 | | FILE_DATA_E_CHECKSUM, |
236 | | FILE_DATA_CONTENT, |
237 | | FILE_EA, |
238 | | FILE_EA_LENGTH, |
239 | | FILE_EA_OFFSET, |
240 | | FILE_EA_SIZE, |
241 | | FILE_EA_ENCODING, |
242 | | FILE_EA_A_CHECKSUM, |
243 | | FILE_EA_E_CHECKSUM, |
244 | | FILE_EA_NAME, |
245 | | FILE_EA_FSTYPE, |
246 | | FILE_CTIME, |
247 | | FILE_MTIME, |
248 | | FILE_ATIME, |
249 | | FILE_GROUP, |
250 | | FILE_GID, |
251 | | FILE_USER, |
252 | | FILE_UID, |
253 | | FILE_MODE, |
254 | | FILE_DEVICE, |
255 | | FILE_DEVICE_MAJOR, |
256 | | FILE_DEVICE_MINOR, |
257 | | FILE_DEVICENO, |
258 | | FILE_INODE, |
259 | | FILE_LINK, |
260 | | FILE_TYPE, |
261 | | FILE_NAME, |
262 | | FILE_ACL, |
263 | | FILE_ACL_DEFAULT, |
264 | | FILE_ACL_ACCESS, |
265 | | FILE_ACL_APPLEEXTENDED, |
266 | | /* BSD file flags. */ |
267 | | FILE_FLAGS, |
268 | | FILE_FLAGS_USER_NODUMP, |
269 | | FILE_FLAGS_USER_IMMUTABLE, |
270 | | FILE_FLAGS_USER_APPEND, |
271 | | FILE_FLAGS_USER_OPAQUE, |
272 | | FILE_FLAGS_USER_NOUNLINK, |
273 | | FILE_FLAGS_SYS_ARCHIVED, |
274 | | FILE_FLAGS_SYS_IMMUTABLE, |
275 | | FILE_FLAGS_SYS_APPEND, |
276 | | FILE_FLAGS_SYS_NOUNLINK, |
277 | | FILE_FLAGS_SYS_SNAPSHOT, |
278 | | /* Linux file flags. */ |
279 | | FILE_EXT2, |
280 | | FILE_EXT2_SecureDeletion, |
281 | | FILE_EXT2_Undelete, |
282 | | FILE_EXT2_Compress, |
283 | | FILE_EXT2_Synchronous, |
284 | | FILE_EXT2_Immutable, |
285 | | FILE_EXT2_AppendOnly, |
286 | | FILE_EXT2_NoDump, |
287 | | FILE_EXT2_NoAtime, |
288 | | FILE_EXT2_CompDirty, |
289 | | FILE_EXT2_CompBlock, |
290 | | FILE_EXT2_NoCompBlock, |
291 | | FILE_EXT2_CompError, |
292 | | FILE_EXT2_BTree, |
293 | | FILE_EXT2_HashIndexed, |
294 | | FILE_EXT2_iMagic, |
295 | | FILE_EXT2_Journaled, |
296 | | FILE_EXT2_NoTail, |
297 | | FILE_EXT2_DirSync, |
298 | | FILE_EXT2_TopDir, |
299 | | FILE_EXT2_Reserved, |
300 | | UNKNOWN, |
301 | | }; |
302 | | |
303 | | struct unknown_tag { |
304 | | struct unknown_tag *next; |
305 | | struct archive_string name; |
306 | | }; |
307 | | |
308 | | struct xar { |
309 | | uint64_t offset; /* Current position in the file. */ |
310 | | int64_t total; |
311 | | uint64_t h_base; |
312 | | int end_of_file; |
313 | 0 | #define OUTBUFF_SIZE (1024 * 64) |
314 | | unsigned char *outbuff; |
315 | | |
316 | | enum xmlstatus xmlsts; |
317 | | enum xmlstatus xmlsts_unknown; |
318 | | struct unknown_tag *unknowntags; |
319 | | int base64text; |
320 | | |
321 | | /* |
322 | | * TOC |
323 | | */ |
324 | | uint64_t toc_remaining; |
325 | | uint64_t toc_total; |
326 | | uint64_t toc_chksum_offset; |
327 | | uint64_t toc_chksum_size; |
328 | | |
329 | | /* |
330 | | * For Decoding data. |
331 | | */ |
332 | | enum enctype rd_encoding; |
333 | | z_stream stream; |
334 | | int stream_valid; |
335 | | #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR) |
336 | | bz_stream bzstream; |
337 | | int bzstream_valid; |
338 | | #endif |
339 | | #if HAVE_LZMA_H && HAVE_LIBLZMA |
340 | | lzma_stream lzstream; |
341 | | int lzstream_valid; |
342 | | #endif |
343 | | /* |
344 | | * For Checksum data. |
345 | | */ |
346 | | struct chksumwork a_sumwrk; |
347 | | struct chksumwork e_sumwrk; |
348 | | |
349 | | struct xar_file *file; /* current reading file. */ |
350 | | struct xattr *xattr; /* current reading extended attribute. */ |
351 | | struct heap_queue file_queue; |
352 | | struct xar_file *hdlink_orgs; |
353 | | struct hdlink *hdlink_list; |
354 | | |
355 | | int entry_init; |
356 | | uint64_t entry_total; |
357 | | uint64_t entry_remaining; |
358 | | size_t entry_unconsumed; |
359 | | uint64_t entry_size; |
360 | | enum enctype entry_encoding; |
361 | | struct chksumval entry_a_sum; |
362 | | struct chksumval entry_e_sum; |
363 | | |
364 | | struct archive_string_conv *sconv; |
365 | | }; |
366 | | |
367 | | struct xmlattr { |
368 | | struct xmlattr *next; |
369 | | char *name; |
370 | | char *value; |
371 | | }; |
372 | | |
373 | | struct xmlattr_list { |
374 | | struct xmlattr *first; |
375 | | struct xmlattr **last; |
376 | | }; |
377 | | |
378 | | static int xar_bid(struct archive_read *, int); |
379 | | static int xar_read_header(struct archive_read *, |
380 | | struct archive_entry *); |
381 | | static int xar_read_data(struct archive_read *, |
382 | | const void **, size_t *, int64_t *); |
383 | | static int xar_read_data_skip(struct archive_read *); |
384 | | static int xar_cleanup(struct archive_read *); |
385 | | static int move_reading_point(struct archive_read *, uint64_t); |
386 | | static int rd_contents_init(struct archive_read *, |
387 | | enum enctype, int, int); |
388 | | static int rd_contents(struct archive_read *, const void **, |
389 | | size_t *, size_t *, uint64_t); |
390 | | static int atou64(const char *, size_t, int, uint64_t *); |
391 | | static size_t atohex(unsigned char *, size_t, const char *, size_t); |
392 | | static time_t parse_time(const char *p, size_t n); |
393 | | static int heap_add_entry(struct archive_read *a, |
394 | | struct heap_queue *, struct xar_file *); |
395 | | static struct xar_file *heap_get_entry(struct heap_queue *); |
396 | | static int add_link(struct archive_read *, |
397 | | struct xar *, struct xar_file *); |
398 | | static void checksum_init(struct archive_read *, int, int); |
399 | | static void checksum_update(struct archive_read *, const void *, |
400 | | size_t, const void *, size_t); |
401 | | static int checksum_final(struct archive_read *, const void *, |
402 | | size_t, const void *, size_t); |
403 | | static void checksum_cleanup(struct archive_read *); |
404 | | static int decompression_init(struct archive_read *, enum enctype); |
405 | | static int decompress(struct archive_read *, const void **, |
406 | | size_t *, const void *, size_t *); |
407 | | static int decompression_cleanup(struct archive_read *); |
408 | | static void xmlattr_cleanup(struct xmlattr_list *); |
409 | | static int file_new(struct archive_read *, |
410 | | struct xar *, struct xmlattr_list *); |
411 | | static void file_free(struct xar_file *); |
412 | | static int xattr_new(struct archive_read *, |
413 | | struct xar *, struct xmlattr_list *); |
414 | | static void xattr_free(struct xattr *); |
415 | | static int getencoding(struct xmlattr_list *); |
416 | | static int getsumalgorithm(struct xmlattr_list *); |
417 | | static int unknowntag_start(struct archive_read *, |
418 | | struct xar *, const char *); |
419 | | static void unknowntag_end(struct xar *, const char *); |
420 | | static int xml_start(struct archive_read *, |
421 | | const char *, struct xmlattr_list *); |
422 | | static void xml_end(void *, const char *); |
423 | | static int xml_data(void *, const char *, size_t); |
424 | | static int xml_parse_file_flags(struct xar *, const char *); |
425 | | static int xml_parse_file_ext2(struct xar *, const char *); |
426 | | #if defined(HAVE_LIBXML_XMLREADER_H) |
427 | | static int xml2_xmlattr_setup(struct archive_read *, |
428 | | struct xmlattr_list *, xmlTextReaderPtr); |
429 | | static int xml2_read_cb(void *, char *, int); |
430 | | static int xml2_close_cb(void *); |
431 | | static void xml2_error_hdr(void *, const char *, xmlParserSeverities, |
432 | | xmlTextReaderLocatorPtr); |
433 | | static int xml2_read_toc(struct archive_read *); |
434 | | #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H) |
435 | | struct expat_userData { |
436 | | int state; |
437 | | struct archive_read *archive; |
438 | | }; |
439 | | static int expat_xmlattr_setup(struct archive_read *, |
440 | | struct xmlattr_list *, const XML_Char **); |
441 | | static void expat_start_cb(void *, const XML_Char *, const XML_Char **); |
442 | | static void expat_end_cb(void *, const XML_Char *); |
443 | | static void expat_data_cb(void *, const XML_Char *, int); |
444 | | static int expat_read_toc(struct archive_read *); |
445 | | #elif defined(HAVE_XMLLITE_H) |
446 | | static int xmllite_read_toc(struct archive_read *); |
447 | | #endif |
448 | | |
449 | | int |
450 | | archive_read_support_format_xar(struct archive *_a) |
451 | 2.49k | { |
452 | 2.49k | struct xar *xar; |
453 | 2.49k | struct archive_read *a = (struct archive_read *)_a; |
454 | 2.49k | int r; |
455 | | |
456 | 2.49k | archive_check_magic(_a, ARCHIVE_READ_MAGIC, |
457 | 2.49k | ARCHIVE_STATE_NEW, "archive_read_support_format_xar"); |
458 | | |
459 | 2.49k | xar = calloc(1, sizeof(*xar)); |
460 | 2.49k | if (xar == NULL) { |
461 | 0 | archive_set_error(&a->archive, ENOMEM, |
462 | 0 | "Can't allocate xar data"); |
463 | 0 | return (ARCHIVE_FATAL); |
464 | 0 | } |
465 | | |
466 | | /* initialize xar->file_queue */ |
467 | 2.49k | xar->file_queue.allocated = 0; |
468 | 2.49k | xar->file_queue.used = 0; |
469 | 2.49k | xar->file_queue.files = NULL; |
470 | | |
471 | 2.49k | r = __archive_read_register_format(a, |
472 | 2.49k | xar, |
473 | 2.49k | "xar", |
474 | 2.49k | xar_bid, |
475 | 2.49k | NULL, |
476 | 2.49k | xar_read_header, |
477 | 2.49k | xar_read_data, |
478 | 2.49k | xar_read_data_skip, |
479 | 2.49k | NULL, |
480 | 2.49k | xar_cleanup, |
481 | 2.49k | NULL, |
482 | 2.49k | NULL); |
483 | 2.49k | if (r != ARCHIVE_OK) |
484 | 0 | free(xar); |
485 | 2.49k | return (r); |
486 | 2.49k | } |
487 | | |
488 | | static int |
489 | | xar_bid(struct archive_read *a, int best_bid) |
490 | 2.45k | { |
491 | 2.45k | const unsigned char *b; |
492 | 2.45k | int bid; |
493 | | |
494 | 2.45k | (void)best_bid; /* UNUSED */ |
495 | | |
496 | 2.45k | b = __archive_read_ahead(a, HEADER_SIZE, NULL); |
497 | 2.45k | if (b == NULL) |
498 | 24 | return (-1); |
499 | | |
500 | 2.43k | bid = 0; |
501 | | /* |
502 | | * Verify magic code |
503 | | */ |
504 | 2.43k | if (archive_be32dec(b) != HEADER_MAGIC) |
505 | 2.32k | return (0); |
506 | 110 | bid += 32; |
507 | | /* |
508 | | * Verify header size |
509 | | */ |
510 | 110 | if (archive_be16dec(b+4) != HEADER_SIZE) |
511 | 0 | return (0); |
512 | 110 | bid += 16; |
513 | | /* |
514 | | * Verify header version |
515 | | */ |
516 | 110 | if (archive_be16dec(b+6) != HEADER_VERSION) |
517 | 0 | return (0); |
518 | 110 | bid += 16; |
519 | | /* |
520 | | * Verify type of checksum |
521 | | */ |
522 | 110 | switch (archive_be32dec(b+24)) { |
523 | 8 | case CKSUM_NONE: |
524 | 104 | case CKSUM_SHA1: |
525 | 110 | case CKSUM_MD5: |
526 | 110 | bid += 32; |
527 | 110 | break; |
528 | 0 | default: |
529 | 0 | return (0); |
530 | 110 | } |
531 | | |
532 | 110 | return (bid); |
533 | 110 | } |
534 | | |
535 | | static int |
536 | | read_toc(struct archive_read *a) |
537 | 110 | { |
538 | 110 | struct xar *xar; |
539 | 110 | struct xar_file *file; |
540 | 110 | const unsigned char *b; |
541 | 110 | uint64_t toc_compressed_size; |
542 | 110 | uint64_t toc_uncompressed_size; |
543 | 110 | uint32_t toc_chksum_alg; |
544 | 110 | ssize_t bytes; |
545 | 110 | int r; |
546 | | |
547 | 110 | xar = (struct xar *)(a->format->data); |
548 | | |
549 | | /* |
550 | | * Read xar header. |
551 | | */ |
552 | 110 | b = __archive_read_ahead(a, HEADER_SIZE, &bytes); |
553 | 110 | if (bytes < 0) |
554 | 0 | return ((int)bytes); |
555 | 110 | if (bytes < HEADER_SIZE) { |
556 | 0 | archive_set_error(&a->archive, |
557 | 0 | ARCHIVE_ERRNO_FILE_FORMAT, |
558 | 0 | "Truncated archive header"); |
559 | 0 | return (ARCHIVE_FATAL); |
560 | 0 | } |
561 | | |
562 | 110 | if (archive_be32dec(b) != HEADER_MAGIC) { |
563 | 0 | archive_set_error(&a->archive, |
564 | 0 | ARCHIVE_ERRNO_FILE_FORMAT, |
565 | 0 | "Invalid header magic"); |
566 | 0 | return (ARCHIVE_FATAL); |
567 | 0 | } |
568 | 110 | if (archive_be16dec(b+6) != HEADER_VERSION) { |
569 | 0 | archive_set_error(&a->archive, |
570 | 0 | ARCHIVE_ERRNO_FILE_FORMAT, |
571 | 0 | "Unsupported header version(%d)", |
572 | 0 | archive_be16dec(b+6)); |
573 | 0 | return (ARCHIVE_FATAL); |
574 | 0 | } |
575 | 110 | toc_compressed_size = archive_be64dec(b+8); |
576 | 110 | xar->toc_remaining = toc_compressed_size; |
577 | 110 | toc_uncompressed_size = archive_be64dec(b+16); |
578 | 110 | toc_chksum_alg = archive_be32dec(b+24); |
579 | 110 | __archive_read_consume(a, HEADER_SIZE); |
580 | 110 | xar->offset += HEADER_SIZE; |
581 | 110 | xar->toc_total = 0; |
582 | | |
583 | | /* |
584 | | * Read TOC(Table of Contents). |
585 | | */ |
586 | | /* Initialize reading contents. */ |
587 | 110 | r = move_reading_point(a, HEADER_SIZE); |
588 | 110 | if (r != ARCHIVE_OK) |
589 | 0 | return (r); |
590 | 110 | r = rd_contents_init(a, GZIP, toc_chksum_alg, CKSUM_NONE); |
591 | 110 | if (r != ARCHIVE_OK) |
592 | 0 | return (r); |
593 | | |
594 | 110 | #ifdef HAVE_LIBXML_XMLREADER_H |
595 | 110 | r = xml2_read_toc(a); |
596 | | #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H) |
597 | | r = expat_read_toc(a); |
598 | | #elif defined(HAVE_XMLLITE_H) |
599 | | r = xmllite_read_toc(a); |
600 | | #endif |
601 | 110 | if (r != ARCHIVE_OK) |
602 | 110 | return (r); |
603 | | |
604 | | /* Set 'The HEAP' base. */ |
605 | 0 | xar->h_base = xar->offset; |
606 | 0 | if (xar->toc_total != toc_uncompressed_size) { |
607 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
608 | 0 | "TOC uncompressed size error"); |
609 | 0 | return (ARCHIVE_FATAL); |
610 | 0 | } |
611 | | |
612 | | /* |
613 | | * Checksum TOC |
614 | | */ |
615 | 0 | if (toc_chksum_alg != CKSUM_NONE) { |
616 | 0 | r = move_reading_point(a, xar->toc_chksum_offset); |
617 | 0 | if (r != ARCHIVE_OK) |
618 | 0 | return (r); |
619 | 0 | b = __archive_read_ahead(a, |
620 | 0 | (size_t)xar->toc_chksum_size, &bytes); |
621 | 0 | if (bytes < 0) |
622 | 0 | return ((int)bytes); |
623 | 0 | if ((uint64_t)bytes < xar->toc_chksum_size) { |
624 | 0 | archive_set_error(&a->archive, |
625 | 0 | ARCHIVE_ERRNO_FILE_FORMAT, |
626 | 0 | "Truncated archive file"); |
627 | 0 | return (ARCHIVE_FATAL); |
628 | 0 | } |
629 | 0 | r = checksum_final(a, b, |
630 | 0 | (size_t)xar->toc_chksum_size, NULL, 0); |
631 | 0 | __archive_read_consume(a, xar->toc_chksum_size); |
632 | 0 | xar->offset += xar->toc_chksum_size; |
633 | | #ifndef DONT_FAIL_ON_CRC_ERROR |
634 | | if (r != ARCHIVE_OK) |
635 | | return (ARCHIVE_FATAL); |
636 | | #endif |
637 | 0 | } |
638 | | |
639 | | /* |
640 | | * Connect hardlinked files. |
641 | | */ |
642 | 0 | for (file = xar->hdlink_orgs; file != NULL; file = file->hdnext) { |
643 | 0 | struct hdlink **hdlink; |
644 | |
|
645 | 0 | for (hdlink = &(xar->hdlink_list); *hdlink != NULL; |
646 | 0 | hdlink = &((*hdlink)->next)) { |
647 | 0 | if ((*hdlink)->id == file->id) { |
648 | 0 | struct hdlink *hltmp; |
649 | 0 | struct xar_file *f2; |
650 | 0 | int nlink = (*hdlink)->cnt + 1; |
651 | |
|
652 | 0 | file->nlink = nlink; |
653 | 0 | for (f2 = (*hdlink)->files; f2 != NULL; |
654 | 0 | f2 = f2->hdnext) { |
655 | 0 | f2->nlink = nlink; |
656 | 0 | archive_string_copy( |
657 | 0 | &(f2->hardlink), &(file->pathname)); |
658 | 0 | } |
659 | | /* Remove resolved files from hdlist_list. */ |
660 | 0 | hltmp = *hdlink; |
661 | 0 | *hdlink = hltmp->next; |
662 | 0 | free(hltmp); |
663 | 0 | break; |
664 | 0 | } |
665 | 0 | } |
666 | 0 | } |
667 | 0 | a->archive.archive_format = ARCHIVE_FORMAT_XAR; |
668 | 0 | a->archive.archive_format_name = "xar"; |
669 | |
|
670 | 0 | return (ARCHIVE_OK); |
671 | 0 | } |
672 | | |
673 | | static int |
674 | | xar_read_header(struct archive_read *a, struct archive_entry *entry) |
675 | 110 | { |
676 | 110 | struct xar *xar; |
677 | 110 | struct xar_file *file; |
678 | 110 | struct xattr *xattr; |
679 | 110 | int r; |
680 | | |
681 | 110 | xar = (struct xar *)(a->format->data); |
682 | 110 | r = ARCHIVE_OK; |
683 | | |
684 | 110 | if (xar->offset == 0) { |
685 | | /* Create a character conversion object. */ |
686 | 110 | if (xar->sconv == NULL) { |
687 | 110 | xar->sconv = archive_string_conversion_from_charset( |
688 | 110 | &(a->archive), "UTF-8", 1); |
689 | 110 | if (xar->sconv == NULL) |
690 | 0 | return (ARCHIVE_FATAL); |
691 | 110 | } |
692 | | |
693 | | /* Read TOC. */ |
694 | 110 | r = read_toc(a); |
695 | 110 | if (r != ARCHIVE_OK) |
696 | 110 | return (r); |
697 | 110 | } |
698 | | |
699 | 0 | for (;;) { |
700 | 0 | file = xar->file = heap_get_entry(&(xar->file_queue)); |
701 | 0 | if (file == NULL) { |
702 | 0 | xar->end_of_file = 1; |
703 | 0 | return (ARCHIVE_EOF); |
704 | 0 | } |
705 | 0 | if ((file->mode & AE_IFMT) != AE_IFDIR) |
706 | 0 | break; |
707 | 0 | if (file->has != (HAS_PATHNAME | HAS_TYPE)) |
708 | 0 | break; |
709 | | /* |
710 | | * If a file type is a directory and it does not have |
711 | | * any metadata, do not export. |
712 | | */ |
713 | 0 | file_free(file); |
714 | 0 | } |
715 | 0 | if (file->has & HAS_ATIME) { |
716 | 0 | archive_entry_set_atime(entry, file->atime, 0); |
717 | 0 | } |
718 | 0 | if (file->has & HAS_CTIME) { |
719 | 0 | archive_entry_set_ctime(entry, file->ctime, 0); |
720 | 0 | } |
721 | 0 | if (file->has & HAS_MTIME) { |
722 | 0 | archive_entry_set_mtime(entry, file->mtime, 0); |
723 | 0 | } |
724 | 0 | archive_entry_set_gid(entry, file->gid); |
725 | 0 | if (file->gname.length > 0 && |
726 | 0 | archive_entry_copy_gname_l(entry, file->gname.s, |
727 | 0 | archive_strlen(&(file->gname)), xar->sconv) != 0) { |
728 | 0 | if (errno == ENOMEM) { |
729 | 0 | archive_set_error(&a->archive, ENOMEM, |
730 | 0 | "Can't allocate memory for Gname"); |
731 | 0 | return (ARCHIVE_FATAL); |
732 | 0 | } |
733 | 0 | archive_set_error(&a->archive, |
734 | 0 | ARCHIVE_ERRNO_FILE_FORMAT, |
735 | 0 | "Gname cannot be converted from %s to current locale", |
736 | 0 | archive_string_conversion_charset_name(xar->sconv)); |
737 | 0 | r = ARCHIVE_WARN; |
738 | 0 | } |
739 | 0 | archive_entry_set_uid(entry, file->uid); |
740 | 0 | if (file->uname.length > 0 && |
741 | 0 | archive_entry_copy_uname_l(entry, file->uname.s, |
742 | 0 | archive_strlen(&(file->uname)), xar->sconv) != 0) { |
743 | 0 | if (errno == ENOMEM) { |
744 | 0 | archive_set_error(&a->archive, ENOMEM, |
745 | 0 | "Can't allocate memory for Uname"); |
746 | 0 | return (ARCHIVE_FATAL); |
747 | 0 | } |
748 | 0 | archive_set_error(&a->archive, |
749 | 0 | ARCHIVE_ERRNO_FILE_FORMAT, |
750 | 0 | "Uname cannot be converted from %s to current locale", |
751 | 0 | archive_string_conversion_charset_name(xar->sconv)); |
752 | 0 | r = ARCHIVE_WARN; |
753 | 0 | } |
754 | 0 | archive_entry_set_mode(entry, file->mode); |
755 | 0 | if (archive_entry_copy_pathname_l(entry, file->pathname.s, |
756 | 0 | archive_strlen(&(file->pathname)), xar->sconv) != 0) { |
757 | 0 | if (errno == ENOMEM) { |
758 | 0 | archive_set_error(&a->archive, ENOMEM, |
759 | 0 | "Can't allocate memory for Pathname"); |
760 | 0 | return (ARCHIVE_FATAL); |
761 | 0 | } |
762 | 0 | archive_set_error(&a->archive, |
763 | 0 | ARCHIVE_ERRNO_FILE_FORMAT, |
764 | 0 | "Pathname cannot be converted from %s to current locale", |
765 | 0 | archive_string_conversion_charset_name(xar->sconv)); |
766 | 0 | r = ARCHIVE_WARN; |
767 | 0 | } |
768 | | |
769 | | |
770 | 0 | if (file->symlink.length > 0 && |
771 | 0 | archive_entry_copy_symlink_l(entry, file->symlink.s, |
772 | 0 | archive_strlen(&(file->symlink)), xar->sconv) != 0) { |
773 | 0 | if (errno == ENOMEM) { |
774 | 0 | archive_set_error(&a->archive, ENOMEM, |
775 | 0 | "Can't allocate memory for Linkname"); |
776 | 0 | return (ARCHIVE_FATAL); |
777 | 0 | } |
778 | 0 | archive_set_error(&a->archive, |
779 | 0 | ARCHIVE_ERRNO_FILE_FORMAT, |
780 | 0 | "Linkname cannot be converted from %s to current locale", |
781 | 0 | archive_string_conversion_charset_name(xar->sconv)); |
782 | 0 | r = ARCHIVE_WARN; |
783 | 0 | } |
784 | | /* Set proper nlink. */ |
785 | 0 | if ((file->mode & AE_IFMT) == AE_IFDIR) |
786 | 0 | archive_entry_set_nlink(entry, file->subdirs + 2); |
787 | 0 | else |
788 | 0 | archive_entry_set_nlink(entry, file->nlink); |
789 | 0 | archive_entry_set_size(entry, file->size); |
790 | 0 | if (archive_strlen(&(file->hardlink)) > 0) |
791 | 0 | archive_entry_set_hardlink(entry, file->hardlink.s); |
792 | 0 | archive_entry_set_ino64(entry, file->ino64); |
793 | 0 | if (file->has & HAS_DEV) |
794 | 0 | archive_entry_set_dev(entry, file->dev); |
795 | 0 | if (file->has & HAS_DEVMAJOR) |
796 | 0 | archive_entry_set_devmajor(entry, file->devmajor); |
797 | 0 | if (file->has & HAS_DEVMINOR) |
798 | 0 | archive_entry_set_devminor(entry, file->devminor); |
799 | 0 | if (archive_strlen(&(file->fflags_text)) > 0) |
800 | 0 | archive_entry_copy_fflags_text(entry, file->fflags_text.s); |
801 | |
|
802 | 0 | xar->entry_init = 1; |
803 | 0 | xar->entry_total = 0; |
804 | 0 | xar->entry_remaining = file->length; |
805 | 0 | xar->entry_size = file->size; |
806 | 0 | xar->entry_encoding = file->encoding; |
807 | 0 | xar->entry_a_sum = file->a_sum; |
808 | 0 | xar->entry_e_sum = file->e_sum; |
809 | | /* |
810 | | * Read extended attributes. |
811 | | */ |
812 | 0 | xattr = file->xattr_list; |
813 | 0 | while (xattr != NULL) { |
814 | 0 | const void *d; |
815 | 0 | size_t outbytes = 0; |
816 | 0 | size_t used = 0; |
817 | |
|
818 | 0 | r = move_reading_point(a, xattr->offset); |
819 | 0 | if (r != ARCHIVE_OK) |
820 | 0 | break; |
821 | 0 | r = rd_contents_init(a, xattr->encoding, |
822 | 0 | xattr->a_sum.alg, xattr->e_sum.alg); |
823 | 0 | if (r != ARCHIVE_OK) |
824 | 0 | break; |
825 | 0 | d = NULL; |
826 | 0 | r = rd_contents(a, &d, &outbytes, &used, xattr->length); |
827 | 0 | if (r != ARCHIVE_OK) |
828 | 0 | break; |
829 | 0 | if (outbytes != xattr->size) { |
830 | 0 | archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC, |
831 | 0 | "Decompressed size error"); |
832 | 0 | r = ARCHIVE_FATAL; |
833 | 0 | break; |
834 | 0 | } |
835 | 0 | r = checksum_final(a, |
836 | 0 | xattr->a_sum.val, xattr->a_sum.len, |
837 | 0 | xattr->e_sum.val, xattr->e_sum.len); |
838 | 0 | if (r != ARCHIVE_OK) { |
839 | | #ifndef DONT_FAIL_ON_CRC_ERROR |
840 | | archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC, |
841 | | "Xattr checksum error"); |
842 | | r = ARCHIVE_WARN; |
843 | | break; |
844 | | #endif |
845 | 0 | } |
846 | 0 | if (xattr->name.s == NULL) { |
847 | 0 | archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC, |
848 | 0 | "Xattr name error"); |
849 | 0 | r = ARCHIVE_WARN; |
850 | 0 | break; |
851 | 0 | } |
852 | 0 | archive_entry_xattr_add_entry(entry, |
853 | 0 | xattr->name.s, d, outbytes); |
854 | 0 | xattr = xattr->next; |
855 | 0 | } |
856 | 0 | if (r != ARCHIVE_OK) { |
857 | 0 | file_free(file); |
858 | 0 | return (r); |
859 | 0 | } |
860 | | |
861 | 0 | if (xar->entry_remaining > 0) |
862 | | /* Move reading point to the beginning of current |
863 | | * file contents. */ |
864 | 0 | r = move_reading_point(a, file->offset); |
865 | 0 | else |
866 | 0 | r = ARCHIVE_OK; |
867 | |
|
868 | 0 | file_free(file); |
869 | 0 | return (r); |
870 | 0 | } |
871 | | |
872 | | static int |
873 | | xar_read_data(struct archive_read *a, |
874 | | const void **buff, size_t *size, int64_t *offset) |
875 | 0 | { |
876 | 0 | struct xar *xar; |
877 | 0 | size_t used = 0; |
878 | 0 | int r; |
879 | |
|
880 | 0 | xar = (struct xar *)(a->format->data); |
881 | |
|
882 | 0 | if (xar->entry_unconsumed) { |
883 | 0 | __archive_read_consume(a, xar->entry_unconsumed); |
884 | 0 | xar->entry_unconsumed = 0; |
885 | 0 | } |
886 | |
|
887 | 0 | if (xar->end_of_file || xar->entry_remaining <= 0) { |
888 | 0 | r = ARCHIVE_EOF; |
889 | 0 | goto abort_read_data; |
890 | 0 | } |
891 | | |
892 | 0 | if (xar->entry_init) { |
893 | 0 | r = rd_contents_init(a, xar->entry_encoding, |
894 | 0 | xar->entry_a_sum.alg, xar->entry_e_sum.alg); |
895 | 0 | if (r != ARCHIVE_OK) { |
896 | 0 | xar->entry_remaining = 0; |
897 | 0 | return (r); |
898 | 0 | } |
899 | 0 | xar->entry_init = 0; |
900 | 0 | } |
901 | | |
902 | 0 | *buff = NULL; |
903 | 0 | r = rd_contents(a, buff, size, &used, xar->entry_remaining); |
904 | 0 | if (r != ARCHIVE_OK) |
905 | 0 | goto abort_read_data; |
906 | | |
907 | 0 | *offset = xar->entry_total; |
908 | 0 | xar->entry_total += *size; |
909 | 0 | xar->total += *size; |
910 | 0 | xar->offset += used; |
911 | 0 | xar->entry_remaining -= used; |
912 | 0 | xar->entry_unconsumed = used; |
913 | |
|
914 | 0 | if (xar->entry_remaining == 0) { |
915 | 0 | if (xar->entry_total != xar->entry_size) { |
916 | 0 | archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC, |
917 | 0 | "Decompressed size error"); |
918 | 0 | r = ARCHIVE_FATAL; |
919 | 0 | goto abort_read_data; |
920 | 0 | } |
921 | 0 | r = checksum_final(a, |
922 | 0 | xar->entry_a_sum.val, xar->entry_a_sum.len, |
923 | 0 | xar->entry_e_sum.val, xar->entry_e_sum.len); |
924 | 0 | if (r != ARCHIVE_OK) |
925 | 0 | goto abort_read_data; |
926 | 0 | } |
927 | | |
928 | 0 | return (ARCHIVE_OK); |
929 | 0 | abort_read_data: |
930 | 0 | *buff = NULL; |
931 | 0 | *size = 0; |
932 | 0 | *offset = (int64_t)xar->entry_total; |
933 | 0 | return (r); |
934 | 0 | } |
935 | | |
936 | | static int |
937 | | xar_read_data_skip(struct archive_read *a) |
938 | 0 | { |
939 | 0 | struct xar *xar; |
940 | 0 | int64_t bytes_skipped; |
941 | |
|
942 | 0 | xar = (struct xar *)(a->format->data); |
943 | 0 | if (xar->end_of_file) |
944 | 0 | return (ARCHIVE_EOF); |
945 | 0 | bytes_skipped = __archive_read_consume(a, xar->entry_remaining + |
946 | 0 | xar->entry_unconsumed); |
947 | 0 | if (bytes_skipped < 0) |
948 | 0 | return (ARCHIVE_FATAL); |
949 | 0 | xar->offset += bytes_skipped; |
950 | 0 | xar->entry_unconsumed = 0; |
951 | 0 | return (ARCHIVE_OK); |
952 | 0 | } |
953 | | |
954 | | static int |
955 | | xar_cleanup(struct archive_read *a) |
956 | 2.49k | { |
957 | 2.49k | struct xar *xar; |
958 | 2.49k | struct hdlink *hdlink; |
959 | 2.49k | int i; |
960 | 2.49k | int r; |
961 | | |
962 | 2.49k | xar = (struct xar *)(a->format->data); |
963 | 2.49k | checksum_cleanup(a); |
964 | 2.49k | r = decompression_cleanup(a); |
965 | 2.49k | hdlink = xar->hdlink_list; |
966 | 2.49k | while (hdlink != NULL) { |
967 | 0 | struct hdlink *next = hdlink->next; |
968 | |
|
969 | 0 | free(hdlink); |
970 | 0 | hdlink = next; |
971 | 0 | } |
972 | 21.7k | for (i = 0; i < xar->file_queue.used; i++) |
973 | 19.2k | file_free(xar->file_queue.files[i]); |
974 | 2.49k | free(xar->file_queue.files); |
975 | 3.43k | while (xar->unknowntags != NULL) { |
976 | 939 | struct unknown_tag *tag; |
977 | | |
978 | 939 | tag = xar->unknowntags; |
979 | 939 | xar->unknowntags = tag->next; |
980 | 939 | archive_string_free(&(tag->name)); |
981 | 939 | free(tag); |
982 | 939 | } |
983 | 2.49k | free(xar->outbuff); |
984 | 2.49k | free(xar); |
985 | 2.49k | a->format->data = NULL; |
986 | 2.49k | return (r); |
987 | 2.49k | } |
988 | | |
989 | | static int |
990 | | move_reading_point(struct archive_read *a, uint64_t offset) |
991 | 110 | { |
992 | 110 | struct xar *xar; |
993 | | |
994 | 110 | xar = (struct xar *)(a->format->data); |
995 | 110 | if (xar->offset - xar->h_base != offset) { |
996 | | /* Seek forward to the start of file contents. */ |
997 | 0 | int64_t step; |
998 | |
|
999 | 0 | step = offset - (xar->offset - xar->h_base); |
1000 | 0 | if (step > 0) { |
1001 | 0 | step = __archive_read_consume(a, step); |
1002 | 0 | if (step < 0) |
1003 | 0 | return ((int)step); |
1004 | 0 | xar->offset += step; |
1005 | 0 | } else { |
1006 | 0 | int64_t pos = __archive_read_seek(a, xar->h_base + offset, SEEK_SET); |
1007 | 0 | if (pos == ARCHIVE_FAILED) { |
1008 | 0 | archive_set_error(&(a->archive), |
1009 | 0 | ARCHIVE_ERRNO_MISC, |
1010 | 0 | "Cannot seek"); |
1011 | 0 | return (ARCHIVE_FAILED); |
1012 | 0 | } |
1013 | 0 | xar->offset = pos; |
1014 | 0 | } |
1015 | 0 | } |
1016 | 110 | return (ARCHIVE_OK); |
1017 | 110 | } |
1018 | | |
1019 | | static int |
1020 | | rd_contents_init(struct archive_read *a, enum enctype encoding, |
1021 | | int a_sum_alg, int e_sum_alg) |
1022 | 110 | { |
1023 | 110 | int r; |
1024 | | |
1025 | | /* Init decompress library. */ |
1026 | 110 | if ((r = decompression_init(a, encoding)) != ARCHIVE_OK) |
1027 | 0 | return (r); |
1028 | | /* Init checksum library. */ |
1029 | 110 | checksum_init(a, a_sum_alg, e_sum_alg); |
1030 | 110 | return (ARCHIVE_OK); |
1031 | 110 | } |
1032 | | |
1033 | | static int |
1034 | | rd_contents(struct archive_read *a, const void **buff, size_t *size, |
1035 | | size_t *used, uint64_t remaining) |
1036 | 2.64k | { |
1037 | 2.64k | const unsigned char *b; |
1038 | 2.64k | ssize_t bytes; |
1039 | | |
1040 | | /* Get whatever bytes are immediately available. */ |
1041 | 2.64k | b = __archive_read_ahead(a, 1, &bytes); |
1042 | 2.64k | if (bytes < 0) |
1043 | 0 | return ((int)bytes); |
1044 | 2.64k | if (bytes == 0) { |
1045 | 55 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1046 | 55 | "Truncated archive file"); |
1047 | 55 | return (ARCHIVE_FATAL); |
1048 | 55 | } |
1049 | 2.58k | if ((uint64_t)bytes > remaining) |
1050 | 6 | bytes = (ssize_t)remaining; |
1051 | | |
1052 | | /* |
1053 | | * Decompress contents of file. |
1054 | | */ |
1055 | 2.58k | *used = bytes; |
1056 | 2.58k | if (decompress(a, buff, size, b, used) != ARCHIVE_OK) |
1057 | 5 | return (ARCHIVE_FATAL); |
1058 | | |
1059 | | /* |
1060 | | * Update checksum of a compressed data and a extracted data. |
1061 | | */ |
1062 | 2.58k | checksum_update(a, b, *used, *buff, *size); |
1063 | | |
1064 | 2.58k | return (ARCHIVE_OK); |
1065 | 2.58k | } |
1066 | | |
1067 | | /* |
1068 | | * Note that this implementation does not (and should not!) obey |
1069 | | * locale settings; you cannot simply substitute strtol here, since |
1070 | | * it does obey locale. |
1071 | | */ |
1072 | | |
1073 | | static int |
1074 | | atou64(const char *p, size_t char_cnt, int base, uint64_t *val) |
1075 | 15.9k | { |
1076 | 15.9k | uint64_t l; |
1077 | | |
1078 | 15.9k | l = 0; |
1079 | 15.9k | if (char_cnt > 0) { |
1080 | 15.7k | int digit; |
1081 | | |
1082 | 15.7k | digit = *p - '0'; |
1083 | 45.4k | while (digit >= 0 && digit < base && char_cnt-- > 0) { |
1084 | 29.7k | if (l > UINT64_MAX / base) |
1085 | 0 | return (ARCHIVE_FATAL); |
1086 | 29.7k | l *= base; |
1087 | 29.7k | if (l > UINT64_MAX - digit) |
1088 | 0 | return (ARCHIVE_FATAL); |
1089 | 29.7k | l += digit; |
1090 | 29.7k | digit = *++p - '0'; |
1091 | 29.7k | } |
1092 | 15.7k | } |
1093 | | |
1094 | 15.9k | *val = l; |
1095 | 15.9k | return (ARCHIVE_OK); |
1096 | 15.9k | } |
1097 | | |
1098 | | static size_t |
1099 | | atohex(unsigned char *b, size_t bsize, const char *p, size_t psize) |
1100 | 15.2k | { |
1101 | 15.2k | size_t fbsize = bsize; |
1102 | | |
1103 | 47.4k | while (bsize && psize > 1) { |
1104 | 45.7k | unsigned char x; |
1105 | | |
1106 | 45.7k | if (p[0] >= 'a' && p[0] <= 'f') |
1107 | 30.0k | x = (p[0] - 'a' + 0x0a) << 4; |
1108 | 15.6k | else if (p[0] >= 'A' && p[0] <= 'F') |
1109 | 1.74k | x = (p[0] - 'A' + 0x0a) << 4; |
1110 | 13.9k | else if (p[0] >= '0' && p[0] <= '9') |
1111 | 8.79k | x = (p[0] - '0') << 4; |
1112 | 5.13k | else |
1113 | 5.13k | return (-1); |
1114 | 40.6k | if (p[1] >= 'a' && p[1] <= 'f') |
1115 | 26.9k | x |= p[1] - 'a' + 0x0a; |
1116 | 13.6k | else if (p[1] >= 'A' && p[1] <= 'F') |
1117 | 763 | x |= p[1] - 'A' + 0x0a; |
1118 | 12.9k | else if (p[1] >= '0' && p[1] <= '9') |
1119 | 4.55k | x |= p[1] - '0'; |
1120 | 8.37k | else |
1121 | 8.37k | return (-1); |
1122 | | |
1123 | 32.2k | *b++ = x; |
1124 | 32.2k | bsize--; |
1125 | 32.2k | p += 2; |
1126 | 32.2k | psize -= 2; |
1127 | 32.2k | } |
1128 | 1.73k | return (fbsize - bsize); |
1129 | 15.2k | } |
1130 | | |
1131 | | static time_t |
1132 | | time_from_tm(struct tm *t) |
1133 | 12 | { |
1134 | | #if HAVE__MKGMTIME |
1135 | | return _mkgmtime(t); |
1136 | | #elif HAVE_TIMEGM |
1137 | | /* Use platform timegm() if available. */ |
1138 | 12 | return (timegm(t)); |
1139 | | #else |
1140 | | /* Else use direct calculation using POSIX assumptions. */ |
1141 | | /* First, fix up tm_yday based on the year/month/day. */ |
1142 | | mktime(t); |
1143 | | /* Then we can compute timegm() from first principles. */ |
1144 | | return (t->tm_sec |
1145 | | + t->tm_min * 60 |
1146 | | + t->tm_hour * 3600 |
1147 | | + t->tm_yday * 86400 |
1148 | | + (t->tm_year - 70) * 31536000 |
1149 | | + ((t->tm_year - 69) / 4) * 86400 |
1150 | | - ((t->tm_year - 1) / 100) * 86400 |
1151 | | + ((t->tm_year + 299) / 400) * 86400); |
1152 | | #endif |
1153 | 12 | } |
1154 | | |
1155 | | static time_t |
1156 | | parse_time(const char *p, size_t n) |
1157 | 1.25k | { |
1158 | 1.25k | struct tm tm; |
1159 | 1.25k | time_t t = 0; |
1160 | 1.25k | uint64_t data; |
1161 | | |
1162 | 1.25k | memset(&tm, 0, sizeof(tm)); |
1163 | 1.25k | if (n != 20) |
1164 | 13 | return (t); |
1165 | 1.24k | if (atou64(p, 4, 10, &data) != ARCHIVE_OK || data < 1900) |
1166 | 321 | return (t); |
1167 | 925 | tm.tm_year = (int)data - 1900; |
1168 | 925 | p += 4; |
1169 | 925 | if (*p++ != '-') |
1170 | 0 | return (t); |
1171 | 925 | if (atou64(p, 4, 10, &data) != ARCHIVE_OK || data < 1 || data > 12) |
1172 | 12 | return (t); |
1173 | 913 | tm.tm_mon = (int)data -1; |
1174 | 913 | p += 2; |
1175 | 913 | if (*p++ != '-') |
1176 | 266 | return (t); |
1177 | 647 | if (atou64(p, 4, 10, &data) != ARCHIVE_OK || data < 1 || data > 31) |
1178 | 185 | return (t); |
1179 | 462 | tm.tm_mday = (int)data; |
1180 | 462 | p += 2; |
1181 | 462 | if (*p++ != 'T') |
1182 | 84 | return (t); |
1183 | 378 | if (atou64(p, 4, 10, &data) != ARCHIVE_OK || data > 23) |
1184 | 0 | return (t); |
1185 | 378 | tm.tm_hour = (int)data; |
1186 | 378 | p += 2; |
1187 | 378 | if (*p++ != ':') |
1188 | 183 | return (t); |
1189 | 195 | if (atou64(p, 4, 10, &data) != ARCHIVE_OK || data > 59) |
1190 | 156 | return (t); |
1191 | 39 | tm.tm_min = (int)data; |
1192 | 39 | p += 2; |
1193 | 39 | if (*p++ != ':') |
1194 | 14 | return (t); |
1195 | 25 | if (atou64(p, 4, 10, &data) != ARCHIVE_OK || data > 60) |
1196 | 13 | return (t); |
1197 | 12 | tm.tm_sec = (int)data; |
1198 | | #if 0 |
1199 | | p += 2; |
1200 | | if (*p != 'Z') |
1201 | | return (t); |
1202 | | #endif |
1203 | | |
1204 | 12 | t = time_from_tm(&tm); |
1205 | | |
1206 | 12 | return (t); |
1207 | 25 | } |
1208 | | |
1209 | | static int |
1210 | | heap_add_entry(struct archive_read *a, |
1211 | | struct heap_queue *heap, struct xar_file *file) |
1212 | 19.2k | { |
1213 | 19.2k | uint64_t file_id, parent_id; |
1214 | 19.2k | int hole, parent; |
1215 | | |
1216 | | /* Expand our pending files list as necessary. */ |
1217 | 19.2k | if (heap->used >= heap->allocated) { |
1218 | 107 | struct xar_file **new_pending_files; |
1219 | 107 | int new_size; |
1220 | | |
1221 | 107 | if (heap->allocated < 1024) |
1222 | 96 | new_size = 1024; |
1223 | 11 | else |
1224 | 11 | new_size = heap->allocated * 2; |
1225 | | /* Overflow might keep us from growing the list. */ |
1226 | 107 | if (new_size <= heap->allocated) { |
1227 | 0 | archive_set_error(&a->archive, |
1228 | 0 | ENOMEM, "Out of memory"); |
1229 | 0 | return (ARCHIVE_FATAL); |
1230 | 0 | } |
1231 | 107 | new_pending_files = (struct xar_file **) |
1232 | 107 | calloc(new_size, sizeof(new_pending_files[0])); |
1233 | 107 | if (new_pending_files == NULL) { |
1234 | 0 | archive_set_error(&a->archive, |
1235 | 0 | ENOMEM, "Out of memory"); |
1236 | 0 | return (ARCHIVE_FATAL); |
1237 | 0 | } |
1238 | 107 | if (heap->allocated) { |
1239 | 11 | memcpy(new_pending_files, heap->files, |
1240 | 11 | heap->allocated * sizeof(new_pending_files[0])); |
1241 | 11 | free(heap->files); |
1242 | 11 | } |
1243 | 107 | heap->files = new_pending_files; |
1244 | 107 | heap->allocated = new_size; |
1245 | 107 | } |
1246 | | |
1247 | 19.2k | file_id = file->id; |
1248 | | |
1249 | | /* |
1250 | | * Start with hole at end, walk it up tree to find insertion point. |
1251 | | */ |
1252 | 19.2k | hole = heap->used++; |
1253 | 19.5k | while (hole > 0) { |
1254 | 19.4k | parent = (hole - 1)/2; |
1255 | 19.4k | parent_id = heap->files[parent]->id; |
1256 | 19.4k | if (file_id >= parent_id) { |
1257 | 19.1k | heap->files[hole] = file; |
1258 | 19.1k | return (ARCHIVE_OK); |
1259 | 19.1k | } |
1260 | | /* Move parent into hole <==> move hole up tree. */ |
1261 | 257 | heap->files[hole] = heap->files[parent]; |
1262 | 257 | hole = parent; |
1263 | 257 | } |
1264 | 102 | heap->files[0] = file; |
1265 | | |
1266 | 102 | return (ARCHIVE_OK); |
1267 | 19.2k | } |
1268 | | |
1269 | | static struct xar_file * |
1270 | | heap_get_entry(struct heap_queue *heap) |
1271 | 0 | { |
1272 | 0 | uint64_t a_id, b_id, c_id; |
1273 | 0 | int a, b, c; |
1274 | 0 | struct xar_file *r, *tmp; |
1275 | |
|
1276 | 0 | if (heap->used < 1) |
1277 | 0 | return (NULL); |
1278 | | |
1279 | | /* |
1280 | | * The first file in the list is the earliest; we'll return this. |
1281 | | */ |
1282 | 0 | r = heap->files[0]; |
1283 | | |
1284 | | /* |
1285 | | * Move the last item in the heap to the root of the tree |
1286 | | */ |
1287 | 0 | heap->files[0] = heap->files[--(heap->used)]; |
1288 | | |
1289 | | /* |
1290 | | * Rebalance the heap. |
1291 | | */ |
1292 | 0 | a = 0; /* Starting element and its heap key */ |
1293 | 0 | a_id = heap->files[a]->id; |
1294 | 0 | for (;;) { |
1295 | 0 | b = a + a + 1; /* First child */ |
1296 | 0 | if (b >= heap->used) |
1297 | 0 | return (r); |
1298 | 0 | b_id = heap->files[b]->id; |
1299 | 0 | c = b + 1; /* Use second child if it is smaller. */ |
1300 | 0 | if (c < heap->used) { |
1301 | 0 | c_id = heap->files[c]->id; |
1302 | 0 | if (c_id < b_id) { |
1303 | 0 | b = c; |
1304 | 0 | b_id = c_id; |
1305 | 0 | } |
1306 | 0 | } |
1307 | 0 | if (a_id <= b_id) |
1308 | 0 | return (r); |
1309 | 0 | tmp = heap->files[a]; |
1310 | 0 | heap->files[a] = heap->files[b]; |
1311 | 0 | heap->files[b] = tmp; |
1312 | 0 | a = b; |
1313 | 0 | } |
1314 | 0 | } |
1315 | | |
1316 | | static int |
1317 | | add_link(struct archive_read *a, struct xar *xar, struct xar_file *file) |
1318 | 0 | { |
1319 | 0 | struct hdlink *hdlink; |
1320 | |
|
1321 | 0 | for (hdlink = xar->hdlink_list; hdlink != NULL; hdlink = hdlink->next) { |
1322 | 0 | if (hdlink->id == file->link) { |
1323 | 0 | file->hdnext = hdlink->files; |
1324 | 0 | hdlink->cnt++; |
1325 | 0 | hdlink->files = file; |
1326 | 0 | return (ARCHIVE_OK); |
1327 | 0 | } |
1328 | 0 | } |
1329 | 0 | hdlink = malloc(sizeof(*hdlink)); |
1330 | 0 | if (hdlink == NULL) { |
1331 | 0 | archive_set_error(&a->archive, ENOMEM, "Out of memory"); |
1332 | 0 | return (ARCHIVE_FATAL); |
1333 | 0 | } |
1334 | 0 | file->hdnext = NULL; |
1335 | 0 | hdlink->id = file->link; |
1336 | 0 | hdlink->cnt = 1; |
1337 | 0 | hdlink->files = file; |
1338 | 0 | hdlink->next = xar->hdlink_list; |
1339 | 0 | xar->hdlink_list = hdlink; |
1340 | 0 | return (ARCHIVE_OK); |
1341 | 0 | } |
1342 | | |
1343 | | static void |
1344 | | _checksum_init(struct chksumwork *sumwrk, int sum_alg) |
1345 | 220 | { |
1346 | 220 | sumwrk->alg = sum_alg; |
1347 | 220 | switch (sum_alg) { |
1348 | 118 | case CKSUM_NONE: |
1349 | 118 | break; |
1350 | 96 | case CKSUM_SHA1: |
1351 | 96 | archive_sha1_init(&(sumwrk->sha1ctx)); |
1352 | 96 | break; |
1353 | 6 | case CKSUM_MD5: |
1354 | 6 | archive_md5_init(&(sumwrk->md5ctx)); |
1355 | 6 | break; |
1356 | 220 | } |
1357 | 220 | } |
1358 | | |
1359 | | static void |
1360 | | _checksum_update(struct chksumwork *sumwrk, const void *buff, size_t size) |
1361 | 5.16k | { |
1362 | | |
1363 | 5.16k | switch (sumwrk->alg) { |
1364 | 2.79k | case CKSUM_NONE: |
1365 | 2.79k | break; |
1366 | 2.04k | case CKSUM_SHA1: |
1367 | 2.04k | archive_sha1_update(&(sumwrk->sha1ctx), buff, size); |
1368 | 2.04k | break; |
1369 | 320 | case CKSUM_MD5: |
1370 | 320 | archive_md5_update(&(sumwrk->md5ctx), buff, size); |
1371 | 320 | break; |
1372 | 5.16k | } |
1373 | 5.16k | } |
1374 | | |
1375 | | static int |
1376 | | _checksum_final(struct chksumwork *sumwrk, const void *val, size_t len) |
1377 | 4.99k | { |
1378 | 4.99k | unsigned char sum[MAX_SUM_SIZE]; |
1379 | 4.99k | int r = ARCHIVE_OK; |
1380 | | |
1381 | 4.99k | switch (sumwrk->alg) { |
1382 | 4.89k | case CKSUM_NONE: |
1383 | 4.89k | break; |
1384 | 96 | case CKSUM_SHA1: |
1385 | 96 | archive_sha1_final(&(sumwrk->sha1ctx), sum); |
1386 | 96 | if (len != SHA1_SIZE || |
1387 | 0 | memcmp(val, sum, SHA1_SIZE) != 0) |
1388 | 96 | r = ARCHIVE_FAILED; |
1389 | 96 | break; |
1390 | 6 | case CKSUM_MD5: |
1391 | 6 | archive_md5_final(&(sumwrk->md5ctx), sum); |
1392 | 6 | if (len != MD5_SIZE || |
1393 | 0 | memcmp(val, sum, MD5_SIZE) != 0) |
1394 | 6 | r = ARCHIVE_FAILED; |
1395 | 6 | break; |
1396 | 4.99k | } |
1397 | 4.99k | return (r); |
1398 | 4.99k | } |
1399 | | |
1400 | | static void |
1401 | | checksum_init(struct archive_read *a, int a_sum_alg, int e_sum_alg) |
1402 | 110 | { |
1403 | 110 | struct xar *xar; |
1404 | | |
1405 | 110 | xar = (struct xar *)(a->format->data); |
1406 | 110 | _checksum_init(&(xar->a_sumwrk), a_sum_alg); |
1407 | 110 | _checksum_init(&(xar->e_sumwrk), e_sum_alg); |
1408 | 110 | } |
1409 | | |
1410 | | static void |
1411 | | checksum_update(struct archive_read *a, const void *abuff, size_t asize, |
1412 | | const void *ebuff, size_t esize) |
1413 | 2.58k | { |
1414 | 2.58k | struct xar *xar; |
1415 | | |
1416 | 2.58k | xar = (struct xar *)(a->format->data); |
1417 | 2.58k | _checksum_update(&(xar->a_sumwrk), abuff, asize); |
1418 | 2.58k | _checksum_update(&(xar->e_sumwrk), ebuff, esize); |
1419 | 2.58k | } |
1420 | | |
1421 | | static int |
1422 | | checksum_final(struct archive_read *a, const void *a_sum_val, |
1423 | | size_t a_sum_len, const void *e_sum_val, size_t e_sum_len) |
1424 | 0 | { |
1425 | 0 | struct xar *xar; |
1426 | 0 | int r; |
1427 | |
|
1428 | 0 | xar = (struct xar *)(a->format->data); |
1429 | 0 | r = _checksum_final(&(xar->a_sumwrk), a_sum_val, a_sum_len); |
1430 | 0 | if (r == ARCHIVE_OK) |
1431 | 0 | r = _checksum_final(&(xar->e_sumwrk), e_sum_val, e_sum_len); |
1432 | 0 | if (r != ARCHIVE_OK) |
1433 | 0 | archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC, |
1434 | 0 | "Sumcheck error"); |
1435 | 0 | return (r); |
1436 | 0 | } |
1437 | | |
1438 | | static int |
1439 | | decompression_init(struct archive_read *a, enum enctype encoding) |
1440 | 110 | { |
1441 | 110 | struct xar *xar; |
1442 | 110 | const char *detail; |
1443 | 110 | int r; |
1444 | | |
1445 | 110 | xar = (struct xar *)(a->format->data); |
1446 | 110 | xar->rd_encoding = encoding; |
1447 | 110 | switch (encoding) { |
1448 | 0 | case NONE: |
1449 | 0 | break; |
1450 | 110 | case GZIP: |
1451 | 110 | if (xar->stream_valid) |
1452 | 0 | r = inflateReset(&(xar->stream)); |
1453 | 110 | else |
1454 | 110 | r = inflateInit(&(xar->stream)); |
1455 | 110 | if (r != Z_OK) { |
1456 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1457 | 0 | "Couldn't initialize zlib stream"); |
1458 | 0 | return (ARCHIVE_FATAL); |
1459 | 0 | } |
1460 | 110 | xar->stream_valid = 1; |
1461 | 110 | xar->stream.total_in = 0; |
1462 | 110 | xar->stream.total_out = 0; |
1463 | 110 | break; |
1464 | 0 | #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR) |
1465 | 0 | case BZIP2: |
1466 | 0 | if (xar->bzstream_valid) { |
1467 | 0 | BZ2_bzDecompressEnd(&(xar->bzstream)); |
1468 | 0 | xar->bzstream_valid = 0; |
1469 | 0 | } |
1470 | 0 | r = BZ2_bzDecompressInit(&(xar->bzstream), 0, 0); |
1471 | 0 | if (r == BZ_MEM_ERROR) |
1472 | 0 | r = BZ2_bzDecompressInit(&(xar->bzstream), 0, 1); |
1473 | 0 | if (r != BZ_OK) { |
1474 | 0 | int err = ARCHIVE_ERRNO_MISC; |
1475 | 0 | detail = NULL; |
1476 | 0 | switch (r) { |
1477 | 0 | case BZ_PARAM_ERROR: |
1478 | 0 | detail = "invalid setup parameter"; |
1479 | 0 | break; |
1480 | 0 | case BZ_MEM_ERROR: |
1481 | 0 | err = ENOMEM; |
1482 | 0 | detail = "out of memory"; |
1483 | 0 | break; |
1484 | 0 | case BZ_CONFIG_ERROR: |
1485 | 0 | detail = "mis-compiled library"; |
1486 | 0 | break; |
1487 | 0 | } |
1488 | 0 | archive_set_error(&a->archive, err, |
1489 | 0 | "Internal error initializing decompressor: %s", |
1490 | 0 | detail == NULL ? "??" : detail); |
1491 | 0 | xar->bzstream_valid = 0; |
1492 | 0 | return (ARCHIVE_FATAL); |
1493 | 0 | } |
1494 | 0 | xar->bzstream_valid = 1; |
1495 | 0 | xar->bzstream.total_in_lo32 = 0; |
1496 | 0 | xar->bzstream.total_in_hi32 = 0; |
1497 | 0 | xar->bzstream.total_out_lo32 = 0; |
1498 | 0 | xar->bzstream.total_out_hi32 = 0; |
1499 | 0 | break; |
1500 | 0 | #endif |
1501 | 0 | #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA) |
1502 | 0 | #if LZMA_VERSION_MAJOR >= 5 |
1503 | | /* Effectively disable the limiter. */ |
1504 | 0 | #define LZMA_MEMLIMIT UINT64_MAX |
1505 | | #else |
1506 | | /* NOTE: This needs to check memory size which running system has. */ |
1507 | | #define LZMA_MEMLIMIT (1U << 30) |
1508 | | #endif |
1509 | 0 | case XZ: |
1510 | 0 | case LZMA: |
1511 | 0 | if (xar->lzstream_valid) { |
1512 | 0 | lzma_end(&(xar->lzstream)); |
1513 | 0 | xar->lzstream_valid = 0; |
1514 | 0 | } |
1515 | 0 | if (xar->entry_encoding == XZ) |
1516 | 0 | r = lzma_stream_decoder(&(xar->lzstream), |
1517 | 0 | LZMA_MEMLIMIT,/* memlimit */ |
1518 | 0 | LZMA_CONCATENATED); |
1519 | 0 | else |
1520 | 0 | r = lzma_alone_decoder(&(xar->lzstream), |
1521 | 0 | LZMA_MEMLIMIT);/* memlimit */ |
1522 | 0 | if (r != LZMA_OK) { |
1523 | 0 | switch (r) { |
1524 | 0 | case LZMA_MEM_ERROR: |
1525 | 0 | archive_set_error(&a->archive, |
1526 | 0 | ENOMEM, |
1527 | 0 | "Internal error initializing " |
1528 | 0 | "compression library: " |
1529 | 0 | "Cannot allocate memory"); |
1530 | 0 | break; |
1531 | 0 | case LZMA_OPTIONS_ERROR: |
1532 | 0 | archive_set_error(&a->archive, |
1533 | 0 | ARCHIVE_ERRNO_MISC, |
1534 | 0 | "Internal error initializing " |
1535 | 0 | "compression library: " |
1536 | 0 | "Invalid or unsupported options"); |
1537 | 0 | break; |
1538 | 0 | default: |
1539 | 0 | archive_set_error(&a->archive, |
1540 | 0 | ARCHIVE_ERRNO_MISC, |
1541 | 0 | "Internal error initializing " |
1542 | 0 | "lzma library"); |
1543 | 0 | break; |
1544 | 0 | } |
1545 | 0 | return (ARCHIVE_FATAL); |
1546 | 0 | } |
1547 | 0 | xar->lzstream_valid = 1; |
1548 | 0 | xar->lzstream.total_in = 0; |
1549 | 0 | xar->lzstream.total_out = 0; |
1550 | 0 | break; |
1551 | 0 | #endif |
1552 | | /* |
1553 | | * Unsupported compression. |
1554 | | */ |
1555 | 0 | default: |
1556 | | #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR) |
1557 | | case BZIP2: |
1558 | | #endif |
1559 | | #if !defined(HAVE_LZMA_H) || !defined(HAVE_LIBLZMA) |
1560 | | case LZMA: |
1561 | | case XZ: |
1562 | | #endif |
1563 | 0 | switch (xar->entry_encoding) { |
1564 | 0 | case BZIP2: detail = "bzip2"; break; |
1565 | 0 | case LZMA: detail = "lzma"; break; |
1566 | 0 | case XZ: detail = "xz"; break; |
1567 | 0 | default: detail = "??"; break; |
1568 | 0 | } |
1569 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1570 | 0 | "%s compression not supported on this platform", |
1571 | 0 | detail); |
1572 | 0 | return (ARCHIVE_FAILED); |
1573 | 110 | } |
1574 | 110 | return (ARCHIVE_OK); |
1575 | 110 | } |
1576 | | |
1577 | | static int |
1578 | | decompress(struct archive_read *a, const void **buff, size_t *outbytes, |
1579 | | const void *b, size_t *used) |
1580 | 2.58k | { |
1581 | 2.58k | struct xar *xar; |
1582 | 2.58k | void *outbuff; |
1583 | 2.58k | size_t avail_in, avail_out; |
1584 | 2.58k | int r; |
1585 | | |
1586 | 2.58k | xar = (struct xar *)(a->format->data); |
1587 | 2.58k | avail_in = *used; |
1588 | 2.58k | outbuff = (void *)(uintptr_t)*buff; |
1589 | 2.58k | if (outbuff == NULL) { |
1590 | 0 | if (xar->outbuff == NULL) { |
1591 | 0 | xar->outbuff = malloc(OUTBUFF_SIZE); |
1592 | 0 | if (xar->outbuff == NULL) { |
1593 | 0 | archive_set_error(&a->archive, ENOMEM, |
1594 | 0 | "Couldn't allocate memory for out buffer"); |
1595 | 0 | return (ARCHIVE_FATAL); |
1596 | 0 | } |
1597 | 0 | } |
1598 | 0 | outbuff = xar->outbuff; |
1599 | 0 | *buff = outbuff; |
1600 | 0 | avail_out = OUTBUFF_SIZE; |
1601 | 0 | } else |
1602 | 2.58k | avail_out = *outbytes; |
1603 | 2.58k | switch (xar->rd_encoding) { |
1604 | 2.58k | case GZIP: |
1605 | 2.58k | xar->stream.next_in = (Bytef *)(uintptr_t)b; |
1606 | 2.58k | xar->stream.avail_in = (uInt)avail_in; |
1607 | 2.58k | xar->stream.next_out = (unsigned char *)outbuff; |
1608 | 2.58k | xar->stream.avail_out = (uInt)avail_out; |
1609 | 2.58k | r = inflate(&(xar->stream), 0); |
1610 | 2.58k | switch (r) { |
1611 | 2.58k | case Z_OK: /* Decompressor made some progress.*/ |
1612 | 2.58k | case Z_STREAM_END: /* Found end of stream. */ |
1613 | 2.58k | break; |
1614 | 5 | default: |
1615 | 5 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
1616 | 5 | "File decompression failed (%d)", r); |
1617 | 5 | return (ARCHIVE_FATAL); |
1618 | 2.58k | } |
1619 | 2.58k | *used = avail_in - xar->stream.avail_in; |
1620 | 2.58k | *outbytes = avail_out - xar->stream.avail_out; |
1621 | 2.58k | break; |
1622 | 0 | #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR) |
1623 | 0 | case BZIP2: |
1624 | 0 | xar->bzstream.next_in = (char *)(uintptr_t)b; |
1625 | 0 | xar->bzstream.avail_in = (unsigned int)avail_in; |
1626 | 0 | xar->bzstream.next_out = (char *)outbuff; |
1627 | 0 | xar->bzstream.avail_out = (unsigned int)avail_out; |
1628 | 0 | r = BZ2_bzDecompress(&(xar->bzstream)); |
1629 | 0 | switch (r) { |
1630 | 0 | case BZ_STREAM_END: /* Found end of stream. */ |
1631 | 0 | switch (BZ2_bzDecompressEnd(&(xar->bzstream))) { |
1632 | 0 | case BZ_OK: |
1633 | 0 | break; |
1634 | 0 | default: |
1635 | 0 | archive_set_error(&(a->archive), |
1636 | 0 | ARCHIVE_ERRNO_MISC, |
1637 | 0 | "Failed to clean up decompressor"); |
1638 | 0 | return (ARCHIVE_FATAL); |
1639 | 0 | } |
1640 | 0 | xar->bzstream_valid = 0; |
1641 | | /* FALLTHROUGH */ |
1642 | 0 | case BZ_OK: /* Decompressor made some progress. */ |
1643 | 0 | break; |
1644 | 0 | default: |
1645 | 0 | archive_set_error(&(a->archive), |
1646 | 0 | ARCHIVE_ERRNO_MISC, |
1647 | 0 | "bzip decompression failed"); |
1648 | 0 | return (ARCHIVE_FATAL); |
1649 | 0 | } |
1650 | 0 | *used = avail_in - xar->bzstream.avail_in; |
1651 | 0 | *outbytes = avail_out - xar->bzstream.avail_out; |
1652 | 0 | break; |
1653 | 0 | #endif |
1654 | 0 | #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA) |
1655 | 0 | case LZMA: |
1656 | 0 | case XZ: |
1657 | 0 | xar->lzstream.next_in = b; |
1658 | 0 | xar->lzstream.avail_in = avail_in; |
1659 | 0 | xar->lzstream.next_out = (unsigned char *)outbuff; |
1660 | 0 | xar->lzstream.avail_out = avail_out; |
1661 | 0 | r = lzma_code(&(xar->lzstream), LZMA_RUN); |
1662 | 0 | switch (r) { |
1663 | 0 | case LZMA_STREAM_END: /* Found end of stream. */ |
1664 | 0 | lzma_end(&(xar->lzstream)); |
1665 | 0 | xar->lzstream_valid = 0; |
1666 | | /* FALLTHROUGH */ |
1667 | 0 | case LZMA_OK: /* Decompressor made some progress. */ |
1668 | 0 | break; |
1669 | 0 | default: |
1670 | 0 | archive_set_error(&(a->archive), |
1671 | 0 | ARCHIVE_ERRNO_MISC, |
1672 | 0 | "%s decompression failed (%d)", |
1673 | 0 | (xar->entry_encoding == XZ)?"xz":"lzma", |
1674 | 0 | r); |
1675 | 0 | return (ARCHIVE_FATAL); |
1676 | 0 | } |
1677 | 0 | *used = avail_in - xar->lzstream.avail_in; |
1678 | 0 | *outbytes = avail_out - xar->lzstream.avail_out; |
1679 | 0 | break; |
1680 | 0 | #endif |
1681 | | #if !defined(HAVE_BZLIB_H) || !defined(BZ_CONFIG_ERROR) |
1682 | | case BZIP2: |
1683 | | #endif |
1684 | | #if !defined(HAVE_LZMA_H) || !defined(HAVE_LIBLZMA) |
1685 | | case LZMA: |
1686 | | case XZ: |
1687 | | #endif |
1688 | 0 | case NONE: |
1689 | 0 | default: |
1690 | 0 | if (outbuff == xar->outbuff) { |
1691 | 0 | *buff = b; |
1692 | 0 | *used = avail_in; |
1693 | 0 | *outbytes = avail_in; |
1694 | 0 | } else { |
1695 | 0 | if (avail_out > avail_in) |
1696 | 0 | avail_out = avail_in; |
1697 | 0 | memcpy(outbuff, b, avail_out); |
1698 | 0 | *used = avail_out; |
1699 | 0 | *outbytes = avail_out; |
1700 | 0 | } |
1701 | 0 | break; |
1702 | 2.58k | } |
1703 | 2.58k | return (ARCHIVE_OK); |
1704 | 2.58k | } |
1705 | | |
1706 | | static int |
1707 | | decompression_cleanup(struct archive_read *a) |
1708 | 2.49k | { |
1709 | 2.49k | struct xar *xar; |
1710 | 2.49k | int r; |
1711 | | |
1712 | 2.49k | xar = (struct xar *)(a->format->data); |
1713 | 2.49k | r = ARCHIVE_OK; |
1714 | 2.49k | if (xar->stream_valid) { |
1715 | 110 | if (inflateEnd(&(xar->stream)) != Z_OK) { |
1716 | 0 | archive_set_error(&a->archive, |
1717 | 0 | ARCHIVE_ERRNO_MISC, |
1718 | 0 | "Failed to clean up zlib decompressor"); |
1719 | 0 | r = ARCHIVE_FATAL; |
1720 | 0 | } |
1721 | 110 | } |
1722 | 2.49k | #if defined(HAVE_BZLIB_H) && defined(BZ_CONFIG_ERROR) |
1723 | 2.49k | if (xar->bzstream_valid) { |
1724 | 0 | if (BZ2_bzDecompressEnd(&(xar->bzstream)) != BZ_OK) { |
1725 | 0 | archive_set_error(&a->archive, |
1726 | 0 | ARCHIVE_ERRNO_MISC, |
1727 | 0 | "Failed to clean up bzip2 decompressor"); |
1728 | 0 | r = ARCHIVE_FATAL; |
1729 | 0 | } |
1730 | 0 | } |
1731 | 2.49k | #endif |
1732 | 2.49k | #if defined(HAVE_LZMA_H) && defined(HAVE_LIBLZMA) |
1733 | 2.49k | if (xar->lzstream_valid) |
1734 | 0 | lzma_end(&(xar->lzstream)); |
1735 | 2.49k | #endif |
1736 | 2.49k | return (r); |
1737 | 2.49k | } |
1738 | | |
1739 | | static void |
1740 | 2.49k | checksum_cleanup(struct archive_read *a) { |
1741 | 2.49k | struct xar *xar; |
1742 | | |
1743 | 2.49k | xar = (struct xar *)(a->format->data); |
1744 | | |
1745 | 2.49k | _checksum_final(&(xar->a_sumwrk), NULL, 0); |
1746 | 2.49k | _checksum_final(&(xar->e_sumwrk), NULL, 0); |
1747 | 2.49k | } |
1748 | | |
1749 | | static void |
1750 | | xmlattr_cleanup(struct xmlattr_list *list) |
1751 | 87.2k | { |
1752 | 87.2k | struct xmlattr *attr, *next; |
1753 | | |
1754 | 87.2k | attr = list->first; |
1755 | 108k | while (attr != NULL) { |
1756 | 21.3k | next = attr->next; |
1757 | 21.3k | free(attr->name); |
1758 | 21.3k | free(attr->value); |
1759 | 21.3k | free(attr); |
1760 | 21.3k | attr = next; |
1761 | 21.3k | } |
1762 | 87.2k | list->first = NULL; |
1763 | 87.2k | list->last = &(list->first); |
1764 | 87.2k | } |
1765 | | |
1766 | | static int |
1767 | | file_new(struct archive_read *a, struct xar *xar, struct xmlattr_list *list) |
1768 | 19.2k | { |
1769 | 19.2k | struct xar_file *file; |
1770 | 19.2k | struct xmlattr *attr; |
1771 | | |
1772 | 19.2k | file = calloc(1, sizeof(*file)); |
1773 | 19.2k | if (file == NULL) { |
1774 | 0 | archive_set_error(&a->archive, ENOMEM, "Out of memory"); |
1775 | 0 | return (ARCHIVE_FATAL); |
1776 | 0 | } |
1777 | 19.2k | file->parent = xar->file; |
1778 | 19.2k | file->mode = 0777 | AE_IFREG; |
1779 | 19.2k | file->atime = 0; |
1780 | 19.2k | file->mtime = 0; |
1781 | 19.2k | xar->xattr = NULL; |
1782 | 21.2k | for (attr = list->first; attr != NULL; attr = attr->next) { |
1783 | 1.95k | if (strcmp(attr->name, "id") == 0) { |
1784 | 1.72k | int r; |
1785 | | |
1786 | 1.72k | r = atou64(attr->value, strlen(attr->value), |
1787 | 1.72k | 10, &file->id); |
1788 | 1.72k | if (r != ARCHIVE_OK) { |
1789 | 0 | free(file); |
1790 | 0 | return (r); |
1791 | 0 | } |
1792 | 1.72k | } |
1793 | 1.95k | } |
1794 | 19.2k | xar->file = file; |
1795 | 19.2k | file->nlink = 1; |
1796 | 19.2k | if (heap_add_entry(a, &(xar->file_queue), file) != ARCHIVE_OK) |
1797 | 0 | return (ARCHIVE_FATAL); |
1798 | 19.2k | return (ARCHIVE_OK); |
1799 | 19.2k | } |
1800 | | |
1801 | | static void |
1802 | | file_free(struct xar_file *file) |
1803 | 19.2k | { |
1804 | 19.2k | struct xattr *xattr; |
1805 | | |
1806 | 19.2k | archive_string_free(&(file->pathname)); |
1807 | 19.2k | archive_string_free(&(file->symlink)); |
1808 | 19.2k | archive_string_free(&(file->uname)); |
1809 | 19.2k | archive_string_free(&(file->gname)); |
1810 | 19.2k | archive_string_free(&(file->hardlink)); |
1811 | 19.2k | archive_string_free(&(file->fflags_text)); |
1812 | 19.2k | xattr = file->xattr_list; |
1813 | 20.2k | while (xattr != NULL) { |
1814 | 949 | struct xattr *next; |
1815 | | |
1816 | 949 | next = xattr->next; |
1817 | 949 | xattr_free(xattr); |
1818 | 949 | xattr = next; |
1819 | 949 | } |
1820 | | |
1821 | 19.2k | free(file); |
1822 | 19.2k | } |
1823 | | |
1824 | | static int |
1825 | | xattr_new(struct archive_read *a, struct xar *xar, struct xmlattr_list *list) |
1826 | 949 | { |
1827 | 949 | struct xattr *xattr, **nx; |
1828 | 949 | struct xmlattr *attr; |
1829 | | |
1830 | 949 | xattr = calloc(1, sizeof(*xattr)); |
1831 | 949 | if (xattr == NULL) { |
1832 | 0 | archive_set_error(&a->archive, ENOMEM, "Out of memory"); |
1833 | 0 | return (ARCHIVE_FATAL); |
1834 | 0 | } |
1835 | 949 | for (attr = list->first; attr != NULL; attr = attr->next) { |
1836 | 0 | if (strcmp(attr->name, "id") == 0) { |
1837 | 0 | int r; |
1838 | |
|
1839 | 0 | r = atou64(attr->value, strlen(attr->value), |
1840 | 0 | 10, &xattr->id); |
1841 | 0 | if (r != ARCHIVE_OK) { |
1842 | 0 | free(xattr); |
1843 | 0 | return (r); |
1844 | 0 | } |
1845 | 0 | } |
1846 | 0 | } |
1847 | 949 | xar->xattr = xattr; |
1848 | | /* Chain to xattr list. */ |
1849 | 949 | for (nx = &(xar->file->xattr_list); |
1850 | 87.8k | *nx != NULL; nx = &((*nx)->next)) { |
1851 | 86.9k | if (xattr->id < (*nx)->id) |
1852 | 0 | break; |
1853 | 86.9k | } |
1854 | 949 | xattr->next = *nx; |
1855 | 949 | *nx = xattr; |
1856 | | |
1857 | 949 | return (ARCHIVE_OK); |
1858 | 949 | } |
1859 | | |
1860 | | static void |
1861 | | xattr_free(struct xattr *xattr) |
1862 | 949 | { |
1863 | 949 | archive_string_free(&(xattr->name)); |
1864 | 949 | archive_string_free(&(xattr->fstype)); |
1865 | 949 | free(xattr); |
1866 | 949 | } |
1867 | | |
1868 | | static int |
1869 | | getencoding(struct xmlattr_list *list) |
1870 | 2.58k | { |
1871 | 2.58k | struct xmlattr *attr; |
1872 | 2.58k | enum enctype encoding = NONE; |
1873 | | |
1874 | 5.19k | for (attr = list->first; attr != NULL; attr = attr->next) { |
1875 | 2.61k | if (strcmp(attr->name, "style") == 0) { |
1876 | 594 | if (strcmp(attr->value, "application/octet-stream") == 0) |
1877 | 0 | encoding = NONE; |
1878 | 594 | else if (strcmp(attr->value, "application/x-gzip") == 0) |
1879 | 0 | encoding = GZIP; |
1880 | 594 | else if (strcmp(attr->value, "application/x-bzip2") == 0) |
1881 | 0 | encoding = BZIP2; |
1882 | 594 | else if (strcmp(attr->value, "application/x-lzma") == 0) |
1883 | 0 | encoding = LZMA; |
1884 | 594 | else if (strcmp(attr->value, "application/x-xz") == 0) |
1885 | 0 | encoding = XZ; |
1886 | 594 | } |
1887 | 2.61k | } |
1888 | 2.58k | return (encoding); |
1889 | 2.58k | } |
1890 | | |
1891 | | static int |
1892 | | getsumalgorithm(struct xmlattr_list *list) |
1893 | 10.5k | { |
1894 | 10.5k | struct xmlattr *attr; |
1895 | 10.5k | int alg = CKSUM_NONE; |
1896 | | |
1897 | 21.0k | for (attr = list->first; attr != NULL; attr = attr->next) { |
1898 | 10.4k | if (strcmp(attr->name, "style") == 0) { |
1899 | 10.2k | const char *v = attr->value; |
1900 | 10.2k | if ((v[0] == 'S' || v[0] == 's') && |
1901 | 1.23k | (v[1] == 'H' || v[1] == 'h') && |
1902 | 1.23k | (v[2] == 'A' || v[2] == 'a') && |
1903 | 267 | v[3] == '1' && v[4] == '\0') |
1904 | 134 | alg = CKSUM_SHA1; |
1905 | 10.2k | if ((v[0] == 'M' || v[0] == 'm') && |
1906 | 8.97k | (v[1] == 'D' || v[1] == 'd') && |
1907 | 3.25k | v[2] == '5' && v[3] == '\0') |
1908 | 79 | alg = CKSUM_MD5; |
1909 | 10.2k | } |
1910 | 10.4k | } |
1911 | 10.5k | return (alg); |
1912 | 10.5k | } |
1913 | | |
1914 | | static int |
1915 | | unknowntag_start(struct archive_read *a, struct xar *xar, const char *name) |
1916 | 43.7k | { |
1917 | 43.7k | struct unknown_tag *tag; |
1918 | | |
1919 | 43.7k | tag = malloc(sizeof(*tag)); |
1920 | 43.7k | if (tag == NULL) { |
1921 | 0 | archive_set_error(&a->archive, ENOMEM, "Out of memory"); |
1922 | 0 | return (ARCHIVE_FATAL); |
1923 | 0 | } |
1924 | 43.7k | tag->next = xar->unknowntags; |
1925 | 43.7k | archive_string_init(&(tag->name)); |
1926 | 43.7k | archive_strcpy(&(tag->name), name); |
1927 | 43.7k | if (xar->unknowntags == NULL) { |
1928 | | #if DEBUG |
1929 | | fprintf(stderr, "UNKNOWNTAG_START:%s\n", name); |
1930 | | #endif |
1931 | 35.9k | xar->xmlsts_unknown = xar->xmlsts; |
1932 | 35.9k | xar->xmlsts = UNKNOWN; |
1933 | 35.9k | } |
1934 | 43.7k | xar->unknowntags = tag; |
1935 | 43.7k | return (ARCHIVE_OK); |
1936 | 43.7k | } |
1937 | | |
1938 | | static void |
1939 | | unknowntag_end(struct xar *xar, const char *name) |
1940 | 42.8k | { |
1941 | 42.8k | struct unknown_tag *tag; |
1942 | | |
1943 | 42.8k | tag = xar->unknowntags; |
1944 | 42.8k | if (tag == NULL || name == NULL) |
1945 | 0 | return; |
1946 | 42.8k | if (strcmp(tag->name.s, name) == 0) { |
1947 | 42.8k | xar->unknowntags = tag->next; |
1948 | 42.8k | archive_string_free(&(tag->name)); |
1949 | 42.8k | free(tag); |
1950 | 42.8k | if (xar->unknowntags == NULL) { |
1951 | | #if DEBUG |
1952 | | fprintf(stderr, "UNKNOWNTAG_END:%s\n", name); |
1953 | | #endif |
1954 | 35.9k | xar->xmlsts = xar->xmlsts_unknown; |
1955 | 35.9k | } |
1956 | 42.8k | } |
1957 | 42.8k | } |
1958 | | |
1959 | | static int |
1960 | | xml_start(struct archive_read *a, const char *name, struct xmlattr_list *list) |
1961 | 87.2k | { |
1962 | 87.2k | struct xar *xar; |
1963 | 87.2k | struct xmlattr *attr; |
1964 | | |
1965 | 87.2k | xar = (struct xar *)(a->format->data); |
1966 | | |
1967 | | #if DEBUG |
1968 | | fprintf(stderr, "xml_sta:[%s]\n", name); |
1969 | | for (attr = list->first; attr != NULL; attr = attr->next) |
1970 | | fprintf(stderr, " attr:\"%s\"=\"%s\"\n", |
1971 | | attr->name, attr->value); |
1972 | | #endif |
1973 | 87.2k | xar->base64text = 0; |
1974 | 87.2k | switch (xar->xmlsts) { |
1975 | 107 | case INIT: |
1976 | 107 | if (strcmp(name, "xar") == 0) |
1977 | 107 | xar->xmlsts = XAR; |
1978 | 0 | else |
1979 | 0 | if (unknowntag_start(a, xar, name) != ARCHIVE_OK) |
1980 | 0 | return (ARCHIVE_FATAL); |
1981 | 107 | break; |
1982 | 107 | case XAR: |
1983 | 107 | if (strcmp(name, "toc") == 0) |
1984 | 107 | xar->xmlsts = TOC; |
1985 | 0 | else |
1986 | 0 | if (unknowntag_start(a, xar, name) != ARCHIVE_OK) |
1987 | 0 | return (ARCHIVE_FATAL); |
1988 | 107 | break; |
1989 | 18.0k | case TOC: |
1990 | 18.0k | if (strcmp(name, "creation-time") == 0) |
1991 | 182 | xar->xmlsts = TOC_CREATION_TIME; |
1992 | 17.8k | else if (strcmp(name, "checksum") == 0) |
1993 | 259 | xar->xmlsts = TOC_CHECKSUM; |
1994 | 17.6k | else if (strcmp(name, "file") == 0) { |
1995 | 16.3k | if (file_new(a, xar, list) != ARCHIVE_OK) |
1996 | 0 | return (ARCHIVE_FATAL); |
1997 | 16.3k | xar->xmlsts = TOC_FILE; |
1998 | 16.3k | } |
1999 | 1.32k | else |
2000 | 1.32k | if (unknowntag_start(a, xar, name) != ARCHIVE_OK) |
2001 | 0 | return (ARCHIVE_FATAL); |
2002 | 18.0k | break; |
2003 | 18.0k | case TOC_CHECKSUM: |
2004 | 1.74k | if (strcmp(name, "offset") == 0) |
2005 | 650 | xar->xmlsts = TOC_CHECKSUM_OFFSET; |
2006 | 1.09k | else if (strcmp(name, "size") == 0) |
2007 | 227 | xar->xmlsts = TOC_CHECKSUM_SIZE; |
2008 | 864 | else |
2009 | 864 | if (unknowntag_start(a, xar, name) != ARCHIVE_OK) |
2010 | 0 | return (ARCHIVE_FATAL); |
2011 | 1.74k | break; |
2012 | 14.8k | case TOC_FILE: |
2013 | 14.8k | if (strcmp(name, "file") == 0) { |
2014 | 2.98k | if (file_new(a, xar, list) != ARCHIVE_OK) |
2015 | 0 | return (ARCHIVE_FATAL); |
2016 | 2.98k | } |
2017 | 11.8k | else if (strcmp(name, "data") == 0) |
2018 | 1.34k | xar->xmlsts = FILE_DATA; |
2019 | 10.5k | else if (strcmp(name, "ea") == 0) { |
2020 | 949 | if (xattr_new(a, xar, list) != ARCHIVE_OK) |
2021 | 0 | return (ARCHIVE_FATAL); |
2022 | 949 | xar->xmlsts = FILE_EA; |
2023 | 949 | } |
2024 | 9.59k | else if (strcmp(name, "ctime") == 0) |
2025 | 87 | xar->xmlsts = FILE_CTIME; |
2026 | 9.50k | else if (strcmp(name, "mtime") == 0) |
2027 | 88 | xar->xmlsts = FILE_MTIME; |
2028 | 9.41k | else if (strcmp(name, "atime") == 0) |
2029 | 87 | xar->xmlsts = FILE_ATIME; |
2030 | 9.33k | else if (strcmp(name, "group") == 0) |
2031 | 382 | xar->xmlsts = FILE_GROUP; |
2032 | 8.94k | else if (strcmp(name, "gid") == 0) |
2033 | 1.31k | xar->xmlsts = FILE_GID; |
2034 | 7.63k | else if (strcmp(name, "user") == 0) |
2035 | 1.31k | xar->xmlsts = FILE_USER; |
2036 | 6.31k | else if (strcmp(name, "uid") == 0) |
2037 | 0 | xar->xmlsts = FILE_UID; |
2038 | 6.31k | else if (strcmp(name, "mode") == 0) |
2039 | 0 | xar->xmlsts = FILE_MODE; |
2040 | 6.31k | else if (strcmp(name, "device") == 0) |
2041 | 0 | xar->xmlsts = FILE_DEVICE; |
2042 | 6.31k | else if (strcmp(name, "deviceno") == 0) |
2043 | 0 | xar->xmlsts = FILE_DEVICENO; |
2044 | 6.31k | else if (strcmp(name, "inode") == 0) |
2045 | 0 | xar->xmlsts = FILE_INODE; |
2046 | 6.31k | else if (strcmp(name, "link") == 0) |
2047 | 0 | xar->xmlsts = FILE_LINK; |
2048 | 6.31k | else if (strcmp(name, "type") == 0) { |
2049 | 0 | xar->xmlsts = FILE_TYPE; |
2050 | 0 | for (attr = list->first; attr != NULL; |
2051 | 0 | attr = attr->next) { |
2052 | 0 | if (strcmp(attr->name, "link") != 0) |
2053 | 0 | continue; |
2054 | 0 | if (xar->file->hdnext != NULL || xar->file->link != 0 || |
2055 | 0 | xar->file == xar->hdlink_orgs) { |
2056 | 0 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
2057 | 0 | "File with multiple link attributes"); |
2058 | 0 | return (ARCHIVE_FATAL); |
2059 | 0 | } |
2060 | 0 | if (strcmp(attr->value, "original") == 0) { |
2061 | 0 | xar->file->hdnext = xar->hdlink_orgs; |
2062 | 0 | xar->hdlink_orgs = xar->file; |
2063 | 0 | } else { |
2064 | 0 | uint64_t val; |
2065 | 0 | int r; |
2066 | 0 | r = atou64(attr->value, |
2067 | 0 | strlen(attr->value), 10, &val); |
2068 | 0 | if (r != ARCHIVE_OK) { |
2069 | 0 | return (r); |
2070 | 0 | } |
2071 | 0 | if (val > UINT_MAX) { |
2072 | 0 | return (ARCHIVE_FATAL); |
2073 | 0 | } |
2074 | 0 | xar->file->link = (unsigned)val; |
2075 | 0 | if (xar->file->link > 0) |
2076 | 0 | if (add_link(a, xar, xar->file) != ARCHIVE_OK) { |
2077 | 0 | return (ARCHIVE_FATAL); |
2078 | 0 | } |
2079 | 0 | } |
2080 | 0 | } |
2081 | 0 | } |
2082 | 6.31k | else if (strcmp(name, "name") == 0) { |
2083 | 0 | xar->xmlsts = FILE_NAME; |
2084 | 0 | for (attr = list->first; attr != NULL; |
2085 | 0 | attr = attr->next) { |
2086 | 0 | if (strcmp(attr->name, "enctype") == 0 && |
2087 | 0 | strcmp(attr->value, "base64") == 0) |
2088 | 0 | xar->base64text = 1; |
2089 | 0 | } |
2090 | 0 | } |
2091 | 6.31k | else if (strcmp(name, "acl") == 0) |
2092 | 0 | xar->xmlsts = FILE_ACL; |
2093 | 6.31k | else if (strcmp(name, "flags") == 0) |
2094 | 0 | xar->xmlsts = FILE_FLAGS; |
2095 | 6.31k | else if (strcmp(name, "ext2") == 0) |
2096 | 0 | xar->xmlsts = FILE_EXT2; |
2097 | 6.31k | else |
2098 | 6.31k | if (unknowntag_start(a, xar, name) != ARCHIVE_OK) |
2099 | 0 | return (ARCHIVE_FATAL); |
2100 | 14.8k | break; |
2101 | 24.7k | case FILE_DATA: |
2102 | 24.7k | if (strcmp(name, "length") == 0) |
2103 | 343 | xar->xmlsts = FILE_DATA_LENGTH; |
2104 | 24.3k | else if (strcmp(name, "offset") == 0) |
2105 | 938 | xar->xmlsts = FILE_DATA_OFFSET; |
2106 | 23.4k | else if (strcmp(name, "size") == 0) |
2107 | 1.30k | xar->xmlsts = FILE_DATA_SIZE; |
2108 | 22.1k | else if (strcmp(name, "encoding") == 0) { |
2109 | 2.42k | xar->xmlsts = FILE_DATA_ENCODING; |
2110 | 2.42k | xar->file->encoding = getencoding(list); |
2111 | 2.42k | } |
2112 | 19.7k | else if (strcmp(name, "archived-checksum") == 0) { |
2113 | 5.82k | xar->xmlsts = FILE_DATA_A_CHECKSUM; |
2114 | 5.82k | xar->file->a_sum.alg = getsumalgorithm(list); |
2115 | 5.82k | } |
2116 | 13.8k | else if (strcmp(name, "extracted-checksum") == 0) { |
2117 | 4.75k | xar->xmlsts = FILE_DATA_E_CHECKSUM; |
2118 | 4.75k | xar->file->e_sum.alg = getsumalgorithm(list); |
2119 | 4.75k | } |
2120 | 9.14k | else if (strcmp(name, "content") == 0) |
2121 | 0 | xar->xmlsts = FILE_DATA_CONTENT; |
2122 | 9.14k | else |
2123 | 9.14k | if (unknowntag_start(a, xar, name) != ARCHIVE_OK) |
2124 | 0 | return (ARCHIVE_FATAL); |
2125 | 24.7k | break; |
2126 | 24.7k | case FILE_DEVICE: |
2127 | 0 | if (strcmp(name, "major") == 0) |
2128 | 0 | xar->xmlsts = FILE_DEVICE_MAJOR; |
2129 | 0 | else if (strcmp(name, "minor") == 0) |
2130 | 0 | xar->xmlsts = FILE_DEVICE_MINOR; |
2131 | 0 | else |
2132 | 0 | if (unknowntag_start(a, xar, name) != ARCHIVE_OK) |
2133 | 0 | return (ARCHIVE_FATAL); |
2134 | 0 | break; |
2135 | 0 | case FILE_DATA_CONTENT: |
2136 | 0 | if (unknowntag_start(a, xar, name) != ARCHIVE_OK) |
2137 | 0 | return (ARCHIVE_FATAL); |
2138 | 0 | break; |
2139 | 1.77k | case FILE_EA: |
2140 | 1.77k | if (strcmp(name, "length") == 0) |
2141 | 0 | xar->xmlsts = FILE_EA_LENGTH; |
2142 | 1.77k | else if (strcmp(name, "offset") == 0) |
2143 | 280 | xar->xmlsts = FILE_EA_OFFSET; |
2144 | 1.49k | else if (strcmp(name, "size") == 0) |
2145 | 207 | xar->xmlsts = FILE_EA_SIZE; |
2146 | 1.28k | else if (strcmp(name, "encoding") == 0) { |
2147 | 156 | xar->xmlsts = FILE_EA_ENCODING; |
2148 | 156 | xar->xattr->encoding = getencoding(list); |
2149 | 1.12k | } else if (strcmp(name, "archived-checksum") == 0) |
2150 | 814 | xar->xmlsts = FILE_EA_A_CHECKSUM; |
2151 | 315 | else if (strcmp(name, "extracted-checksum") == 0) |
2152 | 0 | xar->xmlsts = FILE_EA_E_CHECKSUM; |
2153 | 315 | else if (strcmp(name, "name") == 0) |
2154 | 0 | xar->xmlsts = FILE_EA_NAME; |
2155 | 315 | else if (strcmp(name, "fstype") == 0) |
2156 | 0 | xar->xmlsts = FILE_EA_FSTYPE; |
2157 | 315 | else |
2158 | 315 | if (unknowntag_start(a, xar, name) != ARCHIVE_OK) |
2159 | 0 | return (ARCHIVE_FATAL); |
2160 | 1.77k | break; |
2161 | 1.77k | case FILE_ACL: |
2162 | 0 | if (strcmp(name, "appleextended") == 0) |
2163 | 0 | xar->xmlsts = FILE_ACL_APPLEEXTENDED; |
2164 | 0 | else if (strcmp(name, "default") == 0) |
2165 | 0 | xar->xmlsts = FILE_ACL_DEFAULT; |
2166 | 0 | else if (strcmp(name, "access") == 0) |
2167 | 0 | xar->xmlsts = FILE_ACL_ACCESS; |
2168 | 0 | else |
2169 | 0 | if (unknowntag_start(a, xar, name) != ARCHIVE_OK) |
2170 | 0 | return (ARCHIVE_FATAL); |
2171 | 0 | break; |
2172 | 0 | case FILE_FLAGS: |
2173 | 0 | if (!xml_parse_file_flags(xar, name)) |
2174 | 0 | if (unknowntag_start(a, xar, name) != ARCHIVE_OK) |
2175 | 0 | return (ARCHIVE_FATAL); |
2176 | 0 | break; |
2177 | 0 | case FILE_EXT2: |
2178 | 0 | if (!xml_parse_file_ext2(xar, name)) |
2179 | 0 | if (unknowntag_start(a, xar, name) != ARCHIVE_OK) |
2180 | 0 | return (ARCHIVE_FATAL); |
2181 | 0 | break; |
2182 | 0 | case TOC_CREATION_TIME: |
2183 | 267 | case TOC_CHECKSUM_OFFSET: |
2184 | 5.33k | case TOC_CHECKSUM_SIZE: |
2185 | 5.33k | case FILE_DATA_LENGTH: |
2186 | 5.33k | case FILE_DATA_OFFSET: |
2187 | 5.33k | case FILE_DATA_SIZE: |
2188 | 5.49k | case FILE_DATA_ENCODING: |
2189 | 6.99k | case FILE_DATA_A_CHECKSUM: |
2190 | 8.78k | case FILE_DATA_E_CHECKSUM: |
2191 | 8.78k | case FILE_EA_LENGTH: |
2192 | 9.02k | case FILE_EA_OFFSET: |
2193 | 9.02k | case FILE_EA_SIZE: |
2194 | 9.02k | case FILE_EA_ENCODING: |
2195 | 16.9k | case FILE_EA_A_CHECKSUM: |
2196 | 16.9k | case FILE_EA_E_CHECKSUM: |
2197 | 16.9k | case FILE_EA_NAME: |
2198 | 16.9k | case FILE_EA_FSTYPE: |
2199 | 16.9k | case FILE_CTIME: |
2200 | 17.0k | case FILE_MTIME: |
2201 | 18.0k | case FILE_ATIME: |
2202 | 18.0k | case FILE_GROUP: |
2203 | 18.0k | case FILE_GID: |
2204 | 18.0k | case FILE_USER: |
2205 | 18.0k | case FILE_UID: |
2206 | 18.0k | case FILE_INODE: |
2207 | 18.0k | case FILE_DEVICE_MAJOR: |
2208 | 18.0k | case FILE_DEVICE_MINOR: |
2209 | 18.0k | case FILE_DEVICENO: |
2210 | 18.0k | case FILE_MODE: |
2211 | 18.0k | case FILE_TYPE: |
2212 | 18.0k | case FILE_LINK: |
2213 | 18.0k | case FILE_NAME: |
2214 | 18.0k | case FILE_ACL_DEFAULT: |
2215 | 18.0k | case FILE_ACL_ACCESS: |
2216 | 18.0k | case FILE_ACL_APPLEEXTENDED: |
2217 | 18.0k | case FILE_FLAGS_USER_NODUMP: |
2218 | 18.0k | case FILE_FLAGS_USER_IMMUTABLE: |
2219 | 18.0k | case FILE_FLAGS_USER_APPEND: |
2220 | 18.0k | case FILE_FLAGS_USER_OPAQUE: |
2221 | 18.0k | case FILE_FLAGS_USER_NOUNLINK: |
2222 | 18.0k | case FILE_FLAGS_SYS_ARCHIVED: |
2223 | 18.0k | case FILE_FLAGS_SYS_IMMUTABLE: |
2224 | 18.0k | case FILE_FLAGS_SYS_APPEND: |
2225 | 18.0k | case FILE_FLAGS_SYS_NOUNLINK: |
2226 | 18.0k | case FILE_FLAGS_SYS_SNAPSHOT: |
2227 | 18.0k | case FILE_EXT2_SecureDeletion: |
2228 | 18.0k | case FILE_EXT2_Undelete: |
2229 | 18.0k | case FILE_EXT2_Compress: |
2230 | 18.0k | case FILE_EXT2_Synchronous: |
2231 | 18.0k | case FILE_EXT2_Immutable: |
2232 | 18.0k | case FILE_EXT2_AppendOnly: |
2233 | 18.0k | case FILE_EXT2_NoDump: |
2234 | 18.0k | case FILE_EXT2_NoAtime: |
2235 | 18.0k | case FILE_EXT2_CompDirty: |
2236 | 18.0k | case FILE_EXT2_CompBlock: |
2237 | 18.0k | case FILE_EXT2_NoCompBlock: |
2238 | 18.0k | case FILE_EXT2_CompError: |
2239 | 18.0k | case FILE_EXT2_BTree: |
2240 | 18.0k | case FILE_EXT2_HashIndexed: |
2241 | 18.0k | case FILE_EXT2_iMagic: |
2242 | 18.0k | case FILE_EXT2_Journaled: |
2243 | 18.0k | case FILE_EXT2_NoTail: |
2244 | 18.0k | case FILE_EXT2_DirSync: |
2245 | 18.0k | case FILE_EXT2_TopDir: |
2246 | 18.0k | case FILE_EXT2_Reserved: |
2247 | 25.8k | case UNKNOWN: |
2248 | 25.8k | if (unknowntag_start(a, xar, name) != ARCHIVE_OK) |
2249 | 0 | return (ARCHIVE_FATAL); |
2250 | 25.8k | break; |
2251 | 87.2k | } |
2252 | 87.2k | return (ARCHIVE_OK); |
2253 | 87.2k | } |
2254 | | |
2255 | | static void |
2256 | | xml_end(void *userData, const char *name) |
2257 | 83.9k | { |
2258 | 83.9k | struct archive_read *a; |
2259 | 83.9k | struct xar *xar; |
2260 | | |
2261 | 83.9k | a = (struct archive_read *)userData; |
2262 | 83.9k | xar = (struct xar *)(a->format->data); |
2263 | | |
2264 | | #if DEBUG |
2265 | | fprintf(stderr, "xml_end:[%s]\n", name); |
2266 | | #endif |
2267 | 83.9k | switch (xar->xmlsts) { |
2268 | 0 | case INIT: |
2269 | 0 | break; |
2270 | 0 | case XAR: |
2271 | 0 | if (strcmp(name, "xar") == 0) |
2272 | 0 | xar->xmlsts = INIT; |
2273 | 0 | break; |
2274 | 0 | case TOC: |
2275 | 0 | if (strcmp(name, "toc") == 0) |
2276 | 0 | xar->xmlsts = XAR; |
2277 | 0 | break; |
2278 | 181 | case TOC_CREATION_TIME: |
2279 | 181 | if (strcmp(name, "creation-time") == 0) |
2280 | 181 | xar->xmlsts = TOC; |
2281 | 181 | break; |
2282 | 252 | case TOC_CHECKSUM: |
2283 | 252 | if (strcmp(name, "checksum") == 0) |
2284 | 252 | xar->xmlsts = TOC; |
2285 | 252 | break; |
2286 | 648 | case TOC_CHECKSUM_OFFSET: |
2287 | 648 | if (strcmp(name, "offset") == 0) |
2288 | 648 | xar->xmlsts = TOC_CHECKSUM; |
2289 | 648 | break; |
2290 | 225 | case TOC_CHECKSUM_SIZE: |
2291 | 225 | if (strcmp(name, "size") == 0) |
2292 | 225 | xar->xmlsts = TOC_CHECKSUM; |
2293 | 225 | break; |
2294 | 17.3k | case TOC_FILE: |
2295 | 17.3k | if (strcmp(name, "file") == 0) { |
2296 | 17.3k | if (xar->file->parent != NULL && |
2297 | 1.09k | ((xar->file->mode & AE_IFMT) == AE_IFDIR)) |
2298 | 0 | xar->file->parent->subdirs++; |
2299 | 17.3k | xar->file = xar->file->parent; |
2300 | 17.3k | if (xar->file == NULL) |
2301 | 16.2k | xar->xmlsts = TOC; |
2302 | 17.3k | } |
2303 | 17.3k | break; |
2304 | 1.29k | case FILE_DATA: |
2305 | 1.29k | if (strcmp(name, "data") == 0) |
2306 | 1.29k | xar->xmlsts = TOC_FILE; |
2307 | 1.29k | break; |
2308 | 343 | case FILE_DATA_LENGTH: |
2309 | 343 | if (strcmp(name, "length") == 0) |
2310 | 343 | xar->xmlsts = FILE_DATA; |
2311 | 343 | break; |
2312 | 938 | case FILE_DATA_OFFSET: |
2313 | 938 | if (strcmp(name, "offset") == 0) |
2314 | 938 | xar->xmlsts = FILE_DATA; |
2315 | 938 | break; |
2316 | 1.30k | case FILE_DATA_SIZE: |
2317 | 1.30k | if (strcmp(name, "size") == 0) |
2318 | 1.30k | xar->xmlsts = FILE_DATA; |
2319 | 1.30k | break; |
2320 | 2.42k | case FILE_DATA_ENCODING: |
2321 | 2.42k | if (strcmp(name, "encoding") == 0) |
2322 | 2.42k | xar->xmlsts = FILE_DATA; |
2323 | 2.42k | break; |
2324 | 5.81k | case FILE_DATA_A_CHECKSUM: |
2325 | 5.81k | if (strcmp(name, "archived-checksum") == 0) |
2326 | 5.81k | xar->xmlsts = FILE_DATA; |
2327 | 5.81k | break; |
2328 | 4.74k | case FILE_DATA_E_CHECKSUM: |
2329 | 4.74k | if (strcmp(name, "extracted-checksum") == 0) |
2330 | 4.74k | xar->xmlsts = FILE_DATA; |
2331 | 4.74k | break; |
2332 | 0 | case FILE_DATA_CONTENT: |
2333 | 0 | if (strcmp(name, "content") == 0) |
2334 | 0 | xar->xmlsts = FILE_DATA; |
2335 | 0 | break; |
2336 | 926 | case FILE_EA: |
2337 | 926 | if (strcmp(name, "ea") == 0) { |
2338 | 926 | xar->xmlsts = TOC_FILE; |
2339 | 926 | xar->xattr = NULL; |
2340 | 926 | } |
2341 | 926 | break; |
2342 | 0 | case FILE_EA_LENGTH: |
2343 | 0 | if (strcmp(name, "length") == 0) |
2344 | 0 | xar->xmlsts = FILE_EA; |
2345 | 0 | break; |
2346 | 278 | case FILE_EA_OFFSET: |
2347 | 278 | if (strcmp(name, "offset") == 0) |
2348 | 278 | xar->xmlsts = FILE_EA; |
2349 | 278 | break; |
2350 | 207 | case FILE_EA_SIZE: |
2351 | 207 | if (strcmp(name, "size") == 0) |
2352 | 207 | xar->xmlsts = FILE_EA; |
2353 | 207 | break; |
2354 | 156 | case FILE_EA_ENCODING: |
2355 | 156 | if (strcmp(name, "encoding") == 0) |
2356 | 156 | xar->xmlsts = FILE_EA; |
2357 | 156 | break; |
2358 | 807 | case FILE_EA_A_CHECKSUM: |
2359 | 807 | if (strcmp(name, "archived-checksum") == 0) |
2360 | 807 | xar->xmlsts = FILE_EA; |
2361 | 807 | break; |
2362 | 0 | case FILE_EA_E_CHECKSUM: |
2363 | 0 | if (strcmp(name, "extracted-checksum") == 0) |
2364 | 0 | xar->xmlsts = FILE_EA; |
2365 | 0 | break; |
2366 | 0 | case FILE_EA_NAME: |
2367 | 0 | if (strcmp(name, "name") == 0) |
2368 | 0 | xar->xmlsts = FILE_EA; |
2369 | 0 | break; |
2370 | 0 | case FILE_EA_FSTYPE: |
2371 | 0 | if (strcmp(name, "fstype") == 0) |
2372 | 0 | xar->xmlsts = FILE_EA; |
2373 | 0 | break; |
2374 | 87 | case FILE_CTIME: |
2375 | 87 | if (strcmp(name, "ctime") == 0) |
2376 | 87 | xar->xmlsts = TOC_FILE; |
2377 | 87 | break; |
2378 | 87 | case FILE_MTIME: |
2379 | 87 | if (strcmp(name, "mtime") == 0) |
2380 | 87 | xar->xmlsts = TOC_FILE; |
2381 | 87 | break; |
2382 | 83 | case FILE_ATIME: |
2383 | 83 | if (strcmp(name, "atime") == 0) |
2384 | 83 | xar->xmlsts = TOC_FILE; |
2385 | 83 | break; |
2386 | 382 | case FILE_GROUP: |
2387 | 382 | if (strcmp(name, "group") == 0) |
2388 | 382 | xar->xmlsts = TOC_FILE; |
2389 | 382 | break; |
2390 | 1.31k | case FILE_GID: |
2391 | 1.31k | if (strcmp(name, "gid") == 0) |
2392 | 1.31k | xar->xmlsts = TOC_FILE; |
2393 | 1.31k | break; |
2394 | 1.31k | case FILE_USER: |
2395 | 1.31k | if (strcmp(name, "user") == 0) |
2396 | 1.31k | xar->xmlsts = TOC_FILE; |
2397 | 1.31k | break; |
2398 | 0 | case FILE_UID: |
2399 | 0 | if (strcmp(name, "uid") == 0) |
2400 | 0 | xar->xmlsts = TOC_FILE; |
2401 | 0 | break; |
2402 | 0 | case FILE_MODE: |
2403 | 0 | if (strcmp(name, "mode") == 0) |
2404 | 0 | xar->xmlsts = TOC_FILE; |
2405 | 0 | break; |
2406 | 0 | case FILE_DEVICE: |
2407 | 0 | if (strcmp(name, "device") == 0) |
2408 | 0 | xar->xmlsts = TOC_FILE; |
2409 | 0 | break; |
2410 | 0 | case FILE_DEVICE_MAJOR: |
2411 | 0 | if (strcmp(name, "major") == 0) |
2412 | 0 | xar->xmlsts = FILE_DEVICE; |
2413 | 0 | break; |
2414 | 0 | case FILE_DEVICE_MINOR: |
2415 | 0 | if (strcmp(name, "minor") == 0) |
2416 | 0 | xar->xmlsts = FILE_DEVICE; |
2417 | 0 | break; |
2418 | 0 | case FILE_DEVICENO: |
2419 | 0 | if (strcmp(name, "deviceno") == 0) |
2420 | 0 | xar->xmlsts = TOC_FILE; |
2421 | 0 | break; |
2422 | 0 | case FILE_INODE: |
2423 | 0 | if (strcmp(name, "inode") == 0) |
2424 | 0 | xar->xmlsts = TOC_FILE; |
2425 | 0 | break; |
2426 | 0 | case FILE_LINK: |
2427 | 0 | if (strcmp(name, "link") == 0) |
2428 | 0 | xar->xmlsts = TOC_FILE; |
2429 | 0 | break; |
2430 | 0 | case FILE_TYPE: |
2431 | 0 | if (strcmp(name, "type") == 0) |
2432 | 0 | xar->xmlsts = TOC_FILE; |
2433 | 0 | break; |
2434 | 0 | case FILE_NAME: |
2435 | 0 | if (strcmp(name, "name") == 0) |
2436 | 0 | xar->xmlsts = TOC_FILE; |
2437 | 0 | break; |
2438 | 0 | case FILE_ACL: |
2439 | 0 | if (strcmp(name, "acl") == 0) |
2440 | 0 | xar->xmlsts = TOC_FILE; |
2441 | 0 | break; |
2442 | 0 | case FILE_ACL_DEFAULT: |
2443 | 0 | if (strcmp(name, "default") == 0) |
2444 | 0 | xar->xmlsts = FILE_ACL; |
2445 | 0 | break; |
2446 | 0 | case FILE_ACL_ACCESS: |
2447 | 0 | if (strcmp(name, "access") == 0) |
2448 | 0 | xar->xmlsts = FILE_ACL; |
2449 | 0 | break; |
2450 | 0 | case FILE_ACL_APPLEEXTENDED: |
2451 | 0 | if (strcmp(name, "appleextended") == 0) |
2452 | 0 | xar->xmlsts = FILE_ACL; |
2453 | 0 | break; |
2454 | 0 | case FILE_FLAGS: |
2455 | 0 | if (strcmp(name, "flags") == 0) |
2456 | 0 | xar->xmlsts = TOC_FILE; |
2457 | 0 | break; |
2458 | 0 | case FILE_FLAGS_USER_NODUMP: |
2459 | 0 | if (strcmp(name, "UserNoDump") == 0) |
2460 | 0 | xar->xmlsts = FILE_FLAGS; |
2461 | 0 | break; |
2462 | 0 | case FILE_FLAGS_USER_IMMUTABLE: |
2463 | 0 | if (strcmp(name, "UserImmutable") == 0) |
2464 | 0 | xar->xmlsts = FILE_FLAGS; |
2465 | 0 | break; |
2466 | 0 | case FILE_FLAGS_USER_APPEND: |
2467 | 0 | if (strcmp(name, "UserAppend") == 0) |
2468 | 0 | xar->xmlsts = FILE_FLAGS; |
2469 | 0 | break; |
2470 | 0 | case FILE_FLAGS_USER_OPAQUE: |
2471 | 0 | if (strcmp(name, "UserOpaque") == 0) |
2472 | 0 | xar->xmlsts = FILE_FLAGS; |
2473 | 0 | break; |
2474 | 0 | case FILE_FLAGS_USER_NOUNLINK: |
2475 | 0 | if (strcmp(name, "UserNoUnlink") == 0) |
2476 | 0 | xar->xmlsts = FILE_FLAGS; |
2477 | 0 | break; |
2478 | 0 | case FILE_FLAGS_SYS_ARCHIVED: |
2479 | 0 | if (strcmp(name, "SystemArchived") == 0) |
2480 | 0 | xar->xmlsts = FILE_FLAGS; |
2481 | 0 | break; |
2482 | 0 | case FILE_FLAGS_SYS_IMMUTABLE: |
2483 | 0 | if (strcmp(name, "SystemImmutable") == 0) |
2484 | 0 | xar->xmlsts = FILE_FLAGS; |
2485 | 0 | break; |
2486 | 0 | case FILE_FLAGS_SYS_APPEND: |
2487 | 0 | if (strcmp(name, "SystemAppend") == 0) |
2488 | 0 | xar->xmlsts = FILE_FLAGS; |
2489 | 0 | break; |
2490 | 0 | case FILE_FLAGS_SYS_NOUNLINK: |
2491 | 0 | if (strcmp(name, "SystemNoUnlink") == 0) |
2492 | 0 | xar->xmlsts = FILE_FLAGS; |
2493 | 0 | break; |
2494 | 0 | case FILE_FLAGS_SYS_SNAPSHOT: |
2495 | 0 | if (strcmp(name, "SystemSnapshot") == 0) |
2496 | 0 | xar->xmlsts = FILE_FLAGS; |
2497 | 0 | break; |
2498 | 0 | case FILE_EXT2: |
2499 | 0 | if (strcmp(name, "ext2") == 0) |
2500 | 0 | xar->xmlsts = TOC_FILE; |
2501 | 0 | break; |
2502 | 0 | case FILE_EXT2_SecureDeletion: |
2503 | 0 | if (strcmp(name, "SecureDeletion") == 0) |
2504 | 0 | xar->xmlsts = FILE_EXT2; |
2505 | 0 | break; |
2506 | 0 | case FILE_EXT2_Undelete: |
2507 | 0 | if (strcmp(name, "Undelete") == 0) |
2508 | 0 | xar->xmlsts = FILE_EXT2; |
2509 | 0 | break; |
2510 | 0 | case FILE_EXT2_Compress: |
2511 | 0 | if (strcmp(name, "Compress") == 0) |
2512 | 0 | xar->xmlsts = FILE_EXT2; |
2513 | 0 | break; |
2514 | 0 | case FILE_EXT2_Synchronous: |
2515 | 0 | if (strcmp(name, "Synchronous") == 0) |
2516 | 0 | xar->xmlsts = FILE_EXT2; |
2517 | 0 | break; |
2518 | 0 | case FILE_EXT2_Immutable: |
2519 | 0 | if (strcmp(name, "Immutable") == 0) |
2520 | 0 | xar->xmlsts = FILE_EXT2; |
2521 | 0 | break; |
2522 | 0 | case FILE_EXT2_AppendOnly: |
2523 | 0 | if (strcmp(name, "AppendOnly") == 0) |
2524 | 0 | xar->xmlsts = FILE_EXT2; |
2525 | 0 | break; |
2526 | 0 | case FILE_EXT2_NoDump: |
2527 | 0 | if (strcmp(name, "NoDump") == 0) |
2528 | 0 | xar->xmlsts = FILE_EXT2; |
2529 | 0 | break; |
2530 | 0 | case FILE_EXT2_NoAtime: |
2531 | 0 | if (strcmp(name, "NoAtime") == 0) |
2532 | 0 | xar->xmlsts = FILE_EXT2; |
2533 | 0 | break; |
2534 | 0 | case FILE_EXT2_CompDirty: |
2535 | 0 | if (strcmp(name, "CompDirty") == 0) |
2536 | 0 | xar->xmlsts = FILE_EXT2; |
2537 | 0 | break; |
2538 | 0 | case FILE_EXT2_CompBlock: |
2539 | 0 | if (strcmp(name, "CompBlock") == 0) |
2540 | 0 | xar->xmlsts = FILE_EXT2; |
2541 | 0 | break; |
2542 | 0 | case FILE_EXT2_NoCompBlock: |
2543 | 0 | if (strcmp(name, "NoCompBlock") == 0) |
2544 | 0 | xar->xmlsts = FILE_EXT2; |
2545 | 0 | break; |
2546 | 0 | case FILE_EXT2_CompError: |
2547 | 0 | if (strcmp(name, "CompError") == 0) |
2548 | 0 | xar->xmlsts = FILE_EXT2; |
2549 | 0 | break; |
2550 | 0 | case FILE_EXT2_BTree: |
2551 | 0 | if (strcmp(name, "BTree") == 0) |
2552 | 0 | xar->xmlsts = FILE_EXT2; |
2553 | 0 | break; |
2554 | 0 | case FILE_EXT2_HashIndexed: |
2555 | 0 | if (strcmp(name, "HashIndexed") == 0) |
2556 | 0 | xar->xmlsts = FILE_EXT2; |
2557 | 0 | break; |
2558 | 0 | case FILE_EXT2_iMagic: |
2559 | 0 | if (strcmp(name, "iMagic") == 0) |
2560 | 0 | xar->xmlsts = FILE_EXT2; |
2561 | 0 | break; |
2562 | 0 | case FILE_EXT2_Journaled: |
2563 | 0 | if (strcmp(name, "Journaled") == 0) |
2564 | 0 | xar->xmlsts = FILE_EXT2; |
2565 | 0 | break; |
2566 | 0 | case FILE_EXT2_NoTail: |
2567 | 0 | if (strcmp(name, "NoTail") == 0) |
2568 | 0 | xar->xmlsts = FILE_EXT2; |
2569 | 0 | break; |
2570 | 0 | case FILE_EXT2_DirSync: |
2571 | 0 | if (strcmp(name, "DirSync") == 0) |
2572 | 0 | xar->xmlsts = FILE_EXT2; |
2573 | 0 | break; |
2574 | 0 | case FILE_EXT2_TopDir: |
2575 | 0 | if (strcmp(name, "TopDir") == 0) |
2576 | 0 | xar->xmlsts = FILE_EXT2; |
2577 | 0 | break; |
2578 | 0 | case FILE_EXT2_Reserved: |
2579 | 0 | if (strcmp(name, "Reserved") == 0) |
2580 | 0 | xar->xmlsts = FILE_EXT2; |
2581 | 0 | break; |
2582 | 42.8k | case UNKNOWN: |
2583 | 42.8k | unknowntag_end(xar, name); |
2584 | 42.8k | break; |
2585 | 83.9k | } |
2586 | 83.9k | } |
2587 | | |
2588 | | static const int base64[256] = { |
2589 | | -1, -1, -1, -1, -1, -1, -1, -1, |
2590 | | -1, -1, -1, -1, -1, -1, -1, -1, /* 00 - 0F */ |
2591 | | -1, -1, -1, -1, -1, -1, -1, -1, |
2592 | | -1, -1, -1, -1, -1, -1, -1, -1, /* 10 - 1F */ |
2593 | | -1, -1, -1, -1, -1, -1, -1, -1, |
2594 | | -1, -1, -1, 62, -1, -1, -1, 63, /* 20 - 2F */ |
2595 | | 52, 53, 54, 55, 56, 57, 58, 59, |
2596 | | 60, 61, -1, -1, -1, -1, -1, -1, /* 30 - 3F */ |
2597 | | -1, 0, 1, 2, 3, 4, 5, 6, |
2598 | | 7, 8, 9, 10, 11, 12, 13, 14, /* 40 - 4F */ |
2599 | | 15, 16, 17, 18, 19, 20, 21, 22, |
2600 | | 23, 24, 25, -1, -1, -1, -1, -1, /* 50 - 5F */ |
2601 | | -1, 26, 27, 28, 29, 30, 31, 32, |
2602 | | 33, 34, 35, 36, 37, 38, 39, 40, /* 60 - 6F */ |
2603 | | 41, 42, 43, 44, 45, 46, 47, 48, |
2604 | | 49, 50, 51, -1, -1, -1, -1, -1, /* 70 - 7F */ |
2605 | | -1, -1, -1, -1, -1, -1, -1, -1, |
2606 | | -1, -1, -1, -1, -1, -1, -1, -1, /* 80 - 8F */ |
2607 | | -1, -1, -1, -1, -1, -1, -1, -1, |
2608 | | -1, -1, -1, -1, -1, -1, -1, -1, /* 90 - 9F */ |
2609 | | -1, -1, -1, -1, -1, -1, -1, -1, |
2610 | | -1, -1, -1, -1, -1, -1, -1, -1, /* A0 - AF */ |
2611 | | -1, -1, -1, -1, -1, -1, -1, -1, |
2612 | | -1, -1, -1, -1, -1, -1, -1, -1, /* B0 - BF */ |
2613 | | -1, -1, -1, -1, -1, -1, -1, -1, |
2614 | | -1, -1, -1, -1, -1, -1, -1, -1, /* C0 - CF */ |
2615 | | -1, -1, -1, -1, -1, -1, -1, -1, |
2616 | | -1, -1, -1, -1, -1, -1, -1, -1, /* D0 - DF */ |
2617 | | -1, -1, -1, -1, -1, -1, -1, -1, |
2618 | | -1, -1, -1, -1, -1, -1, -1, -1, /* E0 - EF */ |
2619 | | -1, -1, -1, -1, -1, -1, -1, -1, |
2620 | | -1, -1, -1, -1, -1, -1, -1, -1, /* F0 - FF */ |
2621 | | }; |
2622 | | |
2623 | | static void |
2624 | | strappend_base64(struct xar *xar, |
2625 | | struct archive_string *as, const char *s, size_t l) |
2626 | 0 | { |
2627 | 0 | unsigned char buff[256]; |
2628 | 0 | unsigned char *out; |
2629 | 0 | const unsigned char *b; |
2630 | 0 | size_t len; |
2631 | |
|
2632 | 0 | (void)xar; /* UNUSED */ |
2633 | 0 | len = 0; |
2634 | 0 | out = buff; |
2635 | 0 | b = (const unsigned char *)s; |
2636 | 0 | while (l > 0) { |
2637 | 0 | int n = 0; |
2638 | |
|
2639 | 0 | if (base64[b[0]] < 0 || base64[b[1]] < 0) |
2640 | 0 | break; |
2641 | 0 | n = base64[*b++] << 18; |
2642 | 0 | n |= base64[*b++] << 12; |
2643 | 0 | *out++ = n >> 16; |
2644 | 0 | len++; |
2645 | 0 | l -= 2; |
2646 | |
|
2647 | 0 | if (l > 0) { |
2648 | 0 | if (base64[*b] < 0) |
2649 | 0 | break; |
2650 | 0 | n |= base64[*b++] << 6; |
2651 | 0 | *out++ = (n >> 8) & 0xFF; |
2652 | 0 | len++; |
2653 | 0 | --l; |
2654 | 0 | } |
2655 | 0 | if (l > 0) { |
2656 | 0 | if (base64[*b] < 0) |
2657 | 0 | break; |
2658 | 0 | n |= base64[*b++]; |
2659 | 0 | *out++ = n & 0xFF; |
2660 | 0 | len++; |
2661 | 0 | --l; |
2662 | 0 | } |
2663 | 0 | if (len+3 >= sizeof(buff)) { |
2664 | 0 | archive_strncat(as, (const char *)buff, len); |
2665 | 0 | len = 0; |
2666 | 0 | out = buff; |
2667 | 0 | } |
2668 | 0 | } |
2669 | 0 | if (len > 0) |
2670 | 0 | archive_strncat(as, (const char *)buff, len); |
2671 | 0 | } |
2672 | | |
2673 | | static int |
2674 | | is_string(const char *known, const char *data, size_t len) |
2675 | 0 | { |
2676 | 0 | if (strlen(known) != len) |
2677 | 0 | return -1; |
2678 | 0 | return memcmp(data, known, len); |
2679 | 0 | } |
2680 | | |
2681 | | static int |
2682 | | xml_data(void *userData, const char *s, size_t len) |
2683 | 110k | { |
2684 | 110k | uint64_t val; |
2685 | 110k | struct archive_read *a; |
2686 | 110k | struct xar *xar; |
2687 | 110k | int r; |
2688 | | |
2689 | 110k | a = (struct archive_read *)userData; |
2690 | 110k | xar = (struct xar *)(a->format->data); |
2691 | | |
2692 | | #if DEBUG |
2693 | | { |
2694 | | char buff[1024]; |
2695 | | size_t dlen = len; |
2696 | | if (dlen > sizeof(buff) - 1) |
2697 | | dlen = sizeof(buff) - 1; |
2698 | | strncpy(buff, s, dlen); |
2699 | | buff[dlen] = 0; |
2700 | | fprintf(stderr, "\tlen=%zu:\"%s\"\n", dlen, buff); |
2701 | | } |
2702 | | #endif |
2703 | 110k | switch (xar->xmlsts) { |
2704 | 915 | case TOC_CHECKSUM_OFFSET: |
2705 | 915 | r = atou64(s, len, 10, &xar->toc_chksum_offset); |
2706 | 915 | if (r != ARCHIVE_OK) |
2707 | 0 | return (r); |
2708 | 915 | break; |
2709 | 5.29k | case TOC_CHECKSUM_SIZE: |
2710 | 5.29k | r = atou64(s, len, 10, &xar->toc_chksum_size); |
2711 | 5.29k | if (r != ARCHIVE_OK) |
2712 | 0 | return (r); |
2713 | 5.29k | break; |
2714 | 104k | default: |
2715 | 104k | break; |
2716 | 110k | } |
2717 | 110k | if (xar->file == NULL) |
2718 | 30.0k | return (ARCHIVE_OK); |
2719 | | |
2720 | 80.8k | switch (xar->xmlsts) { |
2721 | 0 | case FILE_NAME: |
2722 | 0 | if (xar->file->has & HAS_PATHNAME) |
2723 | 0 | break; |
2724 | | |
2725 | 0 | if (xar->file->parent != NULL) { |
2726 | 0 | archive_string_concat(&(xar->file->pathname), |
2727 | 0 | &(xar->file->parent->pathname)); |
2728 | 0 | archive_strappend_char(&(xar->file->pathname), '/'); |
2729 | 0 | } |
2730 | 0 | xar->file->has |= HAS_PATHNAME; |
2731 | 0 | if (xar->base64text) { |
2732 | 0 | strappend_base64(xar, |
2733 | 0 | &(xar->file->pathname), s, len); |
2734 | 0 | } else |
2735 | 0 | archive_strncat(&(xar->file->pathname), s, len); |
2736 | 0 | break; |
2737 | 0 | case FILE_LINK: |
2738 | 0 | xar->file->has |= HAS_SYMLINK; |
2739 | 0 | archive_strncpy(&(xar->file->symlink), s, len); |
2740 | 0 | break; |
2741 | 0 | case FILE_TYPE: |
2742 | 0 | if (is_string("file", s, len) == 0 || |
2743 | 0 | is_string("hardlink", s, len) == 0) |
2744 | 0 | xar->file->mode = |
2745 | 0 | (xar->file->mode & ~AE_IFMT) | AE_IFREG; |
2746 | 0 | if (is_string("directory", s, len) == 0) |
2747 | 0 | xar->file->mode = |
2748 | 0 | (xar->file->mode & ~AE_IFMT) | AE_IFDIR; |
2749 | 0 | if (is_string("symlink", s, len) == 0) |
2750 | 0 | xar->file->mode = |
2751 | 0 | (xar->file->mode & ~AE_IFMT) | AE_IFLNK; |
2752 | 0 | if (is_string("character special", s, len) == 0) |
2753 | 0 | xar->file->mode = |
2754 | 0 | (xar->file->mode & ~AE_IFMT) | AE_IFCHR; |
2755 | 0 | if (is_string("block special", s, len) == 0) |
2756 | 0 | xar->file->mode = |
2757 | 0 | (xar->file->mode & ~AE_IFMT) | AE_IFBLK; |
2758 | 0 | if (is_string("socket", s, len) == 0) |
2759 | 0 | xar->file->mode = |
2760 | 0 | (xar->file->mode & ~AE_IFMT) | AE_IFSOCK; |
2761 | 0 | if (is_string("fifo", s, len) == 0) |
2762 | 0 | xar->file->mode = |
2763 | 0 | (xar->file->mode & ~AE_IFMT) | AE_IFIFO; |
2764 | 0 | xar->file->has |= HAS_TYPE; |
2765 | 0 | break; |
2766 | 0 | case FILE_INODE: |
2767 | 0 | r = atou64(s, len, 10, &val); |
2768 | 0 | if (r != ARCHIVE_OK) |
2769 | 0 | return (r); |
2770 | 0 | if (val > (uint64_t)INT64_MAX) |
2771 | 0 | return (ARCHIVE_FATAL); |
2772 | 0 | xar->file->has |= HAS_INO; |
2773 | 0 | xar->file->ino64 = (int64_t)val; |
2774 | 0 | break; |
2775 | 0 | case FILE_DEVICE_MAJOR: |
2776 | 0 | r = atou64(s, len, 10, &val); |
2777 | 0 | if (r != ARCHIVE_OK) |
2778 | 0 | return (r); |
2779 | 0 | if (val != (dev_t)val) |
2780 | 0 | return (ARCHIVE_FATAL); |
2781 | 0 | xar->file->has |= HAS_DEVMAJOR; |
2782 | 0 | xar->file->devmajor = (dev_t)val; |
2783 | 0 | break; |
2784 | 0 | case FILE_DEVICE_MINOR: |
2785 | 0 | r = atou64(s, len, 10, &val); |
2786 | 0 | if (r != ARCHIVE_OK) |
2787 | 0 | return (r); |
2788 | 0 | if (val != (dev_t)val) |
2789 | 0 | return (ARCHIVE_FATAL); |
2790 | 0 | xar->file->has |= HAS_DEVMINOR; |
2791 | 0 | xar->file->devminor = (dev_t)val; |
2792 | 0 | break; |
2793 | 0 | case FILE_DEVICENO: |
2794 | 0 | r = atou64(s, len, 10, &val); |
2795 | 0 | if (r != ARCHIVE_OK) |
2796 | 0 | return (r); |
2797 | 0 | if (val != (dev_t)val) |
2798 | 0 | return (ARCHIVE_FATAL); |
2799 | 0 | xar->file->has |= HAS_DEV; |
2800 | 0 | xar->file->dev = (dev_t)val; |
2801 | 0 | break; |
2802 | 0 | case FILE_MODE: |
2803 | 0 | r = atou64(s, len, 8, &val); |
2804 | 0 | if (r != ARCHIVE_OK) |
2805 | 0 | return (r); |
2806 | 0 | if (val != (mode_t)val) |
2807 | 0 | return (ARCHIVE_FATAL); |
2808 | 0 | xar->file->has |= HAS_MODE; |
2809 | 0 | xar->file->mode = |
2810 | 0 | (xar->file->mode & AE_IFMT) | ((mode_t)val & ~AE_IFMT); |
2811 | 0 | break; |
2812 | 382 | case FILE_GROUP: |
2813 | 382 | xar->file->has |= HAS_GID; |
2814 | 382 | archive_strncpy(&(xar->file->gname), s, len); |
2815 | 382 | break; |
2816 | 1.31k | case FILE_GID: |
2817 | 1.31k | r = atou64(s, len, 10, &val); |
2818 | 1.31k | if (r != ARCHIVE_OK) |
2819 | 0 | return (r); |
2820 | 1.31k | if (val > (uint64_t)INT64_MAX) |
2821 | 0 | return (ARCHIVE_FATAL); |
2822 | 1.31k | xar->file->has |= HAS_GID; |
2823 | 1.31k | xar->file->gid = (int64_t)val; |
2824 | 1.31k | break; |
2825 | 1.31k | case FILE_USER: |
2826 | 1.31k | xar->file->has |= HAS_UID; |
2827 | 1.31k | archive_strncpy(&(xar->file->uname), s, len); |
2828 | 1.31k | break; |
2829 | 0 | case FILE_UID: |
2830 | 0 | r = atou64(s, len, 10, &val); |
2831 | 0 | if (r != ARCHIVE_OK) |
2832 | 0 | return (r); |
2833 | 0 | if (val > (uint64_t)INT64_MAX) |
2834 | 0 | return (ARCHIVE_FATAL); |
2835 | 0 | xar->file->has |= HAS_UID; |
2836 | 0 | xar->file->uid = (int64_t)val; |
2837 | 0 | break; |
2838 | 87 | case FILE_CTIME: |
2839 | 87 | xar->file->has |= HAS_TIME | HAS_CTIME; |
2840 | 87 | xar->file->ctime = parse_time(s, len); |
2841 | 87 | break; |
2842 | 88 | case FILE_MTIME: |
2843 | 88 | xar->file->has |= HAS_TIME | HAS_MTIME; |
2844 | 88 | xar->file->mtime = parse_time(s, len); |
2845 | 88 | break; |
2846 | 1.08k | case FILE_ATIME: |
2847 | 1.08k | xar->file->has |= HAS_TIME | HAS_ATIME; |
2848 | 1.08k | xar->file->atime = parse_time(s, len); |
2849 | 1.08k | break; |
2850 | 343 | case FILE_DATA_LENGTH: |
2851 | 343 | r = atou64(s, len, 10, &xar->file->length); |
2852 | 343 | if (r != ARCHIVE_OK) |
2853 | 0 | return (r); |
2854 | 343 | xar->file->has |= HAS_DATA; |
2855 | 343 | break; |
2856 | 938 | case FILE_DATA_OFFSET: |
2857 | 938 | r = atou64(s, len, 10, &xar->file->offset); |
2858 | 938 | if (r != ARCHIVE_OK) |
2859 | 0 | return (r); |
2860 | 938 | xar->file->has |= HAS_DATA; |
2861 | 938 | break; |
2862 | 1.30k | case FILE_DATA_SIZE: |
2863 | 1.30k | r = atou64(s, len, 10, &xar->file->size); |
2864 | 1.30k | if (r != ARCHIVE_OK) |
2865 | 0 | return (r); |
2866 | 1.30k | xar->file->has |= HAS_DATA; |
2867 | 1.30k | break; |
2868 | 7.32k | case FILE_DATA_A_CHECKSUM: |
2869 | 7.32k | xar->file->a_sum.len = atohex(xar->file->a_sum.val, |
2870 | 7.32k | sizeof(xar->file->a_sum.val), s, len); |
2871 | 7.32k | break; |
2872 | 1.79k | case FILE_DATA_E_CHECKSUM: |
2873 | 1.79k | xar->file->e_sum.len = atohex(xar->file->e_sum.val, |
2874 | 1.79k | sizeof(xar->file->e_sum.val), s, len); |
2875 | 1.79k | break; |
2876 | 0 | case FILE_EA_LENGTH: |
2877 | 0 | r = atou64(s, len, 10, &xar->xattr->length); |
2878 | 0 | if (r != ARCHIVE_OK) |
2879 | 0 | return (r); |
2880 | 0 | xar->file->has |= HAS_XATTR; |
2881 | 0 | break; |
2882 | 513 | case FILE_EA_OFFSET: |
2883 | 513 | r = atou64(s, len, 10, &xar->xattr->offset); |
2884 | 513 | if (r != ARCHIVE_OK) |
2885 | 0 | return (r); |
2886 | 513 | xar->file->has |= HAS_XATTR; |
2887 | 513 | break; |
2888 | 207 | case FILE_EA_SIZE: |
2889 | 207 | r = atou64(s, len, 10, &xar->xattr->size); |
2890 | 207 | if (r != ARCHIVE_OK) |
2891 | 0 | return (r); |
2892 | 207 | xar->file->has |= HAS_XATTR; |
2893 | 207 | break; |
2894 | 6.13k | case FILE_EA_A_CHECKSUM: |
2895 | 6.13k | xar->file->has |= HAS_XATTR; |
2896 | 6.13k | xar->xattr->a_sum.len = atohex(xar->xattr->a_sum.val, |
2897 | 6.13k | sizeof(xar->xattr->a_sum.val), s, len); |
2898 | 6.13k | break; |
2899 | 0 | case FILE_EA_E_CHECKSUM: |
2900 | 0 | xar->file->has |= HAS_XATTR; |
2901 | 0 | xar->xattr->e_sum.len = atohex(xar->xattr->e_sum.val, |
2902 | 0 | sizeof(xar->xattr->e_sum.val), s, len); |
2903 | 0 | break; |
2904 | 0 | case FILE_EA_NAME: |
2905 | 0 | xar->file->has |= HAS_XATTR; |
2906 | 0 | archive_strncpy(&(xar->xattr->name), s, len); |
2907 | 0 | break; |
2908 | 0 | case FILE_EA_FSTYPE: |
2909 | 0 | xar->file->has |= HAS_XATTR; |
2910 | 0 | archive_strncpy(&(xar->xattr->fstype), s, len); |
2911 | 0 | break; |
2912 | 0 | case FILE_ACL_DEFAULT: |
2913 | 0 | case FILE_ACL_ACCESS: |
2914 | 0 | case FILE_ACL_APPLEEXTENDED: |
2915 | 0 | xar->file->has |= HAS_ACL; |
2916 | | /* TODO */ |
2917 | 0 | break; |
2918 | 0 | case INIT: |
2919 | 0 | case XAR: |
2920 | 0 | case TOC: |
2921 | 0 | case TOC_CREATION_TIME: |
2922 | 0 | case TOC_CHECKSUM: |
2923 | 0 | case TOC_CHECKSUM_OFFSET: |
2924 | 0 | case TOC_CHECKSUM_SIZE: |
2925 | 7.20k | case TOC_FILE: |
2926 | 33.2k | case FILE_DATA: |
2927 | 33.3k | case FILE_DATA_ENCODING: |
2928 | 33.3k | case FILE_DATA_CONTENT: |
2929 | 33.3k | case FILE_DEVICE: |
2930 | 36.0k | case FILE_EA: |
2931 | 36.0k | case FILE_EA_ENCODING: |
2932 | 36.0k | case FILE_ACL: |
2933 | 36.0k | case FILE_FLAGS: |
2934 | 36.0k | case FILE_FLAGS_USER_NODUMP: |
2935 | 36.0k | case FILE_FLAGS_USER_IMMUTABLE: |
2936 | 36.0k | case FILE_FLAGS_USER_APPEND: |
2937 | 36.0k | case FILE_FLAGS_USER_OPAQUE: |
2938 | 36.0k | case FILE_FLAGS_USER_NOUNLINK: |
2939 | 36.0k | case FILE_FLAGS_SYS_ARCHIVED: |
2940 | 36.0k | case FILE_FLAGS_SYS_IMMUTABLE: |
2941 | 36.0k | case FILE_FLAGS_SYS_APPEND: |
2942 | 36.0k | case FILE_FLAGS_SYS_NOUNLINK: |
2943 | 36.0k | case FILE_FLAGS_SYS_SNAPSHOT: |
2944 | 36.0k | case FILE_EXT2: |
2945 | 36.0k | case FILE_EXT2_SecureDeletion: |
2946 | 36.0k | case FILE_EXT2_Undelete: |
2947 | 36.0k | case FILE_EXT2_Compress: |
2948 | 36.0k | case FILE_EXT2_Synchronous: |
2949 | 36.0k | case FILE_EXT2_Immutable: |
2950 | 36.0k | case FILE_EXT2_AppendOnly: |
2951 | 36.0k | case FILE_EXT2_NoDump: |
2952 | 36.0k | case FILE_EXT2_NoAtime: |
2953 | 36.0k | case FILE_EXT2_CompDirty: |
2954 | 36.0k | case FILE_EXT2_CompBlock: |
2955 | 36.0k | case FILE_EXT2_NoCompBlock: |
2956 | 36.0k | case FILE_EXT2_CompError: |
2957 | 36.0k | case FILE_EXT2_BTree: |
2958 | 36.0k | case FILE_EXT2_HashIndexed: |
2959 | 36.0k | case FILE_EXT2_iMagic: |
2960 | 36.0k | case FILE_EXT2_Journaled: |
2961 | 36.0k | case FILE_EXT2_NoTail: |
2962 | 36.0k | case FILE_EXT2_DirSync: |
2963 | 36.0k | case FILE_EXT2_TopDir: |
2964 | 36.0k | case FILE_EXT2_Reserved: |
2965 | 58.0k | case UNKNOWN: |
2966 | 58.0k | break; |
2967 | 80.8k | } |
2968 | | |
2969 | 80.8k | return (ARCHIVE_OK); |
2970 | 80.8k | } |
2971 | | |
2972 | | /* |
2973 | | * BSD file flags. |
2974 | | */ |
2975 | | static int |
2976 | | xml_parse_file_flags(struct xar *xar, const char *name) |
2977 | 0 | { |
2978 | 0 | const char *flag = NULL; |
2979 | |
|
2980 | 0 | if (strcmp(name, "UserNoDump") == 0) { |
2981 | 0 | xar->xmlsts = FILE_FLAGS_USER_NODUMP; |
2982 | 0 | flag = "nodump"; |
2983 | 0 | } |
2984 | 0 | else if (strcmp(name, "UserImmutable") == 0) { |
2985 | 0 | xar->xmlsts = FILE_FLAGS_USER_IMMUTABLE; |
2986 | 0 | flag = "uimmutable"; |
2987 | 0 | } |
2988 | 0 | else if (strcmp(name, "UserAppend") == 0) { |
2989 | 0 | xar->xmlsts = FILE_FLAGS_USER_APPEND; |
2990 | 0 | flag = "uappend"; |
2991 | 0 | } |
2992 | 0 | else if (strcmp(name, "UserOpaque") == 0) { |
2993 | 0 | xar->xmlsts = FILE_FLAGS_USER_OPAQUE; |
2994 | 0 | flag = "opaque"; |
2995 | 0 | } |
2996 | 0 | else if (strcmp(name, "UserNoUnlink") == 0) { |
2997 | 0 | xar->xmlsts = FILE_FLAGS_USER_NOUNLINK; |
2998 | 0 | flag = "nouunlink"; |
2999 | 0 | } |
3000 | 0 | else if (strcmp(name, "SystemArchived") == 0) { |
3001 | 0 | xar->xmlsts = FILE_FLAGS_SYS_ARCHIVED; |
3002 | 0 | flag = "archived"; |
3003 | 0 | } |
3004 | 0 | else if (strcmp(name, "SystemImmutable") == 0) { |
3005 | 0 | xar->xmlsts = FILE_FLAGS_SYS_IMMUTABLE; |
3006 | 0 | flag = "simmutable"; |
3007 | 0 | } |
3008 | 0 | else if (strcmp(name, "SystemAppend") == 0) { |
3009 | 0 | xar->xmlsts = FILE_FLAGS_SYS_APPEND; |
3010 | 0 | flag = "sappend"; |
3011 | 0 | } |
3012 | 0 | else if (strcmp(name, "SystemNoUnlink") == 0) { |
3013 | 0 | xar->xmlsts = FILE_FLAGS_SYS_NOUNLINK; |
3014 | 0 | flag = "nosunlink"; |
3015 | 0 | } |
3016 | 0 | else if (strcmp(name, "SystemSnapshot") == 0) { |
3017 | 0 | xar->xmlsts = FILE_FLAGS_SYS_SNAPSHOT; |
3018 | 0 | flag = "snapshot"; |
3019 | 0 | } |
3020 | |
|
3021 | 0 | if (flag == NULL) |
3022 | 0 | return (0); |
3023 | 0 | xar->file->has |= HAS_FFLAGS; |
3024 | 0 | if (archive_strlen(&(xar->file->fflags_text)) > 0) |
3025 | 0 | archive_strappend_char(&(xar->file->fflags_text), ','); |
3026 | 0 | archive_strcat(&(xar->file->fflags_text), flag); |
3027 | 0 | return (1); |
3028 | 0 | } |
3029 | | |
3030 | | /* |
3031 | | * Linux file flags. |
3032 | | */ |
3033 | | static int |
3034 | | xml_parse_file_ext2(struct xar *xar, const char *name) |
3035 | 0 | { |
3036 | 0 | const char *flag = NULL; |
3037 | |
|
3038 | 0 | if (strcmp(name, "SecureDeletion") == 0) { |
3039 | 0 | xar->xmlsts = FILE_EXT2_SecureDeletion; |
3040 | 0 | flag = "securedeletion"; |
3041 | 0 | } |
3042 | 0 | else if (strcmp(name, "Undelete") == 0) { |
3043 | 0 | xar->xmlsts = FILE_EXT2_Undelete; |
3044 | 0 | flag = "nouunlink"; |
3045 | 0 | } |
3046 | 0 | else if (strcmp(name, "Compress") == 0) { |
3047 | 0 | xar->xmlsts = FILE_EXT2_Compress; |
3048 | 0 | flag = "compress"; |
3049 | 0 | } |
3050 | 0 | else if (strcmp(name, "Synchronous") == 0) { |
3051 | 0 | xar->xmlsts = FILE_EXT2_Synchronous; |
3052 | 0 | flag = "sync"; |
3053 | 0 | } |
3054 | 0 | else if (strcmp(name, "Immutable") == 0) { |
3055 | 0 | xar->xmlsts = FILE_EXT2_Immutable; |
3056 | 0 | flag = "simmutable"; |
3057 | 0 | } |
3058 | 0 | else if (strcmp(name, "AppendOnly") == 0) { |
3059 | 0 | xar->xmlsts = FILE_EXT2_AppendOnly; |
3060 | 0 | flag = "sappend"; |
3061 | 0 | } |
3062 | 0 | else if (strcmp(name, "NoDump") == 0) { |
3063 | 0 | xar->xmlsts = FILE_EXT2_NoDump; |
3064 | 0 | flag = "nodump"; |
3065 | 0 | } |
3066 | 0 | else if (strcmp(name, "NoAtime") == 0) { |
3067 | 0 | xar->xmlsts = FILE_EXT2_NoAtime; |
3068 | 0 | flag = "noatime"; |
3069 | 0 | } |
3070 | 0 | else if (strcmp(name, "CompDirty") == 0) { |
3071 | 0 | xar->xmlsts = FILE_EXT2_CompDirty; |
3072 | 0 | flag = "compdirty"; |
3073 | 0 | } |
3074 | 0 | else if (strcmp(name, "CompBlock") == 0) { |
3075 | 0 | xar->xmlsts = FILE_EXT2_CompBlock; |
3076 | 0 | flag = "comprblk"; |
3077 | 0 | } |
3078 | 0 | else if (strcmp(name, "NoCompBlock") == 0) { |
3079 | 0 | xar->xmlsts = FILE_EXT2_NoCompBlock; |
3080 | 0 | flag = "nocomprblk"; |
3081 | 0 | } |
3082 | 0 | else if (strcmp(name, "CompError") == 0) { |
3083 | 0 | xar->xmlsts = FILE_EXT2_CompError; |
3084 | 0 | flag = "comperr"; |
3085 | 0 | } |
3086 | 0 | else if (strcmp(name, "BTree") == 0) { |
3087 | 0 | xar->xmlsts = FILE_EXT2_BTree; |
3088 | 0 | flag = "btree"; |
3089 | 0 | } |
3090 | 0 | else if (strcmp(name, "HashIndexed") == 0) { |
3091 | 0 | xar->xmlsts = FILE_EXT2_HashIndexed; |
3092 | 0 | flag = "hashidx"; |
3093 | 0 | } |
3094 | 0 | else if (strcmp(name, "iMagic") == 0) { |
3095 | 0 | xar->xmlsts = FILE_EXT2_iMagic; |
3096 | 0 | flag = "imagic"; |
3097 | 0 | } |
3098 | 0 | else if (strcmp(name, "Journaled") == 0) { |
3099 | 0 | xar->xmlsts = FILE_EXT2_Journaled; |
3100 | 0 | flag = "journal"; |
3101 | 0 | } |
3102 | 0 | else if (strcmp(name, "NoTail") == 0) { |
3103 | 0 | xar->xmlsts = FILE_EXT2_NoTail; |
3104 | 0 | flag = "notail"; |
3105 | 0 | } |
3106 | 0 | else if (strcmp(name, "DirSync") == 0) { |
3107 | 0 | xar->xmlsts = FILE_EXT2_DirSync; |
3108 | 0 | flag = "dirsync"; |
3109 | 0 | } |
3110 | 0 | else if (strcmp(name, "TopDir") == 0) { |
3111 | 0 | xar->xmlsts = FILE_EXT2_TopDir; |
3112 | 0 | flag = "topdir"; |
3113 | 0 | } |
3114 | 0 | else if (strcmp(name, "Reserved") == 0) { |
3115 | 0 | xar->xmlsts = FILE_EXT2_Reserved; |
3116 | 0 | flag = "reserved"; |
3117 | 0 | } |
3118 | |
|
3119 | 0 | if (flag == NULL) |
3120 | 0 | return (0); |
3121 | 0 | if (archive_strlen(&(xar->file->fflags_text)) > 0) |
3122 | 0 | archive_strappend_char(&(xar->file->fflags_text), ','); |
3123 | 0 | archive_strcat(&(xar->file->fflags_text), flag); |
3124 | 0 | return (1); |
3125 | 0 | } |
3126 | | |
3127 | | #ifdef HAVE_LIBXML_XMLREADER_H |
3128 | | |
3129 | | static int |
3130 | | xml2_xmlattr_setup(struct archive_read *a, |
3131 | | struct xmlattr_list *list, xmlTextReaderPtr reader) |
3132 | 87.2k | { |
3133 | 87.2k | struct xmlattr *attr; |
3134 | 87.2k | int r; |
3135 | | |
3136 | 87.2k | list->first = NULL; |
3137 | 87.2k | list->last = &(list->first); |
3138 | 87.2k | r = xmlTextReaderMoveToFirstAttribute(reader); |
3139 | 108k | while (r == 1) { |
3140 | 21.3k | attr = malloc(sizeof*(attr)); |
3141 | 21.3k | if (attr == NULL) { |
3142 | 0 | archive_set_error(&a->archive, ENOMEM, "Out of memory"); |
3143 | 0 | return (ARCHIVE_FATAL); |
3144 | 0 | } |
3145 | 21.3k | attr->name = strdup( |
3146 | 21.3k | (const char *)xmlTextReaderConstLocalName(reader)); |
3147 | 21.3k | if (attr->name == NULL) { |
3148 | 0 | free(attr); |
3149 | 0 | archive_set_error(&a->archive, ENOMEM, "Out of memory"); |
3150 | 0 | return (ARCHIVE_FATAL); |
3151 | 0 | } |
3152 | 21.3k | attr->value = strdup( |
3153 | 21.3k | (const char *)xmlTextReaderConstValue(reader)); |
3154 | 21.3k | if (attr->value == NULL) { |
3155 | 0 | free(attr->name); |
3156 | 0 | free(attr); |
3157 | 0 | archive_set_error(&a->archive, ENOMEM, "Out of memory"); |
3158 | 0 | return (ARCHIVE_FATAL); |
3159 | 0 | } |
3160 | 21.3k | attr->next = NULL; |
3161 | 21.3k | *list->last = attr; |
3162 | 21.3k | list->last = &(attr->next); |
3163 | 21.3k | r = xmlTextReaderMoveToNextAttribute(reader); |
3164 | 21.3k | } |
3165 | 87.2k | return (r); |
3166 | 87.2k | } |
3167 | | |
3168 | | static int |
3169 | | xml2_read_cb(void *context, char *buffer, int len) |
3170 | 2.64k | { |
3171 | 2.64k | struct archive_read *a; |
3172 | 2.64k | struct xar *xar; |
3173 | 2.64k | const void *d; |
3174 | 2.64k | size_t outbytes; |
3175 | 2.64k | size_t used = 0; |
3176 | 2.64k | int r; |
3177 | | |
3178 | 2.64k | a = (struct archive_read *)context; |
3179 | 2.64k | xar = (struct xar *)(a->format->data); |
3180 | | |
3181 | 2.64k | if (xar->toc_remaining <= 0) |
3182 | 0 | return (0); |
3183 | 2.64k | d = buffer; |
3184 | 2.64k | outbytes = len; |
3185 | 2.64k | r = rd_contents(a, &d, &outbytes, &used, xar->toc_remaining); |
3186 | 2.64k | if (r != ARCHIVE_OK) |
3187 | 60 | return (r); |
3188 | 2.58k | __archive_read_consume(a, used); |
3189 | 2.58k | xar->toc_remaining -= used; |
3190 | 2.58k | xar->offset += used; |
3191 | 2.58k | xar->toc_total += outbytes; |
3192 | 2.58k | PRINT_TOC(buffer, len); |
3193 | | |
3194 | 2.58k | return ((int)outbytes); |
3195 | 2.64k | } |
3196 | | |
3197 | | static int |
3198 | | xml2_close_cb(void *context) |
3199 | 110 | { |
3200 | | |
3201 | 110 | (void)context; /* UNUSED */ |
3202 | 110 | return (0); |
3203 | 110 | } |
3204 | | |
3205 | | static void |
3206 | | xml2_error_hdr(void *arg, const char *msg, xmlParserSeverities severity, |
3207 | | xmlTextReaderLocatorPtr locator) |
3208 | 376 | { |
3209 | 376 | struct archive_read *a; |
3210 | | |
3211 | 376 | (void)locator; /* UNUSED */ |
3212 | 376 | a = (struct archive_read *)arg; |
3213 | 376 | switch (severity) { |
3214 | 0 | case XML_PARSER_SEVERITY_VALIDITY_WARNING: |
3215 | 96 | case XML_PARSER_SEVERITY_WARNING: |
3216 | 96 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
3217 | 96 | "XML Parsing error: %s", msg); |
3218 | 96 | break; |
3219 | 0 | case XML_PARSER_SEVERITY_VALIDITY_ERROR: |
3220 | 280 | case XML_PARSER_SEVERITY_ERROR: |
3221 | 280 | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
3222 | 280 | "XML Parsing error: %s", msg); |
3223 | 280 | break; |
3224 | 376 | } |
3225 | 376 | } |
3226 | | |
3227 | | static int |
3228 | | xml2_read_toc(struct archive_read *a) |
3229 | 110 | { |
3230 | 110 | xmlTextReaderPtr reader; |
3231 | 110 | struct xmlattr_list list; |
3232 | 110 | int r; |
3233 | | |
3234 | 110 | reader = xmlReaderForIO(xml2_read_cb, xml2_close_cb, a, NULL, NULL, 0); |
3235 | 110 | if (reader == NULL) { |
3236 | 0 | archive_set_error(&a->archive, ENOMEM, |
3237 | 0 | "Couldn't allocate memory for xml parser"); |
3238 | 0 | return (ARCHIVE_FATAL); |
3239 | 0 | } |
3240 | 110 | xmlTextReaderSetErrorHandler(reader, xml2_error_hdr, a); |
3241 | | |
3242 | 247k | while ((r = xmlTextReaderRead(reader)) == 1) { |
3243 | 247k | const char *name, *value; |
3244 | 247k | int type, empty; |
3245 | | |
3246 | 247k | type = xmlTextReaderNodeType(reader); |
3247 | 247k | name = (const char *)xmlTextReaderConstLocalName(reader); |
3248 | 247k | switch (type) { |
3249 | 87.2k | case XML_READER_TYPE_ELEMENT: |
3250 | 87.2k | empty = xmlTextReaderIsEmptyElement(reader); |
3251 | 87.2k | r = xml2_xmlattr_setup(a, &list, reader); |
3252 | 87.2k | if (r == ARCHIVE_OK) |
3253 | 87.2k | r = xml_start(a, name, &list); |
3254 | 87.2k | xmlattr_cleanup(&list); |
3255 | 87.2k | if (r != ARCHIVE_OK) { |
3256 | 0 | xmlFreeTextReader(reader); |
3257 | 0 | xmlCleanupParser(); |
3258 | 0 | return (r); |
3259 | 0 | } |
3260 | 87.2k | if (empty) |
3261 | 46.7k | xml_end(a, name); |
3262 | 87.2k | break; |
3263 | 37.2k | case XML_READER_TYPE_END_ELEMENT: |
3264 | 37.2k | xml_end(a, name); |
3265 | 37.2k | break; |
3266 | 110k | case XML_READER_TYPE_TEXT: |
3267 | 110k | value = (const char *)xmlTextReaderConstValue(reader); |
3268 | 110k | r = xml_data(a, value, strlen(value)); |
3269 | 110k | if (r != ARCHIVE_OK) { |
3270 | 0 | xmlFreeTextReader(reader); |
3271 | 0 | xmlCleanupParser(); |
3272 | 0 | return (r); |
3273 | 0 | } |
3274 | 110k | break; |
3275 | 110k | case XML_READER_TYPE_SIGNIFICANT_WHITESPACE: |
3276 | 11.8k | default: |
3277 | 11.8k | break; |
3278 | 247k | } |
3279 | 247k | if (r < 0) |
3280 | 0 | break; |
3281 | 247k | } |
3282 | 110 | xmlFreeTextReader(reader); |
3283 | 110 | xmlCleanupParser(); |
3284 | | |
3285 | 110 | return ((r == 0)?ARCHIVE_OK:ARCHIVE_FATAL); |
3286 | 110 | } |
3287 | | |
3288 | | #elif defined(HAVE_BSDXML_H) || defined(HAVE_EXPAT_H) |
3289 | | |
3290 | | static int |
3291 | | expat_xmlattr_setup(struct archive_read *a, |
3292 | | struct xmlattr_list *list, const XML_Char **atts) |
3293 | | { |
3294 | | struct xmlattr *attr; |
3295 | | char *name, *value; |
3296 | | |
3297 | | list->first = NULL; |
3298 | | list->last = &(list->first); |
3299 | | if (atts == NULL) |
3300 | | return (ARCHIVE_OK); |
3301 | | while (atts[0] != NULL && atts[1] != NULL) { |
3302 | | attr = malloc(sizeof*(attr)); |
3303 | | name = strdup(atts[0]); |
3304 | | value = strdup(atts[1]); |
3305 | | if (attr == NULL || name == NULL || value == NULL) { |
3306 | | archive_set_error(&a->archive, ENOMEM, "Out of memory"); |
3307 | | free(attr); |
3308 | | free(name); |
3309 | | free(value); |
3310 | | return (ARCHIVE_FATAL); |
3311 | | } |
3312 | | attr->name = name; |
3313 | | attr->value = value; |
3314 | | attr->next = NULL; |
3315 | | *list->last = attr; |
3316 | | list->last = &(attr->next); |
3317 | | atts += 2; |
3318 | | } |
3319 | | return (ARCHIVE_OK); |
3320 | | } |
3321 | | |
3322 | | static void |
3323 | | expat_start_cb(void *userData, const XML_Char *name, const XML_Char **atts) |
3324 | | { |
3325 | | struct expat_userData *ud = (struct expat_userData *)userData; |
3326 | | struct archive_read *a = ud->archive; |
3327 | | struct xmlattr_list list; |
3328 | | int r; |
3329 | | |
3330 | | if (ud->state != ARCHIVE_OK) |
3331 | | return; |
3332 | | |
3333 | | r = expat_xmlattr_setup(a, &list, atts); |
3334 | | if (r == ARCHIVE_OK) |
3335 | | r = xml_start(a, (const char *)name, &list); |
3336 | | xmlattr_cleanup(&list); |
3337 | | ud->state = r; |
3338 | | } |
3339 | | |
3340 | | static void |
3341 | | expat_end_cb(void *userData, const XML_Char *name) |
3342 | | { |
3343 | | struct expat_userData *ud = (struct expat_userData *)userData; |
3344 | | |
3345 | | xml_end(ud->archive, (const char *)name); |
3346 | | } |
3347 | | |
3348 | | static void |
3349 | | expat_data_cb(void *userData, const XML_Char *s, int len) |
3350 | | { |
3351 | | struct expat_userData *ud = (struct expat_userData *)userData; |
3352 | | |
3353 | | if (ud->state != ARCHIVE_OK) |
3354 | | return; |
3355 | | |
3356 | | ud->state = xml_data(ud->archive, s, (size_t)len); |
3357 | | } |
3358 | | |
3359 | | static int |
3360 | | expat_read_toc(struct archive_read *a) |
3361 | | { |
3362 | | struct xar *xar; |
3363 | | XML_Parser parser; |
3364 | | struct expat_userData ud; |
3365 | | |
3366 | | ud.state = ARCHIVE_OK; |
3367 | | ud.archive = a; |
3368 | | |
3369 | | xar = (struct xar *)(a->format->data); |
3370 | | |
3371 | | /* Initialize XML Parser library. */ |
3372 | | parser = XML_ParserCreate(NULL); |
3373 | | if (parser == NULL) { |
3374 | | archive_set_error(&a->archive, ENOMEM, |
3375 | | "Couldn't allocate memory for xml parser"); |
3376 | | return (ARCHIVE_FATAL); |
3377 | | } |
3378 | | XML_SetUserData(parser, &ud); |
3379 | | XML_SetElementHandler(parser, expat_start_cb, expat_end_cb); |
3380 | | XML_SetCharacterDataHandler(parser, expat_data_cb); |
3381 | | xar->xmlsts = INIT; |
3382 | | |
3383 | | while (xar->toc_remaining && ud.state == ARCHIVE_OK) { |
3384 | | enum XML_Status xr; |
3385 | | const void *d; |
3386 | | size_t outbytes; |
3387 | | size_t used; |
3388 | | int r; |
3389 | | |
3390 | | d = NULL; |
3391 | | r = rd_contents(a, &d, &outbytes, &used, xar->toc_remaining); |
3392 | | if (r != ARCHIVE_OK || outbytes > INT_MAX) { |
3393 | | XML_ParserFree(parser); |
3394 | | return (r); |
3395 | | } |
3396 | | xar->toc_remaining -= used; |
3397 | | xar->offset += used; |
3398 | | xar->toc_total += outbytes; |
3399 | | PRINT_TOC(d, outbytes); |
3400 | | |
3401 | | xr = XML_Parse(parser, d, (int)outbytes, xar->toc_remaining == 0); |
3402 | | __archive_read_consume(a, used); |
3403 | | if (xr == XML_STATUS_ERROR) { |
3404 | | XML_ParserFree(parser); |
3405 | | archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, |
3406 | | "XML Parsing failed"); |
3407 | | return (ARCHIVE_FATAL); |
3408 | | } |
3409 | | } |
3410 | | XML_ParserFree(parser); |
3411 | | return (ud.state); |
3412 | | } |
3413 | | |
3414 | | #elif defined(HAVE_XMLLITE_H) |
3415 | | |
3416 | | struct ArchiveStreamAdapter { |
3417 | | const ISequentialStreamVtbl *lpVtbl; /* see asaStaticVtable */ |
3418 | | struct archive_read *a; |
3419 | | }; |
3420 | | |
3421 | | static HRESULT STDMETHODCALLTYPE |
3422 | | asaQueryInterface(ISequentialStream *this, REFIID riid, void **ppv) |
3423 | | { |
3424 | | if (!IsEqualIID(riid, &IID_ISequentialStream)) { |
3425 | | *ppv = NULL; |
3426 | | return E_NOINTERFACE; |
3427 | | } |
3428 | | *ppv = this; |
3429 | | return S_OK; |
3430 | | } |
3431 | | |
3432 | | /* |
3433 | | * We can dispense with reference counting as we tightly manage the lifetime |
3434 | | * of an ArchiveStreamAdapter. |
3435 | | */ |
3436 | | static ULONG STDMETHODCALLTYPE |
3437 | | asaAddRef(ISequentialStream *this) |
3438 | | { |
3439 | | (void)this; /* UNUSED */ |
3440 | | return ULONG_MAX; |
3441 | | } |
3442 | | |
3443 | | static ULONG STDMETHODCALLTYPE |
3444 | | asaRelease(ISequentialStream *this) |
3445 | | { |
3446 | | (void)this; /* UNUSED */ |
3447 | | return ULONG_MAX; |
3448 | | } |
3449 | | |
3450 | | static HRESULT STDMETHODCALLTYPE |
3451 | | asaRead(ISequentialStream *this, void *pv, ULONG cb, ULONG *pcbRead) |
3452 | | { |
3453 | | struct ArchiveStreamAdapter *asa = (struct ArchiveStreamAdapter *)this; |
3454 | | struct archive_read *a; |
3455 | | struct xar *xar; |
3456 | | const void *d = pv; |
3457 | | size_t outbytes = cb; |
3458 | | size_t used = 0; |
3459 | | int r; |
3460 | | |
3461 | | a = asa->a; |
3462 | | xar = (struct xar *)(a->format->data); |
3463 | | |
3464 | | *pcbRead = 0; |
3465 | | |
3466 | | if (xar->toc_remaining <= 0) |
3467 | | return cb != 0 ? S_FALSE : S_OK; |
3468 | | |
3469 | | r = rd_contents(a, &d, &outbytes, &used, xar->toc_remaining); |
3470 | | if (r != ARCHIVE_OK) |
3471 | | return E_FAIL; |
3472 | | __archive_read_consume(a, used); |
3473 | | xar->toc_remaining -= used; |
3474 | | xar->offset += used; |
3475 | | xar->toc_total += outbytes; |
3476 | | PRINT_TOC(pv, outbytes); |
3477 | | |
3478 | | *pcbRead = (ULONG)outbytes; |
3479 | | return outbytes < cb ? S_FALSE : S_OK; |
3480 | | } |
3481 | | |
3482 | | static HRESULT STDMETHODCALLTYPE |
3483 | | asaWrite(ISequentialStream *this, const void *pv, ULONG cb, ULONG *pcbWritten) |
3484 | | { |
3485 | | (void)this; /* UNUSED */ |
3486 | | (void)pv; /* UNUSED */ |
3487 | | (void)cb; /* UNUSED */ |
3488 | | if (!pcbWritten) return E_INVALIDARG; |
3489 | | *pcbWritten = 0; |
3490 | | return E_NOTIMPL; |
3491 | | } |
3492 | | |
3493 | | static const ISequentialStreamVtbl asaStaticVtable = { |
3494 | | .QueryInterface = asaQueryInterface, |
3495 | | .AddRef = asaAddRef, |
3496 | | .Release = asaRelease, |
3497 | | .Read = asaRead, |
3498 | | .Write = asaWrite, |
3499 | | }; |
3500 | | |
3501 | | static int |
3502 | | xmllite_create_stream_adapter(struct archive_read *a, |
3503 | | struct ArchiveStreamAdapter **pasa) |
3504 | | { |
3505 | | struct ArchiveStreamAdapter *asa = |
3506 | | calloc(1, sizeof(struct ArchiveStreamAdapter)); |
3507 | | if (!asa) { |
3508 | | archive_set_error(&(a->archive), ENOMEM, "Out of memory"); |
3509 | | return (ARCHIVE_FATAL); |
3510 | | } |
3511 | | asa->lpVtbl = &asaStaticVtable; |
3512 | | asa->a = a; |
3513 | | *pasa = asa; |
3514 | | return (ARCHIVE_OK); |
3515 | | } |
3516 | | |
3517 | | typedef HRESULT(STDMETHODCALLTYPE *xmllite_wstr_func)(IXmlReader *, LPCWSTR *, |
3518 | | UINT *); |
3519 | | |
3520 | | /* |
3521 | | * Returns an narrow-char archive_string in *as after calling |
3522 | | * the wide-char COM API callee() on the XmlReader reader. |
3523 | | * Sets an appropriate error on the archive if it fails. |
3524 | | */ |
3525 | | static int |
3526 | | xmllite_call_return_as(struct archive_read *a, struct archive_string *as, |
3527 | | IXmlReader *reader, xmllite_wstr_func callee) |
3528 | | { |
3529 | | LPCWSTR wcs; |
3530 | | UINT wlen; |
3531 | | |
3532 | | if (FAILED(callee(reader, &wcs, &wlen))) { |
3533 | | archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC, |
3534 | | "Failed to read XML data"); |
3535 | | return (ARCHIVE_FATAL); |
3536 | | } |
3537 | | |
3538 | | archive_string_init(as); |
3539 | | if (archive_string_append_from_wcs(as, wcs, (size_t)wlen) < 0) { |
3540 | | archive_string_free(as); |
3541 | | archive_set_error(&(a->archive), ENOMEM, "Out of memory"); |
3542 | | return (ARCHIVE_FATAL); |
3543 | | } |
3544 | | |
3545 | | return (ARCHIVE_OK); |
3546 | | } |
3547 | | |
3548 | | static char * |
3549 | | xmllite_call_return_mbs(struct archive_read *a, IXmlReader *reader, |
3550 | | xmllite_wstr_func callee) |
3551 | | { |
3552 | | char *ret; |
3553 | | struct archive_string as; |
3554 | | |
3555 | | if (xmllite_call_return_as(a, &as, reader, callee) < 0) { |
3556 | | return NULL; |
3557 | | } |
3558 | | |
3559 | | ret = strdup(as.s); |
3560 | | archive_string_free(&as); |
3561 | | if (ret == NULL) { |
3562 | | archive_set_error(&(a->archive), ENOMEM, "Out of memory"); |
3563 | | return NULL; |
3564 | | } |
3565 | | return ret; |
3566 | | } |
3567 | | |
3568 | | static int |
3569 | | xmllite_xmlattr_setup(struct archive_read *a, |
3570 | | struct xmlattr_list *list, IXmlReader *reader) |
3571 | | { |
3572 | | struct xmlattr *attr; |
3573 | | HRESULT hr; |
3574 | | |
3575 | | list->first = NULL; |
3576 | | list->last = &(list->first); |
3577 | | hr = reader->lpVtbl->MoveToFirstAttribute(reader); |
3578 | | /* Contrary to other checks, we're not using SUCCEEDED/FAILED |
3579 | | * because MoveToNextAttribute returns *S_FALSE* (success!) |
3580 | | * when it runs out of attributes. |
3581 | | */ |
3582 | | while (hr == S_OK) { |
3583 | | /* Attributes implied as being default by the DTD are ignored */ |
3584 | | if (reader->lpVtbl->IsDefault(reader)) |
3585 | | continue; |
3586 | | |
3587 | | attr = malloc(sizeof*(attr)); |
3588 | | if (attr == NULL) { |
3589 | | archive_set_error(&(a->archive), ENOMEM, |
3590 | | "Out of memory"); |
3591 | | return (ARCHIVE_FATAL); |
3592 | | } |
3593 | | |
3594 | | attr->name = xmllite_call_return_mbs(a, reader, |
3595 | | reader->lpVtbl->GetLocalName); |
3596 | | if (attr->name == NULL) { |
3597 | | free(attr); |
3598 | | /* xmllite_call_return_mbs sets an appropriate error */ |
3599 | | return (ARCHIVE_FATAL); |
3600 | | } |
3601 | | |
3602 | | attr->value = xmllite_call_return_mbs(a, reader, |
3603 | | reader->lpVtbl->GetValue); |
3604 | | if (attr->value == NULL) { |
3605 | | free(attr->name); |
3606 | | free(attr); |
3607 | | /* xmllite_call_return_mbs sets an appropriate error */ |
3608 | | return (ARCHIVE_FATAL); |
3609 | | } |
3610 | | |
3611 | | attr->next = NULL; |
3612 | | *list->last = attr; |
3613 | | list->last = &(attr->next); |
3614 | | hr = reader->lpVtbl->MoveToNextAttribute(reader); |
3615 | | } |
3616 | | |
3617 | | if (FAILED(hr)) { |
3618 | | archive_set_error(&(a->archive), ARCHIVE_ERRNO_FILE_FORMAT, |
3619 | | "Failed to parse XML document"); |
3620 | | return (ARCHIVE_FAILED); |
3621 | | } |
3622 | | |
3623 | | return (ARCHIVE_OK); |
3624 | | } |
3625 | | |
3626 | | static int |
3627 | | xmllite_read_toc(struct archive_read *a) |
3628 | | { |
3629 | | struct ArchiveStreamAdapter *asa = NULL; |
3630 | | char *name; |
3631 | | struct archive_string as; |
3632 | | BOOL empty; |
3633 | | XmlNodeType type; |
3634 | | struct xmlattr_list list; |
3635 | | IXmlReader *reader = NULL; |
3636 | | int r = ARCHIVE_OK; |
3637 | | |
3638 | | if ((r = xmllite_create_stream_adapter(a, &asa)) < 0) { |
3639 | | goto out; |
3640 | | } |
3641 | | |
3642 | | if (FAILED(CreateXmlReader(&IID_IXmlReader, (void **)&reader, NULL))) { |
3643 | | r = ARCHIVE_FATAL; |
3644 | | goto out; |
3645 | | } |
3646 | | |
3647 | | if (FAILED(reader->lpVtbl->SetInput(reader, (IUnknown *)asa))) { |
3648 | | archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC, |
3649 | | "Failed to prepare XML stream"); |
3650 | | r = ARCHIVE_FATAL; |
3651 | | goto out; |
3652 | | } |
3653 | | |
3654 | | while (!reader->lpVtbl->IsEOF(reader)) { |
3655 | | if (FAILED(reader->lpVtbl->Read(reader, &type))) { |
3656 | | archive_set_error(&(a->archive), ARCHIVE_ERRNO_MISC, |
3657 | | "Failed to read XML stream"); |
3658 | | r = ARCHIVE_FATAL; |
3659 | | goto out; |
3660 | | } |
3661 | | |
3662 | | switch (type) { |
3663 | | case XmlNodeType_Element: |
3664 | | empty = reader->lpVtbl->IsEmptyElement(reader); |
3665 | | |
3666 | | name = xmllite_call_return_mbs(a, reader, |
3667 | | reader->lpVtbl->GetLocalName); |
3668 | | if (name == NULL) { |
3669 | | /* xmllite_call_return_mbs sets an appropriate error */ |
3670 | | r = ARCHIVE_FATAL; |
3671 | | goto out; |
3672 | | } |
3673 | | |
3674 | | r = xmllite_xmlattr_setup(a, &list, reader); |
3675 | | if (r == ARCHIVE_OK) { |
3676 | | r = xml_start(a, name, &list); |
3677 | | } |
3678 | | xmlattr_cleanup(&list); |
3679 | | if (r == ARCHIVE_OK && empty) { |
3680 | | xml_end(a, name); |
3681 | | } |
3682 | | |
3683 | | free(name); |
3684 | | if (r != ARCHIVE_OK) { |
3685 | | goto out; |
3686 | | } |
3687 | | |
3688 | | break; |
3689 | | case XmlNodeType_EndElement: |
3690 | | name = xmllite_call_return_mbs(a, reader, |
3691 | | reader->lpVtbl->GetLocalName); |
3692 | | if (name == NULL) { |
3693 | | /* xmllite_call_return_mbs sets an appropriate error */ |
3694 | | r = ARCHIVE_FATAL; |
3695 | | goto out; |
3696 | | } |
3697 | | |
3698 | | xml_end(a, name); |
3699 | | free(name); |
3700 | | break; |
3701 | | case XmlNodeType_Text: |
3702 | | r = xmllite_call_return_as(a, &as, reader, |
3703 | | reader->lpVtbl->GetValue); |
3704 | | if (r != ARCHIVE_OK) { |
3705 | | /* xmllite_call_return_as sets an appropriate error */ |
3706 | | goto out; |
3707 | | } |
3708 | | |
3709 | | r = xml_data(a, as.s, archive_strlen(&as)); |
3710 | | if (r != ARCHIVE_OK) { |
3711 | | /* xml_data sets an appropriate error */ |
3712 | | goto out; |
3713 | | } |
3714 | | archive_string_free(&as); |
3715 | | |
3716 | | case XmlNodeType_None: |
3717 | | case XmlNodeType_Attribute: |
3718 | | case XmlNodeType_CDATA: |
3719 | | case XmlNodeType_ProcessingInstruction: |
3720 | | case XmlNodeType_Comment: |
3721 | | case XmlNodeType_DocumentType: |
3722 | | case XmlNodeType_Whitespace: |
3723 | | case XmlNodeType_XmlDeclaration: |
3724 | | default: |
3725 | | break; |
3726 | | } |
3727 | | } |
3728 | | |
3729 | | out: |
3730 | | if (reader) |
3731 | | reader->lpVtbl->Release(reader); |
3732 | | |
3733 | | free(asa); |
3734 | | |
3735 | | return r; |
3736 | | } |
3737 | | #endif /* defined(XMLLITE) */ |
3738 | | |
3739 | | #endif /* Support xar format */ |