LzmaStateDecode.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. LzmaStateDecode.c
  3. LZMA Decoder (State 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 "LzmaStateDecode.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_INIT Code = 0; Range = 0xFFFFFFFF; \
  26. { int i; for(i = 0; i < 5; i++) { Code = (Code << 8) | RC_READ_BYTE; }}
  27. #define RC_NORMALIZE if (Range < kTopValue) { Range <<= 8; Code = (Code << 8) | RC_READ_BYTE; }
  28. #define IfBit0(p) RC_NORMALIZE; bound = (Range >> kNumBitModelTotalBits) * *(p); if (Code < bound)
  29. #define UpdateBit0(p) Range = bound; *(p) += (kBitModelTotal - *(p)) >> kNumMoveBits;
  30. #define UpdateBit1(p) Range -= bound; Code -= bound; *(p) -= (*(p)) >> kNumMoveBits;
  31. #define RC_GET_BIT2(p, mi, A0, A1) IfBit0(p) \
  32. { UpdateBit0(p); mi <<= 1; A0; } else \
  33. { UpdateBit1(p); mi = (mi + mi) + 1; A1; }
  34. #define RC_GET_BIT(p, mi) RC_GET_BIT2(p, mi, ; , ;)
  35. #define RangeDecoderBitTreeDecode(probs, numLevels, res) \
  36. { int i = numLevels; res = 1; \
  37. do { CProb *p = probs + res; RC_GET_BIT(p, res) } while(--i != 0); \
  38. res -= (1 << numLevels); }
  39. #define kNumPosBitsMax 4
  40. #define kNumPosStatesMax (1 << kNumPosBitsMax)
  41. #define kLenNumLowBits 3
  42. #define kLenNumLowSymbols (1 << kLenNumLowBits)
  43. #define kLenNumMidBits 3
  44. #define kLenNumMidSymbols (1 << kLenNumMidBits)
  45. #define kLenNumHighBits 8
  46. #define kLenNumHighSymbols (1 << kLenNumHighBits)
  47. #define LenChoice 0
  48. #define LenChoice2 (LenChoice + 1)
  49. #define LenLow (LenChoice2 + 1)
  50. #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
  51. #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
  52. #define kNumLenProbs (LenHigh + kLenNumHighSymbols)
  53. #define kNumStates 12
  54. #define kNumLitStates 7
  55. #define kStartPosModelIndex 4
  56. #define kEndPosModelIndex 14
  57. #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
  58. #define kNumPosSlotBits 6
  59. #define kNumLenToPosStates 4
  60. #define kNumAlignBits 4
  61. #define kAlignTableSize (1 << kNumAlignBits)
  62. #define kMatchMinLen 2
  63. #define IsMatch 0
  64. #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
  65. #define IsRepG0 (IsRep + kNumStates)
  66. #define IsRepG1 (IsRepG0 + kNumStates)
  67. #define IsRepG2 (IsRepG1 + kNumStates)
  68. #define IsRep0Long (IsRepG2 + kNumStates)
  69. #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
  70. #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
  71. #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
  72. #define LenCoder (Align + kAlignTableSize)
  73. #define RepLenCoder (LenCoder + kNumLenProbs)
  74. #define Literal (RepLenCoder + kNumLenProbs)
  75. #if Literal != LZMA_BASE_SIZE
  76. StopCompilingDueBUG
  77. #endif
  78. /* kRequiredInBufferSize = number of required input bytes for worst case:
  79. longest match with longest distance.
  80. kLzmaInBufferSize must be larger than kRequiredInBufferSize
  81. 23 bits = 2 (match select) + 10 (len) + 6 (distance) + 4(align) + 1 (RC_NORMALIZE)
  82. */
  83. #define kRequiredInBufferSize ((23 * (kNumBitModelTotalBits - kNumMoveBits + 1) + 26 + 9) / 8)
  84. #define kLzmaStreamWasFinishedId (-1)
  85. int LzmaDecodeProperties(CLzmaProperties *propsRes, const unsigned char *propsData, int size)
  86. {
  87. unsigned char prop0;
  88. if (size < LZMA_PROPERTIES_SIZE)
  89. return LZMA_RESULT_DATA_ERROR;
  90. prop0 = propsData[0];
  91. if (prop0 >= (9 * 5 * 5))
  92. return LZMA_RESULT_DATA_ERROR;
  93. {
  94. for (propsRes->pb = 0; prop0 >= (9 * 5); propsRes->pb++, prop0 -= (9 * 5));
  95. for (propsRes->lp = 0; prop0 >= 9; propsRes->lp++, prop0 -= 9);
  96. propsRes->lc = prop0;
  97. /*
  98. unsigned char remainder = (unsigned char)(prop0 / 9);
  99. propsRes->lc = prop0 % 9;
  100. propsRes->pb = remainder / 5;
  101. propsRes->lp = remainder % 5;
  102. */
  103. }
  104. {
  105. int i;
  106. propsRes->DictionarySize = 0;
  107. for (i = 0; i < 4; i++)
  108. propsRes->DictionarySize += (UInt32)(propsData[1 + i]) << (i * 8);
  109. if (propsRes->DictionarySize == 0)
  110. propsRes->DictionarySize = 1;
  111. return LZMA_RESULT_OK;
  112. }
  113. }
  114. int LzmaDecode(
  115. CLzmaDecoderState *vs,
  116. const unsigned char *inStream, SizeT inSize, SizeT *inSizeProcessed,
  117. unsigned char *outStream, SizeT outSize, SizeT *outSizeProcessed,
  118. int finishDecoding)
  119. {
  120. UInt32 Range = vs->Range;
  121. UInt32 Code = vs->Code;
  122. unsigned char *Buffer = vs->Buffer;
  123. int BufferSize = vs->BufferSize; /* don't change it to unsigned int */
  124. CProb *p = vs->Probs;
  125. int state = vs->State;
  126. unsigned char previousByte;
  127. UInt32 rep0 = vs->Reps[0], rep1 = vs->Reps[1], rep2 = vs->Reps[2], rep3 = vs->Reps[3];
  128. SizeT nowPos = 0;
  129. UInt32 posStateMask = (1 << (vs->Properties.pb)) - 1;
  130. UInt32 literalPosMask = (1 << (vs->Properties.lp)) - 1;
  131. int lc = vs->Properties.lc;
  132. int len = vs->RemainLen;
  133. UInt32 globalPos = vs->GlobalPos;
  134. UInt32 distanceLimit = vs->DistanceLimit;
  135. unsigned char *dictionary = vs->Dictionary;
  136. UInt32 dictionarySize = vs->Properties.DictionarySize;
  137. UInt32 dictionaryPos = vs->DictionaryPos;
  138. unsigned char tempDictionary[4];
  139. (*inSizeProcessed) = 0;
  140. (*outSizeProcessed) = 0;
  141. if (len == kLzmaStreamWasFinishedId)
  142. return LZMA_RESULT_OK;
  143. if (dictionarySize == 0)
  144. {
  145. dictionary = tempDictionary;
  146. dictionarySize = 1;
  147. tempDictionary[0] = vs->TempDictionary[0];
  148. }
  149. if (len == kLzmaNeedInitId)
  150. {
  151. while (inSize > 0 && BufferSize < kLzmaInBufferSize)
  152. {
  153. Buffer[BufferSize++] = *inStream++;
  154. (*inSizeProcessed)++;
  155. inSize--;
  156. }
  157. if (BufferSize < 5)
  158. {
  159. vs->BufferSize = BufferSize;
  160. return finishDecoding ? LZMA_RESULT_DATA_ERROR : LZMA_RESULT_OK;
  161. }
  162. {
  163. UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + vs->Properties.lp));
  164. UInt32 i;
  165. for (i = 0; i < numProbs; i++)
  166. p[i] = kBitModelTotal >> 1;
  167. rep0 = rep1 = rep2 = rep3 = 1;
  168. state = 0;
  169. globalPos = 0;
  170. distanceLimit = 0;
  171. dictionaryPos = 0;
  172. dictionary[dictionarySize - 1] = 0;
  173. RC_INIT;
  174. }
  175. len = 0;
  176. }
  177. while(len != 0 && nowPos < outSize)
  178. {
  179. UInt32 pos = dictionaryPos - rep0;
  180. if (pos >= dictionarySize)
  181. pos += dictionarySize;
  182. outStream[nowPos++] = dictionary[dictionaryPos] = dictionary[pos];
  183. if (++dictionaryPos == dictionarySize)
  184. dictionaryPos = 0;
  185. len--;
  186. }
  187. if (dictionaryPos == 0)
  188. previousByte = dictionary[dictionarySize - 1];
  189. else
  190. previousByte = dictionary[dictionaryPos - 1];
  191. for (;;)
  192. {
  193. int bufferPos = (int)(Buffer - vs->Buffer);
  194. if (BufferSize - bufferPos < kRequiredInBufferSize)
  195. {
  196. int i;
  197. BufferSize -= bufferPos;
  198. if (BufferSize < 0)
  199. return LZMA_RESULT_DATA_ERROR;
  200. for (i = 0; i < BufferSize; i++)
  201. vs->Buffer[i] = Buffer[i];
  202. Buffer = vs->Buffer;
  203. while (inSize > 0 && BufferSize < kLzmaInBufferSize)
  204. {
  205. Buffer[BufferSize++] = *inStream++;
  206. (*inSizeProcessed)++;
  207. inSize--;
  208. }
  209. if (BufferSize < kRequiredInBufferSize && !finishDecoding)
  210. break;
  211. }
  212. if (nowPos >= outSize)
  213. break;
  214. {
  215. CProb *prob;
  216. UInt32 bound;
  217. int posState = (int)((nowPos + globalPos) & posStateMask);
  218. prob = p + IsMatch + (state << kNumPosBitsMax) + posState;
  219. IfBit0(prob)
  220. {
  221. int symbol = 1;
  222. UpdateBit0(prob)
  223. prob = p + Literal + (LZMA_LIT_SIZE *
  224. ((((nowPos + globalPos)& literalPosMask) << lc) + (previousByte >> (8 - lc))));
  225. if (state >= kNumLitStates)
  226. {
  227. int matchByte;
  228. UInt32 pos = dictionaryPos - rep0;
  229. if (pos >= dictionarySize)
  230. pos += dictionarySize;
  231. matchByte = dictionary[pos];
  232. do
  233. {
  234. int bit;
  235. CProb *probLit;
  236. matchByte <<= 1;
  237. bit = (matchByte & 0x100);
  238. probLit = prob + 0x100 + bit + symbol;
  239. RC_GET_BIT2(probLit, symbol, if (bit != 0) break, if (bit == 0) break)
  240. }
  241. while (symbol < 0x100);
  242. }
  243. while (symbol < 0x100)
  244. {
  245. CProb *probLit = prob + symbol;
  246. RC_GET_BIT(probLit, symbol)
  247. }
  248. previousByte = (unsigned char)symbol;
  249. outStream[nowPos++] = previousByte;
  250. if (distanceLimit < dictionarySize)
  251. distanceLimit++;
  252. dictionary[dictionaryPos] = previousByte;
  253. if (++dictionaryPos == dictionarySize)
  254. dictionaryPos = 0;
  255. if (state < 4) state = 0;
  256. else if (state < 10) state -= 3;
  257. else state -= 6;
  258. }
  259. else
  260. {
  261. UpdateBit1(prob);
  262. prob = p + IsRep + state;
  263. IfBit0(prob)
  264. {
  265. UpdateBit0(prob);
  266. rep3 = rep2;
  267. rep2 = rep1;
  268. rep1 = rep0;
  269. state = state < kNumLitStates ? 0 : 3;
  270. prob = p + LenCoder;
  271. }
  272. else
  273. {
  274. UpdateBit1(prob);
  275. prob = p + IsRepG0 + state;
  276. IfBit0(prob)
  277. {
  278. UpdateBit0(prob);
  279. prob = p + IsRep0Long + (state << kNumPosBitsMax) + posState;
  280. IfBit0(prob)
  281. {
  282. UInt32 pos;
  283. UpdateBit0(prob);
  284. if (distanceLimit == 0)
  285. return LZMA_RESULT_DATA_ERROR;
  286. if (distanceLimit < dictionarySize)
  287. distanceLimit++;
  288. state = state < kNumLitStates ? 9 : 11;
  289. pos = dictionaryPos - rep0;
  290. if (pos >= dictionarySize)
  291. pos += dictionarySize;
  292. previousByte = dictionary[pos];
  293. dictionary[dictionaryPos] = previousByte;
  294. if (++dictionaryPos == dictionarySize)
  295. dictionaryPos = 0;
  296. outStream[nowPos++] = previousByte;
  297. continue;
  298. }
  299. else
  300. {
  301. UpdateBit1(prob);
  302. }
  303. }
  304. else
  305. {
  306. UInt32 distance;
  307. UpdateBit1(prob);
  308. prob = p + IsRepG1 + state;
  309. IfBit0(prob)
  310. {
  311. UpdateBit0(prob);
  312. distance = rep1;
  313. }
  314. else
  315. {
  316. UpdateBit1(prob);
  317. prob = p + IsRepG2 + state;
  318. IfBit0(prob)
  319. {
  320. UpdateBit0(prob);
  321. distance = rep2;
  322. }
  323. else
  324. {
  325. UpdateBit1(prob);
  326. distance = rep3;
  327. rep3 = rep2;
  328. }
  329. rep2 = rep1;
  330. }
  331. rep1 = rep0;
  332. rep0 = distance;
  333. }
  334. state = state < kNumLitStates ? 8 : 11;
  335. prob = p + RepLenCoder;
  336. }
  337. {
  338. int numBits, offset;
  339. CProb *probLen = prob + LenChoice;
  340. IfBit0(probLen)
  341. {
  342. UpdateBit0(probLen);
  343. probLen = prob + LenLow + (posState << kLenNumLowBits);
  344. offset = 0;
  345. numBits = kLenNumLowBits;
  346. }
  347. else
  348. {
  349. UpdateBit1(probLen);
  350. probLen = prob + LenChoice2;
  351. IfBit0(probLen)
  352. {
  353. UpdateBit0(probLen);
  354. probLen = prob + LenMid + (posState << kLenNumMidBits);
  355. offset = kLenNumLowSymbols;
  356. numBits = kLenNumMidBits;
  357. }
  358. else
  359. {
  360. UpdateBit1(probLen);
  361. probLen = prob + LenHigh;
  362. offset = kLenNumLowSymbols + kLenNumMidSymbols;
  363. numBits = kLenNumHighBits;
  364. }
  365. }
  366. RangeDecoderBitTreeDecode(probLen, numBits, len);
  367. len += offset;
  368. }
  369. if (state < 4)
  370. {
  371. int posSlot;
  372. state += kNumLitStates;
  373. prob = p + PosSlot +
  374. ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
  375. kNumPosSlotBits);
  376. RangeDecoderBitTreeDecode(prob, kNumPosSlotBits, posSlot);
  377. if (posSlot >= kStartPosModelIndex)
  378. {
  379. int numDirectBits = ((posSlot >> 1) - 1);
  380. rep0 = (2 | ((UInt32)posSlot & 1));
  381. if (posSlot < kEndPosModelIndex)
  382. {
  383. rep0 <<= numDirectBits;
  384. prob = p + SpecPos + rep0 - posSlot - 1;
  385. }
  386. else
  387. {
  388. numDirectBits -= kNumAlignBits;
  389. do
  390. {
  391. RC_NORMALIZE
  392. Range >>= 1;
  393. rep0 <<= 1;
  394. if (Code >= Range)
  395. {
  396. Code -= Range;
  397. rep0 |= 1;
  398. }
  399. }
  400. while (--numDirectBits != 0);
  401. prob = p + Align;
  402. rep0 <<= kNumAlignBits;
  403. numDirectBits = kNumAlignBits;
  404. }
  405. {
  406. int i = 1;
  407. int mi = 1;
  408. do
  409. {
  410. CProb *prob3 = prob + mi;
  411. RC_GET_BIT2(prob3, mi, ; , rep0 |= i);
  412. i <<= 1;
  413. }
  414. while(--numDirectBits != 0);
  415. }
  416. }
  417. else
  418. rep0 = posSlot;
  419. if (++rep0 == (UInt32)(0))
  420. {
  421. /* it's for stream version */
  422. len = kLzmaStreamWasFinishedId;
  423. break;
  424. }
  425. }
  426. len += kMatchMinLen;
  427. if (rep0 > distanceLimit)
  428. return LZMA_RESULT_DATA_ERROR;
  429. if (dictionarySize - distanceLimit > (UInt32)len)
  430. distanceLimit += len;
  431. else
  432. distanceLimit = dictionarySize;
  433. do
  434. {
  435. UInt32 pos = dictionaryPos - rep0;
  436. if (pos >= dictionarySize)
  437. pos += dictionarySize;
  438. previousByte = dictionary[pos];
  439. dictionary[dictionaryPos] = previousByte;
  440. if (++dictionaryPos == dictionarySize)
  441. dictionaryPos = 0;
  442. len--;
  443. outStream[nowPos++] = previousByte;
  444. }
  445. while(len != 0 && nowPos < outSize);
  446. }
  447. }
  448. }
  449. RC_NORMALIZE;
  450. BufferSize -= (int)(Buffer - vs->Buffer);
  451. if (BufferSize < 0)
  452. return LZMA_RESULT_DATA_ERROR;
  453. {
  454. int i;
  455. for (i = 0; i < BufferSize; i++)
  456. vs->Buffer[i] = Buffer[i];
  457. }
  458. vs->BufferSize = BufferSize;
  459. vs->Range = Range;
  460. vs->Code = Code;
  461. vs->DictionaryPos = dictionaryPos;
  462. vs->GlobalPos = (UInt32)(globalPos + nowPos);
  463. vs->DistanceLimit = distanceLimit;
  464. vs->Reps[0] = rep0;
  465. vs->Reps[1] = rep1;
  466. vs->Reps[2] = rep2;
  467. vs->Reps[3] = rep3;
  468. vs->State = state;
  469. vs->RemainLen = len;
  470. vs->TempDictionary[0] = tempDictionary[0];
  471. (*outSizeProcessed) = nowPos;
  472. return LZMA_RESULT_OK;
  473. }