Coverage Report

Created: 2026-07-20 07:19

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/xz/src/liblzma/common/index_hash.c
Line
Count
Source
1
// SPDX-License-Identifier: 0BSD
2
3
///////////////////////////////////////////////////////////////////////////////
4
//
5
/// \file       index_hash.c
6
/// \brief      Validates Index by using a hash function
7
//
8
//  Author:     Lasse Collin
9
//
10
///////////////////////////////////////////////////////////////////////////////
11
12
#include "common.h"
13
#include "index.h"
14
#include "check.h"
15
16
17
typedef struct {
18
  /// Sum of the Block sizes (including Block Padding)
19
  lzma_vli blocks_size;
20
21
  /// Sum of the Uncompressed Size fields
22
  lzma_vli uncompressed_size;
23
24
  /// Number of Records
25
  lzma_vli count;
26
27
  /// Size of the List of Index Records as bytes
28
  lzma_vli index_list_size;
29
30
  /// Check calculated from Unpadded Sizes and Uncompressed Sizes.
31
  lzma_check_state check;
32
33
} lzma_index_hash_info;
34
35
36
struct lzma_index_hash_s {
37
  enum {
38
    SEQ_BLOCK,
39
    SEQ_COUNT,
40
    SEQ_UNPADDED,
41
    SEQ_UNCOMPRESSED,
42
    SEQ_PADDING_INIT,
43
    SEQ_PADDING,
44
    SEQ_CRC32,
45
  } sequence;
46
47
  /// Information collected while decoding the actual Blocks.
48
  lzma_index_hash_info blocks;
49
50
  /// Information collected from the Index field.
51
  lzma_index_hash_info records;
52
53
  /// Number of Records not fully decoded
54
  lzma_vli remaining;
55
56
  /// Unpadded Size currently being read from an Index Record.
57
  lzma_vli unpadded_size;
58
59
  /// Uncompressed Size currently being read from an Index Record.
60
  lzma_vli uncompressed_size;
61
62
  /// Position in variable-length integers when decoding them from
63
  /// the List of Records.
64
  size_t pos;
65
66
  /// CRC32 of the Index
67
  uint32_t crc32;
68
};
69
70
71
extern LZMA_API(lzma_index_hash *)
72
lzma_index_hash_init(lzma_index_hash *index_hash,
73
    const lzma_allocator *allocator)
74
4.37k
{
75
4.37k
  if (index_hash == NULL) {
76
3.51k
    index_hash = lzma_alloc(sizeof(lzma_index_hash), allocator);
77
3.51k
    if (index_hash == NULL)
78
0
      return NULL;
79
3.51k
  }
80
81
4.37k
  index_hash->sequence = SEQ_BLOCK;
82
4.37k
  index_hash->blocks.blocks_size = 0;
83
4.37k
  index_hash->blocks.uncompressed_size = 0;
84
4.37k
  index_hash->blocks.count = 0;
85
4.37k
  index_hash->blocks.index_list_size = 0;
86
4.37k
  index_hash->records.blocks_size = 0;
87
4.37k
  index_hash->records.uncompressed_size = 0;
88
4.37k
  index_hash->records.count = 0;
89
4.37k
  index_hash->records.index_list_size = 0;
90
4.37k
  index_hash->unpadded_size = 0;
91
4.37k
  index_hash->uncompressed_size = 0;
92
4.37k
  index_hash->pos = 0;
93
4.37k
  index_hash->crc32 = 0;
94
95
  // These cannot fail because LZMA_CHECK_BEST is known to be supported.
96
4.37k
  (void)lzma_check_init(&index_hash->blocks.check, LZMA_CHECK_BEST);
97
4.37k
  (void)lzma_check_init(&index_hash->records.check, LZMA_CHECK_BEST);
98
99
4.37k
  return index_hash;
100
4.37k
}
101
102
103
extern LZMA_API(void)
104
lzma_index_hash_end(lzma_index_hash *index_hash,
105
    const lzma_allocator *allocator)
106
3.51k
{
107
3.51k
  lzma_free(index_hash, allocator);
108
3.51k
  return;
109
3.51k
}
110
111
112
extern LZMA_API(lzma_vli)
113
lzma_index_hash_size(const lzma_index_hash *index_hash)
114
353
{
115
  // Get the size of the Index from ->blocks instead of ->records for
116
  // cases where application wants to know the Index Size before
117
  // decoding the Index.
118
353
  return index_size(index_hash->blocks.count,
119
353
      index_hash->blocks.index_list_size);
120
353
}
121
122
123
/// Updates the sizes and the hash without any validation.
124
static void
125
hash_append(lzma_index_hash_info *info, lzma_vli unpadded_size,
126
    lzma_vli uncompressed_size)
