Split rust codebase into separate files

This commit is contained in:
2024-12-22 16:10:41 -06:00
parent 902afb925b
commit 0229a578de
3 changed files with 108 additions and 99 deletions

View File

@@ -1,51 +1,16 @@
use std::collections::HashMap;
use std::path;
use salvo::http::HeaderValue;
use salvo::logging::Logger;
use salvo::prelude::{
affix_state, handler, Depot, Listener, Request, Response, Router, Server, Service, StaticDir,
TcpListener,
};
use salvo::prelude::*;
use crate::models::State;
fn search(buf: &[u8], pattern: &[u8], start_index: usize) -> Option<usize> {
let mut i = start_index;
// If the buffer is empty, the pattern is too long
if pattern.len() > buf.len() {
return None;
}
// If the pattern is empty
if pattern.len() == 0 {
return None;
}
// If the starting index is too high
if start_index >= buf.len() {
return None;
}
while i < buf.len() {
for j in 0..pattern.len() {
// If the pattern is too long to fit in the buffer anymore
if i + j >= buf.len() {
return None;
}
// If the pattern stops matching
if buf[i + j] != pattern[j] {
break;
}
// If the pattern is found
if j == pattern.len() - 1 {
return Some(i);
}
}
i += 1;
}
None
}
mod models;
mod utility;
#[handler]
async fn download(depot: &mut Depot, req: &mut Request, res: &mut Response) {
@@ -69,63 +34,6 @@ async fn download(depot: &mut Depot, req: &mut Request, res: &mut Response) {
HeaderValue::from_static("application/octet-stream"),
);
}
#[derive(Default, Clone, Debug)]
struct State<'a> {
executables: HashMap<&'a str, Executable>,
}
#[derive(Default, Clone, Debug)]
struct Executable {
data: Vec<u8>,
filename: String,
key_start: usize,
key_end: usize,
}
impl<'a> State<'a> {
fn add_executable(&mut self, exe_type: &'a str, exe_path: &str) {
let data = std::fs::read(&exe_path).expect("Unable to read file");
let pattern = "a".repeat(1024);
let key_start = search(&data, pattern.as_bytes(), 0).unwrap();
let key_end = key_start + pattern.len();
let filename = path::Path::new(&exe_path)
.file_name()
.unwrap()
.to_string_lossy()
.into_owned();
let exe = Executable {
data,
filename,
key_start: key_start,
key_end: key_end,
};
self.executables.insert(exe_type, exe);
}
}
impl Executable {
fn with_key(&self, new_key: &[u8]) -> Vec<u8> {
let mut data = self.data.clone();
// Copy the key into the data
for i in 0..new_key.len() {
data[self.key_start + i] = new_key[i];
}
// If the new key is shorter than the old key, we just write over the remaining data
if new_key.len() < self.key_end - self.key_start {
for i in self.key_start + new_key.len()..self.key_end {
data[i] = b' ';
}
}
return data;
}
}
#[tokio::main]
async fn main() {

61
src/models.rs Normal file
View File

@@ -0,0 +1,61 @@
use std::{collections::HashMap, path};
use crate::utility::search;
#[derive(Default, Clone, Debug)]
pub(crate) struct State<'a> {
pub executables: HashMap<&'a str, Executable>,
}
impl<'a> State<'a> {
pub(crate) fn add_executable(&mut self, exe_type: &'a str, exe_path: &str) {
let data = std::fs::read(&exe_path).expect("Unable to read file");
let pattern = "a".repeat(1024);
let key_start = search(&data, pattern.as_bytes(), 0).unwrap();
let key_end = key_start + pattern.len();
let filename = path::Path::new(&exe_path)
.file_name()
.unwrap()
.to_string_lossy()
.into_owned();
let exe = Executable {
data,
filename,
key_start: key_start,
key_end: key_end,
};
self.executables.insert(exe_type, exe);
}
}
#[derive(Default, Clone, Debug)]
pub(crate) struct Executable {
pub data: Vec<u8>,
pub filename: String,
pub key_start: usize,
pub key_end: usize,
}
impl Executable {
pub(crate) fn with_key(&self, new_key: &[u8]) -> Vec<u8> {
let mut data = self.data.clone();
// Copy the key into the data
for i in 0..new_key.len() {
data[self.key_start + i] = new_key[i];
}
// If the new key is shorter than the old key, we just write over the remaining data
if new_key.len() < self.key_end - self.key_start {
for i in self.key_start + new_key.len()..self.key_end {
data[i] = b' ';
}
}
return data;
}
}

40
src/utility.rs Normal file
View File

@@ -0,0 +1,40 @@
pub(crate) fn search(buf: &[u8], pattern: &[u8], start_index: usize) -> Option<usize> {
let mut i = start_index;
// If the buffer is empty, the pattern is too long
if pattern.len() > buf.len() {
return None;
}
// If the pattern is empty
if pattern.len() == 0 {
return None;
}
// If the starting index is too high
if start_index >= buf.len() {
return None;
}
while i < buf.len() {
for j in 0..pattern.len() {
// If the pattern is too long to fit in the buffer anymore
if i + j >= buf.len() {
return None;
}
// If the pattern stops matching
if buf[i + j] != pattern[j] {
break;
}
// If the pattern is found
if j == pattern.len() - 1 {
return Some(i);
}
}
i += 1;
}
None
}