1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use std::{ io::Read, fs::File };
use crate::{
arc::Archive,
sec::Sector,
error::ParseError,
sec::{
SECTOR_SIZE,
SECTOR_DATA_SIZE,
SECTOR_HEADER_SIZE
},
};
use super::Store;
pub struct MemoryStore {
data: Vec<u8>
}
impl Store for MemoryStore {
#[inline]
fn new(mut main_file: File) -> crate::Result<Self> {
let mut buffer = Vec::new();
main_file.read_to_end(&mut buffer)?;
Ok(Self { data: buffer })
}
#[inline]
fn read(&self, archive: &Archive) -> crate::Result<Vec<u8>> {
let mut current_sector = archive.sector;
let mut data = vec![0; archive.length];
let mut remaining = archive.length;
let mut current = 0;
let mut chunk = 0;
loop {
let offset = current_sector as usize * SECTOR_SIZE;
if remaining >= SECTOR_DATA_SIZE {
let data_block = &self.data[offset..offset + SECTOR_SIZE];
match Sector::new(data_block, false) {
Ok(sector) => {
sector.header.validate(archive.id, chunk, archive.index_id)?;
current_sector = sector.header.next;
data[current..current + SECTOR_DATA_SIZE].copy_from_slice(sector.data_block);
},
Err(_) => return Err(ParseError::Sector(archive.sector).into())
};
remaining -= SECTOR_DATA_SIZE;
current += SECTOR_DATA_SIZE;
} else {
if remaining == 0 { break; }
let data_block = &self.data[offset..offset + SECTOR_HEADER_SIZE + remaining];
match Sector::new(data_block, false) {
Ok(sector) => {
sector.header.validate(archive.id, chunk, archive.index_id)?;
data[current..current + remaining].copy_from_slice(sector.data_block);
break;
},
Err(_) => return Err(ParseError::Sector(archive.sector).into())
};
}
chunk += 1;
}
Ok(data)
}
}