blob: ce0ccd61e2d007938148979670a9a8d02ec4078b (
plain)
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
|
/// A wrapper for the output slice used when decompressing.
///
/// Using this rather than `Cursor` lets us implement the writing methods directly on
/// the buffer and lets us use a usize rather than u64 for the position which helps with
/// performance on 32-bit systems.
pub struct OutputBuffer<'a> {
slice: &'a mut [u8],
position: usize,
}
impl<'a> OutputBuffer<'a> {
#[inline]
pub fn from_slice_and_pos(slice: &'a mut [u8], position: usize) -> OutputBuffer<'a> {
OutputBuffer { slice, position }
}
#[inline(always)]
pub const fn position(&self) -> usize {
self.position
}
#[inline(always)]
pub fn set_position(&mut self, position: usize) {
self.position = position;
}
/// Write a byte to the current position and increment
///
/// Assumes that there is space.
#[inline]
pub fn write_byte(&mut self, byte: u8) {
self.slice[self.position] = byte;
self.position += 1;
}
/// Write a slice to the current position and increment
///
/// Assumes that there is space.
#[inline]
pub fn write_slice(&mut self, data: &[u8]) {
let len = data.len();
self.slice[self.position..self.position + len].copy_from_slice(data);
self.position += data.len();
}
#[inline]
pub const fn bytes_left(&self) -> usize {
self.slice.len() - self.position
}
#[inline(always)]
pub const fn get_ref(&self) -> &[u8] {
self.slice
}
#[inline(always)]
pub fn get_mut(&mut self) -> &mut [u8] {
self.slice
}
}
/// A wrapper for the output slice used when decompressing.
///
/// Using this rather than `Cursor` lets us implement the writing methods directly on
/// the buffer and lets us use a usize rather than u64 for the position which helps with
/// performance on 32-bit systems.
#[derive(Copy, Clone)]
pub struct InputWrapper<'a> {
slice: &'a [u8],
}
impl<'a> InputWrapper<'a> {
#[inline(always)]
pub const fn as_slice(&self) -> &[u8] {
self.slice
}
#[inline(always)]
pub const fn from_slice(slice: &'a [u8]) -> InputWrapper<'a> {
InputWrapper { slice }
}
#[inline(always)]
pub fn advance(&mut self, steps: usize) {
self.slice = &self.slice[steps..];
}
#[inline]
pub fn read_byte(&mut self) -> Option<u8> {
self.slice.first().map(|n| {
self.advance(1);
*n
})
}
#[inline]
#[cfg(target_pointer_width = "64")]
pub fn read_u32_le(&mut self) -> u32 {
let ret = {
let four_bytes: [u8; 4] = self.slice[..4].try_into().unwrap_or_default();
u32::from_le_bytes(four_bytes)
};
self.advance(4);
ret
}
#[inline(always)]
pub const fn bytes_left(&self) -> usize {
self.slice.len()
}
}
|