Bcj2.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /* Bcj2.c -- BCJ2 Decoder (Converter for x86 code)
  2. 2018-04-28 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. #include "Bcj2.h"
  5. #include "CpuArch.h"
  6. #define CProb UInt16
  7. #define kTopValue ((UInt32)1 << 24)
  8. #define kNumModelBits 11
  9. #define kBitModelTotal (1 << kNumModelBits)
  10. #define kNumMoveBits 5
  11. #define _IF_BIT_0 ttt = *prob; bound = (p->range >> kNumModelBits) * ttt; if (p->code < bound)
  12. #define _UPDATE_0 p->range = bound; *prob = (CProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
  13. #define _UPDATE_1 p->range -= bound; p->code -= bound; *prob = (CProb)(ttt - (ttt >> kNumMoveBits));
  14. void Bcj2Dec_Init(CBcj2Dec *p)
  15. {
  16. unsigned i;
  17. p->state = BCJ2_DEC_STATE_OK;
  18. p->ip = 0;
  19. p->temp[3] = 0;
  20. p->range = 0;
  21. p->code = 0;
  22. for (i = 0; i < sizeof(p->probs) / sizeof(p->probs[0]); i++)
  23. p->probs[i] = kBitModelTotal >> 1;
  24. }
  25. SRes Bcj2Dec_Decode(CBcj2Dec *p)
  26. {
  27. if (p->range <= 5)
  28. {
  29. p->state = BCJ2_DEC_STATE_OK;
  30. for (; p->range != 5; p->range++)
  31. {
  32. if (p->range == 1 && p->code != 0)
  33. return SZ_ERROR_DATA;
  34. if (p->bufs[BCJ2_STREAM_RC] == p->lims[BCJ2_STREAM_RC])
  35. {
  36. p->state = BCJ2_STREAM_RC;
  37. return SZ_OK;
  38. }
  39. p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++;
  40. }
  41. if (p->code == 0xFFFFFFFF)
  42. return SZ_ERROR_DATA;
  43. p->range = 0xFFFFFFFF;
  44. }
  45. else if (p->state >= BCJ2_DEC_STATE_ORIG_0)
  46. {
  47. while (p->state <= BCJ2_DEC_STATE_ORIG_3)
  48. {
  49. Byte *dest = p->dest;
  50. if (dest == p->destLim)
  51. return SZ_OK;
  52. *dest = p->temp[(size_t)p->state - BCJ2_DEC_STATE_ORIG_0];
  53. p->state++;
  54. p->dest = dest + 1;
  55. }
  56. }
  57. /*
  58. if (BCJ2_IS_32BIT_STREAM(p->state))
  59. {
  60. const Byte *cur = p->bufs[p->state];
  61. if (cur == p->lims[p->state])
  62. return SZ_OK;
  63. p->bufs[p->state] = cur + 4;
  64. {
  65. UInt32 val;
  66. Byte *dest;
  67. SizeT rem;
  68. p->ip += 4;
  69. val = GetBe32(cur) - p->ip;
  70. dest = p->dest;
  71. rem = p->destLim - dest;
  72. if (rem < 4)
  73. {
  74. SizeT i;
  75. SetUi32(p->temp, val);
  76. for (i = 0; i < rem; i++)
  77. dest[i] = p->temp[i];
  78. p->dest = dest + rem;
  79. p->state = BCJ2_DEC_STATE_ORIG_0 + (unsigned)rem;
  80. return SZ_OK;
  81. }
  82. SetUi32(dest, val);
  83. p->temp[3] = (Byte)(val >> 24);
  84. p->dest = dest + 4;
  85. p->state = BCJ2_DEC_STATE_OK;
  86. }
  87. }
  88. */
  89. for (;;)
  90. {
  91. if (BCJ2_IS_32BIT_STREAM(p->state))
  92. p->state = BCJ2_DEC_STATE_OK;
  93. else
  94. {
  95. if (p->range < kTopValue)
  96. {
  97. if (p->bufs[BCJ2_STREAM_RC] == p->lims[BCJ2_STREAM_RC])
  98. {
  99. p->state = BCJ2_STREAM_RC;
  100. return SZ_OK;
  101. }
  102. p->range <<= 8;
  103. p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++;
  104. }
  105. {
  106. const Byte *src = p->bufs[BCJ2_STREAM_MAIN];
  107. const Byte *srcLim;
  108. Byte *dest;
  109. SizeT num = p->lims[BCJ2_STREAM_MAIN] - src;
  110. if (num == 0)
  111. {
  112. p->state = BCJ2_STREAM_MAIN;
  113. return SZ_OK;
  114. }
  115. dest = p->dest;
  116. if (num > (SizeT)(p->destLim - dest))
  117. {
  118. num = p->destLim - dest;
  119. if (num == 0)
  120. {
  121. p->state = BCJ2_DEC_STATE_ORIG;
  122. return SZ_OK;
  123. }
  124. }
  125. srcLim = src + num;
  126. if (p->temp[3] == 0x0F && (src[0] & 0xF0) == 0x80)
  127. *dest = src[0];
  128. else for (;;)
  129. {
  130. Byte b = *src;
  131. *dest = b;
  132. if (b != 0x0F)
  133. {
  134. if ((b & 0xFE) == 0xE8)
  135. break;
  136. dest++;
  137. if (++src != srcLim)
  138. continue;
  139. break;
  140. }
  141. dest++;
  142. if (++src == srcLim)
  143. break;
  144. if ((*src & 0xF0) != 0x80)
  145. continue;
  146. *dest = *src;
  147. break;
  148. }
  149. num = src - p->bufs[BCJ2_STREAM_MAIN];
  150. if (src == srcLim)
  151. {
  152. p->temp[3] = src[-1];
  153. p->bufs[BCJ2_STREAM_MAIN] = src;
  154. p->ip += (UInt32)num;
  155. p->dest += num;
  156. p->state =
  157. p->bufs[BCJ2_STREAM_MAIN] ==
  158. p->lims[BCJ2_STREAM_MAIN] ?
  159. (unsigned)BCJ2_STREAM_MAIN :
  160. (unsigned)BCJ2_DEC_STATE_ORIG;
  161. return SZ_OK;
  162. }
  163. {
  164. UInt32 bound, ttt;
  165. CProb *prob;
  166. Byte b = src[0];
  167. Byte prev = (Byte)(num == 0 ? p->temp[3] : src[-1]);
  168. p->temp[3] = b;
  169. p->bufs[BCJ2_STREAM_MAIN] = src + 1;
  170. num++;
  171. p->ip += (UInt32)num;
  172. p->dest += num;
  173. prob = p->probs + (unsigned)(b == 0xE8 ? 2 + (unsigned)prev : (b == 0xE9 ? 1 : 0));
  174. _IF_BIT_0
  175. {
  176. _UPDATE_0
  177. continue;
  178. }
  179. _UPDATE_1
  180. }
  181. }
  182. }
  183. {
  184. UInt32 val;
  185. unsigned cj = (p->temp[3] == 0xE8) ? BCJ2_STREAM_CALL : BCJ2_STREAM_JUMP;
  186. const Byte *cur = p->bufs[cj];
  187. Byte *dest;
  188. SizeT rem;
  189. if (cur == p->lims[cj])
  190. {
  191. p->state = cj;
  192. break;
  193. }
  194. val = GetBe32(cur);
  195. p->bufs[cj] = cur + 4;
  196. p->ip += 4;
  197. val -= p->ip;
  198. dest = p->dest;
  199. rem = p->destLim - dest;
  200. if (rem < 4)
  201. {
  202. p->temp[0] = (Byte)val; if (rem > 0) dest[0] = (Byte)val; val >>= 8;
  203. p->temp[1] = (Byte)val; if (rem > 1) dest[1] = (Byte)val; val >>= 8;
  204. p->temp[2] = (Byte)val; if (rem > 2) dest[2] = (Byte)val; val >>= 8;
  205. p->temp[3] = (Byte)val;
  206. p->dest = dest + rem;
  207. p->state = BCJ2_DEC_STATE_ORIG_0 + (unsigned)rem;
  208. break;
  209. }
  210. SetUi32(dest, val);
  211. p->temp[3] = (Byte)(val >> 24);
  212. p->dest = dest + 4;
  213. }
  214. }
  215. if (p->range < kTopValue && p->bufs[BCJ2_STREAM_RC] != p->lims[BCJ2_STREAM_RC])
  216. {
  217. p->range <<= 8;
  218. p->code = (p->code << 8) | *(p->bufs[BCJ2_STREAM_RC])++;
  219. }
  220. return SZ_OK;
  221. }