Blame libarchive/archive_read_support_format_ar.c

Packit Service 1d0348
/*-
Packit Service 1d0348
 * Copyright (c) 2007 Kai Wang
Packit Service 1d0348
 * Copyright (c) 2007 Tim Kientzle
Packit Service 1d0348
 * All rights reserved.
Packit Service 1d0348
 *
Packit Service 1d0348
 * Redistribution and use in source and binary forms, with or without
Packit Service 1d0348
 * modification, are permitted provided that the following conditions
Packit Service 1d0348
 * are met:
Packit Service 1d0348
 * 1. Redistributions of source code must retain the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer
Packit Service 1d0348
 *    in this position and unchanged.
Packit Service 1d0348
 * 2. Redistributions in binary form must reproduce the above copyright
Packit Service 1d0348
 *    notice, this list of conditions and the following disclaimer in the
Packit Service 1d0348
 *    documentation and/or other materials provided with the distribution.
Packit Service 1d0348
 *
Packit Service 1d0348
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
Packit Service 1d0348
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit Service 1d0348
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit Service 1d0348
 * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit Service 1d0348
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit Service 1d0348
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit Service 1d0348
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit Service 1d0348
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit Service 1d0348
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit Service 1d0348
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit Service 1d0348
 */
Packit Service 1d0348
Packit Service 1d0348
#include "archive_platform.h"
Packit Service 1d0348
__FBSDID("$FreeBSD: head/lib/libarchive/archive_read_support_format_ar.c 201101 2009-12-28 03:06:27Z kientzle $");
Packit Service 1d0348
Packit Service 1d0348
#ifdef HAVE_SYS_STAT_H
Packit Service 1d0348
#include <sys/stat.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_ERRNO_H
Packit Service 1d0348
#include <errno.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_STDLIB_H
Packit Service 1d0348
#include <stdlib.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_STRING_H
Packit Service 1d0348
#include <string.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
#ifdef HAVE_LIMITS_H
Packit Service 1d0348
#include <limits.h>
Packit Service 1d0348
#endif
Packit Service 1d0348
Packit Service 1d0348
#include "archive.h"
Packit Service 1d0348
#include "archive_entry.h"
Packit Service 1d0348
#include "archive_private.h"
Packit Service 1d0348
#include "archive_read_private.h"
Packit Service 1d0348
Packit Service 1d0348
struct ar {
Packit Service 1d0348
	int64_t	 entry_bytes_remaining;
Packit Service 1d0348
	/* unconsumed is purely to track data we've gotten from readahead,
Packit Service 1d0348
	 * but haven't yet marked as consumed.  Must be paired with
Packit Service 1d0348
	 * entry_bytes_remaining usage/modification.
Packit Service 1d0348
	 */
Packit Service 1d0348
	size_t   entry_bytes_unconsumed;
Packit Service 1d0348
	int64_t	 entry_offset;
Packit Service 1d0348
	int64_t	 entry_padding;
Packit Service 1d0348
	char	*strtab;
Packit Service 1d0348
	size_t	 strtab_size;
Packit Service 1d0348
	char	 read_global_header;
Packit Service 1d0348
};
Packit Service 1d0348
Packit Service 1d0348
/*
Packit Service 1d0348
 * Define structure of the "ar" header.
Packit Service 1d0348
 */
Packit Service 1d0348
#define AR_name_offset 0
Packit Service 1d0348
#define AR_name_size 16
Packit Service 1d0348
#define AR_date_offset 16
Packit Service 1d0348
#define AR_date_size 12
Packit Service 1d0348
#define AR_uid_offset 28
Packit Service 1d0348
#define AR_uid_size 6
Packit Service 1d0348
#define AR_gid_offset 34
Packit Service 1d0348
#define AR_gid_size 6
Packit Service 1d0348
#define AR_mode_offset 40
Packit Service 1d0348
#define AR_mode_size 8
Packit Service 1d0348
#define AR_size_offset 48
Packit Service 1d0348
#define AR_size_size 10
Packit Service 1d0348
#define AR_fmag_offset 58
Packit Service 1d0348
#define AR_fmag_size 2
Packit Service 1d0348
Packit Service 1d0348
static int	archive_read_format_ar_bid(struct archive_read *a, int);
Packit Service 1d0348
static int	archive_read_format_ar_cleanup(struct archive_read *a);
Packit Service 1d0348
static int	archive_read_format_ar_read_data(struct archive_read *a,
Packit Service 1d0348
		    const void **buff, size_t *size, int64_t *offset);
