1/* XzIn.c - Xz input
22015-11-08 : Igor Pavlov : Public domain */
3
4#include "Precomp.h"
5
6#include <string.h>
7
8#include "7zCrc.h"
9#include "CpuArch.h"
10#include "Xz.h"
11
12SRes Xz_ReadHeader(CXzStreamFlags *p, ISeqInStream *inStream)
13{
14  Byte sig[XZ_STREAM_HEADER_SIZE];
15  RINOK(SeqInStream_Read2(inStream, sig, XZ_STREAM_HEADER_SIZE, SZ_ERROR_NO_ARCHIVE));
16  if (memcmp(sig, XZ_SIG, XZ_SIG_SIZE) != 0)
17    return SZ_ERROR_NO_ARCHIVE;
18  return Xz_ParseHeader(p, sig);
19}
20
21#define READ_VARINT_AND_CHECK(buf, pos, size, res) \
22  { unsigned s = Xz_ReadVarInt(buf + pos, size - pos, res); \
23  if (s == 0) return SZ_ERROR_ARCHIVE; pos += s; }
24
25SRes XzBlock_ReadHeader(CXzBlock *p, ISeqInStream *inStream, Bool *isIndex, UInt32 *headerSizeRes)
26{
27  Byte header[XZ_BLOCK_HEADER_SIZE_MAX];
28  unsigned headerSize;
29  *headerSizeRes = 0;
30  RINOK(SeqInStream_ReadByte(inStream, &header[0]));
31  headerSize = ((unsigned)header[0] << 2) + 4;
32  if (headerSize == 0)
33  {
34    *headerSizeRes = 1;
35    *isIndex = True;
36    return SZ_OK;
37  }
38
39  *isIndex = False;
40  *headerSizeRes = headerSize;
41  RINOK(SeqInStream_Read(inStream, header + 1, headerSize - 1));
42  return XzBlock_Parse(p, header);
43}
44
45#define ADD_SIZE_CHECH(size, val) \
46  { UInt64 newSize = size + (val); if (newSize < size) return XZ_SIZE_OVERFLOW; size = newSize; }
47
48UInt64 Xz_GetUnpackSize(const CXzStream *p)
49{
50  UInt64 size = 0;
51  size_t i;
52  for (i = 0; i < p->numBlocks; i++)
53    ADD_SIZE_CHECH(size, p->blocks[i].unpackSize);
54  return size;
55}
56
57UInt64 Xz_GetPackSize(const CXzStream *p)
58{
59  UInt64 size = 0;
60  size_t i;
61  for (i = 0; i < p->numBlocks; i++)
62    ADD_SIZE_CHECH(size, (p->blocks[i].totalSize + 3) & ~(UInt64)3);
63  return size;
64}
65
66/*
67SRes XzBlock_ReadFooter(CXzBlock *p, CXzStreamFlags f, ISeqInStream *inStream)
68{
69  return SeqInStream_Read(inStream, p->check, XzFlags_GetCheckSize(f));
70}
71*/
72
73static SRes Xz_ReadIndex2(CXzStream *p, const Byte *buf, size_t size, ISzAlloc *alloc)
74{
75  size_t numBlocks, pos = 1;
76  UInt32 crc;
77
78  if (size < 5 || buf[0] != 0)
79    return SZ_ERROR_ARCHIVE;
80
81  size -= 4;
82  crc = CrcCalc(buf, size);
83  if (crc != GetUi32(buf + size))
84    return SZ_ERROR_ARCHIVE;
85
86  {
87    UInt64 numBlocks64;
88    READ_VARINT_AND_CHECK(buf, pos, size, &numBlocks64);
89    numBlocks = (size_t)numBlocks64;
90    if (numBlocks != numBlocks64 || numBlocks * 2 > size)
91      return SZ_ERROR_ARCHIVE;
92  }
93
94  Xz_Free(p, alloc);
95  if (numBlocks != 0)
96  {
97    size_t i;
98    p->numBlocks = numBlocks;
99    p->numBlocksAllocated = numBlocks;
100    p->blocks = alloc->Alloc(alloc, sizeof(CXzBlockSizes) * numBlocks);
101    if (p->blocks == 0)
102      return SZ_ERROR_MEM;
103    for (i = 0; i < numBlocks; i++)
104    {
105      CXzBlockSizes *block = &p->blocks[i];
106      READ_VARINT_AND_CHECK(buf, pos, size, &block->totalSize);
107      READ_VARINT_AND_CHECK(buf, pos, size, &block->unpackSize);
108      if (block->totalSize == 0)
109        return SZ_ERROR_ARCHIVE;
110    }
111  }
112  while ((pos & 3) != 0)
113    if (buf[pos++] != 0)
114      return SZ_ERROR_ARCHIVE;
115  return (pos == size) ? SZ_OK : SZ_ERROR_ARCHIVE;
116}
117
118static SRes Xz_ReadIndex(CXzStream *p, ILookInStream *stream, UInt64 indexSize, ISzAlloc *alloc)
119{
120  SRes res;
121  size_t size;
122  Byte *buf;
123  if (indexSize > ((UInt32)1 << 31))
124    return SZ_ERROR_UNSUPPORTED;
125  size = (size_t)indexSize;
126  if (size != indexSize)
127    return SZ_ERROR_UNSUPPORTED;
128  buf = alloc->Alloc(alloc, size);
129  if (buf == 0)
130    return SZ_ERROR_MEM;
131  res = LookInStream_Read2(stream, buf, size, SZ_ERROR_UNSUPPORTED);
132  if (res == SZ_OK)
133    res = Xz_ReadIndex2(p, buf, size, alloc);
134  alloc->Free(alloc, buf);
135  return res;
136}
137
138static SRes LookInStream_SeekRead_ForArc(ILookInStream *stream, UInt64 offset, void *buf, size_t size)
139{
140  RINOK(LookInStream_SeekTo(stream, offset));
141  return LookInStream_Read(stream, buf, size);
142  /* return LookInStream_Read2(stream, buf, size, SZ_ERROR_NO_ARCHIVE); */
143}
144
145static SRes Xz_ReadBackward(CXzStream *p, ILookInStream *stream, Int64 *startOffset, ISzAlloc *alloc)
146{
147  UInt64 indexSize;
148  Byte buf[XZ_STREAM_FOOTER_SIZE];
149  UInt64 pos = *startOffset;
150
151  if ((pos & 3) != 0 || pos < XZ_STREAM_FOOTER_SIZE)
152    return SZ_ERROR_NO_ARCHIVE;
153
154  pos -= XZ_STREAM_FOOTER_SIZE;
155  RINOK(LookInStream_SeekRead_ForArc(stream, pos, buf, XZ_STREAM_FOOTER_SIZE));
156
157  if (memcmp(buf + 10, XZ_FOOTER_SIG, XZ_FOOTER_SIG_SIZE) != 0)
158  {
159    UInt32 total = 0;
160    pos += XZ_STREAM_FOOTER_SIZE;
161
162    for (;;)
163    {
164      size_t i;
165      #define TEMP_BUF_SIZE (1 << 10)
166      Byte temp[TEMP_BUF_SIZE];
167
168      i = (pos > TEMP_BUF_SIZE) ? TEMP_BUF_SIZE : (size_t)pos;
169      pos -= i;
170      RINOK(LookInStream_SeekRead_ForArc(stream, pos, temp, i));
171      total += (UInt32)i;
172      for (; i != 0; i--)
173        if (temp[i - 1] != 0)
174          break;
175      if (i != 0)
176      {
177        if ((i & 3) != 0)
178          return SZ_ERROR_NO_ARCHIVE;
179        pos += i;
180        break;
181      }
182      if (pos < XZ_STREAM_FOOTER_SIZE || total > (1 << 16))
183        return SZ_ERROR_NO_ARCHIVE;
184    }
185
186    if (pos < XZ_STREAM_FOOTER_SIZE)
187      return SZ_ERROR_NO_ARCHIVE;
188    pos -= XZ_STREAM_FOOTER_SIZE;
189    RINOK(LookInStream_SeekRead_ForArc(stream, pos, buf, XZ_STREAM_FOOTER_SIZE));
190    if (memcmp(buf + 10, XZ_FOOTER_SIG, XZ_FOOTER_SIG_SIZE) != 0)
191      return SZ_ERROR_NO_ARCHIVE;
192  }
193
194  p->flags = (CXzStreamFlags)GetBe16(buf + 8);
195
196  if (!XzFlags_IsSupported(p->flags))
197    return SZ_ERROR_UNSUPPORTED;
198
199  if (GetUi32(buf) != CrcCalc(buf + 4, 6))
200    return SZ_ERROR_ARCHIVE;
201
202  indexSize = ((UInt64)GetUi32(buf + 4) + 1) << 2;
203
204  if (pos < indexSize)
205    return SZ_ERROR_ARCHIVE;
206
207  pos -= indexSize;
208  RINOK(LookInStream_SeekTo(stream, pos));
209  RINOK(Xz_ReadIndex(p, stream, indexSize, alloc));
210
211  {
212    UInt64 totalSize = Xz_GetPackSize(p);
213    if (totalSize == XZ_SIZE_OVERFLOW
214        || totalSize >= ((UInt64)1 << 63)
215        || pos < totalSize + XZ_STREAM_HEADER_SIZE)
216      return SZ_ERROR_ARCHIVE;
217    pos -= (totalSize + XZ_STREAM_HEADER_SIZE);
218    RINOK(LookInStream_SeekTo(stream, pos));
219    *startOffset = pos;
220  }
221  {
222    CXzStreamFlags headerFlags;
223    CSecToRead secToRead;
224    SecToRead_CreateVTable(&secToRead);
225    secToRead.realStream = stream;
226
227    RINOK(Xz_ReadHeader(&headerFlags, &secToRead.s));
228    return (p->flags == headerFlags) ? SZ_OK : SZ_ERROR_ARCHIVE;
229  }
230}
231
232
233/* ---------- Xz Streams ---------- */
234
235void Xzs_Construct(CXzs *p)
236{
237  p->num = p->numAllocated = 0;
238  p->streams = 0;
239}
240
241void Xzs_Free(CXzs *p, ISzAlloc *alloc)
242{
243  size_t i;
244  for (i = 0; i < p->num; i++)
245    Xz_Free(&p->streams[i], alloc);
246  alloc->Free(alloc, p->streams);
247  p->num = p->numAllocated = 0;
248  p->streams = 0;
249}
250
251UInt64 Xzs_GetNumBlocks(const CXzs *p)
252{
253  UInt64 num = 0;
254  size_t i;
255  for (i = 0; i < p->num; i++)
256    num += p->streams[i].numBlocks;
257  return num;
258}
259
260UInt64 Xzs_GetUnpackSize(const CXzs *p)
261{
262  UInt64 size = 0;
263  size_t i;
264  for (i = 0; i < p->num; i++)
265    ADD_SIZE_CHECH(size, Xz_GetUnpackSize(&p->streams[i]));
266  return size;
267}
268
269/*
270UInt64 Xzs_GetPackSize(const CXzs *p)
271{
272  UInt64 size = 0;
273  size_t i;
274  for (i = 0; i < p->num; i++)
275    ADD_SIZE_CHECH(size, Xz_GetTotalSize(&p->streams[i]));
276  return size;
277}
278*/
279
280SRes Xzs_ReadBackward(CXzs *p, ILookInStream *stream, Int64 *startOffset, ICompressProgress *progress, ISzAlloc *alloc)
281{
282  Int64 endOffset = 0;
283  RINOK(stream->Seek(stream, &endOffset, SZ_SEEK_END));
284  *startOffset = endOffset;
285  for (;;)
286  {
287    CXzStream st;
288    SRes res;
289    Xz_Construct(&st);
290    res = Xz_ReadBackward(&st, stream, startOffset, alloc);
291    st.startOffset = *startOffset;
292    RINOK(res);
293    if (p->num == p->numAllocated)
294    {
295      size_t newNum = p->num + p->num / 4 + 1;
296      Byte *data = (Byte *)alloc->Alloc(alloc, newNum * sizeof(CXzStream));
297      if (data == 0)
298        return SZ_ERROR_MEM;
299      p->numAllocated = newNum;
300      if (p->num != 0)
301        memcpy(data, p->streams, p->num * sizeof(CXzStream));
302      alloc->Free(alloc, p->streams);
303      p->streams = (CXzStream *)data;
304    }
305    p->streams[p->num++] = st;
306    if (*startOffset == 0)
307      break;
308    RINOK(LookInStream_SeekTo(stream, *startOffset));
309    if (progress && progress->Progress(progress, endOffset - *startOffset, (UInt64)(Int64)-1) != SZ_OK)
310      return SZ_ERROR_PROGRESS;
311  }
312  return SZ_OK;
313}
314