bin_encode_decode/decode/func.rs
1use crate::*;
2
3/// decodes a given encoded string based on a specified charset, using 4-character
4/// groups to restore the original bytes. Each character in the `decode_str` string
5/// is mapped to an index in `charset` to form the decoded bytes.
6///
7/// # Parameters
8/// - `charset`: A string representing the character set used to encode and decode
9/// the data. Each character should have a unique position in `charset`.
10/// - `decode_str`: The string to be decoded, which was originally encoded with
11/// the provided `charset`.
12///
13/// # Returns
14/// Returns a `Result` containing the decoded `String` if successful, or a `DecodeError` if the charset is invalid.
15///
16/// # Example
17/// ```
18/// use bin_encode_decode::*;
19///
20/// let charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_=";
21/// let encoded_str = "aab0aabLaabZaab0";
22/// let decoded_str = decode(charset, encoded_str);
23/// assert_eq!(decoded_str.unwrap(), "test");
24/// ```
25pub fn decode(charset: &str, decode_str: &str) -> Result<String, DecodeError> {
26 if !Endecode::judge_charset_safe(charset) {
27 return Err(DecodeError::CharsetError);
28 }
29 let mut buffer: Vec<u8> = Vec::new();
30 let mut decoded: Vec<u8> = Vec::new();
31 for ch in decode_str.chars() {
32 if let Some(idx) = charset.chars().position(|c| c == ch) {
33 buffer.push(idx as u8);
34 }
35 if buffer.len() == 4 {
36 let combined: usize = ((buffer[0] as usize) << 18)
37 | ((buffer[1] as usize) << 12)
38 | ((buffer[2] as usize) << 6)
39 | (buffer[3] as usize);
40 decoded.push((combined >> 16) as u8);
41 decoded.push((combined >> 8) as u8);
42 decoded.push(combined as u8);
43 buffer.clear();
44 }
45 }
46 let decode_res: String =
47 String::from_utf8(decoded.into_iter().filter(|&x| x != 0).collect()).unwrap_or_default();
48 Ok(decode_res)
49}