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
316
317
use std::{
	borrow::Borrow,
	fmt,
	ops::{Deref, DerefMut},
	str::FromStr,
};

use crate::{
	lexical::{self, LexicalFormOf},
	Datatype, ParseXsd, XsdValue,
};

const CHARS: [char; 64] = [
	'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S',
	'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
	'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4',
	'5', '6', '7', '8', '9', '+', '/',
];

const PADDING: char = '=';

#[derive(Debug, thiserror::Error)]
#[error("invalid base64")]
pub struct InvalidBase64;

#[derive(Debug, Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Base64BinaryBuf(Vec<u8>);

impl Base64BinaryBuf {
	pub fn new() -> Self {
		Self::default()
	}

	pub fn from_bytes(bytes: Vec<u8>) -> Self {
		Self(bytes)
	}

	pub fn decode(input: impl AsRef<[u8]>) -> Result<Self, InvalidBase64> {
		let input = input.as_ref();
		let mut bytes = Vec::with_capacity(input.len() * 3 / 4);
		let mut buffer = 0u16;
		let mut buffer_len = 0u16;
		let mut padding = false;

		for &c in input {
			if c == 0x20 {
				continue;
			}

			if padding {
				if c != b'=' {
					return Err(InvalidBase64);
				}
			} else if c == b'=' {
				padding = true
			} else {
				buffer_len += 6;
				buffer = buffer << 6 | decode_char(c)? as u16;

				while buffer_len >= 8 {
					buffer_len -= 8;
					let b = buffer >> buffer_len;
					bytes.push(b as u8)
				}
			}
		}

		Ok(Self(bytes))
	}

	pub fn into_bytes(self) -> Vec<u8> {
		self.0
	}

	pub fn as_bytes(&self) -> &[u8] {
		&self.0
	}

	pub fn as_base64_binary(&self) -> &Base64Binary {
		Base64Binary::new(&self.0)
	}

	pub fn as_base64_binary_mut(&mut self) -> &mut Base64Binary {
		Base64Binary::new_mut(&mut self.0)
	}
}

fn decode_char(c: u8) -> Result<u8, InvalidBase64> {
	match c {
		b'A'..=b'Z' => Ok(c - b'A'),
		b'a'..=b'z' => Ok(c - b'a' + 26),
		b'0'..=b'9' => Ok(c - b'0' + 52),
		b'+' => Ok(62),
		b'/' => Ok(63),
		_ => Err(InvalidBase64),
	}
}

impl fmt::Display for Base64BinaryBuf {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		self.as_base64_binary().fmt(f)
	}
}

impl From<Vec<u8>> for Base64BinaryBuf {
	fn from(value: Vec<u8>) -> Self {
		Base64BinaryBuf::from_bytes(value)
	}
}

impl FromStr for Base64BinaryBuf {
	type Err = InvalidBase64;

	fn from_str(s: &str) -> Result<Self, Self::Err> {
		Self::decode(s)
	}
}

impl AsRef<[u8]> for Base64BinaryBuf {
	fn as_ref(&self) -> &[u8] {
		self.as_bytes()
	}
}

impl AsRef<Base64Binary> for Base64BinaryBuf {
	fn as_ref(&self) -> &Base64Binary {
		self.as_base64_binary()
	}
}

impl Borrow<Base64Binary> for Base64BinaryBuf {
	fn borrow(&self) -> &Base64Binary {
		self.as_base64_binary()
	}
}

impl Deref for Base64BinaryBuf {
	type Target = Base64Binary;

	fn deref(&self) -> &Self::Target {
		self.as_base64_binary()
	}
}

impl DerefMut for Base64BinaryBuf {
	fn deref_mut(&mut self) -> &mut Self::Target {
		self.as_base64_binary_mut()
	}
}

impl XsdValue for Base64BinaryBuf {
	fn datatype(&self) -> Datatype {
		Datatype::Base64Binary
	}
}

impl ParseXsd for Base64BinaryBuf {
	type LexicalForm = lexical::Base64Binary;
}

