oxihuman-core 0.2.1

Core data structures, algorithms, and asset management for OxiHuman
Documentation
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// Copyright (C) 2026 COOLJAPAN OU (Team KitaSan)
// SPDX-License-Identifier: Apache-2.0
#![allow(dead_code)]

//! Snappy block-format compressor and decompressor.
//!
//! Wire format:
//! - Varint-encoded (LEB128) uncompressed length
//! - Followed by elements:
//!   - Literal  (tag & 0x03 == 0x00): upper 6 bits encodes length-1 if ≤ 59, or 59+extra_bytes
//!   - Copy-1   (tag & 0x03 == 0x01): 2-byte copy, offset ≤ 2047, length 4..11
//!   - Copy-2   (tag & 0x03 == 0x02): 3-byte copy, offset ≤ 65535, length 1..64
//!   - Copy-4   (tag & 0x03 == 0x03): 5-byte copy (rarely used, not emitted here)

const HASH_TABLE_BITS: usize = 15;
const HASH_TABLE_SIZE: usize = 1 << HASH_TABLE_BITS; // 32768
const MIN_MATCH: usize = 4;

/// Configuration for the Snappy compressor.
#[derive(Debug, Clone, Default)]
pub struct SnappyConfig {
    pub verify_checksum: bool,
}

/// Snappy compressor.
#[derive(Debug, Clone)]
pub struct SnappyCompressor {
    pub config: SnappyConfig,
}

impl SnappyCompressor {
    pub fn new(config: SnappyConfig) -> Self {
        Self { config }
    }

    pub fn default_compressor() -> Self {
        Self::new(SnappyConfig {
            verify_checksum: false,
        })
    }
}

/// Encode a varint (LEB128) into `out`.
fn write_varint(out: &mut Vec<u8>, mut value: u64) {
    loop {
        let mut byte = (value & 0x7F) as u8;
        value >>= 7;
        if value != 0 {
            byte |= 0x80;
        }
        out.push(byte);
        if value == 0 {
            break;
        }
    }
}

/// Decode a varint (LEB128) from `data` starting at `pos`.
/// Returns (value, new_pos).
fn read_varint(data: &[u8], mut pos: usize) -> Result<(u64, usize), String> {
    let mut value: u64 = 0;
    let mut shift: u32 = 0;
    loop {
        if pos >= data.len() {
            return Err("snappy: truncated varint".to_string());
        }
        let byte = data[pos];
        pos += 1;
        if shift >= 63 && (byte & 0x7F) > 1 {
            return Err("snappy: varint overflow".to_string());
        }
        value |= ((byte & 0x7F) as u64) << shift;
        shift += 7;
        if byte & 0x80 == 0 {
            break;
        }
        if shift > 63 {
            return Err("snappy: varint too long".to_string());
        }
    }
    Ok((value, pos))
}

/// Snappy-specific 4-byte hash.
#[inline]
fn snappy_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]]);
    let h = v.wrapping_mul(0x1E35A7BD);
    (h >> (32 - HASH_TABLE_BITS)) as usize
}

/// Find match length between two positions.
#[inline]
fn match_len(data: &[u8], mut a: usize, mut b: usize, limit: usize) -> usize {
    let start = a;
    while a < limit && b < limit && data[a] == data[b] {
        a += 1;
        b += 1;
    }
    a - start
}

/// Emit a Snappy literal element.
fn emit_literal(out: &mut Vec<u8>, literals: &[u8]) {
    if literals.is_empty() {
        return;
    }
    let n = literals.len();
    if n <= 60 {
        // Upper 6 bits = (n-1), lower 2 bits = 0x00
        out.push(((n - 1) as u8) << 2);
    } else if n <= 256 {
        // 60 extra bytes → value 59 in upper 6 bits (meaning 1 extra byte follows)
        out.push(0xF0); // (59 << 2) | 0x00 = 236 = 0xEC... wait:
                        // Actually: upper 6 bits = 60 means 1 extra length byte follows (0-indexed: 59+1)
                        // snappy spec: if upper 6 bits = 60, one extra byte; 61 → two bytes; etc.
                        // Correct: upper 6 bits = 60 → tag|0x00, n-1 in 1 byte
                        // Let's redo: tag byte = ((60) << 2) | 0x00 = 240 = 0xF0
                        // then n-1 as 1 LE byte
        out.pop(); // remove the premature push
        out.push(60u8 << 2); // = 0xF0
        out.push((n - 1) as u8);
    } else if n <= 65536 {
        // upper 6 bits = 61 → 2 extra bytes
        out.push(61u8 << 2); // = 0xF4
        let nm1 = (n - 1) as u16;
        out.extend_from_slice(&nm1.to_le_bytes());
    } else {
        // upper 6 bits = 62 → 3 extra bytes (covers up to 16MB)
        out.push(62u8 << 2); // = 0xF8
        let nm1 = (n - 1) as u32;
        out.extend_from_slice(&nm1.to_le_bytes()[..3]);
    }
    out.extend_from_slice(literals);
}

