Coverage Report

Created: 2025-06-16 07:00

/src/xz/src/liblzma/common/index.c
Line
Count
Source (jump to first uncovered line)
1
// SPDX-License-Identifier: 0BSD
2
3
///////////////////////////////////////////////////////////////////////////////
4
//
5
/// \file       index.c
6
/// \brief      Handling of .xz Indexes and some other Stream information
7
//
8
//  Author:     Lasse Collin
9
//
10
///////////////////////////////////////////////////////////////////////////////
11
12
#include "common.h"
13
#include "index.h"
14
#include "stream_flags_common.h"
15
16
17
/// \brief      How many Records to allocate at once
18
///
19
/// This should be big enough to avoid making lots of tiny allocations
20
/// but small enough to avoid too much unused memory at once.
21
0
#define INDEX_GROUP_SIZE 512
22
23
24
/// \brief      How many Records can be allocated at once at maximum
25
0
#define PREALLOC_MAX ((SIZE_MAX - sizeof(index_group)) / sizeof(index_record))
26
27
28
/// \brief      Base structure for index_stream and index_group structures
29
typedef struct index_tree_node_s index_tree_node;
30
struct index_tree_node_s {
31
  /// Uncompressed start offset of this Stream (relative to the
32
  /// beginning of the file) or Block (relative to the beginning
33
  /// of the Stream)
34
  lzma_vli uncompressed_base;
35
36
  /// Compressed start offset of this Stream or Block
37
  lzma_vli compressed_base;
38
39
  index_tree_node *parent;
40
  index_tree_node *left;
41
  index_tree_node *right;
42
};
43
44
45
/// \brief      AVL tree to hold index_stream or index_group structures
46
typedef struct {
47
  /// Root node
48
  index_tree_node *root;
49
50
  /// Leftmost node. Since the tree will be filled sequentially,
51
  /// this won't change after the first node has been added to
52
  /// the tree.
53
  index_tree_node *leftmost;
54
55
  /// The rightmost node in the tree. Since the tree is filled
56
  /// sequentially, this is always the node where to add the new data.
57
  index_tree_node *rightmost;
58
59
  /// Number of nodes in the tree
60
  uint32_t count;
61
62
} index_tree;
63
64
65
typedef struct {
66
  lzma_vli uncompressed_sum;
67
  lzma_vli unpadded_sum;
68
} index_record;
69
70
71
typedef struct {
72
  /// Every Record group is part of index_stream.groups tree.
73
  index_tree_node node;
74
75
  /// Number of Blocks in this Stream before this group.
76
  lzma_vli number_base;
77
78
  /// Number of Records that can be put in records[].
79
  size_t allocated;
80
81
  /// Index of the last Record in use.
82
  size_t last;
83
84
  /// The sizes in this array are stored as cumulative sums relative
85
  /// to the beginning of the Stream. This makes it possible to
86
  /// use binary search in lzma_index_locate().
87
  ///
88
  /// Note that the cumulative summing is done specially for
89
  /// unpadded_sum: The previous value is rounded up to the next
90
  /// multiple of four before adding the Unpadded Size of the new
91
  /// Block. The total encoded size of the Blocks in the Stream
92
  /// is records[last].unpadded_sum in the last Record group of
93
  /// the Stream.
94
  ///
95
  /// For example, if the Unpadded Sizes are 39, 57, and 81, the
96
  /// stored values are 39, 97 (40 + 57), and 181 (100 + 181).
97
  /// The total encoded size of these Blocks is 184.
98
  ///
99
  /// This is a flexible array, because it makes easy to optimize
100
  /// memory usage in case someone concatenates many Streams that
101
  /// have only one or few Blocks.
102
  index_record records[];
103
104
} index_group;
105
106
107
typedef struct {
108
  /// Every index_stream is a node in the tree of Streams.
109
  index_tree_node node;
110
111
  /// Number of this Stream (first one is 1)
112
  uint32_t number;
113
114
  /// Total number of Blocks before this Stream
115
  lzma_vli block_number_base;
116
117
  /// Record groups of this Stream are stored in a tree.
118
  /// It's a T-tree with AVL-tree balancing. There are
119
  /// INDEX_GROUP_SIZE Records per node by default.
120
  /// This keeps the number of memory allocations reasonable
121
  /// and finding a Record is fast.
122
  index_tree groups;
123
124
  /// Number of Records in this Stream
125
  lzma_vli record_count;
126
127
  /// Size of the List of Records field in this Stream. This is used
128
  /// together with record_count to calculate the size of the Index
129
  /// field and thus the total size of the Stream.
130
  lzma_vli index_list_size;
131
132
  /// Stream Flags of this Stream. This is meaningful only if
133
  /// the Stream Flags have been told us with lzma_index_stream_flags().
134
  /// Initially stream_flags.version is set to UINT32_MAX to indicate
135
  /// that the Stream Flags are unknown.
136
  lzma_stream_flags stream_flags;
137
138
  /// Amount of Stream Padding after this Stream. This defaults to
139
  /// zero and can be set with lzma_index_stream_padding().
140
  lzma_vli stream_padding;
141
142
} index_stream;
143
144
145
struct lzma_index_s {
146
  /// AVL-tree containing the Stream(s). Often there is just one
147
  /// Stream, but using a tree keeps lookups fast even when there
148
  /// are many concatenated Streams.
149
  index_tree streams;
150
151
  /// Uncompressed size of all the Blocks in the Stream(s)
152
  lzma_vli uncompressed_size;
153
154
  /// Total size of all the Blocks in the Stream(s)
155
  lzma_vli total_size;
156
157
  /// Total number of Records in all Streams in this lzma_index
158
  lzma_vli record_count;
159
160
  /// Size of the List of Records field if all the Streams in this
161
  /// lzma_index were packed into a single Stream (makes it simpler to
162
  /// take many .xz files and combine them into a single Stream).
163
  ///
164
  /// This value together with record_count is needed to calculate
165
  /// Backward Size that is stored into Stream Footer.
166
  lzma_vli index_list_size;
167
168
  /// How many Records to allocate at once in lzma_index_append().
169
  /// This defaults to INDEX_GROUP_SIZE but can be overridden with
170
  /// lzma_index_prealloc().
171
  size_t prealloc;
172
173
  /// Bitmask indicating what integrity check types have been used
174
  /// as set by lzma_index_stream_flags(). The bit of the last Stream
175
  /// is not included here, since it is possible to change it by
176
  /// calling lzma_index_stream_flags() again.
177
  uint32_t checks;
178
};
179
180
181
static void
182
index_tree_init(index_tree *tree)
183
0
{
184
0
  tree->root = NULL;
185
0
  tree->leftmost = NULL;
186
0
  tree->rightmost = NULL;
187
0
  tree->count = 0;
188
0
  return;
189
0
}
190
191
192
/// Helper for index_tree_end()
193
static void
194
index_tree_node_end(index_tree_node *node, const lzma_allocator *allocator,
195
    void (*free_func)(void *node, const lzma_allocator *allocator))
