@@ -2404,3 +2404,148 @@ fn test_dict_inflate() {
24042404 assert_eq!( uncompr[ ..hello. len( ) ] , hello) ;
24052405 } ) ;
24062406}
2407+
2408+ mod stable_api {
2409+ use zlib_rs:: {
2410+ deflate:: { compress_slice, DeflateConfig } ,
2411+ InflateError , ReturnCode ,
2412+ } ;
2413+
2414+ fn with_window_bits ( n : i32 ) {
2415+ let config = DeflateConfig {
2416+ window_bits : n,
2417+ ..DeflateConfig :: default ( )
2418+ } ;
2419+
2420+ let mut output = [ 0u8 ; 64 ] ;
2421+ let input = "Hello World!" ;
2422+ let ( compressed, ret) = compress_slice ( & mut output, input. as_bytes ( ) , config) ;
2423+ assert_eq ! ( ret, ReturnCode :: Ok ) ;
2424+
2425+ let mut decompressed = [ 0u8 ; 64 ] ;
2426+ let mut inflate = zlib_rs:: Inflate :: new ( n >= 0 , n. unsigned_abs ( ) as u8 ) ;
2427+ inflate
2428+ . decompress ( compressed, & mut decompressed, zlib_rs:: InflateFlush :: Finish )
2429+ . unwrap ( ) ;
2430+
2431+ assert_eq ! ( inflate. total_in( ) as usize , compressed. len( ) ) ;
2432+
2433+ assert_eq ! (
2434+ & decompressed[ ..inflate. total_out( ) as usize ] ,
2435+ input. as_bytes( )
2436+ ) ;
2437+ }
2438+
2439+ #[ test]
2440+ fn raw ( ) {
2441+ with_window_bits ( -15 ) ;
2442+ }
2443+
2444+ #[ test]
2445+ fn zlib_header ( ) {
2446+ with_window_bits ( 15 ) ;
2447+ }
2448+
2449+ #[ test]
2450+ fn gz_header ( ) {
2451+ with_window_bits ( 16 + 15 ) ;
2452+ }
2453+
2454+ #[ test]
2455+ #[ should_panic = "StreamError" ]
2456+ fn invalid_config ( ) {
2457+ zlib_rs:: Inflate :: new ( true , 123 ) ;
2458+ }
2459+
2460+ #[ test]
2461+ fn invalid_data ( ) {
2462+ let mut inflate = zlib_rs:: Inflate :: new ( true , 15 ) ;
2463+
2464+ // Clearly invalid input.
2465+ let compressed = [ 0xAA ; 64 ] ;
2466+ let mut decompressed = [ 0u8 ; 64 ] ;
2467+
2468+ let ret = inflate. decompress (
2469+ & compressed,
2470+ & mut decompressed,
2471+ zlib_rs:: InflateFlush :: Finish ,
2472+ ) ;
2473+
2474+ assert_eq ! ( ret, Err ( InflateError :: DataError ) ) ;
2475+ }
2476+
2477+ #[ test]
2478+ fn need_dict ( ) {
2479+ let mut inflate = zlib_rs:: Inflate :: new ( true , 15 ) ;
2480+
2481+ let compressed = [ 0x08 , 0xb8 , 0x0 , 0x0 , 0x0 , 0x1 ] ;
2482+ let mut decompressed = [ 0u8 ; 64 ] ;
2483+
2484+ let ret = inflate. decompress (
2485+ & compressed,
2486+ & mut decompressed,
2487+ zlib_rs:: InflateFlush :: Finish ,
2488+ ) ;
2489+
2490+ assert_eq ! ( ret, Err ( InflateError :: NeedDict { dict_id: 1 } ) ) ;
2491+ }
2492+
2493+ #[ test]
2494+ fn reset_reuse ( ) {
2495+ let input1 = "Hello World!" ;
2496+ let input2 = "Goodbye World!" ;
2497+
2498+ let mut output1 = [ 0u8 ; 64 ] ;
2499+ let config1 = DeflateConfig {
2500+ window_bits : 15 ,
2501+ ..DeflateConfig :: default ( )
2502+ } ;
2503+ let ( compressed1, ret) = compress_slice ( & mut output1, input1. as_bytes ( ) , config1) ;
2504+ assert_eq ! ( ret, ReturnCode :: Ok ) ;
2505+
2506+ let mut output2 = [ 0u8 ; 64 ] ;
2507+ let config2 = DeflateConfig {
2508+ window_bits : -15 ,
2509+ ..DeflateConfig :: default ( )
2510+ } ;
2511+ let ( compressed2, ret) = compress_slice ( & mut output2, input2. as_bytes ( ) , config2) ;
2512+ assert_eq ! ( ret, ReturnCode :: Ok ) ;
2513+
2514+ // Start with header enabled.
2515+ let zlib_header_first = true ;
2516+ let mut inflate = zlib_rs:: Inflate :: new ( zlib_header_first, 15 ) ;
2517+
2518+ let mut decompressed1 = [ 0u8 ; 64 ] ;
2519+ inflate
2520+ . decompress (
2521+ compressed1,
2522+ & mut decompressed1,
2523+ zlib_rs:: InflateFlush :: Finish ,
2524+ )
2525+ . unwrap ( ) ;
2526+
2527+ assert_eq ! ( inflate. total_in( ) as usize , compressed1. len( ) ) ;
2528+ assert_eq ! (
2529+ & decompressed1[ ..inflate. total_out( ) as usize ] ,
2530+ input1. as_bytes( )
2531+ ) ;
2532+
2533+ // Reset for a *raw* stream: swap the zlib_header flag.
2534+ inflate. reset ( !zlib_header_first) ;
2535+
2536+ let mut decompressed2 = [ 0u8 ; 64 ] ;
2537+ inflate
2538+ . decompress (
2539+ compressed2,
2540+ & mut decompressed2,
2541+ zlib_rs:: InflateFlush :: Finish ,
2542+ )
2543+ . unwrap ( ) ;
2544+
2545+ assert_eq ! ( inflate. total_in( ) as usize , compressed2. len( ) ) ;
2546+ assert_eq ! (
2547+ & decompressed2[ ..inflate. total_out( ) as usize ] ,
2548+ input2. as_bytes( )
2549+ ) ;
2550+ }
2551+ }
0 commit comments