2013-10-08 15:07:03 +08:00
|
|
|
/* Bra86.c -- Converter for x86 code (BCJ)
|
2022-08-29 14:23:38 +08:00
|
|
|
2021-02-09 : Igor Pavlov : Public domain */
|
2013-10-08 15:07:03 +08:00
|
|
|
|
2022-08-29 14:23:38 +08:00
|
|
|
#include "Precomp.h"
|
2013-10-08 15:07:03 +08:00
|
|
|
|
2022-08-29 14:23:38 +08:00
|
|
|
#include "Bra.h"
|
2013-10-08 15:07:03 +08:00
|
|
|
|
2022-08-29 14:23:38 +08:00
|
|
|
#define Test86MSByte(b) ((((b) + 1) & 0xFE) == 0)
|
2013-10-08 15:07:03 +08:00
|
|
|
|
|
|
|
SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding)
|
|
|
|
{
|
2022-08-29 14:23:38 +08:00
|
|
|
SizeT pos = 0;
|
|
|
|
UInt32 mask = *state & 7;
|
2013-10-08 15:07:03 +08:00
|
|
|
if (size < 5)
|
|
|
|
return 0;
|
2022-08-29 14:23:38 +08:00
|
|
|
size -= 4;
|
2013-10-08 15:07:03 +08:00
|
|
|
ip += 5;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
2022-08-29 14:23:38 +08:00
|
|
|
Byte *p = data + pos;
|
|
|
|
const Byte *limit = data + size;
|
2013-10-08 15:07:03 +08:00
|
|
|
for (; p < limit; p++)
|
|
|
|
if ((*p & 0xFE) == 0xE8)
|
|
|
|
break;
|
2022-08-29 14:23:38 +08:00
|
|
|
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
2022-08-29 14:23:38 +08:00
|
|
|
SizeT d = (SizeT)(p - data) - pos;
|
|
|
|
pos = (SizeT)(p - data);
|
|
|
|
if (p >= limit)
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
2022-08-29 14:23:38 +08:00
|
|
|
*state = (d > 2 ? 0 : mask >> (unsigned)d);
|
|
|
|
return pos;
|
|
|
|
}
|
|
|
|
if (d > 2)
|
|
|
|
mask = 0;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
mask >>= (unsigned)d;
|
|
|
|
if (mask != 0 && (mask > 4 || mask == 3 || Test86MSByte(p[(size_t)(mask >> 1) + 1])))
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
2022-08-29 14:23:38 +08:00
|
|
|
mask = (mask >> 1) | 4;
|
|
|
|
pos++;
|
2013-10-08 15:07:03 +08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Test86MSByte(p[4]))
|
|
|
|
{
|
2022-08-29 14:23:38 +08:00
|
|
|
UInt32 v = ((UInt32)p[4] << 24) | ((UInt32)p[3] << 16) | ((UInt32)p[2] << 8) | ((UInt32)p[1]);
|
|
|
|
UInt32 cur = ip + (UInt32)pos;
|
|
|
|
pos += 5;
|
|
|
|
if (encoding)
|
|
|
|
v += cur;
|
|
|
|
else
|
|
|
|
v -= cur;
|
|
|
|
if (mask != 0)
|
2013-10-08 15:07:03 +08:00
|
|
|
{
|
2022-08-29 14:23:38 +08:00
|
|
|
unsigned sh = (mask & 6) << 2;
|
|
|
|
if (Test86MSByte((Byte)(v >> sh)))
|
|
|
|
{
|
|
|
|
v ^= (((UInt32)0x100 << sh) - 1);
|
|
|
|
if (encoding)
|
|
|
|
v += cur;
|
|
|
|
else
|
|
|
|
v -= cur;
|
|
|
|
}
|
|
|
|
mask = 0;
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
2022-08-29 14:23:38 +08:00
|
|
|
p[1] = (Byte)v;
|
|
|
|
p[2] = (Byte)(v >> 8);
|
|
|
|
p[3] = (Byte)(v >> 16);
|
|
|
|
p[4] = (Byte)(0 - ((v >> 24) & 1));
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-08-29 14:23:38 +08:00
|
|
|
mask = (mask >> 1) | 4;
|
|
|
|
pos++;
|
2013-10-08 15:07:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|