oodle 0.2.0

Oodle wrapper for rust
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
use libloading::Library;
use std::ffi::{OsStr, c_void};
use std::fmt;

// ---------------------------------------------------------------------------
// Enums
// ---------------------------------------------------------------------------

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(i32)]
pub enum OodleFuzzSafe {
    No = 0,
    Yes = 1,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(i32)]
pub enum OodleCheckCrc {
    No = 0,
    Yes = 1,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(i32)]
pub enum OodleVerbosity {
    None = 0,
    Minimal = 1,
    Some = 2,
    Lots = 3,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(i32)]
pub enum OodleDecodeThreadPhase {
    Phase1 = 1,
    Phase2 = 2,
    All = 3,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(i32)]
pub enum OodleCompressor {
    Invalid = -1,
    None = 3,
    Kraken = 8,
    Mermaid = 9,
    Selkie = 11,
    Hydra = 12,
    Leviathan = 13,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(i32)]
pub enum OodleCompressionLevel {
    HyperFast4 = -4,
    HyperFast3 = -3,
    HyperFast2 = -2,
    HyperFast1 = -1,
    None = 0,
    SuperFast = 1,
    VeryFast = 2,
    Fast = 3,
    Normal = 4,
    Optimal1 = 5,
    Optimal2 = 6,
    Optimal3 = 7,
    Optimal4 = 8,
    Optimal5 = 9,
}

// ---------------------------------------------------------------------------
// FFI function types — matching oodle2.h 2.9.11
// ---------------------------------------------------------------------------

type CompressFn = unsafe extern "C" fn(
    compressor: OodleCompressor,
    raw_buf: *const c_void,
    raw_len: isize,
    comp_buf: *mut c_void,
    level: OodleCompressionLevel,
    p_options: *const c_void,
    dictionary_base: *const c_void,
    lrm: *const c_void,
    scratch_mem: *mut c_void,
    scratch_size: isize,
) -> isize;

type DecompressFn = unsafe extern "C" fn(
    comp_buf: *const c_void,
    comp_buf_size: isize,
    raw_buf: *mut c_void,
    raw_len: isize,
    fuzz_safe: OodleFuzzSafe,
    check_crc: OodleCheckCrc,
    verbosity: OodleVerbosity,
    dec_buf_base: *mut c_void,
    dec_buf_size: isize,
    fp_callback: *mut c_void,
    callback_user_data: *mut c_void,
    decoder_memory: *mut c_void,
    decoder_memory_size: isize,
    thread_phase: OodleDecodeThreadPhase,
) -> isize;

type GetCompressedBufferSizeNeededFn =
    unsafe extern "C" fn(compressor: OodleCompressor, raw_size: isize) -> isize;

type GetDecodeBufferSizeFn = unsafe extern "C" fn(
    compressor: OodleCompressor,
    raw_size: isize,
    corruption_possible: i32,
) -> isize;

type GetCompressScratchMemBoundFn = unsafe extern "C" fn(
    compressor: OodleCompressor,
    level: OodleCompressionLevel,
    raw_len: isize,
    p_options: *const c_void,
) -> isize;

// ---------------------------------------------------------------------------
// Error
// ---------------------------------------------------------------------------

#[derive(Debug)]
pub enum Error {
    LibLoadError(libloading::Error),
    FunctionLoadError(libloading::Error),
    CompressFailed,
    DecompressFailed,
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Error::LibLoadError(e) => write!(f, "failed to load library: {e}"),
            Error::FunctionLoadError(e) => write!(f, "failed to load function: {e}"),
            Error::CompressFailed => write!(f, "compression failed"),
            Error::DecompressFailed => write!(f, "decompression failed"),
        }
    }
}

impl std::error::Error for Error {
    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
        match self {
            Error::LibLoadError(e) | Error::FunctionLoadError(e) => Some(e),
            Error::CompressFailed | Error::DecompressFailed => None,
        }
    }
}

// ---------------------------------------------------------------------------
// Oodle
// ---------------------------------------------------------------------------

/// Oodle compression library loaded at runtime.
pub struct Oodle {
    compress_fn: CompressFn,
    decompress_fn: DecompressFn,
    get_compressed_buffer_size_needed_fn: GetCompressedBufferSizeNeededFn,
    get_decode_buffer_size_fn: GetDecodeBufferSizeFn,
    get_compress_scratch_mem_bound_fn: GetCompressScratchMemBoundFn,
    _lib: Library,
}

