cachekit-core 0.2.0

LZ4 compression, xxHash3 integrity, AES-256-GCM encryption for byte payloads
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
//! C FFI wrappers for ByteStorage compress/decompress operations.
//!
//! Provides C-compatible functions for compression and decompression with:
//! - Panic safety via catch_unwind
//! - Comprehensive null pointer checks
//! - Buffer overflow protection
//! - Clear error reporting via CachekitError enum

use crate::byte_storage::{ByteStorage, ByteStorageError};
use crate::ffi::error::CachekitError;
use std::panic::catch_unwind;
use std::slice;

/// Compress input data using LZ4 and xxHash3-64 checksum.
///
/// # Parameters
/// - `input`: Pointer to input data buffer (must not be null)
/// - `input_len`: Length of input data in bytes
/// - `output`: Pointer to output buffer (must not be null)
/// - `output_len`: Pointer to output buffer size (must not be null)
///   On input: size of output buffer
///   On output: actual size of compressed data (or required size if BufferTooSmall)
///
/// # Returns
/// - `CachekitError::Ok` (0) on success
/// - `CachekitError::NullPointer` (9) if any pointer is null
/// - `CachekitError::InvalidInput` (1) if input data is invalid or too large
/// - `CachekitError::BufferTooSmall` (8) if output buffer is too small
///   (output_len will be set to required size)
///
/// # Safety
/// Caller must ensure:
/// - `input` points to valid memory of at least `input_len` bytes
/// - `output` points to valid writable memory of at least `*output_len` bytes
/// - `output_len` points to valid writable memory
/// - Pointers remain valid for duration of call
///
/// Function is panic-safe and will never unwind across FFI boundary.
#[no_mangle]
pub unsafe extern "C" fn cachekit_compress(
    input: *const u8,
    input_len: usize,
    output: *mut u8,
    output_len: *mut usize,
) -> CachekitError {
    // Wrap entire function in catch_unwind for panic safety
    let result = catch_unwind(|| {
        // SAFETY: Null pointer checks before any dereference
        if input.is_null() {
            return CachekitError::NullPointer;
        }
        if output.is_null() {
            return CachekitError::NullPointer;
        }
        if output_len.is_null() {
            return CachekitError::NullPointer;
        }

        // SAFETY: We've verified output_len is not null
        let available_size = unsafe { *output_len };

        // SAFETY: We've verified input is not null and input_len defines the valid range
        let input_slice = unsafe { slice::from_raw_parts(input, input_len) };

        // Create ByteStorage instance and compress
        let storage = ByteStorage::new(None);
        let compressed = match storage.store(input_slice, None) {
            Ok(data) => data,
            Err(e) => {
                // Map ByteStorageError to CachekitError (using specific error codes)
                return match e {
                    ByteStorageError::InputTooLarge => CachekitError::InputTooLarge,
                    ByteStorageError::SerializationFailed(_) => CachekitError::InvalidInput,
                    _ => CachekitError::InvalidInput,
                };
            }
        };

        // Check if output buffer is large enough
        if compressed.len() > available_size {
            // SAFETY: We've verified output_len is not null
            unsafe {
                *output_len = compressed.len();
            }
            return CachekitError::BufferTooSmall;
        }

        // SAFETY: We've verified:
        // - output is not null
        // - available_size (from *output_len) is the valid size of output buffer
        // - compressed.len() <= available_size (checked above)
        unsafe {
            std::ptr::copy_nonoverlapping(compressed.as_ptr(), output, compressed.len());
            *output_len = compressed.len();
        }

        CachekitError::Ok
    });

    // If panic occurred, return error
    result.unwrap_or(CachekitError::InvalidInput)
}

