[PATCH 7/8] Add deflate_decompress function

Sascha Hauer s.hauer at pengutronix.de
Tue Aug 6 09:07:07 EDT 2013


Needed to implement decompressors for gzip without headers.

Signed-off-by: Sascha Hauer <s.hauer at pengutronix.de>
---
 include/linux/zlib.h     |  4 ++++
 lib/decompress_inflate.c | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 43 insertions(+)

diff --git a/include/linux/zlib.h b/include/linux/zlib.h
index d8bd20c..a3e006b 100644
--- a/include/linux/zlib.h
+++ b/include/linux/zlib.h
@@ -31,6 +31,7 @@
 #define _ZLIB_H
 
 #include <linux/zconf.h>
+#include <linux/types.h>
 
 /* zlib deflate based on ZLIB_VERSION "1.1.3" */
 /* zlib inflate based on ZLIB_VERSION "1.2.3" */
@@ -708,4 +709,7 @@ extern int zlib_inflateInit2(z_streamp strm, int  windowBits);
  * return len or negative error code. */
 extern int zlib_inflate_blob(void *dst, unsigned dst_sz, const void *src, unsigned src_sz);
 
+int deflate_decompress(struct z_stream_s *s, const u8 *src, unsigned int slen,
+		u8 *dst, unsigned int *dlen);
+
 #endif /* _ZLIB_H */
diff --git a/lib/decompress_inflate.c b/lib/decompress_inflate.c
index 5c1ebb6..d5ea01a 100644
--- a/lib/decompress_inflate.c
+++ b/lib/decompress_inflate.c
@@ -183,4 +183,43 @@ gunzip_nomem1:
 	return rc; /* returns Z_OK (0) if successful */
 }
 
+int deflate_decompress(struct z_stream_s *stream, const u8 *src, unsigned int slen, u8 *dst,
+		unsigned int *dlen)
+{
+
+	int ret = 0;
+
+	ret = zlib_inflateReset(stream);
+	if (ret != Z_OK) {
+		ret = -EINVAL;
+		goto out;
+	}
+
+	stream->next_in = (u8 *)src;
+	stream->avail_in = slen;
+	stream->next_out = (u8 *)dst;
+	stream->avail_out = *dlen;
+
+	ret = zlib_inflate(stream, Z_SYNC_FLUSH);
+	/*
+	 * Work around a bug in zlib, which sometimes wants to taste an extra
+	 * byte when being used in the (undocumented) raw deflate mode.
+	 * (From USAGI).
+	 */
+	if (ret == Z_OK && !stream->avail_in && stream->avail_out) {
+		u8 zerostuff = 0;
+		stream->next_in = &zerostuff;
+		stream->avail_in = 1;
+		ret = zlib_inflate(stream, Z_FINISH);
+	}
+	if (ret != Z_STREAM_END) {
+		ret = -EINVAL;
+		goto out;
+	}
+	ret = 0;
+	*dlen = stream->total_out;
+out:
+	return ret;
+}
+
 #define decompress gunzip
-- 
1.8.4.rc1




More information about the barebox mailing list