diff --git a/libretro-common/file/archive_file_7z.c b/libretro-common/file/archive_file_7z.c index ef9f96b9de..89486d668e 100644 --- a/libretro-common/file/archive_file_7z.c +++ b/libretro-common/file/archive_file_7z.c @@ -59,7 +59,7 @@ struct sevenzip_context_t uint32_t parse_index; uint32_t decompress_index; uint32_t packIndex; - uint32_t block_index; + uint32_t block_index; }; static void *sevenzip_stream_alloc_impl(ISzAllocPtr p, size_t len) @@ -71,15 +71,12 @@ static void *sevenzip_stream_alloc_impl(ISzAllocPtr p, size_t len) static void sevenzip_stream_free_impl(ISzAllocPtr p, void *address) { - (void)p; - if (address) free(address); } static void *sevenzip_stream_alloc_tmp_impl(ISzAllocPtr p, size_t len) { - (void)p; if (len == 0) return 0; return malloc(len); @@ -140,11 +137,11 @@ static int64_t sevenzip_file_read( const char *needle, void **buf, const char *optional_outfile) { + CSzArEx db; CFileInStream archiveStream; CLookToRead2 lookStream; ISzAlloc allocImp; ISzAlloc allocTempImp; - CSzArEx db; uint8_t *output = 0; int64_t outsize = -1; @@ -164,18 +161,16 @@ static int64_t sevenzip_file_read( #if defined(_WIN32) && defined(USE_WINDOWS_FILE) && !defined(LEGACY_WIN32) if (!string_is_empty(path)) { - wchar_t *pathW = utf8_to_utf16_string_alloc(path); - - if (pathW) + wchar_t *path_w = utf8_to_utf16_string_alloc(path); + if (path_w) { /* Could not open 7zip archive? */ - if (InFile_OpenW(&archiveStream.file, pathW)) + if (InFile_OpenW(&archiveStream.file, path_w)) { - free(pathW); + free(path_w); return -1; } - - free(pathW); + free(path_w); } } #else @@ -276,7 +271,7 @@ static int64_t sevenzip_file_read( * copy and free the old one. */ *buf = malloc((size_t)(outsize + 1)); ((char*)(*buf))[outsize] = '\0'; - memcpy(*buf,output + offset,outsize); + memcpy(*buf, output + offset, outsize); } break; } @@ -369,18 +364,18 @@ static int sevenzip_parse_file_init(file_archive_transfer_t *state, #if defined(_WIN32) && defined(USE_WINDOWS_FILE) && !defined(LEGACY_WIN32) if (!string_is_empty(file)) { - wchar_t *fileW = utf8_to_utf16_string_alloc(file); + wchar_t *file_w = utf8_to_utf16_string_alloc(file); - if (fileW) + if (file_w) { /* could not open 7zip archive? */ - if (InFile_OpenW(&sevenzip_context->archiveStream.file, fileW)) + if (InFile_OpenW(&sevenzip_context->archiveStream.file, file_w)) { - free(fileW); + free(file_w); goto error; } - free(fileW); + free(file_w); } } #else @@ -439,21 +434,18 @@ static int sevenzip_parse_file_iterate_step_internal( if ( (_len < PATH_MAX_LENGTH) && !SzArEx_IsDir(&sevenzip_context->db, sevenzip_context->parse_index)) { - char infile[PATH_MAX_LENGTH]; SRes res = SZ_ERROR_FAIL; uint16_t *temp = (uint16_t*)malloc(_len * sizeof(uint16_t)); if (!temp) return -1; - infile[0] = '\0'; - SzArEx_GetFileNameUtf16(&sevenzip_context->db, sevenzip_context->parse_index, temp); if (temp) { - res = utf16_to_char_string(temp, infile, sizeof(infile)) + res = utf16_to_char_string(temp, s, PATH_MAX_LENGTH) ? SZ_OK : SZ_ERROR_FAIL; free(temp); } @@ -461,8 +453,6 @@ static int sevenzip_parse_file_iterate_step_internal( if (res != SZ_OK) return -1; - strlcpy(s, infile, PATH_MAX_LENGTH); - *cmode = 0; /* unused for 7zip */ *checksum = sevenzip_context->db.CRCs.Vals[sevenzip_context->parse_index]; *size = (uint32_t)SzArEx_GetFileSize(&sevenzip_context->db, sevenzip_context->parse_index); diff --git a/libretro-common/file/archive_file_zlib.c b/libretro-common/file/archive_file_zlib.c index 352650dd21..29f6f3880f 100644 --- a/libretro-common/file/archive_file_zlib.c +++ b/libretro-common/file/archive_file_zlib.c @@ -61,15 +61,13 @@ typedef struct uint8_t *decompressed_data; } zip_context_t; -static INLINE uint32_t read_le(const uint8_t *data, unsigned size) +static INLINE uint32_t read_le(const uint8_t *data, size_t len) { unsigned i; uint32_t val = 0; - - size *= 8; - for (i = 0; i < size; i += 8) + len *= 8; + for (i = 0; i < len; i += 8) val |= (uint32_t)*data++ << i; - return val; } @@ -114,9 +112,7 @@ static bool zlib_stream_decompress_data_to_file_init( /* seek past most of the local directory header */ #ifdef HAVE_MMAP if (state->archive_mmap_data) - { local_header = state->archive_mmap_data + (size_t)cdata + 26; - } else #endif { @@ -181,13 +177,11 @@ static int zlib_stream_decompress_data_to_file_iterate( if (zip_context->cmode == ZIP_MODE_STORED) { #ifdef HAVE_MMAP + /* Simply copy the data to the output buffer */ if (zip_context->state->archive_mmap_data) - { - /* Simply copy the data to the output buffer */ memcpy(zip_context->decompressed_data, zip_context->state->archive_mmap_data + (size_t)zip_context->fdoffset, zip_context->usize); - } else #endif { @@ -204,27 +198,26 @@ static int zlib_stream_decompress_data_to_file_iterate( } else if (zip_context->cmode == ZIP_MODE_DEFLATED) { - int to_read = MIN(zip_context->csize - zip_context->boffset, _READ_CHUNK_SIZE); uint8_t *dptr; + int to_read = MIN(zip_context->csize - zip_context->boffset, _READ_CHUNK_SIZE); + /* File was uncompressed or decompression finished before */ if (!zip_context->zstream) - { - /* file was uncompressed or decompression finished before */ return 1; - } #ifdef HAVE_MMAP if (state->archive_mmap_data) { /* Decompress from the mapped file */ dptr = state->archive_mmap_data + (size_t)zip_context->fdoffset + zip_context->boffset; - rd = to_read; + rd = to_read; } else #endif { /* Read some compressed data from file to the temp buffer */ - filestream_seek(state->archive_file, zip_context->fdoffset + zip_context->boffset, - RETRO_VFS_SEEK_POSITION_START); + filestream_seek(state->archive_file, + zip_context->fdoffset + zip_context->boffset, + RETRO_VFS_SEEK_POSITION_START); rd = filestream_read(state->archive_file, zip_context->tmpbuf, to_read); if (rd < 0) return -1; @@ -365,7 +358,6 @@ static int64_t zip_file_read( file_archive_transfer_t state = {0}; decomp_state_t decomp = {0}; struct archive_extract_userdata userdata = {0}; - bool returnerr = true; int ret = 0; if (needle) @@ -380,6 +372,7 @@ static int64_t zip_file_read( do { + bool returnerr = true; ret = file_archive_parse_file_iterate(&state, &returnerr, path, "", zip_file_decompressed, &userdata); if (!returnerr) @@ -522,13 +515,13 @@ static int zip_parse_file_iterate_step(void *context, file_archive_file_cb file_cb) { zip_context_t *zip_context = (zip_context_t *)context; - const uint8_t *cdata = NULL; - uint32_t checksum = 0; - uint32_t size = 0; - uint32_t csize = 0; - unsigned cmode = 0; - unsigned payload = 0; - int ret = zip_parse_file_iterate_step_internal(zip_context, + const uint8_t *cdata = NULL; + uint32_t checksum = 0; + uint32_t size = 0; + uint32_t csize = 0; + unsigned cmode = 0; + unsigned payload = 0; + int ret = zip_parse_file_iterate_step_internal(zip_context, userdata->current_file_path, &cdata, &cmode, &size, &csize, &checksum, &payload); if (ret != 1)