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 /* 231d7f3fadSKrishnendu Sadhukhan - Sun Microsystems * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #include <sys/modctl.h> 287c478bd9Sstevel@tonic-gate #include <sys/zmod.h> 297c478bd9Sstevel@tonic-gate 30*17af85d9SToomas Soome #include <zlib.h> 31*17af85d9SToomas Soome #include <zutil.h> 327c478bd9Sstevel@tonic-gate 337c478bd9Sstevel@tonic-gate /* 347c478bd9Sstevel@tonic-gate * Uncompress the buffer 'src' into the buffer 'dst'. The caller must store 357c478bd9Sstevel@tonic-gate * the expected decompressed data size externally so it can be passed in. 367c478bd9Sstevel@tonic-gate * The resulting decompressed size is then returned through dstlen. This 377c478bd9Sstevel@tonic-gate * function return Z_OK on success, or another error code on failure. 387c478bd9Sstevel@tonic-gate */ 397c478bd9Sstevel@tonic-gate int 407c478bd9Sstevel@tonic-gate z_uncompress(void *dst, size_t *dstlen, const void *src, size_t srclen) 417c478bd9Sstevel@tonic-gate { 427c478bd9Sstevel@tonic-gate z_stream zs; 437c478bd9Sstevel@tonic-gate int err; 447c478bd9Sstevel@tonic-gate 457c478bd9Sstevel@tonic-gate bzero(&zs, sizeof (zs)); 467c478bd9Sstevel@tonic-gate zs.next_in = (uchar_t *)src; 477c478bd9Sstevel@tonic-gate zs.avail_in = srclen; 487c478bd9Sstevel@tonic-gate zs.next_out = dst; 497c478bd9Sstevel@tonic-gate zs.avail_out = *dstlen; 507c478bd9Sstevel@tonic-gate 511d7f3fadSKrishnendu Sadhukhan - Sun Microsystems /* 521d7f3fadSKrishnendu Sadhukhan - Sun Microsystems * Call inflateInit2() specifying a window size of DEF_WBITS 531d7f3fadSKrishnendu Sadhukhan - Sun Microsystems * with the 6th bit set to indicate that the compression format 541d7f3fadSKrishnendu Sadhukhan - Sun Microsystems * type (zlib or gzip) should be automatically detected. 551d7f3fadSKrishnendu Sadhukhan - Sun Microsystems */ 561d7f3fadSKrishnendu Sadhukhan - Sun Microsystems if ((err = inflateInit2(&zs, DEF_WBITS | 0x20)) != Z_OK) 577c478bd9Sstevel@tonic-gate return (err); 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate if ((err = inflate(&zs, Z_FINISH)) != Z_STREAM_END) { 607c478bd9Sstevel@tonic-gate (void) inflateEnd(&zs); 617c478bd9Sstevel@tonic-gate return (err == Z_OK ? Z_BUF_ERROR : err); 627c478bd9Sstevel@tonic-gate } 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate *dstlen = zs.total_out; 657c478bd9Sstevel@tonic-gate return (inflateEnd(&zs)); 667c478bd9Sstevel@tonic-gate } 677c478bd9Sstevel@tonic-gate 68c9431fa1Sahl int 69c9431fa1Sahl z_compress_level(void *dst, size_t *dstlen, const void *src, size_t srclen, 70c9431fa1Sahl int level) 71c9431fa1Sahl { 72c9431fa1Sahl 73c9431fa1Sahl z_stream zs; 74c9431fa1Sahl int err; 75c9431fa1Sahl 76c9431fa1Sahl bzero(&zs, sizeof (zs)); 77c9431fa1Sahl zs.next_in = (uchar_t *)src; 78c9431fa1Sahl zs.avail_in = srclen; 79c9431fa1Sahl zs.next_out = dst; 80c9431fa1Sahl zs.avail_out = *dstlen; 81c9431fa1Sahl 82c9431fa1Sahl if ((err = deflateInit(&zs, level)) != Z_OK) 83c9431fa1Sahl return (err); 84c9431fa1Sahl 85c9431fa1Sahl if ((err = deflate(&zs, Z_FINISH)) != Z_STREAM_END) { 86c9431fa1Sahl (void) deflateEnd(&zs); 87c9431fa1Sahl return (err == Z_OK ? Z_BUF_ERROR : err); 88c9431fa1Sahl } 89c9431fa1Sahl 90c9431fa1Sahl *dstlen = zs.total_out; 91c9431fa1Sahl return (deflateEnd(&zs)); 92c9431fa1Sahl } 93c9431fa1Sahl 94c9431fa1Sahl int 95c9431fa1Sahl z_compress(void *dst, size_t *dstlen, const void *src, size_t srclen) 96c9431fa1Sahl { 97c9431fa1Sahl return (z_compress_level(dst, dstlen, src, srclen, 98c9431fa1Sahl Z_DEFAULT_COMPRESSION)); 99c9431fa1Sahl } 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate /* 1027c478bd9Sstevel@tonic-gate * Convert a zlib error code into a string error message. 1037c478bd9Sstevel@tonic-gate */ 1047c478bd9Sstevel@tonic-gate const char * 1057c478bd9Sstevel@tonic-gate z_strerror(int err) 1067c478bd9Sstevel@tonic-gate { 1077c478bd9Sstevel@tonic-gate int i = Z_NEED_DICT - err; 1087c478bd9Sstevel@tonic-gate 109c9431fa1Sahl if (i < 0 || i > Z_NEED_DICT - Z_VERSION_ERROR) 1107c478bd9Sstevel@tonic-gate return ("unknown error"); 1117c478bd9Sstevel@tonic-gate 112c9431fa1Sahl return (zError(err)); 1137c478bd9Sstevel@tonic-gate } 114