1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
use crate::;
use ExtState;
use cmp;
/// Calculates the maximum size of the compressed output.
///
/// If `original_size` is too large to compress, this returns `0`.
pub const
/// Performs LZ4 block compression.
///
/// Ensure that the destination slice has enough capacity.
/// If `dst.len()` is smaller than `lz4::max_compressed_size(src.len())`,
/// this function may fail.
///
/// Returns the number of bytes written into the destination buffer.
///
/// # Example
///
/// ```
/// use lzzzz::lz4;
///
/// let data = b"The quick brown fox jumps over the lazy dog.";
/// let mut buf = [0u8; 256];
///
/// // The slice should have enough capacity.
/// assert!(buf.len() >= lz4::max_compressed_size(data.len()));
///
/// let len = lz4::compress(data, &mut buf, lz4::ACC_LEVEL_DEFAULT)?;
/// let compressed = &buf[..len];
///
/// # let mut buf = [0u8; 256];
/// # let len = lz4::decompress(compressed, &mut buf[..data.len()])?;
/// # assert_eq!(&buf[..len], &data[..]);
/// # Ok::<(), std::io::Error>(())
/// ```
/// Appends compressed data to `Vec<u8>`.
///
/// Returns the number of bytes appended to the given `Vec<u8>`.
///
/// # Example
///
/// ```
/// use lzzzz::lz4;
///
/// let data = b"The quick brown fox jumps over the lazy dog.";
/// let mut buf = Vec::new();
///
/// lz4::compress_to_vec(data, &mut buf, lz4::ACC_LEVEL_DEFAULT)?;
/// # let compressed = &buf;
/// # let mut buf = [0u8; 256];
/// # let len = lz4::decompress(compressed, &mut buf[..data.len()])?;
/// # assert_eq!(&buf[..len], &data[..]);
/// # Ok::<(), std::io::Error>(())
/// ```
/// Compress data to fill `dst`.
///
/// This function either compresses the entire `src` buffer into `dst` if it's
/// large enough, or will fill `dst` with as much data as possible from `src`.
///
/// Returns a pair `(read, wrote)` giving the number of bytes read from `src`
/// and the number of bytes written to `dst`.
///
/// # Example
///
/// ```
/// use lzzzz::lz4;
///
/// let data = b"The quick brown fox jumps over the lazy dog.";
/// let mut buf = [0u8; 256];
///
/// // This slice should have enough capacity.
/// assert!(buf.len() >= lz4::max_compressed_size(data.len()));
///
/// let (read, wrote) = lz4::compress_fill(data, &mut buf)?;
/// assert_eq!(read, data.len());
/// let compressed = &buf[..wrote];
///
/// # let mut buf = [0u8; 256];
/// # let len = lz4::decompress(compressed, &mut buf[..data.len()])?;
/// # assert_eq!(&buf[..len], &data[..]);
///
/// // This slice doesn't have enough capacity, but we can fill it.
/// let mut smallbuf = [0u8; 32];
/// assert!(smallbuf.len() < lz4::max_compressed_size(data.len()));
///
/// let (read, wrote) = lz4::compress_fill(data, &mut smallbuf)?;
/// assert_eq!(wrote, smallbuf.len());
/// let remaining_data = &data[read..];
///
/// # let mut buf = [0u8; 256];
/// # let len = lz4::decompress(&smallbuf, &mut buf)?;
/// # assert_eq!(&buf[..len], &data[..read]);
/// # Ok::<(), std::io::Error>(())
/// ```
/// Decompresses an LZ4 block.
///
/// The length of the destination slice must be equal to the original data length.
///
/// Returns the number of bytes written into the destination buffer.
///
/// # Example
///
/// ```
/// use lzzzz::lz4;
///
/// const ORIGINAL_SIZE: usize = 44;
/// const COMPRESSED_DATA: &str =
/// "8B1UaGUgcXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nLg==";
///
/// let data = base64::decode(COMPRESSED_DATA).unwrap();
/// let mut buf = [0u8; ORIGINAL_SIZE];
///
/// lz4::decompress(&data[..], &mut buf[..])?;
///
/// assert_eq!(
/// &buf[..],
/// &b"The quick brown fox jumps over the lazy dog."[..]
/// );
/// # Ok::<(), std::io::Error>(())
/// ```
/// Decompresses an LZ4 block until the destination slice fills up.
///
/// Returns the number of bytes written into the destination buffer.
///
/// # Example
///
/// ```
/// use lzzzz::lz4;
///
/// const ORIGINAL_SIZE: usize = 44;
/// const COMPRESSED_DATA: &str =
/// "8B1UaGUgcXVpY2sgYnJvd24gZm94IGp1bXBzIG92ZXIgdGhlIGxhenkgZG9nLg==";
///
/// let data = base64::decode(COMPRESSED_DATA).unwrap();
/// let mut buf = [0u8; 24];
///
/// lz4::decompress_partial(&data[..], &mut buf[..], ORIGINAL_SIZE)?;
///
/// assert_eq!(&buf[..], &b"The quick brown fox jump"[..]);
/// # Ok::<(), std::io::Error>(())
/// ```
/// Decompresses an LZ4 block with a dictionary.
///
/// Returns the number of bytes written into the destination buffer.
///
/// # Example
///
/// ```
/// use lzzzz::lz4;
///
/// const ORIGINAL_SIZE: usize = 44;
/// const COMPRESSED_DATA: &str = "DywAFFAgZG9nLg==";
/// const DICT_DATA: &[u8] = b"The quick brown fox jumps over the lazy cat.";
///
/// let data = base64::decode(COMPRESSED_DATA).unwrap();
/// let mut buf = [0u8; ORIGINAL_SIZE];
///
/// lz4::decompress_with_dict(&data[..], &mut buf[..], DICT_DATA)?;
///
/// assert_eq!(
/// &buf[..],
/// &b"The quick brown fox jumps over the lazy dog."[..]
/// );
/// # Ok::<(), std::io::Error>(())
/// ```
/// Decompresses an LZ4 block with a dictionary until the destination slice fills up.
///
/// Returns the number of bytes written into the destination buffer.
///
/// # Example
///
/// ```
/// use lzzzz::lz4;
///
/// const ORIGINAL_SIZE: usize = 44;
/// const COMPRESSED_DATA: &str = "DywAFFAgZG9nLg==";
/// const DICT_DATA: &[u8] = b"The quick brown fox jumps over the lazy cat.";
///
/// let data = base64::decode(COMPRESSED_DATA).unwrap();
/// let mut buf = [0u8; 24];
///
/// lz4::decompress_partial_with_dict(&data[..], &mut buf[..], ORIGINAL_SIZE, DICT_DATA)?;
///
/// assert_eq!(
/// &buf[..],
/// &b"The quick brown fox jump"[..]
/// );
/// # Ok::<(), std::io::Error>(())
/// ```