127
2.09k
{
128
2.09k
  info->blocks_size += vli_ceil4(unpadded_size);
129
2.09k
  info->uncompressed_size += uncompressed_size;
130
2.09k
  info->index_list_size += lzma_vli_size(unpadded_size)
131
2.09k
      + lzma_vli_size(uncompressed_size);
132
2.09k
  ++info->count;
133
134
2.09k
  const lzma_vli sizes[2] = { unpadded_size, uncompressed_size };
135
2.09k
  lzma_check_update(&info->check, LZMA_CHECK_BEST,
136
2.09k
      (const uint8_t *)(sizes), sizeof(sizes));
137
138
2.09k
  return;
139
2.09k
}
140
141
142
extern LZMA_API(lzma_ret)
143
lzma_index_hash_append(lzma_index_hash *index_hash, lzma_vli unpadded_size,
144
    lzma_vli uncompressed_size)
145
1.56k
{
146
  // Validate the arguments.
147
1.56k
  if (index_hash == NULL || index_hash->sequence != SEQ_BLOCK
148
1.56k
      || unpadded_size < UNPADDED_SIZE_MIN
149
1.56k
      || unpadded_size > UNPADDED_SIZE_MAX
150
1.56k
      || uncompressed_size > LZMA_VLI_MAX)
151
0
    return LZMA_PROG_ERROR;
152
153
  // Update the hash.
154
1.56k
  hash_append(&index_hash->blocks, unpadded_size, uncompressed_size);
155
156
  // Validate the properties of *info are still in allowed limits.
157
1.56k
  if (index_hash->blocks.blocks_size > LZMA_VLI_MAX
158
1.56k
      || index_hash->blocks.uncompressed_size > LZMA_VLI_MAX
159
1.56k
      || index_size(index_hash->blocks.count,
160
1.56k
          index_hash->blocks.index_list_size)
161
1.56k
        > LZMA_BACKWARD_SIZE_MAX
162
1.56k
      || index_stream_size(index_hash->blocks.blocks_size,
163
1.56k
          index_hash->blocks.count,
164
1.56k
          index_hash->blocks.index_list_size)
165
1.56k
        > LZMA_VLI_MAX)
166
0
    return LZMA_DATA_ERROR;
167
168
1.56k
  return LZMA_OK;
169
1.56k
}
170
171
172
extern LZMA_API(lzma_ret)
173
lzma_index_hash_decode(lzma_index_hash *index_hash, const uint8_t *in,
174
    size_t *in_pos, size_t in_size)
