xoreos  0.0.5
erffile.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef AURORA_ERFFILE_H
26 #define AURORA_ERFFILE_H
27 
28 #include <vector>
29 
30 #include "src/common/types.h"
31 #include "src/common/scopedptr.h"
32 #include "src/common/ustring.h"
33 
34 #include "src/aurora/types.h"
35 #include "src/aurora/archive.h"
36 #include "src/aurora/aurorafile.h"
37 #include "src/aurora/locstring.h"
38 
39 namespace Common {
40  class SeekableReadStream;
41 }
42 
43 namespace Aurora {
44 
105 class ERFFile : public Archive, public AuroraFile {
106 public:
120  ERFFile(Common::SeekableReadStream *erf, const std::vector<byte> &password = std::vector<byte>());
121  ~ERFFile();
122 
124  const ResourceList &getResources() const;
125 
127  uint32 getResourceSize(uint32 index) const;
128 
130  Common::SeekableReadStream *getResource(uint32 index, bool tryNoCopy = false) const;
131 
133  uint32 getBuildYear() const;
135  uint32 getBuildDay() const;
136 
138  const LocString &getDescription() const;
139 
142 
144  static LocString getDescription(const Common::UString &fileName);
145 
146 private:
147  enum Encryption {
153  };
154 
155  enum Compression {
160  };
161 
163  struct ERFHeader {
165 
168 
172 
174 
177 
179 
182 
184 
187 
189  std::vector<byte> passwordDigest;
190 
191  ERFHeader();
192  ~ERFHeader();
193 
194  void clear();
195  void clearStringTable();
196 
197  bool isSensible(size_t fileSize = SIZE_MAX);
198  };
199 
201  struct IResource {
205  };
206 
207  typedef std::vector<IResource> IResourceList;
208 
210 
212 
215 
218 
221 
223  std::vector<byte> _password;
224 
225  void load();
226 
227  // .--- Header
228  static void verifyVersion(uint32 id, uint32 version, bool utf16le);
229 
230  static void readERFHeader(Common::SeekableReadStream &erf, ERFHeader &header, uint32 version,
231  std::vector<byte> &password);
232  static void readDescription(LocString &description, Common::SeekableReadStream &erf,
233  const ERFHeader &header);
234 
235  void readResources(Common::SeekableReadStream &erf, const ERFHeader &header);
236  // '---
237 
238  // .--- V1.0
239  static void readV10Header(Common::SeekableReadStream &erf, ERFHeader &header);
240  void readV10ResList(Common::SeekableReadStream &erf, const ERFHeader &header);
241  void readV10KeyList(Common::SeekableReadStream &erf, const ERFHeader &header);
242  // '---
243 
244  // .--- V1.1
245  static void readV11Header(Common::SeekableReadStream &erf, ERFHeader &header);
246  void readV11KeyList(Common::SeekableReadStream &erf, const ERFHeader &header);
247  // '---
248 
249  // .--- V2.0
250  static void readV20Header(Common::SeekableReadStream &erf, ERFHeader &header);
251  void readV20ResList(Common::SeekableReadStream &erf, const ERFHeader &header);
252  // '---
253 
254  // .--- V2.1
255  static void readV21Header(Common::SeekableReadStream &erf, ERFHeader &header);
256  void readV21ResList(Common::SeekableReadStream &erf, const ERFHeader &header);
257  // '---
258 
259  // .--- V2.2
260  static void readV22Header(Common::SeekableReadStream &erf, ERFHeader &header, uint32 &flags);
261  void readV22ResList(Common::SeekableReadStream &erf, const ERFHeader &header);
262  // '---
263 
264  // .--- V3.0
265  static void readV30Header(Common::SeekableReadStream &erf, ERFHeader &header, uint32 &flags);
266  void readV30ResList(Common::SeekableReadStream &erf, const ERFHeader &header);
267  // '---
268 
269  // .--- Encryption
270  void verifyPasswordDigest();
271 
273  Encryption encryption, const std::vector<byte> &password);
275  Encryption encryption, const std::vector<byte> &password);
276 
277  static Common::SeekableReadStream *decrypt(Common::SeekableReadStream &erf, size_t pos, size_t size,
278  Encryption encryption, const std::vector<byte> &password);
280  Encryption encryption, const std::vector<byte> &password);
281 
283  const std::vector<byte> &password);
284  static bool findNWNPremiumKey (Common::SeekableReadStream &erf, ERFHeader &header,
285  const std::vector<byte> &md5, std::vector<byte> &password);
286  static void readNWNPremiumHeader (Common::SeekableReadStream &erf, ERFHeader &header,
287  std::vector<byte> &password);
288 
289  void decryptNWNPremium();
290  // '---
291 
292  // .--- Compression
294  uint32 unpackedSize) const;
295 
297  uint32 unpackedSize) const;
299  uint32 unpackedSize) const;
301  uint32 unpackedSize) const;
302 
303  Common::SeekableReadStream *decompressZlib(const byte *compressedData, uint32 packedSize,
304  uint32 unpackedSize, int windowBits) const;
305  // '---
306 
307  const IResource &getIResource(uint32 index) const;
308 };
309 
310 } // End of namespace Aurora
311 
312 #endif // AURORA_ERFFILE_H
Common::SeekableReadStream * decompressZlib(const byte *compressedData, uint32 packedSize, uint32 unpackedSize, int windowBits) const
Definition: erffile.cpp:839
bool isSensible(size_t fileSize=SIZE_MAX)
Definition: erffile.cpp:132
Common::SeekableReadStream * decompress(Common::MemoryReadStream *packedStream, uint32 unpackedSize) const
Definition: erffile.cpp:766
Definition: 2dafile.h:39
Compression compression
The compression algorithm in use.
Definition: erffile.h:186
A class holding an UTF-8 string.
Definition: ustring.h:48
No compression as all.
Definition: erffile.h:156
uint32 stringTableSize
Size of the string table.
Definition: erffile.h:181
Common::SeekableReadStream * decompressStandardZlib(Common::MemoryReadStream *packedStream, uint32 unpackedSize) const
Definition: erffile.cpp:824
A localized string.
Definition: locstring.h:43
static void verifyVersion(uint32 id, uint32 version, bool utf16le)
Definition: erffile.cpp:161
Compression using DEFLATE with default parameters.
Definition: erffile.h:158
static void readV20Header(Common::SeekableReadStream &erf, ERFHeader &header)
Definition: erffile.cpp:359
void readV11KeyList(Common::SeekableReadStream &erf, const ERFHeader &header)
Definition: erffile.cpp:569
void readV20ResList(Common::SeekableReadStream &erf, const ERFHeader &header)
Definition: erffile.cpp:591
ResourceList _resources
External list of resource names and types.
Definition: erffile.h:217
uint32 descriptionSize
Number of bytes in the description structure.
Definition: erffile.h:173
static void readV30Header(Common::SeekableReadStream &erf, ERFHeader &header, uint32 &flags)
Definition: erffile.cpp:409
uint32 offDescription
Offset to the description.
Definition: erffile.h:169
uint32 langCount
Number of language strings in the description.
Definition: erffile.h:166
static void readV22Header(Common::SeekableReadStream &erf, ERFHeader &header, uint32 &flags)
Definition: erffile.cpp:387
Handling various archive files.
void verifyPasswordDigest()
Definition: erffile.cpp:174
void readV22ResList(Common::SeekableReadStream &erf, const ERFHeader &header)
Definition: erffile.cpp:630
The header of an ERF file.
Definition: erffile.h:163
static bool decryptNWNPremiumHeader(Common::SeekableReadStream &erf, ERFHeader &header, const std::vector< byte > &password)
Definition: erffile.cpp:225
void readResources(Common::SeekableReadStream &erf, const ERFHeader &header)
Definition: erffile.cpp:513
const LocString & getDescription() const
Return the description.
Definition: erffile.cpp:693
ERFFile(Common::SeekableReadStream *erf, const std::vector< byte > &password=std::vector< byte >())
Take over this stream and read an ERF file out of it.
Definition: erffile.cpp:150
A simple scoped smart pointer template.
Blowfish encryption as used by Dragon Age II (V3.0).
Definition: erffile.h:151
void readV10ResList(Common::SeekableReadStream &erf, const ERFHeader &header)
Definition: erffile.cpp:582
uint32 buildDay
The day of year the ERF was built.
Definition: erffile.h:176
Blowfish encryption as used by Neverwinter Nights (V1.1).
Definition: erffile.h:152
static void readDescription(LocString &description, Common::SeekableReadStream &erf, const ERFHeader &header)
Definition: erffile.cpp:499
std::vector< byte > _password
The password we were given, if any.
Definition: erffile.h:223
Compression using DEFLATE with an extra header byte.
Definition: erffile.h:157
static void readV10Header(Common::SeekableReadStream &erf, ERFHeader &header)
Definition: erffile.cpp:312
Common::SeekableReadStream * getResource(uint32 index, bool tryNoCopy=false) const
Return a stream of the resource&#39;s contents.
Definition: erffile.cpp:712
const ResourceList & getResources() const
Return the list of resources.
Definition: erffile.cpp:697
std::list< Resource > ResourceList
Definition: archive.h:57
IResourceList _iResources
Internal list of resource offsets and sizes.
Definition: erffile.h:220
Base for BioWare&#39;s Aurora engine files.
void readV21ResList(Common::SeekableReadStream &erf, const ERFHeader &header)
Definition: erffile.cpp:610
bool isNWNPremium
Is this a Neverwinter Nights premium module?
Definition: erffile.h:178
Encryption encryption
The encryption algorithm in use.
Definition: erffile.h:185
std::vector< byte > passwordDigest
Digest of the encryption password, if any.
Definition: erffile.h:189
uint32 unpackedSize
The resource&#39;s unpacked size.
Definition: erffile.h:204
uint32 descriptionID
ID of the description.
Definition: erffile.h:167
static void readV11Header(Common::SeekableReadStream &erf, ERFHeader &header)
Definition: erffile.cpp:329
Simple memory based &#39;stream&#39;, which implements the ReadStream interface for a plain memory block...
Definition: memreadstream.h:66
HashAlgo
The algorithm used for hashing.
Definition: hash.h:37
Low-level type definitions to handle fixed width types portably.
Blowfish encryption as used by Dragon Age: Origins (V2.2).
Definition: erffile.h:150
static bool findNWNPremiumKey(Common::SeekableReadStream &erf, ERFHeader &header, const std::vector< byte > &md5, std::vector< byte > &password)
Definition: erffile.cpp:236
uint32 buildYear
The year the ERF was built.
Definition: erffile.h:175
uint32 getBuildDay() const
Return the day of year the ERF was built.
Definition: erffile.cpp:689
uint32 moduleID
ID of the module this ERF belongs to.
Definition: erffile.h:183
ERFHeader _header
Definition: erffile.h:211
std::vector< IResource > IResourceList
Definition: erffile.h:207
Handling BioWare&#39;s localized strings.
uint32 resCount
Number of resources in this ERF.
Definition: erffile.h:164
Unicode string handling.
static Common::MemoryReadStream * decrypt(Common::SeekableReadStream &cryptStream, Encryption encryption, const std::vector< byte > &password)
Definition: erffile.cpp:731
Basic type definitions to handle files used in BioWare&#39;s Aurora engine.
Common::ScopedArray< char > stringTable
String table used for hashed ERFs.
Definition: erffile.h:180
Common::SeekableReadStream * decompressBiowareZlib(Common::MemoryReadStream *packedStream, uint32 unpackedSize) const
Definition: erffile.cpp:794
Common::ScopedPtr< Common::SeekableReadStream > _erf
Definition: erffile.h:209
XOR encryption as used by V2.2 and V3.0 (UNSUPPORTED!)
Definition: erffile.h:149
uint32 packedSize
The resource&#39;s packed size.
Definition: erffile.h:203
uint32 offset
The offset of the resource within the ERF.
Definition: erffile.h:202
void readV10KeyList(Common::SeekableReadStream &erf, const ERFHeader &header)
Definition: erffile.cpp:556
uint32 offKeyList
Offset to the key list.
Definition: erffile.h:170
No encryption at all.
Definition: erffile.h:148
Class to hold resource data of an ERF archive file.
Definition: erffile.h:105
uint32_t uint32
Definition: types.h:204
void readV30ResList(Common::SeekableReadStream &erf, const ERFHeader &header)
Definition: erffile.cpp:650
Base class for most files found in games using BioWare&#39;s Aurora engine.
Definition: aurorafile.h:52
uint32 getResourceSize(uint32 index) const
Return the size of a resource.
Definition: erffile.cpp:708
Common::HashAlgo getNameHashAlgo() const
Return with which algorithm the name is hashed.
Definition: erffile.cpp:848
LocString _description
The ERF&#39;s description.
Definition: erffile.h:214
#define SIZE_MAX
Definition: types.h:172
const IResource & getIResource(uint32 index) const
Definition: erffile.cpp:701
Compression using DEFLATE, standard zlib chunk.
Definition: erffile.h:159
An abstract file archive.
Definition: archive.h:45
Common::SeekableReadStream * decompressHeaderlessZlib(Common::MemoryReadStream *packedStream, uint32 unpackedSize) const
Definition: erffile.cpp:809
static void readERFHeader(Common::SeekableReadStream &erf, ERFHeader &header, uint32 version, std::vector< byte > &password)
Definition: erffile.cpp:434
void decryptNWNPremium()
Definition: erffile.cpp:302
Internal resource information.
Definition: erffile.h:201
uint32 getBuildYear() const
Return the year the ERF was built.
Definition: erffile.cpp:685
uint32 offResList
Offset to the resource list.
Definition: erffile.h:171
Interface for a seekable & readable data stream.
Definition: readstream.h:265
uint8 byte
Definition: types.h:209
static void readV21Header(Common::SeekableReadStream &erf, ERFHeader &header)
Definition: erffile.cpp:370
static void readNWNPremiumHeader(Common::SeekableReadStream &erf, ERFHeader &header, std::vector< byte > &password)
Definition: erffile.cpp:257