199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * This file and its contents are supplied under the terms of the
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Common Development and Distribution License ("CDDL"), version 1.0.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * You may only use this file in accordance with the terms of version
199767f8919635c4928607450d9e0abb932109ceToomas Soome * 1.0 of the CDDL.
199767f8919635c4928607450d9e0abb932109ceToomas Soome *
199767f8919635c4928607450d9e0abb932109ceToomas Soome * A full copy of the text of the CDDL should have accompanied this
199767f8919635c4928607450d9e0abb932109ceToomas Soome * source. A copy of the CDDL is also available via the Internet at
199767f8919635c4928607450d9e0abb932109ceToomas Soome * http://www.illumos.org/license/CDDL.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright 2015 Toomas Soome <tsoome@me.com>
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Use is subject to license terms.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <zlib.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome#include <strings.h>
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint gzip_decompress(void *, void *, size_t, size_t, int);
199767f8919635c4928607450d9e0abb932109ceToomas Soome/*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Uncompress the buffer 'src' into the buffer 'dst'. The caller must store
199767f8919635c4928607450d9e0abb932109ceToomas Soome * the expected decompressed data size externally so it can be passed in.
199767f8919635c4928607450d9e0abb932109ceToomas Soome * The resulting decompressed size is then returned through dstlen. This
199767f8919635c4928607450d9e0abb932109ceToomas Soome * function return Z_OK on success, or another error code on failure.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soomestatic int
199767f8919635c4928607450d9e0abb932109ceToomas Soomez_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome z_stream zs;
199767f8919635c4928607450d9e0abb932109ceToomas Soome int err;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome bzero(&zs, sizeof (zs));
199767f8919635c4928607450d9e0abb932109ceToomas Soome zs.next_in = (unsigned char *)src;
199767f8919635c4928607450d9e0abb932109ceToomas Soome zs.avail_in = srclen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome zs.next_out = dst;
199767f8919635c4928607450d9e0abb932109ceToomas Soome zs.avail_out = *dstlen;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome /*
199767f8919635c4928607450d9e0abb932109ceToomas Soome * Call inflateInit2() specifying a window size of DEF_WBITS
199767f8919635c4928607450d9e0abb932109ceToomas Soome * with the 6th bit set to indicate that the compression format
199767f8919635c4928607450d9e0abb932109ceToomas Soome * type (zlib or gzip) should be automatically detected.
199767f8919635c4928607450d9e0abb932109ceToomas Soome */
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((err = inflateInit2(&zs, 15 | 0x20)) != Z_OK)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) {
199767f8919635c4928607450d9e0abb932109ceToomas Soome (void) inflateEnd(&zs);
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (err == Z_OK ? Z_BUF_ERROR : err);
199767f8919635c4928607450d9e0abb932109ceToomas Soome }
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome *dstlen = zs.total_out;
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (inflateEnd(&zs));
199767f8919635c4928607450d9e0abb932109ceToomas Soome}
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soomeint
199767f8919635c4928607450d9e0abb932109ceToomas Soomegzip_decompress(void *s_start, void *d_start, size_t s_len, size_t d_len, int n)
199767f8919635c4928607450d9e0abb932109ceToomas Soome{
199767f8919635c4928607450d9e0abb932109ceToomas Soome size_t dstlen = d_len;
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome if (z_uncompress(d_start, &dstlen, s_start, s_len) != Z_OK)
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (-1);
199767f8919635c4928607450d9e0abb932109ceToomas Soome
199767f8919635c4928607450d9e0abb932109ceToomas Soome return (0);
199767f8919635c4928607450d9e0abb932109ceToomas Soome}