Coverage Report

Created: 2026-11-13 06:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/barebox/lib/decompress_inflate.c
Line
Count
Source
1
#ifdef STATIC
2
/* Pre-boot environment: included */
3
4
/* prevent inclusion of _LINUX_KERNEL_H in pre-boot environment: lots
5
 * errors about console_printk etc... on ARM */
6
#define _LINUX_KERNEL_H
7
#include <linux/decompress/mm.h>
8
9
#include "zlib_inflate/inftrees.c"
10
#include "zlib_inflate/inffast.c"
11
#include "zlib_inflate/inflate.c"
12
13
#else /* STATIC */
14
/* initramfs et al: linked */
15
16
7.10k
#define MALLOC malloc
17
7.10k
#define FREE free
18
19
#include <linux/zutil.h>
20
#include <common.h>
21
#include <malloc.h>
22
23
#include "zlib_inflate/inftrees.h"
24
#include "zlib_inflate/inffast.h"
25
#include "zlib_inflate/inflate.h"
26
27
#include "zlib_inflate/infutil.h"
28
29
#endif /* STATIC */
30
31
#include <gunzip.h>
32
33
1.06k
#define GZIP_IOBUF_SIZE (16*1024)
34
35
static long nofill(void *buffer, unsigned long len)
36
1.06k
{
37
1.06k
  return -1;
38
1.06k
}
39
40
/* Included from initramfs et al code */
41
int  gunzip(unsigned char *buf, long len,
42
           long(*fill)(void*, unsigned long),
43
           long(*flush)(void*, unsigned long),
44
           unsigned char *out_buf,
45
           long *pos,
46
2.36k
           void(*error)(char *x)) {
47
2.36k
  u8 *zbuf;
48
2.36k
  struct z_stream_s *strm;
49
2.36k
  int rc;
50
2.36k
  size_t out_len;
51
52
2.36k
  rc = -1;
53
2.36k
  if (flush) {
54
2.36k
    out_len = 0x8000; /* 32 K */
55
2.36k
    out_buf = MALLOC(out_len);
56
2.36k
  } else {
57
0
    out_len = 0x7fffffff; /* no limit */
58
0
  }
59
2.36k
  if (!out_buf) {
60
0
    error("Out of memory while allocating output buffer");
61
0
    goto gunzip_nomem1;
62
0
  }
63
64
2.36k
  if (buf)
65
2.36k
    zbuf = buf;
66
0
  else {
67
0
    zbuf = MALLOC(GZIP_IOBUF_SIZE);
68
0
    len = 0;
69
0
  }
70
2.36k
  if (!zbuf) {
71
0
    error("Out of memory while allocating input buffer");
72
0
    goto gunzip_nomem2;
73
0
  }
74
75
2.36k
  strm = MALLOC(sizeof(*strm));
76
2.36k
  if (strm == NULL) {
77
0
    error("Out of memory while allocating z_stream");
78
0
    goto gunzip_nomem3;
79
0
  }
80
81
2.36k
  strm->workspace = MALLOC(flush ? zlib_inflate_workspacesize() :
82
2.36k
         sizeof(struct inflate_state));
83
2.36k
  if (strm->workspace == NULL) {
84
0
    error("Out of memory while allocating workspace");
85
0
    goto gunzip_nomem4;
86
0
  }
87
88
2.36k
  if (!fill)
89
2.36k
    fill = nofill;
90
91
2.36k
  if (len == 0)
92
0
    len = fill(zbuf, GZIP_IOBUF_SIZE);
93
94
  /* verify the gzip header */
95
2.36k
  if (len < 10 ||
96
2.36k
     zbuf[0] != 0x1f || zbuf[1] != 0x8b || zbuf[2] != 0x08) {
97
0
    if (pos)
98
0
      *pos = 0;
99
0
    error("Not a gzip file");
100
0
    goto gunzip_5;
101
0
  }
102
103
  /* skip over gzip header (1f,8b,08... 10 bytes total +
104
   * possible asciz filename)
105
   */
106
2.36k
  strm->next_in = zbuf + 10;
107
2.36k
  strm->avail_in = len - 10;
108
  /* skip over asciz filename */
109
2.36k
  if (zbuf[3] & 0x8) {
110
5.19k
    do {
111
      /*
112
       * If the filename doesn't fit into the buffer,
113
       * the file is very probably corrupt. Don't try
114
       * to read more data.
115
       */
116
5.19k
      if (strm->avail_in == 0) {
117
20
        error("header error");
118
20
        goto gunzip_5;
119
20
      }
120
5.17k
      --strm->avail_in;
121
5.17k
    } while (*strm->next_in++);
122
205
  }
123
124
2.34k
  strm->next_out = out_buf;
125
2.34k
  strm->avail_out = out_len;
126
127
2.34k
  rc = zlib_inflateInit2(strm, -MAX_WBITS);
128
129
2.34k
  if (!flush) {
130
0
    WS(strm)->inflate_state.wsize = 0;
131
0
    WS(strm)->inflate_state.window = NULL;
132
0
  }
133
134
98.1k
  while (rc == Z_OK) {
135
96.9k
    if (strm->avail_in == 0) {
136
      /* TODO: handle case where both pos and fill are set */
137
1.06k
      len = fill(zbuf, GZIP_IOBUF_SIZE);
138
1.06k
      if (len < 0) {
139
1.06k
        rc = -1;
140
1.06k
        error("read error");
141
1.06k
        break;
142
1.06k
      }
143
0
      strm->next_in = zbuf;
144
0
      strm->avail_in = len;
145
0
    }
146
95.9k
    rc = zlib_inflate(strm, 0);
147
148
    /* Write any data generated */
149
95.9k
    if (flush && strm->next_out > out_buf) {
150
95.0k
      int l = strm->next_out - out_buf;
151
95.0k
      if (l != flush(out_buf, l)) {
152
0
        rc = -1;
153
0
        error("write error");
154
0
        break;
155
0
      }
156
95.0k
      strm->next_out = out_buf;
157
95.0k
      strm->avail_out = out_len;
158
95.0k
    }
159
160
    /* after Z_FINISH, only Z_STREAM_END is "we unpacked it all" */
161
95.9k
    if (rc == Z_STREAM_END) {
162
95
      rc = 0;
163
95
      break;
164
95.8k
    } else if (rc != Z_OK) {
165
1.19k
      error("uncompression error");
166
1.19k
      rc = -1;
167
1.19k
    }
168
95.9k
  }
169
170
2.34k
  zlib_inflateEnd(strm);
171
2.34k
  if (pos)
172
    /* add + 8 to skip over trailer */
173
0
    *pos = strm->next_in - zbuf+8;
174
175
2.36k
gunzip_5:
176
2.36k
  FREE(strm->workspace);
177
2.36k
gunzip_nomem4:
178
2.36k
  FREE(strm);
179
2.36k
gunzip_nomem3:
180
2.36k
  if (!buf)
181
0
    FREE(zbuf);
182
2.36k
gunzip_nomem2:
183
2.36k
  if (flush)
184
2.36k
    FREE(out_buf);
185
2.36k
gunzip_nomem1:
186
2.36k
  return rc; /* returns Z_OK (0) if successful */
187
2.36k
}
188
189
int deflate_decompress(struct z_stream_s *stream, const u8 *src, unsigned int slen, u8 *dst,
190
    unsigned int *dlen)
