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
use std::{
    borrow::Cow,
    collections::hash_map::Entry,
    fs::File,
    io::{Read, Seek, SeekFrom},
    sync::{
        atomic::{AtomicUsize, Ordering},
        Arc,
    },
};

use anyhow::Context;
use binrw::{BinRead, BinReaderExt, NullString};
use parking_lot::RwLock;
use rustc_hash::FxHashMap;

use crate::{
    crypto::PkgGcmState,
    oodle,
    package::{ReadSeek, UEntryHeader, BLOCK_CACHE_SIZE},
    PackageVersion, TagHash,
};

#[derive(BinRead, Debug, Clone)]
pub struct EntryHeader {
    pub reference: u32,

    _type_info: u32,

    #[br(calc = (_type_info >> 9) as u8 & 0x7f)]
    pub file_type: u8,
    #[br(calc = (_type_info >> 6) as u8 & 0x7)]
    pub file_subtype: u8,

    _block_info: u64,

    #[br(calc = _block_info as u32 & 0x3fff)]
    pub starting_block: u32,

    #[br(calc = ((_block_info >> 14) as u32 & 0x3FFF) << 4)]
    pub starting_block_offset: u32,

    #[br(calc = (_block_info >> 28) as u32)]
    pub file_size: u32,
}

#[derive(BinRead, Debug, Clone)]
pub struct BlockHeader {
    pub offset: u32,
    pub size: u32,
    pub patch_id: u16,
    pub flags: u16,
    pub hash: [u8; 20],
    pub gcm_tag: [u8; 16],
}

#[derive(BinRead, Debug, Clone)]
pub struct HashTableEntry {
    pub hash64: u64,
    pub hash32: TagHash,
    pub reference: TagHash,
}

pub const BLOCK_SIZE: usize = 0x40000;

pub struct PackageCommonD2 {
    pub(crate) version: PackageVersion,
    pub(crate) pkg_id: u16,
    pub(crate) patch_id: u16,

    pub(crate) gcm: RwLock<PkgGcmState>,
    pub(crate) _entries: Vec<EntryHeader>,
    pub(crate) entries_unified: Arc<[UEntryHeader]>,
    pub(crate) blocks: Vec<BlockHeader>,
    pub(crate) hashes: Vec<HashTableEntry>,

    pub(crate) reader: RwLock<Box<dyn ReadSeek>>,
    pub(crate) path_base: String,

    /// Used for purging old blocks
    pub(crate) block_counter: AtomicUsize,
    pub(crate) block_cache: RwLock<FxHashMap<usize, (usize, Arc<Vec<u8>>)>>,
    pub(crate) file_handles: RwLock<FxHashMap<usize, File>>,
}

