LzmaDecode.c.svn-base 17 KB

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