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: 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 *, size_t); 214 static int64_t tar_atol10(const char *, size_t); 215 static int64_t tar_atol256(const char *, size_t); 216 static int64_t tar_atol8(const char *, size_t); 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 NULL, 257 archive_read_format_tar_cleanup); 258 259 if (r != ARCHIVE_OK) 260 free(tar); 261 return (ARCHIVE_OK); 262 } 263 264 static int 265 archive_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 289 static int 290 archive_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 361 static int 362 archive_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 */ 400 static void 401 tar_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 */ 423 static int 424 archive_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 521 static int 522 archive_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 584 static int 585 archive_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 */ 612 static int 613 tar_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 */ 778 static int 779 checksum(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 */ 825 static int 826 archive_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 */ 839 static int 840 header_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 */ 939 static int 940 header_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 956 static int 957 set_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 */ 974 static int 975 header_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 */ 997 static int 998 header_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 */ 1010 static int 1011 read_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 */ 1059 static int 1060 header_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 */ 1233 static int 1234 header_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 */ 1262 static int 1263 read_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 */ 1332 static int 1333 header_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 1345 static int 1346 header_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 */ 1381 static int 1382 header_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 */ 1449 static int 1450 pax_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 1617 static int 1618 pax_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 */ 1663 static int 1664 pax_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 if (value == NULL) 1672 value = ""; /* Disable compiler warning; do not pass 1673 * NULL pointer to strlen(). */ 1674 switch (key[0]) { 1675 case 'G': 1676 /* GNU "0.0" sparse pax format. */ 1677 if (strcmp(key, "GNU.sparse.numblocks") == 0) { 1678 tar->sparse_offset = -1; 1679 tar->sparse_numbytes = -1; 1680 tar->sparse_gnu_major = 0; 1681 tar->sparse_gnu_minor = 0; 1682 } 1683 if (strcmp(key, "GNU.sparse.offset") == 0) { 1684 tar->sparse_offset = tar_atol10(value, strlen(value)); 1685 if (tar->sparse_numbytes != -1) { 1686 if (gnu_add_sparse_entry(a, tar, 1687 tar->sparse_offset, tar->sparse_numbytes) 1688 != ARCHIVE_OK) 1689 return (ARCHIVE_FATAL); 1690 tar->sparse_offset = -1; 1691 tar->sparse_numbytes = -1; 1692 } 1693 } 1694 if (strcmp(key, "GNU.sparse.numbytes") == 0) { 1695 tar->sparse_numbytes = tar_atol10(value, strlen(value)); 1696 if (tar->sparse_numbytes != -1) { 1697 if (gnu_add_sparse_entry(a, tar, 1698 tar->sparse_offset, tar->sparse_numbytes) 1699 != ARCHIVE_OK) 1700 return (ARCHIVE_FATAL); 1701 tar->sparse_offset = -1; 1702 tar->sparse_numbytes = -1; 1703 } 1704 } 1705 if (strcmp(key, "GNU.sparse.size") == 0) { 1706 tar->realsize = tar_atol10(value, strlen(value)); 1707 archive_entry_set_size(entry, tar->realsize); 1708 } 1709 1710 /* GNU "0.1" sparse pax format. */ 1711 if (strcmp(key, "GNU.sparse.map") == 0) { 1712 tar->sparse_gnu_major = 0; 1713 tar->sparse_gnu_minor = 1; 1714 if (gnu_sparse_01_parse(a, tar, value) != ARCHIVE_OK) 1715 return (ARCHIVE_WARN); 1716 } 1717 1718 /* GNU "1.0" sparse pax format */ 1719 if (strcmp(key, "GNU.sparse.major") == 0) { 1720 tar->sparse_gnu_major = (int)tar_atol10(value, strlen(value)); 1721 tar->sparse_gnu_pending = 1; 1722 } 1723 if (strcmp(key, "GNU.sparse.minor") == 0) { 1724 tar->sparse_gnu_minor = (int)tar_atol10(value, strlen(value)); 1725 tar->sparse_gnu_pending = 1; 1726 } 1727 if (strcmp(key, "GNU.sparse.name") == 0) { 1728 /* 1729 * The real filename; when storing sparse 1730 * files, GNU tar puts a synthesized name into 1731 * the regular 'path' attribute in an attempt 1732 * to limit confusion. ;-) 1733 */ 1734 archive_strcpy(&(tar->entry_pathname_override), value); 1735 } 1736 if (strcmp(key, "GNU.sparse.realsize") == 0) { 1737 tar->realsize = tar_atol10(value, strlen(value)); 1738 archive_entry_set_size(entry, tar->realsize); 1739 } 1740 break; 1741 case 'L': 1742 /* Our extensions */ 1743 /* TODO: Handle arbitrary extended attributes... */ 1744 /* 1745 if (strcmp(key, "LIBARCHIVE.xxxxxxx") == 0) 1746 archive_entry_set_xxxxxx(entry, value); 1747 */ 1748 if (strcmp(key, "LIBARCHIVE.creationtime") == 0) { 1749 pax_time(value, &s, &n); 1750 archive_entry_set_birthtime(entry, s, n); 1751 } 1752 if (memcmp(key, "LIBARCHIVE.xattr.", 17) == 0) 1753 pax_attribute_xattr(entry, key, value); 1754 break; 1755 case 'S': 1756 /* We support some keys used by the "star" archiver */ 1757 if (strcmp(key, "SCHILY.acl.access") == 0) { 1758 if (tar->sconv_acl == NULL) { 1759 tar->sconv_acl = 1760 archive_string_conversion_from_charset( 1761 &(a->archive), "UTF-8", 1); 1762 if (tar->sconv_acl == NULL) 1763 return (ARCHIVE_FATAL); 1764 } 1765 1766 r = archive_acl_parse_l(archive_entry_acl(entry), 1767 value, ARCHIVE_ENTRY_ACL_TYPE_ACCESS, 1768 tar->sconv_acl); 1769 if (r != ARCHIVE_OK) { 1770 err = r; 1771 if (err == ARCHIVE_FATAL) { 1772 archive_set_error(&a->archive, ENOMEM, 1773 "Can't allocate memory for " 1774 "SCHILY.acl.access"); 1775 return (err); 1776 } 1777 archive_set_error(&a->archive, 1778 ARCHIVE_ERRNO_MISC, 1779 "Parse error: SCHILY.acl.access"); 1780 } 1781 } else if (strcmp(key, "SCHILY.acl.default") == 0) { 1782 if (tar->sconv_acl == NULL) { 1783 tar->sconv_acl = 1784 archive_string_conversion_from_charset( 1785 &(a->archive), "UTF-8", 1); 1786 if (tar->sconv_acl == NULL) 1787 return (ARCHIVE_FATAL); 1788 } 1789 1790 r = archive_acl_parse_l(archive_entry_acl(entry), 1791 value, ARCHIVE_ENTRY_ACL_TYPE_DEFAULT, 1792 tar->sconv_acl); 1793 if (r != ARCHIVE_OK) { 1794 err = r; 1795 if (err == ARCHIVE_FATAL) { 1796 archive_set_error(&a->archive, ENOMEM, 1797 "Can't allocate memory for " 1798 "SCHILY.acl.default"); 1799 return (err); 1800 } 1801 archive_set_error(&a->archive, 1802 ARCHIVE_ERRNO_MISC, 1803 "Parse error: SCHILY.acl.default"); 1804 } 1805 } else if (strcmp(key, "SCHILY.devmajor") == 0) { 1806 archive_entry_set_rdevmajor(entry, 1807 (dev_t)tar_atol10(value, strlen(value))); 1808 } else if (strcmp(key, "SCHILY.devminor") == 0) { 1809 archive_entry_set_rdevminor(entry, 1810 (dev_t)tar_atol10(value, strlen(value))); 1811 } else if (strcmp(key, "SCHILY.fflags") == 0) { 1812 archive_entry_copy_fflags_text(entry, value); 1813 } else if (strcmp(key, "SCHILY.dev") == 0) { 1814 archive_entry_set_dev(entry, 1815 (dev_t)tar_atol10(value, strlen(value))); 1816 } else if (strcmp(key, "SCHILY.ino") == 0) { 1817 archive_entry_set_ino(entry, 1818 tar_atol10(value, strlen(value))); 1819 } else if (strcmp(key, "SCHILY.nlink") == 0) { 1820 archive_entry_set_nlink(entry, (unsigned) 1821 tar_atol10(value, strlen(value))); 1822 } else if (strcmp(key, "SCHILY.realsize") == 0) { 1823 tar->realsize = tar_atol10(value, strlen(value)); 1824 archive_entry_set_size(entry, tar->realsize); 1825 } else if (strcmp(key, "SUN.holesdata") == 0) { 1826 /* A Solaris extension for sparse. */ 1827 r = solaris_sparse_parse(a, tar, entry, value); 1828 if (r < err) { 1829 if (r == ARCHIVE_FATAL) 1830 return (r); 1831 err = r; 1832 archive_set_error(&a->archive, 1833 ARCHIVE_ERRNO_MISC, 1834 "Parse error: SUN.holesdata"); 1835 } 1836 } 1837 break; 1838 case 'a': 1839 if (strcmp(key, "atime") == 0) { 1840 pax_time(value, &s, &n); 1841 archive_entry_set_atime(entry, s, n); 1842 } 1843 break; 1844 case 'c': 1845 if (strcmp(key, "ctime") == 0) { 1846 pax_time(value, &s, &n); 1847 archive_entry_set_ctime(entry, s, n); 1848 } else if (strcmp(key, "charset") == 0) { 1849 /* TODO: Publish charset information in entry. */ 1850 } else if (strcmp(key, "comment") == 0) { 1851 /* TODO: Publish comment in entry. */ 1852 } 1853 break; 1854 case 'g': 1855 if (strcmp(key, "gid") == 0) { 1856 archive_entry_set_gid(entry, 1857 tar_atol10(value, strlen(value))); 1858 } else if (strcmp(key, "gname") == 0) { 1859 archive_strcpy(&(tar->entry_gname), value); 1860 } 1861 break; 1862 case 'h': 1863 if (strcmp(key, "hdrcharset") == 0) { 1864 if (strcmp(value, "BINARY") == 0) 1865 /* Binary mode. */ 1866 tar->pax_hdrcharset_binary = 1; 1867 else if (strcmp(value, "ISO-IR 10646 2000 UTF-8") == 0) 1868 tar->pax_hdrcharset_binary = 0; 1869 } 1870 break; 1871 case 'l': 1872 /* pax interchange doesn't distinguish hardlink vs. symlink. */ 1873 if (strcmp(key, "linkpath") == 0) { 1874 archive_strcpy(&(tar->entry_linkpath), value); 1875 } 1876 break; 1877 case 'm': 1878 if (strcmp(key, "mtime") == 0) { 1879 pax_time(value, &s, &n); 1880 archive_entry_set_mtime(entry, s, n); 1881 } 1882 break; 1883 case 'p': 1884 if (strcmp(key, "path") == 0) { 1885 archive_strcpy(&(tar->entry_pathname), value); 1886 } 1887 break; 1888 case 'r': 1889 /* POSIX has reserved 'realtime.*' */ 1890 break; 1891 case 's': 1892 /* POSIX has reserved 'security.*' */ 1893 /* Someday: if (strcmp(key, "security.acl") == 0) { ... } */ 1894 if (strcmp(key, "size") == 0) { 1895 /* "size" is the size of the data in the entry. */ 1896 tar->entry_bytes_remaining 1897 = tar_atol10(value, strlen(value)); 1898 /* 1899 * But, "size" is not necessarily the size of 1900 * the file on disk; if this is a sparse file, 1901 * the disk size may have already been set from 1902 * GNU.sparse.realsize or GNU.sparse.size or 1903 * an old GNU header field or SCHILY.realsize 1904 * or .... 1905 */ 1906 if (tar->realsize < 0) { 1907 archive_entry_set_size(entry, 1908 tar->entry_bytes_remaining); 1909 tar->realsize 1910 = tar->entry_bytes_remaining; 1911 } 1912 } 1913 break; 1914 case 'u': 1915 if (strcmp(key, "uid") == 0) { 1916 archive_entry_set_uid(entry, 1917 tar_atol10(value, strlen(value))); 1918 } else if (strcmp(key, "uname") == 0) { 1919 archive_strcpy(&(tar->entry_uname), value); 1920 } 1921 break; 1922 } 1923 return (err); 1924 } 1925 1926 1927 1928 /* 1929 * parse a decimal time value, which may include a fractional portion 1930 */ 1931 static void 1932 pax_time(const char *p, int64_t *ps, long *pn) 1933 { 1934 char digit; 1935 int64_t s; 1936 unsigned long l; 1937 int sign; 1938 int64_t limit, last_digit_limit; 1939 1940 limit = INT64_MAX / 10; 1941 last_digit_limit = INT64_MAX % 10; 1942 1943 s = 0; 1944 sign = 1; 1945 if (*p == '-') { 1946 sign = -1; 1947 p++; 1948 } 1949 while (*p >= '0' && *p <= '9') { 1950 digit = *p - '0'; 1951 if (s > limit || 1952 (s == limit && digit > last_digit_limit)) { 1953 s = INT64_MAX; 1954 break; 1955 } 1956 s = (s * 10) + digit; 1957 ++p; 1958 } 1959 1960 *ps = s * sign; 1961 1962 /* Calculate nanoseconds. */ 1963 *pn = 0; 1964 1965 if (*p != '.') 1966 return; 1967 1968 l = 100000000UL; 1969 do { 1970 ++p; 1971 if (*p >= '0' && *p <= '9') 1972 *pn += (*p - '0') * l; 1973 else 1974 break; 1975 } while (l /= 10); 1976 } 1977 1978 /* 1979 * Parse GNU tar header 1980 */ 1981 static int 1982 header_gnutar(struct archive_read *a, struct tar *tar, 1983 struct archive_entry *entry, const void *h, size_t *unconsumed) 1984 { 1985 const struct archive_entry_header_gnutar *header; 1986 int64_t t; 1987 int err = ARCHIVE_OK; 1988 1989 /* 1990 * GNU header is like POSIX ustar, except 'prefix' is 1991 * replaced with some other fields. This also means the 1992 * filename is stored as in old-style archives. 1993 */ 1994 1995 /* Grab fields common to all tar variants. */ 1996 err = header_common(a, tar, entry, h); 1997 if (err == ARCHIVE_FATAL) 1998 return (err); 1999 2000 /* Copy filename over (to ensure null termination). */ 2001 header = (const struct archive_entry_header_gnutar *)h; 2002 if (archive_entry_copy_pathname_l(entry, 2003 header->name, sizeof(header->name), tar->sconv) != 0) { 2004 err = set_conversion_failed_error(a, tar->sconv, "Pathname"); 2005 if (err == ARCHIVE_FATAL) 2006 return (err); 2007 } 2008 2009 /* Fields common to ustar and GNU */ 2010 /* XXX Can the following be factored out since it's common 2011 * to ustar and gnu tar? Is it okay to move it down into 2012 * header_common, perhaps? */ 2013 if (archive_entry_copy_uname_l(entry, 2014 header->uname, sizeof(header->uname), tar->sconv) != 0) { 2015 err = set_conversion_failed_error(a, tar->sconv, "Uname"); 2016 if (err == ARCHIVE_FATAL) 2017 return (err); 2018 } 2019 2020 if (archive_entry_copy_gname_l(entry, 2021 header->gname, sizeof(header->gname), tar->sconv) != 0) { 2022 err = set_conversion_failed_error(a, tar->sconv, "Gname"); 2023 if (err == ARCHIVE_FATAL) 2024 return (err); 2025 } 2026 2027 /* Parse out device numbers only for char and block specials */ 2028 if (header->typeflag[0] == '3' || header->typeflag[0] == '4') { 2029 archive_entry_set_rdevmajor(entry, (dev_t) 2030 tar_atol(header->rdevmajor, sizeof(header->rdevmajor))); 2031 archive_entry_set_rdevminor(entry, (dev_t) 2032 tar_atol(header->rdevminor, sizeof(header->rdevminor))); 2033 } else 2034 archive_entry_set_rdev(entry, 0); 2035 2036 tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining); 2037 2038 /* Grab GNU-specific fields. */ 2039 t = tar_atol(header->atime, sizeof(header->atime)); 2040 if (t > 0) 2041 archive_entry_set_atime(entry, t, 0); 2042 t = tar_atol(header->ctime, sizeof(header->ctime)); 2043 if (t > 0) 2044 archive_entry_set_ctime(entry, t, 0); 2045 2046 if (header->realsize[0] != 0) { 2047 tar->realsize 2048 = tar_atol(header->realsize, sizeof(header->realsize)); 2049 archive_entry_set_size(entry, tar->realsize); 2050 } 2051 2052 if (header->sparse[0].offset[0] != 0) { 2053 if (gnu_sparse_old_read(a, tar, header, unconsumed) 2054 != ARCHIVE_OK) 2055 return (ARCHIVE_FATAL); 2056 } else { 2057 if (header->isextended[0] != 0) { 2058 /* XXX WTF? XXX */ 2059 } 2060 } 2061 2062 return (err); 2063 } 2064 2065 static int 2066 gnu_add_sparse_entry(struct archive_read *a, struct tar *tar, 2067 int64_t offset, int64_t remaining) 2068 { 2069 struct sparse_block *p; 2070 2071 p = (struct sparse_block *)malloc(sizeof(*p)); 2072 if (p == NULL) { 2073 archive_set_error(&a->archive, ENOMEM, "Out of memory"); 2074 return (ARCHIVE_FATAL); 2075 } 2076 memset(p, 0, sizeof(*p)); 2077 if (tar->sparse_last != NULL) 2078 tar->sparse_last->next = p; 2079 else 2080 tar->sparse_list = p; 2081 tar->sparse_last = p; 2082 p->offset = offset; 2083 p->remaining = remaining; 2084 return (ARCHIVE_OK); 2085 } 2086 2087 static void 2088 gnu_clear_sparse_list(struct tar *tar) 2089 { 2090 struct sparse_block *p; 2091 2092 while (tar->sparse_list != NULL) { 2093 p = tar->sparse_list; 2094 tar->sparse_list = p->next; 2095 free(p); 2096 } 2097 tar->sparse_last = NULL; 2098 } 2099 2100 /* 2101 * GNU tar old-format sparse data. 2102 * 2103 * GNU old-format sparse data is stored in a fixed-field 2104 * format. Offset/size values are 11-byte octal fields (same 2105 * format as 'size' field in ustart header). These are 2106 * stored in the header, allocating subsequent header blocks 2107 * as needed. Extending the header in this way is a pretty 2108 * severe POSIX violation; this design has earned GNU tar a 2109 * lot of criticism. 2110 */ 2111 2112 static int 2113 gnu_sparse_old_read(struct archive_read *a, struct tar *tar, 2114 const struct archive_entry_header_gnutar *header, size_t *unconsumed) 2115 { 2116 ssize_t bytes_read; 2117 const void *data; 2118 struct extended { 2119 struct gnu_sparse sparse[21]; 2120 char isextended[1]; 2121 char padding[7]; 2122 }; 2123 const struct extended *ext; 2124 2125 if (gnu_sparse_old_parse(a, tar, header->sparse, 4) != ARCHIVE_OK) 2126 return (ARCHIVE_FATAL); 2127 if (header->isextended[0] == 0) 2128 return (ARCHIVE_OK); 2129 2130 do { 2131 tar_flush_unconsumed(a, unconsumed); 2132 data = __archive_read_ahead(a, 512, &bytes_read); 2133 if (bytes_read < 0) 2134 return (ARCHIVE_FATAL); 2135 if (bytes_read < 512) { 2136 archive_set_error(&a->archive, ARCHIVE_ERRNO_FILE_FORMAT, 2137 "Truncated tar archive " 2138 "detected while reading sparse file data"); 2139 return (ARCHIVE_FATAL); 2140 } 2141 *unconsumed = 512; 2142 ext = (const struct extended *)data; 2143 if (gnu_sparse_old_parse(a, tar, ext->sparse, 21) != ARCHIVE_OK) 2144 return (ARCHIVE_FATAL); 2145 } while (ext->isextended[0] != 0); 2146 if (tar->sparse_list != NULL) 2147 tar->entry_offset = tar->sparse_list->offset; 2148 return (ARCHIVE_OK); 2149 } 2150 2151 static int 2152 gnu_sparse_old_parse(struct archive_read *a, struct tar *tar, 2153 const struct gnu_sparse *sparse, int length) 2154 { 2155 while (length > 0 && sparse->offset[0] != 0) { 2156 if (gnu_add_sparse_entry(a, tar, 2157 tar_atol(sparse->offset, sizeof(sparse->offset)), 2158 tar_atol(sparse->numbytes, sizeof(sparse->numbytes))) 2159 != ARCHIVE_OK) 2160 return (ARCHIVE_FATAL); 2161 sparse++; 2162 length--; 2163 } 2164 return (ARCHIVE_OK); 2165 } 2166 2167 /* 2168 * GNU tar sparse format 0.0 2169 * 2170 * Beginning with GNU tar 1.15, sparse files are stored using 2171 * information in the pax extended header. The GNU tar maintainers 2172 * have gone through a number of variations in the process of working 2173 * out this scheme; fortunately, they're all numbered. 2174 * 2175 * Sparse format 0.0 uses attribute GNU.sparse.numblocks to store the 2176 * number of blocks, and GNU.sparse.offset/GNU.sparse.numbytes to 2177 * store offset/size for each block. The repeated instances of these 2178 * latter fields violate the pax specification (which frowns on 2179 * duplicate keys), so this format was quickly replaced. 2180 */ 2181 2182 /* 2183 * GNU tar sparse format 0.1 2184 * 2185 * This version replaced the offset/numbytes attributes with 2186 * a single "map" attribute that stored a list of integers. This 2187 * format had two problems: First, the "map" attribute could be very 2188 * long, which caused problems for some implementations. More 2189 * importantly, the sparse data was lost when extracted by archivers 2190 * that didn't recognize this extension. 2191 */ 2192 2193 static int 2194 gnu_sparse_01_parse(struct archive_read *a, struct tar *tar, const char *p) 2195 { 2196 const char *e; 2197 int64_t offset = -1, size = -1; 2198 2199 for (;;) { 2200 e = p; 2201 while (*e != '\0' && *e != ',') { 2202 if (*e < '0' || *e > '9') 2203 return (ARCHIVE_WARN); 2204 e++; 2205 } 2206 if (offset < 0) { 2207 offset = tar_atol10(p, e - p); 2208 if (offset < 0) 2209 return (ARCHIVE_WARN); 2210 } else { 2211 size = tar_atol10(p, e - p); 2212 if (size < 0) 2213 return (ARCHIVE_WARN); 2214 if (gnu_add_sparse_entry(a, tar, offset, size) 2215 != ARCHIVE_OK) 2216 return (ARCHIVE_FATAL); 2217 offset = -1; 2218 } 2219 if (*e == '\0') 2220 return (ARCHIVE_OK); 2221 p = e + 1; 2222 } 2223 } 2224 2225 /* 2226 * GNU tar sparse format 1.0 2227 * 2228 * The idea: The offset/size data is stored as a series of base-10 2229 * ASCII numbers prepended to the file data, so that dearchivers that 2230 * don't support this format will extract the block map along with the 2231 * data and a separate post-process can restore the sparseness. 2232 * 2233 * Unfortunately, GNU tar 1.16 had a bug that added unnecessary 2234 * padding to the body of the file when using this format. GNU tar 2235 * 1.17 corrected this bug without bumping the version number, so 2236 * it's not possible to support both variants. This code supports 2237 * the later variant at the expense of not supporting the former. 2238 * 2239 * This variant also replaced GNU.sparse.size with GNU.sparse.realsize 2240 * and introduced the GNU.sparse.major/GNU.sparse.minor attributes. 2241 */ 2242 2243 /* 2244 * Read the next line from the input, and parse it as a decimal 2245 * integer followed by '\n'. Returns positive integer value or 2246 * negative on error. 2247 */ 2248 static int64_t 2249 gnu_sparse_10_atol(struct archive_read *a, struct tar *tar, 2250 int64_t *remaining, size_t *unconsumed) 2251 { 2252 int64_t l, limit, last_digit_limit; 2253 const char *p; 2254 ssize_t bytes_read; 2255 int base, digit; 2256 2257 base = 10; 2258 limit = INT64_MAX / base; 2259 last_digit_limit = INT64_MAX % base; 2260 2261 /* 2262 * Skip any lines starting with '#'; GNU tar specs 2263 * don't require this, but they should. 2264 */ 2265 do { 2266 bytes_read = readline(a, tar, &p, 2267 (ssize_t)tar_min(*remaining, 100), unconsumed); 2268 if (bytes_read <= 0) 2269 return (ARCHIVE_FATAL); 2270 *remaining -= bytes_read; 2271 } while (p[0] == '#'); 2272 2273 l = 0; 2274 while (bytes_read > 0) { 2275 if (*p == '\n') 2276 return (l); 2277 if (*p < '0' || *p >= '0' + base) 2278 return (ARCHIVE_WARN); 2279 digit = *p - '0'; 2280 if (l > limit || (l == limit && digit > last_digit_limit)) 2281 l = INT64_MAX; /* Truncate on overflow. */ 2282 else 2283 l = (l * base) + digit; 2284 p++; 2285 bytes_read--; 2286 } 2287 /* TODO: Error message. */ 2288 return (ARCHIVE_WARN); 2289 } 2290 2291 /* 2292 * Returns length (in bytes) of the sparse data description 2293 * that was read. 2294 */ 2295 static ssize_t 2296 gnu_sparse_10_read(struct archive_read *a, struct tar *tar, size_t *unconsumed) 2297 { 2298 ssize_t bytes_read; 2299 int entries; 2300 int64_t offset, size, to_skip, remaining; 2301 2302 /* Clear out the existing sparse list. */ 2303 gnu_clear_sparse_list(tar); 2304 2305 remaining = tar->entry_bytes_remaining; 2306 2307 /* Parse entries. */ 2308 entries = (int)gnu_sparse_10_atol(a, tar, &remaining, unconsumed); 2309 if (entries < 0) 2310 return (ARCHIVE_FATAL); 2311 /* Parse the individual entries. */ 2312 while (entries-- > 0) { 2313 /* Parse offset/size */ 2314 offset = gnu_sparse_10_atol(a, tar, &remaining, unconsumed); 2315 if (offset < 0) 2316 return (ARCHIVE_FATAL); 2317 size = gnu_sparse_10_atol(a, tar, &remaining, unconsumed); 2318 if (size < 0) 2319 return (ARCHIVE_FATAL); 2320 /* Add a new sparse entry. */ 2321 if (gnu_add_sparse_entry(a, tar, offset, size) != ARCHIVE_OK) 2322 return (ARCHIVE_FATAL); 2323 } 2324 /* Skip rest of block... */ 2325 tar_flush_unconsumed(a, unconsumed); 2326 bytes_read = (ssize_t)(tar->entry_bytes_remaining - remaining); 2327 to_skip = 0x1ff & -bytes_read; 2328 if (to_skip != __archive_read_consume(a, to_skip)) 2329 return (ARCHIVE_FATAL); 2330 return ((ssize_t)(bytes_read + to_skip)); 2331 } 2332 2333 /* 2334 * Solaris pax extension for a sparse file. This is recorded with the 2335 * data and hole pairs. The way recording sparse information by Solaris' 2336 * pax simply indicates where data and sparse are, so the stored contents 2337 * consist of both data and hole. 2338 */ 2339 static int 2340 solaris_sparse_parse(struct archive_read *a, struct tar *tar, 2341 struct archive_entry *entry, const char *p) 2342 { 2343 const char *e; 2344 int64_t start, end; 2345 int hole = 1; 2346 2347 (void)entry; /* UNUSED */ 2348 2349 end = 0; 2350 if (*p == ' ') 2351 p++; 2352 else 2353 return (ARCHIVE_WARN); 2354 for (;;) { 2355 e = p; 2356 while (*e != '\0' && *e != ' ') { 2357 if (*e < '0' || *e > '9') 2358 return (ARCHIVE_WARN); 2359 e++; 2360 } 2361 start = end; 2362 end = tar_atol10(p, e - p); 2363 if (end < 0) 2364 return (ARCHIVE_WARN); 2365 if (start < end) { 2366 if (gnu_add_sparse_entry(a, tar, start, 2367 end - start) != ARCHIVE_OK) 2368 return (ARCHIVE_FATAL); 2369 tar->sparse_last->hole = hole; 2370 } 2371 if (*e == '\0') 2372 return (ARCHIVE_OK); 2373 p = e + 1; 2374 hole = hole == 0; 2375 } 2376 } 2377 2378 /*- 2379 * Convert text->integer. 2380 * 2381 * Traditional tar formats (including POSIX) specify base-8 for 2382 * all of the standard numeric fields. This is a significant limitation 2383 * in practice: 2384 * = file size is limited to 8GB 2385 * = rdevmajor and rdevminor are limited to 21 bits 2386 * = uid/gid are limited to 21 bits 2387 * 2388 * There are two workarounds for this: 2389 * = pax extended headers, which use variable-length string fields 2390 * = GNU tar and STAR both allow either base-8 or base-256 in 2391 * most fields. The high bit is set to indicate base-256. 2392 * 2393 * On read, this implementation supports both extensions. 2394 */ 2395 static int64_t 2396 tar_atol(const char *p, size_t char_cnt) 2397 { 2398 /* 2399 * Technically, GNU tar considers a field to be in base-256 2400 * only if the first byte is 0xff or 0x80. 2401 */ 2402 if (*p & 0x80) 2403 return (tar_atol256(p, char_cnt)); 2404 return (tar_atol8(p, char_cnt)); 2405 } 2406 2407 /* 2408 * Note that this implementation does not (and should not!) obey 2409 * locale settings; you cannot simply substitute strtol here, since 2410 * it does obey locale. 2411 */ 2412 static int64_t 2413 tar_atol_base_n(const char *p, size_t char_cnt, int base) 2414 { 2415 int64_t l, limit, last_digit_limit; 2416 int digit, sign; 2417 2418 limit = INT64_MAX / base; 2419 last_digit_limit = INT64_MAX % base; 2420 2421 /* the pointer will not be dereferenced if char_cnt is zero 2422 * due to the way the && operator is evaulated. 2423 */ 2424 while (char_cnt != 0 && (*p == ' ' || *p == '\t')) { 2425 p++; 2426 char_cnt--; 2427 } 2428 2429 sign = 1; 2430 if (char_cnt != 0 && *p == '-') { 2431 sign = -1; 2432 p++; 2433 char_cnt--; 2434 } 2435 2436 l = 0; 2437 if (char_cnt != 0) { 2438 digit = *p - '0'; 2439 while (digit >= 0 && digit < base && char_cnt != 0) { 2440 if (l>limit || (l == limit && digit > last_digit_limit)) { 2441 l = INT64_MAX; /* Truncate on overflow. */ 2442 break; 2443 } 2444 l = (l * base) + digit; 2445 digit = *++p - '0'; 2446 char_cnt--; 2447 } 2448 } 2449 return (sign < 0) ? -l : l; 2450 } 2451 2452 static int64_t 2453 tar_atol8(const char *p, size_t char_cnt) 2454 { 2455 return tar_atol_base_n(p, char_cnt, 8); 2456 } 2457 2458 static int64_t 2459 tar_atol10(const char *p, size_t char_cnt) 2460 { 2461 return tar_atol_base_n(p, char_cnt, 10); 2462 } 2463 2464 /* 2465 * Parse a base-256 integer. This is just a straight signed binary 2466 * value in big-endian order, except that the high-order bit is 2467 * ignored. 2468 */ 2469 static int64_t 2470 tar_atol256(const char *_p, size_t char_cnt) 2471 { 2472 int64_t l, upper_limit, lower_limit; 2473 const unsigned char *p = (const unsigned char *)_p; 2474 2475 upper_limit = INT64_MAX / 256; 2476 lower_limit = INT64_MIN / 256; 2477 2478 /* Pad with 1 or 0 bits, depending on sign. */ 2479 if ((0x40 & *p) == 0x40) 2480 l = (int64_t)-1; 2481 else 2482 l = 0; 2483 l = (l << 6) | (0x3f & *p++); 2484 while (--char_cnt > 0) { 2485 if (l > upper_limit) { 2486 l = INT64_MAX; /* Truncate on overflow */ 2487 break; 2488 } else if (l < lower_limit) { 2489 l = INT64_MIN; 2490 break; 2491 } 2492 l = (l << 8) | (0xff & (int64_t)*p++); 2493 } 2494 return (l); 2495 } 2496 2497 /* 2498 * Returns length of line (including trailing newline) 2499 * or negative on error. 'start' argument is updated to 2500 * point to first character of line. This avoids copying 2501 * when possible. 2502 */ 2503 static ssize_t 2504 readline(struct archive_read *a, struct tar *tar, const char **start, 2505 ssize_t limit, size_t *unconsumed) 2506 { 2507 ssize_t bytes_read; 2508 ssize_t total_size = 0; 2509 const void *t; 2510 const char *s; 2511 void *p; 2512 2513 tar_flush_unconsumed(a, unconsumed); 2514 2515 t = __archive_read_ahead(a, 1, &bytes_read); 2516 if (bytes_read <= 0) 2517 return (ARCHIVE_FATAL); 2518 s = t; /* Start of line? */ 2519 p = memchr(t, '\n', bytes_read); 2520 /* If we found '\n' in the read buffer, return pointer to that. */ 2521 if (p != NULL) { 2522 bytes_read = 1 + ((const char *)p) - s; 2523 if (bytes_read > limit) { 2524 archive_set_error(&a->archive, 2525 ARCHIVE_ERRNO_FILE_FORMAT, 2526 "Line too long"); 2527 return (ARCHIVE_FATAL); 2528 } 2529 *unconsumed = bytes_read; 2530 *start = s; 2531 return (bytes_read); 2532 } 2533 *unconsumed = bytes_read; 2534 /* Otherwise, we need to accumulate in a line buffer. */ 2535 for (;;) { 2536 if (total_size + bytes_read > limit) { 2537 archive_set_error(&a->archive, 2538 ARCHIVE_ERRNO_FILE_FORMAT, 2539 "Line too long"); 2540 return (ARCHIVE_FATAL); 2541 } 2542 if (archive_string_ensure(&tar->line, total_size + bytes_read) == NULL) { 2543 archive_set_error(&a->archive, ENOMEM, 2544 "Can't allocate working buffer"); 2545 return (ARCHIVE_FATAL); 2546 } 2547 memcpy(tar->line.s + total_size, t, bytes_read); 2548 tar_flush_unconsumed(a, unconsumed); 2549 total_size += bytes_read; 2550 /* If we found '\n', clean up and return. */ 2551 if (p != NULL) { 2552 *start = tar->line.s; 2553 return (total_size); 2554 } 2555 /* Read some more. */ 2556 t = __archive_read_ahead(a, 1, &bytes_read); 2557 if (bytes_read <= 0) 2558 return (ARCHIVE_FATAL); 2559 s = t; /* Start of line? */ 2560 p = memchr(t, '\n', bytes_read); 2561 /* If we found '\n', trim the read. */ 2562 if (p != NULL) { 2563 bytes_read = 1 + ((const char *)p) - s; 2564 } 2565 *unconsumed = bytes_read; 2566 } 2567 } 2568 2569 /* 2570 * base64_decode - Base64 decode 2571 * 2572 * This accepts most variations of base-64 encoding, including: 2573 * * with or without line breaks 2574 * * with or without the final group padded with '=' or '_' characters 2575 * (The most economical Base-64 variant does not pad the last group and 2576 * omits line breaks; RFC1341 used for MIME requires both.) 2577 */ 2578 static char * 2579 base64_decode(const char *s, size_t len, size_t *out_len) 2580 { 2581 static const unsigned char digits[64] = { 2582 'A','B','C','D','E','F','G','H','I','J','K','L','M','N', 2583 'O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b', 2584 'c','d','e','f','g','h','i','j','k','l','m','n','o','p', 2585 'q','r','s','t','u','v','w','x','y','z','0','1','2','3', 2586 '4','5','6','7','8','9','+','/' }; 2587 static unsigned char decode_table[128]; 2588 char *out, *d; 2589 const unsigned char *src = (const unsigned char *)s; 2590 2591 /* If the decode table is not yet initialized, prepare it. */ 2592 if (decode_table[digits[1]] != 1) { 2593 unsigned i; 2594 memset(decode_table, 0xff, sizeof(decode_table)); 2595 for (i = 0; i < sizeof(digits); i++) 2596 decode_table[digits[i]] = i; 2597 } 2598 2599 /* Allocate enough space to hold the entire output. */ 2600 /* Note that we may not use all of this... */ 2601 out = (char *)malloc(len - len / 4 + 1); 2602 if (out == NULL) { 2603 *out_len = 0; 2604 return (NULL); 2605 } 2606 d = out; 2607 2608 while (len > 0) { 2609 /* Collect the next group of (up to) four characters. */ 2610 int v = 0; 2611 int group_size = 0; 2612 while (group_size < 4 && len > 0) { 2613 /* '=' or '_' padding indicates final group. */ 2614 if (*src == '=' || *src == '_') { 2615 len = 0; 2616 break; 2617 } 2618 /* Skip illegal characters (including line breaks) */ 2619 if (*src > 127 || *src < 32 2620 || decode_table[*src] == 0xff) { 2621 len--; 2622 src++; 2623 continue; 2624 } 2625 v <<= 6; 2626 v |= decode_table[*src++]; 2627 len --; 2628 group_size++; 2629 } 2630 /* Align a short group properly. */ 2631 v <<= 6 * (4 - group_size); 2632 /* Unpack the group we just collected. */ 2633 switch (group_size) { 2634 case 4: d[2] = v & 0xff; 2635 /* FALLTHROUGH */ 2636 case 3: d[1] = (v >> 8) & 0xff; 2637 /* FALLTHROUGH */ 2638 case 2: d[0] = (v >> 16) & 0xff; 2639 break; 2640 case 1: /* this is invalid! */ 2641 break; 2642 } 2643 d += group_size * 3 / 4; 2644 } 2645 2646 *out_len = d - out; 2647 return (out); 2648 } 2649 2650 static char * 2651 url_decode(const char *in) 2652 { 2653 char *out, *d; 2654 const char *s; 2655 2656 out = (char *)malloc(strlen(in) + 1); 2657 if (out == NULL) 2658 return (NULL); 2659 for (s = in, d = out; *s != '\0'; ) { 2660 if (s[0] == '%' && s[1] != '\0' && s[2] != '\0') { 2661 /* Try to convert % escape */ 2662 int digit1 = tohex(s[1]); 2663 int digit2 = tohex(s[2]); 2664 if (digit1 >= 0 && digit2 >= 0) { 2665 /* Looks good, consume three chars */ 2666 s += 3; 2667 /* Convert output */ 2668 *d++ = ((digit1 << 4) | digit2); 2669 continue; 2670 } 2671 /* Else fall through and treat '%' as normal char */ 2672 } 2673 *d++ = *s++; 2674 } 2675 *d = '\0'; 2676 return (out); 2677 } 2678 2679 static int 2680 tohex(int c) 2681 { 2682 if (c >= '0' && c <= '9') 2683 return (c - '0'); 2684 else if (c >= 'A' && c <= 'F') 2685 return (c - 'A' + 10); 2686 else if (c >= 'a' && c <= 'f') 2687 return (c - 'a' + 10); 2688 else 2689 return (-1); 2690 } 2691