bin-encode-decode 11.1.0

A high-performance binary encode and decode library that supports customizable character sets beyond Base64.
Documentation
use super::*;

impl Decode {
    /// Emits up to 3 decoded non-zero bytes from four charset symbol indices.
    ///
    /// # Arguments
    ///
    /// - `&mut Vec<u8>` - Destination decoded byte buffer.
    /// - `usize` - First symbol index.
    /// - `usize` - Second symbol index.
    /// - `usize` - Third symbol index.
    /// - `usize` - Fourth symbol index.
    #[inline(always)]
    fn push_decoded_group(decoded: &mut Vec<u8>, v0: usize, v1: usize, v2: usize, v3: usize) {
        let high_bits: usize = v0 | v1;
        let low_high: usize = (v2 >> 2) | (v3 >> 6);
        if (high_bits | low_high) == 0 {
            let b2: u8 = ((v2 << 6) | v3) as u8;
            if b2 != 0 {
                decoded.push(b2);
            }
            return;
        }
        let combined: usize = (v0 << 18) | (v1 << 12) | (v2 << 6) | v3;
        let b0: u8 = (combined >> 16) as u8;
        let b1: u8 = (combined >> 8) as u8;
        let b2: u8 = combined as u8;
        if b0 != 0 {
            decoded.push(b0);
        }
        if b1 != 0 {
            decoded.push(b1);
        }
        if b2 != 0 {
            decoded.push(b2);
        }
    }

    /// Decodes an encoded string using specified character set.
    ///
    /// Processes 4-character groups to reconstruct original bytes by mapping each
    /// character to its position in the charset.
    ///
    /// # Arguments
    ///
    /// - `&str` - The character set used for decoding.
    /// - `&str` - The string to decode.
    ///
    /// # Returns
    ///
    /// - `Result<String, DecodeError>` - Result of decoding operation.
    pub fn execute(charset: &str, decode_str: &str) -> Result<String, DecodeError> {
        if charset.is_ascii() {
            let charset_bytes: &[u8] = charset.as_bytes();
            let bytes: &[u8] = decode_str.as_bytes();
            let mut decoded: Vec<u8> = Vec::with_capacity(bytes.len() / 4);
            if charset_bytes.len() == CHARSET_LEN {
                let lut: [u8; DECODE_LUT_LEN] =
                    Charset::build_ascii_decode_lut_exact(charset_bytes)
                        .ok_or(DecodeError::CharsetError)?;
                let mut pos: usize = 0;
                let out_ptr: *mut u8 = decoded.as_mut_ptr();
                let mut out_len: usize = 0;
                for &[b0, b1, b2_in, b3] in bytes.as_chunks::<4>().0 {
                    let v0: u8 = lut[b0 as usize];
                    let v1: u8 = lut[b1 as usize];
                    let v2: u8 = lut[b2_in as usize];
                    let v3: u8 = lut[b3 as usize];
                    let high_bits: u8 = v0 | v1;
                    let low_bad: u8 = (v2 >> 2) | (v3 & !SIX_BIT_MASK);
                    if (high_bits | low_bad) != 0 {
                        break;
                    }
                    let b2: u8 = (v2 << 6) | v3;
                    if b2 != 0 {
                        unsafe {
                            *out_ptr.add(out_len) = b2;
                        }
                        out_len += 1;
                    }
                    pos += 4;
                }
                unsafe {
                    decoded.set_len(out_len);
                }
                let mut group: [usize; 4] = [0usize; 4];
                let mut group_len: usize = 0;
                for &byte in &bytes[pos..] {
                    let val: u8 = lut[byte as usize];
                    if val != INVALID_DECODE_VAL {
                        group[group_len] = val as usize;
                        group_len += 1;
                        if group_len == 4 {
                            Self::push_decoded_group(
                                &mut decoded,
                                group[0],
                                group[1],
                                group[2],
                                group[3],
                            );
                            group_len = 0;
                        }
                    }
                }
                return Ok(String::from_utf8(decoded).unwrap_or_default());
            }
            let lut: [usize; DECODE_LUT_LEN] =
                Charset::build_ascii_decode_lut_general(charset_bytes)
                    .ok_or(DecodeError::CharsetError)?;
            let mut group: [usize; 4] = [0usize; 4];
            let mut group_len: usize = 0;
            for &byte in bytes {
                let val: usize = lut[byte as usize];
                if val != INVALID_DECODE_INDEX {
                    group[group_len] = val;
                    group_len += 1;
                    if group_len == 4 {
                        Self::push_decoded_group(
                            &mut decoded,
                            group[0],
                            group[1],
                            group[2],
                            group[3],
                        );
                        group_len = 0;
                    }
                }
            }
            return Ok(String::from_utf8(decoded).unwrap_or_default());
        }
        let table: [(char, usize); UNICODE_HASH_SLOTS] =
            Charset::build_unicode_decode_table(charset).ok_or(DecodeError::CharsetError)?;
        let mut decoded: Vec<u8> = Vec::with_capacity(decode_str.len() / 4);
        let mut group: [usize; 4] = [0usize; 4];
        let mut group_len: usize = 0;
        for ch in decode_str.chars() {
            let mut slot: usize =
                (((ch as u32).wrapping_mul(UNICODE_HASH_MULT)) >> 25) as usize & UNICODE_HASH_MASK;
            loop {
                let entry: (char, usize) = table[slot];
                if entry.1 == UNICODE_EMPTY_SLOT {
                    break;
                }
                if entry.0 == ch {
                    group[group_len] = entry.1;
                    group_len += 1;
                    if group_len == 4 {
                        Self::push_decoded_group(
                            &mut decoded,
                            group[0],
                            group[1],
                            group[2],
                            group[3],
                        );
                        group_len = 0;
                    }
                    break;
                }
                slot = (slot + 1) & UNICODE_HASH_MASK;
            }
        }
        Ok(String::from_utf8(decoded).unwrap_or_default())
    }
}