Debian Codegraph @

src/lz4f/frame.rs

raw symbols SCIP
1 //! LZ4 Frame Compressor/Decompressor
2
3 use superrust-analyzer cargo lzzzz 2.0.0 lz4f/::{apirust-analyzer cargo lzzzz 2.0.0 lz4f/api/, Resultrust-analyzer cargo lzzzz 2.0.0 lz4f/error/Result#};
4 use craterust-analyzer cargo lzzzz 2.0.0 crate/::{commonrust-analyzer cargo lzzzz 2.0.0 common/::DEFAULT_BUF_SIZErust-analyzer cargo lzzzz 2.0.0 common/DEFAULT_BUF_SIZE., lz4frust-analyzer cargo lzzzz 2.0.0 lz4f/::Preferencesrust-analyzer cargo lzzzz 2.0.0 lz4f/preferences/Preferences#, Errorrust-analyzer cargo lzzzz 2.0.0 common/error/Error#, ErrorKindrust-analyzer cargo lzzzz 2.0.0 common/error/ErrorKind#};
5 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{cellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/::RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#, opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#};
6
7 /// Calculates the maximum size of the compressed output.
8 ///
9 /// If `original_size` is too large to compress, this returns `0`.
10 ///
11 /// Returned values are reliable only for [`compress`] and [`compress_to_vec`].
12 /// Streaming compressors may produce larger compressed frames.
13 ///
14 /// [`compress`]: fn.compress.html
15 /// [`compress_to_vec`]: fn.compress_to_vec.html
16 #[must_use]
17 pub fn max_compressed_sizerust-analyzer cargo lzzzz 2.0.0 lz4f/frame/max_compressed_size().(original_size: usize, prefs: &Preferencesrust-analyzer cargo lzzzz 2.0.0 lz4f/preferences/Preferences#) -> usize {
18 apirust-analyzer cargo lzzzz 2.0.0 lz4f/api/::compress_frame_boundrust-analyzer cargo lzzzz 2.0.0 lz4f/api/compress_frame_bound().(original_size, prefs)
19 }
20
21 /// Performs LZ4F compression.
22 ///
23 /// Ensure that the destination slice has enough capacity.
24 /// If `dst.len()` is smaller than `lz4f::max_compressed_size(src.len())`,
25 /// this function may fail.
26 ///
27 /// Returns the number of bytes written into the destination buffer.
28 ///
29 /// # Example
30 ///
31 /// Compress data with the default compression mode:
32 /// ```
33 /// use lzzzz::lz4f;
34 ///
35 /// let prefs = lz4f::Preferences::default();
36 /// let data = b"The quick brown fox jumps over the lazy dog.";
37 /// let mut buf = [0u8; 2048];
38 ///
39 /// // The slice should have enough capacity.
40 /// assert!(buf.len() >= lz4f::max_compressed_size(data.len(), &prefs));
41 ///
42 /// let len = lz4f::compress(data, &mut buf, &prefs)?;
43 /// let compressed = &buf[..len];
44 /// # let mut buf = Vec::new();
45 /// # lz4f::decompress_to_vec(compressed, &mut buf)?;
46 /// # assert_eq!(buf.as_slice(), &data[..]);
47 /// # Ok::<(), std::io::Error>(())
48 /// ```
49 pub fn compressrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/compress().(src: &[u8], dst: &mut [u8], prefs: &Preferencesrust-analyzer cargo lzzzz 2.0.0 lz4f/preferences/Preferences#) -> Resultrust-analyzer cargo lzzzz 2.0.0 lz4f/error/Result#<usize> {
50 compress_to_ptrrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/compress_to_ptr().(src, dst.as_mut_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]as_mut_ptr().(), dst.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), prefs)
51 }
52
53 fn compress_to_ptrrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/compress_to_ptr().(src: &[u8], dst: *mut u8, dst_len: usize, prefs: &Preferencesrust-analyzer cargo lzzzz 2.0.0 lz4f/preferences/Preferences#) -> Resultrust-analyzer cargo lzzzz 2.0.0 lz4f/error/Result#<usize> {
54 let mut prefs = *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().prefs;
55 if prefs.frame_inforust-analyzer cargo lzzzz 2.0.0 lz4f/preferences/impl#[Preferences]frame_info().().content_sizerust-analyzer cargo lzzzz 2.0.0 lz4f/frame_info/impl#[FrameInfo]content_size().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().>rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]gt().0 {
56 prefs.set_content_sizerust-analyzer cargo lzzzz 2.0.0 lz4f/preferences/impl#[Preferences]set_content_size().(src.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().());
57 }
58 apirust-analyzer cargo lzzzz 2.0.0 lz4f/api/::compressrust-analyzer cargo lzzzz 2.0.0 lz4f/api/compress().(src, dst, dst_len, &prefs)
59 }
60
61 /// Appends a compressed frame to `Vec<u8>`.
62 ///
63 /// Returns the number of bytes appended to the given `Vec<u8>`.
64 ///
65 /// # Example
66 ///
67 /// Compress data with the default compression mode:
68 /// ```
69 /// use lzzzz::lz4f;
70 ///
71 /// let prefs = lz4f::Preferences::default();
72 /// let data = b"The quick brown fox jumps over the lazy dog.";
73 /// let mut buf = Vec::new();
74 ///
75 /// let len = lz4f::compress_to_vec(data, &mut buf, &prefs)?;
76 /// let compressed = &buf;
77 /// # let mut buf = Vec::new();
78 /// # lz4f::decompress_to_vec(compressed, &mut buf)?;
79 /// # assert_eq!(buf.as_slice(), &data[..]);
80 /// # Ok::<(), std::io::Error>(())
81 /// ```
82 pub fn compress_to_vecrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/compress_to_vec().(src: &[u8], dst: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>, prefs: &Preferencesrust-analyzer cargo lzzzz 2.0.0 lz4f/preferences/Preferences#) -> Resultrust-analyzer cargo lzzzz 2.0.0 lz4f/error/Result#<usize> {
83 let orig_len = dst.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().();
84 dst.reserverust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]reserve().(max_compressed_sizerust-analyzer cargo lzzzz 2.0.0 lz4f/frame/max_compressed_size().(src.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().(), prefs));
85 #[allow(unsafe_code)]
86 unsafe {
87 let result = compress_to_ptrrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/compress_to_ptr().(
88 src,
89 dst.as_mut_ptrrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]as_mut_ptr().().addrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]add().(orig_len),
90 dst.capacityrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]capacity().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().orig_len,
91 prefs,
92 );
93 dst.set_lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]set_len().(orig_len rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<&usize>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<&usize>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<&usize>`]add().result.as_refrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]as_ref().().unwrap_orrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap_or().(&0));
94 result
95 }
96 }
97
98 /// Decompresses an LZ4 frame.
99 ///
100 /// Returns the number of bytes appended to the given `Vec<u8>`.
101 ///
102 /// # Example
103 ///
104 /// ```
105 /// use lzzzz::lz4f;
106 ///
107 /// const COMPRESSED_DATA: &str =
108 /// "BCJNGGBAgiwAAIBUaGUgcXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nLgAAAAA=";
109 ///
110 /// let data = base64::decode(COMPRESSED_DATA).unwrap();
111 /// let mut buf = Vec::new();
112 ///
113 /// lz4f::decompress_to_vec(&data[..], &mut buf)?;
114 ///
115 /// assert_eq!(
116 /// &buf[..],
117 /// &b"The quick brown fox jumps over the lazy dog."[..]
118 /// );
119 /// # Ok::<(), std::io::Error>(())
120 /// ```
121 pub fn decompress_to_vecrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/decompress_to_vec().(src: &[u8], dst: &mut Vecrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/Vec#<u8>) -> Resultrust-analyzer cargo lzzzz 2.0.0 lz4f/error/Result#<usize> {
122 let header_len = dst.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().();
123 let mut src_offset = 0;
124 let mut dst_offset = header_len;
125 DecompressionCtxrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/DecompressionCtx#::withrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/impl#[DecompressionCtx]with().(|ctx| {
126 let mut ctx = ctx.borrow_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]borrow_mut().();
127 ctx.resetrust-analyzer cargo lzzzz 2.0.0 lz4f/api/impl#[DecompressionContext]reset().();
128 loop {
129 dst.resize_withrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]resize_with().(dst.lenrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]len().() rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().+rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Add<Self>`]add().DEFAULT_BUF_SIZErust-analyzer cargo lzzzz 2.0.0 common/DEFAULT_BUF_SIZE., Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/impl#[u8][Default]default().);
130 match ctx.decompress_dictrust-analyzer cargo lzzzz 2.0.0 lz4f/api/impl#[DecompressionContext]decompress_dict().(&src[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().src_offset..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., &mut dst[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut().dst_offset..rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeFrom#]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()., &[], false) {
131 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#((src_len, dst_len, expected)) => {
132 src_offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().src_len;
133 dst_offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().+=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`AddAssign<Self>`]add_assign().dst_len;
134 if expected rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().==rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialEq<Self>`]eq().0 {
135 dst.resize_withrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]resize_with().(dst_offset, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/impl#[u8][Default]default().);
136 return Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(dst_offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().header_len);
137 } else if src_offset rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().>=rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/impls/impl#[usize][`PartialOrd<Self>`]ge().src.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().() {
138 dst.resize_withrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]resize_with().(header_len, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/impl#[u8][Default]default().);
139 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo lzzzz 2.0.0 common/error/Error#::newrust-analyzer cargo lzzzz 2.0.0 common/error/impl#[Error]new().(ErrorKindrust-analyzer cargo lzzzz 2.0.0 common/error/ErrorKind#::CompressedDataIncompleterust-analyzer cargo lzzzz 2.0.0 common/error/ErrorKind#CompressedDataIncomplete#).intorust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/impl#[T][`Into<U>`]into().());
140 }
141 }
142 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err) => {
143 dst.resize_withrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc vec/impl#[`Vec<T, A>`]resize_with().(header_len, Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default#::defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/impl#[u8][Default]default().);
144 return Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(err);
145 }
146 }
147 }
148 })
149 }
150
151 struct DecompressionCtxrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/DecompressionCtx#(RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#<apirust-analyzer cargo lzzzz 2.0.0 lz4f/api/::DecompressionContextrust-analyzer cargo lzzzz 2.0.0 lz4f/api/DecompressionContext#>);
152
153 impl DecompressionCtxrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/DecompressionCtx# {
154 fn newrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/impl#[DecompressionCtx]new().() -> Selfrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/impl#[DecompressionCtx] {
155 Selfrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/impl#[DecompressionCtx](RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/impl#[`RefCell<T>`]new().(apirust-analyzer cargo lzzzz 2.0.0 lz4f/api/::DecompressionContextrust-analyzer cargo lzzzz 2.0.0 lz4f/api/DecompressionContext#::newrust-analyzer cargo lzzzz 2.0.0 lz4f/api/impl#[DecompressionContext]new().().unwraprust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/impl#[`Result<T, E>`]unwrap().()))
156 }
157
158 fn withrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/impl#[DecompressionCtx]with().<F, R>(f: F) -> R
159 where
160 F: FnOncerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/function/FnOnce#(&RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#<apirust-analyzer cargo lzzzz 2.0.0 lz4f/api/::DecompressionContextrust-analyzer cargo lzzzz 2.0.0 lz4f/api/DecompressionContext#>) -> R,
161 {
162 DECOMPRESSION_CTXrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/DECOMPRESSION_CTX..withrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/local/impl#[`LocalKey<T>`]with().(|state| (f)(state))
163 }
164 }
165
166 impl Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref# for DecompressionCtxrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/DecompressionCtx# {
167 type Targetrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/impl#[DecompressionCtx][Deref]Target# = RefCellrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cell/RefCell#<apirust-analyzer cargo lzzzz 2.0.0 lz4f/api/::DecompressionContextrust-analyzer cargo lzzzz 2.0.0 lz4f/api/DecompressionContext#>;
168
169 fn derefrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/impl#[DecompressionCtx][Deref]deref().(&self) -> &Selfrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/impl#[DecompressionCtx][Deref]::Targetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref#Target# {
170 &self.0rust-analyzer cargo lzzzz 2.0.0 lz4f/frame/DecompressionCtx#0.
171 }
172 }
173
174 thread_localrust-analyzer cargo std https://github.com/rust-lang/rust/library/std thread/local/thread_local!!(static DECOMPRESSION_CTXrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/DECOMPRESSION_CTX.: DecompressionCtxrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/DecompressionCtx# = DecompressionCtxrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/DecompressionCtx#::newrust-analyzer cargo lzzzz 2.0.0 lz4f/frame/impl#[DecompressionCtx]new().());