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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0
#![allow(dead_code)]
//! LZ4 block-format compressor and decompressor.
//!
//! Implements the LZ4 block format specification:
//! - No frame header (raw block format)
//! - Sequence: token_byte + [extra_lit_len...] + literal_bytes + offset_le16 + [extra_match_len...]
//! - Token byte high nibble = literal length (15 = extend with 255-byte runs)
//! - Token byte low nibble = match length - 4 (min match 4; 15 = extend similarly)
//! - Final sequence has no offset/match fields
const HASH_TABLE_SIZE: usize = 1 << 16; // 65536 entries
const MIN_MATCH: usize = 4;
const MAX_OFFSET: usize = 65535;
/// Configuration for the LZ4 compressor.
#[derive(Debug, Clone)]
pub struct Lz4Config {
pub acceleration: i32,
pub block_size: usize,
}
impl Default for Lz4Config {
fn default() -> Self {
Self {
acceleration: 1,
block_size: 65536,
}
}
}
/// LZ4 compressor.
#[derive(Debug, Clone)]
pub struct Lz4Compressor {
pub config: Lz4Config,
}
impl Lz4Compressor {
pub fn new(config: Lz4Config) -> Self {
Self { config }
}
pub fn default_compressor() -> Self {
Self::new(Lz4Config::default())
}
}
/// Compute a 4-byte hash for LZ4 hash table.
#[inline]
fn lz4_hash(data: &[u8], pos: usize) -> usize {
if pos + 4 > data.len() {
return 0;
}
let v = u32::from_le_bytes([data[pos], data[pos + 1], data[pos + 2], data[pos + 3]]);
// Knuth's multiplicative hash
let h = v.wrapping_mul(0x9E3779B1u32);
(h >> 16) as usize % HASH_TABLE_SIZE
}
/// Find the length of the match between `data[a..]` and `data[b..]`.
#[inline]
fn match_length(data: &[u8], mut a: usize, mut b: usize) -> usize {
let limit = data.len();
let start = a;
while a < limit && b < limit && data[a] == data[b] {
a += 1;
b += 1;
}
a - start
}
/// Write LZ4 variable-length extension bytes (for lengths >= 15).
/// Writes 255-byte runs until remainder < 255, then the remainder.
fn write_var_len(out: &mut Vec<u8>, mut remainder: usize) {
while remainder >= 255 {
out.push(255);
remainder -= 255;
}
out.push(remainder as u8);
}
/// Compress `data` using the LZ4 block format.
pub fn lz4_compress(data: &[u8]) -> Vec<u8> {
if data.is_empty() {
// Empty block: emit a single token 0x00 (0 literals, 0 match) followed by nothing.
// Actually the spec says final sequence can be just 0 literals — emit the token byte.
return vec![0x00];
}
let mut out = Vec::with_capacity(lz4_compress_bound(data.len()));
// Hash table: maps hash → last position
let mut hash_table: Vec<u32> = vec![u32::MAX; HASH_TABLE_SIZE];
let mut pos: usize = 0;
// `literal_start` tracks where the pending literal run begins
let mut literal_start: usize = 0;
// We must leave the last 5 bytes as literals (LZ4 spec requirement: last match
// must end at least 12 bytes from end, but we use a simpler safe bound here).
let match_limit = if data.len() > 12 { data.len() - 12 } else { 0 };
while pos < data.len() {
// Try to find a match if we have at least MIN_MATCH bytes left and are before match_limit
if pos + MIN_MATCH <= data.len() && pos < match_limit {
let h = lz4_hash(data, pos);
let candidate = hash_table[h] as usize;
hash_table[h] = pos as u32;
if candidate != u32::MAX as usize
&& candidate < pos
&& pos - candidate <= MAX_OFFSET
&& candidate + MIN_MATCH <= data.len()
{
// Verify actual match
let match_len = match_length(data, pos, candidate);
if match_len >= MIN_MATCH {
// Emit sequence: literals [literal_start..pos] + match
let lit_len = pos - literal_start;
let ml_encoded = match_len - MIN_MATCH; // minimum 4 → 0
// Token byte
let lit_nibble = lit_len.min(15) as u8;
let match_nibble = ml_encoded.min(15) as u8;
let token = (lit_nibble << 4) | match_nibble;
out.push(token);
// Extra literal length bytes if needed
if lit_len >= 15 {
write_var_len(&mut out, lit_len - 15);
}
// Literal bytes
out.extend_from_slice(&data[literal_start..pos]);
// Match offset (LE16)
let offset = (pos - candidate) as u16;
out.extend_from_slice(&offset.to_le_bytes());
// Extra match length bytes if needed
if ml_encoded >= 15 {
write_var_len(&mut out, ml_encoded - 15);
}
// Advance past the match
// Update hash table entries within the match region
for i in 1..match_len {
if pos + i + MIN_MATCH <= data.len() {
let hi = lz4_hash(data, pos + i);
hash_table[hi] = (pos + i) as u32;
}
}
pos += match_len;
literal_start = pos;
continue;
}
}
} else if pos + MIN_MATCH <= data.len() {
// Still update hash table
let h = lz4_hash(data, pos);
hash_table[h] = pos as u32;
}
pos += 1;
}
// Final sequence: remaining literals, no match
let lit_len = data.len() - literal_start;
let lit_nibble = lit_len.min(15) as u8;
let token = lit_nibble << 4; // match nibble = 0, no match follows
out.push(token);
if lit_len >= 15 {
write_var_len(&mut out, lit_len - 15);
}
out.extend_from_slice(&data[literal_start..]);
// No offset/match bytes for the final sequence
out
}
/// Read variable-length extension from compressed stream.
/// Returns (total_length_with_base, new_position) where base has already been added.
fn read_var_len(data: &[u8], pos: usize, base: usize) -> Result<(usize, usize), String> {
let mut length = base;
let mut cur = pos;
loop {
if cur >= data.len() {
return Err("lz4: truncated variable-length extension".to_string());
}
let b = data[cur] as usize;
cur += 1;
length += b;
if b < 255 {
break;
}
}
Ok((length, cur))
}
/// Decompress data produced by [`lz4_compress`].
pub fn lz4_decompress(data: &[u8]) -> Result<Vec<u8>, String> {
if data.is_empty() {
return Err("lz4: empty input".to_string());
}
// Special case: single 0x00 token means empty output
if data == [0x00] {
return Ok(Vec::new());
}
let mut out: Vec<u8> = Vec::new();
let mut pos: usize = 0;
let len = data.len();
loop {
if pos >= len {
return Err("lz4: unexpected end of input".to_string());
}
let token = data[pos];
pos += 1;
// Decode literal length
let lit_nibble = (token >> 4) as usize;
let lit_len = if lit_nibble == 15 {
let (l, new_pos) = read_var_len(data, pos, 15)?;
pos = new_pos;
l
} else {
lit_nibble
};
// Copy literals
if pos + lit_len > len {
return Err("lz4: literal run overflows input".to_string());
}
out.extend_from_slice(&data[pos..pos + lit_len]);
pos += lit_len;
// Check if this is the final sequence (no match follows)
if pos >= len {
break;
}
// Decode match offset (LE16)
if pos + 2 > len {
return Err("lz4: truncated match offset".to_string());
}
let offset = u16::from_le_bytes([data[pos], data[pos + 1]]) as usize;
pos += 2;
if offset == 0 {
return Err("lz4: zero match offset".to_string());
}
if offset > out.len() {
return Err(format!(
"lz4: match offset {} exceeds output length {}",
offset,
out.len()
));
}
// Decode match length
let match_nibble = (token & 0x0F) as usize;
let match_len = if match_nibble == 15 {
let (ml, new_pos) = read_var_len(data, pos, 15 + MIN_MATCH)?;
pos = new_pos;
ml
} else {
match_nibble + MIN_MATCH
};
// Copy match (may overlap — must copy byte-by-byte)
let match_start = out.len() - offset;
for i in 0..match_len {
let b = out[match_start + i];
out.push(b);
}
}
Ok(out)
}
/// Return the worst-case compressed size for `input_len` bytes.
pub fn lz4_compress_bound(input_len: usize) -> usize {
input_len + input_len / 255 + 16
}
/// Return whether `data` could be a valid LZ4 block (any 1+ byte slice).
pub fn lz4_is_compressed(data: &[u8]) -> bool {
data.len() >= 4
}
/// Round-trip compress then decompress and verify equality.
pub fn lz4_roundtrip_ok(data: &[u8]) -> bool {
match lz4_decompress(&lz4_compress(data)) {
Ok(out) => out == data,
Err(_) => false,
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_default_config() {
let cfg = Lz4Config::default();
assert_eq!(cfg.acceleration, 1);
}
#[test]
fn test_compress_bound() {
assert!(lz4_compress_bound(100) >= 100);
}
#[test]
fn test_roundtrip_empty() {
assert!(lz4_roundtrip_ok(&[]));
}
#[test]
fn test_roundtrip_hello() {
assert!(lz4_roundtrip_ok(b"Hello, World!"));
}
#[test]
fn test_roundtrip_binary() {
let data: Vec<u8> = (0u8..=255).collect();
assert!(lz4_roundtrip_ok(&data));
}
#[test]
fn test_roundtrip_repetitive() {
let data: Vec<u8> = vec![b'A'; 1000];
assert!(lz4_roundtrip_ok(&data));
}
#[test]
fn test_compress_produces_smaller_than_input_for_repetitive_data() {
let data: Vec<u8> = vec![b'A'; 1000];
let compressed = lz4_compress(&data);
assert!(
compressed.len() < 100,
"Expected compressed size < 100, got {}",
compressed.len()
);
}
#[test]
fn test_decompress_too_short() {
assert!(lz4_decompress(&[]).is_err());
}
#[test]
fn test_is_compressed_false() {
assert!(!lz4_is_compressed(&[]));
}
#[test]
fn test_is_compressed_true() {
assert!(lz4_is_compressed(&[0, 0, 0, 0]));
}
#[test]
fn test_compressor_new() {
let c = Lz4Compressor::default_compressor();
assert_eq!(c.config.acceleration, 1);
}
#[test]
fn test_roundtrip_longer_text() {
let text = b"the quick brown fox jumps over the lazy dog. the quick brown fox!";
assert!(lz4_roundtrip_ok(text));
}
#[test]
fn test_roundtrip_all_zeros() {
let data = vec![0u8; 512];
assert!(lz4_roundtrip_ok(&data));
}
#[test]
fn test_roundtrip_single_byte() {
assert!(lz4_roundtrip_ok(b"x"));
}
}