[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

@ -158,8 +158,6 @@ struct BUG_tar_header {
char c[sizeof(tar_header_t) == TAR_BLOCK_SIZE ? 1 : -1];
};
archive_handle_t *init_handle(void) FAST_FUNC;
char filter_accept_all(archive_handle_t *archive_handle) FAST_FUNC;
@ -205,23 +203,26 @@ void dealloc_bunzip(bunzip_data *bd) FAST_FUNC;
/* Meaning and direction (input/output) of the fields are transformer-specific */
typedef struct transformer_state_t {
smallint check_signature; /* most often referenced member */
int8_t check_signature; /* most often referenced member */
IF_DESKTOP(long long) int FAST_FUNC (*xformer)(struct transformer_state_t *xstate);
USE_FOR_NOMMU(const char *xformer_prog;)
/* Source */
int src_fd;
int src_fd;
/* Output */
int dst_fd;
size_t mem_output_size_max; /* if non-zero, decompress to RAM instead of fd */
size_t mem_output_size;
char *mem_output_buf;
int dst_fd;
const char *dst_dir; /* if non-NULL, extract to dir */
char *dst_name;
uint64_t dst_size;
size_t mem_output_size_max; /* if non-zero, decompress to RAM instead of fd */
size_t mem_output_size;
char *mem_output_buf;
off_t bytes_out;
off_t bytes_in; /* used in unzip code only: needs to know packed size */
uint32_t crc32;
time_t mtime; /* gunzip code may set this on exit */
uint64_t bytes_out;
uint64_t bytes_in; /* used in unzip code only: needs to know packed size */
uint32_t crc32;
time_t mtime; /* gunzip code may set this on exit */
} transformer_state_t;
void init_transformer_state(transformer_state_t *xstate) FAST_FUNC;
@ -229,6 +230,35 @@ ssize_t transformer_write(transformer_state_t *xstate, const void *buf, size_t b
ssize_t xtransformer_write(transformer_state_t *xstate, const void *buf, size_t bufsize) FAST_FUNC;
int check_signature16(transformer_state_t *xstate, unsigned magic16) FAST_FUNC;
static inline int transformer_switch_file(transformer_state_t* xstate)
{
char dst[MAX_PATH];
size_t i, last_slash = 0;
if (xstate->dst_fd > 0) {
_close(xstate->dst_fd);
xstate->dst_fd = -1;
}
_snprintf_s(dst, sizeof(dst), sizeof(dst), "%s/%s", xstate->dst_dir, xstate->dst_name);
for (i = 0; i < strlen(dst); i++) {
if (dst[i] == '/')
dst[i] = '\\';
if (dst[i] == '\\')
last_slash = i;
}
if (bled_switch != NULL)
bled_switch(dst, xstate->dst_size);
dst[last_slash] = 0;
bb_make_directory(dst, 0, 0);
dst[last_slash] = '/';
xstate->dst_fd = _openU(dst, _O_WRONLY | _O_CREAT | _O_TRUNC | _O_BINARY, _S_IREAD | _S_IWRITE);
if (xstate->dst_fd < 0) {
bb_error_msg("Could not create '%s' (errno: %d)", dst, errno);
return -errno;
}
return 0;
}
IF_DESKTOP(long long) int inflate_unzip(transformer_state_t *xstate) FAST_FUNC;
IF_DESKTOP(long long) int unpack_zip_stream(transformer_state_t *xstate) FAST_FUNC;
IF_DESKTOP(long long) int unpack_Z_stream(transformer_state_t *xstate) FAST_FUNC;

View File

@ -1,7 +1,7 @@
/*
* Bled (Base Library for Easy Decompression)
*
* Copyright © 2014-2015 Pete Batard <pete@akeo.ie>
* Copyright © 2014-2020 Pete Batard <pete@akeo.ie>
*
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
*/
@ -24,6 +24,7 @@ printf_t bled_printf = NULL;
read_t bled_read = NULL;
write_t bled_write = NULL;
progress_t bled_progress = NULL;
switch_t bled_switch = NULL;
unsigned long* bled_cancel_request;
static bool bled_initialized = 0;
jmp_buf bb_error_jmp;
@ -189,6 +190,53 @@ err:
return -1;
}
/* Uncompress all files from archive 'src', compressed using 'type', to destination dir 'dir' */
int64_t bled_uncompress_to_dir(const char* src, const char* dir, int type)
{
transformer_state_t xstate;
int64_t ret;
if (!bled_initialized) {
bb_error_msg("The library has not been initialized");
return -1;
}
bb_total_rb = 0;
init_transformer_state(&xstate);
xstate.src_fd = -1;
xstate.dst_fd = -1;
xstate.check_signature = 1;
xstate.src_fd = _openU(src, _O_RDONLY | _O_BINARY, 0);
if (xstate.src_fd < 0) {
bb_error_msg("Could not open '%s' (errno: %d)", src, errno);
goto err;
}
xstate.dst_dir = dir;
// Only zip archives are supported for now
if (type != BLED_COMPRESSION_ZIP) {
bb_error_msg("This compression format is not supported for directory extraction");
goto err;
}
if (setjmp(bb_error_jmp))
goto err;
ret = unpacker[type](&xstate);
_close(xstate.src_fd);
if (xstate.dst_fd > 0)
_close(xstate.dst_fd);
return ret;
err:
if (xstate.src_fd > 0)
_close(xstate.src_fd);
if (xstate.dst_fd > 0)
_close(xstate.dst_fd);
return -1;
}
int64_t bled_uncompress_from_buffer_to_buffer(const char* src, const size_t src_len, char* dst, size_t dst_len, int type)
{
int64_t ret;
@ -226,12 +274,15 @@ int64_t bled_uncompress_from_buffer_to_buffer(const char* src, const size_t src_
* When the parameters are not NULL you can:
* - specify the printf-like function you want to use to output message
* void print_function(const char* format, ...);
* - specify the read/write functions you want to use;
* - specify the function you want to use to display progress, based on number of source archive bytes read
* void progress_function(const uint64_t read_bytes);
* - specify the function you want to use when switching files in an archive
* void switch_function(const char* filename, const uint64_t filesize);
* - point to an unsigned long variable, to be used to cancel operations when set to non zero
*/
int bled_init(printf_t print_function, read_t read_function, write_t write_function,
progress_t progress_function, unsigned long* cancel_request)
progress_t progress_function, switch_t switch_function, unsigned long* cancel_request)
{
if (bled_initialized)
return -1;
@ -240,6 +291,7 @@ int bled_init(printf_t print_function, read_t read_function, write_t write_funct
bled_read = read_function;
bled_write = write_function;
bled_progress = progress_function;
bled_switch = switch_function;
bled_cancel_request = cancel_request;
return 0;
}
@ -249,6 +301,7 @@ void bled_exit(void)
{
bled_printf = NULL;
bled_progress = NULL;
bled_switch = NULL;
bled_cancel_request = NULL;
if (global_crc32_table) {
free(global_crc32_table);

View File

@ -19,6 +19,7 @@ typedef void (*printf_t) (const char* format, ...);
typedef void (*progress_t) (const uint64_t read_bytes);
typedef int (*read_t)(int fd, void* buf, unsigned int count);
typedef int (*write_t)(int fd, const void* buf, unsigned int count);
typedef void (*switch_t)(const char* filename, const uint64_t size);
typedef enum {
BLED_COMPRESSION_NONE = 0,
@ -41,6 +42,9 @@ int64_t bled_uncompress_with_handles(HANDLE hSrc, HANDLE hDst, int type);
/* Uncompress file 'src', compressed using 'type', to buffer 'buf' of size 'size' */
int64_t bled_uncompress_to_buffer(const char* src, char* buf, size_t size, int type);
/* Uncompress all files from archive 'src', compressed using 'type', to destination dir 'dir' */
int64_t bled_uncompress_to_dir(const char* src, const char* dir, int type);
/* Uncompress buffer 'src' of length 'src_len' to buffer 'dst' of size 'dst_len' */
int64_t bled_uncompress_from_buffer_to_buffer(const char* src, const size_t src_len, char* dst, size_t dst_len, int type);
@ -51,10 +55,12 @@ int64_t bled_uncompress_from_buffer_to_buffer(const char* src, const size_t src_
* - specify the read/write functions you want to use;
* - specify the function you want to use to display progress, based on number of source archive bytes read
* void progress_function(const uint64_t read_bytes);
* - specify the function you want to use when switching files in an archive
* void switch_function(const char* filename, const uint64_t filesize);
* - point to an unsigned long variable, to be used to cancel operations when set to non zero
*/
int bled_init(printf_t print_function, read_t read_function, write_t write_function,
progress_t progress_function, unsigned long* cancel_request);
progress_t progress_function, switch_t switch_function, unsigned long* cancel_request);
/* This call frees any resource used by the library */
void bled_exit(void);

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:

View File

@ -41,8 +41,14 @@
#define BB_BUFSIZE 0x40000
#define ENABLE_DESKTOP 1
#if ENABLE_DESKTOP
#define IF_DESKTOP(x) x
#define IF_NOT_DESKTOP(x)
#else
#define IF_DESKTOP(x)
#define IF_NOT_DESKTOP(x) x
#endif
#define IF_NOT_FEATURE_LZMA_FAST(x) x
#define uoff_t unsigned off_t
@ -122,6 +128,7 @@ typedef struct _llist_t {
extern void (*bled_printf) (const char* format, ...);
extern void (*bled_progress) (const uint64_t processed_bytes);
extern void (*bled_switch) (const char* filename, const uint64_t filesize);
extern int (*bled_read)(int fd, void* buf, unsigned int count);
extern int (*bled_write)(int fd, const void* buf, unsigned int count);
extern unsigned long* bled_cancel_request;

View File

@ -1534,7 +1534,7 @@ static BOOL WriteDrive(HANDLE hPhysicalDrive, HANDLE hSourceImage)
}
assert((uintptr_t)sec_buf % SelectedDrive.SectorSize == 0);
sec_buf_pos = 0;
bled_init(_uprintf, NULL, sector_write, update_progress, &FormatStatus);
bled_init(_uprintf, NULL, sector_write, update_progress, NULL, &FormatStatus);
bled_ret = bled_uncompress_with_handles(hSourceImage, hPhysicalDrive, img_report.compression_type);
bled_exit();
if ((bled_ret >= 0) && (sec_buf_pos != 0)) {

View File

@ -1043,6 +1043,15 @@ static __inline int _stat64U(const char *path, struct __stat64 *buffer)
return ret;
}
static __inline int _accessU(const char* path, int mode)
{
int ret;
wconvert(path);
ret = _waccess(wpath, mode);
wfree(path);
return ret;
}
// returned UTF-8 string must be freed
static __inline char* getenvU(const char* varname)
{

View File

@ -922,7 +922,7 @@ static DWORD WINAPI DownloadISOThread(LPVOID param)
free(sig);
uprintf("Signature is valid ✓");
uncompressed_size = *((uint64_t*)&compressed[5]);
if ((uncompressed_size < 1 * MB) && (bled_init(_uprintf, NULL, NULL, NULL, &FormatStatus) >= 0)) {
if ((uncompressed_size < 1 * MB) && (bled_init(_uprintf, NULL, NULL, NULL, NULL, &FormatStatus) >= 0)) {
fido_script = malloc((size_t)uncompressed_size);
size = bled_uncompress_from_buffer_to_buffer(compressed, dwCompressedSize, fido_script, (size_t)uncompressed_size, BLED_COMPRESSION_LZMA);
bled_exit();

View File

@ -33,7 +33,7 @@ LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG DIALOGEX 12, 12, 232, 326
STYLE DS_SETFONT | DS_MODALFRAME | DS_CENTER | WS_MINIMIZEBOX | WS_POPUP | WS_CAPTION | WS_SYSMENU
EXSTYLE WS_EX_ACCEPTFILES
CAPTION "Rufus 3.11.1670"
CAPTION "Rufus 3.11.1671"
FONT 9, "Segoe UI Symbol", 400, 0, 0x0
BEGIN
LTEXT "Drive Properties",IDS_DRIVE_PROPERTIES_TXT,8,6,53,12,NOT WS_GROUP
@ -395,8 +395,8 @@ END
//
VS_VERSION_INFO VERSIONINFO
FILEVERSION 3,11,1670,0
PRODUCTVERSION 3,11,1670,0
FILEVERSION 3,11,1671,0
PRODUCTVERSION 3,11,1671,0
FILEFLAGSMASK 0x3fL
#ifdef _DEBUG
FILEFLAGS 0x1L
@ -414,13 +414,13 @@ BEGIN
VALUE "Comments", "https://rufus.ie"
VALUE "CompanyName", "Akeo Consulting"
VALUE "FileDescription", "Rufus"
VALUE "FileVersion", "3.11.1670"
VALUE "FileVersion", "3.11.1671"
VALUE "InternalName", "Rufus"
VALUE "LegalCopyright", "© 2011-2020 Pete Batard (GPL v3)"
VALUE "LegalTrademarks", "https://www.gnu.org/licenses/gpl-3.0.html"
VALUE "OriginalFilename", "rufus-3.11.exe"
VALUE "ProductName", "Rufus"
VALUE "ProductVersion", "3.11.1670"
VALUE "ProductVersion", "3.11.1671"
END
END
BLOCK "VarFileInfo"

View File

@ -645,9 +645,9 @@ const char *WindowsErrorString(void)
static_sprintf(err_string, "[0x%08lX] ", error_code);
presize = (DWORD)strlen(err_string);
size = FormatMessageU(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, NULL, HRESULT_CODE(error_code),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), &err_string[presize],
sizeof(err_string)-(DWORD)strlen(err_string), NULL);
size = FormatMessageU(FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS, NULL,
HRESULT_CODE(error_code), MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US),
&err_string[presize], sizeof(err_string)-(DWORD)strlen(err_string), NULL);
if (size == 0) {
format_error = GetLastError();
if ((format_error) && (format_error != 0x13D)) // 0x13D, decode error, is returned for unknown codes

View File

@ -112,7 +112,7 @@ static BOOL Get7ZipPath(void)
if ( (GetRegistryKeyStr(REGKEY_HKCU, "7-Zip\\Path", sevenzip_path, sizeof(sevenzip_path)))
|| (GetRegistryKeyStr(REGKEY_HKLM, "7-Zip\\Path", sevenzip_path, sizeof(sevenzip_path))) ) {
static_strcat(sevenzip_path, "\\7z.exe");
return (_access(sevenzip_path, 0) != -1);
return (_accessU(sevenzip_path, 0) != -1);
}
return FALSE;
}
@ -250,7 +250,7 @@ BOOL IsCompressedBootableImage(const char* path)
if (buf == NULL)
return FALSE;
FormatStatus = 0;
bled_init(_uprintf, NULL, NULL, NULL, &FormatStatus);
bled_init(_uprintf, NULL, NULL, NULL, NULL, &FormatStatus);
dc = bled_uncompress_to_buffer(path, (char*)buf, MBR_SIZE, file_assoc[i].type);
bled_exit();
if (dc != MBR_SIZE) {