Cleanup setting of 'sysroot' in common.gypi
[chromium-blink-merge.git] / third_party / lzma_sdk / Bra86.c
blob1ee0e709b88771b7f00662fa77fad886a1a72343
1 /* Bra86.c -- Converter for x86 code (BCJ)
2 2008-10-04 : Igor Pavlov : Public domain */
4 #include "Bra.h"
6 #define Test86MSByte(b) ((b) == 0 || (b) == 0xFF)
8 const Byte kMaskToAllowedStatus[8] = {1, 1, 1, 0, 1, 0, 0, 0};
9 const Byte kMaskToBitNumber[8] = {0, 1, 2, 2, 3, 3, 3, 3};
11 SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding)
13 SizeT bufferPos = 0, prevPosT;
14 UInt32 prevMask = *state & 0x7;
15 if (size < 5)
16 return 0;
17 ip += 5;
18 prevPosT = (SizeT)0 - 1;
20 for (;;)
22 Byte *p = data + bufferPos;
23 Byte *limit = data + size - 4;
24 for (; p < limit; p++)
25 if ((*p & 0xFE) == 0xE8)
26 break;
27 bufferPos = (SizeT)(p - data);
28 if (p >= limit)
29 break;
30 prevPosT = bufferPos - prevPosT;
31 if (prevPosT > 3)
32 prevMask = 0;
33 else
35 prevMask = (prevMask << ((int)prevPosT - 1)) & 0x7;
36 if (prevMask != 0)
38 Byte b = p[4 - kMaskToBitNumber[prevMask]];
39 if (!kMaskToAllowedStatus[prevMask] || Test86MSByte(b))
41 prevPosT = bufferPos;
42 prevMask = ((prevMask << 1) & 0x7) | 1;
43 bufferPos++;
44 continue;
48 prevPosT = bufferPos;
50 if (Test86MSByte(p[4]))
52 UInt32 src = ((UInt32)p[4] << 24) | ((UInt32)p[3] << 16) | ((UInt32)p[2] << 8) | ((UInt32)p[1]);
53 UInt32 dest;
54 for (;;)
56 Byte b;
57 int index;
58 if (encoding)
59 dest = (ip + (UInt32)bufferPos) + src;
60 else
61 dest = src - (ip + (UInt32)bufferPos);
62 if (prevMask == 0)
63 break;
64 index = kMaskToBitNumber[prevMask] * 8;
65 b = (Byte)(dest >> (24 - index));
66 if (!Test86MSByte(b))
67 break;
68 src = dest ^ ((1 << (32 - index)) - 1);
70 p[4] = (Byte)(~(((dest >> 24) & 1) - 1));
71 p[3] = (Byte)(dest >> 16);
72 p[2] = (Byte)(dest >> 8);
73 p[1] = (Byte)dest;
74 bufferPos += 5;
76 else
78 prevMask = ((prevMask << 1) & 0x7) | 1;
79 bufferPos++;
82 prevPosT = bufferPos - prevPosT;
83 *state = ((prevPosT > 3) ? 0 : ((prevMask << ((int)prevPosT - 1)) & 0x7));
84 return bufferPos;