| 1 |
/* |
| 2 |
* Gauche-zlib - zlib module |
| 3 |
* |
| 4 |
* Copyright (c) 2006 Rui Ueyama, 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 |
* |
| 13 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
* notice, this list of conditions and the following disclaimer in the |
| 15 |
* documentation and/or other materials provided with the distribution. |
| 16 |
* |
| 17 |
* 3. Neither the name of the authors nor the names of its contributors |
| 18 |
* may be used to endorse or promote products derived from this |
| 19 |
* software without specific prior written permission. |
| 20 |
* |
| 21 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 22 |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 23 |
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 24 |
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 25 |
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 26 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED |
| 27 |
* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 28 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 29 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 31 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 |
*/ |
| 33 |
|
| 34 |
#include "gauche-zlib.h" |
| 35 |
#include <gauche/exception.h> |
| 36 |
#define CHUNK 4096 |
| 37 |
|
| 38 |
#define DEFAULT_BUFFER_SIZE 4096 |
| 39 |
#define MINIMUM_BUFFER_SIZE 1024 |
| 40 |
|
| 41 |
/*================================================================ |
| 42 |
* Class stuff |
| 43 |
*/ |
| 44 |
|
| 45 |
static ScmClass *port_cpl[] = { |
| 46 |
SCM_CLASS_STATIC_PTR(Scm_PortClass), |
| 47 |
SCM_CLASS_STATIC_PTR(Scm_TopClass), |
| 48 |
NULL |
| 49 |
}; |
| 50 |
|
| 51 |
SCM_DEFINE_BASE_CLASS(Scm_DeflatingPortClass, |
| 52 |
ScmPort, /* instance type */ |
| 53 |
NULL, NULL, NULL, NULL, port_cpl); |
| 54 |
|
| 55 |
SCM_DEFINE_BASE_CLASS(Scm_InflatingPortClass, |
| 56 |
ScmPort, /* instance type */ |
| 57 |
NULL, NULL, NULL, NULL, port_cpl); |
| 58 |
|
| 59 |
/*================================================================ |
| 60 |
* Conditions |
| 61 |
*/ |
| 62 |
|
| 63 |
static ScmClass *zlib_error_cpl[] = { |
| 64 |
SCM_CLASS_STATIC_PTR(Scm_ZlibErrorClass), |
| 65 |
SCM_CLASS_STATIC_PTR(Scm_ErrorClass), |
| 66 |
SCM_CLASS_STATIC_PTR(Scm_MessageConditionClass), |
| 67 |
SCM_CLASS_STATIC_PTR(Scm_SeriousConditionClass), |
| 68 |
SCM_CLASS_STATIC_PTR(Scm_ConditionClass), |
| 69 |
SCM_CLASS_STATIC_PTR(Scm_TopClass), |
| 70 |
NULL |
| 71 |
}; |
| 72 |
|
| 73 |
static ScmObj zliberror_allocate(ScmClass *klass, ScmObj initargs); |
| 74 |
static void message_print(ScmObj obj, ScmPort *port, ScmWriteContext *ctx); |
| 75 |
|
| 76 |
SCM_DEFINE_BASE_CLASS(Scm_ZlibErrorClass, ScmZlibError, |
| 77 |
message_print, NULL, NULL, |
| 78 |
zliberror_allocate, zlib_error_cpl+1); |
| 79 |
SCM_DEFINE_BASE_CLASS(Scm_ZlibNeedDictErrorClass, ScmZlibNeedDictError, |
| 80 |
message_print, NULL, NULL, |
| 81 |
zliberror_allocate, zlib_error_cpl); |
| 82 |
SCM_DEFINE_BASE_CLASS(Scm_ZlibStreamErrorClass, ScmZlibStreamError, |
| 83 |
message_print, NULL, NULL, |
| 84 |
zliberror_allocate, zlib_error_cpl); |
| 85 |
SCM_DEFINE_BASE_CLASS(Scm_ZlibDataErrorClass, ScmZlibDataError, |
| 86 |
message_print, NULL, NULL, |
| 87 |
zliberror_allocate, zlib_error_cpl); |
| 88 |
SCM_DEFINE_BASE_CLASS(Scm_ZlibMemoryErrorClass, ScmZlibMemoryError, |
| 89 |
message_print, NULL, NULL, |
| 90 |
zliberror_allocate, zlib_error_cpl); |
| 91 |
SCM_DEFINE_BASE_CLASS(Scm_ZlibVersionErrorClass, ScmZlibVersionError, |
| 92 |
message_print, NULL, NULL, |
| 93 |
zliberror_allocate, zlib_error_cpl); |
| 94 |
|
| 95 |
static ScmObj zliberror_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED) |
| 96 |
{ |
| 97 |
ScmZlibError *e = SCM_NEW_INSTANCE(ScmZlibError, klass); |
| 98 |
e->message = SCM_FALSE; |
| 99 |
return SCM_OBJ(e); |
| 100 |
} |
| 101 |
|
| 102 |
static void message_print(ScmObj obj, ScmPort *port, |
| 103 |
ScmWriteContext *ctx SCM_UNUSED) |
| 104 |
{ |
| 105 |
ScmClass *k = Scm_ClassOf(obj); |
| 106 |
Scm_Printf(port, "#<%A \"%30.1A\">", |
| 107 |
Scm_ShortClassName(k), |
| 108 |
SCM_ERROR_MESSAGE(obj)); |
| 109 |
} |
| 110 |
|
| 111 |
static ScmClassStaticSlotSpec zliberror_slots[] = { |
| 112 |
SCM_CLASS_SLOT_SPEC_END() |
| 113 |
}; |
| 114 |
|
| 115 |
ScmObj Scm_MakeZlibError(ScmObj message, int error_code) |
| 116 |
{ |
| 117 |
ScmClass *klass = NULL; |
| 118 |
switch (error_code) { |
| 119 |
case Z_NEED_DICT: |
| 120 |
klass = SCM_CLASS_ZLIB_NEED_DICT_ERROR; |
| 121 |
break; |
| 122 |
case Z_DATA_ERROR: |
| 123 |
klass = SCM_CLASS_ZLIB_DATA_ERROR; |
| 124 |
break; |
| 125 |
case Z_STREAM_ERROR: |
| 126 |
klass = SCM_CLASS_ZLIB_STREAM_ERROR; |
| 127 |
break; |
| 128 |
case Z_MEM_ERROR: |
| 129 |
klass = SCM_CLASS_ZLIB_MEMORY_ERROR; |
| 130 |
break; |
| 131 |
case Z_VERSION_ERROR: |
| 132 |
klass = SCM_CLASS_ZLIB_VERSION_ERROR; |
| 133 |
break; |
| 134 |
default: |
| 135 |
fprintf(stderr, "error_code: %d\n", error_code); |
| 136 |
Scm_Error("Scm_MakeZlibError called with unknown error code (%d). " |
| 137 |
"Implementation error?", |
| 138 |
error_code); |
| 139 |
} |
| 140 |
|
| 141 |
ScmZlibError *e = SCM_ZLIB_ERROR(zliberror_allocate(klass, SCM_NIL)); |
| 142 |
e->message = message; |
| 143 |
return SCM_OBJ(e); |
| 144 |
} |
| 145 |
|
| 146 |
void Scm_ZlibError(int error_code, const char *msg, ...) |
| 147 |
{ |
| 148 |
ScmObj e; |
| 149 |
ScmVM *vm = Scm_VM(); |
| 150 |
|
| 151 |
SCM_UNWIND_PROTECT { |
| 152 |
va_list args; |
| 153 |
ScmObj ostr = Scm_MakeOutputStringPort(TRUE); |
| 154 |
va_start(args, msg); |
| 155 |
Scm_Vprintf(SCM_PORT(ostr), msg, args, TRUE); |
| 156 |
va_end(args); |
| 157 |
e = Scm_MakeZlibError(Scm_GetOutputString(SCM_PORT(ostr), 0), |
| 158 |
error_code); |
| 159 |
} |
| 160 |
SCM_WHEN_ERROR { |
| 161 |
/* TODO: should check continuation */ |
| 162 |
e = Scm_MakeError(SCM_MAKE_STR("Error occurred in error handler")); |
| 163 |
} |
| 164 |
SCM_END_PROTECT; |
| 165 |
Scm_VMThrowException(vm, e, 0); |
| 166 |
Scm_Panic("Scm_Error: Scm_VMThrowException returned. something wrong."); |
| 167 |
} |
| 168 |
|
| 169 |
static ScmObj porterror_allocate(ScmClass *klass, ScmObj initargs SCM_UNUSED) |
| 170 |
{ |
| 171 |
ScmPortError *e = SCM_NEW_INSTANCE(ScmPortError, klass); |
| 172 |
e->common.message = SCM_FALSE; /* set by initialize */ |
| 173 |
e->port = NULL; /* set by initialize */ |
| 174 |
return SCM_OBJ(e); |
| 175 |
} |
| 176 |
|
| 177 |
void Scm_ZlibPortError(ScmPort *port, int error_code, const char *msg, ...) |
| 178 |
{ |
| 179 |
ScmObj e; |
| 180 |
ScmVM *vm = Scm_VM(); |
| 181 |
|
| 182 |
SCM_UNWIND_PROTECT { |
| 183 |
va_list args; |
| 184 |
ScmObj ostr = Scm_MakeOutputStringPort(TRUE); |
| 185 |
va_start(args, msg); |
| 186 |
Scm_Vprintf(SCM_PORT(ostr), msg, args, TRUE); |
| 187 |
va_end(args); |
| 188 |
ScmObj smsg = Scm_GetOutputString(SCM_PORT(ostr), 0); |
| 189 |
ScmObj pe = porterror_allocate(SCM_CLASS_IO_READ_ERROR, SCM_NIL); |
| 190 |
SCM_ERROR(pe)->message = smsg; |
| 191 |
SCM_PORT_ERROR(pe)->port = port; |
| 192 |
e = Scm_MakeCompoundCondition(SCM_LIST2(Scm_MakeZlibError(smsg, error_code), |
| 193 |
pe)); |
| 194 |
} |
| 195 |
SCM_WHEN_ERROR { |
| 196 |
/* TODO: should check continuation */ |
| 197 |
e = Scm_MakeError(SCM_MAKE_STR("Error occurred in error handler")); |
| 198 |
} |
| 199 |
SCM_END_PROTECT; |
| 200 |
Scm_VMThrowException(vm, e, 0); |
| 201 |
Scm_Panic("Scm_Error: Scm_VMThrowException returned. something wrong."); |
| 202 |
} |
| 203 |
|
| 204 |
/*================================================================ |
| 205 |
* Common |
| 206 |
*/ |
| 207 |
|
| 208 |
static ScmSize fix_buffer_size(ScmSize siz) |
| 209 |
{ |
| 210 |
if (siz <= 0) return DEFAULT_BUFFER_SIZE; |
| 211 |
if (siz <= MINIMUM_BUFFER_SIZE) return MINIMUM_BUFFER_SIZE; |
| 212 |
return siz; |
| 213 |
} |
| 214 |
|
| 215 |
static ScmObj port_name(const char *type, ScmPort *source) |
| 216 |
{ |
| 217 |
ScmObj out = Scm_MakeOutputStringPort(TRUE); |
| 218 |
Scm_Printf(SCM_PORT(out), "[%s %A]", |
| 219 |
type, Scm_PortName(source)); |
| 220 |
return Scm_GetOutputStringUnsafe(SCM_PORT(out), 0); |
| 221 |
} |
| 222 |
|
| 223 |
/*================================================================ |
| 224 |
* Deflating port |
| 225 |
*/ |
| 226 |
static ScmSize deflate_flusher(ScmPort *port, ScmSize cnt, int forcep) |
| 227 |
{ |
| 228 |
ScmZlibInfo *info = SCM_PORT_ZLIB_INFO(port); |
| 229 |
z_streamp strm = SCM_PORT_ZSTREAM(port); |
| 230 |
ScmSize total = 0; |
| 231 |
unsigned char *inbuf = (u_char*)Scm_PortBufferStruct(port)->buffer; |
| 232 |
unsigned char outbuf[CHUNK]; |
| 233 |
|
| 234 |
strm->next_in = inbuf; |
| 235 |
strm->avail_in = Scm_PortBufferAvail(port); |
| 236 |
|
| 237 |
if (info->flush == Z_NO_FLUSH && forcep) { |
| 238 |
info->flush = Z_SYNC_FLUSH; |
| 239 |
} |
| 240 |
|
| 241 |
for (;;) { |
| 242 |
strm->next_out = outbuf; |
| 243 |
strm->avail_out = CHUNK; |
| 244 |
int ret = deflate(strm, info->flush); |
| 245 |
SCM_ASSERT(ret == Z_OK); |
| 246 |
if (strm->avail_out != 0) { |
| 247 |
info->flush = Z_NO_FLUSH; |
| 248 |
} |
| 249 |
ScmSize nread = strm->next_in - inbuf; |
| 250 |
ScmSize nwrite = strm->next_out - outbuf; |
| 251 |
total += nread; |
| 252 |
if (nwrite > 0) { |
| 253 |
Scm_Putz((char*)outbuf, nwrite, info->remote); |
| 254 |
} |
| 255 |
if (forcep && (total < cnt)) { |
| 256 |
continue; |
| 257 |
} |
| 258 |
return total; |
| 259 |
} |
| 260 |
} |
| 261 |
|
| 262 |
static void deflate_closer(ScmPort *port) |
| 263 |
{ |
| 264 |
ScmZlibInfo *info = SCM_PORT_ZLIB_INFO(port); |
| 265 |
z_streamp strm = SCM_PORT_ZSTREAM(port); |
| 266 |
unsigned char *inbuf = (u_char*)Scm_PortBufferStruct(port)->buffer; |
| 267 |
unsigned char outbuf[CHUNK]; |
| 268 |
|
| 269 |
strm->next_in = inbuf; |
| 270 |
strm->avail_in = Scm_PortBufferAvail(port); |
| 271 |
strm->next_out = outbuf; |
| 272 |
strm->avail_out = CHUNK; |
| 273 |
|
| 274 |
int r; |
| 275 |
do { |
| 276 |
r = deflate(strm, Z_FINISH); |
| 277 |
SCM_ASSERT(r == Z_OK || r == Z_STREAM_END); |
| 278 |
int nwrite = strm->next_out - outbuf; |
| 279 |
if (nwrite > 0) { |
| 280 |
Scm_Putz((char*)outbuf, nwrite, info->remote); |
| 281 |
strm->next_out = outbuf; |
| 282 |
strm->avail_out = CHUNK; |
| 283 |
} |
| 284 |
} while (r != Z_STREAM_END); |
| 285 |
r = deflateEnd(strm); |
| 286 |
if (r != Z_OK) { |
| 287 |
Scm_ZlibError(r, "deflateEnd failed: %s", strm->msg); |
| 288 |
} |
| 289 |
Scm_Flush(info->remote); |
| 290 |
if (info->ownerp) { |
| 291 |
Scm_ClosePort(info->remote); |
| 292 |
} |
| 293 |
} |
| 294 |
|
| 295 |
static int zlib_fileno(ScmPort *port) |
| 296 |
{ |
| 297 |
return Scm_PortFileNo(SCM_PORT_ZLIB_INFO(port)->remote); |
| 298 |
} |
| 299 |
|
| 300 |
ScmObj Scm_MakeDeflatingPort(ScmPort *source, int level, |
| 301 |
int window_bits, int memlevel, |
| 302 |
int strategy, ScmObj dict, |
| 303 |
ScmSize bufsiz, int ownerp) |
| 304 |
{ |
| 305 |
ScmZlibInfo *info = SCM_NEW(ScmZlibInfo); |
| 306 |
z_streamp strm = SCM_NEW_ATOMIC2(z_streamp, sizeof(z_stream)); |
| 307 |
|
| 308 |
bufsiz = fix_buffer_size(bufsiz); |
| 309 |
|
| 310 |
strm->zalloc = NULL; |
| 311 |
strm->zfree = NULL; |
| 312 |
strm->opaque = NULL; |
| 313 |
strm->next_in = NULL; |
| 314 |
strm->avail_in = 0; |
| 315 |
int r = deflateInit2(strm, level, Z_DEFLATED, window_bits, |
| 316 |
memlevel, strategy); |
| 317 |
if (r != Z_OK) { |
| 318 |
Scm_ZlibError(r, "deflateInit2 error: %s", strm->msg); |
| 319 |
} |
| 320 |
|
| 321 |
if (!SCM_FALSEP(dict)) { |
| 322 |
if (!SCM_STRINGP(dict)) |
| 323 |
Scm_Error("String required, but got %S", dict); |
| 324 |
int r = deflateSetDictionary(strm, |
| 325 |
(unsigned char*)SCM_STRING_START(dict), |
| 326 |
SCM_STRING_SIZE(dict)); |
| 327 |
if (r != Z_OK) { |
| 328 |
Scm_ZlibError(r, "deflateSetDictionary failed: %s", strm->msg); |
| 329 |
} |
| 330 |
info->dict_adler = Scm_MakeIntegerU(strm->adler); |
| 331 |
} else { |
| 332 |
info->dict_adler = SCM_FALSE; |
| 333 |
} |
| 334 |
|
| 335 |
info->strm = strm; |
| 336 |
info->remote = source; |
| 337 |
info->bufsiz = 0; |
| 338 |
info->buf = NULL; |
| 339 |
info->ptr = NULL; |
| 340 |
info->ownerp = ownerp; |
| 341 |
info->flush = Z_NO_FLUSH; |
| 342 |
info->stream_endp = FALSE; |
| 343 |
info->level = level; |
| 344 |
info->strategy = strategy; |
| 345 |
|
| 346 |
ScmPortBuffer bufrec; |
| 347 |
memset(&bufrec, 0, sizeof(bufrec)); |
| 348 |
bufrec.size = bufsiz; |
| 349 |
bufrec.buffer = SCM_NEW_ATOMIC2(char *, bufsiz); |
| 350 |
bufrec.mode = SCM_PORT_BUFFER_FULL; |
| 351 |
bufrec.filler = NULL; |
| 352 |
bufrec.flusher = deflate_flusher; |
| 353 |
bufrec.closer = deflate_closer; |
| 354 |
bufrec.ready = NULL; |
| 355 |
bufrec.filenum = zlib_fileno; |
| 356 |
bufrec.data = (void*)info; |
| 357 |
|
| 358 |
ScmObj name = port_name("deflating", source); |
| 359 |
return Scm_MakeBufferedPort(SCM_CLASS_DEFLATING_PORT, name, |
| 360 |
SCM_PORT_OUTPUT, TRUE, &bufrec); |
| 361 |
} |
| 362 |
|
| 363 |
/*================================================================ |
| 364 |
* Inflating port |
| 365 |
*/ |
| 366 |
|
| 367 |
static ScmSize inflate_filler(ScmPort *port, ScmSize mincnt SCM_UNUSED) |
| 368 |
{ |
| 369 |
ScmZlibInfo *info = SCM_PORT_ZLIB_INFO(port); |
| 370 |
z_streamp strm = SCM_PORT_ZSTREAM(port); |
| 371 |
unsigned char *outbuf = (u_char*)Scm_PortBufferStruct(port)->end; |
| 372 |
int r; |
| 373 |
|
| 374 |
if (info->stream_endp) return 0; |
| 375 |
|
| 376 |
ScmSize nread = Scm_Getz(info->ptr, |
| 377 |
info->bufsiz - (info->ptr - info->buf), |
| 378 |
info->remote); |
| 379 |
|
| 380 |
if (nread <= 0) { |
| 381 |
/* input reached EOF */ |
| 382 |
if (info->ptr == info->buf) { |
| 383 |
info->stream_endp = TRUE; |
| 384 |
return 0; |
| 385 |
} |
| 386 |
strm->avail_in = info->ptr - info->buf; |
| 387 |
} else { |
| 388 |
strm->avail_in = (info->ptr + nread) - info->buf; |
| 389 |
} |
| 390 |
|
| 391 |
strm->next_in = (unsigned char*)info->buf; |
| 392 |
strm->next_out = outbuf; |
| 393 |
strm->avail_out = Scm_PortBufferRoom(port); |
| 394 |
|
| 395 |
redo: |
| 396 |
r = inflate(strm, Z_SYNC_FLUSH); |
| 397 |
if (strm->avail_in > 0) { |
| 398 |
memmove(info->buf, strm->next_in, strm->avail_in); |
| 399 |
info->ptr = info->buf + strm->avail_in; |
| 400 |
} else { |
| 401 |
info->ptr = info->buf; |
| 402 |
} |
| 403 |
strm->next_in = (unsigned char*)info->buf; |
| 404 |
SCM_ASSERT(r != Z_STREAM_ERROR); |
| 405 |
switch (r) { |
| 406 |
case Z_NEED_DICT: |
| 407 |
if (info->dict == NULL) { |
| 408 |
Scm_ZlibPortError(info->remote, r, "dictionary required"); |
| 409 |
} |
| 410 |
r = inflateSetDictionary(strm, info->dict, info->dictlen); |
| 411 |
if (r != Z_OK) { |
| 412 |
Scm_ZlibError(r, "inflateSetDictionary error: %s", strm->msg); |
| 413 |
} |
| 414 |
info->dict_adler = Scm_MakeIntegerU(strm->adler); |
| 415 |
if (strm->avail_in > 0) { |
| 416 |
goto redo; |
| 417 |
} else { |
| 418 |
goto end; |
| 419 |
} |
| 420 |
case Z_STREAM_END: |
| 421 |
info->stream_endp = TRUE; |
| 422 |
goto end; |
| 423 |
case Z_OK: |
| 424 |
goto end; |
| 425 |
case Z_DATA_ERROR: |
| 426 |
if (strm->next_out - outbuf > 0) { |
| 427 |
goto end; |
| 428 |
} |
| 429 |
/* fallthru */ |
| 430 |
default: |
| 431 |
Scm_ZlibPortError(info->remote, r, "inflate error: %s", strm->msg); |
| 432 |
} |
| 433 |
end: |
| 434 |
return strm->next_out - outbuf; |
| 435 |
} |
| 436 |
|
| 437 |
static void inflate_closer(ScmPort *port) |
| 438 |
{ |
| 439 |
ScmZlibInfo *info = SCM_PORT_ZLIB_INFO(port); |
| 440 |
z_streamp strm = SCM_PORT_ZSTREAM(port); |
| 441 |
int r = inflateEnd(strm); |
| 442 |
if (r != Z_OK) { |
| 443 |
Scm_ZlibError(r, "inflateEnd failed: %s", strm->msg); |
| 444 |
} |
| 445 |
if (info->ownerp) { |
| 446 |
Scm_ClosePort(info->remote); |
| 447 |
} |
| 448 |
} |
| 449 |
|
| 450 |
static int inflate_ready(ScmPort *port SCM_UNUSED) |
| 451 |
{ |
| 452 |
return 0; |
| 453 |
} |
| 454 |
|
| 455 |
ScmObj Scm_MakeInflatingPort(ScmPort *sink, ScmSize bufsiz, |
| 456 |
int window_bits, ScmObj dict, |
| 457 |
int ownerp) |
| 458 |
{ |
| 459 |
ScmZlibInfo *info = SCM_NEW(ScmZlibInfo); |
| 460 |
z_streamp strm = SCM_NEW_ATOMIC2(z_streamp, sizeof(z_stream)); |
| 461 |
|
| 462 |
bufsiz = fix_buffer_size(bufsiz); |
| 463 |
|
| 464 |
strm->zalloc = NULL; |
| 465 |
strm->zfree = NULL; |
| 466 |
strm->opaque = NULL; |
| 467 |
strm->next_in = NULL; |
| 468 |
strm->avail_in = 0; |
| 469 |
int r = inflateInit2(strm, window_bits); |
| 470 |
if (r != Z_OK) |
| 471 |
Scm_ZlibError(r, "inflateInit2 error: %s", strm->msg); |
| 472 |
|
| 473 |
if (!SCM_FALSEP(dict)) { |
| 474 |
if (!SCM_STRINGP(dict)) { |
| 475 |
Scm_Error("String required, but got %S", dict); |
| 476 |
} |
| 477 |
info->dict = (unsigned char*)SCM_STRING_START(dict); |
| 478 |
info->dictlen = SCM_STRING_SIZE(dict); |
| 479 |
} else { |
| 480 |
info->dict = NULL; |
| 481 |
info->dictlen = 0; |
| 482 |
} |
| 483 |
|
| 484 |
info->strm = strm; |
| 485 |
info->remote = sink; |
| 486 |
info->bufsiz = CHUNK; |
| 487 |
info->buf = SCM_NEW_ATOMIC2(char *, CHUNK); |
| 488 |
info->ptr = info->buf; |
| 489 |
info->ownerp = ownerp; |
| 490 |
info->stream_endp = FALSE; |
| 491 |
info->level = 0; |
| 492 |
info->strategy = 0; |
| 493 |
info->dict_adler = SCM_FALSE; |
| 494 |
|
| 495 |
ScmPortBuffer bufrec; |
| 496 |
memset(&bufrec, 0, sizeof(bufrec)); |
| 497 |
bufrec.size = info->bufsiz; |
| 498 |
bufrec.buffer = SCM_NEW_ATOMIC2(char *, info->bufsiz); |
| 499 |
bufrec.mode = SCM_PORT_BUFFER_FULL; |
| 500 |
bufrec.filler = inflate_filler; |
| 501 |
bufrec.flusher = NULL; |
| 502 |
bufrec.closer = inflate_closer; |
| 503 |
bufrec.ready = inflate_ready; |
| 504 |
bufrec.filenum = zlib_fileno; |
| 505 |
bufrec.data = (void*)info; |
| 506 |
|
| 507 |
ScmObj name = port_name("inflating", sink); |
| 508 |
return Scm_MakeBufferedPort(SCM_CLASS_INFLATING_PORT, name, |
| 509 |
SCM_PORT_INPUT, TRUE, &bufrec); |
| 510 |
} |
| 511 |
|
| 512 |
ScmObj Scm_InflateSync(ScmPort *port) |
| 513 |
{ |
| 514 |
ScmZlibInfo *info = SCM_PORT_ZLIB_INFO(port); |
| 515 |
z_streamp strm = SCM_PORT_ZSTREAM(port); |
| 516 |
unsigned char *outbuf = (u_char*)Scm_PortBufferStruct(port)->end; |
| 517 |
unsigned long curr_in = strm->total_in; |
| 518 |
|
| 519 |
if (info->stream_endp) return SCM_FALSE; |
| 520 |
|
| 521 |
int r; |
| 522 |
do { |
| 523 |
int nread = Scm_Getz(info->ptr, |
| 524 |
info->bufsiz - (info->ptr - info->buf), |
| 525 |
info->remote); |
| 526 |
if (nread <= 0) { |
| 527 |
/* input reached EOF */ |
| 528 |
if (info->ptr == info->buf) { |
| 529 |
info->stream_endp = TRUE; |
| 530 |
return SCM_FALSE; |
| 531 |
} |
| 532 |
strm->avail_in = info->ptr - info->buf; |
| 533 |
} else { |
| 534 |
strm->avail_in = (info->ptr + nread) - info->buf; |
| 535 |
} |
| 536 |
|
| 537 |
strm->next_in = (unsigned char*)info->buf; |
| 538 |
strm->next_out = outbuf; |
| 539 |
strm->avail_out = Scm_PortBufferRoom(port); |
| 540 |
|
| 541 |
r = inflateSync(strm); |
| 542 |
SCM_ASSERT(r != Z_STREAM_ERROR); |
| 543 |
if (strm->avail_in > 0) { |
| 544 |
memmove(info->buf, strm->next_in, strm->avail_in); |
| 545 |
info->ptr = info->buf + strm->avail_in; |
| 546 |
} else { |
| 547 |
info->ptr = info->buf; |
| 548 |
} |
| 549 |
strm->next_in = (unsigned char*)info->buf; |
| 550 |
} while (r != Z_OK); |
| 551 |
return Scm_MakeIntegerU(strm->total_in - curr_in); |
| 552 |
} |
| 553 |
|
| 554 |
/* |
| 555 |
* Module initialization function. |
| 556 |
*/ |
| 557 |
void Scm_Init_zlib(void) |
| 558 |
{ |
| 559 |
/* Create the module if it doesn't exist yet. */ |
| 560 |
ScmModule *mod = SCM_MODULE(SCM_FIND_MODULE("rfc.zlib", TRUE)); |
| 561 |
|
| 562 |
Scm_InitStaticClass(&Scm_DeflatingPortClass, "<deflating-port>", |
| 563 |
mod, NULL, 0); |
| 564 |
Scm_InitStaticClass(&Scm_InflatingPortClass, "<inflating-port>", |
| 565 |
mod, NULL, 0); |
| 566 |
|
| 567 |
ScmClass *cond_meta = Scm_ClassOf(SCM_OBJ(SCM_CLASS_CONDITION)); |
| 568 |
Scm_InitStaticClassWithMeta(SCM_CLASS_ZLIB_ERROR, |
| 569 |
"<zlib-error>", |
| 570 |
mod, cond_meta, SCM_FALSE, |
| 571 |
zliberror_slots, 0); |
| 572 |
Scm_InitStaticClassWithMeta(SCM_CLASS_ZLIB_NEED_DICT_ERROR, |
| 573 |
"<zlib-need-dict-error>", |
| 574 |
mod, cond_meta, SCM_FALSE, |
| 575 |
zliberror_slots, 0); |
| 576 |
Scm_InitStaticClassWithMeta(SCM_CLASS_ZLIB_STREAM_ERROR, |
| 577 |
"<zlib-stream-error>", |
| 578 |
mod, cond_meta, SCM_FALSE, |
| 579 |
zliberror_slots, 0); |
| 580 |
Scm_InitStaticClassWithMeta(SCM_CLASS_ZLIB_DATA_ERROR, |
| 581 |
"<zlib-data-error>", |
| 582 |
mod, cond_meta, SCM_FALSE, |
| 583 |
zliberror_slots, 0); |
| 584 |
Scm_InitStaticClassWithMeta(SCM_CLASS_ZLIB_MEMORY_ERROR, |
| 585 |
"<zlib-memory-error>", |
| 586 |
mod, cond_meta, SCM_FALSE, |
| 587 |
zliberror_slots, 0); |
| 588 |
Scm_InitStaticClassWithMeta(SCM_CLASS_ZLIB_VERSION_ERROR, |
| 589 |
"<zlib-version-error>", |
| 590 |
mod, cond_meta, SCM_FALSE, |
| 591 |
zliberror_slots, 0); |
| 592 |
} |