123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257 |
- /* Bcj2.c -- BCJ2 Decoder (Converter for x86 code)
- 2018-04-28 : Igor Pavlov : Public domain */
- #include "Precomp.h"
- #include "Bcj2.h"
- #include "CpuArch.h"
- #define CProb UInt16
- #define kTopValue ((UInt32)1 << 24)
- #define kNumModelBits 11
- #define kBitModelTotal (1 << kNumModelBits)
- #define kNumMoveBits 5
- #define _IF_BIT_0 ttt = *prob; bound = (p->range >> kNumModelBits) * ttt; if (p->code < bound)
- #define _UPDATE_0 p->range = bound; *prob = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
- #define _UPDATE_1 p->range -= bound; p->code -= bound; *prob = (CProb)(ttt - (ttt >> kNumMoveBits));
- void Bcj2Dec_Init(CBcj2Dec *p)
- {
- unsigned i;
- p->state = BCJ2_DEC_STATE_OK;
- p->ip = 0;
- p->temp[3] = 0;
- p->range = 0;
- p->code = 0;
- for (i = 0; i < sizeof(p->probs) / sizeof(p->probs[0]); i++)
- p->probs[i] = kBitModelTotal >> 1;
- }
- SRes Bcj2Dec_Decode(CBcj2Dec *p)
- {
- if (p->range <= 5)
- {
- p->state = BCJ2_DEC_STATE_OK;
- for (; p->range != 5; p->range++)
- {
- if (p->range == 1 && p->code != 0)
- return SZ_ERROR_DATA;
-
- if (p->bufs[BCJ2_STREAM_RC] == p->lims[BCJ2_STREAM_RC])
- {
- p->state = BCJ2_STREAM_RC;
- return SZ_OK;
- }
- p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++;
- }
-
- if (p->code == 0xFFFFFFFF)
- return SZ_ERROR_DATA;
-
- p->range = 0xFFFFFFFF;
- }
- else if (p->state >= BCJ2_DEC_STATE_ORIG_0)
- {
- while (p->state <= BCJ2_DEC_STATE_ORIG_3)
- {
- Byte *dest = p->dest;
- if (dest == p->destLim)
- return SZ_OK;
- *dest = p->temp[(size_t)p->state - BCJ2_DEC_STATE_ORIG_0];
- p->state++;
- p->dest = dest + 1;
- }
- }
- /*
- if (BCJ2_IS_32BIT_STREAM(p->state))
- {
- const Byte *cur = p->bufs[p->state];
- if (cur == p->lims[p->state])
- return SZ_OK;
- p->bufs[p->state] = cur + 4;
-
- {
- UInt32 val;
- Byte *dest;
- SizeT rem;
-
- p->ip += 4;
- val = GetBe32(cur) - p->ip;
- dest = p->dest;
- rem = p->destLim - dest;
- if (rem < 4)
- {
- SizeT i;
- SetUi32(p->temp, val);
- for (i = 0; i < rem; i++)
- dest[i] = p->temp[i];
- p->dest = dest + rem;
- p->state = BCJ2_DEC_STATE_ORIG_0 + (unsigned)rem;
- return SZ_OK;
- }
- SetUi32(dest, val);
- p->temp[3] = (Byte)(val >> 24);
- p->dest = dest + 4;
- p->state = BCJ2_DEC_STATE_OK;
- }
- }
- */
- for (;;)
- {
- if (BCJ2_IS_32BIT_STREAM(p->state))
- p->state = BCJ2_DEC_STATE_OK;
- else
- {
- if (p->range < kTopValue)
- {
- if (p->bufs[BCJ2_STREAM_RC] == p->lims[BCJ2_STREAM_RC])
- {
- p->state = BCJ2_STREAM_RC;
- return SZ_OK;
- }
- p->range <<= 8;
- p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++;
- }
- {
- const Byte *src = p->bufs[BCJ2_STREAM_MAIN];
- const Byte *srcLim;
- Byte *dest;
- SizeT num = p->lims[BCJ2_STREAM_MAIN] - src;
-
- if (num == 0)
- {
- p->state = BCJ2_STREAM_MAIN;
- return SZ_OK;
- }
-
- dest = p->dest;
- if (num > (SizeT)(p->destLim - dest))
- {
- num = p->destLim - dest;
- if (num == 0)
- {
- p->state = BCJ2_DEC_STATE_ORIG;
- return SZ_OK;
- }
- }
-
- srcLim = src + num;
- if (p->temp[3] == 0x0F && (src[0] & 0xF0) == 0x80)
- *dest = src[0];
- else for (;;)
- {
- Byte b = *src;
- *dest = b;
- if (b != 0x0F)
- {
- if ((b & 0xFE) == 0xE8)
- break;
- dest++;
- if (++src != srcLim)
- continue;
- break;
- }
- dest++;
- if (++src == srcLim)
- break;
- if ((*src & 0xF0) != 0x80)
- continue;
- *dest = *src;
- break;
- }
-
- num = src - p->bufs[BCJ2_STREAM_MAIN];
-
- if (src == srcLim)
- {
- p->temp[3] = src[-1];
- p->bufs[BCJ2_STREAM_MAIN] = src;
- p->ip += (UInt32)num;
- p->dest += num;
- p->state =
- p->bufs[BCJ2_STREAM_MAIN] ==
- p->lims[BCJ2_STREAM_MAIN] ?
- (unsigned)BCJ2_STREAM_MAIN :
- (unsigned)BCJ2_DEC_STATE_ORIG;
- return SZ_OK;
- }
-
- {
- UInt32 bound, ttt;
- CProb *prob;
- Byte b = src[0];
- Byte prev = (Byte)(num == 0 ? p->temp[3] : src[-1]);
-
- p->temp[3] = b;
- p->bufs[BCJ2_STREAM_MAIN] = src + 1;
- num++;
- p->ip += (UInt32)num;
- p->dest += num;
-
- prob = p->probs + (unsigned)(b == 0xE8 ? 2 + (unsigned)prev : (b == 0xE9 ? 1 : 0));
-
- _IF_BIT_0
- {
- _UPDATE_0
- continue;
- }
- _UPDATE_1
-
- }
- }
- }
- {
- UInt32 val;
- unsigned cj = (p->temp[3] == 0xE8) ? BCJ2_STREAM_CALL : BCJ2_STREAM_JUMP;
- const Byte *cur = p->bufs[cj];
- Byte *dest;
- SizeT rem;
-
- if (cur == p->lims[cj])
- {
- p->state = cj;
- break;
- }
-
- val = GetBe32(cur);
- p->bufs[cj] = cur + 4;
- p->ip += 4;
- val -= p->ip;
- dest = p->dest;
- rem = p->destLim - dest;
-
- if (rem < 4)
- {
- p->temp[0] = (Byte)val; if (rem > 0) dest[0] = (Byte)val; val >>= 8;
- p->temp[1] = (Byte)val; if (rem > 1) dest[1] = (Byte)val; val >>= 8;
- p->temp[2] = (Byte)val; if (rem > 2) dest[2] = (Byte)val; val >>= 8;
- p->temp[3] = (Byte)val;
- p->dest = dest + rem;
- p->state = BCJ2_DEC_STATE_ORIG_0 + (unsigned)rem;
- break;
- }
-
- SetUi32(dest, val);
- p->temp[3] = (Byte)(val >> 24);
- p->dest = dest + 4;
- }
- }
- if (p->range < kTopValue && p->bufs[BCJ2_STREAM_RC] != p->lims[BCJ2_STREAM_RC])
- {
- p->range <<= 8;
- p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++;
- }
- return SZ_OK;
- }
|