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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//! Error types for encoding and decoding operations.
/// Encoding error.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum EncodeError {
/// The encoded output length would overflow `usize`.
LengthOverflow,
/// The requested line wrapping policy is invalid.
InvalidLineWrap {
/// Requested line length.
line_len: usize,
},
/// The caller-provided input length exceeds the provided buffer.
InputTooLarge {
/// Requested input bytes.
input_len: usize,
/// Available buffer bytes.
buffer_len: usize,
},
/// The output buffer is too small.
OutputTooSmall {
/// Required output bytes.
required: usize,
/// Available output bytes.
available: usize,
},
}
impl core::fmt::Display for EncodeError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::LengthOverflow => f.write_str("base64 output length overflows usize"),
Self::InvalidLineWrap { line_len } => {
write!(f, "base64 line wrap length {line_len} is invalid")
}
Self::InputTooLarge {
input_len,
buffer_len,
} => write!(
f,
"base64 input length {input_len} exceeds buffer length {buffer_len}"
),
Self::OutputTooSmall {
required,
available,
} => write!(
f,
"base64 output buffer too small: required {required}, available {available}"
),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for EncodeError {}
/// Decoding error.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum DecodeError {
/// The encoded input is malformed, but the decoder intentionally does not
/// disclose a more specific error class.
InvalidInput,
/// The encoded input length is impossible for the selected padding policy.
InvalidLength,
/// A byte is not valid for the selected alphabet.
InvalidByte {
/// Byte index in the input.
index: usize,
/// Invalid byte value.
byte: u8,
},
/// Padding is missing, misplaced, or non-canonical.
InvalidPadding {
/// Byte index where padding became invalid.
index: usize,
},
/// Line wrapping is missing, misplaced, or uses the wrong line ending.
InvalidLineWrap {
/// Byte index where line wrapping became invalid.
index: usize,
},
/// The output buffer is too small.
OutputTooSmall {
/// Required output bytes.
required: usize,
/// Available output bytes.
available: usize,
},
/// The caller-provided constant-time staging buffer is too small.
StagingTooSmall {
/// Required staging bytes.
required: usize,
/// Available staging bytes.
available: usize,
},
}
impl core::fmt::Display for DecodeError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::InvalidInput => f.write_str("malformed base64 input"),
Self::InvalidLength => f.write_str("invalid base64 input length"),
Self::InvalidByte { index, byte } => {
write!(f, "invalid base64 byte 0x{byte:02x} at index {index}")
}
Self::InvalidPadding { index } => write!(f, "invalid base64 padding at index {index}"),
Self::InvalidLineWrap { index } => {
write!(f, "invalid base64 line wrapping at index {index}")
}
Self::OutputTooSmall {
required,
available,
} => write!(
f,
"base64 decode output buffer too small: required {required}, available {available}"
),
Self::StagingTooSmall {
required,
available,
} => write!(
f,
"base64 decode staging buffer too small: required {required}, available {available}"
),
}
}
}
impl DecodeError {
pub(crate) fn with_index_offset(self, offset: usize) -> Self {
match self {
Self::InvalidByte { index, byte } => Self::InvalidByte {
index: index + offset,
byte,
},
Self::InvalidPadding { index } => Self::InvalidPadding {
index: index + offset,
},
Self::InvalidLineWrap { index } => Self::InvalidLineWrap {
index: index + offset,
},
Self::InvalidInput
| Self::InvalidLength
| Self::OutputTooSmall { .. }
| Self::StagingTooSmall { .. } => self,
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for DecodeError {}