impl fmt::Debug for Oodle {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Oodle").finish_non_exhaustive()
    }
}

// SAFETY: The raw `fn` pointers are plain function pointers into the loaded
// shared library. They contain no interior mutability and are safe to share
// across threads. The `Library` handle itself is `Send` and `Sync` in
// libloading 0.8.
const _: () = {
    const fn assert_send_sync<T: Send + Sync>() {}
    assert_send_sync::<Oodle>();
};

impl Oodle {
    /// Load the Oodle shared library from the given path.
    pub fn load(path: impl AsRef<OsStr>) -> Result<Self, Error> {
        unsafe {
            let lib = Library::new(path.as_ref()).map_err(Error::LibLoadError)?;

            let compress_fn = *lib
                .get::<CompressFn>(b"OodleLZ_Compress")
                .map_err(Error::FunctionLoadError)?;
            let decompress_fn = *lib
                .get::<DecompressFn>(b"OodleLZ_Decompress")
                .map_err(Error::FunctionLoadError)?;
            let get_compressed_buffer_size_needed_fn = *lib
                .get::<GetCompressedBufferSizeNeededFn>(b"OodleLZ_GetCompressedBufferSizeNeeded")
                .map_err(Error::FunctionLoadError)?;
            let get_decode_buffer_size_fn = *lib
                .get::<GetDecodeBufferSizeFn>(b"OodleLZ_GetDecodeBufferSize")
                .map_err(Error::FunctionLoadError)?;
            let get_compress_scratch_mem_bound_fn = *lib
                .get::<GetCompressScratchMemBoundFn>(b"OodleLZ_GetCompressScratchMemBound")
                .map_err(Error::FunctionLoadError)?;

            Ok(Self {
                compress_fn,
                decompress_fn,
                get_compressed_buffer_size_needed_fn,
                get_decode_buffer_size_fn,
                get_compress_scratch_mem_bound_fn,
                _lib: lib,
            })
        }
    }

    /// Compress `input` into `output` using the given compressor and level.
    ///
    /// Returns the number of bytes written to `output`.
    pub fn compress(
        &self,
        compressor: OodleCompressor,
        level: OodleCompressionLevel,
        input: &[u8],
        output: &mut [u8],
    ) -> Result<usize, Error> {
        let result = unsafe {
            (self.compress_fn)(
                compressor,
                input.as_ptr() as *const c_void,
                input.len() as isize,
                output.as_mut_ptr() as *mut c_void,
                level,
                std::ptr::null(),
                std::ptr::null(),
                std::ptr::null(),
                std::ptr::null_mut(),
                0,
            )
        };
        if result == 0 {
            Err(Error::CompressFailed)
        } else {
            Ok(result as usize)
        }
    }

    /// Decompress `source` into `dest` with default options.
    ///
    /// Returns the number of decompressed bytes written to `dest`.
    pub fn decompress(&self, source: &[u8], dest: &mut [u8]) -> Result<usize, Error> {
        self.decompress_with_options(
            source,
            dest,
            OodleFuzzSafe::Yes,
            OodleCheckCrc::No,
            OodleVerbosity::None,
            OodleDecodeThreadPhase::All,
        )
    }

    /// Decompress `source` into `dest` with explicit options.
    ///
    /// Returns the number of decompressed bytes written to `dest`.
    pub fn decompress_with_options(
        &self,
        source: &[u8],
        dest: &mut [u8],
        fuzz_safe: OodleFuzzSafe,
        check_crc: OodleCheckCrc,
        verbosity: OodleVerbosity,
        thread_phase: OodleDecodeThreadPhase,
    ) -> Result<usize, Error> {
        let result = unsafe {
            (self.decompress_fn)(
                source.as_ptr() as *const c_void,
                source.len() as isize,
                dest.as_mut_ptr() as *mut c_void,
                dest.len() as isize,
                fuzz_safe,
                check_crc,
                verbosity,
                std::ptr::null_mut(),
                0,
                std::ptr::null_mut(),
                std::ptr::null_mut(),
                std::ptr::null_mut(),
                0,
                thread_phase,
            )
        };
        if result == 0 {
            Err(Error::DecompressFailed)
        } else {
            Ok(result as usize)
        }
    }

