Debian Codegraph @

generic/tclZlib.c

raw
1 /*
2 * tclZlib.c --
3 *
4 * This file provides the interface to the Zlib library.
5 *
6 * Copyright (C) 2004-2005 Pascal Scheffers <[email protected]>
7 * Copyright (C) 2005 Unitas Software B.V.
8 * Copyright (c) 2008-2012 Donal K. Fellows
9 *
10 * Parts written by Jean-Claude Wippler, as part of Tclkit, placed in the
11 * public domain March 2003.
12 *
13 * See the file "license.terms" for information on usage and redistribution of
14 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
15 */
16
17 #include "tclInt.h"
18 #ifdef HAVE_ZLIB
19 #include <zlib.h>
20 #include "tclIO.h"
21
22 /*
23 * The version of the zlib "package" that this implements. Note that this
24 * thoroughly supersedes the versions included with tclkit, which are "1.1",
25 * so this is at least "2.0" (there's no general *commitment* to have the same
26 * interface, even if that is mostly true).
27 */
28
29 #define TCL_ZLIB_VERSION "2.0.1"
30
31 /*
32 * Magic flags used with wbits fields to indicate that we're handling the gzip
33 * format or automatic detection of format. Putting it here is slightly less
34 * gross!
35 */
36
37 #define WBITS_RAW (-MAX_WBITS)
38 #define WBITS_ZLIB (MAX_WBITS)
39 #define WBITS_GZIP (MAX_WBITS | 16)
40 #define WBITS_AUTODETECT (MAX_WBITS | 32)
41
42 /*
43 * Structure used for handling gzip headers that are generated from a
44 * dictionary. It comprises the header structure itself plus some working
45 * space that it is very convenient to have attached.
46 */
47
48 #define MAX_COMMENT_LEN 256
49
50 typedef struct {
51 gz_header header;
52 char nativeFilenameBuf[MAXPATHLEN];
53 char nativeCommentBuf[MAX_COMMENT_LEN];
54 } GzipHeader;
55
56 /*
57 * Structure used for the Tcl_ZlibStream* commands and [zlib stream ...]
58 */
59
60 typedef struct {
61 Tcl_Interp *interp;
62 z_stream stream; /* The interface to the zlib library. */
63 int streamEnd; /* If we've got to end-of-stream. */
64 Tcl_Obj *inData, *outData; /* Input / output buffers (lists) */
65 Tcl_Obj *currentInput; /* Pointer to what is currently being
66 * inflated. */
67 int outPos;
68 int mode; /* Either TCL_ZLIB_STREAM_DEFLATE or
69 * TCL_ZLIB_STREAM_INFLATE. */
70 int format; /* Flags from the TCL_ZLIB_FORMAT_* */
71 int level; /* Default 5, 0-9 */
72 int flush; /* Stores the flush param for deferred the
73 * decompression. */
74 int wbits; /* The encoded compression mode, so we can
75 * restart the stream if necessary. */
76 Tcl_Command cmd; /* Token for the associated Tcl command. */
77 Tcl_Obj *compDictObj; /* Byte-array object containing compression
78 * dictionary (not dictObj!) to use if
79 * necessary. */
80 int flags; /* Miscellaneous flag bits. */
81 GzipHeader *gzHeaderPtr; /* If we've allocated a gzip header
82 * structure. */
83 } ZlibStreamHandle;
84
85 #define DICT_TO_SET 0x1 /* If we need to set a compression dictionary
86 * in the low-level engine at the next
87 * opportunity. */
88
89 /*
90 * Macros to make it clearer in some of the twiddlier accesses what is
91 * happening.
92 */
93
94 #define IsRawStream(zshPtr) ((zshPtr)->format == TCL_ZLIB_FORMAT_RAW)
95 #define HaveDictToSet(zshPtr) ((zshPtr)->flags & DICT_TO_SET)
96 #define DictWasSet(zshPtr) ((zshPtr)->flags |= ~DICT_TO_SET)
97
98 /*
99 * Structure used for stacked channel compression and decompression.
100 */
101
102 typedef struct {
103 Tcl_Channel chan; /* Reference to the channel itself. */
104 Tcl_Channel parent; /* The underlying source and sink of bytes. */
105 int flags; /* General flag bits, see below... */
106 int mode; /* Either the value TCL_ZLIB_STREAM_DEFLATE
107 * for compression on output, or
108 * TCL_ZLIB_STREAM_INFLATE for decompression
109 * on input. */
110 int format; /* What format of data is going on the wire.
111 * Needed so that the correct [fconfigure]
112 * options can be enabled. */
113 int readAheadLimit; /* The maximum number of bytes to read from
114 * the underlying stream in one go. */
115 z_stream inStream; /* Structure used by zlib for decompression of
116 * input. */
117 z_stream outStream; /* Structure used by zlib for compression of
118 * output. */
119 char *inBuffer, *outBuffer; /* Working buffers. */
120 int inAllocated, outAllocated;
121 /* Sizes of working buffers. */
122 GzipHeader inHeader; /* Header read from input stream, when
123 * decompressing a gzip stream. */
124 GzipHeader outHeader; /* Header to write to an output stream, when
125 * compressing a gzip stream. */
126 Tcl_TimerToken timer; /* Timer used for keeping events fresh. */
127 Tcl_Obj *compDictObj; /* Byte-array object containing compression
128 * dictionary (not dictObj!) to use if
129 * necessary. */
130 } ZlibChannelData;
131
132 /*
133 * Value bits for the flags field. Definitions are:
134 * ASYNC - Whether this is an asynchronous channel.
135 * IN_HEADER - Whether the inHeader field has been registered with
136 * the input compressor.
137 * OUT_HEADER - Whether the outputHeader field has been registered
138 * with the output decompressor.
139 * STREAM_DECOMPRESS - Signal decompress pending data.
140 * STREAM_DONE - Flag to signal stream end up to transform input.
141 */
142
143 #define ASYNC 0x01
144 #define IN_HEADER 0x02
145 #define OUT_HEADER 0x04
146 #define STREAM_DECOMPRESS 0x08
147 #define STREAM_DONE 0x10
148
149 /*
150 * Size of buffers allocated by default, and the range it can be set to. The
151 * same sorts of values apply to streams, except with different limits (they
152 * permit byte-level activity). Channels always use bytes unless told to use
153 * larger buffers.
154 */
155
156 #define DEFAULT_BUFFER_SIZE 4096
157 #define MIN_NONSTREAM_BUFFER_SIZE 16
158 #define MAX_BUFFER_SIZE 65536
159
160 /*
161 * Prototypes for private procedures defined later in this file:
162 */
163
164 static Tcl_CmdDeleteProc ZlibStreamCmdDelete;
165 static Tcl_DriverBlockModeProc ZlibTransformBlockMode;
166 static Tcl_DriverCloseProc ZlibTransformClose;
167 static Tcl_DriverGetHandleProc ZlibTransformGetHandle;
168 static Tcl_DriverGetOptionProc ZlibTransformGetOption;
169 static Tcl_DriverHandlerProc ZlibTransformEventHandler;
170 static Tcl_DriverInputProc ZlibTransformInput;
171 static Tcl_DriverOutputProc ZlibTransformOutput;
172 static Tcl_DriverSetOptionProc ZlibTransformSetOption;
173 static Tcl_DriverWatchProc ZlibTransformWatch;
174 static Tcl_ObjCmdProc ZlibCmd;
175 static Tcl_ObjCmdProc ZlibStreamCmd;
176 static Tcl_ObjCmdProc ZlibStreamAddCmd;
177 static Tcl_ObjCmdProc ZlibStreamHeaderCmd;
178 static Tcl_ObjCmdProc ZlibStreamPutCmd;
179
180 static void ConvertError(Tcl_Interp *interp, int code,
181 uLong adler);
182 static Tcl_Obj * ConvertErrorToList(int code, uLong adler);
183 static inline int Deflate(z_streamp strm, void *bufferPtr,
184 int bufferSize, int flush, int *writtenPtr);
185 static void ExtractHeader(gz_header *headerPtr, Tcl_Obj *dictObj);
186 static int GenerateHeader(Tcl_Interp *interp, Tcl_Obj *dictObj,
187 GzipHeader *headerPtr, int *extraSizePtr);
188 static int ZlibPushSubcmd(Tcl_Interp *interp, int objc,
189 Tcl_Obj *const objv[]);
190 static int ResultDecompress(ZlibChannelData *cd, char *buf,
191 int toRead, int flush, int *errorCodePtr);
192 static Tcl_Channel ZlibStackChannelTransform(Tcl_Interp *interp,
193 int mode, int format, int level, int limit,
194 Tcl_Channel channel, Tcl_Obj *gzipHeaderDictPtr,
195 Tcl_Obj *compDictObj);
196 static void ZlibStreamCleanup(ZlibStreamHandle *zshPtr);
197 static int ZlibStreamSubcmd(Tcl_Interp *interp, int objc,
198 Tcl_Obj *const objv[]);
199 static inline void ZlibTransformEventTimerKill(ZlibChannelData *cd);
200 static void ZlibTransformTimerRun(ClientData clientData);
201
202 /*
203 * Type of zlib-based compressing and decompressing channels.
204 */
205
206 static const Tcl_ChannelType zlibChannelType = {
207 "zlib",
208 TCL_CHANNEL_VERSION_5,
209 ZlibTransformClose,
210 ZlibTransformInput,
211 ZlibTransformOutput,
212 NULL, /* seekProc */
213 ZlibTransformSetOption,
214 ZlibTransformGetOption,
215 ZlibTransformWatch,
216 ZlibTransformGetHandle,
217 NULL, /* close2Proc */
218 ZlibTransformBlockMode,
219 NULL, /* flushProc */
220 ZlibTransformEventHandler,
221 NULL, /* wideSeekProc */
222 NULL,
223 NULL
224 };
225
226 /*
227 *----------------------------------------------------------------------
228 *
229 * ConvertError --
230 *
231 * Utility function for converting a zlib error into a Tcl error.
232 *
233 * Results:
234 * None.
235 *
236 * Side effects:
237 * Updates the interpreter result and errorcode.
238 *
239 *----------------------------------------------------------------------
240 */
241
242 static void
243 ConvertError(
244 Tcl_Interp *interp, /* Interpreter to store the error in. May be
245 * NULL, in which case nothing happens. */
246 int code, /* The zlib error code. */
247 uLong adler) /* The checksum expected (for Z_NEED_DICT) */
248 {
249 const char *codeStr, *codeStr2 = NULL;
250 char codeStrBuf[TCL_INTEGER_SPACE];
251
252 if (interp == NULL) {
253 return;
254 }
255
256 switch (code) {
257 /*
258 * Firstly, the case that is *different* because it's really coming
259 * from the OS and is just being reported via zlib. It should be
260 * really uncommon because Tcl handles all I/O rather than delegating
261 * it to zlib, but proving it can't happen is hard.
262 */
263
264 case Z_ERRNO:
265 Tcl_SetObjResult(interp, Tcl_NewStringObj(Tcl_PosixError(interp),-1));
266 return;
267
268 /*
269 * Normal errors/conditions, some of which have additional detail and
270 * some which don't. (This is not defined by array lookup because zlib
271 * error codes are sometimes negative.)
272 */
273
274 case Z_STREAM_ERROR:
275 codeStr = "STREAM";
276 break;
277 case Z_DATA_ERROR:
278 codeStr = "DATA";
279 break;
280 case Z_MEM_ERROR:
281 codeStr = "MEM";
282 break;
283 case Z_BUF_ERROR:
284 codeStr = "BUF";
285 break;
286 case Z_VERSION_ERROR:
287 codeStr = "VERSION";
288 break;
289 case Z_NEED_DICT:
290 codeStr = "NEED_DICT";
291 codeStr2 = codeStrBuf;
292 snprintf(codeStrBuf, sizeof(codeStrBuf), "%lu", adler);
293 break;
294
295 /*
296 * These should _not_ happen! This function is for dealing with error
297 * cases, not non-errors!
298 */
299
300 case Z_OK:
301 Tcl_Panic("unexpected zlib result in error handler: Z_OK");
302 case Z_STREAM_END:
303 Tcl_Panic("unexpected zlib result in error handler: Z_STREAM_END");
304
305 /*
306 * Anything else is bad news; it's unexpected. Convert to generic
307 * error.
308 */
309
310 default:
311 codeStr = "UNKNOWN";
312 codeStr2 = codeStrBuf;
313 snprintf(codeStrBuf, sizeof(codeStrBuf), "%d", code);
314 break;
315 }
316 Tcl_SetObjResult(interp, Tcl_NewStringObj(zError(code), -1));
317
318 /*
319 * Tricky point! We might pass NULL twice here (and will when the error
320 * type is known).
321 */
322
323 Tcl_SetErrorCode(interp, "TCL", "ZLIB", codeStr, codeStr2, (char *)NULL);
324 }
325
326 static Tcl_Obj *
327 ConvertErrorToList(
328 int code, /* The zlib error code. */
329 uLong adler) /* The checksum expected (for Z_NEED_DICT) */
330 {
331 Tcl_Obj *objv[4];
332
333 TclNewLiteralStringObj(objv[0], "TCL");
334 TclNewLiteralStringObj(objv[1], "ZLIB");
335 switch (code) {
336 case Z_STREAM_ERROR:
337 TclNewLiteralStringObj(objv[2], "STREAM");
338 return Tcl_NewListObj(3, objv);
339 case Z_DATA_ERROR:
340 TclNewLiteralStringObj(objv[2], "DATA");
341 return Tcl_NewListObj(3, objv);
342 case Z_MEM_ERROR:
343 TclNewLiteralStringObj(objv[2], "MEM");
344 return Tcl_NewListObj(3, objv);
345 case Z_BUF_ERROR:
346 TclNewLiteralStringObj(objv[2], "BUF");
347 return Tcl_NewListObj(3, objv);
348 case Z_VERSION_ERROR:
349 TclNewLiteralStringObj(objv[2], "VERSION");
350 return Tcl_NewListObj(3, objv);
351 case Z_ERRNO:
352 TclNewLiteralStringObj(objv[2], "POSIX");
353 objv[3] = Tcl_NewStringObj(Tcl_ErrnoId(), -1);
354 return Tcl_NewListObj(4, objv);
355 case Z_NEED_DICT:
356 TclNewLiteralStringObj(objv[2], "NEED_DICT");
357 objv[3] = Tcl_NewWideIntObj((Tcl_WideInt)adler);
358 return Tcl_NewListObj(4, objv);
359
360 /*
361 * These should _not_ happen! This function is for dealing with error
362 * cases, not non-errors!
363 */
364
365 case Z_OK:
366 Tcl_Panic("unexpected zlib result in error handler: Z_OK");
367 case Z_STREAM_END:
368 Tcl_Panic("unexpected zlib result in error handler: Z_STREAM_END");
369
370 /*
371 * Catch-all. Should be unreachable because all cases are already
372 * listed above.
373 */
374
375 default:
376 TclNewLiteralStringObj(objv[2], "UNKNOWN");
377 TclNewIntObj(objv[3], code);
378 return Tcl_NewListObj(4, objv);
379 }
380 }
381
382 /*
383 *----------------------------------------------------------------------
384 *
385 * GenerateHeader --
386 *
387 * Function for creating a gzip header from the contents of a dictionary
388 * (as described in the documentation).
389 *
390 * Results:
391 * A Tcl result code.
392 *
393 * Side effects:
394 * Updates the fields of the given gz_header structure. Adds amount of
395 * extra space required for the header to the variable referenced by the
396 * extraSizePtr argument.
397 *
398 *----------------------------------------------------------------------
399 */
400
401 static int
402 GenerateHeader(
403 Tcl_Interp *interp, /* Where to put error messages. */
404 Tcl_Obj *dictObj, /* The dictionary whose contents are to be
405 * parsed. */
406 GzipHeader *headerPtr, /* Where to store the parsed-out values. */
407 int *extraSizePtr) /* Variable to add the length of header
408 * strings (filename, comment) to. */
409 {
410 Tcl_Obj *value;
411 int len, result = TCL_ERROR;
412 const char *valueStr;
413 Tcl_Encoding latin1enc;
414 static const char *const types[] = {
415 "binary", "text"
416 };
417
418 /*
419 * RFC 1952 says that header strings are in ISO 8859-1 (LATIN-1).
420 */
421
422 latin1enc = Tcl_GetEncoding(NULL, "iso8859-1");
423 if (latin1enc == NULL) {
424 Tcl_Panic("no latin-1 encoding");
425 }
426
427 if (TclDictGet(interp, dictObj, "comment", &value) != TCL_OK) {
428 goto error;
429 } else if (value != NULL) {
430 Tcl_EncodingState state;
431 valueStr = TclGetStringFromObj(value, &len);
432 result = Tcl_UtfToExternal(NULL, latin1enc, valueStr, len,
433 TCL_ENCODING_START|TCL_ENCODING_END|TCL_ENCODING_STOPONERROR, &state,
434 headerPtr->nativeCommentBuf, MAX_COMMENT_LEN-1, NULL, &len,
435 NULL);
436 if (result != TCL_OK) {
437 if (interp) {
438 if (result == TCL_CONVERT_UNKNOWN) {
439 Tcl_AppendResult(interp, "Comment contains characters > 0xFF", (char *)NULL);
440 } else {
441 Tcl_AppendResult(interp, "Comment too large for zip", (char *)NULL);
442 }
443 }
444 result = TCL_ERROR; /* TCL_CONVERT_* -> TCL_ERROR*/
445 goto error;
446 }
447 headerPtr->nativeCommentBuf[len] = '\0';
448 headerPtr->header.comment = (Bytef *) headerPtr->nativeCommentBuf;
449 if (extraSizePtr != NULL) {
450 *extraSizePtr += len;
451 }
452 }
453
454 if (TclDictGet(interp, dictObj, "crc", &value) != TCL_OK) {
455 goto error;
456 } else if (value != NULL &&
457 Tcl_GetBooleanFromObj(interp, value, &headerPtr->header.hcrc)) {
458 goto error;
459 }
460
461 if (TclDictGet(interp, dictObj, "filename", &value) != TCL_OK) {
462 goto error;
463 } else if (value != NULL) {
464 Tcl_EncodingState state;
465 valueStr = TclGetStringFromObj(value, &len);
466 result = Tcl_UtfToExternal(NULL, latin1enc, valueStr, len,
467 TCL_ENCODING_START|TCL_ENCODING_END|TCL_ENCODING_STOPONERROR, &state,
468 headerPtr->nativeFilenameBuf, MAXPATHLEN-1, NULL, &len,
469 NULL);
470 if (result != TCL_OK) {
471 if (interp) {
472 if (result == TCL_CONVERT_UNKNOWN) {
473 Tcl_AppendResult(interp, "Filename contains characters > 0xFF", (char *)NULL);
474 } else {
475 Tcl_AppendResult(interp, "Filename too large for zip", (char *)NULL);
476 }
477 }
478 result = TCL_ERROR; /* TCL_CONVERT_* -> TCL_ERROR*/
479 goto error;
480 }
481 headerPtr->nativeFilenameBuf[len] = '\0';
482 headerPtr->header.name = (Bytef *) headerPtr->nativeFilenameBuf;
483 if (extraSizePtr != NULL) {
484 *extraSizePtr += len;
485 }
486 }
487
488 if (TclDictGet(interp, dictObj, "os", &value) != TCL_OK) {
489 goto error;
490 } else if (value != NULL && Tcl_GetIntFromObj(interp, value,
491 &headerPtr->header.os) != TCL_OK) {
492 goto error;
493 }
494
495 /*
496 * Ignore the 'size' field, since that is controlled by the size of the
497 * input data.
498 */
499
500 if (TclDictGet(interp, dictObj, "time", &value) != TCL_OK) {
501 goto error;
502 } else if (value != NULL && Tcl_GetLongFromObj(interp, value,
503 (long *) &headerPtr->header.time) != TCL_OK) {
504 goto error;
505 }
506
507 if (TclDictGet(interp, dictObj, "type", &value) != TCL_OK) {
508 goto error;
509 } else if (value != NULL && Tcl_GetIndexFromObj(interp, value, types,
510 "type", TCL_EXACT, &headerPtr->header.text) != TCL_OK) {
511 goto error;
512 }
513
514 result = TCL_OK;
515 error:
516 Tcl_FreeEncoding(latin1enc);
517 return result;
518 }
519
520 /*
521 *----------------------------------------------------------------------
522 *
523 * ExtractHeader --
524 *
525 * Take the values out of a gzip header and store them in a dictionary.
526 * SetValue is a helper macro.
527 *
528 * Results:
529 * None.
530 *
531 * Side effects:
532 * Updates the dictionary, which must be writable (i.e. refCount < 2).
533 *
534 *----------------------------------------------------------------------
535 */
536
537 static void
538 ExtractHeader(
539 gz_header *headerPtr, /* The gzip header to extract from. */
540 Tcl_Obj *dictObj) /* The dictionary to store in. */
541 {
542 Tcl_Encoding latin1enc = NULL;
543 Tcl_DString tmp;
544
545 if (headerPtr->comment != Z_NULL) {
546 if (latin1enc == NULL) {
547 /*
548 * RFC 1952 says that header strings are in ISO 8859-1 (LATIN-1).
549 */
550
551 latin1enc = Tcl_GetEncoding(NULL, "iso8859-1");
552 if (latin1enc == NULL) {
553 Tcl_Panic("no latin-1 encoding");
554 }
555 }
556
557 Tcl_ExternalToUtfDString(latin1enc, (char *) headerPtr->comment, -1,
558 &tmp);
559 TclDictPut(NULL, dictObj, "comment", TclDStringToObj(&tmp));
560 }
561 TclDictPut(NULL, dictObj, "crc", Tcl_NewBooleanObj(headerPtr->hcrc));
562 if (headerPtr->name != Z_NULL) {
563 if (latin1enc == NULL) {
564 /*
565 * RFC 1952 says that header strings are in ISO 8859-1 (LATIN-1).
566 */
567
568 latin1enc = Tcl_GetEncoding(NULL, "iso8859-1");
569 if (latin1enc == NULL) {
570 Tcl_Panic("no latin-1 encoding");
571 }
572 }
573
574 Tcl_ExternalToUtfDString(latin1enc, (char *) headerPtr->name, -1,
575 &tmp);
576 TclDictPut(NULL, dictObj, "filename", TclDStringToObj(&tmp));
577 }
578 if (headerPtr->os != 255) {
579 TclDictPut(NULL, dictObj, "os", Tcl_NewIntObj(headerPtr->os));
580 }
581 if (headerPtr->time != 0 /* magic - no time */) {
582 TclDictPut(NULL, dictObj, "time",
583 Tcl_NewLongObj((long) headerPtr->time));
584 }
585 if (headerPtr->text != Z_UNKNOWN) {
586 TclDictPutString(NULL, dictObj, "type",
587 headerPtr->text ? "text" : "binary");
588 }
589
590 if (latin1enc != NULL) {
591 Tcl_FreeEncoding(latin1enc);
592 }
593 }
594
595 /*
596 * Disentangle the worst of how the zlib API is used.
597 */
598
599 static int
600 SetInflateDictionary(
601 z_streamp strm,
602 Tcl_Obj *compDictObj)
603 {
604 if (compDictObj != NULL) {
605 int length;
606 unsigned char *bytes = Tcl_GetByteArrayFromObj(compDictObj, &length);
607
608 return inflateSetDictionary(strm, bytes, length);
609 }
610 return Z_OK;
611 }
612
613 static int
614 SetDeflateDictionary(
615 z_streamp strm,
616 Tcl_Obj *compDictObj)
617 {
618 if (compDictObj != NULL) {
619 int length;
620 unsigned char *bytes = Tcl_GetByteArrayFromObj(compDictObj, &length);
621
622 return deflateSetDictionary(strm, bytes, length);
623 }
624 return Z_OK;
625 }
626
627 static inline int
628 Deflate(
629 z_streamp strm,
630 void *bufferPtr,
631 int bufferSize,
632 int flush,
633 int *writtenPtr)
634 {
635 int e;
636
637 strm->next_out = (Bytef *) bufferPtr;
638 strm->avail_out = bufferSize;
639 e = deflate(strm, flush);
640 if (writtenPtr != NULL) {
641 *writtenPtr = bufferSize - strm->avail_out;
642 }
643 return e;
644 }
645
646 static inline void
647 AppendByteArray(
648 Tcl_Obj *listObj,
649 void *buffer,
650 int size)
651 {
652 if (size > 0) {
653 Tcl_Obj *baObj = Tcl_NewByteArrayObj((unsigned char *) buffer, size);
654
655 Tcl_ListObjAppendElement(NULL, listObj, baObj);
656 }
657 }
658
659 /*
660 *----------------------------------------------------------------------
661 *
662 * Tcl_ZlibStreamInit --
663 *
664 * This command initializes a (de)compression context/handle for
665 * (de)compressing data in chunks.
666 *
667 * Results:
668 * A standard Tcl result.
669 *
670 * Side effects:
671 * The variable pointed to by zshandlePtr is initialised and memory
672 * allocated for internal state. Additionally, if interp is not null, a
673 * Tcl command is created and its name placed in the interp result obj.
674 *
675 * Note:
676 * At least one of interp and zshandlePtr should be non-NULL or the
677 * reference to the stream will be completely lost.
678 *
679 *----------------------------------------------------------------------
680 */
681
682 int
683 Tcl_ZlibStreamInit(
684 Tcl_Interp *interp,
685 int mode, /* Either TCL_ZLIB_STREAM_INFLATE or
686 * TCL_ZLIB_STREAM_DEFLATE. */
687 int format, /* Flags from the TCL_ZLIB_FORMAT_* set. */
688 int level, /* 0-9 or TCL_ZLIB_COMPRESS_DEFAULT. */
689 Tcl_Obj *dictObj, /* Dictionary containing headers for gzip. */
690 Tcl_ZlibStream *zshandlePtr)
691 {
692 int wbits = 0;
693 int e;
694 ZlibStreamHandle *zshPtr = NULL;
695 Tcl_DString cmdname;
696 GzipHeader *gzHeaderPtr = NULL;
697
698 switch (mode) {
699 case TCL_ZLIB_STREAM_DEFLATE:
700 /*
701 * Compressed format is specified by the wbits parameter. See zlib.h
702 * for details.
703 */
704
705 switch (format) {
706 case TCL_ZLIB_FORMAT_RAW:
707 wbits = WBITS_RAW;
708 break;
709 case TCL_ZLIB_FORMAT_GZIP:
710 wbits = WBITS_GZIP;
711 if (dictObj) {
712 gzHeaderPtr = (GzipHeader *)ckalloc(sizeof(GzipHeader));
713 memset(gzHeaderPtr, 0, sizeof(GzipHeader));
714 if (GenerateHeader(interp, dictObj, gzHeaderPtr,
715 NULL) != TCL_OK) {
716 ckfree(gzHeaderPtr);
717 return TCL_ERROR;
718 }
719 }
720 break;
721 case TCL_ZLIB_FORMAT_ZLIB:
722 wbits = WBITS_ZLIB;
723 break;
724 default:
725 Tcl_Panic("incorrect zlib data format, must be "
726 "TCL_ZLIB_FORMAT_ZLIB, TCL_ZLIB_FORMAT_GZIP or "
727 "TCL_ZLIB_FORMAT_RAW");
728 }
729 if (level < -1 || level > 9) {
730 Tcl_Panic("compression level should be between 0 (no compression)"
731 " and 9 (best compression) or -1 for default compression "
732 "level");
733 }
734 break;
735 case TCL_ZLIB_STREAM_INFLATE:
736 /*
737 * wbits are the same as DEFLATE, but FORMAT_AUTO is valid too.
738 */
739
740 switch (format) {
741 case TCL_ZLIB_FORMAT_RAW:
742 wbits = WBITS_RAW;
743 break;
744 case TCL_ZLIB_FORMAT_GZIP:
745 wbits = WBITS_GZIP;
746 gzHeaderPtr = (GzipHeader *)ckalloc(sizeof(GzipHeader));
747 memset(gzHeaderPtr, 0, sizeof(GzipHeader));
748 gzHeaderPtr->header.name = (Bytef *)
749 gzHeaderPtr->nativeFilenameBuf;
750 gzHeaderPtr->header.name_max = MAXPATHLEN - 1;
751 gzHeaderPtr->header.comment = (Bytef *)
752 gzHeaderPtr->nativeCommentBuf;
753 gzHeaderPtr->header.name_max = MAX_COMMENT_LEN - 1;
754 break;
755 case TCL_ZLIB_FORMAT_ZLIB:
756 wbits = WBITS_ZLIB;
757 break;
758 case TCL_ZLIB_FORMAT_AUTO:
759 wbits = WBITS_AUTODETECT;
760 break;
761 default:
762 Tcl_Panic("incorrect zlib data format, must be "
763 "TCL_ZLIB_FORMAT_ZLIB, TCL_ZLIB_FORMAT_GZIP, "
764 "TCL_ZLIB_FORMAT_RAW or TCL_ZLIB_FORMAT_AUTO");
765 }
766 break;
767 default:
768 Tcl_Panic("bad mode, must be TCL_ZLIB_STREAM_DEFLATE or"
769 " TCL_ZLIB_STREAM_INFLATE");
770 }
771
772 zshPtr = (ZlibStreamHandle *)ckalloc(sizeof(ZlibStreamHandle));
773 zshPtr->interp = interp;
774 zshPtr->mode = mode;
775 zshPtr->format = format;
776 zshPtr->level = level;
777 zshPtr->wbits = wbits;
778 zshPtr->currentInput = NULL;
779 zshPtr->streamEnd = 0;
780 zshPtr->compDictObj = NULL;
781 zshPtr->flags = 0;
782 zshPtr->gzHeaderPtr = gzHeaderPtr;
783 memset(&zshPtr->stream, 0, sizeof(z_stream));
784 zshPtr->stream.adler = 1;
785
786 /*
787 * No output buffer available yet
788 */
789
790 if (mode == TCL_ZLIB_STREAM_DEFLATE) {
791 e = deflateInit2(&zshPtr->stream, level, Z_DEFLATED, wbits,
792 MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
793 if (e == Z_OK && zshPtr->gzHeaderPtr) {
794 e = deflateSetHeader(&zshPtr->stream,
795 &zshPtr->gzHeaderPtr->header);
796 }
797 } else {
798 e = inflateInit2(&zshPtr->stream, wbits);
799 if (e == Z_OK && zshPtr->gzHeaderPtr) {
800 e = inflateGetHeader(&zshPtr->stream,
801 &zshPtr->gzHeaderPtr->header);
802 }
803 }
804
805 if (e != Z_OK) {
806 ConvertError(interp, e, zshPtr->stream.adler);
807 goto error;
808 }
809
810 /*
811 * I could do all this in C, but this is easier.
812 */
813
814 if (interp != NULL) {
815 if (Tcl_EvalEx(interp, "::incr ::tcl::zlib::cmdcounter", -1, 0) != TCL_OK) {
816 goto error;
817 }
818 Tcl_DStringInit(&cmdname);
819 TclDStringAppendLiteral(&cmdname, "::tcl::zlib::streamcmd_");
820 TclDStringAppendObj(&cmdname, Tcl_GetObjResult(interp));
821 if (Tcl_FindCommand(interp, Tcl_DStringValue(&cmdname),
822 NULL, 0) != NULL) {
823 Tcl_SetObjResult(interp, Tcl_NewStringObj(
824 "BUG: Stream command name already exists", -1));
825 Tcl_SetErrorCode(interp, "TCL", "BUG", "EXISTING_CMD", (char *)NULL);
826 Tcl_DStringFree(&cmdname);
827 goto error;
828 }
829 Tcl_ResetResult(interp);
830
831 /*
832 * Create the command.
833 */
834
835 zshPtr->cmd = Tcl_CreateObjCommand(interp, Tcl_DStringValue(&cmdname),
836 ZlibStreamCmd, zshPtr, ZlibStreamCmdDelete);
837 Tcl_DStringFree(&cmdname);
838 if (zshPtr->cmd == NULL) {
839 goto error;
840 }
841 } else {
842 zshPtr->cmd = NULL;
843 }
844
845 /*
846 * Prepare the buffers for use.
847 */
848
849 zshPtr->inData = Tcl_NewListObj(0, NULL);
850 Tcl_IncrRefCount(zshPtr->inData);
851 zshPtr->outData = Tcl_NewListObj(0, NULL);
852 Tcl_IncrRefCount(zshPtr->outData);
853
854 zshPtr->outPos = 0;
855
856 /*
857 * Now set the variable pointed to by *zshandlePtr to the pointer to the
858 * zsh struct.
859 */
860
861 if (zshandlePtr) {
862 *zshandlePtr = (Tcl_ZlibStream) zshPtr;
863 }
864
865 return TCL_OK;
866
867 error:
868 if (zshPtr->compDictObj) {
869 Tcl_DecrRefCount(zshPtr->compDictObj);
870 }
871 if (zshPtr->gzHeaderPtr) {
872 ckfree(zshPtr->gzHeaderPtr);
873 }
874 ckfree(zshPtr);
875 return TCL_ERROR;
876 }
877
878 /*
879 *----------------------------------------------------------------------
880 *
881 * ZlibStreamCmdDelete --
882 *
883 * This is the delete command which Tcl invokes when a zlibstream command
884 * is deleted from the interpreter (on stream close, usually).
885 *
886 * Results:
887 * None
888 *
889 * Side effects:
890 * Invalidates the zlib stream handle as obtained from Tcl_ZlibStreamInit
891 *
892 *----------------------------------------------------------------------
893 */
894
895 static void
896 ZlibStreamCmdDelete(
897 ClientData cd)
898 {
899 ZlibStreamHandle *zshPtr = (ZlibStreamHandle *)cd;
900
901 zshPtr->cmd = NULL;
902 ZlibStreamCleanup(zshPtr);
903 }
904
905 /*
906 *----------------------------------------------------------------------
907 *
908 * Tcl_ZlibStreamClose --
909 *
910 * This procedure must be called after (de)compression is done to ensure
911 * memory is freed and the command is deleted from the interpreter (if
912 * any).
913 *
914 * Results:
915 * A standard Tcl result.
916 *
917 * Side effects:
918 * Invalidates the zlib stream handle as obtained from Tcl_ZlibStreamInit
919 *
920 *----------------------------------------------------------------------
921 */
922
923 int
924 Tcl_ZlibStreamClose(
925 Tcl_ZlibStream zshandle) /* As obtained from Tcl_ZlibStreamInit. */
926 {
927 ZlibStreamHandle *zshPtr = (ZlibStreamHandle *) zshandle;
928
929 /*
930 * If the interp is set, deleting the command will trigger
931 * ZlibStreamCleanup in ZlibStreamCmdDelete. If no interp is set, call
932 * ZlibStreamCleanup directly.
933 */
934
935 if (zshPtr->interp && zshPtr->cmd) {
936 Tcl_DeleteCommandFromToken(zshPtr->interp, zshPtr->cmd);
937 } else {
938 ZlibStreamCleanup(zshPtr);
939 }
940 return TCL_OK;
941 }
942
943 /*
944 *----------------------------------------------------------------------
945 *
946 * ZlibStreamCleanup --
947 *
948 * This procedure is called by either Tcl_ZlibStreamClose or
949 * ZlibStreamCmdDelete to cleanup the stream context.
950 *
951 * Results:
952 * None
953 *
954 * Side effects:
955 * Invalidates the zlib stream handle.
956 *
957 *----------------------------------------------------------------------
958 */
959
960 void
961 ZlibStreamCleanup(
962 ZlibStreamHandle *zshPtr)
963 {
964 if (!zshPtr->streamEnd) {
965 if (zshPtr->mode == TCL_ZLIB_STREAM_DEFLATE) {
966 deflateEnd(&zshPtr->stream);
967 } else {
968 inflateEnd(&zshPtr->stream);
969 }
970 }
971
972 if (zshPtr->inData) {
973 Tcl_DecrRefCount(zshPtr->inData);
974 }
975 if (zshPtr->outData) {
976 Tcl_DecrRefCount(zshPtr->outData);
977 }
978 if (zshPtr->currentInput) {
979 Tcl_DecrRefCount(zshPtr->currentInput);
980 }
981 if (zshPtr->compDictObj) {
982 Tcl_DecrRefCount(zshPtr->compDictObj);
983 }
984 if (zshPtr->gzHeaderPtr) {
985 ckfree(zshPtr->gzHeaderPtr);
986 }
987
988 ckfree(zshPtr);
989 }
990
991 /*
992 *----------------------------------------------------------------------
993 *
994 * Tcl_ZlibStreamReset --
995 *
996 * This procedure will reinitialize an existing stream handle.
997 *
998 * Results:
999 * A standard Tcl result.
1000 *
1001 * Side effects:
1002 * Any data left in the (de)compression buffer is lost.
1003 *
1004 *----------------------------------------------------------------------
1005 */
1006
1007 int
1008 Tcl_ZlibStreamReset(
1009 Tcl_ZlibStream zshandle) /* As obtained from Tcl_ZlibStreamInit */
1010 {
1011 ZlibStreamHandle *zshPtr = (ZlibStreamHandle *) zshandle;
1012 int e;
1013
1014 if (!zshPtr->streamEnd) {
1015 if (zshPtr->mode == TCL_ZLIB_STREAM_DEFLATE) {
1016 deflateEnd(&zshPtr->stream);
1017 } else {
1018 inflateEnd(&zshPtr->stream);
1019 }
1020 }
1021 Tcl_SetByteArrayLength(zshPtr->inData, 0);
1022 Tcl_SetByteArrayLength(zshPtr->outData, 0);
1023 if (zshPtr->currentInput) {
1024 Tcl_DecrRefCount(zshPtr->currentInput);
1025 zshPtr->currentInput = NULL;
1026 }
1027
1028 zshPtr->outPos = 0;
1029 zshPtr->streamEnd = 0;
1030 memset(&zshPtr->stream, 0, sizeof(z_stream));
1031
1032 /*
1033 * No output buffer available yet.
1034 */
1035
1036 if (zshPtr->mode == TCL_ZLIB_STREAM_DEFLATE) {
1037 e = deflateInit2(&zshPtr->stream, zshPtr->level, Z_DEFLATED,
1038 zshPtr->wbits, MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY);
1039 if (e == Z_OK && HaveDictToSet(zshPtr)) {
1040 e = SetDeflateDictionary(&zshPtr->stream, zshPtr->compDictObj);
1041 if (e == Z_OK) {
1042 DictWasSet(zshPtr);
1043 }
1044 }
1045 } else {
1046 e = inflateInit2(&zshPtr->stream, zshPtr->wbits);
1047 if (IsRawStream(zshPtr) && HaveDictToSet(zshPtr) && e == Z_OK) {
1048 e = SetInflateDictionary(&zshPtr->stream, zshPtr->compDictObj);
1049 if (e == Z_OK) {
1050 DictWasSet(zshPtr);
1051 }
1052 }
1053 }
1054
1055 if (e != Z_OK) {
1056 ConvertError(zshPtr->interp, e, zshPtr->stream.adler);
1057 /* TODO:cleanup */
1058 return TCL_ERROR;
1059 }
1060
1061 return TCL_OK;
1062 }
1063
1064 /*
1065 *----------------------------------------------------------------------
1066 *
1067 * Tcl_ZlibStreamGetCommandName --
1068 *
1069 * This procedure will return the command name associated with the
1070 * stream.
1071 *
1072 * Results:
1073 * A Tcl_Obj with the name of the Tcl command or NULL if no command is
1074 * associated with the stream.
1075 *
1076 * Side effects:
1077 * None.
1078 *
1079 *----------------------------------------------------------------------
1080 */
1081
1082 Tcl_Obj *
1083 Tcl_ZlibStreamGetCommandName(
1084 Tcl_ZlibStream zshandle) /* As obtained from Tcl_ZlibStreamInit */
1085 {
1086 ZlibStreamHandle *zshPtr = (ZlibStreamHandle *) zshandle;
1087 Tcl_Obj *objPtr;
1088
1089 if (!zshPtr->interp) {
1090 return NULL;
1091 }
1092
1093 TclNewObj(objPtr);
1094 Tcl_GetCommandFullName(zshPtr->interp, zshPtr->cmd, objPtr);
1095 return objPtr;
1096 }
1097
1098 /*
1099 *----------------------------------------------------------------------
1100 *
1101 * Tcl_ZlibStreamEof --
1102 *
1103 * This procedure This function returns 0 or 1 depending on the state of
1104 * the (de)compressor. For decompression, eof is reached when the entire
1105 * compressed stream has been decompressed. For compression, eof is
1106 * reached when the stream has been flushed with TCL_ZLIB_FINALIZE.
1107 *
1108 * Results:
1109 * Integer.
1110 *
1111 * Side effects:
1112 * None.
1113 *
1114 *----------------------------------------------------------------------
1115 */
1116
1117 int
1118 Tcl_ZlibStreamEof(
1119 Tcl_ZlibStream zshandle) /* As obtained from Tcl_ZlibStreamInit */
1120 {
1121 ZlibStreamHandle *zshPtr = (ZlibStreamHandle *) zshandle;
1122
1123 return zshPtr->streamEnd;
1124 }
1125
1126 /*
1127 *----------------------------------------------------------------------
1128 *
1129 * Tcl_ZlibStreamChecksum --
1130 *
1131 * Return the checksum of the uncompressed data seen so far by the
1132 * stream.
1133 *
1134 *----------------------------------------------------------------------
1135 */
1136
1137 int
1138 Tcl_ZlibStreamChecksum(
1139 Tcl_ZlibStream zshandle) /* As obtained from Tcl_ZlibStreamInit */
1140 {
1141 ZlibStreamHandle *zshPtr = (ZlibStreamHandle *) zshandle;
1142
1143 return zshPtr->stream.adler;
1144 }
1145
1146 /*
1147 *----------------------------------------------------------------------
1148 *
1149 * Tcl_ZlibStreamSetCompressionDictionary --
1150 *
1151 * Sets the compression dictionary for a stream. This will be used as
1152 * appropriate for the next compression or decompression action performed
1153 * on the stream.
1154 *
1155 *----------------------------------------------------------------------
1156 */
1157
1158 void
1159 Tcl_ZlibStreamSetCompressionDictionary(
1160 Tcl_ZlibStream zshandle,
1161 Tcl_Obj *compressionDictionaryObj)
1162 {
1163 ZlibStreamHandle *zshPtr = (ZlibStreamHandle *) zshandle;
1164
1165 if (compressionDictionaryObj != NULL) {
1166 if (Tcl_IsShared(compressionDictionaryObj)) {
1167 compressionDictionaryObj =
1168 Tcl_DuplicateObj(compressionDictionaryObj);
1169 }
1170 Tcl_IncrRefCount(compressionDictionaryObj);
1171 zshPtr->flags |= DICT_TO_SET;
1172 } else {
1173 zshPtr->flags &= ~DICT_TO_SET;
1174 }
1175 if (zshPtr->compDictObj != NULL) {
1176 Tcl_DecrRefCount(zshPtr->compDictObj);
1177 }
1178 zshPtr->compDictObj = compressionDictionaryObj;
1179 }
1180
1181 /*
1182 *----------------------------------------------------------------------
1183 *
1184 * Tcl_ZlibStreamPut --
1185 *
1186 * Add data to the stream for compression or decompression from a
1187 * bytearray Tcl_Obj.
1188 *
1189 *----------------------------------------------------------------------
1190 */
1191
1192 #define BUFFER_SIZE_LIMIT 0xFFFF
1193
1194 int
1195 Tcl_ZlibStreamPut(
1196 Tcl_ZlibStream zshandle, /* As obtained from Tcl_ZlibStreamInit */
1197 Tcl_Obj *data, /* Data to compress/decompress */
1198 int flush) /* TCL_ZLIB_NO_FLUSH, TCL_ZLIB_FLUSH,
1199 * TCL_ZLIB_FULLFLUSH, or TCL_ZLIB_FINALIZE */
1200 {
1201 ZlibStreamHandle *zshPtr = (ZlibStreamHandle *) zshandle;
1202 char *dataTmp = NULL;
1203 int e;
1204 int size, outSize, toStore;
1205
1206 if (zshPtr->streamEnd) {
1207 if (zshPtr->interp) {
1208 Tcl_SetObjResult(zshPtr->interp, Tcl_NewStringObj(
1209 "already past compressed stream end", -1));
1210 Tcl_SetErrorCode(zshPtr->interp, "TCL", "ZIP", "CLOSED", (char *)NULL);
1211 }
1212 return TCL_ERROR;
1213 }
1214
1215 if (zshPtr->mode == TCL_ZLIB_STREAM_DEFLATE) {
1216 zshPtr->stream.next_in = Tcl_GetByteArrayFromObj(data, &size);
1217 zshPtr->stream.avail_in = size;
1218
1219 /*
1220 * Must not do a zero-length compress unless finalizing. [Bug 25842c161]
1221 */
1222
1223 if (size == 0 && flush != Z_FINISH) {
1224 return TCL_OK;
1225 }
1226
1227 if (HaveDictToSet(zshPtr)) {
1228 e = SetDeflateDictionary(&zshPtr->stream, zshPtr->compDictObj);
1229 if (e != Z_OK) {
1230 ConvertError(zshPtr->interp, e, zshPtr->stream.adler);
1231 return TCL_ERROR;
1232 }
1233 DictWasSet(zshPtr);
1234 }
1235
1236 /*
1237 * deflateBound() doesn't seem to take various header sizes into
1238 * account, so we add 100 extra bytes. However, we can also loop
1239 * around again so we also set an upper bound on the output buffer
1240 * size.
1241 */
1242
1243 outSize = deflateBound(&zshPtr->stream, size) + 100;
1244 if (outSize > BUFFER_SIZE_LIMIT) {
1245 outSize = BUFFER_SIZE_LIMIT;
1246 }
1247 dataTmp = (char *)ckalloc(outSize);
1248
1249 while (1) {
1250 e = Deflate(&zshPtr->stream, dataTmp, outSize, flush, &toStore);
1251
1252 /*
1253 * Test if we've filled the buffer up and have to ask deflate() to
1254 * give us some more. Note that the condition for needing to
1255 * repeat a buffer transfer when the result is Z_OK is whether
1256 * there is no more space in the buffer we provided; the zlib
1257 * library does not necessarily return a different code in that
1258 * case. [Bug b26e38a3e4] [Tk Bug 10f2e7872b]
1259 */
1260
1261 if ((e != Z_BUF_ERROR) && (e != Z_OK || toStore < outSize)) {
1262 if ((e == Z_OK) || (flush == Z_FINISH && e == Z_STREAM_END)) {
1263 break;
1264 }
1265 ConvertError(zshPtr->interp, e, zshPtr->stream.adler);
1266 return TCL_ERROR;
1267 }
1268
1269 /*
1270 * Output buffer too small to hold the data being generated or we
1271 * are doing the end-of-stream flush (which can spit out masses of
1272 * data). This means we need to put a new buffer into place after
1273 * saving the old generated data to the outData list.
1274 */
1275
1276 AppendByteArray(zshPtr->outData, dataTmp, outSize);
1277
1278 if (outSize < BUFFER_SIZE_LIMIT) {
1279 outSize = BUFFER_SIZE_LIMIT;
1280 /* There may be *lots* of data left to output... */
1281 dataTmp = (char *)ckrealloc(dataTmp, outSize);
1282 }
1283 }
1284
1285 /*
1286 * And append the final data block to the outData list.
1287 */
1288
1289 AppendByteArray(zshPtr->outData, dataTmp, toStore);
1290 ckfree(dataTmp);
1291 } else {
1292 /*
1293 * This is easy. Just append to the inData list.
1294 */
1295
1296 Tcl_ListObjAppendElement(NULL, zshPtr->inData, data);
1297
1298 /*
1299 * and we'll need the flush parameter for the Inflate call.
1300 */
1301
1302 zshPtr->flush = flush;
1303 }
1304
1305 return TCL_OK;
1306 }
1307
1308 /*
1309 *----------------------------------------------------------------------
1310 *
1311 * Tcl_ZlibStreamGet --
1312 *
1313 * Retrieve data (now compressed or decompressed) from the stream into a
1314 * bytearray Tcl_Obj.
1315 *
1316 *----------------------------------------------------------------------
1317 */
1318
1319 int
1320 Tcl_ZlibStreamGet(
1321 Tcl_ZlibStream zshandle, /* As obtained from Tcl_ZlibStreamInit */
1322 Tcl_Obj *data, /* A place to append the data. */
1323 int count) /* Number of bytes to grab as a maximum, you
1324 * may get less! */
1325 {
1326 ZlibStreamHandle *zshPtr = (ZlibStreamHandle *) zshandle;
1327 int e;
1328 int listLen, i, itemLen, dataPos = 0;
1329 Tcl_Obj *itemObj;
1330 unsigned char *dataPtr, *itemPtr;
1331 int existing;
1332
1333 /*
1334 * Getting beyond the of stream, just return empty string.
1335 */
1336
1337 if (zshPtr->streamEnd) {
1338 return TCL_OK;
1339 }
1340
1341 (void) Tcl_GetByteArrayFromObj(data, &existing);
1342
1343 if (zshPtr->mode == TCL_ZLIB_STREAM_INFLATE) {
1344 if (count == -1) {
1345 /*
1346 * The only safe thing to do is restict to 65k. We might cause a
1347 * panic for out of memory if we just kept growing the buffer.
1348 */
1349
1350 count = MAX_BUFFER_SIZE;
1351 }
1352
1353 /*
1354 * Prepare the place to store the data.
1355 */
1356
1357 dataPtr = Tcl_SetByteArrayLength(data, existing+count);
1358 dataPtr += existing;
1359
1360 zshPtr->stream.next_out = dataPtr;
1361 zshPtr->stream.avail_out = count;
1362 if (zshPtr->stream.avail_in == 0) {
1363 /*
1364 * zlib will probably need more data to decompress.
1365 */
1366
1367 if (zshPtr->currentInput) {
1368 Tcl_DecrRefCount(zshPtr->currentInput);
1369 zshPtr->currentInput = NULL;
1370 }
1371 TclListObjLength(NULL, zshPtr->inData, &listLen);
1372 if (listLen > 0) {
1373 /*
1374 * There is more input available, get it from the list and
1375 * give it to zlib. At this point, the data must not be shared
1376 * since we require the bytearray representation to not vanish
1377 * under our feet. [Bug 3081008]
1378 */
1379
1380 Tcl_ListObjIndex(NULL, zshPtr->inData, 0, &itemObj);
1381 if (Tcl_IsShared(itemObj)) {
1382 itemObj = Tcl_DuplicateObj(itemObj);
1383 }
1384 itemPtr = Tcl_GetByteArrayFromObj(itemObj, &itemLen);
1385 Tcl_IncrRefCount(itemObj);
1386 zshPtr->currentInput = itemObj;
1387 zshPtr->stream.next_in = itemPtr;
1388 zshPtr->stream.avail_in = itemLen;
1389
1390 /*
1391 * And remove it from the list
1392 */
1393
1394 Tcl_ListObjReplace(NULL, zshPtr->inData, 0, 1, 0, NULL);
1395 }
1396 }
1397
1398 /*
1399 * When dealing with a raw stream, we set the dictionary here, once.
1400 * (You can't do it in response to getting Z_NEED_DATA as raw streams
1401 * don't ever issue that.)
1402 */
1403
1404 if (IsRawStream(zshPtr) && HaveDictToSet(zshPtr)) {
1405 e = SetInflateDictionary(&zshPtr->stream, zshPtr->compDictObj);
1406 if (e != Z_OK) {
1407 ConvertError(zshPtr->interp, e, zshPtr->stream.adler);
1408 return TCL_ERROR;
1409 }
1410 DictWasSet(zshPtr);
1411 }
1412 e = inflate(&zshPtr->stream, zshPtr->flush);
1413 if (e == Z_NEED_DICT && HaveDictToSet(zshPtr)) {
1414 e = SetInflateDictionary(&zshPtr->stream, zshPtr->compDictObj);
1415 if (e == Z_OK) {
1416 DictWasSet(zshPtr);
1417 e = inflate(&zshPtr->stream, zshPtr->flush);
1418 }
1419 };
1420 TclListObjLength(NULL, zshPtr->inData, &listLen);
1421
1422 while ((zshPtr->stream.avail_out > 0)
1423 && (e == Z_OK || e == Z_BUF_ERROR) && (listLen > 0)) {
1424 /*
1425 * State: We have not satisfied the request yet and there may be
1426 * more to inflate.
1427 */
1428
1429 if (zshPtr->stream.avail_in > 0) {
1430 if (zshPtr->interp) {
1431 Tcl_SetObjResult(zshPtr->interp, Tcl_NewStringObj(
1432 "unexpected zlib internal state during"
1433 " decompression", -1));
1434 Tcl_SetErrorCode(zshPtr->interp, "TCL", "ZIP", "STATE",
1435 (char *)NULL);
1436 }
1437 Tcl_SetByteArrayLength(data, existing);
1438 return TCL_ERROR;
1439 }
1440
1441 if (zshPtr->currentInput) {
1442 Tcl_DecrRefCount(zshPtr->currentInput);
1443 zshPtr->currentInput = 0;
1444 }
1445
1446 /*
1447 * Get the next block of data to go to inflate. At this point, the
1448 * data must not be shared since we require the bytearray
1449 * representation to not vanish under our feet. [Bug 3081008]
1450 */
1451
1452 Tcl_ListObjIndex(zshPtr->interp, zshPtr->inData, 0, &itemObj);
1453 if (Tcl_IsShared(itemObj)) {
1454 itemObj = Tcl_DuplicateObj(itemObj);
1455 }
1456 itemPtr = Tcl_GetByteArrayFromObj(itemObj, &itemLen);
1457 Tcl_IncrRefCount(itemObj);
1458 zshPtr->currentInput = itemObj;
1459 zshPtr->stream.next_in = itemPtr;
1460 zshPtr->stream.avail_in = itemLen;
1461
1462 /*
1463 * Remove it from the list.
1464 */
1465
1466 Tcl_ListObjReplace(NULL, zshPtr->inData, 0, 1, 0, NULL);
1467 listLen--;
1468
1469 /*
1470 * And call inflate again.
1471 */
1472
1473 do {
1474 e = inflate(&zshPtr->stream, zshPtr->flush);
1475 if (e != Z_NEED_DICT || !HaveDictToSet(zshPtr)) {
1476 break;
1477 }
1478 e = SetInflateDictionary(&zshPtr->stream,zshPtr->compDictObj);
1479 DictWasSet(zshPtr);
1480 } while (e == Z_OK);
1481 }
1482 if (zshPtr->stream.avail_out > 0) {
1483 Tcl_SetByteArrayLength(data,
1484 existing + count - zshPtr->stream.avail_out);
1485 }
1486 if (!(e==Z_OK || e==Z_STREAM_END || e==Z_BUF_ERROR)) {
1487 Tcl_SetByteArrayLength(data, existing);
1488 ConvertError(zshPtr->interp, e, zshPtr->stream.adler);
1489 return TCL_ERROR;
1490 }
1491 if (e == Z_STREAM_END) {
1492 zshPtr->streamEnd = 1;
1493 if (zshPtr->currentInput) {
1494 Tcl_DecrRefCount(zshPtr->currentInput);
1495 zshPtr->currentInput = 0;
1496 }
1497 inflateEnd(&zshPtr->stream);
1498 }
1499 } else {
1500 TclListObjLength(NULL, zshPtr->outData, &listLen);
1501 if (count == -1) {
1502 count = 0;
1503 for (i=0; i<listLen; i++) {
1504 Tcl_ListObjIndex(NULL, zshPtr->outData, i, &itemObj);
1505 (void) Tcl_GetByteArrayFromObj(itemObj, &itemLen);
1506 if (i == 0) {
1507 count += itemLen - zshPtr->outPos;
1508 } else {
1509 count += itemLen;
1510 }
1511 }
1512 }
1513
1514 /*
1515 * Prepare the place to store the data.
1516 */
1517
1518 dataPtr = Tcl_SetByteArrayLength(data, existing + count);
1519 dataPtr += existing;
1520
1521 while ((count > dataPos) &&
1522 (TclListObjLength(NULL, zshPtr->outData, &listLen) == TCL_OK)
1523 && (listLen > 0)) {
1524 /*
1525 * Get the next chunk off our list of chunks and grab the data out
1526 * of it.
1527 */
1528
1529 Tcl_ListObjIndex(NULL, zshPtr->outData, 0, &itemObj);
1530 itemPtr = Tcl_GetByteArrayFromObj(itemObj, &itemLen);
1531 if (itemLen-zshPtr->outPos >= count-dataPos) {
1532 size_t len = count - dataPos;
1533
1534 memcpy(dataPtr + dataPos, itemPtr + zshPtr->outPos, len);
1535 zshPtr->outPos += len;
1536 dataPos += len;
1537 if (zshPtr->outPos == itemLen) {
1538 zshPtr->outPos = 0;
1539 }
1540 } else {
1541 size_t len = itemLen - zshPtr->outPos;
1542
1543 memcpy(dataPtr + dataPos, itemPtr + zshPtr->outPos, len);
1544 dataPos += len;
1545 zshPtr->outPos = 0;
1546 }
1547 if (zshPtr->outPos == 0) {
1548 Tcl_ListObjReplace(NULL, zshPtr->outData, 0, 1, 0, NULL);
1549 listLen--;
1550 }
1551 }
1552 Tcl_SetByteArrayLength(data, existing + dataPos);
1553 }
1554 return TCL_OK;
1555 }
1556
1557 /*
1558 *----------------------------------------------------------------------
1559 *
1560 * Tcl_ZlibDeflate --
1561 *
1562 * Compress the contents of Tcl_Obj *data with compression level in
1563 * output format, producing the compressed data in the interpreter
1564 * result.
1565 *
1566 *----------------------------------------------------------------------
1567 */
1568
1569 int
1570 Tcl_ZlibDeflate(
1571 Tcl_Interp *interp,
1572 int format,
1573 Tcl_Obj *data,
1574 int level,
1575 Tcl_Obj *gzipHeaderDictObj)
1576 {
1577 int wbits = 0, e = 0, extraSize = 0;
1578 int inLen = 0;
1579 Byte *inData = NULL;
1580 z_stream stream;
1581 GzipHeader header;
1582 gz_header *headerPtr = NULL;
1583 Tcl_Obj *obj;
1584
1585 if (!interp) {
1586 return TCL_ERROR;
1587 }
1588
1589 /*
1590 * Compressed format is specified by the wbits parameter. See zlib.h for
1591 * details.
1592 */
1593
1594 if (format == TCL_ZLIB_FORMAT_RAW) {
1595 wbits = WBITS_RAW;
1596 } else if (format == TCL_ZLIB_FORMAT_GZIP) {
1597 wbits = WBITS_GZIP;
1598
1599 /*
1600 * Need to allocate extra space for the gzip header and footer. The
1601 * amount of space is (a bit less than) 32 bytes, plus a byte for each
1602 * byte of string that we add. Note that over-allocation is not a
1603 * problem. [Bug 2419061]
1604 */
1605
1606 extraSize = 32;
1607 if (gzipHeaderDictObj) {
1608 headerPtr = &header.header;
1609 memset(headerPtr, 0, sizeof(gz_header));
1610 if (GenerateHeader(interp, gzipHeaderDictObj, &header,
1611 &extraSize) != TCL_OK) {
1612 return TCL_ERROR;
1613 }
1614 }
1615 } else if (format == TCL_ZLIB_FORMAT_ZLIB) {
1616 wbits = WBITS_ZLIB;
1617 } else {
1618 Tcl_Panic("incorrect zlib data format, must be TCL_ZLIB_FORMAT_ZLIB, "
1619 "TCL_ZLIB_FORMAT_GZIP or TCL_ZLIB_FORMAT_ZLIB");
1620 }
1621
1622 if (level < -1 || level > 9) {
1623 Tcl_Panic("compression level should be between 0 (uncompressed) and "
1624 "9 (best compression) or -1 for default compression level");
1625 }
1626
1627 /*
1628 * Allocate some space to store the output.
1629 */
1630
1631 TclNewObj(obj);
1632
1633 /*
1634 * Obtain the pointer to the byte array, we'll pass this pointer straight
1635 * to the deflate command.
1636 */
1637
1638 inData = Tcl_GetByteArrayFromObj(data, &inLen);
1639 memset(&stream, 0, sizeof(z_stream));
1640 stream.avail_in = (uInt) inLen;
1641 stream.next_in = inData;
1642
1643 /*
1644 * No output buffer available yet, will alloc after deflateInit2.
1645 */
1646
1647 e = deflateInit2(&stream, level, Z_DEFLATED, wbits, MAX_MEM_LEVEL,
1648 Z_DEFAULT_STRATEGY);
1649 if (e != Z_OK) {
1650 goto error;
1651 }
1652
1653 if (headerPtr != NULL) {
1654 e = deflateSetHeader(&stream, headerPtr);
1655 if (e != Z_OK) {
1656 goto error;
1657 }
1658 }
1659
1660 /*
1661 * Allocate the output buffer from the value of deflateBound(). This is
1662 * probably too much space. Before returning to the caller, we will reduce
1663 * it back to the actual compressed size.
1664 */
1665
1666 stream.avail_out = deflateBound(&stream, inLen) + extraSize;
1667 stream.next_out = Tcl_SetByteArrayLength(obj, stream.avail_out);
1668
1669 /*
1670 * Perform the compression, Z_FINISH means do it in one go.
1671 */
1672
1673 e = deflate(&stream, Z_FINISH);
1674
1675 if (e != Z_STREAM_END) {
1676 e = deflateEnd(&stream);
1677
1678 /*
1679 * deflateEnd() returns Z_OK when there are bytes left to compress, at
1680 * this point we consider that an error, although we could continue by
1681 * allocating more memory and calling deflate() again.
1682 */
1683
1684 if (e == Z_OK) {
1685 e = Z_BUF_ERROR;
1686 }
1687 } else {
1688 e = deflateEnd(&stream);
1689 }
1690
1691 if (e != Z_OK) {
1692 goto error;
1693 }
1694
1695 /*
1696 * Reduce the ByteArray length to the actual data length produced by
1697 * deflate.
1698 */
1699
1700 Tcl_SetByteArrayLength(obj, stream.total_out);
1701 Tcl_SetObjResult(interp, obj);
1702 return TCL_OK;
1703
1704 error:
1705 ConvertError(interp, e, stream.adler);
1706 TclDecrRefCount(obj);
1707 return TCL_ERROR;
1708 }
1709
1710 /*
1711 *----------------------------------------------------------------------
1712 *
1713 * Tcl_ZlibInflate --
1714 *
1715 * Decompress data in an object into the interpreter result.
1716 *
1717 *----------------------------------------------------------------------
1718 */
1719
1720 int
1721 Tcl_ZlibInflate(
1722 Tcl_Interp *interp,
1723 int format,
1724 Tcl_Obj *data,
1725 int bufferSize,
1726 Tcl_Obj *gzipHeaderDictObj)
1727 {
1728 int wbits = 0, e = 0;
1729 int inLen = 0, newBufferSize;
1730 Byte *inData = NULL, *outData = NULL, *newOutData = NULL;
1731 z_stream stream;
1732 gz_header header, *headerPtr = NULL;
1733 Tcl_Obj *obj;
1734 char *nameBuf = NULL, *commentBuf = NULL;
1735
1736 if (!interp) {
1737 return TCL_ERROR;
1738 }
1739
1740 /*
1741 * Compressed format is specified by the wbits parameter. See zlib.h for
1742 * details.
1743 */
1744
1745 switch (format) {
1746 case TCL_ZLIB_FORMAT_RAW:
1747 wbits = WBITS_RAW;
1748 gzipHeaderDictObj = NULL;
1749 break;
1750 case TCL_ZLIB_FORMAT_ZLIB:
1751 wbits = WBITS_ZLIB;
1752 gzipHeaderDictObj = NULL;
1753 break;
1754 case TCL_ZLIB_FORMAT_GZIP:
1755 wbits = WBITS_GZIP;
1756 break;
1757 case TCL_ZLIB_FORMAT_AUTO:
1758 wbits = WBITS_AUTODETECT;
1759 break;
1760 default:
1761 Tcl_Panic("incorrect zlib data format, must be TCL_ZLIB_FORMAT_ZLIB, "
1762 "TCL_ZLIB_FORMAT_GZIP, TCL_ZLIB_FORMAT_RAW or "
1763 "TCL_ZLIB_FORMAT_AUTO");
1764 }
1765
1766 if (gzipHeaderDictObj) {
1767 headerPtr = &header;
1768 memset(headerPtr, 0, sizeof(gz_header));
1769 nameBuf = (char *)ckalloc(MAXPATHLEN);
1770 header.name = (Bytef *) nameBuf;
1771 header.name_max = MAXPATHLEN - 1;
1772 commentBuf = (char *)ckalloc(MAX_COMMENT_LEN);
1773 header.comment = (Bytef *) commentBuf;
1774 header.comm_max = MAX_COMMENT_LEN - 1;
1775 }
1776
1777 inData = Tcl_GetByteArrayFromObj(data, &inLen);
1778 if (bufferSize < 1) {
1779 /*
1780 * Start with a buffer (up to) 3 times the size of the input data.
1781 */
1782
1783 if (inLen < 32*1024*1024) {
1784 bufferSize = 3*inLen;
1785 } else if (inLen < 256*1024*1024) {
1786 bufferSize = 2*inLen;
1787 } else {
1788 bufferSize = inLen;
1789 }
1790 }
1791
1792 TclNewObj(obj);
1793 outData = Tcl_SetByteArrayLength(obj, bufferSize);
1794 memset(&stream, 0, sizeof(z_stream));
1795 stream.avail_in = (uInt) inLen+1; /* +1 because zlib can "over-request"
1796 * input (but ignore it!) */
1797 stream.next_in = inData;
1798 stream.avail_out = bufferSize;
1799 stream.next_out = outData;
1800
1801 /*
1802 * Initialize zlib for decompression.
1803 */
1804
1805 e = inflateInit2(&stream, wbits);
1806 if (e != Z_OK) {
1807 goto error;
1808 }
1809 if (headerPtr) {
1810 e = inflateGetHeader(&stream, headerPtr);
1811 if (e != Z_OK) {
1812 inflateEnd(&stream);
1813 goto error;
1814 }
1815 }
1816
1817 /*
1818 * Start the decompression cycle.
1819 */
1820
1821 while (1) {
1822 e = inflate(&stream, Z_FINISH);
1823 if (e != Z_BUF_ERROR) {
1824 break;
1825 }
1826
1827 /*
1828 * Not enough room in the output buffer. Increase it by five times the
1829 * bytes still in the input buffer. (Because 3 times didn't do the
1830 * trick before, 5 times is what we do next.) Further optimization
1831 * should be done by the user, specify the decompressed size!
1832 */
1833
1834 if ((stream.avail_in == 0) && (stream.avail_out > 0)) {
1835 e = Z_STREAM_ERROR;
1836 break;
1837 }
1838 newBufferSize = bufferSize + 5 * stream.avail_in;
1839 if (newBufferSize == bufferSize) {
1840 newBufferSize = bufferSize+1000;
1841 }
1842 newOutData = Tcl_SetByteArrayLength(obj, newBufferSize);
1843
1844 /*
1845 * Set next out to the same offset in the new location.
1846 */
1847
1848 stream.next_out = newOutData + stream.total_out;
1849
1850 /*
1851 * And increase avail_out with the number of new bytes allocated.
1852 */
1853
1854 stream.avail_out += newBufferSize - bufferSize;
1855 outData = newOutData;
1856 bufferSize = newBufferSize;
1857 }
1858
1859 if (e != Z_STREAM_END) {
1860 inflateEnd(&stream);
1861 goto error;
1862 }
1863
1864 e = inflateEnd(&stream);
1865 if (e != Z_OK) {
1866 goto error;
1867 }
1868
1869 /*
1870 * Reduce the BA length to the actual data length produced by deflate.
1871 */
1872
1873 Tcl_SetByteArrayLength(obj, stream.total_out);
1874 if (headerPtr != NULL) {
1875 ExtractHeader(&header, gzipHeaderDictObj);
1876 TclDictPut(NULL, gzipHeaderDictObj, "size",
1877 Tcl_NewLongObj(stream.total_out));
1878 ckfree(nameBuf);
1879 ckfree(commentBuf);
1880 }
1881 Tcl_SetObjResult(interp, obj);
1882 return TCL_OK;
1883
1884 error:
1885 TclDecrRefCount(obj);
1886 ConvertError(interp, e, stream.adler);
1887 if (nameBuf) {
1888 ckfree(nameBuf);
1889 }
1890 if (commentBuf) {
1891 ckfree(commentBuf);
1892 }
1893 return TCL_ERROR;
1894 }
1895
1896 /*
1897 *----------------------------------------------------------------------
1898 *
1899 * Tcl_ZlibCRC32, Tcl_ZlibAdler32 --
1900 *
1901 * Access to the checksumming engines.
1902 *
1903 *----------------------------------------------------------------------
1904 */
1905
1906 unsigned int
1907 Tcl_ZlibCRC32(
1908 unsigned int crc,
1909 const unsigned char *buf,
1910 int len)
1911 {
1912 /* Nothing much to do, just wrap the crc32(). */
1913 return crc32(crc, (Bytef *) buf, len);
1914 }
1915
1916 unsigned int
1917 Tcl_ZlibAdler32(
1918 unsigned int adler,
1919 const unsigned char *buf,
1920 int len)
1921 {
1922 return adler32(adler, (Bytef *) buf, len);
1923 }
1924
1925 /*
1926 *----------------------------------------------------------------------
1927 *
1928 * ZlibCmd --
1929 *
1930 * Implementation of the [zlib] command.
1931 *
1932 *----------------------------------------------------------------------
1933 */
1934
1935 static int
1936 ZlibCmd(
1937 ClientData notUsed,
1938 Tcl_Interp *interp,
1939 int objc,
1940 Tcl_Obj *const objv[])
1941 {
1942 int command, i, option, level = -1;
1943 unsigned buffersize = 0;
1944 int dlen;
1945 unsigned start;
1946 Byte *data;
1947 Tcl_Obj *headerDictObj;
1948 const char *extraInfoStr = NULL;
1949 static const char *const commands[] = {
1950 "adler32", "compress", "crc32", "decompress", "deflate", "gunzip",
1951 "gzip", "inflate", "push", "stream",
1952 NULL
1953 };
1954 enum zlibCommands {
1955 CMD_ADLER, CMD_COMPRESS, CMD_CRC, CMD_DECOMPRESS, CMD_DEFLATE,
1956 CMD_GUNZIP, CMD_GZIP, CMD_INFLATE, CMD_PUSH, CMD_STREAM
1957 };
1958
1959 if (objc < 2) {
1960 Tcl_WrongNumArgs(interp, 1, objv, "command arg ?...?");
1961 return TCL_ERROR;
1962 }
1963 if (Tcl_GetIndexFromObj(interp, objv[1], commands, "command", 0,
1964 &command) != TCL_OK) {
1965 return TCL_ERROR;
1966 }
1967
1968 switch ((enum zlibCommands) command) {
1969 case CMD_ADLER: /* adler32 str ?startvalue?
1970 * -> checksum */
1971 if (objc < 3 || objc > 4) {
1972 Tcl_WrongNumArgs(interp, 2, objv, "data ?startValue?");
1973 return TCL_ERROR;
1974 }
1975 if (objc>3 && Tcl_GetIntFromObj(interp, objv[3],
1976 (int *) &start) != TCL_OK) {
1977 return TCL_ERROR;
1978 }
1979 if (objc < 4) {
1980 start = Tcl_ZlibAdler32(0, NULL, 0);
1981 }
1982 data = Tcl_GetByteArrayFromObj(objv[2], &dlen);
1983 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(
1984 (uLong) Tcl_ZlibAdler32(start, data, dlen)));
1985 return TCL_OK;
1986 case CMD_CRC: /* crc32 str ?startvalue?
1987 * -> checksum */
1988 if (objc < 3 || objc > 4) {
1989 Tcl_WrongNumArgs(interp, 2, objv, "data ?startValue?");
1990 return TCL_ERROR;
1991 }
1992 if (objc>3 && Tcl_GetIntFromObj(interp, objv[3],
1993 (int *) &start) != TCL_OK) {
1994 return TCL_ERROR;
1995 }
1996 if (objc < 4) {
1997 start = Tcl_ZlibCRC32(0, NULL, 0);
1998 }
1999 data = Tcl_GetByteArrayFromObj(objv[2], &dlen);
2000 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(
2001 (uLong) Tcl_ZlibCRC32(start, data, dlen)));
2002 return TCL_OK;
2003 case CMD_DEFLATE: /* deflate data ?level?
2004 * -> rawCompressedData */
2005 if (objc < 3 || objc > 4) {
2006 Tcl_WrongNumArgs(interp, 2, objv, "data ?level?");
2007 return TCL_ERROR;
2008 }
2009 if (objc > 3) {
2010 if (Tcl_GetIntFromObj(interp, objv[3], &level) != TCL_OK) {
2011 return TCL_ERROR;
2012 }
2013 if (level < 0 || level > 9) {
2014 goto badLevel;
2015 }
2016 }
2017 return Tcl_ZlibDeflate(interp, TCL_ZLIB_FORMAT_RAW, objv[2], level,
2018 NULL);
2019 case CMD_COMPRESS: /* compress data ?level?
2020 * -> zlibCompressedData */
2021 if (objc < 3 || objc > 4) {
2022 Tcl_WrongNumArgs(interp, 2, objv, "data ?level?");
2023 return TCL_ERROR;
2024 }
2025 if (objc > 3) {
2026 if (Tcl_GetIntFromObj(interp, objv[3], &level) != TCL_OK) {
2027 return TCL_ERROR;
2028 }
2029 if (level < 0 || level > 9) {
2030 goto badLevel;
2031 }
2032 }
2033 return Tcl_ZlibDeflate(interp, TCL_ZLIB_FORMAT_ZLIB, objv[2], level,
2034 NULL);
2035 case CMD_GZIP: /* gzip data ?level?
2036 * -> gzippedCompressedData */
2037 headerDictObj = NULL;
2038
2039 /*
2040 * Legacy argument format support.
2041 */
2042
2043 if (objc == 4
2044 && Tcl_GetIntFromObj(interp, objv[3], &level) == TCL_OK) {
2045 if (level < 0 || level > 9) {
2046 extraInfoStr = "\n (in -level option)";
2047 goto badLevel;
2048 }
2049 return Tcl_ZlibDeflate(interp, TCL_ZLIB_FORMAT_GZIP, objv[2],
2050 level, NULL);
2051 }
2052
2053 if (objc < 3 || objc > 7 || ((objc & 1) == 0)) {
2054 Tcl_WrongNumArgs(interp, 2, objv,
2055 "data ?-level level? ?-header header?");
2056 return TCL_ERROR;
2057 }
2058 for (i=3 ; i<objc ; i+=2) {
2059 static const char *const gzipopts[] = {
2060 "-header", "-level", NULL
2061 };
2062
2063 if (Tcl_GetIndexFromObj(interp, objv[i], gzipopts, "option", 0,
2064 &option) != TCL_OK) {
2065 return TCL_ERROR;
2066 }
2067 switch (option) {
2068 case 0:
2069 headerDictObj = objv[i+1];
2070 break;
2071 case 1:
2072 if (Tcl_GetIntFromObj(interp, objv[i+1],
2073 &level) != TCL_OK) {
2074 return TCL_ERROR;
2075 }
2076 if (level < 0 || level > 9) {
2077 extraInfoStr = "\n (in -level option)";
2078 goto badLevel;
2079 }
2080 break;
2081 }
2082 }
2083 return Tcl_ZlibDeflate(interp, TCL_ZLIB_FORMAT_GZIP, objv[2], level,
2084 headerDictObj);
2085 case CMD_INFLATE: /* inflate rawcomprdata ?bufferSize?
2086 * -> decompressedData */
2087 if (objc < 3 || objc > 4) {
2088 Tcl_WrongNumArgs(interp, 2, objv, "data ?bufferSize?");
2089 return TCL_ERROR;
2090 }
2091 if (objc > 3) {
2092 if (Tcl_GetIntFromObj(interp, objv[3],
2093 (int *) &buffersize) != TCL_OK) {
2094 return TCL_ERROR;
2095 }
2096 if (buffersize < MIN_NONSTREAM_BUFFER_SIZE
2097 || buffersize > MAX_BUFFER_SIZE) {
2098 goto badBuffer;
2099 }
2100 }
2101 return Tcl_ZlibInflate(interp, TCL_ZLIB_FORMAT_RAW, objv[2],
2102 buffersize, NULL);
2103 case CMD_DECOMPRESS: /* decompress zlibcomprdata \
2104 * ?bufferSize?
2105 * -> decompressedData */
2106 if (objc < 3 || objc > 4) {
2107 Tcl_WrongNumArgs(interp, 2, objv, "data ?bufferSize?");
2108 return TCL_ERROR;
2109 }
2110 if (objc > 3) {
2111 if (Tcl_GetIntFromObj(interp, objv[3],
2112 (int *) &buffersize) != TCL_OK) {
2113 return TCL_ERROR;
2114 }
2115 if (buffersize < MIN_NONSTREAM_BUFFER_SIZE
2116 || buffersize > MAX_BUFFER_SIZE) {
2117 goto badBuffer;
2118 }
2119 }
2120 return Tcl_ZlibInflate(interp, TCL_ZLIB_FORMAT_ZLIB, objv[2],
2121 buffersize, NULL);
2122 case CMD_GUNZIP: { /* gunzip gzippeddata ?bufferSize?
2123 * -> decompressedData */
2124 Tcl_Obj *headerVarObj;
2125
2126 if (objc < 3 || objc > 5 || ((objc & 1) == 0)) {
2127 Tcl_WrongNumArgs(interp, 2, objv, "data ?-headerVar varName?");
2128 return TCL_ERROR;
2129 }
2130 headerDictObj = headerVarObj = NULL;
2131 for (i=3 ; i<objc ; i+=2) {
2132 static const char *const gunzipopts[] = {
2133 "-buffersize", "-headerVar", NULL
2134 };
2135
2136 if (Tcl_GetIndexFromObj(interp, objv[i], gunzipopts, "option", 0,
2137 &option) != TCL_OK) {
2138 return TCL_ERROR;
2139 }
2140 switch (option) {
2141 case 0:
2142 if (Tcl_GetIntFromObj(interp, objv[i+1],
2143 (int *) &buffersize) != TCL_OK) {
2144 return TCL_ERROR;
2145 }
2146 if (buffersize < MIN_NONSTREAM_BUFFER_SIZE
2147 || buffersize > MAX_BUFFER_SIZE) {
2148 goto badBuffer;
2149 }
2150 break;
2151 case 1:
2152 headerVarObj = objv[i+1];
2153 TclNewObj(headerDictObj);
2154 break;
2155 }
2156 }
2157 if (Tcl_ZlibInflate(interp, TCL_ZLIB_FORMAT_GZIP, objv[2],
2158 buffersize, headerDictObj) != TCL_OK) {
2159 if (headerDictObj) {
2160 TclDecrRefCount(headerDictObj);
2161 }
2162 return TCL_ERROR;
2163 }
2164 if (headerVarObj != NULL && Tcl_ObjSetVar2(interp, headerVarObj, NULL,
2165 headerDictObj, TCL_LEAVE_ERR_MSG) == NULL) {
2166 return TCL_ERROR;
2167 }
2168 return TCL_OK;
2169 }
2170 case CMD_STREAM: /* stream deflate/inflate/...gunzip \
2171 * ?options...?
2172 * -> handleCmd */
2173 return ZlibStreamSubcmd(interp, objc, objv);
2174 case CMD_PUSH: /* push mode channel options...
2175 * -> channel */
2176 return ZlibPushSubcmd(interp, objc, objv);
2177 };
2178
2179 return TCL_ERROR;
2180
2181 badLevel:
2182 Tcl_SetObjResult(interp, Tcl_NewStringObj("level must be 0 to 9", -1));
2183 Tcl_SetErrorCode(interp, "TCL", "VALUE", "COMPRESSIONLEVEL", (char *)NULL);
2184 if (extraInfoStr) {
2185 Tcl_AddErrorInfo(interp, extraInfoStr);
2186 }
2187 return TCL_ERROR;
2188 badBuffer:
2189 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
2190 "buffer size must be %d to %d",
2191 MIN_NONSTREAM_BUFFER_SIZE, MAX_BUFFER_SIZE));
2192 Tcl_SetErrorCode(interp, "TCL", "VALUE", "BUFFERSIZE", (char *)NULL);
2193 return TCL_ERROR;
2194 }
2195
2196 /*
2197 *----------------------------------------------------------------------
2198 *
2199 * ZlibStreamSubcmd --
2200 *
2201 * Implementation of the [zlib stream] subcommand.
2202 *
2203 *----------------------------------------------------------------------
2204 */
2205
2206 static int
2207 ZlibStreamSubcmd(
2208 Tcl_Interp *interp,
2209 int objc,
2210 Tcl_Obj *const objv[])
2211 {
2212 static const char *const stream_formats[] = {
2213 "compress", "decompress", "deflate", "gunzip", "gzip", "inflate",
2214 NULL
2215 };
2216 enum zlibFormats {
2217 FMT_COMPRESS, FMT_DECOMPRESS, FMT_DEFLATE, FMT_GUNZIP, FMT_GZIP,
2218 FMT_INFLATE
2219 };
2220 int i, format, mode = 0, option, level;
2221 enum objIndices {
2222 OPT_COMPRESSION_DICTIONARY = 0,
2223 OPT_GZIP_HEADER = 1,
2224 OPT_COMPRESSION_LEVEL = 2,
2225 OPT_END = -1
2226 };
2227 Tcl_Obj *obj[3] = { NULL, NULL, NULL };
2228 #define compDictObj obj[OPT_COMPRESSION_DICTIONARY]
2229 #define gzipHeaderObj obj[OPT_GZIP_HEADER]
2230 #define levelObj obj[OPT_COMPRESSION_LEVEL]
2231 typedef struct {
2232 const char *name;
2233 enum objIndices offset;
2234 } OptDescriptor;
2235 static const OptDescriptor compressionOpts[] = {
2236 { "-dictionary", OPT_COMPRESSION_DICTIONARY },
2237 { "-level", OPT_COMPRESSION_LEVEL },
2238 { NULL, OPT_END }
2239 };
2240 static const OptDescriptor gzipOpts[] = {
2241 { "-header", OPT_GZIP_HEADER },
2242 { "-level", OPT_COMPRESSION_LEVEL },
2243 { NULL, OPT_END }
2244 };
2245 static const OptDescriptor expansionOpts[] = {
2246 { "-dictionary", OPT_COMPRESSION_DICTIONARY },
2247 { NULL, OPT_END }
2248 };
2249 static const OptDescriptor gunzipOpts[] = {
2250 { NULL, OPT_END }
2251 };
2252 const OptDescriptor *desc = NULL;
2253 Tcl_ZlibStream zh;
2254
2255 if (objc < 3 || !(objc & 1)) {
2256 Tcl_WrongNumArgs(interp, 2, objv, "mode ?-option value...?");
2257 return TCL_ERROR;
2258 }
2259 if (Tcl_GetIndexFromObj(interp, objv[2], stream_formats, "mode", 0,
2260 &format) != TCL_OK) {
2261 return TCL_ERROR;
2262 }
2263
2264 /*
2265 * The format determines the compression mode and the options that may be
2266 * specified.
2267 */
2268
2269 switch ((enum zlibFormats) format) {
2270 case FMT_DEFLATE:
2271 desc = compressionOpts;
2272 mode = TCL_ZLIB_STREAM_DEFLATE;
2273 format = TCL_ZLIB_FORMAT_RAW;
2274 break;
2275 case FMT_INFLATE:
2276 desc = expansionOpts;
2277 mode = TCL_ZLIB_STREAM_INFLATE;
2278 format = TCL_ZLIB_FORMAT_RAW;
2279 break;
2280 case FMT_COMPRESS:
2281 desc = compressionOpts;
2282 mode = TCL_ZLIB_STREAM_DEFLATE;
2283 format = TCL_ZLIB_FORMAT_ZLIB;
2284 break;
2285 case FMT_DECOMPRESS:
2286 desc = expansionOpts;
2287 mode = TCL_ZLIB_STREAM_INFLATE;
2288 format = TCL_ZLIB_FORMAT_ZLIB;
2289 break;
2290 case FMT_GZIP:
2291 desc = gzipOpts;
2292 mode = TCL_ZLIB_STREAM_DEFLATE;
2293 format = TCL_ZLIB_FORMAT_GZIP;
2294 break;
2295 case FMT_GUNZIP:
2296 desc = gunzipOpts;
2297 mode = TCL_ZLIB_STREAM_INFLATE;
2298 format = TCL_ZLIB_FORMAT_GZIP;
2299 break;
2300 default:
2301 Tcl_Panic("should be unreachable");
2302 }
2303
2304 /*
2305 * Parse the options.
2306 */
2307
2308 for (i=3 ; i<objc ; i+=2) {
2309 if (Tcl_GetIndexFromObjStruct(interp, objv[i], desc,
2310 sizeof(OptDescriptor), "option", 0, &option) != TCL_OK) {
2311 return TCL_ERROR;
2312 }
2313 obj[desc[option].offset] = objv[i+1];
2314 }
2315
2316 /*
2317 * If a compression level was given, parse it (integral: 0..9). Otherwise
2318 * use the default.
2319 */
2320
2321 if (levelObj == NULL) {
2322 level = Z_DEFAULT_COMPRESSION;
2323 } else if (Tcl_GetIntFromObj(interp, levelObj, &level) != TCL_OK) {
2324 return TCL_ERROR;
2325 } else if (level < 0 || level > 9) {
2326 Tcl_SetObjResult(interp, Tcl_NewStringObj("level must be 0 to 9",-1));
2327 Tcl_SetErrorCode(interp, "TCL", "VALUE", "COMPRESSIONLEVEL", (char *)NULL);
2328 Tcl_AddErrorInfo(interp, "\n (in -level option)");
2329 return TCL_ERROR;
2330 }
2331
2332 /*
2333 * Construct the stream now we know its configuration.
2334 */
2335
2336 if (Tcl_ZlibStreamInit(interp, mode, format, level, gzipHeaderObj,
2337 &zh) != TCL_OK) {
2338 return TCL_ERROR;
2339 }
2340 if (compDictObj != NULL) {
2341 Tcl_ZlibStreamSetCompressionDictionary(zh, compDictObj);
2342 }
2343 Tcl_SetObjResult(interp, Tcl_ZlibStreamGetCommandName(zh));
2344 return TCL_OK;
2345 #undef compDictObj
2346 #undef gzipHeaderObj
2347 #undef levelObj
2348 }
2349
2350 /*
2351 *----------------------------------------------------------------------
2352 *
2353 * ZlibPushSubcmd --
2354 *
2355 * Implementation of the [zlib push] subcommand.
2356 *
2357 *----------------------------------------------------------------------
2358 */
2359
2360 static int
2361 ZlibPushSubcmd(
2362 Tcl_Interp *interp,
2363 int objc,
2364 Tcl_Obj *const objv[])
2365 {
2366 static const char *const stream_formats[] = {
2367 "compress", "decompress", "deflate", "gunzip", "gzip", "inflate",
2368 NULL
2369 };
2370 enum zlibFormats {
2371 FMT_COMPRESS, FMT_DECOMPRESS, FMT_DEFLATE, FMT_GUNZIP, FMT_GZIP,
2372 FMT_INFLATE
2373 };
2374 Tcl_Channel chan;
2375 int chanMode, format, mode = 0, level, i, option;
2376 static const char *const pushCompressOptions[] = {
2377 "-dictionary", "-header", "-level", NULL
2378 };
2379 static const char *const pushDecompressOptions[] = {
2380 "-dictionary", "-header", "-level", "-limit", NULL
2381 };
2382 const char *const *pushOptions = pushDecompressOptions;
2383 enum pushOptionsEnum {poDictionary, poHeader, poLevel, poLimit};
2384 Tcl_Obj *headerObj = NULL, *compDictObj = NULL;
2385 int limit = DEFAULT_BUFFER_SIZE;
2386 int dummy;
2387
2388 if (objc < 4) {
2389 Tcl_WrongNumArgs(interp, 2, objv, "mode channel ?options...?");
2390 return TCL_ERROR;
2391 }
2392
2393 if (Tcl_GetIndexFromObj(interp, objv[2], stream_formats, "mode", 0,
2394 &format) != TCL_OK) {
2395 return TCL_ERROR;
2396 }
2397 switch ((enum zlibFormats) format) {
2398 case FMT_DEFLATE:
2399 mode = TCL_ZLIB_STREAM_DEFLATE;
2400 format = TCL_ZLIB_FORMAT_RAW;
2401 pushOptions = pushCompressOptions;
2402 break;
2403 case FMT_INFLATE:
2404 mode = TCL_ZLIB_STREAM_INFLATE;
2405 format = TCL_ZLIB_FORMAT_RAW;
2406 break;
2407 case FMT_COMPRESS:
2408 mode = TCL_ZLIB_STREAM_DEFLATE;
2409 format = TCL_ZLIB_FORMAT_ZLIB;
2410 pushOptions = pushCompressOptions;
2411 break;
2412 case FMT_DECOMPRESS:
2413 mode = TCL_ZLIB_STREAM_INFLATE;
2414 format = TCL_ZLIB_FORMAT_ZLIB;
2415 break;
2416 case FMT_GZIP:
2417 mode = TCL_ZLIB_STREAM_DEFLATE;
2418 format = TCL_ZLIB_FORMAT_GZIP;
2419 pushOptions = pushCompressOptions;
2420 break;
2421 case FMT_GUNZIP:
2422 mode = TCL_ZLIB_STREAM_INFLATE;
2423 format = TCL_ZLIB_FORMAT_GZIP;
2424 break;
2425 default:
2426 Tcl_Panic("should be unreachable");
2427 }
2428
2429 if (TclGetChannelFromObj(interp, objv[3], &chan, &chanMode, 0) != TCL_OK){
2430 return TCL_ERROR;
2431 }
2432
2433 /*
2434 * Sanity checks.
2435 */
2436
2437 if (mode == TCL_ZLIB_STREAM_DEFLATE && !(chanMode & TCL_WRITABLE)) {
2438 Tcl_SetObjResult(interp, Tcl_NewStringObj(
2439 "compression may only be applied to writable channels", -1));
2440 Tcl_SetErrorCode(interp, "TCL", "ZIP", "UNWRITABLE", (char *)NULL);
2441 return TCL_ERROR;
2442 }
2443 if (mode == TCL_ZLIB_STREAM_INFLATE && !(chanMode & TCL_READABLE)) {
2444 Tcl_SetObjResult(interp, Tcl_NewStringObj(
2445 "decompression may only be applied to readable channels", -1));
2446 Tcl_SetErrorCode(interp, "TCL", "ZIP", "UNREADABLE", (char *)NULL);
2447 return TCL_ERROR;
2448 }
2449
2450 /*
2451 * Parse options.
2452 */
2453
2454 level = Z_DEFAULT_COMPRESSION;
2455 for (i=4 ; i<objc ; i++) {
2456 if (Tcl_GetIndexFromObj(interp, objv[i], pushOptions, "option", 0,
2457 &option) != TCL_OK) {
2458 return TCL_ERROR;
2459 }
2460 if (++i > objc-1) {
2461 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
2462 "value missing for %s option", pushOptions[option]));
2463 Tcl_SetErrorCode(interp, "TCL", "ZIP", "NOVAL", (char *)NULL);
2464 return TCL_ERROR;
2465 }
2466 switch ((enum pushOptionsEnum) option) {
2467 case poHeader:
2468 headerObj = objv[i];
2469 if (Tcl_DictObjSize(interp, headerObj, &dummy) != TCL_OK) {
2470 goto genericOptionError;
2471 }
2472 break;
2473 case poLevel:
2474 if (Tcl_GetIntFromObj(interp, objv[i], (int*) &level) != TCL_OK) {
2475 goto genericOptionError;
2476 }
2477 if (level < 0 || level > 9) {
2478 Tcl_SetObjResult(interp, Tcl_NewStringObj(
2479 "level must be 0 to 9", -1));
2480 Tcl_SetErrorCode(interp, "TCL", "VALUE", "COMPRESSIONLEVEL",
2481 (char *)NULL);
2482 goto genericOptionError;
2483 }
2484 break;
2485 case poLimit:
2486 if (Tcl_GetIntFromObj(interp, objv[i], (int*) &limit) != TCL_OK) {
2487 goto genericOptionError;
2488 }
2489 if (limit < 1 || limit > MAX_BUFFER_SIZE) {
2490 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
2491 "read ahead limit must be 1 to %d",
2492 MAX_BUFFER_SIZE));
2493 Tcl_SetErrorCode(interp, "TCL", "VALUE", "BUFFERSIZE", (char *)NULL);
2494 goto genericOptionError;
2495 }
2496 break;
2497 case poDictionary:
2498 if (format == TCL_ZLIB_FORMAT_GZIP) {
2499 Tcl_SetObjResult(interp, Tcl_NewStringObj(
2500 "a compression dictionary may not be set in the "
2501 "gzip format", -1));
2502 Tcl_SetErrorCode(interp, "TCL", "ZIP", "BADOPT", (char *)NULL);
2503 goto genericOptionError;
2504 }
2505 compDictObj = objv[i];
2506 break;
2507 }
2508 }
2509
2510 if (ZlibStackChannelTransform(interp, mode, format, level, limit, chan,
2511 headerObj, compDictObj) == NULL) {
2512 return TCL_ERROR;
2513 }
2514 Tcl_SetObjResult(interp, objv[3]);
2515 return TCL_OK;
2516
2517 genericOptionError:
2518 Tcl_AddErrorInfo(interp, "\n (in ");
2519 Tcl_AddErrorInfo(interp, pushOptions[option]);
2520 Tcl_AddErrorInfo(interp, " option)");
2521 return TCL_ERROR;
2522 }
2523
2524 /*
2525 *----------------------------------------------------------------------
2526 *
2527 * ZlibStreamCmd --
2528 *
2529 * Implementation of the commands returned by [zlib stream].
2530 *
2531 *----------------------------------------------------------------------
2532 */
2533
2534 static int
2535 ZlibStreamCmd(
2536 ClientData cd,
2537 Tcl_Interp *interp,
2538 int objc,
2539 Tcl_Obj *const objv[])
2540 {
2541 Tcl_ZlibStream zstream = (Tcl_ZlibStream)cd;
2542 int command, count, code;
2543 Tcl_Obj *obj;
2544 static const char *const cmds[] = {
2545 "add", "checksum", "close", "eof", "finalize", "flush",
2546 "fullflush", "get", "header", "put", "reset",
2547 NULL
2548 };
2549 enum zlibStreamCommands {
2550 zs_add, zs_checksum, zs_close, zs_eof, zs_finalize, zs_flush,
2551 zs_fullflush, zs_get, zs_header, zs_put, zs_reset
2552 };
2553
2554 if (objc < 2) {
2555 Tcl_WrongNumArgs(interp, 1, objv, "option data ?...?");
2556 return TCL_ERROR;
2557 }
2558
2559 if (Tcl_GetIndexFromObj(interp, objv[1], cmds, "option", 0,
2560 &command) != TCL_OK) {
2561 return TCL_ERROR;
2562 }
2563
2564 switch ((enum zlibStreamCommands) command) {
2565 case zs_add: /* $strm add ?$flushopt? $data */
2566 return ZlibStreamAddCmd(zstream, interp, objc, objv);
2567 case zs_header: /* $strm header */
2568 return ZlibStreamHeaderCmd(zstream, interp, objc, objv);
2569 case zs_put: /* $strm put ?$flushopt? $data */
2570 return ZlibStreamPutCmd(zstream, interp, objc, objv);
2571
2572 case zs_get: /* $strm get ?count? */
2573 if (objc > 3) {
2574 Tcl_WrongNumArgs(interp, 2, objv, "?count?");
2575 return TCL_ERROR;
2576 }
2577
2578 count = -1;
2579 if (objc >= 3) {
2580 if (Tcl_GetIntFromObj(interp, objv[2], &count) != TCL_OK) {
2581 return TCL_ERROR;
2582 }
2583 }
2584 TclNewObj(obj);
2585 code = Tcl_ZlibStreamGet(zstream, obj, count);
2586 if (code == TCL_OK) {
2587 Tcl_SetObjResult(interp, obj);
2588 } else {
2589 TclDecrRefCount(obj);
2590 }
2591 return code;
2592 case zs_flush: /* $strm flush */
2593 if (objc != 2) {
2594 Tcl_WrongNumArgs(interp, 2, objv, NULL);
2595 return TCL_ERROR;
2596 }
2597 TclNewObj(obj);
2598 Tcl_IncrRefCount(obj);
2599 code = Tcl_ZlibStreamPut(zstream, obj, Z_SYNC_FLUSH);
2600 TclDecrRefCount(obj);
2601 return code;
2602 case zs_fullflush: /* $strm fullflush */
2603 if (objc != 2) {
2604 Tcl_WrongNumArgs(interp, 2, objv, NULL);
2605 return TCL_ERROR;
2606 }
2607 TclNewObj(obj);
2608 Tcl_IncrRefCount(obj);
2609 code = Tcl_ZlibStreamPut(zstream, obj, Z_FULL_FLUSH);
2610 TclDecrRefCount(obj);
2611 return code;
2612 case zs_finalize: /* $strm finalize */
2613 if (objc != 2) {
2614 Tcl_WrongNumArgs(interp, 2, objv, NULL);
2615 return TCL_ERROR;
2616 }
2617
2618 /*
2619 * The flush commands slightly abuse the empty result obj as input
2620 * data.
2621 */
2622
2623 TclNewObj(obj);
2624 Tcl_IncrRefCount(obj);
2625 code = Tcl_ZlibStreamPut(zstream, obj, Z_FINISH);
2626 TclDecrRefCount(obj);
2627 return code;
2628 case zs_close: /* $strm close */
2629 if (objc != 2) {
2630 Tcl_WrongNumArgs(interp, 2, objv, NULL);
2631 return TCL_ERROR;
2632 }
2633 return Tcl_ZlibStreamClose(zstream);
2634 case zs_eof: /* $strm eof */
2635 if (objc != 2) {
2636 Tcl_WrongNumArgs(interp, 2, objv, NULL);
2637 return TCL_ERROR;
2638 }
2639 Tcl_SetObjResult(interp, Tcl_NewBooleanObj(Tcl_ZlibStreamEof(zstream)));
2640 return TCL_OK;
2641 case zs_checksum: /* $strm checksum */
2642 if (objc != 2) {
2643 Tcl_WrongNumArgs(interp, 2, objv, NULL);
2644 return TCL_ERROR;
2645 }
2646 Tcl_SetObjResult(interp, Tcl_NewWideIntObj(
2647 (unsigned int)Tcl_ZlibStreamChecksum(zstream)));
2648 return TCL_OK;
2649 case zs_reset: /* $strm reset */
2650 if (objc != 2) {
2651 Tcl_WrongNumArgs(interp, 2, objv, NULL);
2652 return TCL_ERROR;
2653 }
2654 return Tcl_ZlibStreamReset(zstream);
2655 }
2656
2657 return TCL_OK;
2658 }
2659
2660 static int
2661 ZlibStreamAddCmd(
2662 ClientData cd,
2663 Tcl_Interp *interp,
2664 int objc,
2665 Tcl_Obj *const objv[])
2666 {
2667 Tcl_ZlibStream zstream = (Tcl_ZlibStream)cd;
2668 int index, code, buffersize = -1, flush = -1, i;
2669 Tcl_Obj *obj, *compDictObj = NULL;
2670 static const char *const add_options[] = {
2671 "-buffer", "-dictionary", "-finalize", "-flush", "-fullflush", NULL
2672 };
2673 enum addOptions {
2674 ao_buffer, ao_dictionary, ao_finalize, ao_flush, ao_fullflush
2675 };
2676
2677 for (i=2; i<objc-1; i++) {
2678 if (Tcl_GetIndexFromObj(interp, objv[i], add_options, "option", 0,
2679 &index) != TCL_OK) {
2680 return TCL_ERROR;
2681 }
2682
2683 switch ((enum addOptions) index) {
2684 case ao_flush: /* -flush */
2685 if (flush >= 0) {
2686 flush = -2;
2687 } else {
2688 flush = Z_SYNC_FLUSH;
2689 }
2690 break;
2691 case ao_fullflush: /* -fullflush */
2692 if (flush >= 0) {
2693 flush = -2;
2694 } else {
2695 flush = Z_FULL_FLUSH;
2696 }
2697 break;
2698 case ao_finalize: /* -finalize */
2699 if (flush >= 0) {
2700 flush = -2;
2701 } else {
2702 flush = Z_FINISH;
2703 }
2704 break;
2705 case ao_buffer: /* -buffer */
2706 if (i == objc-2) {
2707 Tcl_SetObjResult(interp, Tcl_NewStringObj(
2708 "\"-buffer\" option must be followed by integer "
2709 "decompression buffersize", -1));
2710 Tcl_SetErrorCode(interp, "TCL", "ZIP", "NOVAL", (char *)NULL);
2711 return TCL_ERROR;
2712 }
2713 if (Tcl_GetIntFromObj(interp, objv[++i], &buffersize) != TCL_OK) {
2714 return TCL_ERROR;
2715 }
2716 if (buffersize < 1 || buffersize > MAX_BUFFER_SIZE) {
2717 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
2718 "buffer size must be 1 to %d",
2719 MAX_BUFFER_SIZE));
2720 Tcl_SetErrorCode(interp, "TCL", "VALUE", "BUFFERSIZE", (char *)NULL);
2721 return TCL_ERROR;
2722 }
2723 break;
2724 case ao_dictionary:
2725 if (i == objc-2) {
2726 Tcl_SetObjResult(interp, Tcl_NewStringObj(
2727 "\"-dictionary\" option must be followed by"
2728 " compression dictionary bytes", -1));
2729 Tcl_SetErrorCode(interp, "TCL", "ZIP", "NOVAL", (char *)NULL);
2730 return TCL_ERROR;
2731 }
2732 compDictObj = objv[++i];
2733 break;
2734 }
2735
2736 if (flush == -2) {
2737 Tcl_SetObjResult(interp, Tcl_NewStringObj(
2738 "\"-flush\", \"-fullflush\" and \"-finalize\" options"
2739 " are mutually exclusive", -1));
2740 Tcl_SetErrorCode(interp, "TCL", "ZIP", "EXCLUSIVE", (char *)NULL);
2741 return TCL_ERROR;
2742 }
2743 }
2744 if (flush == -1) {
2745 flush = 0;
2746 }
2747
2748 /*
2749 * Set the compression dictionary if requested.
2750 */
2751
2752 if (compDictObj != NULL) {
2753 int len;
2754
2755 (void) Tcl_GetByteArrayFromObj(compDictObj, &len);
2756 if (len == 0) {
2757 compDictObj = NULL;
2758 }
2759 Tcl_ZlibStreamSetCompressionDictionary(zstream, compDictObj);
2760 }
2761
2762 /*
2763 * Send the data to the stream core, along with any flushing directive.
2764 */
2765
2766 if (Tcl_ZlibStreamPut(zstream, objv[objc-1], flush) != TCL_OK) {
2767 return TCL_ERROR;
2768 }
2769
2770 /*
2771 * Get such data out as we can (up to the requested length).
2772 */
2773
2774 TclNewObj(obj);
2775 code = Tcl_ZlibStreamGet(zstream, obj, buffersize);
2776 if (code == TCL_OK) {
2777 Tcl_SetObjResult(interp, obj);
2778 } else {
2779 TclDecrRefCount(obj);
2780 }
2781 return code;
2782 }
2783
2784 static int
2785 ZlibStreamPutCmd(
2786 ClientData cd,
2787 Tcl_Interp *interp,
2788 int objc,
2789 Tcl_Obj *const objv[])
2790 {
2791 Tcl_ZlibStream zstream = (Tcl_ZlibStream)cd;
2792 int index, flush = -1, i;
2793 Tcl_Obj *compDictObj = NULL;
2794 static const char *const put_options[] = {
2795 "-dictionary", "-finalize", "-flush", "-fullflush", NULL
2796 };
2797 enum putOptions {
2798 po_dictionary, po_finalize, po_flush, po_fullflush
2799 };
2800
2801 for (i=2; i<objc-1; i++) {
2802 if (Tcl_GetIndexFromObj(interp, objv[i], put_options, "option", 0,
2803 &index) != TCL_OK) {
2804 return TCL_ERROR;
2805 }
2806
2807 switch ((enum putOptions) index) {
2808 case po_flush: /* -flush */
2809 if (flush >= 0) {
2810 flush = -2;
2811 } else {
2812 flush = Z_SYNC_FLUSH;
2813 }
2814 break;
2815 case po_fullflush: /* -fullflush */
2816 if (flush >= 0) {
2817 flush = -2;
2818 } else {
2819 flush = Z_FULL_FLUSH;
2820 }
2821 break;
2822 case po_finalize: /* -finalize */
2823 if (flush >= 0) {
2824 flush = -2;
2825 } else {
2826 flush = Z_FINISH;
2827 }
2828 break;
2829 case po_dictionary:
2830 if (i == objc-2) {
2831 Tcl_SetObjResult(interp, Tcl_NewStringObj(
2832 "\"-dictionary\" option must be followed by"
2833 " compression dictionary bytes", -1));
2834 Tcl_SetErrorCode(interp, "TCL", "ZIP", "NOVAL", (char *)NULL);
2835 return TCL_ERROR;
2836 }
2837 compDictObj = objv[++i];
2838 break;
2839 }
2840 if (flush == -2) {
2841 Tcl_SetObjResult(interp, Tcl_NewStringObj(
2842 "\"-flush\", \"-fullflush\" and \"-finalize\" options"
2843 " are mutually exclusive", -1));
2844 Tcl_SetErrorCode(interp, "TCL", "ZIP", "EXCLUSIVE", (char *)NULL);
2845 return TCL_ERROR;
2846 }
2847 }
2848 if (flush == -1) {
2849 flush = 0;
2850 }
2851
2852 /*
2853 * Set the compression dictionary if requested.
2854 */
2855
2856 if (compDictObj != NULL) {
2857 int len;
2858
2859 (void)Tcl_GetByteArrayFromObj(compDictObj, &len);
2860 if (len == 0) {
2861 compDictObj = NULL;
2862 }
2863 Tcl_ZlibStreamSetCompressionDictionary(zstream, compDictObj);
2864 }
2865
2866 /*
2867 * Send the data to the stream core, along with any flushing directive.
2868 */
2869
2870 return Tcl_ZlibStreamPut(zstream, objv[objc-1], flush);
2871 }
2872
2873 static int
2874 ZlibStreamHeaderCmd(
2875 ClientData cd,
2876 Tcl_Interp *interp,
2877 int objc,
2878 Tcl_Obj *const objv[])
2879 {
2880 ZlibStreamHandle *zshPtr = (ZlibStreamHandle *)cd;
2881 Tcl_Obj *resultObj;
2882
2883 if (objc != 2) {
2884 Tcl_WrongNumArgs(interp, 2, objv, NULL);
2885 return TCL_ERROR;
2886 } else if (zshPtr->mode != TCL_ZLIB_STREAM_INFLATE
2887 || zshPtr->format != TCL_ZLIB_FORMAT_GZIP) {
2888 Tcl_SetObjResult(interp, Tcl_NewStringObj(
2889 "only gunzip streams can produce header information", -1));
2890 Tcl_SetErrorCode(interp, "TCL", "ZIP", "BADOP", (char *)NULL);
2891 return TCL_ERROR;
2892 }
2893
2894 TclNewObj(resultObj);
2895 ExtractHeader(&zshPtr->gzHeaderPtr->header, resultObj);
2896 Tcl_SetObjResult(interp, resultObj);
2897 return TCL_OK;
2898 }
2899
2900 /*
2901 *----------------------------------------------------------------------
2902 * Set of functions to support channel stacking.
2903 *----------------------------------------------------------------------
2904 *
2905 * ZlibTransformClose --
2906 *
2907 * How to shut down a stacked compressing/decompressing transform.
2908 *
2909 *----------------------------------------------------------------------
2910 */
2911
2912 static int
2913 ZlibTransformClose(
2914 ClientData instanceData,
2915 Tcl_Interp *interp)
2916 {
2917 ZlibChannelData *cd = (ZlibChannelData *)instanceData;
2918 int e, result = TCL_OK;
2919 int written;
2920
2921 /*
2922 * Delete the support timer.
2923 */
2924
2925 ZlibTransformEventTimerKill(cd);
2926
2927 /*
2928 * Flush any data waiting to be compressed.
2929 */
2930
2931 if (cd->mode == TCL_ZLIB_STREAM_DEFLATE) {
2932 cd->outStream.avail_in = 0;
2933 do {
2934 e = Deflate(&cd->outStream, cd->outBuffer, cd->outAllocated,
2935 Z_FINISH, &written);
2936
2937 /*
2938 * Can't be sure that deflate() won't declare the buffer to be
2939 * full (with Z_BUF_ERROR) so handle that case.
2940 */
2941
2942 if (e == Z_BUF_ERROR) {
2943 e = Z_OK;
2944 written = cd->outAllocated;
2945 }
2946 if (e != Z_OK && e != Z_STREAM_END) {
2947 /* TODO: is this the right way to do errors on close? */
2948 if (!TclInThreadExit()) {
2949 ConvertError(interp, e, cd->outStream.adler);
2950 }
2951 result = TCL_ERROR;
2952 break;
2953 }
2954 if (written && Tcl_WriteRaw(cd->parent, cd->outBuffer, written) < 0) {
2955 /* TODO: is this the right way to do errors on close?
2956 * Note: when close is called from FinalizeIOSubsystem then
2957 * interp may be NULL */
2958 if (!TclInThreadExit() && interp) {
2959 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
2960 "error while finalizing file: %s",
2961 Tcl_PosixError(interp)));
2962 }
2963 result = TCL_ERROR;
2964 break;
2965 }
2966 } while (e != Z_STREAM_END);
2967 (void) deflateEnd(&cd->outStream);
2968 } else {
2969 /*
2970 * If we have unused bytes from the read input (overshot by
2971 * Z_STREAM_END or on possible error), unget them back to the parent
2972 * channel, so that they appear as not being read yet.
2973 */
2974 if (cd->inStream.avail_in) {
2975 Tcl_Ungets (cd->parent, (char *)cd->inStream.next_in, cd->inStream.avail_in, 0);
2976 }
2977
2978 (void) inflateEnd(&cd->inStream);
2979 }
2980
2981 /*
2982 * Release all memory.
2983 */
2984
2985 if (cd->compDictObj) {
2986 Tcl_DecrRefCount(cd->compDictObj);
2987 cd->compDictObj = NULL;
2988 }
2989
2990 if (cd->inBuffer) {
2991 ckfree(cd->inBuffer);
2992 cd->inBuffer = NULL;
2993 }
2994 if (cd->outBuffer) {
2995 ckfree(cd->outBuffer);
2996 cd->outBuffer = NULL;
2997 }
2998 ckfree(cd);
2999 return result;
3000 }
3001
3002 /*
3003 *----------------------------------------------------------------------
3004 *
3005 * ZlibTransformInput --
3006 *
3007 * Reader filter that does decompression.
3008 *
3009 *----------------------------------------------------------------------
3010 */
3011
3012 static int
3013 ZlibTransformInput(
3014 ClientData instanceData,
3015 char *buf,
3016 int toRead,
3017 int *errorCodePtr)
3018 {
3019 ZlibChannelData *cd = (ZlibChannelData *)instanceData;
3020 Tcl_DriverInputProc *inProc =
3021 Tcl_ChannelInputProc(Tcl_GetChannelType(cd->parent));
3022 int readBytes, gotBytes;
3023
3024 if (cd->mode == TCL_ZLIB_STREAM_DEFLATE) {
3025 return inProc(Tcl_GetChannelInstanceData(cd->parent), buf, toRead,
3026 errorCodePtr);
3027 }
3028
3029 gotBytes = 0;
3030 readBytes = cd->inStream.avail_in; /* how many bytes in buffer now */
3031 while (!(cd->flags & STREAM_DONE) && toRead > 0) {
3032 int n, decBytes;
3033
3034 /* if starting from scratch or continuation after full decompression */
3035 if (!cd->inStream.avail_in) {
3036 /* buffer to start, we can read to whole available buffer */
3037 cd->inStream.next_in = (Bytef *) cd->inBuffer;
3038 }
3039 /*
3040 * If done - no read needed anymore, check we have to copy rest of
3041 * decompressed data, otherwise return with size (or 0 for Eof)
3042 */
3043 if (cd->flags & STREAM_DECOMPRESS) {
3044 goto copyDecompressed;
3045 }
3046 /*
3047 * The buffer is exhausted, but the caller wants even more. We now
3048 * have to go to the underlying channel, get more bytes and then
3049 * transform them for delivery. We may not get what we want (full EOF
3050 * or temporarily out of data).
3051 */
3052
3053 /* Check free buffer size and adjust size of next chunk to read. */
3054 n = cd->inAllocated - ((char *)cd->inStream.next_in - cd->inBuffer);
3055 if (n <= 0) {
3056 /* Normally unreachable: not enough input buffer to uncompress.
3057 * Todo: firstly try to realloc inBuffer upto MAX_BUFFER_SIZE.
3058 */
3059 *errorCodePtr = ENOBUFS;
3060 return -1;
3061 }
3062 if (n > cd->readAheadLimit) {
3063 n = cd->readAheadLimit;
3064 }
3065 readBytes = Tcl_ReadRaw(cd->parent, (char *)cd->inStream.next_in, n);
3066
3067 /*
3068 * Three cases here:
3069 * 1. Got some data from the underlying channel (readBytes > 0) so
3070 * it should be fed through the decompression engine.
3071 * 2. Got an error (readBytes < 0) which we should report up except
3072 * for the case where we can convert it to a short read.
3073 * 3. Got an end-of-data from EOF or blocking (readBytes == 0). If
3074 * it is EOF, try flushing the data out of the decompressor.
3075 */
3076
3077 if (readBytes < 0) {
3078
3079 /* See ReflectInput() in tclIORTrans.c */
3080 if (Tcl_InputBlocked(cd->parent) && (gotBytes > 0)) {
3081 break;
3082 }
3083
3084 *errorCodePtr = Tcl_GetErrno();
3085 return -1;
3086 }
3087
3088 /* more bytes (or Eof if readBytes == 0) */
3089 cd->inStream.avail_in += readBytes;
3090
3091 copyDecompressed:
3092
3093 /*
3094 * Transform the read chunk, if not empty. Anything we get
3095 * back is a transformation result to be put into our buffers, and
3096 * the next iteration will put it into the result.
3097 * For the case readBytes is 0 which signaling Eof in parent, the
3098 * partial data waiting is converted and returned.
3099 */
3100
3101 decBytes = ResultDecompress(cd, buf, toRead,
3102 (readBytes != 0) ? Z_NO_FLUSH : Z_SYNC_FLUSH,
3103 errorCodePtr);
3104 if (decBytes == -1) {
3105 return -1;
3106 }
3107 gotBytes += decBytes;
3108 buf += decBytes;
3109 toRead -= decBytes;
3110
3111 if (((decBytes == 0) || (cd->flags & STREAM_DECOMPRESS))) {
3112 /*
3113 * The drain delivered nothing (or buffer too small to decompress).
3114 * Time to deliver what we've got.
3115 */
3116 if (!gotBytes && !(cd->flags & STREAM_DONE)) {
3117 /* if no-data, but not ready - avoid signaling Eof,
3118 * continue in blocking mode, otherwise EAGAIN */
3119 if (Tcl_InputBlocked(cd->parent)) {
3120 continue;
3121 }
3122 *errorCodePtr = EAGAIN;
3123 return -1;
3124 }
3125 break;
3126 }
3127
3128 /*
3129 * Loop until the request is satisfied (or no data available from
3130 * above, possibly EOF).
3131 */
3132 }
3133
3134 return gotBytes;
3135 }
3136
3137 /*
3138 *----------------------------------------------------------------------
3139 *
3140 * ZlibTransformOutput --
3141 *
3142 * Writer filter that does compression.
3143 *
3144 *----------------------------------------------------------------------
3145 */
3146
3147 static int
3148 ZlibTransformOutput(
3149 ClientData instanceData,
3150 const char *buf,
3151 int toWrite,
3152 int *errorCodePtr)
3153 {
3154 ZlibChannelData *cd = (ZlibChannelData *)instanceData;
3155 Tcl_DriverOutputProc *outProc =
3156 Tcl_ChannelOutputProc(Tcl_GetChannelType(cd->parent));
3157 int e;
3158 int produced;
3159 Tcl_Obj *errObj;
3160
3161 if (cd->mode == TCL_ZLIB_STREAM_INFLATE) {
3162 return outProc(Tcl_GetChannelInstanceData(cd->parent), buf, toWrite,
3163 errorCodePtr);
3164 }
3165
3166 /*
3167 * No zero-length writes. Flushes must be explicit.
3168 */
3169
3170 if (toWrite == 0) {
3171 return 0;
3172 }
3173
3174 cd->outStream.next_in = (Bytef *) buf;
3175 cd->outStream.avail_in = toWrite;
3176 while (cd->outStream.avail_in > 0) {
3177 e = Deflate(&cd->outStream, cd->outBuffer, cd->outAllocated,
3178 Z_NO_FLUSH, &produced);
3179 if (e != Z_OK || produced == 0) {
3180 break;
3181 }
3182
3183 if (Tcl_WriteRaw(cd->parent, cd->outBuffer, produced) < 0) {
3184 *errorCodePtr = Tcl_GetErrno();
3185 return -1;
3186 }
3187 }
3188
3189 if (e == Z_OK) {
3190 return toWrite - cd->outStream.avail_in;
3191 }
3192
3193 errObj = Tcl_NewListObj(0, NULL);
3194 Tcl_ListObjAppendElement(NULL, errObj, Tcl_NewStringObj("-errorcode",-1));
3195 Tcl_ListObjAppendElement(NULL, errObj,
3196 ConvertErrorToList(e, cd->outStream.adler));
3197 Tcl_ListObjAppendElement(NULL, errObj,
3198 Tcl_NewStringObj(cd->outStream.msg, -1));
3199 Tcl_SetChannelError(cd->parent, errObj);
3200 *errorCodePtr = EINVAL;
3201 return -1;
3202 }
3203
3204 /*
3205 *----------------------------------------------------------------------
3206 *
3207 * ZlibTransformFlush --
3208 *
3209 * How to perform a flush of a compressing transform.
3210 *
3211 *----------------------------------------------------------------------
3212 */
3213
3214 static int
3215 ZlibTransformFlush(
3216 Tcl_Interp *interp,
3217 ZlibChannelData *cd,
3218 int flushType)
3219 {
3220 int e;
3221 int len;
3222
3223 cd->outStream.avail_in = 0;
3224 do {
3225 /*
3226 * Get the bytes to go out of the compression engine.
3227 */
3228
3229 e = Deflate(&cd->outStream, cd->outBuffer, cd->outAllocated,
3230 flushType, &len);
3231 if (e != Z_OK && e != Z_BUF_ERROR) {
3232 ConvertError(interp, e, cd->outStream.adler);
3233 return TCL_ERROR;
3234 }
3235
3236 /*
3237 * Write the bytes we've received to the next layer.
3238 */
3239
3240 if (len > 0 && Tcl_WriteRaw(cd->parent, cd->outBuffer, len) < 0) {
3241 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
3242 "problem flushing channel: %s",
3243 Tcl_PosixError(interp)));
3244 return TCL_ERROR;
3245 }
3246
3247 /*
3248 * If we get to this point, either we're in the Z_OK or the
3249 * Z_BUF_ERROR state. In the former case, we're done. In the latter
3250 * case, it's because there's more bytes to go than would fit in the
3251 * buffer we provided, and we need to go round again to get some more.
3252 *
3253 * We also stop the loop if we would have done a zero-length write.
3254 * Those can cause problems at the OS level.
3255 */
3256 } while (len > 0 && e == Z_BUF_ERROR);
3257 return TCL_OK;
3258 }
3259
3260 /*
3261 *----------------------------------------------------------------------
3262 *
3263 * ZlibTransformSetOption --
3264 *
3265 * Writing side of [fconfigure] on our channel.
3266 *
3267 *----------------------------------------------------------------------
3268 */
3269
3270 static int
3271 ZlibTransformSetOption( /* not used */
3272 ClientData instanceData,
3273 Tcl_Interp *interp,
3274 const char *optionName,
3275 const char *value)
3276 {
3277 ZlibChannelData *cd = (ZlibChannelData *)instanceData;
3278 Tcl_DriverSetOptionProc *setOptionProc =
3279 Tcl_ChannelSetOptionProc(Tcl_GetChannelType(cd->parent));
3280 static const char *compressChanOptions = "dictionary flush";
3281 static const char *gzipChanOptions = "flush";
3282 static const char *decompressChanOptions = "dictionary limit";
3283 static const char *gunzipChanOptions = "flush limit";
3284 int haveFlushOpt = (cd->mode == TCL_ZLIB_STREAM_DEFLATE);
3285
3286 if (optionName && (strcmp(optionName, "-dictionary") == 0)
3287 && (cd->format != TCL_ZLIB_FORMAT_GZIP)) {
3288 Tcl_Obj *compDictObj;
3289 int code;
3290
3291 TclNewStringObj(compDictObj, value, strlen(value));
3292 Tcl_IncrRefCount(compDictObj);
3293 (void)Tcl_GetByteArrayFromObj(compDictObj, NULL);
3294 if (cd->compDictObj) {
3295 TclDecrRefCount(cd->compDictObj);
3296 }
3297 cd->compDictObj = compDictObj;
3298 code = Z_OK;
3299 if (cd->mode == TCL_ZLIB_STREAM_DEFLATE) {
3300 code = SetDeflateDictionary(&cd->outStream, compDictObj);
3301 if (code != Z_OK) {
3302 ConvertError(interp, code, cd->outStream.adler);
3303 return TCL_ERROR;
3304 }
3305 } else if (cd->format == TCL_ZLIB_FORMAT_RAW) {
3306 code = SetInflateDictionary(&cd->inStream, compDictObj);
3307 if (code != Z_OK) {
3308 ConvertError(interp, code, cd->inStream.adler);
3309 return TCL_ERROR;
3310 }
3311 }
3312 return TCL_OK;
3313 }
3314
3315 if (haveFlushOpt) {
3316 if (optionName && strcmp(optionName, "-flush") == 0) {
3317 int flushType;
3318
3319 if (value[0] == 'f' && strcmp(value, "full") == 0) {
3320 flushType = Z_FULL_FLUSH;
3321 } else if (value[0] == 's' && strcmp(value, "sync") == 0) {
3322 flushType = Z_SYNC_FLUSH;
3323 } else {
3324 Tcl_SetObjResult(interp, Tcl_ObjPrintf(
3325 "unknown -flush type \"%s\": must be full or sync",
3326 value));
3327 Tcl_SetErrorCode(interp, "TCL", "VALUE", "FLUSH", (char *)NULL);
3328 return TCL_ERROR;
3329 }
3330
3331 /*
3332 * Try to actually do the flush now.
3333 */
3334
3335 return ZlibTransformFlush(interp, cd, flushType);
3336 }
3337 } else {
3338 if (optionName && strcmp(optionName, "-limit") == 0) {
3339 int newLimit;
3340
3341 if (Tcl_GetInt(interp, value, &newLimit) != TCL_OK) {
3342 return TCL_ERROR;
3343 } else if (newLimit < 1 || newLimit > MAX_BUFFER_SIZE) {
3344 Tcl_SetObjResult(interp, Tcl_NewStringObj(
3345 "-limit must be between 1 and 65536", -1));
3346 Tcl_SetErrorCode(interp, "TCL", "VALUE", "READLIMIT", (char *)NULL);
3347 return TCL_ERROR;
3348 }
3349 }
3350 }
3351
3352 if (setOptionProc == NULL) {
3353 if (cd->format == TCL_ZLIB_FORMAT_GZIP) {
3354 return Tcl_BadChannelOption(interp, optionName,
3355 (cd->mode == TCL_ZLIB_STREAM_DEFLATE)
3356 ? gzipChanOptions : gunzipChanOptions);
3357 } else {
3358 return Tcl_BadChannelOption(interp, optionName,
3359 (cd->mode == TCL_ZLIB_STREAM_DEFLATE)
3360 ? compressChanOptions : decompressChanOptions);
3361 }
3362 }
3363
3364 /*
3365 * Pass all unknown options down, to deeper transforms and/or the base
3366 * channel.
3367 */
3368
3369 return setOptionProc(Tcl_GetChannelInstanceData(cd->parent), interp,
3370 optionName, value);
3371 }
3372
3373 /*
3374 *----------------------------------------------------------------------
3375 *
3376 * ZlibTransformGetOption --
3377 *
3378 * Reading side of [fconfigure] on our channel.
3379 *
3380 *----------------------------------------------------------------------
3381 */
3382
3383 static int
3384 ZlibTransformGetOption(
3385 ClientData instanceData,
3386 Tcl_Interp *interp,
3387 const char *optionName,
3388 Tcl_DString *dsPtr)
3389 {
3390 ZlibChannelData *cd = (ZlibChannelData *)instanceData;
3391 Tcl_DriverGetOptionProc *getOptionProc =
3392 Tcl_ChannelGetOptionProc(Tcl_GetChannelType(cd->parent));
3393 static const char *compressChanOptions = "checksum dictionary";
3394 static const char *gzipChanOptions = "checksum";
3395 static const char *decompressChanOptions = "checksum dictionary limit";
3396 static const char *gunzipChanOptions = "checksum header limit";
3397
3398 /*
3399 * The "crc" option reports the current CRC (calculated with the Adler32
3400 * or CRC32 algorithm according to the format) given the data that has
3401 * been processed so far.
3402 */
3403
3404 if (optionName == NULL || strcmp(optionName, "-checksum") == 0) {
3405 uLong crc;
3406 char buf[12];
3407
3408 if (cd->mode == TCL_ZLIB_STREAM_DEFLATE) {
3409 crc = cd->outStream.adler;
3410 } else {
3411 crc = cd->inStream.adler;
3412 }
3413
3414 snprintf(buf, sizeof(buf), "%lu", crc);
3415 if (optionName == NULL) {
3416 Tcl_DStringAppendElement(dsPtr, "-checksum");
3417 Tcl_DStringAppendElement(dsPtr, buf);
3418 } else {
3419 Tcl_DStringAppend(dsPtr, buf, -1);
3420 return TCL_OK;
3421 }
3422 }
3423
3424 if ((cd->format != TCL_ZLIB_FORMAT_GZIP) &&
3425 (optionName == NULL || strcmp(optionName, "-dictionary") == 0)) {
3426 /*
3427 * Embedded NUL bytes are ok; they'll be C080-encoded.
3428 */
3429
3430 if (optionName == NULL) {
3431 Tcl_DStringAppendElement(dsPtr, "-dictionary");
3432 if (cd->compDictObj) {
3433 Tcl_DStringAppendElement(dsPtr,
3434 TclGetString(cd->compDictObj));
3435 } else {
3436 Tcl_DStringAppendElement(dsPtr, "");
3437 }
3438 } else {
3439 if (cd->compDictObj) {
3440 int length;
3441 const char *str = TclGetStringFromObj(cd->compDictObj, &length);
3442
3443 Tcl_DStringAppend(dsPtr, str, length);
3444 }
3445 return TCL_OK;
3446 }
3447 }
3448
3449 /*
3450 * The "header" option, which is only valid on inflating gzip channels,
3451 * reports the header that has been read from the start of the stream.
3452 */
3453
3454 if ((cd->flags & IN_HEADER) && ((optionName == NULL) ||
3455 (strcmp(optionName, "-header") == 0))) {
3456 Tcl_Obj *tmpObj;
3457
3458 TclNewObj(tmpObj);
3459 ExtractHeader(&cd->inHeader.header, tmpObj);
3460 if (optionName == NULL) {
3461 Tcl_DStringAppendElement(dsPtr, "-header");
3462 Tcl_DStringAppendElement(dsPtr, TclGetString(tmpObj));
3463 Tcl_DecrRefCount(tmpObj);
3464 } else {
3465 TclDStringAppendObj(dsPtr, tmpObj);
3466 Tcl_DecrRefCount(tmpObj);
3467 return TCL_OK;
3468 }
3469 }
3470
3471 /*
3472 * Now we do the standard processing of the stream we wrapped.
3473 */
3474
3475 if (getOptionProc) {
3476 return getOptionProc(Tcl_GetChannelInstanceData(cd->parent),
3477 interp, optionName, dsPtr);
3478 }
3479 if (optionName == NULL) {
3480 return TCL_OK;
3481 }
3482 if (cd->format == TCL_ZLIB_FORMAT_GZIP) {
3483 return Tcl_BadChannelOption(interp, optionName,
3484 (cd->mode == TCL_ZLIB_STREAM_DEFLATE)
3485 ? gzipChanOptions : gunzipChanOptions);
3486 } else {
3487 return Tcl_BadChannelOption(interp, optionName,
3488 (cd->mode == TCL_ZLIB_STREAM_DEFLATE)
3489 ? compressChanOptions : decompressChanOptions);
3490 }
3491 }
3492
3493 /*
3494 *----------------------------------------------------------------------
3495 *
3496 * ZlibTransformWatch, ZlibTransformEventHandler --
3497 *
3498 * If we have data pending, trigger a readable event after a short time
3499 * (in order to allow a real event to catch up).
3500 *
3501 *----------------------------------------------------------------------
3502 */
3503
3504 static void
3505 ZlibTransformWatch(
3506 ClientData instanceData,
3507 int mask)
3508 {
3509 ZlibChannelData *cd = (ZlibChannelData *)instanceData;
3510 Tcl_DriverWatchProc *watchProc;
3511
3512 /*
3513 * This code is based on the code in tclIORTrans.c
3514 */
3515
3516 watchProc = Tcl_ChannelWatchProc(Tcl_GetChannelType(cd->parent));
3517 watchProc(Tcl_GetChannelInstanceData(cd->parent), mask);
3518
3519 if (!(mask & TCL_READABLE) || !(cd->flags & STREAM_DECOMPRESS)) {
3520 ZlibTransformEventTimerKill(cd);
3521 } else if (cd->timer == NULL) {
3522 cd->timer = Tcl_CreateTimerHandler(SYNTHETIC_EVENT_TIME,
3523 ZlibTransformTimerRun, cd);
3524 }
3525 }
3526
3527 static int
3528 ZlibTransformEventHandler(
3529 ClientData instanceData,
3530 int interestMask)
3531 {
3532 ZlibChannelData *cd = (ZlibChannelData *)instanceData;
3533
3534 ZlibTransformEventTimerKill(cd);
3535 return interestMask;
3536 }
3537
3538 static inline void
3539 ZlibTransformEventTimerKill(
3540 ZlibChannelData *cd)
3541 {
3542 if (cd->timer != NULL) {
3543 Tcl_DeleteTimerHandler(cd->timer);
3544 cd->timer = NULL;
3545 }
3546 }
3547
3548 static void
3549 ZlibTransformTimerRun(
3550 ClientData clientData)
3551 {
3552 ZlibChannelData *cd = (ZlibChannelData *)clientData;
3553
3554 cd->timer = NULL;
3555 Tcl_NotifyChannel(cd->chan, TCL_READABLE);
3556 }
3557
3558 /*
3559 *----------------------------------------------------------------------
3560 *
3561 * ZlibTransformGetHandle --
3562 *
3563 * Anything that needs the OS handle is told to get it from what we are
3564 * stacked on top of.
3565 *
3566 *----------------------------------------------------------------------
3567 */
3568
3569 static int
3570 ZlibTransformGetHandle(
3571 ClientData instanceData,
3572 int direction,
3573 ClientData *handlePtr)
3574 {
3575 ZlibChannelData *cd = (ZlibChannelData *)instanceData;
3576
3577 return Tcl_GetChannelHandle(cd->parent, direction, handlePtr);
3578 }
3579
3580 /*
3581 *----------------------------------------------------------------------
3582 *
3583 * ZlibTransformBlockMode --
3584 *
3585 * We need to keep track of the blocking mode; it changes our behavior.
3586 *
3587 *----------------------------------------------------------------------
3588 */
3589
3590 static int
3591 ZlibTransformBlockMode(
3592 ClientData instanceData,
3593 int mode)
3594 {
3595 ZlibChannelData *cd = (ZlibChannelData *)instanceData;
3596
3597 if (mode == TCL_MODE_NONBLOCKING) {
3598 cd->flags |= ASYNC;
3599 } else {
3600 cd->flags &= ~ASYNC;
3601 }
3602 return TCL_OK;
3603 }
3604
3605 /*
3606 *----------------------------------------------------------------------
3607 *
3608 * ZlibStackChannelTransform --
3609 *
3610 * Stacks either compression or decompression onto a channel.
3611 *
3612 * Results:
3613 * The stacked channel, or NULL if there was an error.
3614 *
3615 *----------------------------------------------------------------------
3616 */
3617
3618 static Tcl_Channel
3619 ZlibStackChannelTransform(
3620 Tcl_Interp *interp, /* Where to write error messages. */
3621 int mode, /* Whether this is a compressing transform
3622 * (TCL_ZLIB_STREAM_DEFLATE) or a
3623 * decompressing transform
3624 * (TCL_ZLIB_STREAM_INFLATE). Note that
3625 * compressing transforms require that the
3626 * channel is writable, and decompressing
3627 * transforms require that the channel is
3628 * readable. */
3629 int format, /* One of the TCL_ZLIB_FORMAT_* values that
3630 * indicates what compressed format to allow.
3631 * TCL_ZLIB_FORMAT_AUTO is only supported for
3632 * decompressing transforms. */
3633 int level, /* What compression level to use. Ignored for
3634 * decompressing transforms. */
3635 int limit, /* The limit on the number of bytes to read
3636 * ahead; always at least 1. */
3637 Tcl_Channel channel, /* The channel to attach to. */
3638 Tcl_Obj *gzipHeaderDictPtr, /* A description of header to use, or NULL to
3639 * use a default. Ignored if not compressing
3640 * to produce gzip-format data. */
3641 Tcl_Obj *compDictObj) /* Byte-array object containing compression
3642 * dictionary (not dictObj!) to use if
3643 * necessary. */
3644 {
3645 ZlibChannelData *cd = (ZlibChannelData *)ckalloc(sizeof(ZlibChannelData));
3646 Tcl_Channel chan;
3647 int wbits = 0;
3648
3649 if (mode != TCL_ZLIB_STREAM_DEFLATE && mode != TCL_ZLIB_STREAM_INFLATE) {
3650 Tcl_Panic("unknown mode: %d", mode);
3651 }
3652
3653 memset(cd, 0, sizeof(ZlibChannelData));
3654 cd->mode = mode;
3655 cd->format = format;
3656 cd->readAheadLimit = limit;
3657
3658 if (format == TCL_ZLIB_FORMAT_GZIP || format == TCL_ZLIB_FORMAT_AUTO) {
3659 if (mode == TCL_ZLIB_STREAM_DEFLATE) {
3660 if (gzipHeaderDictPtr) {
3661 cd->flags |= OUT_HEADER;
3662 if (GenerateHeader(interp, gzipHeaderDictPtr, &cd->outHeader,
3663 NULL) != TCL_OK) {
3664 goto error;
3665 }
3666 }
3667 } else {
3668 cd->flags |= IN_HEADER;
3669 cd->inHeader.header.name = (Bytef *)
3670 &cd->inHeader.nativeFilenameBuf;
3671 cd->inHeader.header.name_max = MAXPATHLEN - 1;
3672 cd->inHeader.header.comment = (Bytef *)
3673 &cd->inHeader.nativeCommentBuf;
3674 cd->inHeader.header.comm_max = MAX_COMMENT_LEN - 1;
3675 }
3676 }
3677
3678 if (compDictObj != NULL) {
3679 cd->compDictObj = Tcl_DuplicateObj(compDictObj);
3680 Tcl_IncrRefCount(cd->compDictObj);
3681 Tcl_GetByteArrayFromObj(cd->compDictObj, NULL);
3682 }
3683
3684 if (format == TCL_ZLIB_FORMAT_RAW) {
3685 wbits = WBITS_RAW;
3686 } else if (format == TCL_ZLIB_FORMAT_ZLIB) {
3687 wbits = WBITS_ZLIB;
3688 } else if (format == TCL_ZLIB_FORMAT_GZIP) {
3689 wbits = WBITS_GZIP;
3690 } else if (format == TCL_ZLIB_FORMAT_AUTO) {
3691 wbits = WBITS_AUTODETECT;
3692 } else {
3693 Tcl_Panic("bad format: %d", format);
3694 }
3695
3696 /*
3697 * Initialize input inflater or the output deflater.
3698 */
3699
3700 if (mode == TCL_ZLIB_STREAM_INFLATE) {
3701 if (inflateInit2(&cd->inStream, wbits) != Z_OK) {
3702 goto error;
3703 }
3704 cd->inAllocated = DEFAULT_BUFFER_SIZE;
3705 if (cd->inAllocated < cd->readAheadLimit) {
3706 cd->inAllocated = cd->readAheadLimit;
3707 }
3708 cd->inBuffer = (char *)ckalloc(cd->inAllocated);
3709 if (cd->flags & IN_HEADER) {
3710 if (inflateGetHeader(&cd->inStream, &cd->inHeader.header) != Z_OK) {
3711 goto error;
3712 }
3713 }
3714 if (cd->format == TCL_ZLIB_FORMAT_RAW && cd->compDictObj) {
3715 if (SetInflateDictionary(&cd->inStream, cd->compDictObj) != Z_OK) {
3716 goto error;
3717 }
3718 }
3719 } else {
3720 if (deflateInit2(&cd->outStream, level, Z_DEFLATED, wbits,
3721 MAX_MEM_LEVEL, Z_DEFAULT_STRATEGY) != Z_OK) {
3722 goto error;
3723 }
3724 cd->outAllocated = DEFAULT_BUFFER_SIZE;
3725 cd->outBuffer = (char *)ckalloc(cd->outAllocated);
3726 if (cd->flags & OUT_HEADER) {
3727 if (deflateSetHeader(&cd->outStream, &cd->outHeader.header) != Z_OK) {
3728 goto error;
3729 }
3730 }
3731 if (cd->compDictObj) {
3732 if (SetDeflateDictionary(&cd->outStream, cd->compDictObj) != Z_OK) {
3733 goto error;
3734 }
3735 }
3736 }
3737
3738 chan = Tcl_StackChannel(interp, &zlibChannelType, cd,
3739 Tcl_GetChannelMode(channel), channel);
3740 if (chan == NULL) {
3741 goto error;
3742 }
3743 cd->chan = chan;
3744 cd->parent = Tcl_GetStackedChannel(chan);
3745 Tcl_SetObjResult(interp, Tcl_NewStringObj(Tcl_GetChannelName(chan), -1));
3746 return chan;
3747
3748 error:
3749 if (cd->inBuffer) {
3750 ckfree(cd->inBuffer);
3751 inflateEnd(&cd->inStream);
3752 }
3753 if (cd->outBuffer) {
3754 ckfree(cd->outBuffer);
3755 deflateEnd(&cd->outStream);
3756 }
3757 if (cd->compDictObj) {
3758 Tcl_DecrRefCount(cd->compDictObj);
3759 }
3760 ckfree(cd);
3761 return NULL;
3762 }
3763
3764 /*
3765 *----------------------------------------------------------------------
3766 *
3767 * ResultDecompress --
3768 *
3769 * Extract uncompressed bytes from the compression engine and store them
3770 * in our buffer (buf) up to toRead bytes.
3771 *
3772 * Result:
3773 * Number of bytes decompressed or -1 if error (with *errorCodePtr updated with reason).
3774 *
3775 * Side effects:
3776 * After execution it updates cd->inStream (next_in, avail_in) to reflect
3777 * the data that has been decompressed.
3778 *
3779 *----------------------------------------------------------------------
3780 */
3781
3782 static int
3783 ResultDecompress(
3784 ZlibChannelData *cd,
3785 char *buf,
3786 int toRead,
3787 int flush,
3788 int *errorCodePtr)
3789 {
3790 int e, written, resBytes = 0;
3791 Tcl_Obj *errObj;
3792
3793
3794 cd->flags &= ~STREAM_DECOMPRESS;
3795 cd->inStream.next_out = (Bytef *) buf;
3796 cd->inStream.avail_out = toRead;
3797 while (cd->inStream.avail_out > 0) {
3798
3799 e = inflate(&cd->inStream, flush);
3800 if (e == Z_NEED_DICT && cd->compDictObj) {
3801 e = SetInflateDictionary(&cd->inStream, cd->compDictObj);
3802 if (e == Z_OK) {
3803 /*
3804 * A repetition of Z_NEED_DICT is just an error.
3805 */
3806 e = inflate(&cd->inStream, flush);
3807 }
3808 }
3809
3810 /*
3811 * avail_out is now the left over space in the output. Therefore
3812 * "toRead - avail_out" is the amount of bytes generated.
3813 */
3814
3815 written = toRead - cd->inStream.avail_out;
3816
3817 /*
3818 * The cases where we're definitely done.
3819 */
3820
3821 if (e == Z_STREAM_END) {
3822 cd->flags |= STREAM_DONE;
3823 resBytes += written;
3824 break;
3825 }
3826 if (e == Z_OK) {
3827 if (written == 0) {
3828 break;
3829 }
3830 resBytes += written;
3831 }
3832
3833 if ((flush == Z_SYNC_FLUSH) && (e == Z_BUF_ERROR)) {
3834 break;
3835 }
3836
3837 /*
3838 * Z_BUF_ERROR can be ignored as per http://www.zlib.net/zlib_how.html
3839 *
3840 * Just indicates that the zlib couldn't consume input/produce output,
3841 * and is fixed by supplying more input.
3842 *
3843 * Otherwise, we've got errors and need to report to higher-up.
3844 */
3845
3846 if ((e != Z_OK) && (e != Z_BUF_ERROR)) {
3847 goto handleError;
3848 }
3849
3850 /*
3851 * Check if the inflate stopped early.
3852 */
3853
3854 if (cd->inStream.avail_in <= 0 && flush != Z_SYNC_FLUSH) {
3855 break;
3856 }
3857 }
3858
3859 if (!(cd->flags & STREAM_DONE)) {
3860 /* if we have pending input data, but no available output buffer */
3861 if (cd->inStream.avail_in && !cd->inStream.avail_out) {
3862 /* next time try to decompress it got readable (new output buffer) */
3863 cd->flags |= STREAM_DECOMPRESS;
3864 }
3865 }
3866
3867 return resBytes;
3868
3869 handleError:
3870 errObj = Tcl_NewListObj(0, NULL);
3871 Tcl_ListObjAppendElement(NULL, errObj, Tcl_NewStringObj("-errorcode",-1));
3872 Tcl_ListObjAppendElement(NULL, errObj,
3873 ConvertErrorToList(e, cd->inStream.adler));
3874 Tcl_ListObjAppendElement(NULL, errObj,
3875 Tcl_NewStringObj(cd->inStream.msg, -1));
3876 Tcl_SetChannelError(cd->parent, errObj);
3877 *errorCodePtr = EINVAL;
3878 return -1;
3879 }
3880
3881 /*
3882 *----------------------------------------------------------------------
3883 * Finally, the TclZlibInit function. Used to install the zlib API.
3884 *----------------------------------------------------------------------
3885 */
3886
3887 int
3888 TclZlibInit(
3889 Tcl_Interp *interp)
3890 {
3891 Tcl_Config cfg[2];
3892
3893 /*
3894 * This does two things. It creates a counter used in the creation of
3895 * stream commands, and it creates the namespace that will contain those
3896 * commands.
3897 */
3898
3899 Tcl_EvalEx(interp, "namespace eval ::tcl::zlib {variable cmdcounter 0}", -1, 0);
3900
3901 /*
3902 * Create the public scripted interface to this file's functionality.
3903 */
3904
3905 Tcl_CreateObjCommand(interp, "zlib", ZlibCmd, 0, 0);
3906
3907 /*
3908 * Store the underlying configuration information.
3909 *
3910 * TODO: Describe whether we're using the system version of the library or
3911 * a compatibility version built into Tcl?
3912 */
3913
3914 cfg[0].key = "zlibVersion";
3915 cfg[0].value = zlibVersion();
3916 cfg[1].key = NULL;
3917 Tcl_RegisterConfig(interp, "zlib", cfg, "iso8859-1");
3918
3919 /*
3920 * Formally provide the package as a Tcl built-in.
3921 */
3922
3923 return Tcl_PkgProvide(interp, "zlib", TCL_ZLIB_VERSION);
3924 }
3925
3926 /*
3927 *----------------------------------------------------------------------
3928 * Stubs used when a suitable zlib installation was not found during
3929 * configure.
3930 *----------------------------------------------------------------------
3931 */
3932
3933 #else /* !HAVE_ZLIB */
3934 int
3935 Tcl_ZlibStreamInit(
3936 Tcl_Interp *interp,
3937 int mode,
3938 int format,
3939 int level,
3940 Tcl_Obj *dictObj,
3941 Tcl_ZlibStream *zshandle)
3942 {
3943 if (interp) {
3944 Tcl_SetObjResult(interp, Tcl_NewStringObj("unimplemented", -1));
3945 Tcl_SetErrorCode(interp, "TCL", "UNIMPLEMENTED", (char *)NULL);
3946 }
3947 return TCL_ERROR;
3948 }
3949
3950 int
3951 Tcl_ZlibStreamClose(
3952 Tcl_ZlibStream zshandle)
3953 {
3954 return TCL_OK;
3955 }
3956
3957 int
3958 Tcl_ZlibStreamReset(
3959 Tcl_ZlibStream zshandle)
3960 {
3961 return TCL_OK;
3962 }
3963
3964 Tcl_Obj *
3965 Tcl_ZlibStreamGetCommandName(
3966 Tcl_ZlibStream zshandle)
3967 {
3968 return NULL;
3969 }
3970
3971 int
3972 Tcl_ZlibStreamEof(
3973 Tcl_ZlibStream zshandle)
3974 {
3975 return 1;
3976 }
3977
3978 int
3979 Tcl_ZlibStreamChecksum(
3980 Tcl_ZlibStream zshandle)
3981 {
3982 return 0;
3983 }
3984
3985 int
3986 Tcl_ZlibStreamPut(
3987 Tcl_ZlibStream zshandle,
3988 Tcl_Obj *data,
3989 int flush)
3990 {
3991 return TCL_OK;
3992 }
3993
3994 int
3995 Tcl_ZlibStreamGet(
3996 Tcl_ZlibStream zshandle,
3997 Tcl_Obj *data,
3998 int count)
3999 {
4000 return TCL_OK;
4001 }
4002
4003 int
4004 Tcl_ZlibDeflate(
4005 Tcl_Interp *interp,
4006 int format,
4007 Tcl_Obj *data,
4008 int level,
4009 Tcl_Obj *gzipHeaderDictObj)
4010 {
4011 if (interp) {
4012 Tcl_SetObjResult(interp, Tcl_NewStringObj("unimplemented", -1));
4013 Tcl_SetErrorCode(interp, "TCL", "UNIMPLEMENTED", (char *)NULL);
4014 }
4015 return TCL_ERROR;
4016 }
4017
4018 int
4019 Tcl_ZlibInflate(
4020 Tcl_Interp *interp,
4021 int format,
4022 Tcl_Obj *data,
4023 int bufferSize,
4024 Tcl_Obj *gzipHeaderDictObj)
4025 {
4026 if (interp) {
4027 Tcl_SetObjResult(interp, Tcl_NewStringObj("unimplemented", -1));
4028 Tcl_SetErrorCode(interp, "TCL", "UNIMPLEMENTED", (char *)NULL);
4029 }
4030 return TCL_ERROR;
4031 }
4032
4033 unsigned int
4034 Tcl_ZlibCRC32(
4035 unsigned int crc,
4036 const char *buf,
4037 int len)
4038 {
4039 return 0;
4040 }
4041
4042 unsigned int
4043 Tcl_ZlibAdler32(
4044 unsigned int adler,
4045 const char *buf,
4046 int len)
4047 {
4048 return 0;
4049 }
4050
4051 void
4052 Tcl_ZlibStreamSetCompressionDictionary(
4053 Tcl_ZlibStream zshandle,
4054 Tcl_Obj *compressionDictionaryObj)
4055 {
4056 /* Do nothing. */
4057 }
4058 #endif /* HAVE_ZLIB */
4059
4060 /*
4061 * Local Variables:
4062 * mode: c
4063 * c-basic-offset: 4
4064 * fill-column: 78
4065 * End:
4066 */