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
#![cfg(not(feature="safe"))]
#[no_mangle]
#[cfg(feature="std")]
use std::{thread,panic, io};
#[cfg(feature="std")]
use std::io::Write;
use core;
use core::slice;
pub mod interface;
pub mod alloc_util;
use self::alloc_util::SubclassableAllocator;
use alloc::Allocator;
use self::interface::{CAllocator, c_void, BrotliDecoderParameter, BrotliDecoderResult, brotli_alloc_func, brotli_free_func};
use ::BrotliResult;
pub use super::state::BrotliDecoderErrorCode;
#[repr(C)]
#[no_mangle]
pub struct BrotliDecoderState {
pub custom_allocator: CAllocator,
pub decompressor: ::BrotliState<SubclassableAllocator,
SubclassableAllocator,
SubclassableAllocator>,
}
#[cfg(not(feature="std"))]
fn brotli_new_decompressor_without_custom_alloc(_to_box: BrotliDecoderState) -> *mut BrotliDecoderState{
panic!("Must supply allocators if calling divans when compiled without features=std");
}
#[cfg(feature="std")]
fn brotli_new_decompressor_without_custom_alloc(to_box: BrotliDecoderState) -> *mut BrotliDecoderState{
alloc_util::Box::<BrotliDecoderState>::into_raw(
alloc_util::Box::<BrotliDecoderState>::new(to_box))
}
#[no_mangle]
pub unsafe extern fn BrotliDecoderCreateInstance(
alloc_func: brotli_alloc_func,
free_func: brotli_free_func,
opaque: *mut c_void,
) -> *mut BrotliDecoderState {
match catch_panic_state(|| {
let allocators = CAllocator {
alloc_func:alloc_func,
free_func:free_func,
opaque:opaque,
};
let custom_dictionary = <SubclassableAllocator as Allocator<u8>>::AllocatedMemory::default();
let to_box = BrotliDecoderState {
custom_allocator: allocators.clone(),
decompressor: ::BrotliState::new_with_custom_dictionary(
SubclassableAllocator::new(allocators.clone()),
SubclassableAllocator::new(allocators.clone()),
SubclassableAllocator::new(allocators.clone()),
custom_dictionary,
),
};
if let Some(alloc) = alloc_func {
if free_func.is_none() {
panic!("either both alloc and free must exist or neither");
}
let ptr = alloc(allocators.opaque, core::mem::size_of::<BrotliDecoderState>());
let brotli_decoder_state_ptr = core::mem::transmute::<*mut c_void, *mut BrotliDecoderState>(ptr);
core::ptr::write(brotli_decoder_state_ptr, to_box);
brotli_decoder_state_ptr
} else {
brotli_new_decompressor_without_custom_alloc(to_box)
}
}) {
Ok(ret) => ret,
Err(e) => {
error_print(e);
core::ptr::null_mut()
},
}
}
#[no_mangle]
pub unsafe extern fn BrotliDecoderSetParameter(_state_ptr: *mut BrotliDecoderState,
_selector: BrotliDecoderParameter,
_value: u32) {
}
#[cfg(not(feature="std"))] #[no_mangle]
pub unsafe extern fn BrotliDecoderDecompress(
_encoded_size: usize,
_encoded_buffer: *const u8,
_decoded_size: *mut usize,
_decoded_buffer: *mut u8,
) -> BrotliDecoderResult {
BrotliDecoderResult::BROTLI_DECODER_RESULT_ERROR }
#[cfg(feature="std")] #[no_mangle]
pub unsafe extern fn BrotliDecoderDecompress(
encoded_size: usize,
encoded_buffer: *const u8,
decoded_size: *mut usize,
decoded_buffer: *mut u8) -> BrotliDecoderResult {
let mut total_out = 0;
let mut available_in = encoded_size;
let mut next_in = encoded_buffer;
let mut available_out = *decoded_size;
let mut next_out = decoded_buffer;
let s = BrotliDecoderCreateInstance(
None,
None,
core::ptr::null_mut(),
);
if s.is_null() { return BrotliDecoderResult::BROTLI_DECODER_RESULT_ERROR;
}
let result = BrotliDecoderDecompressStream(
s, &mut available_in, &mut next_in, &mut available_out, &mut next_out, &mut total_out);
*decoded_size = total_out;
BrotliDecoderDestroyInstance(s);
if let BrotliDecoderResult::BROTLI_DECODER_RESULT_SUCCESS = result {
BrotliDecoderResult::BROTLI_DECODER_RESULT_SUCCESS
} else {
BrotliDecoderResult::BROTLI_DECODER_RESULT_ERROR
}
}
#[cfg(all(feature="std", not(feature="pass-through-ffi-panics")))]
fn catch_panic<F:FnOnce()->BrotliDecoderResult+panic::UnwindSafe>(f: F) -> thread::Result<BrotliDecoderResult> {
panic::catch_unwind(f)
}
#[cfg(all(feature="std", not(feature="pass-through-ffi-panics")))]
fn catch_panic_state<F:FnOnce()->*mut BrotliDecoderState+panic::UnwindSafe>(f: F) -> thread::Result<*mut BrotliDecoderState> {
panic::catch_unwind(f)
}
#[cfg(all(feature="std", not(feature="pass-through-ffi-panics")))]
fn error_print<Err:core::fmt::Debug>(err: Err) {
let _ign = writeln!(&mut io::stderr(), "Internal Error {:?}", err);
}
#[cfg(any(not(feature="std"), feature="pass-through-ffi-panics"))]
fn catch_panic<F:FnOnce()->BrotliDecoderResult>(f: F) -> Result<BrotliDecoderResult, ()> {
Ok(f())
}
#[cfg(any(not(feature="std"), feature="pass-through-ffi-panics"))]
fn catch_panic_state<F:FnOnce()->*mut BrotliDecoderState>(f: F) -> Result<*mut BrotliDecoderState, ()> {
Ok(f())
}
#[cfg(any(not(feature="std"), feature="pass-through-ffi-panics"))]
fn error_print<Err>(_err: Err) {
}
#[no_mangle]
pub unsafe extern fn BrotliDecoderDecompressStream(
state_ptr: *mut BrotliDecoderState,
available_in: *mut usize,
input_buf_ptr: *mut*const u8,
available_out: *mut usize,
output_buf_ptr: *mut*mut u8,
mut total_out: *mut usize) -> BrotliDecoderResult {
match catch_panic(move || {
let mut input_offset = 0usize;
let mut output_offset = 0usize;
let mut fallback_total_out = 0usize;
if total_out.is_null() {
total_out = &mut fallback_total_out;
}
let result;
{
let input_buf = slice::from_raw_parts(*input_buf_ptr, *available_in);
let output_buf = slice::from_raw_parts_mut(*output_buf_ptr, *available_out);
result = match super::decode::BrotliDecompressStream(
&mut *available_in,
&mut input_offset,
input_buf,
&mut *available_out,
&mut output_offset,
output_buf,
&mut *total_out,
&mut (*state_ptr).decompressor,
) {
BrotliResult::ResultSuccess => BrotliDecoderResult::BROTLI_DECODER_RESULT_SUCCESS,
BrotliResult::ResultFailure => BrotliDecoderResult::BROTLI_DECODER_RESULT_ERROR,
BrotliResult::NeedsMoreInput => BrotliDecoderResult::BROTLI_DECODER_RESULT_NEEDS_MORE_INPUT ,
BrotliResult::NeedsMoreOutput => BrotliDecoderResult::BROTLI_DECODER_RESULT_NEEDS_MORE_OUTPUT ,
};
}
*input_buf_ptr = (*input_buf_ptr).offset(input_offset as isize);
*output_buf_ptr = (*output_buf_ptr).offset(output_offset as isize);
result
}) {
Ok(ret) => ret,
Err(readable_err) => { error_print(readable_err);
(*state_ptr).decompressor.error_code = BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE;
BrotliDecoderResult::BROTLI_DECODER_RESULT_ERROR
}
}
}
#[cfg(feature="std")]
unsafe fn free_decompressor_no_custom_alloc(state_ptr: *mut BrotliDecoderState) {
let _state = alloc_util::Box::from_raw(state_ptr);
}
#[cfg(not(feature="std"))]
unsafe fn free_decompressor_no_custom_alloc(_state_ptr: *mut BrotliDecoderState) {
unreachable!();
}
#[no_mangle]
pub unsafe extern fn BrotliDecoderMallocU8(state_ptr: *mut BrotliDecoderState, 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 BrotliDecoderFreeU8(state_ptr: *mut BrotliDecoderState, 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 BrotliDecoderMallocUsize(state_ptr: *mut BrotliDecoderState, 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 BrotliDecoderFreeUsize(state_ptr: *mut BrotliDecoderState, 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 BrotliDecoderDestroyInstance(state_ptr: *mut BrotliDecoderState) {
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 BrotliDecoderState, *mut c_void>(state_ptr);
free_fn((*state_ptr).custom_allocator.opaque, ptr);
}
} else {
free_decompressor_no_custom_alloc(state_ptr);
}
}
#[no_mangle]
pub unsafe extern fn BrotliDecoderHasMoreOutput(state_ptr: *const BrotliDecoderState) -> i32 {
if super::decode::BrotliDecoderHasMoreOutput(&(*state_ptr).decompressor) {1} else {0}
}
#[no_mangle]
pub unsafe extern fn BrotliDecoderTakeOutput(state_ptr: *mut BrotliDecoderState, size: *mut usize) -> *const u8 {
super::decode::BrotliDecoderTakeOutput(&mut (*state_ptr).decompressor, &mut *size).as_ptr()
}
#[no_mangle]
pub unsafe extern fn BrotliDecoderIsUsed(state_ptr: *const BrotliDecoderState) -> i32 {
if super::decode::BrotliDecoderIsUsed(&(*state_ptr).decompressor) {1} else {0}
}
#[no_mangle]
pub unsafe extern fn BrotliDecoderIsFinished(state_ptr: *const BrotliDecoderState) -> i32 {
if super::decode::BrotliDecoderIsFinished(&(*state_ptr).decompressor) {1} else {0}
}
#[no_mangle]
pub unsafe extern fn BrotliDecoderGetErrorCode(state_ptr: *const BrotliDecoderState) -> BrotliDecoderErrorCode {
super::decode::BrotliDecoderGetErrorCode(&(*state_ptr).decompressor)
}
#[no_mangle]
pub extern fn BrotliDecoderErrorString(c: BrotliDecoderErrorCode) -> *const u8 {
match c {
BrotliDecoderErrorCode::BROTLI_DECODER_NO_ERROR => "NO_ERROR\0",
BrotliDecoderErrorCode::BROTLI_DECODER_SUCCESS => "SUCCESS\0",
BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_INPUT => "NEEDS_MORE_INPUT\0",
BrotliDecoderErrorCode::BROTLI_DECODER_NEEDS_MORE_OUTPUT => "NEEDS_MORE_OUTPUT\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_NIBBLE => "ERROR_FORMAT_EXUBERANT_NIBBLE\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_RESERVED => "ERROR_FORMAT_RESERVED\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_EXUBERANT_META_NIBBLE => "ERROR_FORMAT_EXUBERANT_META_NIBBLE\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET => "ERROR_FORMAT_SIMPLE_HUFFMAN_ALPHABET\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_SIMPLE_HUFFMAN_SAME => "ERROR_FORMAT_SIMPLE_HUFFMAN_SAME\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_CL_SPACE => "ERROR_FORMAT_FL_SPACE\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_HUFFMAN_SPACE => "ERROR_FORMAT_HUFFMAN_SPACE\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_CONTEXT_MAP_REPEAT => "ERROR_FORMAT_CONTEXT_MAP_REPEAT\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_1 =>"ERROR_FORMAT_BLOCK_LENGTH_1\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_BLOCK_LENGTH_2 =>"ERROR_FORMAT_BLOCK_LENGTH_2\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_TRANSFORM => "ERROR_FORMAT_TRANSFORM\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_DICTIONARY =>"ERROR_FORMAT_DICTIONARY\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_WINDOW_BITS =>"ERROR_FORMAT_WINDOW_BITS\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_PADDING_1 =>"ERROR_FORMAT_PADDING_1\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_PADDING_2 =>"ERROR_FORMAT_PADDING_2\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_FORMAT_DISTANCE =>"ERROR_FORMAT_DISTANCE\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_DICTIONARY_NOT_SET => "ERROR_DICTIONARY_NOT_SET\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_INVALID_ARGUMENTS => "ERROR_INVALID_ARGUMENTS\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MODES => "ERROR_ALLOC_CONTEXT_MODES\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_TREE_GROUPS => "ERROR_ALLOC_TREE_GROUPS\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_CONTEXT_MAP => "ERROR_ALLOC_CONTEXT_MAP\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_1 => "ERROR_ALLOC_RING_BUFFER_1\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_RING_BUFFER_2 => "ERROR_ALLOC_RING_BUFFER_2\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_ALLOC_BLOCK_TYPE_TREES => "ERROR_ALLOC_BLOCK_TYPE_TREES\0",
BrotliDecoderErrorCode::BROTLI_DECODER_ERROR_UNREACHABLE => "ERROR_UNREACHABLE\0",
}.as_ptr()
}
#[no_mangle]
pub extern fn BrotliDecoderVersion() -> u32 {
0x1000f00
}