2 * Author : Paul Marquess, <pmqs@cpan.org>
3 * Created : 22nd January 1996
6 * Copyright (c) 1995-2007 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 ZMALLOC(to, typ) ((to = (typ *)safemalloc(sizeof(typ))), \
156 /* Figure out the Operating System */
158 # define OS_CODE 0x00
161 #if defined(AMIGA) || defined(AMIGAOS)
162 # define OS_CODE 0x01
165 #if defined(VAXC) || defined(VMS)
166 # define OS_CODE 0x02
170 # define OS_CODE 0x04
173 #if defined(ATARI) || defined(atarist)
174 # define OS_CODE 0x05
178 # define OS_CODE 0x06
181 #if defined(MACOS) || defined(TARGET_OS_MAC)
182 # define OS_CODE 0x07
186 # define OS_CODE 0x08
190 # define OS_CODE 0x09
194 # define OS_CODE 0x0a
197 #ifdef WIN32 /* Window 95 & Windows NT */
198 # define OS_CODE 0x0b
202 # define OS_CODE 0x0c
205 #if 0 /* Acorn RISCOS */
206 # define OS_CODE 0x0d
210 # define OS_CODE 0x0e
213 #ifdef __50SERIES /* Prime/PRIMOS */
214 # define OS_CODE 0x0F
217 /* Default to UNIX */
219 # define OS_CODE 0x03 /* assume Unix */
223 # define GZIP_OS_CODE OS_CODE
226 #define adlerInitial adler32(0L, Z_NULL, 0)
227 #define crcInitial crc32(0L, Z_NULL, 0)
230 static const char * const my_z_errmsg[] = {
231 "need dictionary", /* Z_NEED_DICT 2 */
232 "stream end", /* Z_STREAM_END 1 */
234 "file error", /* Z_ERRNO (-1) */
235 "stream error", /* Z_STREAM_ERROR (-2) */
236 "data error", /* Z_DATA_ERROR (-3) */
237 "insufficient memory", /* Z_MEM_ERROR (-4) */
238 "buffer error", /* Z_BUF_ERROR (-5) */
239 "incompatible version",/* Z_VERSION_ERROR(-6) */
242 #define setDUALstatus(var, err) \
243 sv_setnv(var, (double)err) ; \
244 sv_setpv(var, ((err) ? GetErrorString(err) : "")) ; \
248 #if defined(__SYMBIAN32__)
249 # define NO_WRITEABLE_DATA
252 #define TRACE_DEFAULT 0
254 #ifdef NO_WRITEABLE_DATA
255 # define trace TRACE_DEFAULT
257 static int trace = TRACE_DEFAULT ;
260 /* Dodge PerlIO hiding of these functions. */
265 GetErrorString(int error_no)
267 GetErrorString(error_no)
274 if (error_no == Z_ERRNO) {
275 errstr = Strerror(errno) ;
278 /* errstr = gzerror(fil, &error_no) ; */
279 errstr = (char*) my_z_errmsg[2 - error_no];
288 The following two functions are taken almost directly from
289 examples/gzappend.c. Only cosmetic changes have been made to conform to
290 the coding style of the rest of the code in this file.
294 /* return the greatest common divisor of a and b using Euclid's algorithm,
295 modified to be fast when one argument much greater than the other, and
296 coded to avoid unnecessary swapping */
299 gcd(unsigned a, unsigned b)
324 /* rotate list[0..len-1] left by rot positions, in place */
327 rotate(unsigned char *list, unsigned len, unsigned rot)
329 rotate(list, len, rot)
337 unsigned char *start, *last, *to, *from;
339 /* normalize rot and handle degenerate cases */
341 if (rot >= len) rot %= len;
342 if (rot == 0) return;
344 /* pointer to last entry in list */
345 last = list + (len - 1);
347 /* do simple left shift by one */
350 memcpy(list, list + 1, len - 1);
355 /* do simple right shift by one */
356 if (rot == len - 1) {
358 memmove(list + 1, list, len - 1);
363 /* otherwise do rotate as a set of cycles in place */
364 cycles = gcd(len, rot); /* number of cycles */
366 start = from = list + cycles; /* start index is arbitrary */
367 tmp = *from; /* save entry to be overwritten */
369 to = from; /* next step in cycle */
370 from += rot; /* go right rot positions */
371 if (from > last) from -= len; /* (pointer better not wrap) */
372 if (from == start) break; /* all but one shifted */
373 *to = *from; /* shift left */
375 *to = tmp; /* complete the circle */
379 #endif /* MAGIC_APPEND */
383 DispHex(void * ptr, int length)
390 char * p = (char*)ptr;
392 for (i = 0; i < length; ++i) {
393 printf(" %02x", 0xFF & *(p+i));
400 DispStream(di_stream * s, char * message)
402 DispStream(s, message)
413 #define EnDis(f) (s->flags & f ? "Enabled" : "Disabled")
415 printf("DispStream 0x%p", s) ;
417 printf("- %s \n", message) ;
421 printf(" stream pointer is NULL\n");
424 printf(" stream 0x%p\n", &(s->stream));
425 printf(" zalloc 0x%p\n", s->stream.zalloc);
426 printf(" zfree 0x%p\n", s->stream.zfree);
427 printf(" opaque 0x%p\n", s->stream.opaque);
429 printf(" msg %s\n", s->stream.msg);
432 printf(" next_in 0x%p", s->stream.next_in);
433 if (s->stream.next_in){
435 DispHex(s->stream.next_in, 4);
439 printf(" next_out 0x%p", s->stream.next_out);
440 if (s->stream.next_out){
442 DispHex(s->stream.next_out, 4);
446 printf(" avail_in %lu\n", (unsigned long)s->stream.avail_in);
447 printf(" avail_out %lu\n", (unsigned long)s->stream.avail_out);
448 printf(" total_in %ld\n", s->stream.total_in);
449 printf(" total_out %ld\n", s->stream.total_out);
450 printf(" adler %ld\n", s->stream.adler );
451 printf(" bufsize %ld\n", s->bufsize);
452 printf(" dictionary 0x%p\n", s->dictionary);
453 printf(" dict_adler 0x%ld\n",s->dict_adler);
454 printf(" zip_mode %d\n", s->zip_mode);
455 printf(" crc32 0x%x\n", (unsigned)s->crc32);
456 printf(" adler32 0x%x\n", (unsigned)s->adler32);
457 printf(" flags 0x%x\n", s->flags);
458 printf(" APPEND %s\n", EnDis(FLAG_APPEND));
459 printf(" CRC32 %s\n", EnDis(FLAG_CRC32));
460 printf(" ADLER32 %s\n", EnDis(FLAG_ADLER32));
461 printf(" CONSUME %s\n", EnDis(FLAG_CONSUME_INPUT));
464 printf(" window 0x%p\n", s->window);
480 ZMALLOC(s, di_stream) ;
488 PostInitStream(di_stream * s, int flags, int bufsize, int windowBits)
490 PostInitStream(s, flags, bufsize, windowBits)
497 s->bufsize = bufsize ;
499 s->uncompressedBytes =
502 s->zip_mode = (windowBits < 0) ;
503 if (flags & FLAG_CRC32)
504 s->crc32 = crcInitial ;
505 if (flags & FLAG_ADLER32)
506 s->adler32 = adlerInitial ;
512 deRef(SV * sv, char * string)
529 croak("%s: buffer parameter is not a SCALAR reference", string);
532 croak("%s: buffer parameter is a reference to a reference", string) ;
544 deRef_l(SV * sv, char * string)
566 croak("%s: buffer parameter is not a SCALAR reference", string);
569 croak("%s: buffer parameter is a reference to a reference", string) ;
572 if (SvREADONLY(sv) && PL_curcop != &PL_compiling)
573 croak("%s: buffer parameter is read-only", string);
575 SvUPGRADE(sv, SVt_PV);
587 #include "constants.h"
589 MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib PREFIX = Zip_
594 INCLUDE: constants.xs
597 /* Check this version of zlib is == 1 */
598 if (zlibVersion()[0] != '1')
599 croak("Compress::Raw::Zlib needs zlib version 1.x\n") ;
602 /* Create the $os_code scalar */
603 SV * os_code_sv = perl_get_sv("Compress::Raw::Zlib::gzip_os_code", GV_ADDMULTI) ;
604 sv_setiv(os_code_sv, GZIP_OS_CODE) ;
608 #define Zip_zlib_version() (char*)zlib_version
616 RETVAL = ZLIB_VERNUM ;
618 /* 1.1.4 => 0x1140 */
619 RETVAL = (ZLIB_VERSION[0] - '0') << 12 ;
620 RETVAL += (ZLIB_VERSION[2] - '0') << 8 ;
621 RETVAL += (ZLIB_VERSION[4] - '0') << 4 ;
626 MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib PREFIX = Zip_
628 #define Zip_adler32(buf, adler) adler32(adler, buf, (uInt)len)
631 Zip_adler32(buf, adler=adlerInitial)
632 uLong adler = NO_INIT
634 Bytef * buf = NO_INIT
637 /* If the buffer is a reference, dereference it */
638 sv = deRef(sv, "adler32") ;
639 #ifdef UTF8_AVAILABLE
640 if (DO_UTF8(sv) && !sv_utf8_downgrade(sv, 1))
641 croak("Wide character in Compress::Raw::Zlib::adler32");
643 buf = (Byte*)SvPVbyte(sv, len) ;
646 adler = adlerInitial;
647 else if (SvOK(ST(1)))
648 adler = SvUV(ST(1)) ;
650 adler = adlerInitial;
652 #define Zip_crc32(buf, crc) crc32(crc, buf, (uInt)len)
655 Zip_crc32(buf, crc=crcInitial)
658 Bytef * buf = NO_INIT
661 /* If the buffer is a reference, dereference it */
662 sv = deRef(sv, "crc32") ;
663 #ifdef UTF8_AVAILABLE
664 if (DO_UTF8(sv) && !sv_utf8_downgrade(sv, 1))
665 croak("Wide character in Compress::Raw::Zlib::crc32");
667 buf = (Byte*)SvPVbyte(sv, len) ;
671 else if (SvOK(ST(1)))
678 crc32_combine(crc1, crc2, len2)
683 #ifndef AT_LEAST_ZLIB_1_2_2_1
684 crc1 = crc1; crc2 = crc2 ; len2 = len2; /* Silence -Wall */
685 croak("crc32_combine needs zlib 1.2.3 or better");
687 RETVAL = crc32_combine(crc1, crc2, len2);
694 adler32_combine(adler1, adler2, len2)
699 #ifndef AT_LEAST_ZLIB_1_2_2_1
700 adler1 = adler1; adler2 = adler2 ; len2 = len2; /* Silence -Wall */
701 croak("adler32_combine needs zlib 1.2.3 or better");
703 RETVAL = adler32_combine(adler1, adler2, len2);
709 MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib
712 _deflateInit(flags,level, method, windowBits, memLevel, strategy, bufsize, dictionary)
726 warn("in _deflateInit(level=%d, method=%d, windowBits=%d, memLevel=%d, strategy=%d, bufsize=%ld\n",
727 level, method, windowBits, memLevel, strategy, bufsize) ;
728 if ((s = InitStream() )) {
732 s->WindowBits = windowBits;
733 s->MemLevel = memLevel;
734 s->Strategy = strategy;
736 err = deflateInit2(&(s->stream), level,
737 method, windowBits, memLevel, strategy);
739 /* Check if a dictionary has been specified */
741 if (err == Z_OK && SvCUR(dictionary)) {
742 #ifdef UTF8_AVAILABLE
743 if (DO_UTF8(dictionary) && !sv_utf8_downgrade(dictionary, 1))
744 croak("Wide character in Compress::Raw::Zlib::Deflate::new dicrionary parameter");
746 err = deflateSetDictionary(&(s->stream), (const Bytef*) SvPVbyte_nolen(dictionary),
748 s->dict_adler = s->stream.adler ;
756 PostInitStream(s, flags, bufsize, windowBits) ;
762 XPUSHs(sv_setref_pv(sv_newmortal(),
763 "Compress::Raw::Zlib::deflateStream", (void*)s));
764 if (GIMME == G_ARRAY) {
765 SV * sv = sv_2mortal(newSViv(err)) ;
766 setDUALstatus(sv, err);
771 _inflateInit(flags, windowBits, bufsize, dictionary)
784 croak("inflateScanInit needs zlib 1.2.1 or better");
787 warn("in _inflateInit(windowBits=%d, bufsize=%lu, dictionary=%lu\n",
788 windowBits, bufsize, (unsigned long)SvCUR(dictionary)) ;
789 if ((s = InitStream() )) {
791 s->WindowBits = windowBits;
793 err = inflateInit2(&(s->stream), windowBits);
798 else if (SvCUR(dictionary)) {
799 /* Dictionary specified - take a copy for use in inflate */
800 s->dictionary = newSVsv(dictionary) ;
803 PostInitStream(s, flags, bufsize, windowBits) ;
807 s->window = (unsigned char *)safemalloc(WINDOW_SIZE);
815 XPUSHs(sv_setref_pv(sv_newmortal(),
817 ? "Compress::Raw::Zlib::inflateScanStream"
818 : "Compress::Raw::Zlib::inflateStream",
820 if (GIMME == G_ARRAY) {
821 SV * sv = sv_2mortal(newSViv(err)) ;
822 setDUALstatus(sv, err);
828 MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib::deflateStream
831 DispStream(s, message=NULL)
832 Compress::Raw::Zlib::deflateStream s
837 Compress::Raw::Zlib::deflateStream s
839 RETVAL = deflateReset(&(s->stream)) ;
840 if (RETVAL == Z_OK) {
841 PostInitStream(s, s->flags, s->bufsize, s->WindowBits) ;
847 deflate (s, buf, output)
848 Compress::Raw::Zlib::deflateStream s
851 uInt cur_length = NO_INIT
852 uInt increment = NO_INIT
853 uInt prefix = NO_INIT
855 uLong bufinc = NO_INIT
859 /* If the input buffer is a reference, dereference it */
860 buf = deRef(buf, "deflate") ;
862 /* initialise the input buffer */
863 #ifdef UTF8_AVAILABLE
864 if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
865 croak("Wide character in Compress::Raw::Zlib::Deflate::deflate input parameter");
867 s->stream.next_in = (Bytef*)SvPVbyte_nolen(buf) ;
868 s->stream.avail_in = SvCUR(buf) ;
870 if (s->flags & FLAG_CRC32)
871 s->crc32 = crc32(s->crc32, s->stream.next_in, s->stream.avail_in) ;
873 if (s->flags & FLAG_ADLER32)
874 s->adler32 = adler32(s->adler32, s->stream.next_in, s->stream.avail_in) ;
876 /* and retrieve the output buffer */
877 output = deRef_l(output, "deflate") ;
878 #ifdef UTF8_AVAILABLE
879 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
880 croak("Wide character in Compress::Raw::Zlib::Deflate::deflate output parameter");
883 if((s->flags & FLAG_APPEND) != FLAG_APPEND) {
884 SvCUR_set(output, 0);
885 /* sv_setpvn(output, "", 0); */
887 prefix = cur_length = SvCUR(output) ;
888 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length;
889 increment = SvLEN(output) - cur_length;
890 s->stream.avail_out = increment;
892 /* Check for saved output from deflateParams */
893 if (s->deflateParams_out_valid) {
894 *(s->stream.next_out) = s->deflateParams_out_byte;
895 ++ s->stream.next_out;
896 -- s->stream.avail_out ;
897 s->deflateParams_out_valid = FALSE;
900 /* Check for saved output from deflateParams */
901 if (s->deflateParams_out_length) {
902 uLong plen = s->deflateParams_out_length ;
903 /* printf("Copy %d bytes saved data\n", plen);*/
904 if (s->stream.avail_out < plen) {
905 /*printf("GROW from %d to %d\n", s->stream.avail_out,
906 SvLEN(output) + plen - s->stream.avail_out); */
907 Sv_Grow(output, SvLEN(output) + plen - s->stream.avail_out) ;
910 Copy(s->stream.next_out, s->deflateParams_out_buffer, plen, Bytef) ;
911 cur_length = cur_length + plen;
912 SvCUR_set(output, cur_length);
913 s->stream.next_out += plen ;
914 s->stream.avail_out = SvLEN(output) - cur_length ;
915 increment = s->stream.avail_out;
916 s->deflateParams_out_length = 0;
919 while (s->stream.avail_in != 0) {
921 if (s->stream.avail_out == 0) {
922 /* out of space in the output buffer so make it bigger */
923 Sv_Grow(output, SvLEN(output) + bufinc) ;
924 cur_length += increment ;
925 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length ;
927 s->stream.avail_out = increment;
931 RETVAL = deflate(&(s->stream), Z_NO_FLUSH);
936 s->compressedBytes += cur_length + increment - prefix - s->stream.avail_out ;
937 s->uncompressedBytes += SvCUR(buf) - s->stream.avail_in ;
939 s->last_error = RETVAL ;
940 if (RETVAL == Z_OK) {
942 SvCUR_set(output, cur_length + increment - s->stream.avail_out) ;
951 Compress::Raw::Zlib::deflateStream s
953 deflateEnd(&s->stream) ;
955 SvREFCNT_dec(s->dictionary) ;
957 if (s->deflateParams_out_buffer)
958 Safefree(s->deflateParams_out_buffer);
964 flush(s, output, f=Z_FINISH)
965 Compress::Raw::Zlib::deflateStream s
968 uInt cur_length = NO_INIT
969 uInt increment = NO_INIT
970 uInt prefix = NO_INIT
971 uLong bufinc = NO_INIT
975 s->stream.avail_in = 0; /* should be zero already anyway */
977 /* retrieve the output buffer */
978 output = deRef_l(output, "flush") ;
979 #ifdef UTF8_AVAILABLE
980 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
981 croak("Wide character in Compress::Raw::Zlib::Deflate::flush input parameter");
983 if(! s->flags & FLAG_APPEND) {
984 SvCUR_set(output, 0);
985 /* sv_setpvn(output, "", 0); */
987 prefix = cur_length = SvCUR(output) ;
988 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length;
989 increment = SvLEN(output) - cur_length;
990 s->stream.avail_out = increment;
992 /* Check for saved output from deflateParams */
993 if (s->deflateParams_out_valid) {
994 *(s->stream.next_out) = s->deflateParams_out_byte;
995 ++ s->stream.next_out;
996 -- s->stream.avail_out ;
997 s->deflateParams_out_valid = FALSE;
1000 /* Check for saved output from deflateParams */
1001 if (s->deflateParams_out_length) {
1002 uLong plen = s->deflateParams_out_length ;
1003 /* printf("Copy %d bytes saved data\n", plen); */
1004 if (s->stream.avail_out < plen) {
1005 /* printf("GROW from %d to %d\n", s->stream.avail_out,
1006 SvLEN(output) + plen - s->stream.avail_out); */
1007 Sv_Grow(output, SvLEN(output) + plen - s->stream.avail_out) ;
1010 Copy(s->stream.next_out, s->deflateParams_out_buffer, plen, Bytef) ;
1011 cur_length = cur_length + plen;
1012 SvCUR_set(output, cur_length);
1013 s->stream.next_out += plen ;
1014 s->stream.avail_out = SvLEN(output) - cur_length ;
1015 increment = s->stream.avail_out;
1016 s->deflateParams_out_length = 0;
1021 if (s->stream.avail_out == 0) {
1022 /* consumed all the available output, so extend it */
1023 Sv_Grow(output, SvLEN(output) + bufinc) ;
1024 cur_length += increment ;
1025 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length ;
1026 increment = bufinc ;
1027 s->stream.avail_out = increment;
1030 RETVAL = deflate(&(s->stream), f);
1032 /* deflate has finished flushing only when it hasn't used up
1033 * all the available space in the output buffer:
1035 if (s->stream.avail_out != 0 || RETVAL != Z_OK )
1039 RETVAL = (RETVAL == Z_STREAM_END ? Z_OK : RETVAL) ;
1040 s->last_error = RETVAL ;
1042 s->compressedBytes += cur_length + increment - prefix - s->stream.avail_out ;
1044 if (RETVAL == Z_OK) {
1046 SvCUR_set(output, cur_length + increment - s->stream.avail_out) ;
1054 _deflateParams(s, flags, level, strategy, bufsize)
1055 Compress::Raw::Zlib::deflateStream s
1061 /* printf("_deflateParams(Flags %d Level %d Strategy %d Bufsize %d)\n", flags, level, strategy, bufsize);
1062 printf("Before -- Level %d, Strategy %d, Bufsize %d\n", s->Level, s->Strategy, s->bufsize); */
1066 s->Strategy = strategy ;
1068 s->bufsize = bufsize;
1070 /* printf("After -- Level %d, Strategy %d, Bufsize %d\n", s->Level, s->Strategy, s->bufsize);*/
1072 s->stream.avail_in = 0;
1073 s->stream.next_out = &(s->deflateParams_out_byte) ;
1074 s->stream.avail_out = 1;
1075 RETVAL = deflateParams(&(s->stream), s->Level, s->Strategy);
1076 s->deflateParams_out_valid =
1077 (RETVAL == Z_OK && s->stream.avail_out == 0) ;
1078 /* printf("RETVAL %d, avail out %d, byte %c\n", RETVAL, s->stream.avail_out, s->deflateParams_out_byte); */
1080 /* printf("Level %d Strategy %d, Prev Len %d\n",
1081 s->Level, s->Strategy, s->deflateParams_out_length); */
1082 s->stream.avail_in = 0;
1083 if (s->deflateParams_out_buffer == NULL)
1084 s->deflateParams_out_buffer = safemalloc(deflateParams_BUFFER_SIZE);
1085 s->stream.next_out = s->deflateParams_out_buffer ;
1086 s->stream.avail_out = deflateParams_BUFFER_SIZE;
1088 RETVAL = deflateParams(&(s->stream), s->Level, s->Strategy);
1089 s->deflateParams_out_length = deflateParams_BUFFER_SIZE - s->stream.avail_out;
1090 /* printf("RETVAL %d, length out %d, avail %d\n",
1091 RETVAL, s->deflateParams_out_length, s->stream.avail_out ); */
1099 Compress::Raw::Zlib::deflateStream s
1107 Compress::Raw::Zlib::deflateStream s
1109 RETVAL = s->Strategy ;
1116 Compress::Raw::Zlib::deflateStream s
1118 RETVAL = s->bufsize ;
1125 Compress::Raw::Zlib::deflateStream s
1127 RETVAL = s->last_error ;
1133 Compress::Raw::Zlib::deflateStream s
1141 Compress::Raw::Zlib::deflateStream s
1143 RETVAL = s->dict_adler ;
1149 Compress::Raw::Zlib::deflateStream s
1151 RETVAL = s->adler32 ;
1157 Compress::Raw::Zlib::deflateStream s
1159 RETVAL = s->compressedBytes;
1164 uncompressedBytes(s)
1165 Compress::Raw::Zlib::deflateStream s
1167 RETVAL = s->uncompressedBytes;
1173 Compress::Raw::Zlib::deflateStream s
1175 RETVAL = s->stream.total_in ;
1181 Compress::Raw::Zlib::deflateStream s
1183 RETVAL = s->stream.total_out ;
1189 Compress::Raw::Zlib::deflateStream s
1191 RETVAL = s->stream.msg;
1196 deflateTune(s, good_length, max_lazy, nice_length, max_chain)
1197 Compress::Raw::Zlib::deflateStream s
1203 #ifndef AT_LEAST_ZLIB_1_2_2_3
1204 good_length = good_length; max_lazy = max_lazy ; /* Silence -Wall */
1205 nice_length = nice_length; max_chain = max_chain; /* Silence -Wall */
1206 croak("deflateTune needs zlib 1.2.2.3 or better");
1208 RETVAL = deflateTune(&(s->stream), good_length, max_lazy, nice_length, max_chain);
1214 MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib::inflateStream
1217 DispStream(s, message=NULL)
1218 Compress::Raw::Zlib::inflateStream s
1223 Compress::Raw::Zlib::inflateStream s
1225 RETVAL = inflateReset(&(s->stream)) ;
1226 if (RETVAL == Z_OK) {
1227 PostInitStream(s, s->flags, s->bufsize, s->WindowBits) ;
1233 inflate (s, buf, output, eof=FALSE)
1234 Compress::Raw::Zlib::inflateStream s
1238 uInt cur_length = 0;
1239 uInt prefix_length = 0;
1241 STRLEN stmp = NO_INIT
1242 uLong bufinc = NO_INIT
1244 #ifdef UTF8_AVAILABLE
1245 bool out_utf8 = FALSE;
1248 bufinc = s->bufsize;
1249 /* If the buffer is a reference, dereference it */
1250 buf = deRef(buf, "inflate") ;
1252 if (s->flags & FLAG_CONSUME_INPUT && SvREADONLY(buf))
1253 croak("Compress::Raw::Zlib::Inflate::inflate input parameter cannot be read-only when ConsumeInput is specified");
1254 #ifdef UTF8_AVAILABLE
1255 if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
1256 croak("Wide character in Compress::Raw::Zlib::Inflate::inflate input parameter");
1259 /* initialise the input buffer */
1260 s->stream.next_in = (Bytef*)SvPVbyte_force(buf, stmp) ;
1261 s->stream.avail_in = SvCUR(buf) ;
1263 /* and retrieve the output buffer */
1264 output = deRef_l(output, "inflate") ;
1265 #ifdef UTF8_AVAILABLE
1266 if (DO_UTF8(output))
1268 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
1269 croak("Wide character in Compress::Raw::Zlib::Inflate::inflate output parameter");
1271 if((s->flags & FLAG_APPEND) != FLAG_APPEND) {
1272 SvCUR_set(output, 0);
1274 if (SvLEN(output)) {
1275 prefix_length = cur_length = SvCUR(output) ;
1276 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length;
1277 increment = SvLEN(output) - cur_length - 1;
1278 s->stream.avail_out = increment;
1281 s->stream.avail_out = 0;
1283 s->bytesInflated = 0;
1287 if (s->stream.avail_out == 0 ) {
1288 /* out of space in the output buffer so make it bigger */
1289 Sv_Grow(output, SvLEN(output) + bufinc) ;
1290 cur_length += increment ;
1291 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length ;
1292 increment = bufinc ;
1293 s->stream.avail_out = increment;
1297 RETVAL = inflate(&(s->stream), Z_SYNC_FLUSH);
1299 if (RETVAL == Z_STREAM_ERROR || RETVAL == Z_MEM_ERROR ||
1300 RETVAL == Z_DATA_ERROR || RETVAL == Z_STREAM_END )
1303 if (RETVAL == Z_BUF_ERROR) {
1304 if (s->stream.avail_out == 0)
1306 if (s->stream.avail_in == 0) {
1312 if (RETVAL == Z_NEED_DICT && s->dictionary) {
1313 s->dict_adler = s->stream.adler ;
1314 RETVAL = inflateSetDictionary(&(s->stream),
1315 (const Bytef*)SvPVbyte_nolen(s->dictionary),
1316 SvCUR(s->dictionary));
1322 #ifdef NEED_DUMMY_BYTE_AT_END
1323 if (eof && RETVAL == Z_OK) {
1324 Bytef* nextIn = s->stream.next_in;
1325 uInt availIn = s->stream.avail_in;
1326 s->stream.next_in = (Bytef*) " ";
1327 s->stream.avail_in = 1;
1328 if (s->stream.avail_out == 0) {
1329 /* out of space in the output buffer so make it bigger */
1330 Sv_Grow(output, SvLEN(output) + bufinc) ;
1331 cur_length += increment ;
1332 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length ;
1333 increment = bufinc ;
1334 s->stream.avail_out = increment;
1337 RETVAL = inflate(&(s->stream), Z_SYNC_FLUSH);
1338 s->stream.next_in = nextIn ;
1339 s->stream.avail_in = availIn ;
1343 s->last_error = RETVAL ;
1344 if (RETVAL == Z_OK || RETVAL == Z_STREAM_END || RETVAL == Z_DATA_ERROR) {
1347 s->bytesInflated = cur_length + increment - s->stream.avail_out - prefix_length;
1348 s->uncompressedBytes += s->bytesInflated ;
1349 s->compressedBytes += SvCUR(buf) - s->stream.avail_in ;
1352 SvCUR_set(output, prefix_length + s->bytesInflated) ;
1353 *SvEND(output) = '\0';
1354 #ifdef UTF8_AVAILABLE
1356 sv_utf8_upgrade(output);
1360 if (s->flags & FLAG_CRC32 )
1361 s->crc32 = crc32(s->crc32,
1362 (const Bytef*)SvPVbyte_nolen(output)+prefix_length,
1363 SvCUR(output)-prefix_length) ;
1365 if (s->flags & FLAG_ADLER32)
1366 s->adler32 = adler32(s->adler32,
1367 (const Bytef*)SvPVbyte_nolen(output)+prefix_length,
1368 SvCUR(output)-prefix_length) ;
1370 /* fix the input buffer */
1371 if (s->flags & FLAG_CONSUME_INPUT) {
1372 in = s->stream.avail_in ;
1373 SvCUR_set(buf, in) ;
1375 Move(s->stream.next_in, SvPVbyte_nolen(buf), in, char) ;
1385 Compress::Raw::Zlib::inflateStream s
1387 RETVAL = s->bytesInflated;
1393 Compress::Raw::Zlib::inflateStream s
1395 RETVAL = s->compressedBytes;
1400 uncompressedBytes(s)
1401 Compress::Raw::Zlib::inflateStream s
1403 RETVAL = s->uncompressedBytes;
1409 inflateSync (s, buf)
1410 Compress::Raw::Zlib::inflateStream s
1414 /* If the buffer is a reference, dereference it */
1415 buf = deRef(buf, "inflateSync") ;
1416 #ifdef UTF8_AVAILABLE
1417 if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
1418 croak("Wide character in Compress::Raw::Zlib::Inflate::inflateSync");
1421 /* initialise the input buffer */
1422 s->stream.next_in = (Bytef*)SvPVbyte_nolen(buf) ;
1423 s->stream.avail_in = SvCUR(buf) ;
1425 /* inflateSync doesn't create any output */
1426 s->stream.next_out = (Bytef*) NULL;
1427 s->stream.avail_out = 0;
1429 RETVAL = inflateSync(&(s->stream));
1430 s->last_error = RETVAL ;
1432 /* fix the input buffer */
1434 unsigned in = s->stream.avail_in ;
1435 SvCUR_set(buf, in) ;
1437 Move(s->stream.next_in, SvPVbyte_nolen(buf), in, char) ;
1446 Compress::Raw::Zlib::inflateStream s
1448 inflateEnd(&s->stream) ;
1450 SvREFCNT_dec(s->dictionary) ;
1452 if (s->deflateParams_out_buffer)
1453 Safefree(s->deflateParams_out_buffer);
1457 Safefree(s->window);
1464 Compress::Raw::Zlib::inflateStream s
1466 RETVAL = s->last_error ;
1472 Compress::Raw::Zlib::inflateStream s
1480 Compress::Raw::Zlib::inflateStream s
1482 RETVAL = s->dict_adler ;
1488 Compress::Raw::Zlib::inflateStream s
1490 RETVAL = s->stream.total_in ;
1496 Compress::Raw::Zlib::inflateStream s
1498 RETVAL = s->adler32 ;
1504 Compress::Raw::Zlib::inflateStream s
1506 RETVAL = s->stream.total_out ;
1512 Compress::Raw::Zlib::inflateStream s
1514 RETVAL = s->stream.msg;
1521 Compress::Raw::Zlib::inflateStream s
1523 RETVAL = s->bufsize ;
1529 Compress::Raw::Zlib::inflateStream s
1532 RETVAL = ((s->flags & FLAG_APPEND) == FLAG_APPEND);
1534 s->flags |= FLAG_APPEND ;
1536 s->flags &= ~FLAG_APPEND ;
1540 MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib::inflateScanStream
1544 Compress::Raw::Zlib::inflateScanStream s
1546 inflateEnd(&s->stream) ;
1548 SvREFCNT_dec(s->dictionary) ;
1550 if (s->deflateParams_out_buffer)
1551 Safefree(s->deflateParams_out_buffer);
1555 Safefree(s->window);
1560 DispStream(s, message=NULL)
1561 Compress::Raw::Zlib::inflateScanStream s
1566 Compress::Raw::Zlib::inflateScanStream s
1568 RETVAL = inflateReset(&(s->stream)) ;
1569 if (RETVAL == Z_OK) {
1570 PostInitStream(s, s->flags, s->bufsize, s->WindowBits) ;
1576 scan(s, buf, out=NULL, eof=FALSE)
1577 Compress::Raw::Zlib::inflateScanStream s
1581 bool eof_mode = FALSE;
1582 int start_len = NO_INIT
1583 STRLEN stmp = NO_INIT
1585 /* If the input buffer is a reference, dereference it */
1586 #ifndef MAGIC_APPEND
1588 croak("scan needs zlib 1.2.1 or better");
1590 buf = deRef(buf, "inflateScan") ;
1591 #ifdef UTF8_AVAILABLE
1592 if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
1593 croak("Wide character in Compress::Raw::Zlib::InflateScan::scan input parameter");
1595 /* initialise the input buffer */
1596 s->stream.next_in = (Bytef*)SvPVbyte_force(buf, stmp) ;
1597 s->stream.avail_in = SvCUR(buf) ;
1598 start_len = s->stream.avail_in ;
1599 s->bytesInflated = 0 ;
1602 if (s->stream.avail_in == 0) {
1607 /* set up output to next available section of sliding window */
1608 s->stream.avail_out = WINDOW_SIZE - s->window_have;
1609 s->stream.next_out = s->window + s->window_have;
1611 /* DispStream(s, "before inflate\n"); */
1613 /* inflate and check for errors */
1614 RETVAL = inflate(&(s->stream), Z_BLOCK);
1616 if (start_len > 1 && ! eof_mode)
1617 s->window_lastByte = *(s->stream.next_in - 1 ) ;
1619 if (RETVAL == Z_STREAM_ERROR || RETVAL == Z_MEM_ERROR ||
1620 RETVAL == Z_DATA_ERROR )
1623 if (s->flags & FLAG_CRC32 )
1624 s->crc32 = crc32(s->crc32, s->window + s->window_have,
1625 WINDOW_SIZE - s->window_have - s->stream.avail_out);
1627 if (s->flags & FLAG_ADLER32)
1628 s->adler32 = adler32(s->adler32, s->window + s->window_have,
1629 WINDOW_SIZE - s->window_have - s->stream.avail_out);
1631 s->uncompressedBytes =
1632 s->bytesInflated += WINDOW_SIZE - s->window_have - s->stream.avail_out;
1634 if (s->stream.avail_out)
1635 s->window_have = WINDOW_SIZE - s->stream.avail_out;
1641 /* process end of block */
1642 if (s->stream.data_type & 128) {
1643 if (s->stream.data_type & 64) {
1644 s->window_left = s->stream.data_type & 0x1f;
1647 s->window_lastbit = s->stream.data_type & 0x1f;
1648 s->lastBlockOffset = s->stream.total_in;
1652 } while (RETVAL != Z_STREAM_END);
1654 s->last_error = RETVAL ;
1655 s->window_lastoff = s->stream.total_in ;
1656 s->compressedBytes += SvCUR(buf) - s->stream.avail_in ;
1658 if (RETVAL == Z_STREAM_END)
1660 s->matchedEndBlock = 1 ;
1662 /* save the location of the end of the compressed data */
1663 s->window_end = SvCUR(buf) - s->stream.avail_in - 1 ;
1664 s->window_endOffset = s->stream.total_in ;
1667 -- s->window_endOffset ;
1670 /* if window wrapped, build dictionary from window by rotating */
1671 if (s->window_full) {
1672 rotate(s->window, WINDOW_SIZE, s->window_have);
1673 s->window_have = WINDOW_SIZE;
1676 /* if (s->flags & FLAG_CONSUME_INPUT) { */
1678 unsigned in = s->stream.avail_in ;
1679 SvCUR_set(buf, in) ;
1681 Move(s->stream.next_in, SvPVbyte_nolen(buf), in, char) ;
1693 Compress::Raw::Zlib::inflateScanStream s
1695 #ifndef MAGIC_APPEND
1696 croak("getEndOffset needs zlib 1.2.1 or better");
1698 RETVAL = s->window_endOffset;
1705 Compress::Raw::Zlib::inflateScanStream s
1707 #ifndef MAGIC_APPEND
1708 croak("inflateCount needs zlib 1.2.1 or better");
1710 RETVAL = s->bytesInflated;
1717 Compress::Raw::Zlib::inflateScanStream s
1719 RETVAL = s->compressedBytes;
1724 uncompressedBytes(s)
1725 Compress::Raw::Zlib::inflateScanStream s
1727 RETVAL = s->uncompressedBytes;
1733 getLastBlockOffset(s)
1734 Compress::Raw::Zlib::inflateScanStream s
1736 #ifndef MAGIC_APPEND
1737 croak("getLastBlockOffset needs zlib 1.2.1 or better");
1739 RETVAL = s->lastBlockOffset - (s->window_lastbit != 0);
1745 getLastBufferOffset(s)
1746 Compress::Raw::Zlib::inflateScanStream s
1748 #ifndef MAGIC_APPEND
1749 croak("getLastBufferOffset needs zlib 1.2.1 or better");
1751 RETVAL = s->window_lastoff;
1757 resetLastBlockByte(s, byte)
1758 Compress::Raw::Zlib::inflateScanStream s
1761 #ifndef MAGIC_APPEND
1762 croak("resetLastBlockByte needs zlib 1.2.1 or better");
1765 *byte = *byte ^ (1 << ((8 - s->window_lastbit) & 7));
1770 _createDeflateStream(inf_s, flags,level, method, windowBits, memLevel, strategy, bufsize)
1771 Compress::Raw::Zlib::inflateScanStream inf_s
1781 #ifndef MAGIC_APPEND
1785 windowBits = windowBits;
1786 memLevel = memLevel;
1787 strategy = strategy;
1789 croak("_createDeflateStream needs zlib 1.2.1 or better");
1795 warn("in _createDeflateStream(level=%d, method=%d, windowBits=%d, memLevel=%d, strategy=%d, bufsize=%lu\n",
1796 level, method, windowBits, memLevel, strategy, bufsize) ;
1797 if ((s = InitStream() )) {
1801 s->WindowBits = windowBits;
1802 s->MemLevel = memLevel;
1803 s->Strategy = strategy;
1805 err = deflateInit2(&(s->stream), level,
1806 method, windowBits, memLevel, strategy);
1809 err = deflateSetDictionary(&(s->stream), inf_s->window, inf_s->window_have);
1810 s->dict_adler = s->stream.adler ;
1818 PostInitStream(s, flags, bufsize, windowBits) ;
1819 s->crc32 = inf_s->crc32;
1820 s->adler32 = inf_s->adler32;
1821 s->stream.adler = inf_s->stream.adler ;
1822 /* s->stream.total_out = inf_s->bytesInflated ; */
1823 s->stream.total_in = inf_s->stream.total_out ;
1824 if (inf_s->window_left) {
1825 /* printf("** window_left %d, window_lastByte %d\n", inf_s->window_left, inf_s->window_lastByte); */
1826 deflatePrime(&(s->stream), 8 - inf_s->window_left, inf_s->window_lastByte);
1833 XPUSHs(sv_setref_pv(sv_newmortal(),
1834 "Compress::Raw::Zlib::deflateStream", (void*)s));
1835 if (GIMME == G_ARRAY) {
1836 SV * sv = sv_2mortal(newSViv(err)) ;
1837 setDUALstatus(sv, err);
1845 Compress::Raw::Zlib::inflateScanStream s
1847 RETVAL = s->last_error ;
1853 Compress::Raw::Zlib::inflateScanStream s
1862 Compress::Raw::Zlib::inflateScanStream s
1864 RETVAL = s->adler32 ;