oodle-safe 0.2.0

Safe low-level bindings for the oodle compression library.
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
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//! Minimal safe wrapper around oodle-sys.
//!
//! This crate provides a minimal translation of the
//! [oodle-sys](https://crates.io/crates/oodle-sys) methods to Rust.
//!
//! Check Oodle's [website](http://www.radgametools.com/oodle.htm) for more
//! information.

#[cfg(test)]
mod tests;

use oodle_sys;

include!("constants.rs");

/// Set of compression algorithms.
///
/// Each compressor has its own trade-offs between compression ratio and speed.
pub enum Compressor {
    /// No compression, just a copy
    None,

    /// Fast decompression, high compression ratio
    Kraken,

    /// Slighly slower decompression but higher compression ratio than Kraken
    Leviathan,

    /// Between Kraken and Selkie in speed with decent compression ratio
    Mermaid,

    /// "Super fast" relative to Mermaid. Used for maximum decompression speed
    Selkie,

    /// Automatically selects between Kraken, Leviathan, Mermaid, and Selkie
    Hydra,
}

impl Into<oodle_sys::OodleLZ_Compressor> for Compressor {
    fn into(self) -> oodle_sys::OodleLZ_Compressor {
        match self {
            Compressor::None => oodle_sys::OodleLZ_Compressor_OodleLZ_Compressor_None,
            Compressor::Kraken => oodle_sys::OodleLZ_Compressor_OodleLZ_Compressor_Kraken,
            Compressor::Leviathan => oodle_sys::OodleLZ_Compressor_OodleLZ_Compressor_Leviathan,
            Compressor::Mermaid => oodle_sys::OodleLZ_Compressor_OodleLZ_Compressor_Mermaid,
            Compressor::Selkie => oodle_sys::OodleLZ_Compressor_OodleLZ_Compressor_Selkie,
            Compressor::Hydra => oodle_sys::OodleLZ_Compressor_OodleLZ_Compressor_Hydra,
        }
    }
}

/// Set of compression levels.
///
/// A compressed data stream can be decompressed with any level, but the
/// compression level used to compress the data must be known.
///
/// The compression level controls the amount of work done by the compressor to
/// find the best compressed bitstream. It does not directly impact
/// decompression speed, it trades off encode speed for compression bitstream
/// quality.
pub enum CompressionLevel {
    /// Don't compress, just copy the data
    None,

    /// Lowest compression ratio, super fast
    SuperFast,

    /// Fastest with still decent compression ratio
    VeryFast,

    /// Good for daily use
    Fast,

    /// Standard medium speed
    Normal,

    /// Optimal parse level 1 (fastest)
    Optimal1,

    /// Optimal parse level 2 (recommended baseline)
    Optimal2,

    /// Optimal parse level 3 (slower)
    Optimal3,

    /// Optimal parse level 4 (very slow)
    Optimal4,

    /// Optimal parse level 5 (don't care about speed, just want best ratio)
    Optimal5,

    /// Faster than SuperFast, but lower compression ratio
    HyperFast1,

    /// Faster than HyperFast1, but lower compression ratio
    HyperFast2,

    /// Faster than HyperFast2, but lower compression ratio
    HyperFast3,

    /// Faster than HyperFast3, but lower compression ratio
    HyperFast4,

    /// Alias optimal standard level
    Optimal,

    /// Alias hyperfast base level
    HyperFast,

    /// Alias for the maximum compression level
    Max,

    /// Alias for the minimum compression level
    Min,
}

