[BACK]Return to zlib.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / cvs

Annotation of src/usr.bin/cvs/zlib.c, Revision 1.5

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