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