196
0
{
197
  // The tree won't ever be very huge, so recursion should be fine.
198
  // 20 levels in the tree is likely quite a lot already in practice.
199
0
  if (node->left != NULL)
200
0
    index_tree_node_end(node->left, allocator, free_func);
201
202
0
  if (node->right != NULL)
203
0
    index_tree_node_end(node->right, allocator, free_func);
204
205
0
  free_func(node, allocator);
206
0
  return;
207
0
}
208
209
210
/// Free the memory allocated for a tree. Each node is freed using the
211
/// given free_func which is either &lzma_free or &index_stream_end.
212
/// The latter is used to free the Record groups from each index_stream
213
/// before freeing the index_stream itself.
214
static void
215
index_tree_end(index_tree *tree, const lzma_allocator *allocator,
216
    void (*free_func)(void *node, const lzma_allocator *allocator))
217
0
{
218
0
  assert(free_func != NULL);
219
220
0
  if (tree->root != NULL)
221
0
    index_tree_node_end(tree->root, allocator, free_func);
222
223
0
  return;
224
0
}
225
226
227
/// Add a new node to the tree. node->uncompressed_base and
228
/// node->compressed_base must have been set by the caller already.
229
static void
230
index_tree_append(index_tree *tree, index_tree_node *node)
231
0
{
232
0
  node->parent = tree->rightmost;
233
0
  node->left = NULL;
234
0
  node->right = NULL;
235
236
0
  ++tree->count;
237
238
  // Handle the special case of adding the first node.
239
0
  if (tree->root == NULL) {
240
0
    tree->root = node;
241
0
    tree->leftmost = node;
242
0
    tree->rightmost = node;
243
0
    return;
244
0
  }
245
246
  // The tree is always filled sequentially.
247
0
  assert(tree->rightmost->uncompressed_base <= node->uncompressed_base);
248
0
  assert(tree->rightmost->compressed_base < node->compressed_base);
249
250
  // Add the new node after the rightmost node. It's the correct
251
  // place due to the reason above.
252
0
  tree->rightmost->right = node;
253
0
  tree->rightmost = node;
254
255
  // Balance the AVL-tree if needed. We don't need to keep the balance
256
  // factors in nodes, because we always fill the tree sequentially,
257
  // and thus know the state of the tree just by looking at the node
258
  // count. From the node count we can calculate how many steps to go
259
  // up in the tree to find the rotation root.
260
0
  uint32_t up = tree->count ^ (UINT32_C(1) << bsr32(tree->count));
261
0
  if (up != 0) {
262
    // Locate the root node for the rotation.
263
0
    up = ctz32(tree->count) + 2;
264
0
    do {
265
0
      node = node->parent;
266
0
    } while (--up > 0);
267
268
    // Rotate left using node as the rotation root.
269
0
    index_tree_node *pivot = node->right;
270
271
0
    if (node->parent == NULL) {
272
0
      tree->root = pivot;
273
0
    } else {
274
0
      assert(node->parent->right == node);
275
0
      node->parent->right = pivot;
276
0
    }
277
278
0
    pivot->parent = node->parent;
279
280
0
    node->right = pivot->left;
281
0
    if (node->right != NULL)
282
0
      node->right->parent = node;
283
284
0
    pivot->left = node;
285
0
    node->parent = pivot;
286
0
  }
287
288
0
  return;
289
0
}
290
291
292
/// Get the next node in the tree. Return NULL if there are no more nodes.
293
static void *
294
index_tree_next(const index_tree_node *node)
295
0
{
296
0
  if (node->right != NULL) {
297
0
    node = node->right;
298
0
    while (node->left != NULL)
299
0
      node = node->left;
300
301
0
    return (void *)(node);
302
0
  }
303
304
0
  while (node->parent != NULL && node->parent->right == node)
305
0
    node = node->parent;
306
307
0
  return (void *)(node->parent);
308
0
}
309
310
311
/// Locate a node that contains the given uncompressed offset. It is
312
/// caller's job to check that target is not bigger than the uncompressed
313
/// size of the tree (the last node would be returned in that case still).
314
static void *
315
index_tree_locate(const index_tree *tree, lzma_vli target)
316
0
{
317
0
  const index_tree_node *result = NULL;
318
0
  const index_tree_node *node = tree->root;
319
320
0
  assert(tree->leftmost == NULL
321
0
      || tree->leftmost->uncompressed_base == 0);
322
323
  // Consecutive nodes may have the same uncompressed_base.
324
  // We must pick the rightmost one.
325
0
  while (node != NULL) {
326
0
    if (node->uncompressed_base > target) {
327
0
      node = node->left;
328
0
    } else {
329
0
      result = node;
330
0
      node = node->right;
331
0
    }
332
0
  }
333
334
0
  return (void *)(result);
335
0
}
336
337
338
/// Allocate and initialize a new Stream using the given base offsets.
339
static index_stream *
340
index_stream_init(lzma_vli compressed_base, lzma_vli uncompressed_base,
341
    uint32_t stream_number, lzma_vli block_number_base,
342
    const lzma_allocator *allocator)
