LzmaDec.c 28 KB

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