pf8 0.1.6

A Rust library for encoding and decoding artemis PF8 archive files
Documentation
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! High-level reader for PF6/PF8 archives.

use crate::callbacks::{ArchiveHandler, ControlAction, NoOpHandler, OperationType, ProgressInfo};
use crate::constants::BUFFER_SIZE;
use crate::crypto;
use crate::entry::Pf8Entry;
use crate::error::{Error, Result};
use crate::format::{self, ArchiveFormat};
use std::collections::HashMap;
use std::fs::File;
use std::io::{Read, Seek, SeekFrom};
use std::path::Path;

/// Optimized reader for PF6/PF8 archives with minimal memory usage
///
/// This reader minimizes memory usage by:
/// - Not memory-mapping the entire file
/// - Reading file data on-demand from disk
/// - Supporting streaming operations with configurable buffers
pub struct Pf8Reader {
    /// File handle for reading archive data
    file: File,
    /// List of file entries
    entries: Vec<Pf8Entry>,
    /// Lookup map for fast entry access by path
    entry_map: HashMap<String, usize>,
    /// Encryption key for the archive (None for PF6)
    encryption_key: Option<Vec<u8>>,
    /// Archive format
    format: ArchiveFormat,
}

impl Pf8Reader {
    /// Opens a PF6/PF8 archive for reading with minimal memory usage
    pub fn open<P: AsRef<Path>>(path: P) -> Result<Self> {
        let mut file = File::open(path)?;

        // Read only the header and index data into memory
        let header_size = 11; // minimum header size
        let mut header_buffer = vec![0u8; header_size];
        file.read_exact(&mut header_buffer)?;

        let _format = format::validate_magic(&header_buffer)?;
        let index_size = format::read_u32_le(&header_buffer, format::offsets::INDEX_SIZE)?;

        // Read the entire index into memory
        let total_index_size = format::offsets::INDEX_DATA_START + index_size as usize;
        let mut index_buffer = vec![0u8; total_index_size];
        file.seek(SeekFrom::Start(0))?;
        file.read_exact(&mut index_buffer)?;

        let (raw_entries, format) = format::parse_entries(&index_buffer)?;

        // Generate encryption key only for PF8 format
        let encryption_key = match format {
            ArchiveFormat::Pf8 => Some(crypto::generate_key(&index_buffer, index_size)),
            ArchiveFormat::Pf6 => None,
        };

        let mut entries = Vec::with_capacity(raw_entries.len());
        let mut entry_map = HashMap::new();

        for (index, raw_entry) in raw_entries.into_iter().enumerate() {
            let entry = Pf8Entry::from_raw_with_format(raw_entry, format);
            let path_string = entry.path().to_string_lossy().to_string();
            entry_map.insert(path_string, index);
            entries.push(entry);
        }

        Ok(Self {
            file,
            entries,
            entry_map,
            encryption_key,
            format,
        })
    }

    /// Returns an iterator over all file entries
    pub fn entries(&self) -> impl Iterator<Item = &Pf8Entry> {
        self.entries.iter()
    }

    /// Gets the number of files in the archive
    pub fn len(&self) -> usize {
        self.entries.len()
    }

    /// Returns true if the archive is empty
    pub fn is_empty(&self) -> bool {
        self.entries.is_empty()
    }

    /// Gets the archive format (PF6 or PF8)
    pub fn format(&self) -> ArchiveFormat {
        self.format
    }

    /// Returns true if the archive uses encryption (PF8 only)
    pub fn is_encrypted(&self) -> bool {
        self.encryption_key.is_some()
    }

    /// Gets a file entry by path
    pub fn get_entry<P: AsRef<Path>>(&self, path: P) -> Option<&Pf8Entry> {
        let path_string = path.as_ref().to_string_lossy().to_string();
        self.entry_map
            .get(&path_string)
            .map(|&index| &self.entries[index])
    }

    /// Checks if a file exists in the archive
    pub fn contains<P: AsRef<Path>>(&self, path: P) -> bool {
        self.get_entry(path).is_some()
    }

    /// Reads a file's data by path
    pub fn read_file<P: AsRef<Path>>(&mut self, path: P) -> Result<Vec<u8>> {
        let mut result = Vec::new();
        self.read_file_streaming(path, |chunk| {
            result.extend_from_slice(chunk);
            Ok(())
        })?;
        Ok(result)
    }

