Annotation of src/usr.bin/cvs/zlib.c, Revision 1.2
1.2 ! jfb 1: /* $OpenBSD: zlib.c,v 1.1 2005/01/13 18:59:03 jfb 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:
36: #define CVS_ZLIB_BUFSIZE 1024
37:
38: struct cvs_zlib_ctx {
39: int z_level;
40: z_stream z_instrm;
41: z_stream z_destrm;
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: */
52: CVSZCTX*
53: cvs_zlib_newctx(int level)
54: {
55: CVSZCTX *ctx;
56:
57: if ((level < 0) || (level > 9)) {
58: cvs_log(LP_ERR, "invalid compression level %d "
59: "(must be between 0 and 9)", level);
60: return (NULL);
61: }
62:
63: ctx = (CVSZCTX *)malloc(sizeof(*ctx));
64: if (ctx == NULL) {
65: cvs_log(LP_ERRNO, "failed to allocate zlib context");
66: return (NULL);
67: }
68: memset(ctx, 0, sizeof(*ctx));
69:
70: ctx->z_level = level;
71:
72: ctx->z_instrm.zalloc = Z_NULL;
73: ctx->z_instrm.zfree = Z_NULL;
74: ctx->z_instrm.opaque = Z_NULL;
75: ctx->z_destrm.zalloc = Z_NULL;
76: ctx->z_destrm.zfree = Z_NULL;
77: ctx->z_destrm.opaque = Z_NULL;
78:
79: if ((inflateInit(&(ctx->z_instrm)) != Z_OK) ||
80: (deflateInit(&(ctx->z_destrm), level) != Z_OK)) {
81: cvs_log(LP_ERR, "failed to initialize zlib streams");
82: free(ctx);
83: return (NULL);
84: }
85:
86: return (ctx);
87: }
88:
89:
90: /*
91: * cvs_zlib_free()
92: *
93: * Free a ZLIB context previously allocated with cvs_zlib_newctx().
94: */
95: void
96: cvs_zlib_free(CVSZCTX *ctx)
97: {
98: if (ctx != NULL) {
99: (void)inflateEnd(&(ctx->z_instrm));
100: (void)deflateEnd(&(ctx->z_destrm));
101: free(ctx);
102: }
103: }
104:
105: /*
106: * cvs_zlib_inflate()
107: *
1.2 ! jfb 108: * Decompress the first <slen> bytes of <src> using the zlib context <ctx> and
! 109: * store the resulting data in <dst>.
! 110: * Returns the number of bytes inflated on success, or -1 on failure.
1.1 jfb 111: */
112: int
113: cvs_zlib_inflate(CVSZCTX *ctx, BUF *dst, u_char *src, size_t slen)
114: {
115: int bytes, ret;
116: u_char buf[CVS_ZLIB_BUFSIZE];
117:
118: bytes = 0;
119: cvs_buf_empty(dst);
120: inflateReset(&(ctx->z_instrm));
121:
122: ctx->z_instrm.next_in = src;
123: ctx->z_instrm.avail_in = slen;
124:
125: do {
126: ctx->z_instrm.next_out = buf;
127: ctx->z_instrm.avail_out = sizeof(buf);
128:
129: ret = inflate(&(ctx->z_instrm), Z_FINISH);
130: if ((ret == Z_MEM_ERROR) || (ret == Z_BUF_ERROR) ||
131: (ret == Z_STREAM_ERROR) || (ret == Z_DATA_ERROR)) {
132: cvs_log(LP_ERR, "inflate error: %s", ctx->z_instrm.msg);
133: return (-1);
134: }
135:
136: cvs_buf_append(dst, buf, ctx->z_instrm.avail_out);
137: bytes += sizeof(buf) - ctx->z_instrm.avail_out;
138:
139: } while (ret != Z_STREAM_END);
140:
141: return (bytes);
142: }
143:
144: /*
145: * cvs_zlib_deflate()
146: *
147: * Compress the first <slen> bytes of <src> using the zlib context <ctx> and
148: * store the resulting data in <dst>.
149: * Returns the number of bytes deflated on success, or -1 on failure.
150: */
151: int
152: cvs_zlib_deflate(CVSZCTX *ctx, BUF *dst, u_char *src, size_t slen)
153: {
154: int bytes, ret;
155: u_char buf[CVS_ZLIB_BUFSIZE];
156:
157: bytes = 0;
158: cvs_buf_empty(dst);
159: deflateReset(&(ctx->z_destrm));
160:
161: ctx->z_destrm.next_in = src;
162: ctx->z_destrm.avail_in = slen;
163:
164: do {
165: ctx->z_destrm.next_out = buf;
166: ctx->z_destrm.avail_out = sizeof(buf);
167: ret = deflate(&(ctx->z_destrm), Z_FINISH);
168: if ((ret == Z_STREAM_ERROR) || (ret == Z_BUF_ERROR)) {
169: cvs_log(LP_ERR, "deflate error: %s", ctx->z_destrm.msg);
170: return (-1);
171: }
172:
173: if (cvs_buf_append(dst, buf,
174: sizeof(buf) - ctx->z_destrm.avail_out) < 0)
175: return (-1);
176: bytes += sizeof(buf) - ctx->z_destrm.avail_out;
177: } while (ret != Z_STREAM_END);
178:
179: return (bytes);
180: }