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
//! This module bundles all the data structures, methods, type definitions  to provide the extraction functionality of the archives.

use std::collections::BTreeMap;
use std::error::Error;
use std::fs;
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::io::BufReader;
use std::io::SeekFrom;
use std::path::Path;

use encoding::all::ISO_8859_1; // latin1 code scheme
use encoding::{EncoderTrap, Encoding};
use flate2::bufread::ZlibDecoder;
use serde::Deserialize;

pub type IntLen = u64;
pub type RpaIdxColl = BTreeMap<String, Vec<RpaEntry>>;

/// The supported RPA version from which asssets can be extracted
#[derive(Debug, PartialEq, Clone, Copy)]
pub enum RpaVersion {
    V3,
    V3_2,
    V2,
}

/// Represents the Renpy archive format
#[derive(Debug)]
pub struct RenpyArchive<'a> {
    /// The file descriptor wrapped in a buffered reader
    reader: BufReader<File>,
    /// The immutable path to the file on the filesystem
    path: &'a Path,
    /// Version of the RPA format to use
    version: RpaVersion,
    /// Files present in the archive
    indices: RpaIdx,
    /// Maximum number of bytes of padding to add between files
    pad_len: u16,
    /// Obfuscation key to for en-/decoding
    key: ObfuscationKey,
}

/// Represents the indices (files) present in the archive
#[derive(Debug, Deserialize, Clone)]
pub struct RpaIdx(RpaIdxColl);

/// Represents the metadata of one asset present in `RpaIdx`
#[derive(Debug, Deserialize, Clone, PartialEq)]
#[serde(untagged)] // tells serde to match the data against each variant in order and use the one that successfully deserializes first
pub enum RpaEntry {
    V2(RpaEntryv2),
    V3(RpaEntryv3),
}

#[derive(Debug, Deserialize, PartialEq, Clone)]
pub struct RpaEntryv3 {
    offset: IntLen,
    len: IntLen,
    prefix: String,
}

#[derive(Debug, Deserialize, PartialEq, Clone)]
pub struct RpaEntryv2 {
    offset: IntLen,
    len: IntLen,
}

/// The obfuscation key used for en-/decoding
#[derive(Debug, Clone, Copy)]
pub struct ObfuscationKey(IntLen);

impl<'a> RenpyArchive<'a> {
    /// Construct a `RenpyArchive` from a file on the filesystem
    ///
    /// This performs I/O operations as it reads the magic literal and the rest of the file to decode the indices present in the archive.
    ///
    /// # Errors
    ///
    /// An error occurs if either an invalid `path` has been provided to the OS, I/O errors occurs during reading the file, the zlib decoding fails, or the serde deserialization process fails.
    ///
    /// # Panics
    ///
    /// This function panics if it either encounters an unsupported `RpaVersion`, i.e. a variant not covered in the enum declaration, or the integer parsing while constructing the obfuscation key fails.
    pub fn from_file(path: &'a Path) -> Result<Self, Box<dyn Error>> {
        let file = File::open(path)?;
        let mut reader = BufReader::new(file);

        //  determine RPA version
        let mut first_line = String::new();
        reader.read_line(&mut first_line)?;
        let literal: Vec<_> = first_line.split_ascii_whitespace().collect();
        let rpa_version = Self::get_rpa_version(literal[0]);

        // extract indices and obfuscation key
        let (mut indices, obfuscation_key) = Self::extract_metadata(&rpa_version, &mut reader)?;

        // a deobfuscation with the obfuscation key is only necessary for RpaV3 and RpaV3_2
        if let RpaVersion::V3 | RpaVersion::V3_2 = rpa_version {
            Self::deobfuscate_indices(&mut indices, &obfuscation_key);
        }

        Ok(Self {
            reader,
            path,
            version: rpa_version,
            indices,
            pad_len: 0,
            key: obfuscation_key,
        })
    }

    /// Method to get access to the collection data structure of `RpaIdx`
    pub fn indices_map(&self) -> &RpaIdxColl {
        &self.indices.0
    }

    /// Lists all the files present in one archive
    pub fn list_indices(&self) -> Vec<String> {
        self.indices.0.keys().cloned().collect()
    }

    /// Determines the RPA version from the magic literal present in the first line of the archive
    fn get_rpa_version<S: AsRef<str>>(magic_literal: S) -> RpaVersion {
        match magic_literal.as_ref() {
            "RPA-3.2" => RpaVersion::V3_2,
            "RPA-3.0" => RpaVersion::V3,
            "RPA-2.0" => RpaVersion::V2,
            unsupported => panic!("Unsupported RPA version '{}'", unsupported),
        }
    }

