use std::borrow::Cow;
use std::fmt;
use schemars::{JsonSchema, Schema, SchemaGenerator, json_schema};
use serde::de::{self, Deserializer, Visitor};
use serde::{Deserialize, Serialize, Serializer};
const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum B64Error {
BadChar,
BadLength,
NonCanonical,
}
impl fmt::Display for B64Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
B64Error::BadChar => "invalid base64url character (padding is not allowed)",
B64Error::BadLength => "invalid base64url length",
B64Error::NonCanonical => "non-canonical base64url (trailing bits set)",
})
}
}
impl std::error::Error for B64Error {}
pub fn encode(bytes: &[u8]) -> String {
let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4);
for chunk in bytes.chunks(3) {
let b0 = chunk[0];
let b1 = chunk.get(1).copied().unwrap_or(0);
let b2 = chunk.get(2).copied().unwrap_or(0);
let n = (u32::from(b0) << 16) | (u32::from(b1) << 8) | u32::from(b2);
let chars = chunk.len() + 1;
for i in 0..chars {
let idx = (n >> (18 - 6 * i)) & 0x3f;
out.push(char::from(ALPHABET[idx as usize]));
}
}
out
}
fn value(c: u8) -> Result<u32, B64Error> {
Ok(u32::from(match c {
b'A'..=b'Z' => c - b'A',
b'a'..=b'z' => c - b'a' + 26,
b'0'..=b'9' => c - b'0' + 52,
b'-' => 62,
b'_' => 63,
_ => return Err(B64Error::BadChar),
}))
}
pub fn decode(s: &str) -> Result<Vec<u8>, B64Error> {
let raw = s.as_bytes();
if raw.len() % 4 == 1 {
return Err(B64Error::BadLength);
}
let mut out = Vec::with_capacity(raw.len() / 4 * 3 + 2);
for chunk in raw.chunks(4) {
let mut n = 0u32;
for (i, &c) in chunk.iter().enumerate() {
n |= value(c)? << (18 - 6 * i);
}
let bytes = chunk.len() - 1;
let used_bits = 8 * bytes as u32;
let unused = 24 - used_bits;
if chunk.len() < 4 && n & ((1u32 << unused) - 1) != 0 {
return Err(B64Error::NonCanonical);
}
for i in 0..bytes {
out.push(((n >> (16 - 8 * i)) & 0xff) as u8);
}
}
Ok(out)
}
#[derive(Clone, PartialEq, Eq, Hash, Default)]
pub struct B64(pub Vec<u8>);
impl B64 {
pub fn new(bytes: impl Into<Vec<u8>>) -> B64 {
B64(bytes.into())
}
pub fn as_bytes(&self) -> &[u8] {
&self.0
}
pub fn into_bytes(self) -> Vec<u8> {
self.0
}
pub fn len(&self) -> usize {
self.0.len()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn encoded(&self) -> String {
encode(&self.0)
}
pub fn parse(s: &str) -> Result<B64, B64Error> {
decode(s).map(B64)
}
}
impl fmt::Debug for B64 {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "B64({} bytes)", self.0.len())
}
}
impl From<Vec<u8>> for B64 {
fn from(v: Vec<u8>) -> B64 {
B64(v)
}
}
impl From<&[u8]> for B64 {
fn from(v: &[u8]) -> B64 {
B64(v.to_vec())
}
}
impl Serialize for B64 {
fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
s.serialize_str(&encode(&self.0))
}
}
impl<'de> Deserialize<'de> for B64 {
fn deserialize<D: Deserializer<'de>>(d: D) -> Result<B64, D::Error> {
struct V;
impl Visitor<'_> for V {
type Value = B64;
fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("a base64url string without padding")
}
fn visit_str<E: de::Error>(self, v: &str) -> Result<B64, E> {
decode(v).map(B64).map_err(E::custom)
}
}
d.deserialize_str(V)
}
}
impl JsonSchema for B64 {
fn inline_schema() -> bool {
true
}
fn schema_name() -> Cow<'static, str> {
"B64".into()
}
fn json_schema(_: &mut SchemaGenerator) -> Schema {
json_schema!({
"type": "string",
"pattern": "^[A-Za-z0-9_-]*$",
"description": "Bytes as base64url without padding (RFC 4648 §5); canonical trailing bits."
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn rfc4648_vectors_url_safe_unpadded() {
let cases: &[(&[u8], &str)] = &[
(b"", ""),
(b"f", "Zg"),
(b"fo", "Zm8"),
(b"foo", "Zm9v"),
(b"foob", "Zm9vYg"),
(b"fooba", "Zm9vYmE"),
(b"foobar", "Zm9vYmFy"),
(&[0xfb, 0xff], "-_8"),
];
for (raw, enc) in cases {
assert_eq!(encode(raw), *enc);
assert_eq!(decode(enc).as_deref(), Ok(*raw));
}
}
#[test]
fn strict_decoding() {
assert_eq!(decode("Zg=="), Err(B64Error::BadChar));
assert_eq!(decode("Zm9v+A"), Err(B64Error::BadChar));
assert_eq!(decode("Z"), Err(B64Error::BadLength));
assert_eq!(decode("Zh"), Err(B64Error::NonCanonical));
assert_eq!(decode("Zm9"), Err(B64Error::NonCanonical));
}
#[test]
fn every_length_round_trips() {
for n in 0..70u8 {
let v: Vec<u8> = (0..n).map(|i| i.wrapping_mul(37).wrapping_add(n)).collect();
assert_eq!(decode(&encode(&v)), Ok(v));
}
}
}