jim.c: Allow Jim_WrongNumArgs() to accept NULL for msg
[jimtcl.git] / jim-zlib.c
blobbb9191330dd3f7806c18d70d18e3e6686fd19767
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);
89 /* Some compression methods may need a little more space */
90 strm.avail_out += 100;
92 if (strm.avail_out > INT_MAX) {
93 deflateEnd(&strm);
94 return JIM_ERR;
96 buf = (Bytef *)Jim_Alloc((int)strm.avail_out);
97 strm.next_out = buf;
98 strm.next_in = (Bytef *)in;
99 strm.avail_in = (uInt)len;
101 /* always compress in one pass - the return value holds the entire
102 * decompressed data anyway, so there's no reason to do chunked
103 * decompression */
104 if (deflate(&strm, Z_FINISH) != Z_STREAM_END) {
105 Jim_Free(buf);
106 deflateEnd(&strm);
107 Jim_SetResultString(interp, "not enough output space", -1);
108 return JIM_ERR;
111 deflateEnd(&strm);
113 if (strm.total_out > INT_MAX) {
114 Jim_Free(buf);
115 Jim_SetResultString(interp, "too much output", -1);
116 return JIM_ERR;
119 Jim_SetResult(interp, Jim_NewStringObjNoAlloc(interp, (char *)buf, (int)strm.total_out));
120 return JIM_OK;
123 static int Jim_Deflate(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
125 long level = Z_DEFAULT_COMPRESSION;
126 const char *in;
127 int len;
129 if (argc != 1) {
130 if (Jim_GetLong(interp, argv[1], &level) != JIM_OK) {
131 return JIM_ERR;
135 in = Jim_GetString(argv[0], &len);
136 return Jim_Compress(interp, in, len, level, -MAX_WBITS);
139 static int Jim_Gzip(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
141 long level = Z_DEFAULT_COMPRESSION;
142 const char *in;
143 int len;
145 if (argc == 3) {
146 if (!Jim_CompareStringImmediate(interp, argv[1], "-level")) {
147 return -1;
150 if (Jim_GetLong(interp, argv[2], &level) != JIM_OK) {
151 return -1;
155 else if (argc != 1) {
156 return -1;
159 in = Jim_GetString(argv[0], &len);
160 return Jim_Compress(interp, in, len, level, WBITS_GZIP);
163 static int Jim_Decompress(Jim_Interp *interp, const char *in, int len, long bufsiz, int wbits)
165 z_stream strm = {0};
166 void *buf;
167 Jim_Obj *out;
168 int ret;
170 if (JimZlibCheckBufSize(interp, bufsiz)) {
171 return JIM_ERR;
174 if (inflateInit2(&strm, wbits) != Z_OK) {
175 return JIM_ERR;
178 /* allocate a buffer - decompression is done in chunks, into this buffer;
179 * when the decompressed data size is given, decompression is faster because
180 * it's done in one pass, with less memcpy() overhead */
181 buf = Jim_Alloc((int)bufsiz);
183 out = Jim_NewEmptyStringObj(interp);
184 Jim_IncrRefCount(out);
186 strm.next_in = (Bytef*)in;
187 strm.avail_in = (uInt)len;
188 do {
189 do {
190 strm.next_out = buf;
191 strm.avail_out = (uInt)bufsiz;
193 ret = inflate(&strm, Z_NO_FLUSH);
194 switch (ret) {
195 case Z_OK:
196 case Z_STREAM_END:
197 /* append each chunk to the output object */
198 Jim_AppendString(interp, out, buf, (int)(bufsiz - (long)strm.avail_out));
199 break;
201 default:
202 Jim_DecrRefCount(interp, out);
203 Jim_Free(buf);
204 inflateEnd(&strm);
205 if (strm.msg != NULL)
206 Jim_SetResultString(interp, strm.msg, -1);
207 return JIM_ERR;
209 } while (strm.avail_out == 0);
210 } while (ret != Z_STREAM_END);
212 /* free memory used for decompression before we assign the return value */
213 Jim_Free(buf);
214 inflateEnd(&strm);
216 Jim_SetResult(interp, out);
217 Jim_DecrRefCount(interp, out);
219 return JIM_OK;
222 static int Jim_Inflate(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
224 long bufsiz = DEF_DECOMPRESS_BUFSIZ;
225 const char *in;
226 int len;
228 if (argc != 1) {
229 if (Jim_GetLong(interp, argv[1], &bufsiz) != JIM_OK) {
230 return JIM_ERR;
233 if (JimZlibCheckBufSize(interp, bufsiz)) {
234 return JIM_ERR;
238 in = Jim_GetString(argv[0], &len);
239 return Jim_Decompress(interp, in, len, bufsiz, -MAX_WBITS);
242 static int Jim_Gunzip(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
244 long bufsiz = DEF_DECOMPRESS_BUFSIZ;
245 const char *in;
246 int len;
248 if (argc == 3) {
249 if (!Jim_CompareStringImmediate(interp, argv[1], "-buffersize")) {
250 return -1;
253 if (Jim_GetLong(interp, argv[2], &bufsiz) != JIM_OK) {
254 return -1;
258 else if (argc != 1) {
259 return -1;
262 in = Jim_GetString(argv[0], &len);
263 return Jim_Decompress(interp, in, len, bufsiz, WBITS_GZIP);
266 static const jim_subcmd_type zlib_command_table[] = {
267 { "crc32",
268 "data ?startValue?",
269 Jim_Crc32,
272 /* Description: Calculates the CRC32 checksum of a string */
274 { "deflate",
275 "string ?level?",
276 Jim_Deflate,
279 /* Description: Compresses a string and outputs a raw, zlib-compressed stream */
281 { "gzip",
282 "data ?-level level?",
283 Jim_Gzip,
286 /* Description: Compresses a string and outputs a gzip-compressed stream */
288 { "inflate",
289 "data ?bufferSize?",
290 Jim_Inflate,
293 /* Description: Decompresses a raw, zlib-compressed stream */
295 { "gunzip",
296 "data ?-buffersize size?",
297 Jim_Gunzip,
300 /* Description: Decompresses a gzip-compressed stream */
302 { NULL }
305 static int JimZlibCmd(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
307 return Jim_CallSubCmd(interp, Jim_ParseSubCmd(interp, zlib_command_table, argc, argv), argc, argv);
310 int Jim_zlibInit(Jim_Interp *interp)
312 if (Jim_PackageProvide(interp, "zlib", "1.0", JIM_ERRMSG)) {
313 return JIM_ERR;
316 Jim_CreateCommand(interp, "zlib", JimZlibCmd, 0, 0);
318 return JIM_OK;