Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 23 additions & 27 deletions base64ct/src/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@ const fn encoded_len_inner(n: usize, padded: bool) -> Option<usize> {
/// Encode the input byte slice as Base64, writing the result into the provided
/// destination slice, and returning an ASCII-encoded string value.
#[inline(always)]
pub(crate) fn encode<'a>(
pub(crate) fn encode<'a, F>(
src: &[u8],
dst: &'a mut [u8],
padded: bool,
hi_bytes: (u8, u8),
) -> Result<&'a str, InvalidLengthError> {
f: F,
) -> Result<&'a str, InvalidLengthError>
where
F: Fn(i16) -> u8 + Copy,
{
let elen = match encoded_len_inner(src.len(), padded) {
Some(v) => v,
None => return Err(InvalidLengthError),
Expand All @@ -58,7 +61,7 @@ pub(crate) fn encode<'a>(
let mut dst_chunks = dst.chunks_exact_mut(4);

for (s, d) in (&mut src_chunks).zip(&mut dst_chunks) {
encode_3bytes(s, d, hi_bytes);
encode_3bytes(s, d, f);
}

let src_rem = src_chunks.remainder();
Expand All @@ -67,7 +70,7 @@ pub(crate) fn encode<'a>(
if let Some(dst_rem) = dst_chunks.next() {
let mut tmp = [0u8; 3];
tmp[..src_rem.len()].copy_from_slice(&src_rem);
encode_3bytes(&tmp, dst_rem, hi_bytes);
encode_3bytes(&tmp, dst_rem, f);

let flag = src_rem.len() == 1;
let mask = (flag as u8).wrapping_sub(1);
Expand All @@ -80,7 +83,7 @@ pub(crate) fn encode<'a>(
let mut tmp_in = [0u8; 3];
let mut tmp_out = [0u8; 4];
tmp_in[..src_rem.len()].copy_from_slice(src_rem);
encode_3bytes(&tmp_in, &mut tmp_out, hi_bytes);
encode_3bytes(&tmp_in, &mut tmp_out, f);
dst_rem.copy_from_slice(&tmp_out[..dst_rem.len()]);
}

Expand All @@ -97,10 +100,13 @@ pub(crate) fn encode<'a>(
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
#[inline(always)]
pub(crate) fn encode_string(input: &[u8], padded: bool, hi_bytes: (u8, u8)) -> String {
pub(crate) fn encode_string<F>(input: &[u8], padded: bool, f: F) -> String
where
F: Fn(i16) -> u8 + Copy,
{
let elen = encoded_len_inner(input.len(), padded).expect("input is too big");
let mut dst = vec![0u8; elen];
let res = encode(input, &mut dst, padded, hi_bytes).expect("encoding error");
let res = encode(input, &mut dst, padded, f).expect("encoding error");

debug_assert_eq!(elen, res.len());
debug_assert!(str::from_utf8(&dst).is_ok());
Expand All @@ -110,35 +116,25 @@ pub(crate) fn encode_string(input: &[u8], padded: bool, hi_bytes: (u8, u8)) -> S
}

#[inline(always)]
fn encode_3bytes(src: &[u8], dst: &mut [u8], hi_bytes: (u8, u8)) {
fn encode_3bytes<F>(src: &[u8], dst: &mut [u8], f: F)
where
F: Fn(i16) -> u8 + Copy,
{
debug_assert_eq!(src.len(), 3);
debug_assert!(dst.len() >= 4, "dst too short: {}", dst.len());

let b0 = src[0] as i16;
let b1 = src[1] as i16;
let b2 = src[2] as i16;

dst[0] = encode_6bits(b0 >> 2, hi_bytes);
dst[1] = encode_6bits(((b0 << 4) | (b1 >> 4)) & 63, hi_bytes);
dst[2] = encode_6bits(((b1 << 2) | (b2 >> 6)) & 63, hi_bytes);
dst[3] = encode_6bits(b2 & 63, hi_bytes);
}

#[inline(always)]
fn encode_6bits(src: i16, hi_bytes: (u8, u8)) -> u8 {
let hi_off = 0x1c + (hi_bytes.0 & 4);
let mut diff = 0x41i16;

diff += match_gt_ct(src, 25, 6);
diff -= match_gt_ct(src, 51, 75);
diff -= match_gt_ct(src, 61, hi_bytes.0 as i16 - hi_off as i16);
diff += match_gt_ct(src, 62, hi_bytes.1 as i16 - hi_bytes.0 as i16 - 1);

(src + diff) as u8
dst[0] = f(b0 >> 2);
dst[1] = f(((b0 << 4) | (b1 >> 4)) & 63);
dst[2] = f(((b1 << 2) | (b2 >> 6)) & 63);
dst[3] = f(b2 & 63);
}

/// Match that the given input is greater than the provided threshold.
#[inline(always)]
fn match_gt_ct(input: i16, threshold: u8, ret_on_match: i16) -> i16 {
pub(crate) fn match_gt_ct(input: i16, threshold: u8, ret_on_match: i16) -> i16 {
((threshold as i16 - input) >> 8) & ret_on_match
}
7 changes: 2 additions & 5 deletions base64ct/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,15 @@ extern crate alloc;
#[cfg(feature = "std")]
extern crate std;

pub mod padded;
pub mod unpadded;
pub mod url;

mod decoder;
mod encoder;
mod errors;
mod standard;

pub use errors::{Error, InvalidEncodingError, InvalidLengthError};
pub use standard::{padded, unpadded};

/// Padding character
const PAD: u8 = b'=';

/// Standard encoding for bytes 62 and 63
const STD_HI_BYTES: (u8, u8) = (b'+', b'/');
55 changes: 0 additions & 55 deletions base64ct/src/padded.rs

This file was deleted.

127 changes: 127 additions & 0 deletions base64ct/src/standard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
//! Standard Base64 encoding with `=` padding.
//!
//! ```text
//! [A-Z] [a-z] [0-9] + /
//! 0x41-0x5a, 0x61-0x7a, 0x30-0x39, 0x2b, 0x2f
//! ```

use crate::encoder::match_gt_ct;

/// Standard encoding for bytes 62 and 63
const STD_HI_BYTES: (u8, u8) = (b'+', b'/');

/// Standard Base64 encoding with `=` padding.
pub mod padded {
use super::{encode_6bits, STD_HI_BYTES};
use crate::{decoder, encoder, Error, InvalidEncodingError, InvalidLengthError};

#[cfg(feature = "alloc")]
use alloc::{string::String, vec::Vec};

/// Decode a standard Base64 with padding string into the provided
/// destination buffer.
pub fn decode(src: impl AsRef<[u8]>, dst: &mut [u8]) -> Result<&[u8], Error> {
decoder::decode(src, dst, true, STD_HI_BYTES)
}

/// Decode a standard Base64 string with padding in-place.
pub fn decode_in_place(buf: &mut [u8]) -> Result<&[u8], InvalidEncodingError> {
decoder::decode_in_place(buf, true, STD_HI_BYTES)
}

/// Decode a standard Base64 string with padding into a byte vector.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn decode_vec(input: &str) -> Result<Vec<u8>, Error> {
decoder::decode_vec(input, true, STD_HI_BYTES)
}

/// Encode the input byte slice as standard Base64 with padding.
///
/// Writes the result into the provided destination slice, returning an
/// ASCII-encoded Base64 string value.
pub fn encode<'a>(src: &[u8], dst: &'a mut [u8]) -> Result<&'a str, InvalidLengthError> {
encoder::encode(src, dst, true, encode_6bits)
}

/// Encode input byte slice into a [`String`] containing standard Base64
/// with padding.
///
/// # Panics
/// If `input` length is greater than `usize::MAX/4`.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn encode_string(input: &[u8]) -> String {
encoder::encode_string(input, true, encode_6bits)
}

/// Get the length of padded Base64 produced by encoding the given bytes.
///
/// WARNING: this function will return `0` for lengths greater than `usize::MAX/4`!
pub fn encoded_len(bytes: &[u8]) -> usize {
encoder::encoded_len(bytes, true)
}
}

/// Standard Base64 encoding *without* padding.
pub mod unpadded {
use super::{encode_6bits, STD_HI_BYTES};
use crate::{decoder, encoder, Error, InvalidEncodingError, InvalidLengthError};

#[cfg(feature = "alloc")]
use alloc::{string::String, vec::Vec};

/// Decode a standard Base64 string without padding into the provided
/// destination buffer.
pub fn decode(src: impl AsRef<[u8]>, dst: &mut [u8]) -> Result<&[u8], Error> {
decoder::decode(src, dst, false, STD_HI_BYTES)
}

/// Decode a standard Base64 string without padding in-place.
pub fn decode_in_place(buf: &mut [u8]) -> Result<&[u8], InvalidEncodingError> {
decoder::decode_in_place(buf, false, STD_HI_BYTES)
}

/// Decode a standard Base64 string without padding into a byte vector.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn decode_vec(input: &str) -> Result<Vec<u8>, Error> {
decoder::decode_vec(input, false, STD_HI_BYTES)
}

/// Encode the input byte slice as standard Base64 with padding.
///
/// Writes the result into the provided destination slice, returning an
/// ASCII-encoded Base64 string value.
pub fn encode<'a>(src: &[u8], dst: &'a mut [u8]) -> Result<&'a str, InvalidLengthError> {
encoder::encode(src, dst, false, encode_6bits)
}

/// Encode input byte slice into a [`String`] containing standard Base64
/// without padding.
///
/// # Panics
/// If `input` length is greater than `usize::MAX/4`.
#[cfg(feature = "alloc")]
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))]
pub fn encode_string(input: &[u8]) -> String {
encoder::encode_string(input, false, encode_6bits)
}

/// Get the length of unpadded Base64 produced by encoding the given bytes.
///
/// WARNING: this function will return `0` for lengths greater than `usize::MAX/4`!
pub fn encoded_len(bytes: &[u8]) -> usize {
encoder::encoded_len(bytes, false)
}
}

#[inline(always)]
fn encode_6bits(src: i16) -> u8 {
let mut diff = b'A' as i16;
diff += match_gt_ct(src, 25, 6);
diff -= match_gt_ct(src, 51, 75);
diff -= match_gt_ct(src, 61, b'+' as i16 - 0x1c);
diff += match_gt_ct(src, 62, b'/' as i16 - b'+' as i16 - 1);
(src + diff) as u8
}
55 changes: 0 additions & 55 deletions base64ct/src/unpadded.rs

This file was deleted.

Loading