Annotation of src/usr.bin/cvs/zlib.c, Revision 1.1
1.1 ! jfb 1: /* $OpenBSD$ */
! 2: /*
! 3: * Copyright (c) 2005 Jean-Francois Brousseau <jfb@openbsd.org>
! 4: * All rights reserved.
! 5: *
! 6: * Redistribution and use in source and binary forms, with or without
! 7: * modification, are permitted provided that the following conditions
! 8: * are met:
! 9: *
! 10: * 1. Redistributions of source code must retain the above copyright
! 11: * notice, this list of conditions and the following disclaimer.
! 12: * 2. The name of the author may not be used to endorse or promote products
! 13: * derived from this software without specific prior written permission.
! 14: *
! 15: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
! 16: * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
! 17: * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
! 18: * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
! 19: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
! 20: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
! 21: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
! 22: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
! 23: * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
! 24: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
! 25: */
! 26:
! 27: #include <sys/param.h>
! 28:
! 29: #include <errno.h>
! 30: #include <stdio.h>
! 31: #include <stdlib.h>
! 32: #include <unistd.h>
! 33: #include <string.h>
! 34:
! 35: #include "log.h"
! 36: #include "cvs.h"
! 37: #include "zlib.h"
! 38:
! 39:
! 40: #define CVS_ZLIB_BUFSIZE 1024
! 41:
! 42:
! 43:
! 44: struct cvs_zlib_ctx {
! 45: int z_level;
! 46: z_stream z_instrm;
! 47: z_stream z_destrm;
! 48: };
! 49:
! 50:
! 51: /*
! 52: * cvs_zlib_newctx()
! 53: *
! 54: * Allocate a new ZLIB context structure used for both inflation and deflation
! 55: * of data with compression level <level>, which must be between 0 and 9. A
! 56: * value of 0 means no compression, and 9 is the highest level of compression.
! 57: */
! 58: CVSZCTX*
! 59: cvs_zlib_newctx(int level)
! 60: {
! 61: CVSZCTX *ctx;
! 62:
! 63: if ((level < 0) || (level > 9)) {
! 64: cvs_log(LP_ERR, "invalid compression level %d "
! 65: "(must be between 0 and 9)", level);
! 66: return (NULL);
! 67: }
! 68:
! 69: ctx = (CVSZCTX *)malloc(sizeof(*ctx));
! 70: if (ctx == NULL) {
! 71: cvs_log(LP_ERRNO, "failed to allocate zlib context");
! 72: return (NULL);
! 73: }
! 74: memset(ctx, 0, sizeof(*ctx));
! 75:
! 76: ctx->z_level = level;
! 77:
! 78: ctx->z_instrm.zalloc = Z_NULL;
! 79: ctx->z_instrm.zfree = Z_NULL;
! 80: ctx->z_instrm.opaque = Z_NULL;
! 81: ctx->z_destrm.zalloc = Z_NULL;
! 82: ctx->z_destrm.zfree = Z_NULL;
! 83: ctx->z_destrm.opaque = Z_NULL;
! 84:
! 85: if ((inflateInit(&(ctx->z_instrm)) != Z_OK) ||
! 86: (deflateInit(&(ctx->z_destrm), level) != Z_OK)) {
! 87: cvs_log(LP_ERR, "failed to initialize zlib streams");
! 88: free(ctx);
! 89: return (NULL);
! 90: }
! 91:
! 92: return (ctx);
! 93: }
! 94:
! 95:
! 96: /*
! 97: * cvs_zlib_free()
! 98: *
! 99: * Free a ZLIB context previously allocated with cvs_zlib_newctx().
! 100: */
! 101: void
! 102: cvs_zlib_free(CVSZCTX *ctx)
! 103: {
! 104: if (ctx != NULL) {
! 105: (void)inflateEnd(&(ctx->z_instrm));
! 106: (void)deflateEnd(&(ctx->z_destrm));
! 107: free(ctx);
! 108: }
! 109: }
! 110:
! 111: /*
! 112: * cvs_zlib_inflate()
! 113: *
! 114: */
! 115: int
! 116: cvs_zlib_inflate(CVSZCTX *ctx, BUF *dst, u_char *src, size_t slen)
! 117: {
! 118: int bytes, ret;
! 119: u_char buf[CVS_ZLIB_BUFSIZE];
! 120:
! 121: bytes = 0;
! 122: cvs_buf_empty(dst);
! 123: inflateReset(&(ctx->z_instrm));
! 124:
! 125: ctx->z_instrm.next_in = src;
! 126: ctx->z_instrm.avail_in = slen;
! 127:
! 128: do {
! 129: ctx->z_instrm.next_out = buf;
! 130: ctx->z_instrm.avail_out = sizeof(buf);
! 131:
! 132: ret = inflate(&(ctx->z_instrm), Z_FINISH);
! 133: if ((ret == Z_MEM_ERROR) || (ret == Z_BUF_ERROR) ||
! 134: (ret == Z_STREAM_ERROR) || (ret == Z_DATA_ERROR)) {
! 135: cvs_log(LP_ERR, "inflate error: %s", ctx->z_instrm.msg);
! 136: return (-1);
! 137: }
! 138:
! 139: cvs_buf_append(dst, buf, ctx->z_instrm.avail_out);
! 140: bytes += sizeof(buf) - ctx->z_instrm.avail_out;
! 141:
! 142: } while (ret != Z_STREAM_END);
! 143:
! 144: cvs_log(LP_WARN, "%u bytes decompressed to %d bytes", slen, bytes);
! 145:
! 146: return (bytes);
! 147: }
! 148:
! 149:
! 150: /*
! 151: * cvs_zlib_deflate()
! 152: *
! 153: * Compress the first <slen> bytes of <src> using the zlib context <ctx> and
! 154: * store the resulting data in <dst>.
! 155: * Returns the number of bytes deflated on success, or -1 on failure.
! 156: */
! 157: int
! 158: cvs_zlib_deflate(CVSZCTX *ctx, BUF *dst, u_char *src, size_t slen)
! 159: {
! 160: int bytes, ret;
! 161: u_char buf[CVS_ZLIB_BUFSIZE];
! 162:
! 163: bytes = 0;
! 164: cvs_buf_empty(dst);
! 165: deflateReset(&(ctx->z_destrm));
! 166:
! 167: ctx->z_destrm.next_in = src;
! 168: ctx->z_destrm.avail_in = slen;
! 169:
! 170: do {
! 171: ctx->z_destrm.next_out = buf;
! 172: ctx->z_destrm.avail_out = sizeof(buf);
! 173: ret = deflate(&(ctx->z_destrm), Z_FINISH);
! 174: if ((ret == Z_STREAM_ERROR) || (ret == Z_BUF_ERROR)) {
! 175: #if 0
! 176: if (ret != Z_OK) {
! 177: #endif
! 178: cvs_log(LP_ERR, "deflate error: %s", ctx->z_destrm.msg);
! 179: return (-1);
! 180: }
! 181:
! 182: if (cvs_buf_append(dst, buf,
! 183: sizeof(buf) - ctx->z_destrm.avail_out) < 0)
! 184: return (-1);
! 185: bytes += sizeof(buf) - ctx->z_destrm.avail_out;
! 186: } while (ret != Z_STREAM_END);
! 187:
! 188: cvs_log(LP_WARN, "%u bytes compressed to %d bytes", slen, bytes);
! 189:
! 190: return (bytes);
! 191: }