1/* XzIn.c - Xz input
22014-12-30 : 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 i, 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    p->numBlocks = numBlocks;
98    p->numBlocksAllocated = numBlocks;
99    p->blocks = alloc->Alloc(alloc, sizeof(CXzBlockSizes) * numBlocks);
100    if (p->blocks == 0)
101      return SZ_ERROR_MEM;
102    for (i = 0; i < numBlocks; i++)
103    {
104      CXzBlockSizes *block = &p->blocks[i];
105      READ_VARINT_AND_CHECK(buf, pos, size, &block->totalSize);
106      READ_VARINT_AND_CHECK(buf, pos, size, &block->unpackSize);
107      if (block->totalSize == 0)
108        return SZ_ERROR_ARCHIVE;
109    }
110  }
111  while ((pos & 3) != 0)
112    if (buf[pos++] != 0)
113      return SZ_ERROR_ARCHIVE;
114  return (pos == size) ? SZ_OK : SZ_ERROR_ARCHIVE;
115}
116
117static SRes Xz_ReadIndex(CXzStream *p, ILookInStream *stream, UInt64 indexSize, ISzAlloc *alloc)
118{
119  SRes res;
120  size_t size;
121  Byte *buf;
122  if (indexSize > ((UInt32)1 << 31))
123    return SZ_ERROR_UNSUPPORTED;
124  size = (size_t)indexSize;
125  if (size != indexSize)
126    return SZ_ERROR_UNSUPPORTED;
127  buf = alloc->Alloc(alloc, size);
128  if (buf == 0)
129    return SZ_ERROR_MEM;
130  res = LookInStream_Read2(stream, buf, size, SZ_ERROR_UNSUPPORTED);
131  if (res == SZ_OK)
132    res = Xz_ReadIndex2(p, buf, size, alloc);
133  alloc->Free(alloc, buf);
134  return res;
135}
136
137static SRes SeekFromCur(ILookInStream *inStream, Int64 *res)
138{
139  return inStream->Seek(inStream, res, SZ_SEEK_CUR);
140}
141
142static SRes Xz_ReadBackward(CXzStream *p, ILookInStream *stream, Int64 *startOffset, ISzAlloc *alloc)
143{
144  UInt64 indexSize;
145  Byte buf[XZ_STREAM_FOOTER_SIZE];
146
147  if ((*startOffset & 3) != 0 || *startOffset < XZ_STREAM_FOOTER_SIZE)
148    return SZ_ERROR_NO_ARCHIVE;
149  *startOffset = -XZ_STREAM_FOOTER_SIZE;
150  RINOK(SeekFromCur(stream, startOffset));
151
152  RINOK(LookInStream_Read2(stream, buf, XZ_STREAM_FOOTER_SIZE, SZ_ERROR_NO_ARCHIVE));
153
154  if (memcmp(buf + 10, XZ_FOOTER_SIG, XZ_FOOTER_SIG_SIZE) != 0)
155  {
156    UInt32 total = 0;
157    *startOffset += XZ_STREAM_FOOTER_SIZE;
158    for (;;)
159    {
160      size_t i;
161      #define TEMP_BUF_SIZE (1 << 10)
162      Byte tempBuf[TEMP_BUF_SIZE];
163      if (*startOffset < XZ_STREAM_FOOTER_SIZE || total > (1 << 16))
164        return SZ_ERROR_NO_ARCHIVE;
165      i = (*startOffset > TEMP_BUF_SIZE) ? TEMP_BUF_SIZE : (size_t)*startOffset;
166      total += (UInt32)i;
167      *startOffset = -(Int64)i;
168      RINOK(SeekFromCur(stream, startOffset));
169      RINOK(LookInStream_Read2(stream, tempBuf, i, SZ_ERROR_NO_ARCHIVE));
170      for (; i != 0; i--)
171        if (tempBuf[i - 1] != 0)
172          break;
173      if (i != 0)
174      {
175        if ((i & 3) != 0)
176          return SZ_ERROR_NO_ARCHIVE;
177        *startOffset += i;
178        break;
179      }
180    }
181    if (*startOffset < XZ_STREAM_FOOTER_SIZE)
182      return SZ_ERROR_NO_ARCHIVE;
183    *startOffset -= XZ_STREAM_FOOTER_SIZE;
184    RINOK(stream->Seek(stream, startOffset, SZ_SEEK_SET));
185    RINOK(LookInStream_Read2(stream, buf, XZ_STREAM_FOOTER_SIZE, SZ_ERROR_NO_ARCHIVE));
186    if (memcmp(buf + 10, XZ_FOOTER_SIG, XZ_FOOTER_SIG_SIZE) != 0)
187      return SZ_ERROR_NO_ARCHIVE;
188  }
189
190  p->flags = (CXzStreamFlags)GetBe16(buf + 8);
191
192  if (!XzFlags_IsSupported(p->flags))
193    return SZ_ERROR_UNSUPPORTED;
194
195  if (GetUi32(buf) != CrcCalc(buf + 4, 6))
196    return SZ_ERROR_ARCHIVE;
197
198  indexSize = ((UInt64)GetUi32(buf + 4) + 1) << 2;
199
200  *startOffset = -(Int64)(indexSize + XZ_STREAM_FOOTER_SIZE);
201  RINOK(SeekFromCur(stream, startOffset));
202
203  RINOK(Xz_ReadIndex(p, stream, indexSize, alloc));
204
205  {
206    UInt64 totalSize = Xz_GetPackSize(p);
207    UInt64 sum = XZ_STREAM_HEADER_SIZE + totalSize + indexSize;
208    if (totalSize == XZ_SIZE_OVERFLOW ||
209      sum >= ((UInt64)1 << 63) ||
210      totalSize >= ((UInt64)1 << 63))
211      return SZ_ERROR_ARCHIVE;
212    *startOffset = -(Int64)sum;
213    RINOK(SeekFromCur(stream, startOffset));
214  }
215  {
216    CXzStreamFlags headerFlags;
217    CSecToRead secToRead;
218    SecToRead_CreateVTable(&secToRead);
219    secToRead.realStream = stream;
220
221    RINOK(Xz_ReadHeader(&headerFlags, &secToRead.s));
222    return (p->flags == headerFlags) ? SZ_OK : SZ_ERROR_ARCHIVE;
223  }
224}
225
226
227/* ---------- Xz Streams ---------- */
228
229void Xzs_Construct(CXzs *p)
230{
231  p->num = p->numAllocated = 0;
232  p->streams = 0;
233}
234
235void Xzs_Free(CXzs *p, ISzAlloc *alloc)
236{
237  size_t i;
238  for (i = 0; i < p->num; i++)
239    Xz_Free(&p->streams[i], alloc);
240  alloc->Free(alloc, p->streams);
241  p->num = p->numAllocated = 0;
242  p->streams = 0;
243}
244
245UInt64 Xzs_GetNumBlocks(const CXzs *p)
246{
247  UInt64 num = 0;
248  size_t i;
249  for (i = 0; i < p->num; i++)
250    num += p->streams[i].numBlocks;
251  return num;
252}
253
254UInt64 Xzs_GetUnpackSize(const CXzs *p)
255{
256  UInt64 size = 0;
257  size_t i;
258  for (i = 0; i < p->num; i++)
259    ADD_SIZE_CHECH(size, Xz_GetUnpackSize(&p->streams[i]));
260  return size;
261}
262
263/*
264UInt64 Xzs_GetPackSize(const CXzs *p)
265{
266  UInt64 size = 0;
267  size_t i;
268  for (i = 0; i < p->num; i++)
269    ADD_SIZE_CHECH(size, Xz_GetTotalSize(&p->streams[i]));
270  return size;
271}
272*/
273
274SRes Xzs_ReadBackward(CXzs *p, ILookInStream *stream, Int64 *startOffset, ICompressProgress *progress, ISzAlloc *alloc)
275{
276  Int64 endOffset = 0;
277  RINOK(stream->Seek(stream, &endOffset, SZ_SEEK_END));
278  *startOffset = endOffset;
279  for (;;)
280  {
281    CXzStream st;
282    SRes res;
283    Xz_Construct(&st);
284    res = Xz_ReadBackward(&st, stream, startOffset, alloc);
285    st.startOffset = *startOffset;
286    RINOK(res);
287    if (p->num == p->numAllocated)
288    {
289      size_t newNum = p->num + p->num / 4 + 1;
290      Byte *data = (Byte *)alloc->Alloc(alloc, newNum * sizeof(CXzStream));
291      if (data == 0)
292        return SZ_ERROR_MEM;
293      p->numAllocated = newNum;
294      if (p->num != 0)
295        memcpy(data, p->streams, p->num * sizeof(CXzStream));
296      alloc->Free(alloc, p->streams);
297      p->streams = (CXzStream *)data;
298    }
299    p->streams[p->num++] = st;
300    if (*startOffset == 0)
301      break;
302    RINOK(stream->Seek(stream, startOffset, SZ_SEEK_SET));
303    if (progress && progress->Progress(progress, endOffset - *startOffset, (UInt64)(Int64)-1) != SZ_OK)
304      return SZ_ERROR_PROGRESS;
305  }
306  return SZ_OK;
307}
308