Annotation of src/usr.bin/cvs/compress.c, Revision 1.1
1.1 ! xsa 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 "includes.h"
! 28:
! 29: #include "log.h"
! 30: #include "cvs.h"
! 31: #include "compress.h"
! 32:
! 33: #define CVS_ZLIB_BUFSIZE 1024
! 34:
! 35: struct cvs_zlib_ctx {
! 36: int z_level;
! 37: z_stream z_instrm;
! 38: z_stream z_destrm;
! 39: };
! 40:
! 41:
! 42: /*
! 43: * cvs_zlib_newctx()
! 44: *
! 45: * Allocate a new ZLIB context structure used for both inflation and deflation
! 46: * of data with compression level <level>, which must be between 0 and 9. A
! 47: * value of 0 means no compression, and 9 is the highest level of compression.
! 48: */
! 49: CVSZCTX *
! 50: cvs_zlib_newctx(int level)
! 51: {
! 52: CVSZCTX *ctx;
! 53:
! 54: if ((level < 0) || (level > 9))
! 55: fatal("invalid compression level %d (must be between 0 and 9)",
! 56: level);
! 57:
! 58: ctx = (CVSZCTX *)xmalloc(sizeof(*ctx));
! 59: memset(ctx, 0, sizeof(*ctx));
! 60:
! 61: ctx->z_level = level;
! 62:
! 63: ctx->z_instrm.zalloc = Z_NULL;
! 64: ctx->z_instrm.zfree = Z_NULL;
! 65: ctx->z_instrm.opaque = Z_NULL;
! 66: ctx->z_destrm.zalloc = Z_NULL;
! 67: ctx->z_destrm.zfree = Z_NULL;
! 68: ctx->z_destrm.opaque = Z_NULL;
! 69:
! 70: if ((inflateInit(&(ctx->z_instrm)) != Z_OK) ||
! 71: (deflateInit(&(ctx->z_destrm), level) != Z_OK))
! 72: fatal("failed to initialize zlib streams");
! 73:
! 74: return (ctx);
! 75: }
! 76:
! 77:
! 78: /*
! 79: * cvs_zlib_free()
! 80: *
! 81: * Free a ZLIB context previously allocated with cvs_zlib_newctx().
! 82: */
! 83: void
! 84: cvs_zlib_free(CVSZCTX *ctx)
! 85: {
! 86: if (ctx != NULL) {
! 87: (void)inflateEnd(&(ctx->z_instrm));
! 88: (void)deflateEnd(&(ctx->z_destrm));
! 89: xfree(ctx);
! 90: }
! 91: }
! 92:
! 93: /*
! 94: * cvs_zlib_inflate()
! 95: *
! 96: * Decompress the first <slen> bytes of <src> using the zlib context <ctx> and
! 97: * store the resulting data in <dst>.
! 98: * Returns the number of bytes inflated on success, or -1 on failure.
! 99: */
! 100: int
! 101: cvs_zlib_inflate(CVSZCTX *ctx, BUF *dst, u_char *src, size_t slen)
! 102: {
! 103: int bytes, ret;
! 104: u_char buf[CVS_ZLIB_BUFSIZE];
! 105:
! 106: bytes = 0;
! 107: cvs_buf_empty(dst);
! 108: inflateReset(&(ctx->z_instrm));
! 109:
! 110: ctx->z_instrm.next_in = src;
! 111: ctx->z_instrm.avail_in = slen;
! 112:
! 113: do {
! 114: ctx->z_instrm.next_out = buf;
! 115: ctx->z_instrm.avail_out = sizeof(buf);
! 116:
! 117: ret = inflate(&(ctx->z_instrm), Z_FINISH);
! 118: if ((ret == Z_MEM_ERROR) || (ret == Z_BUF_ERROR) ||
! 119: (ret == Z_STREAM_ERROR) || (ret == Z_DATA_ERROR))
! 120: fatal("inflate error: %s", ctx->z_instrm.msg);
! 121:
! 122: cvs_buf_append(dst, buf, ctx->z_instrm.avail_out);
! 123: bytes += sizeof(buf) - ctx->z_instrm.avail_out;
! 124:
! 125: } while (ret != Z_STREAM_END);
! 126:
! 127: return (bytes);
! 128: }
! 129:
! 130: /*
! 131: * cvs_zlib_deflate()
! 132: *
! 133: * Compress the first <slen> bytes of <src> using the zlib context <ctx> and
! 134: * store the resulting data in <dst>.
! 135: * Returns the number of bytes deflated on success, or -1 on failure.
! 136: */
! 137: int
! 138: cvs_zlib_deflate(CVSZCTX *ctx, BUF *dst, u_char *src, size_t slen)
! 139: {
! 140: int bytes, ret;
! 141: u_char buf[CVS_ZLIB_BUFSIZE];
! 142:
! 143: bytes = 0;
! 144: cvs_buf_empty(dst);
! 145: deflateReset(&(ctx->z_destrm));
! 146:
! 147: ctx->z_destrm.next_in = src;
! 148: ctx->z_destrm.avail_in = slen;
! 149:
! 150: do {
! 151: ctx->z_destrm.next_out = buf;
! 152: ctx->z_destrm.avail_out = sizeof(buf);
! 153: ret = deflate(&(ctx->z_destrm), Z_FINISH);
! 154: if ((ret == Z_STREAM_ERROR) || (ret == Z_BUF_ERROR))
! 155: fatal("deflate error: %s", ctx->z_destrm.msg);
! 156:
! 157: if (cvs_buf_append(dst, buf,
! 158: sizeof(buf) - ctx->z_destrm.avail_out) < 0)
! 159: return (-1);
! 160: bytes += sizeof(buf) - ctx->z_destrm.avail_out;
! 161: } while (ret != Z_STREAM_END);
! 162:
! 163: return (bytes);
! 164: }