brotli-decompressor 6.0.1

A brotli decompressor that with an interface avoiding the rust stdlib. This makes it suitable for embedded devices and kernels. It is designed with a pluggable allocator so that the standard lib's allocator may be employed. The default build also includes a stdlib allocator and stream interface. Disable this with --features=no-stdlib. Alternatively, --features=unsafe turns off array bounds checks and memory initialization but provides a safe interface for the caller. Without adding the --features=unsafe argument, all included code is safe. For compression in addition to this library, download https://github.com/dropbox/rust-brotli
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
use core;
#[cfg(feature="std")]
use std;
use ::alloc;
use super::interface::{c_void, CAllocator};
#[cfg(feature="std")]
use std::vec::Vec;
#[cfg(feature="std")]
pub use std::boxed::Box;

#[cfg(feature="std")]
pub struct MemoryBlock<Ty:Sized+Default>(Box<[Ty]>);
#[cfg(feature="std")]
impl<Ty:Sized+Default> Default for MemoryBlock<Ty> {
    fn default() -> Self {
        MemoryBlock(Vec::<Ty>::new().into_boxed_slice())
    }
}
#[cfg(feature="std")]
impl<Ty:Sized+Default> alloc::SliceWrapper<Ty> for MemoryBlock<Ty> {
    fn slice(&self) -> &[Ty] {
        &self.0[..]
    }
}
#[cfg(feature="std")]
impl<Ty:Sized+Default> alloc::SliceWrapperMut<Ty> for MemoryBlock<Ty> {
    fn slice_mut(&mut self) -> &mut [Ty] {
        &mut self.0[..]
    }
}
#[cfg(feature="std")]
impl<Ty:Sized+Default> core::ops::Index<usize> for MemoryBlock<Ty> {
    type Output = Ty;
    fn index(&self, index:usize) -> &Ty {
        &self.0[index]
    }
}
#[cfg(feature="std")]
impl<Ty:Sized+Default> core::ops::IndexMut<usize> for MemoryBlock<Ty> {

    fn index_mut(&mut self, index:usize) -> &mut Ty {
        &mut self.0[index]
    }
}
#[cfg(feature="std")]
impl<Ty:Sized+Default> Drop for MemoryBlock<Ty> {
    fn drop (&mut self) {
        if self.0.len() != 0 {
            print!("leaking memory block of length {} element size: {}\n", self.0.len(), core::mem::size_of::<Ty>());

            let to_forget = core::mem::replace(self, MemoryBlock::default());
            core::mem::forget(to_forget);// leak it -- it's the only safe way with custom allocators
        }
    }
}

// Fallible counterpart of vec![Ty::default(); size].into_boxed_slice(). vec!
// reports allocation failure through handle_alloc_error, which aborts the
// process (catch_unwind cannot intercept it), so a decoder created without C
// allocator callbacks crashed its host whenever a large allocation such as the
// ring buffer (up to 16 MiB, or 1 GiB with large windows) failed. Returning
// None lets callers report BROTLI_DECODER_ERROR_ALLOC_* exactly as they do
// when a C allocator returns null.
#[cfg(feature = "std")]
fn try_alloc_default_slice<Ty: Sized + Default + Clone>(size: usize) -> Option<Box<[Ty]>> {
    let mut elements = if size == 0 || core::mem::size_of::<Ty>() == 0 {
        // Nothing to allocate: this Vec never touches the allocator.
        Vec::new()
    } else {
        let layout = match std::alloc::Layout::array::<Ty>(size) {
            Ok(layout) => layout,
            Err(_) => return None, // more than isize::MAX bytes
        };
        // Zeroed, as vec! requests for zero-valued elements, so that where the
        // optimizer can see the allocator (e.g. with LTO) the fill below folds
        // away and untouched pages stay uncommitted.
        let data = unsafe { std::alloc::alloc_zeroed(layout) } as *mut Ty;
        if data.is_null() {
            return None;
        }
        // SAFETY: data comes from the global allocator with the layout of
        // `size` elements of Ty, and length 0 exposes no uninitialized Ty.
        unsafe { Vec::from_raw_parts(data, 0, size) }
    };
    // Generic code cannot tell whether all-zero bytes are a valid Ty, so every
    // element is initialized. elements.len() counts the initialized prefix, so
    // if Ty::default() panics, dropping the Vec drops exactly those elements
    // and frees the buffer. It is a plain loop rather than resize/resize_with
    // because in this crate's LTO builds only the loop's zero fill is optimized
    // away after alloc_zeroed; the others kept it, committing every page.
    for index in 0..size {
        unsafe {
            core::ptr::write(elements.as_mut_ptr().add(index), Ty::default());
            elements.set_len(index + 1);
        }
    }
    // The capacity already fits `size`, so this does not reallocate.
    Some(elements.into_boxed_slice())
}

