2 * Author : Paul Marquess, <pmqs@cpan.org>
3 * Created : 22nd January 1996
6 * Copyright (c) 1995-2005 Paul Marquess. All rights reserved.
7 * This program is free software; you can redistribute it and/or
8 * modify it under the same terms as Perl itself.
12 /* Parts of this code are based on the files gzio.c and gzappend.c from
13 * the standard zlib source distribution. Below are the copyright statements
17 /* gzio.c -- IO on .gz files
18 * Copyright (C) 1995 Jean-loup Gailly.
19 * For conditions of distribution and use, see copyright notice in zlib.h
22 /* gzappend -- command to append to a gzip file
24 Copyright (C) 2003 Mark Adler, all rights reserved
25 version 1.1, 4 Nov 2003
36 /* zlib prior to 1.06 doesn't know about z_off_t */
41 #if ! defined(ZLIB_VERNUM) || ZLIB_VERNUM < 0x1200
42 # define NEED_DUMMY_BYTE_AT_END
45 #if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1210
49 #if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1221
50 # define AT_LEAST_ZLIB_1_2_2_1
53 #if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1223
54 # define AT_LEAST_ZLIB_1_2_2_3
57 #if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1230
58 # define AT_LEAST_ZLIB_1_2_3
61 #define NEED_sv_2pvbyte
62 #define NEED_sv_2pv_nolen
65 #if PERL_REVISION == 5 && (PERL_VERSION < 8 || (PERL_VERSION == 8 && PERL_SUBVERSION < 4 ))
67 # ifdef SvPVbyte_force
68 # undef SvPVbyte_force
71 # define SvPVbyte_force(sv,lp) SvPV_force(sv,lp)
75 #ifndef SvPVbyte_nolen
76 # define SvPVbyte_nolen SvPV_nolen
82 # ifndef SvPVbyte_nolen
83 # define SvPVbyte_nolen SvPV_nolen
86 # ifndef SvPVbyte_force
87 # define SvPVbyte_force(sv,lp) SvPV_force(sv,lp)
91 #if PERL_REVISION == 5 && (PERL_VERSION >= 8 || (PERL_VERSION == 8 && PERL_SUBVERSION < 4 ))
92 # define UTF8_AVAILABLE
95 typedef int DualType ;
96 typedef int int_undef ;
98 typedef struct di_stream {
100 #define FLAG_APPEND 1
102 #define FLAG_ADLER32 4
103 #define FLAG_CONSUME_INPUT 8
114 bool deflateParams_out_valid ;
115 Bytef deflateParams_out_byte;
117 #define deflateParams_BUFFER_SIZE 0x4000
118 uLong deflateParams_out_length;
119 Bytef* deflateParams_out_buffer;
126 uLong bytesInflated ;
127 uLong compressedBytes ;
128 uLong uncompressedBytes ;
131 #define WINDOW_SIZE 32768U
133 bool matchedEndBlock;
135 int window_lastbit, window_left, window_full;
136 unsigned window_have;
137 off_t window_lastoff, window_end;
138 off_t window_endOffset;
140 uLong lastBlockOffset ;
141 unsigned char window_lastByte ;
147 typedef di_stream * deflateStream ;
148 typedef di_stream * Compress__Raw__Zlib__deflateStream ;
149 typedef di_stream * inflateStream ;
150 typedef di_stream * Compress__Raw__Zlib__inflateStream ;
151 typedef di_stream * Compress__Raw__Zlib__inflateScanStream ;
153 #define GZERRNO "Compress::Zlib::gzerrno"
155 #define ZMALLOC(to, typ) ((to = (typ *)safemalloc(sizeof(typ))), \
158 /* Figure out the Operating System */
160 # define OS_CODE 0x00
163 #if defined(AMIGA) || defined(AMIGAOS)
164 # define OS_CODE 0x01
167 #if defined(VAXC) || defined(VMS)
168 # define OS_CODE 0x02
172 # define OS_CODE 0x04
175 #if defined(ATARI) || defined(atarist)
176 # define OS_CODE 0x05
180 # define OS_CODE 0x06
183 #if defined(MACOS) || defined(TARGET_OS_MAC)
184 # define OS_CODE 0x07
188 # define OS_CODE 0x08
192 # define OS_CODE 0x09
196 # define OS_CODE 0x0a
199 #ifdef WIN32 /* Window 95 & Windows NT */
200 # define OS_CODE 0x0b
204 # define OS_CODE 0x0c
207 #if 0 /* Acorn RISCOS */
208 # define OS_CODE 0x0d
212 # define OS_CODE 0x0e
215 #ifdef __50SERIES /* Prime/PRIMOS */
216 # define OS_CODE 0x0F
219 /* Default to UNIX */
221 # define OS_CODE 0x03 /* assume Unix */
225 # define GZIP_OS_CODE OS_CODE
228 #define adlerInitial adler32(0L, Z_NULL, 0)
229 #define crcInitial crc32(0L, Z_NULL, 0)
232 static const char * const my_z_errmsg[] = {
233 "need dictionary", /* Z_NEED_DICT 2 */
234 "stream end", /* Z_STREAM_END 1 */
236 "file error", /* Z_ERRNO (-1) */
237 "stream error", /* Z_STREAM_ERROR (-2) */
238 "data error", /* Z_DATA_ERROR (-3) */
239 "insufficient memory", /* Z_MEM_ERROR (-4) */
240 "buffer error", /* Z_BUF_ERROR (-5) */
241 "incompatible version",/* Z_VERSION_ERROR(-6) */
244 #define setDUALstatus(var, err) \
245 sv_setnv(var, (double)err) ; \
246 sv_setpv(var, ((err) ? GetErrorString(err) : "")) ; \
250 #if defined(__SYMBIAN32__)
251 # define NO_WRITEABLE_DATA
254 #define TRACE_DEFAULT 0
256 #ifdef NO_WRITEABLE_DATA
257 # define trace TRACE_DEFAULT
259 static int trace = TRACE_DEFAULT ;
262 /* Dodge PerlIO hiding of these functions. */
267 GetErrorString(int error_no)
269 GetErrorString(error_no)
276 if (error_no == Z_ERRNO) {
277 errstr = Strerror(errno) ;
280 /* errstr = gzerror(fil, &error_no) ; */
281 errstr = (char*) my_z_errmsg[2 - error_no];
289 SetGzErrorNo(int error_no)
291 SetGzErrorNo(error_no)
297 SV * gzerror_sv = perl_get_sv(GZERRNO, FALSE) ;
299 if (error_no == Z_ERRNO) {
301 errstr = Strerror(errno) ;
304 /* errstr = gzerror(fil, &error_no) ; */
305 errstr = (char*) my_z_errmsg[2 - error_no];
307 if (SvIV(gzerror_sv) != error_no) {
308 sv_setiv(gzerror_sv, error_no) ;
309 sv_setpv(gzerror_sv, errstr) ;
310 SvIOK_on(gzerror_sv) ;
318 SetGzError(gzFile file)
326 (void)gzerror(file, &error_no) ;
327 SetGzErrorNo(error_no) ;
335 The following two functions are taken almost directly from
336 examples/gzappend.c. Only cosmetic changes have been made to conform to
337 the coding style of the rest of the code in this file.
341 /* return the greatest common divisor of a and b using Euclid's algorithm,
342 modified to be fast when one argument much greater than the other, and
343 coded to avoid unnecessary swapping */
346 gcd(unsigned a, unsigned b)
371 /* rotate list[0..len-1] left by rot positions, in place */
374 rotate(unsigned char *list, unsigned len, unsigned rot)
376 rotate(list, len, rot)
384 unsigned char *start, *last, *to, *from;
386 /* normalize rot and handle degenerate cases */
388 if (rot >= len) rot %= len;
389 if (rot == 0) return;
391 /* pointer to last entry in list */
392 last = list + (len - 1);
394 /* do simple left shift by one */
397 memcpy(list, list + 1, len - 1);
402 /* do simple right shift by one */
403 if (rot == len - 1) {
405 memmove(list + 1, list, len - 1);
410 /* otherwise do rotate as a set of cycles in place */
411 cycles = gcd(len, rot); /* number of cycles */
413 start = from = list + cycles; /* start index is arbitrary */
414 tmp = *from; /* save entry to be overwritten */
416 to = from; /* next step in cycle */
417 from += rot; /* go right rot positions */
418 if (from > last) from -= len; /* (pointer better not wrap) */
419 if (from == start) break; /* all but one shifted */
420 *to = *from; /* shift left */
422 *to = tmp; /* complete the circle */
426 #endif /* MAGIC_APPEND */
430 DispHex(void * ptr, int length)
437 char * p = (char*)ptr;
439 for (i = 0; i < length; ++i) {
440 printf(" %02x", 0xFF & *(p+i));
447 DispStream(di_stream * s, char * message)
449 DispStream(s, message)
460 #define EnDis(f) (s->flags & f ? "Enabled" : "Disabled")
462 printf("DispStream 0x%p", s) ;
464 printf("- %s \n", message) ;
468 printf(" stream pointer is NULL\n");
471 printf(" stream 0x%p\n", &(s->stream));
472 printf(" zalloc 0x%p\n", s->stream.zalloc);
473 printf(" zfree 0x%p\n", s->stream.zfree);
474 printf(" opaque 0x%p\n", s->stream.opaque);
476 printf(" msg %s\n", s->stream.msg);
479 printf(" next_in 0x%p", s->stream.next_in);
480 if (s->stream.next_in){
482 DispHex(s->stream.next_in, 4);
486 printf(" next_out 0x%p", s->stream.next_out);
487 if (s->stream.next_out){
489 DispHex(s->stream.next_out, 4);
493 printf(" avail_in %lu\n", (unsigned long)s->stream.avail_in);
494 printf(" avail_out %lu\n", (unsigned long)s->stream.avail_out);
495 printf(" total_in %ld\n", s->stream.total_in);
496 printf(" total_out %ld\n", s->stream.total_out);
497 printf(" adler %ld\n", s->stream.adler );
498 printf(" bufsize %ld\n", s->bufsize);
499 printf(" dictionary 0x%p\n", s->dictionary);
500 printf(" dict_adler 0x%ld\n",s->dict_adler);
501 printf(" zip_mode %d\n", s->zip_mode);
502 printf(" crc32 0x%x\n", (unsigned)s->crc32);
503 printf(" adler32 0x%x\n", (unsigned)s->adler32);
504 printf(" flags 0x%x\n", s->flags);
505 printf(" APPEND %s\n", EnDis(FLAG_APPEND));
506 printf(" CRC32 %s\n", EnDis(FLAG_CRC32));
507 printf(" ADLER32 %s\n", EnDis(FLAG_ADLER32));
508 printf(" CONSUME %s\n", EnDis(FLAG_CONSUME_INPUT));
511 printf(" window 0x%p\n", s->window);
527 ZMALLOC(s, di_stream) ;
535 PostInitStream(di_stream * s, int flags, int bufsize, int windowBits)
537 PostInitStream(s, flags, bufsize, windowBits)
544 s->bufsize = bufsize ;
546 s->uncompressedBytes =
549 s->zip_mode = (windowBits < 0) ;
550 if (flags & FLAG_CRC32)
551 s->crc32 = crcInitial ;
552 if (flags & FLAG_ADLER32)
553 s->adler32 = adlerInitial ;
559 deRef(SV * sv, char * string)
576 croak("%s: buffer parameter is not a SCALAR reference", string);
579 croak("%s: buffer parameter is a reference to a reference", string) ;
591 deRef_l(SV * sv, char * string)
613 croak("%s: buffer parameter is not a SCALAR reference", string);
616 croak("%s: buffer parameter is a reference to a reference", string) ;
619 if (SvREADONLY(sv) && PL_curcop != &PL_compiling)
620 croak("%s: buffer parameter is read-only", string);
622 SvUPGRADE(sv, SVt_PV);
634 #include "constants.h"
636 MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib PREFIX = Zip_
641 INCLUDE: constants.xs
644 /* Check this version of zlib is == 1 */
645 if (zlibVersion()[0] != '1')
646 croak("Compress::Raw::Zlib needs zlib version 1.x\n") ;
649 /* Create the $os_code scalar */
650 SV * os_code_sv = perl_get_sv("Compress::Raw::Zlib::gzip_os_code", GV_ADDMULTI) ;
651 sv_setiv(os_code_sv, GZIP_OS_CODE) ;
655 #define Zip_zlib_version() (char*)zlib_version
663 RETVAL = ZLIB_VERNUM ;
665 /* 1.1.4 => 0x1140 */
666 RETVAL = (ZLIB_VERSION[0] - '0') << 12 ;
667 RETVAL += (ZLIB_VERSION[2] - '0') << 8 ;
668 RETVAL += (ZLIB_VERSION[4] - '0') << 4 ;
673 MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib PREFIX = Zip_
675 #define Zip_adler32(buf, adler) adler32(adler, buf, (uInt)len)
678 Zip_adler32(buf, adler=adlerInitial)
679 uLong adler = NO_INIT
681 Bytef * buf = NO_INIT
684 /* If the buffer is a reference, dereference it */
685 sv = deRef(sv, "adler32") ;
686 #ifdef UTF8_AVAILABLE
687 if (DO_UTF8(sv) && !sv_utf8_downgrade(sv, 1))
688 croak("Wide character in Compress::Raw::Zlib::adler32");
690 buf = (Byte*)SvPVbyte(sv, len) ;
693 adler = adlerInitial;
694 else if (SvOK(ST(1)))
695 adler = SvUV(ST(1)) ;
697 adler = adlerInitial;
699 #define Zip_crc32(buf, crc) crc32(crc, buf, (uInt)len)
702 Zip_crc32(buf, crc=crcInitial)
705 Bytef * buf = NO_INIT
708 /* If the buffer is a reference, dereference it */
709 sv = deRef(sv, "crc32") ;
710 #ifdef UTF8_AVAILABLE
711 if (DO_UTF8(sv) && !sv_utf8_downgrade(sv, 1))
712 croak("Wide character in Compress::Raw::Zlib::crc32");
714 buf = (Byte*)SvPVbyte(sv, len) ;
718 else if (SvOK(ST(1)))
725 crc32_combine(crc1, crc2, len2)
730 #ifndef AT_LEAST_ZLIB_1_2_2_1
731 crc1 = crc1; crc2 = crc2 ; len2 = len2; /* Silence -Wall */
732 croak("crc32_combine needs zlib 1.2.3 or better");
734 RETVAL = crc32_combine(crc1, crc2, len2);
741 adler32_combine(adler1, adler2, len2)
746 #ifndef AT_LEAST_ZLIB_1_2_2_1
747 adler1 = adler1; adler2 = adler2 ; len2 = len2; /* Silence -Wall */
748 croak("adler32_combine needs zlib 1.2.3 or better");
750 RETVAL = adler32_combine(adler1, adler2, len2);
756 MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib
759 _deflateInit(flags,level, method, windowBits, memLevel, strategy, bufsize, dictionary)
773 warn("in _deflateInit(level=%d, method=%d, windowBits=%d, memLevel=%d, strategy=%d, bufsize=%ld\n",
774 level, method, windowBits, memLevel, strategy, bufsize) ;
775 if ((s = InitStream() )) {
779 s->WindowBits = windowBits;
780 s->MemLevel = memLevel;
781 s->Strategy = strategy;
783 err = deflateInit2(&(s->stream), level,
784 method, windowBits, memLevel, strategy);
786 /* Check if a dictionary has been specified */
788 if (err == Z_OK && SvCUR(dictionary)) {
789 #ifdef UTF8_AVAILABLE
790 if (DO_UTF8(dictionary) && !sv_utf8_downgrade(dictionary, 1))
791 croak("Wide character in Compress::Raw::Zlib::Deflate::new dicrionary parameter");
793 err = deflateSetDictionary(&(s->stream), (const Bytef*) SvPVbyte_nolen(dictionary),
795 s->dict_adler = s->stream.adler ;
803 PostInitStream(s, flags, bufsize, windowBits) ;
809 XPUSHs(sv_setref_pv(sv_newmortal(),
810 "Compress::Raw::Zlib::deflateStream", (void*)s));
811 if (GIMME == G_ARRAY) {
812 SV * sv = sv_2mortal(newSViv(err)) ;
813 setDUALstatus(sv, err);
818 _inflateInit(flags, windowBits, bufsize, dictionary)
831 croak("inflateScanInit needs zlib 1.2.1 or better");
834 warn("in _inflateInit(windowBits=%d, bufsize=%lu, dictionary=%lu\n",
835 windowBits, bufsize, (unsigned long)SvCUR(dictionary)) ;
836 if ((s = InitStream() )) {
838 s->WindowBits = windowBits;
840 err = inflateInit2(&(s->stream), windowBits);
845 else if (SvCUR(dictionary)) {
846 /* Dictionary specified - take a copy for use in inflate */
847 s->dictionary = newSVsv(dictionary) ;
850 PostInitStream(s, flags, bufsize, windowBits) ;
854 s->window = (unsigned char *)safemalloc(WINDOW_SIZE);
862 XPUSHs(sv_setref_pv(sv_newmortal(),
864 ? "Compress::Raw::Zlib::inflateScanStream"
865 : "Compress::Raw::Zlib::inflateStream",
867 if (GIMME == G_ARRAY) {
868 SV * sv = sv_2mortal(newSViv(err)) ;
869 setDUALstatus(sv, err);
875 MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib::deflateStream
878 DispStream(s, message=NULL)
879 Compress::Raw::Zlib::deflateStream s
884 Compress::Raw::Zlib::deflateStream s
886 RETVAL = deflateReset(&(s->stream)) ;
887 if (RETVAL == Z_OK) {
888 PostInitStream(s, s->flags, s->bufsize, s->WindowBits) ;
894 deflate (s, buf, output)
895 Compress::Raw::Zlib::deflateStream s
898 uInt cur_length = NO_INIT
899 uInt increment = NO_INIT
900 uInt prefix = NO_INIT
902 uLong bufinc = NO_INIT
906 /* If the input buffer is a reference, dereference it */
907 buf = deRef(buf, "deflate") ;
909 /* initialise the input buffer */
910 #ifdef UTF8_AVAILABLE
911 if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
912 croak("Wide character in Compress::Raw::Zlib::Deflate::deflate input parameter");
914 s->stream.next_in = (Bytef*)SvPVbyte_nolen(buf) ;
915 s->stream.avail_in = SvCUR(buf) ;
917 if (s->flags & FLAG_CRC32)
918 s->crc32 = crc32(s->crc32, s->stream.next_in, s->stream.avail_in) ;
920 if (s->flags & FLAG_ADLER32)
921 s->adler32 = adler32(s->adler32, s->stream.next_in, s->stream.avail_in) ;
923 /* and retrieve the output buffer */
924 output = deRef_l(output, "deflate") ;
925 #ifdef UTF8_AVAILABLE
926 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
927 croak("Wide character in Compress::Raw::Zlib::Deflate::deflate output parameter");
930 if((s->flags & FLAG_APPEND) != FLAG_APPEND) {
931 SvCUR_set(output, 0);
932 /* sv_setpvn(output, "", 0); */
934 prefix = cur_length = SvCUR(output) ;
935 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length;
936 increment = SvLEN(output) - cur_length;
937 s->stream.avail_out = increment;
939 /* Check for saved output from deflateParams */
940 if (s->deflateParams_out_valid) {
941 *(s->stream.next_out) = s->deflateParams_out_byte;
942 ++ s->stream.next_out;
943 -- s->stream.avail_out ;
944 s->deflateParams_out_valid = FALSE;
947 /* Check for saved output from deflateParams */
948 if (s->deflateParams_out_length) {
949 uLong plen = s->deflateParams_out_length ;
950 /* printf("Copy %d bytes saved data\n", plen);*/
951 if (s->stream.avail_out < plen) {
952 /*printf("GROW from %d to %d\n", s->stream.avail_out,
953 SvLEN(output) + plen - s->stream.avail_out); */
954 Sv_Grow(output, SvLEN(output) + plen - s->stream.avail_out) ;
957 Copy(s->stream.next_out, s->deflateParams_out_buffer, plen, Bytef) ;
958 cur_length = cur_length + plen;
959 SvCUR_set(output, cur_length);
960 s->stream.next_out += plen ;
961 s->stream.avail_out = SvLEN(output) - cur_length ;
962 increment = s->stream.avail_out;
963 s->deflateParams_out_length = 0;
966 while (s->stream.avail_in != 0) {
968 if (s->stream.avail_out == 0) {
969 /* out of space in the output buffer so make it bigger */
970 Sv_Grow(output, SvLEN(output) + bufinc) ;
971 cur_length += increment ;
972 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length ;
974 s->stream.avail_out = increment;
978 RETVAL = deflate(&(s->stream), Z_NO_FLUSH);
983 s->compressedBytes += cur_length + increment - prefix - s->stream.avail_out ;
984 s->uncompressedBytes += SvCUR(buf) - s->stream.avail_in ;
986 s->last_error = RETVAL ;
987 if (RETVAL == Z_OK) {
989 SvCUR_set(output, cur_length + increment - s->stream.avail_out) ;
998 Compress::Raw::Zlib::deflateStream s
1000 deflateEnd(&s->stream) ;
1002 SvREFCNT_dec(s->dictionary) ;
1004 if (s->deflateParams_out_buffer)
1005 Safefree(s->deflateParams_out_buffer);
1011 flush(s, output, f=Z_FINISH)
1012 Compress::Raw::Zlib::deflateStream s
1015 uInt cur_length = NO_INIT
1016 uInt increment = NO_INIT
1017 uInt prefix = NO_INIT
1018 uLong bufinc = NO_INIT
1020 bufinc = s->bufsize;
1022 s->stream.avail_in = 0; /* should be zero already anyway */
1024 /* retrieve the output buffer */
1025 output = deRef_l(output, "flush") ;
1026 #ifdef UTF8_AVAILABLE
1027 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
1028 croak("Wide character in Compress::Raw::Zlib::Deflate::flush input parameter");
1030 if(! s->flags & FLAG_APPEND) {
1031 SvCUR_set(output, 0);
1032 /* sv_setpvn(output, "", 0); */
1034 prefix = cur_length = SvCUR(output) ;
1035 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length;
1036 increment = SvLEN(output) - cur_length;
1037 s->stream.avail_out = increment;
1039 /* Check for saved output from deflateParams */
1040 if (s->deflateParams_out_valid) {
1041 *(s->stream.next_out) = s->deflateParams_out_byte;
1042 ++ s->stream.next_out;
1043 -- s->stream.avail_out ;
1044 s->deflateParams_out_valid = FALSE;
1047 /* Check for saved output from deflateParams */
1048 if (s->deflateParams_out_length) {
1049 uLong plen = s->deflateParams_out_length ;
1050 /* printf("Copy %d bytes saved data\n", plen); */
1051 if (s->stream.avail_out < plen) {
1052 /* printf("GROW from %d to %d\n", s->stream.avail_out,
1053 SvLEN(output) + plen - s->stream.avail_out); */
1054 Sv_Grow(output, SvLEN(output) + plen - s->stream.avail_out) ;
1057 Copy(s->stream.next_out, s->deflateParams_out_buffer, plen, Bytef) ;
1058 cur_length = cur_length + plen;
1059 SvCUR_set(output, cur_length);
1060 s->stream.next_out += plen ;
1061 s->stream.avail_out = SvLEN(output) - cur_length ;
1062 increment = s->stream.avail_out;
1063 s->deflateParams_out_length = 0;
1068 if (s->stream.avail_out == 0) {
1069 /* consumed all the available output, so extend it */
1070 Sv_Grow(output, SvLEN(output) + bufinc) ;
1071 cur_length += increment ;
1072 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length ;
1073 increment = bufinc ;
1074 s->stream.avail_out = increment;
1077 RETVAL = deflate(&(s->stream), f);
1079 /* deflate has finished flushing only when it hasn't used up
1080 * all the available space in the output buffer:
1082 if (s->stream.avail_out != 0 || RETVAL != Z_OK )
1086 RETVAL = (RETVAL == Z_STREAM_END ? Z_OK : RETVAL) ;
1087 s->last_error = RETVAL ;
1089 s->compressedBytes += cur_length + increment - prefix - s->stream.avail_out ;
1091 if (RETVAL == Z_OK) {
1093 SvCUR_set(output, cur_length + increment - s->stream.avail_out) ;
1101 _deflateParams(s, flags, level, strategy, bufsize)
1102 Compress::Raw::Zlib::deflateStream s
1108 /* printf("_deflateParams(Flags %d Level %d Strategy %d Bufsize %d)\n", flags, level, strategy, bufsize);
1109 printf("Before -- Level %d, Strategy %d, Bufsize %d\n", s->Level, s->Strategy, s->bufsize); */
1113 s->Strategy = strategy ;
1115 s->bufsize = bufsize;
1117 /* printf("After -- Level %d, Strategy %d, Bufsize %d\n", s->Level, s->Strategy, s->bufsize);*/
1119 s->stream.avail_in = 0;
1120 s->stream.next_out = &(s->deflateParams_out_byte) ;
1121 s->stream.avail_out = 1;
1122 RETVAL = deflateParams(&(s->stream), s->Level, s->Strategy);
1123 s->deflateParams_out_valid =
1124 (RETVAL == Z_OK && s->stream.avail_out == 0) ;
1125 /* printf("RETVAL %d, avail out %d, byte %c\n", RETVAL, s->stream.avail_out, s->deflateParams_out_byte); */
1127 /* printf("Level %d Strategy %d, Prev Len %d\n",
1128 s->Level, s->Strategy, s->deflateParams_out_length); */
1129 s->stream.avail_in = 0;
1130 if (s->deflateParams_out_buffer == NULL)
1131 s->deflateParams_out_buffer = safemalloc(deflateParams_BUFFER_SIZE);
1132 s->stream.next_out = s->deflateParams_out_buffer ;
1133 s->stream.avail_out = deflateParams_BUFFER_SIZE;
1135 RETVAL = deflateParams(&(s->stream), s->Level, s->Strategy);
1136 s->deflateParams_out_length = deflateParams_BUFFER_SIZE - s->stream.avail_out;
1137 /* printf("RETVAL %d, length out %d, avail %d\n",
1138 RETVAL, s->deflateParams_out_length, s->stream.avail_out ); */
1146 Compress::Raw::Zlib::deflateStream s
1154 Compress::Raw::Zlib::deflateStream s
1156 RETVAL = s->Strategy ;
1163 Compress::Raw::Zlib::deflateStream s
1165 RETVAL = s->bufsize ;
1172 Compress::Raw::Zlib::deflateStream s
1174 RETVAL = s->last_error ;
1180 Compress::Raw::Zlib::deflateStream s
1188 Compress::Raw::Zlib::deflateStream s
1190 RETVAL = s->dict_adler ;
1196 Compress::Raw::Zlib::deflateStream s
1198 RETVAL = s->adler32 ;
1204 Compress::Raw::Zlib::deflateStream s
1206 RETVAL = s->compressedBytes;
1211 uncompressedBytes(s)
1212 Compress::Raw::Zlib::deflateStream s
1214 RETVAL = s->uncompressedBytes;
1220 Compress::Raw::Zlib::deflateStream s
1222 RETVAL = s->stream.total_in ;
1228 Compress::Raw::Zlib::deflateStream s
1230 RETVAL = s->stream.total_out ;
1236 Compress::Raw::Zlib::deflateStream s
1238 RETVAL = s->stream.msg;
1243 deflateTune(s, good_length, max_lazy, nice_length, max_chain)
1244 Compress::Raw::Zlib::deflateStream s
1250 #ifndef AT_LEAST_ZLIB_1_2_2_3
1251 good_length = good_length; max_lazy = max_lazy ; /* Silence -Wall */
1252 nice_length = nice_length; max_chain = max_chain; /* Silence -Wall */
1253 croak("deflateTune needs zlib 1.2.2.3 or better");
1255 RETVAL = deflateTune(&(s->stream), good_length, max_lazy, nice_length, max_chain);
1261 MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib::inflateStream
1264 DispStream(s, message=NULL)
1265 Compress::Raw::Zlib::inflateStream s
1270 Compress::Raw::Zlib::inflateStream s
1272 RETVAL = inflateReset(&(s->stream)) ;
1273 if (RETVAL == Z_OK) {
1274 PostInitStream(s, s->flags, s->bufsize, s->WindowBits) ;
1280 inflate (s, buf, output, eof=FALSE)
1281 Compress::Raw::Zlib::inflateStream s
1285 uInt cur_length = 0;
1286 uInt prefix_length = 0;
1288 STRLEN stmp = NO_INIT
1289 uLong bufinc = NO_INIT
1291 #ifdef UTF8_AVAILABLE
1292 bool out_utf8 = FALSE;
1295 bufinc = s->bufsize;
1296 /* If the buffer is a reference, dereference it */
1297 buf = deRef(buf, "inflate") ;
1299 if (s->flags & FLAG_CONSUME_INPUT && SvREADONLY(buf))
1300 croak("Compress::Raw::Zlib::Inflate::inflate input parameter cannot be read-only when ConsumeInput is specified");
1301 #ifdef UTF8_AVAILABLE
1302 if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
1303 croak("Wide character in Compress::Raw::Zlib::Inflate::inflate input parameter");
1306 /* initialise the input buffer */
1307 s->stream.next_in = (Bytef*)SvPVbyte_force(buf, stmp) ;
1308 s->stream.avail_in = SvCUR(buf) ;
1310 /* and retrieve the output buffer */
1311 output = deRef_l(output, "inflate") ;
1312 #ifdef UTF8_AVAILABLE
1313 if (DO_UTF8(output))
1315 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
1316 croak("Wide character in Compress::Raw::Zlib::Inflate::inflate output parameter");
1318 if((s->flags & FLAG_APPEND) != FLAG_APPEND) {
1319 SvCUR_set(output, 0);
1321 if (SvLEN(output)) {
1322 prefix_length = cur_length = SvCUR(output) ;
1323 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length;
1324 increment = SvLEN(output) - cur_length - 1;
1325 s->stream.avail_out = increment;
1328 s->stream.avail_out = 0;
1330 s->bytesInflated = 0;
1334 if (s->stream.avail_out == 0 ) {
1335 /* out of space in the output buffer so make it bigger */
1336 Sv_Grow(output, SvLEN(output) + bufinc) ;
1337 cur_length += increment ;
1338 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length ;
1339 increment = bufinc ;
1340 s->stream.avail_out = increment;
1344 RETVAL = inflate(&(s->stream), Z_SYNC_FLUSH);
1346 if (RETVAL == Z_STREAM_ERROR || RETVAL == Z_MEM_ERROR ||
1347 RETVAL == Z_DATA_ERROR || RETVAL == Z_STREAM_END )
1350 if (RETVAL == Z_BUF_ERROR) {
1351 if (s->stream.avail_out == 0)
1353 if (s->stream.avail_in == 0) {
1359 if (RETVAL == Z_NEED_DICT && s->dictionary) {
1360 s->dict_adler = s->stream.adler ;
1361 RETVAL = inflateSetDictionary(&(s->stream),
1362 (const Bytef*)SvPVbyte_nolen(s->dictionary),
1363 SvCUR(s->dictionary));
1369 #ifdef NEED_DUMMY_BYTE_AT_END
1370 if (eof && RETVAL == Z_OK) {
1371 Bytef* nextIn = s->stream.next_in;
1372 uInt availIn = s->stream.avail_in;
1373 s->stream.next_in = (Bytef*) " ";
1374 s->stream.avail_in = 1;
1375 if (s->stream.avail_out == 0) {
1376 /* out of space in the output buffer so make it bigger */
1377 Sv_Grow(output, SvLEN(output) + bufinc) ;
1378 cur_length += increment ;
1379 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length ;
1380 increment = bufinc ;
1381 s->stream.avail_out = increment;
1384 RETVAL = inflate(&(s->stream), Z_SYNC_FLUSH);
1385 s->stream.next_in = nextIn ;
1386 s->stream.avail_in = availIn ;
1390 s->last_error = RETVAL ;
1391 if (RETVAL == Z_OK || RETVAL == Z_STREAM_END || RETVAL == Z_DATA_ERROR) {
1394 s->bytesInflated = cur_length + increment - s->stream.avail_out - prefix_length;
1395 s->uncompressedBytes += s->bytesInflated ;
1396 s->compressedBytes += SvCUR(buf) - s->stream.avail_in ;
1399 SvCUR_set(output, prefix_length + s->bytesInflated) ;
1400 *SvEND(output) = '\0';
1401 #ifdef UTF8_AVAILABLE
1403 sv_utf8_upgrade(output);
1406 if (s->flags & FLAG_CRC32 )
1407 s->crc32 = crc32(s->crc32,
1408 (const Bytef*)SvPVbyte_nolen(output)+prefix_length,
1409 SvCUR(output)-prefix_length) ;
1411 if (s->flags & FLAG_ADLER32)
1412 s->adler32 = adler32(s->adler32,
1413 (const Bytef*)SvPVbyte_nolen(output)+prefix_length,
1414 SvCUR(output)-prefix_length) ;
1416 /* fix the input buffer */
1417 if (s->flags & FLAG_CONSUME_INPUT) {
1418 in = s->stream.avail_in ;
1419 SvCUR_set(buf, in) ;
1421 Move(s->stream.next_in, SvPVbyte_nolen(buf), in, char) ;
1433 Compress::Raw::Zlib::inflateStream s
1435 RETVAL = s->bytesInflated;
1441 Compress::Raw::Zlib::inflateStream s
1443 RETVAL = s->compressedBytes;
1448 uncompressedBytes(s)
1449 Compress::Raw::Zlib::inflateStream s
1451 RETVAL = s->uncompressedBytes;
1457 inflateSync (s, buf)
1458 Compress::Raw::Zlib::inflateStream s
1462 /* If the buffer is a reference, dereference it */
1463 buf = deRef(buf, "inflateSync") ;
1464 #ifdef UTF8_AVAILABLE
1465 if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
1466 croak("Wide character in Compress::Raw::Zlib::Inflate::inflateSync");
1469 /* initialise the input buffer */
1470 s->stream.next_in = (Bytef*)SvPVbyte_nolen(buf) ;
1471 s->stream.avail_in = SvCUR(buf) ;
1473 /* inflateSync doesn't create any output */
1474 s->stream.next_out = (Bytef*) NULL;
1475 s->stream.avail_out = 0;
1477 RETVAL = inflateSync(&(s->stream));
1478 s->last_error = RETVAL ;
1480 /* fix the input buffer */
1482 unsigned in = s->stream.avail_in ;
1483 SvCUR_set(buf, in) ;
1485 Move(s->stream.next_in, SvPVbyte_nolen(buf), in, char) ;
1495 Compress::Raw::Zlib::inflateStream s
1497 inflateEnd(&s->stream) ;
1499 SvREFCNT_dec(s->dictionary) ;
1501 if (s->deflateParams_out_buffer)
1502 Safefree(s->deflateParams_out_buffer);
1506 Safefree(s->window);
1513 Compress::Raw::Zlib::inflateStream s
1515 RETVAL = s->last_error ;
1521 Compress::Raw::Zlib::inflateStream s
1529 Compress::Raw::Zlib::inflateStream s
1531 RETVAL = s->dict_adler ;
1537 Compress::Raw::Zlib::inflateStream s
1539 RETVAL = s->stream.total_in ;
1545 Compress::Raw::Zlib::inflateStream s
1547 RETVAL = s->adler32 ;
1553 Compress::Raw::Zlib::inflateStream s
1555 RETVAL = s->stream.total_out ;
1561 Compress::Raw::Zlib::inflateStream s
1563 RETVAL = s->stream.msg;
1570 Compress::Raw::Zlib::inflateStream s
1572 RETVAL = s->bufsize ;
1578 Compress::Raw::Zlib::inflateStream s
1581 RETVAL = ((s->flags & FLAG_APPEND) == FLAG_APPEND);
1583 s->flags |= FLAG_APPEND ;
1585 s->flags &= ~FLAG_APPEND ;
1589 MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib::inflateScanStream
1593 Compress::Raw::Zlib::inflateScanStream s
1595 inflateEnd(&s->stream) ;
1597 SvREFCNT_dec(s->dictionary) ;
1599 if (s->deflateParams_out_buffer)
1600 Safefree(s->deflateParams_out_buffer);
1604 Safefree(s->window);
1609 DispStream(s, message=NULL)
1610 Compress::Raw::Zlib::inflateScanStream s
1615 Compress::Raw::Zlib::inflateScanStream s
1617 RETVAL = inflateReset(&(s->stream)) ;
1618 if (RETVAL == Z_OK) {
1619 PostInitStream(s, s->flags, s->bufsize, s->WindowBits) ;
1625 scan(s, buf, out=NULL, eof=FALSE)
1626 Compress::Raw::Zlib::inflateScanStream s
1630 bool eof_mode = FALSE;
1631 int start_len = NO_INIT
1632 STRLEN stmp = NO_INIT
1634 /* If the input buffer is a reference, dereference it */
1635 #ifndef MAGIC_APPEND
1637 croak("scan needs zlib 1.2.1 or better");
1639 buf = deRef(buf, "inflateScan") ;
1640 #ifdef UTF8_AVAILABLE
1641 if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
1642 croak("Wide character in Compress::Raw::Zlib::InflateScan::scan input parameter");
1644 /* initialise the input buffer */
1645 s->stream.next_in = (Bytef*)SvPVbyte_force(buf, stmp) ;
1646 s->stream.avail_in = SvCUR(buf) ;
1647 start_len = s->stream.avail_in ;
1648 s->bytesInflated = 0 ;
1651 if (s->stream.avail_in == 0) {
1656 /* set up output to next available section of sliding window */
1657 s->stream.avail_out = WINDOW_SIZE - s->window_have;
1658 s->stream.next_out = s->window + s->window_have;
1660 /* DispStream(s, "before inflate\n"); */
1662 /* inflate and check for errors */
1663 RETVAL = inflate(&(s->stream), Z_BLOCK);
1665 if (start_len > 1 && ! eof_mode)
1666 s->window_lastByte = *(s->stream.next_in - 1 ) ;
1668 if (RETVAL == Z_STREAM_ERROR || RETVAL == Z_MEM_ERROR ||
1669 RETVAL == Z_DATA_ERROR )
1672 if (s->flags & FLAG_CRC32 )
1673 s->crc32 = crc32(s->crc32, s->window + s->window_have,
1674 WINDOW_SIZE - s->window_have - s->stream.avail_out);
1676 if (s->flags & FLAG_ADLER32)
1677 s->adler32 = adler32(s->adler32, s->window + s->window_have,
1678 WINDOW_SIZE - s->window_have - s->stream.avail_out);
1680 s->uncompressedBytes =
1681 s->bytesInflated += WINDOW_SIZE - s->window_have - s->stream.avail_out;
1683 if (s->stream.avail_out)
1684 s->window_have = WINDOW_SIZE - s->stream.avail_out;
1690 /* process end of block */
1691 if (s->stream.data_type & 128) {
1692 if (s->stream.data_type & 64) {
1693 s->window_left = s->stream.data_type & 0x1f;
1696 s->window_lastbit = s->stream.data_type & 0x1f;
1697 s->lastBlockOffset = s->stream.total_in;
1701 } while (RETVAL != Z_STREAM_END);
1703 s->last_error = RETVAL ;
1704 s->window_lastoff = s->stream.total_in ;
1705 s->compressedBytes += SvCUR(buf) - s->stream.avail_in ;
1707 if (RETVAL == Z_STREAM_END)
1709 s->matchedEndBlock = 1 ;
1711 /* save the location of the end of the compressed data */
1712 s->window_end = SvCUR(buf) - s->stream.avail_in - 1 ;
1713 s->window_endOffset = s->stream.total_in ;
1716 -- s->window_endOffset ;
1719 /* if window wrapped, build dictionary from window by rotating */
1720 if (s->window_full) {
1721 rotate(s->window, WINDOW_SIZE, s->window_have);
1722 s->window_have = WINDOW_SIZE;
1725 /* if (s->flags & FLAG_CONSUME_INPUT) { */
1727 unsigned in = s->stream.avail_in ;
1728 SvCUR_set(buf, in) ;
1730 Move(s->stream.next_in, SvPVbyte_nolen(buf), in, char) ;
1742 Compress::Raw::Zlib::inflateScanStream s
1744 #ifndef MAGIC_APPEND
1745 croak("getEndOffset needs zlib 1.2.1 or better");
1747 RETVAL = s->window_endOffset;
1754 Compress::Raw::Zlib::inflateScanStream s
1756 #ifndef MAGIC_APPEND
1757 croak("inflateCount needs zlib 1.2.1 or better");
1759 RETVAL = s->bytesInflated;
1766 Compress::Raw::Zlib::inflateScanStream s
1768 RETVAL = s->compressedBytes;
1773 uncompressedBytes(s)
1774 Compress::Raw::Zlib::inflateScanStream s
1776 RETVAL = s->uncompressedBytes;
1782 getLastBlockOffset(s)
1783 Compress::Raw::Zlib::inflateScanStream s
1785 #ifndef MAGIC_APPEND
1786 croak("getLastBlockOffset needs zlib 1.2.1 or better");
1788 RETVAL = s->lastBlockOffset - (s->window_lastbit != 0);
1794 getLastBufferOffset(s)
1795 Compress::Raw::Zlib::inflateScanStream s
1797 #ifndef MAGIC_APPEND
1798 croak("getLastBufferOffset needs zlib 1.2.1 or better");
1800 RETVAL = s->window_lastoff;
1806 resetLastBlockByte(s, byte)
1807 Compress::Raw::Zlib::inflateScanStream s
1810 #ifndef MAGIC_APPEND
1811 croak("resetLastBlockByte needs zlib 1.2.1 or better");
1814 *byte = *byte ^ (1 << ((8 - s->window_lastbit) & 7));
1819 _createDeflateStream(inf_s, flags,level, method, windowBits, memLevel, strategy, bufsize)
1820 Compress::Raw::Zlib::inflateScanStream inf_s
1830 #ifndef MAGIC_APPEND
1834 windowBits = windowBits;
1835 memLevel = memLevel;
1836 strategy = strategy;
1838 croak("_createDeflateStream needs zlib 1.2.1 or better");
1844 warn("in _createDeflateStream(level=%d, method=%d, windowBits=%d, memLevel=%d, strategy=%d, bufsize=%lu\n",
1845 level, method, windowBits, memLevel, strategy, bufsize) ;
1846 if ((s = InitStream() )) {
1850 s->WindowBits = windowBits;
1851 s->MemLevel = memLevel;
1852 s->Strategy = strategy;
1854 err = deflateInit2(&(s->stream), level,
1855 method, windowBits, memLevel, strategy);
1858 err = deflateSetDictionary(&(s->stream), inf_s->window, inf_s->window_have);
1859 s->dict_adler = s->stream.adler ;
1867 PostInitStream(s, flags, bufsize, windowBits) ;
1868 s->crc32 = inf_s->crc32;
1869 s->adler32 = inf_s->adler32;
1870 s->stream.adler = inf_s->stream.adler ;
1871 /* s->stream.total_out = inf_s->bytesInflated ; */
1872 s->stream.total_in = inf_s->stream.total_out ;
1873 if (inf_s->window_left) {
1874 /* printf("** window_left %d, window_lastByte %d\n", inf_s->window_left, inf_s->window_lastByte); */
1875 deflatePrime(&(s->stream), 8 - inf_s->window_left, inf_s->window_lastByte);
1882 XPUSHs(sv_setref_pv(sv_newmortal(),
1883 "Compress::Raw::Zlib::deflateStream", (void*)s));
1884 if (GIMME == G_ARRAY) {
1885 SV * sv = sv_2mortal(newSViv(err)) ;
1886 setDUALstatus(sv, err);
1894 Compress::Raw::Zlib::inflateScanStream s
1896 RETVAL = s->last_error ;
1902 Compress::Raw::Zlib::inflateScanStream s
1911 Compress::Raw::Zlib::inflateScanStream s
1913 RETVAL = s->adler32 ;