/// Decompress data previously compressed with cachekit_compress.
///
/// # Parameters
/// - `input`: Pointer to compressed data buffer (must not be null)
/// - `input_len`: Length of compressed data in bytes
/// - `output`: Pointer to output buffer (must not be null)
/// - `output_len`: Pointer to output buffer size (must not be null)
///   On input: size of output buffer
///   On output: actual size of decompressed data (or required size if BufferTooSmall)
///
/// # Returns
/// - `CachekitError::Ok` (0) on success
/// - `CachekitError::NullPointer` (9) if any pointer is null
/// - `CachekitError::InvalidInput` (1) if input data is invalid or corrupted
/// - `CachekitError::ChecksumMismatch` (2) if xxHash3-64 checksum verification fails
/// - `CachekitError::DecompressionFailed` (3) if LZ4 decompression fails
/// - `CachekitError::BufferTooSmall` (8) if output buffer is too small
///   (output_len will be set to required size)
///
/// # Empty Data Handling
/// Decompressing data that was originally empty (0-byte input to `cachekit_compress`)
/// is valid and will return `CachekitError::Ok` with `*output_len = 0`. C callers
/// should NOT interpret a zero-length output as failure - check the return code.
///
/// # Safety
/// Caller must ensure:
/// - `input` points to valid memory of at least `input_len` bytes
/// - `output` points to valid writable memory of at least `*output_len` bytes
/// - `output_len` points to valid writable memory
/// - Pointers remain valid for duration of call
///
/// Function is panic-safe and will never unwind across FFI boundary.
#[no_mangle]
pub unsafe extern "C" fn cachekit_decompress(
    input: *const u8,
    input_len: usize,
    output: *mut u8,
    output_len: *mut usize,
) -> CachekitError {
    // Wrap entire function in catch_unwind for panic safety
    let result = catch_unwind(|| {
        // SAFETY: Null pointer checks before any dereference
        if input.is_null() {
            return CachekitError::NullPointer;
        }
        if output.is_null() {
            return CachekitError::NullPointer;
        }
        if output_len.is_null() {
            return CachekitError::NullPointer;
        }

        // SAFETY: We've verified output_len is not null
        let available_size = unsafe { *output_len };

        // SAFETY: We've verified input is not null and input_len defines the valid range
        let input_slice = unsafe { slice::from_raw_parts(input, input_len) };

        // Create ByteStorage instance and decompress
        let storage = ByteStorage::new(None);
        let (decompressed, _format) = match storage.retrieve(input_slice) {
            Ok(data) => data,
            Err(e) => {
                // Map ByteStorageError to CachekitError (using specific error codes per design.md)
                return match e {
                    ByteStorageError::ChecksumMismatch => CachekitError::ChecksumMismatch,
                    ByteStorageError::DecompressionFailed => CachekitError::DecompressionFailed,
                    ByteStorageError::DecompressionBomb => CachekitError::DecompressionBomb,
                    ByteStorageError::InputTooLarge => CachekitError::InputTooLarge,
                    ByteStorageError::SizeValidationFailed => CachekitError::SizeValidationFailed,
                    ByteStorageError::DeserializationFailed(_) => CachekitError::InvalidInput,
                    _ => CachekitError::InvalidInput,
                };
            }
        };

        // Check if output buffer is large enough
        if decompressed.len() > available_size {
            // SAFETY: We've verified output_len is not null
            unsafe {
                *output_len = decompressed.len();
            }
            return CachekitError::BufferTooSmall;
        }

        // SAFETY: We've verified:
        // - output is not null
        // - available_size (from *output_len) is the valid size of output buffer
        // - decompressed.len() <= available_size (checked above)
        unsafe {
            std::ptr::copy_nonoverlapping(decompressed.as_ptr(), output, decompressed.len());
            *output_len = decompressed.len();
        }

        CachekitError::Ok
    });

    // If panic occurred, return error
    result.unwrap_or(CachekitError::DecompressionFailed)
}

