2 * Copyright (c) 2003-2007 Tim Kientzle
3 * Copyright (c) 2011-2012 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)
389 /* Note: The "warn" return is just to inform the options
390 * supervisor that we didn't handle it. It will generate
391 * a suitable error if no one used this option. */
392 return (ARCHIVE_WARN);
395 /* utility function- this exists to centralize the logic of tracking
396 * how much unconsumed data we have floating around, and to consume
397 * anything outstanding since we're going to do read_aheads
400 tar_flush_unconsumed(struct archive_read *a, size_t *unconsumed)
404 void *data = (void *)__archive_read_ahead(a, *unconsumed, NULL);
405 * this block of code is to poison claimed unconsumed space, ensuring
406 * things break if it is in use still.
407 * currently it WILL break things, so enable it only for debugging this issue
409 memset(data, 0xff, *unconsumed);
412 __archive_read_consume(a, *unconsumed);
418 * The function invoked by archive_read_next_header(). This
419 * just sets up a few things and then calls the internal
420 * tar_read_header() function below.
423 archive_read_format_tar_read_header(struct archive_read *a,
424 struct archive_entry *entry)
427 * When converting tar archives to cpio archives, it is
428 * essential that each distinct file have a distinct inode
429 * number. To simplify this, we keep a static count here to
430 * assign fake dev/inode numbers to each tar entry. Note that
431 * pax format archives may overwrite this with something more
434 * Ideally, we would track every file read from the archive so
435 * that we could assign the same dev/ino pair to hardlinks,
436 * but the memory required to store a complete lookup table is
437 * probably not worthwhile just to support the relatively
438 * obscure tar->cpio conversion case.
440 static int default_inode;
441 static int default_dev;
445 size_t l, unconsumed = 0;
447 /* Assign default device/inode values. */
448 archive_entry_set_dev(entry, 1 + default_dev); /* Don't use zero. */
449 archive_entry_set_ino(entry, ++default_inode); /* Don't use zero. */
450 /* Limit generated st_ino number to 16 bits. */
451 if (default_inode >= 0xffff) {
456 tar = (struct tar *)(a->format->data);
457 tar->entry_offset = 0;
458 gnu_clear_sparse_list(tar);
459 tar->realsize = -1; /* Mark this as "unset" */
461 /* Setup default string conversion. */
462 tar->sconv = tar->opt_sconv;
463 if (tar->sconv == NULL) {
464 if (!tar->init_default_conversion) {
466 archive_string_default_conversion_for_read(&(a->archive));
467 tar->init_default_conversion = 1;
469 tar->sconv = tar->sconv_default;
472 r = tar_read_header(a, tar, entry, &unconsumed);
474 tar_flush_unconsumed(a, &unconsumed);
477 * "non-sparse" files are really just sparse files with
480 if (tar->sparse_list == NULL) {
481 if (gnu_add_sparse_entry(a, tar, 0, tar->entry_bytes_remaining)
483 return (ARCHIVE_FATAL);
485 struct sparse_block *sb;
487 for (sb = tar->sparse_list; sb != NULL; sb = sb->next) {
489 archive_entry_sparse_add_entry(entry,
490 sb->offset, sb->remaining);
494 if (r == ARCHIVE_OK) {
496 * "Regular" entry with trailing '/' is really
497 * directory: This is needed for certain old tar
498 * variants and even for some broken newer ones.
501 wp = archive_entry_pathname_w(entry);
504 if (archive_entry_filetype(entry) == AE_IFREG
506 archive_entry_set_filetype(entry, AE_IFDIR);
508 p = archive_entry_pathname(entry);
510 return (ARCHIVE_FAILED);
512 if (archive_entry_filetype(entry) == AE_IFREG
514 archive_entry_set_filetype(entry, AE_IFDIR);
521 archive_read_format_tar_read_data(struct archive_read *a,
522 const void **buff, size_t *size, int64_t *offset)
526 struct sparse_block *p;
528 tar = (struct tar *)(a->format->data);
531 /* Remove exhausted entries from sparse list. */
532 while (tar->sparse_list != NULL &&
533 tar->sparse_list->remaining == 0) {
534 p = tar->sparse_list;
535 tar->sparse_list = p->next;
539 if (tar->entry_bytes_unconsumed) {
540 __archive_read_consume(a, tar->entry_bytes_unconsumed);
541 tar->entry_bytes_unconsumed = 0;
544 /* If we're at end of file, return EOF. */
545 if (tar->sparse_list == NULL ||
546 tar->entry_bytes_remaining == 0) {
547 if (__archive_read_consume(a, tar->entry_padding) < 0)
548 return (ARCHIVE_FATAL);
549 tar->entry_padding = 0;
552 *offset = tar->realsize;
553 return (ARCHIVE_EOF);
556 *buff = __archive_read_ahead(a, 1, &bytes_read);
558 return (ARCHIVE_FATAL);
560 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
561 "Truncated tar archive");
562 return (ARCHIVE_FATAL);
564 if (bytes_read > tar->entry_bytes_remaining)
565 bytes_read = (ssize_t)tar->entry_bytes_remaining;
566 /* Don't read more than is available in the
567 * current sparse block. */
568 if (tar->sparse_list->remaining < bytes_read)
569 bytes_read = (ssize_t)tar->sparse_list->remaining;
571 *offset = tar->sparse_list->offset;
572 tar->sparse_list->remaining -= bytes_read;
573 tar->sparse_list->offset += bytes_read;
574 tar->entry_bytes_remaining -= bytes_read;
575 tar->entry_bytes_unconsumed = bytes_read;
577 if (!tar->sparse_list->hole)
579 /* Current is hole data and skip this. */
584 archive_read_format_tar_skip(struct archive_read *a)
586 int64_t bytes_skipped;
589 tar = (struct tar *)(a->format->data);
591 bytes_skipped = __archive_read_consume(a,
592 tar->entry_bytes_remaining + tar->entry_padding +
593 tar->entry_bytes_unconsumed);
594 if (bytes_skipped < 0)
595 return (ARCHIVE_FATAL);
597 tar->entry_bytes_remaining = 0;
598 tar->entry_bytes_unconsumed = 0;
599 tar->entry_padding = 0;
601 /* Free the sparse list. */
602 gnu_clear_sparse_list(tar);
608 * This function recursively interprets all of the headers associated
609 * with a single entry.
612 tar_read_header(struct archive_read *a, struct tar *tar,
613 struct archive_entry *entry, size_t *unconsumed)
618 const struct archive_entry_header_ustar *header;
620 tar_flush_unconsumed(a, unconsumed);
622 /* Read 512-byte header record */
623 h = __archive_read_ahead(a, 512, &bytes);
626 if (bytes == 0) { /* EOF at a block boundary. */
627 /* Some writers do omit the block of nulls. <sigh> */
628 return (ARCHIVE_EOF);
630 if (bytes < 512) { /* Short block at EOF; this is bad. */
631 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
632 "Truncated tar archive");
633 return (ARCHIVE_FATAL);
637 /* Check for end-of-archive mark. */
638 if (h[0] == 0 && archive_block_is_null(h)) {
639 /* Try to consume a second all-null record, as well. */
640 tar_flush_unconsumed(a, unconsumed);
641 h = __archive_read_ahead(a, 512, NULL);
643 __archive_read_consume(a, 512);
644 archive_clear_error(&a->archive);
645 if (a->archive.archive_format_name == NULL) {
646 a->archive.archive_format = ARCHIVE_FORMAT_TAR;
647 a->archive.archive_format_name = "tar";
649 return (ARCHIVE_EOF);
653 * Note: If the checksum fails and we return ARCHIVE_RETRY,
654 * then the client is likely to just retry. This is a very
655 * crude way to search for the next valid header!
657 * TODO: Improve this by implementing a real header scan.
659 if (!checksum(a, h)) {
660 tar_flush_unconsumed(a, unconsumed);
661 archive_set_error(&a->archive, EINVAL, "Damaged tar archive");
662 return (ARCHIVE_RETRY); /* Retryable: Invalid header */
665 if (++tar->header_recursion_depth > 32) {
666 tar_flush_unconsumed(a, unconsumed);
667 archive_set_error(&a->archive, EINVAL, "Too many special headers");
668 return (ARCHIVE_WARN);
671 /* Determine the format variant. */
672 header = (const struct archive_entry_header_ustar *)h;
674 switch(header->typeflag[0]) {
675 case 'A': /* Solaris tar ACL */
676 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
677 a->archive.archive_format_name = "Solaris tar";
678 err = header_Solaris_ACL(a, tar, entry, h, unconsumed);
680 case 'g': /* POSIX-standard 'g' header. */
681 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
682 a->archive.archive_format_name = "POSIX pax interchange format";
683 err = header_pax_global(a, tar, entry, h, unconsumed);
685 case 'K': /* Long link name (GNU tar, others) */
686 err = header_longlink(a, tar, entry, h, unconsumed);
688 case 'L': /* Long filename (GNU tar, others) */
689 err = header_longname(a, tar, entry, h, unconsumed);
691 case 'V': /* GNU volume header */
692 err = header_volume(a, tar, entry, h, unconsumed);
694 case 'X': /* Used by SUN tar; same as 'x'. */
695 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
696 a->archive.archive_format_name =
697 "POSIX pax interchange format (Sun variant)";
698 err = header_pax_extensions(a, tar, entry, h, unconsumed);
700 case 'x': /* POSIX-standard 'x' header. */
701 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
702 a->archive.archive_format_name = "POSIX pax interchange format";
703 err = header_pax_extensions(a, tar, entry, h, unconsumed);
706 if (memcmp(header->magic, "ustar \0", 8) == 0) {
707 a->archive.archive_format = ARCHIVE_FORMAT_TAR_GNUTAR;
708 a->archive.archive_format_name = "GNU tar format";
709 err = header_gnutar(a, tar, entry, h, unconsumed);
710 } else if (memcmp(header->magic, "ustar", 5) == 0) {
711 if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) {
712 a->archive.archive_format = ARCHIVE_FORMAT_TAR_USTAR;
713 a->archive.archive_format_name = "POSIX ustar format";
715 err = header_ustar(a, tar, entry, h);
717 a->archive.archive_format = ARCHIVE_FORMAT_TAR;
718 a->archive.archive_format_name = "tar (non-POSIX)";
719 err = header_old_tar(a, tar, entry, h);
722 if (err == ARCHIVE_FATAL)
725 tar_flush_unconsumed(a, unconsumed);
730 --tar->header_recursion_depth;
731 /* Yuck. Apple's design here ends up storing long pathname
732 * extensions for both the AppleDouble extension entry and the
735 /* TODO: Should this be disabled on non-Mac platforms? */
736 if ((err == ARCHIVE_WARN || err == ARCHIVE_OK) &&
737 tar->header_recursion_depth == 0) {
738 int err2 = read_mac_metadata_blob(a, tar, entry, h, unconsumed);
743 /* We return warnings or success as-is. Anything else is fatal. */
744 if (err == ARCHIVE_WARN || err == ARCHIVE_OK) {
745 if (tar->sparse_gnu_pending) {
746 if (tar->sparse_gnu_major == 1 &&
747 tar->sparse_gnu_minor == 0) {
750 tar->sparse_gnu_pending = 0;
751 /* Read initial sparse map. */
752 bytes_read = gnu_sparse_10_read(a, tar, unconsumed);
753 tar->entry_bytes_remaining -= bytes_read;
757 archive_set_error(&a->archive,
759 "Unrecognized GNU sparse file format");
760 return (ARCHIVE_WARN);
762 tar->sparse_gnu_pending = 0;
766 if (err == ARCHIVE_EOF)
767 /* EOF when recursively reading a header is bad. */
768 archive_set_error(&a->archive, EINVAL, "Damaged tar archive");
769 return (ARCHIVE_FATAL);
773 * Return true if block checksum is correct.
776 checksum(struct archive_read *a, const void *h)
778 const unsigned char *bytes;
779 const struct archive_entry_header_ustar *header;
782 (void)a; /* UNUSED */
783 bytes = (const unsigned char *)h;
784 header = (const struct archive_entry_header_ustar *)h;
787 * Test the checksum. Note that POSIX specifies _unsigned_
788 * bytes for this calculation.
790 sum = (int)tar_atol(header->checksum, sizeof(header->checksum));
792 for (i = 0; i < 148; i++)
793 check += (unsigned char)bytes[i];
797 check += (unsigned char)bytes[i];
802 * Repeat test with _signed_ bytes, just in case this archive
803 * was created by an old BSD, Solaris, or HP-UX tar with a
804 * broken checksum calculation.
807 for (i = 0; i < 148; i++)
808 check += (signed char)bytes[i];
812 check += (signed char)bytes[i];
820 * Return true if this block contains only nulls.
823 archive_block_is_null(const char *p)
827 for (i = 0; i < 512; i++)
834 * Interpret 'A' Solaris ACL header
837 header_Solaris_ACL(struct archive_read *a, struct tar *tar,
838 struct archive_entry *entry, const void *h, size_t *unconsumed)
840 const struct archive_entry_header_ustar *header;
847 * read_body_to_string adds a NUL terminator, but we need a little
848 * more to make sure that we don't overrun acl_text later.
850 header = (const struct archive_entry_header_ustar *)h;
851 size = (size_t)tar_atol(header->size, sizeof(header->size));
852 err = read_body_to_string(a, tar, &(tar->acl_text), h, unconsumed);
853 if (err != ARCHIVE_OK)
856 /* Recursively read next header */
857 err = tar_read_header(a, tar, entry, unconsumed);
858 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
861 /* TODO: Examine the first characters to see if this
862 * is an AIX ACL descriptor. We'll likely never support
863 * them, but it would be polite to recognize and warn when
866 /* Leading octal number indicates ACL type and number of entries. */
867 p = acl = tar->acl_text.s;
869 while (*p != '\0' && p < acl + size) {
870 if (*p < '0' || *p > '7') {
871 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
872 "Malformed Solaris ACL attribute (invalid digit)");
873 return(ARCHIVE_WARN);
877 if (type > 077777777) {
878 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
879 "Malformed Solaris ACL attribute (count too large)");
880 return (ARCHIVE_WARN);
884 switch ((int)type & ~0777777) {
889 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
890 "Solaris NFSv4 ACLs not supported");
891 return (ARCHIVE_WARN);
893 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
894 "Malformed Solaris ACL attribute (unsupported type %o)",
896 return (ARCHIVE_WARN);
900 if (p >= acl + size) {
901 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
902 "Malformed Solaris ACL attribute (body overflow)");
903 return(ARCHIVE_WARN);
906 /* ACL text is null-terminated; find the end. */
910 while (*p != '\0' && p < acl + size)
913 if (tar->sconv_acl == NULL) {
914 tar->sconv_acl = archive_string_conversion_from_charset(
915 &(a->archive), "UTF-8", 1);
916 if (tar->sconv_acl == NULL)
917 return (ARCHIVE_FATAL);
919 archive_strncpy(&(tar->localname), acl, p - acl);
920 err = archive_acl_parse_l(archive_entry_acl(entry),
921 tar->localname.s, ARCHIVE_ENTRY_ACL_TYPE_ACCESS, tar->sconv_acl);
922 if (err != ARCHIVE_OK) {
923 if (errno == ENOMEM) {
924 archive_set_error(&a->archive, ENOMEM,
925 "Can't allocate memory for ACL");
927 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
928 "Malformed Solaris ACL attribute (unparsable)");
934 * Interpret 'K' long linkname header.
937 header_longlink(struct archive_read *a, struct tar *tar,
938 struct archive_entry *entry, const void *h, size_t *unconsumed)
942 err = read_body_to_string(a, tar, &(tar->longlink), h, unconsumed);
943 if (err != ARCHIVE_OK)
945 err = tar_read_header(a, tar, entry, unconsumed);
946 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
948 /* Set symlink if symlink already set, else hardlink. */
949 archive_entry_copy_link(entry, tar->longlink.s);
954 set_conversion_failed_error(struct archive_read *a,
955 struct archive_string_conv *sconv, const char *name)
957 if (errno == ENOMEM) {
958 archive_set_error(&a->archive, ENOMEM,
959 "Can't allocate memory for %s", name);
960 return (ARCHIVE_FATAL);
962 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
963 "%s can't be converted from %s to current locale.",
964 name, archive_string_conversion_charset_name(sconv));
965 return (ARCHIVE_WARN);
969 * Interpret 'L' long filename header.
972 header_longname(struct archive_read *a, struct tar *tar,
973 struct archive_entry *entry, const void *h, size_t *unconsumed)
977 err = read_body_to_string(a, tar, &(tar->longname), h, unconsumed);
978 if (err != ARCHIVE_OK)
980 /* Read and parse "real" header, then override name. */
981 err = tar_read_header(a, tar, entry, unconsumed);
982 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
984 if (archive_entry_copy_pathname_l(entry, tar->longname.s,
985 archive_strlen(&(tar->longname)), tar->sconv) != 0)
986 err = set_conversion_failed_error(a, tar->sconv, "Pathname");
992 * Interpret 'V' GNU tar volume header.
995 header_volume(struct archive_read *a, struct tar *tar,
996 struct archive_entry *entry, const void *h, size_t *unconsumed)
1000 /* Just skip this and read the next header. */
1001 return (tar_read_header(a, tar, entry, unconsumed));
1005 * Read body of an archive entry into an archive_string object.
1008 read_body_to_string(struct archive_read *a, struct tar *tar,
1009 struct archive_string *as, const void *h, size_t *unconsumed)
1012 const struct archive_entry_header_ustar *header;
1015 (void)tar; /* UNUSED */
1016 header = (const struct archive_entry_header_ustar *)h;
1017 size = tar_atol(header->size, sizeof(header->size));
1018 if ((size > 1048576) || (size < 0)) {
1019 archive_set_error(&a->archive, EINVAL,
1020 "Special header too large");
1021 return (ARCHIVE_FATAL);
1024 /* Fail if we can't make our buffer big enough. */
1025 if (archive_string_ensure(as, (size_t)size+1) == NULL) {
1026 archive_set_error(&a->archive, ENOMEM,
1028 return (ARCHIVE_FATAL);
1031 tar_flush_unconsumed(a, unconsumed);
1033 /* Read the body into the string. */
1034 *unconsumed = (size_t)((size + 511) & ~ 511);
1035 src = __archive_read_ahead(a, *unconsumed, NULL);
1038 return (ARCHIVE_FATAL);
1040 memcpy(as->s, src, (size_t)size);
1042 as->length = (size_t)size;
1043 return (ARCHIVE_OK);
1047 * Parse out common header elements.
1049 * This would be the same as header_old_tar, except that the
1050 * filename is handled slightly differently for old and POSIX
1051 * entries (POSIX entries support a 'prefix'). This factoring
1052 * allows header_old_tar and header_ustar
1053 * to handle filenames differently, while still putting most of the
1054 * common parsing into one place.
1057 header_common(struct archive_read *a, struct tar *tar,
1058 struct archive_entry *entry, const void *h)
1060 const struct archive_entry_header_ustar *header;
1062 int err = ARCHIVE_OK;
1064 header = (const struct archive_entry_header_ustar *)h;
1065 if (header->linkname[0])
1066 archive_strncpy(&(tar->entry_linkpath),
1067 header->linkname, sizeof(header->linkname));
1069 archive_string_empty(&(tar->entry_linkpath));
1071 /* Parse out the numeric fields (all are octal) */
1072 archive_entry_set_mode(entry,
1073 (mode_t)tar_atol(header->mode, sizeof(header->mode)));
1074 archive_entry_set_uid(entry, tar_atol(header->uid, sizeof(header->uid)));
1075 archive_entry_set_gid(entry, tar_atol(header->gid, sizeof(header->gid)));
1076 tar->entry_bytes_remaining = tar_atol(header->size, sizeof(header->size));
1077 if (tar->entry_bytes_remaining < 0) {
1078 tar->entry_bytes_remaining = 0;
1079 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1080 "Tar entry has negative size?");
1083 tar->realsize = tar->entry_bytes_remaining;
1084 archive_entry_set_size(entry, tar->entry_bytes_remaining);
1085 archive_entry_set_mtime(entry, tar_atol(header->mtime, sizeof(header->mtime)), 0);
1087 /* Handle the tar type flag appropriately. */
1088 tartype = header->typeflag[0];
1091 case '1': /* Hard link */
1092 if (archive_entry_copy_hardlink_l(entry, tar->entry_linkpath.s,
1093 archive_strlen(&(tar->entry_linkpath)), tar->sconv) != 0) {
1094 err = set_conversion_failed_error(a, tar->sconv,
1096 if (err == ARCHIVE_FATAL)
1100 * The following may seem odd, but: Technically, tar
1101 * does not store the file type for a "hard link"
1102 * entry, only the fact that it is a hard link. So, I
1103 * leave the type zero normally. But, pax interchange
1104 * format allows hard links to have data, which
1105 * implies that the underlying entry is a regular
1108 if (archive_entry_size(entry) > 0)
1109 archive_entry_set_filetype(entry, AE_IFREG);
1112 * A tricky point: Traditionally, tar readers have
1113 * ignored the size field when reading hardlink
1114 * entries, and some writers put non-zero sizes even
1115 * though the body is empty. POSIX blessed this
1116 * convention in the 1988 standard, but broke with
1117 * this tradition in 2001 by permitting hardlink
1118 * entries to store valid bodies in pax interchange
1119 * format, but not in ustar format. Since there is no
1120 * hard and fast way to distinguish pax interchange
1121 * from earlier archives (the 'x' and 'g' entries are
1122 * optional, after all), we need a heuristic.
1124 if (archive_entry_size(entry) == 0) {
1125 /* If the size is already zero, we're done. */
1126 } else if (a->archive.archive_format
1127 == ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) {
1128 /* Definitely pax extended; must obey hardlink size. */
1129 } else if (a->archive.archive_format == ARCHIVE_FORMAT_TAR
1130 || a->archive.archive_format == ARCHIVE_FORMAT_TAR_GNUTAR)
1132 /* Old-style or GNU tar: we must ignore the size. */
1133 archive_entry_set_size(entry, 0);
1134 tar->entry_bytes_remaining = 0;
1135 } else if (archive_read_format_tar_bid(a, 50) > 50) {
1137 * We don't know if it's pax: If the bid
1138 * function sees a valid ustar header
1139 * immediately following, then let's ignore
1140 * the hardlink size.
1142 archive_entry_set_size(entry, 0);
1143 tar->entry_bytes_remaining = 0;
1146 * TODO: There are still two cases I'd like to handle:
1147 * = a ustar non-pax archive with a hardlink entry at
1148 * end-of-archive. (Look for block of nulls following?)
1149 * = a pax archive that has not seen any pax headers
1150 * and has an entry which is a hardlink entry storing
1151 * a body containing an uncompressed tar archive.
1152 * The first is worth addressing; I don't see any reliable
1153 * way to deal with the second possibility.
1156 case '2': /* Symlink */
1157 archive_entry_set_filetype(entry, AE_IFLNK);
1158 archive_entry_set_size(entry, 0);
1159 tar->entry_bytes_remaining = 0;
1160 if (archive_entry_copy_symlink_l(entry, tar->entry_linkpath.s,
1161 archive_strlen(&(tar->entry_linkpath)), tar->sconv) != 0) {
1162 err = set_conversion_failed_error(a, tar->sconv,
1164 if (err == ARCHIVE_FATAL)
1168 case '3': /* Character device */
1169 archive_entry_set_filetype(entry, AE_IFCHR);
1170 archive_entry_set_size(entry, 0);
1171 tar->entry_bytes_remaining = 0;
1173 case '4': /* Block device */
1174 archive_entry_set_filetype(entry, AE_IFBLK);
1175 archive_entry_set_size(entry, 0);
1176 tar->entry_bytes_remaining = 0;
1179 archive_entry_set_filetype(entry, AE_IFDIR);
1180 archive_entry_set_size(entry, 0);
1181 tar->entry_bytes_remaining = 0;
1183 case '6': /* FIFO device */
1184 archive_entry_set_filetype(entry, AE_IFIFO);
1185 archive_entry_set_size(entry, 0);
1186 tar->entry_bytes_remaining = 0;
1188 case 'D': /* GNU incremental directory type */
1190 * No special handling is actually required here.
1191 * It might be nice someday to preprocess the file list and
1192 * provide it to the client, though.
1194 archive_entry_set_filetype(entry, AE_IFDIR);
1196 case 'M': /* GNU "Multi-volume" (remainder of file from last archive)*/
1198 * As far as I can tell, this is just like a regular file
1199 * entry, except that the contents should be _appended_ to
1200 * the indicated file at the indicated offset. This may
1201 * require some API work to fully support.
1204 case 'N': /* Old GNU "long filename" entry. */
1205 /* The body of this entry is a script for renaming
1206 * previously-extracted entries. Ugh. It will never
1207 * be supported by libarchive. */
1208 archive_entry_set_filetype(entry, AE_IFREG);
1210 case 'S': /* GNU sparse files */
1212 * Sparse files are really just regular files with
1213 * sparse information in the extended area.
1216 default: /* Regular file and non-standard types */
1218 * Per POSIX: non-recognized types should always be
1219 * treated as regular files.
1221 archive_entry_set_filetype(entry, AE_IFREG);
1228 * Parse out header elements for "old-style" tar archives.
1231 header_old_tar(struct archive_read *a, struct tar *tar,
1232 struct archive_entry *entry, const void *h)
1234 const struct archive_entry_header_ustar *header;
1235 int err = ARCHIVE_OK, err2;
1237 /* Copy filename over (to ensure null termination). */
1238 header = (const struct archive_entry_header_ustar *)h;
1239 if (archive_entry_copy_pathname_l(entry,
1240 header->name, sizeof(header->name), tar->sconv) != 0) {
1241 err = set_conversion_failed_error(a, tar->sconv, "Pathname");
1242 if (err == ARCHIVE_FATAL)
1246 /* Grab rest of common fields */
1247 err2 = header_common(a, tar, entry, h);
1251 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1256 * Read a Mac AppleDouble-encoded blob of file metadata,
1260 read_mac_metadata_blob(struct archive_read *a, struct tar *tar,
1261 struct archive_entry *entry, const void *h, size_t *unconsumed)
1265 const char *p, *name;
1266 const wchar_t *wp, *wname;
1268 (void)h; /* UNUSED */
1270 wname = wp = archive_entry_pathname_w(entry);
1272 /* Find the last path element. */
1273 for (; *wp != L'\0'; ++wp) {
1274 if (wp[0] == '/' && wp[1] != L'\0')
1278 * If last path element starts with "._", then
1279 * this is a Mac extension.
1281 if (wname[0] != L'.' || wname[1] != L'_' || wname[2] == L'\0')
1284 /* Find the last path element. */
1285 name = p = archive_entry_pathname(entry);
1287 return (ARCHIVE_FAILED);
1288 for (; *p != '\0'; ++p) {
1289 if (p[0] == '/' && p[1] != '\0')
1293 * If last path element starts with "._", then
1294 * this is a Mac extension.
1296 if (name[0] != '.' || name[1] != '_' || name[2] == '\0')
1300 /* Read the body as a Mac OS metadata blob. */
1301 size = archive_entry_size(entry);
1304 * TODO: Look beyond the body here to peek at the next header.
1305 * If it's a regular header (not an extension header)
1306 * that has the wrong name, just return the current
1307 * entry as-is, without consuming the body here.
1308 * That would reduce the risk of us mis-identifying
1309 * an ordinary file that just happened to have
1310 * a name starting with "._".
1312 * Q: Is the above idea really possible? Even
1313 * when there are GNU or pax extension entries?
1315 data = __archive_read_ahead(a, (size_t)size, NULL);
1318 return (ARCHIVE_FATAL);
1320 archive_entry_copy_mac_metadata(entry, data, (size_t)size);
1321 *unconsumed = (size_t)((size + 511) & ~ 511);
1322 tar_flush_unconsumed(a, unconsumed);
1323 return (tar_read_header(a, tar, entry, unconsumed));
1327 * Parse a file header for a pax extended archive entry.
1330 header_pax_global(struct archive_read *a, struct tar *tar,
1331 struct archive_entry *entry, const void *h, size_t *unconsumed)
1335 err = read_body_to_string(a, tar, &(tar->pax_global), h, unconsumed);
1336 if (err != ARCHIVE_OK)
1338 err = tar_read_header(a, tar, entry, unconsumed);
1343 header_pax_extensions(struct archive_read *a, struct tar *tar,
1344 struct archive_entry *entry, const void *h, size_t *unconsumed)
1348 err = read_body_to_string(a, tar, &(tar->pax_header), h, unconsumed);
1349 if (err != ARCHIVE_OK)
1352 /* Parse the next header. */
1353 err = tar_read_header(a, tar, entry, unconsumed);
1354 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
1358 * TODO: Parse global/default options into 'entry' struct here
1359 * before handling file-specific options.
1361 * This design (parse standard header, then overwrite with pax
1362 * extended attribute data) usually works well, but isn't ideal;
1363 * it would be better to parse the pax extended attributes first
1364 * and then skip any fields in the standard header that were
1365 * defined in the pax header.
1367 err2 = pax_header(a, tar, entry, tar->pax_header.s);
1368 err = err_combine(err, err2);
1369 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1375 * Parse a file header for a Posix "ustar" archive entry. This also
1376 * handles "pax" or "extended ustar" entries.
1379 header_ustar(struct archive_read *a, struct tar *tar,
1380 struct archive_entry *entry, const void *h)
1382 const struct archive_entry_header_ustar *header;
1383 struct archive_string *as;
1384 int err = ARCHIVE_OK, r;
1386 header = (const struct archive_entry_header_ustar *)h;
1388 /* Copy name into an internal buffer to ensure null-termination. */
1389 as = &(tar->entry_pathname);
1390 if (header->prefix[0]) {
1391 archive_strncpy(as, header->prefix, sizeof(header->prefix));
1392 if (as->s[archive_strlen(as) - 1] != '/')
1393 archive_strappend_char(as, '/');
1394 archive_strncat(as, header->name, sizeof(header->name));
1396 archive_strncpy(as, header->name, sizeof(header->name));
1398 if (archive_entry_copy_pathname_l(entry, as->s, archive_strlen(as),
1400 err = set_conversion_failed_error(a, tar->sconv, "Pathname");
1401 if (err == ARCHIVE_FATAL)
1405 /* Handle rest of common fields. */
1406 r = header_common(a, tar, entry, h);
1407 if (r == ARCHIVE_FATAL)
1412 /* Handle POSIX ustar fields. */
1413 if (archive_entry_copy_uname_l(entry,
1414 header->uname, sizeof(header->uname), tar->sconv) != 0) {
1415 err = set_conversion_failed_error(a, tar->sconv, "Uname");
1416 if (err == ARCHIVE_FATAL)
1420 if (archive_entry_copy_gname_l(entry,
1421 header->gname, sizeof(header->gname), tar->sconv) != 0) {
1422 err = set_conversion_failed_error(a, tar->sconv, "Gname");
1423 if (err == ARCHIVE_FATAL)
1427 /* Parse out device numbers only for char and block specials. */
1428 if (header->typeflag[0] == '3' || header->typeflag[0] == '4') {
1429 archive_entry_set_rdevmajor(entry, (dev_t)
1430 tar_atol(header->rdevmajor, sizeof(header->rdevmajor)));
1431 archive_entry_set_rdevminor(entry, (dev_t)
1432 tar_atol(header->rdevminor, sizeof(header->rdevminor)));
1435 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1442 * Parse the pax extended attributes record.
1444 * Returns non-zero if there's an error in the data.
1447 pax_header(struct archive_read *a, struct tar *tar,
1448 struct archive_entry *entry, char *attr)
1450 size_t attr_length, l, line_length;
1453 struct archive_string *as;
1454 struct archive_string_conv *sconv;
1457 attr_length = strlen(attr);
1458 tar->pax_hdrcharset_binary = 0;
1459 archive_string_empty(&(tar->entry_gname));
1460 archive_string_empty(&(tar->entry_linkpath));
1461 archive_string_empty(&(tar->entry_pathname));
1462 archive_string_empty(&(tar->entry_pathname_override));
1463 archive_string_empty(&(tar->entry_uname));
1465 while (attr_length > 0) {
1466 /* Parse decimal length field at start of line. */
1469 p = attr; /* Record start of line. */
1476 if (*p < '0' || *p > '9') {
1477 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1478 "Ignoring malformed pax extended attributes");
1479 return (ARCHIVE_WARN);
1482 line_length += *p - '0';
1483 if (line_length > 999999) {
1484 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1485 "Rejecting pax extended attribute > 1MB");
1486 return (ARCHIVE_WARN);
1493 * Parsed length must be no bigger than available data,
1494 * at least 1, and the last character of the line must
1497 if (line_length > attr_length
1499 || attr[line_length - 1] != '\n')
1501 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1502 "Ignoring malformed pax extended attribute");
1503 return (ARCHIVE_WARN);
1506 /* Null-terminate the line. */
1507 attr[line_length - 1] = '\0';
1509 /* Find end of key and null terminate it. */
1513 while (*p && *p != '=')
1516 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1517 "Invalid pax extended attributes");
1518 return (ARCHIVE_WARN);
1522 /* Identify null-terminated 'value' portion. */
1525 /* Identify this attribute and set it in the entry. */
1526 err2 = pax_attribute(a, tar, entry, key, value);
1527 if (err2 == ARCHIVE_FATAL)
1529 err = err_combine(err, err2);
1531 /* Skip to next line */
1532 attr += line_length;
1533 attr_length -= line_length;
1537 * PAX format uses UTF-8 as default charset for its metadata
1538 * unless hdrcharset=BINARY is present in its header.
1539 * We apply the charset specified by the hdrcharset option only
1540 * when the hdrcharset attribute(in PAX header) is BINARY because
1541 * we respect the charset described in PAX header and BINARY also
1542 * means that metadata(filename,uname and gname) character-set
1545 if (tar->pax_hdrcharset_binary)
1546 sconv = tar->opt_sconv;
1548 sconv = archive_string_conversion_from_charset(
1549 &(a->archive), "UTF-8", 1);
1551 return (ARCHIVE_FATAL);
1553 archive_string_conversion_set_opt(sconv,
1554 SCONV_SET_OPT_UTF8_LIBARCHIVE2X);
1557 if (archive_strlen(&(tar->entry_gname)) > 0) {
1558 if (archive_entry_copy_gname_l(entry, tar->entry_gname.s,
1559 archive_strlen(&(tar->entry_gname)), sconv) != 0) {
1560 err = set_conversion_failed_error(a, sconv, "Gname");
1561 if (err == ARCHIVE_FATAL)
1563 /* Use a converted an original name. */
1564 archive_entry_copy_gname(entry, tar->entry_gname.s);
1567 if (archive_strlen(&(tar->entry_linkpath)) > 0) {
1568 if (archive_entry_copy_link_l(entry, tar->entry_linkpath.s,
1569 archive_strlen(&(tar->entry_linkpath)), sconv) != 0) {
1570 err = set_conversion_failed_error(a, sconv, "Linkname");
1571 if (err == ARCHIVE_FATAL)
1573 /* Use a converted an original name. */
1574 archive_entry_copy_link(entry, tar->entry_linkpath.s);
1578 * Some extensions (such as the GNU sparse file extensions)
1579 * deliberately store a synthetic name under the regular 'path'
1580 * attribute and the real file name under a different attribute.
1581 * Since we're supposed to not care about the order, we
1582 * have no choice but to store all of the various filenames
1583 * we find and figure it all out afterwards. This is the
1584 * figuring out part.
1587 if (archive_strlen(&(tar->entry_pathname_override)) > 0)
1588 as = &(tar->entry_pathname_override);
1589 else if (archive_strlen(&(tar->entry_pathname)) > 0)
1590 as = &(tar->entry_pathname);
1592 if (archive_entry_copy_pathname_l(entry, as->s,
1593 archive_strlen(as), sconv) != 0) {
1594 err = set_conversion_failed_error(a, sconv, "Pathname");
1595 if (err == ARCHIVE_FATAL)
1597 /* Use a converted an original name. */
1598 archive_entry_copy_pathname(entry, as->s);
1601 if (archive_strlen(&(tar->entry_uname)) > 0) {
1602 if (archive_entry_copy_uname_l(entry, tar->entry_uname.s,
1603 archive_strlen(&(tar->entry_uname)), sconv) != 0) {
1604 err = set_conversion_failed_error(a, sconv, "Uname");
1605 if (err == ARCHIVE_FATAL)
1607 /* Use a converted an original name. */
1608 archive_entry_copy_uname(entry, tar->entry_uname.s);
1615 pax_attribute_xattr(struct archive_entry *entry,
1616 char *name, char *value)
1619 void *value_decoded;
1622 if (strlen(name) < 18 || (memcmp(name, "LIBARCHIVE.xattr.", 17)) != 0)
1627 /* URL-decode name */
1628 name_decoded = url_decode(name);
1629 if (name_decoded == NULL)
1632 /* Base-64 decode value */
1633 value_decoded = base64_decode(value, strlen(value), &value_len);
1634 if (value_decoded == NULL) {
1639 archive_entry_xattr_add_entry(entry, name_decoded,
1640 value_decoded, value_len);
1643 free(value_decoded);
1648 * Parse a single key=value attribute. key/value pointers are
1649 * assumed to point into reasonably long-lived storage.
1651 * Note that POSIX reserves all-lowercase keywords. Vendor-specific
1652 * extensions should always have keywords of the form "VENDOR.attribute"
1653 * In particular, it's quite feasible to support many different
1654 * vendor extensions here. I'm using "LIBARCHIVE" for extensions
1655 * unique to this library.
1657 * Investigate other vendor-specific extensions and see if
1658 * any of them look useful.
1661 pax_attribute(struct archive_read *a, struct tar *tar,
1662 struct archive_entry *entry, char *key, char *value)
1666 int err = ARCHIVE_OK, r;
1669 value = ""; /* Disable compiler warning; do not pass
1670 * NULL pointer to strlen(). */
1673 /* GNU "0.0" sparse pax format. */
1674 if (strcmp(key, "GNU.sparse.numblocks") == 0) {
1675 tar->sparse_offset = -1;
1676 tar->sparse_numbytes = -1;
1677 tar->sparse_gnu_major = 0;
1678 tar->sparse_gnu_minor = 0;
1680 if (strcmp(key, "GNU.sparse.offset") == 0) {
1681 tar->sparse_offset = tar_atol10(value, strlen(value));
1682 if (tar->sparse_numbytes != -1) {
1683 if (gnu_add_sparse_entry(a, tar,
1684 tar->sparse_offset, tar->sparse_numbytes)
1686 return (ARCHIVE_FATAL);
1687 tar->sparse_offset = -1;
1688 tar->sparse_numbytes = -1;
1691 if (strcmp(key, "GNU.sparse.numbytes") == 0) {
1692 tar->sparse_numbytes = tar_atol10(value, strlen(value));
1693 if (tar->sparse_numbytes != -1) {
1694 if (gnu_add_sparse_entry(a, tar,
1695 tar->sparse_offset, tar->sparse_numbytes)
1697 return (ARCHIVE_FATAL);
1698 tar->sparse_offset = -1;
1699 tar->sparse_numbytes = -1;
1702 if (strcmp(key, "GNU.sparse.size") == 0) {
1703 tar->realsize = tar_atol10(value, strlen(value));
1704 archive_entry_set_size(entry, tar->realsize);
1707 /* GNU "0.1" sparse pax format. */
1708 if (strcmp(key, "GNU.sparse.map") == 0) {
1709 tar->sparse_gnu_major = 0;
1710 tar->sparse_gnu_minor = 1;
1711 if (gnu_sparse_01_parse(a, tar, value) != ARCHIVE_OK)
1712 return (ARCHIVE_WARN);
1715 /* GNU "1.0" sparse pax format */
1716 if (strcmp(key, "GNU.sparse.major") == 0) {
1717 tar->sparse_gnu_major = (int)tar_atol10(value, strlen(value));
1718 tar->sparse_gnu_pending = 1;
1720 if (strcmp(key, "GNU.sparse.minor") == 0) {
1721 tar->sparse_gnu_minor = (int)tar_atol10(value, strlen(value));
1722 tar->sparse_gnu_pending = 1;
1724 if (strcmp(key, "GNU.sparse.name") == 0) {
1726 * The real filename; when storing sparse
1727 * files, GNU tar puts a synthesized name into
1728 * the regular 'path' attribute in an attempt
1729 * to limit confusion. ;-)
1731 archive_strcpy(&(tar->entry_pathname_override), value);
1733 if (strcmp(key, "GNU.sparse.realsize") == 0) {
1734 tar->realsize = tar_atol10(value, strlen(value));
1735 archive_entry_set_size(entry, tar->realsize);
1739 /* Our extensions */
1740 /* TODO: Handle arbitrary extended attributes... */
1742 if (strcmp(key, "LIBARCHIVE.xxxxxxx") == 0)
1743 archive_entry_set_xxxxxx(entry, value);
1745 if (strcmp(key, "LIBARCHIVE.creationtime") == 0) {
1746 pax_time(value, &s, &n);
1747 archive_entry_set_birthtime(entry, s, n);
1749 if (memcmp(key, "LIBARCHIVE.xattr.", 17) == 0)
1750 pax_attribute_xattr(entry, key, value);
1753 /* We support some keys used by the "star" archiver */
1754 if (strcmp(key, "SCHILY.acl.access") == 0) {
1755 if (tar->sconv_acl == NULL) {
1757 archive_string_conversion_from_charset(
1758 &(a->archive), "UTF-8", 1);
1759 if (tar->sconv_acl == NULL)
1760 return (ARCHIVE_FATAL);
1763 r = archive_acl_parse_l(archive_entry_acl(entry),
1764 value, ARCHIVE_ENTRY_ACL_TYPE_ACCESS,
1766 if (r != ARCHIVE_OK) {
1768 if (err == ARCHIVE_FATAL) {
1769 archive_set_error(&a->archive, ENOMEM,
1770 "Can't allocate memory for "
1771 "SCHILY.acl.access");
1774 archive_set_error(&a->archive,
1776 "Parse error: SCHILY.acl.access");
1778 } else if (strcmp(key, "SCHILY.acl.default") == 0) {
1779 if (tar->sconv_acl == NULL) {
1781 archive_string_conversion_from_charset(
1782 &(a->archive), "UTF-8", 1);
1783 if (tar->sconv_acl == NULL)
1784 return (ARCHIVE_FATAL);
1787 r = archive_acl_parse_l(archive_entry_acl(entry),
1788 value, ARCHIVE_ENTRY_ACL_TYPE_DEFAULT,
1790 if (r != ARCHIVE_OK) {
1792 if (err == ARCHIVE_FATAL) {
1793 archive_set_error(&a->archive, ENOMEM,
1794 "Can't allocate memory for "
1795 "SCHILY.acl.default");
1798 archive_set_error(&a->archive,
1800 "Parse error: SCHILY.acl.default");
1802 } else if (strcmp(key, "SCHILY.devmajor") == 0) {
1803 archive_entry_set_rdevmajor(entry,
1804 (dev_t)tar_atol10(value, strlen(value)));
1805 } else if (strcmp(key, "SCHILY.devminor") == 0) {
1806 archive_entry_set_rdevminor(entry,
1807 (dev_t)tar_atol10(value, strlen(value)));
1808 } else if (strcmp(key, "SCHILY.fflags") == 0) {
1809 archive_entry_copy_fflags_text(entry, value);
1810 } else if (strcmp(key, "SCHILY.dev") == 0) {
1811 archive_entry_set_dev(entry,
1812 (dev_t)tar_atol10(value, strlen(value)));
1813 } else if (strcmp(key, "SCHILY.ino") == 0) {
1814 archive_entry_set_ino(entry,
1815 tar_atol10(value, strlen(value)));
1816 } else if (strcmp(key, "SCHILY.nlink") == 0) {
1817 archive_entry_set_nlink(entry, (unsigned)
1818 tar_atol10(value, strlen(value)));
1819 } else if (strcmp(key, "SCHILY.realsize") == 0) {
1820 tar->realsize = tar_atol10(value, strlen(value));
1821 archive_entry_set_size(entry, tar->realsize);
1822 } else if (strcmp(key, "SUN.holesdata") == 0) {
1823 /* A Solaris extension for sparse. */
1824 r = solaris_sparse_parse(a, tar, entry, value);
1826 if (r == ARCHIVE_FATAL)
1829 archive_set_error(&a->archive,
1831 "Parse error: SUN.holesdata");
1836 if (strcmp(key, "atime") == 0) {
1837 pax_time(value, &s, &n);
1838 archive_entry_set_atime(entry, s, n);
1842 if (strcmp(key, "ctime") == 0) {
1843 pax_time(value, &s, &n);
1844 archive_entry_set_ctime(entry, s, n);
1845 } else if (strcmp(key, "charset") == 0) {
1846 /* TODO: Publish charset information in entry. */
1847 } else if (strcmp(key, "comment") == 0) {
1848 /* TODO: Publish comment in entry. */
1852 if (strcmp(key, "gid") == 0) {
1853 archive_entry_set_gid(entry,
1854 tar_atol10(value, strlen(value)));
1855 } else if (strcmp(key, "gname") == 0) {
1856 archive_strcpy(&(tar->entry_gname), value);
1860 if (strcmp(key, "hdrcharset") == 0) {
1861 if (strcmp(value, "BINARY") == 0)
1863 tar->pax_hdrcharset_binary = 1;
1864 else if (strcmp(value, "ISO-IR 10646 2000 UTF-8") == 0)
1865 tar->pax_hdrcharset_binary = 0;
1869 /* pax interchange doesn't distinguish hardlink vs. symlink. */
1870 if (strcmp(key, "linkpath") == 0) {
1871 archive_strcpy(&(tar->entry_linkpath), value);
1875 if (strcmp(key, "mtime") == 0) {
1876 pax_time(value, &s, &n);
1877 archive_entry_set_mtime(entry, s, n);
1881 if (strcmp(key, "path") == 0) {
1882 archive_strcpy(&(tar->entry_pathname), value);
1886 /* POSIX has reserved 'realtime.*' */
1889 /* POSIX has reserved 'security.*' */
1890 /* Someday: if (strcmp(key, "security.acl") == 0) { ... } */
1891 if (strcmp(key, "size") == 0) {
1892 /* "size" is the size of the data in the entry. */
1893 tar->entry_bytes_remaining
1894 = tar_atol10(value, strlen(value));
1896 * But, "size" is not necessarily the size of
1897 * the file on disk; if this is a sparse file,
1898 * the disk size may have already been set from
1899 * GNU.sparse.realsize or GNU.sparse.size or
1900 * an old GNU header field or SCHILY.realsize
1903 if (tar->realsize < 0) {
1904 archive_entry_set_size(entry,
1905 tar->entry_bytes_remaining);
1907 = tar->entry_bytes_remaining;
1912 if (strcmp(key, "uid") == 0) {
1913 archive_entry_set_uid(entry,
1914 tar_atol10(value, strlen(value)));
1915 } else if (strcmp(key, "uname") == 0) {
1916 archive_strcpy(&(tar->entry_uname), value);
1926 * parse a decimal time value, which may include a fractional portion
1929 pax_time(const char *p, int64_t *ps, long *pn)
1935 int64_t limit, last_digit_limit;
1937 limit = INT64_MAX / 10;
1938 last_digit_limit = INT64_MAX % 10;
1946 while (*p >= '0' && *p <= '9') {
1949 (s == limit && digit > last_digit_limit)) {
1953 s = (s * 10) + digit;
1959 /* Calculate nanoseconds. */
1968 if (*p >= '0' && *p <= '9')
1969 *pn += (*p - '0') * l;
1976 * Parse GNU tar header
1979 header_gnutar(struct archive_read *a, struct tar *tar,
1980 struct archive_entry *entry, const void *h, size_t *unconsumed)
1982 const struct archive_entry_header_gnutar *header;
1984 int err = ARCHIVE_OK;
1987 * GNU header is like POSIX ustar, except 'prefix' is
1988 * replaced with some other fields. This also means the
1989 * filename is stored as in old-style archives.
1992 /* Grab fields common to all tar variants. */
1993 err = header_common(a, tar, entry, h);
1994 if (err == ARCHIVE_FATAL)
1997 /* Copy filename over (to ensure null termination). */
1998 header = (const struct archive_entry_header_gnutar *)h;
1999 if (archive_entry_copy_pathname_l(entry,
2000 header->name, sizeof(header->name), tar->sconv) != 0) {
2001 err = set_conversion_failed_error(a, tar->sconv, "Pathname");
2002 if (err == ARCHIVE_FATAL)
2006 /* Fields common to ustar and GNU */
2007 /* XXX Can the following be factored out since it's common
2008 * to ustar and gnu tar? Is it okay to move it down into
2009 * header_common, perhaps? */
2010 if (archive_entry_copy_uname_l(entry,
2011 header->uname, sizeof(header->uname), tar->sconv) != 0) {
2012 err = set_conversion_failed_error(a, tar->sconv, "Uname");
2013 if (err == ARCHIVE_FATAL)
2017 if (archive_entry_copy_gname_l(entry,
2018 header->gname, sizeof(header->gname), tar->sconv) != 0) {
2019 err = set_conversion_failed_error(a, tar->sconv, "Gname");
2020 if (err == ARCHIVE_FATAL)
2024 /* Parse out device numbers only for char and block specials */
2025 if (header->typeflag[0] == '3' || header->typeflag[0] == '4') {
2026 archive_entry_set_rdevmajor(entry, (dev_t)
2027 tar_atol(header->rdevmajor, sizeof(header->rdevmajor)));
2028 archive_entry_set_rdevminor(entry, (dev_t)
2029 tar_atol(header->rdevminor, sizeof(header->rdevminor)));
2031 archive_entry_set_rdev(entry, 0);
2033 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
2035 /* Grab GNU-specific fields. */
2036 t = tar_atol(header->atime, sizeof(header->atime));
2038 archive_entry_set_atime(entry, t, 0);
2039 t = tar_atol(header->ctime, sizeof(header->ctime));
2041 archive_entry_set_ctime(entry, t, 0);
2043 if (header->realsize[0] != 0) {
2045 = tar_atol(header->realsize, sizeof(header->realsize));
2046 archive_entry_set_size(entry, tar->realsize);
2049 if (header->sparse[0].offset[0] != 0) {
2050 if (gnu_sparse_old_read(a, tar, header, unconsumed)
2052 return (ARCHIVE_FATAL);
2054 if (header->isextended[0] != 0) {
2063 gnu_add_sparse_entry(struct archive_read *a, struct tar *tar,
2064 int64_t offset, int64_t remaining)
2066 struct sparse_block *p;
2068 p = (struct sparse_block *)malloc(sizeof(*p));
2070 archive_set_error(&a->archive, ENOMEM, "Out of memory");
2071 return (ARCHIVE_FATAL);
2073 memset(p, 0, sizeof(*p));
2074 if (tar->sparse_last != NULL)
2075 tar->sparse_last->next = p;
2077 tar->sparse_list = p;
2078 tar->sparse_last = p;
2080 p->remaining = remaining;
2081 return (ARCHIVE_OK);
2085 gnu_clear_sparse_list(struct tar *tar)
2087 struct sparse_block *p;
2089 while (tar->sparse_list != NULL) {
2090 p = tar->sparse_list;
2091 tar->sparse_list = p->next;
2094 tar->sparse_last = NULL;
2098 * GNU tar old-format sparse data.
2100 * GNU old-format sparse data is stored in a fixed-field
2101 * format. Offset/size values are 11-byte octal fields (same
2102 * format as 'size' field in ustart header). These are
2103 * stored in the header, allocating subsequent header blocks
2104 * as needed. Extending the header in this way is a pretty
2105 * severe POSIX violation; this design has earned GNU tar a
2110 gnu_sparse_old_read(struct archive_read *a, struct tar *tar,
2111 const struct archive_entry_header_gnutar *header, size_t *unconsumed)
2116 struct gnu_sparse sparse[21];
2120 const struct extended *ext;
2122 if (gnu_sparse_old_parse(a, tar, header->sparse, 4) != ARCHIVE_OK)
2123 return (ARCHIVE_FATAL);
2124 if (header->isextended[0] == 0)
2125 return (ARCHIVE_OK);
2128 tar_flush_unconsumed(a, unconsumed);
2129 data = __archive_read_ahead(a, 512, &bytes_read);
2131 return (ARCHIVE_FATAL);
2132 if (bytes_read < 512) {
2133 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
2134 "Truncated tar archive "
2135 "detected while reading sparse file data");
2136 return (ARCHIVE_FATAL);
2139 ext = (const struct extended *)data;
2140 if (gnu_sparse_old_parse(a, tar, ext->sparse, 21) != ARCHIVE_OK)
2141 return (ARCHIVE_FATAL);
2142 } while (ext->isextended[0] != 0);
2143 if (tar->sparse_list != NULL)
2144 tar->entry_offset = tar->sparse_list->offset;
2145 return (ARCHIVE_OK);
2149 gnu_sparse_old_parse(struct archive_read *a, struct tar *tar,
2150 const struct gnu_sparse *sparse, int length)
2152 while (length > 0 && sparse->offset[0] != 0) {
2153 if (gnu_add_sparse_entry(a, tar,
2154 tar_atol(sparse->offset, sizeof(sparse->offset)),
2155 tar_atol(sparse->numbytes, sizeof(sparse->numbytes)))
2157 return (ARCHIVE_FATAL);
2161 return (ARCHIVE_OK);
2165 * GNU tar sparse format 0.0
2167 * Beginning with GNU tar 1.15, sparse files are stored using
2168 * information in the pax extended header. The GNU tar maintainers
2169 * have gone through a number of variations in the process of working
2170 * out this scheme; fortunately, they're all numbered.
2172 * Sparse format 0.0 uses attribute GNU.sparse.numblocks to store the
2173 * number of blocks, and GNU.sparse.offset/GNU.sparse.numbytes to
2174 * store offset/size for each block. The repeated instances of these
2175 * latter fields violate the pax specification (which frowns on
2176 * duplicate keys), so this format was quickly replaced.
2180 * GNU tar sparse format 0.1
2182 * This version replaced the offset/numbytes attributes with
2183 * a single "map" attribute that stored a list of integers. This
2184 * format had two problems: First, the "map" attribute could be very
2185 * long, which caused problems for some implementations. More
2186 * importantly, the sparse data was lost when extracted by archivers
2187 * that didn't recognize this extension.
2191 gnu_sparse_01_parse(struct archive_read *a, struct tar *tar, const char *p)
2194 int64_t offset = -1, size = -1;
2198 while (*e != '\0' && *e != ',') {
2199 if (*e < '0' || *e > '9')
2200 return (ARCHIVE_WARN);
2204 offset = tar_atol10(p, e - p);
2206 return (ARCHIVE_WARN);
2208 size = tar_atol10(p, e - p);
2210 return (ARCHIVE_WARN);
2211 if (gnu_add_sparse_entry(a, tar, offset, size)
2213 return (ARCHIVE_FATAL);
2217 return (ARCHIVE_OK);
2223 * GNU tar sparse format 1.0
2225 * The idea: The offset/size data is stored as a series of base-10
2226 * ASCII numbers prepended to the file data, so that dearchivers that
2227 * don't support this format will extract the block map along with the
2228 * data and a separate post-process can restore the sparseness.
2230 * Unfortunately, GNU tar 1.16 had a bug that added unnecessary
2231 * padding to the body of the file when using this format. GNU tar
2232 * 1.17 corrected this bug without bumping the version number, so
2233 * it's not possible to support both variants. This code supports
2234 * the later variant at the expense of not supporting the former.
2236 * This variant also replaced GNU.sparse.size with GNU.sparse.realsize
2237 * and introduced the GNU.sparse.major/GNU.sparse.minor attributes.
2241 * Read the next line from the input, and parse it as a decimal
2242 * integer followed by '\n'. Returns positive integer value or
2243 * negative on error.
2246 gnu_sparse_10_atol(struct archive_read *a, struct tar *tar,
2247 int64_t *remaining, size_t *unconsumed)
2249 int64_t l, limit, last_digit_limit;
2255 limit = INT64_MAX / base;
2256 last_digit_limit = INT64_MAX % base;
2259 * Skip any lines starting with '#'; GNU tar specs
2260 * don't require this, but they should.
2263 bytes_read = readline(a, tar, &p,
2264 (ssize_t)tar_min(*remaining, 100), unconsumed);
2265 if (bytes_read <= 0)
2266 return (ARCHIVE_FATAL);
2267 *remaining -= bytes_read;
2268 } while (p[0] == '#');
2271 while (bytes_read > 0) {
2274 if (*p < '0' || *p >= '0' + base)
2275 return (ARCHIVE_WARN);
2277 if (l > limit || (l == limit && digit > last_digit_limit))
2278 l = INT64_MAX; /* Truncate on overflow. */
2280 l = (l * base) + digit;
2284 /* TODO: Error message. */
2285 return (ARCHIVE_WARN);
2289 * Returns length (in bytes) of the sparse data description
2293 gnu_sparse_10_read(struct archive_read *a, struct tar *tar, size_t *unconsumed)
2297 int64_t offset, size, to_skip, remaining;
2299 /* Clear out the existing sparse list. */
2300 gnu_clear_sparse_list(tar);
2302 remaining = tar->entry_bytes_remaining;
2304 /* Parse entries. */
2305 entries = (int)gnu_sparse_10_atol(a, tar, &remaining, unconsumed);
2307 return (ARCHIVE_FATAL);
2308 /* Parse the individual entries. */
2309 while (entries-- > 0) {
2310 /* Parse offset/size */
2311 offset = gnu_sparse_10_atol(a, tar, &remaining, unconsumed);
2313 return (ARCHIVE_FATAL);
2314 size = gnu_sparse_10_atol(a, tar, &remaining, unconsumed);
2316 return (ARCHIVE_FATAL);
2317 /* Add a new sparse entry. */
2318 if (gnu_add_sparse_entry(a, tar, offset, size) != ARCHIVE_OK)
2319 return (ARCHIVE_FATAL);
2321 /* Skip rest of block... */
2322 tar_flush_unconsumed(a, unconsumed);
2323 bytes_read = (ssize_t)(tar->entry_bytes_remaining - remaining);
2324 to_skip = 0x1ff & -bytes_read;
2325 if (to_skip != __archive_read_consume(a, to_skip))
2326 return (ARCHIVE_FATAL);
2327 return ((ssize_t)(bytes_read + to_skip));
2331 * Solaris pax extension for a sparse file. This is recorded with the
2332 * data and hole pairs. The way recording sparse information by Solaris'
2333 * pax simply indicates where data and sparse are, so the stored contents
2334 * consist of both data and hole.
2337 solaris_sparse_parse(struct archive_read *a, struct tar *tar,
2338 struct archive_entry *entry, const char *p)
2344 (void)entry; /* UNUSED */
2350 return (ARCHIVE_WARN);
2353 while (*e != '\0' && *e != ' ') {
2354 if (*e < '0' || *e > '9')
2355 return (ARCHIVE_WARN);
2359 end = tar_atol10(p, e - p);
2361 return (ARCHIVE_WARN);
2363 if (gnu_add_sparse_entry(a, tar, start,
2364 end - start) != ARCHIVE_OK)
2365 return (ARCHIVE_FATAL);
2366 tar->sparse_last->hole = hole;
2369 return (ARCHIVE_OK);
2376 * Convert text->integer.
2378 * Traditional tar formats (including POSIX) specify base-8 for
2379 * all of the standard numeric fields. This is a significant limitation
2381 * = file size is limited to 8GB
2382 * = rdevmajor and rdevminor are limited to 21 bits
2383 * = uid/gid are limited to 21 bits
2385 * There are two workarounds for this:
2386 * = pax extended headers, which use variable-length string fields
2387 * = GNU tar and STAR both allow either base-8 or base-256 in
2388 * most fields. The high bit is set to indicate base-256.
2390 * On read, this implementation supports both extensions.
2393 tar_atol(const char *p, unsigned char_cnt)
2396 * Technically, GNU tar considers a field to be in base-256
2397 * only if the first byte is 0xff or 0x80.
2400 return (tar_atol256(p, char_cnt));
2401 return (tar_atol8(p, char_cnt));
2405 * Note that this implementation does not (and should not!) obey
2406 * locale settings; you cannot simply substitute strtol here, since
2407 * it does obey locale.
2410 tar_atol8(const char *p, unsigned char_cnt)
2412 int64_t l, limit, last_digit_limit;
2413 int digit, sign, base;
2416 limit = INT64_MAX / base;
2417 last_digit_limit = INT64_MAX % base;
2419 while (*p == ' ' || *p == '\t')
2429 while (digit >= 0 && digit < base && char_cnt-- > 0) {
2430 if (l>limit || (l == limit && digit > last_digit_limit)) {
2431 l = INT64_MAX; /* Truncate on overflow. */
2434 l = (l * base) + digit;
2437 return (sign < 0) ? -l : l;
2441 * Note that this implementation does not (and should not!) obey
2442 * locale settings; you cannot simply substitute strtol here, since
2443 * it does obey locale.
2446 tar_atol10(const char *p, unsigned char_cnt)
2448 int64_t l, limit, last_digit_limit;
2449 int base, digit, sign;
2452 limit = INT64_MAX / base;
2453 last_digit_limit = INT64_MAX % base;
2455 while (*p == ' ' || *p == '\t')
2465 while (digit >= 0 && digit < base && char_cnt-- > 0) {
2466 if (l > limit || (l == limit && digit > last_digit_limit)) {
2467 l = INT64_MAX; /* Truncate on overflow. */
2470 l = (l * base) + digit;
2473 return (sign < 0) ? -l : l;
2477 * Parse a base-256 integer. This is just a straight signed binary
2478 * value in big-endian order, except that the high-order bit is
2482 tar_atol256(const char *_p, unsigned char_cnt)
2484 int64_t l, upper_limit, lower_limit;
2485 const unsigned char *p = (const unsigned char *)_p;
2487 upper_limit = INT64_MAX / 256;
2488 lower_limit = INT64_MIN / 256;
2490 /* Pad with 1 or 0 bits, depending on sign. */
2491 if ((0x40 & *p) == 0x40)
2495 l = (l << 6) | (0x3f & *p++);
2496 while (--char_cnt > 0) {
2497 if (l > upper_limit) {
2498 l = INT64_MAX; /* Truncate on overflow */
2500 } else if (l < lower_limit) {
2504 l = (l << 8) | (0xff & (int64_t)*p++);
2510 * Returns length of line (including trailing newline)
2511 * or negative on error. 'start' argument is updated to
2512 * point to first character of line. This avoids copying
2516 readline(struct archive_read *a, struct tar *tar, const char **start,
2517 ssize_t limit, size_t *unconsumed)
2520 ssize_t total_size = 0;
2525 tar_flush_unconsumed(a, unconsumed);
2527 t = __archive_read_ahead(a, 1, &bytes_read);
2528 if (bytes_read <= 0)
2529 return (ARCHIVE_FATAL);
2530 s = t; /* Start of line? */
2531 p = memchr(t, '\n', bytes_read);
2532 /* If we found '\n' in the read buffer, return pointer to that. */
2534 bytes_read = 1 + ((const char *)p) - s;
2535 if (bytes_read > limit) {
2536 archive_set_error(&a->archive,
2537 ARCHIVE_ERRNO_FILE_FORMAT,
2539 return (ARCHIVE_FATAL);
2541 *unconsumed = bytes_read;
2543 return (bytes_read);
2545 *unconsumed = bytes_read;
2546 /* Otherwise, we need to accumulate in a line buffer. */
2548 if (total_size + bytes_read > limit) {
2549 archive_set_error(&a->archive,
2550 ARCHIVE_ERRNO_FILE_FORMAT,
2552 return (ARCHIVE_FATAL);
2554 if (archive_string_ensure(&tar->line, total_size + bytes_read) == NULL) {
2555 archive_set_error(&a->archive, ENOMEM,
2556 "Can't allocate working buffer");
2557 return (ARCHIVE_FATAL);
2559 memcpy(tar->line.s + total_size, t, bytes_read);
2560 tar_flush_unconsumed(a, unconsumed);
2561 total_size += bytes_read;
2562 /* If we found '\n', clean up and return. */
2564 *start = tar->line.s;
2565 return (total_size);
2567 /* Read some more. */
2568 t = __archive_read_ahead(a, 1, &bytes_read);
2569 if (bytes_read <= 0)
2570 return (ARCHIVE_FATAL);
2571 s = t; /* Start of line? */
2572 p = memchr(t, '\n', bytes_read);
2573 /* If we found '\n', trim the read. */
2575 bytes_read = 1 + ((const char *)p) - s;
2577 *unconsumed = bytes_read;
2582 * base64_decode - Base64 decode
2584 * This accepts most variations of base-64 encoding, including:
2585 * * with or without line breaks
2586 * * with or without the final group padded with '=' or '_' characters
2587 * (The most economical Base-64 variant does not pad the last group and
2588 * omits line breaks; RFC1341 used for MIME requires both.)
2591 base64_decode(const char *s, size_t len, size_t *out_len)
2593 static const unsigned char digits[64] = {
2594 'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
2595 'O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b',
2596 'c','d','e','f','g','h','i','j','k','l','m','n','o','p',
2597 'q','r','s','t','u','v','w','x','y','z','0','1','2','3',
2598 '4','5','6','7','8','9','+','/' };
2599 static unsigned char decode_table[128];
2601 const unsigned char *src = (const unsigned char *)s;
2603 /* If the decode table is not yet initialized, prepare it. */
2604 if (decode_table[digits[1]] != 1) {
2606 memset(decode_table, 0xff, sizeof(decode_table));
2607 for (i = 0; i < sizeof(digits); i++)
2608 decode_table[digits[i]] = i;
2611 /* Allocate enough space to hold the entire output. */
2612 /* Note that we may not use all of this... */
2613 out = (char *)malloc(len - len / 4 + 1);
2621 /* Collect the next group of (up to) four characters. */
2624 while (group_size < 4 && len > 0) {
2625 /* '=' or '_' padding indicates final group. */
2626 if (*src == '=' || *src == '_') {
2630 /* Skip illegal characters (including line breaks) */
2631 if (*src > 127 || *src < 32
2632 || decode_table[*src] == 0xff) {
2638 v |= decode_table[*src++];
2642 /* Align a short group properly. */
2643 v <<= 6 * (4 - group_size);
2644 /* Unpack the group we just collected. */
2645 switch (group_size) {
2646 case 4: d[2] = v & 0xff;
2648 case 3: d[1] = (v >> 8) & 0xff;
2650 case 2: d[0] = (v >> 16) & 0xff;
2652 case 1: /* this is invalid! */
2655 d += group_size * 3 / 4;
2663 url_decode(const char *in)
2668 out = (char *)malloc(strlen(in) + 1);
2671 for (s = in, d = out; *s != '\0'; ) {
2672 if (s[0] == '%' && s[1] != '\0' && s[2] != '\0') {
2673 /* Try to convert % escape */
2674 int digit1 = tohex(s[1]);
2675 int digit2 = tohex(s[2]);
2676 if (digit1 >= 0 && digit2 >= 0) {
2677 /* Looks good, consume three chars */
2679 /* Convert output */
2680 *d++ = ((digit1 << 4) | digit2);
2683 /* Else fall through and treat '%' as normal char */
2694 if (c >= '0' && c <= '9')
2696 else if (c >= 'A' && c <= 'F')
2697 return (c - 'A' + 10);
2698 else if (c >= 'a' && c <= 'f')
2699 return (c - 'a' + 10);