175
854
{
176
  // Catch zero input buffer here, because in contrast to Index encoder
177
  // and decoder functions, applications call this function directly
178
  // instead of via lzma_code(), which does the buffer checking.
179
854
  if (*in_pos >= in_size)
180
0
    return LZMA_BUF_ERROR;
181
182
  // NOTE: This function has many similarities to index_encode() and
183
  // index_decode() functions found from index_encoder.c and
184
  // index_decoder.c. See the comments especially in index_encoder.c.
185
854
  const size_t in_start = *in_pos;
186
854
  lzma_ret ret = LZMA_OK;
187
188
4.29k
  while (*in_pos < in_size)
189
4.20k
  switch (index_hash->sequence) {
190
795
  case SEQ_BLOCK:
191
    // Check the Index Indicator is present.
192
795
    if (in[(*in_pos)++] != INDEX_INDICATOR)
193
0
      return LZMA_DATA_ERROR;
194
195
795
    index_hash->sequence = SEQ_COUNT;
196
795
    break;
197
198
804
  case SEQ_COUNT: {
199
804
    ret = lzma_vli_decode(&index_hash->remaining,
200
804
        &index_hash->pos, in, in_pos, in_size);
201
804
    if (ret != LZMA_STREAM_END)
202
36
      goto out;
203
204
    // The count must match the count of the Blocks decoded.
205
768
    if (index_hash->remaining != index_hash->blocks.count)
206
61
      return LZMA_DATA_ERROR;
207
208
707
    ret = LZMA_OK;
209
707
    index_hash->pos = 0;
210
211
    // Handle the special case when there are no Blocks.
212
707
    index_hash->sequence = index_hash->remaining == 0
213
707
        ? SEQ_PADDING_INIT : SEQ_UNPADDED;
214
707
    break;
215
768
  }
216
217
664
  case SEQ_UNPADDED:
218
1.22k
  case SEQ_UNCOMPRESSED: {
219
1.22k
    lzma_vli *size = index_hash->sequence == SEQ_UNPADDED
220
1.22k
        ? &index_hash->unpadded_size
221
1.22k
        : &index_hash->uncompressed_size;
222
223
1.22k
    ret = lzma_vli_decode(size, &index_hash->pos,
224
1.22k
        in, in_pos, in_size);
225
1.22k
    if (ret != LZMA_STREAM_END)
226
83
      goto out;
227
228
1.14k
    ret = LZMA_OK;
229
1.14k
    index_hash->pos = 0;
230
231
1.14k
    if (index_hash->sequence == SEQ_UNPADDED) {
232
605
      if (index_hash->unpadded_size < UNPADDED_SIZE_MIN
233
595
          || index_hash->unpadded_size
234
595
            > UNPADDED_SIZE_MAX)
235
11
        return LZMA_DATA_ERROR;
236
237
594
      index_hash->sequence = SEQ_UNCOMPRESSED;
238
594
    } else {
239
      // Update the hash.
240
535
      hash_append(&index_hash->records,
241
535
          index_hash->unpadded_size,
242
535
          index_hash->uncompressed_size);
243
244
      // Verify that we don't go over the known sizes. Note
245
      // that this validation is simpler than the one used
246
      // in lzma_index_hash_append(), because here we know
247
      // that values in index_hash->blocks are already
248
      // validated and we are fine as long as we don't
249
      // exceed them in index_hash->records.
250
535
      if (index_hash->blocks.blocks_size
251
535
          < index_hash->records.blocks_size
252
449
          || index_hash->blocks.uncompressed_size
253
449
          < index_hash->records.uncompressed_size
254
428
          || index_hash->blocks.index_list_size
255
428
          < index_hash->records.index_list_size)
256
107
        return LZMA_DATA_ERROR;
257
258
      // Check if this was the last Record.
259
428
      index_hash->sequence = --index_hash->remaining == 0
260
428
          ? SEQ_PADDING_INIT : SEQ_UNPADDED;
261
428
    }
262
263
1.02k
    break;
264
1.14k
  }
265
266
1.02k
  case SEQ_PADDING_INIT:
267
461
    index_hash->pos = (LZMA_VLI_C(4) - index_size_unpadded(
268
461
        index_hash->records.count,
269
461
        index_hash->records.index_list_size)) & 3;
270
271
461
    index_hash->sequence = SEQ_PADDING;
272
461
    FALLTHROUGH;
273
274
1.37k
  case SEQ_PADDING:
275
1.37k
    if (index_hash->pos > 0) {
276
936
      --index_hash->pos;
277
936
      if (in[(*in_pos)++] != 0x00)
278
21
        return LZMA_DATA_ERROR;
279
280
915
      break;
281
936
    }
282
283
    // Compare the sizes.
284
436
    if (index_hash->blocks.blocks_size
285
436
        != index_hash->records.blocks_size
286
430
        || index_hash->blocks.uncompressed_size
287
430
        != index_hash->records.uncompressed_size
288
426
        || index_hash->blocks.index_list_size
289
426
        != index_hash->records.index_list_size)
290
10
      return LZMA_DATA_ERROR;
291
292
    // Finish the hashes and compare them.
293
426
    lzma_check_finish(&index_hash->blocks.check, LZMA_CHECK_BEST);
294
426
    lzma_check_finish(&index_hash->records.check, LZMA_CHECK_BEST);
295
426
    if (memcmp(index_hash->blocks.check.buffer.u8,
296
426
        index_hash->records.check.buffer.u8,
297
426
        lzma_check_size(LZMA_CHECK_BEST)) != 0)
298
2
      return LZMA_DATA_ERROR;
299
300
    // Finish the CRC32 calculation.
301
424
    index_hash->crc32 = lzma_crc32(in + in_start,
302
424
        *in_pos - in_start, index_hash->crc32);
303
304
424
    index_hash->sequence = SEQ_CRC32;
305
424
    FALLTHROUGH;
306
307
435
  case SEQ_CRC32:
308
1.69k
    do {
309
1.69k
      if (*in_pos == in_size)
310
23
        return LZMA_OK;
311
312
1.66k
      if (((index_hash->crc32 >> (index_hash->pos * 8))
313
1.66k
          & 0xFF) != in[(*in_pos)++]) {
314
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
315
        return LZMA_DATA_ERROR;
316
#endif
317
528
      }
318
319
1.66k
    } while (++index_hash->pos < 4);
320
321
412
    return LZMA_STREAM_END;
322
323
0
  default:
324
0
    assert(0);
325
0
    return LZMA_PROG_ERROR;
326
4.20k
  }
327
328
207
out:
329
  // Update the CRC32.
330
  //
331
  // Avoid null pointer + 0 (undefined behavior) in "in + in_start".
332
  // In such a case we had no input and thus in_used == 0.
333
207
  {
334
207
    const size_t in_used = *in_pos - in_start;
335
207
    if (in_used > 0)
336
207
      index_hash->crc32 = lzma_crc32(in + in_start,
337
207
          in_used, index_hash->crc32);
338
207
  }
339
340
207
  return ret;
341
854
}