pub struct MemoryReader { /* private fields */ }
Expand description
Memory reader.
This implementation keeps the entire PAKS file in memory.
Implementations§
Source§impl MemoryReader
impl MemoryReader
Sourcepub fn from_bytes(bytes: &[u8], key: &Key) -> Result<MemoryReader, ErrorKind>
pub fn from_bytes(bytes: &[u8], key: &Key) -> Result<MemoryReader, ErrorKind>
Parses the bytes as the PAKS file format for reading.
§Notes
The reader has specific alignment requirements for the buffer. For this reason the entire byte array will be copied to an internal buffer.
§Errors
ErrorKind::InvalidInput
: Bytes length is not a multiple of the block size.ErrorKind::InvalidData
: Incorrect version info or authentication checks failed.
Sourcepub fn from_blocks(
blocks: Vec<Block>,
key: &Key,
) -> Result<MemoryReader, Vec<Block>>
pub fn from_blocks( blocks: Vec<Block>, key: &Key, ) -> Result<MemoryReader, Vec<Block>>
Parses the blocks as the PAKS file format for reading.
Examples found in repository?
5fn main() {
6 let ref key = [13, 42];
7
8 // Create the editor object to create PAKS files in memory.
9 let mut edit = paks::MemoryEditor::new();
10
11 // Let's create a file `foo` under a directory `sub`.
12 // If a file already exists by this name it will be overwritten.
13 edit.create_file(b"sub/foo", DATA, key);
14
15 // When done the editor object can be finalized and returns the encrypted PAKS file as a `Vec<Block>`.
16 // It also returns the unencrypted directory for final inspection if desired.
17 let (paks, dir) = edit.finish(key);
18
19 // Print the directory.
20 print!("The directory:\n\n```\n{}```\n\n", dir.display().to_string());
21
22 // Print the PAKS file itself.
23 print!("The RAW data:\n\n```\n{:x?}\n```\n", paks);
24
25 // Create the reader object to inspect PAKS files in memory.
26 let read = paks::MemoryReader::from_blocks(paks, key).unwrap();
27
28 // Find the file created earlier and read its data into a `Vec<u8>`.
29 let data = read.read(b"sub/foo", key).unwrap();
30
31 // Check that it still matches the expected data.
32 assert_eq!(DATA, &data[..]);
33}
Source§impl MemoryReader
impl MemoryReader
Sourcepub fn read(&self, path: &[u8], key: &Key) -> Result<Vec<u8>, ErrorKind>
pub fn read(&self, path: &[u8], key: &Key) -> Result<Vec<u8>, ErrorKind>
Reads the contents of a file from the PAKS archive.
Examples found in repository?
5fn main() {
6 let ref key = [13, 42];
7
8 // Create the editor object to create PAKS files in memory.
9 let mut edit = paks::MemoryEditor::new();
10
11 // Let's create a file `foo` under a directory `sub`.
12 // If a file already exists by this name it will be overwritten.
13 edit.create_file(b"sub/foo", DATA, key);
14
15 // When done the editor object can be finalized and returns the encrypted PAKS file as a `Vec<Block>`.
16 // It also returns the unencrypted directory for final inspection if desired.
17 let (paks, dir) = edit.finish(key);
18
19 // Print the directory.
20 print!("The directory:\n\n```\n{}```\n\n", dir.display().to_string());
21
22 // Print the PAKS file itself.
23 print!("The RAW data:\n\n```\n{:x?}\n```\n", paks);
24
25 // Create the reader object to inspect PAKS files in memory.
26 let read = paks::MemoryReader::from_blocks(paks, key).unwrap();
27
28 // Find the file created earlier and read its data into a `Vec<u8>`.
29 let data = read.read(b"sub/foo", key).unwrap();
30
31 // Check that it still matches the expected data.
32 assert_eq!(DATA, &data[..]);
33}
Sourcepub fn read_to_string(
&self,
path: &[u8],
key: &Key,
) -> Result<String, ErrorKind>
pub fn read_to_string( &self, path: &[u8], key: &Key, ) -> Result<String, ErrorKind>
Reads the contents of a file from the PAKS archive into a string.
Sourcepub fn read_section(
&self,
section: &Section,
key: &Key,
) -> Result<Vec<Block>, ErrorKind>
pub fn read_section( &self, section: &Section, key: &Key, ) -> Result<Vec<Block>, ErrorKind>
Decrypts and authenticates the section.
The key is not required to be the same as used to open the PAKS file.
§Errors
ErrorKind::InvalidInput
: The the descriptor is not a file descriptor.ErrorKind::InvalidData
: The file’s MAC is incorrect, the file is corrupted.
Sourcepub fn read_data(
&self,
desc: &Descriptor,
key: &Key,
) -> Result<Vec<u8>, ErrorKind>
pub fn read_data( &self, desc: &Descriptor, key: &Key, ) -> Result<Vec<u8>, ErrorKind>
Decrypts the contents of the given file descriptor.
See read_section
for more information.
Sourcepub fn read_data_into(
&self,
desc: &Descriptor,
key: &Key,
byte_offset: usize,
dest: &mut [u8],
) -> Result<(), ErrorKind>
pub fn read_data_into( &self, desc: &Descriptor, key: &Key, byte_offset: usize, dest: &mut [u8], ) -> Result<(), ErrorKind>
Decrypts the contents of the given file descriptor into the dest buffer.
See read_section
for more information.
Methods from Deref<Target = Directory>§
Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of Descriptor
s in the directory.
Sourcepub fn find_desc(&self, path: &[u8]) -> Option<&Descriptor>
pub fn find_desc(&self, path: &[u8]) -> Option<&Descriptor>
Finds a descriptor by its path.
Sourcepub fn find_file(&self, path: &[u8]) -> Option<&Descriptor>
pub fn find_file(&self, path: &[u8]) -> Option<&Descriptor>
Finds a file descriptor by its path.
Sourcepub fn get_children(&self, path: &[u8]) -> Option<&[Descriptor]>
pub fn get_children(&self, path: &[u8]) -> Option<&[Descriptor]>
Gets the child descriptors of the directory at the given path.
Sourcepub fn display(&self) -> impl '_ + Display
pub fn display(&self) -> impl '_ + Display
Returns a displayable directory.
Examples found in repository?
5fn main() {
6 let ref key = [13, 42];
7
8 // Create the editor object to create PAKS files in memory.
9 let mut edit = paks::MemoryEditor::new();
10
11 // Let's create a file `foo` under a directory `sub`.
12 // If a file already exists by this name it will be overwritten.
13 edit.create_file(b"sub/foo", DATA, key);
14
15 // When done the editor object can be finalized and returns the encrypted PAKS file as a `Vec<Block>`.
16 // It also returns the unencrypted directory for final inspection if desired.
17 let (paks, dir) = edit.finish(key);
18
19 // Print the directory.
20 print!("The directory:\n\n```\n{}```\n\n", dir.display().to_string());
21
22 // Print the PAKS file itself.
23 print!("The RAW data:\n\n```\n{:x?}\n```\n", paks);
24
25 // Create the reader object to inspect PAKS files in memory.
26 let read = paks::MemoryReader::from_blocks(paks, key).unwrap();
27
28 // Find the file created earlier and read its data into a `Vec<u8>`.
29 let data = read.read(b"sub/foo", key).unwrap();
30
31 // Check that it still matches the expected data.
32 assert_eq!(DATA, &data[..]);
33}