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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#![cfg_attr(not(any(feature = "std", test)), no_std)]
#![cfg_attr(feature = "unstable", feature(arm_target_feature))]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![deny(
missing_debug_implementations,
missing_docs,
clippy::all,
clippy::cargo,
clippy::missing_inline_in_public_items
)]
#![warn(clippy::todo)]
#[cfg(feature = "alloc")]
extern crate alloc;
mod error;
pub use self::error::Error;
mod spec;
mod decode;
mod encode;
mod forgiving;
mod multiversion;
#[cfg(test)]
mod tests;
pub use simd_abstraction::tools::OutBuf;
use self::error::ERROR;
use simd_abstraction::item_group;
use simd_abstraction::tools::slice_mut;
#[cfg(feature = "alloc")]
item_group!(
use alloc::boxed::Box;
use simd_abstraction::tools::{alloc_uninit_bytes, assume_init};
);
#[derive(Debug)]
enum Base64Kind {
Standard,
UrlSafe,
}
const STANDARD_CHARSET: &[u8; 64] =
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
const URL_SAFE_CHARSET: &[u8; 64] =
b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
#[derive(Debug)]
pub struct Base64 {
kind: Base64Kind,
padding: bool,
}
impl Base64 {
pub const STANDARD: Self = Self {
kind: Base64Kind::Standard,
padding: true,
};
pub const STANDARD_NO_PAD: Self = Self {
kind: Base64Kind::Standard,
padding: false,
};
pub const URL_SAFE: Self = Self {
kind: Base64Kind::UrlSafe,
padding: true,
};
pub const URL_SAFE_NO_PAD: Self = Self {
kind: Base64Kind::UrlSafe,
padding: false,
};
#[inline]
pub const fn charset(&self) -> &[u8; 64] {
match self.kind {
Base64Kind::Standard => STANDARD_CHARSET,
Base64Kind::UrlSafe => URL_SAFE_CHARSET,
}
}
#[inline]
pub const fn encoded_length(&self, n: usize) -> usize {
assert!(n < usize::MAX / 2);
unsafe { crate::encode::encoded_length_unchecked(n, self.padding) }
}
#[inline]
pub const fn estimated_decoded_length(&self, n: usize) -> usize {
if n % 4 == 0 {
n / 4 * 3
} else {
(n / 4 + 1) * 3
}
}
#[inline]
pub fn decoded_length(&self, data: &[u8]) -> Result<usize, Error> {
let (_, m) = crate::decode::decoded_length(data, self.padding)?;
Ok(m)
}
#[inline]
pub fn encode<'s, 'd>(
&'_ self,
src: &'s [u8],
dst: OutBuf<'d, u8>,
) -> Result<&'d mut [u8], Error> {
unsafe {
let m = crate::encode::encoded_length_unchecked(src.len(), self.padding);
if dst.len() < m {
return Err(ERROR);
}
let mut dst = dst;
let dst = dst.as_mut_ptr();
crate::multiversion::encode_raw::auto_indirect(self, src, dst);
Ok(slice_mut(dst, m))
}
}
#[inline]
pub fn encode_as_str<'s, 'd>(
&'_ self,
src: &'s [u8],
dst: OutBuf<'d, u8>,
) -> Result<&'d mut str, Error> {
let ans = self.encode(src, dst)?;
Ok(unsafe { core::str::from_utf8_unchecked_mut(ans) })
}
#[inline]
pub fn decode<'s, 'd>(
&'_ self,
src: &'s [u8],
dst: OutBuf<'d, u8>,
) -> Result<&'d mut [u8], Error> {
unsafe {
let (n, m) = crate::decode::decoded_length(src, self.padding)?;
if dst.len() < m {
return Err(ERROR);
}
let mut dst = dst;
let src: *const u8 = src.as_ptr();
let dst: *mut u8 = dst.as_mut_ptr();
crate::multiversion::decode_raw::auto_indirect(self, n, m, src, dst)?;
Ok(slice_mut(dst, m))
}
}
#[inline]
pub fn decode_inplace<'d>(&'_ self, data: &'d mut [u8]) -> Result<&'d mut [u8], Error> {
unsafe {
let (n, m) = crate::decode::decoded_length(data, self.padding)?;
let dst: *mut u8 = data.as_mut_ptr();
let src: *const u8 = dst;
crate::multiversion::decode_raw::auto_indirect(self, n, m, src, dst)?;
Ok(slice_mut(dst, m))
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[cfg(feature = "alloc")]
#[inline]
pub fn encode_to_boxed_str(&self, data: &[u8]) -> Box<str> {
if data.is_empty() {
return Box::from("");
}
unsafe {
let m = crate::encode::encoded_length_unchecked(data.len(), self.padding);
assert!(m < usize::MAX / 2);
let mut uninit_buf = alloc_uninit_bytes(m);
let dst: *mut u8 = uninit_buf.as_mut_ptr().cast();
crate::multiversion::encode_raw::auto_indirect(self, data, dst);
let len = uninit_buf.len();
let ptr = Box::into_raw(uninit_buf).cast::<u8>();
Box::from_raw(core::str::from_utf8_unchecked_mut(slice_mut(ptr, len)))
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[cfg(feature = "alloc")]
#[inline]
pub fn decode_to_boxed_bytes(&self, data: &[u8]) -> Result<Box<[u8]>, Error> {
if data.is_empty() {
return Ok(Box::from([]));
}
unsafe {
let (n, m) = crate::decode::decoded_length(data, self.padding)?;
let mut uninit_buf = alloc_uninit_bytes(m);
let dst: *mut u8 = uninit_buf.as_mut_ptr().cast();
let src: *const u8 = data.as_ptr();
crate::multiversion::decode_raw::auto_indirect(self, n, m, src, dst)?;
Ok(assume_init(uninit_buf))
}
}
#[inline]
pub fn forgiving_decode_inplace(data: &mut [u8]) -> Result<&mut [u8], Error> {
let data = crate::forgiving::normalize(data);
Self::STANDARD_NO_PAD.decode_inplace(data)
}
}