@@ -1115,7 +1115,7 @@ PHP_FUNCTION(deflate_add)
11151115{
11161116 zend_string * out ;
11171117 char * in_buf ;
1118- size_t in_len , out_size ;
1118+ size_t in_len , out_size , buffer_used ;
11191119 zval * res ;
11201120 z_stream * ctx ;
11211121 zend_long flush_type = Z_SYNC_FLUSH ;
@@ -1157,14 +1157,29 @@ PHP_FUNCTION(deflate_add)
11571157 out_size = PHP_ZLIB_BUFFER_SIZE_GUESS (ctx -> total_in + in_len );
11581158 out_size = (ctx -> total_out >= out_size ) ? 16 : (out_size - ctx -> total_out );
11591159 out_size = (out_size < 16 ) ? 16 : out_size ;
1160+ out_size += 64 ;
11601161 out = zend_string_alloc (out_size , 0 );
11611162
11621163 ctx -> next_in = (Bytef * ) in_buf ;
11631164 ctx -> next_out = (Bytef * ) ZSTR_VAL (out );
11641165 ctx -> avail_in = in_len ;
11651166 ctx -> avail_out = ZSTR_LEN (out );
11661167
1167- status = deflate (ctx , flush_type );
1168+ buffer_used = 0 ;
1169+
1170+ do {
1171+ if (ctx -> avail_out == 0 ) {
1172+ /* more output buffer space needed; realloc and try again */
1173+ /* adding 64 more bytes solved every issue I have seen */
1174+ /* the + 1 is for the string terminator added below */
1175+ out = zend_string_realloc (out , ZSTR_LEN (out ) + 64 + 1 , 0 );
1176+ ctx -> avail_out = 64 ;
1177+ ctx -> next_out = (Bytef * ) ZSTR_VAL (out ) + buffer_used ;
1178+ }
1179+ status = deflate (ctx , flush_type );
1180+ buffer_used = ZSTR_LEN (out ) - ctx -> avail_out ;
1181+ } while (status == Z_OK && ctx -> avail_out == 0 );
1182+
11681183 switch (status ) {
11691184 case Z_OK :
11701185 ZSTR_LEN (out ) = (char * ) ctx -> next_out - ZSTR_VAL (out );
0 commit comments