]>
git.ipfire.org Git - people/ms/u-boot.git/blob - lib/gunzip.c
2 * (C) Copyright 2000-2006
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5 * SPDX-License-Identifier: GPL-2.0+
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 gunzip(void *dst
, int dstlen
, unsigned char *src
, unsigned long *lenp
)
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");
70 return zunzip(dst
, dstlen
, src
, lenp
, 1, i
);
73 #ifdef CONFIG_CMD_UNZIP
75 void gzwrite_progress_init(u64 expectedsize
)
81 void gzwrite_progress(int iteration
,
85 if (0 == (iteration
& 3))
86 printf("%llu/%llu\r", bytes_written
, total_bytes
);
90 void gzwrite_progress_finish(int returnval
,
97 printf("\n\t%llu bytes, crc 0x%08x\n",
98 total_bytes
, calculated_crc
);
100 printf("\n\tuncompressed %llu of %llu\n"
101 "\tcrcs == 0x%08x/0x%08x\n",
102 bytes_written
, total_bytes
,
103 expected_crc
, calculated_crc
);
107 int gzwrite(unsigned char *src
, int len
,
108 struct blk_desc
*dev
,
109 unsigned long szwritebuf
,
116 unsigned char *writebuf
;
119 lbaint_t blksperbuf
, outblock
;
125 (szwritebuf
% dev
->blksz
) ||
126 (szwritebuf
< dev
->blksz
)) {
127 printf("%s: size %lu not a multiple of %lu\n",
128 __func__
, szwritebuf
, dev
->blksz
);
132 if (startoffs
& (dev
->blksz
-1)) {
133 printf("%s: start offset %llu not a multiple of %lu\n",
134 __func__
, startoffs
, dev
->blksz
);
138 blksperbuf
= szwritebuf
/ dev
->blksz
;
139 outblock
= lldiv(startoffs
, dev
->blksz
);
144 if (src
[2] != DEFLATED
|| (flags
& RESERVED
) != 0) {
145 puts("Error: Bad gzipped data\n");
148 if ((flags
& EXTRA_FIELD
) != 0)
149 i
= 12 + src
[10] + (src
[11] << 8);
150 if ((flags
& ORIG_NAME
) != 0)
151 while (src
[i
++] != 0)
153 if ((flags
& COMMENT
) != 0)
154 while (src
[i
++] != 0)
156 if ((flags
& HEAD_CRC
) != 0)
160 puts("Error: gunzip out of data in header");
164 payload_size
= len
- i
- 8;
166 memcpy(&expected_crc
, src
+ len
- 8, sizeof(expected_crc
));
167 expected_crc
= le32_to_cpu(expected_crc
);
169 memcpy(&szuncompressed
, src
+ len
- 4, sizeof(szuncompressed
));
170 if (szexpected
== 0) {
171 szexpected
= le32_to_cpu(szuncompressed
);
172 } else if (szuncompressed
!= (u32
)szexpected
) {
173 printf("size of %llx doesn't match trailer low bits %x\n",
174 szexpected
, szuncompressed
);
177 if (lldiv(szexpected
, dev
->blksz
) > (dev
->lba
- outblock
)) {
178 printf("%s: uncompressed size %llu exceeds device size\n",
179 __func__
, szexpected
);
183 gzwrite_progress_init(szexpected
);
188 r
= inflateInit2(&s
, -MAX_WBITS
);
190 printf("Error: inflateInit2() returned %d\n", r
);
195 s
.avail_in
= payload_size
+8;
196 writebuf
= (unsigned char *)malloc(szwritebuf
);
198 /* decompress until deflate stream ends or end of file */
200 if (s
.avail_in
== 0) {
201 printf("%s: weird termination with result %d\n",
206 /* run inflate() on input until output buffer not full */
208 unsigned long blocks_written
;
210 lbaint_t writeblocks
;
212 s
.avail_out
= szwritebuf
;
213 s
.next_out
= writebuf
;
214 r
= inflate(&s
, Z_SYNC_FLUSH
);
216 (r
!= Z_STREAM_END
)) {
217 printf("Error: inflate() returned %d\n", r
);
220 numfilled
= szwritebuf
- s
.avail_out
;
221 crc
= crc32(crc
, writebuf
, numfilled
);
222 totalfilled
+= numfilled
;
223 if (numfilled
< szwritebuf
) {
224 writeblocks
= (numfilled
+dev
->blksz
-1)
226 memset(writebuf
+numfilled
, 0,
227 dev
->blksz
-(numfilled
%dev
->blksz
));
229 writeblocks
= blksperbuf
;
232 gzwrite_progress(iteration
++,
235 blocks_written
= blk_dwrite(dev
, outblock
,
236 writeblocks
, writebuf
);
237 outblock
+= blocks_written
;
243 } while (s
.avail_out
== 0);
244 /* done when inflate() says it's done */
245 } while (r
!= Z_STREAM_END
);
247 if ((szexpected
!= totalfilled
) ||
248 (crc
!= expected_crc
))
254 gzwrite_progress_finish(r
, totalfilled
, szexpected
,
264 * Uncompress blocks compressed with zlib without headers
266 int zunzip(void *dst
, int dstlen
, unsigned char *src
, unsigned long *lenp
,
267 int stoponerr
, int offset
)
276 r
= inflateInit2(&s
, -MAX_WBITS
);
278 printf("Error: inflateInit2() returned %d\n", r
);
281 s
.next_in
= src
+ offset
;
282 s
.avail_in
= *lenp
- offset
;
284 s
.avail_out
= dstlen
;
286 r
= inflate(&s
, Z_FINISH
);
287 if (stoponerr
== 1 && r
!= Z_STREAM_END
&&
288 (s
.avail_in
== 0 || s
.avail_out
== 0 || r
!= Z_BUF_ERROR
)) {
289 printf("Error: inflate() returned %d\n", r
);
293 } while (r
== Z_BUF_ERROR
);
294 *lenp
= s
.next_out
- (unsigned char *) dst
;