1420Sstevel /*
2420Sstevel * CDDL HEADER START
3420Sstevel *
4420Sstevel * The contents of this file are subject to the terms of the
53446Smrj * Common Development and Distribution License (the "License").
63446Smrj * You may not use this file except in compliance with the License.
7420Sstevel *
8420Sstevel * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9420Sstevel * or http://www.opensolaris.org/os/licensing.
10420Sstevel * See the License for the specific language governing permissions
11420Sstevel * and limitations under the License.
12420Sstevel *
13420Sstevel * When distributing Covered Code, include this CDDL HEADER in each
14420Sstevel * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15420Sstevel * If applicable, add the following below this CDDL HEADER, with the
16420Sstevel * fields enclosed by brackets "[]" replaced with your own identifying
17420Sstevel * information: Portions Copyright [yyyy] [name of copyright owner]
18420Sstevel *
19420Sstevel * CDDL HEADER END
20420Sstevel */
21420Sstevel
220Sstevel@tonic-gate /*
23*7858SKrishnendu.Sadhukhan@Sun.COM * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate #include <sys/modctl.h>
280Sstevel@tonic-gate #include <sys/zmod.h>
290Sstevel@tonic-gate
300Sstevel@tonic-gate #include "zlib.h"
31*7858SKrishnendu.Sadhukhan@Sun.COM #include "zutil.h"
320Sstevel@tonic-gate
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate * Uncompress the buffer 'src' into the buffer 'dst'. The caller must store
350Sstevel@tonic-gate * the expected decompressed data size externally so it can be passed in.
360Sstevel@tonic-gate * The resulting decompressed size is then returned through dstlen. This
370Sstevel@tonic-gate * function return Z_OK on success, or another error code on failure.
380Sstevel@tonic-gate */
390Sstevel@tonic-gate int
z_uncompress(void * dst,size_t * dstlen,const void * src,size_t srclen)400Sstevel@tonic-gate z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen)
410Sstevel@tonic-gate {
420Sstevel@tonic-gate z_stream zs;
430Sstevel@tonic-gate int err;
440Sstevel@tonic-gate
450Sstevel@tonic-gate bzero(&zs, sizeof (zs));
460Sstevel@tonic-gate zs.next_in = (uchar_t *)src;
470Sstevel@tonic-gate zs.avail_in = srclen;
480Sstevel@tonic-gate zs.next_out = dst;
490Sstevel@tonic-gate zs.avail_out = *dstlen;
500Sstevel@tonic-gate
51*7858SKrishnendu.Sadhukhan@Sun.COM /*
52*7858SKrishnendu.Sadhukhan@Sun.COM * Call inflateInit2() specifying a window size of DEF_WBITS
53*7858SKrishnendu.Sadhukhan@Sun.COM * with the 6th bit set to indicate that the compression format
54*7858SKrishnendu.Sadhukhan@Sun.COM * type (zlib or gzip) should be automatically detected.
55*7858SKrishnendu.Sadhukhan@Sun.COM */
56*7858SKrishnendu.Sadhukhan@Sun.COM if ((err = inflateInit2(&zs, DEF_WBITS | 0x20)) != Z_OK)
570Sstevel@tonic-gate return (err);
580Sstevel@tonic-gate
590Sstevel@tonic-gate if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) {
600Sstevel@tonic-gate (void) inflateEnd(&zs);
610Sstevel@tonic-gate return (err == Z_OK ? Z_BUF_ERROR : err);
620Sstevel@tonic-gate }
630Sstevel@tonic-gate
640Sstevel@tonic-gate *dstlen = zs.total_out;
650Sstevel@tonic-gate return (inflateEnd(&zs));
660Sstevel@tonic-gate }
670Sstevel@tonic-gate
683886Sahl int
z_compress_level(void * dst,size_t * dstlen,const void * src,size_t srclen,int level)693886Sahl z_compress_level(void *dst, size_t *dstlen, const void *src, size_t srclen,
703886Sahl int level)
713886Sahl {
723886Sahl
733886Sahl z_stream zs;
743886Sahl int err;
753886Sahl
763886Sahl bzero(&zs, sizeof (zs));
773886Sahl zs.next_in = (uchar_t *)src;
783886Sahl zs.avail_in = srclen;
793886Sahl zs.next_out = dst;
803886Sahl zs.avail_out = *dstlen;
813886Sahl
823886Sahl if ((err = deflateInit(&zs, level)) != Z_OK)
833886Sahl return (err);
843886Sahl
853886Sahl if ((err = deflate(&zs, Z_FINISH)) != Z_STREAM_END) {
863886Sahl (void) deflateEnd(&zs);
873886Sahl return (err == Z_OK ? Z_BUF_ERROR : err);
883886Sahl }
893886Sahl
903886Sahl *dstlen = zs.total_out;
913886Sahl return (deflateEnd(&zs));
923886Sahl }
933886Sahl
943886Sahl int
z_compress(void * dst,size_t * dstlen,const void * src,size_t srclen)953886Sahl z_compress(void *dst, size_t *dstlen, const void *src, size_t srclen)
963886Sahl {
973886Sahl return (z_compress_level(dst, dstlen, src, srclen,
983886Sahl Z_DEFAULT_COMPRESSION));
993886Sahl }
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate * Convert a zlib error code into a string error message.
1030Sstevel@tonic-gate */
1040Sstevel@tonic-gate const char *
z_strerror(int err)1050Sstevel@tonic-gate z_strerror(int err)
1060Sstevel@tonic-gate {
1070Sstevel@tonic-gate int i = Z_NEED_DICT - err;
1080Sstevel@tonic-gate
1093886Sahl if (i < 0 || i > Z_NEED_DICT - Z_VERSION_ERROR)
1100Sstevel@tonic-gate return ("unknown error");
1110Sstevel@tonic-gate
1123886Sahl return (zError(err));
1130Sstevel@tonic-gate }
114