annotate src/win32/7zip/7z/C/Bra86.c @ 2:3549bbe597ed

adding makefile.am
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:47 -0600
parents f9f4f1b99eed
children
rev   line source
rlm@1 1 /* Bra86.c -- Converter for x86 code (BCJ)
rlm@1 2 2008-10-04 : Igor Pavlov : Public domain */
rlm@1 3
rlm@1 4 #include "Bra.h"
rlm@1 5
rlm@1 6 #define Test86MSByte(b) ((b) == 0 || (b) == 0xFF)
rlm@1 7
rlm@1 8 const Byte kMaskToAllowedStatus[8] = {1, 1, 1, 0, 1, 0, 0, 0};
rlm@1 9 const Byte kMaskToBitNumber[8] = {0, 1, 2, 2, 3, 3, 3, 3};
rlm@1 10
rlm@1 11 SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding)
rlm@1 12 {
rlm@1 13 SizeT bufferPos = 0, prevPosT;
rlm@1 14 UInt32 prevMask = *state & 0x7;
rlm@1 15 if (size < 5)
rlm@1 16 return 0;
rlm@1 17 ip += 5;
rlm@1 18 prevPosT = (SizeT)0 - 1;
rlm@1 19
rlm@1 20 for (;;)
rlm@1 21 {
rlm@1 22 Byte *p = data + bufferPos;
rlm@1 23 Byte *limit = data + size - 4;
rlm@1 24 for (; p < limit; p++)
rlm@1 25 if ((*p & 0xFE) == 0xE8)
rlm@1 26 break;
rlm@1 27 bufferPos = (SizeT)(p - data);
rlm@1 28 if (p >= limit)
rlm@1 29 break;
rlm@1 30 prevPosT = bufferPos - prevPosT;
rlm@1 31 if (prevPosT > 3)
rlm@1 32 prevMask = 0;
rlm@1 33 else
rlm@1 34 {
rlm@1 35 prevMask = (prevMask << ((int)prevPosT - 1)) & 0x7;
rlm@1 36 if (prevMask != 0)
rlm@1 37 {
rlm@1 38 Byte b = p[4 - kMaskToBitNumber[prevMask]];
rlm@1 39 if (!kMaskToAllowedStatus[prevMask] || Test86MSByte(b))
rlm@1 40 {
rlm@1 41 prevPosT = bufferPos;
rlm@1 42 prevMask = ((prevMask << 1) & 0x7) | 1;
rlm@1 43 bufferPos++;
rlm@1 44 continue;
rlm@1 45 }
rlm@1 46 }
rlm@1 47 }
rlm@1 48 prevPosT = bufferPos;
rlm@1 49
rlm@1 50 if (Test86MSByte(p[4]))
rlm@1 51 {
rlm@1 52 UInt32 src = ((UInt32)p[4] << 24) | ((UInt32)p[3] << 16) | ((UInt32)p[2] << 8) | ((UInt32)p[1]);
rlm@1 53 UInt32 dest;
rlm@1 54 for (;;)
rlm@1 55 {
rlm@1 56 Byte b;
rlm@1 57 int index;
rlm@1 58 if (encoding)
rlm@1 59 dest = (ip + (UInt32)bufferPos) + src;
rlm@1 60 else
rlm@1 61 dest = src - (ip + (UInt32)bufferPos);
rlm@1 62 if (prevMask == 0)
rlm@1 63 break;
rlm@1 64 index = kMaskToBitNumber[prevMask] * 8;
rlm@1 65 b = (Byte)((dest >> (24 - index)) & 0xFF);
rlm@1 66 if (!Test86MSByte(b))
rlm@1 67 break;
rlm@1 68 src = dest ^ ((1 << (32 - index)) - 1);
rlm@1 69 }
rlm@1 70 p[4] = (Byte)((~(((dest >> 24) & 1) - 1)) & 0xFF);
rlm@1 71 p[3] = (Byte)((dest >> 16) & 0xFF);
rlm@1 72 p[2] = (Byte)((dest >> 8) & 0xFF);
rlm@1 73 p[1] = (Byte)(dest & 0xFF);
rlm@1 74 bufferPos += 5;
rlm@1 75 }
rlm@1 76 else
rlm@1 77 {
rlm@1 78 prevMask = ((prevMask << 1) & 0x7) | 1;
rlm@1 79 bufferPos++;
rlm@1 80 }
rlm@1 81 }
rlm@1 82 prevPosT = bufferPos - prevPosT;
rlm@1 83 *state = ((prevPosT > 3) ? 0 : ((prevMask << ((int)prevPosT - 1)) & 0x7));
rlm@1 84 return bufferPos;
rlm@1 85 }