1 /* XzIn.c - Xz input
2 2009-06-19 : Igor Pavlov : Public domain */
3 
4 #include <string.h>
5 
6 #if defined(_WIN32)
7 #include <WinSock2.h>
8 #include <Windows.h>
9 #endif
10 
11 #include "clamav.h"
12 #include "7zCrc.h"
13 #include "CpuArch.h"
14 #include "Xz.h"
15 
Xz_ReadHeader(CXzStreamFlags * p,ISeqInStream * inStream)16 SRes Xz_ReadHeader(CXzStreamFlags *p, ISeqInStream *inStream)
17 {
18   Byte sig[XZ_STREAM_HEADER_SIZE];
19   RINOK(SeqInStream_Read2(inStream, sig, XZ_STREAM_HEADER_SIZE, SZ_ERROR_NO_ARCHIVE));
20   if (memcmp(sig, XZ_SIG, XZ_SIG_SIZE) != 0)
21     return SZ_ERROR_NO_ARCHIVE;
22   return Xz_ParseHeader(p, sig);
23 }
24 
25 #define READ_VARINT_AND_CHECK(buf, pos, size, res) \
26   { unsigned s = Xz_ReadVarInt(buf + pos, size - pos, res); \
27   if (s == 0) return SZ_ERROR_ARCHIVE; pos += s; }
28 
XzBlock_ReadHeader(CXzBlock * p,ISeqInStream * inStream,Bool * isIndex,UInt32 * headerSizeRes)29 SRes XzBlock_ReadHeader(CXzBlock *p, ISeqInStream *inStream, Bool *isIndex, UInt32 *headerSizeRes)
30 {
31   Byte header[XZ_BLOCK_HEADER_SIZE_MAX];
32   unsigned headerSize;
33   *headerSizeRes = 0;
34   RINOK(SeqInStream_ReadByte(inStream, &header[0]));
35   headerSize = ((unsigned)header[0] << 2) + 4;
36   if (headerSize == 0)
37   {
38     *headerSizeRes = 1;
39     *isIndex = True;
40     return SZ_OK;
41   }
42 
43   *isIndex = False;
44   *headerSizeRes = headerSize;
45   RINOK(SeqInStream_Read(inStream, header + 1, headerSize - 1));
46   return XzBlock_Parse(p, header);
47 }
48 
49 #define ADD_SIZE_CHECH(size, val) \
50   { UInt64 newSize = size + (val); if (newSize < size) return XZ_SIZE_OVERFLOW; size = newSize; }
51 
Xz_GetUnpackSize(const CXzStream * p)52 UInt64 Xz_GetUnpackSize(const CXzStream *p)
53 {
54   UInt64 size = 0;
55   size_t i;
56   for (i = 0; i < p->numBlocks; i++)
57     ADD_SIZE_CHECH(size, p->blocks[i].unpackSize);
58   return size;
59 }
60 
Xz_GetPackSize(const CXzStream * p)61 UInt64 Xz_GetPackSize(const CXzStream *p)
62 {
63   UInt64 size = 0;
64   size_t i;
65   for (i = 0; i < p->numBlocks; i++)
66     ADD_SIZE_CHECH(size, (p->blocks[i].totalSize + 3) & ~(UInt64)3);
67   return size;
68 }
69 
70 /*
71 SRes XzBlock_ReadFooter(CXzBlock *p, CXzStreamFlags f, ISeqInStream *inStream)
72 {
73   return SeqInStream_Read(inStream, p->check, XzFlags_GetCheckSize(f));
74 }
75 */
76 
Xz_ReadIndex2(CXzStream * p,const Byte * buf,size_t size,ISzAlloc * alloc)77 static SRes Xz_ReadIndex2(CXzStream *p, const Byte *buf, size_t size, ISzAlloc *alloc)
78 {
79   size_t i, numBlocks, crcStartPos, pos = 1;
80   UInt32 crc;
81 
82   if (size < 5 || buf[0] != 0)
83     return SZ_ERROR_ARCHIVE;
84 
85   size -= 4;
86   crc = CrcCalc(buf, size);
87   if (crc != GetUi32(buf + size))
88     return SZ_ERROR_ARCHIVE;
89 
90   {
91     UInt64 numBlocks64;
92     READ_VARINT_AND_CHECK(buf, pos, size, &numBlocks64);
93     numBlocks = (size_t)numBlocks64;
94     if (numBlocks != numBlocks64 || numBlocks * 2 > size)
95       return SZ_ERROR_ARCHIVE;
96   }
97 
98   crcStartPos = pos;
99   Xz_Free(p, alloc);
100   if (numBlocks != 0)
101   {
102     p->numBlocks = numBlocks;
103     p->numBlocksAllocated = numBlocks;
104     p->blocks = alloc->Alloc(alloc, sizeof(CXzBlockSizes) * numBlocks);
105     if (p->blocks == 0)
106       return SZ_ERROR_MEM;
107     for (i = 0; i < numBlocks; i++)
108     {
109       CXzBlockSizes *block = &p->blocks[i];
110       READ_VARINT_AND_CHECK(buf, pos, size, &block->totalSize);
111       READ_VARINT_AND_CHECK(buf, pos, size, &block->unpackSize);
112       if (block->totalSize == 0)
113         return SZ_ERROR_ARCHIVE;
114     }
115   }
116   while ((pos & 3) != 0)
117     if (buf[pos++] != 0)
118       return SZ_ERROR_ARCHIVE;
119   return (pos == size) ? SZ_OK : SZ_ERROR_ARCHIVE;
120 }
121 
Xz_ReadIndex(CXzStream * p,ILookInStream * stream,UInt64 indexSize,ISzAlloc * alloc)122 static SRes Xz_ReadIndex(CXzStream *p, ILookInStream *stream, UInt64 indexSize, ISzAlloc *alloc)
123 {
124   SRes res;
125   size_t size;
126   Byte *buf;
127   if (indexSize > ((UInt32)1 << 31))
128     return SZ_ERROR_UNSUPPORTED;
129   size = (size_t)indexSize;
130   if (size != indexSize)
131     return SZ_ERROR_UNSUPPORTED;
132   buf = alloc->Alloc(alloc, size);
133   if (buf == 0)
134     return SZ_ERROR_MEM;
135   res = LookInStream_Read2(stream, buf, size, SZ_ERROR_UNSUPPORTED);
136   if (res == SZ_OK)
137     res = Xz_ReadIndex2(p, buf, size, alloc);
138   alloc->Free(alloc, buf);
139   return res;
140 }
141 
SeekFromCur(ILookInStream * inStream,Int64 * res)142 static SRes SeekFromCur(ILookInStream *inStream, Int64 *res)
143 {
144   return inStream->Seek(inStream, res, SZ_SEEK_CUR);
145 }
146 
Xz_ReadBackward(CXzStream * p,ILookInStream * stream,Int64 * startOffset,ISzAlloc * alloc)147 static SRes Xz_ReadBackward(CXzStream *p, ILookInStream *stream, Int64 *startOffset, ISzAlloc *alloc)
148 {
149   UInt64 indexSize;
150   Byte buf[XZ_STREAM_FOOTER_SIZE];
151 
152   if ((*startOffset & 3) != 0 || *startOffset < XZ_STREAM_FOOTER_SIZE)
153     return SZ_ERROR_NO_ARCHIVE;
154   *startOffset = -XZ_STREAM_FOOTER_SIZE;
155   RINOK(SeekFromCur(stream, startOffset));
156 
157   RINOK(LookInStream_Read2(stream, buf, XZ_STREAM_FOOTER_SIZE, SZ_ERROR_NO_ARCHIVE));
158 
159   if (memcmp(buf + 10, XZ_FOOTER_SIG, XZ_FOOTER_SIG_SIZE) != 0)
160   {
161     Int64 i = 0;
162     *startOffset += XZ_STREAM_FOOTER_SIZE;
163     for (;;)
164     {
165       int j;
166       size_t processedSize;
167       #define TEMP_BUF_SIZE (1 << 10)
168       Byte tempBuf[TEMP_BUF_SIZE];
169       if (*startOffset < XZ_STREAM_FOOTER_SIZE || i > (1 << 16))
170         return SZ_ERROR_NO_ARCHIVE;
171       processedSize = (*startOffset > TEMP_BUF_SIZE) ? TEMP_BUF_SIZE : (size_t)*startOffset;
172       i += processedSize;
173       *startOffset = -(Int64)processedSize;
174       RINOK(SeekFromCur(stream, startOffset));
175       RINOK(LookInStream_Read2(stream, tempBuf, processedSize, SZ_ERROR_NO_ARCHIVE));
176       for (j = (int)processedSize; j >= 0; j--)
177         if (tempBuf[j -1] != 0)
178           break;
179       if (j != 0)
180       {
181         if ((j & 3) != 0)
182           return SZ_ERROR_NO_ARCHIVE;
183         *startOffset += j;
184         if (*startOffset < XZ_STREAM_FOOTER_SIZE)
185           return SZ_ERROR_NO_ARCHIVE;
186         *startOffset -= XZ_STREAM_FOOTER_SIZE;
187         RINOK(stream->Seek(stream, startOffset, SZ_SEEK_SET));
188         RINOK(LookInStream_Read2(stream, buf, XZ_STREAM_FOOTER_SIZE, SZ_ERROR_NO_ARCHIVE));
189         if (memcmp(buf + 10, XZ_FOOTER_SIG, XZ_FOOTER_SIG_SIZE) != 0)
190           return SZ_ERROR_NO_ARCHIVE;
191         break;
192       }
193     }
194   }
195 
196   p->flags = (CXzStreamFlags)GetBe16(buf + 8);
197 
198   if (!XzFlags_IsSupported(p->flags))
199     return SZ_ERROR_UNSUPPORTED;
200 
201   if (GetUi32(buf) != CrcCalc(buf + 4, 6))
202     return SZ_ERROR_ARCHIVE;
203 
204   indexSize = ((UInt64)GetUi32(buf + 4) + 1) << 2;
205 
206   *startOffset = -(Int64)(indexSize + XZ_STREAM_FOOTER_SIZE);
207   RINOK(SeekFromCur(stream, startOffset));
208 
209   RINOK(Xz_ReadIndex(p, stream, indexSize, alloc));
210 
211   {
212     UInt64 totalSize = Xz_GetPackSize(p);
213     UInt64 sum = XZ_STREAM_HEADER_SIZE + totalSize + indexSize;
214     if (totalSize == XZ_SIZE_OVERFLOW ||
215       sum >= ((UInt64)1 << 63) ||
216       totalSize >= ((UInt64)1 << 63))
217       return SZ_ERROR_ARCHIVE;
218     *startOffset = -(Int64)sum;
219     RINOK(SeekFromCur(stream, startOffset));
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 
Xzs_Construct(CXzs * p)235 void Xzs_Construct(CXzs *p)
236 {
237   p->num = p->numAllocated = 0;
238   p->streams = 0;
239 }
240 
Xzs_Free(CXzs * p,ISzAlloc * alloc)241 void 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 
Xzs_GetNumBlocks(const CXzs * p)251 UInt64 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 
Xzs_GetUnpackSize(const CXzs * p)260 UInt64 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 /*
270 UInt64 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 
Xzs_ReadBackward(CXzs * p,ILookInStream * stream,Int64 * startOffset,ICompressProgress * progress,ISzAlloc * alloc)280 SRes 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       memcpy(data, p->streams, p->num * sizeof(CXzStream));
301       alloc->Free(alloc, p->streams);
302       p->streams = (CXzStream *)data;
303     }
304     p->streams[p->num++] = st;
305     if (*startOffset == 0)
306       break;
307     RINOK(stream->Seek(stream, startOffset, SZ_SEEK_SET));
308     if (progress && progress->Progress(progress, endOffset - *startOffset, (UInt64)(Int64)-1) != SZ_OK)
309       return SZ_ERROR_PROGRESS;
310   }
311   return SZ_OK;
312 }
313