/// Estimate maximum compressed size for input data.
///
/// Returns a conservative upper bound for the output buffer size needed
/// for cachekit_compress with the given input length. The actual compressed
/// size will typically be smaller.
///
/// # Parameters
/// - `input_len`: Length of input data in bytes
///
/// # Returns
/// Maximum possible compressed size in bytes (includes overhead for:
/// - LZ4 compression worst case (incompressible data)
/// - MessagePack envelope serialization
/// - xxHash3-64 checksum (8 bytes)
/// - Format string
///
/// # Safety
/// This is a pure computation with no memory access. Always safe to call.
#[no_mangle]
pub extern "C" fn cachekit_compressed_bound(input_len: usize) -> usize {
    // LZ4 worst case: input_len + (input_len / 255) + 16
    // See: https://github.com/lz4/lz4/blob/dev/lib/lz4.h#L166
    // Use saturating arithmetic to prevent overflow on 32-bit systems
    let lz4_bound = input_len.saturating_add(input_len / 255).saturating_add(16);

    // MessagePack envelope overhead:
    // - Map header: ~10 bytes
    // - "compressed_data" key: ~16 bytes
    // - Bin header for data: ~5 bytes
    // - "checksum" key + 8-byte array: ~16 bytes
    // - "original_size" key + u32: ~20 bytes
    // - "format" key + string: ~20 bytes
    let msgpack_overhead = 120;

    lz4_bound.saturating_add(msgpack_overhead)
}

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

    #[test]
    fn test_compress_decompress_roundtrip() {
        let input = b"Hello, World! This is a test of the FFI compression interface.";
        let input_len = input.len();

        // Allocate output buffer for compression
        let mut compressed = vec![0u8; cachekit_compressed_bound(input_len)];
        let mut compressed_len = compressed.len();

        // Compress
        let result = unsafe {
            cachekit_compress(
                input.as_ptr(),
                input_len,
                compressed.as_mut_ptr(),
                &mut compressed_len,
            )
        };
        assert_eq!(result, CachekitError::Ok);
        assert!(compressed_len > 0);
        assert!(compressed_len < compressed.len());

        // Allocate output buffer for decompression
        let mut decompressed = vec![0u8; input_len + 1000]; // Extra space
        let mut decompressed_len = decompressed.len();

        // Decompress
        let result = unsafe {
            cachekit_decompress(
                compressed.as_ptr(),
                compressed_len,
                decompressed.as_mut_ptr(),
                &mut decompressed_len,
            )
        };
        assert_eq!(result, CachekitError::Ok);
        assert_eq!(decompressed_len, input_len);
        assert_eq!(&decompressed[..decompressed_len], input);
    }

    #[test]
    fn test_null_pointer_checks() {
        let input = b"test";
        let mut output = vec![0u8; 1024];
        let mut output_len = output.len();

        // Null input
        assert_eq!(
            unsafe { cachekit_compress(std::ptr::null(), 4, output.as_mut_ptr(), &mut output_len) },
            CachekitError::NullPointer
        );

        // Null output
        assert_eq!(
            unsafe { cachekit_compress(input.as_ptr(), 4, std::ptr::null_mut(), &mut output_len) },
            CachekitError::NullPointer
        );

        // Null output_len
        assert_eq!(
            unsafe {
                cachekit_compress(input.as_ptr(), 4, output.as_mut_ptr(), std::ptr::null_mut())
            },
            CachekitError::NullPointer
        );
    }

    #[test]
    fn test_buffer_too_small() {
        let input = b"Hello, World!";
        let input_len = input.len();

        // Try to compress with tiny buffer
        let mut compressed = vec![0u8; 10]; // Way too small
        let mut compressed_len = compressed.len();

        let result = unsafe {
            cachekit_compress(
                input.as_ptr(),
                input_len,
                compressed.as_mut_ptr(),
                &mut compressed_len,
            )
        };
        assert_eq!(result, CachekitError::BufferTooSmall);
        // compressed_len should now contain required size
        assert!(compressed_len > 10);
    }

    #[test]
    fn test_checksum_mismatch() {
        let input = b"Hello, World!";
        let input_len = input.len();

        // Compress
        let mut compressed = vec![0u8; cachekit_compressed_bound(input_len)];
        let mut compressed_len = compressed.len();
        let result = unsafe {
            cachekit_compress(
                input.as_ptr(),
                input_len,
                compressed.as_mut_ptr(),
                &mut compressed_len,
            )
        };
        assert_eq!(result, CachekitError::Ok);

        // Corrupt the compressed data (flip a byte in the middle)
        if compressed_len > 10 {
            compressed[compressed_len / 2] ^= 0xFF;
        }

        // Try to decompress corrupted data
        let mut decompressed = vec![0u8; input_len + 1000];
        let mut decompressed_len = decompressed.len();
        let result = unsafe {
            cachekit_decompress(
                compressed.as_ptr(),
                compressed_len,
                decompressed.as_mut_ptr(),
                &mut decompressed_len,
            )
        };

        // Should fail with checksum or decompression error
        assert!(result != CachekitError::Ok);
    }

    #[test]
    fn test_compressed_bound_adequate() {
        // Test that compressed_bound provides enough space
        for size in [0, 1, 10, 100, 1000, 10000] {
            let bound = cachekit_compressed_bound(size);
            assert!(bound > size); // Should be larger than input
            assert!(bound < size * 2 + 200); // But not absurdly large
        }
    }

    #[test]
    fn test_empty_data_roundtrip() {
        // Empty data is valid input and should roundtrip correctly
        // This documents the expected FFI behavior for C callers
        let input: &[u8] = b"";
        let input_len = input.len();
        assert_eq!(input_len, 0);

        // Allocate output buffer for compression
        let mut compressed = vec![0u8; cachekit_compressed_bound(input_len)];
        let mut compressed_len = compressed.len();

        // Compress empty data
        let result = unsafe {
            cachekit_compress(
                input.as_ptr(),
                input_len,
                compressed.as_mut_ptr(),
                &mut compressed_len,
            )
        };
        assert_eq!(result, CachekitError::Ok);
        assert!(compressed_len > 0); // Envelope has overhead even for empty data

        // Decompress back to empty
        let mut decompressed = vec![0u8; 100]; // Extra space
        let mut decompressed_len = decompressed.len();

        let result = unsafe {
            cachekit_decompress(
                compressed.as_ptr(),
                compressed_len,
                decompressed.as_mut_ptr(),
                &mut decompressed_len,
            )
        };
        // KEY: CachekitError::Ok with output_len=0 is VALID, not an error
        assert_eq!(result, CachekitError::Ok);
        assert_eq!(decompressed_len, 0); // Empty data decompresses to empty
    }
}