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
#![cfg(not(feature="safe"))]

#[no_mangle]
use core;
use core::slice;
use ::StaticCommand;
use super::DivansDecompressorFactory;
use super::interface::Decompressor;
pub mod interface;
pub mod alloc_util;
use self::alloc_util::SubclassableAllocator;
mod compressor;
mod decompressor;
use self::compressor::DivansCompressorState;
use self::decompressor::DivansDecompressorState;
use self::interface::{CAllocator, c_void, DivansOptionSelect, DivansReturnCode, DIVANS_FAILURE, DIVANS_SUCCESS, DIVANS_NEEDS_MORE_INPUT, DIVANS_NEEDS_MORE_OUTPUT};
#[no_mangle]
pub extern fn divans_new_compressor() -> *mut compressor::DivansCompressorState{
    unsafe {
        divans_new_compressor_with_custom_alloc(CAllocator{
            alloc_func:None,
            free_func:None,
            opaque: core::ptr::null_mut(),
        })
    }
}



#[cfg(feature="no-stdlib")]
fn divans_new_compressor_without_custom_alloc(_to_box: DivansCompressorState) -> *mut DivansCompressorState{
    panic!("Must supply allocators if calling divans when compiled with features=no-stdlib");
}
#[cfg(not(feature="no-stdlib"))]
fn divans_new_compressor_without_custom_alloc(to_box: DivansCompressorState) -> *mut DivansCompressorState{
    alloc_util::Box::<DivansCompressorState>::into_raw(alloc_util::Box::<DivansCompressorState>::new(to_box))
}
#[no_mangle]
pub unsafe extern fn divans_new_compressor_with_custom_alloc(allocators:CAllocator) -> *mut DivansCompressorState{
    let to_box = DivansCompressorState{
        custom_allocator:allocators.clone(),
        compressor:compressor::CompressorState::default(),
    };
    if let Some(alloc_fn) = allocators.alloc_func {
        let ptr = alloc_fn(allocators.opaque, core::mem::size_of::<DivansCompressorState>());
        let divans_compressor_state_ptr = core::mem::transmute::<*mut c_void, *mut DivansCompressorState>(ptr);
        core::ptr::write(divans_compressor_state_ptr, to_box);
        divans_compressor_state_ptr
    } else {
        divans_new_compressor_without_custom_alloc(to_box)
    }
}




#[no_mangle]
pub unsafe extern fn divans_set_option(state_ptr: *mut DivansCompressorState,
                                       selector: DivansOptionSelect,
                                       value: u32) -> DivansReturnCode {
    match state_ptr.as_mut() {
        None => DIVANS_FAILURE,
        Some(state_ref) => {
            state_ref.compressor.set_option(selector, value)
        }
    }
}
     
#[no_mangle]
pub unsafe extern fn divans_encode(state_ptr: *mut DivansCompressorState,
                                   input_buf_ptr: *const u8, input_size: usize, input_offset_ptr: *mut usize,
                                   output_buf_ptr: *mut u8, output_size: usize, output_offset_ptr: *mut usize) -> DivansReturnCode {
    let input_buf = slice::from_raw_parts(input_buf_ptr, input_size);
    let output_buf = slice::from_raw_parts_mut(output_buf_ptr, output_size);
    match input_offset_ptr.as_mut() {
        None => return DIVANS_FAILURE,
        Some(input_offset) => {
            match output_offset_ptr.as_mut() {
                None => return DIVANS_FAILURE,
                Some(output_offset) => {
                    match state_ptr.as_mut() {
                        None => return DIVANS_FAILURE,
                        Some(state_ref) => {
                            return state_ref.compressor.encode(input_buf, input_offset, output_buf, output_offset, &state_ref.custom_allocator);
                        }
                    }
                }
            }
        }
    }
}

#[no_mangle]
pub unsafe extern fn divans_encode_flush(state_ptr: *mut DivansCompressorState,
                                         output_buf_ptr: *mut u8, output_size: usize, output_offset_ptr: *mut usize) -> DivansReturnCode {
    let output_buf = slice::from_raw_parts_mut(output_buf_ptr, output_size);
    match output_offset_ptr.as_mut() {
        None => return DIVANS_FAILURE,
        Some(output_offset) => {
            match state_ptr.as_mut() {
                None => return DIVANS_FAILURE,
                Some(state_ref) => {
                    return state_ref.compressor.flush(output_buf, output_offset, &state_ref.custom_allocator);
                }
            }
        }
    }
}

