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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
//! 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,
},
}
/// Redacted decoding error class.
///
/// This type intentionally omits input-derived bytes and indexes so callers can
/// log error classes without logging secret-adjacent input content.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum DecodeErrorKind {
/// 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,
/// Padding is missing, misplaced, or non-canonical.
InvalidPadding,
/// Line wrapping is missing, misplaced, or uses the wrong line ending.
InvalidLineWrap,
/// The output buffer is too small.
OutputTooSmall,
/// The caller-provided constant-time staging buffer is too small.
StagingTooSmall,
}
impl DecodeErrorKind {
/// Returns the stable lowercase identifier for this error class.
#[must_use]
pub const fn as_str(self) -> &'static str {
match self {
Self::InvalidInput => "invalid-input",
Self::InvalidLength => "invalid-length",
Self::InvalidByte => "invalid-byte",
Self::InvalidPadding => "invalid-padding",
Self::InvalidLineWrap => "invalid-line-wrap",
Self::OutputTooSmall => "output-too-small",
Self::StagingTooSmall => "staging-too-small",
}
}
}
impl core::fmt::Display for DecodeErrorKind {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.as_str())
}
}
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 {
/// Returns a redacted error class without input-derived bytes or indexes.
///
/// Strict decoders keep exact diagnostics in [`DecodeError`] and
/// [`core::fmt::Display`] for developer debugging. When input may contain
/// secrets or secret-adjacent material, log this kind instead of logging
/// the full error value.
#[must_use]
pub const fn kind(self) -> DecodeErrorKind {
match self {
Self::InvalidInput => DecodeErrorKind::InvalidInput,
Self::InvalidLength => DecodeErrorKind::InvalidLength,
Self::InvalidByte { .. } => DecodeErrorKind::InvalidByte,
Self::InvalidPadding { .. } => DecodeErrorKind::InvalidPadding,
Self::InvalidLineWrap { .. } => DecodeErrorKind::InvalidLineWrap,
Self::OutputTooSmall { .. } => DecodeErrorKind::OutputTooSmall,
Self::StagingTooSmall { .. } => DecodeErrorKind::StagingTooSmall,
}
}
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 {}