/* Bcj2.c -- Converter for x86 code (BCJ2)
2008-10-04 : Igor Pavlov : Public domain */
#include "crn_core.h"
#include "lzma_Bcj2.h"

namespace crnlib {

#ifdef _LZMA_PROB32
#define CProb UInt32
#else
#define CProb UInt16
#endif

#define IsJcc(b0, b1) ((b0) == 0x0F && ((b1)&0xF0) == 0x80)
#define IsJ(b0, b1) ((b1 & 0xFE) == 0xE8 || IsJcc(b0, b1))

#define kNumTopBits 24
#define kTopValue ((UInt32)1 << kNumTopBits)

#define kNumBitModelTotalBits 11
#define kBitModelTotal (1 << kNumBitModelTotalBits)
#define kNumMoveBits 5

#define RC_READ_BYTE (*buffer++)
#define RC_TEST              \
  {                          \
    if (buffer == bufferLim) \
      return SZ_ERROR_DATA;  \
  }
#define RC_INIT2                         \
  code = 0;                              \
  range = 0xFFFFFFFF;                    \
  {                                      \
    int i;                               \
    for (i = 0; i < 5; i++) {            \
      RC_TEST;                           \
      code = (code << 8) | RC_READ_BYTE; \
    }                                    \
  }

#define NORMALIZE                      \
  if (range < kTopValue) {             \
    RC_TEST;                           \
    range <<= 8;                       \
    code = (code << 8) | RC_READ_BYTE; \
  }

#define IF_BIT_0(p)                               \
  ttt = *(p);                                     \
  bound = (range >> kNumBitModelTotalBits) * ttt; \
  if (code < bound)
#define UPDATE_0(p)                                               \
  range = bound;                                                  \
  *(p) = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); \
  NORMALIZE;
#define UPDATE_1(p)                            \
  range -= bound;                              \
  code -= bound;                               \
  *(p) = (CProb)(ttt - (ttt >> kNumMoveBits)); \
  NORMALIZE;

int Bcj2_Decode(
    const Byte* buf0, SizeT size0,
    const Byte* buf1, SizeT size1,
    const Byte* buf2, SizeT size2,
    const Byte* buf3, SizeT size3,
    Byte* outBuf, SizeT outSize) {
  CProb p[256 + 2];
  SizeT inPos = 0, outPos = 0;

  const Byte *buffer, *bufferLim;
  UInt32 range, code;
  Byte prevByte = 0;

  unsigned int i;
  for (i = 0; i < sizeof(p) / sizeof(p[0]); i++)
    p[i] = kBitModelTotal >> 1;

  buffer = buf3;
  bufferLim = buffer + size3;
  RC_INIT2

  if (outSize == 0)
    return SZ_OK;

  for (;;) {
    Byte b;
    CProb* prob;
    UInt32 bound;
    UInt32 ttt;

    SizeT limit = size0 - inPos;
    if (outSize - outPos < limit)
      limit = outSize - outPos;
    while (limit != 0) {
      Byte b = buf0[inPos];
      outBuf[outPos++] = b;
      if (IsJ(prevByte, b))
        break;
      inPos++;
      prevByte = b;
      limit--;
    }

    if (limit == 0 || outPos == outSize)
      break;

    b = buf0[inPos++];

    if (b == 0xE8)
      prob = p + prevByte;
    else if (b == 0xE9)
      prob = p + 256;
    else
      prob = p + 257;

    IF_BIT_0(prob) {
      UPDATE_0(prob)
      prevByte = b;
    }
    else {
      UInt32 dest;
      const Byte* v;
      UPDATE_1(prob)
      if (b == 0xE8) {
        v = buf1;
        if (size1 < 4)
          return SZ_ERROR_DATA;
        buf1 += 4;
        size1 -= 4;
      } else {
        v = buf2;
        if (size2 < 4)
          return SZ_ERROR_DATA;
        buf2 += 4;
        size2 -= 4;
      }
      dest = (((UInt32)v[0] << 24) | ((UInt32)v[1] << 16) |
              ((UInt32)v[2] << 8) | ((UInt32)v[3])) -
             ((UInt32)outPos + 4);
      outBuf[outPos++] = (Byte)dest;
      if (outPos == outSize)
        break;
      outBuf[outPos++] = (Byte)(dest >> 8);
      if (outPos == outSize)
        break;
      outBuf[outPos++] = (Byte)(dest >> 16);
      if (outPos == outSize)
        break;
      outBuf[outPos++] = prevByte = (Byte)(dest >> 24);
    }
  }
  return (outPos == outSize) ? SZ_OK : SZ_ERROR_DATA;
}
}