Packit Service 1d0348
static int	archive_read_format_ar_skip(struct archive_read *a);
Packit Service 1d0348
static int	archive_read_format_ar_read_header(struct archive_read *a,
Packit Service 1d0348
		    struct archive_entry *e);
Packit Service 1d0348
static uint64_t	ar_atol8(const char *p, unsigned char_cnt);
Packit Service 1d0348
static uint64_t	ar_atol10(const char *p, unsigned char_cnt);
Packit Service 1d0348
static int	ar_parse_gnu_filename_table(struct archive_read *a);
Packit Service 1d0348
static int	ar_parse_common_header(struct ar *ar, struct archive_entry *,
Packit Service 1d0348
		    const char *h);
Packit Service 1d0348
Packit Service 1d0348
int
Packit Service 1d0348
archive_read_support_format_ar(struct archive *_a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct archive_read *a = (struct archive_read *)_a;
Packit Service 1d0348
	struct ar *ar;
Packit Service 1d0348
	int r;
Packit Service 1d0348
Packit Service 1d0348
	archive_check_magic(_a, ARCHIVE_READ_MAGIC,
Packit Service 1d0348
	    ARCHIVE_STATE_NEW, "archive_read_support_format_ar");
Packit Service 1d0348
Packit Service 1d0348
	ar = (struct ar *)calloc(1, sizeof(*ar));
Packit Service 1d0348
	if (ar == NULL) {
Packit Service 1d0348
		archive_set_error(&a->archive, ENOMEM,
Packit Service 1d0348
		    "Can't allocate ar data");
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
	ar->strtab = NULL;
Packit Service 1d0348
Packit Service 1d0348
	r = __archive_read_register_format(a,
Packit Service 1d0348
	    ar,
Packit Service 1d0348
	    "ar",
Packit Service 1d0348
	    archive_read_format_ar_bid,
Packit Service 1d0348
	    NULL,
Packit Service 1d0348
	    archive_read_format_ar_read_header,
Packit Service 1d0348
	    archive_read_format_ar_read_data,
Packit Service 1d0348
	    archive_read_format_ar_skip,
Packit Service 1d0348
	    NULL,
Packit Service 1d0348
	    archive_read_format_ar_cleanup,
Packit Service 1d0348
	    NULL,
Packit Service 1d0348
	    NULL);
Packit Service 1d0348
Packit Service 1d0348
	if (r != ARCHIVE_OK) {
Packit Service 1d0348
		free(ar);
Packit Service 1d0348
		return (r);
Packit Service 1d0348
	}
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_read_format_ar_cleanup(struct archive_read *a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct ar *ar;
Packit Service 1d0348
Packit Service 1d0348
	ar = (struct ar *)(a->format->data);
Packit Service 1d0348
	if (ar->strtab)
Packit Service 1d0348
		free(ar->strtab);
Packit Service 1d0348
	free(ar);
Packit Service 1d0348
	(a->format->data) = NULL;
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_read_format_ar_bid(struct archive_read *a, int best_bid)
Packit Service 1d0348
{
Packit Service 1d0348
	const void *h;
Packit Service 1d0348
Packit Service 1d0348
	(void)best_bid; /* UNUSED */
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Verify the 8-byte file signature.
Packit Service 1d0348
	 * TODO: Do we need to check more than this?
Packit Service 1d0348
	 */
Packit Service 1d0348
	if ((h = __archive_read_ahead(a, 8, NULL)) == NULL)
Packit Service 1d0348
		return (-1);
Packit Service 1d0348
	if (memcmp(h, "!<arch>\n", 8) == 0) {
Packit Service 1d0348
		return (64);
Packit Service 1d0348
	}
Packit Service 1d0348
	return (-1);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
_ar_read_header(struct archive_read *a, struct archive_entry *entry,
Packit Service 1d0348
	struct ar *ar, const char *h, size_t *unconsumed)
Packit Service 1d0348
{
Packit Service 1d0348
	char filename[AR_name_size + 1];
Packit Service 1d0348
	uint64_t number; /* Used to hold parsed numbers before validation. */
Packit Service 1d0348
	size_t bsd_name_length, entry_size;
Packit Service 1d0348
	char *p, *st;
Packit Service 1d0348
	const void *b;
Packit Service 1d0348
	int r;
Packit Service 1d0348
Packit Service 1d0348
	/* Verify the magic signature on the file header. */
Packit Service 1d0348
	if (strncmp(h + AR_fmag_offset, "`\n", 2) != 0) {
Packit Service 1d0348
		archive_set_error(&a->archive, EINVAL,
Packit Service 1d0348
		    "Incorrect file header signature");
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Copy filename into work buffer. */
Packit Service 1d0348
	strncpy(filename, h + AR_name_offset, AR_name_size);
Packit Service 1d0348
	filename[AR_name_size] = '\0';
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Guess the format variant based on the filename.
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (a->archive.archive_format == ARCHIVE_FORMAT_AR) {
Packit Service 1d0348
		/* We don't already know the variant, so let's guess. */
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * Biggest clue is presence of '/': GNU starts special
Packit Service 1d0348
		 * filenames with '/', appends '/' as terminator to
Packit Service 1d0348
		 * non-special names, so anything with '/' should be
Packit Service 1d0348
		 * GNU except for BSD long filenames.
Packit Service 1d0348
		 */
Packit Service 1d0348
		if (strncmp(filename, "#1/", 3) == 0)
Packit Service 1d0348
			a->archive.archive_format = ARCHIVE_FORMAT_AR_BSD;
Packit Service 1d0348
		else if (strchr(filename, '/') != NULL)
Packit Service 1d0348
			a->archive.archive_format = ARCHIVE_FORMAT_AR_GNU;
Packit Service 1d0348
		else if (strncmp(filename, "__.SYMDEF", 9) == 0)
Packit Service 1d0348
			a->archive.archive_format = ARCHIVE_FORMAT_AR_BSD;
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * XXX Do GNU/SVR4 'ar' programs ever omit trailing '/'
Packit Service 1d0348
		 * if name exactly fills 16-byte field?  If so, we
Packit Service 1d0348
		 * can't assume entries without '/' are BSD. XXX
Packit Service 1d0348
		 */
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Update format name from the code. */
Packit Service 1d0348
	if (a->archive.archive_format == ARCHIVE_FORMAT_AR_GNU)
Packit Service 1d0348
		a->archive.archive_format_name = "ar (GNU/SVR4)";
Packit Service 1d0348
	else if (a->archive.archive_format == ARCHIVE_FORMAT_AR_BSD)
Packit Service 1d0348
		a->archive.archive_format_name = "ar (BSD)";
Packit Service 1d0348
	else
Packit Service 1d0348
		a->archive.archive_format_name = "ar";
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Remove trailing spaces from the filename.  GNU and BSD
Packit Service 1d0348
	 * variants both pad filename area out with spaces.
Packit Service 1d0348
	 * This will only be wrong if GNU/SVR4 'ar' implementations
Packit Service 1d0348
	 * omit trailing '/' for 16-char filenames and we have
Packit Service 1d0348
	 * a 16-char filename that ends in ' '.
Packit Service 1d0348
	 */
Packit Service 1d0348
	p = filename + AR_name_size - 1;
Packit Service 1d0348
	while (p >= filename && *p == ' ') {
Packit Service 1d0348
		*p = '\0';
Packit Service 1d0348
		p--;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Remove trailing slash unless first character is '/'.
Packit Service 1d0348
	 * (BSD entries never end in '/', so this will only trim
Packit Service 1d0348
	 * GNU-format entries.  GNU special entries start with '/'
Packit Service 1d0348
	 * and are not terminated in '/', so we don't trim anything
Packit Service 1d0348
	 * that starts with '/'.)
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (filename[0] != '/' && p > filename && *p == '/') {
Packit Service 1d0348
		*p = '\0';
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	if (p < filename) {
Packit Service 1d0348
		archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
		    "Found entry with empty filename");
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * '//' is the GNU filename table.
Packit Service 1d0348
	 * Later entries can refer to names in this table.
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (strcmp(filename, "//") == 0) {
Packit Service 1d0348
		/* This must come before any call to _read_ahead. */
Packit Service 1d0348
		ar_parse_common_header(ar, entry, h);
Packit Service 1d0348
		archive_entry_copy_pathname(entry, filename);
Packit Service 1d0348
		archive_entry_set_filetype(entry, AE_IFREG);
Packit Service 1d0348
		/* Get the size of the filename table. */
Packit Service 1d0348
		number = ar_atol10(h + AR_size_offset, AR_size_size);
Packit Service 1d0348
		if (number > SIZE_MAX || number > 1024 * 1024 * 1024) {
Packit Service 1d0348
			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
			    "Filename table too large");
Packit Service 1d0348
			return (ARCHIVE_FATAL);
Packit Service 1d0348
		}
Packit Service 1d0348
		entry_size = (size_t)number;
Packit Service 1d0348
		if (entry_size == 0) {
Packit Service 1d0348
			archive_set_error(&a->archive, EINVAL,
Packit Service 1d0348
			    "Invalid string table");
Packit Service 1d0348
			return (ARCHIVE_FATAL);
Packit Service 1d0348
		}
Packit Service 1d0348
		if (ar->strtab != NULL) {
Packit Service 1d0348
			archive_set_error(&a->archive, EINVAL,
Packit Service 1d0348
			    "More than one string tables exist");
Packit Service 1d0348
			return (ARCHIVE_FATAL);
Packit Service 1d0348
		}
Packit Service 1d0348
Packit Service 1d0348
		/* Read the filename table into memory. */
Packit Service 1d0348
		st = malloc(entry_size);
Packit Service 1d0348
		if (st == NULL) {
Packit Service 1d0348
			archive_set_error(&a->archive, ENOMEM,
Packit Service 1d0348
			    "Can't allocate filename table buffer");
Packit Service 1d0348
			return (ARCHIVE_FATAL);
Packit Service 1d0348
		}
Packit Service 1d0348
		ar->strtab = st;
Packit Service 1d0348
		ar->strtab_size = entry_size;
Packit Service 1d0348
Packit Service 1d0348
		if (*unconsumed) {
Packit Service 1d0348
			__archive_read_consume(a, *unconsumed);
Packit Service 1d0348
			*unconsumed = 0;
Packit Service 1d0348
		}
Packit Service 1d0348
Packit Service 1d0348
		if ((b = __archive_read_ahead(a, entry_size, NULL)) == NULL)
Packit Service 1d0348
			return (ARCHIVE_FATAL);
Packit Service 1d0348
		memcpy(st, b, entry_size);
Packit Service 1d0348
		__archive_read_consume(a, entry_size);
Packit Service 1d0348
		/* All contents are consumed. */
Packit Service 1d0348
		ar->entry_bytes_remaining = 0;
Packit Service 1d0348
		archive_entry_set_size(entry, ar->entry_bytes_remaining);
Packit Service 1d0348
Packit Service 1d0348
		/* Parse the filename table. */
Packit Service 1d0348
		return (ar_parse_gnu_filename_table(a));
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * GNU variant handles long filenames by storing /<number>
Packit Service 1d0348
	 * to indicate a name stored in the filename table.
Packit Service 1d0348
	 * XXX TODO: Verify that it's all digits... Don't be fooled
Packit Service 1d0348
	 * by "/9xyz" XXX
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (filename[0] == '/' && filename[1] >= '0' && filename[1] <= '9') {
Packit Service 1d0348
		number = ar_atol10(h + AR_name_offset + 1, AR_name_size - 1);
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * If we can't look up the real name, warn and return
Packit Service 1d0348
		 * the entry with the wrong name.
Packit Service 1d0348
		 */
Packit Service 1d0348
		if (ar->strtab == NULL || number >= ar->strtab_size) {
Packit Service 1d0348
			archive_set_error(&a->archive, EINVAL,
Packit Service 1d0348
			    "Can't find long filename for GNU/SVR4 archive entry");
Packit Service 1d0348
			archive_entry_copy_pathname(entry, filename);
Packit Service 1d0348
			/* Parse the time, owner, mode, size fields. */
Packit Service 1d0348
			ar_parse_common_header(ar, entry, h);
Packit Service 1d0348
			return (ARCHIVE_FATAL);
Packit Service 1d0348
		}
Packit Service 1d0348
Packit Service 1d0348
		archive_entry_copy_pathname(entry, &ar->strtab[(size_t)number]);
Packit Service 1d0348
		/* Parse the time, owner, mode, size fields. */
Packit Service 1d0348
		return (ar_parse_common_header(ar, entry, h));
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * BSD handles long filenames by storing "#1/" followed by the
Packit Service 1d0348
	 * length of filename as a decimal number, then prepends the
Packit Service 1d0348
	 * the filename to the file contents.
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (strncmp(filename, "#1/", 3) == 0) {
Packit Service 1d0348
		/* Parse the time, owner, mode, size fields. */
Packit Service 1d0348
		/* This must occur before _read_ahead is called again. */
Packit Service 1d0348
		ar_parse_common_header(ar, entry, h);
Packit Service 1d0348
Packit Service 1d0348
		/* Parse the size of the name, adjust the file size. */
Packit Service 1d0348
		number = ar_atol10(h + AR_name_offset + 3, AR_name_size - 3);
Packit Service 1d0348
		/* Sanity check the filename length:
Packit Service 1d0348
		 *   = Must be <= SIZE_MAX - 1
Packit Service 1d0348
		 *   = Must be <= 1MB
Packit Service 1d0348
		 *   = Cannot be bigger than the entire entry
Packit Service 1d0348
		 */
Packit Service 1d0348
		if (number > SIZE_MAX - 1
Packit Service 1d0348
		    || number > 1024 * 1024
Packit Service 1d0348
		    || (int64_t)number > ar->entry_bytes_remaining) {
Packit Service 1d0348
			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
			    "Bad input file size");
Packit Service 1d0348
			return (ARCHIVE_FATAL);
Packit Service 1d0348
		}
Packit Service 1d0348
		bsd_name_length = (size_t)number;
Packit Service 1d0348
		ar->entry_bytes_remaining -= bsd_name_length;
Packit Service 1d0348
		/* Adjust file size reported to client. */
Packit Service 1d0348
		archive_entry_set_size(entry, ar->entry_bytes_remaining);
Packit Service 1d0348
Packit Service 1d0348
		if (*unconsumed) {
Packit Service 1d0348
			__archive_read_consume(a, *unconsumed);
Packit Service 1d0348
			*unconsumed = 0;
Packit Service 1d0348
		}
Packit Service 1d0348
Packit Service 1d0348
		/* Read the long name into memory. */
Packit Service 1d0348
		if ((b = __archive_read_ahead(a, bsd_name_length, NULL)) == NULL) {
Packit Service 1d0348
			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
			    "Truncated input file");
Packit Service 1d0348
			return (ARCHIVE_FATAL);
Packit Service 1d0348
		}
Packit Service 1d0348
		/* Store it in the entry. */
Packit Service 1d0348
		p = (char *)malloc(bsd_name_length + 1);
Packit Service 1d0348
		if (p == NULL) {
Packit Service 1d0348
			archive_set_error(&a->archive, ENOMEM,
Packit Service 1d0348
			    "Can't allocate fname buffer");
Packit Service 1d0348
			return (ARCHIVE_FATAL);
Packit Service 1d0348
		}
Packit Service 1d0348
		strncpy(p, b, bsd_name_length);
Packit Service 1d0348
		p[bsd_name_length] = '\0';
Packit Service 1d0348
Packit Service 1d0348
		__archive_read_consume(a, bsd_name_length);
Packit Service 1d0348
Packit Service 1d0348
		archive_entry_copy_pathname(entry, p);
Packit Service 1d0348
		free(p);
Packit Service 1d0348
		return (ARCHIVE_OK);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * "/" is the SVR4/GNU archive symbol table.
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (strcmp(filename, "/") == 0) {
Packit Service 1d0348
		archive_entry_copy_pathname(entry, "/");
Packit Service 1d0348
		/* Parse the time, owner, mode, size fields. */
Packit Service 1d0348
		r = ar_parse_common_header(ar, entry, h);
Packit Service 1d0348
		/* Force the file type to a regular file. */
Packit Service 1d0348
		archive_entry_set_filetype(entry, AE_IFREG);
Packit Service 1d0348
		return (r);
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * "__.SYMDEF" is a BSD archive symbol table.
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (strcmp(filename, "__.SYMDEF") == 0) {
Packit Service 1d0348
		archive_entry_copy_pathname(entry, filename);
Packit Service 1d0348
		/* Parse the time, owner, mode, size fields. */
Packit Service 1d0348
		return (ar_parse_common_header(ar, entry, h));
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * Otherwise, this is a standard entry.  The filename
Packit Service 1d0348
	 * has already been trimmed as much as possible, based
Packit Service 1d0348
	 * on our current knowledge of the format.
Packit Service 1d0348
	 */
Packit Service 1d0348
	archive_entry_copy_pathname(entry, filename);
Packit Service 1d0348
	return (ar_parse_common_header(ar, entry, h));
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_read_format_ar_read_header(struct archive_read *a,
Packit Service 1d0348
    struct archive_entry *entry)
Packit Service 1d0348
{
Packit Service 1d0348
	struct ar *ar = (struct ar*)(a->format->data);
Packit Service 1d0348
	size_t unconsumed;
Packit Service 1d0348
	const void *header_data;
Packit Service 1d0348
	int ret;
Packit Service 1d0348
Packit Service 1d0348
	if (!ar->read_global_header) {
Packit Service 1d0348
		/*
Packit Service 1d0348
		 * We are now at the beginning of the archive,
Packit Service 1d0348
		 * so we need first consume the ar global header.
Packit Service 1d0348
		 */
Packit Service 1d0348
		__archive_read_consume(a, 8);
Packit Service 1d0348
		ar->read_global_header = 1;
Packit Service 1d0348
		/* Set a default format code for now. */
Packit Service 1d0348
		a->archive.archive_format = ARCHIVE_FORMAT_AR;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	/* Read the header for the next file entry. */
Packit Service 1d0348
	if ((header_data = __archive_read_ahead(a, 60, NULL)) == NULL)
Packit Service 1d0348
		/* Broken header. */
Packit Service 1d0348
		return (ARCHIVE_EOF);
Packit Service 1d0348
	
Packit Service 1d0348
	unconsumed = 60;
Packit Service 1d0348
	
Packit Service 1d0348
	ret = _ar_read_header(a, entry, ar, (const char *)header_data, &unconsumed);
Packit Service 1d0348
Packit Service 1d0348
	if (unconsumed)
Packit Service 1d0348
		__archive_read_consume(a, unconsumed);
Packit Service 1d0348
Packit Service 1d0348
	return ret;
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
ar_parse_common_header(struct ar *ar, struct archive_entry *entry,
Packit Service 1d0348
    const char *h)
Packit Service 1d0348
{
Packit Service 1d0348
	uint64_t n;
Packit Service 1d0348
Packit Service 1d0348
	/* Copy remaining header */
Packit Service 1d0348
	archive_entry_set_mtime(entry,
Packit Service 1d0348
	    (time_t)ar_atol10(h + AR_date_offset, AR_date_size), 0L);
Packit Service 1d0348
	archive_entry_set_uid(entry,
Packit Service 1d0348
	    (uid_t)ar_atol10(h + AR_uid_offset, AR_uid_size));
Packit Service 1d0348
	archive_entry_set_gid(entry,
Packit Service 1d0348
	    (gid_t)ar_atol10(h + AR_gid_offset, AR_gid_size));
Packit Service 1d0348
	archive_entry_set_mode(entry,
Packit Service 1d0348
	    (mode_t)ar_atol8(h + AR_mode_offset, AR_mode_size));
Packit Service 1d0348
	n = ar_atol10(h + AR_size_offset, AR_size_size);
Packit Service 1d0348
Packit Service 1d0348
	ar->entry_offset = 0;
Packit Service 1d0348
	ar->entry_padding = n % 2;
Packit Service 1d0348
	archive_entry_set_size(entry, n);
Packit Service 1d0348
	ar->entry_bytes_remaining = n;
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_read_format_ar_read_data(struct archive_read *a,
Packit Service 1d0348
    const void **buff, size_t *size, int64_t *offset)
Packit Service 1d0348
{
Packit Service 1d0348
	ssize_t bytes_read;
Packit Service 1d0348
	struct ar *ar;
Packit Service 1d0348
Packit Service 1d0348
	ar = (struct ar *)(a->format->data);
Packit Service 1d0348
Packit Service 1d0348
	if (ar->entry_bytes_unconsumed) {
Packit Service 1d0348
		__archive_read_consume(a, ar->entry_bytes_unconsumed);
Packit Service 1d0348
		ar->entry_bytes_unconsumed = 0;
Packit Service 1d0348
	}
Packit Service 1d0348
Packit Service 1d0348
	if (ar->entry_bytes_remaining > 0) {
Packit Service 1d0348
		*buff = __archive_read_ahead(a, 1, &bytes_read);
Packit Service 1d0348
		if (bytes_read == 0) {
Packit Service 1d0348
			archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
			    "Truncated ar archive");
Packit Service 1d0348
			return (ARCHIVE_FATAL);
Packit Service 1d0348
		}
Packit Service 1d0348
		if (bytes_read < 0)
Packit Service 1d0348
			return (ARCHIVE_FATAL);
Packit Service 1d0348
		if (bytes_read > ar->entry_bytes_remaining)
Packit Service 1d0348
			bytes_read = (ssize_t)ar->entry_bytes_remaining;
Packit Service 1d0348
		*size = bytes_read;
Packit Service 1d0348
		ar->entry_bytes_unconsumed = bytes_read;
Packit Service 1d0348
		*offset = ar->entry_offset;
Packit Service 1d0348
		ar->entry_offset += bytes_read;
Packit Service 1d0348
		ar->entry_bytes_remaining -= bytes_read;
Packit Service 1d0348
		return (ARCHIVE_OK);
Packit Service 1d0348
	} else {
Packit Service 1d0348
		int64_t skipped = __archive_read_consume(a, ar->entry_padding);
Packit Service 1d0348
		if (skipped >= 0) {
Packit Service 1d0348
			ar->entry_padding -= skipped;
Packit Service 1d0348
		}
Packit Service 1d0348
		if (ar->entry_padding) {
Packit Service 1d0348
			if (skipped >= 0) {
Packit Service 1d0348
				archive_set_error(&a->archive, ARCHIVE_ERRNO_MISC,
Packit Service 1d0348
					"Truncated ar archive- failed consuming padding");
Packit Service 1d0348
			}
Packit Service 1d0348
			return (ARCHIVE_FATAL);
Packit Service 1d0348
		}
Packit Service 1d0348
		*buff = NULL;
Packit Service 1d0348
		*size = 0;
Packit Service 1d0348
		*offset = ar->entry_offset;
Packit Service 1d0348
		return (ARCHIVE_EOF);
Packit Service 1d0348
	}
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
archive_read_format_ar_skip(struct archive_read *a)
Packit Service 1d0348
{
Packit Service 1d0348
	int64_t bytes_skipped;
Packit Service 1d0348
	struct ar* ar;
Packit Service 1d0348
Packit Service 1d0348
	ar = (struct ar *)(a->format->data);
Packit Service 1d0348
Packit Service 1d0348
	bytes_skipped = __archive_read_consume(a,
Packit Service 1d0348
	    ar->entry_bytes_remaining + ar->entry_padding
Packit Service 1d0348
	    + ar->entry_bytes_unconsumed);
Packit Service 1d0348
	if (bytes_skipped < 0)
Packit Service 1d0348
		return (ARCHIVE_FATAL);
Packit Service 1d0348
Packit Service 1d0348
	ar->entry_bytes_remaining = 0;
Packit Service 1d0348
	ar->entry_bytes_unconsumed = 0;
Packit Service 1d0348
	ar->entry_padding = 0;
Packit Service 1d0348
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static int
Packit Service 1d0348
ar_parse_gnu_filename_table(struct archive_read *a)
Packit Service 1d0348
{
Packit Service 1d0348
	struct ar *ar;
Packit Service 1d0348
	char *p;
Packit Service 1d0348
	size_t size;
Packit Service 1d0348
Packit Service 1d0348
	ar = (struct ar*)(a->format->data);
Packit Service 1d0348
	size = ar->strtab_size;
Packit Service 1d0348
Packit Service 1d0348
	for (p = ar->strtab; p < ar->strtab + size - 1; ++p) {
Packit Service 1d0348
		if (*p == '/') {
Packit Service 1d0348
			*p++ = '\0';
Packit Service 1d0348
			if (*p != '\n')
Packit Service 1d0348
				goto bad_string_table;
Packit Service 1d0348
			*p = '\0';
Packit Service 1d0348
		}
Packit Service 1d0348
	}
Packit Service 1d0348
	/*
Packit Service 1d0348
	 * GNU ar always pads the table to an even size.
Packit Service 1d0348
	 * The pad character is either '\n' or '`'.
Packit Service 1d0348
	 */
Packit Service 1d0348
	if (p != ar->strtab + size && *p != '\n' && *p != '`')
Packit Service 1d0348
		goto bad_string_table;
Packit Service 1d0348
Packit Service 1d0348
	/* Enforce zero termination. */
Packit Service 1d0348
	ar->strtab[size - 1] = '\0';
Packit Service 1d0348
Packit Service 1d0348
	return (ARCHIVE_OK);
Packit Service 1d0348
Packit Service 1d0348
bad_string_table:
Packit Service 1d0348
	archive_set_error(&a->archive, EINVAL,
Packit Service 1d0348
	    "Invalid string table");
Packit Service 1d0348
	free(ar->strtab);
Packit Service 1d0348
	ar->strtab = NULL;
Packit Service 1d0348
	return (ARCHIVE_FATAL);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static uint64_t
Packit Service 1d0348
ar_atol8(const char *p, unsigned char_cnt)
Packit Service 1d0348
{
Packit Service 1d0348
	uint64_t l, limit, last_digit_limit;
Packit Service 1d0348
	unsigned int digit, base;
Packit Service 1d0348
Packit Service 1d0348
	base = 8;
Packit Service 1d0348
	limit = UINT64_MAX / base;
Packit Service 1d0348
	last_digit_limit = UINT64_MAX % base;
Packit Service 1d0348
Packit Service 1d0348
	while ((*p == ' ' || *p == '\t') && char_cnt-- > 0)
Packit Service 1d0348
		p++;
Packit Service 1d0348
Packit Service 1d0348
	l = 0;
Packit Service 1d0348
	digit = *p - '0';
Packit Service 1d0348
	while (*p >= '0' && digit < base  && char_cnt-- > 0) {
Packit Service 1d0348
		if (l>limit || (l == limit && digit > last_digit_limit)) {
Packit Service 1d0348
			l = UINT64_MAX; /* Truncate on overflow. */
Packit Service 1d0348
			break;
Packit Service 1d0348
		}
Packit Service 1d0348
		l = (l * base) + digit;
Packit Service 1d0348
		digit = *++p - '0';
Packit Service 1d0348
	}
Packit Service 1d0348
	return (l);
Packit Service 1d0348
}
Packit Service 1d0348
Packit Service 1d0348
static uint64_t
Packit Service 1d0348
ar_atol10(const char *p, unsigned char_cnt)
Packit Service 1d0348
{
Packit Service 1d0348
	uint64_t l, limit, last_digit_limit;
Packit Service 1d0348
	unsigned int base, digit;
Packit Service 1d0348
Packit Service 1d0348
	base = 10;
Packit Service 1d0348
	limit = UINT64_MAX / base;
Packit Service 1d0348
	last_digit_limit = UINT64_MAX % base;
Packit Service 1d0348
Packit Service 1d0348
	while ((*p == ' ' || *p == '\t') && char_cnt-- > 0)
Packit Service 1d0348
		p++;
Packit Service 1d0348
	l = 0;
Packit Service 1d0348
	digit = *p - '0';
Packit Service 1d0348
	while (*p >= '0' && digit < base  && char_cnt-- > 0) {
Packit Service 1d0348
		if (l > limit || (l == limit && digit > last_digit_limit)) {
Packit Service 1d0348
			l = UINT64_MAX; /* Truncate on overflow. */
Packit Service 1d0348
			break;
Packit Service 1d0348
		}
Packit Service 1d0348
		l = (l * base) + digit;
Packit Service 1d0348
		digit = *++p - '0';
Packit Service 1d0348
	}
Packit Service 1d0348
	return (l);
Packit Service 1d0348
}