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