Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1220 lines
28 KiB

  1. //========= Copyright � 1996-2007, Valve Corporation, All rights reserved. ============//
  2. //
  3. // LZMA Codec.
  4. //
  5. // LZMA SDK 4.43 Copyright (c) 1999-2006 Igor Pavlov (2006-05-01)
  6. // http://www.7-zip.org/
  7. //
  8. //=====================================================================================//
  9. #include "tier0/platform.h"
  10. #include "tier0/dbg.h"
  11. #include "tier1/lzmaDecoder.h"
  12. // memdbgon must be the last include file in a .cpp file!!!
  13. #include "tier0/memdbgon.h"
  14. #ifndef _7ZIP_BYTE_DEFINED
  15. #define _7ZIP_BYTE_DEFINED
  16. typedef unsigned char Byte;
  17. #endif
  18. #ifndef _7ZIP_UINT16_DEFINED
  19. #define _7ZIP_UINT16_DEFINED
  20. typedef unsigned short UInt16;
  21. #endif
  22. #ifndef _7ZIP_UINT32_DEFINED
  23. #define _7ZIP_UINT32_DEFINED
  24. #ifdef _LZMA_UINT32_IS_ULONG
  25. typedef unsigned long UInt32;
  26. #else
  27. typedef unsigned int UInt32;
  28. #endif
  29. #endif
  30. /* #define _LZMA_SYSTEM_SIZE_T */
  31. /* Use system's size_t. You can use it to enable 64-bit sizes supporting */
  32. #ifndef _7ZIP_SIZET_DEFINED
  33. #define _7ZIP_SIZET_DEFINED
  34. #ifdef _LZMA_SYSTEM_SIZE_T
  35. #include <stddef.h>
  36. typedef size_t SizeT;
  37. #else
  38. typedef UInt32 SizeT;
  39. #endif
  40. #endif
  41. /* #define _LZMA_IN_CB */
  42. /* Use callback for input data */
  43. /* #define _LZMA_OUT_READ */
  44. /* Use read function for output data */
  45. #define _LZMA_PROB32
  46. /* It can increase speed on some 32-bit CPUs,
  47. but memory usage will be doubled in that case */
  48. /* #define _LZMA_LOC_OPT */
  49. /* Enable local speed optimizations inside code */
  50. #ifdef _LZMA_PROB32
  51. #define CProb UInt32
  52. #else
  53. #define CProb UInt16
  54. #endif
  55. #define LZMA_RESULT_OK 0
  56. #define LZMA_RESULT_DATA_ERROR 1
  57. #ifdef _LZMA_IN_CB
  58. typedef struct _ILzmaInCallback
  59. {
  60. int (*Read)(void *object, const unsigned char **buffer, SizeT *bufferSize);
  61. } ILzmaInCallback;
  62. #endif
  63. #define LZMA_BASE_SIZE 1846
  64. #define LZMA_LIT_SIZE 768
  65. #define LZMA_PROPERTIES_SIZE 5
  66. typedef struct _CLzmaProperties
  67. {
  68. int lc;
  69. int lp;
  70. int pb;
  71. #ifdef _LZMA_OUT_READ
  72. UInt32 DictionarySize;
  73. #endif
  74. }CLzmaProperties;
  75. int LzmaDecodeProperties(CLzmaProperties *propsRes, const unsigned char *propsData, int size);
  76. #define LzmaGetNumProbs(Properties) (LZMA_BASE_SIZE + (LZMA_LIT_SIZE << ((Properties)->lc + (Properties)->lp)))
  77. #define kLzmaNeedInitId (-2)
  78. typedef struct _CLzmaDecoderState
  79. {
  80. CLzmaProperties Properties;
  81. CProb *Probs;
  82. #ifdef _LZMA_IN_CB
  83. const unsigned char *Buffer;
  84. const unsigned char *BufferLim;
  85. #endif
  86. #ifdef _LZMA_OUT_READ
  87. unsigned char *Dictionary;
  88. UInt32 Range;
  89. UInt32 Code;
  90. UInt32 DictionaryPos;
  91. UInt32 GlobalPos;
  92. UInt32 DistanceLimit;
  93. UInt32 Reps[4];
  94. int State;
  95. int RemainLen;
  96. unsigned char TempDictionary[4];
  97. #endif
  98. } CLzmaDecoderState;
  99. #ifdef _LZMA_OUT_READ
  100. #define LzmaDecoderInit(vs) { (vs)->RemainLen = kLzmaNeedInitId; }
  101. #endif
  102. int LzmaDecode(CLzmaDecoderState *vs,
  103. #ifdef _LZMA_IN_CB
  104. ILzmaInCallback *inCallback,
  105. #else
  106. const unsigned char *inStream, SizeT inSize, SizeT *inSizeProcessed,
  107. #endif
  108. unsigned char *outStream, SizeT outSize, SizeT *outSizeProcessed, LZMAReadProgressCallbackFunc_t pCallbackFunc );
  109. #define kNumTopBits 24
  110. #define kTopValue ((UInt32)1 << kNumTopBits)
  111. #define kNumBitModelTotalBits 11
  112. #define kBitModelTotal (1 << kNumBitModelTotalBits)
  113. #define kNumMoveBits 5
  114. #define RC_READ_BYTE (*Buffer++)
  115. #define RC_INIT2 Code = 0; Range = 0xFFFFFFFF; \
  116. { int i; for(i = 0; i < 5; i++) { RC_TEST; Code = (Code << 8) | RC_READ_BYTE; }}
  117. #ifdef _LZMA_IN_CB
  118. #define RC_TEST { if (Buffer == BufferLim) \
  119. { SizeT size; int result = InCallback->Read(InCallback, &Buffer, &size); if (result != LZMA_RESULT_OK) return result; \
  120. BufferLim = Buffer + size; if (size == 0) return LZMA_RESULT_DATA_ERROR; }}
  121. #define RC_INIT Buffer = BufferLim = 0; RC_INIT2
  122. #else
  123. #define RC_TEST { if (Buffer == BufferLim) return LZMA_RESULT_DATA_ERROR; }
  124. #define RC_INIT(buffer, bufferSize) Buffer = buffer; BufferLim = buffer + bufferSize; RC_INIT2
  125. #endif
  126. #define RC_NORMALIZE if (Range < kTopValue) { RC_TEST; Range <<= 8; Code = (Code << 8) | RC_READ_BYTE; }
  127. #define IfBit0(p) RC_NORMALIZE; bound = (Range >> kNumBitModelTotalBits) * *(p); if (Code < bound)
  128. #define UpdateBit0(p) Range = bound; *(p) += (kBitModelTotal - *(p)) >> kNumMoveBits;
  129. #define UpdateBit1(p) Range -= bound; Code -= bound; *(p) -= (*(p)) >> kNumMoveBits;
  130. #define RC_GET_BIT2(p, mi, A0, A1) IfBit0(p) \
  131. { UpdateBit0(p); mi <<= 1; A0; } else \
  132. { UpdateBit1(p); mi = (mi + mi) + 1; A1; }
  133. #define RC_GET_BIT(p, mi) RC_GET_BIT2(p, mi, ; , ;)
  134. #define RangeDecoderBitTreeDecode(probs, numLevels, res) \
  135. { int i = numLevels; res = 1; \
  136. do { CProb *p = probs + res; RC_GET_BIT(p, res) } while(--i != 0); \
  137. res -= (1 << numLevels); }
  138. #define kNumPosBitsMax 4
  139. #define kNumPosStatesMax (1 << kNumPosBitsMax)
  140. #define kLenNumLowBits 3
  141. #define kLenNumLowSymbols (1 << kLenNumLowBits)
  142. #define kLenNumMidBits 3
  143. #define kLenNumMidSymbols (1 << kLenNumMidBits)
  144. #define kLenNumHighBits 8
  145. #define kLenNumHighSymbols (1 << kLenNumHighBits)
  146. #define LenChoice 0
  147. #define LenChoice2 (LenChoice + 1)
  148. #define LenLow (LenChoice2 + 1)
  149. #define LenMid (LenLow + (kNumPosStatesMax << kLenNumLowBits))
  150. #define LenHigh (LenMid + (kNumPosStatesMax << kLenNumMidBits))
  151. #define kNumLenProbs (LenHigh + kLenNumHighSymbols)
  152. #define kNumStates 12
  153. #define kNumLitStates 7
  154. #define kStartPosModelIndex 4
  155. #define kEndPosModelIndex 14
  156. #define kNumFullDistances (1 << (kEndPosModelIndex >> 1))
  157. #define kNumPosSlotBits 6
  158. #define kNumLenToPosStates 4
  159. #define kNumAlignBits 4
  160. #define kAlignTableSize (1 << kNumAlignBits)
  161. #define kMatchMinLen 2
  162. #define IsMatch 0
  163. #define IsRep (IsMatch + (kNumStates << kNumPosBitsMax))
  164. #define IsRepG0 (IsRep + kNumStates)
  165. #define IsRepG1 (IsRepG0 + kNumStates)
  166. #define IsRepG2 (IsRepG1 + kNumStates)
  167. #define IsRep0Long (IsRepG2 + kNumStates)
  168. #define PosSlot (IsRep0Long + (kNumStates << kNumPosBitsMax))
  169. #define SpecPos (PosSlot + (kNumLenToPosStates << kNumPosSlotBits))
  170. #define Align (SpecPos + kNumFullDistances - kEndPosModelIndex)
  171. #define LenCoder (Align + kAlignTableSize)
  172. #define RepLenCoder (LenCoder + kNumLenProbs)
  173. #define Literal (RepLenCoder + kNumLenProbs)
  174. #if Literal != LZMA_BASE_SIZE
  175. StopCompilingDueBUG
  176. #endif
  177. int LzmaDecodeProperties(CLzmaProperties *propsRes, const unsigned char *propsData, int size)
  178. {
  179. unsigned char prop0;
  180. if (size < LZMA_PROPERTIES_SIZE)
  181. return LZMA_RESULT_DATA_ERROR;
  182. prop0 = propsData[0];
  183. if (prop0 >= (9 * 5 * 5))
  184. return LZMA_RESULT_DATA_ERROR;
  185. {
  186. for (propsRes->pb = 0; prop0 >= (9 * 5); propsRes->pb++, prop0 -= (9 * 5));
  187. for (propsRes->lp = 0; prop0 >= 9; propsRes->lp++, prop0 -= 9);
  188. propsRes->lc = prop0;
  189. /*
  190. unsigned char remainder = (unsigned char)(prop0 / 9);
  191. propsRes->lc = prop0 % 9;
  192. propsRes->pb = remainder / 5;
  193. propsRes->lp = remainder % 5;
  194. */
  195. }
  196. #ifdef _LZMA_OUT_READ
  197. {
  198. int i;
  199. propsRes->DictionarySize = 0;
  200. for (i = 0; i < 4; i++)
  201. propsRes->DictionarySize += (UInt32)(propsData[1 + i]) << (i * 8);
  202. if (propsRes->DictionarySize == 0)
  203. propsRes->DictionarySize = 1;
  204. }
  205. #endif
  206. return LZMA_RESULT_OK;
  207. }
  208. #define kLzmaStreamWasFinishedId (-1)
  209. #define READ_PROGRESS_CALLBACK_BLOCK_SIZE 500000
  210. int LzmaDecodeWithCallback(CLzmaDecoderState *vs,
  211. #ifdef _LZMA_IN_CB
  212. ILzmaInCallback *InCallback,
  213. #else
  214. const unsigned char *inStream, SizeT inSize, SizeT *inSizeProcessed,
  215. #endif
  216. unsigned char *outStream, SizeT outSize, SizeT *outSizeProcessed, LZMAReadProgressCallbackFunc_t pCallbackFunc )
  217. {
  218. CProb *p = vs->Probs;
  219. SizeT nowPos = 0;
  220. Byte previousByte = 0;
  221. UInt32 posStateMask = (1 << (vs->Properties.pb)) - 1;
  222. UInt32 literalPosMask = (1 << (vs->Properties.lp)) - 1;
  223. int lc = vs->Properties.lc;
  224. #ifdef _LZMA_OUT_READ
  225. UInt32 Range = vs->Range;
  226. UInt32 Code = vs->Code;
  227. #ifdef _LZMA_IN_CB
  228. const Byte *Buffer = vs->Buffer;
  229. const Byte *BufferLim = vs->BufferLim;
  230. #else
  231. const Byte *Buffer = inStream;
  232. const Byte *BufferLim = inStream + inSize;
  233. #endif
  234. int state = vs->State;
  235. UInt32 rep0 = vs->Reps[0], rep1 = vs->Reps[1], rep2 = vs->Reps[2], rep3 = vs->Reps[3];
  236. int len = vs->RemainLen;
  237. UInt32 globalPos = vs->GlobalPos;
  238. UInt32 distanceLimit = vs->DistanceLimit;
  239. Byte *dictionary = vs->Dictionary;
  240. UInt32 dictionarySize = vs->Properties.DictionarySize;
  241. UInt32 dictionaryPos = vs->DictionaryPos;
  242. Byte tempDictionary[4];
  243. #ifndef _LZMA_IN_CB
  244. *inSizeProcessed = 0;
  245. #endif
  246. *outSizeProcessed = 0;
  247. if (len == kLzmaStreamWasFinishedId)
  248. return LZMA_RESULT_OK;
  249. if (dictionarySize == 0)
  250. {
  251. dictionary = tempDictionary;
  252. dictionarySize = 1;
  253. tempDictionary[0] = vs->TempDictionary[0];
  254. }
  255. if (len == kLzmaNeedInitId)
  256. {
  257. {
  258. UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + vs->Properties.lp));
  259. UInt32 i;
  260. for (i = 0; i < numProbs; i++)
  261. p[i] = kBitModelTotal >> 1;
  262. rep0 = rep1 = rep2 = rep3 = 1;
  263. state = 0;
  264. globalPos = 0;
  265. distanceLimit = 0;
  266. dictionaryPos = 0;
  267. dictionary[dictionarySize - 1] = 0;
  268. #ifdef _LZMA_IN_CB
  269. RC_INIT;
  270. #else
  271. RC_INIT(inStream, inSize);
  272. #endif
  273. }
  274. len = 0;
  275. }
  276. while(len != 0 && nowPos < outSize)
  277. {
  278. UInt32 pos = dictionaryPos - rep0;
  279. if (pos >= dictionarySize)
  280. pos += dictionarySize;
  281. outStream[nowPos++] = dictionary[dictionaryPos] = dictionary[pos];
  282. if ( ( nowPos % READ_PROGRESS_CALLBACK_BLOCK_SIZE ) == 0 )
  283. {
  284. pCallbackFunc();
  285. }
  286. if (++dictionaryPos == dictionarySize)
  287. dictionaryPos = 0;
  288. len--;
  289. }
  290. if (dictionaryPos == 0)
  291. previousByte = dictionary[dictionarySize - 1];
  292. else
  293. previousByte = dictionary[dictionaryPos - 1];
  294. #else /* if !_LZMA_OUT_READ */
  295. int state = 0;
  296. UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
  297. int len = 0;
  298. const Byte *Buffer;
  299. const Byte *BufferLim;
  300. UInt32 Range;
  301. UInt32 Code;
  302. #ifndef _LZMA_IN_CB
  303. *inSizeProcessed = 0;
  304. #endif
  305. *outSizeProcessed = 0;
  306. {
  307. UInt32 i;
  308. UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + vs->Properties.lp));
  309. for (i = 0; i < numProbs; i++)
  310. p[i] = kBitModelTotal >> 1;
  311. }
  312. #ifdef _LZMA_IN_CB
  313. RC_INIT;
  314. #else
  315. RC_INIT(inStream, inSize);
  316. #endif
  317. #endif /* _LZMA_OUT_READ */
  318. while(nowPos < outSize)
  319. {
  320. CProb *prob;
  321. UInt32 bound;
  322. int posState = (int)(
  323. (nowPos
  324. #ifdef _LZMA_OUT_READ
  325. + globalPos
  326. #endif
  327. )
  328. & posStateMask);
  329. prob = p + IsMatch + (state << kNumPosBitsMax) + posState;
  330. IfBit0(prob)
  331. {
  332. int symbol = 1;
  333. UpdateBit0(prob)
  334. prob = p + Literal + (LZMA_LIT_SIZE *
  335. (((
  336. (nowPos
  337. #ifdef _LZMA_OUT_READ
  338. + globalPos
  339. #endif
  340. )
  341. & literalPosMask) << lc) + (previousByte >> (8 - lc))));
  342. if (state >= kNumLitStates)
  343. {
  344. int matchByte;
  345. #ifdef _LZMA_OUT_READ
  346. UInt32 pos = dictionaryPos - rep0;
  347. if (pos >= dictionarySize)
  348. pos += dictionarySize;
  349. matchByte = dictionary[pos];
  350. #else
  351. matchByte = outStream[nowPos - rep0];
  352. #endif
  353. do
  354. {
  355. int bit;
  356. CProb *probLit;
  357. matchByte <<= 1;
  358. bit = (matchByte & 0x100);
  359. probLit = prob + 0x100 + bit + symbol;
  360. RC_GET_BIT2(probLit, symbol, if (bit != 0) break, if (bit == 0) break)
  361. }
  362. while (symbol < 0x100);
  363. }
  364. while (symbol < 0x100)
  365. {
  366. CProb *probLit = prob + symbol;
  367. RC_GET_BIT(probLit, symbol)
  368. }
  369. previousByte = (Byte)symbol;
  370. outStream[nowPos++] = previousByte;
  371. if ( ( nowPos % READ_PROGRESS_CALLBACK_BLOCK_SIZE ) == 0 )
  372. {
  373. pCallbackFunc();
  374. }
  375. #ifdef _LZMA_OUT_READ
  376. if (distanceLimit < dictionarySize)
  377. distanceLimit++;
  378. dictionary[dictionaryPos] = previousByte;
  379. if (++dictionaryPos == dictionarySize)
  380. dictionaryPos = 0;
  381. #endif
  382. if (state < 4) state = 0;
  383. else if (state < 10) state -= 3;
  384. else state -= 6;
  385. }
  386. else
  387. {
  388. UpdateBit1(prob);
  389. prob = p + IsRep + state;
  390. IfBit0(prob)
  391. {
  392. UpdateBit0(prob);
  393. rep3 = rep2;
  394. rep2 = rep1;
  395. rep1 = rep0;
  396. state = state < kNumLitStates ? 0 : 3;
  397. prob = p + LenCoder;
  398. }
  399. else
  400. {
  401. UpdateBit1(prob);
  402. prob = p + IsRepG0 + state;
  403. IfBit0(prob)
  404. {
  405. UpdateBit0(prob);
  406. prob = p + IsRep0Long + (state << kNumPosBitsMax) + posState;
  407. IfBit0(prob)
  408. {
  409. #ifdef _LZMA_OUT_READ
  410. UInt32 pos;
  411. #endif
  412. UpdateBit0(prob);
  413. #ifdef _LZMA_OUT_READ
  414. if (distanceLimit == 0)
  415. #else
  416. if (nowPos == 0)
  417. #endif
  418. return LZMA_RESULT_DATA_ERROR;
  419. state = state < kNumLitStates ? 9 : 11;
  420. #ifdef _LZMA_OUT_READ
  421. pos = dictionaryPos - rep0;
  422. if (pos >= dictionarySize)
  423. pos += dictionarySize;
  424. previousByte = dictionary[pos];
  425. dictionary[dictionaryPos] = previousByte;
  426. if (++dictionaryPos == dictionarySize)
  427. dictionaryPos = 0;
  428. #else
  429. previousByte = outStream[nowPos - rep0];
  430. #endif
  431. outStream[nowPos++] = previousByte;
  432. if ( ( nowPos % READ_PROGRESS_CALLBACK_BLOCK_SIZE ) == 0 )
  433. {
  434. pCallbackFunc();
  435. }
  436. #ifdef _LZMA_OUT_READ
  437. if (distanceLimit < dictionarySize)
  438. distanceLimit++;
  439. #endif
  440. continue;
  441. }
  442. else
  443. {
  444. UpdateBit1(prob);
  445. }
  446. }
  447. else
  448. {
  449. UInt32 distance;
  450. UpdateBit1(prob);
  451. prob = p + IsRepG1 + state;
  452. IfBit0(prob)
  453. {
  454. UpdateBit0(prob);
  455. distance = rep1;
  456. }
  457. else
  458. {
  459. UpdateBit1(prob);
  460. prob = p + IsRepG2 + state;
  461. IfBit0(prob)
  462. {
  463. UpdateBit0(prob);
  464. distance = rep2;
  465. }
  466. else
  467. {
  468. UpdateBit1(prob);
  469. distance = rep3;
  470. rep3 = rep2;
  471. }
  472. rep2 = rep1;
  473. }
  474. rep1 = rep0;
  475. rep0 = distance;
  476. }
  477. state = state < kNumLitStates ? 8 : 11;
  478. prob = p + RepLenCoder;
  479. }
  480. {
  481. int numBits, offset;
  482. CProb *probLen = prob + LenChoice;
  483. IfBit0(probLen)
  484. {
  485. UpdateBit0(probLen);
  486. probLen = prob + LenLow + (posState << kLenNumLowBits);
  487. offset = 0;
  488. numBits = kLenNumLowBits;
  489. }
  490. else
  491. {
  492. UpdateBit1(probLen);
  493. probLen = prob + LenChoice2;
  494. IfBit0(probLen)
  495. {
  496. UpdateBit0(probLen);
  497. probLen = prob + LenMid + (posState << kLenNumMidBits);
  498. offset = kLenNumLowSymbols;
  499. numBits = kLenNumMidBits;
  500. }
  501. else
  502. {
  503. UpdateBit1(probLen);
  504. probLen = prob + LenHigh;
  505. offset = kLenNumLowSymbols + kLenNumMidSymbols;
  506. numBits = kLenNumHighBits;
  507. }
  508. }
  509. RangeDecoderBitTreeDecode(probLen, numBits, len);
  510. len += offset;
  511. }
  512. if (state < 4)
  513. {
  514. int posSlot;
  515. state += kNumLitStates;
  516. prob = p + PosSlot +
  517. ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
  518. kNumPosSlotBits);
  519. RangeDecoderBitTreeDecode(prob, kNumPosSlotBits, posSlot);
  520. if (posSlot >= kStartPosModelIndex)
  521. {
  522. int numDirectBits = ((posSlot >> 1) - 1);
  523. rep0 = (2 | ((UInt32)posSlot & 1));
  524. if (posSlot < kEndPosModelIndex)
  525. {
  526. rep0 <<= numDirectBits;
  527. prob = p + SpecPos + rep0 - posSlot - 1;
  528. }
  529. else
  530. {
  531. numDirectBits -= kNumAlignBits;
  532. do
  533. {
  534. RC_NORMALIZE
  535. Range >>= 1;
  536. rep0 <<= 1;
  537. if (Code >= Range)
  538. {
  539. Code -= Range;
  540. rep0 |= 1;
  541. }
  542. }
  543. while (--numDirectBits != 0);
  544. prob = p + Align;
  545. rep0 <<= kNumAlignBits;
  546. numDirectBits = kNumAlignBits;
  547. }
  548. {
  549. int i = 1;
  550. int mi = 1;
  551. do
  552. {
  553. CProb *prob3 = prob + mi;
  554. RC_GET_BIT2(prob3, mi, ; , rep0 |= i);
  555. i <<= 1;
  556. }
  557. while(--numDirectBits != 0);
  558. }
  559. }
  560. else
  561. rep0 = posSlot;
  562. if (++rep0 == (UInt32)(0))
  563. {
  564. /* it's for stream version */
  565. len = kLzmaStreamWasFinishedId;
  566. break;
  567. }
  568. }
  569. len += kMatchMinLen;
  570. #ifdef _LZMA_OUT_READ
  571. if (rep0 > distanceLimit)
  572. #else
  573. if (rep0 > nowPos)
  574. #endif
  575. return LZMA_RESULT_DATA_ERROR;
  576. #ifdef _LZMA_OUT_READ
  577. if (dictionarySize - distanceLimit > (UInt32)len)
  578. distanceLimit += len;
  579. else
  580. distanceLimit = dictionarySize;
  581. #endif
  582. do
  583. {
  584. #ifdef _LZMA_OUT_READ
  585. UInt32 pos = dictionaryPos - rep0;
  586. if (pos >= dictionarySize)
  587. pos += dictionarySize;
  588. previousByte = dictionary[pos];
  589. dictionary[dictionaryPos] = previousByte;
  590. if (++dictionaryPos == dictionarySize)
  591. dictionaryPos = 0;
  592. #else
  593. previousByte = outStream[nowPos - rep0];
  594. #endif
  595. len--;
  596. outStream[nowPos++] = previousByte;
  597. if ( ( nowPos % READ_PROGRESS_CALLBACK_BLOCK_SIZE ) == 0 )
  598. {
  599. pCallbackFunc();
  600. }
  601. }
  602. while(len != 0 && nowPos < outSize);
  603. }
  604. }
  605. RC_NORMALIZE;
  606. #ifdef _LZMA_OUT_READ
  607. vs->Range = Range;
  608. vs->Code = Code;
  609. vs->DictionaryPos = dictionaryPos;
  610. vs->GlobalPos = globalPos + (UInt32)nowPos;
  611. vs->DistanceLimit = distanceLimit;
  612. vs->Reps[0] = rep0;
  613. vs->Reps[1] = rep1;
  614. vs->Reps[2] = rep2;
  615. vs->Reps[3] = rep3;
  616. vs->State = state;
  617. vs->RemainLen = len;
  618. vs->TempDictionary[0] = tempDictionary[0];
  619. #endif
  620. #ifdef _LZMA_IN_CB
  621. vs->Buffer = Buffer;
  622. vs->BufferLim = BufferLim;
  623. #else
  624. *inSizeProcessed = (SizeT)(Buffer - inStream);
  625. #endif
  626. *outSizeProcessed = nowPos;
  627. return LZMA_RESULT_OK;
  628. }
  629. int LzmaDecode(CLzmaDecoderState *vs,
  630. #ifdef _LZMA_IN_CB
  631. ILzmaInCallback *InCallback,
  632. #else
  633. const unsigned char *inStream, SizeT inSize, SizeT *inSizeProcessed,
  634. #endif
  635. unsigned char *outStream, SizeT outSize, SizeT *outSizeProcessed, LZMAReadProgressCallbackFunc_t pCallbackFunc )
  636. {
  637. if ( pCallbackFunc )
  638. {
  639. // Call a different version that does checks for when to callback so that we don't take a perf hit on this version without.
  640. return LzmaDecodeWithCallback( vs, inStream, inSize, inSizeProcessed, outStream, outSize, outSizeProcessed, pCallbackFunc );
  641. }
  642. CProb *p = vs->Probs;
  643. SizeT nowPos = 0;
  644. Byte previousByte = 0;
  645. UInt32 posStateMask = (1 << (vs->Properties.pb)) - 1;
  646. UInt32 literalPosMask = (1 << (vs->Properties.lp)) - 1;
  647. int lc = vs->Properties.lc;
  648. #ifdef _LZMA_OUT_READ
  649. UInt32 Range = vs->Range;
  650. UInt32 Code = vs->Code;
  651. #ifdef _LZMA_IN_CB
  652. const Byte *Buffer = vs->Buffer;
  653. const Byte *BufferLim = vs->BufferLim;
  654. #else
  655. const Byte *Buffer = inStream;
  656. const Byte *BufferLim = inStream + inSize;
  657. #endif
  658. int state = vs->State;
  659. UInt32 rep0 = vs->Reps[0], rep1 = vs->Reps[1], rep2 = vs->Reps[2], rep3 = vs->Reps[3];
  660. int len = vs->RemainLen;
  661. UInt32 globalPos = vs->GlobalPos;
  662. UInt32 distanceLimit = vs->DistanceLimit;
  663. Byte *dictionary = vs->Dictionary;
  664. UInt32 dictionarySize = vs->Properties.DictionarySize;
  665. UInt32 dictionaryPos = vs->DictionaryPos;
  666. Byte tempDictionary[4];
  667. #ifndef _LZMA_IN_CB
  668. *inSizeProcessed = 0;
  669. #endif
  670. *outSizeProcessed = 0;
  671. if (len == kLzmaStreamWasFinishedId)
  672. return LZMA_RESULT_OK;
  673. if (dictionarySize == 0)
  674. {
  675. dictionary = tempDictionary;
  676. dictionarySize = 1;
  677. tempDictionary[0] = vs->TempDictionary[0];
  678. }
  679. if (len == kLzmaNeedInitId)
  680. {
  681. {
  682. UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + vs->Properties.lp));
  683. UInt32 i;
  684. for (i = 0; i < numProbs; i++)
  685. p[i] = kBitModelTotal >> 1;
  686. rep0 = rep1 = rep2 = rep3 = 1;
  687. state = 0;
  688. globalPos = 0;
  689. distanceLimit = 0;
  690. dictionaryPos = 0;
  691. dictionary[dictionarySize - 1] = 0;
  692. #ifdef _LZMA_IN_CB
  693. RC_INIT;
  694. #else
  695. RC_INIT(inStream, inSize);
  696. #endif
  697. }
  698. len = 0;
  699. }
  700. while(len != 0 && nowPos < outSize)
  701. {
  702. UInt32 pos = dictionaryPos - rep0;
  703. if (pos >= dictionarySize)
  704. pos += dictionarySize;
  705. outStream[nowPos++] = dictionary[dictionaryPos] = dictionary[pos];
  706. if (++dictionaryPos == dictionarySize)
  707. dictionaryPos = 0;
  708. len--;
  709. }
  710. if (dictionaryPos == 0)
  711. previousByte = dictionary[dictionarySize - 1];
  712. else
  713. previousByte = dictionary[dictionaryPos - 1];
  714. #else /* if !_LZMA_OUT_READ */
  715. int state = 0;
  716. UInt32 rep0 = 1, rep1 = 1, rep2 = 1, rep3 = 1;
  717. int len = 0;
  718. const Byte *Buffer;
  719. const Byte *BufferLim;
  720. UInt32 Range;
  721. UInt32 Code;
  722. #ifndef _LZMA_IN_CB
  723. *inSizeProcessed = 0;
  724. #endif
  725. *outSizeProcessed = 0;
  726. {
  727. UInt32 i;
  728. UInt32 numProbs = Literal + ((UInt32)LZMA_LIT_SIZE << (lc + vs->Properties.lp));
  729. for (i = 0; i < numProbs; i++)
  730. p[i] = kBitModelTotal >> 1;
  731. }
  732. #ifdef _LZMA_IN_CB
  733. RC_INIT;
  734. #else
  735. RC_INIT(inStream, inSize);
  736. #endif
  737. #endif /* _LZMA_OUT_READ */
  738. while(nowPos < outSize)
  739. {
  740. CProb *prob;
  741. UInt32 bound;
  742. int posState = (int)(
  743. (nowPos
  744. #ifdef _LZMA_OUT_READ
  745. + globalPos
  746. #endif
  747. )
  748. & posStateMask);
  749. prob = p + IsMatch + (state << kNumPosBitsMax) + posState;
  750. IfBit0(prob)
  751. {
  752. int symbol = 1;
  753. UpdateBit0(prob)
  754. prob = p + Literal + (LZMA_LIT_SIZE *
  755. (((
  756. (nowPos
  757. #ifdef _LZMA_OUT_READ
  758. + globalPos
  759. #endif
  760. )
  761. & literalPosMask) << lc) + (previousByte >> (8 - lc))));
  762. if (state >= kNumLitStates)
  763. {
  764. int matchByte;
  765. #ifdef _LZMA_OUT_READ
  766. UInt32 pos = dictionaryPos - rep0;
  767. if (pos >= dictionarySize)
  768. pos += dictionarySize;
  769. matchByte = dictionary[pos];
  770. #else
  771. matchByte = outStream[nowPos - rep0];
  772. #endif
  773. do
  774. {
  775. int bit;
  776. CProb *probLit;
  777. matchByte <<= 1;
  778. bit = (matchByte & 0x100);
  779. probLit = prob + 0x100 + bit + symbol;
  780. RC_GET_BIT2(probLit, symbol, if (bit != 0) break, if (bit == 0) break)
  781. }
  782. while (symbol < 0x100);
  783. }
  784. while (symbol < 0x100)
  785. {
  786. CProb *probLit = prob + symbol;
  787. RC_GET_BIT(probLit, symbol)
  788. }
  789. previousByte = (Byte)symbol;
  790. outStream[nowPos++] = previousByte;
  791. #ifdef _LZMA_OUT_READ
  792. if (distanceLimit < dictionarySize)
  793. distanceLimit++;
  794. dictionary[dictionaryPos] = previousByte;
  795. if (++dictionaryPos == dictionarySize)
  796. dictionaryPos = 0;
  797. #endif
  798. if (state < 4) state = 0;
  799. else if (state < 10) state -= 3;
  800. else state -= 6;
  801. }
  802. else
  803. {
  804. UpdateBit1(prob);
  805. prob = p + IsRep + state;
  806. IfBit0(prob)
  807. {
  808. UpdateBit0(prob);
  809. rep3 = rep2;
  810. rep2 = rep1;
  811. rep1 = rep0;
  812. state = state < kNumLitStates ? 0 : 3;
  813. prob = p + LenCoder;
  814. }
  815. else
  816. {
  817. UpdateBit1(prob);
  818. prob = p + IsRepG0 + state;
  819. IfBit0(prob)
  820. {
  821. UpdateBit0(prob);
  822. prob = p + IsRep0Long + (state << kNumPosBitsMax) + posState;
  823. IfBit0(prob)
  824. {
  825. #ifdef _LZMA_OUT_READ
  826. UInt32 pos;
  827. #endif
  828. UpdateBit0(prob);
  829. #ifdef _LZMA_OUT_READ
  830. if (distanceLimit == 0)
  831. #else
  832. if (nowPos == 0)
  833. #endif
  834. return LZMA_RESULT_DATA_ERROR;
  835. state = state < kNumLitStates ? 9 : 11;
  836. #ifdef _LZMA_OUT_READ
  837. pos = dictionaryPos - rep0;
  838. if (pos >= dictionarySize)
  839. pos += dictionarySize;
  840. previousByte = dictionary[pos];
  841. dictionary[dictionaryPos] = previousByte;
  842. if (++dictionaryPos == dictionarySize)
  843. dictionaryPos = 0;
  844. #else
  845. previousByte = outStream[nowPos - rep0];
  846. #endif
  847. outStream[nowPos++] = previousByte;
  848. #ifdef _LZMA_OUT_READ
  849. if (distanceLimit < dictionarySize)
  850. distanceLimit++;
  851. #endif
  852. continue;
  853. }
  854. else
  855. {
  856. UpdateBit1(prob);
  857. }
  858. }
  859. else
  860. {
  861. UInt32 distance;
  862. UpdateBit1(prob);
  863. prob = p + IsRepG1 + state;
  864. IfBit0(prob)
  865. {
  866. UpdateBit0(prob);
  867. distance = rep1;
  868. }
  869. else
  870. {
  871. UpdateBit1(prob);
  872. prob = p + IsRepG2 + state;
  873. IfBit0(prob)
  874. {
  875. UpdateBit0(prob);
  876. distance = rep2;
  877. }
  878. else
  879. {
  880. UpdateBit1(prob);
  881. distance = rep3;
  882. rep3 = rep2;
  883. }
  884. rep2 = rep1;
  885. }
  886. rep1 = rep0;
  887. rep0 = distance;
  888. }
  889. state = state < kNumLitStates ? 8 : 11;
  890. prob = p + RepLenCoder;
  891. }
  892. {
  893. int numBits, offset;
  894. CProb *probLen = prob + LenChoice;
  895. IfBit0(probLen)
  896. {
  897. UpdateBit0(probLen);
  898. probLen = prob + LenLow + (posState << kLenNumLowBits);
  899. offset = 0;
  900. numBits = kLenNumLowBits;
  901. }
  902. else
  903. {
  904. UpdateBit1(probLen);
  905. probLen = prob + LenChoice2;
  906. IfBit0(probLen)
  907. {
  908. UpdateBit0(probLen);
  909. probLen = prob + LenMid + (posState << kLenNumMidBits);
  910. offset = kLenNumLowSymbols;
  911. numBits = kLenNumMidBits;
  912. }
  913. else
  914. {
  915. UpdateBit1(probLen);
  916. probLen = prob + LenHigh;
  917. offset = kLenNumLowSymbols + kLenNumMidSymbols;
  918. numBits = kLenNumHighBits;
  919. }
  920. }
  921. RangeDecoderBitTreeDecode(probLen, numBits, len);
  922. len += offset;
  923. }
  924. if (state < 4)
  925. {
  926. int posSlot;
  927. state += kNumLitStates;
  928. prob = p + PosSlot +
  929. ((len < kNumLenToPosStates ? len : kNumLenToPosStates - 1) <<
  930. kNumPosSlotBits);
  931. RangeDecoderBitTreeDecode(prob, kNumPosSlotBits, posSlot);
  932. if (posSlot >= kStartPosModelIndex)
  933. {
  934. int numDirectBits = ((posSlot >> 1) - 1);
  935. rep0 = (2 | ((UInt32)posSlot & 1));
  936. if (posSlot < kEndPosModelIndex)
  937. {
  938. rep0 <<= numDirectBits;
  939. prob = p + SpecPos + rep0 - posSlot - 1;
  940. }
  941. else
  942. {
  943. numDirectBits -= kNumAlignBits;
  944. do
  945. {
  946. RC_NORMALIZE
  947. Range >>= 1;
  948. rep0 <<= 1;
  949. if (Code >= Range)
  950. {
  951. Code -= Range;
  952. rep0 |= 1;
  953. }
  954. }
  955. while (--numDirectBits != 0);
  956. prob = p + Align;
  957. rep0 <<= kNumAlignBits;
  958. numDirectBits = kNumAlignBits;
  959. }
  960. {
  961. int i = 1;
  962. int mi = 1;
  963. do
  964. {
  965. CProb *prob3 = prob + mi;
  966. RC_GET_BIT2(prob3, mi, ; , rep0 |= i);
  967. i <<= 1;
  968. }
  969. while(--numDirectBits != 0);
  970. }
  971. }
  972. else
  973. rep0 = posSlot;
  974. if (++rep0 == (UInt32)(0))
  975. {
  976. /* it's for stream version */
  977. len = kLzmaStreamWasFinishedId;
  978. break;
  979. }
  980. }
  981. len += kMatchMinLen;
  982. #ifdef _LZMA_OUT_READ
  983. if (rep0 > distanceLimit)
  984. #else
  985. if (rep0 > nowPos)
  986. #endif
  987. return LZMA_RESULT_DATA_ERROR;
  988. #ifdef _LZMA_OUT_READ
  989. if (dictionarySize - distanceLimit > (UInt32)len)
  990. distanceLimit += len;
  991. else
  992. distanceLimit = dictionarySize;
  993. #endif
  994. do
  995. {
  996. #ifdef _LZMA_OUT_READ
  997. UInt32 pos = dictionaryPos - rep0;
  998. if (pos >= dictionarySize)
  999. pos += dictionarySize;
  1000. previousByte = dictionary[pos];
  1001. dictionary[dictionaryPos] = previousByte;
  1002. if (++dictionaryPos == dictionarySize)
  1003. dictionaryPos = 0;
  1004. #else
  1005. previousByte = outStream[nowPos - rep0];
  1006. #endif
  1007. len--;
  1008. outStream[nowPos++] = previousByte;
  1009. }
  1010. while(len != 0 && nowPos < outSize);
  1011. }
  1012. }
  1013. RC_NORMALIZE;
  1014. #ifdef _LZMA_OUT_READ
  1015. vs->Range = Range;
  1016. vs->Code = Code;
  1017. vs->DictionaryPos = dictionaryPos;
  1018. vs->GlobalPos = globalPos + (UInt32)nowPos;
  1019. vs->DistanceLimit = distanceLimit;
  1020. vs->Reps[0] = rep0;
  1021. vs->Reps[1] = rep1;
  1022. vs->Reps[2] = rep2;
  1023. vs->Reps[3] = rep3;
  1024. vs->State = state;
  1025. vs->RemainLen = len;
  1026. vs->TempDictionary[0] = tempDictionary[0];
  1027. #endif
  1028. #ifdef _LZMA_IN_CB
  1029. vs->Buffer = Buffer;
  1030. vs->BufferLim = BufferLim;
  1031. #else
  1032. *inSizeProcessed = (SizeT)(Buffer - inStream);
  1033. #endif
  1034. *outSizeProcessed = nowPos;
  1035. return LZMA_RESULT_OK;
  1036. }
  1037. //-----------------------------------------------------------------------------
  1038. // Returns true if buffer is compressed.
  1039. //-----------------------------------------------------------------------------
  1040. bool CLZMA::IsCompressed( unsigned char *pInput )
  1041. {
  1042. lzma_header_t *pHeader = (lzma_header_t *)pInput;
  1043. if ( pHeader && pHeader->id == LZMA_ID )
  1044. {
  1045. return true;
  1046. }
  1047. // unrecognized
  1048. return false;
  1049. }
  1050. //-----------------------------------------------------------------------------
  1051. // Returns uncompressed size of compressed input buffer. Used for allocating output
  1052. // buffer for decompression. Returns 0 if input buffer is not compressed.
  1053. //-----------------------------------------------------------------------------
  1054. unsigned int CLZMA::GetActualSize( unsigned char *pInput )
  1055. {
  1056. lzma_header_t *pHeader = (lzma_header_t *)pInput;
  1057. if ( pHeader && pHeader->id == LZMA_ID )
  1058. {
  1059. return LittleLong( pHeader->actualSize );
  1060. }
  1061. // unrecognized
  1062. return 0;
  1063. }
  1064. //-----------------------------------------------------------------------------
  1065. // Uncompress a buffer, Returns the uncompressed size. Caller must provide an
  1066. // adequate sized output buffer or memory corruption will occur.
  1067. //-----------------------------------------------------------------------------
  1068. unsigned int CLZMA::Uncompress( unsigned char *pInput, unsigned char *pOutput, LZMAReadProgressCallbackFunc_t pCallback )
  1069. {
  1070. unsigned int actualSize = GetActualSize( pInput );
  1071. if ( !actualSize )
  1072. {
  1073. // unrecognized
  1074. return 0;
  1075. }
  1076. CLzmaDecoderState state;
  1077. if ( LzmaDecodeProperties( &state.Properties, ((lzma_header_t *)pInput)->properties, LZMA_PROPERTIES_SIZE ) != LZMA_RESULT_OK )
  1078. {
  1079. Assert( 0 );
  1080. }
  1081. state.Probs = (CProb *)malloc( LzmaGetNumProbs( &state.Properties ) * sizeof( CProb ) );
  1082. unsigned int lzmaSize = LittleLong( ((lzma_header_t *)pInput)->lzmaSize );
  1083. SizeT inProcessed;
  1084. SizeT outProcessed;
  1085. int result = LzmaDecode( &state, pInput + sizeof( lzma_header_t ), lzmaSize, &inProcessed, pOutput, actualSize, &outProcessed, pCallback );
  1086. free( state.Probs );
  1087. if ( result != LZMA_RESULT_OK || outProcessed != (SizeT)actualSize )
  1088. {
  1089. Assert( 0 );
  1090. return 0;
  1091. }
  1092. return outProcessed;
  1093. }