LzmaDec.c 28 KB

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