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.

1025 lines
27 KiB

  1. /* LzmaDec.c -- LZMA Decoder
  2. 2015-01-01 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. #include "LzmaDec.h"
  5. #include <string.h>
  6. #define kNumTopBits 24
  7. #define kTopValue ((UInt32)1 << kNumTopBits)
  8. #define kNumBitModelTotalBits 11
  9. #define kBitModelTotal (1 << kNumBitModelTotalBits)
  10. #define kNumMoveBits 5
  11. #define RC_INIT_SIZE 5
  12. #define NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | (*buf++); }
  13. #define IF_BIT_0(p) ttt = *(p); NORMALIZE; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
  14. #define UPDATE_0(p) range = bound; *(p) = (CLzmaProb)(ttt + ((kBitModelTotal - ttt) >> kNumMoveBits));
  15. #define UPDATE_1(p) range -= bound; code -= bound; *(p) = (CLzmaProb)(ttt - (ttt >> kNumMoveBits));
  16. #define GET_BIT2(p, i, A0, A1) IF_BIT_0(p) \
  17. { UPDATE_0(p); i = (i + i); A0; } else \
  18. { UPDATE_1(p); i = (i + i) + 1; A1; }
  19. #define GET_BIT(p, i) GET_BIT2(p, i, ; , ;)
  20. #define TREE_GET_BIT(probs, i) { GET_BIT((probs + i), i); }
  21. #define TREE_DECODE(probs, limit, i) \
  22. { i = 1; do { TREE_GET_BIT(probs, i); } while (i < limit); i -= limit; }
  23. /* #define _LZMA_SIZE_OPT */
  24. #ifdef _LZMA_SIZE_OPT
  25. #define TREE_6_DECODE(probs, i) TREE_DECODE(probs, (1 << 6), i)
  26. #else
  27. #define TREE_6_DECODE(probs, i) \
  28. { i = 1; \
  29. TREE_GET_BIT(probs, i); \
  30. TREE_GET_BIT(probs, i); \
  31. TREE_GET_BIT(probs, i); \
  32. TREE_GET_BIT(probs, i); \
  33. TREE_GET_BIT(probs, i); \
  34. TREE_GET_BIT(probs, i); \
  35. i -= 0x40; }
  36. #endif
  37. #define NORMAL_LITER_DEC GET_BIT(prob + symbol, symbol)
  38. #define MATCHED_LITER_DEC \
  39. matchByte <<= 1; \
  40. bit = (matchByte & offs); \
  41. probLit = prob + offs + bit + symbol; \
  42. GET_BIT2(probLit, symbol, offs &= ~bit, offs &= bit)
  43. #define NORMALIZE_CHECK if (range < kTopValue) { if (buf >= bufLimit) return DUMMY_ERROR; range <<= 8; code = (code << 8) | (*buf++); }
  44. #define IF_BIT_0_CHECK(p) ttt = *(p); NORMALIZE_CHECK; bound = (range >> kNumBitModelTotalBits) * ttt; if (code < bound)
  45. #define UPDATE_0_CHECK range = bound;
  46. #define UPDATE_1_CHECK range -= bound; code -= bound;
  47. #define GET_BIT2_CHECK(p, i, A0, A1) IF_BIT_0_CHECK(p) \
  48. { UPDATE_0_CHECK; i = (i + i); A0; } else \
  49. { UPDATE_1_CHECK; i = (i + i) + 1; A1; }
  50. #define GET_BIT_CHECK(p, i) GET_BIT2_CHECK(p, i, ; , ;)
  51. #define TREE_DECODE_CHECK(probs, limit, i) \
  52. { i = 1; do { GET_BIT_CHECK(probs + i, i) } while (i < limit); i -= limit; }
  53. #define kNumPosBitsMax 4
  54. #define kNumPosStatesMax (1 << kNumPosBitsMax)
  55. #define kLenNumLowBits 3
  56. #define kLenNumLowSymbols (1 << kLenNumLowBits)
  57. #define kLenNumMidBits 3
  58. #define kLenNumMidSymbols (1 << kLenNumMidBits)
  59. #define kLenNumHighBits 8
  60. #define kLenNumHighSymbols (1 << kLenNumHighBits)
  61. #define LenChoice 0
  62. #define LenChoice2 (LenChoice + 1)
  63. #define LenLow (LenChoice2 + 1)
  64. #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
  65. #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
  66. #define kNumLenProbs (LenHigh + kLenNumHighSymbols)
  67. #define kNumStates 12
  68. #define kNumLitStates 7
  69. #define kStartPosModelIndex 4
  70. #define kEndPosModelIndex 14
  71. #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
  72. #define kNumPosSlotBits 6
  73. #define kNumLenToPosStates 4
  74. #define kNumAlignBits 4
  75. #define kAlignTableSize (1 << kNumAlignBits)
  76. #define kMatchMinLen 2
  77. #define kMatchSpecLenStart (kMatchMinLen + kLenNumLowSymbols + kLenNumMidSymbols + kLenNumHighSymbols)
  78. #define IsMatch 0
  79. #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
  80. #define IsRepG0 (IsRep + kNumStates)
  81. #define IsRepG1 (IsRepG0 + kNumStates)
  82. #define IsRepG2 (IsRepG1 + kNumStates)
  83. #define IsRep0Long (IsRepG2 + kNumStates)
  84. #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
  85. #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
  86. #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
  87. #define LenCoder (Align + kAlignTableSize)
  88. #define RepLenCoder (LenCoder + kNumLenProbs)
  89. #define Literal (RepLenCoder + kNumLenProbs)
  90. #define LZMA_BASE_SIZE 1846
  91. #define LZMA_LIT_SIZE 768
  92. #define LzmaProps_GetNumProbs(p) ((UInt32)LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((p)->lc + (p)->lp)))
  93. #if Literal != LZMA_BASE_SIZE
  94. StopCompilingDueBUG
  95. #endif
  96. #define LZMA_DIC_MIN (1 << 12)
  97. /* First LZMA-symbol is always decoded.
  98. And it decodes new LZMA-symbols while (buf < bufLimit), but "buf" is without last normalization
  99. Out:
  100. Result:
  101. SZ_OK - OK
  102. SZ_ERROR_DATA - Error
  103. p->remainLen:
  104. < kMatchSpecLenStart : normal remain
  105. = kMatchSpecLenStart : finished
  106. = kMatchSpecLenStart + 1 : Flush marker
  107. = kMatchSpecLenStart + 2 : State Init Marker
  108. */
  109. static int MY_FAST_CALL LzmaDec_DecodeReal(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
  110. {
  111. CLzmaProb *probs = p->probs;
  112. unsigned state = p->state;
  113. UInt32 rep0 = p->reps[0], rep1 = p->reps[1], rep2 = p->reps[2], rep3 = p->reps[3];
  114. unsigned pbMask = ((unsigned)1 << (p->prop.pb)) - 1;
  115. unsigned lpMask = ((unsigned)1 << (p->prop.lp)) - 1;
  116. unsigned lc = p->prop.lc;
  117. Byte *dic = p->dic;
  118. SizeT dicBufSize = p->dicBufSize;
  119. SizeT dicPos = p->dicPos;
  120. UInt32 processedPos = p->processedPos;
  121. UInt32 checkDicSize = p->checkDicSize;
  122. unsigned len = 0;
  123. const Byte *buf = p->buf;
  124. UInt32 range = p->range;
  125. UInt32 code = p->code;
  126. do
  127. {
  128. CLzmaProb *prob;
  129. UInt32 bound;
  130. unsigned ttt;
  131. unsigned posState = processedPos & pbMask;
  132. prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
  133. IF_BIT_0(prob)
  134. {
  135. unsigned symbol;
  136. UPDATE_0(prob);
  137. prob = probs + Literal;
  138. if (checkDicSize != 0 || processedPos != 0)
  139. prob += (LZMA_LIT_SIZE * (((processedPos & lpMask) << lc) +
  140. (dic[(dicPos == 0 ? dicBufSize : dicPos) - 1] >> (8 - lc))));
  141. if (state < kNumLitStates)
  142. {
  143. state -= (state < 4) ? state : 3;
  144. symbol = 1;
  145. #ifdef _LZMA_SIZE_OPT
  146. do { NORMAL_LITER_DEC } while (symbol < 0x100);
  147. #else
  148. NORMAL_LITER_DEC
  149. NORMAL_LITER_DEC
  150. NORMAL_LITER_DEC
  151. NORMAL_LITER_DEC
  152. NORMAL_LITER_DEC
  153. NORMAL_LITER_DEC
  154. NORMAL_LITER_DEC
  155. NORMAL_LITER_DEC
  156. #endif
  157. }
  158. else
  159. {
  160. unsigned matchByte = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
  161. unsigned offs = 0x100;
  162. state -= (state < 10) ? 3 : 6;
  163. symbol = 1;
  164. #ifdef _LZMA_SIZE_OPT
  165. do
  166. {
  167. unsigned bit;
  168. CLzmaProb *probLit;
  169. MATCHED_LITER_DEC
  170. }
  171. while (symbol < 0x100);
  172. #else
  173. {
  174. unsigned bit;
  175. CLzmaProb *probLit;
  176. MATCHED_LITER_DEC
  177. MATCHED_LITER_DEC
  178. MATCHED_LITER_DEC
  179. MATCHED_LITER_DEC
  180. MATCHED_LITER_DEC
  181. MATCHED_LITER_DEC
  182. MATCHED_LITER_DEC
  183. MATCHED_LITER_DEC
  184. }
  185. #endif
  186. }
  187. dic[dicPos++] = (Byte)symbol;
  188. processedPos++;
  189. continue;
  190. }
  191. else
  192. {
  193. UPDATE_1(prob);
  194. prob = probs + IsRep + state;
  195. IF_BIT_0(prob)
  196. {
  197. UPDATE_0(prob);
  198. state += kNumStates;
  199. prob = probs + LenCoder;
  200. }
  201. else
  202. {
  203. UPDATE_1(prob);
  204. if (checkDicSize == 0 && processedPos == 0)
  205. return SZ_ERROR_DATA;
  206. prob = probs + IsRepG0 + state;
  207. IF_BIT_0(prob)
  208. {
  209. UPDATE_0(prob);
  210. prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
  211. IF_BIT_0(prob)
  212. {
  213. UPDATE_0(prob);
  214. dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
  215. dicPos++;
  216. processedPos++;
  217. state = state < kNumLitStates ? 9 : 11;
  218. continue;
  219. }
  220. UPDATE_1(prob);
  221. }
  222. else
  223. {
  224. UInt32 distance;
  225. UPDATE_1(prob);
  226. prob = probs + IsRepG1 + state;
  227. IF_BIT_0(prob)
  228. {
  229. UPDATE_0(prob);
  230. distance = rep1;
  231. }
  232. else
  233. {
  234. UPDATE_1(prob);
  235. prob = probs + IsRepG2 + state;
  236. IF_BIT_0(prob)
  237. {
  238. UPDATE_0(prob);
  239. distance = rep2;
  240. }
  241. else
  242. {
  243. UPDATE_1(prob);
  244. distance = rep3;
  245. rep3 = rep2;
  246. }
  247. rep2 = rep1;
  248. }
  249. rep1 = rep0;
  250. rep0 = distance;
  251. }
  252. state = state < kNumLitStates ? 8 : 11;
  253. prob = probs + RepLenCoder;
  254. }
  255. {
  256. unsigned limit2, offset;
  257. CLzmaProb *probLen = prob + LenChoice;
  258. IF_BIT_0(probLen)
  259. {
  260. UPDATE_0(probLen);
  261. probLen = prob + LenLow + (posState << kLenNumLowBits);
  262. offset = 0;
  263. limit2 = (1 << kLenNumLowBits);
  264. }
  265. else
  266. {
  267. UPDATE_1(probLen);
  268. probLen = prob + LenChoice2;
  269. IF_BIT_0(probLen)
  270. {
  271. UPDATE_0(probLen);
  272. probLen = prob + LenMid + (posState << kLenNumMidBits);
  273. offset = kLenNumLowSymbols;
  274. limit2 = (1 << kLenNumMidBits);
  275. }
  276. else
  277. {
  278. UPDATE_1(probLen);
  279. probLen = prob + LenHigh;
  280. offset = kLenNumLowSymbols + kLenNumMidSymbols;
  281. limit2 = (1 << kLenNumHighBits);
  282. }
  283. }
  284. TREE_DECODE(probLen, limit2, len);
  285. len += offset;
  286. }
  287. if (state >= kNumStates)
  288. {
  289. UInt32 distance;
  290. prob = probs + PosSlot +
  291. ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
  292. TREE_6_DECODE(prob, distance);
  293. if (distance >= kStartPosModelIndex)
  294. {
  295. unsigned posSlot = (unsigned)distance;
  296. int numDirectBits = (int)(((distance >> 1) - 1));
  297. distance = (2 | (distance & 1));
  298. if (posSlot < kEndPosModelIndex)
  299. {
  300. distance <<= numDirectBits;
  301. prob = probs + SpecPos + distance - posSlot - 1;
  302. {
  303. UInt32 mask = 1;
  304. unsigned i = 1;
  305. do
  306. {
  307. GET_BIT2(prob + i, i, ; , distance |= mask);
  308. mask <<= 1;
  309. }
  310. while (--numDirectBits != 0);
  311. }
  312. }
  313. else
  314. {
  315. numDirectBits -= kNumAlignBits;
  316. do
  317. {
  318. NORMALIZE
  319. range >>= 1;
  320. {
  321. UInt32 t;
  322. code -= range;
  323. t = (0 - ((UInt32)code >> 31)); /* (UInt32)((Int32)code >> 31) */
  324. distance = (distance << 1) + (t + 1);
  325. code += range & t;
  326. }
  327. /*
  328. distance <<= 1;
  329. if (code >= range)
  330. {
  331. code -= range;
  332. distance |= 1;
  333. }
  334. */
  335. }
  336. while (--numDirectBits != 0);
  337. prob = probs + Align;
  338. distance <<= kNumAlignBits;
  339. {
  340. unsigned i = 1;
  341. GET_BIT2(prob + i, i, ; , distance |= 1);
  342. GET_BIT2(prob + i, i, ; , distance |= 2);
  343. GET_BIT2(prob + i, i, ; , distance |= 4);
  344. GET_BIT2(prob + i, i, ; , distance |= 8);
  345. }
  346. if (distance == (UInt32)0xFFFFFFFF)
  347. {
  348. len += kMatchSpecLenStart;
  349. state -= kNumStates;
  350. break;
  351. }
  352. }
  353. }
  354. rep3 = rep2;
  355. rep2 = rep1;
  356. rep1 = rep0;
  357. rep0 = distance + 1;
  358. if (checkDicSize == 0)
  359. {
  360. if (distance >= processedPos)
  361. return SZ_ERROR_DATA;
  362. }
  363. else if (distance >= checkDicSize)
  364. return SZ_ERROR_DATA;
  365. state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3;
  366. }
  367. len += kMatchMinLen;
  368. if (limit == dicPos)
  369. return SZ_ERROR_DATA;
  370. {
  371. SizeT rem = limit - dicPos;
  372. unsigned curLen = ((rem < len) ? (unsigned)rem : len);
  373. SizeT pos = (dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0);
  374. processedPos += curLen;
  375. len -= curLen;
  376. if (pos + curLen <= dicBufSize)
  377. {
  378. Byte *dest = dic + dicPos;
  379. ptrdiff_t src = (ptrdiff_t)pos - (ptrdiff_t)dicPos;
  380. const Byte *lim = dest + curLen;
  381. dicPos += curLen;
  382. do
  383. *(dest) = (Byte)*(dest + src);
  384. while (++dest != lim);
  385. }
  386. else
  387. {
  388. do
  389. {
  390. dic[dicPos++] = dic[pos];
  391. if (++pos == dicBufSize)
  392. pos = 0;
  393. }
  394. while (--curLen != 0);
  395. }
  396. }
  397. }
  398. }
  399. while (dicPos < limit && buf < bufLimit);
  400. NORMALIZE;
  401. p->buf = buf;
  402. p->range = range;
  403. p->code = code;
  404. p->remainLen = len;
  405. p->dicPos = dicPos;
  406. p->processedPos = processedPos;
  407. p->reps[0] = rep0;
  408. p->reps[1] = rep1;
  409. p->reps[2] = rep2;
  410. p->reps[3] = rep3;
  411. p->state = state;
  412. return SZ_OK;
  413. }
  414. static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)
  415. {
  416. if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart)
  417. {
  418. Byte *dic = p->dic;
  419. SizeT dicPos = p->dicPos;
  420. SizeT dicBufSize = p->dicBufSize;
  421. unsigned len = p->remainLen;
  422. UInt32 rep0 = p->reps[0];
  423. if (limit - dicPos < len)
  424. len = (unsigned)(limit - dicPos);
  425. if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)
  426. p->checkDicSize = p->prop.dicSize;
  427. p->processedPos += len;
  428. p->remainLen -= len;
  429. while (len != 0)
  430. {
  431. len--;
  432. dic[dicPos] = dic[(dicPos - rep0) + ((dicPos < rep0) ? dicBufSize : 0)];
  433. dicPos++;
  434. }
  435. p->dicPos = dicPos;
  436. }
  437. }
  438. static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
  439. {
  440. do
  441. {
  442. SizeT limit2 = limit;
  443. if (p->checkDicSize == 0)
  444. {
  445. UInt32 rem = p->prop.dicSize - p->processedPos;
  446. if (limit - p->dicPos > rem)
  447. limit2 = p->dicPos + rem;
  448. }
  449. RINOK(LzmaDec_DecodeReal(p, limit2, bufLimit));
  450. if (p->processedPos >= p->prop.dicSize)
  451. p->checkDicSize = p->prop.dicSize;
  452. LzmaDec_WriteRem(p, limit);
  453. }
  454. while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);
  455. if (p->remainLen > kMatchSpecLenStart)
  456. {
  457. p->remainLen = kMatchSpecLenStart;
  458. }
  459. return 0;
  460. }
  461. typedef enum
  462. {
  463. DUMMY_ERROR, /* unexpected end of input stream */
  464. DUMMY_LIT,
  465. DUMMY_MATCH,
  466. DUMMY_REP
  467. } ELzmaDummy;
  468. static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize)
  469. {
  470. UInt32 range = p->range;
  471. UInt32 code = p->code;
  472. const Byte *bufLimit = buf + inSize;
  473. CLzmaProb *probs = p->probs;
  474. unsigned state = p->state;
  475. ELzmaDummy res;
  476. {
  477. CLzmaProb *prob;
  478. UInt32 bound;
  479. unsigned ttt;
  480. unsigned posState = (p->processedPos) & ((1 << p->prop.pb) - 1);
  481. prob = probs + IsMatch + (state << kNumPosBitsMax) + posState;
  482. IF_BIT_0_CHECK(prob)
  483. {
  484. UPDATE_0_CHECK
  485. /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
  486. prob = probs + Literal;
  487. if (p->checkDicSize != 0 || p->processedPos != 0)
  488. prob += (LZMA_LIT_SIZE *
  489. ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) +
  490. (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc))));
  491. if (state < kNumLitStates)
  492. {
  493. unsigned symbol = 1;
  494. do { GET_BIT_CHECK(prob + symbol, symbol) } while (symbol < 0x100);
  495. }
  496. else
  497. {
  498. unsigned matchByte = p->dic[p->dicPos - p->reps[0] +
  499. ((p->dicPos < p->reps[0]) ? p->dicBufSize : 0)];
  500. unsigned offs = 0x100;
  501. unsigned symbol = 1;
  502. do
  503. {
  504. unsigned bit;
  505. CLzmaProb *probLit;
  506. matchByte <<= 1;
  507. bit = (matchByte & offs);
  508. probLit = prob + offs + bit + symbol;
  509. GET_BIT2_CHECK(probLit, symbol, offs &= ~bit, offs &= bit)
  510. }
  511. while (symbol < 0x100);
  512. }
  513. res = DUMMY_LIT;
  514. }
  515. else
  516. {
  517. unsigned len;
  518. UPDATE_1_CHECK;
  519. prob = probs + IsRep + state;
  520. IF_BIT_0_CHECK(prob)
  521. {
  522. UPDATE_0_CHECK;
  523. state = 0;
  524. prob = probs + LenCoder;
  525. res = DUMMY_MATCH;
  526. }
  527. else
  528. {
  529. UPDATE_1_CHECK;
  530. res = DUMMY_REP;
  531. prob = probs + IsRepG0 + state;
  532. IF_BIT_0_CHECK(prob)
  533. {
  534. UPDATE_0_CHECK;
  535. prob = probs + IsRep0Long + (state << kNumPosBitsMax) + posState;
  536. IF_BIT_0_CHECK(prob)
  537. {
  538. UPDATE_0_CHECK;
  539. NORMALIZE_CHECK;
  540. return DUMMY_REP;
  541. }
  542. else
  543. {
  544. UPDATE_1_CHECK;
  545. }
  546. }
  547. else
  548. {
  549. UPDATE_1_CHECK;
  550. prob = probs + IsRepG1 + state;
  551. IF_BIT_0_CHECK(prob)
  552. {
  553. UPDATE_0_CHECK;
  554. }
  555. else
  556. {
  557. UPDATE_1_CHECK;
  558. prob = probs + IsRepG2 + state;
  559. IF_BIT_0_CHECK(prob)
  560. {
  561. UPDATE_0_CHECK;
  562. }
  563. else
  564. {
  565. UPDATE_1_CHECK;
  566. }
  567. }
  568. }
  569. state = kNumStates;
  570. prob = probs + RepLenCoder;
  571. }
  572. {
  573. unsigned limit, offset;
  574. CLzmaProb *probLen = prob + LenChoice;
  575. IF_BIT_0_CHECK(probLen)
  576. {
  577. UPDATE_0_CHECK;
  578. probLen = prob + LenLow + (posState << kLenNumLowBits);
  579. offset = 0;
  580. limit = 1 << kLenNumLowBits;
  581. }
  582. else
  583. {
  584. UPDATE_1_CHECK;
  585. probLen = prob + LenChoice2;
  586. IF_BIT_0_CHECK(probLen)
  587. {
  588. UPDATE_0_CHECK;
  589. probLen = prob + LenMid + (posState << kLenNumMidBits);
  590. offset = kLenNumLowSymbols;
  591. limit = 1 << kLenNumMidBits;
  592. }
  593. else
  594. {
  595. UPDATE_1_CHECK;
  596. probLen = prob + LenHigh;
  597. offset = kLenNumLowSymbols + kLenNumMidSymbols;
  598. limit = 1 << kLenNumHighBits;
  599. }
  600. }
  601. TREE_DECODE_CHECK(probLen, limit, len);
  602. len += offset;
  603. }
  604. if (state < 4)
  605. {
  606. unsigned posSlot;
  607. prob = probs + PosSlot +
  608. ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
  609. kNumPosSlotBits);
  610. TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot);
  611. if (posSlot >= kStartPosModelIndex)
  612. {
  613. int numDirectBits = ((posSlot >> 1) - 1);
  614. /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
  615. if (posSlot < kEndPosModelIndex)
  616. {
  617. prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits) - posSlot - 1;
  618. }
  619. else
  620. {
  621. numDirectBits -= kNumAlignBits;
  622. do
  623. {
  624. NORMALIZE_CHECK
  625. range >>= 1;
  626. code -= range & (((code - range) >> 31) - 1);
  627. /* if (code >= range) code -= range; */
  628. }
  629. while (--numDirectBits != 0);
  630. prob = probs + Align;
  631. numDirectBits = kNumAlignBits;
  632. }
  633. {
  634. unsigned i = 1;
  635. do
  636. {
  637. GET_BIT_CHECK(prob + i, i);
  638. }
  639. while (--numDirectBits != 0);
  640. }
  641. }
  642. }
  643. }
  644. }
  645. NORMALIZE_CHECK;
  646. return res;
  647. }
  648. static void LzmaDec_InitRc(CLzmaDec *p, const Byte *data)
  649. {
  650. p->code = ((UInt32)data[1] << 24) | ((UInt32)data[2] << 16) | ((UInt32)data[3] << 8) | ((UInt32)data[4]);
  651. p->range = 0xFFFFFFFF;
  652. p->needFlush = 0;
  653. }
  654. void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState)
  655. {
  656. p->needFlush = 1;
  657. p->remainLen = 0;
  658. p->tempBufSize = 0;
  659. if (initDic)
  660. {
  661. p->processedPos = 0;
  662. p->checkDicSize = 0;
  663. p->needInitState = 1;
  664. }
  665. if (initState)
  666. p->needInitState = 1;
  667. }
  668. void LzmaDec_Init(CLzmaDec *p)
  669. {
  670. p->dicPos = 0;
  671. LzmaDec_InitDicAndState(p, True, True);
  672. }
  673. static void LzmaDec_InitStateReal(CLzmaDec *p)
  674. {
  675. UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (p->prop.lc + p->prop.lp));
  676. UInt32 i;
  677. CLzmaProb *probs = p->probs;
  678. for (i = 0; i < numProbs; i++)
  679. probs[i] = kBitModelTotal >> 1;
  680. p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1;
  681. p->state = 0;
  682. p->needInitState = 0;
  683. }
  684. SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen,
  685. ELzmaFinishMode finishMode, ELzmaStatus *status)
  686. {
  687. SizeT inSize = *srcLen;
  688. (*srcLen) = 0;
  689. LzmaDec_WriteRem(p, dicLimit);
  690. *status = LZMA_STATUS_NOT_SPECIFIED;
  691. while (p->remainLen != kMatchSpecLenStart)
  692. {
  693. int checkEndMarkNow;
  694. if (p->needFlush != 0)
  695. {
  696. for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)
  697. p->tempBuf[p->tempBufSize++] = *src++;
  698. if (p->tempBufSize < RC_INIT_SIZE)
  699. {
  700. *status = LZMA_STATUS_NEEDS_MORE_INPUT;
  701. return SZ_OK;
  702. }
  703. if (p->tempBuf[0] != 0)
  704. return SZ_ERROR_DATA;
  705. LzmaDec_InitRc(p, p->tempBuf);
  706. p->tempBufSize = 0;
  707. }
  708. checkEndMarkNow = 0;
  709. if (p->dicPos >= dicLimit)
  710. {
  711. if (p->remainLen == 0 && p->code == 0)
  712. {
  713. *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK;
  714. return SZ_OK;
  715. }
  716. if (finishMode == LZMA_FINISH_ANY)
  717. {
  718. *status = LZMA_STATUS_NOT_FINISHED;
  719. return SZ_OK;
  720. }
  721. if (p->remainLen != 0)
  722. {
  723. *status = LZMA_STATUS_NOT_FINISHED;
  724. return SZ_ERROR_DATA;
  725. }
  726. checkEndMarkNow = 1;
  727. }
  728. if (p->needInitState)
  729. LzmaDec_InitStateReal(p);
  730. if (p->tempBufSize == 0)
  731. {
  732. SizeT processed;
  733. const Byte *bufLimit;
  734. if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
  735. {
  736. int dummyRes = LzmaDec_TryDummy(p, src, inSize);
  737. if (dummyRes == DUMMY_ERROR)
  738. {
  739. memcpy(p->tempBuf, src, inSize);
  740. p->tempBufSize = (unsigned)inSize;
  741. (*srcLen) += inSize;
  742. *status = LZMA_STATUS_NEEDS_MORE_INPUT;
  743. return SZ_OK;
  744. }
  745. if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
  746. {
  747. *status = LZMA_STATUS_NOT_FINISHED;
  748. return SZ_ERROR_DATA;
  749. }
  750. bufLimit = src;
  751. }
  752. else
  753. bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX;
  754. p->buf = src;
  755. if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0)
  756. return SZ_ERROR_DATA;
  757. processed = (SizeT)(p->buf - src);
  758. (*srcLen) += processed;
  759. src += processed;
  760. inSize -= processed;
  761. }
  762. else
  763. {
  764. unsigned rem = p->tempBufSize, lookAhead = 0;
  765. while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize)
  766. p->tempBuf[rem++] = src[lookAhead++];
  767. p->tempBufSize = rem;
  768. if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
  769. {
  770. int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, rem);
  771. if (dummyRes == DUMMY_ERROR)
  772. {
  773. (*srcLen) += lookAhead;
  774. *status = LZMA_STATUS_NEEDS_MORE_INPUT;
  775. return SZ_OK;
  776. }
  777. if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
  778. {
  779. *status = LZMA_STATUS_NOT_FINISHED;
  780. return SZ_ERROR_DATA;
  781. }
  782. }
  783. p->buf = p->tempBuf;
  784. if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0)
  785. return SZ_ERROR_DATA;
  786. lookAhead -= (rem - (unsigned)(p->buf - p->tempBuf));
  787. (*srcLen) += lookAhead;
  788. src += lookAhead;
  789. inSize -= lookAhead;
  790. p->tempBufSize = 0;
  791. }
  792. }
  793. if (p->code == 0)
  794. *status = LZMA_STATUS_FINISHED_WITH_MARK;
  795. return (p->code == 0) ? SZ_OK : SZ_ERROR_DATA;
  796. }
  797. SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
  798. {
  799. SizeT outSize = *destLen;
  800. SizeT inSize = *srcLen;
  801. *srcLen = *destLen = 0;
  802. for (;;)
  803. {
  804. SizeT inSizeCur = inSize, outSizeCur, dicPos;
  805. ELzmaFinishMode curFinishMode;
  806. SRes res;
  807. if (p->dicPos == p->dicBufSize)
  808. p->dicPos = 0;
  809. dicPos = p->dicPos;
  810. if (outSize > p->dicBufSize - dicPos)
  811. {
  812. outSizeCur = p->dicBufSize;
  813. curFinishMode = LZMA_FINISH_ANY;
  814. }
  815. else
  816. {
  817. outSizeCur = dicPos + outSize;
  818. curFinishMode = finishMode;
  819. }
  820. res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status);
  821. src += inSizeCur;
  822. inSize -= inSizeCur;
  823. *srcLen += inSizeCur;
  824. outSizeCur = p->dicPos - dicPos;
  825. memcpy(dest, p->dic + dicPos, outSizeCur);
  826. dest += outSizeCur;
  827. outSize -= outSizeCur;
  828. *destLen += outSizeCur;
  829. if (res != 0)
  830. return res;
  831. if (outSizeCur == 0 || outSize == 0)
  832. return SZ_OK;
  833. }
  834. }
  835. void LzmaDec_FreeProbs(CLzmaDec *p, ISzAlloc *alloc)
  836. {
  837. alloc->Free(alloc, p->probs);
  838. p->probs = 0;
  839. }
  840. static void LzmaDec_FreeDict(CLzmaDec *p, ISzAlloc *alloc)
  841. {
  842. alloc->Free(alloc, p->dic);
  843. p->dic = 0;
  844. }
  845. void LzmaDec_Free(CLzmaDec *p, ISzAlloc *alloc)
  846. {
  847. LzmaDec_FreeProbs(p, alloc);
  848. LzmaDec_FreeDict(p, alloc);
  849. }
  850. SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)
  851. {
  852. UInt32 dicSize;
  853. Byte d;
  854. if (size < LZMA_PROPS_SIZE)
  855. return SZ_ERROR_UNSUPPORTED;
  856. else
  857. dicSize = data[1] | ((UInt32)data[2] << 8) | ((UInt32)data[3] << 16) | ((UInt32)data[4] << 24);
  858. if (dicSize < LZMA_DIC_MIN)
  859. dicSize = LZMA_DIC_MIN;
  860. p->dicSize = dicSize;
  861. d = data[0];
  862. if (d >= (9 * 5 * 5))
  863. return SZ_ERROR_UNSUPPORTED;
  864. p->lc = d % 9;
  865. d /= 9;
  866. p->pb = d / 5;
  867. p->lp = d % 5;
  868. return SZ_OK;
  869. }
  870. static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAlloc *alloc)
  871. {
  872. UInt32 numProbs = LzmaProps_GetNumProbs(propNew);
  873. if (p->probs == 0 || numProbs != p->numProbs)
  874. {
  875. LzmaDec_FreeProbs(p, alloc);
  876. p->probs = (CLzmaProb *)alloc->Alloc(alloc, numProbs * sizeof(CLzmaProb));
  877. p->numProbs = numProbs;
  878. if (p->probs == 0)
  879. return SZ_ERROR_MEM;
  880. }
  881. return SZ_OK;
  882. }
  883. SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
  884. {
  885. CLzmaProps propNew;
  886. RINOK(LzmaProps_Decode(&propNew, props, propsSize));
  887. RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
  888. p->prop = propNew;
  889. return SZ_OK;
  890. }
  891. SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAlloc *alloc)
  892. {
  893. CLzmaProps propNew;
  894. SizeT dicBufSize;
  895. RINOK(LzmaProps_Decode(&propNew, props, propsSize));
  896. RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
  897. dicBufSize = propNew.dicSize;
  898. if (p->dic == 0 || dicBufSize != p->dicBufSize)
  899. {
  900. LzmaDec_FreeDict(p, alloc);
  901. p->dic = (Byte *)alloc->Alloc(alloc, dicBufSize);
  902. if (p->dic == 0)
  903. {
  904. LzmaDec_FreeProbs(p, alloc);
  905. return SZ_ERROR_MEM;
  906. }
  907. }
  908. p->dicBufSize = dicBufSize;
  909. p->prop = propNew;
  910. return SZ_OK;
  911. }
  912. SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
  913. const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
  914. ELzmaStatus *status, ISzAlloc *alloc)
  915. {
  916. CLzmaDec p;
  917. SRes res;
  918. SizeT outSize = *destLen, inSize = *srcLen;
  919. *destLen = *srcLen = 0;
  920. *status = LZMA_STATUS_NOT_SPECIFIED;
  921. if (inSize < RC_INIT_SIZE)
  922. return SZ_ERROR_INPUT_EOF;
  923. LzmaDec_Construct(&p);
  924. RINOK(LzmaDec_AllocateProbs(&p, propData, propSize, alloc));
  925. p.dic = dest;
  926. p.dicBufSize = outSize;
  927. LzmaDec_Init(&p);
  928. *srcLen = inSize;
  929. res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
  930. *destLen = p.dicPos;
  931. if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
  932. res = SZ_ERROR_INPUT_EOF;
  933. LzmaDec_FreeProbs(&p, alloc);
  934. return res;
  935. }