    /// Reads a file's data with streaming to minimize memory allocation
    pub fn read_file_streaming<P: AsRef<Path>, F>(&mut self, path: P, mut callback: F) -> Result<()>
    where
        F: FnMut(&[u8]) -> Result<()>,
    {
        // Get entry info and copy values to avoid borrow conflicts
        let (file_size, start_offset, is_encrypted) = {
            let entry = self
                .get_entry(path)
                .ok_or_else(|| Error::FileNotFound("File not found".to_string()))?;
            (
                entry.size() as usize,
                entry.offset() as u64,
                entry.is_encrypted(),
            )
        };

        self.file.seek(SeekFrom::Start(start_offset))?;

        if file_size <= BUFFER_SIZE {
            // Small file: read directly
            let mut data = vec![0u8; file_size];
            self.file.read_exact(&mut data)?;

            if is_encrypted {
                if let Some(key) = self.encryption_key.as_deref() {
                    for (i, byte) in data.iter_mut().enumerate() {
                        *byte ^= key[i % key.len()];
                    }
                } else {
                    return Err(Error::Crypto(
                        "File is encrypted but no key provided".to_string(),
                    ));
                }
            }

            callback(&data)?;
        } else {
            // Large file: stream in chunks
            let mut buffer = vec![0u8; BUFFER_SIZE];
            let mut bytes_read = 0;

            while bytes_read < file_size {
                let chunk_size = (file_size - bytes_read).min(BUFFER_SIZE);
                self.file.read_exact(&mut buffer[..chunk_size])?;

                if is_encrypted {
                    if let Some(key) = self.encryption_key.as_deref() {
                        // Decrypt chunk in-place
                        for (i, byte) in buffer[..chunk_size].iter_mut().enumerate() {
                            *byte ^= key[(bytes_read + i) % key.len()];
                        }
                    } else {
                        return Err(Error::Crypto(
                            "File is encrypted but no key provided".to_string(),
                        ));
                    }
                }

                callback(&buffer[..chunk_size])?;
                bytes_read += chunk_size;
            }
        }

        Ok(())
    }

    /// Extracts all files to the specified directory with memory optimization
    pub fn extract_all<P: AsRef<Path>>(&mut self, output_dir: P) -> Result<()> {
        let mut handler = NoOpHandler;
        self.extract_all_with_progress(output_dir, &mut handler)
    }

    /// Extracts all files with progress reporting and cancellation support
    pub fn extract_all_with_progress<P: AsRef<Path>, H: ArchiveHandler>(
        &mut self,
        output_dir: P,
        handler: &mut H,
    ) -> Result<()> {
        let output_dir = output_dir.as_ref();
        let mut buffer = vec![0u8; BUFFER_SIZE];

        // Calculate total bytes
        let total_bytes: u64 = self.entries.iter().map(|e| e.size() as u64).sum();
        let total_files = self.entries.len();
        let mut total_bytes_processed = 0u64;

        // Notify task started
        if handler.on_started(OperationType::Unpack) == ControlAction::Abort {
            return Err(Error::Cancelled);
        }

        for (index, entry) in self.entries.clone().iter().enumerate() {
            let file_path = output_dir.join(entry.path());
            let entry_name = entry.path().to_string_lossy().to_string();

            // Notify entry started
            if handler.on_entry_started(&entry_name) == ControlAction::Abort {
                return Err(Error::Cancelled);
            }

            // Create parent directories if they don't exist
            if let Some(parent) = file_path.parent() {
                std::fs::create_dir_all(parent)?;
            }

            // Extract with progress
            let bytes_written = self.extract_entry_with_progress(
                entry,
                &file_path,
                &mut buffer,
                index + 1,
                total_files,
                total_bytes_processed,
                total_bytes,
                handler,
            )?;

            total_bytes_processed += bytes_written;

            // Notify entry finished
            if handler.on_entry_finished(&entry_name) == ControlAction::Abort {
                return Err(Error::Cancelled);
            }
        }

        // Notify task finished
        handler.on_finished();

        Ok(())
    }

