diff --git a/include/linux/zlib.h b/include/linux/zlib.h index d8bd20c89..a3e006bad 100644 --- a/include/linux/zlib.h +++ b/include/linux/zlib.h @@ -31,6 +31,7 @@ #define _ZLIB_H #include +#include /* 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 5c1ebb685..d5ea01ad5 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