xoreos  0.0.5
nitrofile.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 /* Most of the Nintendo DS file formats store a BOM signaling their endianness.
26  * We interpret that BOM and return an appropriate SeekableSubReadStreamEndian.
27  *
28  * We have two ways of opening a Nitro file:
29  * - The first takes a reference to a SeekableReadStream
30  * - The second takes a pointer to a SeekableReadStream
31  * The second one *always* takes over the original SeekableReadStream.
32  */
33 
34 #include "src/common/types.h"
35 #include "src/common/scopedptr.h"
36 #include "src/common/readstream.h"
37 
38 #include "src/aurora/nitrofile.h"
39 
40 namespace Aurora {
41 
43  stream.skip(4); // Tag, we don't care about that one here
44 
45  const uint16 bom = stream.readUint16BE();
46  if ((bom != 0xFFFE) && (bom != 0xFEFF))
47  throw Common::Exception("Invalid BOM: 0x%04X", (uint) bom);
48 
49  return bom == 0xFEFF;
50 }
51 
53  const size_t begin = stream.pos();
54  const size_t end = stream.size();
55 
56  const bool bigEndian = isBigEndian(stream);
57 
58  return new Common::SeekableSubReadStreamEndian(&stream, begin, end, bigEndian, false);
59 }
60 
63 
64  const size_t begin = nitroStream->pos();
65  const size_t end = nitroStream->size();
66 
67  const bool bigEndian = isBigEndian(*nitroStream);
68 
69  return new Common::SeekableSubReadStreamEndian(nitroStream.release(), begin, end, bigEndian, true);
70 }
71 
72 } // End of namespace Aurora
This is a wrapper around SeekableSubReadStream, but it adds non-endian read methods whose endianness ...
Definition: readstream.h:383
PointerType release()
Returns the plain pointer value and releases ScopedPtr.
Definition: scopedptr.h:103
static bool isBigEndian(Common::SeekableReadStream &stream)
Definition: nitrofile.cpp:42
A simple scoped smart pointer template.
uint16_t uint16
Definition: types.h:202
uint16 readUint16BE()
Read an unsigned 16-bit word stored in big endian (MSB first) order from the stream and return it...
Definition: readstream.h:155
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
Low-level type definitions to handle fixed width types portably.
StackException Exception
Definition: error.h:59
virtual size_t size() const =0
Obtains the total size of the stream, measured in bytes.
Basic reading stream interfaces.
virtual size_t pos() const =0
Obtains the current value of the stream position indicator of the stream.
Base class for Nitro (Nintendo DS) files.
static Common::SeekableSubReadStreamEndian * open(Common::SeekableReadStream &stream)
Treat this stream as a Nitro file and return an endian&#39;d stream according to its BOM.
Definition: nitrofile.cpp:52
Interface for a seekable & readable data stream.
Definition: readstream.h:265
unsigned int uint
Definition: types.h:211