XzEnc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. /* XzEnc.c -- Xz Encode
  2. 2015-09-16 : Igor Pavlov : Public domain */
  3. #include "Precomp.h"
  4. #include <stdlib.h>
  5. #include <string.h>
  6. #include "7zCrc.h"
  7. #include "Alloc.h"
  8. #include "Bra.h"
  9. #include "CpuArch.h"
  10. #ifdef USE_SUBBLOCK
  11. #include "Bcj3Enc.c"
  12. #include "SbFind.c"
  13. #include "SbEnc.c"
  14. #endif
  15. #include "XzEnc.h"
  16. #define XzBlock_ClearFlags(p) (p)->flags = 0;
  17. #define XzBlock_SetNumFilters(p, n) (p)->flags |= ((n) - 1);
  18. #define XzBlock_SetHasPackSize(p) (p)->flags |= XZ_BF_PACK_SIZE;
  19. #define XzBlock_SetHasUnpackSize(p) (p)->flags |= XZ_BF_UNPACK_SIZE;
  20. static SRes WriteBytes(ISeqOutStream *s, const void *buf, UInt32 size)
  21. {
  22. return (s->Write(s, buf, size) == size) ? SZ_OK : SZ_ERROR_WRITE;
  23. }
  24. static SRes WriteBytesAndCrc(ISeqOutStream *s, const void *buf, UInt32 size, UInt32 *crc)
  25. {
  26. *crc = CrcUpdate(*crc, buf, size);
  27. return WriteBytes(s, buf, size);
  28. }
  29. static SRes Xz_WriteHeader(CXzStreamFlags f, ISeqOutStream *s)
  30. {
  31. UInt32 crc;
  32. Byte header[XZ_STREAM_HEADER_SIZE];
  33. memcpy(header, XZ_SIG, XZ_SIG_SIZE);
  34. header[XZ_SIG_SIZE] = (Byte)(f >> 8);
  35. header[XZ_SIG_SIZE + 1] = (Byte)(f & 0xFF);
  36. crc = CrcCalc(header + XZ_SIG_SIZE, XZ_STREAM_FLAGS_SIZE);
  37. SetUi32(header + XZ_SIG_SIZE + XZ_STREAM_FLAGS_SIZE, crc);
  38. return WriteBytes(s, header, XZ_STREAM_HEADER_SIZE);
  39. }
  40. static SRes XzBlock_WriteHeader(const CXzBlock *p, ISeqOutStream *s)
  41. {
  42. Byte header[XZ_BLOCK_HEADER_SIZE_MAX];
  43. unsigned pos = 1;
  44. unsigned numFilters, i;
  45. header[pos++] = p->flags;
  46. if (XzBlock_HasPackSize(p)) pos += Xz_WriteVarInt(header + pos, p->packSize);
  47. if (XzBlock_HasUnpackSize(p)) pos += Xz_WriteVarInt(header + pos, p->unpackSize);
  48. numFilters = XzBlock_GetNumFilters(p);
  49. for (i = 0; i < numFilters; i++)
  50. {
  51. const CXzFilter *f = &p->filters[i];
  52. pos += Xz_WriteVarInt(header + pos, f->id);
  53. pos += Xz_WriteVarInt(header + pos, f->propsSize);
  54. memcpy(header + pos, f->props, f->propsSize);
  55. pos += f->propsSize;
  56. }
  57. while ((pos & 3) != 0)
  58. header[pos++] = 0;
  59. header[0] = (Byte)(pos >> 2);
  60. SetUi32(header + pos, CrcCalc(header, pos));
  61. return WriteBytes(s, header, pos + 4);
  62. }
  63. static SRes Xz_WriteFooter(CXzStream *p, ISeqOutStream *s)
  64. {
  65. Byte buf[32];
  66. UInt64 globalPos;
  67. {
  68. UInt32 crc = CRC_INIT_VAL;
  69. unsigned pos = 1 + Xz_WriteVarInt(buf + 1, p->numBlocks);
  70. size_t i;
  71. globalPos = pos;
  72. buf[0] = 0;
  73. RINOK(WriteBytesAndCrc(s, buf, pos, &crc));
  74. for (i = 0; i < p->numBlocks; i++)
  75. {
  76. const CXzBlockSizes *block = &p->blocks[i];
  77. pos = Xz_WriteVarInt(buf, block->totalSize);
  78. pos += Xz_WriteVarInt(buf + pos, block->unpackSize);
  79. globalPos += pos;
  80. RINOK(WriteBytesAndCrc(s, buf, pos, &crc));
  81. }
  82. pos = ((unsigned)globalPos & 3);
  83. if (pos != 0)
  84. {
  85. buf[0] = buf[1] = buf[2] = 0;
  86. RINOK(WriteBytesAndCrc(s, buf, 4 - pos, &crc));
  87. globalPos += 4 - pos;
  88. }
  89. {
  90. SetUi32(buf, CRC_GET_DIGEST(crc));
  91. RINOK(WriteBytes(s, buf, 4));
  92. globalPos += 4;
  93. }
  94. }
  95. {
  96. UInt32 indexSize = (UInt32)((globalPos >> 2) - 1);
  97. SetUi32(buf + 4, indexSize);
  98. buf[8] = (Byte)(p->flags >> 8);
  99. buf[9] = (Byte)(p->flags & 0xFF);
  100. SetUi32(buf, CrcCalc(buf + 4, 6));
  101. memcpy(buf + 10, XZ_FOOTER_SIG, XZ_FOOTER_SIG_SIZE);
  102. return WriteBytes(s, buf, 12);
  103. }
  104. }
  105. static SRes Xz_AddIndexRecord(CXzStream *p, UInt64 unpackSize, UInt64 totalSize, ISzAlloc *alloc)
  106. {
  107. if (!p->blocks || p->numBlocksAllocated == p->numBlocks)
  108. {
  109. size_t num = p->numBlocks * 2 + 1;
  110. size_t newSize = sizeof(CXzBlockSizes) * num;
  111. CXzBlockSizes *blocks;
  112. if (newSize / sizeof(CXzBlockSizes) != num)
  113. return SZ_ERROR_MEM;
  114. blocks = (CXzBlockSizes *)alloc->Alloc(alloc, newSize);
  115. if (!blocks)
  116. return SZ_ERROR_MEM;
  117. if (p->numBlocks != 0)
  118. {
  119. memcpy(blocks, p->blocks, p->numBlocks * sizeof(CXzBlockSizes));
  120. alloc->Free(alloc, p->blocks);
  121. }
  122. p->blocks = blocks;
  123. p->numBlocksAllocated = num;
  124. }
  125. {
  126. CXzBlockSizes *block = &p->blocks[p->numBlocks++];
  127. block->unpackSize = unpackSize;
  128. block->totalSize = totalSize;
  129. }
  130. return SZ_OK;
  131. }
  132. /* ---------- CSeqCheckInStream ---------- */
  133. typedef struct
  134. {
  135. ISeqInStream p;
  136. ISeqInStream *realStream;
  137. UInt64 processed;
  138. CXzCheck check;
  139. } CSeqCheckInStream;
  140. static void SeqCheckInStream_Init(CSeqCheckInStream *p, unsigned mode)
  141. {
  142. p->processed = 0;
  143. XzCheck_Init(&p->check, mode);
  144. }
  145. static void SeqCheckInStream_GetDigest(CSeqCheckInStream *p, Byte *digest)
  146. {
  147. XzCheck_Final(&p->check, digest);
  148. }
  149. static SRes SeqCheckInStream_Read(void *pp, void *data, size_t *size)
  150. {
  151. CSeqCheckInStream *p = (CSeqCheckInStream *)pp;
  152. SRes res = p->realStream->Read(p->realStream, data, size);
  153. XzCheck_Update(&p->check, data, *size);
  154. p->processed += *size;
  155. return res;
  156. }
  157. /* ---------- CSeqSizeOutStream ---------- */
  158. typedef struct
  159. {
  160. ISeqOutStream p;
  161. ISeqOutStream *realStream;
  162. UInt64 processed;
  163. } CSeqSizeOutStream;
  164. static size_t MyWrite(void *pp, const void *data, size_t size)
  165. {
  166. CSeqSizeOutStream *p = (CSeqSizeOutStream *)pp;
  167. size = p->realStream->Write(p->realStream, data, size);
  168. p->processed += size;
  169. return size;
  170. }
  171. /* ---------- CSeqInFilter ---------- */
  172. #define FILTER_BUF_SIZE (1 << 20)
  173. typedef struct
  174. {
  175. ISeqInStream p;
  176. ISeqInStream *realStream;
  177. IStateCoder StateCoder;
  178. Byte *buf;
  179. size_t curPos;
  180. size_t endPos;
  181. int srcWasFinished;
  182. } CSeqInFilter;
  183. static SRes SeqInFilter_Read(void *pp, void *data, size_t *size)
  184. {
  185. CSeqInFilter *p = (CSeqInFilter *)pp;
  186. size_t sizeOriginal = *size;
  187. if (sizeOriginal == 0)
  188. return SZ_OK;
  189. *size = 0;
  190. for (;;)
  191. {
  192. if (!p->srcWasFinished && p->curPos == p->endPos)
  193. {
  194. p->curPos = 0;
  195. p->endPos = FILTER_BUF_SIZE;
  196. RINOK(p->realStream->Read(p->realStream, p->buf, &p->endPos));
  197. if (p->endPos == 0)
  198. p->srcWasFinished = 1;
  199. }
  200. {
  201. SizeT srcLen = p->endPos - p->curPos;
  202. int wasFinished;
  203. SRes res;
  204. *size = sizeOriginal;
  205. res = p->StateCoder.Code(p->StateCoder.p, data, size, p->buf + p->curPos, &srcLen,
  206. p->srcWasFinished, CODER_FINISH_ANY, &wasFinished);
  207. p->curPos += srcLen;
  208. if (*size != 0 || srcLen == 0 || res != 0)
  209. return res;
  210. }
  211. }
  212. }
  213. static void SeqInFilter_Construct(CSeqInFilter *p)
  214. {
  215. p->buf = NULL;
  216. p->p.Read = SeqInFilter_Read;
  217. }
  218. static void SeqInFilter_Free(CSeqInFilter *p)
  219. {
  220. if (p->buf)
  221. {
  222. g_Alloc.Free(&g_Alloc, p->buf);
  223. p->buf = NULL;
  224. }
  225. }
  226. SRes BraState_SetFromMethod(IStateCoder *p, UInt64 id, int encodeMode, ISzAlloc *alloc);
  227. static SRes SeqInFilter_Init(CSeqInFilter *p, const CXzFilter *props)
  228. {
  229. if (!p->buf)
  230. {
  231. p->buf = g_Alloc.Alloc(&g_Alloc, FILTER_BUF_SIZE);
  232. if (!p->buf)
  233. return SZ_ERROR_MEM;
  234. }
  235. p->curPos = p->endPos = 0;
  236. p->srcWasFinished = 0;
  237. RINOK(BraState_SetFromMethod(&p->StateCoder, props->id, 1, &g_Alloc));
  238. RINOK(p->StateCoder.SetProps(p->StateCoder.p, props->props, props->propsSize, &g_Alloc));
  239. p->StateCoder.Init(p->StateCoder.p);
  240. return SZ_OK;
  241. }
  242. /* ---------- CSbEncInStream ---------- */
  243. #ifdef USE_SUBBLOCK
  244. typedef struct
  245. {
  246. ISeqInStream p;
  247. ISeqInStream *inStream;
  248. CSbEnc enc;
  249. } CSbEncInStream;
  250. static SRes SbEncInStream_Read(void *pp, void *data, size_t *size)
  251. {
  252. CSbEncInStream *p = (CSbEncInStream *)pp;
  253. size_t sizeOriginal = *size;
  254. if (sizeOriginal == 0)
  255. return S_OK;
  256. for (;;)
  257. {
  258. if (p->enc.needRead && !p->enc.readWasFinished)
  259. {
  260. size_t processed = p->enc.needReadSizeMax;
  261. RINOK(p->inStream->Read(p->inStream, p->enc.buf + p->enc.readPos, &processed));
  262. p->enc.readPos += processed;
  263. if (processed == 0)
  264. {
  265. p->enc.readWasFinished = True;
  266. p->enc.isFinalFinished = True;
  267. }
  268. p->enc.needRead = False;
  269. }
  270. *size = sizeOriginal;
  271. RINOK(SbEnc_Read(&p->enc, data, size));
  272. if (*size != 0 || !p->enc.needRead)
  273. return S_OK;
  274. }
  275. }
  276. void SbEncInStream_Construct(CSbEncInStream *p, ISzAlloc *alloc)
  277. {
  278. SbEnc_Construct(&p->enc, alloc);
  279. p->p.Read = SbEncInStream_Read;
  280. }
  281. SRes SbEncInStream_Init(CSbEncInStream *p)
  282. {
  283. return SbEnc_Init(&p->enc);
  284. }
  285. void SbEncInStream_Free(CSbEncInStream *p)
  286. {
  287. SbEnc_Free(&p->enc);
  288. }
  289. #endif
  290. typedef struct
  291. {
  292. CLzma2EncHandle lzma2;
  293. #ifdef USE_SUBBLOCK
  294. CSbEncInStream sb;
  295. #endif
  296. CSeqInFilter filter;
  297. ISzAlloc *alloc;
  298. ISzAlloc *bigAlloc;
  299. } CLzma2WithFilters;
  300. static void Lzma2WithFilters_Construct(CLzma2WithFilters *p, ISzAlloc *alloc, ISzAlloc *bigAlloc)
  301. {
  302. p->alloc = alloc;
  303. p->bigAlloc = bigAlloc;
  304. p->lzma2 = NULL;
  305. #ifdef USE_SUBBLOCK
  306. SbEncInStream_Construct(&p->sb, alloc);
  307. #endif
  308. SeqInFilter_Construct(&p->filter);
  309. }
  310. static SRes Lzma2WithFilters_Create(CLzma2WithFilters *p)
  311. {
  312. p->lzma2 = Lzma2Enc_Create(p->alloc, p->bigAlloc);
  313. if (!p->lzma2)
  314. return SZ_ERROR_MEM;
  315. return SZ_OK;
  316. }
  317. static void Lzma2WithFilters_Free(CLzma2WithFilters *p)
  318. {
  319. SeqInFilter_Free(&p->filter);
  320. #ifdef USE_SUBBLOCK
  321. SbEncInStream_Free(&p->sb);
  322. #endif
  323. if (p->lzma2)
  324. {
  325. Lzma2Enc_Destroy(p->lzma2);
  326. p->lzma2 = NULL;
  327. }
  328. }
  329. void XzProps_Init(CXzProps *p)
  330. {
  331. p->lzma2Props = NULL;
  332. p->filterProps = NULL;
  333. p->checkId = XZ_CHECK_CRC32;
  334. }
  335. void XzFilterProps_Init(CXzFilterProps *p)
  336. {
  337. p->id = 0;
  338. p->delta = 0;
  339. p->ip = 0;
  340. p->ipDefined = False;
  341. }
  342. static SRes Xz_Compress(CXzStream *xz, CLzma2WithFilters *lzmaf,
  343. ISeqOutStream *outStream, ISeqInStream *inStream,
  344. const CXzProps *props, ICompressProgress *progress)
  345. {
  346. xz->flags = (Byte)props->checkId;
  347. RINOK(Lzma2Enc_SetProps(lzmaf->lzma2, props->lzma2Props));
  348. RINOK(Xz_WriteHeader(xz->flags, outStream));
  349. {
  350. CSeqCheckInStream checkInStream;
  351. CSeqSizeOutStream seqSizeOutStream;
  352. CXzBlock block;
  353. unsigned filterIndex = 0;
  354. CXzFilter *filter = NULL;
  355. const CXzFilterProps *fp = props->filterProps;
  356. XzBlock_ClearFlags(&block);
  357. XzBlock_SetNumFilters(&block, 1 + (fp ? 1 : 0));
  358. if (fp)
  359. {
  360. filter = &block.filters[filterIndex++];
  361. filter->id = fp->id;
  362. filter->propsSize = 0;
  363. if (fp->id == XZ_ID_Delta)
  364. {
  365. filter->props[0] = (Byte)(fp->delta - 1);
  366. filter->propsSize = 1;
  367. }
  368. else if (fp->ipDefined)
  369. {
  370. SetUi32(filter->props, fp->ip);
  371. filter->propsSize = 4;
  372. }
  373. }
  374. {
  375. CXzFilter *f = &block.filters[filterIndex++];
  376. f->id = XZ_ID_LZMA2;
  377. f->propsSize = 1;
  378. f->props[0] = Lzma2Enc_WriteProperties(lzmaf->lzma2);
  379. }
  380. seqSizeOutStream.p.Write = MyWrite;
  381. seqSizeOutStream.realStream = outStream;
  382. seqSizeOutStream.processed = 0;
  383. RINOK(XzBlock_WriteHeader(&block, &seqSizeOutStream.p));
  384. checkInStream.p.Read = SeqCheckInStream_Read;
  385. checkInStream.realStream = inStream;
  386. SeqCheckInStream_Init(&checkInStream, XzFlags_GetCheckType(xz->flags));
  387. if (fp)
  388. {
  389. #ifdef USE_SUBBLOCK
  390. if (fp->id == XZ_ID_Subblock)
  391. {
  392. lzmaf->sb.inStream = &checkInStream.p;
  393. RINOK(SbEncInStream_Init(&lzmaf->sb));
  394. }
  395. else
  396. #endif
  397. {
  398. lzmaf->filter.realStream = &checkInStream.p;
  399. RINOK(SeqInFilter_Init(&lzmaf->filter, filter));
  400. }
  401. }
  402. {
  403. UInt64 packPos = seqSizeOutStream.processed;
  404. SRes res = Lzma2Enc_Encode(lzmaf->lzma2, &seqSizeOutStream.p,
  405. fp ?
  406. #ifdef USE_SUBBLOCK
  407. (fp->id == XZ_ID_Subblock) ? &lzmaf->sb.p:
  408. #endif
  409. &lzmaf->filter.p:
  410. &checkInStream.p,
  411. progress);
  412. RINOK(res);
  413. block.unpackSize = checkInStream.processed;
  414. block.packSize = seqSizeOutStream.processed - packPos;
  415. }
  416. {
  417. unsigned padSize = 0;
  418. Byte buf[128];
  419. while ((((unsigned)block.packSize + padSize) & 3) != 0)
  420. buf[padSize++] = 0;
  421. SeqCheckInStream_GetDigest(&checkInStream, buf + padSize);
  422. RINOK(WriteBytes(&seqSizeOutStream.p, buf, padSize + XzFlags_GetCheckSize(xz->flags)));
  423. RINOK(Xz_AddIndexRecord(xz, block.unpackSize, seqSizeOutStream.processed - padSize, &g_Alloc));
  424. }
  425. }
  426. return Xz_WriteFooter(xz, outStream);
  427. }
  428. SRes Xz_Encode(ISeqOutStream *outStream, ISeqInStream *inStream,
  429. const CXzProps *props, ICompressProgress *progress)
  430. {
  431. SRes res;
  432. CXzStream xz;
  433. CLzma2WithFilters lzmaf;
  434. Xz_Construct(&xz);
  435. Lzma2WithFilters_Construct(&lzmaf, &g_Alloc, &g_BigAlloc);
  436. res = Lzma2WithFilters_Create(&lzmaf);
  437. if (res == SZ_OK)
  438. res = Xz_Compress(&xz, &lzmaf, outStream, inStream, props, progress);
  439. Lzma2WithFilters_Free(&lzmaf);
  440. Xz_Free(&xz, &g_Alloc);
  441. return res;
  442. }
  443. SRes Xz_EncodeEmpty(ISeqOutStream *outStream)
  444. {
  445. SRes res;
  446. CXzStream xz;
  447. Xz_Construct(&xz);
  448. res = Xz_WriteHeader(xz.flags, outStream);
  449. if (res == SZ_OK)
  450. res = Xz_WriteFooter(&xz, outStream);
  451. Xz_Free(&xz, &g_Alloc);
  452. return res;
  453. }