#[no_mangle]
pub unsafe extern fn divans_compressor_malloc_u8(state_ptr: *mut DivansCompressorState, size: usize) -> *mut u8 {
    if let Some(alloc_fn) = (*state_ptr).custom_allocator.alloc_func {
            return core::mem::transmute::<*mut c_void, *mut u8>(alloc_fn((*state_ptr).custom_allocator.opaque, size));
    } else {
        return alloc_util::alloc_stdlib(size);
    }
}

#[no_mangle]
pub unsafe extern fn divans_compressor_free_u8(state_ptr: *mut DivansCompressorState, data: *mut u8, size: usize) {
    if let Some(free_fn) = (*state_ptr).custom_allocator.free_func {
        free_fn((*state_ptr).custom_allocator.opaque, core::mem::transmute::<*mut u8, *mut c_void>(data));
    } else {
        alloc_util::free_stdlib(data, size);
    }
}


#[no_mangle]
pub unsafe extern fn divans_compressor_malloc_usize(state_ptr: *mut DivansCompressorState, size: usize) -> *mut usize {
    if let Some(alloc_fn) = (*state_ptr).custom_allocator.alloc_func {
        return core::mem::transmute::<*mut c_void, *mut usize>(alloc_fn((*state_ptr).custom_allocator.opaque,
                                                                         size * core::mem::size_of::<usize>()));
    } else {
        return alloc_util::alloc_stdlib(size);
    }
}
#[no_mangle]
pub unsafe extern fn divans_compressor_free_usize(state_ptr: *mut DivansCompressorState, data: *mut usize, size: usize) {
    if let Some(free_fn) = (*state_ptr).custom_allocator.free_func {
        free_fn((*state_ptr).custom_allocator.opaque, core::mem::transmute::<*mut usize, *mut c_void>(data));
    } else {
        alloc_util::free_stdlib(data, size);
    }
}


#[cfg(not(feature="no-stdlib"))]
unsafe fn free_compressor_no_custom_alloc(state_ptr: *mut DivansCompressorState) {
    let _state = alloc_util::Box::from_raw(state_ptr);
}

#[cfg(feature="no-stdlib")]
unsafe fn free_compressor_no_custom_alloc(_state_ptr: *mut DivansCompressorState) {
    unreachable!();
}

#[no_mangle]
pub unsafe extern fn divans_free_compressor(state_ptr: *mut DivansCompressorState) {
    if let Some(_) = (*state_ptr).custom_allocator.alloc_func {
        if let Some(free_fn) = (*state_ptr).custom_allocator.free_func {
            let _to_free = core::ptr::read(state_ptr);
            let ptr = core::mem::transmute::<*mut DivansCompressorState, *mut c_void>(state_ptr);
            free_fn((*state_ptr).custom_allocator.opaque, ptr);
        }
    } else {
        free_compressor_no_custom_alloc(state_ptr)
    }
}







#[no_mangle]
pub extern fn divans_new_decompressor() -> *mut DivansDecompressorState{
    unsafe {
        divans_new_decompressor_with_custom_alloc(CAllocator{
            alloc_func:None,
            free_func:None,
            opaque: core::ptr::null_mut(),
        }, 0, 1)
    }
}


#[no_mangle]
pub extern fn divans_new_serial_decompressor() -> *mut DivansDecompressorState{
    unsafe {
        divans_new_decompressor_with_custom_alloc(CAllocator{
            alloc_func:None,
            free_func:None,
            opaque: core::ptr::null_mut(),
        }, 0, 0)
    }
}


#[cfg(feature="no-stdlib")]
fn divans_new_decompressor_without_custom_alloc(_to_box: DivansDecompressorState) -> *mut DivansDecompressorState{
    panic!("Must supply allocators if calling divans when compiled with features=no-stdlib");
}

#[cfg(not(feature="no-stdlib"))]
fn divans_new_decompressor_without_custom_alloc(to_box: DivansDecompressorState) -> *mut DivansDecompressorState{
    alloc_util::Box::<DivansDecompressorState>::into_raw(alloc_util::Box::<DivansDecompressorState>::new(to_box))
}


#[no_mangle]
pub unsafe extern fn divans_new_decompressor_with_custom_alloc(allocators:CAllocator, skip_crc:u8, multithread: u8) -> *mut DivansDecompressorState{
    let to_box = DivansDecompressorState{
        custom_allocator:allocators.clone(),
        decompressor:decompressor::DecompressorFactory::new(
            SubclassableAllocator::<u8>::new(allocators.clone()),
            SubclassableAllocator::<super::DefaultCDF16>::new(allocators.clone()),
            SubclassableAllocator::<StaticCommand>::new(allocators.clone()),
            skip_crc != 0,
            multithread != 0,
        ),
    };
    if let Some(alloc_fn) = allocators.alloc_func {
        let ptr = alloc_fn(allocators.opaque, core::mem::size_of::<DivansDecompressorState>());
        let divans_decompressor_state_ptr = core::mem::transmute::<*mut c_void, *mut DivansDecompressorState>(ptr);
        core::ptr::write(divans_decompressor_state_ptr, to_box);
        divans_decompressor_state_ptr
    } else {
        divans_new_decompressor_without_custom_alloc(to_box)
    }
}


