Debian Codegraph @

texk/web2c/luatexdir/luazlib/lzlib.c

raw
1 /************************************************************************
2 * Author : Tiago Dionizio ([email protected]) *
3 * Library : lzlib - Lua 5 interface to access zlib library functions *
4 * *
5 * Permission is hereby granted, free of charge, to any person obtaining *
6 * a copy of this software and associated documentation files (the *
7 * "Software"), to deal in the Software without restriction, including *
8 * without limitation the rights to use, copy, modify, merge, publish, *
9 * distribute, sublicense, and/or sell copies of the Software, and to *
10 * permit persons to whom the Software is furnished to do so, subject to *
11 * the following conditions: *
12 * *
13 * The above copyright notice and this permission notice shall be *
14 * included in all copies or substantial portions of the Software. *
15 * *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY *
20 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, *
21 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE *
22 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 ************************************************************************/
24
25 /************************************************************************
26 $Id: lzlib.c,v 1.3 2004/07/22 19:10:47 tngd Exp $
27 Changes:
28 ************************************************************************/
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "lua.h"
33 #include "lauxlib.h"
34
35 #include "zlib.h"
36
37
38 /*
39 ** =========================================================================
40 ** compile time options wich determine available functionality
41 ** =========================================================================
42 */
43
44
45 /*
46 ** =========================================================================
47 ** zlib stream metamethods
48 ** =========================================================================
49 */
50 #define ZSTREAMMETA "zlib.zstream"
51
52 #define LZANY (void*)-1
53 #define LZNONE (void*)0
54 #define LZDEFLATE (void*)1
55 #define LZINFLATE (void*)2
56
57 static z_stream *lzstream_new(lua_State *L)
58 {
59 z_stream *s = (z_stream*)lua_newuserdata(L, sizeof(z_stream));
60
61 luaL_getmetatable(L, ZSTREAMMETA);
62 lua_setmetatable(L, -2); /* set metatable */
63
64 s->zalloc = Z_NULL;
65 s->zfree = Z_NULL;
66
67 s->next_out = Z_NULL;
68 s->avail_out = 0;
69 s->next_in = Z_NULL;
70 s->avail_in = 0;
71
72 s->opaque = LZNONE;
73
74 return s;
75 }
76
77 static void lzstream_cleanup(lua_State *L, z_stream *s)
78 {
79 if (s && s->opaque != LZNONE)
80 {
81 if (s->opaque == LZINFLATE)
82 inflateEnd(s);
83 if (s->opaque == LZDEFLATE)
84 deflateEnd(s);
85 s->opaque = LZNONE;
86 }
87 }
88
89 /* ====================================================================== */
90
91 static z_stream *lzstream_get(lua_State *L, int index)
92 {
93 z_stream *s = (z_stream*)luaL_checkudata(L, index, ZSTREAMMETA);
94 if (s == NULL) luaL_argerror(L, index, "bad zlib stream");
95 return s;
96 }
97
98 static z_stream *lzstream_check(lua_State *L, int index, void *state)
99 {
100 z_stream *s = lzstream_get(L, index);
101 if ((state != LZANY && s->opaque != state) || s->opaque == LZNONE)
102 luaL_argerror(L, index, "attempt to use invalid zlib stream");
103 return s;
104 }
105
106 /* ====================================================================== */
107
108 static int lzstream_tostring(lua_State *L)
109 {
110 char buf[100];
111 z_stream *s = (z_stream*)luaL_checkudata(L, 1, ZSTREAMMETA);
112 if (s == NULL) luaL_argerror(L, 1, "bad zlib stream");
113
114 if (s->opaque == LZNONE)
115 strncpy(buf, "zlib stream (closed)", 100);
116 else if (s->opaque == LZDEFLATE)
117 snprintf(buf, 100, "zlib deflate stream (%p)", s);
118 else if (s->opaque == LZINFLATE)
119 snprintf(buf, 100, "zlib inflate stream (%p)", s);
120 else
121 snprintf(buf, 100, "%p", s);
122
123 lua_pushstring(L, buf);
124 return 1;
125 }
126
127 /* ====================================================================== */
128
129 static int lzstream_gc(lua_State *L)
130 {
131 z_stream *s = lzstream_get(L, 1);
132 lzstream_cleanup(L, s);
133 return 0;
134 }
135
136 /* ====================================================================== */
137
138 static int lzstream_close(lua_State *L)
139 {
140 z_stream *s = lzstream_check(L, 1, LZANY);
141 lzstream_cleanup(L, s);
142 return 0;
143 }
144
145 /* ====================================================================== */
146
147 static int lzstream_adler(lua_State *L)
148 {
149 z_stream *s = lzstream_check(L, 1, LZANY);
150 lua_pushnumber(L, s->adler);
151 return 1;
152 }
153
154 /* ====================================================================== */
155
156 static int lzlib_deflate(lua_State *L)
157 {
158 int level = luaL_optint(L, 1, Z_DEFAULT_COMPRESSION);
159 int method = luaL_optint(L, 2, Z_DEFLATED);
160 int windowBits = luaL_optint(L, 3, 15);
161 int memLevel = luaL_optint(L, 4, 8);
162 int strategy = luaL_optint(L, 5, Z_DEFAULT_STRATEGY);
163
164 z_stream *s = lzstream_new(L);
165
166 if (deflateInit2(s, level, method, windowBits, memLevel, strategy) != Z_OK)
167 {
168 lua_pushliteral(L, "failed to start decompressing");
169 lua_error(L);
170 }
171 s->opaque = LZDEFLATE;
172 return 1;
173 }
174
175 /* ====================================================================== */
176
177 static int lzlib_inflate(lua_State *L)
178 {
179 int windowBits = luaL_optint(L, 1, 15);
180
181 z_stream *s = lzstream_new(L);
182
183 if (inflateInit2(s, windowBits) != Z_OK)
184 {
185 lua_pushliteral(L, "failed to start compressing");
186 lua_error(L);
187 }
188
189 s->opaque = LZINFLATE;
190 return 1;
191 }
192
193 /* ====================================================================== */
194
195 static int lzstream_decompress(lua_State *L)
196 {
197 z_stream *s = lzstream_check(L, 1, LZINFLATE);
198 s->next_in = (Bytef*)luaL_checkstring(L, 2);
199 s->avail_in = lua_rawlen(L, 2);
200
201 {
202 int r;
203 luaL_Buffer b;
204 luaL_buffinit(L, &b);
205
206 do {
207 s->next_out = (Bytef*)luaL_prepbuffer(&b);
208 s->avail_out = LUAL_BUFFERSIZE;
209
210 /* munch some more */
211 r = inflate(s, Z_SYNC_FLUSH);
212 if (r != Z_OK && r != Z_STREAM_END) {
213 lua_pushfstring(L, "failed to decompress [%d]", r);
214 lua_error(L);
215 }
216
217 /* push gathered data */
218 luaL_addsize(&b, LUAL_BUFFERSIZE - s->avail_out);
219
220 /* until we have free space in the output buffer - meaning we are done */
221 } while (s->avail_in > 0 || s->avail_out == 0);
222
223 /* send gathered data if any */
224 luaL_pushresult(&b);
225 }
226 return 1;
227 }
228
229 /* ====================================================================== */
230
231 static int lzstream_compress(lua_State *L)
232 {
233 z_stream *s = lzstream_check(L, 1, LZDEFLATE);
234 s->next_in = (Bytef*)luaL_checkstring(L, 2);
235 s->avail_in = lua_rawlen(L, 2);
236
237 {
238 int r;
239 luaL_Buffer b;
240 luaL_buffinit(L, &b);
241
242 do {
243 s->next_out = (Bytef*)luaL_prepbuffer(&b);
244 s->avail_out = LUAL_BUFFERSIZE;
245
246 /* bake some more */
247 if ((r = deflate(s, Z_NO_FLUSH)) != Z_OK)
248 {
249 lua_pushfstring(L, "failed to compress [%d]", r);
250 lua_error(L);
251 }
252
253 /* push gathered data */
254 luaL_addsize(&b, LUAL_BUFFERSIZE - s->avail_out);
255
256 /* until we have free space in the output buffer - meaning we are done */
257 } while (s->avail_out == 0);
258
259 /* send gathered data if any */
260 luaL_pushresult(&b);
261 }
262 return 1;
263 }
264
265 /* ====================================================================== */
266
267 static int lzstream_flush(lua_State *L)
268 {
269 z_stream *s = lzstream_check(L, 1, LZANY);
270
271 if (s->opaque == LZINFLATE) {
272 lua_pushliteral(L, "");
273 return 1;
274 }
275
276 s->next_in = (Bytef*)"";
277 s->avail_in = 0;
278
279 {
280 int r = 0;
281 luaL_Buffer b;
282 luaL_buffinit(L, &b);
283
284 do {
285 s->next_out = (Bytef*)luaL_prepbuffer(&b);
286 s->avail_out = LUAL_BUFFERSIZE;
287
288 r = deflate(s, Z_FINISH);
289
290 if (r != Z_OK && r != Z_STREAM_END) {
291 lua_pushfstring(L, "failed to flush [%d]", r);
292 lua_error(L);
293 }
294
295 /* push gathered data */
296 luaL_addsize(&b, LUAL_BUFFERSIZE - s->avail_out);
297 } while (r != Z_STREAM_END);
298
299 /* send gathered data if any */
300 luaL_pushresult(&b);
301 }
302 return 1;
303 }
304
305 /* ====================================================================== */
306
307 static int lzstream_reset(lua_State *L)
308 {
309 z_stream *s = lzstream_check(L, 1, LZANY);
310
311 if (s->state == LZDEFLATE)
312 lua_pushnumber(L, deflateReset(s));
313 else if (s->opaque == LZINFLATE)
314 lua_pushnumber(L, inflateReset(s));
315 else
316 {
317 lua_pushliteral(L, "invalid zlib stream state");
318 lua_error(L);
319 }
320
321 return 1;
322 }
323
324
325 /*
326 ** =========================================================================
327 ** zlib functions
328 ** =========================================================================
329 */
330
331 static int lzlib_version(lua_State *L)
332 {
333 lua_pushstring(L, zlibVersion());
334 return 1;
335 }
336
337 /* ====================================================================== */
338 static int lzlib_adler32(lua_State *L)
339 {
340 if (lua_gettop(L) == 0)
341 {
342 /* adler32 initial value */
343 lua_pushnumber(L, adler32(0L, Z_NULL, 0));
344 }
345 else
346 {
347 /* update adler32 checksum */
348 int adler = luaL_checkint(L, 1);
349 const char* buf = luaL_checkstring(L, 2);
350 int len = lua_rawlen(L, 2);
351
352 lua_pushnumber(L, adler32(adler, (const Bytef*)buf, len));
353 }
354 return 1;
355 }
356
357 /* ====================================================================== */
358 static int lzlib_crc32(lua_State *L)
359 {
360 if (lua_gettop(L) == 0)
361 {
362 /* crc32 initial value */
363 lua_pushnumber(L, crc32(0L, Z_NULL, 0));
364 }
365 else
366 {
367 /* update crc32 checksum */
368 int crc = luaL_checkint(L, 1);
369 const char* buf = luaL_checkstring(L, 2);
370 int len = lua_rawlen(L, 2);
371
372 lua_pushnumber(L, crc32(crc, (const Bytef*)buf, len));
373 }
374 return 1;
375 }
376
377 /* ====================================================================== */
378
379
380 static int lzlib_compress(lua_State *L)
381 {
382 const char *next_in = luaL_checkstring(L, 1);
383 int avail_in = lua_rawlen(L, 1);
384 int level = luaL_optint(L, 2, Z_DEFAULT_COMPRESSION);
385 int method = luaL_optint(L, 3, Z_DEFLATED);
386 int windowBits = luaL_optint(L, 4, 15);
387 int memLevel = luaL_optint(L, 5, 8);
388 int strategy = luaL_optint(L, 6, Z_DEFAULT_STRATEGY);
389
390 int ret;
391 z_stream zs;
392 luaL_Buffer b;
393 luaL_buffinit(L, &b);
394
395 zs.zalloc = Z_NULL;
396 zs.zfree = Z_NULL;
397
398 zs.next_out = Z_NULL;
399 zs.avail_out = 0;
400 zs.next_in = Z_NULL;
401 zs.avail_in = 0;
402
403 ret = deflateInit2(&zs, level, method, windowBits, memLevel, strategy);
404
405 if (ret != Z_OK)
406 {
407 lua_pushnil(L);
408 lua_pushnumber(L, ret);
409 return 2;
410 }
411
412 zs.next_in = (Bytef*)next_in;
413 zs.avail_in = avail_in;
414
415 for(;;)
416 {
417 zs.next_out = (Bytef*)luaL_prepbuffer(&b);
418 zs.avail_out = LUAL_BUFFERSIZE;
419
420 /* munch some more */
421 ret = deflate(&zs, Z_FINISH);
422
423 /* push gathered data */
424 luaL_addsize(&b, LUAL_BUFFERSIZE - zs.avail_out);
425
426 /* done processing? */
427 if (ret == Z_STREAM_END)
428 break;
429
430 /* error condition? */
431 if (ret != Z_OK)
432 break;
433 }
434
435 /* cleanup */
436 deflateEnd(&zs);
437
438 luaL_pushresult(&b);
439 lua_pushnumber(L, ret);
440 return 2;
441 }
442
443 /* ====================================================================== */
444
445 static int lzlib_decompress(lua_State *L)
446 {
447 const char *next_in = luaL_checkstring(L, 1);
448 int avail_in = lua_rawlen(L, 1);
449 int windowBits = luaL_optint(L, 2, 15);
450
451 int ret;
452 z_stream zs;
453 luaL_Buffer b;
454 luaL_buffinit(L, &b);
455
456
457 zs.zalloc = Z_NULL;
458 zs.zfree = Z_NULL;
459
460 zs.next_out = Z_NULL;
461 zs.avail_out = 0;
462 zs.next_in = Z_NULL;
463 zs.avail_in = 0;
464
465 ret = inflateInit2(&zs, windowBits);
466
467 if (ret != Z_OK)
468 {
469 lua_pushnil(L);
470 lua_pushnumber(L, ret);
471 return 2;
472 }
473
474 zs.next_in = (Bytef*)next_in;
475 zs.avail_in = avail_in;
476
477 for(;;)
478 {
479 zs.next_out = (Bytef*)luaL_prepbuffer(&b);
480 zs.avail_out = LUAL_BUFFERSIZE;
481 /* bake some more */
482 ret = inflate(&zs, Z_NO_FLUSH);
483 /* push gathered data */
484 luaL_addsize(&b, LUAL_BUFFERSIZE - zs.avail_out);
485 /* need dictionary? - no dictionary support here, so just quit */
486 if (ret == Z_NEED_DICT)
487 break;
488 /* done processing? */
489 if (ret == Z_STREAM_END)
490 break;
491 /* error condition? */
492 if (ret != Z_OK)
493 break;
494 }
495
496 /* cleanup */
497 inflateEnd(&zs);
498
499 luaL_pushresult(&b);
500 lua_pushnumber(L, ret);
501 return 2;
502 }
503
504
505 /*
506 ** =========================================================================
507 ** Register functions
508 ** =========================================================================
509 */
510
511
512 LUALIB_API int luaopen_zlib(lua_State *L)
513 {
514 const luaL_Reg zstreamm[] =
515 {
516 {"reset", lzstream_reset },
517
518 {"compress", lzstream_compress },
519 {"decompress", lzstream_decompress },
520 {"flush", lzstream_flush },
521 {"close", lzstream_close },
522
523 {"adler", lzstream_adler },
524
525 {"__tostring", lzstream_tostring },
526 {"__gc", lzstream_gc },
527 {NULL, NULL}
528 };
529
530 const luaL_Reg zlib[] =
531 {
532 {"version", lzlib_version },
533 {"adler32", lzlib_adler32 },
534 {"crc32", lzlib_crc32 },
535
536 {"compressobj", lzlib_deflate },
537 {"decompressobj", lzlib_inflate },
538
539 {"compress", lzlib_compress },
540 {"decompress", lzlib_decompress },
541
542 {NULL, NULL}
543 };
544
545 /* ====================================================================== */
546
547 /* make sure header and library version are consistent */
548 const char* version = zlibVersion();
549 if (strncmp(version, ZLIB_VERSION, 2))
550 {
551 lua_pushfstring(L, "zlib library version does not match - header: %s, library: %s", ZLIB_VERSION, version);
552 lua_error(L);
553 }
554
555 /* create new metatable for zlib compression structures */
556 luaL_newmetatable(L, ZSTREAMMETA);
557 lua_pushliteral(L, "__index");
558 lua_pushvalue(L, -2); /* push metatable */
559 lua_rawset(L, -3); /* metatable.__index = metatable */
560
561 /*
562 ** Stack: metatable
563 */
564 luaL_openlib(L, NULL, zstreamm, 0);
565
566 lua_pop(L, 1); /* remove metatable from stack */
567
568 /*
569 ** Stack:
570 */
571 lua_newtable(L);
572 luaL_openlib(L, "zlib", zlib, 1);
573
574 /*
575 ** Stack: zlib table
576 */
577 return 1;
578 }