2 * (C) Copyright 2000-2006
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
13 #include <u-boot/zlib.h>
15 #define ZALLOC_ALIGNMENT 16
23 void *gzalloc(void *x, unsigned items, unsigned size)
28 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
35 void gzfree(void *x, void *addr, unsigned nb)
40 int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
47 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
48 puts ("Error: Bad gzipped data\n");
51 if ((flags & EXTRA_FIELD) != 0)
52 i = 12 + src[10] + (src[11] << 8);
53 if ((flags & ORIG_NAME) != 0)
56 if ((flags & COMMENT) != 0)
59 if ((flags & HEAD_CRC) != 0)
62 puts ("Error: gunzip out of data in header\n");
66 return zunzip(dst, dstlen, src, lenp, 1, i);
70 * Uncompress blocks compressed with zlib without headers
72 int zunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp,
73 int stoponerr, int offset)
81 r = inflateInit2(&s, -MAX_WBITS);
83 printf ("Error: inflateInit2() returned %d\n", r);
86 s.next_in = src + offset;
87 s.avail_in = *lenp - offset;
91 r = inflate(&s, Z_FINISH);
92 if (stoponerr == 1 && r != Z_STREAM_END &&
93 (s.avail_out == 0 || r != Z_BUF_ERROR)) {
94 printf("Error: inflate() returned %d\n", r);
98 s.avail_in = *lenp - offset - (int)(s.next_out - (unsigned char*)dst);
99 } while (r == Z_BUF_ERROR);
100 *lenp = s.next_out - (unsigned char *) dst;