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