2 * (C) Copyright 2000-2006
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
15 #include <u-boot/zlib.h>
18 #define HEADER0 '\x1f'
19 #define HEADER1 '\x8b'
20 #define ZALLOC_ALIGNMENT 16
28 void *gzalloc(void *x, unsigned items, unsigned size)
33 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
40 void gzfree(void *x, void *addr, unsigned nb)
45 int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
52 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
53 puts ("Error: Bad gzipped data\n");
56 if ((flags & EXTRA_FIELD) != 0)
57 i = 12 + src[10] + (src[11] << 8);
58 if ((flags & ORIG_NAME) != 0)
61 if ((flags & COMMENT) != 0)
64 if ((flags & HEAD_CRC) != 0)
67 puts ("Error: gunzip out of data in header\n");
71 return zunzip(dst, dstlen, src, lenp, 1, i);
74 #ifdef CONFIG_CMD_UNZIP
76 void gzwrite_progress_init(u64 expectedsize)
82 void gzwrite_progress(int iteration,
86 if (0 == (iteration & 3))
87 printf("%llu/%llu\r", bytes_written, total_bytes);
91 void gzwrite_progress_finish(int returnval,
98 printf("\n\t%llu bytes, crc 0x%08x\n",
99 total_bytes, calculated_crc);
101 printf("\n\tuncompressed %llu of %llu\n"
102 "\tcrcs == 0x%08x/0x%08x\n",
103 bytes_written, total_bytes,
104 expected_crc, calculated_crc);
108 int gzwrite(unsigned char *src, int len,
109 struct blk_desc *dev,
110 unsigned long szwritebuf,
117 unsigned char *writebuf;
120 lbaint_t blksperbuf, outblock;
126 (szwritebuf % dev->blksz) ||
127 (szwritebuf < dev->blksz)) {
128 printf("%s: size %lu not a multiple of %lu\n",
129 __func__, szwritebuf, dev->blksz);
133 if (startoffs & (dev->blksz-1)) {
134 printf("%s: start offset %llu not a multiple of %lu\n",
135 __func__, startoffs, dev->blksz);
139 blksperbuf = szwritebuf / dev->blksz;
140 outblock = lldiv(startoffs, dev->blksz);
145 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
146 puts("Error: Bad gzipped data\n");
149 if ((flags & EXTRA_FIELD) != 0)
150 i = 12 + src[10] + (src[11] << 8);
151 if ((flags & ORIG_NAME) != 0)
152 while (src[i++] != 0)
154 if ((flags & COMMENT) != 0)
155 while (src[i++] != 0)
157 if ((flags & HEAD_CRC) != 0)
161 puts("Error: gunzip out of data in header");
165 payload_size = len - i - 8;
167 memcpy(&expected_crc, src + len - 8, sizeof(expected_crc));
168 expected_crc = le32_to_cpu(expected_crc);
170 memcpy(&szuncompressed, src + len - 4, sizeof(szuncompressed));
171 if (szexpected == 0) {
172 szexpected = le32_to_cpu(szuncompressed);
173 } else if (szuncompressed != (u32)szexpected) {
174 printf("size of %llx doesn't match trailer low bits %x\n",
175 szexpected, szuncompressed);
178 if (lldiv(szexpected, dev->blksz) > (dev->lba - outblock)) {
179 printf("%s: uncompressed size %llu exceeds device size\n",
180 __func__, szexpected);
184 gzwrite_progress_init(szexpected);
189 r = inflateInit2(&s, -MAX_WBITS);
191 printf("Error: inflateInit2() returned %d\n", r);
196 s.avail_in = payload_size+8;
197 writebuf = (unsigned char *)malloc_cache_aligned(szwritebuf);
199 /* decompress until deflate stream ends or end of file */
201 if (s.avail_in == 0) {
202 printf("%s: weird termination with result %d\n",
207 /* run inflate() on input until output buffer not full */
209 unsigned long blocks_written;
211 lbaint_t writeblocks;
213 s.avail_out = szwritebuf;
214 s.next_out = writebuf;
215 r = inflate(&s, Z_SYNC_FLUSH);
217 (r != Z_STREAM_END)) {
218 printf("Error: inflate() returned %d\n", r);
221 numfilled = szwritebuf - s.avail_out;
222 crc = crc32(crc, writebuf, numfilled);
223 totalfilled += numfilled;
224 if (numfilled < szwritebuf) {
225 writeblocks = (numfilled+dev->blksz-1)
227 memset(writebuf+numfilled, 0,
228 dev->blksz-(numfilled%dev->blksz));
230 writeblocks = blksperbuf;
233 gzwrite_progress(iteration++,
236 blocks_written = blk_dwrite(dev, outblock,
237 writeblocks, writebuf);
238 outblock += blocks_written;
244 } while (s.avail_out == 0);
245 /* done when inflate() says it's done */
246 } while (r != Z_STREAM_END);
248 if ((szexpected != totalfilled) ||
249 (crc != expected_crc))
255 gzwrite_progress_finish(r, totalfilled, szexpected,
265 * Uncompress blocks compressed with zlib without headers
267 int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
268 int stoponerr, int offset)
277 r = inflateInit2(&s, -MAX_WBITS);
279 printf("Error: inflateInit2() returned %d\n", r);
282 s.next_in = src + offset;
283 s.avail_in = *lenp - offset;
285 s.avail_out = dstlen;
287 r = inflate(&s, Z_FINISH);
288 if (stoponerr == 1 && r != Z_STREAM_END &&
289 (s.avail_in == 0 || s.avail_out == 0 || r != Z_BUF_ERROR)) {
290 printf("Error: inflate() returned %d\n", r);
294 } while (r == Z_BUF_ERROR);
295 *lenp = s.next_out - (unsigned char *) dst;