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 /* 233446Smrj * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate #include <sys/modctl.h> 300Sstevel@tonic-gate #include <sys/zmod.h> 310Sstevel@tonic-gate 320Sstevel@tonic-gate #include "zlib.h" 330Sstevel@tonic-gate 340Sstevel@tonic-gate /* 350Sstevel@tonic-gate * Uncompress the buffer 'src' into the buffer 'dst'. The caller must store 360Sstevel@tonic-gate * the expected decompressed data size externally so it can be passed in. 370Sstevel@tonic-gate * The resulting decompressed size is then returned through dstlen. This 380Sstevel@tonic-gate * function return Z_OK on success, or another error code on failure. 390Sstevel@tonic-gate */ 400Sstevel@tonic-gate int 410Sstevel@tonic-gate z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen) 420Sstevel@tonic-gate { 430Sstevel@tonic-gate z_stream zs; 440Sstevel@tonic-gate int err; 450Sstevel@tonic-gate 460Sstevel@tonic-gate bzero(&zs, sizeof (zs)); 470Sstevel@tonic-gate zs.next_in = (uchar_t *)src; 480Sstevel@tonic-gate zs.avail_in = srclen; 490Sstevel@tonic-gate zs.next_out = dst; 500Sstevel@tonic-gate zs.avail_out = *dstlen; 510Sstevel@tonic-gate 520Sstevel@tonic-gate if ((err = inflateInit(&zs)) != Z_OK) 530Sstevel@tonic-gate return (err); 540Sstevel@tonic-gate 550Sstevel@tonic-gate if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) { 560Sstevel@tonic-gate (void) inflateEnd(&zs); 570Sstevel@tonic-gate return (err == Z_OK ? Z_BUF_ERROR : err); 580Sstevel@tonic-gate } 590Sstevel@tonic-gate 600Sstevel@tonic-gate *dstlen = zs.total_out; 610Sstevel@tonic-gate return (inflateEnd(&zs)); 620Sstevel@tonic-gate } 630Sstevel@tonic-gate 64*3886Sahl int 65*3886Sahl z_compress_level(void *dst, size_t *dstlen, const void *src, size_t srclen, 66*3886Sahl int level) 67*3886Sahl { 68*3886Sahl 69*3886Sahl z_stream zs; 70*3886Sahl int err; 71*3886Sahl 72*3886Sahl bzero(&zs, sizeof (zs)); 73*3886Sahl zs.next_in = (uchar_t *)src; 74*3886Sahl zs.avail_in = srclen; 75*3886Sahl zs.next_out = dst; 76*3886Sahl zs.avail_out = *dstlen; 77*3886Sahl 78*3886Sahl if ((err = deflateInit(&zs, level)) != Z_OK) 79*3886Sahl return (err); 80*3886Sahl 81*3886Sahl if ((err = deflate(&zs, Z_FINISH)) != Z_STREAM_END) { 82*3886Sahl (void) deflateEnd(&zs); 83*3886Sahl return (err == Z_OK ? Z_BUF_ERROR : err); 84*3886Sahl } 85*3886Sahl 86*3886Sahl *dstlen = zs.total_out; 87*3886Sahl return (deflateEnd(&zs)); 88*3886Sahl } 89*3886Sahl 90*3886Sahl int 91*3886Sahl z_compress(void *dst, size_t *dstlen, const void *src, size_t srclen) 92*3886Sahl { 93*3886Sahl return (z_compress_level(dst, dstlen, src, srclen, 94*3886Sahl Z_DEFAULT_COMPRESSION)); 95*3886Sahl } 960Sstevel@tonic-gate 970Sstevel@tonic-gate /* 980Sstevel@tonic-gate * Convert a zlib error code into a string error message. 990Sstevel@tonic-gate */ 1000Sstevel@tonic-gate const char * 1010Sstevel@tonic-gate z_strerror(int err) 1020Sstevel@tonic-gate { 1030Sstevel@tonic-gate int i = Z_NEED_DICT - err; 1040Sstevel@tonic-gate 105*3886Sahl if (i < 0 || i > Z_NEED_DICT - Z_VERSION_ERROR) 1060Sstevel@tonic-gate return ("unknown error"); 1070Sstevel@tonic-gate 108*3886Sahl return (zError(err)); 1090Sstevel@tonic-gate } 110