1#![allow(missing_docs)]
2
3use std::borrow::Cow;
4use std::io;
5use std::io::{Read, Write};
6use std::path::{Component, Path, MAIN_SEPARATOR};
7
8pub mod stream {
10 pub use crate::read::stream::*;
11}
12pub mod write {
14 use crate::write::{FileOptionExtension, FileOptions};
15 pub trait FileOptionsExt {
17 fn with_deprecated_encryption(self, password: &[u8]) -> Self;
21 }
22 impl<T: FileOptionExtension> FileOptionsExt for FileOptions<'_, T> {
23 fn with_deprecated_encryption(self, password: &[u8]) -> FileOptions<'static, T> {
24 self.with_deprecated_encryption(password)
25 }
26 }
27}
28
29pub trait LittleEndianWriteExt: Write {
31 fn write_u16_le(&mut self, input: u16) -> io::Result<()> {
32 self.write_all(&input.to_le_bytes())
33 }
34
35 fn write_u32_le(&mut self, input: u32) -> io::Result<()> {
36 self.write_all(&input.to_le_bytes())
37 }
38
39 fn write_u64_le(&mut self, input: u64) -> io::Result<()> {
40 self.write_all(&input.to_le_bytes())
41 }
42
43 fn write_u128_le(&mut self, input: u128) -> io::Result<()> {
44 self.write_all(&input.to_le_bytes())
45 }
46}
47
48impl<W: Write + ?Sized> LittleEndianWriteExt for W {}
49
50pub trait LittleEndianReadExt: Read {
52 fn read_u16_le(&mut self) -> io::Result<u16> {
53 let mut out = [0u8; 2];
54 self.read_exact(&mut out)?;
55 Ok(u16::from_le_bytes(out))
56 }
57
58 fn read_u32_le(&mut self) -> io::Result<u32> {
59 let mut out = [0u8; 4];
60 self.read_exact(&mut out)?;
61 Ok(u32::from_le_bytes(out))
62 }
63
64 fn read_u64_le(&mut self) -> io::Result<u64> {
65 let mut out = [0u8; 8];
66 self.read_exact(&mut out)?;
67 Ok(u64::from_le_bytes(out))
68 }
69}
70
71impl<R: Read> LittleEndianReadExt for R {}
72
73pub fn path_to_string<T: AsRef<Path>>(path: T) -> Box<str> {
75 let mut maybe_original = None;
76 if let Some(original) = path.as_ref().to_str() {
77 if original.is_empty() || original == "." || original == ".." {
78 return String::new().into_boxed_str();
79 }
80 if original.starts_with(MAIN_SEPARATOR) {
81 if original.len() == 1 {
82 return MAIN_SEPARATOR.to_string().into_boxed_str();
83 } else if (MAIN_SEPARATOR == '/' || !original[1..].contains(MAIN_SEPARATOR))
84 && !original.ends_with('.')
85 && !original.contains([MAIN_SEPARATOR, MAIN_SEPARATOR])
86 && !original.contains([MAIN_SEPARATOR, '.', MAIN_SEPARATOR])
87 && !original.contains([MAIN_SEPARATOR, '.', '.', MAIN_SEPARATOR])
88 {
89 maybe_original = Some(&original[1..]);
90 }
91 } else if !original.contains(MAIN_SEPARATOR) {
92 return original.into();
93 }
94 }
95 let mut recreate = maybe_original.is_none();
96 let mut normalized_components = Vec::new();
97
98 for component in path.as_ref().components() {
99 match component {
100 Component::Normal(os_str) => match os_str.to_str() {
101 Some(valid_str) => normalized_components.push(Cow::Borrowed(valid_str)),
102 None => {
103 recreate = true;
104 normalized_components.push(os_str.to_string_lossy());
105 }
106 },
107 Component::ParentDir => {
108 recreate = true;
109 normalized_components.pop();
110 }
111 _ => {
112 recreate = true;
113 }
114 }
115 }
116 if recreate {
117 normalized_components.join("/").into()
118 } else {
119 maybe_original.unwrap().into()
120 }
121}