]>
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+
13 #include <u-boot/zlib.h>
16 #define HEADER0 '\x1f'
17 #define HEADER1 '\x8b'
18 #define ZALLOC_ALIGNMENT 16
26 void *gzalloc(void *x
, unsigned items
, unsigned size
)
31 size
= (size
+ ZALLOC_ALIGNMENT
- 1) & ~(ZALLOC_ALIGNMENT
- 1);
38 void gzfree(void *x
, void *addr
, unsigned nb
)
43 int gunzip(void *dst
, int dstlen
, unsigned char *src
, unsigned long *lenp
)
50 if (src
[2] != DEFLATED
|| (flags
& RESERVED
) != 0) {
51 puts ("Error: Bad gzipped data\n");
54 if ((flags
& EXTRA_FIELD
) != 0)
55 i
= 12 + src
[10] + (src
[11] << 8);
56 if ((flags
& ORIG_NAME
) != 0)
59 if ((flags
& COMMENT
) != 0)
62 if ((flags
& HEAD_CRC
) != 0)
65 puts ("Error: gunzip out of data in header\n");
69 return zunzip(dst
, dstlen
, src
, lenp
, 1, i
);
73 void gzwrite_progress_init(u64 expectedsize
)
79 void gzwrite_progress(int iteration
,
83 if (0 == (iteration
& 3))
84 printf("%llu/%llu\r", bytes_written
, total_bytes
);
88 void gzwrite_progress_finish(int returnval
,
95 printf("\n\t%llu bytes, crc 0x%08x\n",
96 total_bytes
, calculated_crc
);
98 printf("\n\tuncompressed %llu of %llu\n"
99 "\tcrcs == 0x%08x/0x%08x\n",
100 bytes_written
, total_bytes
,
101 expected_crc
, calculated_crc
);
105 int gzwrite(unsigned char *src
, int len
,
106 struct block_dev_desc
*dev
,
107 unsigned long szwritebuf
,
114 unsigned char *writebuf
;
117 lbaint_t blksperbuf
, outblock
;
123 (szwritebuf
% dev
->blksz
) ||
124 (szwritebuf
< dev
->blksz
)) {
125 printf("%s: size %lu not a multiple of %lu\n",
126 __func__
, szwritebuf
, dev
->blksz
);
130 if (startoffs
& (dev
->blksz
-1)) {
131 printf("%s: start offset %llu not a multiple of %lu\n",
132 __func__
, startoffs
, dev
->blksz
);
136 blksperbuf
= szwritebuf
/ dev
->blksz
;
137 outblock
= lldiv(startoffs
, dev
->blksz
);
142 if (src
[2] != DEFLATED
|| (flags
& RESERVED
) != 0) {
143 puts("Error: Bad gzipped data\n");
146 if ((flags
& EXTRA_FIELD
) != 0)
147 i
= 12 + src
[10] + (src
[11] << 8);
148 if ((flags
& ORIG_NAME
) != 0)
149 while (src
[i
++] != 0)
151 if ((flags
& COMMENT
) != 0)
152 while (src
[i
++] != 0)
154 if ((flags
& HEAD_CRC
) != 0)
158 puts("Error: gunzip out of data in header");
162 payload_size
= len
- i
- 8;
164 memcpy(&expected_crc
, src
+ len
- 8, sizeof(expected_crc
));
165 expected_crc
= le32_to_cpu(expected_crc
);
167 memcpy(&szuncompressed
, src
+ len
- 4, sizeof(szuncompressed
));
168 if (szexpected
== 0) {
169 szexpected
= le32_to_cpu(szuncompressed
);
170 } else if (szuncompressed
!= (u32
)szexpected
) {
171 printf("size of %llx doesn't match trailer low bits %x\n",
172 szexpected
, szuncompressed
);
175 if (lldiv(szexpected
, dev
->blksz
) > (dev
->lba
- outblock
)) {
176 printf("%s: uncompressed size %llu exceeds device size\n",
177 __func__
, szexpected
);
181 gzwrite_progress_init(szexpected
);
186 r
= inflateInit2(&s
, -MAX_WBITS
);
188 printf("Error: inflateInit2() returned %d\n", r
);
193 s
.avail_in
= payload_size
+8;
194 writebuf
= (unsigned char *)malloc(szwritebuf
);
196 /* decompress until deflate stream ends or end of file */
198 if (s
.avail_in
== 0) {
199 printf("%s: weird termination with result %d\n",
204 /* run inflate() on input until output buffer not full */
206 unsigned long blocks_written
;
208 lbaint_t writeblocks
;
210 s
.avail_out
= szwritebuf
;
211 s
.next_out
= writebuf
;
212 r
= inflate(&s
, Z_SYNC_FLUSH
);
214 (r
!= Z_STREAM_END
)) {
215 printf("Error: inflate() returned %d\n", r
);
218 numfilled
= szwritebuf
- s
.avail_out
;
219 crc
= crc32(crc
, writebuf
, numfilled
);
220 totalfilled
+= numfilled
;
221 if (numfilled
< szwritebuf
) {
222 writeblocks
= (numfilled
+dev
->blksz
-1)
224 memset(writebuf
+numfilled
, 0,
225 dev
->blksz
-(numfilled
%dev
->blksz
));
227 writeblocks
= blksperbuf
;
230 gzwrite_progress(iteration
++,
233 blocks_written
= dev
->block_write(dev
->dev
,
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
,
263 * Uncompress blocks compressed with zlib without headers
265 int zunzip(void *dst
, int dstlen
, unsigned char *src
, unsigned long *lenp
,
266 int stoponerr
, int offset
)
275 r
= inflateInit2(&s
, -MAX_WBITS
);
277 printf("Error: inflateInit2() returned %d\n", r
);
280 s
.next_in
= src
+ offset
;
281 s
.avail_in
= *lenp
- offset
;
283 s
.avail_out
= dstlen
;
285 r
= inflate(&s
, Z_FINISH
);
286 if (stoponerr
== 1 && r
!= Z_STREAM_END
&&
287 (s
.avail_out
== 0 || r
!= Z_BUF_ERROR
)) {
288 printf("Error: inflate() returned %d\n", r
);
292 s
.avail_in
= *lenp
- offset
- (int)(s
.next_out
- (unsigned char*)dst
);
293 } while (r
== Z_BUF_ERROR
);
294 *lenp
= s
.next_out
- (unsigned char *) dst
;