ionic/ion/decoder/utilities/
byte_source.rs1use std::sync::Arc;
2
3use crate::ion::{ByteRange, IonError, IonResult};
4
5pub trait ReadBytes: Send + Sync {
6 fn read(&self, range: ByteRange) -> IonResult<SourceBytes>;
7 fn total_len(&self) -> Option<u64>;
8}
9
10#[derive(Clone)]
11pub enum SourceBytes {
12 Owned(Vec<u8>),
13 Memory {
14 data: Arc<[u8]>,
15 start: usize,
16 end: usize,
17 },
18 #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
19 Mapped {
20 map: Arc<memmap2::Mmap>,
21 start: usize,
22 end: usize,
23 },
24}
25
26impl std::ops::Deref for SourceBytes {
27 type Target = [u8];
28 fn deref(&self) -> &[u8] {
29 match self {
30 SourceBytes::Owned(bytes) => bytes,
31 SourceBytes::Memory { data, start, end } => &data[*start..*end],
32 #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
33 SourceBytes::Mapped { map, start, end } => &map[*start..*end],
34 }
35 }
36}
37
38impl SourceBytes {
39 pub fn into_arc(self) -> Arc<[u8]> {
40 match self {
41 SourceBytes::Owned(bytes) => Arc::from(bytes),
42 SourceBytes::Memory { data, start, end } => Arc::from(&data[start..end]),
43 #[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
44 SourceBytes::Mapped { map, start, end } => Arc::from(&map[start..end]),
45 }
46 }
47}
48
49pub struct BytesSource {
50 data: Arc<[u8]>,
51}
52
53impl BytesSource {
54 pub fn new(data: Arc<[u8]>) -> Self {
55 Self { data }
56 }
57}
58
59impl ReadBytes for BytesSource {
60 fn read(&self, range: ByteRange) -> IonResult<SourceBytes> {
61 let start = to_usize(range.offset, "offset")?;
62 let end = start
63 .checked_add(to_usize(range.length, "length")?)
64 .ok_or_else(|| IonError::from("byte source: range overflows"))?;
65 if self.data.get(start..end).is_some() {
66 Ok(SourceBytes::Memory {
67 data: self.data.clone(),
68 start,
69 end,
70 })
71 } else {
72 Err(IonError::from("byte source: read out of bounds"))
73 }
74 }
75
76 fn total_len(&self) -> Option<u64> {
77 Some(self.data.len() as u64)
78 }
79}
80
81#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
82pub struct FileSource {
83 map: Arc<memmap2::Mmap>,
84}
85
86#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
87impl FileSource {
88 pub fn new(map: memmap2::Mmap) -> Self {
89 Self { map: Arc::new(map) }
90 }
91}
92
93#[cfg(not(all(target_arch = "wasm32", not(target_os = "wasi"))))]
94impl ReadBytes for FileSource {
95 fn read(&self, range: ByteRange) -> IonResult<SourceBytes> {
96 let start = to_usize(range.offset, "offset")?;
97 let end = start
98 .checked_add(to_usize(range.length, "length")?)
99 .ok_or_else(|| IonError::from("byte source: range overflows"))?;
100 if self.map.get(start..end).is_some() {
101 Ok(SourceBytes::Mapped {
102 map: self.map.clone(),
103 start,
104 end,
105 })
106 } else {
107 Err(IonError::from("byte source: read out of bounds"))
108 }
109 }
110
111 fn total_len(&self) -> Option<u64> {
112 Some(self.map.len() as u64)
113 }
114}
115
116pub type RangeReader = dyn Fn(ByteRange) -> IonResult<Vec<u8>> + Send + Sync;
117
118pub struct CallbackSource {
119 read: Box<RangeReader>,
120}
121
122impl CallbackSource {
123 pub fn new(read: impl Fn(ByteRange) -> IonResult<Vec<u8>> + Send + Sync + 'static) -> Self {
124 Self {
125 read: Box::new(read),
126 }
127 }
128}
129
130impl ReadBytes for CallbackSource {
131 fn read(&self, range: ByteRange) -> IonResult<SourceBytes> {
132 let bytes = (self.read)(range)?;
133 allow_len(&bytes, range.length)?;
134 Ok(SourceBytes::Owned(bytes))
135 }
136
137 fn total_len(&self) -> Option<u64> {
138 None
139 }
140}
141
142fn allow_len(bytes: &[u8], length: u64) -> IonResult<()> {
143 let expected_len = to_usize(length, "length")?;
144 if bytes.len() != expected_len {
145 return Err(IonError::from(format!(
146 "byte source: expected {expected_len} bytes, got {}",
147 bytes.len()
148 )));
149 }
150 Ok(())
151}
152
153fn to_usize(value: u64, name: &str) -> IonResult<usize> {
154 usize::try_from(value).map_err(|_| IonError::from(format!("byte source: {name} out of range")))
155}