object/read/read_ref.rs
1#![allow(clippy::len_without_is_empty)]
2
3use core::convert::TryInto;
4use core::ops::Range;
5use core::{mem, result};
6
7use crate::pod::{from_bytes, slice_from_bytes, Pod};
8
9type Result<T> = result::Result<T, ()>;
10
11/// A trait for reading references to [`Pod`] types from a block of data.
12///
13/// This allows parsers to handle both of these cases:
14/// - the block of data exists in memory, and it is desirable
15/// to use references to this block instead of copying it,
16/// - the block of data exists in storage, and it is desirable
17/// to read on demand to minimize I/O and memory usage.
18///
19/// A block of data typically exists in memory as a result of using a memory
20/// mapped file, and the crate was written with this use case in mind.
21/// Reading the entire file into a `Vec` is also possible, but it often uses
22/// more I/O and memory.
23/// Both of these are handled by the `ReadRef` implementation for `&[u8]`.
24///
25/// For the second use case, the `ReadRef` trait is implemented for
26/// [`&ReadCache`](super::ReadCache). This is useful for environments where
27/// memory mapped files are not available or not suitable, such as WebAssembly.
28/// This differs from reading into a `Vec` in that it only reads the portions
29/// of the file that are needed for parsing.
30///
31/// The methods accept `self` by value because `Self` is expected to behave
32/// similar to a reference: it may be a reference with a lifetime of `'a`,
33/// or it may be a wrapper of a reference.
34///
35/// The `Clone` and `Copy` bounds are for convenience, and since `Self` is
36/// expected to be similar to a reference, these are easily satisfied.
37///
38/// Object file parsers typically use offsets to locate the structures
39/// in the block, and will most commonly use the `*_at` methods to
40/// read a structure at a known offset.
41///
42/// Occasionally file parsers will need to treat the block as a stream,
43/// and so convenience methods are provided that update an offset with
44/// the size that was read.
45//
46// An alternative would be for methods to accept `&mut self` and use a
47// `seek` method instead of the `offset` parameters, but this is less
48// convenient for implementers.
49pub trait ReadRef<'a>: Clone + Copy {
50 /// The total size of the block of data.
51 fn len(self) -> Result<u64>;
52
53 /// Get a reference to a `u8` slice at the given offset.
54 ///
55 /// Returns an error if offset or size are out of bounds.
56 fn read_bytes_at(self, offset: u64, size: u64) -> Result<&'a [u8]>;
57
58 /// Get a reference to a delimited `u8` slice which starts at range.start.
59 ///
60 /// Does not include the delimiter.
61 ///
62 /// Returns an error if the range is out of bounds or the delimiter is
63 /// not found in the range.
64 fn read_bytes_at_until(self, range: Range<u64>, delimiter: u8) -> Result<&'a [u8]>;
65
66 /// Get a reference to a `u8` slice at the given offset, and update the offset.
67 ///
68 /// Returns an error if offset or size are out of bounds.
69 fn read_bytes(self, offset: &mut u64, size: u64) -> Result<&'a [u8]> {
70 let bytes = self.read_bytes_at(*offset, size)?;
71 *offset = offset.wrapping_add(size);
72 Ok(bytes)
73 }
74
75 /// Get a reference to a `Pod` type at the given offset, and update the offset.
76 ///
77 /// Returns an error if offset or size are out of bounds.
78 ///
79 /// The default implementation uses `read_bytes`, and returns an error if
80 /// `read_bytes` does not return bytes with the correct alignment for `T`.
81 /// Implementors may want to provide their own implementation that ensures
82 /// the alignment can be satisfied. Alternatively, only use this method with
83 /// types that do not need alignment (see the `unaligned` feature of this crate).
84 fn read<T: Pod>(self, offset: &mut u64) -> Result<&'a T> {
85 let size = mem::size_of::<T>().try_into().map_err(|_| ())?;
86 let bytes = self.read_bytes(offset, size)?;
87 let (t, _) = from_bytes(bytes)?;
88 Ok(t)
89 }
90
91 /// Get a reference to a `Pod` type at the given offset.
92 ///
93 /// Returns an error if offset or size are out of bounds.
94 ///
95 /// Also see the `read` method for information regarding alignment of `T`.
96 fn read_at<T: Pod>(self, mut offset: u64) -> Result<&'a T> {
97 self.read(&mut offset)
98 }
99
100 /// Get a reference to a slice of a `Pod` type at the given offset, and update the offset.
101 ///
102 /// Returns an error if offset or size are out of bounds.
103 ///
104 /// Also see the `read` method for information regarding alignment of `T`.
105 fn read_slice<T: Pod>(self, offset: &mut u64, count: usize) -> Result<&'a [T]> {
106 let size = count
107 .checked_mul(mem::size_of::<T>())
108 .ok_or(())?
109 .try_into()
110 .map_err(|_| ())?;
111 let bytes = self.read_bytes(offset, size)?;
112 let (t, _) = slice_from_bytes(bytes, count)?;
113 Ok(t)
114 }
115
116 /// Get a reference to a slice of a `Pod` type at the given offset.
117 ///
118 /// Returns an error if offset or size are out of bounds.
119 ///
120 /// Also see the `read` method for information regarding alignment of `T`.
121 fn read_slice_at<T: Pod>(self, mut offset: u64, count: usize) -> Result<&'a [T]> {
122 self.read_slice(&mut offset, count)
123 }
124}
125
126impl<'a> ReadRef<'a> for &'a [u8] {
127 fn len(self) -> Result<u64> {
128 self.len().try_into().map_err(|_| ())
129 }
130
131 fn read_bytes_at(self, offset: u64, size: u64) -> Result<&'a [u8]> {
132 let offset: usize = offset.try_into().map_err(|_| ())?;
133 let size: usize = size.try_into().map_err(|_| ())?;
134 self.get(offset..).ok_or(())?.get(..size).ok_or(())
135 }
136
137 fn read_bytes_at_until(self, range: Range<u64>, delimiter: u8) -> Result<&'a [u8]> {
138 let start: usize = range.start.try_into().map_err(|_| ())?;
139 let end: usize = range.end.try_into().map_err(|_| ())?;
140 let bytes = self.get(start..end).ok_or(())?;
141 match memchr::memchr(delimiter, bytes) {
142 Some(len) => {
143 // This will never fail.
144 bytes.get(..len).ok_or(())
145 }
146 None => Err(()),
147 }
148 }
149}