343
0
{
344
0
  index_stream *s = lzma_alloc(sizeof(index_stream), allocator);
345
0
  if (s == NULL)
346
0
    return NULL;
347
348
0
  s->node.uncompressed_base = uncompressed_base;
349
0
  s->node.compressed_base = compressed_base;
350
0
  s->node.parent = NULL;
351
0
  s->node.left = NULL;
352
0
  s->node.right = NULL;
353
354
0
  s->number = stream_number;
355
0
  s->block_number_base = block_number_base;
356
357
0
  index_tree_init(&s->groups);
358
359
0
  s->record_count = 0;
360
0
  s->index_list_size = 0;
361
0
  s->stream_flags.version = UINT32_MAX;
362
0
  s->stream_padding = 0;
363
364
0
  return s;
365
0
}
366
367
368
/// Free the memory allocated for a Stream and its Record groups.
369
static void
370
index_stream_end(void *node, const lzma_allocator *allocator)
371
0
{
372
0
  index_stream *s = node;
373
0
  index_tree_end(&s->groups, allocator, &lzma_free);
374
0
  lzma_free(s, allocator);
375
0
  return;
376
0
}
377
378
379
static lzma_index *
380
index_init_plain(const lzma_allocator *allocator)
381
0
{
382
0
  lzma_index *i = lzma_alloc(sizeof(lzma_index), allocator);
383
0
  if (i != NULL) {
384
0
    index_tree_init(&i->streams);
385
0
    i->uncompressed_size = 0;
386
0
    i->total_size = 0;
387
0
    i->record_count = 0;
388
0
    i->index_list_size = 0;
389
0
    i->prealloc = INDEX_GROUP_SIZE;
390
0
    i->checks = 0;
391
0
  }
392
393
0
  return i;
394
0
}
395
396
397
extern LZMA_API(lzma_index *)
398
lzma_index_init(const lzma_allocator *allocator)
399
0
{
400
0
  lzma_index *i = index_init_plain(allocator);
401
0
  if (i == NULL)
402
0
    return NULL;
403
404
0
  index_stream *s = index_stream_init(0, 0, 1, 0, allocator);
405
0
  if (s == NULL) {
406
0
    lzma_free(i, allocator);
407
0
    return NULL;
408
0
  }
409
410
0
  index_tree_append(&i->streams, &s->node);
411
412
0
  return i;
413
0
}
414
415
416
extern LZMA_API(void)
417
lzma_index_end(lzma_index *i, const lzma_allocator *allocator)
418
0
{
419
  // NOTE: If you modify this function, check also the bottom
420
  // of lzma_index_cat().
421
0
  if (i != NULL) {
422
0
    index_tree_end(&i->streams, allocator, &index_stream_end);
423
0
    lzma_free(i, allocator);
424
0
  }
425
426
0
  return;
427
0
}
428
429
430
extern void
431
lzma_index_prealloc(lzma_index *i, lzma_vli records)
432
0
{
433
0
  if (records > PREALLOC_MAX)
434
0
    records = PREALLOC_MAX;
435
436
0
  i->prealloc = (size_t)(records);
437
0
  return;
438
0
}
439
440
441
extern LZMA_API(uint64_t)
442
lzma_index_memusage(lzma_vli streams, lzma_vli blocks)
443
0
{
444
  // This calculates an upper bound that is only a little bit
445
  // bigger than the exact maximum memory usage with the given
446
  // parameters.
447
448
  // Typical malloc() overhead is 2 * sizeof(void *) but we take
449
  // a little bit extra just in case. Using LZMA_MEMUSAGE_BASE
450
  // instead would give too inaccurate estimate.
451
0
  const size_t alloc_overhead = 4 * sizeof(void *);
452
453
  // Amount of memory needed for each Stream base structures.
454
  // We assume that every Stream has at least one Block and
455
  // thus at least one group.
456
0
  const size_t stream_base = sizeof(index_stream)
457
0
      + sizeof(index_group) + 2 * alloc_overhead;
458
459
  // Amount of memory needed per group.
460
0
  const size_t group_base = sizeof(index_group)
461
0
      + INDEX_GROUP_SIZE * sizeof(index_record)
462
0
      + alloc_overhead;
463
464
  // Number of groups. There may actually be more, but that overhead
465
  // has been taken into account in stream_base already.
466
0
  const lzma_vli groups
467
0
      = (blocks + INDEX_GROUP_SIZE - 1) / INDEX_GROUP_SIZE;
468
469
  // Memory used by index_stream and index_group structures.
470
0
  const uint64_t streams_mem = streams * stream_base;
471
0
  const uint64_t groups_mem = groups * group_base;
472
473
  // Memory used by the base structure.
474
0
  const uint64_t index_base = sizeof(lzma_index) + alloc_overhead;
475
476
  // Validate the arguments and catch integer overflows.
477
  // Maximum number of Streams is "only" UINT32_MAX, because
478
  // that limit is used by the tree containing the Streams.
479
0
  const uint64_t limit = UINT64_MAX - index_base;
480
0
  if (streams == 0 || streams > UINT32_MAX || blocks > LZMA_VLI_MAX
481
0
      || streams > limit / stream_base
482
0
      || groups > limit / group_base
483
0
      || limit - streams_mem < groups_mem)
484
0
    return UINT64_MAX;
485
486
0
  return index_base + streams_mem + groups_mem;
487
0
}
488
489
490
extern LZMA_API(uint64_t)
491
lzma_index_memused(const lzma_index *i)
492
0
{
493
0
  return lzma_index_memusage(i->streams.count, i->record_count);
494
0
}
495
496
497
extern LZMA_API(lzma_vli)
498
lzma_index_block_count(const lzma_index *i)
499
0
{
500
0
  return i->record_count;
501
0
}
502
503
504
extern LZMA_API(lzma_vli)
505
lzma_index_stream_count(const lzma_index *i)
506
0
{
507
0
  return i->streams.count;
508
0
}
509
510
511
extern LZMA_API(lzma_vli)
512
lzma_index_size(const lzma_index *i)
513
0
{
514
0
  return index_size(i->record_count, i->index_list_size);
515
0
}
516
517
518
extern LZMA_API(lzma_vli)
519
lzma_index_total_size(const lzma_index *i)
520
0
{
521
0
  return i->total_size;
522
0
}
523
524
525
extern LZMA_API(lzma_vli)
526
lzma_index_stream_size(const lzma_index *i)
527
0
{
528
  // Stream Header + Blocks + Index + Stream Footer
529
0
  return LZMA_STREAM_HEADER_SIZE + i->total_size
530
0
      + index_size(i->record_count, i->index_list_size)
531
0
      + LZMA_STREAM_HEADER_SIZE;
532
0
}
533
534
535
static lzma_vli
536
index_file_size(lzma_vli compressed_base, lzma_vli unpadded_sum,
537
    lzma_vli record_count, lzma_vli index_list_size,
538
    lzma_vli stream_padding)
