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.

493 lines
12 KiB

  1. /* Lzma2Enc.c -- LZMA2 Encoder
  2. 2012-06-19 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. /* #include <stdio.h> */
  5. #include <string.h>
  6. /* #define _7ZIP_ST */
  7. #include "Lzma2Enc.h"
  8. #ifndef _7ZIP_ST
  9. #include "MtCoder.h"
  10. #else
  11. #define NUM_MT_CODER_THREADS_MAX 1
  12. #endif
  13. #define LZMA2_CONTROL_LZMA (1 << 7)
  14. #define LZMA2_CONTROL_COPY_NO_RESET 2
  15. #define LZMA2_CONTROL_COPY_RESET_DIC 1
  16. #define LZMA2_CONTROL_EOF 0
  17. #define LZMA2_LCLP_MAX 4
  18. #define LZMA2_DIC_SIZE_FROM_PROP(p) (((UInt32)2 | ((p) & 1)) << ((p) / 2 + 11))
  19. #define LZMA2_PACK_SIZE_MAX (1 << 16)
  20. #define LZMA2_COPY_CHUNK_SIZE LZMA2_PACK_SIZE_MAX
  21. #define LZMA2_UNPACK_SIZE_MAX (1 << 21)
  22. #define LZMA2_KEEP_WINDOW_SIZE LZMA2_UNPACK_SIZE_MAX
  23. #define LZMA2_CHUNK_SIZE_COMPRESSED_MAX ((1 << 16) + 16)
  24. #define PRF(x) /* x */
  25. /* ---------- CLzma2EncInt ---------- */
  26. typedef struct
  27. {
  28. CLzmaEncHandle enc;
  29. UInt64 srcPos;
  30. Byte props;
  31. Bool needInitState;
  32. Bool needInitProp;
  33. } CLzma2EncInt;
  34. static SRes Lzma2EncInt_Init(CLzma2EncInt *p, const CLzma2EncProps *props)
  35. {
  36. Byte propsEncoded[LZMA_PROPS_SIZE];
  37. SizeT propsSize = LZMA_PROPS_SIZE;
  38. RINOK(LzmaEnc_SetProps(p->enc, &props->lzmaProps));
  39. RINOK(LzmaEnc_WriteProperties(p->enc, propsEncoded, &propsSize));
  40. p->srcPos = 0;
  41. p->props = propsEncoded[0];
  42. p->needInitState = True;
  43. p->needInitProp = True;
  44. return SZ_OK;
  45. }
  46. SRes LzmaEnc_PrepareForLzma2(CLzmaEncHandle pp, ISeqInStream *inStream, UInt32 keepWindowSize,
  47. ISzAlloc *alloc, ISzAlloc *allocBig);
  48. SRes LzmaEnc_MemPrepare(CLzmaEncHandle pp, const Byte *src, SizeT srcLen,
  49. UInt32 keepWindowSize, ISzAlloc *alloc, ISzAlloc *allocBig);
  50. SRes LzmaEnc_CodeOneMemBlock(CLzmaEncHandle pp, Bool reInit,
  51. Byte *dest, size_t *destLen, UInt32 desiredPackSize, UInt32 *unpackSize);
  52. const Byte *LzmaEnc_GetCurBuf(CLzmaEncHandle pp);
  53. void LzmaEnc_Finish(CLzmaEncHandle pp);
  54. void LzmaEnc_SaveState(CLzmaEncHandle pp);
  55. void LzmaEnc_RestoreState(CLzmaEncHandle pp);
  56. static SRes Lzma2EncInt_EncodeSubblock(CLzma2EncInt *p, Byte *outBuf,
  57. size_t *packSizeRes, ISeqOutStream *outStream)
  58. {
  59. size_t packSizeLimit = *packSizeRes;
  60. size_t packSize = packSizeLimit;
  61. UInt32 unpackSize = LZMA2_UNPACK_SIZE_MAX;
  62. unsigned lzHeaderSize = 5 + (p->needInitProp ? 1 : 0);
  63. Bool useCopyBlock;
  64. SRes res;
  65. *packSizeRes = 0;
  66. if (packSize < lzHeaderSize)
  67. return SZ_ERROR_OUTPUT_EOF;
  68. packSize -= lzHeaderSize;
  69. LzmaEnc_SaveState(p->enc);
  70. res = LzmaEnc_CodeOneMemBlock(p->enc, p->needInitState,
  71. outBuf + lzHeaderSize, &packSize, LZMA2_PACK_SIZE_MAX, &unpackSize);
  72. PRF(printf("\npackSize = %7d unpackSize = %7d ", packSize, unpackSize));
  73. if (unpackSize == 0)
  74. return res;
  75. if (res == SZ_OK)
  76. useCopyBlock = (packSize + 2 >= unpackSize || packSize > (1 << 16));
  77. else
  78. {
  79. if (res != SZ_ERROR_OUTPUT_EOF)
  80. return res;
  81. res = SZ_OK;
  82. useCopyBlock = True;
  83. }
  84. if (useCopyBlock)
  85. {
  86. size_t destPos = 0;
  87. PRF(printf("################# COPY "));
  88. while (unpackSize > 0)
  89. {
  90. UInt32 u = (unpackSize < LZMA2_COPY_CHUNK_SIZE) ? unpackSize : LZMA2_COPY_CHUNK_SIZE;
  91. if (packSizeLimit - destPos < u + 3)
  92. return SZ_ERROR_OUTPUT_EOF;
  93. outBuf[destPos++] = (Byte)(p->srcPos == 0 ? LZMA2_CONTROL_COPY_RESET_DIC : LZMA2_CONTROL_COPY_NO_RESET);
  94. outBuf[destPos++] = (Byte)((u - 1) >> 8);
  95. outBuf[destPos++] = (Byte)(u - 1);
  96. memcpy(outBuf + destPos, LzmaEnc_GetCurBuf(p->enc) - unpackSize, u);
  97. unpackSize -= u;
  98. destPos += u;
  99. p->srcPos += u;
  100. if (outStream)
  101. {
  102. *packSizeRes += destPos;
  103. if (outStream->Write(outStream, outBuf, destPos) != destPos)
  104. return SZ_ERROR_WRITE;
  105. destPos = 0;
  106. }
  107. else
  108. *packSizeRes = destPos;
  109. /* needInitState = True; */
  110. }
  111. LzmaEnc_RestoreState(p->enc);
  112. return SZ_OK;
  113. }
  114. {
  115. size_t destPos = 0;
  116. UInt32 u = unpackSize - 1;
  117. UInt32 pm = (UInt32)(packSize - 1);
  118. unsigned mode = (p->srcPos == 0) ? 3 : (p->needInitState ? (p->needInitProp ? 2 : 1) : 0);
  119. PRF(printf(" "));
  120. outBuf[destPos++] = (Byte)(LZMA2_CONTROL_LZMA | (mode << 5) | ((u >> 16) & 0x1F));
  121. outBuf[destPos++] = (Byte)(u >> 8);
  122. outBuf[destPos++] = (Byte)u;
  123. outBuf[destPos++] = (Byte)(pm >> 8);
  124. outBuf[destPos++] = (Byte)pm;
  125. if (p->needInitProp)
  126. outBuf[destPos++] = p->props;
  127. p->needInitProp = False;
  128. p->needInitState = False;
  129. destPos += packSize;
  130. p->srcPos += unpackSize;
  131. if (outStream)
  132. if (outStream->Write(outStream, outBuf, destPos) != destPos)
  133. return SZ_ERROR_WRITE;
  134. *packSizeRes = destPos;
  135. return SZ_OK;
  136. }
  137. }
  138. /* ---------- Lzma2 Props ---------- */
  139. void Lzma2EncProps_Init(CLzma2EncProps *p)
  140. {
  141. LzmaEncProps_Init(&p->lzmaProps);
  142. p->numTotalThreads = -1;
  143. p->numBlockThreads = -1;
  144. p->blockSize = 0;
  145. }
  146. void Lzma2EncProps_Normalize(CLzma2EncProps *p)
  147. {
  148. int t1, t1n, t2, t3;
  149. {
  150. CLzmaEncProps lzmaProps = p->lzmaProps;
  151. LzmaEncProps_Normalize(&lzmaProps);
  152. t1n = lzmaProps.numThreads;
  153. }
  154. t1 = p->lzmaProps.numThreads;
  155. t2 = p->numBlockThreads;
  156. t3 = p->numTotalThreads;
  157. if (t2 > NUM_MT_CODER_THREADS_MAX)
  158. t2 = NUM_MT_CODER_THREADS_MAX;
  159. if (t3 <= 0)
  160. {
  161. if (t2 <= 0)
  162. t2 = 1;
  163. t3 = t1n * t2;
  164. }
  165. else if (t2 <= 0)
  166. {
  167. t2 = t3 / t1n;
  168. if (t2 == 0)
  169. {
  170. t1 = 1;
  171. t2 = t3;
  172. }
  173. if (t2 > NUM_MT_CODER_THREADS_MAX)
  174. t2 = NUM_MT_CODER_THREADS_MAX;
  175. }
  176. else if (t1 <= 0)
  177. {
  178. t1 = t3 / t2;
  179. if (t1 == 0)
  180. t1 = 1;
  181. }
  182. else
  183. t3 = t1n * t2;
  184. p->lzmaProps.numThreads = t1;
  185. LzmaEncProps_Normalize(&p->lzmaProps);
  186. if (p->blockSize == 0)
  187. {
  188. UInt32 dictSize = p->lzmaProps.dictSize;
  189. UInt64 blockSize = (UInt64)dictSize << 2;
  190. const UInt32 kMinSize = (UInt32)1 << 20;
  191. const UInt32 kMaxSize = (UInt32)1 << 28;
  192. if (blockSize < kMinSize) blockSize = kMinSize;
  193. if (blockSize > kMaxSize) blockSize = kMaxSize;
  194. if (blockSize < dictSize) blockSize = dictSize;
  195. p->blockSize = (size_t)blockSize;
  196. }
  197. if (t2 > 1)
  198. {
  199. UInt64 temp = p->lzmaProps.reduceSize + p->blockSize - 1;
  200. if (temp > p->lzmaProps.reduceSize)
  201. {
  202. UInt64 numBlocks = temp / p->blockSize;
  203. if (numBlocks < t2)
  204. {
  205. t2 = (UInt32)numBlocks;
  206. t3 = t1 * t2;
  207. }
  208. }
  209. }
  210. p->numBlockThreads = t2;
  211. p->numTotalThreads = t3;
  212. }
  213. static SRes Progress(ICompressProgress *p, UInt64 inSize, UInt64 outSize)
  214. {
  215. return (p && p->Progress(p, inSize, outSize) != SZ_OK) ? SZ_ERROR_PROGRESS : SZ_OK;
  216. }
  217. /* ---------- Lzma2 ---------- */
  218. typedef struct
  219. {
  220. Byte propEncoded;
  221. CLzma2EncProps props;
  222. Byte *outBuf;
  223. ISzAlloc *alloc;
  224. ISzAlloc *allocBig;
  225. CLzma2EncInt coders[NUM_MT_CODER_THREADS_MAX];
  226. #ifndef _7ZIP_ST
  227. CMtCoder mtCoder;
  228. #endif
  229. } CLzma2Enc;
  230. /* ---------- Lzma2EncThread ---------- */
  231. static SRes Lzma2Enc_EncodeMt1(CLzma2EncInt *p, CLzma2Enc *mainEncoder,
  232. ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress)
  233. {
  234. UInt64 packTotal = 0;
  235. SRes res = SZ_OK;
  236. if (mainEncoder->outBuf == 0)
  237. {
  238. mainEncoder->outBuf = (Byte *)IAlloc_Alloc(mainEncoder->alloc, LZMA2_CHUNK_SIZE_COMPRESSED_MAX);
  239. if (mainEncoder->outBuf == 0)
  240. return SZ_ERROR_MEM;
  241. }
  242. RINOK(Lzma2EncInt_Init(p, &mainEncoder->props));
  243. RINOK(LzmaEnc_PrepareForLzma2(p->enc, inStream, LZMA2_KEEP_WINDOW_SIZE,
  244. mainEncoder->alloc, mainEncoder->allocBig));
  245. for (;;)
  246. {
  247. size_t packSize = LZMA2_CHUNK_SIZE_COMPRESSED_MAX;
  248. res = Lzma2EncInt_EncodeSubblock(p, mainEncoder->outBuf, &packSize, outStream);
  249. if (res != SZ_OK)
  250. break;
  251. packTotal += packSize;
  252. res = Progress(progress, p->srcPos, packTotal);
  253. if (res != SZ_OK)
  254. break;
  255. if (packSize == 0)
  256. break;
  257. }
  258. LzmaEnc_Finish(p->enc);
  259. if (res == SZ_OK)
  260. {
  261. Byte b = 0;
  262. if (outStream->Write(outStream, &b, 1) != 1)
  263. return SZ_ERROR_WRITE;
  264. }
  265. return res;
  266. }
  267. #ifndef _7ZIP_ST
  268. typedef struct
  269. {
  270. IMtCoderCallback funcTable;
  271. CLzma2Enc *lzma2Enc;
  272. } CMtCallbackImp;
  273. static SRes MtCallbackImp_Code(void *pp, unsigned index, Byte *dest, size_t *destSize,
  274. const Byte *src, size_t srcSize, int finished)
  275. {
  276. CMtCallbackImp *imp = (CMtCallbackImp *)pp;
  277. CLzma2Enc *mainEncoder = imp->lzma2Enc;
  278. CLzma2EncInt *p = &mainEncoder->coders[index];
  279. SRes res = SZ_OK;
  280. {
  281. size_t destLim = *destSize;
  282. *destSize = 0;
  283. if (srcSize != 0)
  284. {
  285. RINOK(Lzma2EncInt_Init(p, &mainEncoder->props));
  286. RINOK(LzmaEnc_MemPrepare(p->enc, src, srcSize, LZMA2_KEEP_WINDOW_SIZE,
  287. mainEncoder->alloc, mainEncoder->allocBig));
  288. while (p->srcPos < srcSize)
  289. {
  290. size_t packSize = destLim - *destSize;
  291. res = Lzma2EncInt_EncodeSubblock(p, dest + *destSize, &packSize, NULL);
  292. if (res != SZ_OK)
  293. break;
  294. *destSize += packSize;
  295. if (packSize == 0)
  296. {
  297. res = SZ_ERROR_FAIL;
  298. break;
  299. }
  300. if (MtProgress_Set(&mainEncoder->mtCoder.mtProgress, index, p->srcPos, *destSize) != SZ_OK)
  301. {
  302. res = SZ_ERROR_PROGRESS;
  303. break;
  304. }
  305. }
  306. LzmaEnc_Finish(p->enc);
  307. if (res != SZ_OK)
  308. return res;
  309. }
  310. if (finished)
  311. {
  312. if (*destSize == destLim)
  313. return SZ_ERROR_OUTPUT_EOF;
  314. dest[(*destSize)++] = 0;
  315. }
  316. }
  317. return res;
  318. }
  319. #endif
  320. /* ---------- Lzma2Enc ---------- */
  321. CLzma2EncHandle Lzma2Enc_Create(ISzAlloc *alloc, ISzAlloc *allocBig)
  322. {
  323. CLzma2Enc *p = (CLzma2Enc *)alloc->Alloc(alloc, sizeof(CLzma2Enc));
  324. if (p == 0)
  325. return NULL;
  326. Lzma2EncProps_Init(&p->props);
  327. Lzma2EncProps_Normalize(&p->props);
  328. p->outBuf = 0;
  329. p->alloc = alloc;
  330. p->allocBig = allocBig;
  331. {
  332. unsigned i;
  333. for (i = 0; i < NUM_MT_CODER_THREADS_MAX; i++)
  334. p->coders[i].enc = 0;
  335. }
  336. #ifndef _7ZIP_ST
  337. MtCoder_Construct(&p->mtCoder);
  338. #endif
  339. return p;
  340. }
  341. void Lzma2Enc_Destroy(CLzma2EncHandle pp)
  342. {
  343. CLzma2Enc *p = (CLzma2Enc *)pp;
  344. unsigned i;
  345. for (i = 0; i < NUM_MT_CODER_THREADS_MAX; i++)
  346. {
  347. CLzma2EncInt *t = &p->coders[i];
  348. if (t->enc)
  349. {
  350. LzmaEnc_Destroy(t->enc, p->alloc, p->allocBig);
  351. t->enc = 0;
  352. }
  353. }
  354. #ifndef _7ZIP_ST
  355. MtCoder_Destruct(&p->mtCoder);
  356. #endif
  357. IAlloc_Free(p->alloc, p->outBuf);
  358. IAlloc_Free(p->alloc, pp);
  359. }
  360. SRes Lzma2Enc_SetProps(CLzma2EncHandle pp, const CLzma2EncProps *props)
  361. {
  362. CLzma2Enc *p = (CLzma2Enc *)pp;
  363. CLzmaEncProps lzmaProps = props->lzmaProps;
  364. LzmaEncProps_Normalize(&lzmaProps);
  365. if (lzmaProps.lc + lzmaProps.lp > LZMA2_LCLP_MAX)
  366. return SZ_ERROR_PARAM;
  367. p->props = *props;
  368. Lzma2EncProps_Normalize(&p->props);
  369. return SZ_OK;
  370. }
  371. Byte Lzma2Enc_WriteProperties(CLzma2EncHandle pp)
  372. {
  373. CLzma2Enc *p = (CLzma2Enc *)pp;
  374. unsigned i;
  375. UInt32 dicSize = LzmaEncProps_GetDictSize(&p->props.lzmaProps);
  376. for (i = 0; i < 40; i++)
  377. if (dicSize <= LZMA2_DIC_SIZE_FROM_PROP(i))
  378. break;
  379. return (Byte)i;
  380. }
  381. SRes Lzma2Enc_Encode(CLzma2EncHandle pp,
  382. ISeqOutStream *outStream, ISeqInStream *inStream, ICompressProgress *progress)
  383. {
  384. CLzma2Enc *p = (CLzma2Enc *)pp;
  385. int i;
  386. for (i = 0; i < p->props.numBlockThreads; i++)
  387. {
  388. CLzma2EncInt *t = &p->coders[i];
  389. if (t->enc == NULL)
  390. {
  391. t->enc = LzmaEnc_Create(p->alloc);
  392. if (t->enc == NULL)
  393. return SZ_ERROR_MEM;
  394. }
  395. }
  396. #ifndef _7ZIP_ST
  397. if (p->props.numBlockThreads <= 1)
  398. #endif
  399. return Lzma2Enc_EncodeMt1(&p->coders[0], p, outStream, inStream, progress);
  400. #ifndef _7ZIP_ST
  401. {
  402. CMtCallbackImp mtCallback;
  403. mtCallback.funcTable.Code = MtCallbackImp_Code;
  404. mtCallback.lzma2Enc = p;
  405. p->mtCoder.progress = progress;
  406. p->mtCoder.inStream = inStream;
  407. p->mtCoder.outStream = outStream;
  408. p->mtCoder.alloc = p->alloc;
  409. p->mtCoder.mtCallback = &mtCallback.funcTable;
  410. p->mtCoder.blockSize = p->props.blockSize;
  411. p->mtCoder.destBlockSize = p->props.blockSize + (p->props.blockSize >> 10) + 16;
  412. p->mtCoder.numThreads = p->props.numBlockThreads;
  413. return MtCoder_Code(&p->mtCoder);
  414. }
  415. #endif
  416. }