impl Into<oodle_sys::OodleLZ_CompressionLevel> for CompressionLevel {
    #[rustfmt::skip]
    fn into(self) -> oodle_sys::OodleLZ_CompressionLevel {
        match self {
            CompressionLevel::None => oodle_sys::OodleLZ_CompressionLevel_OodleLZ_CompressionLevel_None,
            CompressionLevel::SuperFast => oodle_sys::OodleLZ_CompressionLevel_OodleLZ_CompressionLevel_SuperFast,
            CompressionLevel::VeryFast => oodle_sys::OodleLZ_CompressionLevel_OodleLZ_CompressionLevel_VeryFast,
            CompressionLevel::Fast => oodle_sys::OodleLZ_CompressionLevel_OodleLZ_CompressionLevel_Fast,
            CompressionLevel::Normal => oodle_sys::OodleLZ_CompressionLevel_OodleLZ_CompressionLevel_Normal,
            CompressionLevel::Optimal1 => oodle_sys::OodleLZ_CompressionLevel_OodleLZ_CompressionLevel_Optimal1,
            CompressionLevel::Optimal2 => oodle_sys::OodleLZ_CompressionLevel_OodleLZ_CompressionLevel_Optimal2,
            CompressionLevel::Optimal3 => oodle_sys::OodleLZ_CompressionLevel_OodleLZ_CompressionLevel_Optimal3,
            CompressionLevel::Optimal4 => oodle_sys::OodleLZ_CompressionLevel_OodleLZ_CompressionLevel_Optimal4,
            CompressionLevel::Optimal5 => oodle_sys::OodleLZ_CompressionLevel_OodleLZ_CompressionLevel_Optimal5,
            CompressionLevel::HyperFast1 => oodle_sys::OodleLZ_CompressionLevel_OodleLZ_CompressionLevel_HyperFast1,
            CompressionLevel::HyperFast2 => oodle_sys::OodleLZ_CompressionLevel_OodleLZ_CompressionLevel_HyperFast2,
            CompressionLevel::HyperFast3 => oodle_sys::OodleLZ_CompressionLevel_OodleLZ_CompressionLevel_HyperFast3,
            CompressionLevel::HyperFast4 => oodle_sys::OodleLZ_CompressionLevel_OodleLZ_CompressionLevel_HyperFast4,
            CompressionLevel::Optimal => oodle_sys::OodleLZ_CompressionLevel_OodleLZ_CompressionLevel_Optimal,
            CompressionLevel::HyperFast => oodle_sys::OodleLZ_CompressionLevel_OodleLZ_CompressionLevel_HyperFast,
            CompressionLevel::Max => oodle_sys::OodleLZ_CompressionLevel_OodleLZ_CompressionLevel_Max,
            CompressionLevel::Min => oodle_sys::OodleLZ_CompressionLevel_OodleLZ_CompressionLevel_Min,
        }
    }
}

impl Default for CompressionLevel {
    fn default() -> Self {
        CompressionLevel::Normal
    }
}

/// Decoder profile to target.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Profile {
    /// Main profile, full feature set
    Main,

    /// Kraken only, limited feature set
    Reduced,
}

impl Into<oodle_sys::OodleLZ_Profile> for Profile {
    fn into(self) -> oodle_sys::OodleLZ_Profile {
        match self {
            Profile::Main => oodle_sys::OodleLZ_Profile_OodleLZ_Profile_Main,
            Profile::Reduced => oodle_sys::OodleLZ_Profile_OodleLZ_Profile_Reduced,
        }
    }
}

impl From<oodle_sys::OodleLZ_Profile> for Profile {
    fn from(profile: oodle_sys::OodleLZ_Profile) -> Self {
        match profile {
            oodle_sys::OodleLZ_Profile_OodleLZ_Profile_Main => Profile::Main,
            oodle_sys::OodleLZ_Profile_OodleLZ_Profile_Reduced => Profile::Reduced,
            _ => panic!("Invalid profile"),
        }
    }
}

/// Controls the amount of internal threading used by the compressor.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Jobify {
    /// Use compressor default for level of internal job usage
    Default,

    /// Do not use jobs at all
    Disable,

    /// Try to balance parallelism with increased memory use
    Normal,

    /// Maximize parallelism at the cost of increased memory use
    Aggressive,
}

