From 3d76e33970405d03cdf1f06ae16d951b41be3610 Mon Sep 17 00:00:00 2001 From: twinaphex Date: Tue, 21 Oct 2014 09:15:51 +0200 Subject: [PATCH] (7zip/Bcj2.c) Refactor --- deps/7zip/Bcj2.c | 51 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/deps/7zip/Bcj2.c b/deps/7zip/Bcj2.c index aa0fb6968e..dd703883a7 100755 --- a/deps/7zip/Bcj2.c +++ b/deps/7zip/Bcj2.c @@ -15,15 +15,6 @@ #define kNumMoveBits 5 #define RC_READ_BYTE (*buffer++) -#define RC_TEST { if (buffer == bufferLim) return SZ_ERROR_DATA; } -#define RC_INIT2 code = 0; range = 0xFFFFFFFF; \ -{ int i; for (i = 0; i < 5; i++) { RC_TEST; code = (code << 8) | RC_READ_BYTE; }} - -#define BCJ2_NORMALIZE if (range < kTopValue) { RC_TEST; range <<= 8; code = (code << 8) | RC_READ_BYTE; } - -#define BCJ2_IF_BIT_0(p) ttt = *(p); bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound) -#define BCJ2_UPDATE_0(p) range = bound; *(p) = (uint16_t)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); BCJ2_NORMALIZE; -#define BCJ2_UPDATE_1(p) range -= bound; code -= bound; *(p) = (uint16_t)(ttt - (ttt >> kNumMoveBits)); BCJ2_NORMALIZE; int Bcj2_Decode( const uint8_t *buf0, size_t size0, @@ -45,7 +36,15 @@ int Bcj2_Decode( buffer = buf3; bufferLim = buffer + size3; - RC_INIT2 + code = 0; + range = 0xFFFFFFFF; + + for (i = 0; i < 5; i++) + { + if (buffer == bufferLim) + return SZ_ERROR_DATA; + code = (code << 8) | RC_READ_BYTE; + } if (outSize == 0) return SZ_OK; @@ -83,16 +82,40 @@ int Bcj2_Decode( else prob = p + 257; - BCJ2_IF_BIT_0(prob) + ttt = *(prob); + bound = (range >> kNumBitModelTotalBits) * ttt; + + if (code < bound) { - BCJ2_UPDATE_0(prob) - prevuint8_t = b; + range = bound; + *(prob) = (uint16_t)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); + + if (range < kTopValue) + { + if (buffer == bufferLim) + return SZ_ERROR_DATA; + range <<= 8; + code = (code << 8) | RC_READ_BYTE; + } + prevuint8_t = b; } else { uint32_t dest; const uint8_t *v; - BCJ2_UPDATE_1(prob) + + range -= bound; + code -= bound; + *(prob) = (uint16_t)(ttt - (ttt >> kNumMoveBits)); + + if (range < kTopValue) + { + if (buffer == bufferLim) + return SZ_ERROR_DATA; + range <<= 8; + code = (code << 8) | RC_READ_BYTE; + } + if (b == 0xE8) { v = buf1;