| 1 |
|
| 2 |
|
| 3 |
#[cfg(feature = "pem")] |
| 4 |
pub(crate) mod pem; |
| 5 |
pub(crate) mod slice; |
| 6 |
|
| 7 |
mod position; |
| 8 |
|
| 9 |
use crate::{ |
| 10 |
Decode, DecodeValue, Encode, EncodingRules, Error, ErrorKind, FixedTag, Header, Length, Tag, |
| 11 |
TagMode, TagNumber, asn1::ContextSpecific, |
| 12 |
}; |
| 13 |
|
| 14 |
#[cfg(feature = "alloc")] |
| 15 |
use alloc::vec::Vec; |
| 16 |
|
| 17 |
#[cfg(feature = "ber")] |
| 18 |
use crate::length::indefinite::read_eoc; |
| 19 |
|
| 20 |
|
| 21 |
pub trait Reader<'r>: Clone { |
| 22 |
|
| 23 |
const CAN_READ_SLICE: bool; |
| 24 |
|
| 25 |
|
| 26 |
fn encoding_rules(&self) -> EncodingRules; |
| 27 |
|
| 28 |
|
| 29 |
fn input_len(&self) -> Length; |
| 30 |
|
| 31 |
|
| 32 |
fn position(&self) -> Length; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
fn read_nested<T, F, E>(&mut self, len: Length, f: F) -> Result<T, E> |
| 39 |
where |
| 40 |
E: From<Error>, |
| 41 |
F: FnOnce(&mut Self) -> Result<T, E>; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
fn read_slice(&mut self, len: Length) -> Result<&'r [u8], Error>; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
fn context_specific<T>( |
| 57 |
&mut self, |
| 58 |
tag_number: TagNumber, |
| 59 |
tag_mode: TagMode, |
| 60 |
) -> Result<Option<T>, T::Error> |
| 61 |
where |
| 62 |
T: DecodeValue<'r> + FixedTag + 'r, |
| 63 |
{ |
| 64 |
Ok(match tag_mode { |
| 65 |
TagMode::Explicit => ContextSpecific::<T>::decode_explicit(self, tag_number)?, |
| 66 |
TagMode::Implicit => ContextSpecific::<T>::decode_implicit(self, tag_number)?, |
| 67 |
} |
| 68 |
.map(|field| field.value)) |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
fn decode<T: Decode<'r>>(&mut self) -> Result<T, T::Error> { |
| 76 |
T::decode(self) |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
fn drain(&mut self, mut amount: Length) -> Result<(), Error> { |
| 84 |
const BUFFER_SIZE: usize = 16; |
| 85 |
let mut buffer = [0u8; BUFFER_SIZE]; |
| 86 |
|
| 87 |
while amount > Length::ZERO { |
| 88 |
let amount_usize = usize::try_from(amount)?; |
| 89 |
|
| 90 |
let nbytes_drained = if amount_usize >= BUFFER_SIZE { |
| 91 |
self.read_into(&mut buffer)?; |
| 92 |
Length::try_from(BUFFER_SIZE)? |
| 93 |
} else { |
| 94 |
self.read_into(&mut buffer[..amount_usize])?; |
| 95 |
amount |
| 96 |
}; |
| 97 |
|
| 98 |
amount = (amount - nbytes_drained)?; |
| 99 |
} |
| 100 |
|
| 101 |
Ok(()) |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
fn error(&mut self, kind: ErrorKind) -> Error { |
| 107 |
kind.at(self.position()) |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
fn finish(self) -> Result<(), Error> { |
| 116 |
if !self.is_finished() { |
| 117 |
Err(ErrorKind::TrailingData { |
| 118 |
decoded: self.position(), |
| 119 |
remaining: self.remaining_len(), |
| 120 |
} |
| 121 |
.at(self.position())) |
| 122 |
} else { |
| 123 |
Ok(()) |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
fn is_finished(&self) -> bool { |
| 129 |
self.remaining_len().is_zero() |
| 130 |
} |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
fn offset(&self) -> Length { |
| 138 |
self.position() |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
fn peek_byte(&self) -> Option<u8> { |
| 143 |
let mut byte = [0]; |
| 144 |
self.peek_into(&mut byte).ok().map(|_| byte[0]) |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
fn peek_into(&self, buf: &mut [u8]) -> Result<(), Error> { |
| 153 |
let mut reader = self.clone(); |
| 154 |
reader.read_into(buf)?; |
| 155 |
Ok(()) |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
#[deprecated(since = "0.8.0", note = "use `Header::peek` instead")] |
| 166 |
fn peek_header(&self) -> Result<Header, Error> { |
| 167 |
Header::peek(self) |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
#[deprecated(since = "0.8.0", note = "use `Tag::peek` instead")] |
| 175 |
fn peek_tag(&self) -> Result<Tag, Error> { |
| 176 |
Tag::peek(self) |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
fn read_byte(&mut self) -> Result<u8, Error> { |
| 184 |
let mut buf = [0]; |
| 185 |
self.read_into(&mut buf)?; |
| 186 |
Ok(buf[0]) |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
fn read_into<'o>(&mut self, buf: &'o mut [u8]) -> Result<&'o [u8], Error> { |
| 195 |
let input = self.read_slice(buf.len().try_into()?)?; |
| 196 |
buf.copy_from_slice(input); |
| 197 |
Ok(buf) |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
#[cfg(feature = "alloc")] |
| 205 |
fn read_vec(&mut self, len: Length) -> Result<Vec<u8>, Error> { |
| 206 |
let mut bytes = vec![0u8; usize::try_from(len)?]; |
| 207 |
self.read_into(&mut bytes)?; |
| 208 |
Ok(bytes) |
| 209 |
} |
| 210 |
|
| 211 |
|
| 212 |
fn remaining_len(&self) -> Length { |
| 213 |
debug_assert!(self.position() <= self.input_len()); |
| 214 |
self.input_len().saturating_sub(self.position()) |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
fn sequence<F, T, E>(&mut self, f: F) -> Result<T, E> |
| 223 |
where |
| 224 |
F: FnOnce(&mut Self) -> Result<T, E>, |
| 225 |
E: From<Error>, |
| 226 |
{ |
| 227 |
let header = Header::decode(self)?; |
| 228 |
header.tag().assert_eq(Tag::Sequence)?; |
| 229 |
read_value(self, header, |r, _| f(r)) |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
fn tlv_bytes(&mut self) -> Result<&'r [u8], Error> { |
| 237 |
let header = Header::peek(self)?; |
| 238 |
let header_len = header.encoded_len()?; |
| 239 |
self.read_slice((header_len + header.length())?) |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
pub(crate) fn read_value<'r, R, T, F, E>(reader: &mut R, header: Header, f: F) -> Result<T, E> |
| 248 |
where |
| 249 |
R: Reader<'r>, |
| 250 |
E: From<Error>, |
| 251 |
F: FnOnce(&mut R, Header) -> Result<T, E>, |
| 252 |
{ |
| 253 |
#[cfg(feature = "ber")] |
| 254 |
let header = header.with_length(header.length().sans_eoc()); |
| 255 |
|
| 256 |
let ret = reader.read_nested(header.length(), |r| f(r, header))?; |
| 257 |
|
| 258 |
|
| 259 |
#[cfg(feature = "ber")] |
| 260 |
if header.length().is_indefinite() { |
| 261 |
read_eoc(reader)?; |
| 262 |
} |
| 263 |
|
| 264 |
Ok(ret) |
| 265 |
} |