191
0
{
192
193
0
  int ret = 0;
194
195
0
  ret = zlib_inflateReset(stream);
196
0
  if (ret != Z_OK) {
197
0
    ret = -EINVAL;
198
0
    goto out;
199
0
  }
200
201
0
  stream->next_in = (u8 *)src;
202
0
  stream->avail_in = slen;
203
0
  stream->next_out = (u8 *)dst;
204
0
  stream->avail_out = *dlen;
205
206
0
  ret = zlib_inflate(stream, Z_SYNC_FLUSH);
207
  /*
208
   * Work around a bug in zlib, which sometimes wants to taste an extra
209
   * byte when being used in the (undocumented) raw deflate mode.
210
   * (From USAGI).
211
   */
212
0
  if (ret == Z_OK && !stream->avail_in && stream->avail_out) {
213
0
    u8 zerostuff = 0;
214
0
    stream->next_in = &zerostuff;
215
0
    stream->avail_in = 1;
216
0
    ret = zlib_inflate(stream, Z_FINISH);
217
0
  }
218
0
  if (ret != Z_STREAM_END) {
219
0
    ret = -EINVAL;
220
0
    goto out;
221
0
  }
222
0
  ret = 0;
223
0
  *dlen = stream->total_out;
224
0
out:
225
0
  return ret;
226
0
}
227
228
#define decompress gunzip