2 * Author : Paul Marquess, <pmqs@cpan.org>
3 * Created : 22nd January 1996
6 * Copyright (c) 1995-2009 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 >= 0x1222
54 # define AT_LEAST_ZLIB_1_2_2_2
57 #if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1223
58 # define AT_LEAST_ZLIB_1_2_2_3
61 #if defined(ZLIB_VERNUM) && ZLIB_VERNUM >= 0x1230
62 # define AT_LEAST_ZLIB_1_2_3
66 # define NEED_sv_2pvbyte
67 # define NEED_sv_2pv_nolen
71 #if PERL_REVISION == 5 && PERL_VERSION == 9
73 # define sv_pvbyte_force(sv,lp) sv_pvbyten_force(sv,lp)
76 #if PERL_REVISION == 5 && (PERL_VERSION < 8 || (PERL_VERSION == 8 && PERL_SUBVERSION < 4 ))
78 # ifdef SvPVbyte_force
79 # undef SvPVbyte_force
82 # define SvPVbyte_force(sv,lp) SvPV_force(sv,lp)
86 #ifndef SvPVbyte_nolen
87 # define SvPVbyte_nolen SvPV_nolen
93 # ifndef SvPVbyte_nolen
94 # define SvPVbyte_nolen SvPV_nolen
97 # ifndef SvPVbyte_force
98 # define SvPVbyte_force(sv,lp) SvPV_force(sv,lp)
102 #if PERL_REVISION == 5 && (PERL_VERSION >= 8 || (PERL_VERSION == 8 && PERL_SUBVERSION < 4 ))
103 # define UTF8_AVAILABLE
106 typedef int DualType ;
107 typedef int int_undef ;
109 typedef struct di_stream {
111 #define FLAG_APPEND 1
113 #define FLAG_ADLER32 4
114 #define FLAG_CONSUME_INPUT 8
115 #define FLAG_LIMIT_OUTPUT 16
126 bool deflateParams_out_valid ;
127 Bytef deflateParams_out_byte;
129 #define deflateParams_BUFFER_SIZE 0x4000
130 uLong deflateParams_out_length;
131 Bytef* deflateParams_out_buffer;
138 uLong bytesInflated ;
139 uLong compressedBytes ;
140 uLong uncompressedBytes ;
143 #define WINDOW_SIZE 32768U
145 bool matchedEndBlock;
147 int window_lastbit, window_left, window_full;
148 unsigned window_have;
149 off_t window_lastoff, window_end;
150 off_t window_endOffset;
152 uLong lastBlockOffset ;
153 unsigned char window_lastByte ;
159 typedef di_stream * deflateStream ;
160 typedef di_stream * Compress__Raw__Zlib__deflateStream ;
161 typedef di_stream * inflateStream ;
162 typedef di_stream * Compress__Raw__Zlib__inflateStream ;
163 typedef di_stream * Compress__Raw__Zlib__inflateScanStream ;
165 #define ZMALLOC(to, typ) ((to = (typ *)safemalloc(sizeof(typ))), \
168 /* Figure out the Operating System */
170 # define OS_CODE 0x00
173 #if defined(AMIGA) || defined(AMIGAOS)
174 # define OS_CODE 0x01
177 #if defined(VAXC) || defined(VMS)
178 # define OS_CODE 0x02
182 # define OS_CODE 0x04
185 #if defined(ATARI) || defined(atarist)
186 # define OS_CODE 0x05
190 # define OS_CODE 0x06
193 #if defined(MACOS) || defined(TARGET_OS_MAC)
194 # define OS_CODE 0x07
198 # define OS_CODE 0x08
202 # define OS_CODE 0x09
206 # define OS_CODE 0x0a
209 #ifdef WIN32 /* Window 95 & Windows NT */
210 # define OS_CODE 0x0b
214 # define OS_CODE 0x0c
217 #if 0 /* Acorn RISCOS */
218 # define OS_CODE 0x0d
222 # define OS_CODE 0x0e
225 #ifdef __50SERIES /* Prime/PRIMOS */
226 # define OS_CODE 0x0F
229 /* Default to UNIX */
231 # define OS_CODE 0x03 /* assume Unix */
235 # define GZIP_OS_CODE OS_CODE
238 #define adlerInitial adler32(0L, Z_NULL, 0)
239 #define crcInitial crc32(0L, Z_NULL, 0)
241 /* static const char * const my_z_errmsg[] = { */
242 static const char my_z_errmsg[][32] = {
243 "need dictionary", /* Z_NEED_DICT 2 */
244 "stream end", /* Z_STREAM_END 1 */
246 "file error", /* Z_ERRNO (-1) */
247 "stream error", /* Z_STREAM_ERROR (-2) */
248 "data error", /* Z_DATA_ERROR (-3) */
249 "insufficient memory", /* Z_MEM_ERROR (-4) */
250 "buffer error", /* Z_BUF_ERROR (-5) */
251 "incompatible version",/* Z_VERSION_ERROR(-6) */
254 #define setDUALstatus(var, err) \
255 sv_setnv(var, (double)err) ; \
256 sv_setpv(var, ((err) ? GetErrorString(err) : "")) ; \
260 #if defined(__SYMBIAN32__)
261 # define NO_WRITEABLE_DATA
264 #define TRACE_DEFAULT 0
266 #ifdef NO_WRITEABLE_DATA
267 # define trace TRACE_DEFAULT
269 static int trace = TRACE_DEFAULT ;
272 /* Dodge PerlIO hiding of these functions. */
277 GetErrorString(int error_no)
279 GetErrorString(error_no)
286 if (error_no == Z_ERRNO) {
287 errstr = Strerror(errno) ;
290 /* errstr = gzerror(fil, &error_no) ; */
291 errstr = (char*) my_z_errmsg[2 - error_no];
300 The following two functions are taken almost directly from
301 examples/gzappend.c. Only cosmetic changes have been made to conform to
302 the coding style of the rest of the code in this file.
306 /* return the greatest common divisor of a and b using Euclid's algorithm,
307 modified to be fast when one argument much greater than the other, and
308 coded to avoid unnecessary swapping */
311 gcd(unsigned a, unsigned b)
336 /* rotate list[0..len-1] left by rot positions, in place */
339 rotate(unsigned char *list, unsigned len, unsigned rot)
341 rotate(list, len, rot)
349 unsigned char *start, *last, *to, *from;
351 /* normalize rot and handle degenerate cases */
353 if (rot >= len) rot %= len;
354 if (rot == 0) return;
356 /* pointer to last entry in list */
357 last = list + (len - 1);
359 /* do simple left shift by one */
362 memcpy(list, list + 1, len - 1);
367 /* do simple right shift by one */
368 if (rot == len - 1) {
370 memmove(list + 1, list, len - 1);
375 /* otherwise do rotate as a set of cycles in place */
376 cycles = gcd(len, rot); /* number of cycles */
378 start = from = list + cycles; /* start index is arbitrary */
379 tmp = *from; /* save entry to be overwritten */
381 to = from; /* next step in cycle */
382 from += rot; /* go right rot positions */
383 if (from > last) from -= len; /* (pointer better not wrap) */
384 if (from == start) break; /* all but one shifted */
385 *to = *from; /* shift left */
387 *to = tmp; /* complete the circle */
391 #endif /* MAGIC_APPEND */
395 DispHex(void * ptr, int length)
402 char * p = (char*)ptr;
404 for (i = 0; i < length; ++i) {
405 printf(" %02x", 0xFF & *(p+i));
412 DispStream(di_stream * s, char * message)
414 DispStream(s, message)
425 #define EnDis(f) (s->flags & f ? "Enabled" : "Disabled")
427 printf("DispStream 0x%p", s) ;
429 printf("- %s \n", message) ;
433 printf(" stream pointer is NULL\n");
436 printf(" stream 0x%p\n", &(s->stream));
437 printf(" zalloc 0x%p\n", s->stream.zalloc);
438 printf(" zfree 0x%p\n", s->stream.zfree);
439 printf(" opaque 0x%p\n", s->stream.opaque);
441 printf(" msg %s\n", s->stream.msg);
444 printf(" next_in 0x%p", s->stream.next_in);
445 if (s->stream.next_in){
447 DispHex(s->stream.next_in, 4);
451 printf(" next_out 0x%p", s->stream.next_out);
452 if (s->stream.next_out){
454 DispHex(s->stream.next_out, 4);
458 printf(" avail_in %lu\n", (unsigned long)s->stream.avail_in);
459 printf(" avail_out %lu\n", (unsigned long)s->stream.avail_out);
460 printf(" total_in %ld\n", s->stream.total_in);
461 printf(" total_out %ld\n", s->stream.total_out);
462 printf(" adler %ld\n", s->stream.adler );
463 printf(" bufsize %ld\n", s->bufsize);
464 printf(" dictionary 0x%p\n", s->dictionary);
465 printf(" dict_adler 0x%ld\n",s->dict_adler);
466 printf(" zip_mode %d\n", s->zip_mode);
467 printf(" crc32 0x%x\n", (unsigned)s->crc32);
468 printf(" adler32 0x%x\n", (unsigned)s->adler32);
469 printf(" flags 0x%x\n", s->flags);
470 printf(" APPEND %s\n", EnDis(FLAG_APPEND));
471 printf(" CRC32 %s\n", EnDis(FLAG_CRC32));
472 printf(" ADLER32 %s\n", EnDis(FLAG_ADLER32));
473 printf(" CONSUME %s\n", EnDis(FLAG_CONSUME_INPUT));
474 printf(" LIMIT %s\n", EnDis(FLAG_LIMIT_OUTPUT));
478 printf(" window 0x%p\n", s->window);
494 ZMALLOC(s, di_stream) ;
502 PostInitStream(di_stream * s, int flags, int bufsize, int windowBits)
504 PostInitStream(s, flags, bufsize, windowBits)
511 s->bufsize = bufsize ;
513 s->uncompressedBytes =
516 s->zip_mode = (windowBits < 0) ;
517 if (flags & FLAG_CRC32)
518 s->crc32 = crcInitial ;
519 if (flags & FLAG_ADLER32)
520 s->adler32 = adlerInitial ;
526 deRef(SV * sv, const char * string)
543 croak("%s: buffer parameter is not a SCALAR reference", string);
548 croak("%s: buffer parameter is a reference to a reference", string) ;
560 deRef_l(SV * sv, const char * string)
582 croak("%s: buffer parameter is not a SCALAR reference", string);
587 croak("%s: buffer parameter is a reference to a reference", string) ;
590 if (SvREADONLY(sv) && PL_curcop != &PL_compiling)
591 croak("%s: buffer parameter is read-only", string);
593 SvUPGRADE(sv, SVt_PV);
605 #include "constants.h"
607 MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib PREFIX = Zip_
612 INCLUDE: constants.xs
615 /* Check this version of zlib is == 1 */
616 if (zlibVersion()[0] != '1')
617 croak("Compress::Raw::Zlib needs zlib version 1.x\n") ;
620 /* Create the $os_code scalar */
621 SV * os_code_sv = perl_get_sv("Compress::Raw::Zlib::gzip_os_code", GV_ADDMULTI) ;
622 sv_setiv(os_code_sv, GZIP_OS_CODE) ;
626 #define Zip_zlib_version() (const char*)zlib_version
634 RETVAL = ZLIB_VERNUM ;
636 /* 1.1.4 => 0x1140 */
637 RETVAL = (ZLIB_VERSION[0] - '0') << 12 ;
638 RETVAL += (ZLIB_VERSION[2] - '0') << 8 ;
639 RETVAL += (ZLIB_VERSION[4] - '0') << 4 ;
644 MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib PREFIX = Zip_
646 #define Zip_adler32(buf, adler) adler32(adler, buf, (uInt)len)
649 Zip_adler32(buf, adler=adlerInitial)
650 uLong adler = NO_INIT
652 Bytef * buf = NO_INIT
655 /* If the buffer is a reference, dereference it */
656 sv = deRef(sv, "adler32") ;
657 #ifdef UTF8_AVAILABLE
658 if (DO_UTF8(sv) && !sv_utf8_downgrade(sv, 1))
659 croak("Wide character in Compress::Raw::Zlib::adler32");
661 buf = (Byte*)SvPVbyte(sv, len) ;
664 adler = adlerInitial;
665 else if (SvOK(ST(1)))
666 adler = SvUV(ST(1)) ;
668 adler = adlerInitial;
672 #define Zip_crc32(buf, crc) crc32(crc, buf, (uInt)len)
675 Zip_crc32(buf, crc=crcInitial)
678 Bytef * buf = NO_INIT
681 /* If the buffer is a reference, dereference it */
682 sv = deRef(sv, "crc32") ;
683 #ifdef UTF8_AVAILABLE
684 if (DO_UTF8(sv) && !sv_utf8_downgrade(sv, 1))
685 croak("Wide character in Compress::Raw::Zlib::crc32");
687 buf = (Byte*)SvPVbyte(sv, len) ;
691 else if (SvOK(ST(1)))
698 crc32_combine(crc1, crc2, len2)
703 #ifndef AT_LEAST_ZLIB_1_2_2_1
704 crc1 = crc1; crc2 = crc2 ; len2 = len2; /* Silence -Wall */
705 croak("crc32_combine needs zlib 1.2.3 or better");
707 RETVAL = crc32_combine(crc1, crc2, len2);
714 adler32_combine(adler1, adler2, len2)
719 #ifndef AT_LEAST_ZLIB_1_2_2_1
720 adler1 = adler1; adler2 = adler2 ; len2 = len2; /* Silence -Wall */
721 croak("adler32_combine needs zlib 1.2.3 or better");
723 RETVAL = adler32_combine(adler1, adler2, len2);
729 MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib
732 _deflateInit(flags,level, method, windowBits, memLevel, strategy, bufsize, dictionary)
746 warn("in _deflateInit(level=%d, method=%d, windowBits=%d, memLevel=%d, strategy=%d, bufsize=%ld dictionary=%p)\n",
747 level, method, windowBits, memLevel, strategy, bufsize, dictionary) ;
748 if ((s = InitStream() )) {
752 s->WindowBits = windowBits;
753 s->MemLevel = memLevel;
754 s->Strategy = strategy;
756 err = deflateInit2(&(s->stream), level,
757 method, windowBits, memLevel, strategy);
759 /* Check if a dictionary has been specified */
761 if (err == Z_OK && SvCUR(dictionary)) {
762 #ifdef UTF8_AVAILABLE
763 if (DO_UTF8(dictionary) && !sv_utf8_downgrade(dictionary, 1))
764 croak("Wide character in Compress::Raw::Zlib::Deflate::new dicrionary parameter");
766 err = deflateSetDictionary(&(s->stream), (const Bytef*) SvPVbyte_nolen(dictionary),
768 s->dict_adler = s->stream.adler ;
776 PostInitStream(s, flags, bufsize, windowBits) ;
783 SV* obj = sv_setref_pv(sv_newmortal(),
784 "Compress::Raw::Zlib::deflateStream", (void*)s);
787 if (GIMME == G_ARRAY) {
788 SV * sv = sv_2mortal(newSViv(err)) ;
789 setDUALstatus(sv, err);
794 _inflateInit(flags, windowBits, bufsize, dictionary)
807 croak("inflateScanInit needs zlib 1.2.1 or better");
810 warn("in _inflateInit(windowBits=%d, bufsize=%lu, dictionary=%lu\n",
811 windowBits, bufsize, (unsigned long)SvCUR(dictionary)) ;
812 if ((s = InitStream() )) {
814 s->WindowBits = windowBits;
816 err = inflateInit2(&(s->stream), windowBits);
821 else if (SvCUR(dictionary)) {
822 #ifdef AT_LEAST_ZLIB_1_2_2_1
823 /* Zlib 1.2.2.1 or better allows a dictionary with raw inflate */
824 if (s->WindowBits < 0) {
825 err = inflateSetDictionary(&(s->stream),
826 (const Bytef*)SvPVbyte_nolen(dictionary),
835 /* Dictionary specified - take a copy for use in inflate */
836 s->dictionary = newSVsv(dictionary) ;
839 PostInitStream(s, flags, bufsize, windowBits) ;
843 s->window = (unsigned char *)safemalloc(WINDOW_SIZE);
852 SV* obj = sv_setref_pv(sv_newmortal(),
854 ? "Compress::Raw::Zlib::inflateScanStream"
855 : "Compress::Raw::Zlib::inflateStream",
859 if (GIMME == G_ARRAY) {
860 SV * sv = sv_2mortal(newSViv(err)) ;
861 setDUALstatus(sv, err);
867 MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib::deflateStream
870 DispStream(s, message=NULL)
871 Compress::Raw::Zlib::deflateStream s
876 Compress::Raw::Zlib::deflateStream s
878 RETVAL = deflateReset(&(s->stream)) ;
879 if (RETVAL == Z_OK) {
880 PostInitStream(s, s->flags, s->bufsize, s->WindowBits) ;
886 deflate (s, buf, output)
887 Compress::Raw::Zlib::deflateStream s
890 uInt cur_length = NO_INIT
891 uInt increment = NO_INIT
892 uInt prefix = NO_INIT
894 uLong bufinc = NO_INIT
898 /* If the input buffer is a reference, dereference it */
899 buf = deRef(buf, "deflate") ;
901 /* initialise the input buffer */
902 #ifdef UTF8_AVAILABLE
903 if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
904 croak("Wide character in Compress::Raw::Zlib::Deflate::deflate input parameter");
906 s->stream.next_in = (Bytef*)SvPVbyte_nolen(buf) ;
907 s->stream.avail_in = SvCUR(buf) ;
909 if (s->flags & FLAG_CRC32)
910 s->crc32 = crc32(s->crc32, s->stream.next_in, s->stream.avail_in) ;
912 if (s->flags & FLAG_ADLER32)
913 s->adler32 = adler32(s->adler32, s->stream.next_in, s->stream.avail_in) ;
915 /* and retrieve the output buffer */
916 output = deRef_l(output, "deflate") ;
917 #ifdef UTF8_AVAILABLE
918 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
919 croak("Wide character in Compress::Raw::Zlib::Deflate::deflate output parameter");
922 if((s->flags & FLAG_APPEND) != FLAG_APPEND) {
923 SvCUR_set(output, 0);
924 /* sv_setpvn(output, "", 0); */
926 prefix = cur_length = SvCUR(output) ;
927 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length;
928 increment = SvLEN(output) - cur_length;
929 s->stream.avail_out = increment;
931 /* Check for saved output from deflateParams */
932 if (s->deflateParams_out_valid) {
933 *(s->stream.next_out) = s->deflateParams_out_byte;
934 ++ s->stream.next_out;
935 -- s->stream.avail_out ;
936 s->deflateParams_out_valid = FALSE;
939 /* Check for saved output from deflateParams */
940 if (s->deflateParams_out_length) {
941 uLong plen = s->deflateParams_out_length ;
942 /* printf("Copy %d bytes saved data\n", plen);*/
943 if (s->stream.avail_out < plen) {
944 /*printf("GROW from %d to %d\n", s->stream.avail_out,
945 SvLEN(output) + plen - s->stream.avail_out); */
946 Sv_Grow(output, SvLEN(output) + plen - s->stream.avail_out) ;
949 Copy(s->stream.next_out, s->deflateParams_out_buffer, plen, Bytef) ;
950 cur_length = cur_length + plen;
951 SvCUR_set(output, cur_length);
952 s->stream.next_out += plen ;
953 s->stream.avail_out = SvLEN(output) - cur_length ;
954 increment = s->stream.avail_out;
955 s->deflateParams_out_length = 0;
958 while (s->stream.avail_in != 0) {
960 if (s->stream.avail_out == 0) {
961 /* out of space in the output buffer so make it bigger */
962 Sv_Grow(output, SvLEN(output) + bufinc) ;
963 cur_length += increment ;
964 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length ;
966 s->stream.avail_out = increment;
970 RETVAL = deflate(&(s->stream), Z_NO_FLUSH);
975 s->compressedBytes += cur_length + increment - prefix - s->stream.avail_out ;
976 s->uncompressedBytes += SvCUR(buf) - s->stream.avail_in ;
978 s->last_error = RETVAL ;
979 if (RETVAL == Z_OK) {
981 SvCUR_set(output, cur_length + increment - s->stream.avail_out) ;
990 Compress::Raw::Zlib::deflateStream s
992 deflateEnd(&s->stream) ;
994 SvREFCNT_dec(s->dictionary) ;
996 if (s->deflateParams_out_buffer)
997 Safefree(s->deflateParams_out_buffer);
1003 flush(s, output, f=Z_FINISH)
1004 Compress::Raw::Zlib::deflateStream s
1007 uInt cur_length = NO_INIT
1008 uInt increment = NO_INIT
1009 uInt prefix = NO_INIT
1010 uLong bufinc = NO_INIT
1012 bufinc = s->bufsize;
1014 s->stream.avail_in = 0; /* should be zero already anyway */
1016 /* retrieve the output buffer */
1017 output = deRef_l(output, "flush") ;
1018 #ifdef UTF8_AVAILABLE
1019 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
1020 croak("Wide character in Compress::Raw::Zlib::Deflate::flush input parameter");
1022 if(! s->flags & FLAG_APPEND) {
1023 SvCUR_set(output, 0);
1024 /* sv_setpvn(output, "", 0); */
1026 prefix = cur_length = SvCUR(output) ;
1027 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length;
1028 increment = SvLEN(output) - cur_length;
1029 s->stream.avail_out = increment;
1031 /* Check for saved output from deflateParams */
1032 if (s->deflateParams_out_valid) {
1033 *(s->stream.next_out) = s->deflateParams_out_byte;
1034 ++ s->stream.next_out;
1035 -- s->stream.avail_out ;
1036 s->deflateParams_out_valid = FALSE;
1039 /* Check for saved output from deflateParams */
1040 if (s->deflateParams_out_length) {
1041 uLong plen = s->deflateParams_out_length ;
1042 /* printf("Copy %d bytes saved data\n", plen); */
1043 if (s->stream.avail_out < plen) {
1044 /* printf("GROW from %d to %d\n", s->stream.avail_out,
1045 SvLEN(output) + plen - s->stream.avail_out); */
1046 Sv_Grow(output, SvLEN(output) + plen - s->stream.avail_out) ;
1049 Copy(s->stream.next_out, s->deflateParams_out_buffer, plen, Bytef) ;
1050 cur_length = cur_length + plen;
1051 SvCUR_set(output, cur_length);
1052 s->stream.next_out += plen ;
1053 s->stream.avail_out = SvLEN(output) - cur_length ;
1054 increment = s->stream.avail_out;
1055 s->deflateParams_out_length = 0;
1060 if (s->stream.avail_out == 0) {
1061 /* consumed all the available output, so extend it */
1062 Sv_Grow(output, SvLEN(output) + bufinc) ;
1063 cur_length += increment ;
1064 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length ;
1065 increment = bufinc ;
1066 s->stream.avail_out = increment;
1069 RETVAL = deflate(&(s->stream), f);
1071 /* deflate has finished flushing only when it hasn't used up
1072 * all the available space in the output buffer:
1074 if (s->stream.avail_out != 0 || RETVAL != Z_OK )
1078 RETVAL = (RETVAL == Z_STREAM_END ? Z_OK : RETVAL) ;
1079 s->last_error = RETVAL ;
1081 s->compressedBytes += cur_length + increment - prefix - s->stream.avail_out ;
1083 if (RETVAL == Z_OK) {
1085 SvCUR_set(output, cur_length + increment - s->stream.avail_out) ;
1093 _deflateParams(s, flags, level, strategy, bufsize)
1094 Compress::Raw::Zlib::deflateStream s
1100 /* printf("_deflateParams(Flags %d Level %d Strategy %d Bufsize %d)\n", flags, level, strategy, bufsize);
1101 printf("Before -- Level %d, Strategy %d, Bufsize %d\n", s->Level, s->Strategy, s->bufsize); */
1105 s->Strategy = strategy ;
1107 s->bufsize = bufsize;
1109 /* printf("After -- Level %d, Strategy %d, Bufsize %d\n", s->Level, s->Strategy, s->bufsize);*/
1111 s->stream.avail_in = 0;
1112 s->stream.next_out = &(s->deflateParams_out_byte) ;
1113 s->stream.avail_out = 1;
1114 RETVAL = deflateParams(&(s->stream), s->Level, s->Strategy);
1115 s->deflateParams_out_valid =
1116 (RETVAL == Z_OK && s->stream.avail_out == 0) ;
1117 /* printf("RETVAL %d, avail out %d, byte %c\n", RETVAL, s->stream.avail_out, s->deflateParams_out_byte); */
1119 /* printf("Level %d Strategy %d, Prev Len %d\n",
1120 s->Level, s->Strategy, s->deflateParams_out_length); */
1121 s->stream.avail_in = 0;
1122 if (s->deflateParams_out_buffer == NULL)
1123 s->deflateParams_out_buffer = safemalloc(deflateParams_BUFFER_SIZE);
1124 s->stream.next_out = s->deflateParams_out_buffer ;
1125 s->stream.avail_out = deflateParams_BUFFER_SIZE;
1127 RETVAL = deflateParams(&(s->stream), s->Level, s->Strategy);
1128 s->deflateParams_out_length = deflateParams_BUFFER_SIZE - s->stream.avail_out;
1129 /* printf("RETVAL %d, length out %d, avail %d\n",
1130 RETVAL, s->deflateParams_out_length, s->stream.avail_out ); */
1138 Compress::Raw::Zlib::deflateStream s
1146 Compress::Raw::Zlib::deflateStream s
1148 RETVAL = s->Strategy ;
1155 Compress::Raw::Zlib::deflateStream s
1157 RETVAL = s->bufsize ;
1164 Compress::Raw::Zlib::deflateStream s
1166 RETVAL = s->last_error ;
1172 Compress::Raw::Zlib::deflateStream s
1180 Compress::Raw::Zlib::deflateStream s
1182 RETVAL = s->dict_adler ;
1188 Compress::Raw::Zlib::deflateStream s
1190 RETVAL = s->adler32 ;
1196 Compress::Raw::Zlib::deflateStream s
1198 RETVAL = s->compressedBytes;
1203 uncompressedBytes(s)
1204 Compress::Raw::Zlib::deflateStream s
1206 RETVAL = s->uncompressedBytes;
1212 Compress::Raw::Zlib::deflateStream s
1214 RETVAL = s->stream.total_in ;
1220 Compress::Raw::Zlib::deflateStream s
1222 RETVAL = s->stream.total_out ;
1228 Compress::Raw::Zlib::deflateStream s
1230 RETVAL = s->stream.msg;
1235 deflateTune(s, good_length, max_lazy, nice_length, max_chain)
1236 Compress::Raw::Zlib::deflateStream s
1242 #ifndef AT_LEAST_ZLIB_1_2_2_3
1243 good_length = good_length; max_lazy = max_lazy ; /* Silence -Wall */
1244 nice_length = nice_length; max_chain = max_chain; /* Silence -Wall */
1245 croak("deflateTune needs zlib 1.2.2.3 or better");
1247 RETVAL = deflateTune(&(s->stream), good_length, max_lazy, nice_length, max_chain);
1253 MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib::inflateStream
1256 DispStream(s, message=NULL)
1257 Compress::Raw::Zlib::inflateStream s
1262 Compress::Raw::Zlib::inflateStream s
1264 RETVAL = inflateReset(&(s->stream)) ;
1265 if (RETVAL == Z_OK) {
1266 PostInitStream(s, s->flags, s->bufsize, s->WindowBits) ;
1272 inflate (s, buf, output, eof=FALSE)
1273 Compress::Raw::Zlib::inflateStream s
1277 uInt cur_length = 0;
1278 uInt prefix_length = 0;
1280 STRLEN stmp = NO_INIT
1281 uLong bufinc = NO_INIT
1283 #ifdef UTF8_AVAILABLE
1284 bool out_utf8 = FALSE;
1287 bufinc = s->bufsize;
1288 /* If the buffer is a reference, dereference it */
1289 buf = deRef(buf, "inflate") ;
1291 if (s->flags & FLAG_CONSUME_INPUT && SvREADONLY(buf))
1292 croak("Compress::Raw::Zlib::Inflate::inflate input parameter cannot be read-only when ConsumeInput is specified");
1293 #ifdef UTF8_AVAILABLE
1294 if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
1295 croak("Wide character in Compress::Raw::Zlib::Inflate::inflate input parameter");
1298 /* initialise the input buffer */
1299 s->stream.next_in = (Bytef*)SvPVbyte_force(buf, stmp) ;
1300 s->stream.avail_in = SvCUR(buf) ;
1302 /* and retrieve the output buffer */
1303 output = deRef_l(output, "inflate") ;
1304 #ifdef UTF8_AVAILABLE
1305 if (DO_UTF8(output))
1307 if (DO_UTF8(output) && !sv_utf8_downgrade(output, 1))
1308 croak("Wide character in Compress::Raw::Zlib::Inflate::inflate output parameter");
1310 if((s->flags & FLAG_APPEND) != FLAG_APPEND) {
1311 SvCUR_set(output, 0);
1314 /* Assume no output buffer - the code below will update if there is any available */
1315 s->stream.avail_out = 0;
1318 if (SvLEN(output)) {
1319 prefix_length = cur_length = SvCUR(output) ;
1321 if (s->flags & FLAG_LIMIT_OUTPUT && SvLEN(output) - cur_length - 1 < bufinc)
1323 Sv_Grow(output, bufinc + cur_length + 1) ;
1326 /* Only setup the stream output pointers if there is spare
1327 capacity in the outout SV
1329 if (SvLEN(output) > cur_length + 1)
1331 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length;
1332 increment = SvLEN(output) - cur_length - 1;
1333 s->stream.avail_out = increment;
1338 s->bytesInflated = 0;
1342 while (RETVAL == Z_OK) {
1343 if (s->stream.avail_out == 0) {
1344 /* out of space in the output buffer so make it bigger */
1345 Sv_Grow(output, SvLEN(output) + bufinc +1) ;
1346 cur_length += increment ;
1347 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length ;
1348 increment = bufinc ;
1349 s->stream.avail_out = increment;
1353 /* printf("INFLATE Availl In %d, Out %d\n", s->stream.avail_in,
1354 s->stream.avail_out);
1355 DispStream(s, "BEFORE");
1356 Perl_sv_dump(output); */
1357 RETVAL = inflate(&(s->stream), Z_SYNC_FLUSH);
1358 /* printf("INFLATE returned %d %s, avail in %d, out %d\n", RETVAL,
1359 GetErrorString(RETVAL), s->stream.avail_in, s->stream.avail_out); */
1362 if (RETVAL == Z_NEED_DICT && s->dictionary) {
1363 s->dict_adler = s->stream.adler ;
1364 RETVAL = inflateSetDictionary(&(s->stream),
1365 (const Bytef*)SvPVbyte_nolen(s->dictionary),
1366 SvCUR(s->dictionary));
1371 if (s->flags & FLAG_LIMIT_OUTPUT &&
1372 (RETVAL == Z_OK || RETVAL == Z_BUF_ERROR ))
1375 if (RETVAL == Z_STREAM_ERROR || RETVAL == Z_MEM_ERROR ||
1376 RETVAL == Z_DATA_ERROR || RETVAL == Z_STREAM_END )
1379 if (RETVAL == Z_BUF_ERROR) {
1380 if (s->stream.avail_out == 0)
1382 if (s->stream.avail_in == 0) {
1388 #ifdef NEED_DUMMY_BYTE_AT_END
1389 if (eof && RETVAL == Z_OK && s->flags & FLAG_LIMIT_OUTPUT == 0) {
1390 Bytef* nextIn = s->stream.next_in;
1391 uInt availIn = s->stream.avail_in;
1392 s->stream.next_in = (Bytef*) " ";
1393 s->stream.avail_in = 1;
1394 if (s->stream.avail_out == 0) {
1395 /* out of space in the output buffer so make it bigger */
1396 Sv_Grow(output, SvLEN(output) + bufinc) ;
1397 cur_length += increment ;
1398 s->stream.next_out = (Bytef*) SvPVbyte_nolen(output) + cur_length ;
1399 increment = bufinc ;
1400 s->stream.avail_out = increment;
1403 RETVAL = inflate(&(s->stream), Z_SYNC_FLUSH);
1404 s->stream.next_in = nextIn ;
1405 s->stream.avail_in = availIn ;
1409 s->last_error = RETVAL ;
1410 if (RETVAL == Z_OK || RETVAL == Z_STREAM_END || RETVAL == Z_BUF_ERROR || RETVAL == Z_DATA_ERROR) {
1413 s->bytesInflated = cur_length + increment - s->stream.avail_out - prefix_length;
1414 s->uncompressedBytes += s->bytesInflated ;
1415 s->compressedBytes += SvCUR(buf) - s->stream.avail_in ;
1418 SvCUR_set(output, prefix_length + s->bytesInflated) ;
1419 *SvEND(output) = '\0';
1420 #ifdef UTF8_AVAILABLE
1422 sv_utf8_upgrade(output);
1426 if (s->flags & FLAG_CRC32 )
1427 s->crc32 = crc32(s->crc32,
1428 (const Bytef*)SvPVbyte_nolen(output)+prefix_length,
1429 SvCUR(output)-prefix_length) ;
1431 if (s->flags & FLAG_ADLER32)
1432 s->adler32 = adler32(s->adler32,
1433 (const Bytef*)SvPVbyte_nolen(output)+prefix_length,
1434 SvCUR(output)-prefix_length) ;
1436 /* fix the input buffer */
1437 if (s->flags & FLAG_CONSUME_INPUT || s->flags & FLAG_LIMIT_OUTPUT) {
1438 in = s->stream.avail_in ;
1439 SvCUR_set(buf, in) ;
1441 Move(s->stream.next_in, SvPVbyte_nolen(buf), in, char) ;
1452 Compress::Raw::Zlib::inflateStream s
1454 RETVAL = s->bytesInflated;
1460 Compress::Raw::Zlib::inflateStream s
1462 RETVAL = s->compressedBytes;
1467 uncompressedBytes(s)
1468 Compress::Raw::Zlib::inflateStream s
1470 RETVAL = s->uncompressedBytes;
1476 inflateSync (s, buf)
1477 Compress::Raw::Zlib::inflateStream s
1481 /* If the buffer is a reference, dereference it */
1482 buf = deRef(buf, "inflateSync") ;
1483 #ifdef UTF8_AVAILABLE
1484 if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
1485 croak("Wide character in Compress::Raw::Zlib::Inflate::inflateSync");
1488 /* initialise the input buffer */
1489 s->stream.next_in = (Bytef*)SvPVbyte_nolen(buf) ;
1490 s->stream.avail_in = SvCUR(buf) ;
1492 /* inflateSync doesn't create any output */
1493 s->stream.next_out = (Bytef*) NULL;
1494 s->stream.avail_out = 0;
1496 RETVAL = inflateSync(&(s->stream));
1497 s->last_error = RETVAL ;
1499 /* fix the input buffer */
1501 unsigned in = s->stream.avail_in ;
1502 SvCUR_set(buf, in) ;
1504 Move(s->stream.next_in, SvPVbyte_nolen(buf), in, char) ;
1513 Compress::Raw::Zlib::inflateStream s
1515 inflateEnd(&s->stream) ;
1517 SvREFCNT_dec(s->dictionary) ;
1519 if (s->deflateParams_out_buffer)
1520 Safefree(s->deflateParams_out_buffer);
1524 Safefree(s->window);
1531 Compress::Raw::Zlib::inflateStream s
1533 RETVAL = s->last_error ;
1539 Compress::Raw::Zlib::inflateStream s
1547 Compress::Raw::Zlib::inflateStream s
1549 RETVAL = s->dict_adler ;
1555 Compress::Raw::Zlib::inflateStream s
1557 RETVAL = s->stream.total_in ;
1563 Compress::Raw::Zlib::inflateStream s
1565 RETVAL = s->adler32 ;
1571 Compress::Raw::Zlib::inflateStream s
1573 RETVAL = s->stream.total_out ;
1579 Compress::Raw::Zlib::inflateStream s
1581 RETVAL = s->stream.msg;
1588 Compress::Raw::Zlib::inflateStream s
1590 RETVAL = s->bufsize ;
1596 Compress::Raw::Zlib::inflateStream s
1599 RETVAL = ((s->flags & FLAG_APPEND) == FLAG_APPEND);
1601 s->flags |= FLAG_APPEND ;
1603 s->flags &= ~FLAG_APPEND ;
1607 MODULE = Compress::Raw::Zlib PACKAGE = Compress::Raw::Zlib::inflateScanStream
1611 Compress::Raw::Zlib::inflateScanStream s
1613 inflateEnd(&s->stream) ;
1615 SvREFCNT_dec(s->dictionary) ;
1617 if (s->deflateParams_out_buffer)
1618 Safefree(s->deflateParams_out_buffer);
1622 Safefree(s->window);
1627 DispStream(s, message=NULL)
1628 Compress::Raw::Zlib::inflateScanStream s
1633 Compress::Raw::Zlib::inflateScanStream s
1635 RETVAL = inflateReset(&(s->stream)) ;
1636 if (RETVAL == Z_OK) {
1637 PostInitStream(s, s->flags, s->bufsize, s->WindowBits) ;
1643 scan(s, buf, out=NULL, eof=FALSE)
1644 Compress::Raw::Zlib::inflateScanStream s
1648 bool eof_mode = FALSE;
1649 int start_len = NO_INIT
1650 STRLEN stmp = NO_INIT
1652 /* If the input buffer is a reference, dereference it */
1653 #ifndef MAGIC_APPEND
1655 croak("scan needs zlib 1.2.1 or better");
1657 buf = deRef(buf, "inflateScan") ;
1658 #ifdef UTF8_AVAILABLE
1659 if (DO_UTF8(buf) && !sv_utf8_downgrade(buf, 1))
1660 croak("Wide character in Compress::Raw::Zlib::InflateScan::scan input parameter");
1662 /* initialise the input buffer */
1663 s->stream.next_in = (Bytef*)SvPVbyte_force(buf, stmp) ;
1664 s->stream.avail_in = SvCUR(buf) ;
1665 start_len = s->stream.avail_in ;
1666 s->bytesInflated = 0 ;
1669 if (s->stream.avail_in == 0) {
1674 /* set up output to next available section of sliding window */
1675 s->stream.avail_out = WINDOW_SIZE - s->window_have;
1676 s->stream.next_out = s->window + s->window_have;
1678 /* DispStream(s, "before inflate\n"); */
1680 /* inflate and check for errors */
1681 RETVAL = inflate(&(s->stream), Z_BLOCK);
1683 if (start_len > 1 && ! eof_mode)
1684 s->window_lastByte = *(s->stream.next_in - 1 ) ;
1686 if (RETVAL == Z_STREAM_ERROR || RETVAL == Z_MEM_ERROR ||
1687 RETVAL == Z_DATA_ERROR )
1690 if (s->flags & FLAG_CRC32 )
1691 s->crc32 = crc32(s->crc32, s->window + s->window_have,
1692 WINDOW_SIZE - s->window_have - s->stream.avail_out);
1694 if (s->flags & FLAG_ADLER32)
1695 s->adler32 = adler32(s->adler32, s->window + s->window_have,
1696 WINDOW_SIZE - s->window_have - s->stream.avail_out);
1698 s->uncompressedBytes =
1699 s->bytesInflated += WINDOW_SIZE - s->window_have - s->stream.avail_out;
1701 if (s->stream.avail_out)
1702 s->window_have = WINDOW_SIZE - s->stream.avail_out;
1708 /* process end of block */
1709 if (s->stream.data_type & 128) {
1710 if (s->stream.data_type & 64) {
1711 s->window_left = s->stream.data_type & 0x1f;
1714 s->window_lastbit = s->stream.data_type & 0x1f;
1715 s->lastBlockOffset = s->stream.total_in;
1719 } while (RETVAL != Z_STREAM_END);
1721 s->last_error = RETVAL ;
1722 s->window_lastoff = s->stream.total_in ;
1723 s->compressedBytes += SvCUR(buf) - s->stream.avail_in ;
1725 if (RETVAL == Z_STREAM_END)
1727 s->matchedEndBlock = 1 ;
1729 /* save the location of the end of the compressed data */
1730 s->window_end = SvCUR(buf) - s->stream.avail_in - 1 ;
1731 s->window_endOffset = s->stream.total_in ;
1734 -- s->window_endOffset ;
1737 /* if window wrapped, build dictionary from window by rotating */
1738 if (s->window_full) {
1739 rotate(s->window, WINDOW_SIZE, s->window_have);
1740 s->window_have = WINDOW_SIZE;
1743 /* if (s->flags & FLAG_CONSUME_INPUT) { */
1745 unsigned in = s->stream.avail_in ;
1746 SvCUR_set(buf, in) ;
1748 Move(s->stream.next_in, SvPVbyte_nolen(buf), in, char) ;
1760 Compress::Raw::Zlib::inflateScanStream s
1762 #ifndef MAGIC_APPEND
1763 croak("getEndOffset needs zlib 1.2.1 or better");
1765 RETVAL = s->window_endOffset;
1772 Compress::Raw::Zlib::inflateScanStream s
1774 #ifndef MAGIC_APPEND
1775 croak("inflateCount needs zlib 1.2.1 or better");
1777 RETVAL = s->bytesInflated;
1784 Compress::Raw::Zlib::inflateScanStream s
1786 RETVAL = s->compressedBytes;
1791 uncompressedBytes(s)
1792 Compress::Raw::Zlib::inflateScanStream s
1794 RETVAL = s->uncompressedBytes;
1800 getLastBlockOffset(s)
1801 Compress::Raw::Zlib::inflateScanStream s
1803 #ifndef MAGIC_APPEND
1804 croak("getLastBlockOffset needs zlib 1.2.1 or better");
1806 RETVAL = s->lastBlockOffset - (s->window_lastbit != 0);
1812 getLastBufferOffset(s)
1813 Compress::Raw::Zlib::inflateScanStream s
1815 #ifndef MAGIC_APPEND
1816 croak("getLastBufferOffset needs zlib 1.2.1 or better");
1818 RETVAL = s->window_lastoff;
1824 resetLastBlockByte(s, byte)
1825 Compress::Raw::Zlib::inflateScanStream s
1828 #ifndef MAGIC_APPEND
1829 croak("resetLastBlockByte needs zlib 1.2.1 or better");
1832 *byte = *byte ^ (1 << ((8 - s->window_lastbit) & 7));
1837 _createDeflateStream(inf_s, flags,level, method, windowBits, memLevel, strategy, bufsize)
1838 Compress::Raw::Zlib::inflateScanStream inf_s
1848 #ifndef MAGIC_APPEND
1852 windowBits = windowBits;
1853 memLevel = memLevel;
1854 strategy = strategy;
1856 croak("_createDeflateStream needs zlib 1.2.1 or better");
1862 warn("in _createDeflateStream(level=%d, method=%d, windowBits=%d, memLevel=%d, strategy=%d, bufsize=%lu\n",
1863 level, method, windowBits, memLevel, strategy, bufsize) ;
1864 if ((s = InitStream() )) {
1868 s->WindowBits = windowBits;
1869 s->MemLevel = memLevel;
1870 s->Strategy = strategy;
1872 err = deflateInit2(&(s->stream), level,
1873 method, windowBits, memLevel, strategy);
1876 err = deflateSetDictionary(&(s->stream), inf_s->window, inf_s->window_have);
1877 s->dict_adler = s->stream.adler ;
1885 PostInitStream(s, flags, bufsize, windowBits) ;
1886 s->crc32 = inf_s->crc32;
1887 s->adler32 = inf_s->adler32;
1888 s->stream.adler = inf_s->stream.adler ;
1889 /* s->stream.total_out = inf_s->bytesInflated ; */
1890 s->stream.total_in = inf_s->stream.total_out ;
1891 if (inf_s->window_left) {
1892 /* printf("** window_left %d, window_lastByte %d\n", inf_s->window_left, inf_s->window_lastByte); */
1893 deflatePrime(&(s->stream), 8 - inf_s->window_left, inf_s->window_lastByte);
1900 XPUSHs(sv_setref_pv(sv_newmortal(),
1901 "Compress::Raw::Zlib::deflateStream", (void*)s));
1902 if (GIMME == G_ARRAY) {
1903 SV * sv = sv_2mortal(newSViv(err)) ;
1904 setDUALstatus(sv, err);
1912 Compress::Raw::Zlib::inflateScanStream s
1914 RETVAL = s->last_error ;
1920 Compress::Raw::Zlib::inflateScanStream s
1929 Compress::Raw::Zlib::inflateScanStream s
1931 RETVAL = s->adler32 ;