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