From 2a5f4add1cb311d01e2d1a8611db5900f7c0f0eb Mon Sep 17 00:00:00 2001 From: Antidote Date: Fri, 15 Feb 2013 20:30:51 -0800 Subject: [PATCH] * Get libzelda compiling --- include/BinaryWriter.hpp | 60 +++--- include/WiiSave.h | 6 +- src/BinaryWriter.cpp | 454 +++++++++++++++++++-------------------- 3 files changed, 259 insertions(+), 261 deletions(-) diff --git a/include/BinaryWriter.hpp b/include/BinaryWriter.hpp index 3faa971..8709b9e 100644 --- a/include/BinaryWriter.hpp +++ b/include/BinaryWriter.hpp @@ -12,11 +12,11 @@ // // You should have received a copy of the GNU General Public License // along with libZelda. If not, see -#ifndef __BINARYWRITER_HPP__ -#define __BINARYWRITER_HPP__ - -#include "Stream.hpp" -#include +#ifndef __BINARYWRITER_HPP__ +#define __BINARYWRITER_HPP__ + +#include "Stream.hpp" +#include /*! \class BinaryWriter * \brief A Stream class for writing binary data @@ -25,40 +25,40 @@ * all work is done using a memory buffer, and not written directly to the disk * this allows for fast, flexible code as well as the ability to quickly modify data * \sa Stream - */ -class BinaryWriter : public Stream -{ + */ +class BinaryWriter : public Stream +{ public: /*! \brief This constructor takes an existing buffer to write to. * * \param data The existing buffer * \param length The length of the existing buffer - */ + */ BinaryWriter(const Uint8* data, Uint64 length); /*! \brief This constructor takes an existing Stream to write to. * * \param stream The stream to write data to - */ + */ BinaryWriter(const Stream& stream); /*! \brief This constructor creates an instance from a file on disk. * * \param filename The file to create the stream from - */ + */ BinaryWriter(const std::string& filename); /*! \brief Saves the file to the specified file. * * \param filename If not empty, the filename to save to - */ - void save(const std::string& filename=""); + */ + void save(const std::string& filename=""); /*! \brief Writes an Int16 to the buffer and advances the buffer. * It also swaps the bytes depending on the platform and Stream settings. * * \sa Endian * \param val The value to write to the buffer - */ + */ void writeInt16(Int16 val); /*! \brief Writes an Uint16 to the buffer and advances the buffer. @@ -66,7 +66,7 @@ public: * * \sa Endian * \param val The value to write to the buffer - */ + */ void writeUInt16(Uint16); /*! \brief Writes an Int32 to the buffer and advances the buffer. @@ -74,7 +74,7 @@ public: * * \sa Endian * \param val The value to write to the buffer - */ + */ void writeInt32(Int32); /*! \brief Writes an Uint32 to the buffer and advances the buffer. @@ -82,7 +82,7 @@ public: * * \sa Endian * \param val The value to write to the buffer - */ + */ void writeUInt32(Uint32); /*! \brief Writes an Int64 to the buffer and advances the buffer. @@ -90,7 +90,7 @@ public: * * \sa Endian * \param val The value to write to the buffer - */ + */ void writeInt64(Int64); /*! \brief Writes an Uint64 to the buffer and advances the buffer. @@ -98,7 +98,7 @@ public: * * \sa Endian * \param val The value to write to the buffer - */ + */ void writeUInt64(Uint64); /*! \brief Writes an float to the buffer and advances the buffer. @@ -106,7 +106,7 @@ public: * * \sa Endian * \param val The value to write to the buffer - */ + */ void writeFloat(float); /*! \brief Writes an double to the buffer and advances the buffer. @@ -114,7 +114,7 @@ public: * * \sa Endian * \param val The value to write to the buffer - */ + */ void writeDouble(double); /*! \brief Writes an bool to the buffer and advances the buffer. @@ -122,7 +122,7 @@ public: * * \sa Endian * \param val The value to write to the buffer - */ + */ void writeBool(bool); /*! \brief Writes an unicode string to the buffer and advances the buffer. @@ -130,13 +130,13 @@ public: * * \sa Endian * \param str The string to write to the buffer - */ - void writeUnicode(const std::string& str); + */ + void writeUnicode(const std::string& str); protected: - Int8 readByte(); + Int8 readByte(); Int8* readBytes(Int64); - bool isOpenForReading(); - std::string m_filename; -}; - -#endif + bool isOpenForReading(); + std::string m_filepath; +}; + +#endif diff --git a/include/WiiSave.h b/include/WiiSave.h index 713eef7..14950c9 100644 --- a/include/WiiSave.h +++ b/include/WiiSave.h @@ -18,11 +18,9 @@ public: WiiSave(); virtual ~WiiSave(); - bool saveToFile(const std::string& filepath, Uint8* macAddress, Uint32 ngId, Uint8* ngPriv, Uint8* ngSig, Uint32 ngKeyId); - void addFile(const std::string& filename, WiiFile* file); - WiiFile* getFile(const std::string& filename) const; - std::unordered_map& getFileList(); + WiiFile* file(const std::string& filename) const; + std::unordered_map& fileList(); void setBanner(WiiBanner* banner); WiiBanner* banner() const; diff --git a/src/BinaryWriter.cpp b/src/BinaryWriter.cpp index d6a44bd..7457d81 100644 --- a/src/BinaryWriter.cpp +++ b/src/BinaryWriter.cpp @@ -13,49 +13,49 @@ // You should have received a copy of the GNU General Public License // along with libZelda. If not, see -#include "BinaryWriter.hpp" -#include "IOException.hpp" +#include "BinaryWriter.hpp" +#include "IOException.hpp" #include "FileNotFoundException.hpp" -#include "utility.hpp" -#include "utf8.h" - -#include -#include -#include +#include "utility.hpp" +#include "utf8.h" + +#include +#include +#include #include - -BinaryWriter::BinaryWriter(const Uint8* data, Uint64 length) - : Stream(data, length) -{} - -BinaryWriter::BinaryWriter(const Stream& stream) : - Stream(stream) -{} - -BinaryWriter::BinaryWriter(const std::string& filename) - : m_filename(filename) -{ - m_length = 0x10; + +BinaryWriter::BinaryWriter(const Uint8* data, Uint64 length) + : Stream(data, length) +{} + +BinaryWriter::BinaryWriter(const Stream& stream) : + Stream(stream) +{} + +BinaryWriter::BinaryWriter(const std::string& filename) + : m_filepath(filename) +{ + m_length = 0x10; m_bitPosition = 0; - m_position = 0; - m_data = new Uint8[m_length]; - if (!m_data) - throw IOException("Could not allocate memory!"); - memset(m_data, 0, m_length); -} - -void BinaryWriter::save(const std::string& filename) -{ - if (filename.empty() && m_filename.empty()) - throw Exception("InvalidOperationException: BinaryWriter::Save() -> No file specified, cannot save."); - - if (!filename.empty()) - m_filename = filename; - - FILE* out = fopen(m_filename.c_str(), "wb"); - if (!out) - throw FileNotFoundException(m_filename); + m_position = 0; + m_data = new Uint8[m_length]; + if (!m_data) + throw IOException("Could not allocate memory!"); + memset(m_data, 0, m_length); +} + +void BinaryWriter::save(const std::string& filename) +{ + if (filename.empty() && m_filepath.empty()) + throw Exception("InvalidOperationException: BinaryWriter::Save() -> No file specified, cannot save."); + + if (!filename.empty()) + m_filepath = filename; + + FILE* out = fopen(m_filepath.c_str(), "wb"); + if (!out) + throw FileNotFoundException(m_filepath); Uint32 done = 0; Uint32 blocksize = BLOCKSZ; @@ -73,203 +73,203 @@ void BinaryWriter::save(const std::string& filename) done += blocksize; std::cout << "Wrote " << done << " bytes" << std::endl; - }while (done < m_length); - - fclose(out); -} - -Int8 BinaryWriter::readByte() -{ - throw IOException("Stream not open for reading"); -} - -Int8* BinaryWriter::readBytes(Int64) -{ - throw IOException("Stream not open for reading"); -} - -void BinaryWriter::writeInt16(Int16 val) -{ - if (m_bitPosition > 0) - { - m_bitPosition = 0; - m_position += sizeof(Uint8); - } - - if (m_position + sizeof(Int16) > m_length && m_autoResize) - resize(m_position + sizeof(Int16)); - else if (m_position > m_length) - throw IOException("BinaryWriter::WriteInt16() -> Position outside stream bounds"); + }while (done < m_length); + + fclose(out); +} + +Int8 BinaryWriter::readByte() +{ + throw IOException("Stream not open for reading"); +} + +Int8* BinaryWriter::readBytes(Int64) +{ + throw IOException("Stream not open for reading"); +} + +void BinaryWriter::writeInt16(Int16 val) +{ + if (m_bitPosition > 0) + { + m_bitPosition = 0; + m_position += sizeof(Uint8); + } + + if (m_position + sizeof(Int16) > m_length && m_autoResize) + resize(m_position + sizeof(Int16)); + else if (m_position > m_length) + throw IOException("BinaryWriter::WriteInt16() -> Position outside stream bounds"); if ((!isSystemBigEndian() && m_endian == Stream::BigEndian) || (isSystemBigEndian() && m_endian == Stream::LittleEndian)) val = swap16(val); - - *(Int16*)(m_data + m_position) = val; - m_position += sizeof(Int16); -} - -void BinaryWriter::writeUInt16(Uint16 val) -{ - if (m_bitPosition > 0) - { - m_bitPosition = 0; - m_position += sizeof(Uint8); - } - - if (m_position + sizeof(Uint16) > m_length && m_autoResize) - resize(m_position + sizeof(Uint16)); - else if (m_position > m_length) - throw IOException("BinaryWriter::WriteUInt16() -> Position outside stream bounds"); - + + *(Int16*)(m_data + m_position) = val; + m_position += sizeof(Int16); +} + +void BinaryWriter::writeUInt16(Uint16 val) +{ + if (m_bitPosition > 0) + { + m_bitPosition = 0; + m_position += sizeof(Uint8); + } + + if (m_position + sizeof(Uint16) > m_length && m_autoResize) + resize(m_position + sizeof(Uint16)); + else if (m_position > m_length) + throw IOException("BinaryWriter::WriteUInt16() -> Position outside stream bounds"); + if ((!isSystemBigEndian() && m_endian == Stream::BigEndian) || (isSystemBigEndian() && m_endian == Stream::LittleEndian)) val = swapU16(val); - - *(Uint16*)(m_data + m_position) = val; - m_position += sizeof(Uint16); -} - -void BinaryWriter::writeInt32(Int32 val) -{ - if (m_bitPosition > 0) - { - m_bitPosition = 0; - m_position += sizeof(Uint8); - } - - if (m_position + sizeof(Int32) > m_length && m_autoResize) - resize(m_position + sizeof(Int32)); - else if (m_position > m_length) - throw IOException("BinaryWriter::WriteInt32() -> Position outside stream bounds"); - if ((!isSystemBigEndian() && m_endian == Stream::BigEndian) || (isSystemBigEndian() && m_endian == Stream::LittleEndian)) - val = swap32(val); - - *(Int32*)(m_data + m_position) = val; - m_position += sizeof(Int32); -} - -void BinaryWriter::writeUInt32(Uint32 val) -{ - if (m_bitPosition > 0) - { - m_bitPosition = 0; - m_position += sizeof(Uint8); - } - - if (m_position + sizeof(Uint32) > m_length && m_autoResize) - resize(m_position + sizeof(Uint32)); - else if (m_position > m_length) - throw IOException("BinaryWriter::WriteUInt32() -> Position outside stream bounds"); - - if ((!isSystemBigEndian() && m_endian == Stream::BigEndian) || (isSystemBigEndian() && m_endian == Stream::LittleEndian)) - val = swap32(val); - - *(Uint32*)(m_data + m_position) = val; - m_position += sizeof(Uint32); -} - -void BinaryWriter::writeInt64(Int64 val) -{ - if (m_bitPosition > 0) - { - m_bitPosition = 0; - m_position += sizeof(Uint8); - } - - if (m_position + sizeof(Int64) > m_length && m_autoResize) - resize(m_position + sizeof(Int64)); - else if (m_position > m_length) - throw IOException("BinaryWriter::WriteInt64() -> Position outside stream bounds"); - + *(Uint16*)(m_data + m_position) = val; + m_position += sizeof(Uint16); +} - if ((!isSystemBigEndian() && m_endian == Stream::BigEndian) || (isSystemBigEndian() && m_endian == Stream::LittleEndian)) - val = swap64(val); - - *(Int64*)(m_data + m_position) = val; - m_position += sizeof(Int64); -} - -void BinaryWriter::writeUInt64(Uint64 val) +void BinaryWriter::writeInt32(Int32 val) { - if (m_bitPosition > 0) - { - m_bitPosition = 0; - m_position += sizeof(Uint8); - } - - if (m_position + sizeof(Uint64) > m_length && m_autoResize) - resize(m_position + sizeof(Uint64)); - else if (m_position > m_length) - throw IOException("BinaryWriter::WriteUInt64() -> Position outside stream bounds"); + if (m_bitPosition > 0) + { + m_bitPosition = 0; + m_position += sizeof(Uint8); + } + + if (m_position + sizeof(Int32) > m_length && m_autoResize) + resize(m_position + sizeof(Int32)); + else if (m_position > m_length) + throw IOException("BinaryWriter::WriteInt32() -> Position outside stream bounds"); + + if ((!isSystemBigEndian() && m_endian == Stream::BigEndian) || (isSystemBigEndian() && m_endian == Stream::LittleEndian)) + val = swap32(val); + + *(Int32*)(m_data + m_position) = val; + m_position += sizeof(Int32); +} + +void BinaryWriter::writeUInt32(Uint32 val) +{ + if (m_bitPosition > 0) + { + m_bitPosition = 0; + m_position += sizeof(Uint8); + } + + if (m_position + sizeof(Uint32) > m_length && m_autoResize) + resize(m_position + sizeof(Uint32)); + else if (m_position > m_length) + throw IOException("BinaryWriter::WriteUInt32() -> Position outside stream bounds"); + + if ((!isSystemBigEndian() && m_endian == Stream::BigEndian) || (isSystemBigEndian() && m_endian == Stream::LittleEndian)) + val = swap32(val); + + *(Uint32*)(m_data + m_position) = val; + m_position += sizeof(Uint32); +} + +void BinaryWriter::writeInt64(Int64 val) +{ + if (m_bitPosition > 0) + { + m_bitPosition = 0; + m_position += sizeof(Uint8); + } + + if (m_position + sizeof(Int64) > m_length && m_autoResize) + resize(m_position + sizeof(Int64)); + else if (m_position > m_length) + throw IOException("BinaryWriter::WriteInt64() -> Position outside stream bounds"); + if ((!isSystemBigEndian() && m_endian == Stream::BigEndian) || (isSystemBigEndian() && m_endian == Stream::LittleEndian)) val = swap64(val); - - *(Uint64*)(m_data + m_position) = val; - m_position += sizeof(Uint64); -} - -void BinaryWriter::writeFloat(float val) -{ - if (m_bitPosition > 0) - { - m_bitPosition = 0; - m_position += sizeof(Uint8); - } - - if (m_position + sizeof(float) > m_length && m_autoResize) - resize(m_position + sizeof(float)); - else if (m_position > m_length) - throw IOException("BinaryWriter::WriteFloat() -> Position outside stream bounds"); - if ((!isSystemBigEndian() && m_endian == Stream::BigEndian) || (isSystemBigEndian() && m_endian == Stream::LittleEndian)) + *(Int64*)(m_data + m_position) = val; + m_position += sizeof(Int64); +} + +void BinaryWriter::writeUInt64(Uint64 val) +{ + if (m_bitPosition > 0) + { + m_bitPosition = 0; + m_position += sizeof(Uint8); + } + + if (m_position + sizeof(Uint64) > m_length && m_autoResize) + resize(m_position + sizeof(Uint64)); + else if (m_position > m_length) + throw IOException("BinaryWriter::WriteUInt64() -> Position outside stream bounds"); + + if ((!isSystemBigEndian() && m_endian == Stream::BigEndian) || (isSystemBigEndian() && m_endian == Stream::LittleEndian)) + val = swap64(val); + + *(Uint64*)(m_data + m_position) = val; + m_position += sizeof(Uint64); +} + +void BinaryWriter::writeFloat(float val) +{ + if (m_bitPosition > 0) + { + m_bitPosition = 0; + m_position += sizeof(Uint8); + } + + if (m_position + sizeof(float) > m_length && m_autoResize) + resize(m_position + sizeof(float)); + else if (m_position > m_length) + throw IOException("BinaryWriter::WriteFloat() -> Position outside stream bounds"); + + if ((!isSystemBigEndian() && m_endian == Stream::BigEndian) || (isSystemBigEndian() && m_endian == Stream::LittleEndian)) val = swapFloat(val); - - *(float*)(m_data + m_position) = val; - m_position += sizeof(float); -} - -void BinaryWriter::writeDouble(double val) -{ - if (m_bitPosition > 0) - { - m_bitPosition = 0; - m_position += sizeof(Uint8); - } - - if (m_position + sizeof(double) > m_length && m_autoResize) - resize(m_position + sizeof(double)); - else if (m_position > m_length) - throw IOException("BinaryWriter::WriteDouble() -> Position outside stream bounds"); - if ((!isSystemBigEndian() && m_endian == Stream::BigEndian) || (isSystemBigEndian() && m_endian == Stream::LittleEndian)) - val = swapDouble(val); - - *(double*)(m_data + m_position)= val; - m_position += sizeof(double); -} - -void BinaryWriter::writeBool(bool val) + *(float*)(m_data + m_position) = val; + m_position += sizeof(float); +} + +void BinaryWriter::writeDouble(double val) { - if (m_bitPosition > 0) - { - m_bitPosition = 0; - m_position += sizeof(Uint8); - } - - if (m_position + sizeof(bool) > m_length && m_autoResize) - resize(m_position + sizeof(bool)); - else if (m_position > m_length) + if (m_bitPosition > 0) + { + m_bitPosition = 0; + m_position += sizeof(Uint8); + } + + if (m_position + sizeof(double) > m_length && m_autoResize) + resize(m_position + sizeof(double)); + else if (m_position > m_length) + throw IOException("BinaryWriter::WriteDouble() -> Position outside stream bounds"); + + if ((!isSystemBigEndian() && m_endian == Stream::BigEndian) || (isSystemBigEndian() && m_endian == Stream::LittleEndian)) + val = swapDouble(val); + + *(double*)(m_data + m_position)= val; + m_position += sizeof(double); +} + +void BinaryWriter::writeBool(bool val) +{ + if (m_bitPosition > 0) + { + m_bitPosition = 0; + m_position += sizeof(Uint8); + } + + if (m_position + sizeof(bool) > m_length && m_autoResize) + resize(m_position + sizeof(bool)); + else if (m_position > m_length) throw IOException("BinaryWriter::WriteBool() -> Position outside stream bounds"); - - - *(bool*)(m_data + m_position) = val; - m_position += sizeof(bool); -} - -void BinaryWriter::writeUnicode(const std::string& str) -{ + + + *(bool*)(m_data + m_position) = val; + m_position += sizeof(bool); +} + +void BinaryWriter::writeUnicode(const std::string& str) +{ std::string tmpStr = "\xEF\xBB\xBF" + str; std::vector tmp; @@ -280,11 +280,11 @@ void BinaryWriter::writeUnicode(const std::string& str) { if (chr != 0xFEFF) writeInt16(chr); - } -} - -bool BinaryWriter::isOpenForReading() -{ - return false; -} - + } +} + +bool BinaryWriter::isOpenForReading() +{ + return false; +} +