Team Fortress 2 Source Code as on 22/4/2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

134 lines
3.1 KiB

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