impl Into<oodle_sys::OodleLZ_Jobify> for Jobify {
    fn into(self) -> oodle_sys::OodleLZ_Jobify {
        match self {
            Jobify::Default => oodle_sys::OodleLZ_Jobify_OodleLZ_Jobify_Default,
            Jobify::Disable => oodle_sys::OodleLZ_Jobify_OodleLZ_Jobify_Disable,
            Jobify::Normal => oodle_sys::OodleLZ_Jobify_OodleLZ_Jobify_Normal,
            Jobify::Aggressive => oodle_sys::OodleLZ_Jobify_OodleLZ_Jobify_Aggressive,
        }
    }
}

impl From<oodle_sys::OodleLZ_Jobify> for Jobify {
    fn from(jobify: oodle_sys::OodleLZ_Jobify) -> Self {
        match jobify {
            oodle_sys::OodleLZ_Jobify_OodleLZ_Jobify_Default => Jobify::Default,
            oodle_sys::OodleLZ_Jobify_OodleLZ_Jobify_Disable => Jobify::Disable,
            oodle_sys::OodleLZ_Jobify_OodleLZ_Jobify_Normal => Jobify::Normal,
            oodle_sys::OodleLZ_Jobify_OodleLZ_Jobify_Aggressive => Jobify::Aggressive,
            _ => panic!("Invalid jobify"),
        }
    }
}

/// Options to use for compression.
///
/// Typically, you would use the default options and only change the fields you
/// need to modify.
///
/// To ensure that the options are valid, call [CompressOptions::validate]
/// after modifying the fields.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct CompressOptions {
    /// Was previously named `verbosity`, set to 0
    unused: u32,

    /// Cannot be used to reduce a compressor's default MML, but can be higher.
    /// On some types of data, a large MML (6 or 8) is a space-speed win.
    min_match_len: i32,

    /// Whether chunks should be independent, for seeking and parallelism
    seek_chunk_reset: bool,

    /// Length of independent seek chunks if [seek_chunk_reset] is true.
    /// This must be a power of 2 and >= [BLOCK_LEN]
    seek_chunk_len: u32,

    /// Decoder profile to target (set to 0)
    profile: Profile,

    /// Sets a maximum offset for matches, if lower than the maximum the format supports.
    /// <= 0 means infinite (use whole buffer).
    /// Often power of 2 but doesn't have to be.
    dictionary_size: i32,

    /// Number of bytes; It must gain at least this many bytes of compressed
    /// size to accept a speed-decreasing decision
    space_speed_tradeoff_bytes: i32,

    /// Was previously named `max_huffmans_per_chunk`, set to 0
    unused2: i32,

    /// Whether the encoder should send CRCs for each compressed quantum for
    /// integrity checking. This is necessary for using `CheckCRC::Yes` in
    /// decompression.
    send_quantum_crcs: bool,

    /// Size of local dictionary before needing a long range matcher.
    /// This does not set a window size for the decoder;
    /// it's useful to limit memory use and time taken in the encoder.
    /// This must be a power of 2 and < [LOCALDICTIONARYSIZE_MAX].
    max_local_dictionary_size: u32,

    /// Whether the encoder should should find matches beyond [max_local_dictionary_size]
    /// when using a long range matcher.
    make_long_range_matcher: bool,

    /// Default is 0. If non-zero, this sets the size of the match finder structure
    /// (often a hash table).
    match_table_size_log2: i32,

    /// Controls internal job usage for the compressor.
    jobify: Jobify,

    /// User pointer passed through to RunJob and WaitJob callbacks.
    jobify_user_ptr: *mut std::ffi::c_void,

    /// Far match must be at least this long.
    far_match_min_len: i32,

    /// If not 0, the log2 of the offset that must meet [far_match_min_len].
    far_match_offset_log2: i32,

    /// Reserved for future use, set to 0
    reserved: [u32; 4],
}

