Browse Source

feat: Added needed types to implement realpath.[

current
sergiotarxz 7 months ago
parent
commit
83ab88f93e
6 changed files with 56 additions and 0 deletions
  1. +1
    -0
      Cargo.toml
  2. +1
    -0
      src/lib.rs
  3. +1
    -0
      src/packet.rs
  4. +1
    -0
      src/packet/types.rs
  5. +23
    -0
      src/packet/types/str.rs
  6. +29
    -0
      src/sftp.rs

+ 1
- 0
Cargo.toml 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"

+ 1
- 0
src/lib.rs View File

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

+ 1
- 0
src/packet.rs 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
- 0
src/packet/types.rs View File

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

+ 23
- 0
src/packet/types/str.rs 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
- 0
src/sftp.rs 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("")
}

Loading…
Cancel
Save