impl PackageCommonD2 {
    pub fn new<R: ReadSeek + 'static>(
        reader: R,
        version: PackageVersion,
        pkg_id: u16,
        patch_id: u16,
        entries: Vec<EntryHeader>,
        blocks: Vec<BlockHeader>,
        hashes: Vec<HashTableEntry>,
        path: String,
    ) -> anyhow::Result<PackageCommonD2> {
        let last_underscore_pos = path.rfind('_').unwrap();
        let path_base = path[..last_underscore_pos].to_owned();

        let entries_unified: Vec<UEntryHeader> = entries
            .iter()
            .map(|e| UEntryHeader {
                reference: e.reference,
                file_type: e.file_type,
                file_subtype: e.file_subtype,
                starting_block: e.starting_block,
                starting_block_offset: e.starting_block_offset,
                file_size: e.file_size,
            })
            .collect();

        Ok(PackageCommonD2 {
            version,
            pkg_id,
            patch_id,
            gcm: RwLock::new(PkgGcmState::new(pkg_id, version)),
            _entries: entries,
            entries_unified: entries_unified.into(),
            blocks,
            hashes,
            reader: RwLock::new(Box::new(reader)),
            path_base,
            block_counter: AtomicUsize::default(),
            block_cache: Default::default(),
            file_handles: Default::default(),
        })
    }

    fn get_block_raw(&self, block_index: usize) -> anyhow::Result<Cow<[u8]>> {
        let _span = tracing::debug_span!("PackageCommonD2::get_block_raw", block_index).entered();

        let bh = &self.blocks[block_index];
        let mut data = vec![0u8; bh.size as usize];

        if self.patch_id == bh.patch_id {
            self.reader
                .write()
                .seek(SeekFrom::Start(bh.offset as u64))?;
            self.reader.write().read_exact(&mut data)?;
        } else {
            match self.file_handles.write().entry(bh.patch_id as _) {
                Entry::Occupied(mut f) => {
                    let f = f.get_mut();
                    f.seek(SeekFrom::Start(bh.offset as u64))?;
                    f.read_exact(&mut data)?;
                }
                Entry::Vacant(e) => {
                    let f = File::open(format!("{}_{}.pkg", self.path_base, bh.patch_id))
                        .with_context(|| {
                            format!(
                                "Failed to open package file {}_{}.pkg",
                                self.path_base, bh.patch_id
                            )
                        })?;

                    let f = e.insert(f);
                    f.seek(SeekFrom::Start(bh.offset as u64))?;
                    f.read_exact(&mut data)?;
                }
            };
        };

        Ok(Cow::Owned(data))
    }

    /// Reads, decrypts and decompresses the specified block
    fn read_block(&self, block_index: usize) -> anyhow::Result<Vec<u8>> {
        let _span = tracing::debug_span!("PackageCommonD2::read_block", block_index).entered();

        let bh = self.blocks[block_index].clone();

        let mut block_data = self.get_block_raw(block_index)?.to_vec();

        if (bh.flags & 0x2) != 0 {
            let _espan =
                tracing::debug_span!("PackageCommonD2::get_block_raw decrypt", block_index)
                    .entered();
            self.gcm
                .write()
                .decrypt_block_in_place(bh.flags, &bh.gcm_tag, &mut block_data)?;
        };

        let decompressed_data = if (bh.flags & 0x1) != 0 {
            let _dspan =
                tracing::debug_span!("PackageCommonD2::get_block_raw decompress", block_index)
                    .entered();

            let mut buffer = vec![0u8; BLOCK_SIZE];
            let _decompressed_size = match self.version {
                // Destiny 1
                PackageVersion::DestinyInternalAlpha
                | PackageVersion::DestinyTheTakenKing
                | PackageVersion::DestinyRiseOfIron => oodle::decompress_3,

                // Destiny 2 (Red War - Beyond Light)
                PackageVersion::Destiny2Beta | PackageVersion::Destiny2Shadowkeep => {
                    oodle::decompress_3
                }

                // Destiny 2 (Beyond Light - Latest)
                PackageVersion::Destiny2BeyondLight
                | PackageVersion::Destiny2WitchQueen
                | PackageVersion::Destiny2Lightfall
                | PackageVersion::Destiny2TheFinalShape => oodle::decompress_9,
            }(&block_data, &mut buffer)?;

            buffer
        } else {
            block_data
        };

        Ok(decompressed_data)
    }

    pub fn get_block(&self, block_index: usize) -> anyhow::Result<Arc<Vec<u8>>> {
        let _span = tracing::debug_span!("PackageCommonD2::get_block", block_index).entered();
        let (_, b) = match self.block_cache.write().entry(block_index) {
            Entry::Occupied(o) => o.get().clone(),
            Entry::Vacant(v) => {
                let block = self.read_block(*v.key())?;
                let b = v
                    .insert((self.block_counter.load(Ordering::Relaxed), Arc::new(block)))
                    .clone();

                self.block_counter.store(
                    self.block_counter.load(Ordering::Relaxed) + 1,
                    Ordering::Relaxed,
                );

                b
            }
        };

        while self.block_cache.read().len() > BLOCK_CACHE_SIZE {
            let bc = self.block_cache.read();
            let (oldest, _) = bc
                .iter()
                .min_by(|(_, (at, _)), (_, (bt, _))| at.cmp(bt))
                .unwrap();

            let oldest = *oldest;
            drop(bc);

            self.block_cache.write().remove(&oldest);
        }

        Ok(b)
    }
}

#[derive(Debug, Clone)]
pub struct PackageNamedTagEntry {
    pub hash: TagHash,
    pub class_hash: u32,
    pub name: String,
}

impl BinRead for PackageNamedTagEntry {
    type Args<'a> = ();

    fn read_options<R: Read + Seek>(
        reader: &mut R,
        endian: binrw::Endian,
        _args: Self::Args<'_>,
    ) -> binrw::BinResult<Self> {
        let hash = reader.read_type(endian)?;
        let class_hash = reader.read_type(endian)?;

        let name_offset: u64 = reader.read_type(endian)?;
        let pos_save = reader.stream_position()?;

        reader.seek(SeekFrom::Start(pos_save - 8 + name_offset))?;
        let name_cstring: NullString = reader.read_type(endian)?;
        reader.seek(SeekFrom::Start(pos_save))?;

        Ok(Self {
            hash,
            class_hash,
            name: name_cstring.to_string(),
        })
    }
}