    fn extract_metadata(
        rpa_version: &RpaVersion,
        reader: &mut BufReader<File>,
    ) -> Result<(RpaIdx, ObfuscationKey), Box<dyn Error>> {
        let mut contents = String::new();

        reader.seek(SeekFrom::Start(0))?;

        reader.read_line(&mut contents)?;
        let metadata: Vec<&str> = contents.split_ascii_whitespace().collect();

        let offset = IntLen::from_str_radix(metadata[1], 16)?;
        // obfuscation key is 0 when RpaV2 is used
        let key = Self::construct_obfuscation_key(rpa_version, &metadata);

        // next step: extract indices
        // seek cursor to the decoded offset
        reader.seek(SeekFrom::Start(offset))?;

        let mut bytes: Vec<u8> = Vec::new();
        // read everything util EOF
        let bytes_read = reader.read_to_end(&mut bytes)?;
        let mut decoded_bytes: Vec<u8> = Vec::with_capacity(2 * bytes_read);

        // read the content by decoding it with zlib
        ZlibDecoder::new(&bytes[..]).read_to_end(&mut decoded_bytes)?;

        let deserialized_indices: RpaIdx = serde_pickle::from_slice(&decoded_bytes)?;
        Ok((deserialized_indices, ObfuscationKey(key)))
    }

    fn construct_obfuscation_key<S: AsRef<str>>(
        rpa_version: &RpaVersion,
        metadata: &[S],
    ) -> IntLen {
        let key: IntLen = match *rpa_version {
            RpaVersion::V3 => metadata.as_ref()[2..]
                .iter()
                .fold(0, |acc: IntLen, sub_key| {
                    acc ^ IntLen::from_str_radix(sub_key.as_ref(), 16).unwrap()
                }),
            RpaVersion::V3_2 => metadata.as_ref()[3..]
                .iter()
                .fold(0, |acc: IntLen, sub_key| {
                    acc ^ IntLen::from_str_radix(sub_key.as_ref(), 16).unwrap()
                }),
            RpaVersion::V2 => 0,
        };

        key
    }

    /// Reads the byte buffer of the specified file in the archive into memory
    pub fn read_file_from_archive<S: AsRef<str>>(
        &mut self,
        filename: S,
    ) -> Result<Vec<u8>, Box<dyn Error>> {
        let rpa_idx = match self.indices.0.get(filename.as_ref()) {
            Some(idx) => Ok(idx),
            None => Err(format!("No entry for key '{}'", filename.as_ref())),
        }?;

        let rpa_idx = &rpa_idx[0];

        let (offset, len, prefix) = match rpa_idx {
            RpaEntry::V3(rpa_v3) => (rpa_v3.offset, rpa_v3.len, Some(rpa_v3.prefix.as_str())),
            RpaEntry::V2(rpa_v2) => (rpa_v2.offset, rpa_v2.len, None),
        };

        println!(
            "Reading file '{}' from archive '{}' (offset: {}, len: {} bytes).",
            filename.as_ref(),
            self.path.display(),
            offset,
            len
        );

        self.reader.seek(SeekFrom::Start(offset))?;

        let mut encoded_prefix = ISO_8859_1.encode(prefix.unwrap_or(""), EncoderTrap::Strict)?;

        let desired_capacity = len as usize - prefix.unwrap_or("").len();

        let mut buf = vec![0u8; desired_capacity];

        // now read exactly `desired_capacity` bytes
        self.reader.read_exact(&mut buf)?;
        assert_eq!(desired_capacity, buf.len());
        println!("Successfully read {} bytes.", buf.len());

        // append the byte vector at the prefix vector if it's not empty
        if self.version == RpaVersion::V3 && !encoded_prefix.is_empty() {
            encoded_prefix.append(&mut buf);
            Ok(encoded_prefix)
        } else {
            Ok(buf)
        }
    }

    /// Writes the byte buffer of one file to disk
    pub fn write_file<S: AsRef<str>>(&self, filepath: S, file_buf: &[u8]) -> io::Result<()> {
        let path = Path::new(filepath.as_ref());

        // use `parent` method of the path and create those directories before wanting to extract the file
        if let Some(parent_dirs) = path.parent() {
            if !parent_dirs.exists() {
                println!(
                    "Creating path '{}' before extracting.",
                    parent_dirs.display()
                );
                fs::create_dir_all(parent_dirs)?;
            }
        }

        print!("Writing file '{}' to disk...", filepath.as_ref());

        let mut file = File::create(path)?;
        // buffered writing for an entire file at once offers no benefits, so normal writing is sufficient
        file.write_all(&file_buf)?;
        println!("Done.");

        Ok(())
    }

    /// Deobfuscates the indices with the obfuscation key
    fn deobfuscate_indices(rpa_idx: &mut RpaIdx, key: &ObfuscationKey) {
        let key = key.0;

        let deobfuscate = |rpa_entry: &mut RpaEntry| match rpa_entry {
            RpaEntry::V2(rpa_entry) => {
                rpa_entry.offset ^= key;
                rpa_entry.len ^= key;
            }
            RpaEntry::V3(rpa_entry) => {
                rpa_entry.offset ^= key;
                rpa_entry.len ^= key;
            }
        };

        for rpa_list in rpa_idx.0.values_mut() {
            for rpa_entry in rpa_list.iter_mut() {
                deobfuscate(rpa_entry);
            }
        }
    }
}

impl RpaEntry {
    /// Construct a new `RpaEntry` from a prefix
    pub fn with_prefix<S: AsRef<str>>(offset: IntLen, len: IntLen, prefix: S) -> Self {
        RpaEntry::V3(RpaEntryv3 {
            offset,
            len,
            prefix: String::from(prefix.as_ref()),
        })
    }
}