Coverage Report

Created: 2026-11-28 06:58

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/lib/lzo/lzo1x_decompress_safe.c
Line
Count
Source
1
/*
2
 *  LZO1X Decompressor from LZO
3
 *
4
 *  Copyright (C) 1996-2012 Markus F.X.J. Oberhumer <markus@oberhumer.com>
5
 *
6
 *  The full LZO package can be found at:
7
 *  http://www.oberhumer.com/opensource/lzo/
8
 *
9
 *  Changed for Linux kernel use by:
10
 *  Nitin Gupta <nitingupta910@gmail.com>
11
 *  Richard Purdie <rpurdie@openedhand.com>
12
 */
13
14
#include <asm/unaligned.h>
15
#include <common.h>
16
#include <lzo.h>
17
#include "lzodefs.h"
18
19
435k
#define HAVE_IP(x)      ((size_t)(ip_end - ip) >= (size_t)(x))
20
129k
#define HAVE_OP(x)      ((size_t)(op_end - op) >= (size_t)(x))
21
435k
#define NEED_IP(x)      if (!HAVE_IP(x)) goto input_overrun
22
129k
#define NEED_OP(x)      if (!HAVE_OP(x)) goto output_overrun
23
61.4k
#define TEST_LB(m_pos)  if ((m_pos) < out) goto lookbehind_overrun
24
25
/* This MAX_255_COUNT is the maximum number of times we can add 255 to a base
26
 * count without overflowing an integer. The multiply will overflow when
27
 * multiplying 255 by more than MAXINT/255. The sum will overflow earlier
28
 * depending on the base count. Since the base count is taken from a u8
29
 * and a few bits, it is safe to assume that it will always be lower than
30
 * or equal to 2*255, thus we can always prevent any overflow by accepting
31
 * two less 255 steps. See Documentation/lzo.txt for more information.
32
 */
33
#define MAX_255_COUNT      ((((size_t)~0) / 255) - 2)
34
35
int lzo1x_decompress_safe(const unsigned char *in, size_t in_len,
36
      unsigned char *out, size_t *out_len)