/// Emit a Snappy copy element.
fn emit_copy(out: &mut Vec<u8>, offset: usize, length: usize) {
    // Try Copy-1 (offset ≤ 2047, length 4..11)
    if offset < 2048 && (4..=11).contains(&length) {
        // tag: lower 2 bits = 01, bits 2..4 = offset[8..10], bits 5..7 = length-4
        let len_bits = ((length - 4) as u8) << 2; // bits [4:2]
        let off_high = ((offset >> 8) & 0x07) as u8; // bits [2:0] of offset high
        let tag = 0x01 | len_bits | (off_high << 5);
        out.push(tag);
        out.push((offset & 0xFF) as u8);
        return;
    }

    // Use Copy-2 (offset ≤ 65535, length 1..64)
    // Emit in chunks of up to 64 if length > 64
    let remaining = length;
    let off = offset;
    if remaining > 0 {
        let chunk = remaining.min(64);
        // tag: lower 2 bits = 10, upper 6 bits = chunk - 1
        let tag = (((chunk - 1) as u8) << 2) | 0x02;
        out.push(tag);
        out.extend_from_slice(&(off as u16).to_le_bytes());
        // subsequent copies use offset relative to where we are in output,
        // but for simplicity we only do one chunk since offset stays fixed
        // The offset remains the same for all chunks (pointing back to original match)
        // Actually offset grows by chunk each iteration, but since we already output
        // the literals, the pattern repeats — keep same offset
        let _ = off; // suppress warning; off unchanged for repeated pattern
    }
}

/// Compress `data` using the Snappy block format.
pub fn snappy_compress(data: &[u8]) -> Vec<u8> {
    let mut out = Vec::with_capacity(snappy_max_compressed_length(data.len()));

    // Write uncompressed length as varint
    write_varint(&mut out, data.len() as u64);

    if data.is_empty() {
        return out;
    }

    let mut hash_table: Vec<u32> = vec![u32::MAX; HASH_TABLE_SIZE];

    let mut pos: usize = 0;
    let mut lit_start: usize = 0;

    let match_limit = if data.len() > 15 { data.len() - 15 } else { 0 };

    while pos < data.len() {
        if pos + MIN_MATCH <= data.len() && pos < match_limit {
            let h = snappy_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 <= 65535
                && candidate + MIN_MATCH <= data.len()
            {
                let ml = match_len(data, pos, candidate, data.len());
                if ml >= MIN_MATCH {
                    // Flush pending literals
                    if pos > lit_start {
                        emit_literal(&mut out, &data[lit_start..pos]);
                    }

                    let offset = pos - candidate;
                    // Emit copies, possibly in multiple chunks for long matches
                    let mut emitted = 0;
                    while emitted < ml {
                        let chunk = (ml - emitted).min(64);
                        emit_copy(&mut out, offset, chunk);
                        emitted += chunk;
                    }

                    // Update hash table for positions inside the match
                    for i in 1..ml {
                        if pos + i + MIN_MATCH <= data.len() {
                            let hi = snappy_hash(data, pos + i);
                            hash_table[hi] = (pos + i) as u32;
                        }
                    }
                    pos += ml;
                    lit_start = pos;
                    continue;
                }
            }
        } else if pos + MIN_MATCH <= data.len() {
            let h = snappy_hash(data, pos);
            hash_table[h] = pos as u32;
        }

        pos += 1;
    }

    // Flush remaining literals
    if lit_start < data.len() {
        emit_literal(&mut out, &data[lit_start..]);
    }

    out
}

