Team Fortress 2 Source Code as on 22/4/2020
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.

352 lines
9.6 KiB

  1. /* Lzma2Dec.c -- LZMA2 Decoder
  2. 2010-12-15 : Igor Pavlov : Public domain */
  3. /* #define SHOW_DEBUG_INFO */
  4. #include "Precomp.h"
  5. #ifdef SHOW_DEBUG_INFO
  6. #include <stdio.h>
  7. #endif
  8. #include <string.h>
  9. #include "Lzma2Dec.h"
  10. /*
  11. 00000000 - EOS
  12. 00000001 U U - Uncompressed Reset Dic
  13. 00000010 U U - Uncompressed No Reset
  14. 100uuuuu U U P P - LZMA no reset
  15. 101uuuuu U U P P - LZMA reset state
  16. 110uuuuu U U P P S - LZMA reset state + new prop
  17. 111uuuuu U U P P S - LZMA reset state + new prop + reset dic
  18. u, U - Unpack Size
  19. P - Pack Size
  20. S - Props
  21. */
  22. #define LZMA2_CONTROL_LZMA (1 << 7)
  23. #define LZMA2_CONTROL_COPY_NO_RESET 2
  24. #define LZMA2_CONTROL_COPY_RESET_DIC 1
  25. #define LZMA2_CONTROL_EOF 0
  26. #define LZMA2_IS_UNCOMPRESSED_STATE(p) (((p)->control & LZMA2_CONTROL_LZMA) == 0)
  27. #define LZMA2_GET_LZMA_MODE(p) (((p)->control >> 5) & 3)
  28. #define LZMA2_IS_THERE_PROP(mode) ((mode) >= 2)
  29. #define LZMA2_LCLP_MAX 4
  30. #define LZMA2_DIC_SIZE_FROM_PROP(p) (((UInt32)2 | ((p) & 1)) << ((p) / 2 + 11))
  31. #ifdef SHOW_DEBUG_INFO
  32. #define PRF(x) x
  33. #else
  34. #define PRF(x)
  35. #endif
  36. typedef enum
  37. {
  38. LZMA2_STATE_CONTROL,
  39. LZMA2_STATE_UNPACK0,
  40. LZMA2_STATE_UNPACK1,
  41. LZMA2_STATE_PACK0,
  42. LZMA2_STATE_PACK1,
  43. LZMA2_STATE_PROP,
  44. LZMA2_STATE_DATA,
  45. LZMA2_STATE_DATA_CONT,
  46. LZMA2_STATE_FINISHED,
  47. LZMA2_STATE_ERROR
  48. } ELzma2State;
  49. static SRes Lzma2Dec_GetOldProps(Byte prop, Byte *props)
  50. {
  51. UInt32 dicSize;
  52. if (prop > 40)
  53. return SZ_ERROR_UNSUPPORTED;
  54. dicSize = (prop == 40) ? 0xFFFFFFFF : LZMA2_DIC_SIZE_FROM_PROP(prop);
  55. props[0] = (Byte)LZMA2_LCLP_MAX;
  56. props[1] = (Byte)(dicSize);
  57. props[2] = (Byte)(dicSize >> 8);
  58. props[3] = (Byte)(dicSize >> 16);
  59. props[4] = (Byte)(dicSize >> 24);
  60. return SZ_OK;
  61. }
  62. SRes Lzma2Dec_AllocateProbs(CLzma2Dec *p, Byte prop, ISzAlloc *alloc)
  63. {
  64. Byte props[LZMA_PROPS_SIZE];
  65. RINOK(Lzma2Dec_GetOldProps(prop, props));
  66. return LzmaDec_AllocateProbs(&p->decoder, props, LZMA_PROPS_SIZE, alloc);
  67. }
  68. SRes Lzma2Dec_Allocate(CLzma2Dec *p, Byte prop, ISzAlloc *alloc)
  69. {
  70. Byte props[LZMA_PROPS_SIZE];
  71. RINOK(Lzma2Dec_GetOldProps(prop, props));
  72. return LzmaDec_Allocate(&p->decoder, props, LZMA_PROPS_SIZE, alloc);
  73. }
  74. void Lzma2Dec_Init(CLzma2Dec *p)
  75. {
  76. p->state = LZMA2_STATE_CONTROL;
  77. p->needInitDic = True;
  78. p->needInitState = True;
  79. p->needInitProp = True;
  80. LzmaDec_Init(&p->decoder);
  81. }
  82. static ELzma2State Lzma2Dec_UpdateState(CLzma2Dec *p, Byte b)
  83. {
  84. switch(p->state)
  85. {
  86. case LZMA2_STATE_CONTROL:
  87. p->control = b;
  88. PRF(printf("\n %4X ", p->decoder.dicPos));
  89. PRF(printf(" %2X", b));
  90. if (p->control == 0)
  91. return LZMA2_STATE_FINISHED;
  92. if (LZMA2_IS_UNCOMPRESSED_STATE(p))
  93. {
  94. if ((p->control & 0x7F) > 2)
  95. return LZMA2_STATE_ERROR;
  96. p->unpackSize = 0;
  97. }
  98. else
  99. p->unpackSize = (UInt32)(p->control & 0x1F) << 16;
  100. return LZMA2_STATE_UNPACK0;
  101. case LZMA2_STATE_UNPACK0:
  102. p->unpackSize |= (UInt32)b << 8;
  103. return LZMA2_STATE_UNPACK1;
  104. case LZMA2_STATE_UNPACK1:
  105. p->unpackSize |= (UInt32)b;
  106. p->unpackSize++;
  107. PRF(printf(" %8d", p->unpackSize));
  108. return (LZMA2_IS_UNCOMPRESSED_STATE(p)) ? LZMA2_STATE_DATA : LZMA2_STATE_PACK0;
  109. case LZMA2_STATE_PACK0:
  110. p->packSize = (UInt32)b << 8;
  111. return LZMA2_STATE_PACK1;
  112. case LZMA2_STATE_PACK1:
  113. p->packSize |= (UInt32)b;
  114. p->packSize++;
  115. PRF(printf(" %8d", p->packSize));
  116. return LZMA2_IS_THERE_PROP(LZMA2_GET_LZMA_MODE(p)) ? LZMA2_STATE_PROP:
  117. (p->needInitProp ? LZMA2_STATE_ERROR : LZMA2_STATE_DATA);
  118. case LZMA2_STATE_PROP:
  119. {
  120. int lc, lp;
  121. if (b >= (9 * 5 * 5))
  122. return LZMA2_STATE_ERROR;
  123. lc = b % 9;
  124. b /= 9;
  125. p->decoder.prop.pb = b / 5;
  126. lp = b % 5;
  127. if (lc + lp > LZMA2_LCLP_MAX)
  128. return LZMA2_STATE_ERROR;
  129. p->decoder.prop.lc = lc;
  130. p->decoder.prop.lp = lp;
  131. p->needInitProp = False;
  132. return LZMA2_STATE_DATA;
  133. }
  134. }
  135. return LZMA2_STATE_ERROR;
  136. }
  137. static void LzmaDec_UpdateWithUncompressed(CLzmaDec *p, const Byte *src, SizeT size)
  138. {
  139. memcpy(p->dic + p->dicPos, src, size);
  140. p->dicPos += size;
  141. if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= size)
  142. p->checkDicSize = p->prop.dicSize;
  143. p->processedPos += (UInt32)size;
  144. }
  145. void LzmaDec_InitDicAndState(CLzmaDec *p, Bool initDic, Bool initState);
  146. SRes Lzma2Dec_DecodeToDic(CLzma2Dec *p, SizeT dicLimit,
  147. const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
  148. {
  149. SizeT inSize = *srcLen;
  150. *srcLen = 0;
  151. *status = LZMA_STATUS_NOT_SPECIFIED;
  152. while (p->state != LZMA2_STATE_FINISHED)
  153. {
  154. SizeT dicPos = p->decoder.dicPos;
  155. if (p->state == LZMA2_STATE_ERROR)
  156. return SZ_ERROR_DATA;
  157. if (dicPos == dicLimit && finishMode == LZMA_FINISH_ANY)
  158. {
  159. *status = LZMA_STATUS_NOT_FINISHED;
  160. return SZ_OK;
  161. }
  162. if (p->state != LZMA2_STATE_DATA && p->state != LZMA2_STATE_DATA_CONT)
  163. {
  164. if (*srcLen == inSize)
  165. {
  166. *status = LZMA_STATUS_NEEDS_MORE_INPUT;
  167. return SZ_OK;
  168. }
  169. (*srcLen)++;
  170. p->state = Lzma2Dec_UpdateState(p, *src++);
  171. continue;
  172. }
  173. {
  174. SizeT destSizeCur = dicLimit - dicPos;
  175. SizeT srcSizeCur = inSize - *srcLen;
  176. ELzmaFinishMode curFinishMode = LZMA_FINISH_ANY;
  177. if (p->unpackSize <= destSizeCur)
  178. {
  179. destSizeCur = (SizeT)p->unpackSize;
  180. curFinishMode = LZMA_FINISH_END;
  181. }
  182. if (LZMA2_IS_UNCOMPRESSED_STATE(p))
  183. {
  184. if (*srcLen == inSize)
  185. {
  186. *status = LZMA_STATUS_NEEDS_MORE_INPUT;
  187. return SZ_OK;
  188. }
  189. if (p->state == LZMA2_STATE_DATA)
  190. {
  191. Bool initDic = (p->control == LZMA2_CONTROL_COPY_RESET_DIC);
  192. if (initDic)
  193. p->needInitProp = p->needInitState = True;
  194. else if (p->needInitDic)
  195. return SZ_ERROR_DATA;
  196. p->needInitDic = False;
  197. LzmaDec_InitDicAndState(&p->decoder, initDic, False);
  198. }
  199. if (srcSizeCur > destSizeCur)
  200. srcSizeCur = destSizeCur;
  201. if (srcSizeCur == 0)
  202. return SZ_ERROR_DATA;
  203. LzmaDec_UpdateWithUncompressed(&p->decoder, src, srcSizeCur);
  204. src += srcSizeCur;
  205. *srcLen += srcSizeCur;
  206. p->unpackSize -= (UInt32)srcSizeCur;
  207. p->state = (p->unpackSize == 0) ? LZMA2_STATE_CONTROL : LZMA2_STATE_DATA_CONT;
  208. }
  209. else
  210. {
  211. SizeT outSizeProcessed;
  212. SRes res;
  213. if (p->state == LZMA2_STATE_DATA)
  214. {
  215. int mode = LZMA2_GET_LZMA_MODE(p);
  216. Bool initDic = (mode == 3);
  217. Bool initState = (mode > 0);
  218. if ((!initDic && p->needInitDic) || (!initState && p->needInitState))
  219. return SZ_ERROR_DATA;
  220. LzmaDec_InitDicAndState(&p->decoder, initDic, initState);
  221. p->needInitDic = False;
  222. p->needInitState = False;
  223. p->state = LZMA2_STATE_DATA_CONT;
  224. }
  225. if (srcSizeCur > p->packSize)
  226. srcSizeCur = (SizeT)p->packSize;
  227. res = LzmaDec_DecodeToDic(&p->decoder, dicPos + destSizeCur, src, &srcSizeCur, curFinishMode, status);
  228. src += srcSizeCur;
  229. *srcLen += srcSizeCur;
  230. p->packSize -= (UInt32)srcSizeCur;
  231. outSizeProcessed = p->decoder.dicPos - dicPos;
  232. p->unpackSize -= (UInt32)outSizeProcessed;
  233. RINOK(res);
  234. if (*status == LZMA_STATUS_NEEDS_MORE_INPUT)
  235. return res;
  236. if (srcSizeCur == 0 && outSizeProcessed == 0)
  237. {
  238. if (*status != LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK ||
  239. p->unpackSize != 0 || p->packSize != 0)
  240. return SZ_ERROR_DATA;
  241. p->state = LZMA2_STATE_CONTROL;
  242. }
  243. if (*status == LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK)
  244. *status = LZMA_STATUS_NOT_FINISHED;
  245. }
  246. }
  247. }
  248. *status = LZMA_STATUS_FINISHED_WITH_MARK;
  249. return SZ_OK;
  250. }
  251. SRes Lzma2Dec_DecodeToBuf(CLzma2Dec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
  252. {
  253. SizeT outSize = *destLen, inSize = *srcLen;
  254. *srcLen = *destLen = 0;
  255. for (;;)
  256. {
  257. SizeT srcSizeCur = inSize, outSizeCur, dicPos;
  258. ELzmaFinishMode curFinishMode;
  259. SRes res;
  260. if (p->decoder.dicPos == p->decoder.dicBufSize)
  261. p->decoder.dicPos = 0;
  262. dicPos = p->decoder.dicPos;
  263. if (outSize > p->decoder.dicBufSize - dicPos)
  264. {
  265. outSizeCur = p->decoder.dicBufSize;
  266. curFinishMode = LZMA_FINISH_ANY;
  267. }
  268. else
  269. {
  270. outSizeCur = dicPos + outSize;
  271. curFinishMode = finishMode;
  272. }
  273. res = Lzma2Dec_DecodeToDic(p, outSizeCur, src, &srcSizeCur, curFinishMode, status);
  274. src += srcSizeCur;
  275. inSize -= srcSizeCur;
  276. *srcLen += srcSizeCur;
  277. outSizeCur = p->decoder.dicPos - dicPos;
  278. memcpy(dest, p->decoder.dic + dicPos, outSizeCur);
  279. dest += outSizeCur;
  280. outSize -= outSizeCur;
  281. *destLen += outSizeCur;
  282. if (res != 0)
  283. return res;
  284. if (outSizeCur == 0 || outSize == 0)
  285. return SZ_OK;
  286. }
  287. }
  288. SRes Lzma2Decode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
  289. Byte prop, ELzmaFinishMode finishMode, ELzmaStatus *status, ISzAlloc *alloc)
  290. {
  291. CLzma2Dec p;
  292. SRes res;
  293. SizeT outSize = *destLen, inSize = *srcLen;
  294. *destLen = *srcLen = 0;
  295. *status = LZMA_STATUS_NOT_SPECIFIED;
  296. Lzma2Dec_Construct(&p);
  297. RINOK(Lzma2Dec_AllocateProbs(&p, prop, alloc));
  298. p.decoder.dic = dest;
  299. p.decoder.dicBufSize = outSize;
  300. Lzma2Dec_Init(&p);
  301. *srcLen = inSize;
  302. res = Lzma2Dec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
  303. *destLen = p.decoder.dicPos;
  304. if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
  305. res = SZ_ERROR_INPUT_EOF;
  306. Lzma2Dec_FreeProbs(&p, alloc);
  307. return res;
  308. }