feat: Added needed types to implement realpath.[

This commit is contained in:
sergiotarxz 2020-11-07 00:55:34 +01:00
parent 8dc05f5346
commit 83ab88f93e
6 changed files with 56 additions and 0 deletions

View File

@ -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"

View File

@ -1,2 +1,3 @@
pub mod packet;
pub mod constants;
pub mod sftp;

View File

@ -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;

1
src/packet/types.rs Normal file
View File

@ -0,0 +1 @@
pub mod str;

23
src/packet/types/str.rs Normal file
View File

@ -0,0 +1,23 @@
use regex::Regex;
use std::io::BufRead;
pub fn serialize(string_to_serialize: &str) -> Vec<u8> {
[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<u8> {
let mut return_array: Vec<u8> = Vec::new();
u8_array.read_until(0, &mut return_array).expect("Could not read string from u8_array");
return_array
}

29
src/sftp.rs Normal file
View File

@ -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("")
}