2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2011 Michihiro NAKAJIMA
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: head/lib/libarchive/archive_read_support_format_tar.c 201161 2009-12-29 05:44:39Z kientzle $");
42 #include "archive_acl_private.h" /* For ACL parsing routines. */
43 #include "archive_entry.h"
44 #include "archive_entry_locale.h"
45 #include "archive_private.h"
46 #include "archive_read_private.h"
48 #define tar_min(a,b) ((a) < (b) ? (a) : (b))
51 * Layout of POSIX 'ustar' tar header.
53 struct archive_entry_header_ustar {
62 char linkname[100]; /* "old format" header ends here */
63 char magic[6]; /* For POSIX: "ustar\0" */
64 char version[2]; /* For POSIX: "00" */
73 * Structure of GNU tar header
80 struct archive_entry_header_gnutar {
90 char magic[8]; /* "ustar \0" (note blank/blank/null at end) */
100 struct gnu_sparse sparse[4];
104 * Old GNU format doesn't use POSIX 'prefix' field; they use
105 * the 'L' (longname) entry instead.
110 * Data specific to this format.
112 struct sparse_block {
113 struct sparse_block *next;
120 struct archive_string acl_text;
121 struct archive_string entry_pathname;
122 /* For "GNU.sparse.name" and other similar path extensions. */
123 struct archive_string entry_pathname_override;
124 struct archive_string entry_linkpath;
125 struct archive_string entry_uname;
126 struct archive_string entry_gname;
127 struct archive_string longlink;
128 struct archive_string longname;
129 struct archive_string pax_header;
130 struct archive_string pax_global;
131 struct archive_string line;
132 int pax_hdrcharset_binary;
133 int header_recursion_depth;
134 int64_t entry_bytes_remaining;
135 int64_t entry_offset;
136 int64_t entry_padding;
137 int64_t entry_bytes_unconsumed;
139 struct sparse_block *sparse_list;
140 struct sparse_block *sparse_last;
141 int64_t sparse_offset;
142 int64_t sparse_numbytes;
143 int sparse_gnu_major;
144 int sparse_gnu_minor;
145 char sparse_gnu_pending;
147 struct archive_string localname;
148 struct archive_string_conv *opt_sconv;
149 struct archive_string_conv *sconv;
150 struct archive_string_conv *sconv_acl;
151 struct archive_string_conv *sconv_default;
152 int init_default_conversion;
156 static int archive_block_is_null(const char *p);
157 static char *base64_decode(const char *, size_t, size_t *);
158 static int gnu_add_sparse_entry(struct archive_read *, struct tar *,
159 int64_t offset, int64_t remaining);
161 static void gnu_clear_sparse_list(struct tar *);
162 static int gnu_sparse_old_read(struct archive_read *, struct tar *,
163 const struct archive_entry_header_gnutar *header, size_t *);
164 static int gnu_sparse_old_parse(struct archive_read *, struct tar *,
165 const struct gnu_sparse *sparse, int length);
166 static int gnu_sparse_01_parse(struct archive_read *, struct tar *,
168 static ssize_t gnu_sparse_10_read(struct archive_read *, struct tar *,
170 static int header_Solaris_ACL(struct archive_read *, struct tar *,
171 struct archive_entry *, const void *, size_t *);
172 static int header_common(struct archive_read *, struct tar *,
173 struct archive_entry *, const void *);
174 static int header_old_tar(struct archive_read *, struct tar *,
175 struct archive_entry *, const void *);
176 static int header_pax_extensions(struct archive_read *, struct tar *,
177 struct archive_entry *, const void *, size_t *);
178 static int header_pax_global(struct archive_read *, struct tar *,
179 struct archive_entry *, const void *h, size_t *);
180 static int header_longlink(struct archive_read *, struct tar *,
181 struct archive_entry *, const void *h, size_t *);
182 static int header_longname(struct archive_read *, struct tar *,
183 struct archive_entry *, const void *h, size_t *);
184 static int read_mac_metadata_blob(struct archive_read *, struct tar *,
185 struct archive_entry *, const void *h, size_t *);
186 static int header_volume(struct archive_read *, struct tar *,
187 struct archive_entry *, const void *h, size_t *);
188 static int header_ustar(struct archive_read *, struct tar *,
189 struct archive_entry *, const void *h);
190 static int header_gnutar(struct archive_read *, struct tar *,
191 struct archive_entry *, const void *h, size_t *);
192 static int archive_read_format_tar_bid(struct archive_read *, int);
193 static int archive_read_format_tar_options(struct archive_read *,
194 const char *, const char *);
195 static int archive_read_format_tar_cleanup(struct archive_read *);
196 static int archive_read_format_tar_read_data(struct archive_read *a,
197 const void **buff, size_t *size, int64_t *offset);
198 static int archive_read_format_tar_skip(struct archive_read *a);
199 static int archive_read_format_tar_read_header(struct archive_read *,
200 struct archive_entry *);
201 static int checksum(struct archive_read *, const void *);
202 static int pax_attribute(struct archive_read *, struct tar *,
203 struct archive_entry *, char *key, char *value);
204 static int pax_header(struct archive_read *, struct tar *,
205 struct archive_entry *, char *attr);
206 static void pax_time(const char *, int64_t *sec, long *nanos);
207 static ssize_t readline(struct archive_read *, struct tar *, const char **,
208 ssize_t limit, size_t *);
209 static int read_body_to_string(struct archive_read *, struct tar *,
210 struct archive_string *, const void *h, size_t *);
211 static int solaris_sparse_parse(struct archive_read *, struct tar *,
212 struct archive_entry *, const char *);
213 static int64_t tar_atol(const char *, unsigned);
214 static int64_t tar_atol10(const char *, unsigned);
215 static int64_t tar_atol256(const char *, unsigned);
216 static int64_t tar_atol8(const char *, unsigned);
217 static int tar_read_header(struct archive_read *, struct tar *,
218 struct archive_entry *, size_t *);
219 static int tohex(int c);
220 static char *url_decode(const char *);
221 static void tar_flush_unconsumed(struct archive_read *, size_t *);
225 archive_read_support_format_gnutar(struct archive *a)
227 archive_check_magic(a, ARCHIVE_READ_MAGIC,
228 ARCHIVE_STATE_NEW, "archive_read_support_format_gnutar");
229 return (archive_read_support_format_tar(a));
234 archive_read_support_format_tar(struct archive *_a)
236 struct archive_read *a = (struct archive_read *)_a;
240 archive_check_magic(_a, ARCHIVE_READ_MAGIC,
241 ARCHIVE_STATE_NEW, "archive_read_support_format_tar");
243 tar = (struct tar *)calloc(1, sizeof(*tar));
245 archive_set_error(&a->archive, ENOMEM,
246 "Can't allocate tar data");
247 return (ARCHIVE_FATAL);
250 r = __archive_read_register_format(a, tar, "tar",
251 archive_read_format_tar_bid,
252 archive_read_format_tar_options,
253 archive_read_format_tar_read_header,
254 archive_read_format_tar_read_data,
255 archive_read_format_tar_skip,
256 archive_read_format_tar_cleanup);
264 archive_read_format_tar_cleanup(struct archive_read *a)
268 tar = (struct tar *)(a->format->data);
269 gnu_clear_sparse_list(tar);
270 archive_string_free(&tar->acl_text);
271 archive_string_free(&tar->entry_pathname);
272 archive_string_free(&tar->entry_pathname_override);
273 archive_string_free(&tar->entry_linkpath);
274 archive_string_free(&tar->entry_uname);
275 archive_string_free(&tar->entry_gname);
276 archive_string_free(&tar->line);
277 archive_string_free(&tar->pax_global);
278 archive_string_free(&tar->pax_header);
279 archive_string_free(&tar->longname);
280 archive_string_free(&tar->longlink);
281 archive_string_free(&tar->localname);
283 (a->format->data) = NULL;
289 archive_read_format_tar_bid(struct archive_read *a, int best_bid)
293 const struct archive_entry_header_ustar *header;
295 (void)best_bid; /* UNUSED */
299 /* Now let's look at the actual header and see if it matches. */
300 h = __archive_read_ahead(a, 512, NULL);
304 /* If it's an end-of-archive mark, we can handle it. */
305 if (h[0] == 0 && archive_block_is_null(h)) {
307 * Usually, I bid the number of bits verified, but
308 * in this case, 4096 seems excessive so I picked 10 as
309 * an arbitrary but reasonable-seeming value.
314 /* If it's not an end-of-archive mark, it must have a valid checksum.*/
317 bid += 48; /* Checksum is usually 6 octal digits. */
319 header = (const struct archive_entry_header_ustar *)h;
321 /* Recognize POSIX formats. */
322 if ((memcmp(header->magic, "ustar\0", 6) == 0)
323 && (memcmp(header->version, "00", 2) == 0))
326 /* Recognize GNU tar format. */
327 if ((memcmp(header->magic, "ustar ", 6) == 0)
328 && (memcmp(header->version, " \0", 2) == 0))
331 /* Type flag must be null, digit or A-Z, a-z. */
332 if (header->typeflag[0] != 0 &&
333 !( header->typeflag[0] >= '0' && header->typeflag[0] <= '9') &&
334 !( header->typeflag[0] >= 'A' && header->typeflag[0] <= 'Z') &&
335 !( header->typeflag[0] >= 'a' && header->typeflag[0] <= 'z') )
337 bid += 2; /* 6 bits of variation in an 8-bit field leaves 2 bits. */
339 /* Sanity check: Look at first byte of mode field. */
340 switch (255 & (unsigned)header->mode[0]) {
342 /* Base-256 value: No further verification possible! */
344 case ' ': /* Not recommended, but not illegal, either. */
346 case '0': case '1': case '2': case '3':
347 case '4': case '5': case '6': case '7':
349 /* TODO: Check format of remainder of this field. */
352 /* Not a valid mode; bail out here. */
355 /* TODO: Sanity test uid/gid/size/mtime/rdevmajor/rdevminor fields. */
361 archive_read_format_tar_options(struct archive_read *a,
362 const char *key, const char *val)
365 int ret = ARCHIVE_FAILED;
367 tar = (struct tar *)(a->format->data);
368 if (strcmp(key, "compat-2x") == 0) {
369 /* Handle UTF-8 filnames as libarchive 2.x */
370 tar->compat_2x = (val != NULL)?1:0;
371 tar->init_default_conversion = tar->compat_2x;
373 } else if (strcmp(key, "hdrcharset") == 0) {
374 if (val == NULL || val[0] == 0)
375 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
376 "tar: hdrcharset option needs a character-set name");
379 archive_string_conversion_from_charset(
380 &a->archive, val, 0);
381 if (tar->opt_sconv != NULL)
387 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
388 "tar: unknown keyword ``%s''", key);
393 /* utility function- this exists to centralize the logic of tracking
394 * how much unconsumed data we have floating around, and to consume
395 * anything outstanding since we're going to do read_aheads
398 tar_flush_unconsumed(struct archive_read *a, size_t *unconsumed)
402 void *data = (void *)__archive_read_ahead(a, *unconsumed, NULL);
403 * this block of code is to poison claimed unconsumed space, ensuring
404 * things break if it is in use still.
405 * currently it WILL break things, so enable it only for debugging this issue
407 memset(data, 0xff, *unconsumed);
410 __archive_read_consume(a, *unconsumed);
416 * The function invoked by archive_read_next_header(). This
417 * just sets up a few things and then calls the internal
418 * tar_read_header() function below.
421 archive_read_format_tar_read_header(struct archive_read *a,
422 struct archive_entry *entry)
425 * When converting tar archives to cpio archives, it is
426 * essential that each distinct file have a distinct inode
427 * number. To simplify this, we keep a static count here to
428 * assign fake dev/inode numbers to each tar entry. Note that
429 * pax format archives may overwrite this with something more
432 * Ideally, we would track every file read from the archive so
433 * that we could assign the same dev/ino pair to hardlinks,
434 * but the memory required to store a complete lookup table is
435 * probably not worthwhile just to support the relatively
436 * obscure tar->cpio conversion case.
438 static int default_inode;
439 static int default_dev;
443 size_t l, unconsumed = 0;
445 /* Assign default device/inode values. */
446 archive_entry_set_dev(entry, 1 + default_dev); /* Don't use zero. */
447 archive_entry_set_ino(entry, ++default_inode); /* Don't use zero. */
448 /* Limit generated st_ino number to 16 bits. */
449 if (default_inode >= 0xffff) {
454 tar = (struct tar *)(a->format->data);
455 tar->entry_offset = 0;
456 gnu_clear_sparse_list(tar);
457 tar->realsize = -1; /* Mark this as "unset" */
459 /* Setup default string conversion. */
460 tar->sconv = tar->opt_sconv;
461 if (tar->sconv == NULL) {
462 if (!tar->init_default_conversion) {
464 archive_string_default_conversion_for_read(&(a->archive));
465 tar->init_default_conversion = 1;
467 tar->sconv = tar->sconv_default;
470 r = tar_read_header(a, tar, entry, &unconsumed);
472 tar_flush_unconsumed(a, &unconsumed);
475 * "non-sparse" files are really just sparse files with
478 if (tar->sparse_list == NULL) {
479 if (gnu_add_sparse_entry(a, tar, 0, tar->entry_bytes_remaining)
481 return (ARCHIVE_FATAL);
483 struct sparse_block *sb;
485 for (sb = tar->sparse_list; sb != NULL; sb = sb->next) {
487 archive_entry_sparse_add_entry(entry,
488 sb->offset, sb->remaining);
492 if (r == ARCHIVE_OK) {
494 * "Regular" entry with trailing '/' is really
495 * directory: This is needed for certain old tar
496 * variants and even for some broken newer ones.
499 wp = archive_entry_pathname_w(entry);
502 if (archive_entry_filetype(entry) == AE_IFREG
504 archive_entry_set_filetype(entry, AE_IFDIR);
506 p = archive_entry_pathname(entry);
508 return (ARCHIVE_FAILED);
510 if (archive_entry_filetype(entry) == AE_IFREG
512 archive_entry_set_filetype(entry, AE_IFDIR);
519 archive_read_format_tar_read_data(struct archive_read *a,
520 const void **buff, size_t *size, int64_t *offset)
524 struct sparse_block *p;
526 tar = (struct tar *)(a->format->data);
529 /* Remove exhausted entries from sparse list. */
530 while (tar->sparse_list != NULL &&
531 tar->sparse_list->remaining == 0) {
532 p = tar->sparse_list;
533 tar->sparse_list = p->next;
537 if (tar->entry_bytes_unconsumed) {
538 __archive_read_consume(a, tar->entry_bytes_unconsumed);
539 tar->entry_bytes_unconsumed = 0;
542 /* If we're at end of file, return EOF. */
543 if (tar->sparse_list == NULL || tar->entry_bytes_remaining == 0) {
544 if (__archive_read_consume(a, tar->entry_padding) < 0)
545 return (ARCHIVE_FATAL);
546 tar->entry_padding = 0;
549 *offset = tar->realsize;
550 return (ARCHIVE_EOF);
553 *buff = __archive_read_ahead(a, 1, &bytes_read);
555 return (ARCHIVE_FATAL);
557 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
558 "Truncated tar archive");
559 return (ARCHIVE_FATAL);
561 if (bytes_read > tar->entry_bytes_remaining)
562 bytes_read = tar->entry_bytes_remaining;
563 /* Don't read more than is available in the
564 * current sparse block. */
565 if (tar->sparse_list->remaining < bytes_read)
566 bytes_read = tar->sparse_list->remaining;
568 *offset = tar->sparse_list->offset;
569 tar->sparse_list->remaining -= bytes_read;
570 tar->sparse_list->offset += bytes_read;
571 tar->entry_bytes_remaining -= bytes_read;
572 tar->entry_bytes_unconsumed = bytes_read;
574 if (tar->sparse_list->hole)
581 archive_read_format_tar_skip(struct archive_read *a)
583 int64_t bytes_skipped;
586 tar = (struct tar *)(a->format->data);
588 bytes_skipped = __archive_read_consume(a,
589 tar->entry_bytes_remaining + tar->entry_padding +
590 tar->entry_bytes_unconsumed);
591 if (bytes_skipped < 0)
592 return (ARCHIVE_FATAL);
594 tar->entry_bytes_remaining = 0;
595 tar->entry_bytes_unconsumed = 0;
596 tar->entry_padding = 0;
598 /* Free the sparse list. */
599 gnu_clear_sparse_list(tar);
605 * This function recursively interprets all of the headers associated
606 * with a single entry.
609 tar_read_header(struct archive_read *a, struct tar *tar,
610 struct archive_entry *entry, size_t *unconsumed)
615 const struct archive_entry_header_ustar *header;
617 tar_flush_unconsumed(a, unconsumed);
619 /* Read 512-byte header record */
620 h = __archive_read_ahead(a, 512, &bytes);
623 if (bytes == 0) { /* EOF at a block boundary. */
624 /* Some writers do omit the block of nulls. <sigh> */
625 return (ARCHIVE_EOF);
627 if (bytes < 512) { /* Short block at EOF; this is bad. */
628 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
629 "Truncated tar archive");
630 return (ARCHIVE_FATAL);
634 /* Check for end-of-archive mark. */
635 if (h[0] == 0 && archive_block_is_null(h)) {
636 /* Try to consume a second all-null record, as well. */
637 tar_flush_unconsumed(a, unconsumed);
638 h = __archive_read_ahead(a, 512, NULL);
640 __archive_read_consume(a, 512);
641 archive_clear_error(&a->archive);
642 if (a->archive.archive_format_name == NULL) {
643 a->archive.archive_format = ARCHIVE_FORMAT_TAR;
644 a->archive.archive_format_name = "tar";
646 return (ARCHIVE_EOF);
650 * Note: If the checksum fails and we return ARCHIVE_RETRY,
651 * then the client is likely to just retry. This is a very
652 * crude way to search for the next valid header!
654 * TODO: Improve this by implementing a real header scan.
656 if (!checksum(a, h)) {
657 tar_flush_unconsumed(a, unconsumed);
658 archive_set_error(&a->archive, EINVAL, "Damaged tar archive");
659 return (ARCHIVE_RETRY); /* Retryable: Invalid header */
662 if (++tar->header_recursion_depth > 32) {
663 tar_flush_unconsumed(a, unconsumed);
664 archive_set_error(&a->archive, EINVAL, "Too many special headers");
665 return (ARCHIVE_WARN);
668 /* Determine the format variant. */
669 header = (const struct archive_entry_header_ustar *)h;
671 switch(header->typeflag[0]) {
672 case 'A': /* Solaris tar ACL */
673 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
674 a->archive.archive_format_name = "Solaris tar";
675 err = header_Solaris_ACL(a, tar, entry, h, unconsumed);
677 case 'g': /* POSIX-standard 'g' header. */
678 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
679 a->archive.archive_format_name = "POSIX pax interchange format";
680 err = header_pax_global(a, tar, entry, h, unconsumed);
682 case 'K': /* Long link name (GNU tar, others) */
683 err = header_longlink(a, tar, entry, h, unconsumed);
685 case 'L': /* Long filename (GNU tar, others) */
686 err = header_longname(a, tar, entry, h, unconsumed);
688 case 'V': /* GNU volume header */
689 err = header_volume(a, tar, entry, h, unconsumed);
691 case 'X': /* Used by SUN tar; same as 'x'. */
692 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
693 a->archive.archive_format_name =
694 "POSIX pax interchange format (Sun variant)";
695 err = header_pax_extensions(a, tar, entry, h, unconsumed);
697 case 'x': /* POSIX-standard 'x' header. */
698 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
699 a->archive.archive_format_name = "POSIX pax interchange format";
700 err = header_pax_extensions(a, tar, entry, h, unconsumed);
703 if (memcmp(header->magic, "ustar \0", 8) == 0) {
704 a->archive.archive_format = ARCHIVE_FORMAT_TAR_GNUTAR;
705 a->archive.archive_format_name = "GNU tar format";
706 err = header_gnutar(a, tar, entry, h, unconsumed);
707 } else if (memcmp(header->magic, "ustar", 5) == 0) {
708 if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) {
709 a->archive.archive_format = ARCHIVE_FORMAT_TAR_USTAR;
710 a->archive.archive_format_name = "POSIX ustar format";
712 err = header_ustar(a, tar, entry, h);
714 a->archive.archive_format = ARCHIVE_FORMAT_TAR;
715 a->archive.archive_format_name = "tar (non-POSIX)";
716 err = header_old_tar(a, tar, entry, h);
719 if (err == ARCHIVE_FATAL)
722 tar_flush_unconsumed(a, unconsumed);
727 --tar->header_recursion_depth;
728 /* Yuck. Apple's design here ends up storing long pathname
729 * extensions for both the AppleDouble extension entry and the
732 /* TODO: Should this be disabled on non-Mac platforms? */
733 if ((err == ARCHIVE_WARN || err == ARCHIVE_OK) &&
734 tar->header_recursion_depth == 0) {
735 int err2 = read_mac_metadata_blob(a, tar, entry, h, unconsumed);
740 /* We return warnings or success as-is. Anything else is fatal. */
741 if (err == ARCHIVE_WARN || err == ARCHIVE_OK) {
742 if (tar->sparse_gnu_pending) {
743 if (tar->sparse_gnu_major == 1 &&
744 tar->sparse_gnu_minor == 0) {
747 tar->sparse_gnu_pending = 0;
748 /* Read initial sparse map. */
749 bytes_read = gnu_sparse_10_read(a, tar, unconsumed);
750 tar->entry_bytes_remaining -= bytes_read;
754 archive_set_error(&a->archive,
756 "Unrecognized GNU sparse file format");
757 return (ARCHIVE_WARN);
759 tar->sparse_gnu_pending = 0;
763 if (err == ARCHIVE_EOF)
764 /* EOF when recursively reading a header is bad. */
765 archive_set_error(&a->archive, EINVAL, "Damaged tar archive");
766 return (ARCHIVE_FATAL);
770 * Return true if block checksum is correct.
773 checksum(struct archive_read *a, const void *h)
775 const unsigned char *bytes;
776 const struct archive_entry_header_ustar *header;
779 (void)a; /* UNUSED */
780 bytes = (const unsigned char *)h;
781 header = (const struct archive_entry_header_ustar *)h;
784 * Test the checksum. Note that POSIX specifies _unsigned_
785 * bytes for this calculation.
787 sum = tar_atol(header->checksum, sizeof(header->checksum));
789 for (i = 0; i < 148; i++)
790 check += (unsigned char)bytes[i];
794 check += (unsigned char)bytes[i];
799 * Repeat test with _signed_ bytes, just in case this archive
800 * was created by an old BSD, Solaris, or HP-UX tar with a
801 * broken checksum calculation.
804 for (i = 0; i < 148; i++)
805 check += (signed char)bytes[i];
809 check += (signed char)bytes[i];
817 * Return true if this block contains only nulls.
820 archive_block_is_null(const char *p)
824 for (i = 0; i < 512; i++)
831 * Interpret 'A' Solaris ACL header
834 header_Solaris_ACL(struct archive_read *a, struct tar *tar,
835 struct archive_entry *entry, const void *h, size_t *unconsumed)
837 const struct archive_entry_header_ustar *header;
844 * read_body_to_string adds a NUL terminator, but we need a little
845 * more to make sure that we don't overrun acl_text later.
847 header = (const struct archive_entry_header_ustar *)h;
848 size = tar_atol(header->size, sizeof(header->size));
849 err = read_body_to_string(a, tar, &(tar->acl_text), h, unconsumed);
850 if (err != ARCHIVE_OK)
853 /* Recursively read next header */
854 err = tar_read_header(a, tar, entry, unconsumed);
855 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
858 /* TODO: Examine the first characters to see if this
859 * is an AIX ACL descriptor. We'll likely never support
860 * them, but it would be polite to recognize and warn when
863 /* Leading octal number indicates ACL type and number of entries. */
864 p = acl = tar->acl_text.s;
866 while (*p != '\0' && p < acl + size) {
867 if (*p < '0' || *p > '7') {
868 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
869 "Malformed Solaris ACL attribute (invalid digit)");
870 return(ARCHIVE_WARN);
874 if (type > 077777777) {
875 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
876 "Malformed Solaris ACL attribute (count too large)");
877 return (ARCHIVE_WARN);
881 switch ((int)type & ~0777777) {
886 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
887 "Solaris NFSv4 ACLs not supported");
888 return (ARCHIVE_WARN);
890 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
891 "Malformed Solaris ACL attribute (unsupported type %o)",
893 return (ARCHIVE_WARN);
897 if (p >= acl + size) {
898 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
899 "Malformed Solaris ACL attribute (body overflow)");
900 return(ARCHIVE_WARN);
903 /* ACL text is null-terminated; find the end. */
907 while (*p != '\0' && p < acl + size)
910 if (tar->sconv_acl == NULL) {
911 tar->sconv_acl = archive_string_conversion_from_charset(
912 &(a->archive), "UTF-8", 1);
913 if (tar->sconv_acl == NULL)
914 return (ARCHIVE_FATAL);
916 archive_strncpy(&(tar->localname), acl, p - acl);
917 err = archive_acl_parse_l(archive_entry_acl(entry),
918 tar->localname.s, ARCHIVE_ENTRY_ACL_TYPE_ACCESS, tar->sconv_acl);
919 if (err != ARCHIVE_OK) {
920 if (errno == ENOMEM) {
921 archive_set_error(&a->archive, ENOMEM,
922 "Can't allocate memory for ACL");
924 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
925 "Malformed Solaris ACL attribute (unparsable)");
931 * Interpret 'K' long linkname header.
934 header_longlink(struct archive_read *a, struct tar *tar,
935 struct archive_entry *entry, const void *h, size_t *unconsumed)
939 err = read_body_to_string(a, tar, &(tar->longlink), h, unconsumed);
940 if (err != ARCHIVE_OK)
942 err = tar_read_header(a, tar, entry, unconsumed);
943 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
945 /* Set symlink if symlink already set, else hardlink. */
946 archive_entry_copy_link(entry, tar->longlink.s);
951 set_conversion_failed_error(struct archive_read *a,
952 struct archive_string_conv *sconv, const char *name)
954 if (errno == ENOMEM) {
955 archive_set_error(&a->archive, ENOMEM,
956 "Can't allocate memory for %s", name);
957 return (ARCHIVE_FATAL);
959 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
960 "%s can't be converted from %s to current locale.",
961 name, archive_string_conversion_charset_name(sconv));
962 return (ARCHIVE_WARN);
966 * Interpret 'L' long filename header.
969 header_longname(struct archive_read *a, struct tar *tar,
970 struct archive_entry *entry, const void *h, size_t *unconsumed)
974 err = read_body_to_string(a, tar, &(tar->longname), h, unconsumed);
975 if (err != ARCHIVE_OK)
977 /* Read and parse "real" header, then override name. */
978 err = tar_read_header(a, tar, entry, unconsumed);
979 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
981 if (archive_entry_copy_pathname_l(entry, tar->longname.s,
982 archive_strlen(&(tar->longname)), tar->sconv) != 0)
983 err = set_conversion_failed_error(a, tar->sconv, "Pathname");
989 * Interpret 'V' GNU tar volume header.
992 header_volume(struct archive_read *a, struct tar *tar,
993 struct archive_entry *entry, const void *h, size_t *unconsumed)
997 /* Just skip this and read the next header. */
998 return (tar_read_header(a, tar, entry, unconsumed));
1002 * Read body of an archive entry into an archive_string object.
1005 read_body_to_string(struct archive_read *a, struct tar *tar,
1006 struct archive_string *as, const void *h, size_t *unconsumed)
1009 const struct archive_entry_header_ustar *header;
1012 (void)tar; /* UNUSED */
1013 header = (const struct archive_entry_header_ustar *)h;
1014 size = tar_atol(header->size, sizeof(header->size));
1015 if ((size > 1048576) || (size < 0)) {
1016 archive_set_error(&a->archive, EINVAL,
1017 "Special header too large");
1018 return (ARCHIVE_FATAL);
1021 /* Fail if we can't make our buffer big enough. */
1022 if (archive_string_ensure(as, size+1) == NULL) {
1023 archive_set_error(&a->archive, ENOMEM,
1025 return (ARCHIVE_FATAL);
1028 tar_flush_unconsumed(a, unconsumed);
1030 /* Read the body into the string. */
1031 *unconsumed = (size + 511) & ~ 511;
1032 src = __archive_read_ahead(a, *unconsumed, NULL);
1035 return (ARCHIVE_FATAL);
1037 memcpy(as->s, src, size);
1040 return (ARCHIVE_OK);
1044 * Parse out common header elements.
1046 * This would be the same as header_old_tar, except that the
1047 * filename is handled slightly differently for old and POSIX
1048 * entries (POSIX entries support a 'prefix'). This factoring
1049 * allows header_old_tar and header_ustar
1050 * to handle filenames differently, while still putting most of the
1051 * common parsing into one place.
1054 header_common(struct archive_read *a, struct tar *tar,
1055 struct archive_entry *entry, const void *h)
1057 const struct archive_entry_header_ustar *header;
1059 int err = ARCHIVE_OK;
1061 header = (const struct archive_entry_header_ustar *)h;
1062 if (header->linkname[0])
1063 archive_strncpy(&(tar->entry_linkpath),
1064 header->linkname, sizeof(header->linkname));
1066 archive_string_empty(&(tar->entry_linkpath));
1068 /* Parse out the numeric fields (all are octal) */
1069 archive_entry_set_mode(entry, tar_atol(header->mode, sizeof(header->mode)));
1070 archive_entry_set_uid(entry, tar_atol(header->uid, sizeof(header->uid)));
1071 archive_entry_set_gid(entry, tar_atol(header->gid, sizeof(header->gid)));
1072 tar->entry_bytes_remaining = tar_atol(header->size, sizeof(header->size));
1073 if (tar->entry_bytes_remaining < 0) {
1074 tar->entry_bytes_remaining = 0;
1075 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1076 "Tar entry has negative size?");
1079 tar->realsize = tar->entry_bytes_remaining;
1080 archive_entry_set_size(entry, tar->entry_bytes_remaining);
1081 archive_entry_set_mtime(entry, tar_atol(header->mtime, sizeof(header->mtime)), 0);
1083 /* Handle the tar type flag appropriately. */
1084 tartype = header->typeflag[0];
1087 case '1': /* Hard link */
1088 if (archive_entry_copy_hardlink_l(entry, tar->entry_linkpath.s,
1089 archive_strlen(&(tar->entry_linkpath)), tar->sconv) != 0) {
1090 err = set_conversion_failed_error(a, tar->sconv,
1092 if (err == ARCHIVE_FATAL)
1096 * The following may seem odd, but: Technically, tar
1097 * does not store the file type for a "hard link"
1098 * entry, only the fact that it is a hard link. So, I
1099 * leave the type zero normally. But, pax interchange
1100 * format allows hard links to have data, which
1101 * implies that the underlying entry is a regular
1104 if (archive_entry_size(entry) > 0)
1105 archive_entry_set_filetype(entry, AE_IFREG);
1108 * A tricky point: Traditionally, tar readers have
1109 * ignored the size field when reading hardlink
1110 * entries, and some writers put non-zero sizes even
1111 * though the body is empty. POSIX blessed this
1112 * convention in the 1988 standard, but broke with
1113 * this tradition in 2001 by permitting hardlink
1114 * entries to store valid bodies in pax interchange
1115 * format, but not in ustar format. Since there is no
1116 * hard and fast way to distinguish pax interchange
1117 * from earlier archives (the 'x' and 'g' entries are
1118 * optional, after all), we need a heuristic.
1120 if (archive_entry_size(entry) == 0) {
1121 /* If the size is already zero, we're done. */
1122 } else if (a->archive.archive_format
1123 == ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) {
1124 /* Definitely pax extended; must obey hardlink size. */
1125 } else if (a->archive.archive_format == ARCHIVE_FORMAT_TAR
1126 || a->archive.archive_format == ARCHIVE_FORMAT_TAR_GNUTAR)
1128 /* Old-style or GNU tar: we must ignore the size. */
1129 archive_entry_set_size(entry, 0);
1130 tar->entry_bytes_remaining = 0;
1131 } else if (archive_read_format_tar_bid(a, 50) > 50) {
1133 * We don't know if it's pax: If the bid
1134 * function sees a valid ustar header
1135 * immediately following, then let's ignore
1136 * the hardlink size.
1138 archive_entry_set_size(entry, 0);
1139 tar->entry_bytes_remaining = 0;
1142 * TODO: There are still two cases I'd like to handle:
1143 * = a ustar non-pax archive with a hardlink entry at
1144 * end-of-archive. (Look for block of nulls following?)
1145 * = a pax archive that has not seen any pax headers
1146 * and has an entry which is a hardlink entry storing
1147 * a body containing an uncompressed tar archive.
1148 * The first is worth addressing; I don't see any reliable
1149 * way to deal with the second possibility.
1152 case '2': /* Symlink */
1153 archive_entry_set_filetype(entry, AE_IFLNK);
1154 archive_entry_set_size(entry, 0);
1155 tar->entry_bytes_remaining = 0;
1156 if (archive_entry_copy_symlink_l(entry, tar->entry_linkpath.s,
1157 archive_strlen(&(tar->entry_linkpath)), tar->sconv) != 0) {
1158 err = set_conversion_failed_error(a, tar->sconv,
1160 if (err == ARCHIVE_FATAL)
1164 case '3': /* Character device */
1165 archive_entry_set_filetype(entry, AE_IFCHR);
1166 archive_entry_set_size(entry, 0);
1167 tar->entry_bytes_remaining = 0;
1169 case '4': /* Block device */
1170 archive_entry_set_filetype(entry, AE_IFBLK);
1171 archive_entry_set_size(entry, 0);
1172 tar->entry_bytes_remaining = 0;
1175 archive_entry_set_filetype(entry, AE_IFDIR);
1176 archive_entry_set_size(entry, 0);
1177 tar->entry_bytes_remaining = 0;
1179 case '6': /* FIFO device */
1180 archive_entry_set_filetype(entry, AE_IFIFO);
1181 archive_entry_set_size(entry, 0);
1182 tar->entry_bytes_remaining = 0;
1184 case 'D': /* GNU incremental directory type */
1186 * No special handling is actually required here.
1187 * It might be nice someday to preprocess the file list and
1188 * provide it to the client, though.
1190 archive_entry_set_filetype(entry, AE_IFDIR);
1192 case 'M': /* GNU "Multi-volume" (remainder of file from last archive)*/
1194 * As far as I can tell, this is just like a regular file
1195 * entry, except that the contents should be _appended_ to
1196 * the indicated file at the indicated offset. This may
1197 * require some API work to fully support.
1200 case 'N': /* Old GNU "long filename" entry. */
1201 /* The body of this entry is a script for renaming
1202 * previously-extracted entries. Ugh. It will never
1203 * be supported by libarchive. */
1204 archive_entry_set_filetype(entry, AE_IFREG);
1206 case 'S': /* GNU sparse files */
1208 * Sparse files are really just regular files with
1209 * sparse information in the extended area.
1212 default: /* Regular file and non-standard types */
1214 * Per POSIX: non-recognized types should always be
1215 * treated as regular files.
1217 archive_entry_set_filetype(entry, AE_IFREG);
1224 * Parse out header elements for "old-style" tar archives.
1227 header_old_tar(struct archive_read *a, struct tar *tar,
1228 struct archive_entry *entry, const void *h)
1230 const struct archive_entry_header_ustar *header;
1231 int err = ARCHIVE_OK, err2;
1233 /* Copy filename over (to ensure null termination). */
1234 header = (const struct archive_entry_header_ustar *)h;
1235 if (archive_entry_copy_pathname_l(entry,
1236 header->name, sizeof(header->name), tar->sconv) != 0) {
1237 err = set_conversion_failed_error(a, tar->sconv, "Pathname");
1238 if (err == ARCHIVE_FATAL)
1242 /* Grab rest of common fields */
1243 err2 = header_common(a, tar, entry, h);
1247 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1252 * Read a Mac AppleDouble-encoded blob of file metadata,
1256 read_mac_metadata_blob(struct archive_read *a, struct tar *tar,
1257 struct archive_entry *entry, const void *h, size_t *unconsumed)
1261 const char *p, *name;
1262 const wchar_t *wp, *wname;
1264 (void)h; /* UNUSED */
1266 wname = wp = archive_entry_pathname_w(entry);
1268 /* Find the last path element. */
1269 for (; *wp != L'\0'; ++wp) {
1270 if (wp[0] == '/' && wp[1] != L'\0')
1274 * If last path element starts with "._", then
1275 * this is a Mac extension.
1277 if (wname[0] != L'.' || wname[1] != L'_' || wname[2] == L'\0')
1280 /* Find the last path element. */
1281 name = p = archive_entry_pathname(entry);
1283 return (ARCHIVE_FAILED);
1284 for (; *p != '\0'; ++p) {
1285 if (p[0] == '/' && p[1] != '\0')
1289 * If last path element starts with "._", then
1290 * this is a Mac extension.
1292 if (name[0] != '.' || name[1] != '_' || name[2] == '\0')
1296 /* Read the body as a Mac OS metadata blob. */
1297 size = archive_entry_size(entry);
1300 * TODO: Look beyond the body here to peek at the next header.
1301 * If it's a regular header (not an extension header)
1302 * that has the wrong name, just return the current
1303 * entry as-is, without consuming the body here.
1304 * That would reduce the risk of us mis-identifying
1305 * an ordinary file that just happened to have
1306 * a name starting with "._".
1308 * Q: Is the above idea really possible? Even
1309 * when there are GNU or pax extension entries?
1311 data = __archive_read_ahead(a, size, NULL);
1314 return (ARCHIVE_FATAL);
1316 archive_entry_copy_mac_metadata(entry, data, size);
1317 *unconsumed = (size + 511) & ~ 511;
1318 tar_flush_unconsumed(a, unconsumed);
1319 return (tar_read_header(a, tar, entry, unconsumed));
1323 * Parse a file header for a pax extended archive entry.
1326 header_pax_global(struct archive_read *a, struct tar *tar,
1327 struct archive_entry *entry, const void *h, size_t *unconsumed)
1331 err = read_body_to_string(a, tar, &(tar->pax_global), h, unconsumed);
1332 if (err != ARCHIVE_OK)
1334 err = tar_read_header(a, tar, entry, unconsumed);
1339 header_pax_extensions(struct archive_read *a, struct tar *tar,
1340 struct archive_entry *entry, const void *h, size_t *unconsumed)
1344 err = read_body_to_string(a, tar, &(tar->pax_header), h, unconsumed);
1345 if (err != ARCHIVE_OK)
1348 /* Parse the next header. */
1349 err = tar_read_header(a, tar, entry, unconsumed);
1350 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
1354 * TODO: Parse global/default options into 'entry' struct here
1355 * before handling file-specific options.
1357 * This design (parse standard header, then overwrite with pax
1358 * extended attribute data) usually works well, but isn't ideal;
1359 * it would be better to parse the pax extended attributes first
1360 * and then skip any fields in the standard header that were
1361 * defined in the pax header.
1363 err2 = pax_header(a, tar, entry, tar->pax_header.s);
1364 err = err_combine(err, err2);
1365 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1371 * Parse a file header for a Posix "ustar" archive entry. This also
1372 * handles "pax" or "extended ustar" entries.
1375 header_ustar(struct archive_read *a, struct tar *tar,
1376 struct archive_entry *entry, const void *h)
1378 const struct archive_entry_header_ustar *header;
1379 struct archive_string *as;
1380 int err = ARCHIVE_OK, r;
1382 header = (const struct archive_entry_header_ustar *)h;
1384 /* Copy name into an internal buffer to ensure null-termination. */
1385 as = &(tar->entry_pathname);
1386 if (header->prefix[0]) {
1387 archive_strncpy(as, header->prefix, sizeof(header->prefix));
1388 if (as->s[archive_strlen(as) - 1] != '/')
1389 archive_strappend_char(as, '/');
1390 archive_strncat(as, header->name, sizeof(header->name));
1392 archive_strncpy(as, header->name, sizeof(header->name));
1394 if (archive_entry_copy_pathname_l(entry, as->s, archive_strlen(as),
1396 err = set_conversion_failed_error(a, tar->sconv, "Pathname");
1397 if (err == ARCHIVE_FATAL)
1401 /* Handle rest of common fields. */
1402 r = header_common(a, tar, entry, h);
1403 if (r == ARCHIVE_FATAL)
1408 /* Handle POSIX ustar fields. */
1409 if (archive_entry_copy_uname_l(entry,
1410 header->uname, sizeof(header->uname), tar->sconv) != 0) {
1411 err = set_conversion_failed_error(a, tar->sconv, "Uname");
1412 if (err == ARCHIVE_FATAL)
1416 if (archive_entry_copy_gname_l(entry,
1417 header->gname, sizeof(header->gname), tar->sconv) != 0) {
1418 err = set_conversion_failed_error(a, tar->sconv, "Gname");
1419 if (err == ARCHIVE_FATAL)
1423 /* Parse out device numbers only for char and block specials. */
1424 if (header->typeflag[0] == '3' || header->typeflag[0] == '4') {
1425 archive_entry_set_rdevmajor(entry,
1426 tar_atol(header->rdevmajor, sizeof(header->rdevmajor)));
1427 archive_entry_set_rdevminor(entry,
1428 tar_atol(header->rdevminor, sizeof(header->rdevminor)));
1431 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1438 * Parse the pax extended attributes record.
1440 * Returns non-zero if there's an error in the data.
1443 pax_header(struct archive_read *a, struct tar *tar,
1444 struct archive_entry *entry, char *attr)
1446 size_t attr_length, l, line_length;
1449 struct archive_string *as;
1450 struct archive_string_conv *sconv;
1453 attr_length = strlen(attr);
1454 tar->pax_hdrcharset_binary = 0;
1455 archive_string_empty(&(tar->entry_gname));
1456 archive_string_empty(&(tar->entry_linkpath));
1457 archive_string_empty(&(tar->entry_pathname));
1458 archive_string_empty(&(tar->entry_pathname_override));
1459 archive_string_empty(&(tar->entry_uname));
1461 while (attr_length > 0) {
1462 /* Parse decimal length field at start of line. */
1465 p = attr; /* Record start of line. */
1472 if (*p < '0' || *p > '9') {
1473 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1474 "Ignoring malformed pax extended attributes");
1475 return (ARCHIVE_WARN);
1478 line_length += *p - '0';
1479 if (line_length > 999999) {
1480 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1481 "Rejecting pax extended attribute > 1MB");
1482 return (ARCHIVE_WARN);
1489 * Parsed length must be no bigger than available data,
1490 * at least 1, and the last character of the line must
1493 if (line_length > attr_length
1495 || attr[line_length - 1] != '\n')
1497 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1498 "Ignoring malformed pax extended attribute");
1499 return (ARCHIVE_WARN);
1502 /* Null-terminate the line. */
1503 attr[line_length - 1] = '\0';
1505 /* Find end of key and null terminate it. */
1509 while (*p && *p != '=')
1512 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1513 "Invalid pax extended attributes");
1514 return (ARCHIVE_WARN);
1518 /* Identify null-terminated 'value' portion. */
1521 /* Identify this attribute and set it in the entry. */
1522 err2 = pax_attribute(a, tar, entry, key, value);
1523 if (err2 == ARCHIVE_FATAL)
1525 err = err_combine(err, err2);
1527 /* Skip to next line */
1528 attr += line_length;
1529 attr_length -= line_length;
1533 * PAX format uses UTF-8 as default charset for its metadata
1534 * unless hdrcharset=BINARY is present in its header.
1535 * We apply the charset specified by the hdrcharset option only
1536 * when the hdrcharset attribute(in PAX header) is BINARY because
1537 * we respect the charset described in PAX header and BINARY also
1538 * means that metadata(filename,uname and gname) character-set
1541 if (tar->pax_hdrcharset_binary)
1542 sconv = tar->opt_sconv;
1544 sconv = archive_string_conversion_from_charset(
1545 &(a->archive), "UTF-8", 1);
1547 return (ARCHIVE_FATAL);
1549 archive_string_conversion_set_opt(sconv,
1550 SCONV_SET_OPT_UTF8_LIBARCHIVE2X);
1553 if (archive_strlen(&(tar->entry_gname)) > 0) {
1554 if (archive_entry_copy_gname_l(entry, tar->entry_gname.s,
1555 archive_strlen(&(tar->entry_gname)), sconv) != 0) {
1556 err = set_conversion_failed_error(a, sconv, "Gname");
1557 if (err == ARCHIVE_FATAL)
1559 /* Use a converted an original name. */
1560 archive_entry_copy_gname(entry, tar->entry_gname.s);
1563 if (archive_strlen(&(tar->entry_linkpath)) > 0) {
1564 if (archive_entry_copy_link_l(entry, tar->entry_linkpath.s,
1565 archive_strlen(&(tar->entry_linkpath)), sconv) != 0) {
1566 err = set_conversion_failed_error(a, sconv, "Linkname");
1567 if (err == ARCHIVE_FATAL)
1569 /* Use a converted an original name. */
1570 archive_entry_copy_link(entry, tar->entry_linkpath.s);
1574 * Some extensions (such as the GNU sparse file extensions)
1575 * deliberately store a synthetic name under the regular 'path'
1576 * attribute and the real file name under a different attribute.
1577 * Since we're supposed to not care about the order, we
1578 * have no choice but to store all of the various filenames
1579 * we find and figure it all out afterwards. This is the
1580 * figuring out part.
1583 if (archive_strlen(&(tar->entry_pathname_override)) > 0)
1584 as = &(tar->entry_pathname_override);
1585 else if (archive_strlen(&(tar->entry_pathname)) > 0)
1586 as = &(tar->entry_pathname);
1588 if (archive_entry_copy_pathname_l(entry, as->s,
1589 archive_strlen(as), sconv) != 0) {
1590 err = set_conversion_failed_error(a, sconv, "Pathname");
1591 if (err == ARCHIVE_FATAL)
1593 /* Use a converted an original name. */
1594 archive_entry_copy_pathname(entry, as->s);
1597 if (archive_strlen(&(tar->entry_uname)) > 0) {
1598 if (archive_entry_copy_uname_l(entry, tar->entry_uname.s,
1599 archive_strlen(&(tar->entry_uname)), sconv) != 0) {
1600 err = set_conversion_failed_error(a, sconv, "Uname");
1601 if (err == ARCHIVE_FATAL)
1603 /* Use a converted an original name. */
1604 archive_entry_copy_uname(entry, tar->entry_uname.s);
1611 pax_attribute_xattr(struct archive_entry *entry,
1612 char *name, char *value)
1615 void *value_decoded;
1618 if (strlen(name) < 18 || (memcmp(name, "LIBARCHIVE.xattr.", 17)) != 0)
1623 /* URL-decode name */
1624 name_decoded = url_decode(name);
1625 if (name_decoded == NULL)
1628 /* Base-64 decode value */
1629 value_decoded = base64_decode(value, strlen(value), &value_len);
1630 if (value_decoded == NULL) {
1635 archive_entry_xattr_add_entry(entry, name_decoded,
1636 value_decoded, value_len);
1639 free(value_decoded);
1644 * Parse a single key=value attribute. key/value pointers are
1645 * assumed to point into reasonably long-lived storage.
1647 * Note that POSIX reserves all-lowercase keywords. Vendor-specific
1648 * extensions should always have keywords of the form "VENDOR.attribute"
1649 * In particular, it's quite feasible to support many different
1650 * vendor extensions here. I'm using "LIBARCHIVE" for extensions
1651 * unique to this library.
1653 * Investigate other vendor-specific extensions and see if
1654 * any of them look useful.
1657 pax_attribute(struct archive_read *a, struct tar *tar,
1658 struct archive_entry *entry, char *key, char *value)
1662 int err = ARCHIVE_OK, r;
1666 /* GNU "0.0" sparse pax format. */
1667 if (strcmp(key, "GNU.sparse.numblocks") == 0) {
1668 tar->sparse_offset = -1;
1669 tar->sparse_numbytes = -1;
1670 tar->sparse_gnu_major = 0;
1671 tar->sparse_gnu_minor = 0;
1673 if (strcmp(key, "GNU.sparse.offset") == 0) {
1674 tar->sparse_offset = tar_atol10(value, strlen(value));
1675 if (tar->sparse_numbytes != -1) {
1676 if (gnu_add_sparse_entry(a, tar,
1677 tar->sparse_offset, tar->sparse_numbytes)
1679 return (ARCHIVE_FATAL);
1680 tar->sparse_offset = -1;
1681 tar->sparse_numbytes = -1;
1684 if (strcmp(key, "GNU.sparse.numbytes") == 0) {
1685 tar->sparse_numbytes = tar_atol10(value, strlen(value));
1686 if (tar->sparse_numbytes != -1) {
1687 if (gnu_add_sparse_entry(a, tar,
1688 tar->sparse_offset, tar->sparse_numbytes)
1690 return (ARCHIVE_FATAL);
1691 tar->sparse_offset = -1;
1692 tar->sparse_numbytes = -1;
1695 if (strcmp(key, "GNU.sparse.size") == 0) {
1696 tar->realsize = tar_atol10(value, strlen(value));
1697 archive_entry_set_size(entry, tar->realsize);
1700 /* GNU "0.1" sparse pax format. */
1701 if (strcmp(key, "GNU.sparse.map") == 0) {
1702 tar->sparse_gnu_major = 0;
1703 tar->sparse_gnu_minor = 1;
1704 if (gnu_sparse_01_parse(a, tar, value) != ARCHIVE_OK)
1705 return (ARCHIVE_WARN);
1708 /* GNU "1.0" sparse pax format */
1709 if (strcmp(key, "GNU.sparse.major") == 0) {
1710 tar->sparse_gnu_major = tar_atol10(value, strlen(value));
1711 tar->sparse_gnu_pending = 1;
1713 if (strcmp(key, "GNU.sparse.minor") == 0) {
1714 tar->sparse_gnu_minor = tar_atol10(value, strlen(value));
1715 tar->sparse_gnu_pending = 1;
1717 if (strcmp(key, "GNU.sparse.name") == 0) {
1719 * The real filename; when storing sparse
1720 * files, GNU tar puts a synthesized name into
1721 * the regular 'path' attribute in an attempt
1722 * to limit confusion. ;-)
1724 archive_strcpy(&(tar->entry_pathname_override), value);
1726 if (strcmp(key, "GNU.sparse.realsize") == 0) {
1727 tar->realsize = tar_atol10(value, strlen(value));
1728 archive_entry_set_size(entry, tar->realsize);
1732 /* Our extensions */
1733 /* TODO: Handle arbitrary extended attributes... */
1735 if (strcmp(key, "LIBARCHIVE.xxxxxxx") == 0)
1736 archive_entry_set_xxxxxx(entry, value);
1738 if (strcmp(key, "LIBARCHIVE.creationtime") == 0) {
1739 pax_time(value, &s, &n);
1740 archive_entry_set_birthtime(entry, s, n);
1742 if (memcmp(key, "LIBARCHIVE.xattr.", 17) == 0)
1743 pax_attribute_xattr(entry, key, value);
1746 /* We support some keys used by the "star" archiver */
1747 if (strcmp(key, "SCHILY.acl.access") == 0) {
1748 if (tar->sconv_acl == NULL) {
1750 archive_string_conversion_from_charset(
1751 &(a->archive), "UTF-8", 1);
1752 if (tar->sconv_acl == NULL)
1753 return (ARCHIVE_FATAL);
1756 r = archive_acl_parse_l(archive_entry_acl(entry),
1757 value, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
1759 if (r != ARCHIVE_OK) {
1761 if (err == ARCHIVE_FATAL) {
1762 archive_set_error(&a->archive, ENOMEM,
1763 "Can't allocate memory for "
1764 "SCHILY.acl.access");
1767 archive_set_error(&a->archive,
1769 "Parse error: SCHILY.acl.access");
1771 } else if (strcmp(key, "SCHILY.acl.default") == 0) {
1772 if (tar->sconv_acl == NULL) {
1774 archive_string_conversion_from_charset(
1775 &(a->archive), "UTF-8", 1);
1776 if (tar->sconv_acl == NULL)
1777 return (ARCHIVE_FATAL);
1780 r = archive_acl_parse_l(archive_entry_acl(entry),
1781 value, ARCHIVE_ENTRY_ACL_TYPE_DEFAULT,
1783 if (r != ARCHIVE_OK) {
1785 if (err == ARCHIVE_FATAL) {
1786 archive_set_error(&a->archive, ENOMEM,
1787 "Can't allocate memory for "
1788 "SCHILY.acl.default");
1791 archive_set_error(&a->archive,
1793 "Parse error: SCHILY.acl.default");
1795 } else if (strcmp(key, "SCHILY.devmajor") == 0) {
1796 archive_entry_set_rdevmajor(entry,
1797 tar_atol10(value, strlen(value)));
1798 } else if (strcmp(key, "SCHILY.devminor") == 0) {
1799 archive_entry_set_rdevminor(entry,
1800 tar_atol10(value, strlen(value)));
1801 } else if (strcmp(key, "SCHILY.fflags") == 0) {
1802 archive_entry_copy_fflags_text(entry, value);
1803 } else if (strcmp(key, "SCHILY.dev") == 0) {
1804 archive_entry_set_dev(entry,
1805 tar_atol10(value, strlen(value)));
1806 } else if (strcmp(key, "SCHILY.ino") == 0) {
1807 archive_entry_set_ino(entry,
1808 tar_atol10(value, strlen(value)));
1809 } else if (strcmp(key, "SCHILY.nlink") == 0) {
1810 archive_entry_set_nlink(entry,
1811 tar_atol10(value, strlen(value)));
1812 } else if (strcmp(key, "SCHILY.realsize") == 0) {
1813 tar->realsize = tar_atol10(value, strlen(value));
1814 archive_entry_set_size(entry, tar->realsize);
1815 } else if (strcmp(key, "SUN.holesdata") == 0) {
1816 /* A Solaris extension for sparse. */
1817 r = solaris_sparse_parse(a, tar, entry, value);
1819 if (r == ARCHIVE_FATAL)
1822 archive_set_error(&a->archive,
1824 "Parse error: SUN.holesdata");
1829 if (strcmp(key, "atime") == 0) {
1830 pax_time(value, &s, &n);
1831 archive_entry_set_atime(entry, s, n);
1835 if (strcmp(key, "ctime") == 0) {
1836 pax_time(value, &s, &n);
1837 archive_entry_set_ctime(entry, s, n);
1838 } else if (strcmp(key, "charset") == 0) {
1839 /* TODO: Publish charset information in entry. */
1840 } else if (strcmp(key, "comment") == 0) {
1841 /* TODO: Publish comment in entry. */
1845 if (strcmp(key, "gid") == 0) {
1846 archive_entry_set_gid(entry,
1847 tar_atol10(value, strlen(value)));
1848 } else if (strcmp(key, "gname") == 0) {
1849 archive_strcpy(&(tar->entry_gname), value);
1853 if (strcmp(key, "hdrcharset") == 0) {
1854 if (strcmp(value, "BINARY") == 0)
1856 tar->pax_hdrcharset_binary = 1;
1857 else if (strcmp(value, "ISO-IR 10646 2000 UTF-8") == 0)
1858 tar->pax_hdrcharset_binary = 0;
1862 /* pax interchange doesn't distinguish hardlink vs. symlink. */
1863 if (strcmp(key, "linkpath") == 0) {
1864 archive_strcpy(&(tar->entry_linkpath), value);
1868 if (strcmp(key, "mtime") == 0) {
1869 pax_time(value, &s, &n);
1870 archive_entry_set_mtime(entry, s, n);
1874 if (strcmp(key, "path") == 0) {
1875 archive_strcpy(&(tar->entry_pathname), value);
1879 /* POSIX has reserved 'realtime.*' */
1882 /* POSIX has reserved 'security.*' */
1883 /* Someday: if (strcmp(key, "security.acl") == 0) { ... } */
1884 if (strcmp(key, "size") == 0) {
1885 /* "size" is the size of the data in the entry. */
1886 tar->entry_bytes_remaining
1887 = tar_atol10(value, strlen(value));
1889 * But, "size" is not necessarily the size of
1890 * the file on disk; if this is a sparse file,
1891 * the disk size may have already been set from
1892 * GNU.sparse.realsize or GNU.sparse.size or
1893 * an old GNU header field or SCHILY.realsize
1896 if (tar->realsize < 0) {
1897 archive_entry_set_size(entry,
1898 tar->entry_bytes_remaining);
1900 = tar->entry_bytes_remaining;
1905 if (strcmp(key, "uid") == 0) {
1906 archive_entry_set_uid(entry,
1907 tar_atol10(value, strlen(value)));
1908 } else if (strcmp(key, "uname") == 0) {
1909 archive_strcpy(&(tar->entry_uname), value);
1919 * parse a decimal time value, which may include a fractional portion
1922 pax_time(const char *p, int64_t *ps, long *pn)
1928 int64_t limit, last_digit_limit;
1930 limit = INT64_MAX / 10;
1931 last_digit_limit = INT64_MAX % 10;
1939 while (*p >= '0' && *p <= '9') {
1942 (s == limit && digit > last_digit_limit)) {
1946 s = (s * 10) + digit;
1952 /* Calculate nanoseconds. */
1961 if (*p >= '0' && *p <= '9')
1962 *pn += (*p - '0') * l;
1969 * Parse GNU tar header
1972 header_gnutar(struct archive_read *a, struct tar *tar,
1973 struct archive_entry *entry, const void *h, size_t *unconsumed)
1975 const struct archive_entry_header_gnutar *header;
1977 int err = ARCHIVE_OK;
1980 * GNU header is like POSIX ustar, except 'prefix' is
1981 * replaced with some other fields. This also means the
1982 * filename is stored as in old-style archives.
1985 /* Grab fields common to all tar variants. */
1986 err = header_common(a, tar, entry, h);
1987 if (err == ARCHIVE_FATAL)
1990 /* Copy filename over (to ensure null termination). */
1991 header = (const struct archive_entry_header_gnutar *)h;
1992 if (archive_entry_copy_pathname_l(entry,
1993 header->name, sizeof(header->name), tar->sconv) != 0) {
1994 err = set_conversion_failed_error(a, tar->sconv, "Pathname");
1995 if (err == ARCHIVE_FATAL)
1999 /* Fields common to ustar and GNU */
2000 /* XXX Can the following be factored out since it's common
2001 * to ustar and gnu tar? Is it okay to move it down into
2002 * header_common, perhaps? */
2003 if (archive_entry_copy_uname_l(entry,
2004 header->uname, sizeof(header->uname), tar->sconv) != 0) {
2005 err = set_conversion_failed_error(a, tar->sconv, "Uname");
2006 if (err == ARCHIVE_FATAL)
2010 if (archive_entry_copy_gname_l(entry,
2011 header->gname, sizeof(header->gname), tar->sconv) != 0) {
2012 err = set_conversion_failed_error(a, tar->sconv, "Gname");
2013 if (err == ARCHIVE_FATAL)
2017 /* Parse out device numbers only for char and block specials */
2018 if (header->typeflag[0] == '3' || header->typeflag[0] == '4') {
2019 archive_entry_set_rdevmajor(entry,
2020 tar_atol(header->rdevmajor, sizeof(header->rdevmajor)));
2021 archive_entry_set_rdevminor(entry,
2022 tar_atol(header->rdevminor, sizeof(header->rdevminor)));
2024 archive_entry_set_rdev(entry, 0);
2026 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
2028 /* Grab GNU-specific fields. */
2029 t = tar_atol(header->atime, sizeof(header->atime));
2031 archive_entry_set_atime(entry, t, 0);
2032 t = tar_atol(header->ctime, sizeof(header->ctime));
2034 archive_entry_set_ctime(entry, t, 0);
2036 if (header->realsize[0] != 0) {
2038 = tar_atol(header->realsize, sizeof(header->realsize));
2039 archive_entry_set_size(entry, tar->realsize);
2042 if (header->sparse[0].offset[0] != 0) {
2043 if (gnu_sparse_old_read(a, tar, header, unconsumed)
2045 return (ARCHIVE_FATAL);
2047 if (header->isextended[0] != 0) {
2056 gnu_add_sparse_entry(struct archive_read *a, struct tar *tar,
2057 int64_t offset, int64_t remaining)
2059 struct sparse_block *p;
2061 p = (struct sparse_block *)malloc(sizeof(*p));
2063 archive_set_error(&a->archive, ENOMEM, "Out of memory");
2064 return (ARCHIVE_FATAL);
2066 memset(p, 0, sizeof(*p));
2067 if (tar->sparse_last != NULL)
2068 tar->sparse_last->next = p;
2070 tar->sparse_list = p;
2071 tar->sparse_last = p;
2073 p->remaining = remaining;
2074 return (ARCHIVE_OK);
2078 gnu_clear_sparse_list(struct tar *tar)
2080 struct sparse_block *p;
2082 while (tar->sparse_list != NULL) {
2083 p = tar->sparse_list;
2084 tar->sparse_list = p->next;
2087 tar->sparse_last = NULL;
2091 * GNU tar old-format sparse data.
2093 * GNU old-format sparse data is stored in a fixed-field
2094 * format. Offset/size values are 11-byte octal fields (same
2095 * format as 'size' field in ustart header). These are
2096 * stored in the header, allocating subsequent header blocks
2097 * as needed. Extending the header in this way is a pretty
2098 * severe POSIX violation; this design has earned GNU tar a
2103 gnu_sparse_old_read(struct archive_read *a, struct tar *tar,
2104 const struct archive_entry_header_gnutar *header, size_t *unconsumed)
2109 struct gnu_sparse sparse[21];
2113 const struct extended *ext;
2115 if (gnu_sparse_old_parse(a, tar, header->sparse, 4) != ARCHIVE_OK)
2116 return (ARCHIVE_FATAL);
2117 if (header->isextended[0] == 0)
2118 return (ARCHIVE_OK);
2121 tar_flush_unconsumed(a, unconsumed);
2122 data = __archive_read_ahead(a, 512, &bytes_read);
2124 return (ARCHIVE_FATAL);
2125 if (bytes_read < 512) {
2126 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2127 "Truncated tar archive "
2128 "detected while reading sparse file data");
2129 return (ARCHIVE_FATAL);
2132 ext = (const struct extended *)data;
2133 if (gnu_sparse_old_parse(a, tar, ext->sparse, 21) != ARCHIVE_OK)
2134 return (ARCHIVE_FATAL);
2135 } while (ext->isextended[0] != 0);
2136 if (tar->sparse_list != NULL)
2137 tar->entry_offset = tar->sparse_list->offset;
2138 return (ARCHIVE_OK);
2142 gnu_sparse_old_parse(struct archive_read *a, struct tar *tar,
2143 const struct gnu_sparse *sparse, int length)
2145 while (length > 0 && sparse->offset[0] != 0) {
2146 if (gnu_add_sparse_entry(a, tar,
2147 tar_atol(sparse->offset, sizeof(sparse->offset)),
2148 tar_atol(sparse->numbytes, sizeof(sparse->numbytes)))
2150 return (ARCHIVE_FATAL);
2154 return (ARCHIVE_OK);
2158 * GNU tar sparse format 0.0
2160 * Beginning with GNU tar 1.15, sparse files are stored using
2161 * information in the pax extended header. The GNU tar maintainers
2162 * have gone through a number of variations in the process of working
2163 * out this scheme; fortunately, they're all numbered.
2165 * Sparse format 0.0 uses attribute GNU.sparse.numblocks to store the
2166 * number of blocks, and GNU.sparse.offset/GNU.sparse.numbytes to
2167 * store offset/size for each block. The repeated instances of these
2168 * latter fields violate the pax specification (which frowns on
2169 * duplicate keys), so this format was quickly replaced.
2173 * GNU tar sparse format 0.1
2175 * This version replaced the offset/numbytes attributes with
2176 * a single "map" attribute that stored a list of integers. This
2177 * format had two problems: First, the "map" attribute could be very
2178 * long, which caused problems for some implementations. More
2179 * importantly, the sparse data was lost when extracted by archivers
2180 * that didn't recognize this extension.
2184 gnu_sparse_01_parse(struct archive_read *a, struct tar *tar, const char *p)
2187 int64_t offset = -1, size = -1;
2191 while (*e != '\0' && *e != ',') {
2192 if (*e < '0' || *e > '9')
2193 return (ARCHIVE_WARN);
2197 offset = tar_atol10(p, e - p);
2199 return (ARCHIVE_WARN);
2201 size = tar_atol10(p, e - p);
2203 return (ARCHIVE_WARN);
2204 if (gnu_add_sparse_entry(a, tar, offset, size)
2206 return (ARCHIVE_FATAL);
2210 return (ARCHIVE_OK);
2216 * GNU tar sparse format 1.0
2218 * The idea: The offset/size data is stored as a series of base-10
2219 * ASCII numbers prepended to the file data, so that dearchivers that
2220 * don't support this format will extract the block map along with the
2221 * data and a separate post-process can restore the sparseness.
2223 * Unfortunately, GNU tar 1.16 had a bug that added unnecessary
2224 * padding to the body of the file when using this format. GNU tar
2225 * 1.17 corrected this bug without bumping the version number, so
2226 * it's not possible to support both variants. This code supports
2227 * the later variant at the expense of not supporting the former.
2229 * This variant also replaced GNU.sparse.size with GNU.sparse.realsize
2230 * and introduced the GNU.sparse.major/GNU.sparse.minor attributes.
2234 * Read the next line from the input, and parse it as a decimal
2235 * integer followed by '\n'. Returns positive integer value or
2236 * negative on error.
2239 gnu_sparse_10_atol(struct archive_read *a, struct tar *tar,
2240 int64_t *remaining, size_t *unconsumed)
2242 int64_t l, limit, last_digit_limit;
2248 limit = INT64_MAX / base;
2249 last_digit_limit = INT64_MAX % base;
2252 * Skip any lines starting with '#'; GNU tar specs
2253 * don't require this, but they should.
2256 bytes_read = readline(a, tar, &p, tar_min(*remaining, 100), unconsumed);
2257 if (bytes_read <= 0)
2258 return (ARCHIVE_FATAL);
2259 *remaining -= bytes_read;
2260 } while (p[0] == '#');
2263 while (bytes_read > 0) {
2266 if (*p < '0' || *p >= '0' + base)
2267 return (ARCHIVE_WARN);
2269 if (l > limit || (l == limit && digit > last_digit_limit))
2270 l = INT64_MAX; /* Truncate on overflow. */
2272 l = (l * base) + digit;
2276 /* TODO: Error message. */
2277 return (ARCHIVE_WARN);
2281 * Returns length (in bytes) of the sparse data description
2285 gnu_sparse_10_read(struct archive_read *a, struct tar *tar, size_t *unconsumed)
2289 int64_t offset, size, to_skip, remaining;
2291 /* Clear out the existing sparse list. */
2292 gnu_clear_sparse_list(tar);
2294 remaining = tar->entry_bytes_remaining;
2296 /* Parse entries. */
2297 entries = gnu_sparse_10_atol(a, tar, &remaining, unconsumed);
2299 return (ARCHIVE_FATAL);
2300 /* Parse the individual entries. */
2301 while (entries-- > 0) {
2302 /* Parse offset/size */
2303 offset = gnu_sparse_10_atol(a, tar, &remaining, unconsumed);
2305 return (ARCHIVE_FATAL);
2306 size = gnu_sparse_10_atol(a, tar, &remaining, unconsumed);
2308 return (ARCHIVE_FATAL);
2309 /* Add a new sparse entry. */
2310 if (gnu_add_sparse_entry(a, tar, offset, size) != ARCHIVE_OK)
2311 return (ARCHIVE_FATAL);
2313 /* Skip rest of block... */
2314 tar_flush_unconsumed(a, unconsumed);
2315 bytes_read = tar->entry_bytes_remaining - remaining;
2316 to_skip = 0x1ff & -bytes_read;
2317 if (to_skip != __archive_read_consume(a, to_skip))
2318 return (ARCHIVE_FATAL);
2319 return (bytes_read + to_skip);
2323 * Solaris pax extension for a sparse file. This is recorded with the
2324 * data and hole pairs. The way recording sparse infomation by Solaris'
2325 * pax simply indicates where data and sparse are, so the stored contents
2326 * consist of both data and hole.
2329 solaris_sparse_parse(struct archive_read *a, struct tar *tar,
2330 struct archive_entry *entry, const char *p)
2340 return (ARCHIVE_WARN);
2343 while (*e != '\0' && *e != ' ') {
2344 if (*e < '0' || *e > '9')
2345 return (ARCHIVE_WARN);
2349 end = tar_atol10(p, e - p);
2351 return (ARCHIVE_WARN);
2353 if (gnu_add_sparse_entry(a, tar, start,
2354 end - start) != ARCHIVE_OK)
2355 return (ARCHIVE_FATAL);
2356 tar->sparse_last->hole = hole;
2359 return (ARCHIVE_OK);
2366 * Convert text->integer.
2368 * Traditional tar formats (including POSIX) specify base-8 for
2369 * all of the standard numeric fields. This is a significant limitation
2371 * = file size is limited to 8GB
2372 * = rdevmajor and rdevminor are limited to 21 bits
2373 * = uid/gid are limited to 21 bits
2375 * There are two workarounds for this:
2376 * = pax extended headers, which use variable-length string fields
2377 * = GNU tar and STAR both allow either base-8 or base-256 in
2378 * most fields. The high bit is set to indicate base-256.
2380 * On read, this implementation supports both extensions.
2383 tar_atol(const char *p, unsigned char_cnt)
2386 * Technically, GNU tar considers a field to be in base-256
2387 * only if the first byte is 0xff or 0x80.
2390 return (tar_atol256(p, char_cnt));
2391 return (tar_atol8(p, char_cnt));
2395 * Note that this implementation does not (and should not!) obey
2396 * locale settings; you cannot simply substitute strtol here, since
2397 * it does obey locale.
2400 tar_atol8(const char *p, unsigned char_cnt)
2402 int64_t l, limit, last_digit_limit;
2403 int digit, sign, base;
2406 limit = INT64_MAX / base;
2407 last_digit_limit = INT64_MAX % base;
2409 while (*p == ' ' || *p == '\t')
2419 while (digit >= 0 && digit < base && char_cnt-- > 0) {
2420 if (l>limit || (l == limit && digit > last_digit_limit)) {
2421 l = INT64_MAX; /* Truncate on overflow. */
2424 l = (l * base) + digit;
2427 return (sign < 0) ? -l : l;
2431 * Note that this implementation does not (and should not!) obey
2432 * locale settings; you cannot simply substitute strtol here, since
2433 * it does obey locale.
2436 tar_atol10(const char *p, unsigned char_cnt)
2438 int64_t l, limit, last_digit_limit;
2439 int base, digit, sign;
2442 limit = INT64_MAX / base;
2443 last_digit_limit = INT64_MAX % base;
2445 while (*p == ' ' || *p == '\t')
2455 while (digit >= 0 && digit < base && char_cnt-- > 0) {
2456 if (l > limit || (l == limit && digit > last_digit_limit)) {
2457 l = INT64_MAX; /* Truncate on overflow. */
2460 l = (l * base) + digit;
2463 return (sign < 0) ? -l : l;
2467 * Parse a base-256 integer. This is just a straight signed binary
2468 * value in big-endian order, except that the high-order bit is
2472 tar_atol256(const char *_p, unsigned char_cnt)
2474 int64_t l, upper_limit, lower_limit;
2475 const unsigned char *p = (const unsigned char *)_p;
2477 upper_limit = INT64_MAX / 256;
2478 lower_limit = INT64_MIN / 256;
2480 /* Pad with 1 or 0 bits, depending on sign. */
2481 if ((0x40 & *p) == 0x40)
2485 l = (l << 6) | (0x3f & *p++);
2486 while (--char_cnt > 0) {
2487 if (l > upper_limit) {
2488 l = INT64_MAX; /* Truncate on overflow */
2490 } else if (l < lower_limit) {
2494 l = (l << 8) | (0xff & (int64_t)*p++);
2500 * Returns length of line (including trailing newline)
2501 * or negative on error. 'start' argument is updated to
2502 * point to first character of line. This avoids copying
2506 readline(struct archive_read *a, struct tar *tar, const char **start,
2507 ssize_t limit, size_t *unconsumed)
2510 ssize_t total_size = 0;
2515 tar_flush_unconsumed(a, unconsumed);
2517 t = __archive_read_ahead(a, 1, &bytes_read);
2518 if (bytes_read <= 0)
2519 return (ARCHIVE_FATAL);
2520 s = t; /* Start of line? */
2521 p = memchr(t, '\n', bytes_read);
2522 /* If we found '\n' in the read buffer, return pointer to that. */
2524 bytes_read = 1 + ((const char *)p) - s;
2525 if (bytes_read > limit) {
2526 archive_set_error(&a->archive,
2527 ARCHIVE_ERRNO_FILE_FORMAT,
2529 return (ARCHIVE_FATAL);
2531 *unconsumed = bytes_read;
2533 return (bytes_read);
2535 *unconsumed = bytes_read;
2536 /* Otherwise, we need to accumulate in a line buffer. */
2538 if (total_size + bytes_read > limit) {
2539 archive_set_error(&a->archive,
2540 ARCHIVE_ERRNO_FILE_FORMAT,
2542 return (ARCHIVE_FATAL);
2544 if (archive_string_ensure(&tar->line, total_size + bytes_read) == NULL) {
2545 archive_set_error(&a->archive, ENOMEM,
2546 "Can't allocate working buffer");
2547 return (ARCHIVE_FATAL);
2549 memcpy(tar->line.s + total_size, t, bytes_read);
2550 tar_flush_unconsumed(a, unconsumed);
2551 total_size += bytes_read;
2552 /* If we found '\n', clean up and return. */
2554 *start = tar->line.s;
2555 return (total_size);
2557 /* Read some more. */
2558 t = __archive_read_ahead(a, 1, &bytes_read);
2559 if (bytes_read <= 0)
2560 return (ARCHIVE_FATAL);
2561 s = t; /* Start of line? */
2562 p = memchr(t, '\n', bytes_read);
2563 /* If we found '\n', trim the read. */
2565 bytes_read = 1 + ((const char *)p) - s;
2567 *unconsumed = bytes_read;
2572 * base64_decode - Base64 decode
2574 * This accepts most variations of base-64 encoding, including:
2575 * * with or without line breaks
2576 * * with or without the final group padded with '=' or '_' characters
2577 * (The most economical Base-64 variant does not pad the last group and
2578 * omits line breaks; RFC1341 used for MIME requires both.)
2581 base64_decode(const char *s, size_t len, size_t *out_len)
2583 static const unsigned char digits[64] = {
2584 'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
2585 'O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b',
2586 'c','d','e','f','g','h','i','j','k','l','m','n','o','p',
2587 'q','r','s','t','u','v','w','x','y','z','0','1','2','3',
2588 '4','5','6','7','8','9','+','/' };
2589 static unsigned char decode_table[128];
2591 const unsigned char *src = (const unsigned char *)s;
2593 /* If the decode table is not yet initialized, prepare it. */
2594 if (decode_table[digits[1]] != 1) {
2596 memset(decode_table, 0xff, sizeof(decode_table));
2597 for (i = 0; i < sizeof(digits); i++)
2598 decode_table[digits[i]] = i;
2601 /* Allocate enough space to hold the entire output. */
2602 /* Note that we may not use all of this... */
2603 out = (char *)malloc(len - len / 4 + 1);
2611 /* Collect the next group of (up to) four characters. */
2614 while (group_size < 4 && len > 0) {
2615 /* '=' or '_' padding indicates final group. */
2616 if (*src == '=' || *src == '_') {
2620 /* Skip illegal characters (including line breaks) */
2621 if (*src > 127 || *src < 32
2622 || decode_table[*src] == 0xff) {
2628 v |= decode_table[*src++];
2632 /* Align a short group properly. */
2633 v <<= 6 * (4 - group_size);
2634 /* Unpack the group we just collected. */
2635 switch (group_size) {
2636 case 4: d[2] = v & 0xff;
2638 case 3: d[1] = (v >> 8) & 0xff;
2640 case 2: d[0] = (v >> 16) & 0xff;
2642 case 1: /* this is invalid! */
2645 d += group_size * 3 / 4;
2653 url_decode(const char *in)
2658 out = (char *)malloc(strlen(in) + 1);
2661 for (s = in, d = out; *s != '\0'; ) {
2662 if (s[0] == '%' && s[1] != '\0' && s[2] != '\0') {
2663 /* Try to convert % escape */
2664 int digit1 = tohex(s[1]);
2665 int digit2 = tohex(s[2]);
2666 if (digit1 >= 0 && digit2 >= 0) {
2667 /* Looks good, consume three chars */
2669 /* Convert output */
2670 *d++ = ((digit1 << 4) | digit2);
2673 /* Else fall through and treat '%' as normal char */
2684 if (c >= '0' && c <= '9')
2686 else if (c >= 'A' && c <= 'F')
2687 return (c - 'A' + 10);
2688 else if (c >= 'a' && c <= 'f')
2689 return (c - 'a' + 10);