539
0
{
540
  // Earlier Streams and Stream Paddings + Stream Header
541
  // + Blocks + Index + Stream Footer + Stream Padding
542
  //
543
  // This might go over LZMA_VLI_MAX due to too big unpadded_sum
544
  // when this function is used in lzma_index_append().
545
0
  lzma_vli file_size = compressed_base + 2 * LZMA_STREAM_HEADER_SIZE
546
0
      + stream_padding + vli_ceil4(unpadded_sum);
547
0
  if (file_size > LZMA_VLI_MAX)
548
0
    return LZMA_VLI_UNKNOWN;
549
550
  // The same applies here.
551
0
  file_size += index_size(record_count, index_list_size);
552
0
  if (file_size > LZMA_VLI_MAX)
553
0
    return LZMA_VLI_UNKNOWN;
554
555
0
  return file_size;
556
0
}
557
558
559
extern LZMA_API(lzma_vli)
560
lzma_index_file_size(const lzma_index *i)
561
0
{
562
0
  const index_stream *s = (const index_stream *)(i->streams.rightmost);
563
0
  const index_group *g = (const index_group *)(s->groups.rightmost);
564
0
  return index_file_size(s->node.compressed_base,
565
0
      g == NULL ? 0 : g->records[g->last].unpadded_sum,
566
0
      s->record_count, s->index_list_size,
567
0
      s->stream_padding);
568
0
}
569
570
571
extern LZMA_API(lzma_vli)
572
lzma_index_uncompressed_size(const lzma_index *i)
573
0
{
574
0
  return i->uncompressed_size;
575
0
}
576
577
578
extern LZMA_API(uint32_t)
579
lzma_index_checks(const lzma_index *i)
580
0
{
581
0
  uint32_t checks = i->checks;
582
583
  // Get the type of the Check of the last Stream too.
584
0
  const index_stream *s = (const index_stream *)(i->streams.rightmost);
585
0
  if (s->stream_flags.version != UINT32_MAX)
586
0
    checks |= UINT32_C(1) << s->stream_flags.check;
587
588
0
  return checks;
589
0
}
590
591
592
extern uint32_t
593
lzma_index_padding_size(const lzma_index *i)
594
0
{
595
0
  return (LZMA_VLI_C(4) - index_size_unpadded(
596
0
      i->record_count, i->index_list_size)) & 3;
597
0
}
598
599
600
extern LZMA_API(lzma_ret)
601
lzma_index_stream_flags(lzma_index *i, const lzma_stream_flags *stream_flags)
602
0
{
603
0
  if (i == NULL || stream_flags == NULL)
604
0
    return LZMA_PROG_ERROR;
605
606
  // Validate the Stream Flags.
607
0
  return_if_error(lzma_stream_flags_compare(
608
0
      stream_flags, stream_flags));
609
610
0
  index_stream *s = (index_stream *)(i->streams.rightmost);
611
0
  s->stream_flags = *stream_flags;
612
613
0
  return LZMA_OK;
614
0
}
615
616
617
extern LZMA_API(lzma_ret)
618
lzma_index_stream_padding(lzma_index *i, lzma_vli stream_padding)
619
0
{
620
0
  if (i == NULL || stream_padding > LZMA_VLI_MAX
621
0
      || (stream_padding & 3) != 0)
622
0
    return LZMA_PROG_ERROR;
623
624
0
  index_stream *s = (index_stream *)(i->streams.rightmost);
625
626
  // Check that the new value won't make the file grow too big.
627
0
  const lzma_vli old_stream_padding = s->stream_padding;
628
0
  s->stream_padding = 0;
629
0
  if (lzma_index_file_size(i) + stream_padding > LZMA_VLI_MAX) {
630
0
    s->stream_padding = old_stream_padding;
631
0
    return LZMA_DATA_ERROR;
632
0
  }
633
634
0
  s->stream_padding = stream_padding;
635
0
  return LZMA_OK;
636
0
}
637
638
639
extern LZMA_API(lzma_ret)
640
lzma_index_append(lzma_index *i, const lzma_allocator *allocator,
641
    lzma_vli unpadded_size, lzma_vli uncompressed_size)