pub struct SubclassableAllocator {
    alloc: CAllocator
    // have alternative ty here
}

impl SubclassableAllocator {
    pub unsafe fn new(sub_alloc:CAllocator) -> Self {
        SubclassableAllocator{
            alloc:sub_alloc,
        }
    }
}
#[cfg(feature="std")]
impl<Ty:Sized+Default+Clone> alloc::Allocator<Ty> for SubclassableAllocator {
    type AllocatedMemory = MemoryBlock<Ty>;
    fn alloc_cell(&mut self, size:usize) ->MemoryBlock<Ty>{
        if size == 0 {
            return MemoryBlock::<Ty>::default();
        }
        if let Some(alloc_fn) = self.alloc.alloc_func {
            let alloc_size = match size.checked_mul(core::mem::size_of::<Ty>()) {
                Some(alloc_size) => alloc_size,
                None => return MemoryBlock::<Ty>::default(),
            };
            let ptr = alloc_fn(self.alloc.opaque, alloc_size);
            if ptr.is_null() {
                return MemoryBlock::<Ty>::default();
            }
            let typed_ptr = unsafe {core::mem::transmute::<*mut c_void, *mut Ty>(ptr)};
            let slice_ref = unsafe {super::slice_from_raw_parts_or_nil_mut(typed_ptr, size)};
            for item in slice_ref.iter_mut() {
                unsafe{core::ptr::write(item, Ty::default())};
            }
            return MemoryBlock(unsafe{Box::from_raw(slice_ref)})
        }
        match try_alloc_default_slice(size) {
            Some(data) => MemoryBlock(data),
            None => MemoryBlock::<Ty>::default(),
        }
    }
    fn free_cell(&mut self, mut bv:MemoryBlock<Ty>) {
        if (*bv.0).len() != 0 {
            if let Some(_) = self.alloc.alloc_func {
                let slice_ptr = (*bv.0).as_mut_ptr();
                let _box_ptr = Box::into_raw(core::mem::replace(&mut bv.0, Vec::<Ty>::new().into_boxed_slice()));
                if let Some(free_fn) = self.alloc.free_func {
                    unsafe {free_fn(self.alloc.opaque, core::mem::transmute::<*mut Ty, *mut c_void>(slice_ptr))};
                }
            } else {
                let _to_free = core::mem::replace(&mut bv.0, Vec::<Ty>::new().into_boxed_slice());
            }
        }
    }
}











