Mercurial > vba-linux
diff src/win32/7zip/7z/C/Bra86.c @ 1:f9f4f1b99eed
importing src directory
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 03 Mar 2012 10:31:27 -0600 |
parents | |
children |
line wrap: on
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/win32/7zip/7z/C/Bra86.c Sat Mar 03 10:31:27 2012 -0600 1.3 @@ -0,0 +1,85 @@ 1.4 +/* Bra86.c -- Converter for x86 code (BCJ) 1.5 +2008-10-04 : Igor Pavlov : Public domain */ 1.6 + 1.7 +#include "Bra.h" 1.8 + 1.9 +#define Test86MSByte(b) ((b) == 0 || (b) == 0xFF) 1.10 + 1.11 +const Byte kMaskToAllowedStatus[8] = {1, 1, 1, 0, 1, 0, 0, 0}; 1.12 +const Byte kMaskToBitNumber[8] = {0, 1, 2, 2, 3, 3, 3, 3}; 1.13 + 1.14 +SizeT x86_Convert(Byte *data, SizeT size, UInt32 ip, UInt32 *state, int encoding) 1.15 +{ 1.16 + SizeT bufferPos = 0, prevPosT; 1.17 + UInt32 prevMask = *state & 0x7; 1.18 + if (size < 5) 1.19 + return 0; 1.20 + ip += 5; 1.21 + prevPosT = (SizeT)0 - 1; 1.22 + 1.23 + for (;;) 1.24 + { 1.25 + Byte *p = data + bufferPos; 1.26 + Byte *limit = data + size - 4; 1.27 + for (; p < limit; p++) 1.28 + if ((*p & 0xFE) == 0xE8) 1.29 + break; 1.30 + bufferPos = (SizeT)(p - data); 1.31 + if (p >= limit) 1.32 + break; 1.33 + prevPosT = bufferPos - prevPosT; 1.34 + if (prevPosT > 3) 1.35 + prevMask = 0; 1.36 + else 1.37 + { 1.38 + prevMask = (prevMask << ((int)prevPosT - 1)) & 0x7; 1.39 + if (prevMask != 0) 1.40 + { 1.41 + Byte b = p[4 - kMaskToBitNumber[prevMask]]; 1.42 + if (!kMaskToAllowedStatus[prevMask] || Test86MSByte(b)) 1.43 + { 1.44 + prevPosT = bufferPos; 1.45 + prevMask = ((prevMask << 1) & 0x7) | 1; 1.46 + bufferPos++; 1.47 + continue; 1.48 + } 1.49 + } 1.50 + } 1.51 + prevPosT = bufferPos; 1.52 + 1.53 + if (Test86MSByte(p[4])) 1.54 + { 1.55 + UInt32 src = ((UInt32)p[4] << 24) | ((UInt32)p[3] << 16) | ((UInt32)p[2] << 8) | ((UInt32)p[1]); 1.56 + UInt32 dest; 1.57 + for (;;) 1.58 + { 1.59 + Byte b; 1.60 + int index; 1.61 + if (encoding) 1.62 + dest = (ip + (UInt32)bufferPos) + src; 1.63 + else 1.64 + dest = src - (ip + (UInt32)bufferPos); 1.65 + if (prevMask == 0) 1.66 + break; 1.67 + index = kMaskToBitNumber[prevMask] * 8; 1.68 + b = (Byte)((dest >> (24 - index)) & 0xFF); 1.69 + if (!Test86MSByte(b)) 1.70 + break; 1.71 + src = dest ^ ((1 << (32 - index)) - 1); 1.72 + } 1.73 + p[4] = (Byte)((~(((dest >> 24) & 1) - 1)) & 0xFF); 1.74 + p[3] = (Byte)((dest >> 16) & 0xFF); 1.75 + p[2] = (Byte)((dest >> 8) & 0xFF); 1.76 + p[1] = (Byte)(dest & 0xFF); 1.77 + bufferPos += 5; 1.78 + } 1.79 + else 1.80 + { 1.81 + prevMask = ((prevMask << 1) & 0x7) | 1; 1.82 + bufferPos++; 1.83 + } 1.84 + } 1.85 + prevPosT = bufferPos - prevPosT; 1.86 + *state = ((prevPosT > 3) ? 0 : ((prevMask << ((int)prevPosT - 1)) & 0x7)); 1.87 + return bufferPos; 1.88 +}