sark-http 0.40.0

HTTP wire types, semantics, codecs, and ownership invariants for Sark
Documentation
use crate::field::base64::{
    self, Error,
    shape::{Shape, Tail},
};

const LANES: usize = 16;

/// Borrowed standard Base64 text.
#[derive(Clone, Copy)]
pub struct Encoded<'a>(&'a [u8]);

impl<'a> Encoded<'a> {
    pub const fn new(bytes: &'a [u8]) -> Self {
        Self(bytes)
    }

    pub fn decoded_len(self) -> Result<usize, Error> {
        Shape::new(self.0).map(Shape::decoded)
    }

    pub fn validate(self) -> Result<usize, Error> {
        let shape = self.shape()?;
        Ok(shape.decoded())
    }

    pub(super) fn shape(self) -> Result<Shape, Error> {
        let shape = Shape::new(self.0)?;
        let (checked, mask, padding) = match shape.tail() {
            Tail::None => (shape.symbols(), 0, 0),
            Tail::Two => (shape.groups() * 4 + 1, 0x0f, 1),
            Tail::Three => (shape.groups() * 4 + 2, 0x03, 1),
        };
        let blocks = checked / LANES;
        let (chunks, _) = self.0.as_chunks::<LANES>();
        for (block, bytes) in chunks.iter().take(blocks).enumerate() {
            Self::check_chunk(bytes, block * LANES)?;
        }
        let start = blocks * LANES;
        let tail = self.0.iter().copied().enumerate().skip(start);
        for (index, byte) in tail.take(checked - start + padding) {
            let value = base64::value(byte, index)?;
            if index == checked && value & mask != 0 {
                return Err(Error::Padding { index, byte });
            }
        }
        Ok(shape)
    }

    fn check_chunk(bytes: &[u8; LANES], index: usize) -> Result<(), Error> {
        let valid = bytes.iter().fold(true, |valid, &byte| {
            valid
                & ((byte.wrapping_sub(b'A') <= 25)
                    | (byte.wrapping_sub(b'a') <= 25)
                    | (byte.wrapping_sub(b'0') <= 9)
                    | (byte == b'+')
                    | (byte == b'/'))
        });
        if valid {
            return Ok(());
        }
        for (offset, &byte) in bytes.iter().enumerate() {
            base64::value(byte, index + offset)?;
        }
        Ok(())
    }
}