#[cfg(not(feature="std"))]
pub struct MemoryBlock<Ty:Sized+Default>(*mut[Ty]);
#[cfg(not(feature="std"))]
impl<Ty:Sized+Default> Default for MemoryBlock<Ty> {
    fn default() -> Self {
        // Even an empty slice reference needs a non-null, Ty-aligned pointer.
        MemoryBlock(core::ptr::slice_from_raw_parts_mut(
            core::ptr::NonNull::<Ty>::dangling().as_ptr(), 0))
    }
}
#[cfg(not(feature="std"))]
impl<Ty:Sized+Default> alloc::SliceWrapper<Ty> for MemoryBlock<Ty> {
    fn slice(&self) -> &[Ty] {
        // The pointer is either an aligned empty slice or an initialized
        // allocation from alloc_cell; the borrow cannot outlive this block.
        unsafe { &*self.0 }
    }
}
#[cfg(not(feature="std"))]
impl<Ty:Sized+Default> alloc::SliceWrapperMut<Ty> for MemoryBlock<Ty> {
    fn slice_mut(&mut self) -> &mut [Ty] {
        // As above, with exclusive access guaranteed by the mutable borrow.
        unsafe { &mut *self.0 }
    }
}

#[cfg(not(feature="std"))]
#[cfg(feature="no-stdlib-ffi-binding")]
#[panic_handler]
extern fn panic_impl(_: &::core::panic::PanicInfo) -> ! {
    loop {}
}
#[cfg(not(feature="std"))]
#[cfg(feature="no-stdlib-ffi-binding")]
#[lang = "eh_personality"]
extern "C" fn eh_personality() {
}

#[cfg(not(feature="std"))]
impl<Ty:Sized+Default> core::ops::Index<usize> for MemoryBlock<Ty> {
    type Output = Ty;
    fn index(&self, index:usize) -> &Ty {
        &alloc::SliceWrapper::slice(self)[index]
    }
}
#[cfg(not(feature="std"))]
impl<Ty:Sized+Default> core::ops::IndexMut<usize> for MemoryBlock<Ty> {

    fn index_mut(&mut self, index:usize) -> &mut Ty {
        &mut alloc::SliceWrapperMut::slice_mut(self)[index]
    }
}

#[cfg(not(feature="std"))]
impl<Ty:Sized+Default+Clone> alloc::Allocator<Ty> for SubclassableAllocator {
    type AllocatedMemory = MemoryBlock<Ty>;
    fn alloc_cell(&mut self, size:usize) ->MemoryBlock<Ty>{
        if size == 0 {
            return MemoryBlock::<Ty>::default();
        }
        if let Some(alloc_fn) = self.alloc.alloc_func {
            let alloc_size = match size.checked_mul(core::mem::size_of::<Ty>()) {
                Some(alloc_size) => alloc_size,
                None => return MemoryBlock::<Ty>::default(),
            };
            let ptr = alloc_fn(self.alloc.opaque, alloc_size);
            if ptr.is_null() {
                return MemoryBlock::<Ty>::default();
            }
            let typed_ptr = unsafe {core::mem::transmute::<*mut c_void, *mut Ty>(ptr)};
            let slice_ref = unsafe {super::slice_from_raw_parts_or_nil_mut(typed_ptr, size)};
            for item in slice_ref.iter_mut() {
                unsafe{core::ptr::write(item, Ty::default())};
            }
            return MemoryBlock(slice_ref)
        } else {
            panic!("Must provide allocators in no-stdlib code");
        }
    }
    fn free_cell(&mut self, mut bv:MemoryBlock<Ty>) {
        use alloc::SliceWrapper;
        use alloc::SliceWrapperMut;
        if bv.slice().len() != 0 {
            if let Some(_) = self.alloc.alloc_func {
                if let Some(free_fn) = self.alloc.free_func {
                    unsafe {free_fn(self.alloc.opaque, core::mem::transmute::<*mut Ty, *mut c_void>(&mut bv.slice_mut()[0]))};
                }
                let _ = core::mem::replace(&mut bv,
                                           MemoryBlock::<Ty>::default());
            } else {
                panic!("Must provide allocators in no-stdlib code");
            }
        }
    }
}


#[cfg(not(feature="std"))]
pub fn free_stdlib<T>(_data: *mut T, _size: usize) {
    panic!("Must supply allocators if calling divans when compiled with features=no-stdlib");
}
#[cfg(not(feature="std"))]
pub fn alloc_stdlib<T:Sized+Default+Copy+Clone>(_size: usize) -> *mut T {
    panic!("Must supply allocators if calling divans when compiled with features=no-stdlib");
}

