fast image operations
Diffstat (limited to 'src/term/b64.rs')
| -rw-r--r-- | src/term/b64.rs | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/src/term/b64.rs b/src/term/b64.rs new file mode 100644 index 0000000..7e2cdde --- /dev/null +++ b/src/term/b64.rs @@ -0,0 +1,48 @@ +#[test] +fn b64() { + fn t(i: &'static str, o: &'static str) { + let mut x = vec![]; + encode(i.as_bytes(), &mut x).unwrap(); + assert_eq!(x, o.as_bytes()); + } + + t("Hello World!", "SGVsbG8gV29ybGQh"); + t("Hello World", "SGVsbG8gV29ybGQ="); +} + +pub fn encode(mut input: &[u8], output: &mut impl std::io::Write) -> std::io::Result<()> { + const Α: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + while let [a, b, c, rest @ ..] = input { + let α = ((*a as usize) << 16) | ((*b as usize) << 8) | *c as usize; + output.write_all(&[ + Α[α >> 18], + Α[(α >> 12) & 0x3F], + Α[(α >> 6) & 0x3F], + Α[α & 0x3F], + ])?; + input = rest; + } + if !input.is_empty() { + let mut α = (input[0] as usize) << 16; + if input.len() > 1 { + α |= (input[1] as usize) << 8; + } + output.write_all(&[Α[α >> 18], Α[α >> 12 & 0x3F]])?; + if input.len() > 1 { + output.write_all(&[Α[α >> 6 & 0x3f]])?; + } else { + output.write_all(&[b'='])?; + } + output.write_all(&[b'='])?; + } + Ok(()) +} + +pub const fn size(of: &[u8]) -> usize { + let use_pad = of.len() % 3 != 0; + if use_pad { + 4 * (of.len() / 3 + 1) + } else { + 4 * (of.len() / 3) + } +} |