xoreos  0.0.5
bzffile.cpp
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 
28 #include <cassert>
29 
30 #include "src/common/util.h"
31 #include "src/common/strutil.h"
32 #include "src/common/error.h"
34 #include "src/common/lzma.h"
35 
36 #include "src/aurora/bzffile.h"
37 #include "src/aurora/keyfile.h"
38 
39 static const uint32 kBZFID = MKTAG('B', 'I', 'F', 'F');
40 static const uint32 kVersion1 = MKTAG('V', '1', ' ', ' ');
41 
42 namespace Aurora {
43 
45  assert(_bzf);
46 
47  load(*_bzf);
48 }
49 
51 }
52 
54  readHeader(bzf);
55 
56  if (_id != kBZFID)
57  throw Common::Exception("Not a BZF file (%s)", Common::debugTag(_id).c_str());
58 
59  if (_version != kVersion1)
60  throw Common::Exception("Unsupported BZF file version %s", Common::debugTag(_version).c_str());
61 
62  uint32 varResCount = bzf.readUint32LE();
63  uint32 fixResCount = bzf.readUint32LE();
64 
65  if (fixResCount != 0)
66  throw Common::Exception("TODO: Fixed BZF resources");
67 
68  _iResources.resize(varResCount);
69 
70  uint32 offVarResTable = bzf.readUint32LE();
71 
72  try {
73 
74  readVarResTable(bzf, offVarResTable);
75 
76  } catch (Common::Exception &e) {
77  e.add("Failed reading BZF file");
78  throw;
79  }
80 
81 }
82 
84  bzf.seek(offset);
85 
86  for (uint32 i = 0; i < _iResources.size(); i++) {
87  bzf.skip(4); // ID
88 
89  _iResources[i].offset = bzf.readUint32LE();
90  _iResources[i].size = bzf.readUint32LE();
91  _iResources[i].type = (FileType) bzf.readUint32LE();
92 
93  if (i > 0)
94  _iResources[i - 1].packedSize = _iResources[i].offset - _iResources[i - 1].offset;
95  }
96 
97  if (!_iResources.empty())
98  _iResources.back().packedSize = bzf.size() - _iResources.back().offset;
99 }
100 
101 void BZFFile::mergeKEY(const KEYFile &key, uint32 dataFileIndex) {
102  const KEYFile::ResourceList &keyResList = key.getResources();
103 
104  for (KEYFile::ResourceList::const_iterator keyRes = keyResList.begin(); keyRes != keyResList.end(); ++keyRes) {
105  if (keyRes->bifIndex != dataFileIndex)
106  continue;
107 
108  if (keyRes->resIndex >= _iResources.size()) {
109  warning("Resource index out of range (%d/%d)", keyRes->resIndex, (int) _iResources.size());
110  continue;
111  }
112 
113  if (keyRes->type != _iResources[keyRes->resIndex].type)
114  warning("KEY and BZF disagree on the type of the resource \"%s\" (%d, %d). Trusting the BZF",
115  keyRes->name.c_str(), keyRes->type, _iResources[keyRes->resIndex].type);
116 
117  Resource res;
118 
119  res.name = keyRes->name;
120  res.type = _iResources[keyRes->resIndex].type;
121  res.index = keyRes->resIndex;
122 
123  _resources.push_back(res);
124  }
125 
126 }
127 
129  return _resources;
130 }
131 
133  if (index >= _iResources.size())
134  throw Common::Exception("Resource index out of range (%u/%u)", index, (uint)_iResources.size());
135 
136  return _iResources[index];
137 }
138 
140  return getIResource(index).size;
141 }
142 
144  const IResource &res = getIResource(index);
145 
146  _bzf->seek(res.offset);
147 
148  return Common::decompressLZMA1(*_bzf, res.packedSize, res.size, true);
149 }
150 
151 } // End of namespace Aurora
#define MKTAG(a0, a1, a2, a3)
A wrapper macro used around four character constants, like &#39;DATA&#39;, to ensure portability.
Definition: endianness.h:140
void add(const char *s,...) GCC_PRINTF(2
Definition: error.cpp:58
uint32 readUint32LE()
Read an unsigned 32-bit word stored in little endian (LSB first) order from the stream and return it...
Definition: readstream.h:133
Internal resource information.
Definition: bzffile.h:82
const ResourceList & getResources() const
Return the list of resources.
Definition: bzffile.cpp:128
virtual size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)=0
Sets the stream position indicator for the stream.
const ResourceList & getResources() const
Return a list of all containing resources.
Definition: keyfile.cpp:145
static const uint32 kVersion1
Definition: bzffile.cpp:40
Implementing the reading stream interfaces for plain memory blocks.
BZFFile(Common::SeekableReadStream *bzf)
Take over this stream and read a BZF file out of it.
Definition: bzffile.cpp:44
Common::UString name
The resource&#39;s name.
Definition: archive.h:49
Decompress LZMA, using liblzma.
Common::ScopedPtr< Common::SeekableReadStream > _bzf
Definition: bzffile.h:93
Class to hold resource index information of a KEY file.
Definition: keyfile.h:70
Utility templates and functions for working with strings and streams.
Exception that provides a stack of explanations.
Definition: error.h:36
static void readHeader(Common::ReadStream &stream, uint32 &id, uint32 &version, bool &utf16le)
Read the header out of a stream.
Definition: aurorafile.cpp:53
IResourceList _iResources
Internal list of resource offsets and sizes.
Definition: bzffile.h:99
const IResource & getIResource(uint32 index) const
Definition: bzffile.cpp:132
Handling BioWare&#39;s KEYs (resource index files).
ResourceList _resources
External list of resource names and types.
Definition: bzffile.h:96
Basic exceptions to throw.
#define UNUSED(x)
Definition: system.h:170
Utility templates and functions.
Handling BioWare&#39;s BZFs (resource data files), used in the iOS version of Knights of the Old Republic...
std::list< Resource > ResourceList
Definition: archive.h:57
virtual size_t skip(ptrdiff_t offset)
Skip the specified number of bytes, adding that offset to the current position in the stream...
Definition: readstream.h:317
uint32 offset
The offset of the resource within the BZF.
Definition: bzffile.h:85
uint32 _id
The file&#39;s ID.
Definition: aurorafile.h:77
StackException Exception
Definition: error.h:59
A resource within the archive.
Definition: archive.h:48
uint32 _version
The file&#39;s version.
Definition: aurorafile.h:78
void warning(const char *s,...)
Definition: util.cpp:33
virtual size_t size() const =0
Obtains the total size of the stream, measured in bytes.
byte * decompressLZMA1(const byte *data, size_t inputSize, size_t outputSize, bool noEndMarker)
Decompress using the LZMA1 algorithm.
Definition: lzma.cpp:65
uint32 size
The resource&#39;s size.
Definition: bzffile.h:86
void readVarResTable(Common::SeekableReadStream &bzf, uint32 offset)
Definition: bzffile.cpp:83
void mergeKEY(const KEYFile &key, uint32 dataFileIndex)
Merge information from the KEY into the data file.
Definition: bzffile.cpp:101
uint32_t uint32
Definition: types.h:204
UString debugTag(uint32 tag, bool trim)
Create an elaborate string from an integer tag, for debugging purposes.
Definition: strutil.cpp:117
Common::SeekableReadStream * getResource(uint32 index, bool tryNoCopy=false) const
Return a stream of the resource&#39;s contents.
Definition: bzffile.cpp:143
uint32 getResourceSize(uint32 index) const
Return the size of a resource.
Definition: bzffile.cpp:139
FileType
Various file types used by the Aurora engine and found in archives.
Definition: types.h:56
static const uint32 kBZFID
Definition: bzffile.cpp:39
uint32 packedSize
Raw, compressed data size.
Definition: bzffile.h:88
Interface for a seekable & readable data stream.
Definition: readstream.h:265
std::vector< Resource > ResourceList
Definition: keyfile.h:81
unsigned int uint
Definition: types.h:211
void load(Common::SeekableReadStream &bzf)
Definition: bzffile.cpp:53