1 /* Bra86.c -- Converter for x86 code (BCJ)
\r
2 2021-02-09 : Igor Pavlov : Public domain */
\r
8 #define Test86MSByte(b) ((((b) + 1) & 0xFE) == 0)
\r
10 SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding)
\r
13 UInt32 mask = *state & 7;
\r
21 Byte *p = data + pos;
\r
22 const Byte *limit = data + size;
\r
23 for (; p < limit; p++)
\r
24 if ((*p & 0xFE) == 0xE8)
\r
28 SizeT d = (SizeT)(p - data) - pos;
\r
29 pos = (SizeT)(p - data);
\r
32 *state = (d > 2 ? 0 : mask >> (unsigned)d);
\r
39 mask >>= (unsigned)d;
\r
40 if (mask != 0 && (mask > 4 || mask == 3 || Test86MSByte(p[(size_t)(mask >> 1) + 1])))
\r
42 mask = (mask >> 1) | 4;
\r
49 if (Test86MSByte(p[4]))
\r
51 UInt32 v = ((UInt32)p[4] << 24) | ((UInt32)p[3] << 16) | ((UInt32)p[2] << 8) | ((UInt32)p[1]);
\r
52 UInt32 cur = ip + (UInt32)pos;
\r
60 unsigned sh = (mask & 6) << 2;
\r
61 if (Test86MSByte((Byte)(v >> sh)))
\r
63 v ^= (((UInt32)0x100 << sh) - 1);
\r
72 p[2] = (Byte)(v >> 8);
\r
73 p[3] = (Byte)(v >> 16);
\r
74 p[4] = (Byte)(0 - ((v >> 24) & 1));
\r
78 mask = (mask >> 1) | 4;
\r