From 4aca87515a5083ae0e31ce3177189fd43b6d05ac Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sat, 3 Jan 2015 13:58:15 +0100 Subject: patch to Vanilla Tomato 1.28 --- .../busybox/archival/libunarchive/get_header_ar.c | 139 +++++++++++---------- 1 file changed, 72 insertions(+), 67 deletions(-) (limited to 'release/src/router/busybox/archival/libunarchive/get_header_ar.c') diff --git a/release/src/router/busybox/archival/libunarchive/get_header_ar.c b/release/src/router/busybox/archival/libunarchive/get_header_ar.c index 6c576a8d..d476a9d2 100644 --- a/release/src/router/busybox/archival/libunarchive/get_header_ar.c +++ b/release/src/router/busybox/archival/libunarchive/get_header_ar.c @@ -1,122 +1,127 @@ -/* - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. +/* vi: set sw=4 ts=4: */ +/* Copyright 2001 Glenn McGrath. * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Library General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. */ -#include -#include -#include -#include -#include "unarchive.h" #include "libbb.h" +#include "unarchive.h" -extern char get_header_ar(archive_handle_t *archive_handle) +char FAST_FUNC get_header_ar(archive_handle_t *archive_handle) { + int err; file_header_t *typed = archive_handle->file_header; union { char raw[60]; - struct { - char name[16]; - char date[12]; - char uid[6]; - char gid[6]; - char mode[8]; - char size[10]; - char magic[2]; - } formated; + struct { + char name[16]; + char date[12]; + char uid[6]; + char gid[6]; + char mode[8]; + char size[10]; + char magic[2]; + } formatted; } ar; -#ifdef CONFIG_FEATURE_AR_LONG_FILENAMES +#if ENABLE_FEATURE_AR_LONG_FILENAMES static char *ar_long_names; - static unsigned int ar_long_name_size; + static unsigned ar_long_name_size; #endif - /* dont use bb_xread as we want to handle the error ourself */ + /* dont use xread as we want to handle the error ourself */ if (read(archive_handle->src_fd, ar.raw, 60) != 60) { /* End Of File */ - return(EXIT_FAILURE); - } + return EXIT_FAILURE; + } - /* Some ar entries have a trailing '\n' after the previous data entry */ + /* ar header starts on an even byte (2 byte aligned) + * '\n' is used for padding + */ if (ar.raw[0] == '\n') { /* fix up the header, we started reading 1 byte too early */ memmove(ar.raw, &ar.raw[1], 59); - ar.raw[59] = bb_xread_char(archive_handle->src_fd); + ar.raw[59] = xread_char(archive_handle->src_fd); archive_handle->offset++; } archive_handle->offset += 60; - + /* align the headers based on the header magic */ - if ((ar.formated.magic[0] != '`') || (ar.formated.magic[1] != '\n')) { - bb_error_msg_and_die("Invalid ar header"); - } + if (ar.formatted.magic[0] != '`' || ar.formatted.magic[1] != '\n') + bb_error_msg_and_die("invalid ar header"); - typed->mode = strtol(ar.formated.mode, NULL, 8); - typed->mtime = atoi(ar.formated.date); - typed->uid = atoi(ar.formated.uid); - typed->gid = atoi(ar.formated.gid); - typed->size = atoi(ar.formated.size); + /* FIXME: more thorough routine would be in order here */ + /* (we have something like that in tar) */ + /* but for now we are lax. This code works because */ + /* on misformatted numbers bb_strtou returns all-ones */ + typed->mode = err = bb_strtou(ar.formatted.mode, NULL, 8); + if (err == -1) bb_error_msg_and_die("invalid ar header"); + typed->mtime = err = bb_strtou(ar.formatted.date, NULL, 10); + if (err == -1) bb_error_msg_and_die("invalid ar header"); + typed->uid = err = bb_strtou(ar.formatted.uid, NULL, 10); + if (err == -1) bb_error_msg_and_die("invalid ar header"); + typed->gid = err = bb_strtou(ar.formatted.gid, NULL, 10); + if (err == -1) bb_error_msg_and_die("invalid ar header"); + typed->size = err = bb_strtou(ar.formatted.size, NULL, 10); + if (err == -1) bb_error_msg_and_die("invalid ar header"); /* long filenames have '/' as the first character */ - if (ar.formated.name[0] == '/') { -#ifdef CONFIG_FEATURE_AR_LONG_FILENAMES - if (ar.formated.name[1] == '/') { + if (ar.formatted.name[0] == '/') { +#if ENABLE_FEATURE_AR_LONG_FILENAMES + unsigned long_offset; + + if (ar.formatted.name[1] == '/') { /* If the second char is a '/' then this entries data section * stores long filename for multiple entries, they are stored * in static variable long_names for use in future entries */ ar_long_name_size = typed->size; ar_long_names = xmalloc(ar_long_name_size); - bb_xread_all(archive_handle->src_fd, ar_long_names, ar_long_name_size); + xread(archive_handle->src_fd, ar_long_names, ar_long_name_size); archive_handle->offset += ar_long_name_size; /* This ar entries data section only contained filenames for other records * they are stored in the static ar_long_names for future reference */ - return (get_header_ar(archive_handle)); /* Return next header */ - } else if (ar.formated.name[1] == ' ') { + return get_header_ar(archive_handle); /* Return next header */ + } + + if (ar.formatted.name[1] == ' ') { /* This is the index of symbols in the file for compilers */ data_skip(archive_handle); - return (get_header_ar(archive_handle)); /* Return next header */ - } else { - /* The number after the '/' indicates the offset in the ar data section - (saved in variable long_name) that conatains the real filename */ - const unsigned int long_offset = atoi(&ar.formated.name[1]); - if (long_offset >= ar_long_name_size) { - bb_error_msg_and_die("Cant resolve long filename"); - } - typed->name = bb_xstrdup(ar_long_names + long_offset); + archive_handle->offset += typed->size; + return get_header_ar(archive_handle); /* Return next header */ + } + + /* The number after the '/' indicates the offset in the ar data section + * (saved in variable long_name) that conatains the real filename */ + long_offset = atoi(&ar.formatted.name[1]); + if (long_offset >= ar_long_name_size) { + bb_error_msg_and_die("can't resolve long filename"); } + typed->name = xstrdup(ar_long_names + long_offset); #else bb_error_msg_and_die("long filenames not supported"); #endif } else { /* short filenames */ - typed->name = bb_xstrndup(ar.formated.name, 16); + typed->name = xstrndup(ar.formatted.name, 16); } typed->name[strcspn(typed->name, " /")] = '\0'; if (archive_handle->filter(archive_handle) == EXIT_SUCCESS) { archive_handle->action_header(typed); +#if ENABLE_DPKG || ENABLE_DPKG_DEB if (archive_handle->sub_archive) { - while (archive_handle->action_data_subarchive(archive_handle->sub_archive) == EXIT_SUCCESS); - } else { + while (archive_handle->action_data_subarchive(archive_handle->sub_archive) == EXIT_SUCCESS) + continue; + } else +#endif archive_handle->action_data(archive_handle); - } } else { - data_skip(archive_handle); + data_skip(archive_handle); } - archive_handle->offset += typed->size + 1; + archive_handle->offset += typed->size; + /* Set the file pointer to the correct spot, we may have been reading a compressed file */ + lseek(archive_handle->src_fd, archive_handle->offset, SEEK_SET); - return(EXIT_SUCCESS); + return EXIT_SUCCESS; } - -- cgit v1.2.3-54-g00ecf