COMMON: Add a common base class for the Windows resource classes
This commit is contained in:
parent
46056aba3c
commit
aa9a41545a
10 changed files with 177 additions and 120 deletions
|
@ -20,6 +20,8 @@
|
|||
*
|
||||
*/
|
||||
|
||||
#include "common/file.h"
|
||||
#include "common/memstream.h"
|
||||
#include "common/str.h"
|
||||
#include "common/winexe.h"
|
||||
|
||||
|
@ -78,4 +80,84 @@ String WinResourceID::toString() const {
|
|||
return "";
|
||||
}
|
||||
|
||||
bool WinResources::loadFromEXE(const String &fileName) {
|
||||
if (fileName.empty())
|
||||
return false;
|
||||
|
||||
File *file = new File();
|
||||
|
||||
if (!file->open(fileName)) {
|
||||
delete file;
|
||||
return false;
|
||||
}
|
||||
|
||||
return loadFromEXE(file);
|
||||
}
|
||||
|
||||
bool WinResources::loadFromCompressedEXE(const String &fileName) {
|
||||
// Based on http://www.cabextract.org.uk/libmspack/doc/szdd_kwaj_format.html
|
||||
|
||||
// TODO: Merge this with with loadFromEXE() so the handling of the compressed
|
||||
// EXE's is transparent
|
||||
|
||||
File file;
|
||||
|
||||
if (!file.open(fileName))
|
||||
return false;
|
||||
|
||||
// First part of the signature
|
||||
if (file.readUint32BE() != MKTAG('S','Z','D','D'))
|
||||
return false;
|
||||
|
||||
// Second part of the signature
|
||||
if (file.readUint32BE() != 0x88F02733)
|
||||
return false;
|
||||
|
||||
// Compression mode must be 'A'
|
||||
if (file.readByte() != 'A')
|
||||
return false;
|
||||
|
||||
file.readByte(); // file name character change
|
||||
uint32 unpackedLength = file.readUint32LE();
|
||||
|
||||
byte *window = new byte[0x1000];
|
||||
int pos = 0x1000 - 16;
|
||||
memset(window, 0x20, 0x1000); // Initialize to all spaces
|
||||
|
||||
byte *unpackedData = (byte *)malloc(unpackedLength);
|
||||
assert(unpackedData);
|
||||
byte *dataPos = unpackedData;
|
||||
|
||||
// Apply simple LZSS decompression
|
||||
for (;;) {
|
||||
byte controlByte = file.readByte();
|
||||
|
||||
if (file.eos())
|
||||
break;
|
||||
|
||||
for (byte i = 0; i < 8; i++) {
|
||||
if (controlByte & (1 << i)) {
|
||||
*dataPos++ = window[pos++] = file.readByte();
|
||||
pos &= 0xFFF;
|
||||
} else {
|
||||
int matchPos = file.readByte();
|
||||
int matchLen = file.readByte();
|
||||
matchPos |= (matchLen & 0xF0) << 4;
|
||||
matchLen = (matchLen & 0xF) + 3;
|
||||
while (matchLen--) {
|
||||
*dataPos++ = window[pos++] = window[matchPos++];
|
||||
pos &= 0xFFF;
|
||||
matchPos &= 0xFFF;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
delete[] window;
|
||||
SeekableReadStream *stream = new MemoryReadStream(unpackedData, unpackedLength);
|
||||
|
||||
return loadFromEXE(stream);
|
||||
}
|
||||
|
||||
} // End of namespace Common
|
||||
|
|
|
@ -28,6 +28,8 @@
|
|||
|
||||
namespace Common {
|
||||
|
||||
class SeekableReadStream;
|
||||
|
||||
/** The default Windows resources. */
|
||||
enum WinResourceType {
|
||||
kWinCursor = 0x01,
|
||||
|
@ -90,6 +92,44 @@ struct WinResourceID_EqualTo {
|
|||
bool operator()(const WinResourceID &id1, const WinResourceID &id2) const { return id1 == id2; }
|
||||
};
|
||||
|
||||
/**
|
||||
* A class able to load resources from a Windows Executable, such
|
||||
* as cursors, bitmaps, and sounds.
|
||||
*/
|
||||
class WinResources {
|
||||
public:
|
||||
virtual ~WinResources() {};
|
||||
|
||||
/** Clear all information. */
|
||||
virtual void clear() = 0;
|
||||
|
||||
/** Load from an EXE file. */
|
||||
virtual bool loadFromEXE(const String &fileName);
|
||||
|
||||
/** Load from a Windows compressed EXE file. */
|
||||
virtual bool loadFromCompressedEXE(const String &fileName);
|
||||
|
||||
/** Load from a stream. */
|
||||
virtual bool loadFromEXE(SeekableReadStream *stream) = 0;
|
||||
|
||||
/** Return a list of IDs for a given type. */
|
||||
virtual const Array<WinResourceID> getIDList(const WinResourceID &type) const = 0;
|
||||
|
||||
/** Return a list of languages for a given type and ID. */
|
||||
virtual const Array<WinResourceID> getLangList(const WinResourceID &type, const WinResourceID &id) const {
|
||||
Array<WinResourceID> array;
|
||||
return array;
|
||||
}
|
||||
|
||||
/** Return a stream to the specified resource, taking the first language found (or 0 if non-existent). */
|
||||
virtual SeekableReadStream *getResource(const WinResourceID &type, const WinResourceID &id) = 0;
|
||||
|
||||
/** Return a stream to the specified resource (or 0 if non-existent). */
|
||||
virtual SeekableReadStream *getResource(const WinResourceID &type, const WinResourceID &id, const WinResourceID &lang) {
|
||||
return getResource(type, id);
|
||||
}
|
||||
};
|
||||
|
||||
} // End of namespace Common
|
||||
|
||||
#endif
|
||||
|
|
|
@ -21,8 +21,6 @@
|
|||
*/
|
||||
|
||||
#include "common/debug.h"
|
||||
#include "common/file.h"
|
||||
#include "common/memstream.h"
|
||||
#include "common/str.h"
|
||||
#include "common/stream.h"
|
||||
#include "common/winexe_ne.h"
|
||||
|
@ -46,20 +44,6 @@ void NEResources::clear() {
|
|||
_resources.clear();
|
||||
}
|
||||
|
||||
bool NEResources::loadFromEXE(const String &fileName) {
|
||||
if (fileName.empty())
|
||||
return false;
|
||||
|
||||
File *file = new File();
|
||||
|
||||
if (!file->open(fileName)) {
|
||||
delete file;
|
||||
return false;
|
||||
}
|
||||
|
||||
return loadFromEXE(file);
|
||||
}
|
||||
|
||||
bool NEResources::loadFromEXE(SeekableReadStream *stream) {
|
||||
clear();
|
||||
|
||||
|
@ -80,72 +64,6 @@ bool NEResources::loadFromEXE(SeekableReadStream *stream) {
|
|||
return true;
|
||||
}
|
||||
|
||||
bool NEResources::loadFromCompressedEXE(const String &fileName) {
|
||||
// Based on http://www.cabextract.org.uk/libmspack/doc/szdd_kwaj_format.html
|
||||
|
||||
// TODO: Merge this with with loadFromEXE() so the handling of the compressed
|
||||
// EXE's is transparent
|
||||
|
||||
File file;
|
||||
|
||||
if (!file.open(fileName))
|
||||
return false;
|
||||
|
||||
// First part of the signature
|
||||
if (file.readUint32BE() != MKTAG('S','Z','D','D'))
|
||||
return false;
|
||||
|
||||
// Second part of the signature
|
||||
if (file.readUint32BE() != 0x88F02733)
|
||||
return false;
|
||||
|
||||
// Compression mode must be 'A'
|
||||
if (file.readByte() != 'A')
|
||||
return false;
|
||||
|
||||
file.readByte(); // file name character change
|
||||
uint32 unpackedLength = file.readUint32LE();
|
||||
|
||||
byte *window = new byte[0x1000];
|
||||
int pos = 0x1000 - 16;
|
||||
memset(window, 0x20, 0x1000); // Initialize to all spaces
|
||||
|
||||
byte *unpackedData = (byte *)malloc(unpackedLength);
|
||||
assert(unpackedData);
|
||||
byte *dataPos = unpackedData;
|
||||
|
||||
// Apply simple LZSS decompression
|
||||
for (;;) {
|
||||
byte controlByte = file.readByte();
|
||||
|
||||
if (file.eos())
|
||||
break;
|
||||
|
||||
for (byte i = 0; i < 8; i++) {
|
||||
if (controlByte & (1 << i)) {
|
||||
*dataPos++ = window[pos++] = file.readByte();
|
||||
pos &= 0xFFF;
|
||||
} else {
|
||||
int matchPos = file.readByte();
|
||||
int matchLen = file.readByte();
|
||||
matchPos |= (matchLen & 0xF0) << 4;
|
||||
matchLen = (matchLen & 0xF) + 3;
|
||||
while (matchLen--) {
|
||||
*dataPos++ = window[pos++] = window[matchPos++];
|
||||
pos &= 0xFFF;
|
||||
matchPos &= 0xFFF;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
delete[] window;
|
||||
SeekableReadStream *stream = new MemoryReadStream(unpackedData, unpackedLength);
|
||||
|
||||
return loadFromEXE(stream);
|
||||
}
|
||||
|
||||
uint32 NEResources::getResourceTableOffset() {
|
||||
if (!_exe)
|
||||
return 0xFFFFFFFF;
|
||||
|
|
|
@ -38,7 +38,7 @@ class SeekableReadStream;
|
|||
*
|
||||
* See http://en.wikipedia.org/wiki/New_Executable for more info.
|
||||
*/
|
||||
class NEResources {
|
||||
class NEResources : public WinResources {
|
||||
public:
|
||||
NEResources();
|
||||
~NEResources();
|
||||
|
@ -47,10 +47,7 @@ public:
|
|||
void clear();
|
||||
|
||||
/** Load from an EXE file. */
|
||||
bool loadFromEXE(const String &fileName);
|
||||
|
||||
/** Load from a Windows compressed EXE file. */
|
||||
bool loadFromCompressedEXE(const String &fileName);
|
||||
using WinResources::loadFromEXE;
|
||||
|
||||
/** Load from a stream. */
|
||||
bool loadFromEXE(SeekableReadStream *stream);
|
||||
|
|
|
@ -23,7 +23,6 @@
|
|||
#include "common/array.h"
|
||||
#include "common/debug.h"
|
||||
#include "common/endian.h"
|
||||
#include "common/file.h"
|
||||
#include "common/str.h"
|
||||
#include "common/stream.h"
|
||||
#include "common/winexe_pe.h"
|
||||
|
@ -44,20 +43,6 @@ void PEResources::clear() {
|
|||
delete _exe; _exe = nullptr;
|
||||
}
|
||||
|
||||
bool PEResources::loadFromEXE(const String &fileName) {
|
||||
if (fileName.empty())
|
||||
return false;
|
||||
|
||||
File *file = new File();
|
||||
|
||||
if (!file->open(fileName)) {
|
||||
delete file;
|
||||
return false;
|
||||
}
|
||||
|
||||
return loadFromEXE(file);
|
||||
}
|
||||
|
||||
bool PEResources::loadFromEXE(SeekableReadStream *stream) {
|
||||
clear();
|
||||
|
||||
|
|
|
@ -37,7 +37,7 @@ class SeekableReadStream;
|
|||
* A class able to load resources from a Windows Portable Executable, such
|
||||
* as cursors, bitmaps, and sounds.
|
||||
*/
|
||||
class PEResources {
|
||||
class PEResources : public WinResources {
|
||||
public:
|
||||
PEResources();
|
||||
~PEResources();
|
||||
|
@ -46,7 +46,7 @@ public:
|
|||
void clear();
|
||||
|
||||
/** Load from an EXE file. */
|
||||
bool loadFromEXE(const String &fileName);
|
||||
using WinResources::loadFromEXE;
|
||||
|
||||
/** Load from a stream. */
|
||||
bool loadFromEXE(SeekableReadStream *stream);
|
||||
|
|
|
@ -80,4 +80,18 @@ String WinResourceID::toString() const {
|
|||
return "";
|
||||
}
|
||||
|
||||
bool WinResources::loadFromEXE(const String &fileName) {
|
||||
if (fileName.empty())
|
||||
return false;
|
||||
|
||||
File *file = new File();
|
||||
|
||||
if (!file->open(fileName.c_str())) {
|
||||
delete file;
|
||||
return false;
|
||||
}
|
||||
|
||||
return loadFromEXE(file);
|
||||
}
|
||||
|
||||
} // End of namespace Common
|
||||
|
|
|
@ -23,6 +23,7 @@
|
|||
#ifndef COMMON_WINEXE_H
|
||||
#define COMMON_WINEXE_H
|
||||
|
||||
#include "file.h"
|
||||
#include "hash-str.h"
|
||||
#include "str.h"
|
||||
|
||||
|
@ -90,6 +91,40 @@ struct WinResourceID_EqualTo {
|
|||
bool operator()(const WinResourceID &id1, const WinResourceID &id2) const { return id1 == id2; }
|
||||
};
|
||||
|
||||
/**
|
||||
* A class able to load resources from a Windows Executable, such
|
||||
* as cursors, bitmaps, and sounds.
|
||||
*/
|
||||
class WinResources {
|
||||
public:
|
||||
virtual ~WinResources() {}
|
||||
|
||||
/** Clear all information. */
|
||||
virtual void clear() = 0;
|
||||
|
||||
/** Load from an EXE file. */
|
||||
virtual bool loadFromEXE(const String &fileName);
|
||||
|
||||
virtual bool loadFromEXE(File *stream) = 0;
|
||||
|
||||
/** Return a list of IDs for a given type. */
|
||||
virtual const Array<WinResourceID> getIDList(const WinResourceID &type) const = 0;
|
||||
|
||||
/** Return a list of languages for a given type and ID. */
|
||||
virtual const Array<WinResourceID> getLangList(const WinResourceID &type, const WinResourceID &id) const {
|
||||
Array<WinResourceID> array;
|
||||
return array;
|
||||
};
|
||||
|
||||
/** Return a stream to the specified resource, taking the first language found (or 0 if non-existent). */
|
||||
virtual File *getResource(const WinResourceID &type, const WinResourceID &id) = 0;
|
||||
|
||||
/** Return a stream to the specified resource (or 0 if non-existent). */
|
||||
virtual File *getResource(const WinResourceID &type, const WinResourceID &id, const WinResourceID &lang) {
|
||||
return getResource(type, id);
|
||||
}
|
||||
};
|
||||
|
||||
} // End of namespace Common
|
||||
|
||||
#endif
|
||||
|
|
|
@ -44,20 +44,6 @@ void PEResources::clear() {
|
|||
delete _exe; _exe = 0;
|
||||
}
|
||||
|
||||
bool PEResources::loadFromEXE(const String &fileName) {
|
||||
if (fileName.empty())
|
||||
return false;
|
||||
|
||||
File *file = new File();
|
||||
|
||||
if (!file->open(fileName.c_str())) {
|
||||
delete file;
|
||||
return false;
|
||||
}
|
||||
|
||||
return loadFromEXE(file);
|
||||
}
|
||||
|
||||
bool PEResources::loadFromEXE(File *stream) {
|
||||
clear();
|
||||
|
||||
|
|
|
@ -38,7 +38,7 @@ class SeekableReadStream;
|
|||
* A class able to load resources from a Windows Portable Executable, such
|
||||
* as cursors, bitmaps, and sounds.
|
||||
*/
|
||||
class PEResources {
|
||||
class PEResources : WinResources {
|
||||
public:
|
||||
PEResources();
|
||||
~PEResources();
|
||||
|
@ -47,7 +47,7 @@ public:
|
|||
void clear();
|
||||
|
||||
/** Load from an EXE file. */
|
||||
bool loadFromEXE(const String &fileName);
|
||||
using WinResources::loadFromEXE;
|
||||
|
||||
bool loadFromEXE(File *stream);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue