LzmaDecodeSize.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. LzmaDecodeSize.c
  3. LZMA Decoder (optimized for Size 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. typedef struct _CRangeDecoder
  25. {
  26. const Byte *Buffer;
  27. const Byte *BufferLim;
  28. UInt32 Range;
  29. UInt32 Code;
  30. #ifdef _LZMA_IN_CB
  31. ILzmaInCallback *InCallback;
  32. int Result;
  33. #endif
  34. int ExtraBytes;
  35. } CRangeDecoder;
  36. Byte RangeDecoderReadByte(CRangeDecoder *rd)
  37. {
  38. if (rd->Buffer == rd->BufferLim)
  39. {
  40. #ifdef _LZMA_IN_CB
  41. SizeT size;
  42. rd->Result = rd->InCallback->Read(rd->InCallback, &rd->Buffer, &size);
  43. rd->BufferLim = rd->Buffer + size;
  44. if (size == 0)
  45. #endif
  46. {
  47. rd->ExtraBytes = 1;
  48. return 0xFF;
  49. }
  50. }
  51. return (*rd->Buffer++);
  52. }
  53. /* #define ReadByte (*rd->Buffer++) */
  54. #define ReadByte (RangeDecoderReadByte(rd))
  55. void RangeDecoderInit(CRangeDecoder *rd
  56. #ifndef _LZMA_IN_CB
  57. , const Byte *stream, SizeT bufferSize
  58. #endif
  59. )
  60. {
  61. int i;
  62. #ifdef _LZMA_IN_CB
  63. rd->Buffer = rd->BufferLim = 0;
  64. #else
  65. rd->Buffer = stream;
  66. rd->BufferLim = stream + bufferSize;
  67. #endif
  68. rd->ExtraBytes = 0;
  69. rd->Code = 0;
  70. rd->Range = (0xFFFFFFFF);
  71. for(i = 0; i < 5; i++)
  72. rd->Code = (rd->Code << 8) | ReadByte;
  73. }
  74. #define RC_INIT_VAR UInt32 range = rd->Range; UInt32 code = rd->Code;
  75. #define RC_FLUSH_VAR rd->Range = range; rd->Code = code;
  76. #define RC_NORMALIZE if (range < kTopValue) { range <<= 8; code = (code << 8) | ReadByte; }
  77. UInt32 RangeDecoderDecodeDirectBits(CRangeDecoder *rd, int numTotalBits)
  78. {
  79. RC_INIT_VAR
  80. UInt32 result = 0;
  81. int i;
  82. for (i = numTotalBits; i != 0; i--)
  83. {
  84. /* UInt32 t; */
  85. range >>= 1;
  86. result <<= 1;
  87. if (code >= range)
  88. {
  89. code -= range;
  90. result |= 1;
  91. }
  92. /*
  93. t = (code - range) >> 31;
  94. t &= 1;
  95. code -= range & (t - 1);
  96. result = (result + result) | (1 - t);
  97. */
  98. RC_NORMALIZE
  99. }
  100. RC_FLUSH_VAR
  101. return result;
  102. }
  103. int RangeDecoderBitDecode(CProb *prob, CRangeDecoder *rd)
  104. {
  105. UInt32 bound = (rd->Range >> kNumBitModelTotalBits) * *prob;
  106. if (rd->Code < bound)
  107. {
  108. rd->Range = bound;
  109. *prob += (kBitModelTotal - *prob) >> kNumMoveBits;
  110. if (rd->Range < kTopValue)
  111. {
  112. rd->Code = (rd->Code << 8) | ReadByte;
  113. rd->Range <<= 8;
  114. }
  115. return 0;
  116. }
  117. else
  118. {
  119. rd->Range -= bound;
  120. rd->Code -= bound;
  121. *prob -= (*prob) >> kNumMoveBits;
  122. if (rd->Range < kTopValue)
  123. {
  124. rd->Code = (rd->Code << 8) | ReadByte;
  125. rd->Range <<= 8;
  126. }
  127. return 1;
  128. }
  129. }
  130. #define RC_GET_BIT2(prob, mi, A0, A1) \
  131. UInt32 bound = (range >> kNumBitModelTotalBits) * *prob; \
  132. if (code < bound) \
  133. { A0; range = bound; *prob += (kBitModelTotal - *prob) >> kNumMoveBits; mi <<= 1; } \
  134. else \
  135. { A1; range -= bound; code -= bound; *prob -= (*prob) >> kNumMoveBits; mi = (mi + mi) + 1; } \
  136. RC_NORMALIZE
  137. #define RC_GET_BIT(prob, mi) RC_GET_BIT2(prob, mi, ; , ;)
  138. int RangeDecoderBitTreeDecode(CProb *probs, int numLevels, CRangeDecoder *rd)
  139. {
  140. int mi = 1;
  141. int i;
  142. #ifdef _LZMA_LOC_OPT
  143. RC_INIT_VAR
  144. #endif
  145. for(i = numLevels; i != 0; i--)
  146. {
  147. #ifdef _LZMA_LOC_OPT
  148. CProb *prob = probs + mi;
  149. RC_GET_BIT(prob, mi)
  150. #else
  151. mi = (mi + mi) + RangeDecoderBitDecode(probs + mi, rd);
  152. #endif
  153. }
  154. #ifdef _LZMA_LOC_OPT
  155. RC_FLUSH_VAR
  156. #endif
  157. return mi - (1 << numLevels);
  158. }
  159. int RangeDecoderReverseBitTreeDecode(CProb *probs, int numLevels, CRangeDecoder *rd)
  160. {
  161. int mi = 1;
  162. int i;
  163. int symbol = 0;
  164. #ifdef _LZMA_LOC_OPT
  165. RC_INIT_VAR
  166. #endif
  167. for(i = 0; i < numLevels; i++)
  168. {
  169. #ifdef _LZMA_LOC_OPT
  170. CProb *prob = probs + mi;
  171. RC_GET_BIT2(prob, mi, ; , symbol |= (1 << i))
  172. #else
  173. int bit = RangeDecoderBitDecode(probs + mi, rd);
  174. mi = mi + mi + bit;
  175. symbol |= (bit << i);
  176. #endif
  177. }
  178. #ifdef _LZMA_LOC_OPT
  179. RC_FLUSH_VAR
  180. #endif
  181. return symbol;
  182. }
  183. Byte LzmaLiteralDecode(CProb *probs, CRangeDecoder *rd)
  184. {
  185. int symbol = 1;
  186. #ifdef _LZMA_LOC_OPT
  187. RC_INIT_VAR
  188. #endif
  189. do
  190. {
  191. #ifdef _LZMA_LOC_OPT
  192. CProb *prob = probs + symbol;
  193. RC_GET_BIT(prob, symbol)
  194. #else
  195. symbol = (symbol + symbol) | RangeDecoderBitDecode(probs + symbol, rd);
  196. #endif
  197. }
  198. while (symbol < 0x100);
  199. #ifdef _LZMA_LOC_OPT
  200. RC_FLUSH_VAR
  201. #endif
  202. return symbol;
  203. }
  204. Byte LzmaLiteralDecodeMatch(CProb *probs, CRangeDecoder *rd, Byte matchByte)
  205. {
  206. int symbol = 1;
  207. #ifdef _LZMA_LOC_OPT
  208. RC_INIT_VAR
  209. #endif
  210. do
  211. {
  212. int bit;
  213. int matchBit = (matchByte >> 7) & 1;
  214. matchByte <<= 1;
  215. #ifdef _LZMA_LOC_OPT
  216. {
  217. CProb *prob = probs + 0x100 + (matchBit << 8) + symbol;
  218. RC_GET_BIT2(prob, symbol, bit = 0, bit = 1)
  219. }
  220. #else
  221. bit = RangeDecoderBitDecode(probs + 0x100 + (matchBit << 8) + symbol, rd);
  222. symbol = (symbol << 1) | bit;
  223. #endif
  224. if (matchBit != bit)
  225. {
  226. while (symbol < 0x100)
  227. {
  228. #ifdef _LZMA_LOC_OPT
  229. CProb *prob = probs + symbol;
  230. RC_GET_BIT(prob, symbol)
  231. #else
  232. symbol = (symbol + symbol) | RangeDecoderBitDecode(probs + symbol, rd);
  233. #endif
  234. }
  235. break;
  236. }
  237. }
  238. while (symbol < 0x100);
  239. #ifdef _LZMA_LOC_OPT
  240. RC_FLUSH_VAR
  241. #endif
  242. return symbol;
  243. }
  244. #define kNumPosBitsMax 4
  245. #define kNumPosStatesMax (1 << kNumPosBitsMax)
  246. #define kLenNumLowBits 3
  247. #define kLenNumLowSymbols (1 << kLenNumLowBits)
  248. #define kLenNumMidBits 3
  249. #define kLenNumMidSymbols (1 << kLenNumMidBits)
  250. #define kLenNumHighBits 8
  251. #define kLenNumHighSymbols (1 << kLenNumHighBits)
  252. #define LenChoice 0
  253. #define LenChoice2 (LenChoice + 1)
  254. #define LenLow (LenChoice2 + 1)
  255. #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
  256. #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
  257. #define kNumLenProbs (LenHigh + kLenNumHighSymbols)
  258. int LzmaLenDecode(CProb *p, CRangeDecoder *rd, int posState)
  259. {
  260. if(RangeDecoderBitDecode(p + LenChoice, rd) == 0)
  261. return RangeDecoderBitTreeDecode(p + LenLow +
  262. (posState << kLenNumLowBits), kLenNumLowBits, rd);
  263. if(RangeDecoderBitDecode(p + LenChoice2, rd) == 0)
  264. return kLenNumLowSymbols + RangeDecoderBitTreeDecode(p + LenMid +
  265. (posState << kLenNumMidBits), kLenNumMidBits, rd);
  266. return kLenNumLowSymbols + kLenNumMidSymbols +
  267. RangeDecoderBitTreeDecode(p + LenHigh, kLenNumHighBits, rd);
  268. }
  269. #define kNumStates 12
  270. #define kNumLitStates 7
  271. #define kStartPosModelIndex 4
  272. #define kEndPosModelIndex 14
  273. #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
  274. #define kNumPosSlotBits 6
  275. #define kNumLenToPosStates 4
  276. #define kNumAlignBits 4
  277. #define kAlignTableSize (1 << kNumAlignBits)
  278. #define kMatchMinLen 2
  279. #define IsMatch 0
  280. #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
  281. #define IsRepG0 (IsRep + kNumStates)
  282. #define IsRepG1 (IsRepG0 + kNumStates)
  283. #define IsRepG2 (IsRepG1 + kNumStates)
  284. #define IsRep0Long (IsRepG2 + kNumStates)
  285. #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
  286. #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
  287. #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
  288. #define LenCoder (Align + kAlignTableSize)
  289. #define RepLenCoder (LenCoder + kNumLenProbs)
  290. #define Literal (RepLenCoder + kNumLenProbs)
  291. #if Literal != LZMA_BASE_SIZE
  292. StopCompilingDueBUG
  293. #endif
  294. int LzmaDecodeProperties(CLzmaProperties *propsRes, const unsigned char *propsData, int size)
  295. {
  296. unsigned char prop0;
  297. if (size < LZMA_PROPERTIES_SIZE)
  298. return LZMA_RESULT_DATA_ERROR;
  299. prop0 = propsData[0];
  300. if (prop0 >= (9 * 5 * 5))
  301. return LZMA_RESULT_DATA_ERROR;
  302. {
  303. for (propsRes->pb = 0; prop0 >= (9 * 5); propsRes->pb++, prop0 -= (9 * 5));
  304. for (propsRes->lp = 0; prop0 >= 9; propsRes->lp++, prop0 -= 9);
  305. propsRes->lc = prop0;
  306. /*
  307. unsigned char remainder = (unsigned char)(prop0 / 9);
  308. propsRes->lc = prop0 % 9;
  309. propsRes->pb = remainder / 5;
  310. propsRes->lp = remainder % 5;
  311. */
  312. }
  313. #ifdef _LZMA_OUT_READ
  314. {
  315. int i;
  316. propsRes->DictionarySize = 0;
  317. for (i = 0; i < 4; i++)
  318. propsRes->DictionarySize += (UInt32)(propsData[1 + i]) << (i * 8);
  319. if (propsRes->DictionarySize == 0)
  320. propsRes->DictionarySize = 1;
  321. }
  322. #endif
  323. return LZMA_RESULT_OK;
  324. }
  325. #define kLzmaStreamWasFinishedId (-1)
  326. int LzmaDecode(CLzmaDecoderState *vs,
  327. #ifdef _LZMA_IN_CB
  328. ILzmaInCallback *InCallback,
  329. #else
  330. const unsigned char *inStream, SizeT inSize, SizeT *inSizeProcessed,
  331. #endif
  332. unsigned char *outStream, SizeT outSize, SizeT *outSizeProcessed)
  333. {
  334. CProb *p = vs->Probs;
  335. SizeT nowPos = 0;
  336. Byte previousByte = 0;
  337. UInt32 posStateMask = (1 << (vs->Properties.pb)) - 1;
  338. UInt32 literalPosMask = (1 << (vs->Properties.lp)) - 1;
  339. int lc = vs->Properties.lc;
  340. CRangeDecoder rd;
  341. #ifdef _LZMA_OUT_READ
  342. int state = vs->State;
  343. UInt32 rep0 = vs->Reps[0], rep1 = vs->Reps[1], rep2 = vs->Reps[2], rep3 = vs->Reps[3];
  344. int len = vs->RemainLen;
  345. UInt32 globalPos = vs->GlobalPos;
  346. UInt32 distanceLimit = vs->DistanceLimit;
  347. Byte *dictionary = vs->Dictionary;
  348. UInt32 dictionarySize = vs->Properties.DictionarySize;
  349. UInt32 dictionaryPos = vs->DictionaryPos;
  350. Byte tempDictionary[4];
  351. rd.Range = vs->Range;
  352. rd.Code = vs->Code;
  353. #ifdef _LZMA_IN_CB
  354. rd.InCallback = InCallback;
  355. rd.Buffer = vs->Buffer;
  356. rd.BufferLim = vs->BufferLim;
  357. #else
  358. rd.Buffer = inStream;
  359. rd.BufferLim = inStream + inSize;
  360. #endif
  361. #ifndef _LZMA_IN_CB
  362. *inSizeProcessed = 0;
  363. #endif
  364. *outSizeProcessed = 0;
  365. if (len == kLzmaStreamWasFinishedId)
  366. return LZMA_RESULT_OK;
  367. if (dictionarySize == 0)
  368. {
  369. dictionary = tempDictionary;
  370. dictionarySize = 1;
  371. tempDictionary[0] = vs->TempDictionary[0];
  372. }
  373. if (len == kLzmaNeedInitId)
  374. {
  375. {
  376. UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + vs->Properties.lp));
  377. UInt32 i;
  378. for (i = 0; i < numProbs; i++)
  379. p[i] = kBitModelTotal >> 1;
  380. rep0 = rep1 = rep2 = rep3 = 1;
  381. state = 0;
  382. globalPos = 0;
  383. distanceLimit = 0;
  384. dictionaryPos = 0;
  385. dictionary[dictionarySize - 1] = 0;
  386. RangeDecoderInit(&rd
  387. #ifndef _LZMA_IN_CB
  388. , inStream, inSize
  389. #endif
  390. );
  391. #ifdef _LZMA_IN_CB
  392. if (rd.Result != LZMA_RESULT_OK)
  393. return rd.Result;
  394. #endif
  395. if (rd.ExtraBytes != 0)
  396. return LZMA_RESULT_DATA_ERROR;
  397. }
  398. len = 0;
  399. }
  400. while(len != 0 && nowPos < outSize)
  401. {
  402. UInt32 pos = dictionaryPos - rep0;
  403. if (pos >= dictionarySize)
  404. pos += dictionarySize;
  405. outStream[nowPos++] = dictionary[dictionaryPos] = dictionary[pos];
  406. if (++dictionaryPos == dictionarySize)
  407. dictionaryPos = 0;
  408. len--;
  409. }
  410. if (dictionaryPos == 0)
  411. previousByte = dictionary[dictionarySize - 1];
  412. else
  413. previousByte = dictionary[dictionaryPos - 1];
  414. #ifdef _LZMA_IN_CB
  415. rd.Result = LZMA_RESULT_OK;
  416. #endif
  417. rd.ExtraBytes = 0;
  418. #else /* if !_LZMA_OUT_READ */
  419. int state = 0;
  420. UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
  421. int len = 0;
  422. #ifndef _LZMA_IN_CB
  423. *inSizeProcessed = 0;
  424. #endif
  425. *outSizeProcessed = 0;
  426. {
  427. UInt32 i;
  428. UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + vs->Properties.lp));
  429. for (i = 0; i < numProbs; i++)
  430. p[i] = kBitModelTotal >> 1;
  431. }
  432. #ifdef _LZMA_IN_CB
  433. rd.InCallback = InCallback;
  434. #endif
  435. RangeDecoderInit(&rd
  436. #ifndef _LZMA_IN_CB
  437. , inStream, inSize
  438. #endif
  439. );
  440. #ifdef _LZMA_IN_CB
  441. if (rd.Result != LZMA_RESULT_OK)
  442. return rd.Result;
  443. #endif
  444. if (rd.ExtraBytes != 0)
  445. return LZMA_RESULT_DATA_ERROR;
  446. #endif /* _LZMA_OUT_READ */
  447. while(nowPos < outSize)
  448. {
  449. int posState = (int)(
  450. (nowPos
  451. #ifdef _LZMA_OUT_READ
  452. + globalPos
  453. #endif
  454. )
  455. & posStateMask);
  456. #ifdef _LZMA_IN_CB
  457. if (rd.Result != LZMA_RESULT_OK)
  458. return rd.Result;
  459. #endif
  460. if (rd.ExtraBytes != 0)
  461. return LZMA_RESULT_DATA_ERROR;
  462. if (RangeDecoderBitDecode(p + IsMatch + (state << kNumPosBitsMax) + posState, &rd) == 0)
  463. {
  464. CProb *probs = p + Literal + (LZMA_LIT_SIZE *
  465. (((
  466. (nowPos
  467. #ifdef _LZMA_OUT_READ
  468. + globalPos
  469. #endif
  470. )
  471. & literalPosMask) << lc) + (previousByte >> (8 - lc))));
  472. if (state >= kNumLitStates)
  473. {
  474. Byte matchByte;
  475. #ifdef _LZMA_OUT_READ
  476. UInt32 pos = dictionaryPos - rep0;
  477. if (pos >= dictionarySize)
  478. pos += dictionarySize;
  479. matchByte = dictionary[pos];
  480. #else
  481. matchByte = outStream[nowPos - rep0];
  482. #endif
  483. previousByte = LzmaLiteralDecodeMatch(probs, &rd, matchByte);
  484. }
  485. else
  486. previousByte = LzmaLiteralDecode(probs, &rd);
  487. outStream[nowPos++] = previousByte;
  488. #ifdef _LZMA_OUT_READ
  489. if (distanceLimit < dictionarySize)
  490. distanceLimit++;
  491. dictionary[dictionaryPos] = previousByte;
  492. if (++dictionaryPos == dictionarySize)
  493. dictionaryPos = 0;
  494. #endif
  495. if (state < 4) state = 0;
  496. else if (state < 10) state -= 3;
  497. else state -= 6;
  498. }
  499. else
  500. {
  501. if (RangeDecoderBitDecode(p + IsRep + state, &rd) == 1)
  502. {
  503. if (RangeDecoderBitDecode(p + IsRepG0 + state, &rd) == 0)
  504. {
  505. if (RangeDecoderBitDecode(p + IsRep0Long + (state << kNumPosBitsMax) + posState, &rd) == 0)
  506. {
  507. #ifdef _LZMA_OUT_READ
  508. UInt32 pos;
  509. #endif
  510. #ifdef _LZMA_OUT_READ
  511. if (distanceLimit == 0)
  512. #else
  513. if (nowPos == 0)
  514. #endif
  515. return LZMA_RESULT_DATA_ERROR;
  516. state = state < 7 ? 9 : 11;
  517. #ifdef _LZMA_OUT_READ
  518. pos = dictionaryPos - rep0;
  519. if (pos >= dictionarySize)
  520. pos += dictionarySize;
  521. previousByte = dictionary[pos];
  522. dictionary[dictionaryPos] = previousByte;
  523. if (++dictionaryPos == dictionarySize)
  524. dictionaryPos = 0;
  525. #else
  526. previousByte = outStream[nowPos - rep0];
  527. #endif
  528. outStream[nowPos++] = previousByte;
  529. #ifdef _LZMA_OUT_READ
  530. if (distanceLimit < dictionarySize)
  531. distanceLimit++;
  532. #endif
  533. continue;
  534. }
  535. }
  536. else
  537. {
  538. UInt32 distance;
  539. if(RangeDecoderBitDecode(p + IsRepG1 + state, &rd) == 0)
  540. distance = rep1;
  541. else
  542. {
  543. if(RangeDecoderBitDecode(p + IsRepG2 + state, &rd) == 0)
  544. distance = rep2;
  545. else
  546. {
  547. distance = rep3;
  548. rep3 = rep2;
  549. }
  550. rep2 = rep1;
  551. }
  552. rep1 = rep0;
  553. rep0 = distance;
  554. }
  555. len = LzmaLenDecode(p + RepLenCoder, &rd, posState);
  556. state = state < 7 ? 8 : 11;
  557. }
  558. else
  559. {
  560. int posSlot;
  561. rep3 = rep2;
  562. rep2 = rep1;
  563. rep1 = rep0;
  564. state = state < 7 ? 7 : 10;
  565. len = LzmaLenDecode(p + LenCoder, &rd, posState);
  566. posSlot = RangeDecoderBitTreeDecode(p + PosSlot +
  567. ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
  568. kNumPosSlotBits), kNumPosSlotBits, &rd);
  569. if (posSlot >= kStartPosModelIndex)
  570. {
  571. int numDirectBits = ((posSlot >> 1) - 1);
  572. rep0 = ((2 | ((UInt32)posSlot & 1)) << numDirectBits);
  573. if (posSlot < kEndPosModelIndex)
  574. {
  575. rep0 += RangeDecoderReverseBitTreeDecode(
  576. p + SpecPos + rep0 - posSlot - 1, numDirectBits, &rd);
  577. }
  578. else
  579. {
  580. rep0 += RangeDecoderDecodeDirectBits(&rd,
  581. numDirectBits - kNumAlignBits) << kNumAlignBits;
  582. rep0 += RangeDecoderReverseBitTreeDecode(p + Align, kNumAlignBits, &rd);
  583. }
  584. }
  585. else
  586. rep0 = posSlot;
  587. if (++rep0 == (UInt32)(0))
  588. {
  589. /* it's for stream version */
  590. len = kLzmaStreamWasFinishedId;
  591. break;
  592. }
  593. }
  594. len += kMatchMinLen;
  595. #ifdef _LZMA_OUT_READ
  596. if (rep0 > distanceLimit)
  597. #else
  598. if (rep0 > nowPos)
  599. #endif
  600. return LZMA_RESULT_DATA_ERROR;
  601. #ifdef _LZMA_OUT_READ
  602. if (dictionarySize - distanceLimit > (UInt32)len)
  603. distanceLimit += len;
  604. else
  605. distanceLimit = dictionarySize;
  606. #endif
  607. do
  608. {
  609. #ifdef _LZMA_OUT_READ
  610. UInt32 pos = dictionaryPos - rep0;
  611. if (pos >= dictionarySize)
  612. pos += dictionarySize;
  613. previousByte = dictionary[pos];
  614. dictionary[dictionaryPos] = previousByte;
  615. if (++dictionaryPos == dictionarySize)
  616. dictionaryPos = 0;
  617. #else
  618. previousByte = outStream[nowPos - rep0];
  619. #endif
  620. len--;
  621. outStream[nowPos++] = previousByte;
  622. }
  623. while(len != 0 && nowPos < outSize);
  624. }
  625. }
  626. #ifdef _LZMA_OUT_READ
  627. vs->Range = rd.Range;
  628. vs->Code = rd.Code;
  629. vs->DictionaryPos = dictionaryPos;
  630. vs->GlobalPos = globalPos + (UInt32)nowPos;
  631. vs->DistanceLimit = distanceLimit;
  632. vs->Reps[0] = rep0;
  633. vs->Reps[1] = rep1;
  634. vs->Reps[2] = rep2;
  635. vs->Reps[3] = rep3;
  636. vs->State = state;
  637. vs->RemainLen = len;
  638. vs->TempDictionary[0] = tempDictionary[0];
  639. #endif
  640. #ifdef _LZMA_IN_CB
  641. vs->Buffer = rd.Buffer;
  642. vs->BufferLim = rd.BufferLim;
  643. #else
  644. *inSizeProcessed = (SizeT)(rd.Buffer - inStream);
  645. #endif
  646. *outSizeProcessed = nowPos;
  647. return LZMA_RESULT_OK;
  648. }