Coverage Report

Created: 2025-06-16 07:00

/src/xz/src/liblzma/delta/delta_encoder.c
Line
Count
Source (jump to first uncovered line)
1
// SPDX-License-Identifier: 0BSD
2
3
///////////////////////////////////////////////////////////////////////////////
4
//
5
/// \file       delta_encoder.c
6
/// \brief      Delta filter encoder
7
//
8
//  Author:     Lasse Collin
9
//
10
///////////////////////////////////////////////////////////////////////////////
11
12
#include "delta_encoder.h"
13
#include "delta_private.h"
14
15
16
/// Copies and encodes the data at the same time. This is used when Delta
17
/// is the first filter in the chain (and thus the last filter in the
18
/// encoder's filter stack).
19
static void
20
copy_and_encode(lzma_delta_coder *coder,
21
    const uint8_t *restrict in, uint8_t *restrict out, size_t size)
22
0
{
23
0
  const size_t distance = coder->distance;
24
25
0
  for (size_t i = 0; i < size; ++i) {
26
0
    const uint8_t tmp = coder->history[
27
0
        (distance + coder->pos) & 0xFF];
28
0
    coder->history[coder->pos-- & 0xFF] = in[i];
29
0
    out[i] = in[i] - tmp;
30
0
  }
31
0
}
32
33
34
/// Encodes the data in place. This is used when we are the last filter
35
/// in the chain (and thus non-last filter in the encoder's filter stack).
36
static void
37
encode_in_place(lzma_delta_coder *coder, uint8_t *buffer, size_t size)
38
0
{
39
0
  const size_t distance = coder->distance;
40
41
0
  for (size_t i = 0; i < size; ++i) {
42
0
    const uint8_t tmp = coder->history[
43
0
        (distance + coder->pos) & 0xFF];
44
0
    coder->history[coder->pos-- & 0xFF] = buffer[i];
45
0
    buffer[i] -= tmp;
46
0
  }
47
0
}
48
49
50
static lzma_ret
51
delta_encode(void *coder_ptr, const lzma_allocator *allocator,
52
    const uint8_t *restrict in, size_t *restrict in_pos,
53
    size_t in_size, uint8_t *restrict out,
54
    size_t *restrict out_pos, size_t out_size, lzma_action action)
55
0
{
56
0
  lzma_delta_coder *coder = coder_ptr;
57
58
0
  lzma_ret ret;
59
60
0
  if (coder->next.code == NULL) {
61
0
    const size_t in_avail = in_size - *in_pos;
62
0
    const size_t out_avail = out_size - *out_pos;
63
0
    const size_t size = my_min(in_avail, out_avail);
64
65
    // in and out might be NULL. In such cases size == 0.
66
    // Null pointer + 0 is undefined behavior so skip
67
    // the call in that case as it would do nothing anyway.
68
0
    if (size > 0)
69
0
      copy_and_encode(coder, in + *in_pos, out + *out_pos,
70
0
          size);
71
72
0
    *in_pos += size;
73
0
    *out_pos += size;
74
75
0
    ret = action != LZMA_RUN && *in_pos == in_size
76
0
        ? LZMA_STREAM_END : LZMA_OK;
77
78
0
  } else {
79
0
    const size_t out_start = *out_pos;
80
81
0
    ret = coder->next.code(coder->next.coder, allocator,
82
0
        in, in_pos, in_size, out, out_pos, out_size,
83
0
        action);
84
85
    // Like above, avoid null pointer + 0.
86
0
    const size_t size = *out_pos - out_start;
87
0
    if (size > 0)
88
0
      encode_in_place(coder, out + out_start, size);
89
0
  }
90
91
0
  return ret;
92
0
}
93
94
95
static lzma_ret
96
delta_encoder_update(void *coder_ptr, const lzma_allocator *allocator,
97
    const lzma_filter *filters_null lzma_attribute((__unused__)),
98
    const lzma_filter *reversed_filters)
99
0
{
100
0
  lzma_delta_coder *coder = coder_ptr;
101
102
  // Delta doesn't and will never support changing the options in
103
  // the middle of encoding. If the app tries to change them, we
104
  // simply ignore them.
105
0
  return lzma_next_filter_update(
106
0
      &coder->next, allocator, reversed_filters + 1);
107
0
}
108
109
110
extern lzma_ret
111
lzma_delta_encoder_init(lzma_next_coder *next, const lzma_allocator *allocator,
112
    const lzma_filter_info *filters)
113
0
{
114
0
  next->code = &delta_encode;
115
0
  next->update = &delta_encoder_update;
116
0
  return lzma_delta_coder_init(next, allocator, filters);
117
0
}
118
119
120
extern lzma_ret
121
lzma_delta_props_encode(const void *options, uint8_t *out)
122
0
{
123
  // The caller must have already validated the options, so it's
124
  // LZMA_PROG_ERROR if they are invalid.
125
0
  if (lzma_delta_coder_memusage(options) == UINT64_MAX)
126
0
    return LZMA_PROG_ERROR;
127
128
0
  const lzma_options_delta *opt = options;
129
0
  out[0] = opt->dist - LZMA_DELTA_DIST_MIN;
130
131
0
  return LZMA_OK;
132
0
}