impl LexicalFormOf<Base64BinaryBuf> for lexical::Base64Binary {
	type ValueError = std::convert::Infallible;

	fn try_as_value(&self) -> Result<Base64BinaryBuf, Self::ValueError> {
		Ok(self.value())
	}
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Base64Binary([u8]);

impl Base64Binary {
	pub fn new(bytes: &[u8]) -> &Self {
		unsafe { std::mem::transmute(bytes) }
	}

	pub fn new_mut(bytes: &mut [u8]) -> &mut Self {
		unsafe { std::mem::transmute(bytes) }
	}

	pub fn chars(&self) -> Chars {
		Chars {
			offset: 0,
			rest: 0,
			padding: false,
			bytes: self.0.iter(),
		}
	}

	pub fn as_bytes(&self) -> &[u8] {
		&self.0
	}
}

impl<'a> From<&'a [u8]> for &'a Base64Binary {
	fn from(value: &'a [u8]) -> Self {
		Base64Binary::new(value)
	}
}

impl<'a> From<&'a mut [u8]> for &'a Base64Binary {
	fn from(value: &'a mut [u8]) -> Self {
		Base64Binary::new(value)
	}
}

impl<'a> From<&'a mut [u8]> for &'a mut Base64Binary {
	fn from(value: &'a mut [u8]) -> Self {
		Base64Binary::new_mut(value)
	}
}

impl fmt::Display for Base64Binary {
	fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
		for c in self.chars() {
			c.fmt(f)?;
		}

		Ok(())
	}
}

impl ToOwned for Base64Binary {
	type Owned = Base64BinaryBuf;

	fn to_owned(&self) -> Self::Owned {
		Base64BinaryBuf::from_bytes(self.as_bytes().to_vec())
	}
}

pub struct Chars<'a> {
	offset: u8,
	rest: u8,
	padding: bool,
	bytes: std::slice::Iter<'a, u8>,
}

impl<'a> Iterator for Chars<'a> {
	type Item = char;

	fn next(&mut self) -> Option<Self::Item> {
		if self.padding {
			if self.offset <= 6 {
				self.offset += 2;
				Some(PADDING)
			} else {
				None
			}
		} else if self.offset == 6 {
			let sextet = self.rest;
			self.rest = 0;
			self.offset = 0;
			Some(CHARS[sextet as usize])
		} else {
			match self.bytes.next() {
				Some(b) => {
					let sextet = self.rest | (b >> 2 >> self.offset & 0b111111);
					self.offset += 2;
					self.rest = b << (6 - self.offset) & 0b111111;
					Some(CHARS[sextet as usize])
				}
				None => {
					if self.offset > 0 {
						let sextet = self.rest;
						self.offset += 2;
						self.padding = true;
						Some(CHARS[sextet as usize])
					} else {
						None
					}
				}
			}
		}
	}
}

impl XsdValue for Base64Binary {
	fn datatype(&self) -> Datatype {
		Datatype::Base64Binary
	}
}

#[cfg(test)]
mod tests {
	use super::*;

	const TESTS: [(&'static [u8], &'static str); 9] = [
		(b"M", "TQ=="),
		(b"Ma", "TWE="),
		(b"Man", "TWFu"),
		(b"light w", "bGlnaHQgdw=="),
		(b"light wo", "bGlnaHQgd28="),
		(b"light wor", "bGlnaHQgd29y"),
		(b"light work", "bGlnaHQgd29yaw=="),
		(b"light work.", "bGlnaHQgd29yay4="),
		(
			b"Many hands make light work.",
			"TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu",
		),
	];

	#[test]
	fn encoding() {
		for (input, expected) in TESTS {
			let output = Base64Binary::new(input).to_string();
			assert_eq!(output, expected)
		}
	}

	#[test]
	fn decoding() {
		for (expected, input) in TESTS {
			let output = Base64BinaryBuf::decode(input).unwrap();
			assert_eq!(output.as_bytes(), expected)
		}
	}
}