1
1
//! Implementation for C backends.
2
- use std:: cmp;
3
2
use std:: fmt;
4
3
use std:: marker;
4
+ use std:: mem:: MaybeUninit ;
5
5
use std:: os:: raw:: { c_int, c_uint} ;
6
6
use std:: ptr;
7
7
@@ -226,34 +226,12 @@ pub struct Inflate {
226
226
pub inner : Stream < DirDecompress > ,
227
227
}
228
228
229
- impl InflateBackend for Inflate {
230
- fn make ( zlib_header : bool , window_bits : u8 ) -> Self {
231
- unsafe {
232
- let state = StreamWrapper :: default ( ) ;
233
- let ret = mz_inflateInit2 (
234
- state. inner ,
235
- if zlib_header {
236
- window_bits as c_int
237
- } else {
238
- -( window_bits as c_int )
239
- } ,
240
- ) ;
241
- assert_eq ! ( ret, 0 ) ;
242
- Inflate {
243
- inner : Stream {
244
- stream_wrapper : state,
245
- total_in : 0 ,
246
- total_out : 0 ,
247
- _marker : marker:: PhantomData ,
248
- } ,
249
- }
250
- }
251
- }
252
-
253
- fn decompress (
229
+ impl Inflate {
230
+ unsafe fn decompress_inner (
254
231
& mut self ,
255
232
input : & [ u8 ] ,
256
- output : & mut [ u8 ] ,
233
+ output_ptr : * mut u8 ,
234
+ output_len : usize ,
257
235
flush : FlushDecompress ,
258
236
) -> Result < Status , DecompressError > {
259
237
let raw = self . inner . stream_wrapper . inner ;
@@ -263,16 +241,16 @@ impl InflateBackend for Inflate {
263
241
unsafe {
264
242
( * raw) . msg = ptr:: null_mut ( ) ;
265
243
( * raw) . next_in = input. as_ptr ( ) as * mut u8 ;
266
- ( * raw) . avail_in = cmp :: min ( input. len ( ) , c_uint:: MAX as usize ) as c_uint ;
267
- ( * raw) . next_out = output . as_mut_ptr ( ) ;
268
- ( * raw) . avail_out = cmp :: min ( output . len ( ) , c_uint:: MAX as usize ) as c_uint ;
244
+ ( * raw) . avail_in = input. len ( ) . min ( c_uint:: MAX as usize ) as c_uint ;
245
+ ( * raw) . next_out = output_ptr ;
246
+ ( * raw) . avail_out = output_len . min ( c_uint:: MAX as usize ) as c_uint ;
269
247
270
248
let rc = mz_inflate ( raw, flush as c_int ) ;
271
249
272
250
// Unfortunately the total counters provided by zlib might be only
273
251
// 32 bits wide and overflow while processing large amounts of data.
274
252
self . inner . total_in += ( ( * raw) . next_in as usize - input. as_ptr ( ) as usize ) as u64 ;
275
- self . inner . total_out += ( ( * raw) . next_out as usize - output . as_ptr ( ) as usize ) as u64 ;
253
+ self . inner . total_out += ( ( * raw) . next_out as usize - output_ptr as usize ) as u64 ;
276
254
277
255
// reset these pointers so we don't accidentally read them later
278
256
( * raw) . next_in = ptr:: null_mut ( ) ;
@@ -293,6 +271,47 @@ impl InflateBackend for Inflate {
293
271
}
294
272
}
295
273
}
274
+ }
275
+ impl InflateBackend for Inflate {
276
+ fn make ( zlib_header : bool , window_bits : u8 ) -> Self {
277
+ unsafe {
278
+ let state = StreamWrapper :: default ( ) ;
279
+ let ret = mz_inflateInit2 (
280
+ state. inner ,
281
+ if zlib_header {
282
+ window_bits as c_int
283
+ } else {
284
+ -( window_bits as c_int )
285
+ } ,
286
+ ) ;
287
+ assert_eq ! ( ret, 0 ) ;
288
+ Inflate {
289
+ inner : Stream {
290
+ stream_wrapper : state,
291
+ total_in : 0 ,
292
+ total_out : 0 ,
293
+ _marker : marker:: PhantomData ,
294
+ } ,
295
+ }
296
+ }
297
+ }
298
+
299
+ fn decompress (
300
+ & mut self ,
301
+ input : & [ u8 ] ,
302
+ output : & mut [ u8 ] ,
303
+ flush : FlushDecompress ,
304
+ ) -> Result < Status , DecompressError > {
305
+ unsafe { self . decompress_inner ( input, output. as_mut_ptr ( ) , output. len ( ) , flush) }
306
+ }
307
+ fn decompress_uninit (
308
+ & mut self ,
309
+ input : & [ u8 ] ,
310
+ output : & mut [ MaybeUninit < u8 > ] ,
311
+ flush : FlushDecompress ,
312
+ ) -> Result < Status , DecompressError > {
313
+ unsafe { self . decompress_inner ( input, output. as_mut_ptr ( ) as * mut _ , output. len ( ) , flush) }
314
+ }
296
315
297
316
fn reset ( & mut self , zlib_header : bool ) {
298
317
let bits = if zlib_header {
@@ -325,37 +344,12 @@ pub struct Deflate {
325
344
pub inner : Stream < DirCompress > ,
326
345
}
327
346
328
- impl DeflateBackend for Deflate {
329
- fn make ( level : Compression , zlib_header : bool , window_bits : u8 ) -> Self {
330
- unsafe {
331
- let state = StreamWrapper :: default ( ) ;
332
- let ret = mz_deflateInit2 (
333
- state. inner ,
334
- level. 0 as c_int ,
335
- MZ_DEFLATED ,
336
- if zlib_header {
337
- window_bits as c_int
338
- } else {
339
- -( window_bits as c_int )
340
- } ,
341
- 8 ,
342
- MZ_DEFAULT_STRATEGY ,
343
- ) ;
344
- assert_eq ! ( ret, 0 ) ;
345
- Deflate {
346
- inner : Stream {
347
- stream_wrapper : state,
348
- total_in : 0 ,
349
- total_out : 0 ,
350
- _marker : marker:: PhantomData ,
351
- } ,
352
- }
353
- }
354
- }
355
- fn compress (
347
+ impl Deflate {
348
+ unsafe fn compress_inner (
356
349
& mut self ,
357
350
input : & [ u8 ] ,
358
- output : & mut [ u8 ] ,
351
+ output_ptr : * mut u8 ,
352
+ output_len : usize ,
359
353
flush : FlushCompress ,
360
354
) -> Result < Status , CompressError > {
361
355
let raw = self . inner . stream_wrapper . inner ;
@@ -365,17 +359,17 @@ impl DeflateBackend for Deflate {
365
359
unsafe {
366
360
( * raw) . msg = ptr:: null_mut ( ) ;
367
361
( * raw) . next_in = input. as_ptr ( ) as * mut _ ;
368
- ( * raw) . avail_in = cmp :: min ( input. len ( ) , c_uint:: MAX as usize ) as c_uint ;
369
- ( * raw) . next_out = output . as_mut_ptr ( ) ;
370
- ( * raw) . avail_out = cmp :: min ( output . len ( ) , c_uint:: MAX as usize ) as c_uint ;
362
+ ( * raw) . avail_in = input. len ( ) . min ( c_uint:: MAX as usize ) as c_uint ;
363
+ ( * raw) . next_out = output_ptr ;
364
+ ( * raw) . avail_out = output_len . min ( c_uint:: MAX as usize ) as c_uint ;
371
365
372
366
let rc = mz_deflate ( raw, flush as c_int ) ;
373
367
374
368
// Unfortunately the total counters provided by zlib might be only
375
369
// 32 bits wide and overflow while processing large amounts of data.
376
370
377
371
self . inner . total_in += ( ( * raw) . next_in as usize - input. as_ptr ( ) as usize ) as u64 ;
378
- self . inner . total_out += ( ( * raw) . next_out as usize - output . as_ptr ( ) as usize ) as u64 ;
372
+ self . inner . total_out += ( ( * raw) . next_out as usize - output_ptr as usize ) as u64 ;
379
373
// reset these pointers so we don't accidentally read them later
380
374
( * raw) . next_in = ptr:: null_mut ( ) ;
381
375
( * raw) . avail_in = 0 ;
@@ -391,7 +385,51 @@ impl DeflateBackend for Deflate {
391
385
}
392
386
}
393
387
}
388
+ }
394
389
390
+ impl DeflateBackend for Deflate {
391
+ fn make ( level : Compression , zlib_header : bool , window_bits : u8 ) -> Self {
392
+ unsafe {
393
+ let state = StreamWrapper :: default ( ) ;
394
+ let ret = mz_deflateInit2 (
395
+ state. inner ,
396
+ level. 0 as c_int ,
397
+ MZ_DEFLATED ,
398
+ if zlib_header {
399
+ window_bits as c_int
400
+ } else {
401
+ -( window_bits as c_int )
402
+ } ,
403
+ 8 ,
404
+ MZ_DEFAULT_STRATEGY ,
405
+ ) ;
406
+ assert_eq ! ( ret, 0 ) ;
407
+ Deflate {
408
+ inner : Stream {
409
+ stream_wrapper : state,
410
+ total_in : 0 ,
411
+ total_out : 0 ,
412
+ _marker : marker:: PhantomData ,
413
+ } ,
414
+ }
415
+ }
416
+ }
417
+ fn compress (
418
+ & mut self ,
419
+ input : & [ u8 ] ,
420
+ output : & mut [ u8 ] ,
421
+ flush : FlushCompress ,
422
+ ) -> Result < Status , CompressError > {
423
+ unsafe { self . compress_inner ( input, output. as_mut_ptr ( ) , output. len ( ) , flush) }
424
+ }
425
+ fn compress_uninit (
426
+ & mut self ,
427
+ input : & [ u8 ] ,
428
+ output : & mut [ MaybeUninit < u8 > ] ,
429
+ flush : FlushCompress ,
430
+ ) -> Result < Status , CompressError > {
431
+ unsafe { self . compress_inner ( input, output. as_mut_ptr ( ) as * mut _ , output. len ( ) , flush) }
432
+ }
395
433
fn reset ( & mut self ) {
396
434
self . inner . total_in = 0 ;
397
435
self . inner . total_out = 0 ;
0 commit comments