xoreos  0.0.5
pefile.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 #include "src/common/ptrvector.h"
26 #include "src/common/error.h"
27 #include "src/common/pe_exe.h"
30 
31 #include "src/aurora/pefile.h"
32 
33 namespace Aurora {
34 
35 PEFile::PEFile(Common::SeekableReadStream *exe, const std::vector<Common::UString> &remap) {
36  assert(exe);
37 
39 
40  load(remap);
41 }
42 
44 }
45 
47  return _resources;
48 }
49 
51  // Convert from the PE cursor group/cursor format to the standalone
52  // cursor format.
53 
55  cursorGroup(_peFile->getResource(Common::kPEGroupCursor, index));
56 
57  if (!cursorGroup)
58  throw Common::Exception("No such PE resource %u", index);
59 
60 
62 
63  // Cursor Group Header
64  out.writeUint16LE(cursorGroup->readUint16LE());
65  out.writeUint16LE(cursorGroup->readUint16LE());
66 
67  const size_t cursorCount = cursorGroup->readUint16LE();
68  out.writeUint16LE(cursorCount);
69 
70 
72  cursorStreams.resize(cursorCount);
73 
74  uint32 startOffset = 6 + cursorCount * 16;
75 
76  for (size_t i = 0; i < cursorCount; i++) {
77  out.writeByte(cursorGroup->readUint16LE()); // width
78  out.writeByte(cursorGroup->readUint16LE() / 2); // height
79  cursorGroup->readUint16LE(); // planes
80  out.writeByte(cursorGroup->readUint16LE()); // bits per pixel
81  out.writeByte(0); // reserved
82 
83  cursorGroup->readUint32LE(); // data size
84  uint16 id = cursorGroup->readUint16LE();
85 
86  cursorStreams[i] = _peFile->getResource(Common::kPECursor, id);
87  if (!cursorStreams[i])
88  throw Common::Exception("Could not get cursor resource %d", id);
89 
90  out.writeUint16LE(cursorStreams[i]->readUint16LE()); // hotspot X
91  out.writeUint16LE(cursorStreams[i]->readUint16LE()); // hotspot Y
92  out.writeUint32LE(cursorStreams[i]->size() - 4); // size
93  out.writeUint32LE(startOffset); // offset
94  startOffset += cursorStreams[i]->size() - 4;
95  }
96 
97  for (size_t i = 0; i < cursorCount; i++)
98  out.writeStream(*cursorStreams[i], cursorStreams[i]->size() - 4);
99 
100  out.setDisposable(false);
101  return new Common::MemoryReadStream(out.getData(), out.size(), true);
102 }
103 
104 void PEFile::load(const std::vector<Common::UString> &remap) {
105  std::vector<Common::PEResourceID> cursorList = _peFile->getNameList(Common::kPEGroupCursor);
106 
107  for (std::vector<Common::PEResourceID>::const_iterator it = cursorList.begin(); it != cursorList.end(); ++it) {
108  Resource res;
109 
110  if (it->getID() == 0xFFFFFFFF)
111  throw Common::Exception("Found non-integer cursor group");
112 
113  uint32 id = it->getID() - 1;
114  if (id >= remap.size())
115  res.name = Common::UString::format("cursor%d", id);
116  else
117  res.name = remap[id];
118 
119  res.type = kFileTypeCUR;
120  res.index = id + 1;
121 
122  _resources.push_back(res);
123  }
124 }
125 
126 } // End of namespace Aurora
void load(const std::vector< Common::UString > &remap)
Definition: pefile.cpp:104
A stream that dynamically grows as it&#39;s written to.
void reset(PointerType o=0)
Resets the pointer with the new value.
Definition: scopedptr.h:87
void resize(typename std::vector< T *>::size_type n, typename std::vector< T *>::value_type val=typename std::vector< T *>::value_type())
Definition: ptrvector.h:76
Implementing the reading stream interfaces for plain memory blocks.
Common::UString name
The resource&#39;s name.
Definition: archive.h:49
PEFile(Common::SeekableReadStream *exe, const std::vector< Common::UString > &remap)
Take over this stream and read a PE file out of it.
Definition: pefile.cpp:35
Exception that provides a stack of explanations.
Definition: error.h:36
Common::SeekableReadStream * getResource(uint32 index, bool tryNoCopy=false) const
Return a stream of the resource&#39;s contents.
Definition: pefile.cpp:50
Cursor, Windows cursor.
Definition: types.h:286
size_t size() const
Return the number of bytes written to this stream in total.
Basic exceptions to throw.
static UString format(const char *s,...) GCC_PRINTF(1
Print formatted data into an UString object, similar to sprintf().
Definition: ustring.cpp:718
uint16_t uint16
Definition: types.h:202
#define UNUSED(x)
Definition: system.h:170
Implementing the writing stream interfaces for memory blocks.
std::list< Resource > ResourceList
Definition: archive.h:57
Simple memory based &#39;stream&#39;, which implements the ReadStream interface for a plain memory block...
Definition: memreadstream.h:66
void writeUint16LE(uint16 value)
Definition: writestream.h:98
StackException Exception
Definition: error.h:59
A vector storing pointer to objects, with automatic deletion.
void setDisposable(bool disposeMemory)
A resource within the archive.
Definition: archive.h:48
void writeByte(byte value)
Definition: writestream.h:88
A class able to load resources from a Windows Portable Executable, such as cursors, bitmaps, and sounds.
Definition: pe_exe.h:102
const ResourceList & getResources() const
Return the list of resources.
Definition: pefile.cpp:46
Portable executable parsing.
FileType type
The resource&#39;s type.
Definition: archive.h:51
A portable executable archive.
uint32_t uint32
Definition: types.h:204
size_t writeStream(ReadStream &stream, size_t n)
Copy n bytes of the given stream into the stream.
Definition: writestream.cpp:72
Common::ScopedPtr< Common::PEResources > _peFile
The actual exe.
Definition: pefile.h:59
ResourceList _resources
External list of resource names and types.
Definition: pefile.h:62
Interface for a seekable & readable data stream.
Definition: readstream.h:265
void writeUint32LE(uint32 value)
Definition: writestream.h:104
uint32 index
The resource&#39;s local index within the archive.
Definition: archive.h:52