2 * Copyright (c) 2003-2007 Tim Kientzle
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include "archive_platform.h"
27 __FBSDID("$FreeBSD: src/lib/libarchive/archive_read_support_format_tar.c,v 1.54 2007/04/15 00:53:38 kientzle Exp $");
33 /* #include <stdint.h> */ /* See archive_platform.h */
41 /* Obtain suitable wide-character manipulation functions. */
45 /* Good enough for equality testing, which is all we need. */
46 static int wcscmp(const wchar_t *s1
, const wchar_t *s2
)
49 while (*s1
&& diff
== 0)
50 diff
= (int)*++s1
- (int)*++s2
;
53 /* Good enough for equality testing, which is all we need. */
54 static int wcsncmp(const wchar_t *s1
, const wchar_t *s2
, size_t n
)
57 while (*s1
&& diff
== 0 && n
-- > 0)
58 diff
= (int)*++s1
- (int)*++s2
;
61 static size_t wcslen(const wchar_t *s
)
71 #include "archive_entry.h"
72 #include "archive_private.h"
73 #include "archive_read_private.h"
76 * Layout of POSIX 'ustar' tar header.
78 struct archive_entry_header_ustar
{
87 char linkname
[100]; /* "old format" header ends here */
88 char magic
[6]; /* For POSIX: "ustar\0" */
89 char version
[2]; /* For POSIX: "00" */
98 * Structure of GNU tar header
105 struct archive_entry_header_gnutar
{
115 char magic
[8]; /* "ustar \0" (note blank/blank/null at end) */
125 struct gnu_sparse sparse
[4];
129 * GNU doesn't use POSIX 'prefix' field; they use the 'L' (longname)
135 * Data specific to this format.
137 struct sparse_block
{
138 struct sparse_block
*next
;
144 struct archive_string acl_text
;
145 struct archive_string entry_name
;
146 struct archive_string entry_linkname
;
147 struct archive_string entry_uname
;
148 struct archive_string entry_gname
;
149 struct archive_string longlink
;
150 struct archive_string longname
;
151 struct archive_string pax_header
;
152 struct archive_string pax_global
;
154 size_t pax_entry_length
;
155 int header_recursion_depth
;
156 off_t entry_bytes_remaining
;
159 struct sparse_block
*sparse_list
;
162 static size_t UTF8_mbrtowc(wchar_t *pwc
, const char *s
, size_t n
);
163 static int archive_block_is_null(const unsigned char *p
);
164 static char *base64_decode(const wchar_t *, size_t, size_t *);
165 static int gnu_read_sparse_data(struct archive_read
*, struct tar
*,
166 const struct archive_entry_header_gnutar
*header
);
167 static void gnu_parse_sparse_data(struct archive_read
*, struct tar
*,
168 const struct gnu_sparse
*sparse
, int length
);
169 static int header_Solaris_ACL(struct archive_read
*, struct tar
*,
170 struct archive_entry
*, const void *);
171 static int header_common(struct archive_read
*, struct tar
*,
172 struct archive_entry
*, const void *);
173 static int header_old_tar(struct archive_read
*, struct tar
*,
174 struct archive_entry
*, const void *);
175 static int header_pax_extensions(struct archive_read
*, struct tar
*,
176 struct archive_entry
*, const void *);
177 static int header_pax_global(struct archive_read
*, struct tar
*,
178 struct archive_entry
*, const void *h
);
179 static int header_longlink(struct archive_read
*, struct tar
*,
180 struct archive_entry
*, const void *h
);
181 static int header_longname(struct archive_read
*, struct tar
*,
182 struct archive_entry
*, const void *h
);
183 static int header_volume(struct archive_read
*, struct tar
*,
184 struct archive_entry
*, const void *h
);
185 static int header_ustar(struct archive_read
*, struct tar
*,
186 struct archive_entry
*, const void *h
);
187 static int header_gnutar(struct archive_read
*, struct tar
*,
188 struct archive_entry
*, const void *h
);
189 static int archive_read_format_tar_bid(struct archive_read
*);
190 static int archive_read_format_tar_cleanup(struct archive_read
*);
191 static int archive_read_format_tar_read_data(struct archive_read
*a
,
192 const void **buff
, size_t *size
, off_t
*offset
);
193 static int archive_read_format_tar_skip(struct archive_read
*a
);
194 static int archive_read_format_tar_read_header(struct archive_read
*,
195 struct archive_entry
*);
196 static int checksum(struct archive_read
*, const void *);
197 static int pax_attribute(struct archive_entry
*,
198 wchar_t *key
, wchar_t *value
);
199 static int pax_header(struct archive_read
*, struct tar
*,
200 struct archive_entry
*, char *attr
);
201 static void pax_time(const wchar_t *, int64_t *sec
, long *nanos
);
202 static int read_body_to_string(struct archive_read
*, struct tar
*,
203 struct archive_string
*, const void *h
);
204 static int64_t tar_atol(const char *, unsigned);
205 static int64_t tar_atol10(const wchar_t *, unsigned);
206 static int64_t tar_atol256(const char *, unsigned);
207 static int64_t tar_atol8(const char *, unsigned);
208 static int tar_read_header(struct archive_read
*, struct tar
*,
209 struct archive_entry
*);
210 static int tohex(int c
);
211 static char *url_decode(const char *);
212 static int utf8_decode(wchar_t *, const char *, size_t length
);
213 static char *wide_to_narrow(const wchar_t *wval
);
216 archive_read_support_format_gnutar(struct archive
*a
)
218 return (archive_read_support_format_tar(a
));
223 archive_read_support_format_tar(struct archive
*_a
)
225 struct archive_read
*a
= (struct archive_read
*)_a
;
229 tar
= (struct tar
*)malloc(sizeof(*tar
));
231 archive_set_error(&a
->archive
, ENOMEM
,
232 "Can't allocate tar data");
233 return (ARCHIVE_FATAL
);
235 memset(tar
, 0, sizeof(*tar
));
237 r
= __archive_read_register_format(a
, tar
,
238 archive_read_format_tar_bid
,
239 archive_read_format_tar_read_header
,
240 archive_read_format_tar_read_data
,
241 archive_read_format_tar_skip
,
242 archive_read_format_tar_cleanup
);
250 archive_read_format_tar_cleanup(struct archive_read
*a
)
254 tar
= (struct tar
*)(a
->format
->data
);
255 archive_string_free(&tar
->acl_text
);
256 archive_string_free(&tar
->entry_name
);
257 archive_string_free(&tar
->entry_linkname
);
258 archive_string_free(&tar
->entry_uname
);
259 archive_string_free(&tar
->entry_gname
);
260 archive_string_free(&tar
->pax_global
);
261 archive_string_free(&tar
->pax_header
);
262 if (tar
->pax_entry
!= NULL
)
263 free(tar
->pax_entry
);
265 (a
->format
->data
) = NULL
;
271 archive_read_format_tar_bid(struct archive_read
*a
)
276 const struct archive_entry_header_ustar
*header
;
279 * If we're already reading a non-tar file, don't
282 if (a
->archive
.archive_format
!= 0 &&
283 (a
->archive
.archive_format
& ARCHIVE_FORMAT_BASE_MASK
) !=
289 * If we're already reading a tar format, start the bid at 1 as
292 if ((a
->archive
.archive_format
& ARCHIVE_FORMAT_BASE_MASK
) ==
296 /* Now let's look at the actual header and see if it matches. */
297 if (a
->decompressor
->read_ahead
!= NULL
)
298 bytes_read
= (a
->decompressor
->read_ahead
)(a
, &h
, 512);
300 bytes_read
= 0; /* Empty file. */
302 return (ARCHIVE_FATAL
);
303 if (bytes_read
== 0 && bid
> 0) {
304 /* An archive without a proper end-of-archive marker. */
305 /* Hold our nose and bid 1 anyway. */
308 if (bytes_read
< 512) {
309 /* If it's a new archive, then just return a zero bid. */
313 * If we already know this is a tar archive,
314 * then we have a problem.
316 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
317 "Truncated tar archive");
318 return (ARCHIVE_FATAL
);
321 /* If it's an end-of-archive mark, we can handle it. */
322 if ((*(const char *)h
) == 0 && archive_block_is_null((const unsigned char *)h
)) {
323 /* If it's a known tar file, end-of-archive is definite. */
324 if ((a
->archive
.archive_format
& ARCHIVE_FORMAT_BASE_MASK
) ==
331 /* If it's not an end-of-archive mark, it must have a valid checksum.*/
334 bid
+= 48; /* Checksum is usually 6 octal digits. */
336 header
= (const struct archive_entry_header_ustar
*)h
;
338 /* Recognize POSIX formats. */
339 if ((memcmp(header
->magic
, "ustar\0", 6) == 0)
340 &&(memcmp(header
->version
, "00", 2)==0))
343 /* Recognize GNU tar format. */
344 if ((memcmp(header
->magic
, "ustar ", 6) == 0)
345 &&(memcmp(header
->version
, " \0", 2)==0))
348 /* Type flag must be null, digit or A-Z, a-z. */
349 if (header
->typeflag
[0] != 0 &&
350 !( header
->typeflag
[0] >= '0' && header
->typeflag
[0] <= '9') &&
351 !( header
->typeflag
[0] >= 'A' && header
->typeflag
[0] <= 'Z') &&
352 !( header
->typeflag
[0] >= 'a' && header
->typeflag
[0] <= 'z') )
354 bid
+= 2; /* 6 bits of variation in an 8-bit field leaves 2 bits. */
356 /* Sanity check: Look at first byte of mode field. */
357 switch (255 & (unsigned)header
->mode
[0]) {
359 /* Base-256 value: No further verification possible! */
361 case ' ': /* Not recommended, but not illegal, either. */
363 case '0': case '1': case '2': case '3':
364 case '4': case '5': case '6': case '7':
366 /* TODO: Check format of remainder of this field. */
369 /* Not a valid mode; bail out here. */
372 /* TODO: Sanity test uid/gid/size/mtime/rdevmajor/rdevminor fields. */
378 * The function invoked by archive_read_header(). This
379 * just sets up a few things and then calls the internal
380 * tar_read_header() function below.
383 archive_read_format_tar_read_header(struct archive_read
*a
,
384 struct archive_entry
*entry
)
387 * When converting tar archives to cpio archives, it is
388 * essential that each distinct file have a distinct inode
389 * number. To simplify this, we keep a static count here to
390 * assign fake dev/inode numbers to each tar entry. Note that
391 * pax format archives may overwrite this with something more
394 * Ideally, we would track every file read from the archive so
395 * that we could assign the same dev/ino pair to hardlinks,
396 * but the memory required to store a complete lookup table is
397 * probably not worthwhile just to support the relatively
398 * obscure tar->cpio conversion case.
400 static int default_inode
;
401 static int default_dev
;
407 /* Assign default device/inode values. */
408 archive_entry_set_dev(entry
, 1 + default_dev
); /* Don't use zero. */
409 archive_entry_set_ino(entry
, ++default_inode
); /* Don't use zero. */
410 /* Limit generated st_ino number to 16 bits. */
411 if (default_inode
>= 0xffff) {
416 tar
= (struct tar
*)(a
->format
->data
);
417 tar
->entry_offset
= 0;
419 r
= tar_read_header(a
, tar
, entry
);
421 if (r
== ARCHIVE_OK
) {
423 * "Regular" entry with trailing '/' is really
424 * directory: This is needed for certain old tar
425 * variants and even for some broken newer ones.
427 p
= archive_entry_pathname(entry
);
429 if (archive_entry_filetype(entry
) == AE_IFREG
431 archive_entry_set_filetype(entry
, AE_IFDIR
);
437 archive_read_format_tar_read_data(struct archive_read
*a
,
438 const void **buff
, size_t *size
, off_t
*offset
)
442 struct sparse_block
*p
;
444 tar
= (struct tar
*)(a
->format
->data
);
445 if (tar
->sparse_list
!= NULL
) {
446 /* Remove exhausted entries from sparse list. */
447 while (tar
->sparse_list
!= NULL
&&
448 tar
->sparse_list
->remaining
== 0) {
449 p
= tar
->sparse_list
;
450 tar
->sparse_list
= p
->next
;
453 if (tar
->sparse_list
== NULL
) {
454 /* We exhausted the entire sparse list. */
455 tar
->entry_bytes_remaining
= 0;
459 if (tar
->entry_bytes_remaining
> 0) {
460 bytes_read
= (a
->decompressor
->read_ahead
)(a
, buff
, 1);
461 if (bytes_read
== 0) {
462 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
463 "Truncated tar archive");
464 return (ARCHIVE_FATAL
);
467 return (ARCHIVE_FATAL
);
468 if (bytes_read
> tar
->entry_bytes_remaining
)
469 bytes_read
= tar
->entry_bytes_remaining
;
470 if (tar
->sparse_list
!= NULL
) {
471 /* Don't read more than is available in the
472 * current sparse block. */
473 if (tar
->sparse_list
->remaining
< bytes_read
)
474 bytes_read
= tar
->sparse_list
->remaining
;
475 tar
->entry_offset
= tar
->sparse_list
->offset
;
476 tar
->sparse_list
->remaining
-= bytes_read
;
477 tar
->sparse_list
->offset
+= bytes_read
;
480 *offset
= tar
->entry_offset
;
481 tar
->entry_offset
+= bytes_read
;
482 tar
->entry_bytes_remaining
-= bytes_read
;
483 (a
->decompressor
->consume
)(a
, bytes_read
);
486 if ((a
->decompressor
->skip
)(a
, tar
->entry_padding
) < 0)
487 return (ARCHIVE_FATAL
);
488 tar
->entry_padding
= 0;
491 *offset
= tar
->entry_offset
;
492 return (ARCHIVE_EOF
);
497 archive_read_format_tar_skip(struct archive_read
*a
)
501 struct sparse_block
*p
;
503 tar
= (struct tar
*)(a
->format
->data
);
506 * Compression layer skip functions are required to either skip the
507 * length requested or fail, so we can rely upon the entire entry
508 * plus padding being skipped.
510 bytes_skipped
= (a
->decompressor
->skip
)(a
, tar
->entry_bytes_remaining
+
512 if (bytes_skipped
< 0)
513 return (ARCHIVE_FATAL
);
515 tar
->entry_bytes_remaining
= 0;
516 tar
->entry_padding
= 0;
518 /* Free the sparse list. */
519 while (tar
->sparse_list
!= NULL
) {
520 p
= tar
->sparse_list
;
521 tar
->sparse_list
= p
->next
;
529 * This function recursively interprets all of the headers associated
530 * with a single entry.
533 tar_read_header(struct archive_read
*a
, struct tar
*tar
,
534 struct archive_entry
*entry
)
539 const struct archive_entry_header_ustar
*header
;
541 /* Read 512-byte header record */
542 bytes
= (a
->decompressor
->read_ahead
)(a
, &h
, 512);
545 * If we're here, it's becase the _bid function accepted
546 * this file. So just call a short read end-of-archive
547 * and be done with it.
549 return (ARCHIVE_EOF
);
551 (a
->decompressor
->consume
)(a
, 512);
553 /* Check for end-of-archive mark. */
554 if (((*(const char *)h
)==0) && archive_block_is_null((const unsigned char *)h
)) {
555 /* Try to consume a second all-null record, as well. */
556 bytes
= (a
->decompressor
->read_ahead
)(a
, &h
, 512);
558 (a
->decompressor
->consume
)(a
, bytes
);
559 archive_set_error(&a
->archive
, 0, NULL
);
560 return (ARCHIVE_EOF
);
564 * Note: If the checksum fails and we return ARCHIVE_RETRY,
565 * then the client is likely to just retry. This is a very
566 * crude way to search for the next valid header!
568 * TODO: Improve this by implementing a real header scan.
570 if (!checksum(a
, h
)) {
571 archive_set_error(&a
->archive
, EINVAL
, "Damaged tar archive");
572 return (ARCHIVE_RETRY
); /* Retryable: Invalid header */
575 if (++tar
->header_recursion_depth
> 32) {
576 archive_set_error(&a
->archive
, EINVAL
, "Too many special headers");
577 return (ARCHIVE_WARN
);
580 /* Determine the format variant. */
581 header
= (const struct archive_entry_header_ustar
*)h
;
582 switch(header
->typeflag
[0]) {
583 case 'A': /* Solaris tar ACL */
584 a
->archive
.archive_format
= ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE
;
585 a
->archive
.archive_format_name
= "Solaris tar";
586 err
= header_Solaris_ACL(a
, tar
, entry
, h
);
588 case 'g': /* POSIX-standard 'g' header. */
589 a
->archive
.archive_format
= ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE
;
590 a
->archive
.archive_format_name
= "POSIX pax interchange format";
591 err
= header_pax_global(a
, tar
, entry
, h
);
593 case 'K': /* Long link name (GNU tar, others) */
594 err
= header_longlink(a
, tar
, entry
, h
);
596 case 'L': /* Long filename (GNU tar, others) */
597 err
= header_longname(a
, tar
, entry
, h
);
599 case 'V': /* GNU volume header */
600 err
= header_volume(a
, tar
, entry
, h
);
602 case 'X': /* Used by SUN tar; same as 'x'. */
603 a
->archive
.archive_format
= ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE
;
604 a
->archive
.archive_format_name
=
605 "POSIX pax interchange format (Sun variant)";
606 err
= header_pax_extensions(a
, tar
, entry
, h
);
608 case 'x': /* POSIX-standard 'x' header. */
609 a
->archive
.archive_format
= ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE
;
610 a
->archive
.archive_format_name
= "POSIX pax interchange format";
611 err
= header_pax_extensions(a
, tar
, entry
, h
);
614 if (memcmp(header
->magic
, "ustar \0", 8) == 0) {
615 a
->archive
.archive_format
= ARCHIVE_FORMAT_TAR_GNUTAR
;
616 a
->archive
.archive_format_name
= "GNU tar format";
617 err
= header_gnutar(a
, tar
, entry
, h
);
618 } else if (memcmp(header
->magic
, "ustar", 5) == 0) {
619 if (a
->archive
.archive_format
!= ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE
) {
620 a
->archive
.archive_format
= ARCHIVE_FORMAT_TAR_USTAR
;
621 a
->archive
.archive_format_name
= "POSIX ustar format";
623 err
= header_ustar(a
, tar
, entry
, h
);
625 a
->archive
.archive_format
= ARCHIVE_FORMAT_TAR
;
626 a
->archive
.archive_format_name
= "tar (non-POSIX)";
627 err
= header_old_tar(a
, tar
, entry
, h
);
630 --tar
->header_recursion_depth
;
635 * Return true if block checksum is correct.
638 checksum(struct archive_read
*a
, const void *h
)
640 const unsigned char *bytes
;
641 const struct archive_entry_header_ustar
*header
;
644 (void)a
; /* UNUSED */
645 bytes
= (const unsigned char *)h
;
646 header
= (const struct archive_entry_header_ustar
*)h
;
649 * Test the checksum. Note that POSIX specifies _unsigned_
650 * bytes for this calculation.
652 sum
= tar_atol(header
->checksum
, sizeof(header
->checksum
));
654 for (i
= 0; i
< 148; i
++)
655 check
+= (unsigned char)bytes
[i
];
659 check
+= (unsigned char)bytes
[i
];
664 * Repeat test with _signed_ bytes, just in case this archive
665 * was created by an old BSD, Solaris, or HP-UX tar with a
666 * broken checksum calculation.
669 for (i
= 0; i
< 148; i
++)
670 check
+= (signed char)bytes
[i
];
674 check
+= (signed char)bytes
[i
];
682 * Return true if this block contains only nulls.
685 archive_block_is_null(const unsigned char *p
)
689 for (i
= 0; i
< ARCHIVE_BYTES_PER_RECORD
/ sizeof(*p
); i
++)
696 * Interpret 'A' Solaris ACL header
699 header_Solaris_ACL(struct archive_read
*a
, struct tar
*tar
,
700 struct archive_entry
*entry
, const void *h
)
706 err
= read_body_to_string(a
, tar
, &(tar
->acl_text
), h
);
707 err2
= tar_read_header(a
, tar
, entry
);
708 err
= err_combine(err
, err2
);
710 /* XXX Ensure p doesn't overrun acl_text */
712 /* Skip leading octal number. */
713 /* XXX TODO: Parse the octal number and sanity-check it. */
719 wp
= (wchar_t *)malloc((strlen(p
) + 1) * sizeof(wchar_t));
721 utf8_decode(wp
, p
, strlen(p
));
722 err2
= __archive_entry_acl_parse_w(entry
, wp
,
723 ARCHIVE_ENTRY_ACL_TYPE_ACCESS
);
724 err
= err_combine(err
, err2
);
732 * Interpret 'K' long linkname header.
735 header_longlink(struct archive_read
*a
, struct tar
*tar
,
736 struct archive_entry
*entry
, const void *h
)
740 err
= read_body_to_string(a
, tar
, &(tar
->longlink
), h
);
741 err2
= tar_read_header(a
, tar
, entry
);
742 if (err
== ARCHIVE_OK
&& err2
== ARCHIVE_OK
) {
743 /* Set symlink if symlink already set, else hardlink. */
744 archive_entry_set_link(entry
, tar
->longlink
.s
);
746 return (err_combine(err
, err2
));
750 * Interpret 'L' long filename header.
753 header_longname(struct archive_read
*a
, struct tar
*tar
,
754 struct archive_entry
*entry
, const void *h
)
758 err
= read_body_to_string(a
, tar
, &(tar
->longname
), h
);
759 /* Read and parse "real" header, then override name. */
760 err2
= tar_read_header(a
, tar
, entry
);
761 if (err
== ARCHIVE_OK
&& err2
== ARCHIVE_OK
)
762 archive_entry_set_pathname(entry
, tar
->longname
.s
);
763 return (err_combine(err
, err2
));
768 * Interpret 'V' GNU tar volume header.
771 header_volume(struct archive_read
*a
, struct tar
*tar
,
772 struct archive_entry
*entry
, const void *h
)
776 /* Just skip this and read the next header. */
777 return (tar_read_header(a
, tar
, entry
));
781 * Read body of an archive entry into an archive_string object.
784 read_body_to_string(struct archive_read
*a
, struct tar
*tar
,
785 struct archive_string
*as
, const void *h
)
787 off_t size
, padded_size
;
788 ssize_t bytes_read
, bytes_to_copy
;
789 const struct archive_entry_header_ustar
*header
;
793 (void)tar
; /* UNUSED */
794 header
= (const struct archive_entry_header_ustar
*)h
;
795 size
= tar_atol(header
->size
, sizeof(header
->size
));
797 /* Read the body into the string. */
798 archive_string_ensure(as
, size
+1);
799 padded_size
= (size
+ 511) & ~ 511;
801 while (padded_size
> 0) {
802 bytes_read
= (a
->decompressor
->read_ahead
)(a
, &src
, padded_size
);
804 return (ARCHIVE_FATAL
);
805 if (bytes_read
> padded_size
)
806 bytes_read
= padded_size
;
807 (a
->decompressor
->consume
)(a
, bytes_read
);
808 bytes_to_copy
= bytes_read
;
809 if ((off_t
)bytes_to_copy
> size
)
810 bytes_to_copy
= (ssize_t
)size
;
811 memcpy(dest
, src
, bytes_to_copy
);
812 dest
+= bytes_to_copy
;
813 size
-= bytes_to_copy
;
814 padded_size
-= bytes_read
;
821 * Parse out common header elements.
823 * This would be the same as header_old_tar, except that the
824 * filename is handled slightly differently for old and POSIX
825 * entries (POSIX entries support a 'prefix'). This factoring
826 * allows header_old_tar and header_ustar
827 * to handle filenames differently, while still putting most of the
828 * common parsing into one place.
831 header_common(struct archive_read
*a
, struct tar
*tar
,
832 struct archive_entry
*entry
, const void *h
)
834 const struct archive_entry_header_ustar
*header
;
837 (void)a
; /* UNUSED */
839 header
= (const struct archive_entry_header_ustar
*)h
;
840 if (header
->linkname
[0])
841 archive_strncpy(&(tar
->entry_linkname
), header
->linkname
,
842 sizeof(header
->linkname
));
844 archive_string_empty(&(tar
->entry_linkname
));
846 /* Parse out the numeric fields (all are octal) */
847 archive_entry_set_mode(entry
, tar_atol(header
->mode
, sizeof(header
->mode
)));
848 archive_entry_set_uid(entry
, tar_atol(header
->uid
, sizeof(header
->uid
)));
849 archive_entry_set_gid(entry
, tar_atol(header
->gid
, sizeof(header
->gid
)));
850 archive_entry_set_size(entry
, tar_atol(header
->size
, sizeof(header
->size
)));
851 archive_entry_set_mtime(entry
, tar_atol(header
->mtime
, sizeof(header
->mtime
)), 0);
853 /* Handle the tar type flag appropriately. */
854 tartype
= header
->typeflag
[0];
857 case '1': /* Hard link */
858 archive_entry_set_hardlink(entry
, tar
->entry_linkname
.s
);
860 * The following may seem odd, but: Technically, tar
861 * does not store the file type for a "hard link"
862 * entry, only the fact that it is a hard link. So, I
863 * leave the type zero normally. But, pax interchange
864 * format allows hard links to have data, which
865 * implies that the underlying entry is a regular
868 if (archive_entry_size(entry
) > 0)
869 archive_entry_set_filetype(entry
, AE_IFREG
);
872 * A tricky point: Traditionally, tar readers have
873 * ignored the size field when reading hardlink
874 * entries, and some writers put non-zero sizes even
875 * though the body is empty. POSIX.1-2001 broke with
876 * this tradition by permitting hardlink entries to
877 * store valid bodies in pax interchange format, but
878 * not in ustar format. Since there is no hard and
879 * fast way to distinguish pax interchange from
880 * earlier archives (the 'x' and 'g' entries are
881 * optional, after all), we need a heuristic. Here, I
882 * use the bid function to test whether or not there's
883 * a valid header following. Of course, if we know
884 * this is pax interchange format, then we must obey
887 * This heuristic will only fail for a pax interchange
888 * archive that is storing hardlink bodies, no pax
889 * extended attribute entries have yet occurred, and
890 * we encounter a hardlink entry for a file that is
891 * itself an uncompressed tar archive.
893 if (archive_entry_size(entry
) > 0 &&
894 a
->archive
.archive_format
!= ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE
&&
895 archive_read_format_tar_bid(a
) > 50)
896 archive_entry_set_size(entry
, 0);
898 case '2': /* Symlink */
899 archive_entry_set_filetype(entry
, AE_IFLNK
);
900 archive_entry_set_size(entry
, 0);
901 archive_entry_set_symlink(entry
, tar
->entry_linkname
.s
);
903 case '3': /* Character device */
904 archive_entry_set_filetype(entry
, AE_IFCHR
);
905 archive_entry_set_size(entry
, 0);
907 case '4': /* Block device */
908 archive_entry_set_filetype(entry
, AE_IFBLK
);
909 archive_entry_set_size(entry
, 0);
912 archive_entry_set_filetype(entry
, AE_IFDIR
);
913 archive_entry_set_size(entry
, 0);
915 case '6': /* FIFO device */
916 archive_entry_set_filetype(entry
, AE_IFIFO
);
917 archive_entry_set_size(entry
, 0);
919 case 'D': /* GNU incremental directory type */
921 * No special handling is actually required here.
922 * It might be nice someday to preprocess the file list and
923 * provide it to the client, though.
925 archive_entry_set_filetype(entry
, AE_IFDIR
);
927 case 'M': /* GNU "Multi-volume" (remainder of file from last archive)*/
929 * As far as I can tell, this is just like a regular file
930 * entry, except that the contents should be _appended_ to
931 * the indicated file at the indicated offset. This may
932 * require some API work to fully support.
935 case 'N': /* Old GNU "long filename" entry. */
936 /* The body of this entry is a script for renaming
937 * previously-extracted entries. Ugh. It will never
938 * be supported by libarchive. */
939 archive_entry_set_filetype(entry
, AE_IFREG
);
941 case 'S': /* GNU sparse files */
943 * Sparse files are really just regular files with
944 * sparse information in the extended area.
947 default: /* Regular file and non-standard types */
949 * Per POSIX: non-recognized types should always be
950 * treated as regular files.
952 archive_entry_set_filetype(entry
, AE_IFREG
);
959 * Parse out header elements for "old-style" tar archives.
962 header_old_tar(struct archive_read
*a
, struct tar
*tar
,
963 struct archive_entry
*entry
, const void *h
)
965 const struct archive_entry_header_ustar
*header
;
967 /* Copy filename over (to ensure null termination). */
968 header
= (const struct archive_entry_header_ustar
*)h
;
969 archive_strncpy(&(tar
->entry_name
), header
->name
, sizeof(header
->name
));
970 archive_entry_set_pathname(entry
, tar
->entry_name
.s
);
972 /* Grab rest of common fields */
973 header_common(a
, tar
, entry
, h
);
975 tar
->entry_bytes_remaining
= archive_entry_size(entry
);
976 tar
->entry_padding
= 0x1ff & (-tar
->entry_bytes_remaining
);
981 * Parse a file header for a pax extended archive entry.
984 header_pax_global(struct archive_read
*a
, struct tar
*tar
,
985 struct archive_entry
*entry
, const void *h
)
989 err
= read_body_to_string(a
, tar
, &(tar
->pax_global
), h
);
990 err2
= tar_read_header(a
, tar
, entry
);
991 return (err_combine(err
, err2
));
995 header_pax_extensions(struct archive_read
*a
, struct tar
*tar
,
996 struct archive_entry
*entry
, const void *h
)
1000 read_body_to_string(a
, tar
, &(tar
->pax_header
), h
);
1002 /* Parse the next header. */
1003 err
= tar_read_header(a
, tar
, entry
);
1006 * TODO: Parse global/default options into 'entry' struct here
1007 * before handling file-specific options.
1009 * This design (parse standard header, then overwrite with pax
1010 * extended attribute data) usually works well, but isn't ideal;
1011 * it would be better to parse the pax extended attributes first
1012 * and then skip any fields in the standard header that were
1013 * defined in the pax header.
1015 err2
= pax_header(a
, tar
, entry
, tar
->pax_header
.s
);
1016 err
= err_combine(err
, err2
);
1017 tar
->entry_bytes_remaining
= archive_entry_size(entry
);
1018 tar
->entry_padding
= 0x1ff & (-tar
->entry_bytes_remaining
);
1024 * Parse a file header for a Posix "ustar" archive entry. This also
1025 * handles "pax" or "extended ustar" entries.
1028 header_ustar(struct archive_read
*a
, struct tar
*tar
,
1029 struct archive_entry
*entry
, const void *h
)
1031 const struct archive_entry_header_ustar
*header
;
1032 struct archive_string
*as
;
1034 header
= (const struct archive_entry_header_ustar
*)h
;
1036 /* Copy name into an internal buffer to ensure null-termination. */
1037 as
= &(tar
->entry_name
);
1038 if (header
->prefix
[0]) {
1039 archive_strncpy(as
, header
->prefix
, sizeof(header
->prefix
));
1040 if (as
->s
[archive_strlen(as
) - 1] != '/')
1041 archive_strappend_char(as
, '/');
1042 archive_strncat(as
, header
->name
, sizeof(header
->name
));
1044 archive_strncpy(as
, header
->name
, sizeof(header
->name
));
1046 archive_entry_set_pathname(entry
, as
->s
);
1048 /* Handle rest of common fields. */
1049 header_common(a
, tar
, entry
, h
);
1051 /* Handle POSIX ustar fields. */
1052 archive_strncpy(&(tar
->entry_uname
), header
->uname
,
1053 sizeof(header
->uname
));
1054 archive_entry_set_uname(entry
, tar
->entry_uname
.s
);
1056 archive_strncpy(&(tar
->entry_gname
), header
->gname
,
1057 sizeof(header
->gname
));
1058 archive_entry_set_gname(entry
, tar
->entry_gname
.s
);
1060 /* Parse out device numbers only for char and block specials. */
1061 if (header
->typeflag
[0] == '3' || header
->typeflag
[0] == '4') {
1062 archive_entry_set_rdevmajor(entry
,
1063 tar_atol(header
->rdevmajor
, sizeof(header
->rdevmajor
)));
1064 archive_entry_set_rdevminor(entry
,
1065 tar_atol(header
->rdevminor
, sizeof(header
->rdevminor
)));
1068 tar
->entry_bytes_remaining
= archive_entry_size(entry
);
1069 tar
->entry_padding
= 0x1ff & (-tar
->entry_bytes_remaining
);
1076 * Parse the pax extended attributes record.
1078 * Returns non-zero if there's an error in the data.
1081 pax_header(struct archive_read
*a
, struct tar
*tar
,
1082 struct archive_entry
*entry
, char *attr
)
1084 size_t attr_length
, l
, line_length
;
1086 wchar_t *key
, *wp
, *value
;
1089 attr_length
= strlen(attr
);
1091 while (attr_length
> 0) {
1092 /* Parse decimal length field at start of line. */
1095 line
= p
= attr
; /* Record start of line. */
1102 if (*p
< '0' || *p
> '9')
1105 line_length
+= *p
- '0';
1106 if (line_length
> 999999) {
1107 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1108 "Rejecting pax extended attribute > 1MB");
1109 return (ARCHIVE_WARN
);
1115 if (line_length
> attr_length
)
1118 /* Ensure pax_entry buffer is big enough. */
1119 if (tar
->pax_entry_length
<= line_length
) {
1120 wchar_t *old_entry
= tar
->pax_entry
;
1122 if (tar
->pax_entry_length
<= 0)
1123 tar
->pax_entry_length
= 1024;
1124 while (tar
->pax_entry_length
<= line_length
+ 1)
1125 tar
->pax_entry_length
*= 2;
1127 old_entry
= tar
->pax_entry
;
1128 tar
->pax_entry
= (wchar_t *)realloc(tar
->pax_entry
,
1129 tar
->pax_entry_length
* sizeof(wchar_t));
1130 if (tar
->pax_entry
== NULL
) {
1132 archive_set_error(&a
->archive
, ENOMEM
,
1134 return (ARCHIVE_FATAL
);
1138 /* Decode UTF-8 to wchar_t, null-terminate result. */
1139 if (utf8_decode(tar
->pax_entry
, p
,
1140 line_length
- (p
- attr
) - 1)) {
1141 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1142 "Invalid UTF8 character in pax extended attribute");
1143 err
= err_combine(err
, ARCHIVE_WARN
);
1146 /* Null-terminate 'key' value. */
1147 wp
= key
= tar
->pax_entry
;
1150 while (*wp
&& *wp
!= L
'=')
1152 if (*wp
== L
'\0' || wp
== NULL
) {
1153 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_MISC
,
1154 "Invalid pax extended attributes");
1155 return (ARCHIVE_WARN
);
1159 /* Identify null-terminated 'value' portion. */
1162 /* Identify this attribute and set it in the entry. */
1163 err2
= pax_attribute(entry
, key
, value
);
1164 err
= err_combine(err
, err2
);
1166 /* Skip to next line */
1167 attr
+= line_length
;
1168 attr_length
-= line_length
;
1174 pax_attribute_xattr(struct archive_entry
*entry
,
1175 wchar_t *name
, wchar_t *value
)
1177 char *name_decoded
, *name_narrow
;
1178 void *value_decoded
;
1181 if (wcslen(name
) < 18 || (wcsncmp(name
, L
"LIBARCHIVE.xattr.", 17)) != 0)
1186 /* URL-decode name */
1187 name_narrow
= wide_to_narrow(name
);
1188 if (name_narrow
== NULL
)
1190 name_decoded
= url_decode(name_narrow
);
1192 if (name_decoded
== NULL
)
1195 /* Base-64 decode value */
1196 value_decoded
= base64_decode(value
, wcslen(value
), &value_len
);
1197 if (value_decoded
== NULL
) {
1202 archive_entry_xattr_add_entry(entry
, name_decoded
,
1203 value_decoded
, value_len
);
1206 free(value_decoded
);
1211 * Parse a single key=value attribute. key/value pointers are
1212 * assumed to point into reasonably long-lived storage.
1214 * Note that POSIX reserves all-lowercase keywords. Vendor-specific
1215 * extensions should always have keywords of the form "VENDOR.attribute"
1216 * In particular, it's quite feasible to support many different
1217 * vendor extensions here. I'm using "LIBARCHIVE" for extensions
1218 * unique to this library (currently, there are none).
1220 * Investigate other vendor-specific extensions, as well and see if
1221 * any of them look useful.
1224 pax_attribute(struct archive_entry
*entry
,
1225 wchar_t *key
, wchar_t *value
)
1232 /* Our extensions */
1233 /* TODO: Handle arbitrary extended attributes... */
1235 if (strcmp(key, "LIBARCHIVE.xxxxxxx")==0)
1236 archive_entry_set_xxxxxx(entry, value);
1238 if (wcsncmp(key
, L
"LIBARCHIVE.xattr.", 17)==0)
1239 pax_attribute_xattr(entry
, key
, value
);
1242 /* We support some keys used by the "star" archiver */
1243 if (wcscmp(key
, L
"SCHILY.acl.access")==0)
1244 __archive_entry_acl_parse_w(entry
, value
,
1245 ARCHIVE_ENTRY_ACL_TYPE_ACCESS
);
1246 else if (wcscmp(key
, L
"SCHILY.acl.default")==0)
1247 __archive_entry_acl_parse_w(entry
, value
,
1248 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT
);
1249 else if (wcscmp(key
, L
"SCHILY.devmajor")==0)
1250 archive_entry_set_rdevmajor(entry
, tar_atol10(value
, wcslen(value
)));
1251 else if (wcscmp(key
, L
"SCHILY.devminor")==0)
1252 archive_entry_set_rdevminor(entry
, tar_atol10(value
, wcslen(value
)));
1253 else if (wcscmp(key
, L
"SCHILY.fflags")==0)
1254 archive_entry_copy_fflags_text_w(entry
, value
);
1255 else if (wcscmp(key
, L
"SCHILY.dev")==0)
1256 archive_entry_set_dev(entry
, tar_atol10(value
, wcslen(value
)));
1257 else if (wcscmp(key
, L
"SCHILY.ino")==0)
1258 archive_entry_set_ino(entry
, tar_atol10(value
, wcslen(value
)));
1259 else if (wcscmp(key
, L
"SCHILY.nlink")==0)
1260 archive_entry_set_nlink(entry
, tar_atol10(value
, wcslen(value
)));
1263 if (wcscmp(key
, L
"atime")==0) {
1264 pax_time(value
, &s
, &n
);
1265 archive_entry_set_atime(entry
, s
, n
);
1269 if (wcscmp(key
, L
"ctime")==0) {
1270 pax_time(value
, &s
, &n
);
1271 archive_entry_set_ctime(entry
, s
, n
);
1272 } else if (wcscmp(key
, L
"charset")==0) {
1273 /* TODO: Publish charset information in entry. */
1274 } else if (wcscmp(key
, L
"comment")==0) {
1275 /* TODO: Publish comment in entry. */
1279 if (wcscmp(key
, L
"gid")==0)
1280 archive_entry_set_gid(entry
, tar_atol10(value
, wcslen(value
)));
1281 else if (wcscmp(key
, L
"gname")==0)
1282 archive_entry_copy_gname_w(entry
, value
);
1285 /* pax interchange doesn't distinguish hardlink vs. symlink. */
1286 if (wcscmp(key
, L
"linkpath")==0) {
1287 if (archive_entry_hardlink(entry
))
1288 archive_entry_copy_hardlink_w(entry
, value
);
1290 archive_entry_copy_symlink_w(entry
, value
);
1294 if (wcscmp(key
, L
"mtime")==0) {
1295 pax_time(value
, &s
, &n
);
1296 archive_entry_set_mtime(entry
, s
, n
);
1300 if (wcscmp(key
, L
"path")==0)
1301 archive_entry_copy_pathname_w(entry
, value
);
1304 /* POSIX has reserved 'realtime.*' */
1307 /* POSIX has reserved 'security.*' */
1308 /* Someday: if (wcscmp(key, L"security.acl")==0) { ... } */
1309 if (wcscmp(key
, L
"size")==0)
1310 archive_entry_set_size(entry
, tar_atol10(value
, wcslen(value
)));
1313 if (wcscmp(key
, L
"uid")==0)
1314 archive_entry_set_uid(entry
, tar_atol10(value
, wcslen(value
)));
1315 else if (wcscmp(key
, L
"uname")==0)
1316 archive_entry_copy_uname_w(entry
, value
);
1325 * parse a decimal time value, which may include a fractional portion
1328 pax_time(const wchar_t *p
, int64_t *ps
, long *pn
)
1334 int64_t limit
, last_digit_limit
;
1336 limit
= INT64_MAX
/ 10;
1337 last_digit_limit
= INT64_MAX
% 10;
1345 while (*p
>= '0' && *p
<= '9') {
1348 (s
== limit
&& digit
> last_digit_limit
)) {
1352 s
= (s
* 10) + digit
;
1358 /* Calculate nanoseconds. */
1367 if (*p
>= '0' && *p
<= '9')
1368 *pn
+= (*p
- '0') * l
;
1375 * Parse GNU tar header
1378 header_gnutar(struct archive_read
*a
, struct tar
*tar
,
1379 struct archive_entry
*entry
, const void *h
)
1381 const struct archive_entry_header_gnutar
*header
;
1386 * GNU header is like POSIX ustar, except 'prefix' is
1387 * replaced with some other fields. This also means the
1388 * filename is stored as in old-style archives.
1391 /* Grab fields common to all tar variants. */
1392 header_common(a
, tar
, entry
, h
);
1394 /* Copy filename over (to ensure null termination). */
1395 header
= (const struct archive_entry_header_gnutar
*)h
;
1396 archive_strncpy(&(tar
->entry_name
), header
->name
,
1397 sizeof(header
->name
));
1398 archive_entry_set_pathname(entry
, tar
->entry_name
.s
);
1400 /* Fields common to ustar and GNU */
1401 /* XXX Can the following be factored out since it's common
1402 * to ustar and gnu tar? Is it okay to move it down into
1403 * header_common, perhaps? */
1404 archive_strncpy(&(tar
->entry_uname
),
1405 header
->uname
, sizeof(header
->uname
));
1406 archive_entry_set_uname(entry
, tar
->entry_uname
.s
);
1408 archive_strncpy(&(tar
->entry_gname
),
1409 header
->gname
, sizeof(header
->gname
));
1410 archive_entry_set_gname(entry
, tar
->entry_gname
.s
);
1412 /* Parse out device numbers only for char and block specials */
1413 if (header
->typeflag
[0] == '3' || header
->typeflag
[0] == '4') {
1414 archive_entry_set_rdevmajor(entry
,
1415 tar_atol(header
->rdevmajor
, sizeof(header
->rdevmajor
)));
1416 archive_entry_set_rdevminor(entry
,
1417 tar_atol(header
->rdevminor
, sizeof(header
->rdevminor
)));
1419 archive_entry_set_rdev(entry
, 0);
1421 tar
->entry_bytes_remaining
= archive_entry_size(entry
);
1422 tar
->entry_padding
= 0x1ff & (-tar
->entry_bytes_remaining
);
1424 /* Grab GNU-specific fields. */
1425 archive_entry_set_atime(entry
,
1426 tar_atol(header
->atime
, sizeof(header
->atime
)), 0);
1427 archive_entry_set_ctime(entry
,
1428 tar_atol(header
->ctime
, sizeof(header
->ctime
)), 0);
1429 if (header
->realsize
[0] != 0) {
1430 archive_entry_set_size(entry
,
1431 tar_atol(header
->realsize
, sizeof(header
->realsize
)));
1434 if (header
->sparse
[0].offset
[0] != 0) {
1435 gnu_read_sparse_data(a
, tar
, header
);
1437 if (header
->isextended
[0] != 0) {
1446 gnu_read_sparse_data(struct archive_read
*a
, struct tar
*tar
,
1447 const struct archive_entry_header_gnutar
*header
)
1452 struct gnu_sparse sparse
[21];
1456 const struct extended
*ext
;
1458 gnu_parse_sparse_data(a
, tar
, header
->sparse
, 4);
1459 if (header
->isextended
[0] == 0)
1460 return (ARCHIVE_OK
);
1463 bytes_read
= (a
->decompressor
->read_ahead
)(a
, &data
, 512);
1465 return (ARCHIVE_FATAL
);
1466 if (bytes_read
< 512) {
1467 archive_set_error(&a
->archive
, ARCHIVE_ERRNO_FILE_FORMAT
,
1468 "Truncated tar archive "
1469 "detected while reading sparse file data");
1470 return (ARCHIVE_FATAL
);
1472 (a
->decompressor
->consume
)(a
, 512);
1473 ext
= (const struct extended
*)data
;
1474 gnu_parse_sparse_data(a
, tar
, ext
->sparse
, 21);
1475 } while (ext
->isextended
[0] != 0);
1476 if (tar
->sparse_list
!= NULL
)
1477 tar
->entry_offset
= tar
->sparse_list
->offset
;
1478 return (ARCHIVE_OK
);
1482 gnu_parse_sparse_data(struct archive_read
*a
, struct tar
*tar
,
1483 const struct gnu_sparse
*sparse
, int length
)
1485 struct sparse_block
*last
;
1486 struct sparse_block
*p
;
1488 (void)a
; /* UNUSED */
1490 last
= tar
->sparse_list
;
1491 while (last
!= NULL
&& last
->next
!= NULL
)
1494 while (length
> 0 && sparse
->offset
[0] != 0) {
1495 p
= (struct sparse_block
*)malloc(sizeof(*p
));
1497 __archive_errx(1, "Out of memory");
1498 memset(p
, 0, sizeof(*p
));
1502 tar
->sparse_list
= p
;
1504 p
->offset
= tar_atol(sparse
->offset
, sizeof(sparse
->offset
));
1506 tar_atol(sparse
->numbytes
, sizeof(sparse
->numbytes
));
1513 * Convert text->integer.
1515 * Traditional tar formats (including POSIX) specify base-8 for
1516 * all of the standard numeric fields. This is a significant limitation
1518 * = file size is limited to 8GB
1519 * = rdevmajor and rdevminor are limited to 21 bits
1520 * = uid/gid are limited to 21 bits
1522 * There are two workarounds for this:
1523 * = pax extended headers, which use variable-length string fields
1524 * = GNU tar and STAR both allow either base-8 or base-256 in
1525 * most fields. The high bit is set to indicate base-256.
1527 * On read, this implementation supports both extensions.
1530 tar_atol(const char *p
, unsigned char_cnt
)
1533 * Technically, GNU tar considers a field to be in base-256
1534 * only if the first byte is 0xff or 0x80.
1537 return (tar_atol256(p
, char_cnt
));
1538 return (tar_atol8(p
, char_cnt
));
1542 * Note that this implementation does not (and should not!) obey
1543 * locale settings; you cannot simply substitute strtol here, since
1544 * it does obey locale.
1547 tar_atol8(const char *p
, unsigned char_cnt
)
1549 int64_t l
, limit
, last_digit_limit
;
1550 int digit
, sign
, base
;
1553 limit
= INT64_MAX
/ base
;
1554 last_digit_limit
= INT64_MAX
% base
;
1556 while (*p
== ' ' || *p
== '\t')
1566 while (digit
>= 0 && digit
< base
&& char_cnt
-- > 0) {
1567 if (l
>limit
|| (l
== limit
&& digit
> last_digit_limit
)) {
1568 l
= UINT64_MAX
; /* Truncate on overflow. */
1571 l
= (l
* base
) + digit
;
1574 return (sign
< 0) ? -l
: l
;
1579 * Note that this implementation does not (and should not!) obey
1580 * locale settings; you cannot simply substitute strtol here, since
1581 * it does obey locale.
1584 tar_atol10(const wchar_t *p
, unsigned char_cnt
)
1586 int64_t l
, limit
, last_digit_limit
;
1587 int base
, digit
, sign
;
1590 limit
= INT64_MAX
/ base
;
1591 last_digit_limit
= INT64_MAX
% base
;
1593 while (*p
== ' ' || *p
== '\t')
1603 while (digit
>= 0 && digit
< base
&& char_cnt
-- > 0) {
1604 if (l
> limit
|| (l
== limit
&& digit
> last_digit_limit
)) {
1605 l
= UINT64_MAX
; /* Truncate on overflow. */
1608 l
= (l
* base
) + digit
;
1611 return (sign
< 0) ? -l
: l
;
1615 * Parse a base-256 integer. This is just a straight signed binary
1616 * value in big-endian order, except that the high-order bit is
1617 * ignored. Remember that "int64_t" may or may not be exactly 64
1618 * bits; the implementation here tries to avoid making any assumptions
1619 * about the actual size of an int64_t. It does assume we're using
1620 * twos-complement arithmetic, though.
1623 tar_atol256(const char *_p
, unsigned char_cnt
)
1625 int64_t l
, upper_limit
, lower_limit
;
1626 const unsigned char *p
= (const unsigned char *)_p
;
1628 upper_limit
= INT64_MAX
/ 256;
1629 lower_limit
= INT64_MIN
/ 256;
1631 /* Pad with 1 or 0 bits, depending on sign. */
1632 if ((0x40 & *p
) == 0x40)
1636 l
= (l
<< 6) | (0x3f & *p
++);
1637 while (--char_cnt
> 0) {
1638 if (l
> upper_limit
) {
1639 l
= INT64_MAX
; /* Truncate on overflow */
1641 } else if (l
< lower_limit
) {
1645 l
= (l
<< 8) | (0xff & (int64_t)*p
++);
1651 utf8_decode(wchar_t *dest
, const char *src
, size_t length
)
1657 while (length
> 0) {
1658 n
= UTF8_mbrtowc(dest
, src
, length
);
1670 * Copied and simplified from FreeBSD libc/locale.
1673 UTF8_mbrtowc(wchar_t *pwc
, const char *s
, size_t n
)
1675 int ch
, i
, len
, mask
;
1678 if (s
== NULL
|| n
== 0 || pwc
== NULL
)
1682 * Determine the number of octets that make up this character from
1683 * the first octet, and a mask that extracts the interesting bits of
1686 ch
= (unsigned char)*s
;
1687 if ((ch
& 0x80) == 0) {
1690 } else if ((ch
& 0xe0) == 0xc0) {
1693 } else if ((ch
& 0xf0) == 0xe0) {
1696 } else if ((ch
& 0xf8) == 0xf0) {
1699 } else if ((ch
& 0xfc) == 0xf8) {
1702 } else if ((ch
& 0xfe) == 0xfc) {
1706 /* Invalid first byte; convert to '?' */
1711 if (n
< (size_t)len
) {
1712 /* Invalid first byte; convert to '?' */
1718 * Decode the octet sequence representing the character in chunks
1719 * of 6 bits, most significant first.
1721 wch
= (unsigned char)*s
++ & mask
;
1724 if ((*s
& 0xc0) != 0x80) {
1725 /* Invalid intermediate byte; consume one byte and
1734 /* Assign the value to the output; out-of-range values
1735 * just get truncated. */
1736 *pwc
= (wchar_t)wch
;
1739 * If platform has WCHAR_MAX, we can do something
1740 * more sensible with out-of-range values.
1742 if (wch
>= WCHAR_MAX
)
1745 /* Return number of bytes input consumed: 0 for end-of-string. */
1746 return (wch
== L
'\0' ? 0 : len
);
1751 * base64_decode - Base64 decode
1753 * This accepts most variations of base-64 encoding, including:
1754 * * with or without line breaks
1755 * * with or without the final group padded with '=' or '_' characters
1756 * (The most economical Base-64 variant does not pad the last group and
1757 * omits line breaks; RFC1341 used for MIME requires both.)
1760 base64_decode(const wchar_t *src
, size_t len
, size_t *out_len
)
1762 static const unsigned char digits
[64] = {
1763 'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
1764 'O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b',
1765 'c','d','e','f','g','h','i','j','k','l','m','n','o','p',
1766 'q','r','s','t','u','v','w','x','y','z','0','1','2','3',
1767 '4','5','6','7','8','9','+','/' };
1768 static unsigned char decode_table
[128];
1771 /* If the decode table is not yet initialized, prepare it. */
1772 if (decode_table
[digits
[1]] != 1) {
1774 memset(decode_table
, 0xff, sizeof(decode_table
));
1775 for (i
= 0; i
< sizeof(digits
); i
++)
1776 decode_table
[digits
[i
]] = i
;
1779 /* Allocate enough space to hold the entire output. */
1780 /* Note that we may not use all of this... */
1781 out
= (char *)malloc((len
* 3 + 3) / 4);
1789 /* Collect the next group of (up to) four characters. */
1792 while (group_size
< 4 && len
> 0) {
1793 /* '=' or '_' padding indicates final group. */
1794 if (*src
== '=' || *src
== '_') {
1798 /* Skip illegal characters (including line breaks) */
1799 if (*src
> 127 || *src
< 32
1800 || decode_table
[*src
] == 0xff) {
1806 v
|= decode_table
[*src
++];
1810 /* Align a short group properly. */
1811 v
<<= 6 * (4 - group_size
);
1812 /* Unpack the group we just collected. */
1813 switch (group_size
) {
1814 case 4: d
[2] = v
& 0xff;
1816 case 3: d
[1] = (v
>> 8) & 0xff;
1818 case 2: d
[0] = (v
>> 16) & 0xff;
1820 case 1: /* this is invalid! */
1823 d
+= group_size
* 3 / 4;
1831 * This is a little tricky because the C99 standard wcstombs()
1832 * function returns the number of bytes that were converted,
1833 * not the number that should be converted. As a result,
1834 * we can never accurately size the output buffer (without
1835 * doing a tedious output size calculation in advance).
1836 * This approach (try a conversion, then try again if it fails)
1837 * will almost always succeed on the first try, and is thus
1838 * much faster, at the cost of sometimes requiring multiple
1839 * passes while we expand the buffer.
1842 wide_to_narrow(const wchar_t *wval
)
1844 int converted_length
;
1845 /* Guess an output buffer size and try the conversion. */
1846 int alloc_length
= wcslen(wval
) * 3;
1847 char *mbs_val
= (char *)malloc(alloc_length
+ 1);
1848 if (mbs_val
== NULL
)
1850 converted_length
= wcstombs(mbs_val
, wval
, alloc_length
);
1852 /* If we exhausted the buffer, resize and try again. */
1853 while (converted_length
>= alloc_length
) {
1856 mbs_val
= (char *)malloc(alloc_length
+ 1);
1857 if (mbs_val
== NULL
)
1859 converted_length
= wcstombs(mbs_val
, wval
, alloc_length
);
1862 /* Ensure a trailing null and return the final string. */
1863 mbs_val
[alloc_length
] = '\0';
1868 url_decode(const char *in
)
1873 out
= (char *)malloc(strlen(in
) + 1);
1876 for (s
= in
, d
= out
; *s
!= '\0'; ) {
1878 /* Try to convert % escape */
1879 int digit1
= tohex(s
[1]);
1880 int digit2
= tohex(s
[2]);
1881 if (digit1
>= 0 && digit2
>= 0) {
1882 /* Looks good, consume three chars */
1884 /* Convert output */
1885 *d
++ = ((digit1
<< 4) | digit2
);
1888 /* Else fall through and treat '%' as normal char */
1899 if (c
>= '0' && c
<= '9')
1901 else if (c
>= 'A' && c
<= 'F')
1902 return (c
- 'A' + 10);
1903 else if (c
>= 'a' && c
<= 'f')
1904 return (c
- 'a' + 10);