10
10
11
11
/*!
12
12
13
- Simple compression
13
+ Simple [DEFLATE][def]-based compression. This is a wrapper around the
14
+ [`miniz`][mz] library, which is a one-file pure-C implementation of zlib.
15
+
16
+ [def]: https://en.wikipedia.org/wiki/DEFLATE
17
+ [mz]: https://code.google.com/p/miniz/
14
18
15
19
*/
16
20
@@ -31,23 +35,21 @@ extern crate libc;
31
35
use std:: c_vec:: CVec ;
32
36
use libc:: { c_void, size_t, c_int} ;
33
37
34
-
35
- pub mod rustrt {
36
- use libc:: { c_void, size_t, c_int} ;
37
- #[ link( name = "miniz" , kind = "static" ) ]
38
- extern {
39
- pub fn tdefl_compress_mem_to_heap ( psrc_buf : * c_void ,
40
- src_buf_len : size_t ,
41
- pout_len : * mut size_t ,
42
- flags : c_int )
43
- -> * mut c_void ;
44
-
45
- pub fn tinfl_decompress_mem_to_heap ( psrc_buf : * c_void ,
46
- src_buf_len : size_t ,
47
- pout_len : * mut size_t ,
48
- flags : c_int )
49
- -> * mut c_void ;
50
- }
38
+ #[ link( name = "miniz" , kind = "static" ) ]
39
+ extern {
40
+ /// Raw miniz compression function.
41
+ fn tdefl_compress_mem_to_heap ( psrc_buf : * c_void ,
42
+ src_buf_len : size_t ,
43
+ pout_len : * mut size_t ,
44
+ flags : c_int )
45
+ -> * mut c_void ;
46
+
47
+ /// Raw miniz decompression function.
48
+ fn tinfl_decompress_mem_to_heap ( psrc_buf : * c_void ,
49
+ src_buf_len : size_t ,
50
+ pout_len : * mut size_t ,
51
+ flags : c_int )
52
+ -> * mut c_void ;
51
53
}
52
54
53
55
static LZ_NORM : c_int = 0x80 ; // LZ with 128 probes, "normal"
@@ -57,7 +59,7 @@ static TDEFL_WRITE_ZLIB_HEADER : c_int = 0x01000; // write zlib header and adler
57
59
fn deflate_bytes_internal ( bytes : & [ u8 ] , flags : c_int ) -> Option < CVec < u8 > > {
58
60
unsafe {
59
61
let mut outsz : size_t = 0 ;
60
- let res = rustrt :: tdefl_compress_mem_to_heap ( bytes. as_ptr ( ) as * c_void ,
62
+ let res = tdefl_compress_mem_to_heap ( bytes. as_ptr ( ) as * c_void ,
61
63
bytes. len ( ) as size_t ,
62
64
& mut outsz,
63
65
flags) ;
@@ -69,18 +71,20 @@ fn deflate_bytes_internal(bytes: &[u8], flags: c_int) -> Option<CVec<u8>> {
69
71
}
70
72
}
71
73
74
+ /// Compress a buffer, without writing any sort of header on the output.
72
75
pub fn deflate_bytes ( bytes : & [ u8 ] ) -> Option < CVec < u8 > > {
73
76
deflate_bytes_internal ( bytes, LZ_NORM )
74
77
}
75
78
79
+ /// Compress a buffer, using a header that zlib can understand.
76
80
pub fn deflate_bytes_zlib ( bytes : & [ u8 ] ) -> Option < CVec < u8 > > {
77
81
deflate_bytes_internal ( bytes, LZ_NORM | TDEFL_WRITE_ZLIB_HEADER )
78
82
}
79
83
80
84
fn inflate_bytes_internal ( bytes : & [ u8 ] , flags : c_int ) -> Option < CVec < u8 > > {
81
85
unsafe {
82
86
let mut outsz : size_t = 0 ;
83
- let res = rustrt :: tinfl_decompress_mem_to_heap ( bytes. as_ptr ( ) as * c_void ,
87
+ let res = tinfl_decompress_mem_to_heap ( bytes. as_ptr ( ) as * c_void ,
84
88
bytes. len ( ) as size_t ,
85
89
& mut outsz,
86
90
flags) ;
@@ -92,10 +96,12 @@ fn inflate_bytes_internal(bytes: &[u8], flags: c_int) -> Option<CVec<u8>> {
92
96
}
93
97
}
94
98
99
+ /// Decompress a buffer, without parsing any sort of header on the input.
95
100
pub fn inflate_bytes ( bytes : & [ u8 ] ) -> Option < CVec < u8 > > {
96
101
inflate_bytes_internal ( bytes, 0 )
97
102
}
98
103
104
+ /// Decompress a buffer that starts with a zlib header.
99
105
pub fn inflate_bytes_zlib ( bytes : & [ u8 ] ) -> Option < CVec < u8 > > {
100
106
inflate_bytes_internal ( bytes, TINFL_FLAG_PARSE_ZLIB_HEADER )
101
107
}
0 commit comments