decrypt_truck 1.3.6

Fast library to decrypt game saves on ETS2 and ATS
Documentation
mod decoder;
mod strucs;
mod utils;

use decoder::bsii_decoder::decode;

use strucs::data_sii::SignatureType;
use utils::aes::decrypt;
use utils::file_type::try_read_u32;
use utils::zlib::uncompress;

pub fn decrypt_bin_file(file_bin: &Vec<u8>) -> Result<Vec<u8>, String> {
    let file_type = match try_read_u32(file_bin) {
        Ok(res) => res,
        Err(e) => return Err(e),
    };

    if file_type == SignatureType::PlainText as u32 {
        return Ok(file_bin.clone());
    }

    if file_type == SignatureType::Encrypted as u32 {
        let mut data = match decrypt(file_bin) {
            Ok(res) => res,
            Err(_) => return Err("Error decrypting data".to_string()),
        };

        match uncompress(&data.data) {
            Ok(res) => data.data = res,
            Err(e) => return Err(e),
        };

        let file_type_verify = match try_read_u32(&data.data) {
            Ok(file_type) => file_type,
            Err(e) => return Err(e),
        };

        if file_type_verify == SignatureType::PlainText as u32 {
            return Ok(data.data);
        }

        decode(&data.data)
    } else if file_type == SignatureType::Binary as u32 {
        decode(file_bin)
    } else {
        Err("Invalid file type".to_string())
    }
}