xoreos  0.0.5
ndsrom.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 
25 // Based on http://dsibrew.org/wiki/NDS_Format
26 
27 #include <cassert>
28 
29 #include "src/common/util.h"
30 #include "src/common/ustring.h"
31 #include "src/common/error.h"
33 #include "src/common/readfile.h"
34 #include "src/common/encoding.h"
35 
36 #include "src/aurora/ndsrom.h"
37 #include "src/aurora/util.h"
38 
39 namespace Aurora {
40 
42  _nds.reset(new Common::ReadFile(fileName));
43 
44  load(*_nds);
45 }
46 
48  assert(_nds);
49 
50  load(*_nds);
51 }
52 
54 }
55 
57  if (!isNDS(nds, _title, _code, _maker))
58  throw Common::Exception("Not a supported NDS ROM file");
59 
60  nds.seek(0x40);
61 
62  uint32 fileNameTableOffset = nds.readUint32LE();
63  uint32 fileNameTableLength = nds.readUint32LE();
64  uint32 fatOffset = nds.readUint32LE();
65  //uint32 fatLength = nds.readUint32LE();
66 
67  try {
68 
69  readNames(nds, fileNameTableOffset, fileNameTableLength);
70  readFAT(nds, fatOffset);
71 
72  } catch (Common::Exception &e) {
73  e.add("Failed reading NDS file");
74  throw;
75  }
76 
77 }
78 
80  nds.seek(offset + 8);
81 
82  uint32 index = 0;
83  while (((size_t)nds.pos()) < (size_t)(offset + length)) {
84  Resource res;
85 
86  byte nameLength = nds.readByte();
87  if ((nameLength == 0) || ((size_t)nds.pos() >= (size_t)(offset + length)))
88  break;
89 
91 
92  res.name = TypeMan.setFileType(name, kFileTypeNone);
93  res.type = TypeMan.getFileType(name);
94  res.index = index++;
95 
96  _resources.push_back(res);
97  }
98 }
99 
101  nds.seek(offset);
102 
103  _iResources.resize(_resources.size());
104  for (IResourceList::iterator res = _iResources.begin(); res != _iResources.end(); ++res) {
105  res->offset = nds.readUint32LE();
106  res->size = nds.readUint32LE() - res->offset; // Value is the end offset
107  }
108 }
109 
111  return _title;
112 }
113 
115  return _code;
116 }
117 
119  return _maker;
120 }
121 
123  Common::UString &title, Common::UString &code, Common::UString &maker) {
124 
125  if (stream.size() < 0x40)
126  return false;
127 
128  try {
129  stream.seek(0);
130 
131  title = Common::readStringFixed(stream, Common::kEncodingASCII, 12);
134  } catch (...) {
135  return false;
136  }
137 
138  return true;
139 }
140 
142  name.makeLower();
143 
144  for (ResourceList::const_iterator r = _resources.begin(); r != _resources.end(); ++r)
145  if (TypeMan.setFileType(r->name, r->type) == name)
146  return true;
147 
148  return false;
149 }
150 
152  return _resources;
153 }
154 
156  if (index >= _iResources.size())
157  throw Common::Exception("Resource index out of range (%u/%u)", index, (uint)_iResources.size());
158 
159  return _iResources[index];
160 }
161 
163  return getIResource(index).size;
164 }
165 
167  const IResource &res = getIResource(index);
168 
169  _nds->seek(res.offset);
170 
171  if (tryNoCopy)
172  return new Common::SeekableSubReadStream(_nds.get(), res.offset, res.offset + res.size);
173 
174  _nds->seek(res.offset);
175 
176  return _nds->readStream(res.size);
177 }
178 
179 } // End of namespace Aurora
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
const Common::UString & getTitle() const
Return the game title string stored in the NDS header.
Definition: ndsrom.cpp:110
#define TypeMan
Shortcut for accessing the file type manager.
Definition: util.h:85
Common::UString _title
Definition: ndsrom.h:87
A class holding an UTF-8 string.
Definition: ustring.h:48
virtual size_t seek(ptrdiff_t offset, Origin whence=kOriginBegin)=0
Sets the stream position indicator for the stream.
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
ResourceList _resources
External list of resource names and types.
Definition: ndsrom.h:92
const Common::UString & getMaker() const
Return the maker code string stored in the NDS header.
Definition: ndsrom.cpp:118
A simple streaming file reading class.
Definition: readfile.h:40
Implementing the reading stream interfaces for plain memory blocks.
Utility functions to handle files used in BioWare&#39;s Aurora engine.
Common::UString name
The resource&#39;s name.
Definition: archive.h:49
bool hasResource(Common::UString name) const
Does the Nintendo DS ROM contain a certain resource?
Definition: ndsrom.cpp:141
Exception that provides a stack of explanations.
Definition: error.h:36
void readFAT(Common::SeekableReadStream &nds, uint32 offset)
Definition: ndsrom.cpp:100
Basic exceptions to throw.
Utility templates and functions.
Internal resource information.
Definition: ndsrom.h:78
std::list< Resource > ResourceList
Definition: archive.h:57
Utility functions for working with differing string encodings.
Common::UString _code
Definition: ndsrom.h:88
uint32 offset
The offset of the resource within the NDS.
Definition: ndsrom.h:79
StackException Exception
Definition: error.h:59
A resource within the archive.
Definition: archive.h:48
const Common::UString & getCode() const
Return the game code string stored in the NDS header.
Definition: ndsrom.cpp:114
virtual size_t size() const =0
Obtains the total size of the stream, measured in bytes.
virtual size_t pos() const =0
Obtains the current value of the stream position indicator of the stream.
Common::SeekableReadStream * getResource(uint32 index, bool tryNoCopy=false) const
Return a stream of the resource&#39;s contents.
Definition: ndsrom.cpp:166
Nintendo DS ROM parsing.
Implementing the stream reading interfaces for files.
Unicode string handling.
void readNames(Common::SeekableReadStream &nds, uint32 offset, uint32 length)
Definition: ndsrom.cpp:79
Plain, unextended ASCII (7bit clean).
Definition: encoding.h:40
PointerType get() const
Returns the plain pointer value.
Definition: scopedptr.h:96
IResourceList _iResources
Internal list of resource offsets and sizes.
Definition: ndsrom.h:95
UString toLower() const
Return a lowercased copy of the string.
Definition: ustring.cpp:481
Common::ScopedPtr< Common::SeekableReadStream > _nds
Definition: ndsrom.h:85
FileType type
The resource&#39;s type.
Definition: archive.h:51
uint32_t uint32
Definition: types.h:204
NDSFile(const Common::UString &fileName)
Over this file in the filesystem and read a NDS file out of it.
Definition: ndsrom.cpp:41
const IResource & getIResource(uint32 index) const
Definition: ndsrom.cpp:155
static bool isNDS(Common::SeekableReadStream &stream, Common::UString &title, Common::UString &code, Common::UString &maker)
Check if a stream is a valid Nintendo DS ROM and read its title, code and maker strings.
Definition: ndsrom.cpp:122
UString readStringFixed(SeekableReadStream &stream, Encoding encoding, size_t length)
Read length bytes as a string with the given encoding out of a stream.
Definition: encoding.cpp:297
SeekableSubReadStream provides access to a SeekableReadStream restricted to the range [begin...
Definition: readstream.h:359
const ResourceList & getResources() const
Return the list of resources.
Definition: ndsrom.cpp:151
Common::UString _maker
Definition: ndsrom.h:89
uint32 getResourceSize(uint32 index) const
Return the size of a resource.
Definition: ndsrom.cpp:162
Interface for a seekable & readable data stream.
Definition: readstream.h:265
void makeLower()
Convert the string to lowercase.
Definition: ustring.cpp:473
byte readByte()
Read an unsigned byte from the stream and return it.
Definition: readstream.h:92
uint8 byte
Definition: types.h:209
uint32 index
The resource&#39;s local index within the archive.
Definition: archive.h:52
unsigned int uint
Definition: types.h:211
uint32 size
The resource&#39;s size.
Definition: ndsrom.h:80
void load(Common::SeekableReadStream &nds)
Definition: ndsrom.cpp:56