1 // SPDX-License-Identifier: GPL-2.0+
3 * (C) Copyright 2000-2006
4 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
14 #include <u-boot/zlib.h>
17 #define HEADER0 '\x1f'
18 #define HEADER1 '\x8b'
19 #define ZALLOC_ALIGNMENT 16
27 void *gzalloc(void *x, unsigned items, unsigned size)
32 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
39 void gzfree(void *x, void *addr, unsigned nb)
44 int gzip_parse_header(const unsigned char *src, unsigned long len)
51 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
52 puts ("Error: Bad gzipped data\n");
55 if ((flags & EXTRA_FIELD) != 0)
56 i = 12 + src[10] + (src[11] << 8);
57 if ((flags & ORIG_NAME) != 0)
60 if ((flags & COMMENT) != 0)
63 if ((flags & HEAD_CRC) != 0)
66 puts ("Error: gunzip out of data in header\n");
72 int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
74 int offset = gzip_parse_header(src, *lenp);
79 return zunzip(dst, dstlen, src, lenp, 1, offset);
82 #ifdef CONFIG_CMD_UNZIP
84 void gzwrite_progress_init(u64 expectedsize)
90 void gzwrite_progress(int iteration,
94 if (0 == (iteration & 3))
95 printf("%llu/%llu\r", bytes_written, total_bytes);
99 void gzwrite_progress_finish(int returnval,
105 if (0 == returnval) {
106 printf("\n\t%llu bytes, crc 0x%08x\n",
107 total_bytes, calculated_crc);
109 printf("\n\tuncompressed %llu of %llu\n"
110 "\tcrcs == 0x%08x/0x%08x\n",
111 bytes_written, total_bytes,
112 expected_crc, calculated_crc);
116 int gzwrite(unsigned char *src, int len,
117 struct blk_desc *dev,
118 unsigned long szwritebuf,
125 unsigned char *writebuf;
128 lbaint_t blksperbuf, outblock;
134 (szwritebuf % dev->blksz) ||
135 (szwritebuf < dev->blksz)) {
136 printf("%s: size %lu not a multiple of %lu\n",
137 __func__, szwritebuf, dev->blksz);
141 if (startoffs & (dev->blksz-1)) {
142 printf("%s: start offset %llu not a multiple of %lu\n",
143 __func__, startoffs, dev->blksz);
147 blksperbuf = szwritebuf / dev->blksz;
148 outblock = lldiv(startoffs, dev->blksz);
153 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
154 puts("Error: Bad gzipped data\n");
157 if ((flags & EXTRA_FIELD) != 0)
158 i = 12 + src[10] + (src[11] << 8);
159 if ((flags & ORIG_NAME) != 0)
160 while (src[i++] != 0)
162 if ((flags & COMMENT) != 0)
163 while (src[i++] != 0)
165 if ((flags & HEAD_CRC) != 0)
169 puts("Error: gunzip out of data in header");
173 payload_size = len - i - 8;
175 memcpy(&expected_crc, src + len - 8, sizeof(expected_crc));
176 expected_crc = le32_to_cpu(expected_crc);
178 memcpy(&szuncompressed, src + len - 4, sizeof(szuncompressed));
179 if (szexpected == 0) {
180 szexpected = le32_to_cpu(szuncompressed);
181 } else if (szuncompressed != (u32)szexpected) {
182 printf("size of %llx doesn't match trailer low bits %x\n",
183 szexpected, szuncompressed);
186 if (lldiv(szexpected, dev->blksz) > (dev->lba - outblock)) {
187 printf("%s: uncompressed size %llu exceeds device size\n",
188 __func__, szexpected);
192 gzwrite_progress_init(szexpected);
197 r = inflateInit2(&s, -MAX_WBITS);
199 printf("Error: inflateInit2() returned %d\n", r);
204 s.avail_in = payload_size+8;
205 writebuf = (unsigned char *)malloc_cache_aligned(szwritebuf);
207 /* decompress until deflate stream ends or end of file */
209 if (s.avail_in == 0) {
210 printf("%s: weird termination with result %d\n",
215 /* run inflate() on input until output buffer not full */
217 unsigned long blocks_written;
219 lbaint_t writeblocks;
221 s.avail_out = szwritebuf;
222 s.next_out = writebuf;
223 r = inflate(&s, Z_SYNC_FLUSH);
225 (r != Z_STREAM_END)) {
226 printf("Error: inflate() returned %d\n", r);
229 numfilled = szwritebuf - s.avail_out;
230 crc = crc32(crc, writebuf, numfilled);
231 totalfilled += numfilled;
232 if (numfilled < szwritebuf) {
233 writeblocks = (numfilled+dev->blksz-1)
235 memset(writebuf+numfilled, 0,
236 dev->blksz-(numfilled%dev->blksz));
238 writeblocks = blksperbuf;
241 gzwrite_progress(iteration++,
244 blocks_written = blk_dwrite(dev, outblock,
245 writeblocks, writebuf);
246 outblock += blocks_written;
252 } while (s.avail_out == 0);
253 /* done when inflate() says it's done */
254 } while (r != Z_STREAM_END);
256 if ((szexpected != totalfilled) ||
257 (crc != expected_crc))
263 gzwrite_progress_finish(r, totalfilled, szexpected,
273 * Uncompress blocks compressed with zlib without headers
275 int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
276 int stoponerr, int offset)
285 r = inflateInit2(&s, -MAX_WBITS);
287 printf("Error: inflateInit2() returned %d\n", r);
290 s.next_in = src + offset;
291 s.avail_in = *lenp - offset;
293 s.avail_out = dstlen;
295 r = inflate(&s, Z_FINISH);
296 if (stoponerr == 1 && r != Z_STREAM_END &&
297 (s.avail_in == 0 || s.avail_out == 0 || r != Z_BUF_ERROR)) {
298 printf("Error: inflate() returned %d\n", r);
302 } while (r == Z_BUF_ERROR);
303 *lenp = s.next_out - (unsigned char *) dst;