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
use core::slice;
use std::io::{self, Write};
use aes::cipher::generic_array::GenericArray;
use block_modes::BlockMode;
use sha1::digest::generic_array::typenum::Unsigned;
use crate::archive::{Archivable, Archive, ArchiveLenSha1};
use crate::pakentry::FLAG_DELETED;
use crate::pakindex::PakIndex;
use crate::pakindexv1::PakIndexV1;
use crate::pakindexv2::PakIndexV2;
use crate::{aes256_base64_key, aes256_ecb_cipher, Aes256BlockSize};
use crate::{Aes256Cipher, Aes256Key, PakEntry, PakFile, PakInfo, PakVersion};
fn align_arbitrary(v: u64, alignment: u64) -> u64 {
match alignment {
0 => v,
_ => ((v + alignment - 1) / alignment) * alignment,
}
}
pub struct Cipher {
cipher: Aes256Cipher,
buf: GenericArray<u8, Aes256BlockSize>,
pending: usize,
}
impl Cipher {
pub fn new(cipher: Aes256Cipher) -> Self {
Self { cipher, buf: Default::default(), pending: 0 }
}
}
pub struct AssetWriter<'a, A: Archive> {
cipher: Option<Cipher>,
builder: &'a mut PakFileBuilder,
ar: ArchiveLenSha1<A>,
name: String,
entry: PakEntry,
import: bool,
}
impl<'a, A: Archive> AssetWriter<'a, A> {
pub fn size(&self) -> u64 {
self.builder.pos - self.entry.offset
}
pub fn get_mut(&mut self) -> &mut A {
self.ar.get_mut()
}
pub fn finalize(mut self) -> io::Result<&'a mut PakEntry> {
if let Some(cipher) = &mut self.cipher {
if cipher.pending > 0 {
let zeros = GenericArray::<u8, Aes256BlockSize>::default();
let n = cipher.buf.len() - cipher.pending;
cipher.buf[cipher.pending..].copy_from_slice(&zeros[..n]);
cipher.cipher.encrypt_blocks(slice::from_mut(&mut cipher.buf));
self.ar.write_all(&cipher.buf)?;
self.builder.pos += Aes256BlockSize::U64;
self.entry.uncompressed_size += Aes256BlockSize::U64;
cipher.pending = 0;
}
}
self.flush()?;
let (size, hash) = self.ar.len_sha1();
if self.import {
if self.entry.size != size {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"imported entry size doesn't match written size",
));
}
if self.entry.hash != hash {
return Err(io::Error::new(
io::ErrorKind::InvalidInput,
"imported entry hash doesn't match written hash",
));
}
} else {
self.entry.hash = hash;
self.entry.size = size;
}
let entry = self.builder.index.add(self.name, self.entry);
Ok(entry)
}
}
impl<'a, A: Archive> io::Write for AssetWriter<'a, A> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.write_all(buf)?;
Ok(buf.len())
}
fn write_all(&mut self, mut buf: &[u8]) -> io::Result<()> {
if let Some(cipher) = &mut self.cipher {
while !buf.is_empty() {
let n = (cipher.buf.len() - cipher.pending).min(buf.len());
cipher.buf[cipher.pending..].copy_from_slice(&buf[..n]);
cipher.pending += n;
if cipher.pending == Aes256BlockSize::USIZE {
cipher.cipher.encrypt_blocks(slice::from_mut(&mut cipher.buf));
self.ar.write_all(&cipher.buf)?;
self.builder.pos += Aes256BlockSize::U64;
self.entry.uncompressed_size += Aes256BlockSize::U64;
}
buf = &buf[n..];
}
} else {
self.ar.write_all(buf)?;
let len = buf.len() as u64;
self.builder.pos += len;
self.entry.uncompressed_size += len;
}
Ok(())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
pub struct PakFileBuilder {
pos: u64,
info: PakInfo,
index: PakIndexV1,
key: Option<Aes256Key>,
}
impl PakFileBuilder {
pub fn new(version: PakVersion) -> Self {
Self { pos: 0, info: PakInfo::new(version), index: PakIndexV1::default(), key: None }
}
pub fn encrypted(&mut self, key: &str) -> io::Result<()> {
self.key = Some(aes256_base64_key(key)?);
Ok(())
}
pub fn finalize<A: Archive>(mut self, ar: &mut A) -> io::Result<PakFile> {
let version = self.info.version;
self.info.index_offset = self.pos;
let mut sha1_ar = ArchiveLenSha1::new(&mut *ar);
if self.info.index_is_frozen {
return Err(io::Error::new(
io::ErrorKind::Other,
"frozen index is not supported and is deprecated since UE4.26",
));
} else {
self.index.ser_de(&mut sha1_ar, version)?;
}
let (len, hash) = sha1_ar.len_sha1();
self.info.index_size = len;
self.info.index_hash = hash;
self.info.ser_de(ar)?;
let pak = PakFile {
info: self.info,
index: if version >= PakVersion::PathHashIndex {
let mut v2 = PakIndexV2::default();
for (name, entry) in self.index.take_entries() {
v2.add(name, entry, version)?;
}
PakIndex::V2(v2)
} else {
PakIndex::V1(self.index)
},
key: self.key,
};
Ok(pak)
}
pub fn pad<A: Archive>(&mut self, ar: A, alignment: u64) -> io::Result<()> {
let pos = align_arbitrary(self.pos, alignment);
self.seek(ar, pos)
}
pub fn seek<A: Archive>(&mut self, mut ar: A, pos: u64) -> io::Result<()> {
while self.pos < pos {
const ZEROS: &[u8; 4096] = &[0u8; 4096];
let size = (pos - self.pos).min(4096);
ar.write_all(&ZEROS[0..size as usize])?;
self.pos += size;
}
if self.pos != pos {
Err(io::Error::new(
io::ErrorKind::InvalidInput,
format!(
"requested seek position {} is invalid, current position is {}",
pos, self.pos
),
))
} else {
Ok(())
}
}
fn cipher(&self) -> Option<Cipher> {
self.key.as_ref().map(|key| Cipher::new(aes256_ecb_cipher(key)))
}
pub fn import<A: Archive>(
&mut self,
ar: A,
name: String,
mut entry: PakEntry,
) -> AssetWriter<'_, A> {
entry.offset = self.pos;
let ar = ArchiveLenSha1::new(ar);
let cipher = self.cipher();
AssetWriter { builder: self, ar, name, entry, import: true, cipher }
}
pub fn add<A: Archive>(&mut self, ar: A, name: String) -> AssetWriter<'_, A> {
let entry = PakEntry { offset: self.pos, ..PakEntry::default() };
let ar = ArchiveLenSha1::new(ar);
let cipher = self.cipher();
AssetWriter { builder: self, ar, name, entry, import: false, cipher }
}
pub fn deleted(&mut self, name: &str) -> io::Result<&mut PakEntry> {
let entry = PakEntry { offset: self.pos, flags: FLAG_DELETED, ..PakEntry::default() };
let entry = self.index.add(name.to_string(), entry);
Ok(entry)
}
}