Import libarchive-2.4.17. See NEWS for details.
[dragonfly.git] / contrib / libarchive-2 / libarchive / archive_read_support_format_tar.c
blob26b2abbe5784e8e2d4c8eaedd5bbafdb1b9f6183
1 /*-
2 * Copyright (c) 2003-2007 Tim Kientzle
3 * All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
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.65 2008/01/31 07:41:45 kientzle Exp $");
29 #ifdef HAVE_ERRNO_H
30 #include <errno.h>
31 #endif
32 #include <stddef.h>
33 /* #include <stdint.h> */ /* See archive_platform.h */
34 #ifdef HAVE_STDLIB_H
35 #include <stdlib.h>
36 #endif
37 #ifdef HAVE_STRING_H
38 #include <string.h>
39 #endif
41 /* Obtain suitable wide-character manipulation functions. */
42 #ifdef HAVE_WCHAR_H
43 #include <wchar.h>
44 #else
45 /* Good enough for equality testing, which is all we need. */
46 static int wcscmp(const wchar_t *s1, const wchar_t *s2)
48 int diff = *s1 - *s2;
49 while (*s1 && diff == 0)
50 diff = (int)*++s1 - (int)*++s2;
51 return diff;
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)
56 int diff = *s1 - *s2;
57 while (*s1 && diff == 0 && n-- > 0)
58 diff = (int)*++s1 - (int)*++s2;
59 return diff;
61 static size_t wcslen(const wchar_t *s)
63 const wchar_t *p = s;
64 while (*p)
65 p++;
66 return p - s;
68 #endif
70 #include "archive.h"
71 #include "archive_entry.h"
72 #include "archive_private.h"
73 #include "archive_read_private.h"
75 #define tar_min(a,b) ((a) < (b) ? (a) : (b))
78 * Layout of POSIX 'ustar' tar header.
80 struct archive_entry_header_ustar {
81 char name[100];
82 char mode[8];
83 char uid[8];
84 char gid[8];
85 char size[12];
86 char mtime[12];
87 char checksum[8];
88 char typeflag[1];
89 char linkname[100]; /* "old format" header ends here */
90 char magic[6]; /* For POSIX: "ustar\0" */
91 char version[2]; /* For POSIX: "00" */
92 char uname[32];
93 char gname[32];
94 char rdevmajor[8];
95 char rdevminor[8];
96 char prefix[155];
100 * Structure of GNU tar header
102 struct gnu_sparse {
103 char offset[12];
104 char numbytes[12];
107 struct archive_entry_header_gnutar {
108 char name[100];
109 char mode[8];
110 char uid[8];
111 char gid[8];
112 char size[12];
113 char mtime[12];
114 char checksum[8];
115 char typeflag[1];
116 char linkname[100];
117 char magic[8]; /* "ustar \0" (note blank/blank/null at end) */
118 char uname[32];
119 char gname[32];
120 char rdevmajor[8];
121 char rdevminor[8];
122 char atime[12];
123 char ctime[12];
124 char offset[12];
125 char longnames[4];
126 char unused[1];
127 struct gnu_sparse sparse[4];
128 char isextended[1];
129 char realsize[12];
131 * Old GNU format doesn't use POSIX 'prefix' field; they use
132 * the 'L' (longname) entry instead.
137 * Data specific to this format.
139 struct sparse_block {
140 struct sparse_block *next;
141 off_t offset;
142 off_t remaining;
145 struct tar {
146 struct archive_string acl_text;
147 struct archive_string entry_pathname;
148 struct archive_string entry_linkpath;
149 struct archive_string entry_uname;
150 struct archive_string entry_gname;
151 struct archive_string longlink;
152 struct archive_string longname;
153 struct archive_string pax_header;
154 struct archive_string pax_global;
155 struct archive_string line;
156 int pax_hdrcharset_binary;
157 wchar_t *pax_entry;
158 size_t pax_entry_length;
159 int header_recursion_depth;
160 off_t entry_bytes_remaining;
161 off_t entry_offset;
162 off_t entry_padding;
163 off_t realsize;
164 struct sparse_block *sparse_list;
165 struct sparse_block *sparse_last;
166 int64_t sparse_offset;
167 int64_t sparse_numbytes;
168 int sparse_gnu_major;
169 int sparse_gnu_minor;
170 char sparse_gnu_pending;
173 static ssize_t UTF8_mbrtowc(wchar_t *pwc, const char *s, size_t n);
174 static int archive_block_is_null(const unsigned char *p);
175 static char *base64_decode(const char *, size_t, size_t *);
176 static void gnu_add_sparse_entry(struct tar *,
177 off_t offset, off_t remaining);
178 static void gnu_clear_sparse_list(struct tar *);
179 static int gnu_sparse_old_read(struct archive_read *, struct tar *,
180 const struct archive_entry_header_gnutar *header);
181 static void gnu_sparse_old_parse(struct tar *,
182 const struct gnu_sparse *sparse, int length);
183 static int gnu_sparse_01_parse(struct tar *, const char *);
184 static ssize_t gnu_sparse_10_read(struct archive_read *, struct tar *);
185 static int header_Solaris_ACL(struct archive_read *, struct tar *,
186 struct archive_entry *, const void *);
187 static int header_common(struct archive_read *, struct tar *,
188 struct archive_entry *, const void *);
189 static int header_old_tar(struct archive_read *, struct tar *,
190 struct archive_entry *, const void *);
191 static int header_pax_extensions(struct archive_read *, struct tar *,
192 struct archive_entry *, const void *);
193 static int header_pax_global(struct archive_read *, struct tar *,
194 struct archive_entry *, const void *h);
195 static int header_longlink(struct archive_read *, struct tar *,
196 struct archive_entry *, const void *h);
197 static int header_longname(struct archive_read *, struct tar *,
198 struct archive_entry *, const void *h);
199 static int header_volume(struct archive_read *, struct tar *,
200 struct archive_entry *, const void *h);
201 static int header_ustar(struct archive_read *, struct tar *,
202 struct archive_entry *, const void *h);
203 static int header_gnutar(struct archive_read *, struct tar *,
204 struct archive_entry *, const void *h);
205 static int archive_read_format_tar_bid(struct archive_read *);
206 static int archive_read_format_tar_cleanup(struct archive_read *);
207 static int archive_read_format_tar_read_data(struct archive_read *a,
208 const void **buff, size_t *size, off_t *offset);
209 static int archive_read_format_tar_skip(struct archive_read *a);
210 static int archive_read_format_tar_read_header(struct archive_read *,
211 struct archive_entry *);
212 static int checksum(struct archive_read *, const void *);
213 static int pax_attribute(struct tar *, struct archive_entry *,
214 char *key, char *value);
215 static int pax_header(struct archive_read *, struct tar *,
216 struct archive_entry *, char *attr);
217 static void pax_time(const char *, int64_t *sec, long *nanos);
218 static ssize_t readline(struct archive_read *, struct tar *, const char **,
219 ssize_t limit);
220 static int read_body_to_string(struct archive_read *, struct tar *,
221 struct archive_string *, const void *h);
222 static int64_t tar_atol(const char *, unsigned);
223 static int64_t tar_atol10(const char *, unsigned);
224 static int64_t tar_atol256(const char *, unsigned);
225 static int64_t tar_atol8(const char *, unsigned);
226 static int tar_read_header(struct archive_read *, struct tar *,
227 struct archive_entry *);
228 static int tohex(int c);
229 static char *url_decode(const char *);
230 static wchar_t *utf8_decode(struct tar *, const char *, size_t length);
233 archive_read_support_format_gnutar(struct archive *a)
235 return (archive_read_support_format_tar(a));
240 archive_read_support_format_tar(struct archive *_a)
242 struct archive_read *a = (struct archive_read *)_a;
243 struct tar *tar;
244 int r;
246 tar = (struct tar *)malloc(sizeof(*tar));
247 if (tar == NULL) {
248 archive_set_error(&a->archive, ENOMEM,
249 "Can't allocate tar data");
250 return (ARCHIVE_FATAL);
252 memset(tar, 0, sizeof(*tar));
254 r = __archive_read_register_format(a, tar,
255 archive_read_format_tar_bid,
256 archive_read_format_tar_read_header,
257 archive_read_format_tar_read_data,
258 archive_read_format_tar_skip,
259 archive_read_format_tar_cleanup);
261 if (r != ARCHIVE_OK)
262 free(tar);
263 return (ARCHIVE_OK);
266 static int
267 archive_read_format_tar_cleanup(struct archive_read *a)
269 struct tar *tar;
271 tar = (struct tar *)(a->format->data);
272 gnu_clear_sparse_list(tar);
273 archive_string_free(&tar->acl_text);
274 archive_string_free(&tar->entry_pathname);
275 archive_string_free(&tar->entry_linkpath);
276 archive_string_free(&tar->entry_uname);
277 archive_string_free(&tar->entry_gname);
278 archive_string_free(&tar->line);
279 archive_string_free(&tar->pax_global);
280 archive_string_free(&tar->pax_header);
281 archive_string_free(&tar->longname);
282 archive_string_free(&tar->longlink);
283 free(tar->pax_entry);
284 free(tar);
285 (a->format->data) = NULL;
286 return (ARCHIVE_OK);
290 static int
291 archive_read_format_tar_bid(struct archive_read *a)
293 int bid;
294 ssize_t bytes_read;
295 const void *h;
296 const struct archive_entry_header_ustar *header;
298 bid = 0;
300 /* Now let's look at the actual header and see if it matches. */
301 if (a->decompressor->read_ahead != NULL)
302 bytes_read = (a->decompressor->read_ahead)(a, &h, 512);
303 else
304 bytes_read = 0; /* Empty file. */
305 if (bytes_read < 0)
306 return (ARCHIVE_FATAL);
307 if (bytes_read < 512)
308 return (0);
310 /* If it's an end-of-archive mark, we can handle it. */
311 if ((*(const char *)h) == 0
312 && archive_block_is_null((const unsigned char *)h)) {
314 * Usually, I bid the number of bits verified, but
315 * in this case, 4096 seems excessive so I picked 10 as
316 * an arbitrary but reasonable-seeming value.
318 return (10);
321 /* If it's not an end-of-archive mark, it must have a valid checksum.*/
322 if (!checksum(a, h))
323 return (0);
324 bid += 48; /* Checksum is usually 6 octal digits. */
326 header = (const struct archive_entry_header_ustar *)h;
328 /* Recognize POSIX formats. */
329 if ((memcmp(header->magic, "ustar\0", 6) == 0)
330 &&(memcmp(header->version, "00", 2)==0))
331 bid += 56;
333 /* Recognize GNU tar format. */
334 if ((memcmp(header->magic, "ustar ", 6) == 0)
335 &&(memcmp(header->version, " \0", 2)==0))
336 bid += 56;
338 /* Type flag must be null, digit or A-Z, a-z. */
339 if (header->typeflag[0] != 0 &&
340 !( header->typeflag[0] >= '0' && header->typeflag[0] <= '9') &&
341 !( header->typeflag[0] >= 'A' && header->typeflag[0] <= 'Z') &&
342 !( header->typeflag[0] >= 'a' && header->typeflag[0] <= 'z') )
343 return (0);
344 bid += 2; /* 6 bits of variation in an 8-bit field leaves 2 bits. */
346 /* Sanity check: Look at first byte of mode field. */
347 switch (255 & (unsigned)header->mode[0]) {
348 case 0: case 255:
349 /* Base-256 value: No further verification possible! */
350 break;
351 case ' ': /* Not recommended, but not illegal, either. */
352 break;
353 case '0': case '1': case '2': case '3':
354 case '4': case '5': case '6': case '7':
355 /* Octal Value. */
356 /* TODO: Check format of remainder of this field. */
357 break;
358 default:
359 /* Not a valid mode; bail out here. */
360 return (0);
362 /* TODO: Sanity test uid/gid/size/mtime/rdevmajor/rdevminor fields. */
364 return (bid);
368 * The function invoked by archive_read_header(). This
369 * just sets up a few things and then calls the internal
370 * tar_read_header() function below.
372 static int
373 archive_read_format_tar_read_header(struct archive_read *a,
374 struct archive_entry *entry)
377 * When converting tar archives to cpio archives, it is
378 * essential that each distinct file have a distinct inode
379 * number. To simplify this, we keep a static count here to
380 * assign fake dev/inode numbers to each tar entry. Note that
381 * pax format archives may overwrite this with something more
382 * useful.
384 * Ideally, we would track every file read from the archive so
385 * that we could assign the same dev/ino pair to hardlinks,
386 * but the memory required to store a complete lookup table is
387 * probably not worthwhile just to support the relatively
388 * obscure tar->cpio conversion case.
390 static int default_inode;
391 static int default_dev;
392 struct tar *tar;
393 struct sparse_block *sp;
394 const char *p;
395 int r;
396 size_t l;
398 /* Assign default device/inode values. */
399 archive_entry_set_dev(entry, 1 + default_dev); /* Don't use zero. */
400 archive_entry_set_ino(entry, ++default_inode); /* Don't use zero. */
401 /* Limit generated st_ino number to 16 bits. */
402 if (default_inode >= 0xffff) {
403 ++default_dev;
404 default_inode = 0;
407 tar = (struct tar *)(a->format->data);
408 tar->entry_offset = 0;
409 while (tar->sparse_list != NULL) {
410 sp = tar->sparse_list;
411 tar->sparse_list = sp->next;
412 free(sp);
414 tar->sparse_last = NULL;
415 tar->realsize = -1; /* Mark this as "unset" */
417 r = tar_read_header(a, tar, entry);
420 * "non-sparse" files are really just sparse files with
421 * a single block.
423 if (tar->sparse_list == NULL)
424 gnu_add_sparse_entry(tar, 0, tar->entry_bytes_remaining);
426 if (r == ARCHIVE_OK) {
428 * "Regular" entry with trailing '/' is really
429 * directory: This is needed for certain old tar
430 * variants and even for some broken newer ones.
432 p = archive_entry_pathname(entry);
433 l = strlen(p);
434 if (archive_entry_filetype(entry) == AE_IFREG
435 && p[l-1] == '/')
436 archive_entry_set_filetype(entry, AE_IFDIR);
438 return (r);
441 static int
442 archive_read_format_tar_read_data(struct archive_read *a,
443 const void **buff, size_t *size, off_t *offset)
445 ssize_t bytes_read;
446 struct tar *tar;
447 struct sparse_block *p;
449 tar = (struct tar *)(a->format->data);
451 if (tar->sparse_gnu_pending) {
452 if (tar->sparse_gnu_major == 1 && tar->sparse_gnu_minor == 0) {
453 tar->sparse_gnu_pending = 0;
454 /* Read initial sparse map. */
455 bytes_read = gnu_sparse_10_read(a, tar);
456 tar->entry_bytes_remaining -= bytes_read;
457 if (bytes_read < 0)
458 return (bytes_read);
459 } else {
460 *size = 0;
461 *offset = 0;
462 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
463 "Unrecognized GNU sparse file format");
464 return (ARCHIVE_WARN);
466 tar->sparse_gnu_pending = 0;
469 /* Remove exhausted entries from sparse list. */
470 while (tar->sparse_list != NULL &&
471 tar->sparse_list->remaining == 0) {
472 p = tar->sparse_list;
473 tar->sparse_list = p->next;
474 free(p);
477 /* If we're at end of file, return EOF. */
478 if (tar->sparse_list == NULL || tar->entry_bytes_remaining == 0) {
479 if ((a->decompressor->skip)(a, tar->entry_padding) < 0)
480 return (ARCHIVE_FATAL);
481 tar->entry_padding = 0;
482 *buff = NULL;
483 *size = 0;
484 *offset = tar->realsize;
485 return (ARCHIVE_EOF);
488 bytes_read = (a->decompressor->read_ahead)(a, buff, 1);
489 if (bytes_read == 0) {
490 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
491 "Truncated tar archive");
492 return (ARCHIVE_FATAL);
494 if (bytes_read < 0)
495 return (ARCHIVE_FATAL);
496 if (bytes_read > tar->entry_bytes_remaining)
497 bytes_read = tar->entry_bytes_remaining;
498 /* Don't read more than is available in the
499 * current sparse block. */
500 if (tar->sparse_list->remaining < bytes_read)
501 bytes_read = tar->sparse_list->remaining;
502 *size = bytes_read;
503 *offset = tar->sparse_list->offset;
504 tar->sparse_list->remaining -= bytes_read;
505 tar->sparse_list->offset += bytes_read;
506 tar->entry_bytes_remaining -= bytes_read;
507 (a->decompressor->consume)(a, bytes_read);
508 return (ARCHIVE_OK);
511 static int
512 archive_read_format_tar_skip(struct archive_read *a)
514 off_t bytes_skipped;
515 struct tar* tar;
517 tar = (struct tar *)(a->format->data);
520 * Compression layer skip functions are required to either skip the
521 * length requested or fail, so we can rely upon the entire entry
522 * plus padding being skipped.
524 bytes_skipped = (a->decompressor->skip)(a, tar->entry_bytes_remaining +
525 tar->entry_padding);
526 if (bytes_skipped < 0)
527 return (ARCHIVE_FATAL);
529 tar->entry_bytes_remaining = 0;
530 tar->entry_padding = 0;
532 /* Free the sparse list. */
533 gnu_clear_sparse_list(tar);
535 return (ARCHIVE_OK);
539 * This function recursively interprets all of the headers associated
540 * with a single entry.
542 static int
543 tar_read_header(struct archive_read *a, struct tar *tar,
544 struct archive_entry *entry)
546 ssize_t bytes;
547 int err;
548 const void *h;
549 const struct archive_entry_header_ustar *header;
551 /* Read 512-byte header record */
552 bytes = (a->decompressor->read_ahead)(a, &h, 512);
553 if (bytes < 0)
554 return (bytes);
555 if (bytes == 0) {
557 * An archive that just ends without a proper
558 * end-of-archive marker. Yes, there are tar programs
559 * that do this; hold our nose and accept it.
561 return (ARCHIVE_EOF);
563 if (bytes < 512) {
564 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
565 "Truncated tar archive");
566 return (ARCHIVE_FATAL);
568 (a->decompressor->consume)(a, 512);
571 /* Check for end-of-archive mark. */
572 if (((*(const char *)h)==0) && archive_block_is_null((const unsigned char *)h)) {
573 /* Try to consume a second all-null record, as well. */
574 bytes = (a->decompressor->read_ahead)(a, &h, 512);
575 if (bytes > 0)
576 (a->decompressor->consume)(a, bytes);
577 archive_set_error(&a->archive, 0, NULL);
578 if (a->archive.archive_format_name == NULL) {
579 a->archive.archive_format = ARCHIVE_FORMAT_TAR;
580 a->archive.archive_format_name = "tar";
582 return (ARCHIVE_EOF);
586 * Note: If the checksum fails and we return ARCHIVE_RETRY,
587 * then the client is likely to just retry. This is a very
588 * crude way to search for the next valid header!
590 * TODO: Improve this by implementing a real header scan.
592 if (!checksum(a, h)) {
593 archive_set_error(&a->archive, EINVAL, "Damaged tar archive");
594 return (ARCHIVE_RETRY); /* Retryable: Invalid header */
597 if (++tar->header_recursion_depth > 32) {
598 archive_set_error(&a->archive, EINVAL, "Too many special headers");
599 return (ARCHIVE_WARN);
602 /* Determine the format variant. */
603 header = (const struct archive_entry_header_ustar *)h;
604 switch(header->typeflag[0]) {
605 case 'A': /* Solaris tar ACL */
606 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
607 a->archive.archive_format_name = "Solaris tar";
608 err = header_Solaris_ACL(a, tar, entry, h);
609 break;
610 case 'g': /* POSIX-standard 'g' header. */
611 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
612 a->archive.archive_format_name = "POSIX pax interchange format";
613 err = header_pax_global(a, tar, entry, h);
614 break;
615 case 'K': /* Long link name (GNU tar, others) */
616 err = header_longlink(a, tar, entry, h);
617 break;
618 case 'L': /* Long filename (GNU tar, others) */
619 err = header_longname(a, tar, entry, h);
620 break;
621 case 'V': /* GNU volume header */
622 err = header_volume(a, tar, entry, h);
623 break;
624 case 'X': /* Used by SUN tar; same as 'x'. */
625 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
626 a->archive.archive_format_name =
627 "POSIX pax interchange format (Sun variant)";
628 err = header_pax_extensions(a, tar, entry, h);
629 break;
630 case 'x': /* POSIX-standard 'x' header. */
631 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
632 a->archive.archive_format_name = "POSIX pax interchange format";
633 err = header_pax_extensions(a, tar, entry, h);
634 break;
635 default:
636 if (memcmp(header->magic, "ustar \0", 8) == 0) {
637 a->archive.archive_format = ARCHIVE_FORMAT_TAR_GNUTAR;
638 a->archive.archive_format_name = "GNU tar format";
639 err = header_gnutar(a, tar, entry, h);
640 } else if (memcmp(header->magic, "ustar", 5) == 0) {
641 if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) {
642 a->archive.archive_format = ARCHIVE_FORMAT_TAR_USTAR;
643 a->archive.archive_format_name = "POSIX ustar format";
645 err = header_ustar(a, tar, entry, h);
646 } else {
647 a->archive.archive_format = ARCHIVE_FORMAT_TAR;
648 a->archive.archive_format_name = "tar (non-POSIX)";
649 err = header_old_tar(a, tar, entry, h);
652 --tar->header_recursion_depth;
653 /* We return warnings or success as-is. Anything else is fatal. */
654 if (err == ARCHIVE_WARN || err == ARCHIVE_OK)
655 return (err);
656 if (err == ARCHIVE_EOF)
657 /* EOF when recursively reading a header is bad. */
658 archive_set_error(&a->archive, EINVAL, "Damaged tar archive");
659 return (ARCHIVE_FATAL);
663 * Return true if block checksum is correct.
665 static int
666 checksum(struct archive_read *a, const void *h)
668 const unsigned char *bytes;
669 const struct archive_entry_header_ustar *header;
670 int check, i, sum;
672 (void)a; /* UNUSED */
673 bytes = (const unsigned char *)h;
674 header = (const struct archive_entry_header_ustar *)h;
677 * Test the checksum. Note that POSIX specifies _unsigned_
678 * bytes for this calculation.
680 sum = tar_atol(header->checksum, sizeof(header->checksum));
681 check = 0;
682 for (i = 0; i < 148; i++)
683 check += (unsigned char)bytes[i];
684 for (; i < 156; i++)
685 check += 32;
686 for (; i < 512; i++)
687 check += (unsigned char)bytes[i];
688 if (sum == check)
689 return (1);
692 * Repeat test with _signed_ bytes, just in case this archive
693 * was created by an old BSD, Solaris, or HP-UX tar with a
694 * broken checksum calculation.
696 check = 0;
697 for (i = 0; i < 148; i++)
698 check += (signed char)bytes[i];
699 for (; i < 156; i++)
700 check += 32;
701 for (; i < 512; i++)
702 check += (signed char)bytes[i];
703 if (sum == check)
704 return (1);
706 return (0);
710 * Return true if this block contains only nulls.
712 static int
713 archive_block_is_null(const unsigned char *p)
715 unsigned i;
717 for (i = 0; i < ARCHIVE_BYTES_PER_RECORD / sizeof(*p); i++)
718 if (*p++)
719 return (0);
720 return (1);
724 * Interpret 'A' Solaris ACL header
726 static int
727 header_Solaris_ACL(struct archive_read *a, struct tar *tar,
728 struct archive_entry *entry, const void *h)
730 const struct archive_entry_header_ustar *header;
731 size_t size;
732 int err;
733 char *acl, *p;
734 wchar_t *wp;
737 * read_body_to_string adds a NUL terminator, but we need a little
738 * more to make sure that we don't overrun acl_text later.
740 header = (const struct archive_entry_header_ustar *)h;
741 size = tar_atol(header->size, sizeof(header->size));
742 err = read_body_to_string(a, tar, &(tar->acl_text), h);
743 if (err != ARCHIVE_OK)
744 return (err);
745 err = tar_read_header(a, tar, entry);
746 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
747 return (err);
749 /* Skip leading octal number. */
750 /* XXX TODO: Parse the octal number and sanity-check it. */
751 p = acl = tar->acl_text.s;
752 while (*p != '\0' && p < acl + size)
753 p++;
754 p++;
756 if (p >= acl + size) {
757 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
758 "Malformed Solaris ACL attribute");
759 return(ARCHIVE_WARN);
762 /* Skip leading octal number. */
763 size -= (p - acl);
764 acl = p;
766 while (*p != '\0' && p < acl + size)
767 p++;
769 wp = utf8_decode(tar, acl, p - acl);
770 err = __archive_entry_acl_parse_w(entry, wp,
771 ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
772 return (err);
776 * Interpret 'K' long linkname header.
778 static int
779 header_longlink(struct archive_read *a, struct tar *tar,
780 struct archive_entry *entry, const void *h)
782 int err;
784 err = read_body_to_string(a, tar, &(tar->longlink), h);
785 if (err != ARCHIVE_OK)
786 return (err);
787 err = tar_read_header(a, tar, entry);
788 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
789 return (err);
790 /* Set symlink if symlink already set, else hardlink. */
791 archive_entry_copy_link(entry, tar->longlink.s);
792 return (ARCHIVE_OK);
796 * Interpret 'L' long filename header.
798 static int
799 header_longname(struct archive_read *a, struct tar *tar,
800 struct archive_entry *entry, const void *h)
802 int err;
804 err = read_body_to_string(a, tar, &(tar->longname), h);
805 if (err != ARCHIVE_OK)
806 return (err);
807 /* Read and parse "real" header, then override name. */
808 err = tar_read_header(a, tar, entry);
809 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
810 return (err);
811 archive_entry_copy_pathname(entry, tar->longname.s);
812 return (ARCHIVE_OK);
817 * Interpret 'V' GNU tar volume header.
819 static int
820 header_volume(struct archive_read *a, struct tar *tar,
821 struct archive_entry *entry, const void *h)
823 (void)h;
825 /* Just skip this and read the next header. */
826 return (tar_read_header(a, tar, entry));
830 * Read body of an archive entry into an archive_string object.
832 static int
833 read_body_to_string(struct archive_read *a, struct tar *tar,
834 struct archive_string *as, const void *h)
836 off_t size, padded_size;
837 ssize_t bytes_read, bytes_to_copy;
838 const struct archive_entry_header_ustar *header;
839 const void *src;
840 char *dest;
842 (void)tar; /* UNUSED */
843 header = (const struct archive_entry_header_ustar *)h;
844 size = tar_atol(header->size, sizeof(header->size));
845 if ((size > 1048576) || (size < 0)) {
846 archive_set_error(&a->archive, EINVAL,
847 "Special header too large");
848 return (ARCHIVE_FATAL);
851 /* Fail if we can't make our buffer big enough. */
852 if (archive_string_ensure(as, size+1) == NULL) {
853 archive_set_error(&a->archive, ENOMEM,
854 "No memory");
855 return (ARCHIVE_FATAL);
858 /* Read the body into the string. */
859 padded_size = (size + 511) & ~ 511;
860 dest = as->s;
861 while (padded_size > 0) {
862 bytes_read = (a->decompressor->read_ahead)(a, &src, padded_size);
863 if (bytes_read == 0)
864 return (ARCHIVE_EOF);
865 if (bytes_read < 0)
866 return (ARCHIVE_FATAL);
867 if (bytes_read > padded_size)
868 bytes_read = padded_size;
869 (a->decompressor->consume)(a, bytes_read);
870 bytes_to_copy = bytes_read;
871 if ((off_t)bytes_to_copy > size)
872 bytes_to_copy = (ssize_t)size;
873 memcpy(dest, src, bytes_to_copy);
874 dest += bytes_to_copy;
875 size -= bytes_to_copy;
876 padded_size -= bytes_read;
878 *dest = '\0';
879 return (ARCHIVE_OK);
883 * Parse out common header elements.
885 * This would be the same as header_old_tar, except that the
886 * filename is handled slightly differently for old and POSIX
887 * entries (POSIX entries support a 'prefix'). This factoring
888 * allows header_old_tar and header_ustar
889 * to handle filenames differently, while still putting most of the
890 * common parsing into one place.
892 static int
893 header_common(struct archive_read *a, struct tar *tar,
894 struct archive_entry *entry, const void *h)
896 const struct archive_entry_header_ustar *header;
897 char tartype;
899 (void)a; /* UNUSED */
901 header = (const struct archive_entry_header_ustar *)h;
902 if (header->linkname[0])
903 archive_strncpy(&(tar->entry_linkpath), header->linkname,
904 sizeof(header->linkname));
905 else
906 archive_string_empty(&(tar->entry_linkpath));
908 /* Parse out the numeric fields (all are octal) */
909 archive_entry_set_mode(entry, tar_atol(header->mode, sizeof(header->mode)));
910 archive_entry_set_uid(entry, tar_atol(header->uid, sizeof(header->uid)));
911 archive_entry_set_gid(entry, tar_atol(header->gid, sizeof(header->gid)));
912 tar->entry_bytes_remaining = tar_atol(header->size, sizeof(header->size));
913 tar->realsize = tar->entry_bytes_remaining;
914 archive_entry_set_size(entry, tar->entry_bytes_remaining);
915 archive_entry_set_mtime(entry, tar_atol(header->mtime, sizeof(header->mtime)), 0);
917 /* Handle the tar type flag appropriately. */
918 tartype = header->typeflag[0];
920 switch (tartype) {
921 case '1': /* Hard link */
922 archive_entry_copy_hardlink(entry, tar->entry_linkpath.s);
924 * The following may seem odd, but: Technically, tar
925 * does not store the file type for a "hard link"
926 * entry, only the fact that it is a hard link. So, I
927 * leave the type zero normally. But, pax interchange
928 * format allows hard links to have data, which
929 * implies that the underlying entry is a regular
930 * file.
932 if (archive_entry_size(entry) > 0)
933 archive_entry_set_filetype(entry, AE_IFREG);
936 * A tricky point: Traditionally, tar readers have
937 * ignored the size field when reading hardlink
938 * entries, and some writers put non-zero sizes even
939 * though the body is empty. POSIX blessed this
940 * convention in the 1988 standard, but broke with
941 * this tradition in 2001 by permitting hardlink
942 * entries to store valid bodies in pax interchange
943 * format, but not in ustar format. Since there is no
944 * hard and fast way to distinguish pax interchange
945 * from earlier archives (the 'x' and 'g' entries are
946 * optional, after all), we need a heuristic.
948 if (archive_entry_size(entry) == 0) {
949 /* If the size is already zero, we're done. */
950 } else if (a->archive.archive_format
951 == ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) {
952 /* Definitely pax extended; must obey hardlink size. */
953 } else if (a->archive.archive_format == ARCHIVE_FORMAT_TAR
954 || a->archive.archive_format == ARCHIVE_FORMAT_TAR_GNUTAR)
956 /* Old-style or GNU tar: we must ignore the size. */
957 archive_entry_set_size(entry, 0);
958 tar->entry_bytes_remaining = 0;
959 } else if (archive_read_format_tar_bid(a) > 50) {
961 * We don't know if it's pax: If the bid
962 * function sees a valid ustar header
963 * immediately following, then let's ignore
964 * the hardlink size.
966 archive_entry_set_size(entry, 0);
967 tar->entry_bytes_remaining = 0;
970 * TODO: There are still two cases I'd like to handle:
971 * = a ustar non-pax archive with a hardlink entry at
972 * end-of-archive. (Look for block of nulls following?)
973 * = a pax archive that has not seen any pax headers
974 * and has an entry which is a hardlink entry storing
975 * a body containing an uncompressed tar archive.
976 * The first is worth addressing; I don't see any reliable
977 * way to deal with the second possibility.
979 break;
980 case '2': /* Symlink */
981 archive_entry_set_filetype(entry, AE_IFLNK);
982 archive_entry_set_size(entry, 0);
983 tar->entry_bytes_remaining = 0;
984 archive_entry_copy_symlink(entry, tar->entry_linkpath.s);
985 break;
986 case '3': /* Character device */
987 archive_entry_set_filetype(entry, AE_IFCHR);
988 archive_entry_set_size(entry, 0);
989 tar->entry_bytes_remaining = 0;
990 break;
991 case '4': /* Block device */
992 archive_entry_set_filetype(entry, AE_IFBLK);
993 archive_entry_set_size(entry, 0);
994 tar->entry_bytes_remaining = 0;
995 break;
996 case '5': /* Dir */
997 archive_entry_set_filetype(entry, AE_IFDIR);
998 archive_entry_set_size(entry, 0);
999 tar->entry_bytes_remaining = 0;
1000 break;
1001 case '6': /* FIFO device */
1002 archive_entry_set_filetype(entry, AE_IFIFO);
1003 archive_entry_set_size(entry, 0);
1004 tar->entry_bytes_remaining = 0;
1005 break;
1006 case 'D': /* GNU incremental directory type */
1008 * No special handling is actually required here.
1009 * It might be nice someday to preprocess the file list and
1010 * provide it to the client, though.
1012 archive_entry_set_filetype(entry, AE_IFDIR);
1013 break;
1014 case 'M': /* GNU "Multi-volume" (remainder of file from last archive)*/
1016 * As far as I can tell, this is just like a regular file
1017 * entry, except that the contents should be _appended_ to
1018 * the indicated file at the indicated offset. This may
1019 * require some API work to fully support.
1021 break;
1022 case 'N': /* Old GNU "long filename" entry. */
1023 /* The body of this entry is a script for renaming
1024 * previously-extracted entries. Ugh. It will never
1025 * be supported by libarchive. */
1026 archive_entry_set_filetype(entry, AE_IFREG);
1027 break;
1028 case 'S': /* GNU sparse files */
1030 * Sparse files are really just regular files with
1031 * sparse information in the extended area.
1033 /* FALLTHROUGH */
1034 default: /* Regular file and non-standard types */
1036 * Per POSIX: non-recognized types should always be
1037 * treated as regular files.
1039 archive_entry_set_filetype(entry, AE_IFREG);
1040 break;
1042 return (0);
1046 * Parse out header elements for "old-style" tar archives.
1048 static int
1049 header_old_tar(struct archive_read *a, struct tar *tar,
1050 struct archive_entry *entry, const void *h)
1052 const struct archive_entry_header_ustar *header;
1054 /* Copy filename over (to ensure null termination). */
1055 header = (const struct archive_entry_header_ustar *)h;
1056 archive_strncpy(&(tar->entry_pathname), header->name, sizeof(header->name));
1057 archive_entry_copy_pathname(entry, tar->entry_pathname.s);
1059 /* Grab rest of common fields */
1060 header_common(a, tar, entry, h);
1062 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1063 return (0);
1067 * Parse a file header for a pax extended archive entry.
1069 static int
1070 header_pax_global(struct archive_read *a, struct tar *tar,
1071 struct archive_entry *entry, const void *h)
1073 int err;
1075 err = read_body_to_string(a, tar, &(tar->pax_global), h);
1076 if (err != ARCHIVE_OK)
1077 return (err);
1078 err = tar_read_header(a, tar, entry);
1079 return (err);
1082 static int
1083 header_pax_extensions(struct archive_read *a, struct tar *tar,
1084 struct archive_entry *entry, const void *h)
1086 int err, err2;
1088 err = read_body_to_string(a, tar, &(tar->pax_header), h);
1089 if (err != ARCHIVE_OK)
1090 return (err);
1092 /* Parse the next header. */
1093 err = tar_read_header(a, tar, entry);
1094 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN))
1095 return (err);
1098 * TODO: Parse global/default options into 'entry' struct here
1099 * before handling file-specific options.
1101 * This design (parse standard header, then overwrite with pax
1102 * extended attribute data) usually works well, but isn't ideal;
1103 * it would be better to parse the pax extended attributes first
1104 * and then skip any fields in the standard header that were
1105 * defined in the pax header.
1107 err2 = pax_header(a, tar, entry, tar->pax_header.s);
1108 err = err_combine(err, err2);
1109 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1110 return (err);
1115 * Parse a file header for a Posix "ustar" archive entry. This also
1116 * handles "pax" or "extended ustar" entries.
1118 static int
1119 header_ustar(struct archive_read *a, struct tar *tar,
1120 struct archive_entry *entry, const void *h)
1122 const struct archive_entry_header_ustar *header;
1123 struct archive_string *as;
1125 header = (const struct archive_entry_header_ustar *)h;
1127 /* Copy name into an internal buffer to ensure null-termination. */
1128 as = &(tar->entry_pathname);
1129 if (header->prefix[0]) {
1130 archive_strncpy(as, header->prefix, sizeof(header->prefix));
1131 if (as->s[archive_strlen(as) - 1] != '/')
1132 archive_strappend_char(as, '/');
1133 archive_strncat(as, header->name, sizeof(header->name));
1134 } else
1135 archive_strncpy(as, header->name, sizeof(header->name));
1137 archive_entry_copy_pathname(entry, as->s);
1139 /* Handle rest of common fields. */
1140 header_common(a, tar, entry, h);
1142 /* Handle POSIX ustar fields. */
1143 archive_strncpy(&(tar->entry_uname), header->uname,
1144 sizeof(header->uname));
1145 archive_entry_copy_uname(entry, tar->entry_uname.s);
1147 archive_strncpy(&(tar->entry_gname), header->gname,
1148 sizeof(header->gname));
1149 archive_entry_copy_gname(entry, tar->entry_gname.s);
1151 /* Parse out device numbers only for char and block specials. */
1152 if (header->typeflag[0] == '3' || header->typeflag[0] == '4') {
1153 archive_entry_set_rdevmajor(entry,
1154 tar_atol(header->rdevmajor, sizeof(header->rdevmajor)));
1155 archive_entry_set_rdevminor(entry,
1156 tar_atol(header->rdevminor, sizeof(header->rdevminor)));
1159 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1161 return (0);
1166 * Parse the pax extended attributes record.
1168 * Returns non-zero if there's an error in the data.
1170 static int
1171 pax_header(struct archive_read *a, struct tar *tar,
1172 struct archive_entry *entry, char *attr)
1174 size_t attr_length, l, line_length;
1175 char *line, *p;
1176 char *key, *value;
1177 wchar_t *wp;
1178 int err, err2;
1180 attr_length = strlen(attr);
1181 tar->pax_hdrcharset_binary = 0;
1182 archive_string_empty(&(tar->entry_gname));
1183 archive_string_empty(&(tar->entry_linkpath));
1184 archive_string_empty(&(tar->entry_pathname));
1185 archive_string_empty(&(tar->entry_uname));
1186 err = ARCHIVE_OK;
1187 while (attr_length > 0) {
1188 /* Parse decimal length field at start of line. */
1189 line_length = 0;
1190 l = attr_length;
1191 line = p = attr; /* Record start of line. */
1192 while (l>0) {
1193 if (*p == ' ') {
1194 p++;
1195 l--;
1196 break;
1198 if (*p < '0' || *p > '9') {
1199 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1200 "Ignoring malformed pax extended attributes");
1201 return (ARCHIVE_WARN);
1203 line_length *= 10;
1204 line_length += *p - '0';
1205 if (line_length > 999999) {
1206 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1207 "Rejecting pax extended attribute > 1MB");
1208 return (ARCHIVE_WARN);
1210 p++;
1211 l--;
1215 * Parsed length must be no bigger than available data,
1216 * at least 1, and the last character of the line must
1217 * be '\n'.
1219 if (line_length > attr_length
1220 || line_length < 1
1221 || attr[line_length - 1] != '\n')
1223 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1224 "Ignoring malformed pax extended attribute");
1225 return (ARCHIVE_WARN);
1228 /* Null-terminate the line. */
1229 attr[line_length - 1] = '\0';
1231 /* Find end of key and null terminate it. */
1232 key = p;
1233 if (key[0] == '=')
1234 return (-1);
1235 while (*p && *p != '=')
1236 ++p;
1237 if (*p == '\0') {
1238 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
1239 "Invalid pax extended attributes");
1240 return (ARCHIVE_WARN);
1242 *p = '\0';
1244 /* Identify null-terminated 'value' portion. */
1245 value = p + 1;
1247 /* Identify this attribute and set it in the entry. */
1248 err2 = pax_attribute(tar, entry, key, value);
1249 err = err_combine(err, err2);
1251 /* Skip to next line */
1252 attr += line_length;
1253 attr_length -= line_length;
1255 if (archive_strlen(&(tar->entry_gname)) > 0) {
1256 value = tar->entry_gname.s;
1257 if (tar->pax_hdrcharset_binary)
1258 archive_entry_copy_gname(entry, value);
1259 else {
1260 wp = utf8_decode(tar, value, strlen(value));
1261 if (wp == NULL)
1262 archive_entry_copy_gname(entry, value);
1263 else
1264 archive_entry_copy_gname_w(entry, wp);
1267 if (archive_strlen(&(tar->entry_linkpath)) > 0) {
1268 value = tar->entry_linkpath.s;
1269 if (tar->pax_hdrcharset_binary)
1270 archive_entry_copy_link(entry, value);
1271 else {
1272 wp = utf8_decode(tar, value, strlen(value));
1273 if (wp == NULL)
1274 archive_entry_copy_link(entry, value);
1275 else
1276 archive_entry_copy_link_w(entry, wp);
1279 if (archive_strlen(&(tar->entry_pathname)) > 0) {
1280 value = tar->entry_pathname.s;
1281 if (tar->pax_hdrcharset_binary)
1282 archive_entry_copy_pathname(entry, value);
1283 else {
1284 wp = utf8_decode(tar, value, strlen(value));
1285 if (wp == NULL)
1286 archive_entry_copy_pathname(entry, value);
1287 else
1288 archive_entry_copy_pathname_w(entry, wp);
1291 if (archive_strlen(&(tar->entry_uname)) > 0) {
1292 value = tar->entry_uname.s;
1293 if (tar->pax_hdrcharset_binary)
1294 archive_entry_copy_uname(entry, value);
1295 else {
1296 wp = utf8_decode(tar, value, strlen(value));
1297 if (wp == NULL)
1298 archive_entry_copy_uname(entry, value);
1299 else
1300 archive_entry_copy_uname_w(entry, wp);
1303 return (err);
1306 static int
1307 pax_attribute_xattr(struct archive_entry *entry,
1308 char *name, char *value)
1310 char *name_decoded;
1311 void *value_decoded;
1312 size_t value_len;
1314 if (strlen(name) < 18 || (strncmp(name, "LIBARCHIVE.xattr.", 17)) != 0)
1315 return 3;
1317 name += 17;
1319 /* URL-decode name */
1320 name_decoded = url_decode(name);
1321 if (name_decoded == NULL)
1322 return 2;
1324 /* Base-64 decode value */
1325 value_decoded = base64_decode(value, strlen(value), &value_len);
1326 if (value_decoded == NULL) {
1327 free(name_decoded);
1328 return 1;
1331 archive_entry_xattr_add_entry(entry, name_decoded,
1332 value_decoded, value_len);
1334 free(name_decoded);
1335 free(value_decoded);
1336 return 0;
1340 * Parse a single key=value attribute. key/value pointers are
1341 * assumed to point into reasonably long-lived storage.
1343 * Note that POSIX reserves all-lowercase keywords. Vendor-specific
1344 * extensions should always have keywords of the form "VENDOR.attribute"
1345 * In particular, it's quite feasible to support many different
1346 * vendor extensions here. I'm using "LIBARCHIVE" for extensions
1347 * unique to this library.
1349 * Investigate other vendor-specific extensions and see if
1350 * any of them look useful.
1352 static int
1353 pax_attribute(struct tar *tar, struct archive_entry *entry,
1354 char *key, char *value)
1356 int64_t s;
1357 long n;
1358 wchar_t *wp;
1360 switch (key[0]) {
1361 case 'G':
1362 /* GNU "0.0" sparse pax format. */
1363 if (strcmp(key, "GNU.sparse.numblocks") == 0) {
1364 tar->sparse_offset = -1;
1365 tar->sparse_numbytes = -1;
1366 tar->sparse_gnu_major = 0;
1367 tar->sparse_gnu_minor = 0;
1369 if (strcmp(key, "GNU.sparse.offset") == 0) {
1370 tar->sparse_offset = tar_atol10(value, strlen(value));
1371 if (tar->sparse_numbytes != -1) {
1372 gnu_add_sparse_entry(tar,
1373 tar->sparse_offset, tar->sparse_numbytes);
1374 tar->sparse_offset = -1;
1375 tar->sparse_numbytes = -1;
1378 if (strcmp(key, "GNU.sparse.numbytes") == 0) {
1379 tar->sparse_numbytes = tar_atol10(value, strlen(value));
1380 if (tar->sparse_numbytes != -1) {
1381 gnu_add_sparse_entry(tar,
1382 tar->sparse_offset, tar->sparse_numbytes);
1383 tar->sparse_offset = -1;
1384 tar->sparse_numbytes = -1;
1387 if (strcmp(key, "GNU.sparse.size") == 0) {
1388 tar->realsize = tar_atol10(value, strlen(value));
1389 archive_entry_set_size(entry, tar->realsize);
1392 /* GNU "0.1" sparse pax format. */
1393 if (strcmp(key, "GNU.sparse.map") == 0) {
1394 tar->sparse_gnu_major = 0;
1395 tar->sparse_gnu_minor = 1;
1396 if (gnu_sparse_01_parse(tar, value) != ARCHIVE_OK)
1397 return (ARCHIVE_WARN);
1400 /* GNU "1.0" sparse pax format */
1401 if (strcmp(key, "GNU.sparse.major") == 0) {
1402 tar->sparse_gnu_major = tar_atol10(value, strlen(value));
1403 tar->sparse_gnu_pending = 1;
1405 if (strcmp(key, "GNU.sparse.minor") == 0) {
1406 tar->sparse_gnu_minor = tar_atol10(value, strlen(value));
1407 tar->sparse_gnu_pending = 1;
1409 if (strcmp(key, "GNU.sparse.name") == 0) {
1410 wp = utf8_decode(tar, value, strlen(value));
1411 if (wp != NULL)
1412 archive_entry_copy_pathname_w(entry, wp);
1413 else
1414 archive_entry_copy_pathname(entry, value);
1416 if (strcmp(key, "GNU.sparse.realsize") == 0) {
1417 tar->realsize = tar_atol10(value, strlen(value));
1418 archive_entry_set_size(entry, tar->realsize);
1420 break;
1421 case 'L':
1422 /* Our extensions */
1423 /* TODO: Handle arbitrary extended attributes... */
1425 if (strcmp(key, "LIBARCHIVE.xxxxxxx")==0)
1426 archive_entry_set_xxxxxx(entry, value);
1428 if (strncmp(key, "LIBARCHIVE.xattr.", 17)==0)
1429 pax_attribute_xattr(entry, key, value);
1430 break;
1431 case 'S':
1432 /* We support some keys used by the "star" archiver */
1433 if (strcmp(key, "SCHILY.acl.access")==0) {
1434 wp = utf8_decode(tar, value, strlen(value));
1435 /* TODO: if (wp == NULL) */
1436 __archive_entry_acl_parse_w(entry, wp,
1437 ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
1438 } else if (strcmp(key, "SCHILY.acl.default")==0) {
1439 wp = utf8_decode(tar, value, strlen(value));
1440 /* TODO: if (wp == NULL) */
1441 __archive_entry_acl_parse_w(entry, wp,
1442 ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
1443 } else if (strcmp(key, "SCHILY.devmajor")==0) {
1444 archive_entry_set_rdevmajor(entry,
1445 tar_atol10(value, strlen(value)));
1446 } else if (strcmp(key, "SCHILY.devminor")==0) {
1447 archive_entry_set_rdevminor(entry,
1448 tar_atol10(value, strlen(value)));
1449 } else if (strcmp(key, "SCHILY.fflags")==0) {
1450 wp = utf8_decode(tar, value, strlen(value));
1451 /* TODO: if (wp == NULL) */
1452 archive_entry_copy_fflags_text_w(entry, wp);
1453 } else if (strcmp(key, "SCHILY.dev")==0) {
1454 archive_entry_set_dev(entry,
1455 tar_atol10(value, strlen(value)));
1456 } else if (strcmp(key, "SCHILY.ino")==0) {
1457 archive_entry_set_ino(entry,
1458 tar_atol10(value, strlen(value)));
1459 } else if (strcmp(key, "SCHILY.nlink")==0) {
1460 archive_entry_set_nlink(entry,
1461 tar_atol10(value, strlen(value)));
1462 } else if (strcmp(key, "SCHILY.realsize")==0) {
1463 tar->realsize = tar_atol10(value, strlen(value));
1464 archive_entry_set_size(entry, tar->realsize);
1466 break;
1467 case 'a':
1468 if (strcmp(key, "atime")==0) {
1469 pax_time(value, &s, &n);
1470 archive_entry_set_atime(entry, s, n);
1472 break;
1473 case 'c':
1474 if (strcmp(key, "ctime")==0) {
1475 pax_time(value, &s, &n);
1476 archive_entry_set_ctime(entry, s, n);
1477 } else if (strcmp(key, "charset")==0) {
1478 /* TODO: Publish charset information in entry. */
1479 } else if (strcmp(key, "comment")==0) {
1480 /* TODO: Publish comment in entry. */
1482 break;
1483 case 'g':
1484 if (strcmp(key, "gid")==0) {
1485 archive_entry_set_gid(entry,
1486 tar_atol10(value, strlen(value)));
1487 } else if (strcmp(key, "gname")==0) {
1488 archive_strcpy(&(tar->entry_gname), value);
1490 break;
1491 case 'h':
1492 if (strcmp(key, "hdrcharset") == 0) {
1493 if (strcmp(value, "BINARY") == 0)
1494 tar->pax_hdrcharset_binary = 1;
1495 else if (strcmp(value, "ISO-IR 10646 2000 UTF-8") == 0)
1496 tar->pax_hdrcharset_binary = 0;
1497 else {
1498 /* TODO: Warn about unsupported hdrcharset */
1501 break;
1502 case 'l':
1503 /* pax interchange doesn't distinguish hardlink vs. symlink. */
1504 if (strcmp(key, "linkpath")==0) {
1505 archive_strcpy(&(tar->entry_linkpath), value);
1507 break;
1508 case 'm':
1509 if (strcmp(key, "mtime")==0) {
1510 pax_time(value, &s, &n);
1511 archive_entry_set_mtime(entry, s, n);
1513 break;
1514 case 'p':
1515 if (strcmp(key, "path")==0) {
1516 archive_strcpy(&(tar->entry_pathname), value);
1518 break;
1519 case 'r':
1520 /* POSIX has reserved 'realtime.*' */
1521 break;
1522 case 's':
1523 /* POSIX has reserved 'security.*' */
1524 /* Someday: if (strcmp(key, "security.acl")==0) { ... } */
1525 if (strcmp(key, "size")==0) {
1526 /* "size" is the size of the data in the entry. */
1527 tar->entry_bytes_remaining
1528 = tar_atol10(value, strlen(value));
1530 * But, "size" is not necessarily the size of
1531 * the file on disk; if this is a sparse file,
1532 * the disk size may have already been set from
1533 * GNU.sparse.realsize or GNU.sparse.size or
1534 * an old GNU header field or SCHILY.realsize
1535 * or ....
1537 if (tar->realsize < 0) {
1538 archive_entry_set_size(entry,
1539 tar->entry_bytes_remaining);
1540 tar->realsize
1541 = tar->entry_bytes_remaining;
1544 break;
1545 case 'u':
1546 if (strcmp(key, "uid")==0) {
1547 archive_entry_set_uid(entry,
1548 tar_atol10(value, strlen(value)));
1549 } else if (strcmp(key, "uname")==0) {
1550 archive_strcpy(&(tar->entry_uname), value);
1552 break;
1554 return (0);
1560 * parse a decimal time value, which may include a fractional portion
1562 static void
1563 pax_time(const char *p, int64_t *ps, long *pn)
1565 char digit;
1566 int64_t s;
1567 unsigned long l;
1568 int sign;
1569 int64_t limit, last_digit_limit;
1571 limit = INT64_MAX / 10;
1572 last_digit_limit = INT64_MAX % 10;
1574 s = 0;
1575 sign = 1;
1576 if (*p == '-') {
1577 sign = -1;
1578 p++;
1580 while (*p >= '0' && *p <= '9') {
1581 digit = *p - '0';
1582 if (s > limit ||
1583 (s == limit && digit > last_digit_limit)) {
1584 s = UINT64_MAX;
1585 break;
1587 s = (s * 10) + digit;
1588 ++p;
1591 *ps = s * sign;
1593 /* Calculate nanoseconds. */
1594 *pn = 0;
1596 if (*p != '.')
1597 return;
1599 l = 100000000UL;
1600 do {
1601 ++p;
1602 if (*p >= '0' && *p <= '9')
1603 *pn += (*p - '0') * l;
1604 else
1605 break;
1606 } while (l /= 10);
1610 * Parse GNU tar header
1612 static int
1613 header_gnutar(struct archive_read *a, struct tar *tar,
1614 struct archive_entry *entry, const void *h)
1616 const struct archive_entry_header_gnutar *header;
1618 (void)a;
1621 * GNU header is like POSIX ustar, except 'prefix' is
1622 * replaced with some other fields. This also means the
1623 * filename is stored as in old-style archives.
1626 /* Grab fields common to all tar variants. */
1627 header_common(a, tar, entry, h);
1629 /* Copy filename over (to ensure null termination). */
1630 header = (const struct archive_entry_header_gnutar *)h;
1631 archive_strncpy(&(tar->entry_pathname), header->name,
1632 sizeof(header->name));
1633 archive_entry_copy_pathname(entry, tar->entry_pathname.s);
1635 /* Fields common to ustar and GNU */
1636 /* XXX Can the following be factored out since it's common
1637 * to ustar and gnu tar? Is it okay to move it down into
1638 * header_common, perhaps? */
1639 archive_strncpy(&(tar->entry_uname),
1640 header->uname, sizeof(header->uname));
1641 archive_entry_copy_uname(entry, tar->entry_uname.s);
1643 archive_strncpy(&(tar->entry_gname),
1644 header->gname, sizeof(header->gname));
1645 archive_entry_copy_gname(entry, tar->entry_gname.s);
1647 /* Parse out device numbers only for char and block specials */
1648 if (header->typeflag[0] == '3' || header->typeflag[0] == '4') {
1649 archive_entry_set_rdevmajor(entry,
1650 tar_atol(header->rdevmajor, sizeof(header->rdevmajor)));
1651 archive_entry_set_rdevminor(entry,
1652 tar_atol(header->rdevminor, sizeof(header->rdevminor)));
1653 } else
1654 archive_entry_set_rdev(entry, 0);
1656 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
1658 /* Grab GNU-specific fields. */
1659 archive_entry_set_atime(entry,
1660 tar_atol(header->atime, sizeof(header->atime)), 0);
1661 archive_entry_set_ctime(entry,
1662 tar_atol(header->ctime, sizeof(header->ctime)), 0);
1663 if (header->realsize[0] != 0) {
1664 tar->realsize
1665 = tar_atol(header->realsize, sizeof(header->realsize));
1666 archive_entry_set_size(entry, tar->realsize);
1669 if (header->sparse[0].offset[0] != 0) {
1670 gnu_sparse_old_read(a, tar, header);
1671 } else {
1672 if (header->isextended[0] != 0) {
1673 /* XXX WTF? XXX */
1677 return (0);
1680 static void
1681 gnu_add_sparse_entry(struct tar *tar, off_t offset, off_t remaining)
1683 struct sparse_block *p;
1685 p = (struct sparse_block *)malloc(sizeof(*p));
1686 if (p == NULL)
1687 __archive_errx(1, "Out of memory");
1688 memset(p, 0, sizeof(*p));
1689 if (tar->sparse_last != NULL)
1690 tar->sparse_last->next = p;
1691 else
1692 tar->sparse_list = p;
1693 tar->sparse_last = p;
1694 p->offset = offset;
1695 p->remaining = remaining;
1698 static void
1699 gnu_clear_sparse_list(struct tar *tar)
1701 struct sparse_block *p;
1703 while (tar->sparse_list != NULL) {
1704 p = tar->sparse_list;
1705 tar->sparse_list = p->next;
1706 free(p);
1708 tar->sparse_last = NULL;
1712 * GNU tar old-format sparse data.
1714 * GNU old-format sparse data is stored in a fixed-field
1715 * format. Offset/size values are 11-byte octal fields (same
1716 * format as 'size' field in ustart header). These are
1717 * stored in the header, allocating subsequent header blocks
1718 * as needed. Extending the header in this way is a pretty
1719 * severe POSIX violation; this design has earned GNU tar a
1720 * lot of criticism.
1723 static int
1724 gnu_sparse_old_read(struct archive_read *a, struct tar *tar,
1725 const struct archive_entry_header_gnutar *header)
1727 ssize_t bytes_read;
1728 const void *data;
1729 struct extended {
1730 struct gnu_sparse sparse[21];
1731 char isextended[1];
1732 char padding[7];
1734 const struct extended *ext;
1736 gnu_sparse_old_parse(tar, header->sparse, 4);
1737 if (header->isextended[0] == 0)
1738 return (ARCHIVE_OK);
1740 do {
1741 bytes_read = (a->decompressor->read_ahead)(a, &data, 512);
1742 if (bytes_read < 0)
1743 return (ARCHIVE_FATAL);
1744 if (bytes_read < 512) {
1745 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT,
1746 "Truncated tar archive "
1747 "detected while reading sparse file data");
1748 return (ARCHIVE_FATAL);
1750 (a->decompressor->consume)(a, 512);
1751 ext = (const struct extended *)data;
1752 gnu_sparse_old_parse(tar, ext->sparse, 21);
1753 } while (ext->isextended[0] != 0);
1754 if (tar->sparse_list != NULL)
1755 tar->entry_offset = tar->sparse_list->offset;
1756 return (ARCHIVE_OK);
1759 static void
1760 gnu_sparse_old_parse(struct tar *tar,
1761 const struct gnu_sparse *sparse, int length)
1763 while (length > 0 && sparse->offset[0] != 0) {
1764 gnu_add_sparse_entry(tar,
1765 tar_atol(sparse->offset, sizeof(sparse->offset)),
1766 tar_atol(sparse->numbytes, sizeof(sparse->numbytes)));
1767 sparse++;
1768 length--;
1773 * GNU tar sparse format 0.0
1775 * Beginning with GNU tar 1.15, sparse files are stored using
1776 * information in the pax extended header. The GNU tar maintainers
1777 * have gone through a number of variations in the process of working
1778 * out this scheme; furtunately, they're all numbered.
1780 * Sparse format 0.0 uses attribute GNU.sparse.numblocks to store the
1781 * number of blocks, and GNU.sparse.offset/GNU.sparse.numbytes to
1782 * store offset/size for each block. The repeated instances of these
1783 * latter fields violate the pax specification (which frowns on
1784 * duplicate keys), so this format was quickly replaced.
1788 * GNU tar sparse format 0.1
1790 * This version replaced the offset/numbytes attributes with
1791 * a single "map" attribute that stored a list of integers. This
1792 * format had two problems: First, the "map" attribute could be very
1793 * long, which caused problems for some implementations. More
1794 * importantly, the sparse data was lost when extracted by archivers
1795 * that didn't recognize this extension.
1798 static int
1799 gnu_sparse_01_parse(struct tar *tar, const char *p)
1801 const char *e;
1802 off_t offset = -1, size = -1;
1804 for (;;) {
1805 e = p;
1806 while (*e != '\0' && *e != ',') {
1807 if (*e < '0' || *e > '9')
1808 return (ARCHIVE_WARN);
1809 e++;
1811 if (offset < 0) {
1812 offset = tar_atol10(p, e - p);
1813 if (offset < 0)
1814 return (ARCHIVE_WARN);
1815 } else {
1816 size = tar_atol10(p, e - p);
1817 if (size < 0)
1818 return (ARCHIVE_WARN);
1819 gnu_add_sparse_entry(tar, offset, size);
1820 offset = -1;
1822 if (*e == '\0')
1823 return (ARCHIVE_OK);
1824 p = e + 1;
1829 * GNU tar sparse format 1.0
1831 * The idea: The offset/size data is stored as a series of base-10
1832 * ASCII numbers prepended to the file data, so that dearchivers that
1833 * don't support this format will extract the block map along with the
1834 * data and a separate post-process can restore the sparseness.
1836 * Unfortunately, GNU tar 1.16 had a bug that added unnecessary
1837 * padding to the body of the file when using this format. GNU tar
1838 * 1.17 corrected this bug without bumping the version number, so
1839 * it's not possible to support both variants. This code supports
1840 * the later variant at the expense of not supporting the former.
1842 * This variant also replaced GNU.sparse.size with GNU.sparse.realsize
1843 * and introduced the GNU.sparse.major/GNU.sparse.minor attributes.
1847 * Read the next line from the input, and parse it as a decimal
1848 * integer followed by '\n'. Returns positive integer value or
1849 * negative on error.
1851 static int64_t
1852 gnu_sparse_10_atol(struct archive_read *a, struct tar *tar,
1853 ssize_t *remaining)
1855 int64_t l, limit, last_digit_limit;
1856 const char *p;
1857 ssize_t bytes_read;
1858 int base, digit;
1860 base = 10;
1861 limit = INT64_MAX / base;
1862 last_digit_limit = INT64_MAX % base;
1865 * Skip any lines starting with '#'; GNU tar specs
1866 * don't require this, but they should.
1868 do {
1869 bytes_read = readline(a, tar, &p, tar_min(*remaining, 100));
1870 if (bytes_read <= 0)
1871 return (ARCHIVE_FATAL);
1872 *remaining -= bytes_read;
1873 } while (p[0] == '#');
1875 l = 0;
1876 while (bytes_read > 0) {
1877 if (*p == '\n')
1878 return (l);
1879 if (*p < '0' || *p >= '0' + base)
1880 return (ARCHIVE_WARN);
1881 digit = *p - '0';
1882 if (l > limit || (l == limit && digit > last_digit_limit))
1883 l = UINT64_MAX; /* Truncate on overflow. */
1884 else
1885 l = (l * base) + digit;
1886 p++;
1887 bytes_read--;
1889 /* TODO: Error message. */
1890 return (ARCHIVE_WARN);
1894 * Returns length (in bytes) of the sparse data description
1895 * that was read.
1897 static ssize_t
1898 gnu_sparse_10_read(struct archive_read *a, struct tar *tar)
1900 ssize_t remaining, bytes_read;
1901 int entries;
1902 off_t offset, size, to_skip;
1904 /* Clear out the existing sparse list. */
1905 gnu_clear_sparse_list(tar);
1907 remaining = tar->entry_bytes_remaining;
1909 /* Parse entries. */
1910 entries = gnu_sparse_10_atol(a, tar, &remaining);
1911 if (entries < 0)
1912 return (ARCHIVE_FATAL);
1913 /* Parse the individual entries. */
1914 while (entries-- > 0) {
1915 /* Parse offset/size */
1916 offset = gnu_sparse_10_atol(a, tar, &remaining);
1917 if (offset < 0)
1918 return (ARCHIVE_FATAL);
1919 size = gnu_sparse_10_atol(a, tar, &remaining);
1920 if (size < 0)
1921 return (ARCHIVE_FATAL);
1922 /* Add a new sparse entry. */
1923 gnu_add_sparse_entry(tar, offset, size);
1925 /* Skip rest of block... */
1926 bytes_read = tar->entry_bytes_remaining - remaining;
1927 to_skip = 0x1ff & -bytes_read;
1928 if (to_skip != (a->decompressor->skip)(a, to_skip))
1929 return (ARCHIVE_FATAL);
1930 return (bytes_read + to_skip);
1934 * Convert text->integer.
1936 * Traditional tar formats (including POSIX) specify base-8 for
1937 * all of the standard numeric fields. This is a significant limitation
1938 * in practice:
1939 * = file size is limited to 8GB
1940 * = rdevmajor and rdevminor are limited to 21 bits
1941 * = uid/gid are limited to 21 bits
1943 * There are two workarounds for this:
1944 * = pax extended headers, which use variable-length string fields
1945 * = GNU tar and STAR both allow either base-8 or base-256 in
1946 * most fields. The high bit is set to indicate base-256.
1948 * On read, this implementation supports both extensions.
1950 static int64_t
1951 tar_atol(const char *p, unsigned char_cnt)
1954 * Technically, GNU tar considers a field to be in base-256
1955 * only if the first byte is 0xff or 0x80.
1957 if (*p & 0x80)
1958 return (tar_atol256(p, char_cnt));
1959 return (tar_atol8(p, char_cnt));
1963 * Note that this implementation does not (and should not!) obey
1964 * locale settings; you cannot simply substitute strtol here, since
1965 * it does obey locale.
1967 static int64_t
1968 tar_atol8(const char *p, unsigned char_cnt)
1970 int64_t l, limit, last_digit_limit;
1971 int digit, sign, base;
1973 base = 8;
1974 limit = INT64_MAX / base;
1975 last_digit_limit = INT64_MAX % base;
1977 while (*p == ' ' || *p == '\t')
1978 p++;
1979 if (*p == '-') {
1980 sign = -1;
1981 p++;
1982 } else
1983 sign = 1;
1985 l = 0;
1986 digit = *p - '0';
1987 while (digit >= 0 && digit < base && char_cnt-- > 0) {
1988 if (l>limit || (l == limit && digit > last_digit_limit)) {
1989 l = UINT64_MAX; /* Truncate on overflow. */
1990 break;
1992 l = (l * base) + digit;
1993 digit = *++p - '0';
1995 return (sign < 0) ? -l : l;
1999 * Note that this implementation does not (and should not!) obey
2000 * locale settings; you cannot simply substitute strtol here, since
2001 * it does obey locale.
2003 static int64_t
2004 tar_atol10(const char *p, unsigned char_cnt)
2006 int64_t l, limit, last_digit_limit;
2007 int base, digit, sign;
2009 base = 10;
2010 limit = INT64_MAX / base;
2011 last_digit_limit = INT64_MAX % base;
2013 while (*p == ' ' || *p == '\t')
2014 p++;
2015 if (*p == '-') {
2016 sign = -1;
2017 p++;
2018 } else
2019 sign = 1;
2021 l = 0;
2022 digit = *p - '0';
2023 while (digit >= 0 && digit < base && char_cnt-- > 0) {
2024 if (l > limit || (l == limit && digit > last_digit_limit)) {
2025 l = UINT64_MAX; /* Truncate on overflow. */
2026 break;
2028 l = (l * base) + digit;
2029 digit = *++p - '0';
2031 return (sign < 0) ? -l : l;
2035 * Parse a base-256 integer. This is just a straight signed binary
2036 * value in big-endian order, except that the high-order bit is
2037 * ignored.
2039 static int64_t
2040 tar_atol256(const char *_p, unsigned char_cnt)
2042 int64_t l, upper_limit, lower_limit;
2043 const unsigned char *p = (const unsigned char *)_p;
2045 upper_limit = INT64_MAX / 256;
2046 lower_limit = INT64_MIN / 256;
2048 /* Pad with 1 or 0 bits, depending on sign. */
2049 if ((0x40 & *p) == 0x40)
2050 l = (int64_t)-1;
2051 else
2052 l = 0;
2053 l = (l << 6) | (0x3f & *p++);
2054 while (--char_cnt > 0) {
2055 if (l > upper_limit) {
2056 l = INT64_MAX; /* Truncate on overflow */
2057 break;
2058 } else if (l < lower_limit) {
2059 l = INT64_MIN;
2060 break;
2062 l = (l << 8) | (0xff & (int64_t)*p++);
2064 return (l);
2068 * Returns length of line (including trailing newline)
2069 * or negative on error. 'start' argument is updated to
2070 * point to first character of line. This avoids copying
2071 * when possible.
2073 static ssize_t
2074 readline(struct archive_read *a, struct tar *tar, const char **start,
2075 ssize_t limit)
2077 ssize_t bytes_read;
2078 ssize_t total_size = 0;
2079 const void *t;
2080 const char *s;
2081 void *p;
2083 bytes_read = (a->decompressor->read_ahead)(a, &t, 1);
2084 if (bytes_read <= 0)
2085 return (ARCHIVE_FATAL);
2086 s = t; /* Start of line? */
2087 p = memchr(t, '\n', bytes_read);
2088 /* If we found '\n' in the read buffer, return pointer to that. */
2089 if (p != NULL) {
2090 bytes_read = 1 + ((const char *)p) - s;
2091 if (bytes_read > limit) {
2092 archive_set_error(&a->archive,
2093 ARCHIVE_ERRNO_FILE_FORMAT,
2094 "Line too long");
2095 return (ARCHIVE_FATAL);
2097 (a->decompressor->consume)(a, bytes_read);
2098 *start = s;
2099 return (bytes_read);
2101 /* Otherwise, we need to accumulate in a line buffer. */
2102 for (;;) {
2103 if (total_size + bytes_read > limit) {
2104 archive_set_error(&a->archive,
2105 ARCHIVE_ERRNO_FILE_FORMAT,
2106 "Line too long");
2107 return (ARCHIVE_FATAL);
2109 if (archive_string_ensure(&tar->line, total_size + bytes_read) == NULL) {
2110 archive_set_error(&a->archive, ENOMEM,
2111 "Can't allocate working buffer");
2112 return (ARCHIVE_FATAL);
2114 memcpy(tar->line.s + total_size, t, bytes_read);
2115 (a->decompressor->consume)(a, bytes_read);
2116 total_size += bytes_read;
2117 /* If we found '\n', clean up and return. */
2118 if (p != NULL) {
2119 *start = tar->line.s;
2120 return (total_size);
2122 /* Read some more. */
2123 bytes_read = (a->decompressor->read_ahead)(a, &t, 1);
2124 if (bytes_read <= 0)
2125 return (ARCHIVE_FATAL);
2126 s = t; /* Start of line? */
2127 p = memchr(t, '\n', bytes_read);
2128 /* If we found '\n', trim the read. */
2129 if (p != NULL) {
2130 bytes_read = 1 + ((const char *)p) - s;
2135 static wchar_t *
2136 utf8_decode(struct tar *tar, const char *src, size_t length)
2138 wchar_t *dest;
2139 ssize_t n;
2140 int err;
2142 /* Ensure pax_entry buffer is big enough. */
2143 if (tar->pax_entry_length <= length) {
2144 wchar_t *old_entry = tar->pax_entry;
2146 if (tar->pax_entry_length <= 0)
2147 tar->pax_entry_length = 1024;
2148 while (tar->pax_entry_length <= length + 1)
2149 tar->pax_entry_length *= 2;
2151 old_entry = tar->pax_entry;
2152 tar->pax_entry = (wchar_t *)realloc(tar->pax_entry,
2153 tar->pax_entry_length * sizeof(wchar_t));
2154 if (tar->pax_entry == NULL) {
2155 free(old_entry);
2156 /* TODO: Handle this error. */
2157 return (NULL);
2161 dest = tar->pax_entry;
2162 err = 0;
2163 while (length > 0) {
2164 n = UTF8_mbrtowc(dest, src, length);
2165 if (n < 0)
2166 return (NULL);
2167 if (n == 0)
2168 break;
2169 dest++;
2170 src += n;
2171 length -= n;
2173 *dest++ = L'\0';
2174 return (tar->pax_entry);
2178 * Copied and simplified from FreeBSD libc/locale.
2180 static ssize_t
2181 UTF8_mbrtowc(wchar_t *pwc, const char *s, size_t n)
2183 int ch, i, len, mask;
2184 unsigned long wch;
2186 if (s == NULL || n == 0 || pwc == NULL)
2187 return (0);
2190 * Determine the number of octets that make up this character from
2191 * the first octet, and a mask that extracts the interesting bits of
2192 * the first octet.
2194 ch = (unsigned char)*s;
2195 if ((ch & 0x80) == 0) {
2196 mask = 0x7f;
2197 len = 1;
2198 } else if ((ch & 0xe0) == 0xc0) {
2199 mask = 0x1f;
2200 len = 2;
2201 } else if ((ch & 0xf0) == 0xe0) {
2202 mask = 0x0f;
2203 len = 3;
2204 } else if ((ch & 0xf8) == 0xf0) {
2205 mask = 0x07;
2206 len = 4;
2207 } else {
2208 /* Invalid first byte. */
2209 return (-1);
2212 if (n < (size_t)len) {
2213 /* Valid first byte but truncated. */
2214 return (-2);
2218 * Decode the octet sequence representing the character in chunks
2219 * of 6 bits, most significant first.
2221 wch = (unsigned char)*s++ & mask;
2222 i = len;
2223 while (--i != 0) {
2224 if ((*s & 0xc0) != 0x80) {
2225 /* Invalid intermediate byte; consume one byte and
2226 * emit '?' */
2227 *pwc = '?';
2228 return (1);
2230 wch <<= 6;
2231 wch |= *s++ & 0x3f;
2234 /* Assign the value to the output; out-of-range values
2235 * just get truncated. */
2236 *pwc = (wchar_t)wch;
2237 #ifdef WCHAR_MAX
2239 * If platform has WCHAR_MAX, we can do something
2240 * more sensible with out-of-range values.
2242 if (wch >= WCHAR_MAX)
2243 *pwc = '?';
2244 #endif
2245 /* Return number of bytes input consumed: 0 for end-of-string. */
2246 return (wch == L'\0' ? 0 : len);
2251 * base64_decode - Base64 decode
2253 * This accepts most variations of base-64 encoding, including:
2254 * * with or without line breaks
2255 * * with or without the final group padded with '=' or '_' characters
2256 * (The most economical Base-64 variant does not pad the last group and
2257 * omits line breaks; RFC1341 used for MIME requires both.)
2259 static char *
2260 base64_decode(const char *s, size_t len, size_t *out_len)
2262 static const unsigned char digits[64] = {
2263 'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
2264 'O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b',
2265 'c','d','e','f','g','h','i','j','k','l','m','n','o','p',
2266 'q','r','s','t','u','v','w','x','y','z','0','1','2','3',
2267 '4','5','6','7','8','9','+','/' };
2268 static unsigned char decode_table[128];
2269 char *out, *d;
2270 const unsigned char *src = (const unsigned char *)s;
2272 /* If the decode table is not yet initialized, prepare it. */
2273 if (decode_table[digits[1]] != 1) {
2274 size_t i;
2275 memset(decode_table, 0xff, sizeof(decode_table));
2276 for (i = 0; i < sizeof(digits); i++)
2277 decode_table[digits[i]] = i;
2280 /* Allocate enough space to hold the entire output. */
2281 /* Note that we may not use all of this... */
2282 out = (char *)malloc((len * 3 + 3) / 4);
2283 if (out == NULL) {
2284 *out_len = 0;
2285 return (NULL);
2287 d = out;
2289 while (len > 0) {
2290 /* Collect the next group of (up to) four characters. */
2291 int v = 0;
2292 int group_size = 0;
2293 while (group_size < 4 && len > 0) {
2294 /* '=' or '_' padding indicates final group. */
2295 if (*src == '=' || *src == '_') {
2296 len = 0;
2297 break;
2299 /* Skip illegal characters (including line breaks) */
2300 if (*src > 127 || *src < 32
2301 || decode_table[*src] == 0xff) {
2302 len--;
2303 src++;
2304 continue;
2306 v <<= 6;
2307 v |= decode_table[*src++];
2308 len --;
2309 group_size++;
2311 /* Align a short group properly. */
2312 v <<= 6 * (4 - group_size);
2313 /* Unpack the group we just collected. */
2314 switch (group_size) {
2315 case 4: d[2] = v & 0xff;
2316 /* FALLTHROUGH */
2317 case 3: d[1] = (v >> 8) & 0xff;
2318 /* FALLTHROUGH */
2319 case 2: d[0] = (v >> 16) & 0xff;
2320 break;
2321 case 1: /* this is invalid! */
2322 break;
2324 d += group_size * 3 / 4;
2327 *out_len = d - out;
2328 return (out);
2331 static char *
2332 url_decode(const char *in)
2334 char *out, *d;
2335 const char *s;
2337 out = (char *)malloc(strlen(in) + 1);
2338 if (out == NULL)
2339 return (NULL);
2340 for (s = in, d = out; *s != '\0'; ) {
2341 if (*s == '%') {
2342 /* Try to convert % escape */
2343 int digit1 = tohex(s[1]);
2344 int digit2 = tohex(s[2]);
2345 if (digit1 >= 0 && digit2 >= 0) {
2346 /* Looks good, consume three chars */
2347 s += 3;
2348 /* Convert output */
2349 *d++ = ((digit1 << 4) | digit2);
2350 continue;
2352 /* Else fall through and treat '%' as normal char */
2354 *d++ = *s++;
2356 *d = '\0';
2357 return (out);
2360 static int
2361 tohex(int c)
2363 if (c >= '0' && c <= '9')
2364 return (c - '0');
2365 else if (c >= 'A' && c <= 'F')
2366 return (c - 'A' + 10);
2367 else if (c >= 'a' && c <= 'f')
2368 return (c - 'a' + 10);
2369 else
2370 return (-1);