misc/libphysfs/lzma/C/Compress/Branch/BranchX86_2.c
changeset 12213 bb5522e88ab2
equal deleted inserted replaced
12212:ea891871f481 12213:bb5522e88ab2
       
     1 // BranchX86_2.c
       
     2 
       
     3 #include "BranchX86_2.h"
       
     4 
       
     5 #include "../../Alloc.h"
       
     6 
       
     7 #ifdef _LZMA_PROB32
       
     8 #define CProb UInt32
       
     9 #else
       
    10 #define CProb UInt16
       
    11 #endif
       
    12 
       
    13 #define IsJcc(b0, b1) ((b0) == 0x0F && ((b1) & 0xF0) == 0x80)
       
    14 #define IsJ(b0, b1) ((b1 & 0xFE) == 0xE8 || IsJcc(b0, b1))
       
    15 
       
    16 #define kNumTopBits 24
       
    17 #define kTopValue ((UInt32)1 << kNumTopBits)
       
    18 
       
    19 #define kNumBitModelTotalBits 11
       
    20 #define kBitModelTotal (1 << kNumBitModelTotalBits)
       
    21 #define kNumMoveBits 5
       
    22 
       
    23 #define RC_READ_BYTE (*Buffer++)
       
    24 
       
    25 #define RC_INIT2 Code = 0; Range = 0xFFFFFFFF; \
       
    26   { int i; for(i = 0; i < 5; i++) { RC_TEST; Code = (Code << 8) | RC_READ_BYTE; }}
       
    27 
       
    28 #define RC_TEST { if (Buffer == BufferLim) return BCJ2_RESULT_DATA_ERROR; }
       
    29 
       
    30 #define RC_INIT(buffer, bufferSize) Buffer = buffer; BufferLim = buffer + bufferSize; RC_INIT2
       
    31  
       
    32 #define RC_NORMALIZE if (Range < kTopValue) { RC_TEST; Range <<= 8; Code = (Code << 8) | RC_READ_BYTE; }
       
    33 
       
    34 #define IfBit0(p) RC_NORMALIZE; bound = (Range >> kNumBitModelTotalBits) * *(p); if (Code < bound)
       
    35 #define UpdateBit0(p) Range = bound; *(p) += (kBitModelTotal - *(p)) >> kNumMoveBits;
       
    36 #define UpdateBit1(p) Range -= bound; Code -= bound; *(p) -= (*(p)) >> kNumMoveBits;
       
    37 // #define UpdateBit0(p) Range = bound; *(p) = (CProb)(*(p) + ((kBitModelTotal - *(p)) >> kNumMoveBits));
       
    38 // #define UpdateBit1(p) Range -= bound; Code -= bound; *(p) = (CProb)(*(p) - (*(p) >> kNumMoveBits));
       
    39 
       
    40 int x86_2_Decode(
       
    41     const Byte *buf0, SizeT size0, 
       
    42     const Byte *buf1, SizeT size1, 
       
    43     const Byte *buf2, SizeT size2, 
       
    44     const Byte *buf3, SizeT size3, 
       
    45     Byte *outBuf, SizeT outSize)
       
    46 {
       
    47   CProb p[256 + 2];
       
    48   SizeT inPos = 0, outPos = 0;
       
    49 
       
    50   const Byte *Buffer, *BufferLim;
       
    51   UInt32 Range, Code;
       
    52   Byte prevByte = 0;
       
    53 
       
    54   unsigned int i;
       
    55   for (i = 0; i < sizeof(p) / sizeof(p[0]); i++)
       
    56     p[i] = kBitModelTotal >> 1; 
       
    57   RC_INIT(buf3, size3);
       
    58 
       
    59   if (outSize == 0)
       
    60     return BCJ2_RESULT_OK;
       
    61 
       
    62   for (;;)
       
    63   {
       
    64     Byte b;
       
    65     CProb *prob;
       
    66     UInt32 bound;
       
    67 
       
    68     SizeT limit = size0 - inPos;
       
    69     if (outSize - outPos < limit)
       
    70       limit = outSize - outPos;
       
    71     while (limit != 0)
       
    72     {
       
    73       Byte b = buf0[inPos];
       
    74       outBuf[outPos++] = b;
       
    75       if (IsJ(prevByte, b))
       
    76         break;
       
    77       inPos++;
       
    78       prevByte = b;
       
    79       limit--;
       
    80     }
       
    81 
       
    82     if (limit == 0 || outPos == outSize)
       
    83       break;
       
    84 
       
    85     b = buf0[inPos++];
       
    86 
       
    87     if (b == 0xE8)
       
    88       prob = p + prevByte;
       
    89     else if (b == 0xE9)
       
    90       prob = p + 256;
       
    91     else
       
    92       prob = p + 257;
       
    93 
       
    94     IfBit0(prob)
       
    95     {
       
    96       UpdateBit0(prob)
       
    97       prevByte = b;
       
    98     }
       
    99     else
       
   100     {
       
   101       UInt32 dest;
       
   102       const Byte *v;
       
   103       UpdateBit1(prob)
       
   104       if (b == 0xE8)
       
   105       {
       
   106         v = buf1;
       
   107         if (size1 < 4)
       
   108           return BCJ2_RESULT_DATA_ERROR;
       
   109         buf1 += 4;
       
   110         size1 -= 4;
       
   111       }
       
   112       else
       
   113       {
       
   114         v = buf2;
       
   115         if (size2 < 4)
       
   116           return BCJ2_RESULT_DATA_ERROR;
       
   117         buf2 += 4;
       
   118         size2 -= 4;
       
   119       }
       
   120       dest = (((UInt32)v[0] << 24) | ((UInt32)v[1] << 16) | 
       
   121           ((UInt32)v[2] << 8) | ((UInt32)v[3])) - ((UInt32)outPos + 4);
       
   122       outBuf[outPos++] = (Byte)dest;
       
   123       if (outPos == outSize)
       
   124         break;
       
   125       outBuf[outPos++] = (Byte)(dest >> 8);
       
   126       if (outPos == outSize)
       
   127         break;
       
   128       outBuf[outPos++] = (Byte)(dest >> 16);
       
   129       if (outPos == outSize)
       
   130         break;
       
   131       outBuf[outPos++] = prevByte = (Byte)(dest >> 24);
       
   132     }
       
   133   }
       
   134   return (outPos == outSize) ? BCJ2_RESULT_OK : BCJ2_RESULT_DATA_ERROR;
       
   135 }