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