impl CompressOptions {
    pub fn validate(&mut self) {
        let options: *mut oodle_sys::OodleLZ_CompressOptions = &mut (*self).into();
        unsafe { oodle_sys::OodleLZ_CompressOptions_Validate(options) };
        *self = CompressOptions::from(unsafe { *options });
    }
}

impl Into<oodle_sys::OodleLZ_CompressOptions> for CompressOptions {
    fn into(self) -> oodle_sys::OodleLZ_CompressOptions {
        oodle_sys::OodleLZ_CompressOptions {
            unused_was_verbosity: self.unused,
            minMatchLen: self.min_match_len,
            seekChunkReset: if self.seek_chunk_reset { 1 } else { 0 },
            seekChunkLen: self.seek_chunk_len as i32,
            profile: self.profile.into(),
            dictionarySize: self.dictionary_size,
            spaceSpeedTradeoffBytes: self.space_speed_tradeoff_bytes,
            unused_was_maxHuffmansPerChunk: self.unused2,
            sendQuantumCRCs: if self.send_quantum_crcs { 1 } else { 0 },
            maxLocalDictionarySize: self.max_local_dictionary_size as i32,
            makeLongRangeMatcher: if self.make_long_range_matcher { 1 } else { 0 },
            matchTableSizeLog2: self.match_table_size_log2,
            jobify: self.jobify.into(),
            jobifyUserPtr: self.jobify_user_ptr,
            farMatchMinLen: self.far_match_min_len,
            farMatchOffsetLog2: self.far_match_offset_log2,
            reserved: self.reserved,
        }
    }
}

impl From<oodle_sys::OodleLZ_CompressOptions> for CompressOptions {
    fn from(options: oodle_sys::OodleLZ_CompressOptions) -> Self {
        Self {
            unused: options.unused_was_verbosity,
            min_match_len: options.minMatchLen,
            seek_chunk_reset: options.seekChunkReset != 0,
            seek_chunk_len: options.seekChunkLen as u32,
            profile: options.profile.into(),
            dictionary_size: options.dictionarySize,
            space_speed_tradeoff_bytes: options.spaceSpeedTradeoffBytes,
            unused2: options.unused_was_maxHuffmansPerChunk,
            send_quantum_crcs: options.sendQuantumCRCs != 0,
            max_local_dictionary_size: options.maxLocalDictionarySize as u32,
            make_long_range_matcher: options.makeLongRangeMatcher != 0,
            match_table_size_log2: options.matchTableSizeLog2,
            jobify: options.jobify.into(),
            jobify_user_ptr: options.jobifyUserPtr,
            far_match_min_len: options.farMatchMinLen,
            far_match_offset_log2: options.farMatchOffsetLog2,
            reserved: options.reserved,
        }
    }
}

impl Default for CompressOptions {
    fn default() -> Self {
        let options = unsafe {
            *oodle_sys::OodleLZ_CompressOptions_GetDefault(
                Compressor::None.into(),
                CompressionLevel::None.into(),
            )
        };

        options.into()
    }
}