#[cfg(feature="std")]
pub unsafe fn free_stdlib<T>(ptr: *mut T, size: usize) {
    if ptr.is_null() {
        return;
    }
    let slice_ref = super::slice_from_raw_parts_or_nil_mut(ptr, size);
    let _ = Box::from_raw(slice_ref); // free on drop
}
#[cfg(feature="std")]
pub fn alloc_stdlib<T:Sized+Default+Copy+Clone>(size: usize) -> *mut T {
    match try_alloc_default_slice::<T>(size) {
        Some(newly_allocated) => Box::into_raw(newly_allocated) as *mut T,
        None => core::ptr::null_mut(),
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use ::alloc::{Allocator, SliceWrapper, SliceWrapperMut};

    #[repr(align(64))]
    #[derive(Clone, Default)]
    struct OverAligned {
        _byte: u8,
    }

    fn assert_empty_block<Ty: Sized + Default>(mut block: MemoryBlock<Ty>) {
        #[cfg(not(feature="std"))]
        {
            // Inspect the raw pointer first, so a regression fails without
            // creating an invalid reference just to check the slice length.
            let data = block.0 as *mut Ty;
            assert!(!data.is_null());
            assert_eq!(data as usize % core::mem::align_of::<Ty>(), 0);
        }
        assert!(block.slice().is_empty());
        assert_eq!(block.slice().as_ptr() as usize % core::mem::align_of::<Ty>(), 0);
        let slice = block.slice_mut();
        assert!(slice.is_empty());
        assert_eq!(slice.as_mut_ptr() as usize % core::mem::align_of::<Ty>(), 0);
    }

    #[test]
    fn default_empty_blocks_are_aligned_for_their_element_type() {
        assert_empty_block(MemoryBlock::<u8>::default());
        assert_empty_block(MemoryBlock::<u32>::default());
        assert_empty_block(MemoryBlock::<super::super::HuffmanCode>::default());
        assert_empty_block(MemoryBlock::<OverAligned>::default());
    }

    #[cfg(not(feature="std"))]
    #[test]
    fn nonempty_block_supports_slices_and_indexing() {
        let mut data = [1u32, 2, 3];
        {
            let mut block = MemoryBlock(&mut data[..] as *mut [u32]);
            assert_eq!(block.slice(), &[1, 2, 3]);
            assert_eq!(block[1], 2);
            block[1] = 4;
            block.slice_mut()[2] = 5;
            assert_eq!(block.slice(), &[1, 4, 5]);
        }
        assert_eq!(data, [1, 4, 5]);
    }

    extern "C" fn failing_alloc(_data: *mut c_void, _size: usize) -> *mut c_void {
        core::ptr::null_mut()
    }

    #[test]
    fn failed_custom_allocation_returns_empty_block() {
        let c_allocator = CAllocator {
            alloc_func: Some(failing_alloc),
            free_func: None,
            opaque: core::ptr::null_mut(),
        };
        let mut allocator = unsafe { SubclassableAllocator::new(c_allocator) };
        let block =
            <SubclassableAllocator as Allocator<u8>>::alloc_cell(&mut allocator, 1);

        assert_eq!(block.slice().len(), 0);
    }

    #[test]
    fn zero_and_failed_allocations_return_aligned_empty_blocks() {
        let mut allocator = unsafe {
            SubclassableAllocator::new(CAllocator {
                alloc_func: Some(failing_alloc),
                free_func: None,
                opaque: core::ptr::null_mut(),
            })
        };
        for &size in &[0usize, 1, usize::MAX] {
            let block = <SubclassableAllocator as Allocator<OverAligned>>::alloc_cell(
                &mut allocator, size);
            assert_empty_block(block);
        }
    }

    // Without C callbacks alloc_cell used vec!, which panics ("capacity
    // overflow") on sizes it cannot represent and aborts when the allocation
    // itself fails, instead of returning the empty block callers check for.
    #[cfg(feature = "std")]
    #[test]
    fn default_allocator_failures_return_empty_blocks() {
        let mut allocator = unsafe {
            SubclassableAllocator::new(CAllocator {
                alloc_func: None,
                free_func: None,
                opaque: core::ptr::null_mut(),
            })
        };
        assert_empty_block(<SubclassableAllocator as Allocator<u8>>::alloc_cell(
            &mut allocator,
            isize::MAX as usize + 1,
        ));
        assert_empty_block(<SubclassableAllocator as Allocator<u32>>::alloc_cell(
            &mut allocator,
            usize::MAX,
        ));
        assert_empty_block(
            <SubclassableAllocator as Allocator<OverAligned>>::alloc_cell(
                &mut allocator,
                usize::MAX,
            ),
        );
        assert!(alloc_stdlib::<u8>(isize::MAX as usize + 1).is_null());

        let block = <SubclassableAllocator as Allocator<u32>>::alloc_cell(&mut allocator, 5);
        assert_eq!(block.slice(), &[0u32; 5]);
        <SubclassableAllocator as Allocator<u32>>::free_cell(&mut allocator, block);
        let block =
            <SubclassableAllocator as Allocator<OverAligned>>::alloc_cell(&mut allocator, 3);
        assert_eq!(block.slice().len(), 3);
        assert_eq!(
            block.slice().as_ptr() as usize % core::mem::align_of::<OverAligned>(),
            0
        );
        <SubclassableAllocator as Allocator<OverAligned>>::free_cell(&mut allocator, block);
    }

    // Default and Clone both make a new value, and the third one panics.
    #[cfg(feature = "std")]
    mod panicking_element {
        use super::*;
        use std::cell::Cell;

        thread_local! {
            static MADE: Cell<usize> = Cell::new(0);
            static DROPS: Cell<usize> = Cell::new(0);
        }

        struct PanicsOnThirdValue {
            _value: u32,
        }

        impl PanicsOnThirdValue {
            fn new() -> Self {
                let made = MADE.with(|count| count.replace(count.get() + 1));
                assert!(made < 2, "third value");
                PanicsOnThirdValue { _value: 7 }
            }
        }

        impl Default for PanicsOnThirdValue {
            fn default() -> Self {
                PanicsOnThirdValue::new()
            }
        }

        impl Clone for PanicsOnThirdValue {
            fn clone(&self) -> Self {
                PanicsOnThirdValue::new()
            }
        }

        impl Drop for PanicsOnThirdValue {
            fn drop(&mut self) {
                DROPS.with(|count| count.set(count.get() + 1));
            }
        }

        fn alloc_five() -> MemoryBlock<PanicsOnThirdValue> {
            let mut allocator = unsafe {
                SubclassableAllocator::new(CAllocator {
                    alloc_func: None,
                    free_func: None,
                    opaque: core::ptr::null_mut(),
                })
            };
            <SubclassableAllocator as Allocator<PanicsOnThirdValue>>::alloc_cell(&mut allocator, 5)
        }

        // The panic reaches the caller instead of becoming an allocation
        // failure. This also runs under panic=abort, where the panic ends the
        // process that the test harness starts for this test.
        #[test]
        #[should_panic(expected = "third value")]
        fn default_allocator_propagates_the_panic() {
            alloc_five();
        }

        // With unwinding, the values made before the panic must not leak; vec!,
        // which alloc_cell used before, drops them too. Under panic=abort
        // nothing unwinds, so there is nothing to drop.
        #[cfg(panic = "unwind")]
        #[test]
        fn default_allocator_drops_the_values_made_before_the_panic() {
            assert!(std::panic::catch_unwind(alloc_five).is_err());
            assert_eq!(MADE.with(|count| count.get()), 3);
            assert_eq!(DROPS.with(|count| count.get()), 2);
        }
    }
}