1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
|
/* -*- mode: C; mode: fold; -*- */
/*
Copyright (C) 2008-2014 John E. Davis
This file is part of the S-Lang Library.
The S-Lang Library is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2 of the
License, or (at your option) any later version.
The S-Lang Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.
*/
#include "config.h"
#include <stdio.h>
#include <errno.h>
#include <slang.h>
#include <string.h>
#include <zlib.h>
SLANG_MODULE(zlib);
static SLFUTURE_CONST char *Module_Version_String = "0.1.0";
#define MODULE_VERSION_NUMBER (0*10000 + 1*100 + 0)
typedef struct
{
int type;
#define DEFLATE_TYPE 1
#define INFLATE_TYPE 2
int initialized;
z_stream zs;
#define DEFAULT_START_BUFLEN 0x4000
SLstrlen_Type start_buflen;
#define DEFAULT_BUFLEN_INC 0x4000
SLstrlen_Type dbuflen;
int windowbits;
}
ZLib_Type;
static int ZLib_Type_Id = -1;
int ZLib_Error = -1;
static int check_zerror (int e)
{
switch (e)
{
case Z_ERRNO:
e = errno;
(void) SLerrno_set_errno (e);
SLang_verror (ZLib_Error, "Z library errno error: %s", SLerrno_strerror(e));
break;
case Z_STREAM_ERROR:
SLang_verror (ZLib_Error, "Z library stream error");
break;
case Z_DATA_ERROR:
SLang_verror (ZLib_Error, "Z library data error");
break;
case Z_MEM_ERROR:
SLang_verror (SL_Malloc_Error, "Z library memory allocation error");
break;
case Z_BUF_ERROR:
SLang_verror (ZLib_Error, "Z library buffer error");
break;
case Z_VERSION_ERROR:
SLang_verror (ZLib_Error, "Z library version mismatch error");
break;
case Z_NEED_DICT: /* not handled by this module */
SLang_verror (ZLib_Error, "Z library dictionary error");
break;
default:
if (e >= 0)
return 0;
SLang_verror (ZLib_Error, "Unknown Error Code");
}
return -1;
}
static int check_deflate_object (ZLib_Type *zp)
{
if (zp->type != DEFLATE_TYPE)
{
SLang_verror (SL_TypeMismatch_Error, "Expecting a Zlib_Type deflate object");
return -1;
}
return 0;
}
static int check_inflate_object (ZLib_Type *zp)
{
if (zp->type != INFLATE_TYPE)
{
SLang_verror (SL_TypeMismatch_Error, "Expecting a Zlib_Type inflate object");
return -1;
}
return 0;
}
static int init_deflate_object (ZLib_Type *z,
int level, int method, int windowbits,
int memlevel, int strategy)
{
z_stream *zs;
int ret;
memset ((char *) z, 0, sizeof(ZLib_Type));
z->type = DEFLATE_TYPE;
z->start_buflen = DEFAULT_START_BUFLEN;
z->dbuflen = DEFAULT_BUFLEN_INC;
zs = &z->zs;
zs->zalloc = Z_NULL;
zs->zfree = Z_NULL;
zs->opaque = Z_NULL;
ret = deflateInit2 (zs, level, method, windowbits, memlevel, strategy);
if (ret == Z_STREAM_ERROR)
{
SLang_verror (ZLib_Error, "One of more deflate parameters are invalid.");
deflateEnd (zs);
}
if (-1 == check_zerror (ret))
{
deflateEnd (zs);
return -1;
}
z->initialized = 1;
return 0;
}
static int run_deflate (ZLib_Type *z, int flush,
unsigned char *str, SLstrlen_Type len,
unsigned char **bufp, SLstrlen_Type *totalp)
{
z_stream *zs;
unsigned char *buf;
SLstrlen_Type buflen;
zs = &z->zs;
buflen = z->start_buflen;
if (NULL == (buf = (unsigned char *) SLmalloc (buflen+1)))
{
*bufp = NULL;
*totalp = 0;
return -1;
}
zs->next_in = str;
zs->avail_in = len;
zs->next_out = buf;
zs->avail_out = buflen;
while (1)
{
SLstrlen_Type total;
int ret;
ret = deflate (zs, flush);
if (ret != Z_BUF_ERROR)
{
if (-1 == check_zerror (ret))
goto return_error;
}
total = buflen - zs->avail_out;
if (/* done -- flush == Z_FINISH */
(ret == Z_STREAM_END)
/* Done with current input */
|| ((zs->avail_in == 0) && (zs->avail_out != 0))
)
{
if (total != buflen)
{
unsigned char *new_buf;
new_buf = (unsigned char *) SLrealloc ((char *)buf, total+1);
if (new_buf == NULL)
goto return_error;
buf = new_buf;
}
buf[total] = 0; /* ok, because of +1 in malloc calls */
*bufp = buf;
*totalp = total;
return 0;
}
if (zs->avail_out == 0)
{
unsigned char *new_buf;
SLstrlen_Type dbuflen = z->dbuflen;
buflen += dbuflen;
new_buf = (unsigned char *)SLrealloc ((char *) buf, buflen+1);
if (new_buf == NULL)
goto return_error;
buf = new_buf;
zs->avail_out = dbuflen;
zs->next_out = buf + total;
}
}
return_error:
SLfree ((char *) buf);
*bufp = NULL;
*totalp = 0;
return -1;
}
static void deflate_intrin (ZLib_Type *zp, SLang_BString_Type *inbstr, int *flush)
{
SLang_BString_Type *outbstr;
unsigned char *inbuf, *outbuf;
SLstrlen_Type inlen, outlen;
if (-1 == check_deflate_object (zp))
return;
if (NULL == (inbuf = SLbstring_get_pointer (inbstr, &inlen)))
return;
if (zp->start_buflen < inlen)
zp->start_buflen = inlen;
if (-1 == run_deflate (zp, *flush, inbuf, inlen, &outbuf, &outlen))
return;
if (NULL == (outbstr = SLbstring_create_malloced (outbuf, outlen, 1)))
return;
(void) SLang_push_bstring (outbstr);
SLbstring_free (outbstr);
}
static void deflate_reset_intrin (ZLib_Type *z)
{
if (-1 == check_deflate_object (z))
return;
check_zerror (deflateReset (&z->zs));
}
static void deflate_flush_intrin (ZLib_Type *z, int *flush)
{
unsigned char *buf;
SLstrlen_Type len;
SLang_BString_Type *bstr;
if (-1 == check_deflate_object (z))
return;
if (-1 == run_deflate (z, *flush, (unsigned char *)"", 0, &buf, &len))
return;
if (NULL == (bstr = SLbstring_create_malloced (buf, len, 1)))
return;
(void) SLang_push_bstring (bstr);
SLbstring_free (bstr);
}
static void free_deflate_object (ZLib_Type *z)
{
if (z->initialized)
deflateEnd (&z->zs);
SLfree ((char *) z);
}
static void deflate_new_intrin (int *level, int *method, int *wbits,
int *memlevel, int *strategy)
{
/* According to the docs, the parameters are checked by deflateInit2 */
ZLib_Type *z;
SLang_MMT_Type *mmt;
if (NULL == (z = (ZLib_Type *) SLmalloc (sizeof (ZLib_Type))))
return;
if (-1 == init_deflate_object (z, *level, *method, *wbits, *memlevel, *strategy))
{
SLfree ((char *) z);
return;
}
if (NULL == (mmt = SLang_create_mmt (ZLib_Type_Id, (VOID_STAR) z)))
{
free_deflate_object (z);
return;
}
if (0 == SLang_push_mmt (mmt))
return;
SLang_free_mmt (mmt);
}
static int run_inflate (ZLib_Type *z, int flush,
unsigned char *str, SLstrlen_Type len,
unsigned char **bufp, SLstrlen_Type *totalp)
{
z_stream *zs;
unsigned char *buf;
SLstrlen_Type buflen;
zs = &z->zs;
zs->next_in = str;
zs->avail_in = len;
if (z->initialized == 0)
{
zs = &z->zs;
zs->zalloc = Z_NULL;
zs->zfree = Z_NULL;
zs->opaque = Z_NULL;
if (-1 == check_zerror (inflateInit2 (zs, z->windowbits)))
{
inflateEnd (zs);
return -1;
}
z->initialized = 1;
}
buflen = z->start_buflen;
if (NULL == (buf = (unsigned char *) SLmalloc (buflen+1)))
{
*bufp = NULL;
*totalp = 0;
return -1;
}
zs->next_out = buf;
zs->avail_out = buflen;
while (1)
{
SLstrlen_Type total;
int ret;
ret = inflate (zs, flush);
if (ret != Z_BUF_ERROR)
{
if (-1 == check_zerror (ret))
goto return_error;
}
total = buflen - zs->avail_out;
if (/* done -- flush == Z_FINISH */
(ret == Z_STREAM_END)
/* Done with current input */
|| ((zs->avail_in == 0) && (zs->avail_out != 0))
)
{
if (total != buflen)
{
unsigned char *new_buf;
new_buf = (unsigned char *) SLrealloc ((char *)buf, total+1);
if (new_buf == NULL)
goto return_error;
buf = new_buf;
}
buf[total] = 0; /* ok, because of +1 in malloc calls */
*bufp = buf;
*totalp = total;
return 0;
}
if (zs->avail_out == 0)
{
unsigned char *new_buf;
SLstrlen_Type dbuflen = z->dbuflen;
buflen += dbuflen;
new_buf = (unsigned char *)SLrealloc ((char *) buf, buflen+1);
if (new_buf == NULL)
goto return_error;
buf = new_buf;
zs->avail_out = dbuflen;
zs->next_out = buf + total;
}
}
return_error:
SLfree ((char *) buf);
*bufp = NULL;
*totalp = 0;
return -1;
}
static void inflate_intrin (ZLib_Type *zp, SLang_BString_Type *inbstr, int *flush)
{
SLang_BString_Type *outbstr;
unsigned char *inbuf, *outbuf;
SLstrlen_Type inlen, outlen;
if (-1 == check_inflate_object (zp))
return;
if (NULL == (inbuf = SLbstring_get_pointer (inbstr, &inlen)))
return;
if (zp->start_buflen < inlen)
zp->start_buflen = inlen;
if (-1 == run_inflate (zp, *flush, inbuf, inlen, &outbuf, &outlen))
return;
if (NULL == (outbstr = SLbstring_create_malloced (outbuf, outlen, 1)))
return;
(void) SLang_push_bstring (outbstr);
SLbstring_free (outbstr);
}
static int init_inflate_object (ZLib_Type *z, int windowbits)
{
memset ((char *) z, 0, sizeof(ZLib_Type));
z->type = INFLATE_TYPE;
z->windowbits = windowbits;
z->initialized = 0;
z->start_buflen = DEFAULT_START_BUFLEN;
z->dbuflen = DEFAULT_BUFLEN_INC;
return 0;
}
static void inflate_new_intrin (int *windowbits)
{
ZLib_Type *z;
SLang_MMT_Type *mmt;
if (NULL == (z = (ZLib_Type *) SLmalloc (sizeof (ZLib_Type))))
return;
if (-1 == init_inflate_object (z, *windowbits))
{
SLfree ((char *) z);
return;
}
if (NULL == (mmt = SLang_create_mmt (ZLib_Type_Id, (VOID_STAR) z)))
{
free_deflate_object (z);
return;
}
if (0 == SLang_push_mmt (mmt))
return;
SLang_free_mmt (mmt);
}
static void inflate_reset_intrin (ZLib_Type *z)
{
if (-1 == check_inflate_object (z))
return;
if (z->initialized)
check_zerror (deflateReset (&z->zs));
}
static void inflate_flush_intrin (ZLib_Type *z, int *flush)
{
unsigned char *buf;
SLstrlen_Type len;
SLang_BString_Type *bstr;
if (-1 == check_inflate_object (z))
return;
if (-1 == run_inflate (z, *flush, (unsigned char *)"", 0, &buf, &len))
return;
if (NULL == (bstr = SLbstring_create_malloced (buf, len, 1)))
return;
(void) SLang_push_bstring (bstr);
SLbstring_free (bstr);
}
static void free_inflate_object (ZLib_Type *z)
{
if (z->initialized)
inflateEnd (&z->zs);
SLfree ((char *) z);
}
static void destroy_zlib_type (SLtype type, VOID_STAR f)
{
ZLib_Type *z;
(void) type;
z = (ZLib_Type *) f;
if (z->type == DEFLATE_TYPE)
free_deflate_object (z);
else
free_inflate_object (z);
}
static const char *zlib_version_intrin (void)
{
return zlibVersion ();
}
#define DUMMY_ZLIB_TYPE ((SLtype)-1)
#define I SLANG_INT_TYPE
#define B SLANG_BSTRING_TYPE
static SLang_Intrin_Fun_Type Module_Intrinsics [] =
{
MAKE_INTRINSIC_0("zlib_version", zlib_version_intrin, SLANG_STRING_TYPE),
MAKE_INTRINSIC_5("_zlib_deflate_new", deflate_new_intrin, SLANG_VOID_TYPE, I, I, I, I, I),
MAKE_INTRINSIC_3("_zlib_deflate", deflate_intrin, SLANG_VOID_TYPE, DUMMY_ZLIB_TYPE, B, I),
MAKE_INTRINSIC_2("_zlib_deflate_flush", deflate_flush_intrin, SLANG_VOID_TYPE, DUMMY_ZLIB_TYPE, I),
MAKE_INTRINSIC_1("_zlib_deflate_reset", deflate_reset_intrin, SLANG_VOID_TYPE, DUMMY_ZLIB_TYPE),
MAKE_INTRINSIC_1("_zlib_inflate_new", inflate_new_intrin, SLANG_VOID_TYPE, I),
MAKE_INTRINSIC_3("_zlib_inflate", inflate_intrin, SLANG_VOID_TYPE, DUMMY_ZLIB_TYPE, B, I),
MAKE_INTRINSIC_2("_zlib_inflate_flush", inflate_flush_intrin, SLANG_VOID_TYPE, DUMMY_ZLIB_TYPE, I),
MAKE_INTRINSIC_1("_zlib_inflate_reset", inflate_reset_intrin, SLANG_VOID_TYPE, DUMMY_ZLIB_TYPE),
SLANG_END_INTRIN_FUN_TABLE
};
#undef I
static SLang_Intrin_Var_Type Module_Variables [] =
{
MAKE_VARIABLE("_zlib_module_version_string", &Module_Version_String, SLANG_STRING_TYPE, 1),
SLANG_END_INTRIN_VAR_TABLE
};
static SLang_IConstant_Type Module_IConstants [] =
{
MAKE_ICONSTANT("_zlib_module_version", MODULE_VERSION_NUMBER),
MAKE_ICONSTANT("ZLIB_NO_COMPRESSION", Z_NO_COMPRESSION),
MAKE_ICONSTANT("ZLIB_BEST_SPEED", Z_BEST_SPEED),
MAKE_ICONSTANT("ZLIB_BEST_COMPRESSION", Z_BEST_COMPRESSION),
MAKE_ICONSTANT("ZLIB_DEFAULT_COMPRESSION", Z_DEFAULT_COMPRESSION),
MAKE_ICONSTANT("ZLIB_NO_FLUSH", Z_NO_FLUSH),
MAKE_ICONSTANT("ZLIB_SYNC_FLUSH", Z_SYNC_FLUSH),
MAKE_ICONSTANT("ZLIB_FULL_FLUSH", Z_FULL_FLUSH),
MAKE_ICONSTANT("ZLIB_FINISH", Z_FINISH),
MAKE_ICONSTANT("ZLIB_FILTERED", Z_FILTERED),
MAKE_ICONSTANT("ZLIB_HUFFMAN_ONLY", Z_HUFFMAN_ONLY),
#ifdef Z_RLE
MAKE_ICONSTANT("ZLIB_RLE", Z_RLE),
#endif
#ifdef Z_FIXED
MAKE_ICONSTANT("ZLIB_FIXED", Z_FIXED),
#endif
MAKE_ICONSTANT("ZLIB_DEFAULT_STRATEGY", Z_DEFAULT_STRATEGY),
MAKE_ICONSTANT("ZLIB_DEFLATED", Z_DEFLATED),
SLANG_END_ICONST_TABLE
};
static int register_classes (void)
{
SLang_Class_Type *cl;
if (ZLib_Type_Id != -1)
return 0;
if (NULL == (cl = SLclass_allocate_class ("ZLib_Type")))
return -1;
(void) SLclass_set_destroy_function (cl, destroy_zlib_type);
if (-1 == SLclass_register_class (cl, SLANG_VOID_TYPE,
sizeof (ZLib_Type),
SLANG_CLASS_TYPE_MMT))
return -1;
ZLib_Type_Id = SLclass_get_class_id (cl);
if (-1 == SLclass_patch_intrin_fun_table1 (Module_Intrinsics, DUMMY_ZLIB_TYPE, ZLib_Type_Id))
return -1;
return 0;
}
int init_zlib_module_ns (char *ns_name)
{
SLang_NameSpace_Type *ns = SLns_create_namespace (ns_name);
if (ns == NULL)
return -1;
if (-1 == register_classes ())
return -1;
if ((-1 == ZLib_Error)
&& (-1 == (ZLib_Error = SLerr_new_exception (SL_RunTime_Error, "ZLibError", "ZLib Error"))))
return -1;
if (
(-1 == SLns_add_intrin_var_table (ns, Module_Variables, NULL))
|| (-1 == SLns_add_intrin_fun_table (ns, Module_Intrinsics, NULL))
|| (-1 == SLns_add_iconstant_table (ns, Module_IConstants, NULL))
)
return -1;
return 0;
}
/* This function is optional */
void deinit_zlib_module (void)
{
}
|