From 88804a05de87bb52785ea291cf15c684f551b176 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Wed, 1 Jul 2020 19:12:20 +0200 Subject: [PATCH] (libchdr) Cleanups + attempts to rebase against upstream --- libretro-common/formats/libchdr/libchdr_huffman.c | 12 +++++------- libretro-common/formats/libchdr/libchdr_lzma.c | 2 +- libretro-common/formats/libchdr/libchdr_zlib.c | 10 +++++++--- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/libretro-common/formats/libchdr/libchdr_huffman.c b/libretro-common/formats/libchdr/libchdr_huffman.c index bf54b59aeb..910a6f1f06 100644 --- a/libretro-common/formats/libchdr/libchdr_huffman.c +++ b/libretro-common/formats/libchdr/libchdr_huffman.c @@ -335,7 +335,8 @@ enum huffman_error huffman_compute_tree_from_histo(struct huffman_decoder* decod /* binary search to achieve the optimum encoding */ upperweight = sdatacount * 2; - while (1) + + for (;;) { /* build a tree using the current weight */ uint32_t curweight = (upperweight + lowerweight) / 2; @@ -548,16 +549,13 @@ void huffman_build_lookup_table(struct huffman_decoder* decoder) struct node_t* node = &decoder->huffnode[curcode]; if (node->numbits > 0) { - int shift; - lookup_value *dest; - lookup_value *destend; /* set up the entry */ lookup_value value = MAKE_LOOKUP(curcode, node->numbits); /* fill all matching entries */ - shift = decoder->maxbits - node->numbits; - dest = &decoder->lookup[node->bits << shift]; - destend = &decoder->lookup[((node->bits + 1) << shift) - 1]; + int shift = decoder->maxbits - node->numbits; + lookup_value *dest = &decoder->lookup[node->bits << shift]; + lookup_value *destend = &decoder->lookup[((node->bits + 1) << shift) - 1]; while (dest <= destend) *dest++ = value; } diff --git a/libretro-common/formats/libchdr/libchdr_lzma.c b/libretro-common/formats/libchdr/libchdr_lzma.c index 71602cc5b9..c947b16298 100644 --- a/libretro-common/formats/libchdr/libchdr_lzma.c +++ b/libretro-common/formats/libchdr/libchdr_lzma.c @@ -103,7 +103,7 @@ static void *lzma_fast_alloc(void *p, size_t size) /* set the low bit of the size so we don't match next time */ *addr = (uint32_t)(size | 1); - return addr + 1; + return addr + (sizeof(uint32_t) == sizeof(uintptr_t) ? 1 : 2); } /*------------------------------------------------- diff --git a/libretro-common/formats/libchdr/libchdr_zlib.c b/libretro-common/formats/libchdr/libchdr_zlib.c index 6aaae8d009..6e4a5dea7d 100644 --- a/libretro-common/formats/libchdr/libchdr_zlib.c +++ b/libretro-common/formats/libchdr/libchdr_zlib.c @@ -174,6 +174,10 @@ chd_error zlib_codec_init(void *codec, uint32_t hunkbytes) else err = CHDERR_NONE; + /* handle an error */ + if (err != CHDERR_NONE) + free(data); + return err; } @@ -225,7 +229,7 @@ chd_error zlib_codec_decompress(void *codec, const uint8_t *src, uint32_t comple /* do it */ zerr = inflate(&data->inflater, Z_FINISH); - (void)zerr; + (void)zerr; if (data->inflater.total_out != destlen) return CHDERR_DECOMPRESSION_ERROR; @@ -273,7 +277,7 @@ voidpf zlib_fast_alloc(voidpf opaque, uInt items, uInt size) /* set the low bit of the size so we don't match next time */ *ptr = size | 1; - return ptr + 1; + return ptr + (sizeof(uint32_t) == sizeof(uintptr_t) ? 1 : 2); } /*------------------------------------------------- @@ -284,7 +288,7 @@ voidpf zlib_fast_alloc(voidpf opaque, uInt items, uInt size) void zlib_fast_free(voidpf opaque, voidpf address) { zlib_allocator *alloc = (zlib_allocator *)opaque; - UINT32 *ptr = (UINT32 *)address - 1; + UINT32 *ptr = (UINT32 *)address - (sizeof(uint32_t) == sizeof(uintptr_t) ? 1 : 2); int i; /* find the hunk */