642
0
{
643
  // Validate.
644
0
  if (i == NULL || unpadded_size < UNPADDED_SIZE_MIN
645
0
      || unpadded_size > UNPADDED_SIZE_MAX
646
0
      || uncompressed_size > LZMA_VLI_MAX)
647
0
    return LZMA_PROG_ERROR;
648
649
0
  index_stream *s = (index_stream *)(i->streams.rightmost);
650
0
  index_group *g = (index_group *)(s->groups.rightmost);
651
652
0
  const lzma_vli compressed_base = g == NULL ? 0
653
0
      : vli_ceil4(g->records[g->last].unpadded_sum);
654
0
  const lzma_vli uncompressed_base = g == NULL ? 0
655
0
      : g->records[g->last].uncompressed_sum;
656
0
  const uint32_t index_list_size_add = lzma_vli_size(unpadded_size)
657
0
      + lzma_vli_size(uncompressed_size);
658
659
  // Check that uncompressed size will not overflow.
660
0
  if (uncompressed_base + uncompressed_size > LZMA_VLI_MAX)
661
0
    return LZMA_DATA_ERROR;
662
663
  // Check that the new unpadded sum will not overflow. This is
664
  // checked again in index_file_size(), but the unpadded sum is
665
  // passed to vli_ceil4() which expects a valid lzma_vli value.
666
0
  if (compressed_base + unpadded_size > UNPADDED_SIZE_MAX)
667
0
    return LZMA_DATA_ERROR;
668
669
  // Check that the file size will stay within limits.
670
0
  if (index_file_size(s->node.compressed_base,
671
0
      compressed_base + unpadded_size, s->record_count + 1,
672
0
      s->index_list_size + index_list_size_add,
673
0
      s->stream_padding) == LZMA_VLI_UNKNOWN)
674
0
    return LZMA_DATA_ERROR;
675
676
  // The size of the Index field must not exceed the maximum value
677
  // that can be stored in the Backward Size field.
678
0
  if (index_size(i->record_count + 1,
679
0
      i->index_list_size + index_list_size_add)
680
0
      > LZMA_BACKWARD_SIZE_MAX)
681
0
    return LZMA_DATA_ERROR;
682
683
0
  if (g != NULL && g->last + 1 < g->allocated) {
684
    // There is space in the last group at least for one Record.
685
0
    ++g->last;
686
0
  } else {
687
    // We need to allocate a new group.
688
0
    g = lzma_alloc(sizeof(index_group)
689
0
        + i->prealloc * sizeof(index_record),
690
0
        allocator);
691
0
    if (g == NULL)
692
0
      return LZMA_MEM_ERROR;
693
694
0
    g->last = 0;
695
0
    g->allocated = i->prealloc;
696
697
    // Reset prealloc so that if the application happens to
698
    // add new Records, the allocation size will be sane.
699
0
    i->prealloc = INDEX_GROUP_SIZE;
700
701
    // Set the start offsets of this group.
702
0
    g->node.uncompressed_base = uncompressed_base;
703
0
    g->node.compressed_base = compressed_base;
704
0
    g->number_base = s->record_count + 1;
705
706
    // Add the new group to the Stream.
707
0
    index_tree_append(&s->groups, &g->node);
708
0
  }
709
710
  // Add the new Record to the group.
711
0
  g->records[g->last].uncompressed_sum
712
0
      = uncompressed_base + uncompressed_size;
713
0
  g->records[g->last].unpadded_sum
714
0
      = compressed_base + unpadded_size;
715
716
  // Update the totals.
717
0
  ++s->record_count;
718
0
  s->index_list_size += index_list_size_add;
719
720
0
  i->total_size += vli_ceil4(unpadded_size);
721
0
  i->uncompressed_size += uncompressed_size;
722
0
  ++i->record_count;
723
0
  i->index_list_size += index_list_size_add;
724
725
0
  return LZMA_OK;
726
0
}
727
728
729
/// Structure to pass info to index_cat_helper()
730
typedef struct {
731
  /// Uncompressed size of the destination
732
  lzma_vli uncompressed_size;
733
734
  /// Compressed file size of the destination
735
  lzma_vli file_size;
736
737
  /// Same as above but for Block numbers
738
  lzma_vli block_number_add;
739
740
  /// Number of Streams that were in the destination index before we
741
  /// started appending new Streams from the source index. This is
742
  /// used to fix the Stream numbering.
743
  uint32_t stream_number_add;
744
745
  /// Destination index' Stream tree
746
  index_tree *streams;
747
748
} index_cat_info;
749
750
751
/// Add the Stream nodes from the source index to dest using recursion.
752
/// Simplest iterative traversal of the source tree wouldn't work, because
753
/// we update the pointers in nodes when moving them to the destination tree.
754
static void
755
index_cat_helper(const index_cat_info *info, index_stream *this)
756
0
{
757
0
  index_stream *left = (index_stream *)(this->node.left);
758
0
  index_stream *right = (index_stream *)(this->node.right);
759
760
0
  if (left != NULL)
761
0
    index_cat_helper(info, left);
762
763
0
  this->node.uncompressed_base += info->uncompressed_size;
764
0
  this->node.compressed_base += info->file_size;
765
0
  this->number += info->stream_number_add;
766
0
  this->block_number_base += info->block_number_add;
767
0
  index_tree_append(info->streams, &this->node);
768
769
0
  if (right != NULL)
770
0
    index_cat_helper(info, right);
771
772
0
  return;
773
0
}
774
775
776
extern LZMA_API(lzma_ret)
777
lzma_index_cat(lzma_index *restrict dest, lzma_index *restrict src,
778
    const lzma_allocator *allocator)
