jim.c: Fix Object leak in zlib support
[jimtcl.git] / jim-zlib.c
blobcbbc291d0bf7f30a8b6f5bbb5c2504f9bc63bce1
1 /*
2 * Jim - zlib bindings
4 * Copyright 2015, 2016 Dima Krasner <dima@dimakrasner.com>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following
14 * disclaimer in the documentation and/or other materials
15 * provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE JIM TCL PROJECT ``AS IS'' AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
20 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * JIM TCL PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 * The views and conclusions contained in the software and documentation
31 * are those of the authors and should not be interpreted as representing
32 * official policies, either expressed or implied, of the Jim Tcl Project.
35 #include <zlib.h>
37 #include <jim.h>
38 #include <jim-subcmd.h>
40 #define WBITS_GZIP (MAX_WBITS | 16)
41 /* use small 64K chunks if no size was specified during decompression, to reduce memory consumption */
42 #define DEF_DECOMPRESS_BUFSIZ (64 * 1024)
44 static int JimZlibCheckBufSize(Jim_Interp *interp, jim_wide bufsiz)
46 if ((bufsiz <= 0) || (bufsiz > INT_MAX)) {
47 Jim_SetResultFormatted(interp, "buffer size must be 0 to %#s", Jim_NewIntObj(interp, INT_MAX));
48 return JIM_ERR;
50 return JIM_OK;
53 static int Jim_Crc32(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
55 long init;
56 const char *in;
57 int len;
59 if (argc == 1) {
60 init = crc32(0L, Z_NULL, 0);
61 } else {
62 if (Jim_GetLong(interp, argv[1], &init) != JIM_OK) {
63 return JIM_ERR;
67 in = Jim_GetString(argv[0], &len);
68 Jim_SetResultInt(interp, crc32((uLong)init, (const Bytef *)in, (uInt)len) & 0xFFFFFFFF);
70 return JIM_OK;
73 static int Jim_Compress(Jim_Interp *interp, const char *in, int len, long level, int wbits)
75 z_stream strm = {0};
76 Bytef *buf;
78 if ((level != Z_DEFAULT_COMPRESSION) && ((level < Z_NO_COMPRESSION) || (level > Z_BEST_COMPRESSION))) {
79 Jim_SetResultString(interp, "level must be 0 to 9", -1);
80 return JIM_ERR;
83 if (deflateInit2(&strm, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) {
84 return JIM_ERR;
87 strm.avail_out = deflateBound(&strm, (uLong)len);
88 if (strm.avail_out > INT_MAX) {
89 deflateEnd(&strm);
90 return JIM_ERR;
92 buf = (Bytef *)Jim_Alloc((int)strm.avail_out);
93 strm.next_out = buf;
94 strm.next_in = (Bytef *)in;
95 strm.avail_in = (uInt)len;
97 /* always compress in one pass - the return value holds the entire
98 * decompressed data anyway, so there's no reason to do chunked
99 * decompression */
100 if (deflate(&strm, Z_FINISH) != Z_STREAM_END) {
101 Jim_Free(strm.next_out);
102 deflateEnd(&strm);
103 return JIM_ERR;
106 deflateEnd(&strm);
108 if (strm.total_out > INT_MAX) {
109 Jim_Free(strm.next_out);
110 return JIM_ERR;
113 Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, (char *)buf, (int)strm.total_out));
114 return JIM_OK;
117 static int Jim_Deflate(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
119 long level = Z_DEFAULT_COMPRESSION;
120 const char *in;
121 int len;
123 if (argc != 1) {
124 if (Jim_GetLong(interp, argv[1], &level) != JIM_OK) {
125 return JIM_ERR;
129 in = Jim_GetString(argv[0], &len);
130 return Jim_Compress(interp, in, len, level, -MAX_WBITS);
133 static int Jim_Gzip(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
135 long level = Z_DEFAULT_COMPRESSION;
136 const char *in;
137 int len;
139 if (argc == 3) {
140 if (!Jim_CompareStringImmediate(interp, argv[1], "-level")) {
141 return -1;
144 if (Jim_GetLong(interp, argv[2], &level) != JIM_OK) {
145 return -1;
149 else if (argc != 1) {
150 return -1;
153 in = Jim_GetString(argv[0], &len);
154 return Jim_Compress(interp, in, len, level, WBITS_GZIP);
157 static int Jim_Decompress(Jim_Interp *interp, const char *in, int len, long bufsiz, int wbits)
159 z_stream strm = {0};
160 void *buf;
161 Jim_Obj *out;
162 int ret;
164 if (JimZlibCheckBufSize(interp, bufsiz)) {
165 return JIM_ERR;
168 if (inflateInit2(&strm, wbits) != Z_OK) {
169 return JIM_ERR;
172 /* allocate a buffer - decompression is done in chunks, into this buffer;
173 * when the decompressed data size is given, decompression is faster because
174 * it's done in one pass, with less memcpy() overhead */
175 buf = Jim_Alloc((int)bufsiz);
177 out = Jim_NewEmptyStringObj(interp);
178 Jim_IncrRefCount(out);
180 strm.next_in = (Bytef*)in;
181 strm.avail_in = (uInt)len;
182 do {
183 do {
184 strm.next_out = buf;
185 strm.avail_out = (uInt)bufsiz;
187 ret = inflate(&strm, Z_NO_FLUSH);
188 switch (ret) {
189 case Z_OK:
190 case Z_STREAM_END:
191 /* append each chunk to the output object */
192 Jim_AppendString(interp, out, buf, (int)(bufsiz - (long)strm.avail_out));
193 break;
195 default:
196 Jim_DecrRefCount(interp, out);
197 Jim_Free(buf);
198 inflateEnd(&strm);
199 if (strm.msg != NULL)
200 Jim_SetResultString(interp, strm.msg, -1);
201 return JIM_ERR;
203 } while (strm.avail_out == 0);
204 } while (ret != Z_STREAM_END);
206 /* free memory used for decompression before we assign the return value */
207 Jim_Free(buf);
208 inflateEnd(&strm);
210 Jim_SetResult(interp, out);
211 Jim_DecrRefCount(interp, out);
213 return JIM_OK;
216 static int Jim_Inflate(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
218 long bufsiz = DEF_DECOMPRESS_BUFSIZ;
219 const char *in;
220 int len;
222 if (argc != 1) {
223 if (Jim_GetLong(interp, argv[1], &bufsiz) != JIM_OK) {
224 return JIM_ERR;
227 if (JimZlibCheckBufSize(interp, bufsiz)) {
228 return JIM_ERR;
232 in = Jim_GetString(argv[0], &len);
233 return Jim_Decompress(interp, in, len, bufsiz, -MAX_WBITS);
236 static int Jim_Gunzip(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
238 long bufsiz = DEF_DECOMPRESS_BUFSIZ;
239 const char *in;
240 int len;
242 if (argc == 3) {
243 if (!Jim_CompareStringImmediate(interp, argv[1], "-buffersize")) {
244 return -1;
247 if (Jim_GetLong(interp, argv[2], &bufsiz) != JIM_OK) {
248 return -1;
252 else if (argc != 1) {
253 return -1;
256 in = Jim_GetString(argv[0], &len);
257 return Jim_Decompress(interp, in, len, bufsiz, WBITS_GZIP);
260 static const jim_subcmd_type zlib_command_table[] = {
261 { "crc32",
262 "data ?startValue?",
263 Jim_Crc32,
266 /* Description: Calculates the CRC32 checksum of a string */
268 { "deflate",
269 "string ?level?",
270 Jim_Deflate,
273 /* Description: Compresses a string and outputs a raw, zlib-compressed stream */
275 { "gzip",
276 "data ?-level level?",
277 Jim_Gzip,
280 /* Description: Compresses a string and outputs a gzip-compressed stream */
282 { "inflate",
283 "data ?bufferSize?",
284 Jim_Inflate,
287 /* Description: Decompresses a raw, zlib-compressed stream */
289 { "gunzip",
290 "data ?-buffersize size?",
291 Jim_Gunzip,
294 /* Description: Decompresses a gzip-compressed stream */
296 { NULL }
299 static int JimZlibCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
301 return Jim_CallSubCmd(interp, Jim_ParseSubCmd(interp, zlib_command_table, argc, argv), argc, argv);
304 int Jim_zlibInit(Jim_Interp *interp)
306 if (Jim_PackageProvide(interp, "zlib", "1.0", JIM_ERRMSG)) {
307 return JIM_ERR;
310 Jim_CreateCommand(interp, "zlib", JimZlibCmd, 0, 0);
312 return JIM_OK;