@@ -66,7 +66,7 @@ typedef struct deflate_filter_config_t
66
66
int windowSize ;
67
67
int memlevel ;
68
68
int compressionlevel ;
69
- apr_size_t bufferSize ;
69
+ int bufferSize ;
70
70
const char * note_ratio_name ;
71
71
const char * note_input_name ;
72
72
const char * note_output_name ;
@@ -254,7 +254,7 @@ static const char *deflate_set_buffer_size(cmd_parms *cmd, void *dummy,
254
254
return "DeflateBufferSize should be positive" ;
255
255
}
256
256
257
- c -> bufferSize = ( apr_size_t ) n ;
257
+ c -> bufferSize = n ;
258
258
259
259
return NULL ;
260
260
}
@@ -416,35 +416,40 @@ typedef struct deflate_ctx_t
416
416
/* Do update ctx->crc, see comment in flush_libz_buffer */
417
417
#define UPDATE_CRC 1
418
418
419
+ static void consume_buffer (deflate_ctx * ctx , deflate_filter_config * c ,
420
+ int len , int crc , apr_bucket_brigade * bb )
421
+ {
422
+ apr_bucket * b ;
423
+
424
+ /*
425
+ * Do we need to update ctx->crc? Usually this is the case for
426
+ * inflate action where we need to do a crc on the output, whereas
427
+ * in the deflate case we need to do a crc on the input
428
+ */
429
+ if (crc ) {
430
+ ctx -> crc = crc32 (ctx -> crc , (const Bytef * )ctx -> buffer , len );
431
+ }
432
+
433
+ b = apr_bucket_heap_create ((char * )ctx -> buffer , len , NULL ,
434
+ bb -> bucket_alloc );
435
+ APR_BRIGADE_INSERT_TAIL (bb , b );
436
+
437
+ ctx -> stream .next_out = ctx -> buffer ;
438
+ ctx -> stream .avail_out = c -> bufferSize ;
439
+ }
440
+
419
441
static int flush_libz_buffer (deflate_ctx * ctx , deflate_filter_config * c ,
420
- struct apr_bucket_alloc_t * bucket_alloc ,
421
442
int (* libz_func )(z_streamp , int ), int flush ,
422
443
int crc )
423
444
{
424
445
int zRC = Z_OK ;
425
446
int done = 0 ;
426
- unsigned int deflate_len ;
427
- apr_bucket * b ;
447
+ int deflate_len ;
428
448
429
449
for (;;) {
430
450
deflate_len = c -> bufferSize - ctx -> stream .avail_out ;
431
-
432
- if (deflate_len != 0 ) {
433
- /*
434
- * Do we need to update ctx->crc? Usually this is the case for
435
- * inflate action where we need to do a crc on the output, whereas
436
- * in the deflate case we need to do a crc on the input
437
- */
438
- if (crc ) {
439
- ctx -> crc = crc32 (ctx -> crc , (const Bytef * )ctx -> buffer ,
440
- deflate_len );
441
- }
442
- b = apr_bucket_heap_create ((char * )ctx -> buffer ,
443
- deflate_len , NULL ,
444
- bucket_alloc );
445
- APR_BRIGADE_INSERT_TAIL (ctx -> bb , b );
446
- ctx -> stream .next_out = ctx -> buffer ;
447
- ctx -> stream .avail_out = c -> bufferSize ;
451
+ if (deflate_len > 0 ) {
452
+ consume_buffer (ctx , c , deflate_len , crc , ctx -> bb );
448
453
}
449
454
450
455
if (done )
@@ -560,6 +565,7 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
560
565
request_rec * r = f -> r ;
561
566
deflate_ctx * ctx = f -> ctx ;
562
567
int zRC ;
568
+ apr_status_t rv ;
563
569
apr_size_t len = 0 , blen ;
564
570
const char * data ;
565
571
deflate_filter_config * c ;
@@ -891,8 +897,7 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
891
897
892
898
ctx -> stream .avail_in = 0 ; /* should be zero already anyway */
893
899
/* flush the remaining data from the zlib buffers */
894
- flush_libz_buffer (ctx , c , f -> c -> bucket_alloc , deflate , Z_FINISH ,
895
- NO_UPDATE_CRC );
900
+ flush_libz_buffer (ctx , c , deflate , Z_FINISH , NO_UPDATE_CRC );
896
901
897
902
buf = apr_palloc (r -> pool , VALIDATION_SIZE );
898
903
putLong ((unsigned char * )& buf [0 ], ctx -> crc );
@@ -945,15 +950,15 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
945
950
/* Okay, we've seen the EOS.
946
951
* Time to pass it along down the chain.
947
952
*/
948
- return ap_pass_brigade (f -> next , ctx -> bb );
953
+ rv = ap_pass_brigade (f -> next , ctx -> bb );
954
+ apr_brigade_cleanup (ctx -> bb );
955
+ return rv ;
949
956
}
950
957
951
958
if (APR_BUCKET_IS_FLUSH (e )) {
952
- apr_status_t rv ;
953
-
954
959
/* flush the remaining data from the zlib buffers */
955
- zRC = flush_libz_buffer (ctx , c , f -> c -> bucket_alloc , deflate ,
956
- Z_SYNC_FLUSH , NO_UPDATE_CRC );
960
+ zRC = flush_libz_buffer (ctx , c , deflate , Z_SYNC_FLUSH ,
961
+ NO_UPDATE_CRC );
957
962
if (zRC != Z_OK ) {
958
963
ap_log_rerror (APLOG_MARK , APLOG_ERR , 0 , r , APLOGNO (01385 )
959
964
"Zlib error %d flushing zlib output buffer (%s)" ,
@@ -965,6 +970,7 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
965
970
APR_BUCKET_REMOVE (e );
966
971
APR_BRIGADE_INSERT_TAIL (ctx -> bb , e );
967
972
rv = ap_pass_brigade (f -> next , ctx -> bb );
973
+ apr_brigade_cleanup (ctx -> bb );
968
974
if (rv != APR_SUCCESS ) {
969
975
return rv ;
970
976
}
@@ -999,21 +1005,15 @@ static apr_status_t deflate_out_filter(ap_filter_t *f,
999
1005
ctx -> stream .next_in = (unsigned char * )data ; /* We just lost const-ness,
1000
1006
* but we'll just have to
1001
1007
* trust zlib */
1002
- ctx -> stream .avail_in = len ;
1008
+ ctx -> stream .avail_in = ( int ) len ;
1003
1009
1004
1010
while (ctx -> stream .avail_in != 0 ) {
1005
1011
if (ctx -> stream .avail_out == 0 ) {
1006
- apr_status_t rv ;
1007
-
1008
- ctx -> stream .next_out = ctx -> buffer ;
1009
- len = c -> bufferSize - ctx -> stream .avail_out ;
1012
+ consume_buffer (ctx , c , c -> bufferSize , NO_UPDATE_CRC , ctx -> bb );
1010
1013
1011
- b = apr_bucket_heap_create ((char * )ctx -> buffer , len ,
1012
- NULL , f -> c -> bucket_alloc );
1013
- APR_BRIGADE_INSERT_TAIL (ctx -> bb , b );
1014
- ctx -> stream .avail_out = c -> bufferSize ;
1015
1014
/* Send what we have right now to the next filter. */
1016
1015
rv = ap_pass_brigade (f -> next , ctx -> bb );
1016
+ apr_brigade_cleanup (ctx -> bb );
1017
1017
if (rv != APR_SUCCESS ) {
1018
1018
return rv ;
1019
1019
}
@@ -1340,14 +1340,8 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
1340
1340
return APR_EINVAL ;
1341
1341
}
1342
1342
1343
- len = c -> bufferSize - ctx -> stream .avail_out ;
1344
- ctx -> crc = crc32 (ctx -> crc , (const Bytef * )ctx -> buffer , len );
1345
- tmp_b = apr_bucket_heap_create ((char * )ctx -> buffer , len ,
1346
- NULL , f -> c -> bucket_alloc );
1347
- APR_BRIGADE_INSERT_TAIL (ctx -> proc_bb , tmp_b );
1348
-
1349
- ctx -> stream .next_out = ctx -> buffer ;
1350
- ctx -> stream .avail_out = c -> bufferSize ;
1343
+ consume_buffer (ctx , c , c -> bufferSize - ctx -> stream .avail_out ,
1344
+ UPDATE_CRC , ctx -> proc_bb );
1351
1345
1352
1346
/* Flush everything so far in the returning brigade, but continue
1353
1347
* reading should EOS/more follow (don't lose them).
@@ -1393,16 +1387,8 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
1393
1387
if (!ctx -> validation_buffer ) {
1394
1388
while (ctx -> stream .avail_in != 0 ) {
1395
1389
if (ctx -> stream .avail_out == 0 ) {
1396
- apr_bucket * tmp_heap ;
1397
-
1398
- ctx -> stream .next_out = ctx -> buffer ;
1399
- len = c -> bufferSize - ctx -> stream .avail_out ;
1400
-
1401
- ctx -> crc = crc32 (ctx -> crc , (const Bytef * )ctx -> buffer , len );
1402
- tmp_heap = apr_bucket_heap_create ((char * )ctx -> buffer , len ,
1403
- NULL , f -> c -> bucket_alloc );
1404
- APR_BRIGADE_INSERT_TAIL (ctx -> proc_bb , tmp_heap );
1405
- ctx -> stream .avail_out = c -> bufferSize ;
1390
+ consume_buffer (ctx , c , c -> bufferSize , UPDATE_CRC ,
1391
+ ctx -> proc_bb );
1406
1392
}
1407
1393
1408
1394
ctx -> inflate_total += ctx -> stream .avail_out ;
@@ -1445,7 +1431,6 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
1445
1431
}
1446
1432
1447
1433
if (ctx -> validation_buffer ) {
1448
- apr_bucket * tmp_heap ;
1449
1434
apr_size_t avail , valid ;
1450
1435
unsigned char * buf = ctx -> validation_buffer ;
1451
1436
@@ -1474,13 +1459,8 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
1474
1459
(apr_uint64_t )ctx -> stream .total_in ,
1475
1460
(apr_uint64_t )ctx -> stream .total_out , r -> uri );
1476
1461
1477
- len = c -> bufferSize - ctx -> stream .avail_out ;
1478
-
1479
- ctx -> crc = crc32 (ctx -> crc , (const Bytef * )ctx -> buffer , len );
1480
- tmp_heap = apr_bucket_heap_create ((char * )ctx -> buffer , len ,
1481
- NULL , f -> c -> bucket_alloc );
1482
- APR_BRIGADE_INSERT_TAIL (ctx -> proc_bb , tmp_heap );
1483
- ctx -> stream .avail_out = c -> bufferSize ;
1462
+ consume_buffer (ctx , c , c -> bufferSize - ctx -> stream .avail_out ,
1463
+ UPDATE_CRC , ctx -> proc_bb );
1484
1464
1485
1465
{
1486
1466
unsigned long compCRC , compLen ;
@@ -1526,16 +1506,8 @@ static apr_status_t deflate_in_filter(ap_filter_t *f,
1526
1506
if (block == APR_BLOCK_READ &&
1527
1507
APR_BRIGADE_EMPTY (ctx -> proc_bb ) &&
1528
1508
ctx -> stream .avail_out < c -> bufferSize ) {
1529
- apr_bucket * tmp_heap ;
1530
- apr_size_t len ;
1531
- ctx -> stream .next_out = ctx -> buffer ;
1532
- len = c -> bufferSize - ctx -> stream .avail_out ;
1533
-
1534
- ctx -> crc = crc32 (ctx -> crc , (const Bytef * )ctx -> buffer , len );
1535
- tmp_heap = apr_bucket_heap_create ((char * )ctx -> buffer , len ,
1536
- NULL , f -> c -> bucket_alloc );
1537
- APR_BRIGADE_INSERT_TAIL (ctx -> proc_bb , tmp_heap );
1538
- ctx -> stream .avail_out = c -> bufferSize ;
1509
+ consume_buffer (ctx , c , c -> bufferSize - ctx -> stream .avail_out ,
1510
+ UPDATE_CRC , ctx -> proc_bb );
1539
1511
}
1540
1512
1541
1513
if (!APR_BRIGADE_EMPTY (ctx -> proc_bb )) {
@@ -1651,7 +1623,6 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
1651
1623
while (!APR_BRIGADE_EMPTY (bb ))
1652
1624
{
1653
1625
const char * data ;
1654
- apr_bucket * b ;
1655
1626
apr_size_t len ;
1656
1627
1657
1628
e = APR_BRIGADE_FIRST (bb );
@@ -1673,8 +1644,7 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
1673
1644
* fails, whereas in the deflate case you can empty a filled output
1674
1645
* buffer and call it again until no more output can be created.
1675
1646
*/
1676
- flush_libz_buffer (ctx , c , f -> c -> bucket_alloc , inflate , Z_SYNC_FLUSH ,
1677
- UPDATE_CRC );
1647
+ flush_libz_buffer (ctx , c , inflate , Z_SYNC_FLUSH , UPDATE_CRC );
1678
1648
ap_log_rerror (APLOG_MARK , APLOG_DEBUG , 0 , r , APLOGNO (01398 )
1679
1649
"Zlib: Inflated %" APR_UINT64_T_FMT
1680
1650
" to %" APR_UINT64_T_FMT " : URL %s" ,
@@ -1716,15 +1686,14 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
1716
1686
* Okay, we've seen the EOS.
1717
1687
* Time to pass it along down the chain.
1718
1688
*/
1719
- return ap_pass_brigade (f -> next , ctx -> bb );
1689
+ rv = ap_pass_brigade (f -> next , ctx -> bb );
1690
+ apr_brigade_cleanup (ctx -> bb );
1691
+ return rv ;
1720
1692
}
1721
1693
1722
1694
if (APR_BUCKET_IS_FLUSH (e )) {
1723
- apr_status_t rv ;
1724
-
1725
1695
/* flush the remaining data from the zlib buffers */
1726
- zRC = flush_libz_buffer (ctx , c , f -> c -> bucket_alloc , inflate ,
1727
- Z_SYNC_FLUSH , UPDATE_CRC );
1696
+ zRC = flush_libz_buffer (ctx , c , inflate , Z_SYNC_FLUSH , UPDATE_CRC );
1728
1697
if (zRC == Z_STREAM_END ) {
1729
1698
if (ctx -> validation_buffer == NULL ) {
1730
1699
ctx -> validation_buffer = apr_pcalloc (f -> r -> pool ,
@@ -1742,6 +1711,7 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
1742
1711
APR_BUCKET_REMOVE (e );
1743
1712
APR_BRIGADE_INSERT_TAIL (ctx -> bb , e );
1744
1713
rv = ap_pass_brigade (f -> next , ctx -> bb );
1714
+ apr_brigade_cleanup (ctx -> bb );
1745
1715
if (rv != APR_SUCCESS ) {
1746
1716
return rv ;
1747
1717
}
@@ -1858,16 +1828,11 @@ static apr_status_t inflate_out_filter(ap_filter_t *f,
1858
1828
1859
1829
while (ctx -> stream .avail_in != 0 ) {
1860
1830
if (ctx -> stream .avail_out == 0 ) {
1861
- ctx -> stream .next_out = ctx -> buffer ;
1862
- len = c -> bufferSize - ctx -> stream .avail_out ;
1863
-
1864
- ctx -> crc = crc32 (ctx -> crc , (const Bytef * )ctx -> buffer , len );
1865
- b = apr_bucket_heap_create ((char * )ctx -> buffer , len ,
1866
- NULL , f -> c -> bucket_alloc );
1867
- APR_BRIGADE_INSERT_TAIL (ctx -> bb , b );
1868
- ctx -> stream .avail_out = c -> bufferSize ;
1831
+ consume_buffer (ctx , c , c -> bufferSize , UPDATE_CRC , ctx -> bb );
1832
+
1869
1833
/* Send what we have right now to the next filter. */
1870
1834
rv = ap_pass_brigade (f -> next , ctx -> bb );
1835
+ apr_brigade_cleanup (ctx -> bb );
1871
1836
if (rv != APR_SUCCESS ) {
1872
1837
return rv ;
1873
1838
}
0 commit comments