37
6.48k
{
38
6.48k
  unsigned char *op;
39
6.48k
  const unsigned char *ip;
40
6.48k
  size_t t, next;
41
6.48k
  size_t state = 0;
42
6.48k
  const unsigned char *m_pos;
43
6.48k
  const unsigned char * const ip_end = in + in_len;
44
6.48k
  unsigned char * const op_end = out + *out_len;
45
46
6.48k
  op = out;
47
6.48k
  ip = in;
48
49
6.48k
  if (unlikely(in_len < 3))
50
382
    goto input_overrun;
51
6.10k
  if (*ip > 17) {
52
5.38k
    t = *ip++ - 17;
53
5.38k
    if (t < 4) {
54
1.59k
      next = t;
55
1.59k
      goto match_next;
56
1.59k
    }
57
3.79k
    goto copy_literal_run;
58
5.38k
  }
59
60
69.6k
  for (;;) {
61
69.6k
    t = *ip++;
62
69.6k
    if (t < 16) {
63
11.1k
      if (likely(state == 0)) {
64
5.86k
        if (unlikely(t == 0)) {
65
4.48k
          size_t offset;
66
4.48k
          const unsigned char *ip_last = ip;
67
68
60.8k
          while (unlikely(*ip == 0)) {
69
56.6k
            ip++;
70
56.6k
            NEED_IP(1);
71
56.6k
          }
72
4.20k
          offset = ip - ip_last;
73
4.20k
          if (unlikely(offset > MAX_255_COUNT))
74
0
            return LZO_E_ERROR;
75
76
4.20k
          offset = (offset << 8) - offset;
77
4.20k
          t += offset + 15 + *ip++;
78
4.20k
        }
79
5.58k
        t += 3;
80
9.38k
copy_literal_run:
81
#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
82
        if (likely(HAVE_IP(t + 15) && HAVE_OP(t + 15))) {
83
          const unsigned char *ie = ip + t;
84
          unsigned char *oe = op + t;
85
          do {
86
            COPY8(op, ip);
87
            op += 8;
88
            ip += 8;
89
            COPY8(op, ip);
90
            op += 8;
91
            ip += 8;
92
          } while (ip < ie);
93
          ip = ie;
94
          op = oe;
95
        } else
96
#endif
97
9.38k
        {
98
9.38k
          NEED_OP(t);
99
9.25k
          NEED_IP(t + 3);
100
2.61M
          do {
101
2.61M
            *op++ = *ip++;
102
2.61M
          } while (--t > 0);
103
8.36k
        }
104
0
        state = 4;
105
8.36k
        continue;
106
9.25k
      } else if (state != 4) {
107
3.21k
        next = t & 3;
108
3.21k
        m_pos = op - 1;
109
3.21k
        m_pos -= t >> 2;
110
3.21k
        m_pos -= *ip++ << 2;
111
3.21k
        TEST_LB(m_pos);
112
2.98k
        NEED_OP(2);
113
2.97k
        op[0] = m_pos[0];
114
2.97k
        op[1] = m_pos[1];
115
2.97k
        op += 2;
116
2.97k
        goto match_next;
117
2.98k
      } else {
118
2.08k
        next = t & 3;
119
2.08k
        m_pos = op - (1 + M2_MAX_OFFSET);
120
2.08k
        m_pos -= t >> 2;
121
2.08k
        m_pos -= *ip++ << 2;
122
2.08k
        t = 3;
123
2.08k
      }
124
58.4k
    } else if (t >= 64) {
125
23.5k
      next = t & 3;
126
23.5k
      m_pos = op - 1;
127
23.5k
      m_pos -= (t >> 2) & 7;
128
23.5k
      m_pos -= *ip++ << 3;
129
23.5k
      t = (t >> 5) - 1 + (3 - 1);
130
34.9k
    } else if (t >= 32) {
131
26.8k
      t = (t & 31) + (3 - 1);
132
26.8k
      if (unlikely(t == 2)) {
133
24.9k
        size_t offset;
134
24.9k
        const unsigned char *ip_last = ip;
135
136
228k
        while (unlikely(*ip == 0)) {
137
203k
          ip++;
138
203k
          NEED_IP(1);
139
203k
        }
140
24.6k
        offset = ip - ip_last;
141
24.6k
        if (unlikely(offset > MAX_255_COUNT))
142
0
          return LZO_E_ERROR;
143
144
24.6k
        offset = (offset << 8) - offset;
145
24.6k
        t += offset + 31 + *ip++;
146
24.6k
        NEED_IP(2);
147
24.6k
      }
148
26.3k
      m_pos = op - 1;
149
26.3k
      next = get_unaligned_le16(ip);
150
26.3k
      ip += 2;
151
26.3k
      m_pos -= next >> 2;
152
26.3k
      next &= 3;
153
26.3k
    } else {
154
8.06k
      m_pos = op;
155
8.06k
      m_pos -= (t & 8) << 11;
156
8.06k
      t = (t & 7) + (3 - 1);
157
8.06k
      if (unlikely(t == 2)) {
158
1.28k
        size_t offset;
159
1.28k
        const unsigned char *ip_last = ip;
160
161
80.3k
        while (unlikely(*ip == 0)) {
162
79.1k
          ip++;
163
79.1k
          NEED_IP(1);
164
79.1k
        }
165
1.19k
        offset = ip - ip_last;
166
1.19k
        if (unlikely(offset > MAX_255_COUNT))
167
0
          return LZO_E_ERROR;
168
169
1.19k
        offset = (offset << 8) - offset;
170
1.19k
        t += offset + 7 + *ip++;
171
1.19k
        NEED_IP(2);
172
1.19k
      }
173
7.70k
      next = get_unaligned_le16(ip);
174
7.70k
      ip += 2;
175
7.70k
      m_pos -= next >> 2;
176
7.70k
      next &= 3;
177
7.70k
      if (m_pos == op)
178
1.40k
        goto eof_found;
179
6.30k
      m_pos -= 0x4000;
180
6.30k
    }
181
58.2k
    TEST_LB(m_pos);
182
#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
183
    if (op - m_pos >= 8) {
184
      unsigned char *oe = op + t;
185
      if (likely(HAVE_OP(t + 15))) {
186
        do {
187
          COPY8(op, m_pos);
188
          op += 8;
189
          m_pos += 8;
190
          COPY8(op, m_pos);
191
          op += 8;
192
          m_pos += 8;
193
        } while (op < oe);
194
        op = oe;
195
        if (HAVE_IP(6)) {
196
          state = next;
197
          COPY4(op, ip);
198
          op += next;
199
          ip += next;
200
          continue;
201
        }
202
      } else {
203
        NEED_OP(t);
204
        do {
205
          *op++ = *m_pos++;
206
        } while (op < oe);
207
      }
208
    } else
209
#endif
210
56.4k
    {
211
56.4k
      unsigned char *oe = op + t;
212
56.4k
      NEED_OP(t);
213
56.3k
      op[0] = m_pos[0];
214
56.3k
      op[1] = m_pos[1];
215
56.3k
      op += 2;
216
56.3k
      m_pos += 2;
217
54.1M
      do {
218
54.1M
        *op++ = *m_pos++;
219
54.1M
      } while (op < oe);
220
56.3k
    }
221
60.9k
match_next:
222
60.9k
    state = next;
223
60.9k
    t = next;
224
#if defined(CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS)
225
    if (likely(HAVE_IP(6) && HAVE_OP(4))) {
226
      COPY4(op, ip);
227
      op += t;
228
      ip += t;
229
    } else
230
#endif
231
60.9k
    {
232
60.9k
      NEED_IP(t + 3);
233
60.5k
      NEED_OP(t);
234
108k
      while (t > 0) {
235
47.9k
        *op++ = *ip++;
236
47.9k
        t--;
237
47.9k
      }
238
60.5k
    }
239
60.5k
  }
240
241
1.40k
eof_found:
242
1.40k
  *out_len = op - out;
243
1.40k
  return (t != 3       ? LZO_E_ERROR :
244
1.40k
    ip == ip_end ? LZO_E_OK :
245
1.11k
    ip <  ip_end ? LZO_E_INPUT_NOT_CONSUMED : LZO_E_INPUT_OVERRUN);
246
247
2.83k
input_overrun:
248
2.83k
  *out_len = op - out;
249
2.83k
  return LZO_E_INPUT_OVERRUN;
250
251
246
output_overrun:
252
246
  *out_len = op - out;
253
246
  return LZO_E_OUTPUT_OVERRUN;
254
255
1.99k
lookbehind_overrun:
256
1.99k
  *out_len = op - out;
257
1.99k
  return LZO_E_LOOKBEHIND_OVERRUN;
258
714
}
259
260
EXPORT_SYMBOL(lzo1x_decompress_safe);