Bcj2.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /* Bcj2.c -- Converter for x86 code (BCJ2)
  2. 2008-10-04 : Igor Pavlov : Public domain */
  3. #include "Bcj2.h"
  4. #ifdef _LZMA_PROB32
  5. #define CProb UInt32
  6. #else
  7. #define CProb UInt16
  8. #endif
  9. #define IsJcc(b0, b1) ((b0) == 0x0F && ((b1) & 0xF0) == 0x80)
  10. #define IsJ(b0, b1) ((b1 & 0xFE) == 0xE8 || IsJcc(b0, b1))
  11. #define kNumTopBits 24
  12. #define kTopValue ((UInt32)1 << kNumTopBits)
  13. #define kNumBitModelTotalBits 11
  14. #define kBitModelTotal (1 << kNumBitModelTotalBits)
  15. #define kNumMoveBits 5
  16. #define RC_READ_BYTE (*buffer++)
  17. #define RC_TEST { if (buffer == bufferLim) return SZ_ERROR_DATA; }
  18. #define RC_INIT2 code = 0; range = 0xFFFFFFFF; \
  19. { int i; for (i = 0; i < 5; i++) { RC_TEST; code = (code << 8) | RC_READ_BYTE; }}
  20. #define NORMALIZE if (range < kTopValue) { RC_TEST; range <<= 8; code = (code << 8) | RC_READ_BYTE; }
  21. #define IF_BIT_0(p) ttt = *(p); bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
  22. #define UPDATE_0(p) range = bound; *(p) = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits)); NORMALIZE;
  23. #define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CProb)(ttt - (ttt >> kNumMoveBits)); NORMALIZE;
  24. int Bcj2_Decode(
  25. const Byte *buf0, SizeT size0,
  26. const Byte *buf1, SizeT size1,
  27. const Byte *buf2, SizeT size2,
  28. const Byte *buf3, SizeT size3,
  29. Byte *outBuf, SizeT outSize)
  30. {
  31. CProb p[256 + 2];
  32. SizeT inPos = 0, outPos = 0;
  33. const Byte *buffer, *bufferLim;
  34. UInt32 range, code;
  35. Byte prevByte = 0;
  36. unsigned int i;
  37. for (i = 0; i < sizeof(p) / sizeof(p[0]); i++)
  38. p[i] = kBitModelTotal >> 1;
  39. buffer = buf3;
  40. bufferLim = buffer + size3;
  41. RC_INIT2
  42. if (outSize == 0)
  43. return SZ_OK;
  44. for (;;)
  45. {
  46. Byte b;
  47. CProb *prob;
  48. UInt32 bound;
  49. UInt32 ttt;
  50. SizeT limit = size0 - inPos;
  51. if (outSize - outPos < limit)
  52. limit = outSize - outPos;
  53. while (limit != 0)
  54. {
  55. Byte b = buf0[inPos];
  56. outBuf[outPos++] = b;
  57. if (IsJ(prevByte, b))
  58. break;
  59. inPos++;
  60. prevByte = b;
  61. limit--;
  62. }
  63. if (limit == 0 || outPos == outSize)
  64. break;
  65. b = buf0[inPos++];
  66. if (b == 0xE8)
  67. prob = p + prevByte;
  68. else if (b == 0xE9)
  69. prob = p + 256;
  70. else
  71. prob = p + 257;
  72. IF_BIT_0(prob)
  73. {
  74. UPDATE_0(prob)
  75. prevByte = b;
  76. }
  77. else
  78. {
  79. UInt32 dest;
  80. const Byte *v;
  81. UPDATE_1(prob)
  82. if (b == 0xE8)
  83. {
  84. v = buf1;
  85. if (size1 < 4)
  86. return SZ_ERROR_DATA;
  87. buf1 += 4;
  88. size1 -= 4;
  89. }
  90. else
  91. {
  92. v = buf2;
  93. if (size2 < 4)
  94. return SZ_ERROR_DATA;
  95. buf2 += 4;
  96. size2 -= 4;
  97. }
  98. dest = (((UInt32)v[0] << 24) | ((UInt32)v[1] << 16) |
  99. ((UInt32)v[2] << 8) | ((UInt32)v[3])) - ((UInt32)outPos + 4);
  100. outBuf[outPos++] = (Byte)dest;
  101. if (outPos == outSize)
  102. break;
  103. outBuf[outPos++] = (Byte)(dest >> 8);
  104. if (outPos == outSize)
  105. break;
  106. outBuf[outPos++] = (Byte)(dest >> 16);
  107. if (outPos == outSize)
  108. break;
  109. outBuf[outPos++] = prevByte = (Byte)(dest >> 24);
  110. }
  111. }
  112. return (outPos == outSize) ? SZ_OK : SZ_ERROR_DATA;
  113. }