Lzma2Dec.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  1. /* Lzma2Dec.c -- LZMA2 Decoder
  2. 2019-02-02 : 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 - End of data
  12. 00000001 U U - Uncompressed, reset dic, need reset state and set new prop
  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 + set new prop
  17. 111uuuuu U U P P S - LZMA, reset state + set new prop, reset dic
  18. u, U - Unpack Size
  19. P - Pack Size
  20. S - Props
  21. */
  22. #define LZMA2_CONTROL_COPY_RESET_DIC 1
  23. #define LZMA2_IS_UNCOMPRESSED_STATE(p) (((p)->control & (1 << 7)) == 0)
  24. #define LZMA2_LCLP_MAX 4
  25. #define LZMA2_DIC_SIZE_FROM_PROP(p) (((UInt32)2 | ((p) & 1)) << ((p) / 2 + 11))
  26. #ifdef SHOW_DEBUG_INFO
  27. #define PRF(x) x
  28. #else
  29. #define PRF(x)
  30. #endif
  31. typedef enum
  32. {
  33. LZMA2_STATE_CONTROL,
  34. LZMA2_STATE_UNPACK0,
  35. LZMA2_STATE_UNPACK1,
  36. LZMA2_STATE_PACK0,
  37. LZMA2_STATE_PACK1,
  38. LZMA2_STATE_PROP,
  39. LZMA2_STATE_DATA,
  40. LZMA2_STATE_DATA_CONT,
  41. LZMA2_STATE_FINISHED,
  42. LZMA2_STATE_ERROR
  43. } ELzma2State;
  44. static SRes Lzma2Dec_GetOldProps(Byte prop, Byte *props)
  45. {
  46. UInt32 dicSize;
  47. if (prop > 40)
  48. return SZ_ERROR_UNSUPPORTED;
  49. dicSize = (prop == 40) ? 0xFFFFFFFF : LZMA2_DIC_SIZE_FROM_PROP(prop);
  50. props[0] = (Byte)LZMA2_LCLP_MAX;
  51. props[1] = (Byte)(dicSize);
  52. props[2] = (Byte)(dicSize >> 8);
  53. props[3] = (Byte)(dicSize >> 16);
  54. props[4] = (Byte)(dicSize >> 24);
  55. return SZ_OK;
  56. }
  57. SRes Lzma2Dec_AllocateProbs(CLzma2Dec *p, Byte prop, ISzAllocPtr alloc)
  58. {
  59. Byte props[LZMA_PROPS_SIZE];
  60. RINOK(Lzma2Dec_GetOldProps(prop, props));
  61. return LzmaDec_AllocateProbs(&p->decoder, props, LZMA_PROPS_SIZE, alloc);
  62. }
  63. SRes Lzma2Dec_Allocate(CLzma2Dec *p, Byte prop, ISzAllocPtr alloc)
  64. {
  65. Byte props[LZMA_PROPS_SIZE];
  66. RINOK(Lzma2Dec_GetOldProps(prop, props));
  67. return LzmaDec_Allocate(&p->decoder, props, LZMA_PROPS_SIZE, alloc);
  68. }
  69. void Lzma2Dec_Init(CLzma2Dec *p)
  70. {
  71. p->state = LZMA2_STATE_CONTROL;
  72. p->needInitLevel = 0xE0;
  73. p->isExtraMode = False;
  74. p->unpackSize = 0;
  75. // p->decoder.dicPos = 0; // we can use it instead of full init
  76. LzmaDec_Init(&p->decoder);
  77. }
  78. static ELzma2State Lzma2Dec_UpdateState(CLzma2Dec *p, Byte b)
  79. {
  80. switch (p->state)
  81. {
  82. case LZMA2_STATE_CONTROL:
  83. p->isExtraMode = False;
  84. p->control = b;
  85. PRF(printf("\n %8X", (unsigned)p->decoder.dicPos));
  86. PRF(printf(" %02X", (unsigned)b));
  87. if (b == 0)
  88. return LZMA2_STATE_FINISHED;
  89. if (LZMA2_IS_UNCOMPRESSED_STATE(p))
  90. {
  91. if (b == LZMA2_CONTROL_COPY_RESET_DIC)
  92. p->needInitLevel = 0xC0;
  93. else if (b > 2 || p->needInitLevel == 0xE0)
  94. return LZMA2_STATE_ERROR;
  95. }
  96. else
  97. {
  98. if (b < p->needInitLevel)
  99. return LZMA2_STATE_ERROR;
  100. p->needInitLevel = 0;
  101. p->unpackSize = (UInt32)(b & 0x1F) << 16;
  102. }
  103. return LZMA2_STATE_UNPACK0;
  104. case LZMA2_STATE_UNPACK0:
  105. p->unpackSize |= (UInt32)b << 8;
  106. return LZMA2_STATE_UNPACK1;
  107. case LZMA2_STATE_UNPACK1:
  108. p->unpackSize |= (UInt32)b;
  109. p->unpackSize++;
  110. PRF(printf(" %7u", (unsigned)p->unpackSize));
  111. return LZMA2_IS_UNCOMPRESSED_STATE(p) ? LZMA2_STATE_DATA : LZMA2_STATE_PACK0;
  112. case LZMA2_STATE_PACK0:
  113. p->packSize = (UInt32)b << 8;
  114. return LZMA2_STATE_PACK1;
  115. case LZMA2_STATE_PACK1:
  116. p->packSize |= (UInt32)b;
  117. p->packSize++;
  118. // if (p->packSize < 5) return LZMA2_STATE_ERROR;
  119. PRF(printf(" %5u", (unsigned)p->packSize));
  120. return (p->control & 0x40) ? LZMA2_STATE_PROP : LZMA2_STATE_DATA;
  121. case LZMA2_STATE_PROP:
  122. {
  123. unsigned lc, lp;
  124. if (b >= (9 * 5 * 5))
  125. return LZMA2_STATE_ERROR;
  126. lc = b % 9;
  127. b /= 9;
  128. p->decoder.prop.pb = (Byte)(b / 5);
  129. lp = b % 5;
  130. if (lc + lp > LZMA2_LCLP_MAX)
  131. return LZMA2_STATE_ERROR;
  132. p->decoder.prop.lc = (Byte)lc;
  133. p->decoder.prop.lp = (Byte)lp;
  134. return LZMA2_STATE_DATA;
  135. }
  136. }
  137. return LZMA2_STATE_ERROR;
  138. }
  139. static void LzmaDec_UpdateWithUncompressed(CLzmaDec *p, const Byte *src, SizeT size)
  140. {
  141. memcpy(p->dic + p->dicPos, src, size);
  142. p->dicPos += size;
  143. if (p->checkDicSize == 0 && p->prop.dicSize - p->processedPos <= size)
  144. p->checkDicSize = p->prop.dicSize;
  145. p->processedPos += (UInt32)size;
  146. }
  147. void LzmaDec_InitDicAndState(CLzmaDec *p, BoolInt initDic, BoolInt initState);
  148. SRes Lzma2Dec_DecodeToDic(CLzma2Dec *p, SizeT dicLimit,
  149. const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
  150. {
  151. SizeT inSize = *srcLen;
  152. *srcLen = 0;
  153. *status = LZMA_STATUS_NOT_SPECIFIED;
  154. while (p->state != LZMA2_STATE_ERROR)
  155. {
  156. SizeT dicPos;
  157. if (p->state == LZMA2_STATE_FINISHED)
  158. {
  159. *status = LZMA_STATUS_FINISHED_WITH_MARK;
  160. return SZ_OK;
  161. }
  162. dicPos = p->decoder.dicPos;
  163. if (dicPos == dicLimit && finishMode == LZMA_FINISH_ANY)
  164. {
  165. *status = LZMA_STATUS_NOT_FINISHED;
  166. return SZ_OK;
  167. }
  168. if (p->state != LZMA2_STATE_DATA && p->state != LZMA2_STATE_DATA_CONT)
  169. {
  170. if (*srcLen == inSize)
  171. {
  172. *status = LZMA_STATUS_NEEDS_MORE_INPUT;
  173. return SZ_OK;
  174. }
  175. (*srcLen)++;
  176. p->state = Lzma2Dec_UpdateState(p, *src++);
  177. if (dicPos == dicLimit && p->state != LZMA2_STATE_FINISHED)
  178. break;
  179. continue;
  180. }
  181. {
  182. SizeT inCur = inSize - *srcLen;
  183. SizeT outCur = dicLimit - dicPos;
  184. ELzmaFinishMode curFinishMode = LZMA_FINISH_ANY;
  185. if (outCur >= p->unpackSize)
  186. {
  187. outCur = (SizeT)p->unpackSize;
  188. curFinishMode = LZMA_FINISH_END;
  189. }
  190. if (LZMA2_IS_UNCOMPRESSED_STATE(p))
  191. {
  192. if (inCur == 0)
  193. {
  194. *status = LZMA_STATUS_NEEDS_MORE_INPUT;
  195. return SZ_OK;
  196. }
  197. if (p->state == LZMA2_STATE_DATA)
  198. {
  199. BoolInt initDic = (p->control == LZMA2_CONTROL_COPY_RESET_DIC);
  200. LzmaDec_InitDicAndState(&p->decoder, initDic, False);
  201. }
  202. if (inCur > outCur)
  203. inCur = outCur;
  204. if (inCur == 0)
  205. break;
  206. LzmaDec_UpdateWithUncompressed(&p->decoder, src, inCur);
  207. src += inCur;
  208. *srcLen += inCur;
  209. p->unpackSize -= (UInt32)inCur;
  210. p->state = (p->unpackSize == 0) ? LZMA2_STATE_CONTROL : LZMA2_STATE_DATA_CONT;
  211. }
  212. else
  213. {
  214. SRes res;
  215. if (p->state == LZMA2_STATE_DATA)
  216. {
  217. BoolInt initDic = (p->control >= 0xE0);
  218. BoolInt initState = (p->control >= 0xA0);
  219. LzmaDec_InitDicAndState(&p->decoder, initDic, initState);
  220. p->state = LZMA2_STATE_DATA_CONT;
  221. }
  222. if (inCur > p->packSize)
  223. inCur = (SizeT)p->packSize;
  224. res = LzmaDec_DecodeToDic(&p->decoder, dicPos + outCur, src, &inCur, curFinishMode, status);
  225. src += inCur;
  226. *srcLen += inCur;
  227. p->packSize -= (UInt32)inCur;
  228. outCur = p->decoder.dicPos - dicPos;
  229. p->unpackSize -= (UInt32)outCur;
  230. if (res != 0)
  231. break;
  232. if (*status == LZMA_STATUS_NEEDS_MORE_INPUT)
  233. {
  234. if (p->packSize == 0)
  235. break;
  236. return SZ_OK;
  237. }
  238. if (inCur == 0 && outCur == 0)
  239. {
  240. if (*status != LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK
  241. || p->unpackSize != 0
  242. || p->packSize != 0)
  243. break;
  244. p->state = LZMA2_STATE_CONTROL;
  245. }
  246. *status = LZMA_STATUS_NOT_SPECIFIED;
  247. }
  248. }
  249. }
  250. *status = LZMA_STATUS_NOT_SPECIFIED;
  251. p->state = LZMA2_STATE_ERROR;
  252. return SZ_ERROR_DATA;
  253. }
  254. ELzma2ParseStatus Lzma2Dec_Parse(CLzma2Dec *p,
  255. SizeT outSize,
  256. const Byte *src, SizeT *srcLen,
  257. int checkFinishBlock)
  258. {
  259. SizeT inSize = *srcLen;
  260. *srcLen = 0;
  261. while (p->state != LZMA2_STATE_ERROR)
  262. {
  263. if (p->state == LZMA2_STATE_FINISHED)
  264. return (ELzma2ParseStatus)LZMA_STATUS_FINISHED_WITH_MARK;
  265. if (outSize == 0 && !checkFinishBlock)
  266. return (ELzma2ParseStatus)LZMA_STATUS_NOT_FINISHED;
  267. if (p->state != LZMA2_STATE_DATA && p->state != LZMA2_STATE_DATA_CONT)
  268. {
  269. if (*srcLen == inSize)
  270. return (ELzma2ParseStatus)LZMA_STATUS_NEEDS_MORE_INPUT;
  271. (*srcLen)++;
  272. p->state = Lzma2Dec_UpdateState(p, *src++);
  273. if (p->state == LZMA2_STATE_UNPACK0)
  274. {
  275. // if (p->decoder.dicPos != 0)
  276. if (p->control == LZMA2_CONTROL_COPY_RESET_DIC || p->control >= 0xE0)
  277. return LZMA2_PARSE_STATUS_NEW_BLOCK;
  278. // if (outSize == 0) return LZMA_STATUS_NOT_FINISHED;
  279. }
  280. // The following code can be commented.
  281. // It's not big problem, if we read additional input bytes.
  282. // It will be stopped later in LZMA2_STATE_DATA / LZMA2_STATE_DATA_CONT state.
  283. if (outSize == 0 && p->state != LZMA2_STATE_FINISHED)
  284. {
  285. // checkFinishBlock is true. So we expect that block must be finished,
  286. // We can return LZMA_STATUS_NOT_SPECIFIED or LZMA_STATUS_NOT_FINISHED here
  287. // break;
  288. return (ELzma2ParseStatus)LZMA_STATUS_NOT_FINISHED;
  289. }
  290. if (p->state == LZMA2_STATE_DATA)
  291. return LZMA2_PARSE_STATUS_NEW_CHUNK;
  292. continue;
  293. }
  294. if (outSize == 0)
  295. return (ELzma2ParseStatus)LZMA_STATUS_NOT_FINISHED;
  296. {
  297. SizeT inCur = inSize - *srcLen;
  298. if (LZMA2_IS_UNCOMPRESSED_STATE(p))
  299. {
  300. if (inCur == 0)
  301. return (ELzma2ParseStatus)LZMA_STATUS_NEEDS_MORE_INPUT;
  302. if (inCur > p->unpackSize)
  303. inCur = p->unpackSize;
  304. if (inCur > outSize)
  305. inCur = outSize;
  306. p->decoder.dicPos += inCur;
  307. src += inCur;
  308. *srcLen += inCur;
  309. outSize -= inCur;
  310. p->unpackSize -= (UInt32)inCur;
  311. p->state = (p->unpackSize == 0) ? LZMA2_STATE_CONTROL : LZMA2_STATE_DATA_CONT;
  312. }
  313. else
  314. {
  315. p->isExtraMode = True;
  316. if (inCur == 0)
  317. {
  318. if (p->packSize != 0)
  319. return (ELzma2ParseStatus)LZMA_STATUS_NEEDS_MORE_INPUT;
  320. }
  321. else if (p->state == LZMA2_STATE_DATA)
  322. {
  323. p->state = LZMA2_STATE_DATA_CONT;
  324. if (*src != 0)
  325. {
  326. // first byte of lzma chunk must be Zero
  327. *srcLen += 1;
  328. p->packSize--;
  329. break;
  330. }
  331. }
  332. if (inCur > p->packSize)
  333. inCur = (SizeT)p->packSize;
  334. src += inCur;
  335. *srcLen += inCur;
  336. p->packSize -= (UInt32)inCur;
  337. if (p->packSize == 0)
  338. {
  339. SizeT rem = outSize;
  340. if (rem > p->unpackSize)
  341. rem = p->unpackSize;
  342. p->decoder.dicPos += rem;
  343. p->unpackSize -= (UInt32)rem;
  344. outSize -= rem;
  345. if (p->unpackSize == 0)
  346. p->state = LZMA2_STATE_CONTROL;
  347. }
  348. }
  349. }
  350. }
  351. p->state = LZMA2_STATE_ERROR;
  352. return (ELzma2ParseStatus)LZMA_STATUS_NOT_SPECIFIED;
  353. }
  354. SRes Lzma2Dec_DecodeToBuf(CLzma2Dec *p, Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen, ELzmaFinishMode finishMode, ELzmaStatus *status)
  355. {
  356. SizeT outSize = *destLen, inSize = *srcLen;
  357. *srcLen = *destLen = 0;
  358. for (;;)
  359. {
  360. SizeT inCur = inSize, outCur, dicPos;
  361. ELzmaFinishMode curFinishMode;
  362. SRes res;
  363. if (p->decoder.dicPos == p->decoder.dicBufSize)
  364. p->decoder.dicPos = 0;
  365. dicPos = p->decoder.dicPos;
  366. curFinishMode = LZMA_FINISH_ANY;
  367. outCur = p->decoder.dicBufSize - dicPos;
  368. if (outCur >= outSize)
  369. {
  370. outCur = outSize;
  371. curFinishMode = finishMode;
  372. }
  373. res = Lzma2Dec_DecodeToDic(p, dicPos + outCur, src, &inCur, curFinishMode, status);
  374. src += inCur;
  375. inSize -= inCur;
  376. *srcLen += inCur;
  377. outCur = p->decoder.dicPos - dicPos;
  378. memcpy(dest, p->decoder.dic + dicPos, outCur);
  379. dest += outCur;
  380. outSize -= outCur;
  381. *destLen += outCur;
  382. if (res != 0)
  383. return res;
  384. if (outCur == 0 || outSize == 0)
  385. return SZ_OK;
  386. }
  387. }
  388. SRes Lzma2Decode(Byte *dest, SizeT *destLen, const Byte *src, SizeT *srcLen,
  389. Byte prop, ELzmaFinishMode finishMode, ELzmaStatus *status, ISzAllocPtr alloc)
  390. {
  391. CLzma2Dec p;
  392. SRes res;
  393. SizeT outSize = *destLen, inSize = *srcLen;
  394. *destLen = *srcLen = 0;
  395. *status = LZMA_STATUS_NOT_SPECIFIED;
  396. Lzma2Dec_Construct(&p);
  397. RINOK(Lzma2Dec_AllocateProbs(&p, prop, alloc));
  398. p.decoder.dic = dest;
  399. p.decoder.dicBufSize = outSize;
  400. Lzma2Dec_Init(&p);
  401. *srcLen = inSize;
  402. res = Lzma2Dec_DecodeToDic(&p, outSize, src, srcLen, finishMode, status);
  403. *destLen = p.decoder.dicPos;
  404. if (res == SZ_OK && *status == LZMA_STATUS_NEEDS_MORE_INPUT)
  405. res = SZ_ERROR_INPUT_EOF;
  406. Lzma2Dec_FreeProbs(&p, alloc);
  407. return res;
  408. }