#[no_mangle]
pub unsafe extern fn divans_decode(state_ptr: *mut DivansDecompressorState,
                                   input_buf_ptr: *const u8, input_size: usize, input_offset_ptr: *mut usize,
                                   output_buf_ptr: *mut u8, output_size: usize, output_offset_ptr: *mut usize) -> DivansReturnCode {
    let input_buf = slice::from_raw_parts(input_buf_ptr, input_size);
    let output_buf = slice::from_raw_parts_mut(output_buf_ptr, output_size);
    match input_offset_ptr.as_mut() {
        None => return DIVANS_FAILURE,
        Some(input_offset) => {
            match output_offset_ptr.as_mut() {
                None => return DIVANS_FAILURE,
                Some(output_offset) => {
                    match state_ptr.as_mut() {
                        None => return DIVANS_FAILURE,
                        Some(state_ref) => {
                            match state_ref.decompressor.decode(input_buf, input_offset, output_buf, output_offset) {
                                ::interface::DivansResult::Success => return DIVANS_SUCCESS,
                                ::interface::DivansResult::Failure(_) => return DIVANS_FAILURE,
                                ::interface::DivansResult::NeedsMoreInput => return DIVANS_NEEDS_MORE_INPUT,
                                ::interface::DivansResult::NeedsMoreOutput => return DIVANS_NEEDS_MORE_OUTPUT,
                            }
                        }
                    }
                }
            }
        }
    }
}

#[cfg(not(feature="no-stdlib"))]
unsafe fn free_decompressor_no_custom_alloc(state_ptr: *mut DivansDecompressorState) {
    let _state = alloc_util::Box::from_raw(state_ptr);
}

#[cfg(feature="no-stdlib")]
unsafe fn free_decompressor_no_custom_alloc(_state_ptr: *mut DivansDecompressorState) {
    unreachable!();
}


#[no_mangle]
pub unsafe extern fn divans_decompressor_malloc_u8(state_ptr: *mut DivansDecompressorState, size: usize) -> *mut u8 {
    if let Some(alloc_fn) = (*state_ptr).custom_allocator.alloc_func {
        return core::mem::transmute::<*mut c_void, *mut u8>(alloc_fn((*state_ptr).custom_allocator.opaque, size));
    } else {
        return alloc_util::alloc_stdlib(size);
    }
}

#[no_mangle]
pub unsafe extern fn divans_decompressor_free_u8(state_ptr: *mut DivansDecompressorState, data: *mut u8, size: usize) {
    if let Some(free_fn) = (*state_ptr).custom_allocator.free_func {
        free_fn((*state_ptr).custom_allocator.opaque, core::mem::transmute::<*mut u8, *mut c_void>(data));
    } else {
        alloc_util::free_stdlib(data, size);
    }
}

#[no_mangle]
pub unsafe extern fn divans_decompressor_malloc_usize(state_ptr: *mut DivansDecompressorState, size: usize) -> *mut usize {
    if let Some(alloc_fn) = (*state_ptr).custom_allocator.alloc_func {
        return core::mem::transmute::<*mut c_void, *mut usize>(alloc_fn((*state_ptr).custom_allocator.opaque,
                                                                         size * core::mem::size_of::<usize>()));
    } else {
        return alloc_util::alloc_stdlib(size);
    }
}
#[no_mangle]
pub unsafe extern fn divans_decompressor_free_usize(state_ptr: *mut DivansDecompressorState, data: *mut usize, size: usize) {
    if let Some(free_fn) = (*state_ptr).custom_allocator.free_func {
        free_fn((*state_ptr).custom_allocator.opaque, core::mem::transmute::<*mut usize, *mut c_void>(data));
    } else {
        alloc_util::free_stdlib(data, size);
    }
}

#[no_mangle]
pub unsafe extern fn divans_free_decompressor(state_ptr: *mut DivansDecompressorState) {
    if let Some(_) = (*state_ptr).custom_allocator.alloc_func {
        if let Some(free_fn) = (*state_ptr).custom_allocator.free_func {
            //(*state_ptr).drop();
            let _to_free = core::ptr::read(state_ptr);
            let ptr = core::mem::transmute::<*mut DivansDecompressorState, *mut c_void>(state_ptr);
            free_fn((*state_ptr).custom_allocator.opaque, ptr);
        }
    } else {
        free_decompressor_no_custom_alloc(state_ptr);
    }
}