@@ -120,7 +120,7 @@ brotli_set_window_size(cmd_parms *cmd, void *dummy, const char *arg)
120
120
& brotli_module );
121
121
int i = atoi (arg );
122
122
123
- if (i < kBrotliMinWindowBits || i > kBrotliMaxWindowBits ) {
123
+ if (i < BROTLI_MIN_WINDOW_BITS || i > BROTLI_MAX_WINDOW_BITS ) {
124
124
return "BrotliWindowSize is not a valid range" ;
125
125
}
126
126
@@ -182,7 +182,6 @@ typedef struct brotli_ctx_t
182
182
unsigned int filter_init :1 ;
183
183
size_t bytes_in ;
184
184
size_t bytes_out ;
185
- size_t ring_size ;
186
185
} brotli_ctx ;
187
186
188
187
static apr_status_t
@@ -256,27 +255,35 @@ have_ssl_compression(request_rec *r)
256
255
}
257
256
258
257
static apr_status_t
259
- brotli_compress (unsigned int last , unsigned int flush ,
258
+ brotli_compress (unsigned int operation ,
259
+ size_t len , const char * data ,
260
260
brotli_ctx * ctx , apr_pool_t * pool ,
261
261
struct apr_bucket_alloc_t * bucket_alloc )
262
262
{
263
- uint8_t * out ;
264
- size_t size ;
263
+ const uint8_t * next_in = ( uint8_t * ) data ;
264
+ size_t avail_in = len ;
265
265
266
- if (! BrotliEncoderWriteData ( ctx -> state , last , flush , & size , & out ) ) {
267
- return APR_EGENERAL ;
268
- }
266
+ while ( avail_in >= 0 ) {
267
+ uint8_t * next_out = NULL ;
268
+ size_t avail_out = 0 ;
269
269
270
- ctx -> bytes_out += size ;
270
+ if (!BrotliEncoderCompressStream (ctx -> state , operation ,
271
+ & avail_in , & next_in ,
272
+ & avail_out , & next_out , NULL )) {
273
+ return APR_EGENERAL ;
274
+ }
271
275
272
- char * buffer = (char * )apr_palloc (pool , size );
273
- if (!buffer ) {
274
- return APR_EGENERAL ;
275
- }
276
- memcpy (buffer , out , size );
276
+ if (BrotliEncoderHasMoreOutput (ctx -> state )) {
277
+ size_t size = 0 ;
278
+ char * buffer = (char * )BrotliEncoderTakeOutput (ctx -> state , & size );
279
+ ctx -> bytes_out += size ;
277
280
278
- apr_bucket * b = apr_bucket_heap_create (buffer , size , NULL , bucket_alloc );
279
- APR_BRIGADE_INSERT_TAIL (ctx -> bb , b );
281
+ apr_bucket * b = apr_bucket_heap_create (buffer , size , NULL , bucket_alloc );
282
+ APR_BRIGADE_INSERT_TAIL (ctx -> bb , b );
283
+ } else if (avail_in == 0 ) {
284
+ break ;
285
+ }
286
+ }
280
287
281
288
return APR_SUCCESS ;
282
289
}
@@ -506,7 +513,7 @@ brotli_out_filter(ap_filter_t *f, apr_bucket_brigade *bb)
506
513
uint32_t quality = (uint32_t )c -> compressionlevel ;
507
514
uint32_t lgwin = (uint32_t )c -> windowSize ;
508
515
if (len > 0 ) {
509
- while (len < (1 << (lgwin - 1 )) && lgwin > kBrotliMinWindowBits ) {
516
+ while (len < (1 << (lgwin - 1 )) && lgwin > BROTLI_MIN_WINDOW_BITS ) {
510
517
lgwin -- ;
511
518
}
512
519
}
@@ -516,7 +523,6 @@ brotli_out_filter(ap_filter_t *f, apr_bucket_brigade *bb)
516
523
517
524
ctx -> bytes_in = 0 ;
518
525
ctx -> bytes_out = 0 ;
519
- ctx -> ring_size = 0 ;
520
526
521
527
ap_log_rerror (APLOG_MARK , APLOG_DEBUG , 0 , r , APLOGNO (0485 )
522
528
"brotli encoder: quality: %d lgwin: %d" , quality , lgwin );
@@ -583,8 +589,8 @@ brotli_out_filter(ap_filter_t *f, apr_bucket_brigade *bb)
583
589
584
590
if (APR_BUCKET_IS_EOS (e )) {
585
591
/* flush the remaining data from the brotli buffers */
586
- apr_status_t rv = brotli_compress (1 , 0 , ctx , r -> pool , f -> c -> bucket_alloc );
587
- ctx -> ring_size = 0 ;
592
+ apr_status_t rv = brotli_compress (BROTLI_OPERATION_FINISH , 0 , NULL ,
593
+ ctx , r -> pool , f -> c -> bucket_alloc ) ;
588
594
if (rv != APR_SUCCESS ) {
589
595
ap_log_rerror (APLOG_MARK , APLOG_ERR , 0 , r , APLOGNO (0554 )
590
596
"Brotli compress error" );
@@ -667,33 +673,12 @@ brotli_out_filter(ap_filter_t *f, apr_bucket_brigade *bb)
667
673
668
674
/* write */
669
675
if (len != 0 ) {
670
- size_t offset = 0 , block_space = BrotliEncoderInputBlockSize (ctx -> state );
671
- while (len != offset ) {
672
- size_t copy_len = len - offset ;
673
-
674
- if ((ctx -> ring_size + copy_len ) > block_space ) {
675
- copy_len = block_space - ctx -> ring_size ;
676
- }
677
-
678
- if (copy_len > 0 ) {
679
- BrotliEncoderCopyInputToRingBuffer (ctx -> state , copy_len ,
680
- (const uint8_t * )data + offset );
681
- offset += copy_len ;
682
- ctx -> ring_size += copy_len ;
683
- ctx -> bytes_in += copy_len ;
684
- }
685
-
686
- if ((copy_len == block_space ) || (copy_len == 0 )) {
687
- /* flush the remaining data from the brotli buffers */
688
- apr_status_t rv = brotli_compress (0 , 1 , ctx , r -> pool ,
689
- f -> c -> bucket_alloc );
690
- ctx -> ring_size = 0 ;
691
- if (rv != APR_SUCCESS ) {
692
- ap_log_rerror (APLOG_MARK , APLOG_ERR , 0 , r , APLOGNO (0657 )
693
- "Brotli compress error" );
694
- return rv ;
695
- }
696
- }
676
+ apr_status_t rv = brotli_compress (BROTLI_OPERATION_PROCESS , len , data ,
677
+ ctx , r -> pool , f -> c -> bucket_alloc );
678
+ if (rv != APR_SUCCESS ) {
679
+ ap_log_rerror (APLOG_MARK , APLOG_ERR , 0 , r , APLOGNO (0657 )
680
+ "Brotli compress error" );
681
+ return APR_EGENERAL ;
697
682
}
698
683
}
699
684
0 commit comments