LzmaDecode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584
  1. /*
  2. LzmaDecode.c
  3. LZMA Decoder (optimized for Speed version)
  4. LZMA SDK 4.40 Copyright (c) 1999-2006 Igor Pavlov (2006-05-01)
  5. http://www.7-zip.org/
  6. LZMA SDK is licensed under two licenses:
  7. 1) GNU Lesser General Public License (GNU LGPL)
  8. 2) Common Public License (CPL)
  9. It means that you can select one of these two licenses and
  10. follow rules of that license.
  11. SPECIAL EXCEPTION:
  12. Igor Pavlov, as the author of this Code, expressly permits you to
  13. statically or dynamically link your Code (or bind by name) to the
  14. interfaces of this file without subjecting your linked Code to the
  15. terms of the CPL or GNU LGPL. Any modifications or additions
  16. to this file, however, are subject to the LGPL or CPL terms.
  17. */
  18. #include "LzmaDecode.h"
  19. #define kNumTopBits 24
  20. #define kTopValue ((UInt32)1 << kNumTopBits)
  21. #define kNumBitModelTotalBits 11
  22. #define kBitModelTotal (1 << kNumBitModelTotalBits)
  23. #define kNumMoveBits 5
  24. #define RC_READ_BYTE (*Buffer++)
  25. #define RC_INIT2 Code = 0; Range = 0xFFFFFFFF; \
  26. { int i; for(i = 0; i < 5; i++) { RC_TEST; Code = (Code << 8) | RC_READ_BYTE; }}
  27. #ifdef _LZMA_IN_CB
  28. #define RC_TEST { if (Buffer == BufferLim) \
  29. { SizeT size; int result = InCallback->Read(InCallback, &Buffer, &size); if (result != LZMA_RESULT_OK) return result; \
  30. BufferLim = Buffer + size; if (size == 0) return LZMA_RESULT_DATA_ERROR; }}
  31. #define RC_INIT Buffer = BufferLim = 0; RC_INIT2
  32. #else
  33. #define RC_TEST { if (Buffer == BufferLim) return LZMA_RESULT_DATA_ERROR; }
  34. #define RC_INIT(buffer, bufferSize) Buffer = buffer; BufferLim = buffer + bufferSize; RC_INIT2
  35. #endif
  36. #define RC_NORMALIZE if (Range < kTopValue) { RC_TEST; Range <<= 8; Code = (Code << 8) | RC_READ_BYTE; }
  37. #define IfBit0(p) RC_NORMALIZE; bound = (Range >> kNumBitModelTotalBits) * *(p); if (Code < bound)
  38. #define UpdateBit0(p) Range = bound; *(p) += (kBitModelTotal - *(p)) >> kNumMoveBits;
  39. #define UpdateBit1(p) Range -= bound; Code -= bound; *(p) -= (*(p)) >> kNumMoveBits;
  40. #define RC_GET_BIT2(p, mi, A0, A1) IfBit0(p) \
  41. { UpdateBit0(p); mi <<= 1; A0; } else \
  42. { UpdateBit1(p); mi = (mi + mi) + 1; A1; }
  43. #define RC_GET_BIT(p, mi) RC_GET_BIT2(p, mi, ; , ;)
  44. #define RangeDecoderBitTreeDecode(probs, numLevels, res) \
  45. { int i = numLevels; res = 1; \
  46. do { CProb *p = probs + res; RC_GET_BIT(p, res) } while(--i != 0); \
  47. res -= (1 << numLevels); }
  48. #define kNumPosBitsMax 4
  49. #define kNumPosStatesMax (1 << kNumPosBitsMax)
  50. #define kLenNumLowBits 3
  51. #define kLenNumLowSymbols (1 << kLenNumLowBits)
  52. #define kLenNumMidBits 3
  53. #define kLenNumMidSymbols (1 << kLenNumMidBits)
  54. #define kLenNumHighBits 8
  55. #define kLenNumHighSymbols (1 << kLenNumHighBits)
  56. #define LenChoice 0
  57. #define LenChoice2 (LenChoice + 1)
  58. #define LenLow (LenChoice2 + 1)
  59. #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
  60. #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
  61. #define kNumLenProbs (LenHigh + kLenNumHighSymbols)
  62. #define kNumStates 12
  63. #define kNumLitStates 7
  64. #define kStartPosModelIndex 4
  65. #define kEndPosModelIndex 14
  66. #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
  67. #define kNumPosSlotBits 6
  68. #define kNumLenToPosStates 4
  69. #define kNumAlignBits 4
  70. #define kAlignTableSize (1 << kNumAlignBits)
  71. #define kMatchMinLen 2
  72. #define IsMatch 0
  73. #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
  74. #define IsRepG0 (IsRep + kNumStates)
  75. #define IsRepG1 (IsRepG0 + kNumStates)
  76. #define IsRepG2 (IsRepG1 + kNumStates)
  77. #define IsRep0Long (IsRepG2 + kNumStates)
  78. #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
  79. #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
  80. #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
  81. #define LenCoder (Align + kAlignTableSize)
  82. #define RepLenCoder (LenCoder + kNumLenProbs)
  83. #define Literal (RepLenCoder + kNumLenProbs)
  84. #if Literal != LZMA_BASE_SIZE
  85. StopCompilingDueBUG
  86. #endif
  87. int LzmaDecodeProperties(CLzmaProperties *propsRes, const unsigned char *propsData, int size)
  88. {
  89. unsigned char prop0;
  90. if (size < LZMA_PROPERTIES_SIZE)
  91. return LZMA_RESULT_DATA_ERROR;
  92. prop0 = propsData[0];
  93. if (prop0 >= (9 * 5 * 5))
  94. return LZMA_RESULT_DATA_ERROR;
  95. {
  96. for (propsRes->pb = 0; prop0 >= (9 * 5); propsRes->pb++, prop0 -= (9 * 5));
  97. for (propsRes->lp = 0; prop0 >= 9; propsRes->lp++, prop0 -= 9);
  98. propsRes->lc = prop0;
  99. /*
  100. unsigned char remainder = (unsigned char)(prop0 / 9);
  101. propsRes->lc = prop0 % 9;
  102. propsRes->pb = remainder / 5;
  103. propsRes->lp = remainder % 5;
  104. */
  105. }
  106. #ifdef _LZMA_OUT_READ
  107. {
  108. int i;
  109. propsRes->DictionarySize = 0;
  110. for (i = 0; i < 4; i++)
  111. propsRes->DictionarySize += (UInt32)(propsData[1 + i]) << (i * 8);
  112. if (propsRes->DictionarySize == 0)
  113. propsRes->DictionarySize = 1;
  114. }
  115. #endif
  116. return LZMA_RESULT_OK;
  117. }
  118. #define kLzmaStreamWasFinishedId (-1)
  119. int LzmaDecode(CLzmaDecoderState *vs,
  120. #ifdef _LZMA_IN_CB
  121. ILzmaInCallback *InCallback,
  122. #else
  123. const unsigned char *inStream, SizeT inSize, SizeT *inSizeProcessed,
  124. #endif
  125. unsigned char *outStream, SizeT outSize, SizeT *outSizeProcessed)
  126. {
  127. CProb *p = vs->Probs;
  128. SizeT nowPos = 0;
  129. Byte previousByte = 0;
  130. UInt32 posStateMask = (1 << (vs->Properties.pb)) - 1;
  131. UInt32 literalPosMask = (1 << (vs->Properties.lp)) - 1;
  132. int lc = vs->Properties.lc;
  133. #ifdef _LZMA_OUT_READ
  134. UInt32 Range = vs->Range;
  135. UInt32 Code = vs->Code;
  136. #ifdef _LZMA_IN_CB
  137. const Byte *Buffer = vs->Buffer;
  138. const Byte *BufferLim = vs->BufferLim;
  139. #else
  140. const Byte *Buffer = inStream;
  141. const Byte *BufferLim = inStream + inSize;
  142. #endif
  143. int state = vs->State;
  144. UInt32 rep0 = vs->Reps[0], rep1 = vs->Reps[1], rep2 = vs->Reps[2], rep3 = vs->Reps[3];
  145. int len = vs->RemainLen;
  146. UInt32 globalPos = vs->GlobalPos;
  147. UInt32 distanceLimit = vs->DistanceLimit;
  148. Byte *dictionary = vs->Dictionary;
  149. UInt32 dictionarySize = vs->Properties.DictionarySize;
  150. UInt32 dictionaryPos = vs->DictionaryPos;
  151. Byte tempDictionary[4];
  152. #ifndef _LZMA_IN_CB
  153. *inSizeProcessed = 0;
  154. #endif
  155. *outSizeProcessed = 0;
  156. if (len == kLzmaStreamWasFinishedId)
  157. return LZMA_RESULT_OK;
  158. if (dictionarySize == 0)
  159. {
  160. dictionary = tempDictionary;
  161. dictionarySize = 1;
  162. tempDictionary[0] = vs->TempDictionary[0];
  163. }
  164. if (len == kLzmaNeedInitId)
  165. {
  166. {
  167. UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + vs->Properties.lp));
  168. UInt32 i;
  169. for (i = 0; i < numProbs; i++)
  170. p[i] = kBitModelTotal >> 1;
  171. rep0 = rep1 = rep2 = rep3 = 1;
  172. state = 0;
  173. globalPos = 0;
  174. distanceLimit = 0;
  175. dictionaryPos = 0;
  176. dictionary[dictionarySize - 1] = 0;
  177. #ifdef _LZMA_IN_CB
  178. RC_INIT;
  179. #else
  180. RC_INIT(inStream, inSize);
  181. #endif
  182. }
  183. len = 0;
  184. }
  185. while(len != 0 && nowPos < outSize)
  186. {
  187. UInt32 pos = dictionaryPos - rep0;
  188. if (pos >= dictionarySize)
  189. pos += dictionarySize;
  190. outStream[nowPos++] = dictionary[dictionaryPos] = dictionary[pos];
  191. if (++dictionaryPos == dictionarySize)
  192. dictionaryPos = 0;
  193. len--;
  194. }
  195. if (dictionaryPos == 0)
  196. previousByte = dictionary[dictionarySize - 1];
  197. else
  198. previousByte = dictionary[dictionaryPos - 1];
  199. #else /* if !_LZMA_OUT_READ */
  200. int state = 0;
  201. UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
  202. int len = 0;
  203. const Byte *Buffer;
  204. const Byte *BufferLim;
  205. UInt32 Range;
  206. UInt32 Code;
  207. #ifndef _LZMA_IN_CB
  208. *inSizeProcessed = 0;
  209. #endif
  210. *outSizeProcessed = 0;
  211. {
  212. UInt32 i;
  213. UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + vs->Properties.lp));
  214. for (i = 0; i < numProbs; i++)
  215. p[i] = kBitModelTotal >> 1;
  216. }
  217. #ifdef _LZMA_IN_CB
  218. RC_INIT;
  219. #else
  220. RC_INIT(inStream, inSize);
  221. #endif
  222. #endif /* _LZMA_OUT_READ */
  223. while(nowPos < outSize)
  224. {
  225. CProb *prob;
  226. UInt32 bound;
  227. int posState = (int)(
  228. (nowPos
  229. #ifdef _LZMA_OUT_READ
  230. + globalPos
  231. #endif
  232. )
  233. & posStateMask);
  234. prob = p + IsMatch + (state << kNumPosBitsMax) + posState;
  235. IfBit0(prob)
  236. {
  237. int symbol = 1;
  238. UpdateBit0(prob)
  239. prob = p + Literal + (LZMA_LIT_SIZE *
  240. (((
  241. (nowPos
  242. #ifdef _LZMA_OUT_READ
  243. + globalPos
  244. #endif
  245. )
  246. & literalPosMask) << lc) + (previousByte >> (8 - lc))));
  247. if (state >= kNumLitStates)
  248. {
  249. int matchByte;
  250. #ifdef _LZMA_OUT_READ
  251. UInt32 pos = dictionaryPos - rep0;
  252. if (pos >= dictionarySize)
  253. pos += dictionarySize;
  254. matchByte = dictionary[pos];
  255. #else
  256. matchByte = outStream[nowPos - rep0];
  257. #endif
  258. do
  259. {
  260. int bit;
  261. CProb *probLit;
  262. matchByte <<= 1;
  263. bit = (matchByte & 0x100);
  264. probLit = prob + 0x100 + bit + symbol;
  265. RC_GET_BIT2(probLit, symbol, if (bit != 0) break, if (bit == 0) break)
  266. }
  267. while (symbol < 0x100);
  268. }
  269. while (symbol < 0x100)
  270. {
  271. CProb *probLit = prob + symbol;
  272. RC_GET_BIT(probLit, symbol)
  273. }
  274. previousByte = (Byte)symbol;
  275. outStream[nowPos++] = previousByte;
  276. #ifdef _LZMA_OUT_READ
  277. if (distanceLimit < dictionarySize)
  278. distanceLimit++;
  279. dictionary[dictionaryPos] = previousByte;
  280. if (++dictionaryPos == dictionarySize)
  281. dictionaryPos = 0;
  282. #endif
  283. if (state < 4) state = 0;
  284. else if (state < 10) state -= 3;
  285. else state -= 6;
  286. }
  287. else
  288. {
  289. UpdateBit1(prob);
  290. prob = p + IsRep + state;
  291. IfBit0(prob)
  292. {
  293. UpdateBit0(prob);
  294. rep3 = rep2;
  295. rep2 = rep1;
  296. rep1 = rep0;
  297. state = state < kNumLitStates ? 0 : 3;
  298. prob = p + LenCoder;
  299. }
  300. else
  301. {
  302. UpdateBit1(prob);
  303. prob = p + IsRepG0 + state;
  304. IfBit0(prob)
  305. {
  306. UpdateBit0(prob);
  307. prob = p + IsRep0Long + (state << kNumPosBitsMax) + posState;
  308. IfBit0(prob)
  309. {
  310. #ifdef _LZMA_OUT_READ
  311. UInt32 pos;
  312. #endif
  313. UpdateBit0(prob);
  314. #ifdef _LZMA_OUT_READ
  315. if (distanceLimit == 0)
  316. #else
  317. if (nowPos == 0)
  318. #endif
  319. return LZMA_RESULT_DATA_ERROR;
  320. state = state < kNumLitStates ? 9 : 11;
  321. #ifdef _LZMA_OUT_READ
  322. pos = dictionaryPos - rep0;
  323. if (pos >= dictionarySize)
  324. pos += dictionarySize;
  325. previousByte = dictionary[pos];
  326. dictionary[dictionaryPos] = previousByte;
  327. if (++dictionaryPos == dictionarySize)
  328. dictionaryPos = 0;
  329. #else
  330. previousByte = outStream[nowPos - rep0];
  331. #endif
  332. outStream[nowPos++] = previousByte;
  333. #ifdef _LZMA_OUT_READ
  334. if (distanceLimit < dictionarySize)
  335. distanceLimit++;
  336. #endif
  337. continue;
  338. }
  339. else
  340. {
  341. UpdateBit1(prob);
  342. }
  343. }
  344. else
  345. {
  346. UInt32 distance;
  347. UpdateBit1(prob);
  348. prob = p + IsRepG1 + state;
  349. IfBit0(prob)
  350. {
  351. UpdateBit0(prob);
  352. distance = rep1;
  353. }
  354. else
  355. {
  356. UpdateBit1(prob);
  357. prob = p + IsRepG2 + state;
  358. IfBit0(prob)
  359. {
  360. UpdateBit0(prob);
  361. distance = rep2;
  362. }
  363. else
  364. {
  365. UpdateBit1(prob);
  366. distance = rep3;
  367. rep3 = rep2;
  368. }
  369. rep2 = rep1;
  370. }
  371. rep1 = rep0;
  372. rep0 = distance;
  373. }
  374. state = state < kNumLitStates ? 8 : 11;
  375. prob = p + RepLenCoder;
  376. }
  377. {
  378. int numBits, offset;
  379. CProb *probLen = prob + LenChoice;
  380. IfBit0(probLen)
  381. {
  382. UpdateBit0(probLen);
  383. probLen = prob + LenLow + (posState << kLenNumLowBits);
  384. offset = 0;
  385. numBits = kLenNumLowBits;
  386. }
  387. else
  388. {
  389. UpdateBit1(probLen);
  390. probLen = prob + LenChoice2;
  391. IfBit0(probLen)
  392. {
  393. UpdateBit0(probLen);
  394. probLen = prob + LenMid + (posState << kLenNumMidBits);
  395. offset = kLenNumLowSymbols;
  396. numBits = kLenNumMidBits;
  397. }
  398. else
  399. {
  400. UpdateBit1(probLen);
  401. probLen = prob + LenHigh;
  402. offset = kLenNumLowSymbols + kLenNumMidSymbols;
  403. numBits = kLenNumHighBits;
  404. }
  405. }
  406. RangeDecoderBitTreeDecode(probLen, numBits, len);
  407. len += offset;
  408. }
  409. if (state < 4)
  410. {
  411. int posSlot;
  412. state += kNumLitStates;
  413. prob = p + PosSlot +
  414. ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
  415. kNumPosSlotBits);
  416. RangeDecoderBitTreeDecode(prob, kNumPosSlotBits, posSlot);
  417. if (posSlot >= kStartPosModelIndex)
  418. {
  419. int numDirectBits = ((posSlot >> 1) - 1);
  420. rep0 = (2 | ((UInt32)posSlot & 1));
  421. if (posSlot < kEndPosModelIndex)
  422. {
  423. rep0 <<= numDirectBits;
  424. prob = p + SpecPos + rep0 - posSlot - 1;
  425. }
  426. else
  427. {
  428. numDirectBits -= kNumAlignBits;
  429. do
  430. {
  431. RC_NORMALIZE
  432. Range >>= 1;
  433. rep0 <<= 1;
  434. if (Code >= Range)
  435. {
  436. Code -= Range;
  437. rep0 |= 1;
  438. }
  439. }
  440. while (--numDirectBits != 0);
  441. prob = p + Align;
  442. rep0 <<= kNumAlignBits;
  443. numDirectBits = kNumAlignBits;
  444. }
  445. {
  446. int i = 1;
  447. int mi = 1;
  448. do
  449. {
  450. CProb *prob3 = prob + mi;
  451. RC_GET_BIT2(prob3, mi, ; , rep0 |= i);
  452. i <<= 1;
  453. }
  454. while(--numDirectBits != 0);
  455. }
  456. }
  457. else
  458. rep0 = posSlot;
  459. if (++rep0 == (UInt32)(0))
  460. {
  461. /* it's for stream version */
  462. len = kLzmaStreamWasFinishedId;
  463. break;
  464. }
  465. }
  466. len += kMatchMinLen;
  467. #ifdef _LZMA_OUT_READ
  468. if (rep0 > distanceLimit)
  469. #else
  470. if (rep0 > nowPos)
  471. #endif
  472. return LZMA_RESULT_DATA_ERROR;
  473. #ifdef _LZMA_OUT_READ
  474. if (dictionarySize - distanceLimit > (UInt32)len)
  475. distanceLimit += len;
  476. else
  477. distanceLimit = dictionarySize;
  478. #endif
  479. do
  480. {
  481. #ifdef _LZMA_OUT_READ
  482. UInt32 pos = dictionaryPos - rep0;
  483. if (pos >= dictionarySize)
  484. pos += dictionarySize;
  485. previousByte = dictionary[pos];
  486. dictionary[dictionaryPos] = previousByte;
  487. if (++dictionaryPos == dictionarySize)
  488. dictionaryPos = 0;
  489. #else
  490. previousByte = outStream[nowPos - rep0];
  491. #endif
  492. len--;
  493. outStream[nowPos++] = previousByte;
  494. }
  495. while(len != 0 && nowPos < outSize);
  496. }
  497. }
  498. RC_NORMALIZE;
  499. #ifdef _LZMA_OUT_READ
  500. vs->Range = Range;
  501. vs->Code = Code;
  502. vs->DictionaryPos = dictionaryPos;
  503. vs->GlobalPos = globalPos + (UInt32)nowPos;
  504. vs->DistanceLimit = distanceLimit;
  505. vs->Reps[0] = rep0;
  506. vs->Reps[1] = rep1;
  507. vs->Reps[2] = rep2;
  508. vs->Reps[3] = rep3;
  509. vs->State = state;
  510. vs->RemainLen = len;
  511. vs->TempDictionary[0] = tempDictionary[0];
  512. #endif
  513. #ifdef _LZMA_IN_CB
  514. vs->Buffer = Buffer;
  515. vs->BufferLim = BufferLim;
  516. #else
  517. *inSizeProcessed = (SizeT)(Buffer - inStream);
  518. #endif
  519. *outSizeProcessed = nowPos;
  520. return LZMA_RESULT_OK;
  521. }