]> git.proxmox.com Git - mirror_edk2.git/blob - IntelFrameworkModulePkg/Library/LzmaCustomDecompressLib/Sdk/C/Bra86.c
Add LzmaArchCustomDecompressLib library instance, which supports for X86 (IA32/X64...
[mirror_edk2.git] / IntelFrameworkModulePkg / Library / LzmaCustomDecompressLib / Sdk / C / Bra86.c
1 /* Bra86.c -- Converter for x86 code (BCJ)
2 2008-10-04 : Igor Pavlov : Public domain */
3
4 #include "Bra.h"
5
6 #define Test86MSByte(b) ((b) == 0 || (b) == 0xFF)
7
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};
10
11 SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding)
12 {
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;
19
20 for (;;)
21 {
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
34 {
35 prevMask = (prevMask << ((int)prevPosT - 1)) & 0x7;
36 if (prevMask != 0)
37 {
38 Byte b = p[4 - kMaskToBitNumber[prevMask]];
39 if (!kMaskToAllowedStatus[prevMask] || Test86MSByte(b))
40 {
41 prevPosT = bufferPos;
42 prevMask = ((prevMask << 1) & 0x7) | 1;
43 bufferPos++;
44 continue;
45 }
46 }
47 }
48 prevPosT = bufferPos;
49
50 if (Test86MSByte(p[4]))
51 {
52 UInt32 src = ((UInt32)p[4] << 24) | ((UInt32)p[3] << 16) | ((UInt32)p[2] << 8) | ((UInt32)p[1]);
53 UInt32 dest;
54 for (;;)
55 {
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);
69 }
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;
75 }
76 else
77 {
78 prevMask = ((prevMask << 1) & 0x7) | 1;
79 bufferPos++;
80 }
81 }
82 prevPosT = bufferPos - prevPosT;
83 *state = ((prevPosT > 3) ? 0 : ((prevMask << ((int)prevPosT - 1)) & 0x7));
84 return bufferPos;
85 }