annotate formats/src/th06/pbg3.rs @ 761:f506ad5c9b17

formats: Replace unwrap() with proper io::Error.
author Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
date Wed, 06 Jan 2021 01:47:06 +0100
parents eba9a3d0c484
children ccb04468c5fa
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
637
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
1 //! PBG3 archive files handling.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
2 //!
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
3 //! This module provides classes for handling the PBG3 file format.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
4 //! The PBG3 format is the archive format used by Touhou 6: EoSD.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
5 //!
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
6 //! PBG3 files are merely a bitstream composed of a header, a file
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
7 //! table, and LZSS-compressed files.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
8
757
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
9 use touhou_utils::bitstream::BitStream;
21b186be2590 Split the Rust version into multiple crates.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 746
diff changeset
10 use touhou_utils::lzss;
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 645
diff changeset
11 use std::fs::File;
637
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
12 use std::io;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
13 use std::collections::hash_map::{self, HashMap};
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 645
diff changeset
14 use std::path::Path;
637
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
15
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
16 /// Helper struct to handle strings and integers in PBG3 bitstreams.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
17 pub struct PBG3BitStream<R: io::Read + io::Seek> {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
18 bitstream: BitStream<R>,
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
19 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
20
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
21 impl<R: io::Read + io::Seek> PBG3BitStream<R> {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
22 /// Create a bitstream capable of reading u32 and strings.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
23 pub fn new(bitstream: BitStream<R>) -> PBG3BitStream<R> {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
24 PBG3BitStream {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
25 bitstream,
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
26 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
27 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
28
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
29 /// Seek inside the bitstream, ditching any unused data read.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
30 pub fn seek(&mut self, seek_from: io::SeekFrom) -> io::Result<u64> {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
31 self.bitstream.seek(seek_from)
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
32 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
33
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
34 /// Return the current position in the stream.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
35 pub fn tell(&mut self) -> io::Result<u64> {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
36 self.bitstream.seek(io::SeekFrom::Current(0))
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
37 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
38
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
39 /// Read a given amount of bits.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
40 pub fn read(&mut self, nb_bits: usize) -> io::Result<usize> {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
41 self.bitstream.read(nb_bits)
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
42 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
43
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
44 /// Read a given amount of bytes.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
45 pub fn read_bytes(&mut self, nb_bytes: usize) -> io::Result<Vec<u8>> {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
46 self.bitstream.read_bytes(nb_bytes)
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
47 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
48
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
49 /// Read an integer from the bitstream.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
50 ///
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
51 /// Integers have variable sizes. They begin with a two-bit value indicating
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
52 /// the number of (non-aligned) bytes to read.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
53 pub fn read_u32(&mut self) -> io::Result<u32> {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
54 let size = self.read(2)?;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
55 Ok(self.read((size + 1) * 8)? as u32)
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
56 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
57
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
58 /// Read a string from the bitstream.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
59 ///
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
60 /// Strings are stored as NULL-terminated sequences of bytes.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
61 /// The only catch is that they are not byte-aligned.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
62 pub fn read_string(&mut self, mut max_size: usize) -> io::Result<Vec<u8>> {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
63 let mut buf = Vec::new();
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
64 while max_size > 0 {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
65 let byte = self.read(8)? as u8;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
66 if byte == 0 {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
67 break;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
68 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
69 buf.push(byte);
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
70 max_size -= 1;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
71 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
72 Ok(buf)
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
73 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
74 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
75
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
76 type Entry = (u32, u32, u32, u32, u32);
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
77
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
78 /// Handle PBG3 archive files.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
79 ///
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
80 /// PBG3 is a file archive format used in Touhou 6: EoSD.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
81 /// This class provides a representation of such files, as well as functions to
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
82 /// read and extract files from a PBG3 archive.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
83 pub struct PBG3<R: io::Read + io::Seek> {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
84 /// List of PBG3Entry objects describing files present in the archive.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
85 entries: HashMap<String, Entry>,
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
86
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
87 /// PBG3BitStream struct.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
88 bitstream: PBG3BitStream<R>,
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
89 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
90
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
91 impl<R: io::Read + io::Seek> PBG3<R> {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
92 /// Create a PBG3 archive.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
93 fn new(entries: HashMap<String, Entry>, bitstream: PBG3BitStream<R>) -> PBG3<R> {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
94 PBG3 {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
95 entries,
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
96 bitstream,
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
97 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
98 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
99
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
100 /// Open a PBG3 archive.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
101 pub fn from_file(mut file: R) -> io::Result<PBG3<R>> {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
102 let mut magic = [0; 4];
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
103 file.read(&mut magic)?;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
104 if &magic != b"PBG3" {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
105 return Err(io::Error::new(io::ErrorKind::Other, "Wrong magic!"));
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
106 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
107
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
108 let bitstream = BitStream::new(file);
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
109 let mut bitstream = PBG3BitStream::new(bitstream);
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
110 let mut entries = HashMap::new();
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
111
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
112 let nb_entries = bitstream.read_u32()?;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
113 let offset = bitstream.read_u32()?;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
114 bitstream.seek(io::SeekFrom::Start(offset as u64))?;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
115
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
116 for _ in 0..nb_entries {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
117 let unknown_1 = bitstream.read_u32()?;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
118 let unknown_2 = bitstream.read_u32()?;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
119 let checksum = bitstream.read_u32()?; // Checksum of *compressed data*
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
120 let offset = bitstream.read_u32()?;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
121 let size = bitstream.read_u32()?;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
122 let name = bitstream.read_string(255)?;
761
f506ad5c9b17 formats: Replace unwrap() with proper io::Error.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 760
diff changeset
123 let name = String::from_utf8(name).map_err(|err| io::Error::new(io::ErrorKind::InvalidData, err))?;
637
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
124 entries.insert(name, (unknown_1, unknown_2, checksum, offset, size));
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
125 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
126
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
127 Ok(PBG3::new(entries, bitstream))
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
128 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
129
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
130 /// List all file entries in this PBG3 archive.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
131 pub fn list_files(&self) -> hash_map::Keys<String, Entry> {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
132 self.entries.keys()
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
133 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
134
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
135 /// Read a single file from this PBG3 archive.
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 645
diff changeset
136 pub fn get_file(&mut self, filename: &str, check: bool) -> io::Result<Vec<u8>> {
761
f506ad5c9b17 formats: Replace unwrap() with proper io::Error.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 760
diff changeset
137 let (_unknown_1, _unknown_2, checksum, offset, size) =
f506ad5c9b17 formats: Replace unwrap() with proper io::Error.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 760
diff changeset
138 self.entries.get(filename)
f506ad5c9b17 formats: Replace unwrap() with proper io::Error.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 760
diff changeset
139 .ok_or_else(|| io::Error::new(io::ErrorKind::NotFound, format!("File not found in PBG3: {}", filename)))?;
637
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
140 self.bitstream.seek(io::SeekFrom::Start(*offset as u64))?;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
141 let data = lzss::decompress(&mut self.bitstream.bitstream, *size as usize, 0x2000, 13, 4, 3)?;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
142 if check {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
143 // Verify the checksum.
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
144 let compressed_size = self.bitstream.tell()? as u32 - *offset;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
145 self.bitstream.seek(io::SeekFrom::Start(*offset as u64))?;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
146 let mut value: u32 = 0;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
147 for c in self.bitstream.read_bytes(compressed_size as usize)? {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
148 value += c as u32;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
149 value &= 0xffffffff;
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
150 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
151 if value != *checksum {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
152 return Err(io::Error::new(io::ErrorKind::Other, "Corrupted data!"));
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
153 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
154 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
155 Ok(data)
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
156 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
157 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
158
746
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 645
diff changeset
159 /// Open a PBG3 archive from its path.
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 645
diff changeset
160 pub fn from_path_buffered<P: AsRef<Path>>(path: P) -> io::Result<PBG3<io::BufReader<File>>> {
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 645
diff changeset
161 let file = File::open(path)?;
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 645
diff changeset
162 let buf_file = io::BufReader::new(file);
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 645
diff changeset
163 PBG3::from_file(buf_file)
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 645
diff changeset
164 }
0ebf6467e4ff examples: Add a menu example.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 645
diff changeset
165
637
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
166 #[cfg(test)]
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
167 mod tests {
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
168 use super::*;
760
eba9a3d0c484 formats: Fix the tests.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 757
diff changeset
169 use std::io::Cursor;
637
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
170
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
171 #[test]
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
172 fn bitstream() {
760
eba9a3d0c484 formats: Fix the tests.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 757
diff changeset
173 let data = Cursor::new(b"Hello world!\0");
637
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
174 let bitstream = BitStream::new(data);
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
175 let mut pbg3 = PBG3BitStream::new(bitstream);
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
176 assert_eq!(pbg3.read_string(42).unwrap(), b"Hello world!");
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
177 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
178
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
179 #[test]
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
180 fn file_present() {
645
7bde50132735 Don’t hardcode my home directory in tests.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 637
diff changeset
181 let file = File::open("EoSD/MD.DAT").unwrap();
637
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
182 let file = io::BufReader::new(file);
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
183 let pbg3 = PBG3::from_file(file).unwrap();
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
184 let files = pbg3.list_files().cloned().collect::<Vec<String>>();
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
185 assert!(files.contains(&String::from("th06_01.pos")));
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
186 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
187
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
188 #[test]
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
189 fn check_all_files() {
645
7bde50132735 Don’t hardcode my home directory in tests.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 637
diff changeset
190 let file = File::open("EoSD/MD.DAT").unwrap();
637
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
191 let file = io::BufReader::new(file);
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
192 let mut pbg3 = PBG3::from_file(file).unwrap();
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
193 let files = pbg3.list_files().cloned().collect::<Vec<String>>();
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
194 for filename in files {
760
eba9a3d0c484 formats: Fix the tests.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 757
diff changeset
195 pbg3.get_file(&filename, true).unwrap();
637
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
196 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
197 }
afa012bb8021 Hello Rust!
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
diff changeset
198 }