Coverage Report

Created: 2025-12-31 06:11

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