From 83ab88f93e3d62a24173926ddc33957720abfd40 Mon Sep 17 00:00:00 2001 From: sergiotarxz Date: Sat, 7 Nov 2020 00:55:34 +0100 Subject: [PATCH] feat: Added needed types to implement realpath.[ --- Cargo.toml | 1 + src/lib.rs | 1 + src/packet.rs | 1 + src/packet/types.rs | 1 + src/packet/types/str.rs | 23 +++++++++++++++++++++++ src/sftp.rs | 29 +++++++++++++++++++++++++++++ 6 files changed, 56 insertions(+) create mode 100644 src/packet/types.rs create mode 100644 src/packet/types/str.rs create mode 100644 src/sftp.rs diff --git a/Cargo.toml b/Cargo.toml index d6e9f0e..d7381b6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,3 +8,4 @@ include = [ "lib/**/*.php" ] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +regex = "1.1.9" diff --git a/src/lib.rs b/src/lib.rs index 43e7ca6..b2d874a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,2 +1,3 @@ pub mod packet; pub mod constants; +pub mod sftp; diff --git a/src/packet.rs b/src/packet.rs index cc625a2..f309162 100644 --- a/src/packet.rs +++ b/src/packet.rs @@ -1,5 +1,6 @@ pub mod data; pub mod header; +pub mod types; use crate::constants::SSH_FXP_INIT; use crate::constants::SSH_FXP_VERSION; diff --git a/src/packet/types.rs b/src/packet/types.rs new file mode 100644 index 0000000..3bb9df5 --- /dev/null +++ b/src/packet/types.rs @@ -0,0 +1 @@ +pub mod str; diff --git a/src/packet/types/str.rs b/src/packet/types/str.rs new file mode 100644 index 0000000..df0eae3 --- /dev/null +++ b/src/packet/types/str.rs @@ -0,0 +1,23 @@ +use regex::Regex; +use std::io::BufRead; + +pub fn serialize(string_to_serialize: &str) -> Vec { + [string_to_serialize, "\0"].join("").as_bytes().to_vec() +} + +pub fn deserialize(bytes_to_deserialize: &[u8]) -> String { + Regex::new("\0$") + .expect("Unable to parse regex \\0$.") + .replace( + &String::from_utf8(bytes_to_deserialize.to_vec()) + .expect("Get a utf-8 encoded ftp client."), + "", + ) + .to_string() +} + +pub fn get_u8_array_c_string_from_u8_array(mut u8_array: &[u8]) -> Vec { + let mut return_array: Vec = Vec::new(); + u8_array.read_until(0, &mut return_array).expect("Could not read string from u8_array"); + return_array +} diff --git a/src/sftp.rs b/src/sftp.rs new file mode 100644 index 0000000..6cdcdc6 --- /dev/null +++ b/src/sftp.rs @@ -0,0 +1,29 @@ +use regex::Regex; + +pub fn realpath(path: &str) -> String { + let components: Vec<&str> = Regex::new("/+") + .expect("Error parsing regex /+.") + .split(path) + .collect(); + let mut abs_path: Vec<&str> = Vec::new(); + for component in components { + if (Regex::new("^\\.$") + .expect("Error parsing regex ^\\.$") + .is_match(component)) + || (Regex::new("^$") + .expect("Error parsing regex ^$.") + .is_match(component)) + { + continue; + } else if Regex::new("^\\.\\.$") + .expect("Error parsing regex ^\\.\\.$.") + .is_match(component) + { + abs_path.pop(); + } else { + abs_path.push(component); + } + } + let abs_path: &str = &abs_path.join("/"); + ["/", abs_path].join("") +}