[cmp] update Bled to latest

* Also ensure that we support Unicode paths for 7-zip
* Also ensure that error messages are displayed in English
This commit is contained in:
Pete Batard 2020-06-10 20:00:47 +01:00
parent ad918c8f74
commit 71ede6d9a0
No known key found for this signature in database
GPG key ID: 38E0CF5E69EDD671
11 changed files with 187 additions and 69 deletions

View file

@ -1,7 +1,7 @@
/*
* unzip implementation for Bled/busybox
*
* Copyright © 2015 Pete Batard <pete@akeo.ie>
* Copyright © 2015-2020 Pete Batard <pete@akeo.ie>
* Based on mini unzip implementation for busybox © Ed Clark
* Loosely based on original busybox unzip applet © Laurence Anderson.
*
@ -162,19 +162,19 @@ struct BUG_cde_header_must_be_16_bytes {
#define BAD_CDF_OFFSET ((uint32_t)0xffffffff)
/* NB: does not preserve file position! */
static uint32_t find_cdf_offset(void)
static uint32_t find_cdf_offset(int fd)
{
cde_header_t cde_header;
unsigned char *p;
off_t end;
unsigned char *buf = xzalloc(PEEK_FROM_END);
end = xlseek(zip_fd, 0, SEEK_END);
end = lseek(fd, 0, SEEK_END);
end -= PEEK_FROM_END;
if (end < 0)
end = 0;
xlseek(zip_fd, end, SEEK_SET);
full_read(zip_fd, buf, PEEK_FROM_END);
lseek(fd, end, SEEK_SET);
full_read(fd, buf, PEEK_FROM_END);
cde_header.formatted.cdf_offset = BAD_CDF_OFFSET;
p = buf;
@ -205,18 +205,18 @@ static uint32_t find_cdf_offset(void)
return cde_header.formatted.cdf_offset;
};
static uint32_t read_next_cdf(uint32_t cdf_offset, cdf_header_t *cdf_ptr)
static uint32_t read_next_cdf(int fd, uint32_t cdf_offset, cdf_header_t *cdf_ptr)
{
off_t org;
org = xlseek(zip_fd, 0, SEEK_CUR);
org = lseek(fd, 0, SEEK_CUR);
if (!cdf_offset)
cdf_offset = find_cdf_offset();
cdf_offset = find_cdf_offset(fd);
if (cdf_offset != BAD_CDF_OFFSET) {
xlseek(zip_fd, cdf_offset + 4, SEEK_SET);
xread(zip_fd, cdf_ptr->raw, CDF_HEADER_LEN);
lseek(fd, cdf_offset + 4, SEEK_SET);
_read(fd, cdf_ptr->raw, CDF_HEADER_LEN);
FIX_ENDIANNESS_CDF(*cdf_ptr);
cdf_offset += 4 + CDF_HEADER_LEN
+ cdf_ptr->formatted.file_name_length
@ -224,29 +224,29 @@ static uint32_t read_next_cdf(uint32_t cdf_offset, cdf_header_t *cdf_ptr)
+ cdf_ptr->formatted.file_comment_length;
}
xlseek(zip_fd, org, SEEK_SET);
lseek(fd, org, SEEK_SET);
return cdf_offset;
};
#endif
static void unzip_skip(int zip_fd, off_t skip)
static void unzip_skip(int fd, off_t skip)
{
if (skip != 0)
if (lseek(zip_fd, skip, SEEK_CUR) == (off_t)-1)
bb_copyfd_exact_size(zip_fd, -1, skip);
if (lseek(fd, skip, SEEK_CUR) == (off_t)-1)
bb_copyfd_exact_size(fd, -1, skip);
}
IF_DESKTOP(long long) int FAST_FUNC unpack_zip_stream(transformer_state_t *xstate)
{
IF_DESKTOP(long long) int n = -EFAULT;
zip_header_t zip_header;
char *filename = NULL;
#if ENABLE_DESKTOP
uint32_t cdf_offset = 0;
#endif
while (1) {
uint32_t magic;
bool is_dir = false;
/* Check magic number */
safe_read(xstate->src_fd, &magic, 4);
/* Central directory? It's at the end, so exit */
@ -261,12 +261,12 @@ IF_DESKTOP(long long) int FAST_FUNC unpack_zip_stream(transformer_state_t *xstat
}
#endif
if (magic != ZIP_FILEHEADER_MAGIC)
bb_error_msg_and_err("invalid zip magic %08X", (int)magic);
bb_error_msg_and_err("invalid zip magic 0x%08X", magic);
/* Read the file header */
safe_read(xstate->src_fd, zip_header.raw, ZIP_HEADER_LEN);
FIX_ENDIANNESS_ZIP(zip_header);
if (zip_header.formatted.method != 8) {
if ((zip_header.formatted.method != 8) && (zip_header.formatted.method != 0)) {
bb_error_msg_and_err("zip method method %d is not supported", zip_header.formatted.method);
}
#if !ENABLE_DESKTOP
@ -281,22 +281,20 @@ IF_DESKTOP(long long) int FAST_FUNC unpack_zip_stream(transformer_state_t *xstat
if (cdf_offset != BAD_CDF_OFFSET) {
cdf_header_t cdf_header;
cdf_offset = read_next_cdf(cdf_offset, &cdf_header);
cdf_offset = read_next_cdf(xstate->src_fd, cdf_offset, &cdf_header);
/*
* Note: cdf_offset can become BAD_CDF_OFFSET after the above call.
*/
* Note: cdf_offset can become BAD_CDF_OFFSET after the above call.
*/
if (zip_header.formatted.zip_flags & SWAP_LE16(0x0008)) {
/* 0x0008 - streaming. [u]cmpsize can be reliably gotten
* only from Central Directory. See unzip_doc.txt
*/
* only from Central Directory. See unzip_doc.txt
*/
zip_header.formatted.crc32 = cdf_header.formatted.crc32;
zip_header.formatted.cmpsize = cdf_header.formatted.cmpsize;
zip_header.formatted.ucmpsize = cdf_header.formatted.ucmpsize;
}
if ((cdf_header.formatted.version_made_by >> 8) == 3) {
/* This archive is created on Unix */
dir_mode = file_mode = (cdf_header.formatted.external_file_attributes >> 16);
}
/* Check for UNIX/DOS/WIN directory */
is_dir = cdf_header.formatted.external_file_attributes & 0x40000010;
}
if (cdf_offset == BAD_CDF_OFFSET
&& (zip_header.formatted.zip_flags & SWAP_LE16(0x0008))
@ -306,28 +304,43 @@ IF_DESKTOP(long long) int FAST_FUNC unpack_zip_stream(transformer_state_t *xstat
}
#endif
/* Read filename */
filename = xzalloc(zip_header.formatted.filename_len + 1);
safe_read(xstate->src_fd, filename, zip_header.formatted.filename_len);
bb_printf("Processing archive file '%s'", filename);
free(filename);
/* Handle multiple file switching */
if ((!is_dir) && (xstate->dst_dir != NULL)) {
xstate->dst_size = zip_header.formatted.ucmpsize;
xstate->dst_name = xzalloc(zip_header.formatted.filename_len + 1);
safe_read(xstate->src_fd, xstate->dst_name, zip_header.formatted.filename_len);
n = transformer_switch_file(xstate);
free(xstate->dst_name);
if (n < 0)
goto err;
} else {
unzip_skip(xstate->src_fd, zip_header.formatted.filename_len);
}
/* Skip extra header bytes */
unzip_skip(xstate->src_fd, zip_header.formatted.extra_len);
/* Method 8 - inflate */
xstate->bytes_in = zip_header.formatted.cmpsize;
n = inflate_unzip(xstate);
if (zip_header.formatted.method == 0) {
if (!is_dir)
bb_error_msg_and_err("zip method method 0 is only supported for directories");
} else {
/* Method 8 - inflate */
xstate->bytes_in = zip_header.formatted.cmpsize;
n = inflate_unzip(xstate);
/* Validate decompression */
if (n >= 0) {
if (zip_header.formatted.ucmpsize != xstate->bytes_out)
bb_error_msg_and_err("bad length");
else if (zip_header.formatted.crc32 != (xstate->crc32 ^ 0xffffffffL))
bb_error_msg_and_err("crc error");
} else if (n != -ENOSPC) {
bb_error_msg_and_err("inflate error");
/* Validate decompression */
if (n >= 0) {
if (zip_header.formatted.ucmpsize != xstate->bytes_out)
bb_error_msg_and_err("bad length");
else if (zip_header.formatted.crc32 != (xstate->crc32 ^ 0xffffffffL))
bb_error_msg_and_err("crc error");
} else if (n != -ENOSPC) {
bb_error_msg_and_err("inflate error");
}
}
/* Only process the first file if not extracting to a dir */
if (xstate->dst_dir == NULL)
break;
}
err: