17c478bd9Sstevel@tonic-gate /* 2a0b85df4Sstevel * CDDL HEADER START 3a0b85df4Sstevel * 4a0b85df4Sstevel * The contents of this file are subject to the terms of the 5ae115bc7Smrj * Common Development and Distribution License (the "License"). 6ae115bc7Smrj * You may not use this file except in compliance with the License. 7a0b85df4Sstevel * 8a0b85df4Sstevel * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9a0b85df4Sstevel * or http://www.opensolaris.org/os/licensing. 10a0b85df4Sstevel * See the License for the specific language governing permissions 11a0b85df4Sstevel * and limitations under the License. 12a0b85df4Sstevel * 13a0b85df4Sstevel * When distributing Covered Code, include this CDDL HEADER in each 14a0b85df4Sstevel * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15a0b85df4Sstevel * If applicable, add the following below this CDDL HEADER, with the 16a0b85df4Sstevel * fields enclosed by brackets "[]" replaced with your own identifying 17a0b85df4Sstevel * information: Portions Copyright [yyyy] [name of copyright owner] 18a0b85df4Sstevel * 19a0b85df4Sstevel * CDDL HEADER END 20a0b85df4Sstevel */ 21a0b85df4Sstevel 22a0b85df4Sstevel /* 23ae115bc7Smrj * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 307c478bd9Sstevel@tonic-gate #include <sys/zmod.h> 317c478bd9Sstevel@tonic-gate 327c478bd9Sstevel@tonic-gate #include "zlib.h" 337c478bd9Sstevel@tonic-gate 347c478bd9Sstevel@tonic-gate /* 357c478bd9Sstevel@tonic-gate * Uncompress the buffer 'src' into the buffer 'dst'. The caller must store 367c478bd9Sstevel@tonic-gate * the expected decompressed data size externally so it can be passed in. 377c478bd9Sstevel@tonic-gate * The resulting decompressed size is then returned through dstlen. This 387c478bd9Sstevel@tonic-gate * function return Z_OK on success, or another error code on failure. 397c478bd9Sstevel@tonic-gate */ 407c478bd9Sstevel@tonic-gate int 417c478bd9Sstevel@tonic-gate z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen) 427c478bd9Sstevel@tonic-gate { 437c478bd9Sstevel@tonic-gate z_stream zs; 447c478bd9Sstevel@tonic-gate int err; 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate bzero(&zs, sizeof (zs)); 477c478bd9Sstevel@tonic-gate zs.next_in = (uchar_t *)src; 487c478bd9Sstevel@tonic-gate zs.avail_in = srclen; 497c478bd9Sstevel@tonic-gate zs.next_out = dst; 507c478bd9Sstevel@tonic-gate zs.avail_out = *dstlen; 517c478bd9Sstevel@tonic-gate 527c478bd9Sstevel@tonic-gate if ((err = inflateInit(&zs)) != Z_OK) 537c478bd9Sstevel@tonic-gate return (err); 547c478bd9Sstevel@tonic-gate 557c478bd9Sstevel@tonic-gate if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) { 567c478bd9Sstevel@tonic-gate (void) inflateEnd(&zs); 577c478bd9Sstevel@tonic-gate return (err == Z_OK ? Z_BUF_ERROR : err); 587c478bd9Sstevel@tonic-gate } 597c478bd9Sstevel@tonic-gate 607c478bd9Sstevel@tonic-gate *dstlen = zs.total_out; 617c478bd9Sstevel@tonic-gate return (inflateEnd(&zs)); 627c478bd9Sstevel@tonic-gate } 637c478bd9Sstevel@tonic-gate 64*c9431fa1Sahl int 65*c9431fa1Sahl z_compress_level(void *dst, size_t *dstlen, const void *src, size_t srclen, 66*c9431fa1Sahl int level) 67*c9431fa1Sahl { 68*c9431fa1Sahl 69*c9431fa1Sahl z_stream zs; 70*c9431fa1Sahl int err; 71*c9431fa1Sahl 72*c9431fa1Sahl bzero(&zs, sizeof (zs)); 73*c9431fa1Sahl zs.next_in = (uchar_t *)src; 74*c9431fa1Sahl zs.avail_in = srclen; 75*c9431fa1Sahl zs.next_out = dst; 76*c9431fa1Sahl zs.avail_out = *dstlen; 77*c9431fa1Sahl 78*c9431fa1Sahl if ((err = deflateInit(&zs, level)) != Z_OK) 79*c9431fa1Sahl return (err); 80*c9431fa1Sahl 81*c9431fa1Sahl if ((err = deflate(&zs, Z_FINISH)) != Z_STREAM_END) { 82*c9431fa1Sahl (void) deflateEnd(&zs); 83*c9431fa1Sahl return (err == Z_OK ? Z_BUF_ERROR : err); 84*c9431fa1Sahl } 85*c9431fa1Sahl 86*c9431fa1Sahl *dstlen = zs.total_out; 87*c9431fa1Sahl return (deflateEnd(&zs)); 88*c9431fa1Sahl } 89*c9431fa1Sahl 90*c9431fa1Sahl int 91*c9431fa1Sahl z_compress(void *dst, size_t *dstlen, const void *src, size_t srclen) 92*c9431fa1Sahl { 93*c9431fa1Sahl return (z_compress_level(dst, dstlen, src, srclen, 94*c9431fa1Sahl Z_DEFAULT_COMPRESSION)); 95*c9431fa1Sahl } 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate /* 987c478bd9Sstevel@tonic-gate * Convert a zlib error code into a string error message. 997c478bd9Sstevel@tonic-gate */ 1007c478bd9Sstevel@tonic-gate const char * 1017c478bd9Sstevel@tonic-gate z_strerror(int err) 1027c478bd9Sstevel@tonic-gate { 1037c478bd9Sstevel@tonic-gate int i = Z_NEED_DICT - err; 1047c478bd9Sstevel@tonic-gate 105*c9431fa1Sahl if (i < 0 || i > Z_NEED_DICT - Z_VERSION_ERROR) 1067c478bd9Sstevel@tonic-gate return ("unknown error"); 1077c478bd9Sstevel@tonic-gate 108*c9431fa1Sahl return (zError(err)); 1097c478bd9Sstevel@tonic-gate } 110