/// Decompress data produced by [`snappy_compress`].
pub fn snappy_decompress(data: &[u8]) -> Result<Vec<u8>, String> {
    if data.is_empty() {
        return Err("snappy: empty input".to_string());
    }

    let (uncompressed_len, mut pos) = read_varint(data, 0)?;
    let uncompressed_len = uncompressed_len as usize;

    let mut out: Vec<u8> = Vec::with_capacity(uncompressed_len);

    while pos < data.len() {
        let tag = data[pos];
        let element_type = tag & 0x03;
        pos += 1;

        match element_type {
            0x00 => {
                // Literal
                let upper6 = (tag >> 2) as usize;
                let lit_len = if upper6 < 60 {
                    upper6 + 1
                } else {
                    let extra_bytes = upper6 - 59; // 1..=4
                    if pos + extra_bytes > data.len() {
                        return Err("snappy: truncated literal length".to_string());
                    }
                    let mut n: usize = 0;
                    for i in 0..extra_bytes {
                        n |= (data[pos + i] as usize) << (8 * i);
                    }
                    pos += extra_bytes;
                    n + 1
                };
                if pos + lit_len > data.len() {
                    return Err("snappy: literal overflows input".to_string());
                }
                out.extend_from_slice(&data[pos..pos + lit_len]);
                pos += lit_len;
            }
            0x01 => {
                // Copy-1: 2-byte element
                if pos >= data.len() {
                    return Err("snappy: truncated copy-1".to_string());
                }
                let off_low = data[pos] as usize;
                pos += 1;
                let len = ((tag >> 2) & 0x07) as usize + 4;
                let off_high = ((tag >> 5) & 0x07) as usize;
                let offset = (off_high << 8) | off_low;
                if offset == 0 || offset > out.len() {
                    return Err(format!(
                        "snappy: copy-1 invalid offset {} (output len {})",
                        offset,
                        out.len()
                    ));
                }
                let match_start = out.len() - offset;
                for i in 0..len {
                    let b = out[match_start + i];
                    out.push(b);
                }
            }
            0x02 => {
                // Copy-2: 3-byte element
                if pos + 2 > data.len() {
                    return Err("snappy: truncated copy-2".to_string());
                }
                let offset = u16::from_le_bytes([data[pos], data[pos + 1]]) as usize;
                pos += 2;
                let len = ((tag >> 2) as usize) + 1;
                if offset == 0 || offset > out.len() {
                    return Err(format!(
                        "snappy: copy-2 invalid offset {} (output len {})",
                        offset,
                        out.len()
                    ));
                }
                let match_start = out.len() - offset;
                for i in 0..len {
                    let b = out[match_start + i];
                    out.push(b);
                }
            }
            0x03 => {
                // Copy-4: 5-byte element (not emitted by this encoder, but handle for completeness)
                if pos + 4 > data.len() {
                    return Err("snappy: truncated copy-4".to_string());
                }
                let offset =
                    u32::from_le_bytes([data[pos], data[pos + 1], data[pos + 2], data[pos + 3]])
                        as usize;
                pos += 4;
                let len = ((tag >> 2) as usize) + 1;
                if offset == 0 || offset > out.len() {
                    return Err(format!(
                        "snappy: copy-4 invalid offset {} (output len {})",
                        offset,
                        out.len()
                    ));
                }
                let match_start = out.len() - offset;
                for i in 0..len {
                    let b = out[match_start + i];
                    out.push(b);
                }
            }
            _ => unreachable!(),
        }
    }

    if out.len() != uncompressed_len {
        return Err(format!(
            "snappy: decompressed {} bytes, expected {}",
            out.len(),
            uncompressed_len
        ));
    }

    Ok(out)
}

/// Return the maximum output size for a given input length.
pub fn snappy_max_compressed_length(input_len: usize) -> usize {
    32 + input_len + input_len / 6
}

/// Return whether a byte slice could be a valid Snappy frame (has at least a varint).
pub fn snappy_validate_compressed_buffer(data: &[u8]) -> bool {
    data.len() >= 4
}

/// Verify round-trip integrity.
pub fn snappy_roundtrip_ok(data: &[u8]) -> bool {
    snappy_decompress(&snappy_compress(data))
        .map(|d| d == data)
        .unwrap_or(false)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_default_config() {
        let c = SnappyCompressor::default_compressor();
        assert!(!c.config.verify_checksum);
    }

    #[test]
    fn test_compress_empty() {
        let out = snappy_compress(&[]);
        // Just the varint 0
        assert!(!out.is_empty());
        assert!(snappy_roundtrip_ok(&[]));
    }

    #[test]
    fn test_roundtrip_ascii() {
        assert!(snappy_roundtrip_ok(b"snappy test string"));
    }

    #[test]
    fn test_roundtrip_binary() {
        let data: Vec<u8> = (0u8..=255).collect();
        assert!(snappy_roundtrip_ok(&data));
    }

    #[test]
    fn test_decompress_too_short() {
        assert!(snappy_decompress(&[]).is_err());
    }

    #[test]
    fn test_max_compressed_length() {
        assert!(snappy_max_compressed_length(100) > 100);
    }

    #[test]
    fn test_validate_valid() {
        assert!(snappy_validate_compressed_buffer(&[0, 0, 0, 0]));
    }

    #[test]
    fn test_validate_too_short() {
        assert!(!snappy_validate_compressed_buffer(&[0, 0, 0]));
    }

    #[test]
    fn test_new_config() {
        let c = SnappyCompressor::new(SnappyConfig {
            verify_checksum: true,
        });
        assert!(c.config.verify_checksum);
    }

    #[test]
    fn test_compress_repetitive_yields_smaller() {
        let data: Vec<u8> = vec![b'A'; 1000];
        let compressed = snappy_compress(&data);
        assert!(
            compressed.len() < 100,
            "Expected < 100 bytes, got {}",
            compressed.len()
        );
    }

    #[test]
    fn test_roundtrip_repetitive() {
        let data: Vec<u8> = vec![b'X'; 1000];
        assert!(snappy_roundtrip_ok(&data));
    }

    #[test]
    fn test_roundtrip_single_byte() {
        assert!(snappy_roundtrip_ok(b"z"));
    }

    #[test]
    fn test_roundtrip_longer_text() {
        let text = b"the quick brown fox jumps over the lazy dog. the quick brown fox!";
        assert!(snappy_roundtrip_ok(text));
    }
}