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