1/*- 2 * Copyright (c) 2003-2007 Tim Kientzle 3 * Copyright (c) 2011-2012 Michihiro NAKAJIMA 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27#include "archive_platform.h" 28__FBSDID("$FreeBSD$"); 29 30#ifdef HAVE_ERRNO_H 31#include <errno.h> 32#endif 33#include <stddef.h> 34#ifdef HAVE_STDLIB_H 35#include <stdlib.h> 36#endif 37#ifdef HAVE_STRING_H 38#include <string.h> 39#endif 40 41#include "archive.h" 42#include "archive_acl_private.h" /* For ACL parsing routines. */ 43#include "archive_entry.h" 44#include "archive_entry_locale.h" 45#include "archive_private.h" 46#include "archive_read_private.h" 47 48#define tar_min(a,b) ((a) < (b) ? (a) : (b)) 49 50/* 51 * Layout of POSIX 'ustar' tar header. 52 */ 53struct archive_entry_header_ustar { 54 char name[100]; 55 char mode[8]; 56 char uid[8]; 57 char gid[8]; 58 char size[12]; 59 char mtime[12]; 60 char checksum[8]; 61 char typeflag[1]; 62 char linkname[100]; /* "old format" header ends here */ 63 char magic[6]; /* For POSIX: "ustar\0" */ 64 char version[2]; /* For POSIX: "00" */ 65 char uname[32]; 66 char gname[32]; 67 char rdevmajor[8]; 68 char rdevminor[8]; 69 char prefix[155]; 70}; 71 72/* 73 * Structure of GNU tar header 74 */ 75struct gnu_sparse { 76 char offset[12]; 77 char numbytes[12]; 78}; 79 80struct archive_entry_header_gnutar { 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]; 90 char magic[8]; /* "ustar \0" (note blank/blank/null at end) */ 91 char uname[32]; 92 char gname[32]; 93 char rdevmajor[8]; 94 char rdevminor[8]; 95 char atime[12]; 96 char ctime[12]; 97 char offset[12]; 98 char longnames[4]; 99 char unused[1]; 100 struct gnu_sparse sparse[4]; 101 char isextended[1]; 102 char realsize[12]; 103 /* 104 * Old GNU format doesn't use POSIX 'prefix' field; they use 105 * the 'L' (longname) entry instead. 106 */ 107}; 108 109/* 110 * Data specific to this format. 111 */ 112struct sparse_block { 113 struct sparse_block *next; 114 int64_t offset; 115 int64_t remaining; 116 int hole; 117}; 118 119struct tar { 120 struct archive_string acl_text; 121 struct archive_string entry_pathname; 122 /* For "GNU.sparse.name" and other similar path extensions. */ 123 struct archive_string entry_pathname_override; 124 struct archive_string entry_linkpath; 125 struct archive_string entry_uname; 126 struct archive_string entry_gname; 127 struct archive_string longlink; 128 struct archive_string longname; 129 struct archive_string pax_header; 130 struct archive_string pax_global; 131 struct archive_string line; 132 int pax_hdrcharset_binary; 133 int header_recursion_depth; 134 int64_t entry_bytes_remaining; 135 int64_t entry_offset; 136 int64_t entry_padding; 137 int64_t entry_bytes_unconsumed; 138 int64_t realsize; 139 struct sparse_block *sparse_list; 140 struct sparse_block *sparse_last; 141 int64_t sparse_offset; 142 int64_t sparse_numbytes; 143 int sparse_gnu_major; 144 int sparse_gnu_minor; 145 char sparse_gnu_pending; 146 147 struct archive_string localname; 148 struct archive_string_conv *opt_sconv; 149 struct archive_string_conv *sconv; 150 struct archive_string_conv *sconv_acl; 151 struct archive_string_conv *sconv_default; 152 int init_default_conversion; 153 int compat_2x; 154}; 155 156static int archive_block_is_null(const char *p); 157static char *base64_decode(const char *, size_t, size_t *); 158static int gnu_add_sparse_entry(struct archive_read *, struct tar *, 159 int64_t offset, int64_t remaining); 160 161static void gnu_clear_sparse_list(struct tar *); 162static int gnu_sparse_old_read(struct archive_read *, struct tar *, 163 const struct archive_entry_header_gnutar *header, size_t *); 164static int gnu_sparse_old_parse(struct archive_read *, struct tar *, 165 const struct gnu_sparse *sparse, int length); 166static int gnu_sparse_01_parse(struct archive_read *, struct tar *, 167 const char *); 168static ssize_t gnu_sparse_10_read(struct archive_read *, struct tar *, 169 size_t *); 170static int header_Solaris_ACL(struct archive_read *, struct tar *, 171 struct archive_entry *, const void *, size_t *); 172static int header_common(struct archive_read *, struct tar *, 173 struct archive_entry *, const void *); 174static int header_old_tar(struct archive_read *, struct tar *, 175 struct archive_entry *, const void *); 176static int header_pax_extensions(struct archive_read *, struct tar *, 177 struct archive_entry *, const void *, size_t *); 178static int header_pax_global(struct archive_read *, struct tar *, 179 struct archive_entry *, const void *h, size_t *); 180static int header_longlink(struct archive_read *, struct tar *, 181 struct archive_entry *, const void *h, size_t *); 182static int header_longname(struct archive_read *, struct tar *, 183 struct archive_entry *, const void *h, size_t *); 184static int read_mac_metadata_blob(struct archive_read *, struct tar *, 185 struct archive_entry *, const void *h, size_t *); 186static int header_volume(struct archive_read *, struct tar *, 187 struct archive_entry *, const void *h, size_t *); 188static int header_ustar(struct archive_read *, struct tar *, 189 struct archive_entry *, const void *h); 190static int header_gnutar(struct archive_read *, struct tar *, 191 struct archive_entry *, const void *h, size_t *); 192static int archive_read_format_tar_bid(struct archive_read *, int); 193static int archive_read_format_tar_options(struct archive_read *, 194 const char *, const char *); 195static int archive_read_format_tar_cleanup(struct archive_read *); 196static int archive_read_format_tar_read_data(struct archive_read *a, 197 const void **buff, size_t *size, int64_t *offset); 198static int archive_read_format_tar_skip(struct archive_read *a); 199static int archive_read_format_tar_read_header(struct archive_read *, 200 struct archive_entry *); 201static int checksum(struct archive_read *, const void *); 202static int pax_attribute(struct archive_read *, struct tar *, 203 struct archive_entry *, char *key, char *value); 204static int pax_header(struct archive_read *, struct tar *, 205 struct archive_entry *, char *attr); 206static void pax_time(const char *, int64_t *sec, long *nanos); 207static ssize_t readline(struct archive_read *, struct tar *, const char **, 208 ssize_t limit, size_t *); 209static int read_body_to_string(struct archive_read *, struct tar *, 210 struct archive_string *, const void *h, size_t *); 211static int solaris_sparse_parse(struct archive_read *, struct tar *, 212 struct archive_entry *, const char *); 213static int64_t tar_atol(const char *, size_t); 214static int64_t tar_atol10(const char *, size_t); 215static int64_t tar_atol256(const char *, size_t); 216static int64_t tar_atol8(const char *, size_t); 217static int tar_read_header(struct archive_read *, struct tar *, 218 struct archive_entry *, size_t *); 219static int tohex(int c); 220static char *url_decode(const char *); 221static void tar_flush_unconsumed(struct archive_read *, size_t *); 222 223 224int 225archive_read_support_format_gnutar(struct archive *a) 226{ 227 archive_check_magic(a, ARCHIVE_READ_MAGIC, 228 ARCHIVE_STATE_NEW, "archive_read_support_format_gnutar"); 229 return (archive_read_support_format_tar(a)); 230} 231 232 233int 234archive_read_support_format_tar(struct archive *_a) 235{ 236 struct archive_read *a = (struct archive_read *)_a; 237 struct tar *tar; 238 int r; 239 240 archive_check_magic(_a, ARCHIVE_READ_MAGIC, 241 ARCHIVE_STATE_NEW, "archive_read_support_format_tar"); 242 243 tar = (struct tar *)calloc(1, sizeof(*tar)); 244 if (tar == NULL) { 245 archive_set_error(&a->archive, ENOMEM, 246 "Can't allocate tar data"); 247 return (ARCHIVE_FATAL); 248 } 249 250 r = __archive_read_register_format(a, tar, "tar", 251 archive_read_format_tar_bid, 252 archive_read_format_tar_options, 253 archive_read_format_tar_read_header, 254 archive_read_format_tar_read_data, 255 archive_read_format_tar_skip, 256 NULL, 257 archive_read_format_tar_cleanup); 258 259 if (r != ARCHIVE_OK) 260 free(tar); 261 return (ARCHIVE_OK); 262} 263 264static int 265archive_read_format_tar_cleanup(struct archive_read *a) 266{ 267 struct tar *tar; 268 269 tar = (struct tar *)(a->format->data); 270 gnu_clear_sparse_list(tar); 271 archive_string_free(&tar->acl_text); 272 archive_string_free(&tar->entry_pathname); 273 archive_string_free(&tar->entry_pathname_override); 274 archive_string_free(&tar->entry_linkpath); 275 archive_string_free(&tar->entry_uname); 276 archive_string_free(&tar->entry_gname); 277 archive_string_free(&tar->line); 278 archive_string_free(&tar->pax_global); 279 archive_string_free(&tar->pax_header); 280 archive_string_free(&tar->longname); 281 archive_string_free(&tar->longlink); 282 archive_string_free(&tar->localname); 283 free(tar); 284 (a->format->data) = NULL; 285 return (ARCHIVE_OK); 286} 287 288 289static int 290archive_read_format_tar_bid(struct archive_read *a, int best_bid) 291{ 292 int bid; 293 const char *h; 294 const struct archive_entry_header_ustar *header; 295 296 (void)best_bid; /* UNUSED */ 297 298 bid = 0; 299 300 /* Now let's look at the actual header and see if it matches. */ 301 h = __archive_read_ahead(a, 512, NULL); 302 if (h == NULL) 303 return (-1); 304 305 /* If it's an end-of-archive mark, we can handle it. */ 306 if (h[0] == 0 && archive_block_is_null(h)) { 307 /* 308 * Usually, I bid the number of bits verified, but 309 * in this case, 4096 seems excessive so I picked 10 as 310 * an arbitrary but reasonable-seeming value. 311 */ 312 return (10); 313 } 314 315 /* If it's not an end-of-archive mark, it must have a valid checksum.*/ 316 if (!checksum(a, h)) 317 return (0); 318 bid += 48; /* Checksum is usually 6 octal digits. */ 319 320 header = (const struct archive_entry_header_ustar *)h; 321 322 /* Recognize POSIX formats. */ 323 if ((memcmp(header->magic, "ustar\0", 6) == 0) 324 && (memcmp(header->version, "00", 2) == 0)) 325 bid += 56; 326 327 /* Recognize GNU tar format. */ 328 if ((memcmp(header->magic, "ustar ", 6) == 0) 329 && (memcmp(header->version, " \0", 2) == 0)) 330 bid += 56; 331 332 /* Type flag must be null, digit or A-Z, a-z. */ 333 if (header->typeflag[0] != 0 && 334 !( header->typeflag[0] >= '0' && header->typeflag[0] <= '9') && 335 !( header->typeflag[0] >= 'A' && header->typeflag[0] <= 'Z') && 336 !( header->typeflag[0] >= 'a' && header->typeflag[0] <= 'z') ) 337 return (0); 338 bid += 2; /* 6 bits of variation in an 8-bit field leaves 2 bits. */ 339 340 /* Sanity check: Look at first byte of mode field. */ 341 switch (255 & (unsigned)header->mode[0]) { 342 case 0: case 255: 343 /* Base-256 value: No further verification possible! */ 344 break; 345 case ' ': /* Not recommended, but not illegal, either. */ 346 break; 347 case '0': case '1': case '2': case '3': 348 case '4': case '5': case '6': case '7': 349 /* Octal Value. */ 350 /* TODO: Check format of remainder of this field. */ 351 break; 352 default: 353 /* Not a valid mode; bail out here. */ 354 return (0); 355 } 356 /* TODO: Sanity test uid/gid/size/mtime/rdevmajor/rdevminor fields. */ 357 358 return (bid); 359} 360 361static int 362archive_read_format_tar_options(struct archive_read *a, 363 const char *key, const char *val) 364{ 365 struct tar *tar; 366 int ret = ARCHIVE_FAILED; 367 368 tar = (struct tar *)(a->format->data); 369 if (strcmp(key, "compat-2x") == 0) { 370 /* Handle UTF-8 filnames as libarchive 2.x */ 371 tar->compat_2x = (val != NULL)?1:0; 372 tar->init_default_conversion = tar->compat_2x; 373 return (ARCHIVE_OK); 374 } else if (strcmp(key, "hdrcharset") == 0) { 375 if (val == NULL || val[0] == 0) 376 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 377 "tar: hdrcharset option needs a character-set name"); 378 else { 379 tar->opt_sconv = 380 archive_string_conversion_from_charset( 381 &a->archive, val, 0); 382 if (tar->opt_sconv != NULL) 383 ret = ARCHIVE_OK; 384 else 385 ret = ARCHIVE_FATAL; 386 } 387 return (ret); 388 } 389 390 /* Note: The "warn" return is just to inform the options 391 * supervisor that we didn't handle it. It will generate 392 * a suitable error if no one used this option. */ 393 return (ARCHIVE_WARN); 394} 395 396/* utility function- this exists to centralize the logic of tracking 397 * how much unconsumed data we have floating around, and to consume 398 * anything outstanding since we're going to do read_aheads 399 */ 400static void 401tar_flush_unconsumed(struct archive_read *a, size_t *unconsumed) 402{ 403 if (*unconsumed) { 404/* 405 void *data = (void *)__archive_read_ahead(a, *unconsumed, NULL); 406 * this block of code is to poison claimed unconsumed space, ensuring 407 * things break if it is in use still. 408 * currently it WILL break things, so enable it only for debugging this issue 409 if (data) { 410 memset(data, 0xff, *unconsumed); 411 } 412*/ 413 __archive_read_consume(a, *unconsumed); 414 *unconsumed = 0; 415 } 416} 417 418/* 419 * The function invoked by archive_read_next_header(). This 420 * just sets up a few things and then calls the internal 421 * tar_read_header() function below. 422 */ 423static int 424archive_read_format_tar_read_header(struct archive_read *a, 425 struct archive_entry *entry) 426{ 427 /* 428 * When converting tar archives to cpio archives, it is 429 * essential that each distinct file have a distinct inode 430 * number. To simplify this, we keep a static count here to 431 * assign fake dev/inode numbers to each tar entry. Note that 432 * pax format archives may overwrite this with something more 433 * useful. 434 * 435 * Ideally, we would track every file read from the archive so 436 * that we could assign the same dev/ino pair to hardlinks, 437 * but the memory required to store a complete lookup table is 438 * probably not worthwhile just to support the relatively 439 * obscure tar->cpio conversion case. 440 */ 441 static int default_inode; 442 static int default_dev; 443 struct tar *tar; 444 const char *p; 445 int r; 446 size_t l, unconsumed = 0; 447 448 /* Assign default device/inode values. */ 449 archive_entry_set_dev(entry, 1 + default_dev); /* Don't use zero. */ 450 archive_entry_set_ino(entry, ++default_inode); /* Don't use zero. */ 451 /* Limit generated st_ino number to 16 bits. */ 452 if (default_inode >= 0xffff) { 453 ++default_dev; 454 default_inode = 0; 455 } 456 457 tar = (struct tar *)(a->format->data); 458 tar->entry_offset = 0; 459 gnu_clear_sparse_list(tar); 460 tar->realsize = -1; /* Mark this as "unset" */ 461 462 /* Setup default string conversion. */ 463 tar->sconv = tar->opt_sconv; 464 if (tar->sconv == NULL) { 465 if (!tar->init_default_conversion) { 466 tar->sconv_default = 467 archive_string_default_conversion_for_read(&(a->archive)); 468 tar->init_default_conversion = 1; 469 } 470 tar->sconv = tar->sconv_default; 471 } 472 473 r = tar_read_header(a, tar, entry, &unconsumed); 474 475 tar_flush_unconsumed(a, &unconsumed); 476 477 /* 478 * "non-sparse" files are really just sparse files with 479 * a single block. 480 */ 481 if (tar->sparse_list == NULL) { 482 if (gnu_add_sparse_entry(a, tar, 0, tar->entry_bytes_remaining) 483 != ARCHIVE_OK) 484 return (ARCHIVE_FATAL); 485 } else { 486 struct sparse_block *sb; 487 488 for (sb = tar->sparse_list; sb != NULL; sb = sb->next) { 489 if (!sb->hole) 490 archive_entry_sparse_add_entry(entry, 491 sb->offset, sb->remaining); 492 } 493 } 494 495 if (r == ARCHIVE_OK) { 496 /* 497 * "Regular" entry with trailing '/' is really 498 * directory: This is needed for certain old tar 499 * variants and even for some broken newer ones. 500 */ 501 const wchar_t *wp; 502 wp = archive_entry_pathname_w(entry); 503 if (wp != NULL) { 504 l = wcslen(wp); 505 if (archive_entry_filetype(entry) == AE_IFREG 506 && wp[l-1] == L'/') 507 archive_entry_set_filetype(entry, AE_IFDIR); 508 } else { 509 p = archive_entry_pathname(entry); 510 if (p == NULL) 511 return (ARCHIVE_FAILED); 512 l = strlen(p); 513 if (archive_entry_filetype(entry) == AE_IFREG 514 && p[l-1] == '/') 515 archive_entry_set_filetype(entry, AE_IFDIR); 516 } 517 } 518 return (r); 519} 520 521static int 522archive_read_format_tar_read_data(struct archive_read *a, 523 const void **buff, size_t *size, int64_t *offset) 524{ 525 ssize_t bytes_read; 526 struct tar *tar; 527 struct sparse_block *p; 528 529 tar = (struct tar *)(a->format->data); 530 531 for (;;) { 532 /* Remove exhausted entries from sparse list. */ 533 while (tar->sparse_list != NULL && 534 tar->sparse_list->remaining == 0) { 535 p = tar->sparse_list; 536 tar->sparse_list = p->next; 537 free(p); 538 } 539 540 if (tar->entry_bytes_unconsumed) { 541 __archive_read_consume(a, tar->entry_bytes_unconsumed); 542 tar->entry_bytes_unconsumed = 0; 543 } 544 545 /* If we're at end of file, return EOF. */ 546 if (tar->sparse_list == NULL || 547 tar->entry_bytes_remaining == 0) { 548 if (__archive_read_consume(a, tar->entry_padding) < 0) 549 return (ARCHIVE_FATAL); 550 tar->entry_padding = 0; 551 *buff = NULL; 552 *size = 0; 553 *offset = tar->realsize; 554 return (ARCHIVE_EOF); 555 } 556 557 *buff = __archive_read_ahead(a, 1, &bytes_read); 558 if (bytes_read < 0) 559 return (ARCHIVE_FATAL); 560 if (*buff == NULL) { 561 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 562 "Truncated tar archive"); 563 return (ARCHIVE_FATAL); 564 } 565 if (bytes_read > tar->entry_bytes_remaining) 566 bytes_read = (ssize_t)tar->entry_bytes_remaining; 567 /* Don't read more than is available in the 568 * current sparse block. */ 569 if (tar->sparse_list->remaining < bytes_read) 570 bytes_read = (ssize_t)tar->sparse_list->remaining; 571 *size = bytes_read; 572 *offset = tar->sparse_list->offset; 573 tar->sparse_list->remaining -= bytes_read; 574 tar->sparse_list->offset += bytes_read; 575 tar->entry_bytes_remaining -= bytes_read; 576 tar->entry_bytes_unconsumed = bytes_read; 577 578 if (!tar->sparse_list->hole) 579 return (ARCHIVE_OK); 580 /* Current is hole data and skip this. */ 581 } 582} 583 584static int 585archive_read_format_tar_skip(struct archive_read *a) 586{ 587 int64_t bytes_skipped; 588 struct tar* tar; 589 590 tar = (struct tar *)(a->format->data); 591 592 bytes_skipped = __archive_read_consume(a, 593 tar->entry_bytes_remaining + tar->entry_padding + 594 tar->entry_bytes_unconsumed); 595 if (bytes_skipped < 0) 596 return (ARCHIVE_FATAL); 597 598 tar->entry_bytes_remaining = 0; 599 tar->entry_bytes_unconsumed = 0; 600 tar->entry_padding = 0; 601 602 /* Free the sparse list. */ 603 gnu_clear_sparse_list(tar); 604 605 return (ARCHIVE_OK); 606} 607 608/* 609 * This function recursively interprets all of the headers associated 610 * with a single entry. 611 */ 612static int 613tar_read_header(struct archive_read *a, struct tar *tar, 614 struct archive_entry *entry, size_t *unconsumed) 615{ 616 ssize_t bytes; 617 int err; 618 const char *h; 619 const struct archive_entry_header_ustar *header; 620 const struct archive_entry_header_gnutar *gnuheader; 621 622 tar_flush_unconsumed(a, unconsumed); 623 624 /* Read 512-byte header record */ 625 h = __archive_read_ahead(a, 512, &bytes); 626 if (bytes < 0) 627 return ((int)bytes); 628 if (bytes == 0) { /* EOF at a block boundary. */ 629 /* Some writers do omit the block of nulls. <sigh> */ 630 return (ARCHIVE_EOF); 631 } 632 if (bytes < 512) { /* Short block at EOF; this is bad. */ 633 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 634 "Truncated tar archive"); 635 return (ARCHIVE_FATAL); 636 } 637 *unconsumed = 512; 638 639 /* Check for end-of-archive mark. */ 640 if (h[0] == 0 && archive_block_is_null(h)) { 641 /* Try to consume a second all-null record, as well. */ 642 tar_flush_unconsumed(a, unconsumed); 643 h = __archive_read_ahead(a, 512, NULL); 644 if (h != NULL) 645 __archive_read_consume(a, 512); 646 archive_clear_error(&a->archive); 647 if (a->archive.archive_format_name == NULL) { 648 a->archive.archive_format = ARCHIVE_FORMAT_TAR; 649 a->archive.archive_format_name = "tar"; 650 } 651 return (ARCHIVE_EOF); 652 } 653 654 /* 655 * Note: If the checksum fails and we return ARCHIVE_RETRY, 656 * then the client is likely to just retry. This is a very 657 * crude way to search for the next valid header! 658 * 659 * TODO: Improve this by implementing a real header scan. 660 */ 661 if (!checksum(a, h)) { 662 tar_flush_unconsumed(a, unconsumed); 663 archive_set_error(&a->archive, EINVAL, "Damaged tar archive"); 664 return (ARCHIVE_RETRY); /* Retryable: Invalid header */ 665 } 666 667 if (++tar->header_recursion_depth > 32) { 668 tar_flush_unconsumed(a, unconsumed); 669 archive_set_error(&a->archive, EINVAL, "Too many special headers"); 670 return (ARCHIVE_WARN); 671 } 672 673 /* Determine the format variant. */ 674 header = (const struct archive_entry_header_ustar *)h; 675 676 switch(header->typeflag[0]) { 677 case 'A': /* Solaris tar ACL */ 678 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE; 679 a->archive.archive_format_name = "Solaris tar"; 680 err = header_Solaris_ACL(a, tar, entry, h, unconsumed); 681 break; 682 case 'g': /* POSIX-standard 'g' header. */ 683 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE; 684 a->archive.archive_format_name = "POSIX pax interchange format"; 685 err = header_pax_global(a, tar, entry, h, unconsumed); 686 break; 687 case 'K': /* Long link name (GNU tar, others) */ 688 err = header_longlink(a, tar, entry, h, unconsumed); 689 break; 690 case 'L': /* Long filename (GNU tar, others) */ 691 err = header_longname(a, tar, entry, h, unconsumed); 692 break; 693 case 'V': /* GNU volume header */ 694 err = header_volume(a, tar, entry, h, unconsumed); 695 break; 696 case 'X': /* Used by SUN tar; same as 'x'. */ 697 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE; 698 a->archive.archive_format_name = 699 "POSIX pax interchange format (Sun variant)"; 700 err = header_pax_extensions(a, tar, entry, h, unconsumed); 701 break; 702 case 'x': /* POSIX-standard 'x' header. */ 703 a->archive.archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE; 704 a->archive.archive_format_name = "POSIX pax interchange format"; 705 err = header_pax_extensions(a, tar, entry, h, unconsumed); 706 break; 707 default: 708 gnuheader = (const struct archive_entry_header_gnutar *)h; 709 if (memcmp(gnuheader->magic, "ustar \0", 8) == 0) { 710 a->archive.archive_format = ARCHIVE_FORMAT_TAR_GNUTAR; 711 a->archive.archive_format_name = "GNU tar format"; 712 err = header_gnutar(a, tar, entry, h, unconsumed); 713 } else if (memcmp(header->magic, "ustar", 5) == 0) { 714 if (a->archive.archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) { 715 a->archive.archive_format = ARCHIVE_FORMAT_TAR_USTAR; 716 a->archive.archive_format_name = "POSIX ustar format"; 717 } 718 err = header_ustar(a, tar, entry, h); 719 } else { 720 a->archive.archive_format = ARCHIVE_FORMAT_TAR; 721 a->archive.archive_format_name = "tar (non-POSIX)"; 722 err = header_old_tar(a, tar, entry, h); 723 } 724 } 725 if (err == ARCHIVE_FATAL) 726 return (err); 727 728 tar_flush_unconsumed(a, unconsumed); 729 730 h = NULL; 731 header = NULL; 732 733 --tar->header_recursion_depth; 734 /* Yuck. Apple's design here ends up storing long pathname 735 * extensions for both the AppleDouble extension entry and the 736 * regular entry. 737 */ 738 /* TODO: Should this be disabled on non-Mac platforms? */ 739 if ((err == ARCHIVE_WARN || err == ARCHIVE_OK) && 740 tar->header_recursion_depth == 0) { 741 int err2 = read_mac_metadata_blob(a, tar, entry, h, unconsumed); 742 if (err2 < err) 743 err = err2; 744 } 745 746 /* We return warnings or success as-is. Anything else is fatal. */ 747 if (err == ARCHIVE_WARN || err == ARCHIVE_OK) { 748 if (tar->sparse_gnu_pending) { 749 if (tar->sparse_gnu_major == 1 && 750 tar->sparse_gnu_minor == 0) { 751 ssize_t bytes_read; 752 753 tar->sparse_gnu_pending = 0; 754 /* Read initial sparse map. */ 755 bytes_read = gnu_sparse_10_read(a, tar, unconsumed); 756 tar->entry_bytes_remaining -= bytes_read; 757 if (bytes_read < 0) 758 return ((int)bytes_read); 759 } else { 760 archive_set_error(&a->archive, 761 ARCHIVE_ERRNO_MISC, 762 "Unrecognized GNU sparse file format"); 763 return (ARCHIVE_WARN); 764 } 765 tar->sparse_gnu_pending = 0; 766 } 767 return (err); 768 } 769 if (err == ARCHIVE_EOF) 770 /* EOF when recursively reading a header is bad. */ 771 archive_set_error(&a->archive, EINVAL, "Damaged tar archive"); 772 return (ARCHIVE_FATAL); 773} 774 775/* 776 * Return true if block checksum is correct. 777 */ 778static int 779checksum(struct archive_read *a, const void *h) 780{ 781 const unsigned char *bytes; 782 const struct archive_entry_header_ustar *header; 783 int check, i, sum; 784 785 (void)a; /* UNUSED */ 786 bytes = (const unsigned char *)h; 787 header = (const struct archive_entry_header_ustar *)h; 788 789 /* 790 * Test the checksum. Note that POSIX specifies _unsigned_ 791 * bytes for this calculation. 792 */ 793 sum = (int)tar_atol(header->checksum, sizeof(header->checksum)); 794 check = 0; 795 for (i = 0; i < 148; i++) 796 check += (unsigned char)bytes[i]; 797 for (; i < 156; i++) 798 check += 32; 799 for (; i < 512; i++) 800 check += (unsigned char)bytes[i]; 801 if (sum == check) 802 return (1); 803 804 /* 805 * Repeat test with _signed_ bytes, just in case this archive 806 * was created by an old BSD, Solaris, or HP-UX tar with a 807 * broken checksum calculation. 808 */ 809 check = 0; 810 for (i = 0; i < 148; i++) 811 check += (signed char)bytes[i]; 812 for (; i < 156; i++) 813 check += 32; 814 for (; i < 512; i++) 815 check += (signed char)bytes[i]; 816 if (sum == check) 817 return (1); 818 819 return (0); 820} 821 822/* 823 * Return true if this block contains only nulls. 824 */ 825static int 826archive_block_is_null(const char *p) 827{ 828 unsigned i; 829 830 for (i = 0; i < 512; i++) 831 if (*p++) 832 return (0); 833 return (1); 834} 835 836/* 837 * Interpret 'A' Solaris ACL header 838 */ 839static int 840header_Solaris_ACL(struct archive_read *a, struct tar *tar, 841 struct archive_entry *entry, const void *h, size_t *unconsumed) 842{ 843 const struct archive_entry_header_ustar *header; 844 size_t size; 845 int err; 846 int64_t type; 847 char *acl, *p; 848 849 /* 850 * read_body_to_string adds a NUL terminator, but we need a little 851 * more to make sure that we don't overrun acl_text later. 852 */ 853 header = (const struct archive_entry_header_ustar *)h; 854 size = (size_t)tar_atol(header->size, sizeof(header->size)); 855 err = read_body_to_string(a, tar, &(tar->acl_text), h, unconsumed); 856 if (err != ARCHIVE_OK) 857 return (err); 858 859 /* Recursively read next header */ 860 err = tar_read_header(a, tar, entry, unconsumed); 861 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN)) 862 return (err); 863 864 /* TODO: Examine the first characters to see if this 865 * is an AIX ACL descriptor. We'll likely never support 866 * them, but it would be polite to recognize and warn when 867 * we do see them. */ 868 869 /* Leading octal number indicates ACL type and number of entries. */ 870 p = acl = tar->acl_text.s; 871 type = 0; 872 while (*p != '\0' && p < acl + size) { 873 if (*p < '0' || *p > '7') { 874 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 875 "Malformed Solaris ACL attribute (invalid digit)"); 876 return(ARCHIVE_WARN); 877 } 878 type <<= 3; 879 type += *p - '0'; 880 if (type > 077777777) { 881 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 882 "Malformed Solaris ACL attribute (count too large)"); 883 return (ARCHIVE_WARN); 884 } 885 p++; 886 } 887 switch ((int)type & ~0777777) { 888 case 01000000: 889 /* POSIX.1e ACL */ 890 break; 891 case 03000000: 892 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 893 "Solaris NFSv4 ACLs not supported"); 894 return (ARCHIVE_WARN); 895 default: 896 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 897 "Malformed Solaris ACL attribute (unsupported type %o)", 898 (int)type); 899 return (ARCHIVE_WARN); 900 } 901 p++; 902 903 if (p >= acl + size) { 904 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 905 "Malformed Solaris ACL attribute (body overflow)"); 906 return(ARCHIVE_WARN); 907 } 908 909 /* ACL text is null-terminated; find the end. */ 910 size -= (p - acl); 911 acl = p; 912 913 while (*p != '\0' && p < acl + size) 914 p++; 915 916 if (tar->sconv_acl == NULL) { 917 tar->sconv_acl = archive_string_conversion_from_charset( 918 &(a->archive), "UTF-8", 1); 919 if (tar->sconv_acl == NULL) 920 return (ARCHIVE_FATAL); 921 } 922 archive_strncpy(&(tar->localname), acl, p - acl); 923 err = archive_acl_parse_l(archive_entry_acl(entry), 924 tar->localname.s, ARCHIVE_ENTRY_ACL_TYPE_ACCESS, tar->sconv_acl); 925 if (err != ARCHIVE_OK) { 926 if (errno == ENOMEM) { 927 archive_set_error(&a->archive, ENOMEM, 928 "Can't allocate memory for ACL"); 929 } else 930 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 931 "Malformed Solaris ACL attribute (unparsable)"); 932 } 933 return (err); 934} 935 936/* 937 * Interpret 'K' long linkname header. 938 */ 939static int 940header_longlink(struct archive_read *a, struct tar *tar, 941 struct archive_entry *entry, const void *h, size_t *unconsumed) 942{ 943 int err; 944 945 err = read_body_to_string(a, tar, &(tar->longlink), h, unconsumed); 946 if (err != ARCHIVE_OK) 947 return (err); 948 err = tar_read_header(a, tar, entry, unconsumed); 949 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN)) 950 return (err); 951 /* Set symlink if symlink already set, else hardlink. */ 952 archive_entry_copy_link(entry, tar->longlink.s); 953 return (ARCHIVE_OK); 954} 955 956static int 957set_conversion_failed_error(struct archive_read *a, 958 struct archive_string_conv *sconv, const char *name) 959{ 960 if (errno == ENOMEM) { 961 archive_set_error(&a->archive, ENOMEM, 962 "Can't allocate memory for %s", name); 963 return (ARCHIVE_FATAL); 964 } 965 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 966 "%s can't be converted from %s to current locale.", 967 name, archive_string_conversion_charset_name(sconv)); 968 return (ARCHIVE_WARN); 969} 970 971/* 972 * Interpret 'L' long filename header. 973 */ 974static int 975header_longname(struct archive_read *a, struct tar *tar, 976 struct archive_entry *entry, const void *h, size_t *unconsumed) 977{ 978 int err; 979 980 err = read_body_to_string(a, tar, &(tar->longname), h, unconsumed); 981 if (err != ARCHIVE_OK) 982 return (err); 983 /* Read and parse "real" header, then override name. */ 984 err = tar_read_header(a, tar, entry, unconsumed); 985 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN)) 986 return (err); 987 if (archive_entry_copy_pathname_l(entry, tar->longname.s, 988 archive_strlen(&(tar->longname)), tar->sconv) != 0) 989 err = set_conversion_failed_error(a, tar->sconv, "Pathname"); 990 return (err); 991} 992 993 994/* 995 * Interpret 'V' GNU tar volume header. 996 */ 997static int 998header_volume(struct archive_read *a, struct tar *tar, 999 struct archive_entry *entry, const void *h, size_t *unconsumed) 1000{ 1001 (void)h; 1002 1003 /* Just skip this and read the next header. */ 1004 return (tar_read_header(a, tar, entry, unconsumed)); 1005} 1006 1007/* 1008 * Read body of an archive entry into an archive_string object. 1009 */ 1010static int 1011read_body_to_string(struct archive_read *a, struct tar *tar, 1012 struct archive_string *as, const void *h, size_t *unconsumed) 1013{ 1014 int64_t size; 1015 const struct archive_entry_header_ustar *header; 1016 const void *src; 1017 1018 (void)tar; /* UNUSED */ 1019 header = (const struct archive_entry_header_ustar *)h; 1020 size = tar_atol(header->size, sizeof(header->size)); 1021 if ((size > 1048576) || (size < 0)) { 1022 archive_set_error(&a->archive, EINVAL, 1023 "Special header too large"); 1024 return (ARCHIVE_FATAL); 1025 } 1026 1027 /* Fail if we can't make our buffer big enough. */ 1028 if (archive_string_ensure(as, (size_t)size+1) == NULL) { 1029 archive_set_error(&a->archive, ENOMEM, 1030 "No memory"); 1031 return (ARCHIVE_FATAL); 1032 } 1033 1034 tar_flush_unconsumed(a, unconsumed); 1035 1036 /* Read the body into the string. */ 1037 *unconsumed = (size_t)((size + 511) & ~ 511); 1038 src = __archive_read_ahead(a, *unconsumed, NULL); 1039 if (src == NULL) { 1040 *unconsumed = 0; 1041 return (ARCHIVE_FATAL); 1042 } 1043 memcpy(as->s, src, (size_t)size); 1044 as->s[size] = '\0'; 1045 as->length = (size_t)size; 1046 return (ARCHIVE_OK); 1047} 1048 1049/* 1050 * Parse out common header elements. 1051 * 1052 * This would be the same as header_old_tar, except that the 1053 * filename is handled slightly differently for old and POSIX 1054 * entries (POSIX entries support a 'prefix'). This factoring 1055 * allows header_old_tar and header_ustar 1056 * to handle filenames differently, while still putting most of the 1057 * common parsing into one place. 1058 */ 1059static int 1060header_common(struct archive_read *a, struct tar *tar, 1061 struct archive_entry *entry, const void *h) 1062{ 1063 const struct archive_entry_header_ustar *header; 1064 char tartype; 1065 int err = ARCHIVE_OK; 1066 1067 header = (const struct archive_entry_header_ustar *)h; 1068 if (header->linkname[0]) 1069 archive_strncpy(&(tar->entry_linkpath), 1070 header->linkname, sizeof(header->linkname)); 1071 else 1072 archive_string_empty(&(tar->entry_linkpath)); 1073 1074 /* Parse out the numeric fields (all are octal) */ 1075 archive_entry_set_mode(entry, 1076 (mode_t)tar_atol(header->mode, sizeof(header->mode))); 1077 archive_entry_set_uid(entry, tar_atol(header->uid, sizeof(header->uid))); 1078 archive_entry_set_gid(entry, tar_atol(header->gid, sizeof(header->gid))); 1079 tar->entry_bytes_remaining = tar_atol(header->size, sizeof(header->size)); 1080 if (tar->entry_bytes_remaining < 0) { 1081 tar->entry_bytes_remaining = 0; 1082 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1083 "Tar entry has negative size?"); 1084 err = ARCHIVE_WARN; 1085 } 1086 tar->realsize = tar->entry_bytes_remaining; 1087 archive_entry_set_size(entry, tar->entry_bytes_remaining); 1088 archive_entry_set_mtime(entry, tar_atol(header->mtime, sizeof(header->mtime)), 0); 1089 1090 /* Handle the tar type flag appropriately. */ 1091 tartype = header->typeflag[0]; 1092 1093 switch (tartype) { 1094 case '1': /* Hard link */ 1095 if (archive_entry_copy_hardlink_l(entry, tar->entry_linkpath.s, 1096 archive_strlen(&(tar->entry_linkpath)), tar->sconv) != 0) { 1097 err = set_conversion_failed_error(a, tar->sconv, 1098 "Linkname"); 1099 if (err == ARCHIVE_FATAL) 1100 return (err); 1101 } 1102 /* 1103 * The following may seem odd, but: Technically, tar 1104 * does not store the file type for a "hard link" 1105 * entry, only the fact that it is a hard link. So, I 1106 * leave the type zero normally. But, pax interchange 1107 * format allows hard links to have data, which 1108 * implies that the underlying entry is a regular 1109 * file. 1110 */ 1111 if (archive_entry_size(entry) > 0) 1112 archive_entry_set_filetype(entry, AE_IFREG); 1113 1114 /* 1115 * A tricky point: Traditionally, tar readers have 1116 * ignored the size field when reading hardlink 1117 * entries, and some writers put non-zero sizes even 1118 * though the body is empty. POSIX blessed this 1119 * convention in the 1988 standard, but broke with 1120 * this tradition in 2001 by permitting hardlink 1121 * entries to store valid bodies in pax interchange 1122 * format, but not in ustar format. Since there is no 1123 * hard and fast way to distinguish pax interchange 1124 * from earlier archives (the 'x' and 'g' entries are 1125 * optional, after all), we need a heuristic. 1126 */ 1127 if (archive_entry_size(entry) == 0) { 1128 /* If the size is already zero, we're done. */ 1129 } else if (a->archive.archive_format 1130 == ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) { 1131 /* Definitely pax extended; must obey hardlink size. */ 1132 } else if (a->archive.archive_format == ARCHIVE_FORMAT_TAR 1133 || a->archive.archive_format == ARCHIVE_FORMAT_TAR_GNUTAR) 1134 { 1135 /* Old-style or GNU tar: we must ignore the size. */ 1136 archive_entry_set_size(entry, 0); 1137 tar->entry_bytes_remaining = 0; 1138 } else if (archive_read_format_tar_bid(a, 50) > 50) { 1139 /* 1140 * We don't know if it's pax: If the bid 1141 * function sees a valid ustar header 1142 * immediately following, then let's ignore 1143 * the hardlink size. 1144 */ 1145 archive_entry_set_size(entry, 0); 1146 tar->entry_bytes_remaining = 0; 1147 } 1148 /* 1149 * TODO: There are still two cases I'd like to handle: 1150 * = a ustar non-pax archive with a hardlink entry at 1151 * end-of-archive. (Look for block of nulls following?) 1152 * = a pax archive that has not seen any pax headers 1153 * and has an entry which is a hardlink entry storing 1154 * a body containing an uncompressed tar archive. 1155 * The first is worth addressing; I don't see any reliable 1156 * way to deal with the second possibility. 1157 */ 1158 break; 1159 case '2': /* Symlink */ 1160 archive_entry_set_filetype(entry, AE_IFLNK); 1161 archive_entry_set_size(entry, 0); 1162 tar->entry_bytes_remaining = 0; 1163 if (archive_entry_copy_symlink_l(entry, tar->entry_linkpath.s, 1164 archive_strlen(&(tar->entry_linkpath)), tar->sconv) != 0) { 1165 err = set_conversion_failed_error(a, tar->sconv, 1166 "Linkname"); 1167 if (err == ARCHIVE_FATAL) 1168 return (err); 1169 } 1170 break; 1171 case '3': /* Character device */ 1172 archive_entry_set_filetype(entry, AE_IFCHR); 1173 archive_entry_set_size(entry, 0); 1174 tar->entry_bytes_remaining = 0; 1175 break; 1176 case '4': /* Block device */ 1177 archive_entry_set_filetype(entry, AE_IFBLK); 1178 archive_entry_set_size(entry, 0); 1179 tar->entry_bytes_remaining = 0; 1180 break; 1181 case '5': /* Dir */ 1182 archive_entry_set_filetype(entry, AE_IFDIR); 1183 archive_entry_set_size(entry, 0); 1184 tar->entry_bytes_remaining = 0; 1185 break; 1186 case '6': /* FIFO device */ 1187 archive_entry_set_filetype(entry, AE_IFIFO); 1188 archive_entry_set_size(entry, 0); 1189 tar->entry_bytes_remaining = 0; 1190 break; 1191 case 'D': /* GNU incremental directory type */ 1192 /* 1193 * No special handling is actually required here. 1194 * It might be nice someday to preprocess the file list and 1195 * provide it to the client, though. 1196 */ 1197 archive_entry_set_filetype(entry, AE_IFDIR); 1198 break; 1199 case 'M': /* GNU "Multi-volume" (remainder of file from last archive)*/ 1200 /* 1201 * As far as I can tell, this is just like a regular file 1202 * entry, except that the contents should be _appended_ to 1203 * the indicated file at the indicated offset. This may 1204 * require some API work to fully support. 1205 */ 1206 break; 1207 case 'N': /* Old GNU "long filename" entry. */ 1208 /* The body of this entry is a script for renaming 1209 * previously-extracted entries. Ugh. It will never 1210 * be supported by libarchive. */ 1211 archive_entry_set_filetype(entry, AE_IFREG); 1212 break; 1213 case 'S': /* GNU sparse files */ 1214 /* 1215 * Sparse files are really just regular files with 1216 * sparse information in the extended area. 1217 */ 1218 /* FALLTHROUGH */ 1219 default: /* Regular file and non-standard types */ 1220 /* 1221 * Per POSIX: non-recognized types should always be 1222 * treated as regular files. 1223 */ 1224 archive_entry_set_filetype(entry, AE_IFREG); 1225 break; 1226 } 1227 return (err); 1228} 1229 1230/* 1231 * Parse out header elements for "old-style" tar archives. 1232 */ 1233static int 1234header_old_tar(struct archive_read *a, struct tar *tar, 1235 struct archive_entry *entry, const void *h) 1236{ 1237 const struct archive_entry_header_ustar *header; 1238 int err = ARCHIVE_OK, err2; 1239 1240 /* Copy filename over (to ensure null termination). */ 1241 header = (const struct archive_entry_header_ustar *)h; 1242 if (archive_entry_copy_pathname_l(entry, 1243 header->name, sizeof(header->name), tar->sconv) != 0) { 1244 err = set_conversion_failed_error(a, tar->sconv, "Pathname"); 1245 if (err == ARCHIVE_FATAL) 1246 return (err); 1247 } 1248 1249 /* Grab rest of common fields */ 1250 err2 = header_common(a, tar, entry, h); 1251 if (err > err2) 1252 err = err2; 1253 1254 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining); 1255 return (err); 1256} 1257 1258/* 1259 * Read a Mac AppleDouble-encoded blob of file metadata, 1260 * if there is one. 1261 */ 1262static int 1263read_mac_metadata_blob(struct archive_read *a, struct tar *tar, 1264 struct archive_entry *entry, const void *h, size_t *unconsumed) 1265{ 1266 int64_t size; 1267 const void *data; 1268 const char *p, *name; 1269 const wchar_t *wp, *wname; 1270 1271 (void)h; /* UNUSED */ 1272 1273 wname = wp = archive_entry_pathname_w(entry); 1274 if (wp != NULL) { 1275 /* Find the last path element. */ 1276 for (; *wp != L'\0'; ++wp) { 1277 if (wp[0] == '/' && wp[1] != L'\0') 1278 wname = wp + 1; 1279 } 1280 /* 1281 * If last path element starts with "._", then 1282 * this is a Mac extension. 1283 */ 1284 if (wname[0] != L'.' || wname[1] != L'_' || wname[2] == L'\0') 1285 return ARCHIVE_OK; 1286 } else { 1287 /* Find the last path element. */ 1288 name = p = archive_entry_pathname(entry); 1289 if (p == NULL) 1290 return (ARCHIVE_FAILED); 1291 for (; *p != '\0'; ++p) { 1292 if (p[0] == '/' && p[1] != '\0') 1293 name = p + 1; 1294 } 1295 /* 1296 * If last path element starts with "._", then 1297 * this is a Mac extension. 1298 */ 1299 if (name[0] != '.' || name[1] != '_' || name[2] == '\0') 1300 return ARCHIVE_OK; 1301 } 1302 1303 /* Read the body as a Mac OS metadata blob. */ 1304 size = archive_entry_size(entry); 1305 1306 /* 1307 * TODO: Look beyond the body here to peek at the next header. 1308 * If it's a regular header (not an extension header) 1309 * that has the wrong name, just return the current 1310 * entry as-is, without consuming the body here. 1311 * That would reduce the risk of us mis-identifying 1312 * an ordinary file that just happened to have 1313 * a name starting with "._". 1314 * 1315 * Q: Is the above idea really possible? Even 1316 * when there are GNU or pax extension entries? 1317 */ 1318 data = __archive_read_ahead(a, (size_t)size, NULL); 1319 if (data == NULL) { 1320 *unconsumed = 0; 1321 return (ARCHIVE_FATAL); 1322 } 1323 archive_entry_copy_mac_metadata(entry, data, (size_t)size); 1324 *unconsumed = (size_t)((size + 511) & ~ 511); 1325 tar_flush_unconsumed(a, unconsumed); 1326 return (tar_read_header(a, tar, entry, unconsumed)); 1327} 1328 1329/* 1330 * Parse a file header for a pax extended archive entry. 1331 */ 1332static int 1333header_pax_global(struct archive_read *a, struct tar *tar, 1334 struct archive_entry *entry, const void *h, size_t *unconsumed) 1335{ 1336 int err; 1337 1338 err = read_body_to_string(a, tar, &(tar->pax_global), h, unconsumed); 1339 if (err != ARCHIVE_OK) 1340 return (err); 1341 err = tar_read_header(a, tar, entry, unconsumed); 1342 return (err); 1343} 1344 1345static int 1346header_pax_extensions(struct archive_read *a, struct tar *tar, 1347 struct archive_entry *entry, const void *h, size_t *unconsumed) 1348{ 1349 int err, err2; 1350 1351 err = read_body_to_string(a, tar, &(tar->pax_header), h, unconsumed); 1352 if (err != ARCHIVE_OK) 1353 return (err); 1354 1355 /* Parse the next header. */ 1356 err = tar_read_header(a, tar, entry, unconsumed); 1357 if ((err != ARCHIVE_OK) && (err != ARCHIVE_WARN)) 1358 return (err); 1359 1360 /* 1361 * TODO: Parse global/default options into 'entry' struct here 1362 * before handling file-specific options. 1363 * 1364 * This design (parse standard header, then overwrite with pax 1365 * extended attribute data) usually works well, but isn't ideal; 1366 * it would be better to parse the pax extended attributes first 1367 * and then skip any fields in the standard header that were 1368 * defined in the pax header. 1369 */ 1370 err2 = pax_header(a, tar, entry, tar->pax_header.s); 1371 err = err_combine(err, err2); 1372 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining); 1373 return (err); 1374} 1375 1376 1377/* 1378 * Parse a file header for a Posix "ustar" archive entry. This also 1379 * handles "pax" or "extended ustar" entries. 1380 */ 1381static int 1382header_ustar(struct archive_read *a, struct tar *tar, 1383 struct archive_entry *entry, const void *h) 1384{ 1385 const struct archive_entry_header_ustar *header; 1386 struct archive_string *as; 1387 int err = ARCHIVE_OK, r; 1388 1389 header = (const struct archive_entry_header_ustar *)h; 1390 1391 /* Copy name into an internal buffer to ensure null-termination. */ 1392 as = &(tar->entry_pathname); 1393 if (header->prefix[0]) { 1394 archive_strncpy(as, header->prefix, sizeof(header->prefix)); 1395 if (as->s[archive_strlen(as) - 1] != '/') 1396 archive_strappend_char(as, '/'); 1397 archive_strncat(as, header->name, sizeof(header->name)); 1398 } else { 1399 archive_strncpy(as, header->name, sizeof(header->name)); 1400 } 1401 if (archive_entry_copy_pathname_l(entry, as->s, archive_strlen(as), 1402 tar->sconv) != 0) { 1403 err = set_conversion_failed_error(a, tar->sconv, "Pathname"); 1404 if (err == ARCHIVE_FATAL) 1405 return (err); 1406 } 1407 1408 /* Handle rest of common fields. */ 1409 r = header_common(a, tar, entry, h); 1410 if (r == ARCHIVE_FATAL) 1411 return (r); 1412 if (r < err) 1413 err = r; 1414 1415 /* Handle POSIX ustar fields. */ 1416 if (archive_entry_copy_uname_l(entry, 1417 header->uname, sizeof(header->uname), tar->sconv) != 0) { 1418 err = set_conversion_failed_error(a, tar->sconv, "Uname"); 1419 if (err == ARCHIVE_FATAL) 1420 return (err); 1421 } 1422 1423 if (archive_entry_copy_gname_l(entry, 1424 header->gname, sizeof(header->gname), tar->sconv) != 0) { 1425 err = set_conversion_failed_error(a, tar->sconv, "Gname"); 1426 if (err == ARCHIVE_FATAL) 1427 return (err); 1428 } 1429 1430 /* Parse out device numbers only for char and block specials. */ 1431 if (header->typeflag[0] == '3' || header->typeflag[0] == '4') { 1432 archive_entry_set_rdevmajor(entry, (dev_t) 1433 tar_atol(header->rdevmajor, sizeof(header->rdevmajor))); 1434 archive_entry_set_rdevminor(entry, (dev_t) 1435 tar_atol(header->rdevminor, sizeof(header->rdevminor))); 1436 } 1437 1438 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining); 1439 1440 return (err); 1441} 1442 1443 1444/* 1445 * Parse the pax extended attributes record. 1446 * 1447 * Returns non-zero if there's an error in the data. 1448 */ 1449static int 1450pax_header(struct archive_read *a, struct tar *tar, 1451 struct archive_entry *entry, char *attr) 1452{ 1453 size_t attr_length, l, line_length; 1454 char *p; 1455 char *key, *value; 1456 struct archive_string *as; 1457 struct archive_string_conv *sconv; 1458 int err, err2; 1459 1460 attr_length = strlen(attr); 1461 tar->pax_hdrcharset_binary = 0; 1462 archive_string_empty(&(tar->entry_gname)); 1463 archive_string_empty(&(tar->entry_linkpath)); 1464 archive_string_empty(&(tar->entry_pathname)); 1465 archive_string_empty(&(tar->entry_pathname_override)); 1466 archive_string_empty(&(tar->entry_uname)); 1467 err = ARCHIVE_OK; 1468 while (attr_length > 0) { 1469 /* Parse decimal length field at start of line. */ 1470 line_length = 0; 1471 l = attr_length; 1472 p = attr; /* Record start of line. */ 1473 while (l>0) { 1474 if (*p == ' ') { 1475 p++; 1476 l--; 1477 break; 1478 } 1479 if (*p < '0' || *p > '9') { 1480 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1481 "Ignoring malformed pax extended attributes"); 1482 return (ARCHIVE_WARN); 1483 } 1484 line_length *= 10; 1485 line_length += *p - '0'; 1486 if (line_length > 999999) { 1487 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1488 "Rejecting pax extended attribute > 1MB"); 1489 return (ARCHIVE_WARN); 1490 } 1491 p++; 1492 l--; 1493 } 1494 1495 /* 1496 * Parsed length must be no bigger than available data, 1497 * at least 1, and the last character of the line must 1498 * be '\n'. 1499 */ 1500 if (line_length > attr_length 1501 || line_length < 1 1502 || attr[line_length - 1] != '\n') 1503 { 1504 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1505 "Ignoring malformed pax extended attribute"); 1506 return (ARCHIVE_WARN); 1507 } 1508 1509 /* Null-terminate the line. */ 1510 attr[line_length - 1] = '\0'; 1511 1512 /* Find end of key and null terminate it. */ 1513 key = p; 1514 if (key[0] == '=') 1515 return (-1); 1516 while (*p && *p != '=') 1517 ++p; 1518 if (*p == '\0') { 1519 archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC, 1520 "Invalid pax extended attributes"); 1521 return (ARCHIVE_WARN); 1522 } 1523 *p = '\0'; 1524 1525 /* Identify null-terminated 'value' portion. */ 1526 value = p + 1; 1527 1528 /* Identify this attribute and set it in the entry. */ 1529 err2 = pax_attribute(a, tar, entry, key, value); 1530 if (err2 == ARCHIVE_FATAL) 1531 return (err2); 1532 err = err_combine(err, err2); 1533 1534 /* Skip to next line */ 1535 attr += line_length; 1536 attr_length -= line_length; 1537 } 1538 1539 /* 1540 * PAX format uses UTF-8 as default charset for its metadata 1541 * unless hdrcharset=BINARY is present in its header. 1542 * We apply the charset specified by the hdrcharset option only 1543 * when the hdrcharset attribute(in PAX header) is BINARY because 1544 * we respect the charset described in PAX header and BINARY also 1545 * means that metadata(filename,uname and gname) character-set 1546 * is unknown. 1547 */ 1548 if (tar->pax_hdrcharset_binary) 1549 sconv = tar->opt_sconv; 1550 else { 1551 sconv = archive_string_conversion_from_charset( 1552 &(a->archive), "UTF-8", 1); 1553 if (sconv == NULL) 1554 return (ARCHIVE_FATAL); 1555 if (tar->compat_2x) 1556 archive_string_conversion_set_opt(sconv, 1557 SCONV_SET_OPT_UTF8_LIBARCHIVE2X); 1558 } 1559 1560 if (archive_strlen(&(tar->entry_gname)) > 0) { 1561 if (archive_entry_copy_gname_l(entry, tar->entry_gname.s, 1562 archive_strlen(&(tar->entry_gname)), sconv) != 0) { 1563 err = set_conversion_failed_error(a, sconv, "Gname"); 1564 if (err == ARCHIVE_FATAL) 1565 return (err); 1566 /* Use a converted an original name. */ 1567 archive_entry_copy_gname(entry, tar->entry_gname.s); 1568 } 1569 } 1570 if (archive_strlen(&(tar->entry_linkpath)) > 0) { 1571 if (archive_entry_copy_link_l(entry, tar->entry_linkpath.s, 1572 archive_strlen(&(tar->entry_linkpath)), sconv) != 0) { 1573 err = set_conversion_failed_error(a, sconv, "Linkname"); 1574 if (err == ARCHIVE_FATAL) 1575 return (err); 1576 /* Use a converted an original name. */ 1577 archive_entry_copy_link(entry, tar->entry_linkpath.s); 1578 } 1579 } 1580 /* 1581 * Some extensions (such as the GNU sparse file extensions) 1582 * deliberately store a synthetic name under the regular 'path' 1583 * attribute and the real file name under a different attribute. 1584 * Since we're supposed to not care about the order, we 1585 * have no choice but to store all of the various filenames 1586 * we find and figure it all out afterwards. This is the 1587 * figuring out part. 1588 */ 1589 as = NULL; 1590 if (archive_strlen(&(tar->entry_pathname_override)) > 0) 1591 as = &(tar->entry_pathname_override); 1592 else if (archive_strlen(&(tar->entry_pathname)) > 0) 1593 as = &(tar->entry_pathname); 1594 if (as != NULL) { 1595 if (archive_entry_copy_pathname_l(entry, as->s, 1596 archive_strlen(as), sconv) != 0) { 1597 err = set_conversion_failed_error(a, sconv, "Pathname"); 1598 if (err == ARCHIVE_FATAL) 1599 return (err); 1600 /* Use a converted an original name. */ 1601 archive_entry_copy_pathname(entry, as->s); 1602 } 1603 } 1604 if (archive_strlen(&(tar->entry_uname)) > 0) { 1605 if (archive_entry_copy_uname_l(entry, tar->entry_uname.s, 1606 archive_strlen(&(tar->entry_uname)), sconv) != 0) { 1607 err = set_conversion_failed_error(a, sconv, "Uname"); 1608 if (err == ARCHIVE_FATAL) 1609 return (err); 1610 /* Use a converted an original name. */ 1611 archive_entry_copy_uname(entry, tar->entry_uname.s); 1612 } 1613 } 1614 return (err); 1615} 1616 1617static int 1618pax_attribute_xattr(struct archive_entry *entry, 1619 char *name, char *value) 1620{ 1621 char *name_decoded; 1622 void *value_decoded; 1623 size_t value_len; 1624 1625 if (strlen(name) < 18 || (memcmp(name, "LIBARCHIVE.xattr.", 17)) != 0) 1626 return 3; 1627 1628 name += 17; 1629 1630 /* URL-decode name */ 1631 name_decoded = url_decode(name); 1632 if (name_decoded == NULL) 1633 return 2; 1634 1635 /* Base-64 decode value */ 1636 value_decoded = base64_decode(value, strlen(value), &value_len); 1637 if (value_decoded == NULL) { 1638 free(name_decoded); 1639 return 1; 1640 } 1641 1642 archive_entry_xattr_add_entry(entry, name_decoded, 1643 value_decoded, value_len); 1644 1645 free(name_decoded); 1646 free(value_decoded); 1647 return 0; 1648} 1649 1650/* 1651 * Parse a single key=value attribute. key/value pointers are 1652 * assumed to point into reasonably long-lived storage. 1653 * 1654 * Note that POSIX reserves all-lowercase keywords. Vendor-specific 1655 * extensions should always have keywords of the form "VENDOR.attribute" 1656 * In particular, it's quite feasible to support many different 1657 * vendor extensions here. I'm using "LIBARCHIVE" for extensions 1658 * unique to this library. 1659 * 1660 * Investigate other vendor-specific extensions and see if 1661 * any of them look useful. 1662 */ 1663static int 1664pax_attribute(struct archive_read *a, struct tar *tar, 1665 struct archive_entry *entry, char *key, char *value) 1666{ 1667 int64_t s; 1668 long n; 1669 int err = ARCHIVE_OK, r; 1670 1671#ifndef __FreeBSD__ 1672 if (value == NULL) 1673 value = ""; /* Disable compiler warning; do not pass 1674 * NULL pointer to strlen(). */ 1675#endif 1676 switch (key[0]) { 1677 case 'G': 1678 /* GNU "0.0" sparse pax format. */ 1679 if (strcmp(key, "GNU.sparse.numblocks") == 0) { 1680 tar->sparse_offset = -1; 1681 tar->sparse_numbytes = -1; 1682 tar->sparse_gnu_major = 0; 1683 tar->sparse_gnu_minor = 0; 1684 } 1685 if (strcmp(key, "GNU.sparse.offset") == 0) { 1686 tar->sparse_offset = tar_atol10(value, strlen(value)); 1687 if (tar->sparse_numbytes != -1) { 1688 if (gnu_add_sparse_entry(a, tar, 1689 tar->sparse_offset, tar->sparse_numbytes) 1690 != ARCHIVE_OK) 1691 return (ARCHIVE_FATAL); 1692 tar->sparse_offset = -1; 1693 tar->sparse_numbytes = -1; 1694 } 1695 } 1696 if (strcmp(key, "GNU.sparse.numbytes") == 0) { 1697 tar->sparse_numbytes = tar_atol10(value, strlen(value)); 1698 if (tar->sparse_numbytes != -1) { 1699 if (gnu_add_sparse_entry(a, tar, 1700 tar->sparse_offset, tar->sparse_numbytes) 1701 != ARCHIVE_OK) 1702 return (ARCHIVE_FATAL); 1703 tar->sparse_offset = -1; 1704 tar->sparse_numbytes = -1; 1705 } 1706 } 1707 if (strcmp(key, "GNU.sparse.size") == 0) { 1708 tar->realsize = tar_atol10(value, strlen(value)); 1709 archive_entry_set_size(entry, tar->realsize); 1710 } 1711 1712 /* GNU "0.1" sparse pax format. */ 1713 if (strcmp(key, "GNU.sparse.map") == 0) { 1714 tar->sparse_gnu_major = 0; 1715 tar->sparse_gnu_minor = 1; 1716 if (gnu_sparse_01_parse(a, tar, value) != ARCHIVE_OK) 1717 return (ARCHIVE_WARN); 1718 } 1719 1720 /* GNU "1.0" sparse pax format */ 1721 if (strcmp(key, "GNU.sparse.major") == 0) { 1722 tar->sparse_gnu_major = (int)tar_atol10(value, strlen(value)); 1723 tar->sparse_gnu_pending = 1; 1724 } 1725 if (strcmp(key, "GNU.sparse.minor") == 0) { 1726 tar->sparse_gnu_minor = (int)tar_atol10(value, strlen(value)); 1727 tar->sparse_gnu_pending = 1; 1728 } 1729 if (strcmp(key, "GNU.sparse.name") == 0) { 1730 /* 1731 * The real filename; when storing sparse 1732 * files, GNU tar puts a synthesized name into 1733 * the regular 'path' attribute in an attempt 1734 * to limit confusion. ;-) 1735 */ 1736 archive_strcpy(&(tar->entry_pathname_override), value); 1737 } 1738 if (strcmp(key, "GNU.sparse.realsize") == 0) { 1739 tar->realsize = tar_atol10(value, strlen(value)); 1740 archive_entry_set_size(entry, tar->realsize); 1741 } 1742 break; 1743 case 'L': 1744 /* Our extensions */ 1745/* TODO: Handle arbitrary extended attributes... */ 1746/* 1747 if (strcmp(key, "LIBARCHIVE.xxxxxxx") == 0) 1748 archive_entry_set_xxxxxx(entry, value); 1749*/ 1750 if (strcmp(key, "LIBARCHIVE.creationtime") == 0) { 1751 pax_time(value, &s, &n); 1752 archive_entry_set_birthtime(entry, s, n); 1753 } 1754 if (memcmp(key, "LIBARCHIVE.xattr.", 17) == 0) 1755 pax_attribute_xattr(entry, key, value); 1756 break; 1757 case 'S': 1758 /* We support some keys used by the "star" archiver */ 1759 if (strcmp(key, "SCHILY.acl.access") == 0) { 1760 if (tar->sconv_acl == NULL) { 1761 tar->sconv_acl = 1762 archive_string_conversion_from_charset( 1763 &(a->archive), "UTF-8", 1); 1764 if (tar->sconv_acl == NULL) 1765 return (ARCHIVE_FATAL); 1766 } 1767 1768 r = archive_acl_parse_l(archive_entry_acl(entry), 1769 value, ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 1770 tar->sconv_acl); 1771 if (r != ARCHIVE_OK) { 1772 err = r; 1773 if (err == ARCHIVE_FATAL) { 1774 archive_set_error(&a->archive, ENOMEM, 1775 "Can't allocate memory for " 1776 "SCHILY.acl.access"); 1777 return (err); 1778 } 1779 archive_set_error(&a->archive, 1780 ARCHIVE_ERRNO_MISC, 1781 "Parse error: SCHILY.acl.access"); 1782 } 1783 } else if (strcmp(key, "SCHILY.acl.default") == 0) { 1784 if (tar->sconv_acl == NULL) { 1785 tar->sconv_acl = 1786 archive_string_conversion_from_charset( 1787 &(a->archive), "UTF-8", 1); 1788 if (tar->sconv_acl == NULL) 1789 return (ARCHIVE_FATAL); 1790 } 1791 1792 r = archive_acl_parse_l(archive_entry_acl(entry), 1793 value, ARCHIVE_ENTRY_ACL_TYPE_DEFAULT, 1794 tar->sconv_acl); 1795 if (r != ARCHIVE_OK) { 1796 err = r; 1797 if (err == ARCHIVE_FATAL) { 1798 archive_set_error(&a->archive, ENOMEM, 1799 "Can't allocate memory for " 1800 "SCHILY.acl.default"); 1801 return (err); 1802 } 1803 archive_set_error(&a->archive, 1804 ARCHIVE_ERRNO_MISC, 1805 "Parse error: SCHILY.acl.default"); 1806 } 1807 } else if (strcmp(key, "SCHILY.devmajor") == 0) { 1808 archive_entry_set_rdevmajor(entry, 1809 (dev_t)tar_atol10(value, strlen(value))); 1810 } else if (strcmp(key, "SCHILY.devminor") == 0) { 1811 archive_entry_set_rdevminor(entry, 1812 (dev_t)tar_atol10(value, strlen(value))); 1813 } else if (strcmp(key, "SCHILY.fflags") == 0) { 1814 archive_entry_copy_fflags_text(entry, value); 1815 } else if (strcmp(key, "SCHILY.dev") == 0) { 1816 archive_entry_set_dev(entry, 1817 (dev_t)tar_atol10(value, strlen(value))); 1818 } else if (strcmp(key, "SCHILY.ino") == 0) { 1819 archive_entry_set_ino(entry, 1820 tar_atol10(value, strlen(value))); 1821 } else if (strcmp(key, "SCHILY.nlink") == 0) { 1822 archive_entry_set_nlink(entry, (unsigned) 1823 tar_atol10(value, strlen(value))); 1824 } else if (strcmp(key, "SCHILY.realsize") == 0) { 1825 tar->realsize = tar_atol10(value, strlen(value)); 1826 archive_entry_set_size(entry, tar->realsize); 1827 } else if (strcmp(key, "SUN.holesdata") == 0) { 1828 /* A Solaris extension for sparse. */ 1829 r = solaris_sparse_parse(a, tar, entry, value); 1830 if (r < err) { 1831 if (r == ARCHIVE_FATAL) 1832 return (r); 1833 err = r; 1834 archive_set_error(&a->archive, 1835 ARCHIVE_ERRNO_MISC, 1836 "Parse error: SUN.holesdata"); 1837 } 1838 } 1839 break; 1840 case 'a': 1841 if (strcmp(key, "atime") == 0) { 1842 pax_time(value, &s, &n); 1843 archive_entry_set_atime(entry, s, n); 1844 } 1845 break; 1846 case 'c': 1847 if (strcmp(key, "ctime") == 0) { 1848 pax_time(value, &s, &n); 1849 archive_entry_set_ctime(entry, s, n); 1850 } else if (strcmp(key, "charset") == 0) { 1851 /* TODO: Publish charset information in entry. */ 1852 } else if (strcmp(key, "comment") == 0) { 1853 /* TODO: Publish comment in entry. */ 1854 } 1855 break; 1856 case 'g': 1857 if (strcmp(key, "gid") == 0) { 1858 archive_entry_set_gid(entry, 1859 tar_atol10(value, strlen(value))); 1860 } else if (strcmp(key, "gname") == 0) { 1861 archive_strcpy(&(tar->entry_gname), value); 1862 } 1863 break; 1864 case 'h': 1865 if (strcmp(key, "hdrcharset") == 0) { 1866 if (strcmp(value, "BINARY") == 0) 1867 /* Binary mode. */ 1868 tar->pax_hdrcharset_binary = 1; 1869 else if (strcmp(value, "ISO-IR 10646 2000 UTF-8") == 0) 1870 tar->pax_hdrcharset_binary = 0; 1871 } 1872 break; 1873 case 'l': 1874 /* pax interchange doesn't distinguish hardlink vs. symlink. */ 1875 if (strcmp(key, "linkpath") == 0) { 1876 archive_strcpy(&(tar->entry_linkpath), value); 1877 } 1878 break; 1879 case 'm': 1880 if (strcmp(key, "mtime") == 0) { 1881 pax_time(value, &s, &n); 1882 archive_entry_set_mtime(entry, s, n); 1883 } 1884 break; 1885 case 'p': 1886 if (strcmp(key, "path") == 0) { 1887 archive_strcpy(&(tar->entry_pathname), value); 1888 } 1889 break; 1890 case 'r': 1891 /* POSIX has reserved 'realtime.*' */ 1892 break; 1893 case 's': 1894 /* POSIX has reserved 'security.*' */ 1895 /* Someday: if (strcmp(key, "security.acl") == 0) { ... } */ 1896 if (strcmp(key, "size") == 0) { 1897 /* "size" is the size of the data in the entry. */ 1898 tar->entry_bytes_remaining 1899 = tar_atol10(value, strlen(value)); 1900 /* 1901 * But, "size" is not necessarily the size of 1902 * the file on disk; if this is a sparse file, 1903 * the disk size may have already been set from 1904 * GNU.sparse.realsize or GNU.sparse.size or 1905 * an old GNU header field or SCHILY.realsize 1906 * or .... 1907 */ 1908 if (tar->realsize < 0) { 1909 archive_entry_set_size(entry, 1910 tar->entry_bytes_remaining); 1911 tar->realsize 1912 = tar->entry_bytes_remaining; 1913 } 1914 } 1915 break; 1916 case 'u': 1917 if (strcmp(key, "uid") == 0) { 1918 archive_entry_set_uid(entry, 1919 tar_atol10(value, strlen(value))); 1920 } else if (strcmp(key, "uname") == 0) { 1921 archive_strcpy(&(tar->entry_uname), value); 1922 } 1923 break; 1924 } 1925 return (err); 1926} 1927 1928 1929 1930/* 1931 * parse a decimal time value, which may include a fractional portion 1932 */ 1933static void 1934pax_time(const char *p, int64_t *ps, long *pn) 1935{ 1936 char digit; 1937 int64_t s; 1938 unsigned long l; 1939 int sign; 1940 int64_t limit, last_digit_limit; 1941 1942 limit = INT64_MAX / 10; 1943 last_digit_limit = INT64_MAX % 10; 1944 1945 s = 0; 1946 sign = 1; 1947 if (*p == '-') { 1948 sign = -1; 1949 p++; 1950 } 1951 while (*p >= '0' && *p <= '9') { 1952 digit = *p - '0'; 1953 if (s > limit || 1954 (s == limit && digit > last_digit_limit)) { 1955 s = INT64_MAX; 1956 break; 1957 } 1958 s = (s * 10) + digit; 1959 ++p; 1960 } 1961 1962 *ps = s * sign; 1963 1964 /* Calculate nanoseconds. */ 1965 *pn = 0; 1966 1967 if (*p != '.') 1968 return; 1969 1970 l = 100000000UL; 1971 do { 1972 ++p; 1973 if (*p >= '0' && *p <= '9') 1974 *pn += (*p - '0') * l; 1975 else 1976 break; 1977 } while (l /= 10); 1978} 1979 1980/* 1981 * Parse GNU tar header 1982 */ 1983static int 1984header_gnutar(struct archive_read *a, struct tar *tar, 1985 struct archive_entry *entry, const void *h, size_t *unconsumed) 1986{ 1987 const struct archive_entry_header_gnutar *header; 1988 int64_t t; 1989 int err = ARCHIVE_OK; 1990 1991 /* 1992 * GNU header is like POSIX ustar, except 'prefix' is 1993 * replaced with some other fields. This also means the 1994 * filename is stored as in old-style archives. 1995 */ 1996 1997 /* Grab fields common to all tar variants. */ 1998 err = header_common(a, tar, entry, h); 1999 if (err == ARCHIVE_FATAL) 2000 return (err); 2001 2002 /* Copy filename over (to ensure null termination). */ 2003 header = (const struct archive_entry_header_gnutar *)h; 2004 if (archive_entry_copy_pathname_l(entry, 2005 header->name, sizeof(header->name), tar->sconv) != 0) { 2006 err = set_conversion_failed_error(a, tar->sconv, "Pathname"); 2007 if (err == ARCHIVE_FATAL) 2008 return (err); 2009 } 2010 2011 /* Fields common to ustar and GNU */ 2012 /* XXX Can the following be factored out since it's common 2013 * to ustar and gnu tar? Is it okay to move it down into 2014 * header_common, perhaps? */ 2015 if (archive_entry_copy_uname_l(entry, 2016 header->uname, sizeof(header->uname), tar->sconv) != 0) { 2017 err = set_conversion_failed_error(a, tar->sconv, "Uname"); 2018 if (err == ARCHIVE_FATAL) 2019 return (err); 2020 } 2021 2022 if (archive_entry_copy_gname_l(entry, 2023 header->gname, sizeof(header->gname), tar->sconv) != 0) { 2024 err = set_conversion_failed_error(a, tar->sconv, "Gname"); 2025 if (err == ARCHIVE_FATAL) 2026 return (err); 2027 } 2028 2029 /* Parse out device numbers only for char and block specials */ 2030 if (header->typeflag[0] == '3' || header->typeflag[0] == '4') { 2031 archive_entry_set_rdevmajor(entry, (dev_t) 2032 tar_atol(header->rdevmajor, sizeof(header->rdevmajor))); 2033 archive_entry_set_rdevminor(entry, (dev_t) 2034 tar_atol(header->rdevminor, sizeof(header->rdevminor))); 2035 } else 2036 archive_entry_set_rdev(entry, 0); 2037 2038 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining); 2039 2040 /* Grab GNU-specific fields. */ 2041 t = tar_atol(header->atime, sizeof(header->atime)); 2042 if (t > 0) 2043 archive_entry_set_atime(entry, t, 0); 2044 t = tar_atol(header->ctime, sizeof(header->ctime)); 2045 if (t > 0) 2046 archive_entry_set_ctime(entry, t, 0); 2047 2048 if (header->realsize[0] != 0) { 2049 tar->realsize 2050 = tar_atol(header->realsize, sizeof(header->realsize)); 2051 archive_entry_set_size(entry, tar->realsize); 2052 } 2053 2054 if (header->sparse[0].offset[0] != 0) { 2055 if (gnu_sparse_old_read(a, tar, header, unconsumed) 2056 != ARCHIVE_OK) 2057 return (ARCHIVE_FATAL); 2058 } else { 2059 if (header->isextended[0] != 0) { 2060 /* XXX WTF? XXX */ 2061 } 2062 } 2063 2064 return (err); 2065} 2066 2067static int 2068gnu_add_sparse_entry(struct archive_read *a, struct tar *tar, 2069 int64_t offset, int64_t remaining) 2070{ 2071 struct sparse_block *p; 2072 2073 p = (struct sparse_block *)malloc(sizeof(*p)); 2074 if (p == NULL) { 2075 archive_set_error(&a->archive, ENOMEM, "Out of memory"); 2076 return (ARCHIVE_FATAL); 2077 } 2078 memset(p, 0, sizeof(*p)); 2079 if (tar->sparse_last != NULL) 2080 tar->sparse_last->next = p; 2081 else 2082 tar->sparse_list = p; 2083 tar->sparse_last = p; 2084 p->offset = offset; 2085 p->remaining = remaining; 2086 return (ARCHIVE_OK); 2087} 2088 2089static void 2090gnu_clear_sparse_list(struct tar *tar) 2091{ 2092 struct sparse_block *p; 2093 2094 while (tar->sparse_list != NULL) { 2095 p = tar->sparse_list; 2096 tar->sparse_list = p->next; 2097 free(p); 2098 } 2099 tar->sparse_last = NULL; 2100} 2101 2102/* 2103 * GNU tar old-format sparse data. 2104 * 2105 * GNU old-format sparse data is stored in a fixed-field 2106 * format. Offset/size values are 11-byte octal fields (same 2107 * format as 'size' field in ustart header). These are 2108 * stored in the header, allocating subsequent header blocks 2109 * as needed. Extending the header in this way is a pretty 2110 * severe POSIX violation; this design has earned GNU tar a 2111 * lot of criticism. 2112 */ 2113 2114static int 2115gnu_sparse_old_read(struct archive_read *a, struct tar *tar, 2116 const struct archive_entry_header_gnutar *header, size_t *unconsumed) 2117{ 2118 ssize_t bytes_read; 2119 const void *data; 2120 struct extended { 2121 struct gnu_sparse sparse[21]; 2122 char isextended[1]; 2123 char padding[7]; 2124 }; 2125 const struct extended *ext; 2126 2127 if (gnu_sparse_old_parse(a, tar, header->sparse, 4) != ARCHIVE_OK) 2128 return (ARCHIVE_FATAL); 2129 if (header->isextended[0] == 0) 2130 return (ARCHIVE_OK); 2131 2132 do { 2133 tar_flush_unconsumed(a, unconsumed); 2134 data = __archive_read_ahead(a, 512, &bytes_read); 2135 if (bytes_read < 0) 2136 return (ARCHIVE_FATAL); 2137 if (bytes_read < 512) { 2138 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 2139 "Truncated tar archive " 2140 "detected while reading sparse file data"); 2141 return (ARCHIVE_FATAL); 2142 } 2143 *unconsumed = 512; 2144 ext = (const struct extended *)data; 2145 if (gnu_sparse_old_parse(a, tar, ext->sparse, 21) != ARCHIVE_OK) 2146 return (ARCHIVE_FATAL); 2147 } while (ext->isextended[0] != 0); 2148 if (tar->sparse_list != NULL) 2149 tar->entry_offset = tar->sparse_list->offset; 2150 return (ARCHIVE_OK); 2151} 2152 2153static int 2154gnu_sparse_old_parse(struct archive_read *a, struct tar *tar, 2155 const struct gnu_sparse *sparse, int length) 2156{ 2157 while (length > 0 && sparse->offset[0] != 0) { 2158 if (gnu_add_sparse_entry(a, tar, 2159 tar_atol(sparse->offset, sizeof(sparse->offset)), 2160 tar_atol(sparse->numbytes, sizeof(sparse->numbytes))) 2161 != ARCHIVE_OK) 2162 return (ARCHIVE_FATAL); 2163 sparse++; 2164 length--; 2165 } 2166 return (ARCHIVE_OK); 2167} 2168 2169/* 2170 * GNU tar sparse format 0.0 2171 * 2172 * Beginning with GNU tar 1.15, sparse files are stored using 2173 * information in the pax extended header. The GNU tar maintainers 2174 * have gone through a number of variations in the process of working 2175 * out this scheme; fortunately, they're all numbered. 2176 * 2177 * Sparse format 0.0 uses attribute GNU.sparse.numblocks to store the 2178 * number of blocks, and GNU.sparse.offset/GNU.sparse.numbytes to 2179 * store offset/size for each block. The repeated instances of these 2180 * latter fields violate the pax specification (which frowns on 2181 * duplicate keys), so this format was quickly replaced. 2182 */ 2183 2184/* 2185 * GNU tar sparse format 0.1 2186 * 2187 * This version replaced the offset/numbytes attributes with 2188 * a single "map" attribute that stored a list of integers. This 2189 * format had two problems: First, the "map" attribute could be very 2190 * long, which caused problems for some implementations. More 2191 * importantly, the sparse data was lost when extracted by archivers 2192 * that didn't recognize this extension. 2193 */ 2194 2195static int 2196gnu_sparse_01_parse(struct archive_read *a, struct tar *tar, const char *p) 2197{ 2198 const char *e; 2199 int64_t offset = -1, size = -1; 2200 2201 for (;;) { 2202 e = p; 2203 while (*e != '\0' && *e != ',') { 2204 if (*e < '0' || *e > '9') 2205 return (ARCHIVE_WARN); 2206 e++; 2207 } 2208 if (offset < 0) { 2209 offset = tar_atol10(p, e - p); 2210 if (offset < 0) 2211 return (ARCHIVE_WARN); 2212 } else { 2213 size = tar_atol10(p, e - p); 2214 if (size < 0) 2215 return (ARCHIVE_WARN); 2216 if (gnu_add_sparse_entry(a, tar, offset, size) 2217 != ARCHIVE_OK) 2218 return (ARCHIVE_FATAL); 2219 offset = -1; 2220 } 2221 if (*e == '\0') 2222 return (ARCHIVE_OK); 2223 p = e + 1; 2224 } 2225} 2226 2227/* 2228 * GNU tar sparse format 1.0 2229 * 2230 * The idea: The offset/size data is stored as a series of base-10 2231 * ASCII numbers prepended to the file data, so that dearchivers that 2232 * don't support this format will extract the block map along with the 2233 * data and a separate post-process can restore the sparseness. 2234 * 2235 * Unfortunately, GNU tar 1.16 had a bug that added unnecessary 2236 * padding to the body of the file when using this format. GNU tar 2237 * 1.17 corrected this bug without bumping the version number, so 2238 * it's not possible to support both variants. This code supports 2239 * the later variant at the expense of not supporting the former. 2240 * 2241 * This variant also replaced GNU.sparse.size with GNU.sparse.realsize 2242 * and introduced the GNU.sparse.major/GNU.sparse.minor attributes. 2243 */ 2244 2245/* 2246 * Read the next line from the input, and parse it as a decimal 2247 * integer followed by '\n'. Returns positive integer value or 2248 * negative on error. 2249 */ 2250static int64_t 2251gnu_sparse_10_atol(struct archive_read *a, struct tar *tar, 2252 int64_t *remaining, size_t *unconsumed) 2253{ 2254 int64_t l, limit, last_digit_limit; 2255 const char *p; 2256 ssize_t bytes_read; 2257 int base, digit; 2258 2259 base = 10; 2260 limit = INT64_MAX / base; 2261 last_digit_limit = INT64_MAX % base; 2262 2263 /* 2264 * Skip any lines starting with '#'; GNU tar specs 2265 * don't require this, but they should. 2266 */ 2267 do { 2268 bytes_read = readline(a, tar, &p, 2269 (ssize_t)tar_min(*remaining, 100), unconsumed); 2270 if (bytes_read <= 0) 2271 return (ARCHIVE_FATAL); 2272 *remaining -= bytes_read; 2273 } while (p[0] == '#'); 2274 2275 l = 0; 2276 while (bytes_read > 0) { 2277 if (*p == '\n') 2278 return (l); 2279 if (*p < '0' || *p >= '0' + base) 2280 return (ARCHIVE_WARN); 2281 digit = *p - '0'; 2282 if (l > limit || (l == limit && digit > last_digit_limit)) 2283 l = INT64_MAX; /* Truncate on overflow. */ 2284 else 2285 l = (l * base) + digit; 2286 p++; 2287 bytes_read--; 2288 } 2289 /* TODO: Error message. */ 2290 return (ARCHIVE_WARN); 2291} 2292 2293/* 2294 * Returns length (in bytes) of the sparse data description 2295 * that was read. 2296 */ 2297static ssize_t 2298gnu_sparse_10_read(struct archive_read *a, struct tar *tar, size_t *unconsumed) 2299{ 2300 ssize_t bytes_read; 2301 int entries; 2302 int64_t offset, size, to_skip, remaining; 2303 2304 /* Clear out the existing sparse list. */ 2305 gnu_clear_sparse_list(tar); 2306 2307 remaining = tar->entry_bytes_remaining; 2308 2309 /* Parse entries. */ 2310 entries = (int)gnu_sparse_10_atol(a, tar, &remaining, unconsumed); 2311 if (entries < 0) 2312 return (ARCHIVE_FATAL); 2313 /* Parse the individual entries. */ 2314 while (entries-- > 0) { 2315 /* Parse offset/size */ 2316 offset = gnu_sparse_10_atol(a, tar, &remaining, unconsumed); 2317 if (offset < 0) 2318 return (ARCHIVE_FATAL); 2319 size = gnu_sparse_10_atol(a, tar, &remaining, unconsumed); 2320 if (size < 0) 2321 return (ARCHIVE_FATAL); 2322 /* Add a new sparse entry. */ 2323 if (gnu_add_sparse_entry(a, tar, offset, size) != ARCHIVE_OK) 2324 return (ARCHIVE_FATAL); 2325 } 2326 /* Skip rest of block... */ 2327 tar_flush_unconsumed(a, unconsumed); 2328 bytes_read = (ssize_t)(tar->entry_bytes_remaining - remaining); 2329 to_skip = 0x1ff & -bytes_read; 2330 if (to_skip != __archive_read_consume(a, to_skip)) 2331 return (ARCHIVE_FATAL); 2332 return ((ssize_t)(bytes_read + to_skip)); 2333} 2334 2335/* 2336 * Solaris pax extension for a sparse file. This is recorded with the 2337 * data and hole pairs. The way recording sparse information by Solaris' 2338 * pax simply indicates where data and sparse are, so the stored contents 2339 * consist of both data and hole. 2340 */ 2341static int 2342solaris_sparse_parse(struct archive_read *a, struct tar *tar, 2343 struct archive_entry *entry, const char *p) 2344{ 2345 const char *e; 2346 int64_t start, end; 2347 int hole = 1; 2348 2349 (void)entry; /* UNUSED */ 2350 2351 end = 0; 2352 if (*p == ' ') 2353 p++; 2354 else 2355 return (ARCHIVE_WARN); 2356 for (;;) { 2357 e = p; 2358 while (*e != '\0' && *e != ' ') { 2359 if (*e < '0' || *e > '9') 2360 return (ARCHIVE_WARN); 2361 e++; 2362 } 2363 start = end; 2364 end = tar_atol10(p, e - p); 2365 if (end < 0) 2366 return (ARCHIVE_WARN); 2367 if (start < end) { 2368 if (gnu_add_sparse_entry(a, tar, start, 2369 end - start) != ARCHIVE_OK) 2370 return (ARCHIVE_FATAL); 2371 tar->sparse_last->hole = hole; 2372 } 2373 if (*e == '\0') 2374 return (ARCHIVE_OK); 2375 p = e + 1; 2376 hole = hole == 0; 2377 } 2378} 2379 2380/*- 2381 * Convert text->integer. 2382 * 2383 * Traditional tar formats (including POSIX) specify base-8 for 2384 * all of the standard numeric fields. This is a significant limitation 2385 * in practice: 2386 * = file size is limited to 8GB 2387 * = rdevmajor and rdevminor are limited to 21 bits 2388 * = uid/gid are limited to 21 bits 2389 * 2390 * There are two workarounds for this: 2391 * = pax extended headers, which use variable-length string fields 2392 * = GNU tar and STAR both allow either base-8 or base-256 in 2393 * most fields. The high bit is set to indicate base-256. 2394 * 2395 * On read, this implementation supports both extensions. 2396 */ 2397static int64_t 2398tar_atol(const char *p, size_t char_cnt) 2399{ 2400 /* 2401 * Technically, GNU tar considers a field to be in base-256 2402 * only if the first byte is 0xff or 0x80. 2403 */ 2404 if (*p & 0x80) 2405 return (tar_atol256(p, char_cnt)); 2406 return (tar_atol8(p, char_cnt)); 2407} 2408 2409/* 2410 * Note that this implementation does not (and should not!) obey 2411 * locale settings; you cannot simply substitute strtol here, since 2412 * it does obey locale. 2413 */ 2414static int64_t 2415tar_atol_base_n(const char *p, size_t char_cnt, int base) 2416{ 2417 int64_t l, limit, last_digit_limit; 2418 int digit, sign; 2419 2420 limit = INT64_MAX / base; 2421 last_digit_limit = INT64_MAX % base; 2422 2423 /* the pointer will not be dereferenced if char_cnt is zero 2424 * due to the way the && operator is evaulated. 2425 */ 2426 while (char_cnt != 0 && (*p == ' ' || *p == '\t')) { 2427 p++; 2428 char_cnt--; 2429 } 2430 2431 sign = 1; 2432 if (char_cnt != 0 && *p == '-') { 2433 sign = -1; 2434 p++; 2435 char_cnt--; 2436 } 2437 2438 l = 0; 2439 if (char_cnt != 0) { 2440 digit = *p - '0'; 2441 while (digit >= 0 && digit < base && char_cnt != 0) { 2442 if (l>limit || (l == limit && digit > last_digit_limit)) { 2443 l = INT64_MAX; /* Truncate on overflow. */ 2444 break; 2445 } 2446 l = (l * base) + digit; 2447 digit = *++p - '0'; 2448 char_cnt--; 2449 } 2450 } 2451 return (sign < 0) ? -l : l; 2452} 2453 2454static int64_t 2455tar_atol8(const char *p, size_t char_cnt) 2456{ 2457 return tar_atol_base_n(p, char_cnt, 8); 2458} 2459 2460static int64_t 2461tar_atol10(const char *p, size_t char_cnt) 2462{ 2463 return tar_atol_base_n(p, char_cnt, 10); 2464} 2465 2466/* 2467 * Parse a base-256 integer. This is just a straight signed binary 2468 * value in big-endian order, except that the high-order bit is 2469 * ignored. 2470 */ 2471static int64_t 2472tar_atol256(const char *_p, size_t char_cnt) 2473{ 2474 int64_t l, upper_limit, lower_limit; 2475 const unsigned char *p = (const unsigned char *)_p; 2476 2477 upper_limit = INT64_MAX / 256; 2478 lower_limit = INT64_MIN / 256; 2479 2480 /* Pad with 1 or 0 bits, depending on sign. */ 2481 if ((0x40 & *p) == 0x40) 2482 l = (int64_t)-1; 2483 else 2484 l = 0; 2485 l = (l << 6) | (0x3f & *p++); 2486 while (--char_cnt > 0) { 2487 if (l > upper_limit) { 2488 l = INT64_MAX; /* Truncate on overflow */ 2489 break; 2490 } else if (l < lower_limit) { 2491 l = INT64_MIN; 2492 break; 2493 } 2494 l = (l << 8) | (0xff & (int64_t)*p++); 2495 } 2496 return (l); 2497} 2498 2499/* 2500 * Returns length of line (including trailing newline) 2501 * or negative on error. 'start' argument is updated to 2502 * point to first character of line. This avoids copying 2503 * when possible. 2504 */ 2505static ssize_t 2506readline(struct archive_read *a, struct tar *tar, const char **start, 2507 ssize_t limit, size_t *unconsumed) 2508{ 2509 ssize_t bytes_read; 2510 ssize_t total_size = 0; 2511 const void *t; 2512 const char *s; 2513 void *p; 2514 2515 tar_flush_unconsumed(a, unconsumed); 2516 2517 t = __archive_read_ahead(a, 1, &bytes_read); 2518 if (bytes_read <= 0) 2519 return (ARCHIVE_FATAL); 2520 s = t; /* Start of line? */ 2521 p = memchr(t, '\n', bytes_read); 2522 /* If we found '\n' in the read buffer, return pointer to that. */ 2523 if (p != NULL) { 2524 bytes_read = 1 + ((const char *)p) - s; 2525 if (bytes_read > limit) { 2526 archive_set_error(&a->archive, 2527 ARCHIVE_ERRNO_FILE_FORMAT, 2528 "Line too long"); 2529 return (ARCHIVE_FATAL); 2530 } 2531 *unconsumed = bytes_read; 2532 *start = s; 2533 return (bytes_read); 2534 } 2535 *unconsumed = bytes_read; 2536 /* Otherwise, we need to accumulate in a line buffer. */ 2537 for (;;) { 2538 if (total_size + bytes_read > limit) { 2539 archive_set_error(&a->archive, 2540 ARCHIVE_ERRNO_FILE_FORMAT, 2541 "Line too long"); 2542 return (ARCHIVE_FATAL); 2543 } 2544 if (archive_string_ensure(&tar->line, total_size + bytes_read) == NULL) { 2545 archive_set_error(&a->archive, ENOMEM, 2546 "Can't allocate working buffer"); 2547 return (ARCHIVE_FATAL); 2548 } 2549 memcpy(tar->line.s + total_size, t, bytes_read); 2550 tar_flush_unconsumed(a, unconsumed); 2551 total_size += bytes_read; 2552 /* If we found '\n', clean up and return. */ 2553 if (p != NULL) { 2554 *start = tar->line.s; 2555 return (total_size); 2556 } 2557 /* Read some more. */ 2558 t = __archive_read_ahead(a, 1, &bytes_read); 2559 if (bytes_read <= 0) 2560 return (ARCHIVE_FATAL); 2561 s = t; /* Start of line? */ 2562 p = memchr(t, '\n', bytes_read); 2563 /* If we found '\n', trim the read. */ 2564 if (p != NULL) { 2565 bytes_read = 1 + ((const char *)p) - s; 2566 } 2567 *unconsumed = bytes_read; 2568 } 2569} 2570 2571/* 2572 * base64_decode - Base64 decode 2573 * 2574 * This accepts most variations of base-64 encoding, including: 2575 * * with or without line breaks 2576 * * with or without the final group padded with '=' or '_' characters 2577 * (The most economical Base-64 variant does not pad the last group and 2578 * omits line breaks; RFC1341 used for MIME requires both.) 2579 */ 2580static char * 2581base64_decode(const char *s, size_t len, size_t *out_len) 2582{ 2583 static const unsigned char digits[64] = { 2584 'A','B','C','D','E','F','G','H','I','J','K','L','M','N', 2585 'O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b', 2586 'c','d','e','f','g','h','i','j','k','l','m','n','o','p', 2587 'q','r','s','t','u','v','w','x','y','z','0','1','2','3', 2588 '4','5','6','7','8','9','+','/' }; 2589 static unsigned char decode_table[128]; 2590 char *out, *d; 2591 const unsigned char *src = (const unsigned char *)s; 2592 2593 /* If the decode table is not yet initialized, prepare it. */ 2594 if (decode_table[digits[1]] != 1) { 2595 unsigned i; 2596 memset(decode_table, 0xff, sizeof(decode_table)); 2597 for (i = 0; i < sizeof(digits); i++) 2598 decode_table[digits[i]] = i; 2599 } 2600 2601 /* Allocate enough space to hold the entire output. */ 2602 /* Note that we may not use all of this... */ 2603 out = (char *)malloc(len - len / 4 + 1); 2604 if (out == NULL) { 2605 *out_len = 0; 2606 return (NULL); 2607 } 2608 d = out; 2609 2610 while (len > 0) { 2611 /* Collect the next group of (up to) four characters. */ 2612 int v = 0; 2613 int group_size = 0; 2614 while (group_size < 4 && len > 0) { 2615 /* '=' or '_' padding indicates final group. */ 2616 if (*src == '=' || *src == '_') { 2617 len = 0; 2618 break; 2619 } 2620 /* Skip illegal characters (including line breaks) */ 2621 if (*src > 127 || *src < 32 2622 || decode_table[*src] == 0xff) { 2623 len--; 2624 src++; 2625 continue; 2626 } 2627 v <<= 6; 2628 v |= decode_table[*src++]; 2629 len --; 2630 group_size++; 2631 } 2632 /* Align a short group properly. */ 2633 v <<= 6 * (4 - group_size); 2634 /* Unpack the group we just collected. */ 2635 switch (group_size) { 2636 case 4: d[2] = v & 0xff; 2637 /* FALLTHROUGH */ 2638 case 3: d[1] = (v >> 8) & 0xff; 2639 /* FALLTHROUGH */ 2640 case 2: d[0] = (v >> 16) & 0xff; 2641 break; 2642 case 1: /* this is invalid! */ 2643 break; 2644 } 2645 d += group_size * 3 / 4; 2646 } 2647 2648 *out_len = d - out; 2649 return (out); 2650} 2651 2652static char * 2653url_decode(const char *in) 2654{ 2655 char *out, *d; 2656 const char *s; 2657 2658 out = (char *)malloc(strlen(in) + 1); 2659 if (out == NULL) 2660 return (NULL); 2661 for (s = in, d = out; *s != '\0'; ) { 2662 if (s[0] == '%' && s[1] != '\0' && s[2] != '\0') { 2663 /* Try to convert % escape */ 2664 int digit1 = tohex(s[1]); 2665 int digit2 = tohex(s[2]); 2666 if (digit1 >= 0 && digit2 >= 0) { 2667 /* Looks good, consume three chars */ 2668 s += 3; 2669 /* Convert output */ 2670 *d++ = ((digit1 << 4) | digit2); 2671 continue; 2672 } 2673 /* Else fall through and treat '%' as normal char */ 2674 } 2675 *d++ = *s++; 2676 } 2677 *d = '\0'; 2678 return (out); 2679} 2680 2681static int 2682tohex(int c) 2683{ 2684 if (c >= '0' && c <= '9') 2685 return (c - '0'); 2686 else if (c >= 'A' && c <= 'F') 2687 return (c - 'A' + 10); 2688 else if (c >= 'a' && c <= 'f') 2689 return (c - 'a' + 10); 2690 else 2691 return (-1); 2692} 2693