/// Compress some data from memory to memory synchronously.
///
/// # Arguments
///
/// * `compressor` - The compressor to use.
/// * `decompressed` - The buffer containing the data to compress.
/// * `compressed` - The buffer to write the compressed data to.
/// * `level` - The compression level to use.
/// * `options` - Additional options to use for compression.
/// * `dictionary_base` - Preconditioned dictionary to use for compression.
/// * `scratch_memory` - Scratch memory to use for compression.
///
/// When setting optionnal parameters to `None`, the default value will be used.
///
/// # Returns
///
/// The size of the compressed data.
///
/// # Example
///
/// ```rust
/// // Load decompressed data from a file (or any other source).
/// let decompressed = include_bytes!("../test_data/decompressed");
///
/// // Allocate compressed buffer with some extra space in case the compression
/// // adds some overhead like the OodleLZ block header.
/// let mut compressed = vec![0u8; decompressed.len() + 8];
///
/// // Compress the data.
/// let compressed_size = oodle_safe::compress(
///     oodle_safe::Compressor::Kraken,
///     decompressed,
///     &mut compressed,
///     oodle_safe::CompressionLevel::Normal, // same as default
///     Some(oodle_safe::CompressOptions::default()), // same as default
///     None,
///     None,
/// )
/// .unwrap_or_else(|_| panic!("compression failed"));
///
/// // Trim the output buffer to the actual size of the compressed data.
/// let compressed = &compressed[..compressed_size];
/// ```
pub fn compress(
    compressor: Compressor,
    decompressed: &[u8],
    compressed: &mut [u8],
    level: CompressionLevel,
    options: Option<CompressOptions>,
    dictionary_base: Option<&[u8]>,
    scratch_memory: Option<&mut [u8]>,
) -> Result<usize, u32> {
    let options = match options {
        Some(x) => &x.into(),
        None => std::ptr::null() as *const _,
    };

    let dictionary_base = match dictionary_base {
        Some(x) => x.as_ptr(),
        None => std::ptr::null(),
    };

    let (scratch_memory, scratch_memory_len) = match scratch_memory {
        Some(x) => (x.as_mut_ptr(), x.len() as isize),
        None => (std::ptr::null_mut(), 0),
    };

    let result = unsafe {
        oodle_sys::OodleLZ_Compress(
            compressor.into(),
            decompressed.as_ptr() as *const _,
            decompressed.len() as isize,
            compressed.as_mut_ptr() as *mut _,
            level.into(),
            options,
            dictionary_base as *const _,
            std::ptr::null(), // TODO: add long_range_matcher
            scratch_memory as *mut _,
            scratch_memory_len,
        ) as usize
    };

    if result == FAILED as usize {
        Err(FAILED)
    } else {
        // This is necessary to avoid double free-ing the buffer when slicing
        // the compressed buffer after the compression.
        let compressed_data = compressed[..result].to_vec();
        compressed[..result].copy_from_slice(&compressed_data);
        Ok(result)
    }
}

/// Bool enum for the LZ decoder to check the CRC of the compressed data.
///
/// To use [CheckCRC::Yes], the compressed data must have been compressed with
/// the CRC option enabled.
pub enum CheckCRC {
    No,
    Yes,
}

impl Default for CheckCRC {
    fn default() -> Self {
        CheckCRC::No
    }
}

impl Into<oodle_sys::OodleLZ_CheckCRC> for CheckCRC {
    fn into(self) -> oodle_sys::OodleLZ_CheckCRC {
        match self {
            CheckCRC::No => oodle_sys::OodleLZ_CheckCRC_OodleLZ_CheckCRC_No,
            CheckCRC::Yes => oodle_sys::OodleLZ_CheckCRC_OodleLZ_CheckCRC_Yes,
        }
    }
}

/// Verbosity level for LZ decompression.
pub enum Verbosity {
    /// Will not log anything, even when the decoder sees corrupted data.
    None,
    Minimal,
    Some,
    Lots,
}

impl Default for Verbosity {
    fn default() -> Self {
        Verbosity::None
    }
}

impl Into<oodle_sys::OodleLZ_Verbosity> for Verbosity {
    fn into(self) -> oodle_sys::OodleLZ_Verbosity {
        match self {
            Verbosity::None => oodle_sys::OodleLZ_Verbosity_OodleLZ_Verbosity_None,
            Verbosity::Minimal => oodle_sys::OodleLZ_Verbosity_OodleLZ_Verbosity_Minimal,
            Verbosity::Some => oodle_sys::OodleLZ_Verbosity_OodleLZ_Verbosity_Some,
            Verbosity::Lots => oodle_sys::OodleLZ_Verbosity_OodleLZ_Verbosity_Lots,
        }
    }
}

