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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#[macro_use]
extern crate bitflags;
extern crate byteorder;
extern crate cached_file_view;
mod build;
mod crypto;
pub mod error;
mod parse;
use error::Result;
use parse::LazyLoadingPackedFile;
use std::borrow::Borrow;
use std::sync::Arc;
use std::sync::Mutex;
use std::fs::File;
use std::fmt;
use std::path::Path;
use cached_file_view::FileView;
use cached_file_view::FileViewMapping;
static DEBUG: bool = false;
const PFH5_PREAMBLE: u32 = 0x35484650;
const PFH4_PREAMBLE: u32 = 0x34484650;
const PFH3_PREAMBLE: u32 = 0x33484650;
const PFH2_PREAMBLE: u32 = 0x32484650;
const PFH0_PREAMBLE: u32 = 0x30484650;
const FILE_TYPE_BOOT: u32 = 0;
const FILE_TYPE_RELEASE: u32 = 1;
const FILE_TYPE_PATCH: u32 = 2;
const FILE_TYPE_MOD: u32 = 3;
const FILE_TYPE_MOVIE: u32 = 4;
bitflags! {
pub struct PFHFlags: u32 {
const HAS_BIG_HEADER = 0b0000_0001_0000_0000;
const HAS_ENCRYPTED_INDEX = 0b0000_0000_1000_0000;
const HAS_INDEX_WITH_TIMESTAMPS = 0b0000_0000_0100_0000;
const HAS_ENCRYPTED_CONTENT = 0b0000_0000_0001_0000;
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PFHVersion {
PFH5,
PFH4,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PFHFileType {
Boot,
Release,
Patch,
Mod,
Movie,
Other(u32),
}
#[derive(Clone)]
pub struct PackFile {
view: FileView,
begin: FileViewMapping
}
pub struct PackedFile {
pub timestamp: Option<u32>,
pub path: String,
data: Mutex<PackedFileData>
}
#[derive(Clone)]
pub(crate) enum PackedFileData {
DataBacked(Arc<Vec<u8>>),
LazyLoading(LazyLoadingPackedFile)
}
impl PFHFileType {
pub fn get_value(&self) -> u32 {
match *self {
PFHFileType::Boot => FILE_TYPE_BOOT,
PFHFileType::Release => FILE_TYPE_RELEASE,
PFHFileType::Patch => FILE_TYPE_PATCH,
PFHFileType::Mod => FILE_TYPE_MOD,
PFHFileType::Movie => FILE_TYPE_MOVIE,
PFHFileType::Other(value) => value
}
}
}
impl PFHVersion {
pub(crate) fn get_preamble(&self) -> u32 {
match *self {
PFHVersion::PFH5 => PFH5_PREAMBLE,
PFHVersion::PFH4 => PFH4_PREAMBLE,
}
}
}
impl PackFile {
pub fn get_version(&self) -> ::PFHVersion {
match parse::get_preamble(&self.view) {
PFH5_PREAMBLE => PFHVersion::PFH5,
PFH4_PREAMBLE => PFHVersion::PFH4,
_ => unreachable!()
}
}
pub fn get_file_type(&self) -> PFHFileType {
match parse::get_file_type(&self.view) {
FILE_TYPE_BOOT => PFHFileType::Boot,
FILE_TYPE_RELEASE => PFHFileType::Release,
FILE_TYPE_PATCH => PFHFileType::Patch,
FILE_TYPE_MOD => PFHFileType::Mod,
FILE_TYPE_MOVIE => PFHFileType::Movie,
value => PFHFileType::Other(value)
}
}
pub fn get_bitmask(&self) -> ::PFHFlags {
parse::get_bitmask(&self.view)
}
pub fn get_timestamp(&self) -> u32 {
parse::get_timestamp(&self.view)
}
pub fn get_pack_file_index(&self) -> Vec<String> {
parse::get_pack_file_index(&self.view)
}
}
impl PackedFile {
pub fn new(timestamp: Option<u32>, path: String, data: Vec<u8>) -> Self {
PackedFile {
data: Mutex::new(PackedFileData::DataBacked(Arc::new(data))),
timestamp,
path
}
}
pub fn load_data(&self) -> Result<()> {
let packed_file_data = &mut *self.data.lock().unwrap();
let data = if let PackedFileData::LazyLoading(lazy) = packed_file_data {
if lazy.is_encrypted {
let plaintext = crypto::decrypt_file(&lazy.file_view.read(&lazy.range)?.to_vec(), (lazy.range.end - lazy.range.start) as usize, false);
assert!(plaintext.len() as u64 == lazy.range.end - lazy.range.start, format!("{} != {}", plaintext.len(), lazy.range.end - lazy.range.start));
Arc::new(plaintext)
} else {
Arc::new(lazy.file_view.read(&lazy.range)?.to_vec())
}
} else { return Ok(()) };
*packed_file_data = PackedFileData::DataBacked(data);
Ok(())
}
pub fn get_data(&self) -> Result<Arc<Vec<u8>>> {
let packed_file_data = &mut *self.data.lock().unwrap();
let data = match &packed_file_data {
PackedFileData::LazyLoading(lazy) => {
if DEBUG {
println!("PackedFile get_data (0x{:x?}-0x{:x?})", lazy.range.start, lazy.range.end);
}
if lazy.is_encrypted {
let plaintext = crypto::decrypt_file(&lazy.file_view.read(&lazy.range)?.to_vec(), (lazy.range.end - lazy.range.start) as usize, false);
assert!(plaintext.len() as u64 == lazy.range.end - lazy.range.start, format!("{} != {}", plaintext.len(), lazy.range.end - lazy.range.start));
Arc::new(plaintext)
} else {
Arc::new(lazy.file_view.read(&lazy.range)?.to_vec())
}
},
PackedFileData::DataBacked(data) => {
return Ok(data.clone());
}
};
*packed_file_data = PackedFileData::DataBacked(data.clone());
Ok(data)
}
pub fn set_data(&mut self, data: Arc<Vec<u8>>) {
let packed_file_data = &mut *self.data.lock().unwrap();
*packed_file_data = PackedFileData::DataBacked(data);
}
}
impl Clone for PackedFile {
fn clone(&self) -> Self {
match &*self.data.lock().unwrap() {
PackedFileData::DataBacked(ref data) => PackedFile {
data: Mutex::new(PackedFileData::DataBacked(data.clone())),
timestamp: self.timestamp,
path: self.path.clone()
},
PackedFileData::LazyLoading(ref lazy) => PackedFile {
data: Mutex::new(PackedFileData::LazyLoading(lazy.clone())),
timestamp: self.timestamp,
path: self.path.clone()
}
}
}
}
impl fmt::Debug for PackedFile {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "PackedFile {{ timestamp: {:?}, path: {:?} }}", self.timestamp, &self.path)
}
}
pub fn parse_pack(input_file: File) -> Result<::PackFile> {
let pack_file = parse::parse_pack(input_file)?;
Ok(pack_file)
}
pub fn build_pack_from_filesystem(input_directory: &Path, output_file: &mut File, version: PFHVersion, bitmask: PFHFlags, file_type: ::PFHFileType, pfh_timestamp: u32, pack_files: &[String]) -> Result<()> {
build::build_pack_from_filesystem(input_directory, output_file, version, bitmask, file_type, pfh_timestamp, pack_files)
}
pub fn build_pack_from_memory<P: Borrow<PackedFile>>(input: &mut Vec<P>, output_file: &mut File, version: PFHVersion, bitmask: PFHFlags, file_type: ::PFHFileType, pfh_timestamp: u32, pack_files: &[String]) -> Result<()> {
build::build_pack_from_memory(pack_files, input, output_file, version, bitmask, file_type, pfh_timestamp)
}