    /// Extracts a single file with progress reporting
    pub fn extract_file_with_progress<P: AsRef<Path>, Q: AsRef<Path>, H: ArchiveHandler>(
        &mut self,
        archive_path: P,
        output_path: Q,
        handler: &mut H,
    ) -> Result<()> {
        // Create parent directories if they don't exist
        if let Some(parent) = output_path.as_ref().parent() {
            std::fs::create_dir_all(parent)?;
        }

        // Get entry info
        let entry = self
            .get_entry(&archive_path)
            .ok_or_else(|| Error::FileNotFound("File not found".to_string()))?
            .clone();

        let mut buffer = vec![0u8; BUFFER_SIZE];
        let total_bytes = entry.size() as u64;
        let entry_name = entry.path().to_string_lossy().to_string();

        // Notify task started
        if handler.on_started(OperationType::Unpack) == ControlAction::Abort {
            return Err(Error::Cancelled);
        }

        // Notify entry started
        if handler.on_entry_started(&entry_name) == ControlAction::Abort {
            return Err(Error::Cancelled);
        }

        // Extract with progress
        self.extract_entry_with_progress(
            &entry,
            output_path,
            &mut buffer,
            1,
            1,
            0,
            total_bytes,
            handler,
        )?;

        // Notify entry finished
        if handler.on_entry_finished(&entry_name) == ControlAction::Abort {
            return Err(Error::Cancelled);
        }

        // Notify task finished
        handler.on_finished();

        Ok(())
    }

    /// Extracts a single entry using streaming with progress reporting
    #[allow(clippy::too_many_arguments)]
    fn extract_entry_with_progress<P: AsRef<Path>, H: ArchiveHandler>(
        &mut self,
        entry: &Pf8Entry,
        output_path: P,
        buffer: &mut [u8],
        processed_files: usize,
        total_files: usize,
        total_bytes_processed: u64,
        total_bytes: u64,
        handler: &mut H,
    ) -> Result<u64> {
        use std::io::Write;

        let mut output_file = File::create(output_path)?;

        // Copy entry info to avoid borrow conflicts
        let (file_size, start_offset, is_encrypted) = {
            (
                entry.size() as usize,
                entry.offset() as u64,
                entry.is_encrypted(),
            )
        };

        self.file.seek(SeekFrom::Start(start_offset))?;

        let mut current_file_bytes = 0u64;

        if file_size <= buffer.len() {
            // Small file: read directly into buffer
            let mut temp_buffer = vec![0u8; file_size];
            self.file.read_exact(&mut temp_buffer)?;

            if is_encrypted {
                if let Some(key) = self.encryption_key.as_deref() {
                    for (i, byte) in temp_buffer.iter_mut().enumerate() {
                        *byte ^= key[i % key.len()];
                    }
                } else {
                    return Err(Error::Crypto(
                        "File is encrypted but no key provided".to_string(),
                    ));
                }
            }

            output_file.write_all(&temp_buffer)?;
            current_file_bytes = file_size as u64;

            // Report progress
            let progress = ProgressInfo {
                processed_bytes: total_bytes_processed + current_file_bytes,
                total_bytes: Some(total_bytes),
                processed_files,
                total_files: Some(total_files),
                current_file: entry.path().to_string_lossy().to_string(),
            };
            if handler.on_progress(&progress) == ControlAction::Abort {
                return Err(Error::Cancelled);
            }
        } else {
            // Large file: stream in chunks
            let buffer_size = buffer.len();
            let mut bytes_written = 0;

            while bytes_written < file_size {
                let chunk_size = (file_size - bytes_written).min(buffer_size);
                self.file.read_exact(&mut buffer[..chunk_size])?;

                if is_encrypted {
                    if let Some(key) = self.encryption_key.as_deref() {
                        for (i, byte) in buffer[..chunk_size].iter_mut().enumerate() {
                            *byte ^= key[(bytes_written + i) % key.len()];
                        }
                    } else {
                        return Err(Error::Crypto(
                            "File is encrypted but no key provided".to_string(),
                        ));
                    }
                }

                output_file.write_all(&buffer[..chunk_size])?;
                bytes_written += chunk_size;
                current_file_bytes += chunk_size as u64;

                // Report progress
                let progress = ProgressInfo {
                    processed_bytes: total_bytes_processed + current_file_bytes,
                    total_bytes: Some(total_bytes),
                    processed_files,
                    total_files: Some(total_files),
                    current_file: entry.path().to_string_lossy().to_string(),
                };
                if handler.on_progress(&progress) == ControlAction::Abort {
                    return Err(Error::Cancelled);
                }
            }
        }

        Ok(current_file_bytes)
    }
}