OLD | NEW |
1 /* BranchX86.c */ | 1 /* Bra86.c -- Converter for x86 code (BCJ) |
| 2 2008-10-04 : Igor Pavlov : Public domain */ |
2 | 3 |
3 #include "BranchX86.h" | 4 #include "Bra.h" |
4 | 5 |
5 #define Test86MSByte(b) ((b) == 0 || (b) == 0xFF) | 6 #define Test86MSByte(b) ((b) == 0 || (b) == 0xFF) |
6 | 7 |
7 const Byte kMaskToAllowedStatus[8] = {1, 1, 1, 0, 1, 0, 0, 0}; | 8 const Byte kMaskToAllowedStatus[8] = {1, 1, 1, 0, 1, 0, 0, 0}; |
8 const Byte kMaskToBitNumber[8] = {0, 1, 2, 2, 3, 3, 3, 3}; | 9 const Byte kMaskToBitNumber[8] = {0, 1, 2, 2, 3, 3, 3, 3}; |
9 | 10 |
10 SizeT x86_Convert(Byte *buffer, SizeT endPos, UInt32 nowPos, UInt32 *prevMaskMix
, int encoding) | 11 SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding
) |
11 { | 12 { |
12 SizeT bufferPos = 0, prevPosT; | 13 SizeT bufferPos = 0, prevPosT; |
13 UInt32 prevMask = *prevMaskMix & 0x7; | 14 UInt32 prevMask = *state & 0x7; |
14 if (endPos < 5) | 15 if (size < 5) |
15 return 0; | 16 return 0; |
16 nowPos += 5; | 17 ip += 5; |
17 prevPosT = (SizeT)0 - 1; | 18 prevPosT = (SizeT)0 - 1; |
18 | 19 |
19 for(;;) | 20 for (;;) |
20 { | 21 { |
21 Byte *p = buffer + bufferPos; | 22 Byte *p = data + bufferPos; |
22 Byte *limit = buffer + endPos - 4; | 23 Byte *limit = data + size - 4; |
23 for (; p < limit; p++) | 24 for (; p < limit; p++) |
24 if ((*p & 0xFE) == 0xE8) | 25 if ((*p & 0xFE) == 0xE8) |
25 break; | 26 break; |
26 bufferPos = (SizeT)(p - buffer); | 27 bufferPos = (SizeT)(p - data); |
27 if (p >= limit) | 28 if (p >= limit) |
28 break; | 29 break; |
29 prevPosT = bufferPos - prevPosT; | 30 prevPosT = bufferPos - prevPosT; |
30 if (prevPosT > 3) | 31 if (prevPosT > 3) |
31 prevMask = 0; | 32 prevMask = 0; |
32 else | 33 else |
33 { | 34 { |
34 prevMask = (prevMask << ((int)prevPosT - 1)) & 0x7; | 35 prevMask = (prevMask << ((int)prevPosT - 1)) & 0x7; |
35 if (prevMask != 0) | 36 if (prevMask != 0) |
36 { | 37 { |
(...skipping 11 matching lines...) Expand all Loading... |
48 | 49 |
49 if (Test86MSByte(p[4])) | 50 if (Test86MSByte(p[4])) |
50 { | 51 { |
51 UInt32 src = ((UInt32)p[4] << 24) | ((UInt32)p[3] << 16) | ((UInt32)p[2] <
< 8) | ((UInt32)p[1]); | 52 UInt32 src = ((UInt32)p[4] << 24) | ((UInt32)p[3] << 16) | ((UInt32)p[2] <
< 8) | ((UInt32)p[1]); |
52 UInt32 dest; | 53 UInt32 dest; |
53 for (;;) | 54 for (;;) |
54 { | 55 { |
55 Byte b; | 56 Byte b; |
56 int index; | 57 int index; |
57 if (encoding) | 58 if (encoding) |
58 dest = (nowPos + (UInt32)bufferPos) + src; | 59 dest = (ip + (UInt32)bufferPos) + src; |
59 else | 60 else |
60 dest = src - (nowPos + (UInt32)bufferPos); | 61 dest = src - (ip + (UInt32)bufferPos); |
61 if (prevMask == 0) | 62 if (prevMask == 0) |
62 break; | 63 break; |
63 index = kMaskToBitNumber[prevMask] * 8; | 64 index = kMaskToBitNumber[prevMask] * 8; |
64 b = (Byte)(dest >> (24 - index)); | 65 b = (Byte)(dest >> (24 - index)); |
65 if (!Test86MSByte(b)) | 66 if (!Test86MSByte(b)) |
66 break; | 67 break; |
67 src = dest ^ ((1 << (32 - index)) - 1); | 68 src = dest ^ ((1 << (32 - index)) - 1); |
68 } | 69 } |
69 p[4] = (Byte)(~(((dest >> 24) & 1) - 1)); | 70 p[4] = (Byte)(~(((dest >> 24) & 1) - 1)); |
70 p[3] = (Byte)(dest >> 16); | 71 p[3] = (Byte)(dest >> 16); |
71 p[2] = (Byte)(dest >> 8); | 72 p[2] = (Byte)(dest >> 8); |
72 p[1] = (Byte)dest; | 73 p[1] = (Byte)dest; |
73 bufferPos += 5; | 74 bufferPos += 5; |
74 } | 75 } |
75 else | 76 else |
76 { | 77 { |
77 prevMask = ((prevMask << 1) & 0x7) | 1; | 78 prevMask = ((prevMask << 1) & 0x7) | 1; |
78 bufferPos++; | 79 bufferPos++; |
79 } | 80 } |
80 } | 81 } |
81 prevPosT = bufferPos - prevPosT; | 82 prevPosT = bufferPos - prevPosT; |
82 *prevMaskMix = ((prevPosT > 3) ? 0 : ((prevMask << ((int)prevPosT - 1)) & 0x7)
); | 83 *state = ((prevPosT > 3) ? 0 : ((prevMask << ((int)prevPosT - 1)) & 0x7)); |
83 return bufferPos; | 84 return bufferPos; |
84 } | 85 } |
OLD | NEW |