1
/* Bcj2.c -- Converter for x86 code (BCJ2)
2
2008-10-04 : Igor Pavlov : Public domain */
12
#define IsJcc(b0, b1) ((b0) == 0x0F && ((b1) & 0xF0) == 0x80)
13
#define IsJ(b0, b1) ((b1 & 0xFE) == 0xE8 || IsJcc(b0, b1))
15
#define kNumTopBits 24
16
#define kTopValue ((UInt32)1 << kNumTopBits)
18
#define kNumBitModelTotalBits 11
19
#define kBitModelTotal (1 << kNumBitModelTotalBits)
20
#define kNumMoveBits 5
22
#define RC_READ_BYTE (*buffer++)
23
#define RC_TEST { if (buffer == bufferLim) return SZ_ERROR_DATA; }
24
#define RC_INIT2 code = 0; range = 0xFFFFFFFF; \
25
{ int i; for (i = 0; i < 5; i++) { RC_TEST; code = (code << 8) | RC_READ_BYTE; }}
27
#define NORMALIZE if (range < kTopValue) { RC_TEST; range <<= 8; code = (code << 8) | RC_READ_BYTE; }
29
#define IF_BIT_0(p) ttt = *(p); bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
30
#define UPDATE_0(p) range = bound; *(p) = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); NORMALIZE;
31
#define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CProb)(ttt - (ttt >> kNumMoveBits)); NORMALIZE;
34
const Byte *buf0, SizeT size0,
35
const Byte *buf1, SizeT size1,
36
const Byte *buf2, SizeT size2,
37
const Byte *buf3, SizeT size3,
38
Byte *outBuf, SizeT outSize)
41
SizeT inPos = 0, outPos = 0;
43
const Byte *buffer, *bufferLim;
48
for (i = 0; i < sizeof(p) / sizeof(p[0]); i++)
49
p[i] = kBitModelTotal >> 1;
52
bufferLim = buffer + size3;
65
SizeT limit = size0 - inPos;
66
if (outSize - outPos < limit)
67
limit = outSize - outPos;
79
if (limit == 0 || outPos == outSize)
105
return SZ_ERROR_DATA;
113
return SZ_ERROR_DATA;
117
dest = (((UInt32)v[0] << 24) | ((UInt32)v[1] << 16) |
118
((UInt32)v[2] << 8) | ((UInt32)v[3])) - ((UInt32)outPos + 4);
119
outBuf[outPos++] = (Byte)dest;
120
if (outPos == outSize)
122
outBuf[outPos++] = (Byte)(dest >> 8);
123
if (outPos == outSize)
125
outBuf[outPos++] = (Byte)(dest >> 16);
126
if (outPos == outSize)
128
outBuf[outPos++] = prevByte = (Byte)(dest >> 24);
131
return (outPos == outSize) ? SZ_OK : SZ_ERROR_DATA;