other-licenses/7zstub/src/7zip/Compress/Branch/BranchX86.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/other-licenses/7zstub/src/7zip/Compress/Branch/BranchX86.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,101 @@
     1.4 +/* BranchX86.c */
     1.5 +
     1.6 +#include "BranchX86.h"
     1.7 +
     1.8 +/*
     1.9 +static int inline Test86MSByte(Byte b)
    1.10 +{
    1.11 +  return (b == 0 || b == 0xFF);
    1.12 +}
    1.13 +*/
    1.14 +#define Test86MSByte(b) ((b) == 0 || (b) == 0xFF)
    1.15 +
    1.16 +const int kMaskToAllowedStatus[8] = {1, 1, 1, 0, 1, 0, 0, 0};
    1.17 +const Byte kMaskToBitNumber[8] = {0, 1, 2, 2, 3, 3, 3, 3};
    1.18 +
    1.19 +/*
    1.20 +void x86_Convert_Init(UInt32 *prevMask, UInt32 *prevPos)
    1.21 +{
    1.22 +  *prevMask = 0;
    1.23 +  *prevPos = (UInt32)(-5);
    1.24 +}
    1.25 +*/
    1.26 +
    1.27 +UInt32 x86_Convert(Byte *buffer, UInt32 endPos, UInt32 nowPos, 
    1.28 +    UInt32 *prevMask, UInt32 *prevPos, int encoding)
    1.29 +{
    1.30 +  UInt32 bufferPos = 0;
    1.31 +  UInt32 limit;
    1.32 +
    1.33 +  if (endPos < 5)
    1.34 +    return 0;
    1.35 +  
    1.36 +  if (nowPos - *prevPos > 5)
    1.37 +    *prevPos = nowPos - 5;
    1.38 +  
    1.39 +  limit = endPos - 5;
    1.40 +  while(bufferPos <= limit)
    1.41 +  {
    1.42 +    Byte b = buffer[bufferPos];
    1.43 +    UInt32 offset;
    1.44 +    if (b != 0xE8 && b != 0xE9)
    1.45 +    {
    1.46 +      bufferPos++;
    1.47 +      continue;
    1.48 +    }
    1.49 +    offset = (nowPos + bufferPos - *prevPos);
    1.50 +    *prevPos = (nowPos + bufferPos);
    1.51 +    if (offset > 5)
    1.52 +      *prevMask = 0;
    1.53 +    else
    1.54 +    {
    1.55 +      UInt32 i;
    1.56 +      for (i = 0; i < offset; i++)
    1.57 +      {
    1.58 +        *prevMask &= 0x77;
    1.59 +        *prevMask <<= 1;
    1.60 +      }
    1.61 +    }
    1.62 +    b = buffer[bufferPos + 4];
    1.63 +    if (Test86MSByte(b) && kMaskToAllowedStatus[(*prevMask >> 1) & 0x7] && 
    1.64 +      (*prevMask >> 1) < 0x10)
    1.65 +    {
    1.66 +      UInt32 src = 
    1.67 +        ((UInt32)(b) << 24) |
    1.68 +        ((UInt32)(buffer[bufferPos + 3]) << 16) |
    1.69 +        ((UInt32)(buffer[bufferPos + 2]) << 8) |
    1.70 +        (buffer[bufferPos + 1]);
    1.71 +      
    1.72 +      UInt32 dest;
    1.73 +      while(1)
    1.74 +      {
    1.75 +        UInt32 index;
    1.76 +        if (encoding)
    1.77 +          dest = (nowPos + bufferPos + 5) + src;
    1.78 +        else
    1.79 +          dest = src - (nowPos + bufferPos + 5);
    1.80 +        if (*prevMask == 0)
    1.81 +          break;
    1.82 +        index = kMaskToBitNumber[*prevMask >> 1];
    1.83 +        b = (Byte)(dest >> (24 - index * 8));
    1.84 +        if (!Test86MSByte(b))
    1.85 +          break;
    1.86 +        src = dest ^ ((1 << (32 - index * 8)) - 1);
    1.87 +      }
    1.88 +      buffer[bufferPos + 4] = (Byte)(~(((dest >> 24) & 1) - 1));
    1.89 +      buffer[bufferPos + 3] = (Byte)(dest >> 16);
    1.90 +      buffer[bufferPos + 2] = (Byte)(dest >> 8);
    1.91 +      buffer[bufferPos + 1] = (Byte)dest;
    1.92 +      bufferPos += 5;
    1.93 +      *prevMask = 0;
    1.94 +    }
    1.95 +    else
    1.96 +    {
    1.97 +      bufferPos++;
    1.98 +      *prevMask |= 1;
    1.99 +      if (Test86MSByte(b))
   1.100 +        *prevMask |= 0x10;
   1.101 +    }
   1.102 +  }
   1.103 +  return bufferPos;
   1.104 +}

mercurial