3 * Lei Wen <leiwen@marvell.com>, Marvell Inc.
5 * See file CREDITS for list of people who contributed to this
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #include <u-boot/zlib.h>
30 #include "zlib/zutil.h"
32 #ifndef CONFIG_GZIP_COMPRESS_DEF_SZ
33 #define CONFIG_GZIP_COMPRESS_DEF_SZ 0x200
35 #define ZALLOC_ALIGNMENT 16
37 static void *zalloc(void *x
, unsigned items
, unsigned size
)
42 size
= (size
+ ZALLOC_ALIGNMENT
- 1) & ~(ZALLOC_ALIGNMENT
- 1);
49 static void zfree(void *x
, void *addr
, unsigned nb
)
54 int gzip(void *dst
, unsigned long *lenp
,
55 unsigned char *src
, unsigned long srclen
)
57 return zzip(dst
, lenp
, src
, srclen
, 1, NULL
);
61 * Compress blocks with zlib
63 int zzip(void *dst
, unsigned long *lenp
, unsigned char *src
,
64 unsigned long srclen
, int stoponerr
,
65 int (*func
)(unsigned long, unsigned long))
68 int r
, flush
, orig
, window
;
69 unsigned long comp_len
, left_len
;
77 window
= 2 * MAX_WBITS
;
84 r
= deflateInit2_(&s
, Z_BEST_SPEED
, Z_DEFLATED
, window
,
85 DEF_MEM_LEVEL
, Z_DEFAULT_STRATEGY
,
86 ZLIB_VERSION
, sizeof(z_stream
));
88 printf ("Error: deflateInit2_() returned %d\n", r
);
93 comp_len
= (srclen
> CONFIG_GZIP_COMPRESS_DEF_SZ
) ?
94 CONFIG_GZIP_COMPRESS_DEF_SZ
: srclen
;
97 s
.avail_in
= comp_len
;
98 flush
= (srclen
> CONFIG_GZIP_COMPRESS_DEF_SZ
)?
99 Z_NO_FLUSH
: Z_FINISH
;
102 left_len
= (*lenp
> CONFIG_GZIP_COMPRESS_DEF_SZ
) ?
103 CONFIG_GZIP_COMPRESS_DEF_SZ
: *lenp
;
105 s
.avail_out
= left_len
;
106 r
= deflate(&s
, flush
);
107 if (r
== Z_STREAM_ERROR
&& stoponerr
== 1) {
108 printf("Error: deflate() returned %d\n", r
);
113 dst
+= (left_len
- s
.avail_out
);
114 *lenp
-= (left_len
- s
.avail_out
);
115 } else if (left_len
- s
.avail_out
> 0) {
116 r
= func((unsigned long)dst
,
117 left_len
- s
.avail_out
);
121 } while (s
.avail_out
== 0 && (*lenp
> 0));
123 printf("Deflate failed to consume %u bytes", s
.avail_in
);
128 printf("Deflate need more space to compress "
129 "left %lu bytes\n", srclen
);
140 *lenp
= orig
- *lenp
;