LzmaDec.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209
  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
  204. {
  205. NORMAL_LITER_DEC
  206. }
  207. while (symbol < 0x100);
  208. #else
  209. NORMAL_LITER_DEC
  210. NORMAL_LITER_DEC
  211. NORMAL_LITER_DEC
  212. NORMAL_LITER_DEC
  213. NORMAL_LITER_DEC
  214. NORMAL_LITER_DEC
  215. NORMAL_LITER_DEC
  216. NORMAL_LITER_DEC
  217. #endif
  218. }
  219. else
  220. {
  221. unsigned matchByte = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)];
  222. unsigned offs = 0x100;
  223. state -= (state < 10) ? 3 : 6;
  224. symbol = 1;
  225. #ifdef _LZMA_SIZE_OPT
  226. do
  227. {
  228. unsigned bit;
  229. CLzmaProb *probLit;
  230. MATCHED_LITER_DEC
  231. }
  232. while (symbol < 0x100);
  233. #else
  234. {
  235. unsigned bit;
  236. CLzmaProb *probLit;
  237. MATCHED_LITER_DEC
  238. MATCHED_LITER_DEC
  239. MATCHED_LITER_DEC
  240. MATCHED_LITER_DEC
  241. MATCHED_LITER_DEC
  242. MATCHED_LITER_DEC
  243. MATCHED_LITER_DEC
  244. MATCHED_LITER_DEC
  245. }
  246. #endif
  247. }
  248. dic[dicPos++] = (Byte) symbol;
  249. continue;
  250. }
  251. {
  252. UPDATE_1(prob);
  253. prob = probs + IsRep + state;
  254. IF_BIT_0(prob)
  255. {
  256. UPDATE_0(prob);
  257. state += kNumStates;
  258. prob = probs + LenCoder;
  259. }
  260. else
  261. {
  262. UPDATE_1(prob);
  263. /*
  264. // that case was checked before with kBadRepCode
  265. if (checkDicSize == 0 && processedPos == 0)
  266. return SZ_ERROR_DATA;
  267. */
  268. prob = probs + IsRepG0 + state;
  269. IF_BIT_0(prob)
  270. {
  271. UPDATE_0(prob);
  272. prob = probs + IsRep0Long + COMBINED_PS_STATE;
  273. IF_BIT_0(prob)
  274. {
  275. UPDATE_0(prob);
  276. dic[dicPos] = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)];
  277. dicPos++;
  278. processedPos++;
  279. state = state < kNumLitStates ? 9 : 11;
  280. continue;
  281. }
  282. UPDATE_1(prob);
  283. }
  284. else
  285. {
  286. UInt32 distance;
  287. UPDATE_1(prob);
  288. prob = probs + IsRepG1 + state;
  289. IF_BIT_0(prob)
  290. {
  291. UPDATE_0(prob);
  292. distance = rep1;
  293. }
  294. else
  295. {
  296. UPDATE_1(prob);
  297. prob = probs + IsRepG2 + state;
  298. IF_BIT_0(prob)
  299. {
  300. UPDATE_0(prob);
  301. distance = rep2;
  302. }
  303. else
  304. {
  305. UPDATE_1(prob);
  306. distance = rep3;
  307. rep3 = rep2;
  308. }
  309. rep2 = rep1;
  310. }
  311. rep1 = rep0;
  312. rep0 = distance;
  313. }
  314. state = state < kNumLitStates ? 8 : 11;
  315. prob = probs + RepLenCoder;
  316. }
  317. #ifdef _LZMA_SIZE_OPT
  318. {
  319. unsigned lim, offset;
  320. CLzmaProb *probLen = prob + LenChoice;
  321. IF_BIT_0(probLen)
  322. {
  323. UPDATE_0(probLen);
  324. probLen = prob + LenLow + GET_LEN_STATE;
  325. offset = 0;
  326. lim = (1 << kLenNumLowBits);
  327. }
  328. else
  329. {
  330. UPDATE_1(probLen);
  331. probLen = prob + LenChoice2;
  332. IF_BIT_0(probLen)
  333. {
  334. UPDATE_0(probLen);
  335. probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits);
  336. offset = kLenNumLowSymbols;
  337. lim = (1 << kLenNumLowBits);
  338. }
  339. else
  340. {
  341. UPDATE_1(probLen);
  342. probLen = prob + LenHigh;
  343. offset = kLenNumLowSymbols * 2;
  344. lim = (1 << kLenNumHighBits);
  345. }
  346. }
  347. TREE_DECODE(probLen, lim, len);
  348. len += offset;
  349. }
  350. #else
  351. {
  352. CLzmaProb *probLen = prob + LenChoice;
  353. IF_BIT_0(probLen)
  354. {
  355. UPDATE_0(probLen);
  356. probLen = prob + LenLow + GET_LEN_STATE;
  357. len = 1;
  358. TREE_GET_BIT(probLen, len);
  359. TREE_GET_BIT(probLen, len);
  360. TREE_GET_BIT(probLen, len);
  361. len -= 8;
  362. }
  363. else
  364. {
  365. UPDATE_1(probLen);
  366. probLen = prob + LenChoice2;
  367. IF_BIT_0(probLen)
  368. {
  369. UPDATE_0(probLen);
  370. probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits);
  371. len = 1;
  372. TREE_GET_BIT(probLen, len);
  373. TREE_GET_BIT(probLen, len);
  374. TREE_GET_BIT(probLen, len);
  375. }
  376. else
  377. {
  378. UPDATE_1(probLen);
  379. probLen = prob + LenHigh;
  380. TREE_DECODE(probLen, (1 << kLenNumHighBits), len);
  381. len += kLenNumLowSymbols * 2;
  382. }
  383. }
  384. }
  385. #endif
  386. if (state >= kNumStates)
  387. {
  388. UInt32 distance;
  389. prob = probs + PosSlot +
  390. ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) << kNumPosSlotBits);
  391. TREE_6_DECODE(prob, distance);
  392. if (distance >= kStartPosModelIndex)
  393. {
  394. unsigned posSlot = (unsigned) distance;
  395. unsigned numDirectBits = (unsigned)(((distance >> 1) - 1));
  396. distance = (2 | (distance & 1));
  397. if (posSlot < kEndPosModelIndex)
  398. {
  399. distance <<= numDirectBits;
  400. prob = probs + SpecPos;
  401. {
  402. UInt32 m = 1;
  403. distance++;
  404. do
  405. {
  406. REV_BIT_VAR(prob, distance, m);
  407. }
  408. while (--numDirectBits);
  409. distance -= m;
  410. }
  411. }
  412. else
  413. {
  414. numDirectBits -= kNumAlignBits;
  415. do
  416. {
  417. NORMALIZE
  418. range >>= 1;
  419. {
  420. UInt32 t;
  421. code -= range;
  422. t = (0 - ((UInt32) code >> 31)); /* (UInt32)((Int32)code >> 31) */
  423. distance = (distance << 1) + (t + 1);
  424. code += range & t;
  425. }
  426. /*
  427. distance <<= 1;
  428. if (code >= range)
  429. {
  430. code -= range;
  431. distance |= 1;
  432. }
  433. */
  434. }
  435. while (--numDirectBits);
  436. prob = probs + Align;
  437. distance <<= kNumAlignBits;
  438. {
  439. unsigned i = 1;
  440. REV_BIT_CONST(prob, i, 1);
  441. REV_BIT_CONST(prob, i, 2);
  442. REV_BIT_CONST(prob, i, 4);
  443. REV_BIT_LAST(prob, i, 8);
  444. distance |= i;
  445. }
  446. if (distance == (UInt32) 0xFFFFFFFF)
  447. {
  448. len = kMatchSpecLenStart;
  449. state -= kNumStates;
  450. break;
  451. }
  452. }
  453. }
  454. rep3 = rep2;
  455. rep2 = rep1;
  456. rep1 = rep0;
  457. rep0 = distance + 1;
  458. state = (state < kNumStates + kNumLitStates) ? kNumLitStates : kNumLitStates + 3;
  459. if (distance >= (checkDicSize == 0 ? processedPos : checkDicSize))
  460. {
  461. p->dicPos = dicPos;
  462. return SZ_ERROR_DATA;
  463. }
  464. }
  465. len += kMatchMinLen;
  466. {
  467. SizeT rem;
  468. unsigned curLen;
  469. SizeT pos;
  470. if ((rem = limit - dicPos) == 0)
  471. {
  472. p->dicPos = dicPos;
  473. return SZ_ERROR_DATA;
  474. }
  475. curLen = ((rem < len) ? (unsigned) rem : len);
  476. pos = dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0);
  477. processedPos += (UInt32) curLen;
  478. len -= curLen;
  479. if (curLen <= dicBufSize - pos)
  480. {
  481. Byte *dest = dic + dicPos;
  482. ptrdiff_t src = (ptrdiff_t) pos - (ptrdiff_t) dicPos;
  483. const Byte *lim = dest + curLen;
  484. dicPos += (SizeT) curLen;
  485. do
  486. *(dest) = (Byte) * (dest + src);
  487. while (++dest != lim);
  488. }
  489. else
  490. {
  491. do
  492. {
  493. dic[dicPos++] = dic[pos];
  494. if (++pos == dicBufSize)
  495. pos = 0;
  496. }
  497. while (--curLen != 0);
  498. }
  499. }
  500. }
  501. }
  502. while (dicPos < limit && buf < bufLimit);
  503. NORMALIZE;
  504. p->buf = buf;
  505. p->range = range;
  506. p->code = code;
  507. p->remainLen = (UInt32) len;
  508. p->dicPos = dicPos;
  509. p->processedPos = processedPos;
  510. p->reps[0] = rep0;
  511. p->reps[1] = rep1;
  512. p->reps[2] = rep2;
  513. p->reps[3] = rep3;
  514. p->state = (UInt32) state;
  515. return SZ_OK;
  516. }
  517. #endif
  518. static void MY_FAST_CALL LzmaDec_WriteRem(CLzmaDec *p, SizeT limit)
  519. {
  520. if (p->remainLen != 0 && p->remainLen < kMatchSpecLenStart)
  521. {
  522. Byte *dic = p->dic;
  523. SizeT dicPos = p->dicPos;
  524. SizeT dicBufSize = p->dicBufSize;
  525. unsigned len = (unsigned) p->remainLen;
  526. SizeT rep0 = p->reps[0]; /* we use SizeT to avoid the BUG of VC14 for AMD64 */
  527. SizeT rem = limit - dicPos;
  528. if (rem < len)
  529. len = (unsigned)(rem);
  530. if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= len)
  531. p->checkDicSize = p->prop.dicSize;
  532. p->processedPos += (UInt32) len;
  533. p->remainLen -= (UInt32) len;
  534. while (len != 0)
  535. {
  536. len--;
  537. dic[dicPos] = dic[dicPos - rep0 + (dicPos < rep0 ? dicBufSize : 0)];
  538. dicPos++;
  539. }
  540. p->dicPos = dicPos;
  541. }
  542. }
  543. #define kRange0 0xFFFFFFFF
  544. #define kBound0 ((kRange0 >> kNumBitModelTotalBits) << (kNumBitModelTotalBits - 1))
  545. #define kBadRepCode (kBound0 + (((kRange0 - kBound0) >> kNumBitModelTotalBits) << (kNumBitModelTotalBits - 1)))
  546. #if kBadRepCode != (0xC0000000 - 0x400)
  547. #error Stop_Compiling_Bad_LZMA_Check
  548. #endif
  549. static int MY_FAST_CALL LzmaDec_DecodeReal2(CLzmaDec *p, SizeT limit, const Byte *bufLimit)
  550. {
  551. do
  552. {
  553. SizeT limit2 = limit;
  554. if (p->checkDicSize == 0)
  555. {
  556. UInt32 rem = p->prop.dicSize - p->processedPos;
  557. if (limit - p->dicPos > rem)
  558. limit2 = p->dicPos + rem;
  559. if (p->processedPos == 0)
  560. if (p->code >= kBadRepCode)
  561. return SZ_ERROR_DATA;
  562. }
  563. RINOK(LZMA_DECODE_REAL(p, limit2, bufLimit));
  564. if (p->checkDicSize == 0 && p->processedPos >= p->prop.dicSize)
  565. p->checkDicSize = p->prop.dicSize;
  566. LzmaDec_WriteRem(p, limit);
  567. }
  568. while (p->dicPos < limit && p->buf < bufLimit && p->remainLen < kMatchSpecLenStart);
  569. return 0;
  570. }
  571. typedef enum
  572. {
  573. DUMMY_ERROR, /* unexpected end of input stream */
  574. DUMMY_LIT,
  575. DUMMY_MATCH,
  576. DUMMY_REP
  577. } ELzmaDummy;
  578. static ELzmaDummy LzmaDec_TryDummy(const CLzmaDec *p, const Byte *buf, SizeT inSize)
  579. {
  580. UInt32 range = p->range;
  581. UInt32 code = p->code;
  582. const Byte *bufLimit = buf + inSize;
  583. const CLzmaProb *probs = GET_PROBS;
  584. unsigned state = (unsigned) p->state;
  585. ELzmaDummy res;
  586. {
  587. const CLzmaProb *prob;
  588. UInt32 bound;
  589. unsigned ttt;
  590. unsigned posState = CALC_POS_STATE(p->processedPos, (1 << p->prop.pb) - 1);
  591. prob = probs + IsMatch + COMBINED_PS_STATE;
  592. IF_BIT_0_CHECK(prob)
  593. {
  594. UPDATE_0_CHECK
  595. /* if (bufLimit - buf >= 7) return DUMMY_LIT; */
  596. prob = probs + Literal;
  597. if (p->checkDicSize != 0 || p->processedPos != 0)
  598. prob += ((UInt32) LZMA_LIT_SIZE *
  599. ((((p->processedPos) & ((1 << (p->prop.lp)) - 1)) << p->prop.lc) +
  600. (p->dic[(p->dicPos == 0 ? p->dicBufSize : p->dicPos) - 1] >> (8 - p->prop.lc))));
  601. if (state < kNumLitStates)
  602. {
  603. unsigned symbol = 1;
  604. do
  605. {
  606. GET_BIT_CHECK(prob + symbol, symbol)
  607. }
  608. while (symbol < 0x100);
  609. }
  610. else
  611. {
  612. unsigned matchByte = p->dic[p->dicPos - p->reps[0] +
  613. (p->dicPos < p->reps[0] ? p->dicBufSize : 0)];
  614. unsigned offs = 0x100;
  615. unsigned symbol = 1;
  616. do
  617. {
  618. unsigned bit;
  619. const CLzmaProb *probLit;
  620. matchByte += matchByte;
  621. bit = offs;
  622. offs &= matchByte;
  623. probLit = prob + (offs + bit + symbol);
  624. GET_BIT2_CHECK(probLit, symbol, offs ^= bit;,;)
  625. }
  626. while (symbol < 0x100);
  627. }
  628. res = DUMMY_LIT;
  629. }
  630. else
  631. {
  632. unsigned len;
  633. UPDATE_1_CHECK;
  634. prob = probs + IsRep + state;
  635. IF_BIT_0_CHECK(prob)
  636. {
  637. UPDATE_0_CHECK;
  638. state = 0;
  639. prob = probs + LenCoder;
  640. res = DUMMY_MATCH;
  641. }
  642. else
  643. {
  644. UPDATE_1_CHECK;
  645. res = DUMMY_REP;
  646. prob = probs + IsRepG0 + state;
  647. IF_BIT_0_CHECK(prob)
  648. {
  649. UPDATE_0_CHECK;
  650. prob = probs + IsRep0Long + COMBINED_PS_STATE;
  651. IF_BIT_0_CHECK(prob)
  652. {
  653. UPDATE_0_CHECK;
  654. NORMALIZE_CHECK;
  655. return DUMMY_REP;
  656. }
  657. else
  658. {
  659. UPDATE_1_CHECK;
  660. }
  661. }
  662. else
  663. {
  664. UPDATE_1_CHECK;
  665. prob = probs + IsRepG1 + state;
  666. IF_BIT_0_CHECK(prob)
  667. {
  668. UPDATE_0_CHECK;
  669. }
  670. else
  671. {
  672. UPDATE_1_CHECK;
  673. prob = probs + IsRepG2 + state;
  674. IF_BIT_0_CHECK(prob)
  675. {
  676. UPDATE_0_CHECK;
  677. }
  678. else
  679. {
  680. UPDATE_1_CHECK;
  681. }
  682. }
  683. }
  684. state = kNumStates;
  685. prob = probs + RepLenCoder;
  686. }
  687. {
  688. unsigned limit, offset;
  689. const CLzmaProb *probLen = prob + LenChoice;
  690. IF_BIT_0_CHECK(probLen)
  691. {
  692. UPDATE_0_CHECK;
  693. probLen = prob + LenLow + GET_LEN_STATE;
  694. offset = 0;
  695. limit = 1 << kLenNumLowBits;
  696. }
  697. else
  698. {
  699. UPDATE_1_CHECK;
  700. probLen = prob + LenChoice2;
  701. IF_BIT_0_CHECK(probLen)
  702. {
  703. UPDATE_0_CHECK;
  704. probLen = prob + LenLow + GET_LEN_STATE + (1 << kLenNumLowBits);
  705. offset = kLenNumLowSymbols;
  706. limit = 1 << kLenNumLowBits;
  707. }
  708. else
  709. {
  710. UPDATE_1_CHECK;
  711. probLen = prob + LenHigh;
  712. offset = kLenNumLowSymbols * 2;
  713. limit = 1 << kLenNumHighBits;
  714. }
  715. }
  716. TREE_DECODE_CHECK(probLen, limit, len);
  717. len += offset;
  718. }
  719. if (state < 4)
  720. {
  721. unsigned posSlot;
  722. prob = probs + PosSlot +
  723. ((len < kNumLenToPosStates - 1 ? len : kNumLenToPosStates - 1) <<
  724. kNumPosSlotBits);
  725. TREE_DECODE_CHECK(prob, 1 << kNumPosSlotBits, posSlot);
  726. if (posSlot >= kStartPosModelIndex)
  727. {
  728. unsigned numDirectBits = ((posSlot >> 1) - 1);
  729. /* if (bufLimit - buf >= 8) return DUMMY_MATCH; */
  730. if (posSlot < kEndPosModelIndex)
  731. {
  732. prob = probs + SpecPos + ((2 | (posSlot & 1)) << numDirectBits);
  733. }
  734. else
  735. {
  736. numDirectBits -= kNumAlignBits;
  737. do
  738. {
  739. NORMALIZE_CHECK
  740. range >>= 1;
  741. code -= range & (((code - range) >> 31) - 1);
  742. /* if (code >= range) code -= range; */
  743. }
  744. while (--numDirectBits);
  745. prob = probs + Align;
  746. numDirectBits = kNumAlignBits;
  747. }
  748. {
  749. unsigned i = 1;
  750. unsigned m = 1;
  751. do
  752. {
  753. REV_BIT_CHECK(prob, i, m);
  754. }
  755. while (--numDirectBits);
  756. }
  757. }
  758. }
  759. }
  760. }
  761. NORMALIZE_CHECK;
  762. return res;
  763. }
  764. void LzmaDec_InitDicAndState(CLzmaDec *p, BoolInt initDic, BoolInt initState)
  765. {
  766. p->remainLen = kMatchSpecLenStart + 1;
  767. p->tempBufSize = 0;
  768. if (initDic)
  769. {
  770. p->processedPos = 0;
  771. p->checkDicSize = 0;
  772. p->remainLen = kMatchSpecLenStart + 2;
  773. }
  774. if (initState)
  775. p->remainLen = kMatchSpecLenStart + 2;
  776. }
  777. void LzmaDec_Init(CLzmaDec *p)
  778. {
  779. p->dicPos = 0;
  780. LzmaDec_InitDicAndState(p, True, True);
  781. }
  782. SRes LzmaDec_DecodeToDic(CLzmaDec *p, SizeT dicLimit, const Byte *src, SizeT *srcLen,
  783. ELzmaFinishMode finishMode, ELzmaStatus *status)
  784. {
  785. SizeT inSize = *srcLen;
  786. (*srcLen) = 0;
  787. *status = LZMA_STATUS_NOT_SPECIFIED;
  788. if (p->remainLen > kMatchSpecLenStart)
  789. {
  790. for (; inSize > 0 && p->tempBufSize < RC_INIT_SIZE; (*srcLen)++, inSize--)
  791. p->tempBuf[p->tempBufSize++] = *src++;
  792. if (p->tempBufSize != 0 && p->tempBuf[0] != 0)
  793. return SZ_ERROR_DATA;
  794. if (p->tempBufSize < RC_INIT_SIZE)
  795. {
  796. *status = LZMA_STATUS_NEEDS_MORE_INPUT;
  797. return SZ_OK;
  798. }
  799. p->code =
  800. ((UInt32) p->tempBuf[1] << 24)
  801. | ((UInt32) p->tempBuf[2] << 16)
  802. | ((UInt32) p->tempBuf[3] << 8)
  803. | ((UInt32) p->tempBuf[4]);
  804. p->range = 0xFFFFFFFF;
  805. p->tempBufSize = 0;
  806. if (p->remainLen > kMatchSpecLenStart + 1)
  807. {
  808. SizeT numProbs = LzmaProps_GetNumProbs(&p->prop);
  809. SizeT i;
  810. CLzmaProb *probs = p->probs;
  811. for (i = 0; i < numProbs; i++)
  812. probs[i] = kBitModelTotal >> 1;
  813. p->reps[0] = p->reps[1] = p->reps[2] = p->reps[3] = 1;
  814. p->state = 0;
  815. }
  816. p->remainLen = 0;
  817. }
  818. LzmaDec_WriteRem(p, dicLimit);
  819. while (p->remainLen != kMatchSpecLenStart)
  820. {
  821. int checkEndMarkNow = 0;
  822. if (p->dicPos >= dicLimit)
  823. {
  824. if (p->remainLen == 0 && p->code == 0)
  825. {
  826. *status = LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK;
  827. return SZ_OK;
  828. }
  829. if (finishMode == LZMA_FINISH_ANY)
  830. {
  831. *status = LZMA_STATUS_NOT_FINISHED;
  832. return SZ_OK;
  833. }
  834. if (p->remainLen != 0)
  835. {
  836. *status = LZMA_STATUS_NOT_FINISHED;
  837. return SZ_ERROR_DATA;
  838. }
  839. checkEndMarkNow = 1;
  840. }
  841. if (p->tempBufSize == 0)
  842. {
  843. SizeT processed;
  844. const Byte *bufLimit;
  845. if (inSize < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
  846. {
  847. int dummyRes = LzmaDec_TryDummy(p, src, inSize);
  848. if (dummyRes == DUMMY_ERROR)
  849. {
  850. memcpy(p->tempBuf, src, inSize);
  851. p->tempBufSize = (unsigned) inSize;
  852. (*srcLen) += inSize;
  853. *status = LZMA_STATUS_NEEDS_MORE_INPUT;
  854. return SZ_OK;
  855. }
  856. if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
  857. {
  858. *status = LZMA_STATUS_NOT_FINISHED;
  859. return SZ_ERROR_DATA;
  860. }
  861. bufLimit = src;
  862. }
  863. else
  864. bufLimit = src + inSize - LZMA_REQUIRED_INPUT_MAX;
  865. p->buf = src;
  866. if (LzmaDec_DecodeReal2(p, dicLimit, bufLimit) != 0)
  867. return SZ_ERROR_DATA;
  868. processed = (SizeT)(p->buf - src);
  869. (*srcLen) += processed;
  870. src += processed;
  871. inSize -= processed;
  872. }
  873. else
  874. {
  875. unsigned rem = p->tempBufSize, lookAhead = 0;
  876. while (rem < LZMA_REQUIRED_INPUT_MAX && lookAhead < inSize)
  877. p->tempBuf[rem++] = src[lookAhead++];
  878. p->tempBufSize = rem;
  879. if (rem < LZMA_REQUIRED_INPUT_MAX || checkEndMarkNow)
  880. {
  881. int dummyRes = LzmaDec_TryDummy(p, p->tempBuf, (SizeT) rem);
  882. if (dummyRes == DUMMY_ERROR)
  883. {
  884. (*srcLen) += (SizeT) lookAhead;
  885. *status = LZMA_STATUS_NEEDS_MORE_INPUT;
  886. return SZ_OK;
  887. }
  888. if (checkEndMarkNow && dummyRes != DUMMY_MATCH)
  889. {
  890. *status = LZMA_STATUS_NOT_FINISHED;
  891. return SZ_ERROR_DATA;
  892. }
  893. }
  894. p->buf = p->tempBuf;
  895. if (LzmaDec_DecodeReal2(p, dicLimit, p->buf) != 0)
  896. return SZ_ERROR_DATA;
  897. {
  898. unsigned kkk = (unsigned)(p->buf - p->tempBuf);
  899. if (rem < kkk)
  900. return SZ_ERROR_FAIL; /* some internal error */
  901. rem -= kkk;
  902. if (lookAhead < rem)
  903. return SZ_ERROR_FAIL; /* some internal error */
  904. lookAhead -= rem;
  905. }
  906. (*srcLen) += (SizeT) lookAhead;
  907. src += lookAhead;
  908. inSize -= (SizeT) lookAhead;
  909. p->tempBufSize = 0;
  910. }
  911. }
  912. if (p->code != 0)
  913. return SZ_ERROR_DATA;
  914. *status = LZMA_STATUS_FINISHED_WITH_MARK;
  915. return SZ_OK;
  916. }
  917. SRes LzmaDec_DecodeToBuf(CLzmaDec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
  918. {
  919. SizeT outSize = *destLen;
  920. SizeT inSize = *srcLen;
  921. *srcLen = *destLen = 0;
  922. for (;;)
  923. {
  924. SizeT inSizeCur = inSize, outSizeCur, dicPos;
  925. ELzmaFinishMode curFinishMode;
  926. SRes res;
  927. if (p->dicPos == p->dicBufSize)
  928. p->dicPos = 0;
  929. dicPos = p->dicPos;
  930. if (outSize > p->dicBufSize - dicPos)
  931. {
  932. outSizeCur = p->dicBufSize;
  933. curFinishMode = LZMA_FINISH_ANY;
  934. }
  935. else
  936. {
  937. outSizeCur = dicPos + outSize;
  938. curFinishMode = finishMode;
  939. }
  940. res = LzmaDec_DecodeToDic(p, outSizeCur, src, &inSizeCur, curFinishMode, status);
  941. src += inSizeCur;
  942. inSize -= inSizeCur;
  943. *srcLen += inSizeCur;
  944. outSizeCur = p->dicPos - dicPos;
  945. memcpy(dest, p->dic + dicPos, outSizeCur);
  946. dest += outSizeCur;
  947. outSize -= outSizeCur;
  948. *destLen += outSizeCur;
  949. if (res != 0)
  950. return res;
  951. if (outSizeCur == 0 || outSize == 0)
  952. return SZ_OK;
  953. }
  954. }
  955. void LzmaDec_FreeProbs(CLzmaDec *p, ISzAllocPtr alloc)
  956. {
  957. ISzAlloc_Free(alloc, p->probs);
  958. p->probs = NULL;
  959. }
  960. static void LzmaDec_FreeDict(CLzmaDec *p, ISzAllocPtr alloc)
  961. {
  962. ISzAlloc_Free(alloc, p->dic);
  963. p->dic = NULL;
  964. }
  965. void LzmaDec_Free(CLzmaDec *p, ISzAllocPtr alloc)
  966. {
  967. LzmaDec_FreeProbs(p, alloc);
  968. LzmaDec_FreeDict(p, alloc);
  969. }
  970. SRes LzmaProps_Decode(CLzmaProps *p, const Byte *data, unsigned size)
  971. {
  972. UInt32 dicSize;
  973. Byte d;
  974. if (size < LZMA_PROPS_SIZE)
  975. return SZ_ERROR_UNSUPPORTED;
  976. else
  977. dicSize = data[1] | ((UInt32) data[2] << 8) | ((UInt32) data[3] << 16) | ((UInt32) data[4] << 24);
  978. if (dicSize < LZMA_DIC_MIN)
  979. dicSize = LZMA_DIC_MIN;
  980. p->dicSize = dicSize;
  981. d = data[0];
  982. if (d >= (9 * 5 * 5))
  983. return SZ_ERROR_UNSUPPORTED;
  984. p->lc = (Byte)(d % 9);
  985. d /= 9;
  986. p->pb = (Byte)(d / 5);
  987. p->lp = (Byte)(d % 5);
  988. return SZ_OK;
  989. }
  990. static SRes LzmaDec_AllocateProbs2(CLzmaDec *p, const CLzmaProps *propNew, ISzAllocPtr alloc)
  991. {
  992. UInt32 numProbs = LzmaProps_GetNumProbs(propNew);
  993. if (!p->probs || numProbs != p->numProbs)
  994. {
  995. LzmaDec_FreeProbs(p, alloc);
  996. p->probs = (CLzmaProb *) ISzAlloc_Alloc(alloc, numProbs * sizeof(CLzmaProb));
  997. if (!p->probs)
  998. return SZ_ERROR_MEM;
  999. p->probs_1664 = p->probs + 1664;
  1000. p->numProbs = numProbs;
  1001. }
  1002. return SZ_OK;
  1003. }
  1004. SRes LzmaDec_AllocateProbs(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc)
  1005. {
  1006. CLzmaProps propNew;
  1007. RINOK(LzmaProps_Decode(&propNew, props, propsSize));
  1008. RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
  1009. p->prop = propNew;
  1010. return SZ_OK;
  1011. }
  1012. SRes LzmaDec_Allocate(CLzmaDec *p, const Byte *props, unsigned propsSize, ISzAllocPtr alloc)
  1013. {
  1014. CLzmaProps propNew;
  1015. SizeT dicBufSize;
  1016. RINOK(LzmaProps_Decode(&propNew, props, propsSize));
  1017. RINOK(LzmaDec_AllocateProbs2(p, &propNew, alloc));
  1018. {
  1019. UInt32 dictSize = propNew.dicSize;
  1020. SizeT mask = ((UInt32) 1 << 12) - 1;
  1021. if (dictSize >= ((UInt32) 1 << 30)) mask = ((UInt32) 1 << 22) - 1;
  1022. else if (dictSize >= ((UInt32) 1 << 22)) mask = ((UInt32) 1 << 20) - 1;
  1023. ;
  1024. dicBufSize = ((SizeT) dictSize + mask) & ~mask;
  1025. if (dicBufSize < dictSize)
  1026. dicBufSize = dictSize;
  1027. }
  1028. if (!p->dic || dicBufSize != p->dicBufSize)
  1029. {
  1030. LzmaDec_FreeDict(p, alloc);
  1031. p->dic = (Byte *) ISzAlloc_Alloc(alloc, dicBufSize);
  1032. if (!p->dic)
  1033. {
  1034. LzmaDec_FreeProbs(p, alloc);
  1035. return SZ_ERROR_MEM;
  1036. }
  1037. }
  1038. p->dicBufSize = dicBufSize;
  1039. p->prop = propNew;
  1040. return SZ_OK;
  1041. }
  1042. SRes LzmaDecode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
  1043. const Byte *propData, unsigned propSize, ELzmaFinishMode finishMode,
  1044. ELzmaStatus *status, ISzAllocPtr alloc)
  1045. {
  1046. CLzmaDec p;
  1047. SRes res;
  1048. SizeT outSize = *destLen, inSize = *srcLen;
  1049. *destLen = *srcLen = 0;
  1050. *status = LZMA_STATUS_NOT_SPECIFIED;
  1051. if (inSize < RC_INIT_SIZE)
  1052. return SZ_ERROR_INPUT_EOF;
  1053. LzmaDec_Construct(&p);
  1054. RINOK(LzmaDec_AllocateProbs(&p, propData, propSize, alloc));
  1055. p.dic = dest;
  1056. p.dicBufSize = outSize;
  1057. LzmaDec_Init(&p);
  1058. *srcLen = inSize;
  1059. res = LzmaDec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
  1060. *destLen = p.dicPos;
  1061. if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
  1062. res = SZ_ERROR_INPUT_EOF;
  1063. LzmaDec_FreeProbs(&p, alloc);
  1064. return res;
  1065. }