779
0
{
780
0
  if (dest == NULL || src == NULL)
781
0
    return LZMA_PROG_ERROR;
782
783
0
  const lzma_vli dest_file_size = lzma_index_file_size(dest);
784
785
  // Check that we don't exceed the file size limits.
786
0
  if (dest_file_size + lzma_index_file_size(src) > LZMA_VLI_MAX
787
0
      || dest->uncompressed_size + src->uncompressed_size
788
0
        > LZMA_VLI_MAX)
789
0
    return LZMA_DATA_ERROR;
790
791
  // Check that the encoded size of the combined lzma_indexes stays
792
  // within limits. In theory, this should be done only if we know
793
  // that the user plans to actually combine the Streams and thus
794
  // construct a single Index (probably rare). However, exceeding
795
  // this limit is quite theoretical, so we do this check always
796
  // to simplify things elsewhere.
797
0
  {
798
0
    const lzma_vli dest_size = index_size_unpadded(
799
0
        dest->record_count, dest->index_list_size);
800
0
    const lzma_vli src_size = index_size_unpadded(
801
0
        src->record_count, src->index_list_size);
802
0
    if (vli_ceil4(dest_size + src_size) > LZMA_BACKWARD_SIZE_MAX)
803
0
      return LZMA_DATA_ERROR;
804
0
  }
805
806
  // Optimize the last group to minimize memory usage. Allocation has
807
  // to be done before modifying dest or src.
808
0
  {
809
0
    index_stream *s = (index_stream *)(dest->streams.rightmost);
810
0
    index_group *g = (index_group *)(s->groups.rightmost);
811
0
    if (g != NULL && g->last + 1 < g->allocated) {
812
0
      assert(g->node.left == NULL);
813
0
      assert(g->node.right == NULL);
814
815
0
      index_group *newg = lzma_alloc(sizeof(index_group)
816
0
          + (g->last + 1)
817
0
          * sizeof(index_record),
818
0
          allocator);
819
0
      if (newg == NULL)
820
0
        return LZMA_MEM_ERROR;
821
822
0
      newg->node = g->node;
823
0
      newg->allocated = g->last + 1;
824
0
      newg->last = g->last;
825
0
      newg->number_base = g->number_base;
826
827
0
      memcpy(newg->records, g->records, newg->allocated
828
0
          * sizeof(index_record));
829
830
0
      if (g->node.parent != NULL) {
831
0
        assert(g->node.parent->right == &g->node);
832
0
        g->node.parent->right = &newg->node;
833
0
      }
834
835
0
      if (s->groups.leftmost == &g->node) {
836
0
        assert(s->groups.root == &g->node);
837
0
        s->groups.leftmost = &newg->node;
838
0
        s->groups.root = &newg->node;
839
0
      }
840
841
0
      assert(s->groups.rightmost == &g->node);
842
0
      s->groups.rightmost = &newg->node;
843
844
0
      lzma_free(g, allocator);
845
846
      // NOTE: newg isn't leaked here because
847
      // newg == (void *)&newg->node.
848
0
    }
849
0
  }
850
851
  // dest->checks includes the check types of all except the last Stream
852
  // in dest. Set the bit for the check type of the last Stream now so
853
  // that it won't get lost when Stream(s) from src are appended to dest.
854
0
  dest->checks = lzma_index_checks(dest);
855
856
  // Add all the Streams from src to dest. Update the base offsets
857
  // of each Stream from src.
858
0
  const index_cat_info info = {
859
0
    .uncompressed_size = dest->uncompressed_size,
860
0
    .file_size = dest_file_size,
861
0
    .stream_number_add = dest->streams.count,
862
0
    .block_number_add = dest->record_count,
863
0
    .streams = &dest->streams,
864
0
  };
865
0
  index_cat_helper(&info, (index_stream *)(src->streams.root));
866
867
  // Update info about all the combined Streams.
868
0
  dest->uncompressed_size += src->uncompressed_size;
869
0
  dest->total_size += src->total_size;
870
0
  dest->record_count += src->record_count;
871
0
  dest->index_list_size += src->index_list_size;
872
0
  dest->checks |= src->checks;
873
874
  // There's nothing else left in src than the base structure.
875
0
  lzma_free(src, allocator);
876
877
0
  return LZMA_OK;
878
0
}
879
880
881
/// Duplicate an index_stream.
882
static index_stream *
883
index_dup_stream(const index_stream *src, const lzma_allocator *allocator)
884
0
{
885
  // Catch a somewhat theoretical integer overflow.
886
0
  if (src->record_count > PREALLOC_MAX)
887
0
    return NULL;
888
889
  // Allocate and initialize a new Stream.
890
0
  index_stream *dest = index_stream_init(src->node.compressed_base,
891
0
      src->node.uncompressed_base, src->number,
892
0
      src->block_number_base, allocator);
893
0
  if (dest == NULL)
894
0
    return NULL;
895
896
  // Copy the overall information.
897
0
  dest->record_count = src->record_count;
898
0
  dest->index_list_size = src->index_list_size;
899
0
  dest->stream_flags = src->stream_flags;
900
0
  dest->stream_padding = src->stream_padding;
901
902
  // Return if there are no groups to duplicate.
903
0
  if (src->groups.leftmost == NULL)
904
0
    return dest;
905
906
  // Allocate memory for the Records. We put all the Records into
907
  // a single group. It's simplest and also tends to make
908
  // lzma_index_locate() a little bit faster with very big Indexes.
909
0
  index_group *destg = lzma_alloc(sizeof(index_group)
910
0
      + src->record_count * sizeof(index_record),
911
0
      allocator);
912
0
  if (destg == NULL) {
913
0
    index_stream_end(dest, allocator);
914
0
    return NULL;
915
0
  }
916
917
  // Initialize destg.
918
0
  destg->node.uncompressed_base = 0;
919
0
  destg->node.compressed_base = 0;
920
0
  destg->number_base = 1;
921
0
  destg->allocated = src->record_count;
922
0
  destg->last = src->record_count - 1;
923
924
  // Go through all the groups in src and copy the Records into destg.
925
0
  const index_group *srcg = (const index_group *)(src->groups.leftmost);
926
0
  size_t i = 0;
927
0
  do {
928
0
    memcpy(destg->records + i, srcg->records,
929
0
        (srcg->last + 1) * sizeof(index_record));
930
0
    i += srcg->last + 1;
931
0
    srcg = index_tree_next(&srcg->node);
932
0
  } while (srcg != NULL);
933
934
0
  assert(i == destg->allocated);
935
936
  // Add the group to the new Stream.
937
0
  index_tree_append(&dest->groups, &destg->node);
938
939
0
  return dest;
940
0
}
941
942
943
extern LZMA_API(lzma_index *)
944
lzma_index_dup(const lzma_index *src, const lzma_allocator *allocator)
945
0
{
946
  // Allocate the base structure (no initial Stream).
947
0
  lzma_index *dest = index_init_plain(allocator);
948
0
  if (dest == NULL)
949
0
    return NULL;
950
951
  // Copy the totals.
952
0
  dest->uncompressed_size = src->uncompressed_size;
953
0
  dest->total_size = src->total_size;
954
0
  dest->record_count = src->record_count;
955
0
  dest->index_list_size = src->index_list_size;
956
957
  // Copy the Streams and the groups in them.
958
0
  const index_stream *srcstream
959
0
      = (const index_stream *)(src->streams.leftmost);
960
0
  do {
961
0
    index_stream *deststream = index_dup_stream(
962
0
        srcstream, allocator);
963
0
    if (deststream == NULL) {
964
0
      lzma_index_end(dest, allocator);
965
0
      return NULL;
966
0
    }
967
968
0
    index_tree_append(&dest->streams, &deststream->node);
969
970
0
    srcstream = index_tree_next(&srcstream->node);
971
0
  } while (srcstream != NULL);
972
973
0
  return dest;
974
0
}
975
976
977
/// Indexing for lzma_index_iter.internal[]
978
enum {
979
  ITER_INDEX,
980
  ITER_STREAM,
981
  ITER_GROUP,
982
  ITER_RECORD,
983
  ITER_METHOD,
984
};
985
986
987
/// Values for lzma_index_iter.internal[ITER_METHOD].s
988
enum {
989
  ITER_METHOD_NORMAL,
990
  ITER_METHOD_NEXT,
991
  ITER_METHOD_LEFTMOST,
992
};
993
994
995
static void
996
iter_set_info(lzma_index_iter *iter)
997
0
{
998
0
  const lzma_index *i = iter->internal[ITER_INDEX].p;
999
0
  const index_stream *stream = iter->internal[ITER_STREAM].p;
1000
0
  const index_group *group = iter->internal[ITER_GROUP].p;
1001
0
  const size_t record = iter->internal[ITER_RECORD].s;
1002
1003
  // lzma_index_iter.internal must not contain a pointer to the last
1004
  // group in the index, because that may be reallocated by
1005
  // lzma_index_cat().
1006
0
  if (group == NULL) {
1007
    // There are no groups.
1008
0
    assert(stream->groups.root == NULL);
1009
0
    iter->internal[ITER_METHOD].s = ITER_METHOD_LEFTMOST;
1010
1011
0
  } else if (i->streams.rightmost != &stream->node
1012
0
      || stream->groups.rightmost != &group->node) {
1013
    // The group is not not the last group in the index.
1014
0
    iter->internal[ITER_METHOD].s = ITER_METHOD_NORMAL;
1015
1016
0
  } else if (stream->groups.leftmost != &group->node) {
1017
    // The group isn't the only group in the Stream, thus we
1018
    // know that it must have a parent group i.e. it's not
1019
    // the root node.
1020
0
    assert(stream->groups.root != &group->node);
1021
0
    assert(group->node.parent->right == &group->node);
1022
0
    iter->internal[ITER_METHOD].s = ITER_METHOD_NEXT;
1023
0
    iter->internal[ITER_GROUP].p = group->node.parent;
1024
1025
0
  } else {
1026
    // The Stream has only one group.
1027
0
    assert(stream->groups.root == &group->node);
1028
0
    assert(group->node.parent == NULL);
1029
0
    iter->internal[ITER_METHOD].s = ITER_METHOD_LEFTMOST;
1030
0
    iter->internal[ITER_GROUP].p = NULL;
1031
0
  }
1032
1033
  // NOTE: lzma_index_iter.stream.number is lzma_vli but we use uint32_t
1034
  // internally.
1035
0
  iter->stream.number = stream->number;
1036
0
  iter->stream.block_count = stream->record_count;
1037
0
  iter->stream.compressed_offset = stream->node.compressed_base;
1038
0
  iter->stream.uncompressed_offset = stream->node.uncompressed_base;
1039
1040
  // iter->stream.flags will be NULL if the Stream Flags haven't been
1041
  // set with lzma_index_stream_flags().
1042
0
  iter->stream.flags = stream->stream_flags.version == UINT32_MAX
1043
0
      ? NULL : &stream->stream_flags;
1044
0
  iter->stream.padding = stream->stream_padding;
1045
1046
0
  if (stream->groups.rightmost == NULL) {
1047
    // Stream has no Blocks.
1048
0
    iter->stream.compressed_size = index_size(0, 0)
1049
0
        + 2 * LZMA_STREAM_HEADER_SIZE;
1050
0
    iter->stream.uncompressed_size = 0;
1051
0
  } else {
1052
0
    const index_group *g = (const index_group *)(
1053
0
        stream->groups.rightmost);
1054
1055
    // Stream Header + Stream Footer + Index + Blocks
1056
0
    iter->stream.compressed_size = 2 * LZMA_STREAM_HEADER_SIZE
1057
0
        + index_size(stream->record_count,
1058
0
          stream->index_list_size)
1059
0
        + vli_ceil4(g->records[g->last].unpadded_sum);
1060
0
    iter->stream.uncompressed_size
1061
0
        = g->records[g->last].uncompressed_sum;
1062
0
  }
1063
1064
0
  if (group != NULL) {
1065
0
    iter->block.number_in_stream = group->number_base + record;
1066
0
    iter->block.number_in_file = iter->block.number_in_stream
1067
0
        + stream->block_number_base;
1068
1069
0
    iter->block.compressed_stream_offset
1070
0
        = record == 0 ? group->node.compressed_base
1071
0
        : vli_ceil4(group->records[
1072
0
          record - 1].unpadded_sum);
1073
0
    iter->block.uncompressed_stream_offset
1074
0
        = record == 0 ? group->node.uncompressed_base
1075
0
        : group->records[record - 1].uncompressed_sum;
1076
1077
0
    iter->block.uncompressed_size
1078
0
        = group->records[record].uncompressed_sum
1079
0
        - iter->block.uncompressed_stream_offset;
1080
0
    iter->block.unpadded_size
1081
0
        = group->records[record].unpadded_sum
1082
0
        - iter->block.compressed_stream_offset;
1083
0
    iter->block.total_size = vli_ceil4(iter->block.unpadded_size);
1084
1085
0
    iter->block.compressed_stream_offset
1086
0
        += LZMA_STREAM_HEADER_SIZE;
1087
1088
0
    iter->block.compressed_file_offset
1089
0
        = iter->block.compressed_stream_offset
1090
0
        + iter->stream.compressed_offset;
1091
0
    iter->block.uncompressed_file_offset
1092
0
        = iter->block.uncompressed_stream_offset
1093
0
        + iter->stream.uncompressed_offset;
1094
0
  }
1095
1096
0
  return;
1097
0
}
1098
1099
1100
extern LZMA_API(void)
1101
lzma_index_iter_init(lzma_index_iter *iter, const lzma_index *i)
1102
0
{
1103
0
  iter->internal[ITER_INDEX].p = i;
1104
0
  lzma_index_iter_rewind(iter);
1105
0
  return;
1106
0
}
1107
1108
1109
extern LZMA_API(void)
1110
lzma_index_iter_rewind(lzma_index_iter *iter)
1111
0
{
1112
0
  iter->internal[ITER_STREAM].p = NULL;
1113
0
  iter->internal[ITER_GROUP].p = NULL;
1114
0
  iter->internal[ITER_RECORD].s = 0;
1115
0
  iter->internal[ITER_METHOD].s = ITER_METHOD_NORMAL;
1116
0
  return;
1117
0
}
1118
1119
1120
extern LZMA_API(lzma_bool)
1121
lzma_index_iter_next(lzma_index_iter *iter, lzma_index_iter_mode mode)
1122
0
{
1123
  // Catch unsupported mode values.
1124
0
  if ((unsigned int)(mode) > LZMA_INDEX_ITER_NONEMPTY_BLOCK)
1125
0
    return true;
1126
1127
0
  const lzma_index *i = iter->internal[ITER_INDEX].p;
1128
0
  const index_stream *stream = iter->internal[ITER_STREAM].p;
1129
0
  const index_group *group = NULL;
1130
0
  size_t record = iter->internal[ITER_RECORD].s;
1131
1132
  // If we are being asked for the next Stream, leave group to NULL
1133
  // so that the rest of the this function thinks that this Stream
1134
  // has no groups and will thus go to the next Stream.
1135
0
  if (mode != LZMA_INDEX_ITER_STREAM) {
1136
    // Get the pointer to the current group. See iter_set_inf()
1137
    // for explanation.
1138
0
    switch (iter->internal[ITER_METHOD].s) {
1139
0
    case ITER_METHOD_NORMAL:
1140
0
      group = iter->internal[ITER_GROUP].p;
1141
0
      break;
1142
1143
0
    case ITER_METHOD_NEXT:
1144
0
      group = index_tree_next(iter->internal[ITER_GROUP].p);
1145
0
      break;
1146
1147
0
    case ITER_METHOD_LEFTMOST:
1148
0
      group = (const index_group *)(
1149
0
          stream->groups.leftmost);
1150
0
      break;
1151
0
    }
1152
0
  }
1153
1154
0
again:
1155
0
  if (stream == NULL) {
1156
    // We at the beginning of the lzma_index.
1157
    // Locate the first Stream.
1158
0
    stream = (const index_stream *)(i->streams.leftmost);
1159
0
    if (mode >= LZMA_INDEX_ITER_BLOCK) {
1160
      // Since we are being asked to return information
1161
      // about the first a Block, skip Streams that have
1162
      // no Blocks.
1163
0
      while (stream->groups.leftmost == NULL) {
1164
0
        stream = index_tree_next(&stream->node);
1165
0
        if (stream == NULL)
1166
0
          return true;
1167
0
      }
1168
0
    }
1169
1170
    // Start from the first Record in the Stream.
1171
0
    group = (const index_group *)(stream->groups.leftmost);
1172
0
    record = 0;
1173
1174
0
  } else if (group != NULL && record < group->last) {
1175
    // The next Record is in the same group.
1176
0
    ++record;
1177
1178
0
  } else {
1179
    // This group has no more Records or this Stream has
1180
    // no Blocks at all.
1181
0
    record = 0;
1182
1183
    // If group is not NULL, this Stream has at least one Block
1184
    // and thus at least one group. Find the next group.
1185
0
    if (group != NULL)
1186
0
      group = index_tree_next(&group->node);
1187
1188
0
    if (group == NULL) {
1189
      // This Stream has no more Records. Find the next
1190
      // Stream. If we are being asked to return information
1191
      // about a Block, we skip empty Streams.
1192
0
      do {
1193
0
        stream = index_tree_next(&stream->node);
1194
0
        if (stream == NULL)
1195
0
          return true;
1196
0
      } while (mode >= LZMA_INDEX_ITER_BLOCK
1197
0
          && stream->groups.leftmost == NULL);
1198
1199
0
      group = (const index_group *)(
1200
0
          stream->groups.leftmost);
1201
0
    }
1202
0
  }
1203
1204
0
  if (mode == LZMA_INDEX_ITER_NONEMPTY_BLOCK) {
1205
    // We need to look for the next Block again if this Block
1206
    // is empty.
1207
0
    if (record == 0) {
1208
0
      if (group->node.uncompressed_base
1209
0
          == group->records[0].uncompressed_sum)
1210
0
        goto again;
1211
0
    } else if (group->records[record - 1].uncompressed_sum
1212
0
        == group->records[record].uncompressed_sum) {
1213
0
      goto again;
1214
0
    }
1215
0
  }
1216
1217
0
  iter->internal[ITER_STREAM].p = stream;
1218
0
  iter->internal[ITER_GROUP].p = group;
1219
0
  iter->internal[ITER_RECORD].s = record;
1220
1221
0
  iter_set_info(iter);
1222
1223
0
  return false;
1224
0
}
1225
1226
1227
extern LZMA_API(lzma_bool)
1228
lzma_index_iter_locate(lzma_index_iter *iter, lzma_vli target)
1229
0
{
1230
0
  const lzma_index *i = iter->internal[ITER_INDEX].p;
1231
1232
  // If the target is past the end of the file, return immediately.
1233
0
  if (i->uncompressed_size <= target)
1234
0
    return true;
1235
1236
  // Locate the Stream containing the target offset.
1237
0
  const index_stream *stream = index_tree_locate(&i->streams, target);
1238
0
  assert(stream != NULL);
1239
0
  target -= stream->node.uncompressed_base;
1240
1241
  // Locate the group containing the target offset.
1242
0
  const index_group *group = index_tree_locate(&stream->groups, target);
1243
0
  assert(group != NULL);
1244
1245
  // Use binary search to locate the exact Record. It is the first
1246
  // Record whose uncompressed_sum is greater than target.
1247
  // This is because we want the rightmost Record that fulfills the
1248
  // search criterion. It is possible that there are empty Blocks;
1249
  // we don't want to return them.
1250
0
  size_t left = 0;
1251
0
  size_t right = group->last;
1252
1253
0
  while (left < right) {
1254
0
    const size_t pos = left + (right - left) / 2;
1255
0
    if (group->records[pos].uncompressed_sum <= target)
1256
0
      left = pos + 1;
1257
0
    else
1258
0
      right = pos;
1259
0
  }
1260
1261
0
  iter->internal[ITER_STREAM].p = stream;
1262
0
  iter->internal[ITER_GROUP].p = group;
1263
0
  iter->internal[ITER_RECORD].s = left;
1264
1265
0
  iter_set_info(iter);
1266
1267
0
  return false;
1268
0
}