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
use crate::{Encoder, Length, Result, Tag};
pub(super) fn decode_to_slice(bytes: &[u8]) -> Result<&[u8]> {
match bytes {
[] => Err(Tag::Integer.non_canonical_error()),
[0] => Ok(bytes),
[0, byte, ..] if *byte < 0x80 => Err(Tag::Integer.non_canonical_error()),
[0, rest @ ..] => Ok(rest),
[byte, ..] if *byte >= 0x80 => Err(Tag::Integer.value_error()),
_ => Ok(bytes),
}
}
pub(super) fn decode_to_array<const N: usize>(bytes: &[u8]) -> Result<[u8; N]> {
let input = decode_to_slice(bytes)?;
let mut output = [0u8; N];
output[N.saturating_sub(input.len())..].copy_from_slice(input);
Ok(output)
}
pub(super) fn encode_bytes(encoder: &mut Encoder<'_>, bytes: &[u8]) -> Result<()> {
let bytes = strip_leading_zeroes(bytes);
if needs_leading_zero(bytes) {
encoder.byte(0)?;
}
encoder.bytes(bytes)
}
#[inline]
pub(super) fn encoded_len(bytes: &[u8]) -> Result<Length> {
let bytes = strip_leading_zeroes(bytes);
Length::try_from(bytes.len())? + needs_leading_zero(bytes) as u8
}
pub(super) fn strip_leading_zeroes(mut bytes: &[u8]) -> &[u8] {
while let Some((byte, rest)) = bytes.split_first() {
if *byte == 0 && !rest.is_empty() {
bytes = rest;
} else {
break;
}
}
bytes
}
fn needs_leading_zero(bytes: &[u8]) -> bool {
matches!(bytes.get(0), Some(byte) if *byte >= 0x80)
}