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
use crate::decode::decoded_length;
use crate::encode::encoded_length_unchecked;
use crate::{AppendBase64Decode, AppendBase64Encode};
use crate::{Base64, Error};
use crate::{FromBase64Decode, FromBase64Encode};
use vsimd::tools::{alloc_uninit_bytes, assume_init, boxed_str, slice_parts};
use alloc::boxed::Box;
use alloc::string::String;
use alloc::vec::Vec;
#[inline]
fn encode_to_boxed_str(base64: &Base64, data: &[u8]) -> Box<str> {
if data.is_empty() {
return Box::from("");
}
unsafe {
let m = encoded_length_unchecked(data.len(), base64.config);
assert!(m <= usize::MAX / 2);
let mut buf = alloc_uninit_bytes(m);
{
let (src, len) = slice_parts(data);
let dst: *mut u8 = buf.as_mut_ptr().cast();
crate::multiversion::encode::auto(src, len, dst, base64.config);
}
boxed_str(assume_init(buf))
}
}
#[inline]
fn encode_append_vec(base64: &Base64, src: &[u8], buf: &mut Vec<u8>) {
if src.is_empty() {
return;
}
unsafe {
let m = encoded_length_unchecked(src.len(), base64.config);
assert!(m <= usize::MAX / 2);
buf.reserve_exact(m);
let prev_len = buf.len();
{
let (src, len) = slice_parts(src);
let dst = buf.as_mut_ptr().add(prev_len);
crate::multiversion::encode::auto(src, len, dst, base64.config);
}
buf.set_len(prev_len + m);
}
}
#[inline]
fn decode_to_boxed_bytes(base64: &Base64, data: &[u8]) -> Result<Box<[u8]>, Error> {
if data.is_empty() {
return Ok(Box::from([]));
}
unsafe {
let (n, m) = decoded_length(data, base64.config)?;
let mut buf = alloc_uninit_bytes(m);
{
let dst = buf.as_mut_ptr().cast();
let src = data.as_ptr();
crate::multiversion::decode::auto(src, dst, n, base64.config)?;
}
Ok(assume_init(buf))
}
}
#[inline]
fn decode_append_vec(base64: &Base64, src: &[u8], buf: &mut Vec<u8>) -> Result<(), Error> {
if src.is_empty() {
return Ok(());
}
unsafe {
let (n, m) = decoded_length(src, base64.config)?;
buf.reserve_exact(m);
let prev_len = buf.len();
let dst = buf.as_mut_ptr().add(prev_len);
let src = src.as_ptr();
crate::multiversion::decode::auto(src, dst, n, base64.config)?;
buf.set_len(prev_len + m);
Ok(())
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl FromBase64Decode for Box<[u8]> {
#[inline]
fn from_base64_decode(base64: &Base64, data: &[u8]) -> Result<Self, Error> {
decode_to_boxed_bytes(base64, data)
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl FromBase64Decode for Vec<u8> {
#[inline]
fn from_base64_decode(base64: &Base64, data: &[u8]) -> Result<Self, crate::Error> {
let ans = decode_to_boxed_bytes(base64, data)?;
Ok(Vec::from(ans))
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl FromBase64Encode for Box<[u8]> {
#[inline]
fn from_base64_encode(base64: &Base64, data: &[u8]) -> Self {
let ans = encode_to_boxed_str(base64, data);
ans.into_boxed_bytes()
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl FromBase64Encode for Box<str> {
#[inline]
fn from_base64_encode(base64: &Base64, data: &[u8]) -> Self {
encode_to_boxed_str(base64, data)
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl FromBase64Encode for Vec<u8> {
#[inline]
fn from_base64_encode(base64: &Base64, data: &[u8]) -> Self {
let ans = encode_to_boxed_str(base64, data);
Vec::from(ans.into_boxed_bytes())
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl FromBase64Encode for String {
#[inline]
fn from_base64_encode(base64: &Base64, data: &[u8]) -> Self {
let ans = encode_to_boxed_str(base64, data);
String::from(ans)
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl AppendBase64Encode for Vec<u8> {
#[inline]
fn append_base64_encode(base64: &Base64, src: &[u8], dst: &mut Self) {
encode_append_vec(base64, src, dst);
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl AppendBase64Encode for String {
#[inline]
fn append_base64_encode(base64: &Base64, src: &[u8], dst: &mut Self) {
unsafe { encode_append_vec(base64, src, dst.as_mut_vec()) };
}
}
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
impl AppendBase64Decode for Vec<u8> {
#[inline]
fn append_base64_decode(base64: &Base64, src: &[u8], dst: &mut Self) -> Result<(), Error> {
decode_append_vec(base64, src, dst)
}
}