2004-02-09 23:22:54 +00:00
|
|
|
/*-
|
2006-03-21 16:55:46 +00:00
|
|
|
* Copyright (c) 2003-2006 Tim Kientzle
|
2004-02-09 23:22:54 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer
|
|
|
|
* in this position and unchanged.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
|
|
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|
|
|
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|
|
|
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|
|
|
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|
|
|
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2004-03-09 19:50:41 +00:00
|
|
|
#include "archive_platform.h"
|
2004-02-09 23:22:54 +00:00
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
#ifdef HAVE_SYS_STAT_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <sys/stat.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
2005-11-08 03:52:42 +00:00
|
|
|
#ifdef MAJOR_IN_MKDEV
|
|
|
|
#include <sys/mkdev.h>
|
|
|
|
#else
|
|
|
|
#ifdef MAJOR_IN_SYSMACROS
|
|
|
|
#include <sys/sysmacros.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
2006-11-10 06:39:46 +00:00
|
|
|
#ifdef HAVE_ERRNO_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <errno.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
2005-09-10 22:58:06 +00:00
|
|
|
#include <stddef.h>
|
2004-03-19 22:37:06 +00:00
|
|
|
/* #include <stdint.h> */ /* See archive_platform.h */
|
2006-11-10 06:39:46 +00:00
|
|
|
#ifdef HAVE_STDLIB_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <stdlib.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_STRING_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <string.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
|
|
|
#ifdef HAVE_UNISTD_H
|
2004-02-09 23:22:54 +00:00
|
|
|
#include <unistd.h>
|
2006-11-10 06:39:46 +00:00
|
|
|
#endif
|
2005-09-10 22:58:06 +00:00
|
|
|
|
|
|
|
/* Obtain suitable wide-character manipulation functions. */
|
|
|
|
#ifdef HAVE_WCHAR_H
|
2004-12-22 06:40:28 +00:00
|
|
|
#include <wchar.h>
|
2005-09-10 22:58:06 +00:00
|
|
|
#else
|
2006-05-01 01:02:19 +00:00
|
|
|
/* Good enough for equality testing, which is all we need. */
|
2005-09-10 22:58:06 +00:00
|
|
|
static int wcscmp(const wchar_t *s1, const wchar_t *s2)
|
|
|
|
{
|
|
|
|
int diff = *s1 - *s2;
|
2006-03-21 16:55:46 +00:00
|
|
|
while (*s1 && diff == 0)
|
2005-09-10 22:58:06 +00:00
|
|
|
diff = (int)*++s1 - (int)*++s2;
|
|
|
|
return diff;
|
|
|
|
}
|
2006-05-01 01:02:19 +00:00
|
|
|
/* Good enough for equality testing, which is all we need. */
|
|
|
|
static int wcsncmp(const wchar_t *s1, const wchar_t *s2, size_t n)
|
|
|
|
{
|
|
|
|
int diff = *s1 - *s2;
|
|
|
|
while (*s1 && diff == 0 && n-- > 0)
|
|
|
|
diff = (int)*++s1 - (int)*++s2;
|
|
|
|
return diff;
|
|
|
|
}
|
2005-09-10 22:58:06 +00:00
|
|
|
static size_t wcslen(const wchar_t *s)
|
|
|
|
{
|
|
|
|
const wchar_t *p = s;
|
|
|
|
while (*p)
|
|
|
|
p++;
|
|
|
|
return p - s;
|
|
|
|
}
|
|
|
|
#endif
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
#include "archive.h"
|
|
|
|
#include "archive_entry.h"
|
|
|
|
#include "archive_private.h"
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Layout of POSIX 'ustar' tar header.
|
|
|
|
*/
|
|
|
|
struct archive_entry_header_ustar {
|
|
|
|
char name[100];
|
|
|
|
char mode[8];
|
|
|
|
char uid[8];
|
|
|
|
char gid[8];
|
|
|
|
char size[12];
|
|
|
|
char mtime[12];
|
|
|
|
char checksum[8];
|
|
|
|
char typeflag[1];
|
|
|
|
char linkname[100]; /* "old format" header ends here */
|
|
|
|
char magic[6]; /* For POSIX: "ustar\0" */
|
|
|
|
char version[2]; /* For POSIX: "00" */
|
|
|
|
char uname[32];
|
|
|
|
char gname[32];
|
2004-06-27 18:38:13 +00:00
|
|
|
char rdevmajor[8];
|
|
|
|
char rdevminor[8];
|
2004-02-09 23:22:54 +00:00
|
|
|
char prefix[155];
|
|
|
|
};
|
|
|
|
|
2004-06-27 01:15:31 +00:00
|
|
|
/*
|
|
|
|
* Structure of GNU tar header
|
|
|
|
*/
|
|
|
|
struct gnu_sparse {
|
|
|
|
char offset[12];
|
|
|
|
char numbytes[12];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct archive_entry_header_gnutar {
|
|
|
|
char name[100];
|
|
|
|
char mode[8];
|
|
|
|
char uid[8];
|
|
|
|
char gid[8];
|
|
|
|
char size[12];
|
|
|
|
char mtime[12];
|
|
|
|
char checksum[8];
|
|
|
|
char typeflag[1];
|
|
|
|
char linkname[100];
|
|
|
|
char magic[8]; /* "ustar \0" (note blank/blank/null at end) */
|
|
|
|
char uname[32];
|
|
|
|
char gname[32];
|
2004-06-27 18:38:13 +00:00
|
|
|
char rdevmajor[8];
|
|
|
|
char rdevminor[8];
|
2004-06-27 01:15:31 +00:00
|
|
|
char atime[12];
|
|
|
|
char ctime[12];
|
|
|
|
char offset[12];
|
|
|
|
char longnames[4];
|
|
|
|
char unused[1];
|
|
|
|
struct gnu_sparse sparse[4];
|
|
|
|
char isextended[1];
|
|
|
|
char realsize[12];
|
|
|
|
/*
|
|
|
|
* GNU doesn't use POSIX 'prefix' field; they use the 'L' (longname)
|
|
|
|
* entry instead.
|
|
|
|
*/
|
|
|
|
};
|
|
|
|
|
2004-03-19 22:37:06 +00:00
|
|
|
/*
|
|
|
|
* Data specific to this format.
|
|
|
|
*/
|
2004-06-27 01:15:31 +00:00
|
|
|
struct sparse_block {
|
|
|
|
struct sparse_block *next;
|
|
|
|
off_t offset;
|
|
|
|
off_t remaining;
|
|
|
|
};
|
|
|
|
|
2004-03-19 22:37:06 +00:00
|
|
|
struct tar {
|
|
|
|
struct archive_string acl_text;
|
|
|
|
struct archive_string entry_name;
|
|
|
|
struct archive_string entry_linkname;
|
|
|
|
struct archive_string entry_uname;
|
|
|
|
struct archive_string entry_gname;
|
|
|
|
struct archive_string longlink;
|
|
|
|
struct archive_string longname;
|
|
|
|
struct archive_string pax_header;
|
|
|
|
struct archive_string pax_global;
|
|
|
|
wchar_t *pax_entry;
|
|
|
|
size_t pax_entry_length;
|
|
|
|
int header_recursion_depth;
|
2004-06-02 08:14:43 +00:00
|
|
|
off_t entry_bytes_remaining;
|
|
|
|
off_t entry_offset;
|
|
|
|
off_t entry_padding;
|
2004-06-27 01:15:31 +00:00
|
|
|
struct sparse_block *sparse_list;
|
2004-03-19 22:37:06 +00:00
|
|
|
};
|
|
|
|
|
2004-03-20 22:35:33 +00:00
|
|
|
static size_t UTF8_mbrtowc(wchar_t *pwc, const char *s, size_t n);
|
2004-02-09 23:22:54 +00:00
|
|
|
static int archive_block_is_null(const unsigned char *p);
|
2006-03-21 16:55:46 +00:00
|
|
|
static char *base64_decode(const wchar_t *, size_t, size_t *);
|
2005-10-12 15:38:45 +00:00
|
|
|
static int gnu_read_sparse_data(struct archive *, struct tar *,
|
2004-06-27 01:15:31 +00:00
|
|
|
const struct archive_entry_header_gnutar *header);
|
2005-10-12 15:38:45 +00:00
|
|
|
static void gnu_parse_sparse_data(struct archive *, struct tar *,
|
2004-06-27 01:15:31 +00:00
|
|
|
const struct gnu_sparse *sparse, int length);
|
2004-03-19 22:37:06 +00:00
|
|
|
static int header_Solaris_ACL(struct archive *, struct tar *,
|
|
|
|
struct archive_entry *, struct stat *, const void *);
|
|
|
|
static int header_common(struct archive *, struct tar *,
|
2004-02-09 23:22:54 +00:00
|
|
|
struct archive_entry *, struct stat *, const void *);
|
2004-03-19 22:37:06 +00:00
|
|
|
static int header_old_tar(struct archive *, struct tar *,
|
2004-02-09 23:22:54 +00:00
|
|
|
struct archive_entry *, struct stat *, const void *);
|
2004-03-19 22:37:06 +00:00
|
|
|
static int header_pax_extensions(struct archive *, struct tar *,
|
|
|
|
struct archive_entry *, struct stat *, const void *);
|
|
|
|
static int header_pax_global(struct archive *, struct tar *,
|
|
|
|
struct archive_entry *, struct stat *, const void *h);
|
|
|
|
static int header_longlink(struct archive *, struct tar *,
|
|
|
|
struct archive_entry *, struct stat *, const void *h);
|
|
|
|
static int header_longname(struct archive *, struct tar *,
|
|
|
|
struct archive_entry *, struct stat *, const void *h);
|
2004-04-12 01:16:16 +00:00
|
|
|
static int header_volume(struct archive *, struct tar *,
|
|
|
|
struct archive_entry *, struct stat *, const void *h);
|
2004-03-19 22:37:06 +00:00
|
|
|
static int header_ustar(struct archive *, struct tar *,
|
2004-02-09 23:22:54 +00:00
|
|
|
struct archive_entry *, struct stat *, const void *h);
|
2004-04-12 01:16:16 +00:00
|
|
|
static int header_gnutar(struct archive *, struct tar *,
|
|
|
|
struct archive_entry *, struct stat *, const void *h);
|
2004-02-09 23:22:54 +00:00
|
|
|
static int archive_read_format_tar_bid(struct archive *);
|
2004-03-19 22:37:06 +00:00
|
|
|
static int archive_read_format_tar_cleanup(struct archive *);
|
2004-06-02 08:14:43 +00:00
|
|
|
static int archive_read_format_tar_read_data(struct archive *a,
|
|
|
|
const void **buff, size_t *size, off_t *offset);
|
2006-07-30 00:29:01 +00:00
|
|
|
static int archive_read_format_tar_skip(struct archive *a);
|
2004-02-09 23:22:54 +00:00
|
|
|
static int archive_read_format_tar_read_header(struct archive *,
|
|
|
|
struct archive_entry *);
|
|
|
|
static int checksum(struct archive *, const void *);
|
2004-03-19 22:37:06 +00:00
|
|
|
static int pax_attribute(struct archive_entry *, struct stat *,
|
|
|
|
wchar_t *key, wchar_t *value);
|
|
|
|
static int pax_header(struct archive *, struct tar *,
|
|
|
|
struct archive_entry *, struct stat *, char *attr);
|
2004-04-21 05:13:42 +00:00
|
|
|
static void pax_time(const wchar_t *, int64_t *sec, long *nanos);
|
2004-06-02 08:14:43 +00:00
|
|
|
static int read_body_to_string(struct archive *, struct tar *,
|
|
|
|
struct archive_string *, const void *h);
|
2004-02-09 23:22:54 +00:00
|
|
|
static int64_t tar_atol(const char *, unsigned);
|
2004-03-19 22:37:06 +00:00
|
|
|
static int64_t tar_atol10(const wchar_t *, unsigned);
|
2004-02-09 23:22:54 +00:00
|
|
|
static int64_t tar_atol256(const char *, unsigned);
|
|
|
|
static int64_t tar_atol8(const char *, unsigned);
|
2004-03-19 22:37:06 +00:00
|
|
|
static int tar_read_header(struct archive *, struct tar *,
|
|
|
|
struct archive_entry *, struct stat *);
|
2006-03-21 16:55:46 +00:00
|
|
|
static int tohex(int c);
|
|
|
|
static char *url_decode(const char *);
|
2004-03-19 22:37:06 +00:00
|
|
|
static int utf8_decode(wchar_t *, const char *, size_t length);
|
2006-03-21 16:55:46 +00:00
|
|
|
static char *wide_to_narrow(const wchar_t *wval);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-07-24 17:46:45 +00:00
|
|
|
/*
|
|
|
|
* ANSI C99 defines constants for these, but not everyone supports
|
|
|
|
* those constants, so I define a couple of static variables here and
|
|
|
|
* compute the values. These calculations should be portable to any
|
|
|
|
* 2s-complement architecture.
|
|
|
|
*/
|
|
|
|
#ifdef UINT64_MAX
|
|
|
|
static const uint64_t max_uint64 = UINT64_MAX;
|
|
|
|
#else
|
|
|
|
static const uint64_t max_uint64 = ~(uint64_t)0;
|
|
|
|
#endif
|
|
|
|
#ifdef INT64_MAX
|
|
|
|
static const int64_t max_int64 = INT64_MAX;
|
|
|
|
#else
|
|
|
|
static const int64_t max_int64 = (int64_t)((~(uint64_t)0) >> 1);
|
|
|
|
#endif
|
|
|
|
#ifdef INT64_MIN
|
|
|
|
static const int64_t min_int64 = INT64_MIN;
|
|
|
|
#else
|
2004-08-07 06:38:40 +00:00
|
|
|
static const int64_t min_int64 = (int64_t)(~((~(uint64_t)0) >> 1));
|
2004-07-24 17:46:45 +00:00
|
|
|
#endif
|
|
|
|
|
2004-04-12 01:16:16 +00:00
|
|
|
int
|
|
|
|
archive_read_support_format_gnutar(struct archive *a)
|
|
|
|
{
|
|
|
|
return (archive_read_support_format_tar(a));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
int
|
|
|
|
archive_read_support_format_tar(struct archive *a)
|
|
|
|
{
|
2004-03-19 22:37:06 +00:00
|
|
|
struct tar *tar;
|
2004-04-13 23:45:37 +00:00
|
|
|
int r;
|
2004-03-19 22:37:06 +00:00
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
tar = (struct tar *)malloc(sizeof(*tar));
|
2005-09-21 04:25:06 +00:00
|
|
|
if (tar == NULL) {
|
|
|
|
archive_set_error(a, ENOMEM, "Can't allocate tar data");
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
2004-03-19 22:37:06 +00:00
|
|
|
memset(tar, 0, sizeof(*tar));
|
|
|
|
|
2004-04-13 23:45:37 +00:00
|
|
|
r = __archive_read_register_format(a, tar,
|
2004-02-09 23:22:54 +00:00
|
|
|
archive_read_format_tar_bid,
|
|
|
|
archive_read_format_tar_read_header,
|
2004-06-02 08:14:43 +00:00
|
|
|
archive_read_format_tar_read_data,
|
2006-07-30 00:29:01 +00:00
|
|
|
archive_read_format_tar_skip,
|
2004-04-13 23:45:37 +00:00
|
|
|
archive_read_format_tar_cleanup);
|
|
|
|
|
|
|
|
if (r != ARCHIVE_OK)
|
|
|
|
free(tar);
|
|
|
|
return (ARCHIVE_OK);
|
2004-03-19 22:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
archive_read_format_tar_cleanup(struct archive *a)
|
|
|
|
{
|
|
|
|
struct tar *tar;
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
tar = (struct tar *)*(a->pformat_data);
|
2004-04-13 23:45:37 +00:00
|
|
|
archive_string_free(&tar->acl_text);
|
|
|
|
archive_string_free(&tar->entry_name);
|
|
|
|
archive_string_free(&tar->entry_linkname);
|
|
|
|
archive_string_free(&tar->entry_uname);
|
|
|
|
archive_string_free(&tar->entry_gname);
|
|
|
|
archive_string_free(&tar->pax_global);
|
|
|
|
archive_string_free(&tar->pax_header);
|
|
|
|
if (tar->pax_entry != NULL)
|
|
|
|
free(tar->pax_entry);
|
2004-03-19 22:37:06 +00:00
|
|
|
free(tar);
|
|
|
|
*(a->pformat_data) = NULL;
|
|
|
|
return (ARCHIVE_OK);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
archive_read_format_tar_bid(struct archive *a)
|
|
|
|
{
|
|
|
|
int bid;
|
|
|
|
ssize_t bytes_read;
|
|
|
|
const void *h;
|
|
|
|
const struct archive_entry_header_ustar *header;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're already reading a non-tar file, don't
|
|
|
|
* bother to bid.
|
|
|
|
*/
|
|
|
|
if (a->archive_format != 0 &&
|
|
|
|
(a->archive_format & ARCHIVE_FORMAT_BASE_MASK) !=
|
|
|
|
ARCHIVE_FORMAT_TAR)
|
|
|
|
return (0);
|
|
|
|
bid = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If we're already reading a tar format, start the bid at 1 as
|
|
|
|
* a failsafe.
|
|
|
|
*/
|
|
|
|
if ((a->archive_format & ARCHIVE_FORMAT_BASE_MASK) ==
|
|
|
|
ARCHIVE_FORMAT_TAR)
|
|
|
|
bid++;
|
|
|
|
|
|
|
|
/* Now let's look at the actual header and see if it matches. */
|
2004-10-27 05:15:23 +00:00
|
|
|
if (a->compression_read_ahead != NULL)
|
|
|
|
bytes_read = (a->compression_read_ahead)(a, &h, 512);
|
|
|
|
else
|
|
|
|
bytes_read = 0; /* Empty file. */
|
2004-06-04 10:27:23 +00:00
|
|
|
if (bytes_read < 0)
|
2004-02-09 23:22:54 +00:00
|
|
|
return (ARCHIVE_FATAL);
|
2004-06-04 10:27:23 +00:00
|
|
|
if (bytes_read == 0 && bid > 0) {
|
|
|
|
/* An archive without a proper end-of-archive marker. */
|
|
|
|
/* Hold our nose and bid 1 anyway. */
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
if (bytes_read < 512) {
|
2004-06-07 04:32:10 +00:00
|
|
|
/* If it's a new archive, then just return a zero bid. */
|
|
|
|
if (bid == 0)
|
|
|
|
return (0);
|
|
|
|
/*
|
|
|
|
* If we already know this is a tar archive,
|
|
|
|
* then we have a problem.
|
|
|
|
*/
|
|
|
|
archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
|
|
|
|
"Truncated tar archive");
|
2004-06-04 10:27:23 +00:00
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/* If it's an end-of-archive mark, we can handle it. */
|
2006-11-10 06:39:46 +00:00
|
|
|
if ((*(const char *)h) == 0 && archive_block_is_null((const unsigned char *)h)) {
|
2004-12-22 00:49:16 +00:00
|
|
|
/* If it's a known tar file, end-of-archive is definite. */
|
|
|
|
if ((a->archive_format & ARCHIVE_FORMAT_BASE_MASK) ==
|
|
|
|
ARCHIVE_FORMAT_TAR)
|
|
|
|
return (512);
|
|
|
|
/* Empty archive? */
|
|
|
|
return (1);
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/* If it's not an end-of-archive mark, it must have a valid checksum.*/
|
|
|
|
if (!checksum(a, h))
|
|
|
|
return (0);
|
|
|
|
bid += 48; /* Checksum is usually 6 octal digits. */
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
header = (const struct archive_entry_header_ustar *)h;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-05-18 18:16:30 +00:00
|
|
|
/* Recognize POSIX formats. */
|
2004-02-09 23:22:54 +00:00
|
|
|
if ((memcmp(header->magic, "ustar\0", 6) == 0)
|
|
|
|
&&(memcmp(header->version, "00", 2)==0))
|
|
|
|
bid += 56;
|
|
|
|
|
2004-05-18 18:16:30 +00:00
|
|
|
/* Recognize GNU tar format. */
|
2004-04-12 01:16:16 +00:00
|
|
|
if ((memcmp(header->magic, "ustar ", 6) == 0)
|
|
|
|
&&(memcmp(header->version, " \0", 2)==0))
|
|
|
|
bid += 56;
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/* Type flag must be null, digit or A-Z, a-z. */
|
|
|
|
if (header->typeflag[0] != 0 &&
|
|
|
|
!( header->typeflag[0] >= '0' && header->typeflag[0] <= '9') &&
|
|
|
|
!( header->typeflag[0] >= 'A' && header->typeflag[0] <= 'Z') &&
|
|
|
|
!( header->typeflag[0] >= 'a' && header->typeflag[0] <= 'z') )
|
|
|
|
return (0);
|
2004-12-22 00:49:16 +00:00
|
|
|
bid += 2; /* 6 bits of variation in an 8-bit field leaves 2 bits. */
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/* Sanity check: Look at first byte of mode field. */
|
|
|
|
switch (255 & (unsigned)header->mode[0]) {
|
|
|
|
case 0: case 255:
|
|
|
|
/* Base-256 value: No further verification possible! */
|
|
|
|
break;
|
|
|
|
case ' ': /* Not recommended, but not illegal, either. */
|
|
|
|
break;
|
|
|
|
case '0': case '1': case '2': case '3':
|
|
|
|
case '4': case '5': case '6': case '7':
|
|
|
|
/* Octal Value. */
|
|
|
|
/* TODO: Check format of remainder of this field. */
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* Not a valid mode; bail out here. */
|
|
|
|
return (0);
|
|
|
|
}
|
2004-06-27 18:38:13 +00:00
|
|
|
/* TODO: Sanity test uid/gid/size/mtime/rdevmajor/rdevminor fields. */
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
return (bid);
|
|
|
|
}
|
|
|
|
|
2004-03-19 22:37:06 +00:00
|
|
|
/*
|
|
|
|
* The function invoked by archive_read_header(). This
|
|
|
|
* just sets up a few things and then calls the internal
|
|
|
|
* tar_read_header() function below.
|
|
|
|
*/
|
2004-02-09 23:22:54 +00:00
|
|
|
static int
|
|
|
|
archive_read_format_tar_read_header(struct archive *a,
|
|
|
|
struct archive_entry *entry)
|
|
|
|
{
|
2005-08-02 03:17:57 +00:00
|
|
|
/*
|
|
|
|
* When converting tar archives to cpio archives, it is
|
|
|
|
* essential that each distinct file have a distinct inode
|
|
|
|
* number. To simplify this, we keep a static count here to
|
|
|
|
* assign fake dev/inode numbers to each tar entry. Note that
|
|
|
|
* pax format archives may overwrite this with something more
|
|
|
|
* useful.
|
|
|
|
*
|
|
|
|
* Ideally, we would track every file read from the archive so
|
|
|
|
* that we could assign the same dev/ino pair to hardlinks,
|
|
|
|
* but the memory required to store a complete lookup table is
|
|
|
|
* probably not worthwhile just to support the relatively
|
|
|
|
* obscure tar->cpio conversion case.
|
|
|
|
*/
|
|
|
|
static int default_inode;
|
|
|
|
static int default_dev;
|
2004-02-09 23:22:54 +00:00
|
|
|
struct stat st;
|
2004-03-19 22:37:06 +00:00
|
|
|
struct tar *tar;
|
2004-09-04 21:49:42 +00:00
|
|
|
const char *p;
|
|
|
|
int r;
|
|
|
|
size_t l;
|
2004-03-19 22:37:06 +00:00
|
|
|
|
|
|
|
memset(&st, 0, sizeof(st));
|
2005-08-02 03:17:57 +00:00
|
|
|
/* Assign default device/inode values. */
|
|
|
|
st.st_dev = 1 + default_dev; /* Don't use zero. */
|
|
|
|
st.st_ino = ++default_inode; /* Don't use zero. */
|
|
|
|
/* Limit generated st_ino number to 16 bits. */
|
|
|
|
if (default_inode >= 0xffff) {
|
|
|
|
++default_dev;
|
|
|
|
default_inode = 0;
|
|
|
|
}
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
tar = (struct tar *)*(a->pformat_data);
|
2004-06-02 08:14:43 +00:00
|
|
|
tar->entry_offset = 0;
|
2004-03-19 22:37:06 +00:00
|
|
|
|
2004-09-04 21:49:42 +00:00
|
|
|
r = tar_read_header(a, tar, entry, &st);
|
|
|
|
|
|
|
|
if (r == ARCHIVE_OK) {
|
|
|
|
/*
|
|
|
|
* "Regular" entry with trailing '/' is really
|
|
|
|
* directory: This is needed for certain old tar
|
|
|
|
* variants and even for some broken newer ones.
|
|
|
|
*/
|
|
|
|
p = archive_entry_pathname(entry);
|
|
|
|
l = strlen(p);
|
|
|
|
if (S_ISREG(st.st_mode) && p[l-1] == '/') {
|
|
|
|
st.st_mode &= ~S_IFMT;
|
|
|
|
st.st_mode |= S_IFDIR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Copy the final stat data into the entry. */
|
|
|
|
archive_entry_copy_stat(entry, &st);
|
|
|
|
}
|
|
|
|
return (r);
|
2004-03-19 22:37:06 +00:00
|
|
|
}
|
|
|
|
|
2004-06-02 08:14:43 +00:00
|
|
|
static int
|
|
|
|
archive_read_format_tar_read_data(struct archive *a,
|
|
|
|
const void **buff, size_t *size, off_t *offset)
|
|
|
|
{
|
|
|
|
ssize_t bytes_read;
|
|
|
|
struct tar *tar;
|
2004-06-27 01:15:31 +00:00
|
|
|
struct sparse_block *p;
|
2004-06-02 08:14:43 +00:00
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
tar = (struct tar *)*(a->pformat_data);
|
2005-10-12 03:27:46 +00:00
|
|
|
if (tar->sparse_list != NULL) {
|
|
|
|
/* Remove exhausted entries from sparse list. */
|
2004-06-27 01:15:31 +00:00
|
|
|
while (tar->sparse_list != NULL &&
|
|
|
|
tar->sparse_list->remaining == 0) {
|
|
|
|
p = tar->sparse_list;
|
|
|
|
tar->sparse_list = p->next;
|
|
|
|
free(p);
|
|
|
|
}
|
2005-10-12 03:27:46 +00:00
|
|
|
if (tar->sparse_list == NULL) {
|
|
|
|
/* We exhausted the entire sparse list. */
|
|
|
|
tar->entry_bytes_remaining = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (tar->entry_bytes_remaining > 0) {
|
|
|
|
bytes_read = (a->compression_read_ahead)(a, buff, 1);
|
2006-11-13 16:50:18 +00:00
|
|
|
if (bytes_read == 0) {
|
|
|
|
archive_set_error(a, ARCHIVE_ERRNO_MISC,
|
|
|
|
"Truncated tar archive");
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
|
|
|
if (bytes_read < 0)
|
2005-10-12 03:27:46 +00:00
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
if (bytes_read > tar->entry_bytes_remaining)
|
|
|
|
bytes_read = tar->entry_bytes_remaining;
|
2004-06-27 01:15:31 +00:00
|
|
|
if (tar->sparse_list != NULL) {
|
2005-10-12 03:27:46 +00:00
|
|
|
/* Don't read more than is available in the
|
|
|
|
* current sparse block. */
|
2004-06-27 01:15:31 +00:00
|
|
|
if (tar->sparse_list->remaining < bytes_read)
|
|
|
|
bytes_read = tar->sparse_list->remaining;
|
2005-10-12 03:27:46 +00:00
|
|
|
tar->entry_offset = tar->sparse_list->offset;
|
2004-06-27 01:15:31 +00:00
|
|
|
tar->sparse_list->remaining -= bytes_read;
|
2005-10-12 03:27:46 +00:00
|
|
|
tar->sparse_list->offset += bytes_read;
|
2004-06-27 01:15:31 +00:00
|
|
|
}
|
2004-06-02 08:14:43 +00:00
|
|
|
*size = bytes_read;
|
|
|
|
*offset = tar->entry_offset;
|
|
|
|
tar->entry_offset += bytes_read;
|
|
|
|
tar->entry_bytes_remaining -= bytes_read;
|
|
|
|
(a->compression_read_consume)(a, bytes_read);
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
} else {
|
|
|
|
while (tar->entry_padding > 0) {
|
|
|
|
bytes_read = (a->compression_read_ahead)(a, buff, 1);
|
|
|
|
if (bytes_read <= 0)
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
if (bytes_read > tar->entry_padding)
|
|
|
|
bytes_read = tar->entry_padding;
|
|
|
|
(a->compression_read_consume)(a, bytes_read);
|
|
|
|
tar->entry_padding -= bytes_read;
|
|
|
|
}
|
|
|
|
*buff = NULL;
|
|
|
|
*size = 0;
|
|
|
|
*offset = tar->entry_offset;
|
|
|
|
return (ARCHIVE_EOF);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-07-30 00:29:01 +00:00
|
|
|
static int
|
|
|
|
archive_read_format_tar_skip(struct archive *a)
|
|
|
|
{
|
|
|
|
ssize_t bytes_skipped;
|
|
|
|
struct tar* tar;
|
|
|
|
struct sparse_block *p;
|
|
|
|
int r = ARCHIVE_OK;
|
|
|
|
const void *b; /* dummy variables */
|
|
|
|
size_t s;
|
|
|
|
off_t o;
|
2006-09-05 05:59:46 +00:00
|
|
|
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
tar = (struct tar *)*(a->pformat_data);
|
2006-07-30 00:29:01 +00:00
|
|
|
if (a->compression_skip == NULL) {
|
|
|
|
while (r == ARCHIVE_OK)
|
|
|
|
r = archive_read_format_tar_read_data(a, &b, &s, &o);
|
|
|
|
return (r);
|
|
|
|
}
|
2007-01-03 21:47:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Compression layer skip functions are required to either skip the
|
|
|
|
* length requested or fail, so we can rely upon the entire entry
|
|
|
|
* plus padding being skipped.
|
|
|
|
*/
|
|
|
|
bytes_skipped = (a->compression_skip)(a, tar->entry_bytes_remaining +
|
|
|
|
tar->entry_padding);
|
2006-07-30 00:29:01 +00:00
|
|
|
if (bytes_skipped < 0)
|
|
|
|
return (ARCHIVE_FATAL);
|
2007-01-03 21:47:35 +00:00
|
|
|
|
|
|
|
tar->entry_bytes_remaining = 0;
|
|
|
|
tar->entry_padding = 0;
|
|
|
|
|
|
|
|
/* Free the sparse list. */
|
|
|
|
while (tar->sparse_list != NULL) {
|
2006-07-30 00:29:01 +00:00
|
|
|
p = tar->sparse_list;
|
|
|
|
tar->sparse_list = p->next;
|
|
|
|
free(p);
|
|
|
|
}
|
2007-01-03 21:47:35 +00:00
|
|
|
|
|
|
|
return (ARCHIVE_OK);
|
2006-07-30 00:29:01 +00:00
|
|
|
}
|
|
|
|
|
2004-03-19 22:37:06 +00:00
|
|
|
/*
|
|
|
|
* This function recursively interprets all of the headers associated
|
|
|
|
* with a single entry.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
tar_read_header(struct archive *a, struct tar *tar,
|
|
|
|
struct archive_entry *entry, struct stat *st)
|
|
|
|
{
|
2004-02-09 23:22:54 +00:00
|
|
|
ssize_t bytes;
|
|
|
|
int err;
|
|
|
|
const void *h;
|
|
|
|
const struct archive_entry_header_ustar *header;
|
|
|
|
|
|
|
|
/* Read 512-byte header record */
|
|
|
|
bytes = (a->compression_read_ahead)(a, &h, 512);
|
|
|
|
if (bytes < 512) {
|
2004-06-04 10:27:23 +00:00
|
|
|
/*
|
|
|
|
* If we're here, it's becase the _bid function accepted
|
|
|
|
* this file. So just call a short read end-of-archive
|
|
|
|
* and be done with it.
|
|
|
|
*/
|
|
|
|
return (ARCHIVE_EOF);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
(a->compression_read_consume)(a, 512);
|
|
|
|
|
|
|
|
/* Check for end-of-archive mark. */
|
2006-11-10 06:39:46 +00:00
|
|
|
if (((*(const char *)h)==0) && archive_block_is_null((const unsigned char *)h)) {
|
2004-06-04 10:27:23 +00:00
|
|
|
/* Try to consume a second all-null record, as well. */
|
|
|
|
bytes = (a->compression_read_ahead)(a, &h, 512);
|
|
|
|
if (bytes > 0)
|
|
|
|
(a->compression_read_consume)(a, bytes);
|
2004-02-09 23:22:54 +00:00
|
|
|
archive_set_error(a, 0, NULL);
|
|
|
|
return (ARCHIVE_EOF);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note: If the checksum fails and we return ARCHIVE_RETRY,
|
2004-03-19 22:37:06 +00:00
|
|
|
* then the client is likely to just retry. This is a very
|
|
|
|
* crude way to search for the next valid header!
|
2004-02-09 23:22:54 +00:00
|
|
|
*
|
|
|
|
* TODO: Improve this by implementing a real header scan.
|
|
|
|
*/
|
|
|
|
if (!checksum(a, h)) {
|
|
|
|
archive_set_error(a, EINVAL, "Damaged tar archive");
|
|
|
|
return (ARCHIVE_RETRY); /* Retryable: Invalid header */
|
|
|
|
}
|
|
|
|
|
2004-03-19 22:37:06 +00:00
|
|
|
if (++tar->header_recursion_depth > 32) {
|
|
|
|
archive_set_error(a, EINVAL, "Too many special headers");
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/* Determine the format variant. */
|
2006-11-10 06:39:46 +00:00
|
|
|
header = (const struct archive_entry_header_ustar *)h;
|
2004-04-12 01:16:16 +00:00
|
|
|
switch(header->typeflag[0]) {
|
2004-03-19 22:37:06 +00:00
|
|
|
case 'A': /* Solaris tar ACL */
|
2004-02-09 23:22:54 +00:00
|
|
|
a->archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
|
2004-03-19 22:37:06 +00:00
|
|
|
a->archive_format_name = "Solaris tar";
|
|
|
|
err = header_Solaris_ACL(a, tar, entry, st, h);
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
2004-03-19 22:37:06 +00:00
|
|
|
case 'g': /* POSIX-standard 'g' header. */
|
2004-02-09 23:22:54 +00:00
|
|
|
a->archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
|
|
|
|
a->archive_format_name = "POSIX pax interchange format";
|
2004-03-19 22:37:06 +00:00
|
|
|
err = header_pax_global(a, tar, entry, st, h);
|
|
|
|
break;
|
2004-04-12 01:16:16 +00:00
|
|
|
case 'K': /* Long link name (GNU tar, others) */
|
2004-03-19 22:37:06 +00:00
|
|
|
err = header_longlink(a, tar, entry, st, h);
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
2004-04-12 01:16:16 +00:00
|
|
|
case 'L': /* Long filename (GNU tar, others) */
|
2004-03-19 22:37:06 +00:00
|
|
|
err = header_longname(a, tar, entry, st, h);
|
|
|
|
break;
|
2004-04-12 01:16:16 +00:00
|
|
|
case 'V': /* GNU volume header */
|
|
|
|
err = header_volume(a, tar, entry, st, h);
|
|
|
|
break;
|
2004-03-19 22:37:06 +00:00
|
|
|
case 'X': /* Used by SUN tar; same as 'x'. */
|
2004-02-09 23:22:54 +00:00
|
|
|
a->archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
|
|
|
|
a->archive_format_name =
|
|
|
|
"POSIX pax interchange format (Sun variant)";
|
2004-03-19 22:37:06 +00:00
|
|
|
err = header_pax_extensions(a, tar, entry, st, h);
|
|
|
|
break;
|
|
|
|
case 'x': /* POSIX-standard 'x' header. */
|
|
|
|
a->archive_format = ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE;
|
|
|
|
a->archive_format_name = "POSIX pax interchange format";
|
|
|
|
err = header_pax_extensions(a, tar, entry, st, h);
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
default:
|
2004-04-12 01:16:16 +00:00
|
|
|
if (memcmp(header->magic, "ustar \0", 8) == 0) {
|
|
|
|
a->archive_format = ARCHIVE_FORMAT_TAR_GNUTAR;
|
|
|
|
a->archive_format_name = "GNU tar format";
|
|
|
|
err = header_gnutar(a, tar, entry, st, h);
|
|
|
|
} else if (memcmp(header->magic, "ustar", 5) == 0) {
|
|
|
|
if (a->archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE) {
|
|
|
|
a->archive_format = ARCHIVE_FORMAT_TAR_USTAR;
|
|
|
|
a->archive_format_name = "POSIX ustar format";
|
|
|
|
}
|
|
|
|
err = header_ustar(a, tar, entry, st, h);
|
|
|
|
} else {
|
|
|
|
a->archive_format = ARCHIVE_FORMAT_TAR;
|
|
|
|
a->archive_format_name = "tar (non-POSIX)";
|
|
|
|
err = header_old_tar(a, tar, entry, st, h);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
}
|
2004-03-19 22:37:06 +00:00
|
|
|
--tar->header_recursion_depth;
|
2004-02-09 23:22:54 +00:00
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return true if block checksum is correct.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
checksum(struct archive *a, const void *h)
|
|
|
|
{
|
|
|
|
const unsigned char *bytes;
|
|
|
|
const struct archive_entry_header_ustar *header;
|
|
|
|
int check, i, sum;
|
|
|
|
|
|
|
|
(void)a; /* UNUSED */
|
2006-11-10 06:39:46 +00:00
|
|
|
bytes = (const unsigned char *)h;
|
|
|
|
header = (const struct archive_entry_header_ustar *)h;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Test the checksum. Note that POSIX specifies _unsigned_
|
|
|
|
* bytes for this calculation.
|
|
|
|
*/
|
|
|
|
sum = tar_atol(header->checksum, sizeof(header->checksum));
|
|
|
|
check = 0;
|
|
|
|
for (i = 0; i < 148; i++)
|
|
|
|
check += (unsigned char)bytes[i];
|
|
|
|
for (; i < 156; i++)
|
|
|
|
check += 32;
|
|
|
|
for (; i < 512; i++)
|
|
|
|
check += (unsigned char)bytes[i];
|
|
|
|
if (sum == check)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Repeat test with _signed_ bytes, just in case this archive
|
|
|
|
* was created by an old BSD, Solaris, or HP-UX tar with a
|
|
|
|
* broken checksum calculation.
|
|
|
|
*/
|
|
|
|
check = 0;
|
|
|
|
for (i = 0; i < 148; i++)
|
|
|
|
check += (signed char)bytes[i];
|
|
|
|
for (; i < 156; i++)
|
|
|
|
check += 32;
|
|
|
|
for (; i < 512; i++)
|
|
|
|
check += (signed char)bytes[i];
|
|
|
|
if (sum == check)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return true if this block contains only nulls.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
archive_block_is_null(const unsigned char *p)
|
|
|
|
{
|
|
|
|
unsigned i;
|
|
|
|
|
|
|
|
for (i = 0; i < ARCHIVE_BYTES_PER_RECORD / sizeof(*p); i++)
|
|
|
|
if (*p++)
|
|
|
|
return (0);
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
2004-03-19 22:37:06 +00:00
|
|
|
/*
|
|
|
|
* Interpret 'A' Solaris ACL header
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
header_Solaris_ACL(struct archive *a, struct tar *tar,
|
|
|
|
struct archive_entry *entry, struct stat *st, const void *h)
|
|
|
|
{
|
|
|
|
int err, err2;
|
2004-04-12 01:16:16 +00:00
|
|
|
char *p;
|
|
|
|
wchar_t *wp;
|
2004-03-19 22:37:06 +00:00
|
|
|
|
2004-06-02 08:14:43 +00:00
|
|
|
err = read_body_to_string(a, tar, &(tar->acl_text), h);
|
2004-03-19 22:37:06 +00:00
|
|
|
err2 = tar_read_header(a, tar, entry, st);
|
2004-04-12 01:16:16 +00:00
|
|
|
err = err_combine(err, err2);
|
|
|
|
|
|
|
|
/* XXX Ensure p doesn't overrun acl_text */
|
2004-03-19 22:37:06 +00:00
|
|
|
|
2004-04-12 01:16:16 +00:00
|
|
|
/* Skip leading octal number. */
|
|
|
|
/* XXX TODO: Parse the octal number and sanity-check it. */
|
|
|
|
p = tar->acl_text.s;
|
|
|
|
while (*p != '\0')
|
|
|
|
p++;
|
|
|
|
p++;
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
wp = (wchar_t *)malloc((strlen(p) + 1) * sizeof(wchar_t));
|
2004-04-12 01:16:16 +00:00
|
|
|
if (wp != NULL) {
|
|
|
|
utf8_decode(wp, p, strlen(p));
|
|
|
|
err2 = __archive_entry_acl_parse_w(entry, wp,
|
|
|
|
ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
|
|
|
|
err = err_combine(err, err2);
|
|
|
|
free(wp);
|
2004-03-19 22:37:06 +00:00
|
|
|
}
|
|
|
|
|
2004-04-12 01:16:16 +00:00
|
|
|
return (err);
|
2004-03-19 22:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Interpret 'K' long linkname header.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
header_longlink(struct archive *a, struct tar *tar,
|
|
|
|
struct archive_entry *entry, struct stat *st, const void *h)
|
|
|
|
{
|
|
|
|
int err, err2;
|
|
|
|
|
2004-06-02 08:14:43 +00:00
|
|
|
err = read_body_to_string(a, tar, &(tar->longlink), h);
|
2004-03-19 22:37:06 +00:00
|
|
|
err2 = tar_read_header(a, tar, entry, st);
|
|
|
|
if (err == ARCHIVE_OK && err2 == ARCHIVE_OK) {
|
2004-04-12 01:16:16 +00:00
|
|
|
/* Set symlink if symlink already set, else hardlink. */
|
|
|
|
archive_entry_set_link(entry, tar->longlink.s);
|
2004-03-19 22:37:06 +00:00
|
|
|
}
|
|
|
|
return (err_combine(err, err2));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Interpret 'L' long filename header.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
header_longname(struct archive *a, struct tar *tar,
|
|
|
|
struct archive_entry *entry, struct stat *st, const void *h)
|
|
|
|
{
|
|
|
|
int err, err2;
|
|
|
|
|
2004-06-02 08:14:43 +00:00
|
|
|
err = read_body_to_string(a, tar, &(tar->longname), h);
|
2004-03-19 22:37:06 +00:00
|
|
|
/* Read and parse "real" header, then override name. */
|
|
|
|
err2 = tar_read_header(a, tar, entry, st);
|
|
|
|
if (err == ARCHIVE_OK && err2 == ARCHIVE_OK)
|
|
|
|
archive_entry_set_pathname(entry, tar->longname.s);
|
|
|
|
return (err_combine(err, err2));
|
|
|
|
}
|
|
|
|
|
2004-04-12 01:16:16 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Interpret 'V' GNU tar volume header.
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
header_volume(struct archive *a, struct tar *tar,
|
|
|
|
struct archive_entry *entry, struct stat *st, const void *h)
|
|
|
|
{
|
|
|
|
(void)h;
|
|
|
|
|
|
|
|
/* Just skip this and read the next header. */
|
|
|
|
return (tar_read_header(a, tar, entry, st));
|
|
|
|
}
|
|
|
|
|
2004-03-19 22:37:06 +00:00
|
|
|
/*
|
|
|
|
* Read body of an archive entry into an archive_string object.
|
|
|
|
*/
|
|
|
|
static int
|
2004-06-02 08:14:43 +00:00
|
|
|
read_body_to_string(struct archive *a, struct tar *tar,
|
|
|
|
struct archive_string *as, const void *h)
|
2004-03-19 22:37:06 +00:00
|
|
|
{
|
2004-06-04 23:24:21 +00:00
|
|
|
off_t size, padded_size;
|
|
|
|
ssize_t bytes_read, bytes_to_copy;
|
2004-03-19 22:37:06 +00:00
|
|
|
const struct archive_entry_header_ustar *header;
|
2004-06-04 23:24:21 +00:00
|
|
|
const void *src;
|
|
|
|
char *dest;
|
2004-03-19 22:37:06 +00:00
|
|
|
|
2004-06-04 23:24:21 +00:00
|
|
|
(void)tar; /* UNUSED */
|
2006-11-10 06:39:46 +00:00
|
|
|
header = (const struct archive_entry_header_ustar *)h;
|
2004-03-19 22:37:06 +00:00
|
|
|
size = tar_atol(header->size, sizeof(header->size));
|
|
|
|
|
|
|
|
/* Read the body into the string. */
|
|
|
|
archive_string_ensure(as, size+1);
|
2004-06-04 23:24:21 +00:00
|
|
|
padded_size = (size + 511) & ~ 511;
|
|
|
|
dest = as->s;
|
|
|
|
while (padded_size > 0) {
|
|
|
|
bytes_read = (a->compression_read_ahead)(a, &src, padded_size);
|
|
|
|
if (bytes_read < 0)
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
if (bytes_read > padded_size)
|
|
|
|
bytes_read = padded_size;
|
|
|
|
(a->compression_read_consume)(a, bytes_read);
|
|
|
|
bytes_to_copy = bytes_read;
|
|
|
|
if ((off_t)bytes_to_copy > size)
|
|
|
|
bytes_to_copy = (ssize_t)size;
|
|
|
|
memcpy(dest, src, bytes_to_copy);
|
|
|
|
dest += bytes_to_copy;
|
|
|
|
size -= bytes_to_copy;
|
|
|
|
padded_size -= bytes_read;
|
|
|
|
}
|
|
|
|
*dest = '\0';
|
|
|
|
return (ARCHIVE_OK);
|
2004-03-19 22:37:06 +00:00
|
|
|
}
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/*
|
|
|
|
* Parse out common header elements.
|
|
|
|
*
|
2004-03-19 22:37:06 +00:00
|
|
|
* This would be the same as header_old_tar, except that the
|
2004-02-09 23:22:54 +00:00
|
|
|
* filename is handled slightly differently for old and POSIX
|
|
|
|
* entries (POSIX entries support a 'prefix'). This factoring
|
2004-03-19 22:37:06 +00:00
|
|
|
* allows header_old_tar and header_ustar
|
2004-02-09 23:22:54 +00:00
|
|
|
* to handle filenames differently, while still putting most of the
|
|
|
|
* common parsing into one place.
|
|
|
|
*/
|
|
|
|
static int
|
2004-03-19 22:37:06 +00:00
|
|
|
header_common(struct archive *a, struct tar *tar, struct archive_entry *entry,
|
2004-02-09 23:22:54 +00:00
|
|
|
struct stat *st, const void *h)
|
|
|
|
{
|
|
|
|
const struct archive_entry_header_ustar *header;
|
|
|
|
char tartype;
|
|
|
|
|
2004-03-19 22:37:06 +00:00
|
|
|
(void)a; /* UNUSED */
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
header = (const struct archive_entry_header_ustar *)h;
|
2004-02-09 23:22:54 +00:00
|
|
|
if (header->linkname[0])
|
2004-03-19 22:37:06 +00:00
|
|
|
archive_strncpy(&(tar->entry_linkname), header->linkname,
|
2004-02-09 23:22:54 +00:00
|
|
|
sizeof(header->linkname));
|
|
|
|
else
|
2004-03-19 22:37:06 +00:00
|
|
|
archive_string_empty(&(tar->entry_linkname));
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/* Parse out the numeric fields (all are octal) */
|
|
|
|
st->st_mode = tar_atol(header->mode, sizeof(header->mode));
|
|
|
|
st->st_uid = tar_atol(header->uid, sizeof(header->uid));
|
|
|
|
st->st_gid = tar_atol(header->gid, sizeof(header->gid));
|
|
|
|
st->st_size = tar_atol(header->size, sizeof(header->size));
|
|
|
|
st->st_mtime = tar_atol(header->mtime, sizeof(header->mtime));
|
|
|
|
|
|
|
|
/* Handle the tar type flag appropriately. */
|
|
|
|
tartype = header->typeflag[0];
|
|
|
|
st->st_mode &= ~S_IFMT;
|
|
|
|
|
|
|
|
switch (tartype) {
|
|
|
|
case '1': /* Hard link */
|
2004-03-19 22:37:06 +00:00
|
|
|
archive_entry_set_hardlink(entry, tar->entry_linkname.s);
|
2004-02-09 23:22:54 +00:00
|
|
|
/*
|
|
|
|
* The following may seem odd, but: Technically, tar
|
|
|
|
* does not store the file type for a "hard link"
|
|
|
|
* entry, only the fact that it is a hard link. So, I
|
|
|
|
* leave the type zero normally. But, pax interchange
|
|
|
|
* format allows hard links to have data, which
|
|
|
|
* implies that the underlying entry is a regular
|
|
|
|
* file.
|
|
|
|
*/
|
|
|
|
if (st->st_size > 0)
|
|
|
|
st->st_mode |= S_IFREG;
|
2004-05-19 06:35:47 +00:00
|
|
|
|
2004-05-18 18:16:30 +00:00
|
|
|
/*
|
2004-05-19 06:35:47 +00:00
|
|
|
* A tricky point: Traditionally, tar readers have
|
2004-05-18 18:16:30 +00:00
|
|
|
* ignored the size field when reading hardlink
|
2004-05-19 06:35:47 +00:00
|
|
|
* entries, and some writers put non-zero sizes even
|
|
|
|
* though the body is empty. POSIX.1-2001 broke with
|
|
|
|
* this tradition by permitting hardlink entries to
|
|
|
|
* store valid bodies in pax interchange format, but
|
|
|
|
* not in ustar format. Since there is no hard and
|
|
|
|
* fast way to distinguish pax interchange from
|
2004-05-18 18:16:30 +00:00
|
|
|
* earlier archives (the 'x' and 'g' entries are
|
|
|
|
* optional, after all), we need a heuristic. Here, I
|
|
|
|
* use the bid function to test whether or not there's
|
2004-05-19 06:35:47 +00:00
|
|
|
* a valid header following. Of course, if we know
|
|
|
|
* this is pax interchange format, then we must obey
|
|
|
|
* the size.
|
|
|
|
*
|
|
|
|
* This heuristic will only fail for a pax interchange
|
|
|
|
* archive that is storing hardlink bodies, no pax
|
|
|
|
* extended attribute entries have yet occurred, and
|
|
|
|
* we encounter a hardlink entry for a file that is
|
|
|
|
* itself an uncompressed tar archive.
|
2004-05-18 18:16:30 +00:00
|
|
|
*/
|
2004-05-19 06:35:47 +00:00
|
|
|
if (st->st_size > 0 &&
|
|
|
|
a->archive_format != ARCHIVE_FORMAT_TAR_PAX_INTERCHANGE &&
|
|
|
|
archive_read_format_tar_bid(a) > 50)
|
2004-05-18 18:16:30 +00:00
|
|
|
st->st_size = 0;
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
case '2': /* Symlink */
|
|
|
|
st->st_mode |= S_IFLNK;
|
|
|
|
st->st_size = 0;
|
2004-03-19 22:37:06 +00:00
|
|
|
archive_entry_set_symlink(entry, tar->entry_linkname.s);
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
case '3': /* Character device */
|
|
|
|
st->st_mode |= S_IFCHR;
|
|
|
|
st->st_size = 0;
|
|
|
|
break;
|
|
|
|
case '4': /* Block device */
|
|
|
|
st->st_mode |= S_IFBLK;
|
|
|
|
st->st_size = 0;
|
|
|
|
break;
|
|
|
|
case '5': /* Dir */
|
|
|
|
st->st_mode |= S_IFDIR;
|
|
|
|
st->st_size = 0;
|
|
|
|
break;
|
|
|
|
case '6': /* FIFO device */
|
|
|
|
st->st_mode |= S_IFIFO;
|
|
|
|
st->st_size = 0;
|
|
|
|
break;
|
2004-04-12 01:16:16 +00:00
|
|
|
case 'D': /* GNU incremental directory type */
|
|
|
|
/*
|
|
|
|
* No special handling is actually required here.
|
|
|
|
* It might be nice someday to preprocess the file list and
|
|
|
|
* provide it to the client, though.
|
|
|
|
*/
|
|
|
|
st->st_mode |= S_IFDIR;
|
|
|
|
break;
|
|
|
|
case 'M': /* GNU "Multi-volume" (remainder of file from last archive)*/
|
|
|
|
/*
|
|
|
|
* As far as I can tell, this is just like a regular file
|
|
|
|
* entry, except that the contents should be _appended_ to
|
|
|
|
* the indicated file at the indicated offset. This may
|
|
|
|
* require some API work to fully support.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
case 'N': /* Old GNU "long filename" entry. */
|
|
|
|
/* The body of this entry is a script for renaming
|
|
|
|
* previously-extracted entries. Ugh. It will never
|
|
|
|
* be supported by libarchive. */
|
|
|
|
st->st_mode |= S_IFREG;
|
|
|
|
break;
|
|
|
|
case 'S': /* GNU sparse files */
|
2004-10-27 05:15:23 +00:00
|
|
|
/*
|
|
|
|
* Sparse files are really just regular files with
|
|
|
|
* sparse information in the extended area.
|
|
|
|
*/
|
|
|
|
/* FALL THROUGH */
|
2004-02-09 23:22:54 +00:00
|
|
|
default: /* Regular file and non-standard types */
|
|
|
|
/*
|
|
|
|
* Per POSIX: non-recognized types should always be
|
|
|
|
* treated as regular files.
|
|
|
|
*/
|
|
|
|
st->st_mode |= S_IFREG;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-03-19 22:37:06 +00:00
|
|
|
* Parse out header elements for "old-style" tar archives.
|
2004-02-09 23:22:54 +00:00
|
|
|
*/
|
|
|
|
static int
|
2004-03-19 22:37:06 +00:00
|
|
|
header_old_tar(struct archive *a, struct tar *tar, struct archive_entry *entry,
|
2004-02-09 23:22:54 +00:00
|
|
|
struct stat *st, const void *h)
|
|
|
|
{
|
|
|
|
const struct archive_entry_header_ustar *header;
|
|
|
|
|
|
|
|
/* Copy filename over (to ensure null termination). */
|
2006-11-10 06:39:46 +00:00
|
|
|
header = (const struct archive_entry_header_ustar *)h;
|
2004-03-19 22:37:06 +00:00
|
|
|
archive_strncpy(&(tar->entry_name), header->name, sizeof(header->name));
|
|
|
|
archive_entry_set_pathname(entry, tar->entry_name.s);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/* Grab rest of common fields */
|
2004-03-19 22:37:06 +00:00
|
|
|
header_common(a, tar, entry, st, h);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-06-02 08:14:43 +00:00
|
|
|
tar->entry_bytes_remaining = st->st_size;
|
|
|
|
tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
|
2004-02-09 23:22:54 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a file header for a pax extended archive entry.
|
|
|
|
*/
|
|
|
|
static int
|
2004-03-19 22:37:06 +00:00
|
|
|
header_pax_global(struct archive *a, struct tar *tar,
|
|
|
|
struct archive_entry *entry, struct stat *st, const void *h)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2004-03-19 22:37:06 +00:00
|
|
|
int err, err2;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-06-02 08:14:43 +00:00
|
|
|
err = read_body_to_string(a, tar, &(tar->pax_global), h);
|
2004-03-19 22:37:06 +00:00
|
|
|
err2 = tar_read_header(a, tar, entry, st);
|
|
|
|
return (err_combine(err, err2));
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-03-19 22:37:06 +00:00
|
|
|
header_pax_extensions(struct archive *a, struct tar *tar,
|
2004-02-09 23:22:54 +00:00
|
|
|
struct archive_entry *entry, struct stat *st, const void *h)
|
|
|
|
{
|
2004-03-19 22:37:06 +00:00
|
|
|
int err, err2;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-06-02 08:14:43 +00:00
|
|
|
read_body_to_string(a, tar, &(tar->pax_header), h);
|
2004-03-20 22:35:33 +00:00
|
|
|
|
2004-03-19 22:37:06 +00:00
|
|
|
/* Parse the next header. */
|
|
|
|
err = tar_read_header(a, tar, entry, st);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* TODO: Parse global/default options into 'entry' struct here
|
|
|
|
* before handling file-specific options.
|
|
|
|
*
|
|
|
|
* This design (parse standard header, then overwrite with pax
|
|
|
|
* extended attribute data) usually works well, but isn't ideal;
|
|
|
|
* it would be better to parse the pax extended attributes first
|
|
|
|
* and then skip any fields in the standard header that were
|
|
|
|
* defined in the pax header.
|
|
|
|
*/
|
2004-03-19 22:37:06 +00:00
|
|
|
err2 = pax_header(a, tar, entry, st, tar->pax_header.s);
|
|
|
|
err = err_combine(err, err2);
|
2004-06-02 08:14:43 +00:00
|
|
|
tar->entry_bytes_remaining = st->st_size;
|
|
|
|
tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
|
2004-02-09 23:22:54 +00:00
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a file header for a Posix "ustar" archive entry. This also
|
|
|
|
* handles "pax" or "extended ustar" entries.
|
|
|
|
*/
|
|
|
|
static int
|
2004-03-19 22:37:06 +00:00
|
|
|
header_ustar(struct archive *a, struct tar *tar, struct archive_entry *entry,
|
2004-02-09 23:22:54 +00:00
|
|
|
struct stat *st, const void *h)
|
|
|
|
{
|
|
|
|
const struct archive_entry_header_ustar *header;
|
2004-05-19 17:09:24 +00:00
|
|
|
struct archive_string *as;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
header = (const struct archive_entry_header_ustar *)h;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/* Copy name into an internal buffer to ensure null-termination. */
|
2004-05-19 17:09:24 +00:00
|
|
|
as = &(tar->entry_name);
|
2004-02-09 23:22:54 +00:00
|
|
|
if (header->prefix[0]) {
|
2004-05-19 17:09:24 +00:00
|
|
|
archive_strncpy(as, header->prefix, sizeof(header->prefix));
|
|
|
|
if (as->s[archive_strlen(as) - 1] != '/')
|
|
|
|
archive_strappend_char(as, '/');
|
|
|
|
archive_strncat(as, header->name, sizeof(header->name));
|
2004-02-09 23:22:54 +00:00
|
|
|
} else
|
2004-05-19 17:09:24 +00:00
|
|
|
archive_strncpy(as, header->name, sizeof(header->name));
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-05-19 17:09:24 +00:00
|
|
|
archive_entry_set_pathname(entry, as->s);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/* Handle rest of common fields. */
|
2004-03-19 22:37:06 +00:00
|
|
|
header_common(a, tar, entry, st, h);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/* Handle POSIX ustar fields. */
|
2004-03-19 22:37:06 +00:00
|
|
|
archive_strncpy(&(tar->entry_uname), header->uname,
|
2004-02-09 23:22:54 +00:00
|
|
|
sizeof(header->uname));
|
2004-03-19 22:37:06 +00:00
|
|
|
archive_entry_set_uname(entry, tar->entry_uname.s);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-03-19 22:37:06 +00:00
|
|
|
archive_strncpy(&(tar->entry_gname), header->gname,
|
2004-02-09 23:22:54 +00:00
|
|
|
sizeof(header->gname));
|
2004-03-19 22:37:06 +00:00
|
|
|
archive_entry_set_gname(entry, tar->entry_gname.s);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/* Parse out device numbers only for char and block specials. */
|
|
|
|
if (header->typeflag[0] == '3' || header->typeflag[0] == '4') {
|
|
|
|
st->st_rdev = makedev(
|
2004-06-27 18:38:13 +00:00
|
|
|
tar_atol(header->rdevmajor, sizeof(header->rdevmajor)),
|
|
|
|
tar_atol(header->rdevminor, sizeof(header->rdevminor)));
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
2004-06-02 08:14:43 +00:00
|
|
|
tar->entry_bytes_remaining = st->st_size;
|
|
|
|
tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse the pax extended attributes record.
|
|
|
|
*
|
|
|
|
* Returns non-zero if there's an error in the data.
|
|
|
|
*/
|
|
|
|
static int
|
2004-03-19 22:37:06 +00:00
|
|
|
pax_header(struct archive *a, struct tar *tar, struct archive_entry *entry,
|
|
|
|
struct stat *st, char *attr)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2004-03-19 22:37:06 +00:00
|
|
|
size_t attr_length, l, line_length;
|
|
|
|
char *line, *p;
|
|
|
|
wchar_t *key, *wp, *value;
|
|
|
|
int err, err2;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-03-19 22:37:06 +00:00
|
|
|
attr_length = strlen(attr);
|
|
|
|
err = ARCHIVE_OK;
|
2004-02-09 23:22:54 +00:00
|
|
|
while (attr_length > 0) {
|
|
|
|
/* Parse decimal length field at start of line. */
|
|
|
|
line_length = 0;
|
|
|
|
l = attr_length;
|
|
|
|
line = p = attr; /* Record start of line. */
|
|
|
|
while (l>0) {
|
|
|
|
if (*p == ' ') {
|
|
|
|
p++;
|
|
|
|
l--;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (*p < '0' || *p > '9')
|
|
|
|
return (-1);
|
|
|
|
line_length *= 10;
|
|
|
|
line_length += *p - '0';
|
2005-03-13 02:35:52 +00:00
|
|
|
if (line_length > 999999) {
|
|
|
|
archive_set_error(a, ARCHIVE_ERRNO_MISC,
|
|
|
|
"Rejecting pax extended attribute > 1MB");
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
p++;
|
|
|
|
l--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (line_length > attr_length)
|
|
|
|
return (0);
|
|
|
|
|
2004-03-19 22:37:06 +00:00
|
|
|
/* Ensure pax_entry buffer is big enough. */
|
|
|
|
if (tar->pax_entry_length <= line_length) {
|
2005-09-21 04:25:06 +00:00
|
|
|
wchar_t *old_entry = tar->pax_entry;
|
|
|
|
|
2004-03-19 22:37:06 +00:00
|
|
|
if (tar->pax_entry_length <= 0)
|
2004-04-13 23:45:37 +00:00
|
|
|
tar->pax_entry_length = 1024;
|
2004-03-19 22:37:06 +00:00
|
|
|
while (tar->pax_entry_length <= line_length + 1)
|
|
|
|
tar->pax_entry_length *= 2;
|
|
|
|
|
2005-09-21 04:25:06 +00:00
|
|
|
old_entry = tar->pax_entry;
|
2006-11-10 06:39:46 +00:00
|
|
|
tar->pax_entry = (wchar_t *)realloc(tar->pax_entry,
|
2004-03-19 22:37:06 +00:00
|
|
|
tar->pax_entry_length * sizeof(wchar_t));
|
2005-09-21 04:25:06 +00:00
|
|
|
if (tar->pax_entry == NULL) {
|
|
|
|
free(old_entry);
|
|
|
|
archive_set_error(a, ENOMEM,
|
|
|
|
"No memory");
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
2004-03-19 22:37:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Decode UTF-8 to wchar_t, null-terminate result. */
|
|
|
|
if (utf8_decode(tar->pax_entry, p,
|
|
|
|
line_length - (p - attr) - 1)) {
|
|
|
|
archive_set_error(a, ARCHIVE_ERRNO_MISC,
|
|
|
|
"Invalid UTF8 character in pax extended attribute");
|
|
|
|
err = err_combine(err, ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/* Null-terminate 'key' value. */
|
2005-09-10 22:58:06 +00:00
|
|
|
wp = key = tar->pax_entry;
|
2004-03-19 22:37:06 +00:00
|
|
|
if (key[0] == L'=')
|
2004-02-09 23:22:54 +00:00
|
|
|
return (-1);
|
2005-09-10 22:58:06 +00:00
|
|
|
while (*wp && *wp != L'=')
|
|
|
|
++wp;
|
|
|
|
if (*wp == L'\0' || wp == NULL) {
|
2004-03-19 22:37:06 +00:00
|
|
|
archive_set_error(a, ARCHIVE_ERRNO_MISC,
|
|
|
|
"Invalid pax extended attributes");
|
|
|
|
return (ARCHIVE_WARN);
|
|
|
|
}
|
|
|
|
*wp = 0;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-03-19 22:37:06 +00:00
|
|
|
/* Identify null-terminated 'value' portion. */
|
|
|
|
value = wp + 1;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-03-19 22:37:06 +00:00
|
|
|
/* Identify this attribute and set it in the entry. */
|
|
|
|
err2 = pax_attribute(entry, st, key, value);
|
|
|
|
err = err_combine(err, err2);
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/* Skip to next line */
|
|
|
|
attr += line_length;
|
|
|
|
attr_length -= line_length;
|
|
|
|
}
|
2004-03-19 22:37:06 +00:00
|
|
|
return (err);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
|
2006-03-21 16:55:46 +00:00
|
|
|
static int
|
|
|
|
pax_attribute_xattr(struct archive_entry *entry,
|
|
|
|
wchar_t *name, wchar_t *value)
|
|
|
|
{
|
|
|
|
char *name_decoded, *name_narrow;
|
|
|
|
void *value_decoded;
|
|
|
|
size_t value_len;
|
|
|
|
|
|
|
|
if (wcslen(name) < 18 || (wcsncmp(name, L"LIBARCHIVE.xattr.", 17)) != 0)
|
|
|
|
return 3;
|
|
|
|
|
|
|
|
name += 17;
|
|
|
|
|
|
|
|
/* URL-decode name */
|
|
|
|
name_narrow = wide_to_narrow(name);
|
|
|
|
if (name_narrow == NULL)
|
|
|
|
return 2;
|
|
|
|
name_decoded = url_decode(name_narrow);
|
|
|
|
free(name_narrow);
|
|
|
|
if (name_decoded == NULL)
|
|
|
|
return 2;
|
|
|
|
|
|
|
|
/* Base-64 decode value */
|
|
|
|
value_decoded = base64_decode(value, wcslen(value), &value_len);
|
|
|
|
if (value_decoded == NULL) {
|
|
|
|
free(name_decoded);
|
|
|
|
return 1;
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2006-03-21 16:55:46 +00:00
|
|
|
archive_entry_xattr_add_entry(entry, name_decoded,
|
|
|
|
value_decoded, value_len);
|
|
|
|
|
|
|
|
free(name_decoded);
|
|
|
|
free(value_decoded);
|
|
|
|
return 0;
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Parse a single key=value attribute. key/value pointers are
|
|
|
|
* assumed to point into reasonably long-lived storage.
|
|
|
|
*
|
|
|
|
* Note that POSIX reserves all-lowercase keywords. Vendor-specific
|
|
|
|
* extensions should always have keywords of the form "VENDOR.attribute"
|
|
|
|
* In particular, it's quite feasible to support many different
|
|
|
|
* vendor extensions here. I'm using "LIBARCHIVE" for extensions
|
|
|
|
* unique to this library (currently, there are none).
|
|
|
|
*
|
|
|
|
* Investigate other vendor-specific extensions, as well and see if
|
|
|
|
* any of them look useful.
|
|
|
|
*/
|
|
|
|
static int
|
2004-03-19 22:37:06 +00:00
|
|
|
pax_attribute(struct archive_entry *entry, struct stat *st,
|
|
|
|
wchar_t *key, wchar_t *value)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2004-04-21 05:13:42 +00:00
|
|
|
int64_t s;
|
|
|
|
long n;
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
switch (key[0]) {
|
|
|
|
case 'L':
|
|
|
|
/* Our extensions */
|
|
|
|
/* TODO: Handle arbitrary extended attributes... */
|
|
|
|
/*
|
|
|
|
if (strcmp(key, "LIBARCHIVE.xxxxxxx")==0)
|
|
|
|
archive_entry_set_xxxxxx(entry, value);
|
|
|
|
*/
|
2006-03-21 16:55:46 +00:00
|
|
|
if (wcsncmp(key, L"LIBARCHIVE.xattr.", 17)==0)
|
|
|
|
pax_attribute_xattr(entry, key, value);
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
/* We support some keys used by the "star" archiver */
|
2004-03-19 22:37:06 +00:00
|
|
|
if (wcscmp(key, L"SCHILY.acl.access")==0)
|
2004-04-05 21:12:29 +00:00
|
|
|
__archive_entry_acl_parse_w(entry, value,
|
|
|
|
ARCHIVE_ENTRY_ACL_TYPE_ACCESS);
|
2004-03-19 22:37:06 +00:00
|
|
|
else if (wcscmp(key, L"SCHILY.acl.default")==0)
|
2004-04-05 21:12:29 +00:00
|
|
|
__archive_entry_acl_parse_w(entry, value,
|
|
|
|
ARCHIVE_ENTRY_ACL_TYPE_DEFAULT);
|
2004-03-19 22:37:06 +00:00
|
|
|
else if (wcscmp(key, L"SCHILY.devmajor")==0)
|
|
|
|
st->st_rdev = makedev(tar_atol10(value, wcslen(value)),
|
2004-06-27 18:38:13 +00:00
|
|
|
minor(st->st_rdev));
|
2004-03-19 22:37:06 +00:00
|
|
|
else if (wcscmp(key, L"SCHILY.devminor")==0)
|
2004-06-27 18:38:13 +00:00
|
|
|
st->st_rdev = makedev(major(st->st_rdev),
|
2004-03-19 22:37:06 +00:00
|
|
|
tar_atol10(value, wcslen(value)));
|
|
|
|
else if (wcscmp(key, L"SCHILY.fflags")==0)
|
2004-04-26 23:37:54 +00:00
|
|
|
archive_entry_copy_fflags_text_w(entry, value);
|
2004-03-19 22:37:06 +00:00
|
|
|
else if (wcscmp(key, L"SCHILY.nlink")==0)
|
|
|
|
st->st_nlink = tar_atol10(value, wcslen(value));
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
case 'a':
|
2004-04-21 05:13:42 +00:00
|
|
|
if (wcscmp(key, L"atime")==0) {
|
|
|
|
pax_time(value, &s, &n);
|
|
|
|
st->st_atime = s;
|
|
|
|
ARCHIVE_STAT_SET_ATIME_NANOS(st, n);
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
case 'c':
|
2004-04-21 05:13:42 +00:00
|
|
|
if (wcscmp(key, L"ctime")==0) {
|
|
|
|
pax_time(value, &s, &n);
|
|
|
|
st->st_ctime = s;
|
|
|
|
ARCHIVE_STAT_SET_CTIME_NANOS(st, n);
|
|
|
|
} else if (wcscmp(key, L"charset")==0) {
|
2004-02-09 23:22:54 +00:00
|
|
|
/* TODO: Publish charset information in entry. */
|
2004-03-19 22:37:06 +00:00
|
|
|
} else if (wcscmp(key, L"comment")==0) {
|
2004-02-09 23:22:54 +00:00
|
|
|
/* TODO: Publish comment in entry. */
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'g':
|
2004-03-19 22:37:06 +00:00
|
|
|
if (wcscmp(key, L"gid")==0)
|
|
|
|
st->st_gid = tar_atol10(value, wcslen(value));
|
|
|
|
else if (wcscmp(key, L"gname")==0)
|
|
|
|
archive_entry_copy_gname_w(entry, value);
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
case 'l':
|
|
|
|
/* pax interchange doesn't distinguish hardlink vs. symlink. */
|
2004-03-19 22:37:06 +00:00
|
|
|
if (wcscmp(key, L"linkpath")==0) {
|
2004-02-09 23:22:54 +00:00
|
|
|
if (archive_entry_hardlink(entry))
|
2004-03-19 22:37:06 +00:00
|
|
|
archive_entry_copy_hardlink_w(entry, value);
|
2004-02-09 23:22:54 +00:00
|
|
|
else
|
2004-03-19 22:37:06 +00:00
|
|
|
archive_entry_copy_symlink_w(entry, value);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'm':
|
2004-04-21 05:13:42 +00:00
|
|
|
if (wcscmp(key, L"mtime")==0) {
|
|
|
|
pax_time(value, &s, &n);
|
|
|
|
st->st_mtime = s;
|
|
|
|
ARCHIVE_STAT_SET_MTIME_NANOS(st, n);
|
|
|
|
}
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
case 'p':
|
2004-03-19 22:37:06 +00:00
|
|
|
if (wcscmp(key, L"path")==0)
|
|
|
|
archive_entry_copy_pathname_w(entry, value);
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
/* POSIX has reserved 'realtime.*' */
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
/* POSIX has reserved 'security.*' */
|
2004-03-19 22:37:06 +00:00
|
|
|
/* Someday: if (wcscmp(key, L"security.acl")==0) { ... } */
|
|
|
|
if (wcscmp(key, L"size")==0)
|
|
|
|
st->st_size = tar_atol10(value, wcslen(value));
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
case 'u':
|
2004-03-19 22:37:06 +00:00
|
|
|
if (wcscmp(key, L"uid")==0)
|
|
|
|
st->st_uid = tar_atol10(value, wcslen(value));
|
|
|
|
else if (wcscmp(key, L"uname")==0)
|
|
|
|
archive_entry_copy_uname_w(entry, value);
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* parse a decimal time value, which may include a fractional portion
|
|
|
|
*/
|
|
|
|
static void
|
2004-04-21 05:13:42 +00:00
|
|
|
pax_time(const wchar_t *p, int64_t *ps, long *pn)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
|
|
|
char digit;
|
|
|
|
int64_t s;
|
|
|
|
unsigned long l;
|
|
|
|
int sign;
|
2004-07-24 17:46:45 +00:00
|
|
|
int64_t limit, last_digit_limit;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-07-24 17:46:45 +00:00
|
|
|
limit = max_int64 / 10;
|
|
|
|
last_digit_limit = max_int64 % 10;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
s = 0;
|
|
|
|
sign = 1;
|
|
|
|
if (*p == '-') {
|
|
|
|
sign = -1;
|
|
|
|
p++;
|
|
|
|
}
|
|
|
|
while (*p >= '0' && *p <= '9') {
|
|
|
|
digit = *p - '0';
|
2004-07-24 17:46:45 +00:00
|
|
|
if (s > limit ||
|
|
|
|
(s == limit && digit > last_digit_limit)) {
|
|
|
|
s = max_uint64;
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
s = (s * 10) + digit;
|
|
|
|
++p;
|
|
|
|
}
|
|
|
|
|
2004-04-21 05:13:42 +00:00
|
|
|
*ps = s * sign;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
/* Calculate nanoseconds. */
|
2004-04-21 05:13:42 +00:00
|
|
|
*pn = 0;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
if (*p != '.')
|
|
|
|
return;
|
|
|
|
|
|
|
|
l = 100000000UL;
|
|
|
|
do {
|
|
|
|
++p;
|
|
|
|
if (*p >= '0' && *p <= '9')
|
2004-04-21 05:13:42 +00:00
|
|
|
*pn += (*p - '0') * l;
|
2004-02-09 23:22:54 +00:00
|
|
|
else
|
|
|
|
break;
|
|
|
|
} while (l /= 10);
|
|
|
|
}
|
|
|
|
|
2004-04-12 01:16:16 +00:00
|
|
|
/*
|
|
|
|
* Parse GNU tar header
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
header_gnutar(struct archive *a, struct tar *tar, struct archive_entry *entry,
|
|
|
|
struct stat *st, const void *h)
|
|
|
|
{
|
|
|
|
const struct archive_entry_header_gnutar *header;
|
|
|
|
|
|
|
|
(void)a;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GNU header is like POSIX ustar, except 'prefix' is
|
|
|
|
* replaced with some other fields. This also means the
|
|
|
|
* filename is stored as in old-style archives.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* Grab fields common to all tar variants. */
|
|
|
|
header_common(a, tar, entry, st, h);
|
|
|
|
|
|
|
|
/* Copy filename over (to ensure null termination). */
|
2006-11-10 06:39:46 +00:00
|
|
|
header = (const struct archive_entry_header_gnutar *)h;
|
2004-04-12 01:16:16 +00:00
|
|
|
archive_strncpy(&(tar->entry_name), header->name,
|
|
|
|
sizeof(header->name));
|
|
|
|
archive_entry_set_pathname(entry, tar->entry_name.s);
|
|
|
|
|
|
|
|
/* Fields common to ustar and GNU */
|
|
|
|
/* XXX Can the following be factored out since it's common
|
|
|
|
* to ustar and gnu tar? Is it okay to move it down into
|
|
|
|
* header_common, perhaps? */
|
|
|
|
archive_strncpy(&(tar->entry_uname),
|
|
|
|
header->uname, sizeof(header->uname));
|
|
|
|
archive_entry_set_uname(entry, tar->entry_uname.s);
|
|
|
|
|
|
|
|
archive_strncpy(&(tar->entry_gname),
|
|
|
|
header->gname, sizeof(header->gname));
|
|
|
|
archive_entry_set_gname(entry, tar->entry_gname.s);
|
|
|
|
|
|
|
|
/* Parse out device numbers only for char and block specials */
|
|
|
|
if (header->typeflag[0] == '3' || header->typeflag[0] == '4')
|
|
|
|
st->st_rdev = makedev (
|
2004-06-27 18:38:13 +00:00
|
|
|
tar_atol(header->rdevmajor, sizeof(header->rdevmajor)),
|
|
|
|
tar_atol(header->rdevminor, sizeof(header->rdevminor)));
|
2004-04-12 01:16:16 +00:00
|
|
|
else
|
|
|
|
st->st_rdev = 0;
|
|
|
|
|
2004-06-27 01:15:31 +00:00
|
|
|
tar->entry_bytes_remaining = st->st_size;
|
|
|
|
tar->entry_padding = 0x1ff & (-tar->entry_bytes_remaining);
|
|
|
|
|
2004-04-12 01:16:16 +00:00
|
|
|
/* Grab GNU-specific fields. */
|
|
|
|
st->st_atime = tar_atol(header->atime, sizeof(header->atime));
|
2004-04-20 20:09:06 +00:00
|
|
|
st->st_ctime = tar_atol(header->ctime, sizeof(header->ctime));
|
2004-06-27 01:15:31 +00:00
|
|
|
if (header->realsize[0] != 0) {
|
|
|
|
st->st_size = tar_atol(header->realsize,
|
|
|
|
sizeof(header->realsize));
|
|
|
|
}
|
2004-04-12 01:16:16 +00:00
|
|
|
|
2004-06-27 01:15:31 +00:00
|
|
|
if (header->sparse[0].offset[0] != 0) {
|
|
|
|
gnu_read_sparse_data(a, tar, header);
|
|
|
|
} else {
|
|
|
|
if (header->isextended[0] != 0) {
|
|
|
|
/* XXX WTF? XXX */
|
|
|
|
}
|
|
|
|
}
|
2004-04-12 01:16:16 +00:00
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2005-10-12 15:38:45 +00:00
|
|
|
static int
|
2004-06-27 01:15:31 +00:00
|
|
|
gnu_read_sparse_data(struct archive *a, struct tar *tar,
|
|
|
|
const struct archive_entry_header_gnutar *header)
|
|
|
|
{
|
|
|
|
ssize_t bytes_read;
|
|
|
|
const void *data;
|
|
|
|
struct extended {
|
|
|
|
struct gnu_sparse sparse[21];
|
|
|
|
char isextended[1];
|
|
|
|
char padding[7];
|
|
|
|
};
|
|
|
|
const struct extended *ext;
|
|
|
|
|
|
|
|
gnu_parse_sparse_data(a, tar, header->sparse, 4);
|
|
|
|
if (header->isextended[0] == 0)
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
|
|
|
|
do {
|
|
|
|
bytes_read = (a->compression_read_ahead)(a, &data, 512);
|
|
|
|
if (bytes_read < 0)
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
if (bytes_read < 512) {
|
|
|
|
archive_set_error(a, ARCHIVE_ERRNO_FILE_FORMAT,
|
|
|
|
"Truncated tar archive "
|
|
|
|
"detected while reading sparse file data");
|
|
|
|
return (ARCHIVE_FATAL);
|
|
|
|
}
|
|
|
|
(a->compression_read_consume)(a, 512);
|
|
|
|
ext = (const struct extended *)data;
|
|
|
|
gnu_parse_sparse_data(a, tar, ext->sparse, 21);
|
|
|
|
} while (ext->isextended[0] != 0);
|
|
|
|
if (tar->sparse_list != NULL)
|
|
|
|
tar->entry_offset = tar->sparse_list->offset;
|
|
|
|
return (ARCHIVE_OK);
|
|
|
|
}
|
|
|
|
|
2005-10-12 15:38:45 +00:00
|
|
|
static void
|
2004-06-27 01:15:31 +00:00
|
|
|
gnu_parse_sparse_data(struct archive *a, struct tar *tar,
|
|
|
|
const struct gnu_sparse *sparse, int length)
|
|
|
|
{
|
|
|
|
struct sparse_block *last;
|
|
|
|
struct sparse_block *p;
|
|
|
|
|
|
|
|
(void)a; /* UNUSED */
|
|
|
|
|
|
|
|
last = tar->sparse_list;
|
|
|
|
while (last != NULL && last->next != NULL)
|
|
|
|
last = last->next;
|
|
|
|
|
|
|
|
while (length > 0 && sparse->offset[0] != 0) {
|
2006-11-10 06:39:46 +00:00
|
|
|
p = (struct sparse_block *)malloc(sizeof(*p));
|
2005-09-21 04:25:06 +00:00
|
|
|
if (p == NULL)
|
|
|
|
__archive_errx(1, "Out of memory");
|
2004-06-27 01:15:31 +00:00
|
|
|
memset(p, 0, sizeof(*p));
|
|
|
|
if (last != NULL)
|
|
|
|
last->next = p;
|
|
|
|
else
|
|
|
|
tar->sparse_list = p;
|
|
|
|
last = p;
|
|
|
|
p->offset = tar_atol(sparse->offset, sizeof(sparse->offset));
|
|
|
|
p->remaining =
|
|
|
|
tar_atol(sparse->numbytes, sizeof(sparse->numbytes));
|
|
|
|
sparse++;
|
|
|
|
length--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-09 23:22:54 +00:00
|
|
|
/*-
|
|
|
|
* Convert text->integer.
|
|
|
|
*
|
|
|
|
* Traditional tar formats (including POSIX) specify base-8 for
|
|
|
|
* all of the standard numeric fields. This is a significant limitation
|
|
|
|
* in practice:
|
|
|
|
* = file size is limited to 8GB
|
2004-06-27 18:38:13 +00:00
|
|
|
* = rdevmajor and rdevminor are limited to 21 bits
|
2004-02-09 23:22:54 +00:00
|
|
|
* = uid/gid are limited to 21 bits
|
|
|
|
*
|
|
|
|
* There are two workarounds for this:
|
|
|
|
* = pax extended headers, which use variable-length string fields
|
|
|
|
* = GNU tar and STAR both allow either base-8 or base-256 in
|
|
|
|
* most fields. The high bit is set to indicate base-256.
|
|
|
|
*
|
|
|
|
* On read, this implementation supports both extensions.
|
|
|
|
*/
|
|
|
|
static int64_t
|
|
|
|
tar_atol(const char *p, unsigned char_cnt)
|
|
|
|
{
|
2004-07-24 17:46:45 +00:00
|
|
|
/*
|
|
|
|
* Technically, GNU tar considers a field to be in base-256
|
|
|
|
* only if the first byte is 0xff or 0x80.
|
|
|
|
*/
|
2004-02-09 23:22:54 +00:00
|
|
|
if (*p & 0x80)
|
|
|
|
return (tar_atol256(p, char_cnt));
|
|
|
|
return (tar_atol8(p, char_cnt));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that this implementation does not (and should not!) obey
|
|
|
|
* locale settings; you cannot simply substitute strtol here, since
|
|
|
|
* it does obey locale.
|
|
|
|
*/
|
|
|
|
static int64_t
|
|
|
|
tar_atol8(const char *p, unsigned char_cnt)
|
|
|
|
{
|
2004-07-24 17:46:45 +00:00
|
|
|
int64_t l, limit, last_digit_limit;
|
|
|
|
int digit, sign, base;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-07-24 17:46:45 +00:00
|
|
|
base = 8;
|
|
|
|
limit = max_int64 / base;
|
|
|
|
last_digit_limit = max_int64 % base;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
while (*p == ' ' || *p == '\t')
|
|
|
|
p++;
|
|
|
|
if (*p == '-') {
|
|
|
|
sign = -1;
|
|
|
|
p++;
|
|
|
|
} else
|
|
|
|
sign = 1;
|
|
|
|
|
|
|
|
l = 0;
|
|
|
|
digit = *p - '0';
|
|
|
|
while (digit >= 0 && digit < base && char_cnt-- > 0) {
|
|
|
|
if (l>limit || (l == limit && digit > last_digit_limit)) {
|
2004-07-24 17:46:45 +00:00
|
|
|
l = max_uint64; /* Truncate on overflow. */
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
l = (l * base) + digit;
|
|
|
|
digit = *++p - '0';
|
|
|
|
}
|
|
|
|
return (sign < 0) ? -l : l;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Note that this implementation does not (and should not!) obey
|
|
|
|
* locale settings; you cannot simply substitute strtol here, since
|
|
|
|
* it does obey locale.
|
|
|
|
*/
|
|
|
|
static int64_t
|
2004-03-19 22:37:06 +00:00
|
|
|
tar_atol10(const wchar_t *p, unsigned char_cnt)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2004-07-24 17:46:45 +00:00
|
|
|
int64_t l, limit, last_digit_limit;
|
|
|
|
int base, digit, sign;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-07-24 17:46:45 +00:00
|
|
|
base = 10;
|
|
|
|
limit = max_int64 / base;
|
|
|
|
last_digit_limit = max_int64 % base;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
|
|
|
while (*p == ' ' || *p == '\t')
|
|
|
|
p++;
|
|
|
|
if (*p == '-') {
|
|
|
|
sign = -1;
|
|
|
|
p++;
|
|
|
|
} else
|
|
|
|
sign = 1;
|
|
|
|
|
|
|
|
l = 0;
|
|
|
|
digit = *p - '0';
|
|
|
|
while (digit >= 0 && digit < base && char_cnt-- > 0) {
|
|
|
|
if (l > limit || (l == limit && digit > last_digit_limit)) {
|
2004-07-24 17:46:45 +00:00
|
|
|
l = max_uint64; /* Truncate on overflow. */
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
l = (l * base) + digit;
|
|
|
|
digit = *++p - '0';
|
|
|
|
}
|
|
|
|
return (sign < 0) ? -l : l;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2004-07-24 17:46:45 +00:00
|
|
|
* Parse a base-256 integer. This is just a straight signed binary
|
|
|
|
* value in big-endian order, except that the high-order bit is
|
|
|
|
* ignored. Remember that "int64_t" may or may not be exactly 64
|
|
|
|
* bits; the implementation here tries to avoid making any assumptions
|
|
|
|
* about the actual size of an int64_t. It does assume we're using
|
|
|
|
* twos-complement arithmetic, though.
|
2004-02-09 23:22:54 +00:00
|
|
|
*/
|
|
|
|
static int64_t
|
2004-07-24 17:46:45 +00:00
|
|
|
tar_atol256(const char *_p, unsigned char_cnt)
|
2004-02-09 23:22:54 +00:00
|
|
|
{
|
2004-07-24 17:46:45 +00:00
|
|
|
int64_t l, upper_limit, lower_limit;
|
2005-09-24 21:15:00 +00:00
|
|
|
const unsigned char *p = (const unsigned char *)_p;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-07-24 17:46:45 +00:00
|
|
|
upper_limit = max_int64 / 256;
|
|
|
|
lower_limit = min_int64 / 256;
|
2004-02-09 23:22:54 +00:00
|
|
|
|
2004-07-24 17:46:45 +00:00
|
|
|
/* Pad with 1 or 0 bits, depending on sign. */
|
|
|
|
if ((0x40 & *p) == 0x40)
|
|
|
|
l = (int64_t)-1;
|
|
|
|
else
|
|
|
|
l = 0;
|
|
|
|
l = (l << 6) | (0x3f & *p++);
|
|
|
|
while (--char_cnt > 0) {
|
|
|
|
if (l > upper_limit) {
|
|
|
|
l = max_int64; /* Truncate on overflow */
|
|
|
|
break;
|
|
|
|
} else if (l < lower_limit) {
|
|
|
|
l = min_int64;
|
2004-02-09 23:22:54 +00:00
|
|
|
break;
|
|
|
|
}
|
2004-07-24 17:46:45 +00:00
|
|
|
l = (l << 8) | (0xff & (int64_t)*p++);
|
2004-02-09 23:22:54 +00:00
|
|
|
}
|
|
|
|
return (l);
|
|
|
|
}
|
2004-03-19 22:37:06 +00:00
|
|
|
|
|
|
|
static int
|
|
|
|
utf8_decode(wchar_t *dest, const char *src, size_t length)
|
|
|
|
{
|
|
|
|
size_t n;
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = 0;
|
2006-03-21 16:55:46 +00:00
|
|
|
while (length > 0) {
|
2004-03-20 22:35:33 +00:00
|
|
|
n = UTF8_mbrtowc(dest, src, length);
|
2004-03-19 22:37:06 +00:00
|
|
|
if (n == 0)
|
|
|
|
break;
|
|
|
|
dest++;
|
|
|
|
src += n;
|
|
|
|
length -= n;
|
|
|
|
}
|
|
|
|
*dest++ = L'\0';
|
|
|
|
return (err);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2006-05-01 01:02:19 +00:00
|
|
|
* Copied and simplified from FreeBSD libc/locale.
|
2004-03-19 22:37:06 +00:00
|
|
|
*/
|
|
|
|
static size_t
|
2004-03-20 22:35:33 +00:00
|
|
|
UTF8_mbrtowc(wchar_t *pwc, const char *s, size_t n)
|
2004-03-19 22:37:06 +00:00
|
|
|
{
|
|
|
|
int ch, i, len, mask;
|
2006-05-01 01:02:19 +00:00
|
|
|
unsigned long wch;
|
2004-03-19 22:37:06 +00:00
|
|
|
|
2006-05-01 01:02:19 +00:00
|
|
|
if (s == NULL || n == 0 || pwc == NULL)
|
2004-03-19 22:37:06 +00:00
|
|
|
return (0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine the number of octets that make up this character from
|
|
|
|
* the first octet, and a mask that extracts the interesting bits of
|
|
|
|
* the first octet.
|
|
|
|
*/
|
|
|
|
ch = (unsigned char)*s;
|
|
|
|
if ((ch & 0x80) == 0) {
|
|
|
|
mask = 0x7f;
|
|
|
|
len = 1;
|
|
|
|
} else if ((ch & 0xe0) == 0xc0) {
|
|
|
|
mask = 0x1f;
|
|
|
|
len = 2;
|
|
|
|
} else if ((ch & 0xf0) == 0xe0) {
|
|
|
|
mask = 0x0f;
|
|
|
|
len = 3;
|
|
|
|
} else if ((ch & 0xf8) == 0xf0) {
|
|
|
|
mask = 0x07;
|
|
|
|
len = 4;
|
|
|
|
} else if ((ch & 0xfc) == 0xf8) {
|
|
|
|
mask = 0x03;
|
|
|
|
len = 5;
|
2006-05-01 01:02:19 +00:00
|
|
|
} else if ((ch & 0xfe) == 0xfc) {
|
2004-03-19 22:37:06 +00:00
|
|
|
mask = 0x01;
|
|
|
|
len = 6;
|
|
|
|
} else {
|
2006-05-01 01:02:19 +00:00
|
|
|
/* Invalid first byte; convert to '?' */
|
|
|
|
*pwc = '?';
|
|
|
|
return (1);
|
2004-03-19 22:37:06 +00:00
|
|
|
}
|
|
|
|
|
2006-05-01 01:02:19 +00:00
|
|
|
if (n < (size_t)len) {
|
|
|
|
/* Invalid first byte; convert to '?' */
|
|
|
|
*pwc = '?';
|
|
|
|
return (1);
|
|
|
|
}
|
2004-03-19 22:37:06 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Decode the octet sequence representing the character in chunks
|
|
|
|
* of 6 bits, most significant first.
|
|
|
|
*/
|
|
|
|
wch = (unsigned char)*s++ & mask;
|
|
|
|
i = len;
|
|
|
|
while (--i != 0) {
|
|
|
|
if ((*s & 0xc0) != 0x80) {
|
2006-05-01 01:02:19 +00:00
|
|
|
/* Invalid intermediate byte; consume one byte and
|
|
|
|
* emit '?' */
|
|
|
|
*pwc = '?';
|
|
|
|
return (1);
|
2004-03-19 22:37:06 +00:00
|
|
|
}
|
|
|
|
wch <<= 6;
|
|
|
|
wch |= *s++ & 0x3f;
|
|
|
|
}
|
2006-05-01 01:02:19 +00:00
|
|
|
|
|
|
|
/* Assign the value to the output; out-of-range values
|
|
|
|
* just get truncated. */
|
|
|
|
*pwc = (wchar_t)wch;
|
2004-08-14 03:45:45 +00:00
|
|
|
#ifdef WCHAR_MAX
|
2006-05-01 01:02:19 +00:00
|
|
|
/*
|
|
|
|
* If platform has WCHAR_MAX, we can do something
|
|
|
|
* more sensible with out-of-range values.
|
|
|
|
*/
|
|
|
|
if (wch >= WCHAR_MAX)
|
|
|
|
*pwc = '?';
|
2004-08-14 03:45:45 +00:00
|
|
|
#endif
|
2006-05-01 01:02:19 +00:00
|
|
|
/* Return number of bytes input consumed: 0 for end-of-string. */
|
2004-03-19 22:37:06 +00:00
|
|
|
return (wch == L'\0' ? 0 : len);
|
|
|
|
}
|
2006-03-21 16:55:46 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* base64_decode - Base64 decode
|
|
|
|
*
|
|
|
|
* This accepts most variations of base-64 encoding, including:
|
|
|
|
* * with or without line breaks
|
|
|
|
* * with or without the final group padded with '=' or '_' characters
|
|
|
|
* (The most economical Base-64 variant does not pad the last group and
|
|
|
|
* omits line breaks; RFC1341 used for MIME requires both.)
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
base64_decode(const wchar_t *src, size_t len, size_t *out_len)
|
|
|
|
{
|
2006-11-10 06:39:46 +00:00
|
|
|
static const unsigned char digits[64] = {
|
|
|
|
'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
|
|
|
|
'O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b',
|
|
|
|
'c','d','e','f','g','h','i','j','k','l','m','n','o','p',
|
|
|
|
'q','r','s','t','u','v','w','x','y','z','0','1','2','3',
|
|
|
|
'4','5','6','7','8','9','+','/' };
|
2006-03-21 16:55:46 +00:00
|
|
|
static unsigned char decode_table[128];
|
|
|
|
char *out, *d;
|
|
|
|
|
|
|
|
/* If the decode table is not yet initialized, prepare it. */
|
|
|
|
if (decode_table[digits[1]] != 1) {
|
|
|
|
size_t i;
|
|
|
|
memset(decode_table, 0xff, sizeof(decode_table));
|
|
|
|
for (i = 0; i < sizeof(digits); i++)
|
|
|
|
decode_table[digits[i]] = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Allocate enough space to hold the entire output. */
|
|
|
|
/* Note that we may not use all of this... */
|
2006-11-10 06:39:46 +00:00
|
|
|
out = (char *)malloc((len * 3 + 3) / 4);
|
2006-03-21 16:55:46 +00:00
|
|
|
if (out == NULL) {
|
|
|
|
*out_len = 0;
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
d = out;
|
|
|
|
|
|
|
|
while (len > 0) {
|
|
|
|
/* Collect the next group of (up to) four characters. */
|
|
|
|
int v = 0;
|
|
|
|
int group_size = 0;
|
|
|
|
while (group_size < 4 && len > 0) {
|
|
|
|
/* '=' or '_' padding indicates final group. */
|
|
|
|
if (*src == '=' || *src == '_') {
|
|
|
|
len = 0;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* Skip illegal characters (including line breaks) */
|
|
|
|
if (*src > 127 || *src < 32
|
|
|
|
|| decode_table[*src] == 0xff) {
|
|
|
|
len--;
|
|
|
|
src++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
v <<= 6;
|
|
|
|
v |= decode_table[*src++];
|
|
|
|
len --;
|
|
|
|
group_size++;
|
|
|
|
}
|
|
|
|
/* Align a short group properly. */
|
|
|
|
v <<= 6 * (4 - group_size);
|
|
|
|
/* Unpack the group we just collected. */
|
|
|
|
switch (group_size) {
|
|
|
|
case 4: d[2] = v & 0xff;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 3: d[1] = (v >> 8) & 0xff;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
case 2: d[0] = (v >> 16) & 0xff;
|
|
|
|
break;
|
|
|
|
case 1: /* this is invalid! */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
d += group_size * 3 / 4;
|
|
|
|
}
|
|
|
|
|
|
|
|
*out_len = d - out;
|
|
|
|
return (out);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This is a little tricky because the C99 standard wcstombs()
|
|
|
|
* function returns the number of bytes that were converted,
|
|
|
|
* not the number that should be converted. As a result,
|
|
|
|
* we can never accurately size the output buffer (without
|
|
|
|
* doing a tedious output size calculation in advance).
|
|
|
|
* This approach (try a conversion, then try again if it fails)
|
|
|
|
* will almost always succeed on the first try, and is thus
|
|
|
|
* much faster, at the cost of sometimes requiring multiple
|
|
|
|
* passes while we expand the buffer.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
wide_to_narrow(const wchar_t *wval)
|
|
|
|
{
|
|
|
|
int converted_length;
|
|
|
|
/* Guess an output buffer size and try the conversion. */
|
|
|
|
int alloc_length = wcslen(wval) * 3;
|
2006-11-10 06:39:46 +00:00
|
|
|
char *mbs_val = (char *)malloc(alloc_length + 1);
|
2006-03-21 16:55:46 +00:00
|
|
|
if (mbs_val == NULL)
|
|
|
|
return (NULL);
|
|
|
|
converted_length = wcstombs(mbs_val, wval, alloc_length);
|
|
|
|
|
|
|
|
/* If we exhausted the buffer, resize and try again. */
|
|
|
|
while (converted_length >= alloc_length) {
|
|
|
|
free(mbs_val);
|
|
|
|
alloc_length *= 2;
|
2006-11-10 06:39:46 +00:00
|
|
|
mbs_val = (char *)malloc(alloc_length + 1);
|
2006-03-21 16:55:46 +00:00
|
|
|
if (mbs_val == NULL)
|
|
|
|
return (NULL);
|
|
|
|
converted_length = wcstombs(mbs_val, wval, alloc_length);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ensure a trailing null and return the final string. */
|
|
|
|
mbs_val[alloc_length] = '\0';
|
|
|
|
return (mbs_val);
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *
|
|
|
|
url_decode(const char *in)
|
|
|
|
{
|
|
|
|
char *out, *d;
|
|
|
|
const char *s;
|
|
|
|
|
2006-11-10 06:39:46 +00:00
|
|
|
out = (char *)malloc(strlen(in) + 1);
|
2006-03-21 16:55:46 +00:00
|
|
|
if (out == NULL)
|
|
|
|
return (NULL);
|
|
|
|
for (s = in, d = out; *s != '\0'; ) {
|
|
|
|
if (*s == '%') {
|
|
|
|
/* Try to convert % escape */
|
|
|
|
int digit1 = tohex(s[1]);
|
|
|
|
int digit2 = tohex(s[2]);
|
|
|
|
if (digit1 >= 0 && digit2 >= 0) {
|
|
|
|
/* Looks good, consume three chars */
|
|
|
|
s += 3;
|
|
|
|
/* Convert output */
|
|
|
|
*d++ = ((digit1 << 4) | digit2);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
/* Else fall through and treat '%' as normal char */
|
|
|
|
}
|
|
|
|
*d++ = *s++;
|
|
|
|
}
|
|
|
|
*d = '\0';
|
|
|
|
return (out);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
tohex(int c)
|
|
|
|
{
|
|
|
|
if (c >= '0' && c <= '9')
|
|
|
|
return (c - '0');
|
|
|
|
else if (c >= 'A' && c <= 'F')
|
|
|
|
return (c - 'A' + 10);
|
|
|
|
else if (c >= 'a' && c <= 'f')
|
|
|
|
return (c - 'a' + 10);
|
|
|
|
else
|
|
|
|
return (-1);
|
|
|
|
}
|