LzmaDec.c 27 KB

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