LzmaDec.c 31 KB

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