7474# define AT_LEAST_ZLIB_1_2_8
7575#endif
7676
77+ #if defined(ZLIB_VERNUM ) && ZLIB_VERNUM >= 0x1290
78+ # define AT_LEAST_ZLIB_1_2_9
79+ #endif
80+
7781#ifdef USE_PPPORT_H
7882# define NEED_sv_2pvbyte
7983# define NEED_sv_2pv_nolen
@@ -134,12 +138,13 @@ typedef struct di_stream {
134138 uLong dict_adler ;
135139 int last_error ;
136140 bool zip_mode ;
137- #define SETP_BYTE
141+ /* #define SETP_BYTE */
138142#ifdef SETP_BYTE
143+ /* SETP_BYTE only works with zlib up to 1.2.8 */
139144 bool deflateParams_out_valid ;
140145 Bytef deflateParams_out_byte ;
141146#else
142- #define deflateParams_BUFFER_SIZE 0x4000
147+ #define deflateParams_BUFFER_SIZE 0x40000
143148 uLong deflateParams_out_length ;
144149 Bytef * deflateParams_out_buffer ;
145150#endif
@@ -636,6 +641,103 @@ char * string ;
636641 return sv ;
637642}
638643
644+ #if 0
645+ int
646+ flushToBuffer (di_stream * s , int flush )
647+ {
648+ dTHX ;
649+ int ret ;
650+ z_stream * strm = & s -> stream ;
651+
652+ Bytef * output = s -> deflateParams_out_buffer ;
653+
654+ strm -> next_in = NULL ;
655+ strm -> avail_in = 0 ;
656+
657+ uLong total_output = 0 ;
658+ uLong have = 0 ;
659+
660+ do
661+ {
662+ if (output )
663+ output = (unsigned char * )saferealloc (output , total_output + s -> bufsize );
664+ else
665+ output = (unsigned char * )safemalloc (s -> bufsize );
666+
667+ strm -> next_out = output + total_output ;
668+ strm -> avail_out = s -> bufsize ;
669+
670+ ret = deflate (strm , flush ); /* no bad return value */
671+ //assert(ret != Z_STREAM_ERROR); /* state not clobbered */
672+ if (ret == Z_STREAM_ERROR )
673+ {
674+ safefree (output );
675+ return ret ;
676+ }
677+ have = s -> bufsize - strm -> avail_out ;
678+ total_output += have ;
679+
680+ //fprintf(stderr, "FLUSH %s %d, return %d\n", flush_flags[flush], have, ret);
681+
682+ } while (strm -> avail_out == 0 );
683+
684+ s -> deflateParams_out_buffer = output ;
685+ s -> deflateParams_out_length = total_output ;
686+
687+ return Z_OK ;
688+ }
689+ #endif
690+
691+ #ifndef SETP_BYTE
692+ int
693+ flushParams (di_stream * s )
694+ {
695+ dTHX ;
696+ int ret ;
697+ z_stream * strm = & s -> stream ;
698+
699+ Bytef * output = s -> deflateParams_out_buffer ;
700+ uLong total_output = s -> deflateParams_out_length ;
701+
702+ uLong have = 0 ;
703+
704+ strm -> next_in = NULL ;
705+ strm -> avail_in = 0 ;
706+
707+ do
708+ {
709+ if (output )
710+ output = (unsigned char * )saferealloc (output , total_output + s -> bufsize );
711+ else
712+ output = (unsigned char * )safemalloc (s -> bufsize );
713+
714+ strm -> next_out = output + total_output ;
715+ strm -> avail_out = s -> bufsize ;
716+
717+ ret = deflateParams (& (s -> stream ), s -> Level , s -> Strategy );
718+ /* fprintf(stderr, "deflateParams %d %s %lu\n", ret,
719+ GetErrorString(ret), s->bufsize - strm->avail_out); */
720+
721+ if (ret == Z_STREAM_ERROR )
722+ break ;
723+
724+ have = s -> bufsize - strm -> avail_out ;
725+ total_output += have ;
726+
727+
728+ } while (ret == Z_BUF_ERROR ) ;
729+
730+ if (ret == Z_STREAM_ERROR )
731+ safefree (output );
732+ else
733+ {
734+ s -> deflateParams_out_buffer = output ;
735+ s -> deflateParams_out_length = total_output ;
736+ }
737+
738+ return ret ;
739+ }
740+ #endif /* ! SETP_BYTE */
639741
640742#include "constants.h"
641743
@@ -991,20 +1093,24 @@ deflate (s, buf, output)
9911093 /* Check for saved output from deflateParams */
9921094 if (s -> deflateParams_out_length ) {
9931095 uLong plen = s -> deflateParams_out_length ;
994- /* printf("Copy %d bytes saved data\n", plen);*/
1096+ /* printf("Copy %lu bytes saved data\n", plen); */
9951097 if (s -> stream .avail_out < plen ) {
996- /*printf("GROW from %d to %d\n", s->stream.avail_out,
997- SvLEN(output) + plen - s->stream.avail_out); */
998- Sv_Grow (output , SvLEN (output ) + plen - s -> stream .avail_out ) ;
1098+ /* printf("GROW from %d to %lu\n", s->stream.avail_out,
1099+ SvLEN(output) + plen - s->stream.avail_out); */
1100+ s -> stream .next_out = (Bytef * ) Sv_Grow (output , SvLEN (output ) + plen - s -> stream .avail_out ) ;
1101+ s -> stream .next_out += cur_length ;
9991102 }
10001103
1001- Copy (s -> stream . next_out , s -> deflateParams_out_buffer , plen , Bytef ) ;
1002- cur_length = cur_length + plen ;
1104+ Copy (s -> deflateParams_out_buffer , s -> stream . next_out , plen , Bytef ) ;
1105+ cur_length += plen ;
10031106 SvCUR_set (output , cur_length );
1004- s -> stream .next_out += plen ;
1005- s -> stream .avail_out = SvLEN (output ) - cur_length ;
1006- increment = s -> stream .avail_out ;
1007- s -> deflateParams_out_length = 0 ;
1107+ s -> stream .next_out += plen ;
1108+ s -> stream .avail_out = SvLEN (output ) - cur_length ;
1109+ increment = s -> stream .avail_out ;
1110+
1111+ s -> deflateParams_out_length = 0 ;
1112+ Safefree (s -> deflateParams_out_buffer );
1113+ s -> deflateParams_out_buffer = NULL ;
10081114 }
10091115#endif
10101116 RETVAL = Z_OK ;
@@ -1027,6 +1133,12 @@ deflate (s, buf, output)
10271133 }
10281134
10291135 RETVAL = deflate (& (s -> stream ), Z_NO_FLUSH );
1136+ /* if (RETVAL != Z_STREAM_ERROR) {
1137+ int done = increment - s->stream.avail_out ;
1138+ printf("std DEFLATEr returned %d '%s' avail in %d, out %d wrote %d\n", RETVAL,
1139+ GetErrorString(RETVAL), s->stream.avail_in,
1140+ s->stream.avail_out, done);
1141+ } */
10301142
10311143 if (trace ) {
10321144 printf ("DEFLATE returned %d %s, avail in %d, out %d\n" , RETVAL ,
@@ -1080,7 +1192,6 @@ flush(s, output, f=Z_FINISH)
10801192 CODE :
10811193 bufinc = s -> bufsize ;
10821194
1083- s -> stream .avail_in = 0 ; /* should be zero already anyway */
10841195
10851196 /* retrieve the output buffer */
10861197 output = deRef_l (output , "flush" ) ;
@@ -1108,20 +1219,24 @@ flush(s, output, f=Z_FINISH)
11081219 /* Check for saved output from deflateParams */
11091220 if (s -> deflateParams_out_length ) {
11101221 uLong plen = s -> deflateParams_out_length ;
1111- /* printf("Copy %d bytes saved data\n", plen); */
1222+ /* printf("Copy %lu bytes saved data\n", plen); */
11121223 if (s -> stream .avail_out < plen ) {
1113- /* printf("GROW from %d to %d \n", s->stream.avail_out,
1224+ /* printf("GROW from %d to %lu \n", s->stream.avail_out,
11141225 SvLEN(output) + plen - s->stream.avail_out); */
1115- Sv_Grow (output , SvLEN (output ) + plen - s -> stream .avail_out ) ;
1226+ s -> stream .next_out = (Bytef * ) Sv_Grow (output , SvLEN (output ) + plen - s -> stream .avail_out ) ;
1227+ s -> stream .next_out += cur_length ;
11161228 }
11171229
1118- Copy (s -> stream . next_out , s -> deflateParams_out_buffer , plen , Bytef ) ;
1119- cur_length = cur_length + plen ;
1230+ Copy (s -> deflateParams_out_buffer , s -> stream . next_out , plen , Bytef ) ;
1231+ cur_length += plen ;
11201232 SvCUR_set (output , cur_length );
1121- s -> stream .next_out += plen ;
1122- s -> stream .avail_out = SvLEN (output ) - cur_length ;
1123- increment = s -> stream .avail_out ;
1124- s -> deflateParams_out_length = 0 ;
1233+ s -> stream .next_out += plen ;
1234+ s -> stream .avail_out = SvLEN (output ) - cur_length ;
1235+ increment = s -> stream .avail_out ;
1236+
1237+ s -> deflateParams_out_length = 0 ;
1238+ Safefree (s -> deflateParams_out_buffer );
1239+ s -> deflateParams_out_buffer = NULL ;
11251240 }
11261241#endif
11271242
@@ -1145,9 +1260,15 @@ flush(s, output, f=Z_FINISH)
11451260 }
11461261
11471262 RETVAL = deflate (& (s -> stream ), f );
1263+ /* if (RETVAL != Z_STREAM_ERROR) {
1264+ int done = availableout - s->stream.avail_out ;
1265+ printf("flush DEFLATEr returned %d '%s' avail in %d, out %d wrote %d\n", RETVAL,
1266+ GetErrorString(RETVAL), s->stream.avail_in,
1267+ s->stream.avail_out, done);
1268+ } */
11481269
11491270 if (trace ) {
1150- printf ("flush DEFLATE returned %d %s , avail in %d, out %d\n" , RETVAL ,
1271+ printf ("flush DEFLATE returned %d '%s' , avail in %d, out %d\n" , RETVAL ,
11511272 GetErrorString (RETVAL ), s -> stream .avail_in , s -> stream .avail_out );
11521273 DispStream (s , "AFTER" );
11531274 }
@@ -1184,41 +1305,38 @@ _deflateParams(s, flags, level, strategy, bufsize)
11841305 int level
11851306 int strategy
11861307 uLong bufsize
1308+ bool changed = FALSE;
11871309 CODE :
1188- /* printf("_deflateParams(Flags %d Level %d Strategy %d Bufsize %d)\n", flags, level, strategy, bufsize);
1189- printf("Before -- Level %d, Strategy %d, Bufsize %d\n", s->Level, s->Strategy, s->bufsize); */
1190- if (flags & 1 )
1191- s -> Level = level ;
1192- if (flags & 2 )
1193- s -> Strategy = strategy ;
1194- if (flags & 4 ) {
1310+ /* printf("_deflateParams(Flags %d Level %d Strategy %d Bufsize %d)\n", flags, level, strategy, bufsize);
1311+ printf("Before -- Level %d, Strategy %d, Bufsize %d\n", s->Level, s->Strategy, s->bufsize); */
1312+ if (flags & 1 && level != s -> Level ) {
1313+ s -> Level = level ;
1314+ changed = TRUE;
1315+ }
1316+ if (flags & 2 && strategy != s -> Strategy ) {
1317+ s -> Strategy = strategy ;
1318+ changed = TRUE;
1319+ }
1320+ if (flags & 4 )
11951321 s -> bufsize = bufsize ;
1196- }
1197- /* printf("After -- Level %d, Strategy %d, Bufsize %d\n", s->Level, s->Strategy, s->bufsize);*/
1322+ if (changed ) {
11981323#ifdef SETP_BYTE
1199- s -> stream .avail_in = 0 ;
1200- s -> stream .next_out = & (s -> deflateParams_out_byte ) ;
1201- s -> stream .avail_out = 1 ;
1202- RETVAL = deflateParams (& (s -> stream ), s -> Level , s -> Strategy );
1203- s -> deflateParams_out_valid =
1204- (RETVAL == Z_OK && s -> stream .avail_out == 0 ) ;
1205- /* printf("RETVAL %d, avail out %d, byte %c\n", RETVAL, s->stream.avail_out, s->deflateParams_out_byte); */
1324+ s -> stream .avail_in = 0 ;
1325+ s -> stream .next_out = & (s -> deflateParams_out_byte ) ;
1326+ s -> stream .avail_out = 1 ;
1327+ RETVAL = deflateParams (& (s -> stream ), s -> Level , s -> Strategy );
1328+ s -> deflateParams_out_valid =
1329+ (RETVAL == Z_OK && s -> stream .avail_out == 0 ) ;
12061330#else
1207- /* printf("Level %d Strategy %d, Prev Len %d\n",
1331+ /* printf("Level %d Strategy %d, Prev Len %d\n",
12081332 s->Level, s->Strategy, s->deflateParams_out_length); */
1209- s -> stream .avail_in = 0 ;
1210- if (s -> deflateParams_out_buffer == NULL )
1211- s -> deflateParams_out_buffer = safemalloc (deflateParams_BUFFER_SIZE );
1212- s -> stream .next_out = s -> deflateParams_out_buffer ;
1213- s -> stream .avail_out = deflateParams_BUFFER_SIZE ;
1214-
1215- RETVAL = deflateParams (& (s -> stream ), s -> Level , s -> Strategy );
1216- s -> deflateParams_out_length = deflateParams_BUFFER_SIZE - s -> stream .avail_out ;
1217- /* printf("RETVAL %d, length out %d, avail %d\n",
1218- RETVAL, s->deflateParams_out_length, s->stream.avail_out ); */
1333+ RETVAL = flushParams (s );
12191334#endif
1335+ }
1336+ else
1337+ RETVAL = Z_OK ;
12201338 OUTPUT :
1221- RETVAL
1339+ RETVAL
12221340
12231341
12241342int
0 commit comments