/// Thread phase for threaded decompression.
///
/// Note that threaded decompression is only available for the Kraken compressor.
pub enum DecodeThreadPhase {
    One,
    Two,
    All,
    Unthreaded,
}

impl Default for DecodeThreadPhase {
    fn default() -> Self {
        DecodeThreadPhase::Unthreaded
    }
}

impl Into<oodle_sys::OodleLZ_Decode_ThreadPhase> for DecodeThreadPhase {
    #[rustfmt::skip]
    fn into(self) -> oodle_sys::OodleLZ_Decode_ThreadPhase {
        match self {
            DecodeThreadPhase::One => oodle_sys::OodleLZ_Decode_ThreadPhase_OodleLZ_Decode_ThreadPhase1,
            DecodeThreadPhase::Two => oodle_sys::OodleLZ_Decode_ThreadPhase_OodleLZ_Decode_ThreadPhase2,
            DecodeThreadPhase::All => oodle_sys::OodleLZ_Decode_ThreadPhase_OodleLZ_Decode_ThreadPhaseAll,
            DecodeThreadPhase::Unthreaded => oodle_sys::OodleLZ_Decode_ThreadPhase_OodleLZ_Decode_Unthreaded,
        }
    }
}

/// Decompress some data from memory to memory synchronously.
///
/// # Arguments
///
/// * `compressed` - The buffer containing the compressed data.
/// * `decompressed` - The buffer to write the decompressed data to.
/// * `dictionary_base` - Preconditioned dictionary to use for decompression.
/// The dictionary must be the same as the one used for compression.
/// * `check_crc` - Whether to check the validity of the compressed data.
/// * `verbosity` - The verbosity of the decompression.
/// * `thread_phase` - The thread phase for threaded decompression.
///
/// When setting optionnal parameters to `None`, the default value will be used.
///
/// # Returns
///
/// The size of the decompressed data.
///
/// # Example
/// ```rust
/// // Load compressed data from a file (or any other source).
/// let compressed = include_bytes!("../test_data/compressed");
///
/// # let decompressed_size = u32::from_le_bytes(compressed[..4].try_into().unwrap()) as usize;
/// // Allocate decompressed buffer with the size of the decompressed data.
/// let mut decompressed = vec![0u8; decompressed_size];
///
/// let result = oodle_safe::decompress(
///     &compressed[4..],
///     &mut decompressed,
///     None,
///     Some(oodle_safe::CheckCRC::No), // same as default
///     Some(oodle_safe::Verbosity::None), // same as default
///     Some(oodle_safe::DecodeThreadPhase::Unthreaded), // same as default
/// )
/// .unwrap_or_else(|_| panic!("decompression failed"));
/// ```
pub fn decompress(
    compressed: &[u8],
    decompressed: &mut [u8],
    dictionary_base: Option<&mut [u8]>,
    check_crc: Option<CheckCRC>,
    verbosity: Option<Verbosity>,
    thread_phase: Option<DecodeThreadPhase>,
) -> Result<usize, u32> {
    let (dictionary_base, dictionary_base_len) = match dictionary_base {
        Some(x) => (x.as_mut_ptr(), x.len() as isize),
        None => (std::ptr::null_mut(), 0),
    };

    let result = unsafe {
        oodle_sys::OodleLZ_Decompress(
            compressed.as_ptr() as *const _,
            compressed.len() as isize,
            decompressed.as_mut_ptr() as *mut _,
            decompressed.len() as isize,
            oodle_sys::OodleLZ_FuzzSafe_OodleLZ_FuzzSafe_Yes,
            check_crc.unwrap_or_default().into(),
            verbosity.unwrap_or_default().into(),
            dictionary_base as *mut _,
            dictionary_base_len,
            None, // TODO: add callback
            std::ptr::null_mut(),
            std::ptr::null_mut(),
            0,
            thread_phase.unwrap_or_default().into(),
        ) as usize
    };

    if result == FAILED as usize {
        Err(FAILED)
    } else {
        Ok(result)
    }
}