e9571_base64_pro_lib 0.1.0

A Rust library for base64 encoding/decoding with special character handling and file conversion support
Documentation
use base64::{engine::general_purpose, Engine as _};
use std::fs::{File, OpenOptions};
use std::io::{Read, Write};
use std::path::Path;

// V4 version
// 1 Supports secure encoding of HTTP GET variables, resolving issues with data truncation when variables contain special characters
// 2 Supports base64-encoded compression for transmission, serving as an auxiliary parameter for light encryption and secondary encryption to enhance communication security
// 3 Supports converting files to base64 and restoring files from base64

pub mod e9571_base64_pro_lib {
    use std::fs::{File, OpenOptions};
    use std::io::{Read, Write};
    use std::path::Path;
    use base64::Engine;
    use base64::engine::general_purpose;

    /// Decodes a base64-encoded string, reverting transport interference.
    pub fn decode_go_base64(input: &str) -> String {
        let reverted = replace_revert(input);
        match general_purpose::STANDARD.decode(&reverted) {
            Ok(decoded) => String::from_utf8_lossy(&decoded).to_string(),
            Err(_) => String::new(),
        }
    }

    /// Encodes a string to base64, applying transport interference replacements.
    pub fn encode_go_base64(input: &str) -> String {
        let encoded = general_purpose::STANDARD.encode(input.as_bytes());
        replace_data(&encoded)
    }

    /// Replaces special characters to avoid transport interference.
    pub fn replace_data(str: &str) -> String {
        str.replace("/", "@")
            .replace("+", "_")
            .replace("=", "$")
    }

    /// Reverts special character replacements for decoding.
    pub fn replace_revert(str: &str) -> String {
        str.replace("@", "/")
            .replace("_", "+")
            .replace("$", "=")
    }

    /// Repeatedly encodes a string with base64 `num` times.
    pub fn zip_base64(str: &str, num: i32) -> String {
        let mut result = str.to_string();
        for _ in 0..num {
            result = encode_go_base64(&result);
        }
        result
    }

    /// Repeatedly decodes a base64 string `num` times.
    pub fn unzip_base64(str: &str, num: i32) -> String {
        let mut result = str.to_string();
        for _ in 0..num {
            result = decode_go_base64(&result);
        }
        result
    }

    /// Converts a file to base64 and writes the result to an output file.
    pub fn file_to_base64<P: AsRef<Path>>(path: P, outfile: P) -> bool {
        let data = match File::open(&path) {
            Ok(mut file) => {
                let mut buffer = Vec::new();
                match file.read_to_end(&mut buffer) {
                    Ok(_) => buffer,
                    Err(e) => {
                        eprintln!("Error reading file: {}", e);
                        return false;
                    }
                }
            }
            Err(e) => {
                eprintln!("Error opening file: {}", e);
                return false;
            }
        };

        let base64_str = general_purpose::STANDARD.encode(&data);
        let mut file = match OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(&outfile)
        {
            Ok(file) => file,
            Err(e) => {
                eprintln!("Error opening output file: {}", e);
                return false;
            }
        };

        match file.write_all(base64_str.as_bytes()) {
            Ok(_) => true,
            Err(e) => {
                eprintln!("Error writing to file: {}", e);
                false
            }
        }
    }

    /// Reads a base64 string from a file, decodes it, and writes to an output file.
    pub fn base64_to_file<P: AsRef<Path>>(path: P, outfile: P) -> bool {
        let data = match File::open(&path) {
            Ok(mut file) => {
                let mut buffer = String::new();
                match file.read_to_string(&mut buffer) {
                    Ok(_) => buffer,
                    Err(e) => {
                        eprintln!("Error reading file: {}", e);
                        return false;
                    }
                }
            }
            Err(e) => {
                eprintln!("Error opening file: {}", e);
                return false;
            }
        };

        let decoded_data = match general_purpose::STANDARD.decode(&data) {
            Ok(data) => data,
            Err(e) => {
                eprintln!("Error decoding base64: {}", e);
                return false;
            }
        };

        let mut file = match OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(&outfile)
        {
            Ok(file) => file,
            Err(e) => {
                eprintln!("Error opening output file: {}", e);
                return false;
            }
        };

        match file.write_all(&decoded_data) {
            Ok(_) => true,
            Err(e) => {
                eprintln!("Error writing to file: {}", e);
                false
            }
        }
    }

    /// Decodes a base64 string and writes it to a file.
    pub fn base64_to_path_file<P: AsRef<Path>>(str: &str, outfile: P) -> bool {
        let decoded_data = match general_purpose::STANDARD.decode(str) {
            Ok(data) => data,
            Err(e) => {
                eprintln!("Error decoding base64: {}", e);
                return false;
            }
        };

        let mut file = match OpenOptions::new()
            .write(true)
            .create(true)
            .truncate(true)
            .open(&outfile)
        {
            Ok(file) => file,
            Err(e) => {
                eprintln!("Error opening output file: {}", e);
                return false;
            }
        };

        match file.write_all(&decoded_data) {
            Ok(_) => true,
            Err(e) => {
                eprintln!("Error writing to file: {}", e);
                false
            }
        }
    }

    /// Reads a file and returns its base64-encoded string.
    pub fn base64_to_str<P: AsRef<Path>>(path: P) -> String {
        let data = match File::open(&path) {
            Ok(mut file) => {
                let mut buffer = Vec::new();
                match file.read_to_end(&mut buffer) {
                    Ok(_) => buffer,
                    Err(e) => {
                        eprintln!("Error reading file: {}", e);
                        return String::new();
                    }
                }
            }
            Err(e) => {
                eprintln!("Error opening file: {}", e);
                return String::new();
            }
        };

        general_purpose::STANDARD.encode(&data)
    }
}