    /// Returns the buffer size needed to hold the compressed output for the
    /// given compressor and raw data size.
    pub fn get_compressed_buffer_size_needed(
        &self,
        compressor: OodleCompressor,
        raw_size: usize,
    ) -> usize {
        unsafe {
            (self.get_compressed_buffer_size_needed_fn)(compressor, raw_size as isize) as usize
        }
    }

    /// Returns the buffer size needed for decompression, optionally accounting
    /// for possible data corruption.
    pub fn get_decode_buffer_size(
        &self,
        compressor: OodleCompressor,
        raw_size: usize,
        corruption_possible: bool,
    ) -> usize {
        unsafe {
            (self.get_decode_buffer_size_fn)(
                compressor,
                raw_size as isize,
                corruption_possible as i32,
            ) as usize
        }
    }

    /// Returns the scratch memory size bound needed for compression.
    pub fn get_compress_scratch_mem_bound(
        &self,
        compressor: OodleCompressor,
        level: OodleCompressionLevel,
        raw_len: usize,
    ) -> usize {
        unsafe {
            (self.get_compress_scratch_mem_bound_fn)(
                compressor,
                level,
                raw_len as isize,
                std::ptr::null(),
            ) as usize
        }
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    #[test]
    fn enum_compressor_discriminants() {
        assert_eq!(OodleCompressor::Invalid as i32, -1);
        assert_eq!(OodleCompressor::None as i32, 3);
        assert_eq!(OodleCompressor::Kraken as i32, 8);
        assert_eq!(OodleCompressor::Mermaid as i32, 9);
        assert_eq!(OodleCompressor::Selkie as i32, 11);
        assert_eq!(OodleCompressor::Hydra as i32, 12);
        assert_eq!(OodleCompressor::Leviathan as i32, 13);
    }

    #[test]
    fn enum_compression_level_discriminants() {
        assert_eq!(OodleCompressionLevel::HyperFast4 as i32, -4);
        assert_eq!(OodleCompressionLevel::HyperFast3 as i32, -3);
        assert_eq!(OodleCompressionLevel::HyperFast2 as i32, -2);
        assert_eq!(OodleCompressionLevel::HyperFast1 as i32, -1);
        assert_eq!(OodleCompressionLevel::None as i32, 0);
        assert_eq!(OodleCompressionLevel::SuperFast as i32, 1);
        assert_eq!(OodleCompressionLevel::VeryFast as i32, 2);
        assert_eq!(OodleCompressionLevel::Fast as i32, 3);
        assert_eq!(OodleCompressionLevel::Normal as i32, 4);
        assert_eq!(OodleCompressionLevel::Optimal1 as i32, 5);
        assert_eq!(OodleCompressionLevel::Optimal2 as i32, 6);
        assert_eq!(OodleCompressionLevel::Optimal3 as i32, 7);
        assert_eq!(OodleCompressionLevel::Optimal4 as i32, 8);
        assert_eq!(OodleCompressionLevel::Optimal5 as i32, 9);
    }

    #[test]
    fn enum_copy_and_eq() {
        let a = OodleCompressor::Kraken;
        let b = a;
        assert_eq!(a, b);
    }

    #[test]
    fn enum_hash() {
        use std::collections::HashSet;
        let mut set = HashSet::new();
        set.insert(OodleCompressor::Kraken);
        set.insert(OodleCompressor::Mermaid);
        assert_eq!(set.len(), 2);
        assert!(set.contains(&OodleCompressor::Kraken));
    }

    #[test]
    fn error_display() {
        assert_eq!(Error::CompressFailed.to_string(), "compression failed");
        assert_eq!(Error::DecompressFailed.to_string(), "decompression failed");
    }

    #[test]
    fn error_is_std_error() {
        fn assert_error<T: std::error::Error>() {}
        assert_error::<Error>();
    }

    #[test]
    fn load_nonexistent_library() {
        let result = Oodle::load("nonexistent_library.so");
        assert!(result.is_err());
        assert!(matches!(result.unwrap_err(), Error::LibLoadError(_)));
    }

    #[test]
    fn oodle_is_send_and_sync() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<Oodle>();
    }
}