* Fix botched commit (forgot -a)

This commit is contained in:
Antidote
2013-07-20 20:57:20 -07:00
parent 8f550d3a5e
commit e86d64e7bb
56 changed files with 3213 additions and 1741 deletions

View File

@@ -16,6 +16,8 @@
#ifndef __ALTTP_ENUMS_HPP__
#define __ALTTP_ENUMS_HPP__
#ifndef __DOXYGEN_IGNORE__
#include "Types.hpp"
enum BowType : char
@@ -103,4 +105,5 @@ enum ALTTPTagAlong
AfterBoss
};
#endif // __DOXYGEN_IGNORE__
#endif // __ALTTP_ENUMS_HPP__

View File

@@ -19,10 +19,13 @@
#include <Types.hpp>
#include <vector>
namespace zelda
{
class ALTTPQuest;
/*! \class ALTTPFile
* \brief A Link to the Past data container class
* \brief A Link to the Past data container class class
*
* Contains all relevant data for an A Link to the Past
* SRM file.
@@ -84,4 +87,5 @@ private:
std::vector<ALTTPQuest*> m_backup;
};
} // zelda
#endif // __ALTTP_FILE_HPP__

View File

@@ -21,14 +21,38 @@
#include "BinaryReader.hpp"
#include "ALTTPQuest.hpp"
namespace zelda
{
/*! \class ALTTPFileReader
* \brief A Link to the Past save data reader class
*
* A Class for reading binary data from an ALTTP Save File,
* all work is done using a memory buffer, and not read directly from the disk.
* \sa BinaryReader
*/
class ALTTPFile;
class ALTTPFileReader : public BinaryReader
class ALTTPFileReader : public io::BinaryReader
{
public:
/*! \brief This constructor takes an existing buffer to read from.
*
* \param data The existing buffer
* \param length The length of the existing buffer
*/
ALTTPFileReader(Uint8*, Uint64);
/*! \brief This constructor creates an instance from a file on disk.
*
* \param filename The file to create the stream from
*/
ALTTPFileReader(const std::string&);
/*! \brief Reads the SRAM data from the buffer
*
* \return ALTTPFile* SRAM data
*/
ALTTPFile* readFile();
private:
ALTTPRoomFlags* readRoomFlags();
@@ -36,4 +60,5 @@ private:
ALTTPDungeonItemFlags readDungeonFlags();
};
} // zelda
#endif // __ALTTP_FILE_READER_HPP__

View File

@@ -21,15 +21,40 @@
#include <BinaryWriter.hpp>
#include "ALTTPQuest.hpp"
namespace zelda
{
class ALTTPFile;
class ALTTPFileWriter : public BinaryWriter
/*! \class ALTTPFileWriter
* \brief A Link to the Past save data writer class
*
* A Class for writing binary data to an ALTTP Save File,
* all work is done using a memory buffer, and not written directly to the disk.
* \sa BinaryReader
*/
class ALTTPFileWriter : public io::BinaryWriter
{
public:
/*! \brief This constructor takes an existing buffer to write to.
*
* \param data The existing buffer
* \param length The length of the existing buffer
*/
ALTTPFileWriter(Uint8*, Uint64);
/*! \brief This constructor creates an instance from a file on disk.
*
* \param filename The file to create the stream from
*/
ALTTPFileWriter(const std::string&);
/*! \brief Writes the given SRAM data to a file on disk
*
* \param file SRAM data to right
*/
void writeFile(ALTTPFile* file);
private:
void writeRoomFlags(ALTTPRoomFlags*);
void writeOverworldEvent(ALTTPOverworldEvent*);
@@ -37,4 +62,6 @@ private:
Uint16 calculateChecksum(Uint32 game);
};
} // zelda
#endif // __ALTTP_FILE_WRITER_HPP__

View File

@@ -22,6 +22,9 @@
#include "ALTTPStructs.hpp"
#include "ALTTPEnums.hpp"
namespace zelda
{
/*! \class ALTTPQuest
* \brief A Link to the Past Quest container class
*
@@ -31,146 +34,607 @@
class ALTTPQuest
{
public:
/*!
* \brief ALTTPQuest
*/
ALTTPQuest();
~ALTTPQuest();
/*!
* \brief setRoomFlags
* \param flags
*/
void setRoomFlags(std::vector<ALTTPRoomFlags*> flags);
/*!
* \brief setRoomFlags
* \param rf
* \param id
*/
void setRoomFlags(ALTTPRoomFlags* rf, Uint32 id);
/*!
* \brief roomFlags
* \return
*/
std::vector<ALTTPRoomFlags*> roomFlags();
/*!
* \brief roomFlags
* \param id
* \return
*/
ALTTPRoomFlags* roomFlags(Uint32 id);
/*!
* \brief setOverworldEvents
* \param events
*/
void setOverworldEvents(std::vector<ALTTPOverworldEvent*> events);
/*!
* \brief setOverworldEvents
* \param ow
* \param id
*/
void setOverworldEvents(ALTTPOverworldEvent* ow, Uint32 id);
/*!
* \brief overworldEvents
* \return
*/
std::vector<ALTTPOverworldEvent*> overworldEvents() const;
/*!
* \brief overworldEvent
* \param id
* \return
*/
ALTTPOverworldEvent* overworldEvent(Uint32 id) const;
/*!
* \brief setInventory
* \param inv
*/
void setInventory(ALTTPInventory* inv);
/*!
* \brief inventory
* \return
*/
ALTTPInventory* inventory() const;
/*!
* \brief setRupeeMax
* \param val
*/
void setRupeeMax(Uint16 val);
/*!
* \brief rupeeMax
* \return
*/
Uint16 rupeeMax() const;
/*!
* \brief setRupeeCurrent
* \param val
*/
void setRupeeCurrent(Uint16 val);
/*!
* \brief rupeeCurrent
* \return
*/
Uint16 rupeeCurrent() const;
/*!
* \brief setCompasses
* \param flags
*/
void setCompasses(ALTTPDungeonItemFlags flags);
/*!
* \brief compasses
* \return
*/
ALTTPDungeonItemFlags compasses() const;
/*!
* \brief setBigKeys
* \param flags
*/
void setBigKeys(ALTTPDungeonItemFlags flags);
/*!
* \brief bigKeys
* \return
*/
ALTTPDungeonItemFlags bigKeys() const;
/*!
* \brief setDungeonMaps
* \param flags
*/
void setDungeonMaps(ALTTPDungeonItemFlags flags);
/*!
* \brief dungeonMaps
* \return
*/
ALTTPDungeonItemFlags dungeonMaps() const;
/*!
* \brief setWishingPond
* \param val
*/
void setWishingPond(Uint16 val);
/*!
* \brief wishingPond
* \return
*/
Uint16 wishingPond() const;
/*!
* \brief setHealthMax
* \param val
*/
void setHealthMax(Uint8 val);
/*!
* \brief healthMax
* \return
*/
Uint8 healthMax() const;
/*!
* \brief setHealth
* \param val
*/
void setHealth(Uint8 val);
/*!
* \brief health
* \return
*/
Uint8 health() const;
/*!
* \brief setMagicPower
* \param val
*/
void setMagicPower(Uint8 val);
/*!
* \brief magicPower
* \return
*/
Uint8 magicPower() const;
/*!
* \brief setKeys
* \param val
*/
void setKeys(Uint8 val);
/*!
* \brief keys
* \return
*/
Uint8 keys() const;
/*!
* \brief setBombUpgrades
* \param val
*/
void setBombUpgrades(Uint8 val);
/*!
* \brief bombUpgrades
* \return
*/
Uint8 bombUpgrades() const;
/*!
* \brief setArrowUpgrades
* \param val
*/
void setArrowUpgrades(Uint8 val);
/*!
* \brief arrowUpgrades
* \return
*/
Uint8 arrowUpgrades() const;
/*!
* \brief setHealthFiller
* \param val
*/
void setHealthFiller(Uint8 val);
/*!
* \brief healthFiller
* \return
*/
Uint8 healthFiller() const;
/*!
* \brief setMagicFiller
* \param val
*/
void setMagicFiller(Uint8 val);
/*!
* \brief magicFiller
* \return
*/
Uint8 magicFiller() const;
/*!
* \brief setPendants
* \param val
*/
void setPendants(ALTTPPendants val);
/*!
* \brief pendants
* \return
*/
ALTTPPendants pendants() const;
/*!
* \brief setBombFiller
* \param val
*/
void setBombFiller(Uint8 val);
/*!
* \brief bombFiller
* \return
*/
Uint8 bombFiller() const;
/*!
* \brief setArrowFiller
* \param val
*/
void setArrowFiller(Uint8 val);
/*!
* \brief arrowFiller
* \return
*/
Uint8 arrowFiller() const;
/*!
* \brief setArrows
* \param val
*/
void setArrows(Uint8 val);
/*!
* \brief arrows
* \return
*/
Uint8 arrows() const;
/*!
* \brief setAbilityFlags
* \param val
*/
void setAbilityFlags(ALTTPAbilities val);
/*!
* \brief abilityFlags
* \return
*/
ALTTPAbilities abilityFlags() const;
void setCrystals(ALTTPCrystals val);
/*!
* \brief setCrystals
* \param val
*/
void setCrystals(ALTTPCrystals val);\
/*!
* \brief crystals
* \return
*/
ALTTPCrystals crystals() const;
/*!
* \brief setMagicUsage
* \param val
*/
void setMagicUsage(ALTTPMagicUsage val);
/*!
* \brief magicUsage
* \return
*/
ALTTPMagicUsage magicUsage() const;
/*!
* \brief setDungeonKeys
* \param val
*/
void setDungeonKeys(std::vector<Uint8> val);
/*!
* \brief setDungeonKeys
* \param id
* \param val
*/
void setDungeonKeys(Uint32 id, Uint8 val);
/*!
* \brief dungeonKeys
* \param id
* \return
*/
Uint8 dungeonKeys(Uint32 id) const;
/*!
* \brief dungeonCount
* \return
*/
Uint32 dungeonCount() const;
/*!
* \brief setProgressIndicator
* \param val
*/
void setProgressIndicator(ALTTPProgressIndicator val);
/*!
* \brief progressIndicator
* \return
*/
ALTTPProgressIndicator progressIndicator() const;
/*!
* \brief setProgressFlags1
* \param val
*/
void setProgressFlags1(ALTTPProgressFlags1 val);
/*!
* \brief progressFlags1
* \return
*/
ALTTPProgressFlags1 progressFlags1() const;
/*!
* \brief setMapIcon
* \param val
*/
void setMapIcon(ALTTPMapIcon val);
/*!
* \brief mapIcon
* \return
*/
ALTTPMapIcon mapIcon() const;
/*!
* \brief setStartLocation
* \param val
*/
void setStartLocation(ALTTPStartLocation val);
/*!
* \brief startLocation
* \return
*/
ALTTPStartLocation startLocation() const;
/*!
* \brief setProgressFlags2
* \param val
*/
void setProgressFlags2(ALTTPProgressFlags2 val);
/*!
* \brief progressFlags2
* \return
*/
ALTTPProgressFlags2 progressFlags2() const;
/*!
* \brief setLightDarkWorldIndicator
* \param val
*/
void setLightDarkWorldIndicator(ALTTPLightDarkWorldIndicator val);
/*!
* \brief lightDarkWorldIndicator
* \return
*/
ALTTPLightDarkWorldIndicator lightDarkWorldIndicator() const;
/*!
* \brief setTagAlong
* \param val
*/
void setTagAlong(ALTTPTagAlong val);
/*!
* \brief tagAlong
* \return
*/
ALTTPTagAlong tagAlong() const;
/*!
* \brief setOldManFlags
* \param flags
*/
void setOldManFlags(std::vector<Uint8> flags);
/*!
* \brief setOldManFlag
* \param id
* \param val
*/
void setOldManFlag(Uint32 id, Uint8 val);
/*!
* \brief oldManFlag
* \param id
* \return
*/
Uint8 oldManFlag(Uint32 id);
/*!
* \brief oldManFlagCount
* \return
*/
Uint32 oldManFlagCount() const;
/*!
* \brief setBombFlag
* \param flag
*/
void setBombFlag(Uint8 flag);
/*!
* \brief bombFlag
* \return
*/
Uint8 bombFlag() const;
/*!
* \brief setUnknown1
* \param flags
*/
void setUnknown1(std::vector<Uint8> flags);
/*!
* \brief setUnknown1
* \param id
* \param val
*/
void setUnknown1(Uint32 id, Uint8 val);
/*!
* \brief unknown1
* \param id
* \return
*/
Uint8 unknown1(Uint32 id);
/*!
* \brief unknown1Count
* \return
*/
Uint32 unknown1Count() const;
/*!
* \brief setPlayerName
* \param playerName
*/
void setPlayerName(std::vector<Uint16> playerName);
/*!
* \brief setPlayerName
* \param playerName
*/
void setPlayerName(const std::string& playerName);
/*!
* \brief playerName
* \return
*/
std::vector<Uint16> playerName() const;
/*!
* \brief playerNameToString
* \return
*/
std::string playerNameToString() const;
/*!
* \brief setValid
* \param val
*/
void setValid(bool val);
/*!
* \brief valid
* \return
*/
bool valid();
/*!
* \brief setDungeonDeathTotals
* \param val
*/
void setDungeonDeathTotals(std::vector<Uint16> val);
/*!
* \brief setDungeonDeathTotal
* \param id
* \param val
*/
void setDungeonDeathTotal(Uint32 id, Uint16 val);
/*!
* \brief dungeonDeathTotal
* \param id
* \return
*/
Uint16 dungeonDeathTotal(Uint32 id) const;
/*!
* \brief dungeonDeathTotalCount
* \return
*/
Uint16 dungeonDeathTotalCount() const;
/*!
* \brief setUnknown2
* \param val
*/
void setUnknown2(Uint16 val);
/*!
* \brief unknown2
* \return
*/
Uint16 unknown2() const;
/*!
* \brief setDeathSaveCount
* \param val
*/
void setDeathSaveCount(Uint16 val);
/*!
* \brief deathSaveCount
* \return
*/
Uint16 deathSaveCount() const;
/*!
* \brief setPostGameDeathCounter
* \param val
*/
void setPostGameDeathCounter(Int16 val);
/*!
* \brief postGameDeathCounter
* \return
*/
Int16 postGameDeathCounter() const;
/*!
* \brief setChecksum
* \param checksum
*/
void setChecksum(Uint16 checksum);
/*!
* \brief checksum
* \return
*/
Uint16 checksum() const;
private:
std::vector<ALTTPRoomFlags*> m_roomFlags;
@@ -217,5 +681,6 @@ private:
Uint16 m_checksum;
};
} // zelda
#endif // __ALTTP_QUEST_HPP__

View File

@@ -16,11 +16,14 @@
#ifndef __ALTTP_STRUCTS_HPP__
#define __ALTTP_STRUCTS_HPP__
#ifndef __DOXYGEN_IGNORE__
#include <string>
#include "Types.hpp"
/*! \struct ALTTPRoomFlags
*/
namespace zelda
{
struct ALTTPRoomFlags
{
bool Chest1:1;
@@ -41,8 +44,6 @@ struct ALTTPRoomFlags
bool ChestOrTile:1;
};
/*! \struct ALTTPOverworldEvent
*/
struct ALTTPOverworldEvent
{
bool Unused1:1;
@@ -55,8 +56,6 @@ struct ALTTPOverworldEvent
bool Unused5:1;
};
/*! \struct ALTTPInventory
*/
struct ALTTPInventory
{
char Bow;
@@ -88,12 +87,6 @@ struct ALTTPInventory
char Shield;
char Armor;
char BottleTypes[4];
std::string bowType();
std::string boomerangType();
std::string magicType();
std::string armorType();
std::string bottleType(Uint32);
};
/*! \struct ALTTPLightDarkWorldIndicator
@@ -204,4 +197,7 @@ struct ALTTPProgressFlags2
bool SmithsHaveSword:1;
};
}
#endif // __DOXYGEN_IGNORE__
#endif // __ALTTP_STRUCTS_HPP__

View File

@@ -12,12 +12,17 @@
//
// You should have received a copy of the GNU General Public License
// along with libZelda. If not, see <http://www.gnu.org/licenses/>
#ifndef __BINARYREADER_HPP__
#define __BINARYREADER_HPP__
#include "Stream.hpp"
#include <string>
namespace zelda
{
namespace io
{
/*! \class BinaryReader
* \brief A Stream class for reading binary data
*
@@ -139,6 +144,13 @@ public:
*/
std::string readUnicode();
/*! \brief Reads a string and advances the position in the file
*
* \return std::string The value at the current address
* \throw IOException when address is out of range
*/
std::string readString();
protected:
/*! \brief Overload of isOpenForWriting in Stream
*
@@ -155,7 +167,14 @@ protected:
* \throw IOException
*/
void writeBytes(Int8*, Int64);
std::string m_filepath;
std::string m_filepath; //!< Path to the target file
};
}
}
#endif
#ifndef BINARYREADER_BASE
#define BINARYREADER_BASE \
private: \
typedef zelda::io::BinaryReader base;
#endif // BINARYREADER_BASE
#endif // __BINARYREADER_HPP__

View File

@@ -19,6 +19,11 @@
#include "Stream.hpp"
#include <string>
namespace zelda
{
namespace io
{
/*! \class BinaryWriter
* \brief A Stream class for writing binary data
*
@@ -136,8 +141,15 @@ public:
protected:
Int8 readByte();
Int8* readBytes(Int64);
bool isOpenForReading();
std::string m_filepath;
bool isOpenForReading(); //!< Overridden from \sa Stream
std::string m_filepath; //!< Path to the target file
};
}
}
#endif
#ifndef BINARYWRITER_BASE
#define BINARYWRITER_BASE \
private: \
typedef zelda::io::BinaryWriter base;
#endif // BINARYWRITER_BASE
#endif // __BINARY_WRITER_HPP__

View File

@@ -18,6 +18,9 @@
#include <string>
namespace zelda
{
/*! \class Exception
* \brief The baseclass for all Exceptions.
*
@@ -33,7 +36,7 @@ public:
inline Exception(const std::string& message) :
m_message(message)
{
};
}
/*! \brief Returns the Error message of the exception
* \return std::string The error message
@@ -41,9 +44,10 @@ public:
inline std::string message() const
{
return m_message;
};
}
protected:
std::string m_message;
std::string m_message; //!< The error message string
};
} // zelda
#endif

View File

@@ -18,6 +18,9 @@
#include "Exception.hpp"
namespace zelda
{
/*! \class FileNotFoundException
* \brief An excpeption thrown when a file could not be found at the given path.
*
@@ -45,4 +48,6 @@ private:
std::string m_filename;
};
} // zelda
#endif

View File

@@ -13,11 +13,14 @@
// You should have received a copy of the GNU General Public License
// along with libZelda. If not, see <http://www.gnu.org/licenses/>
#ifndef __IOEXCEPTION_HPP__
#define __IOEXCEPTION_HPP__
#include "Exception.hpp"
#ifndef __IOEXCEPTION_HPP__
#define __IOEXCEPTION_HPP__
#include "Exception.hpp"
namespace zelda
{
/*! \class IOException
* \brief An excpeption thrown on inappropriate IO calls.
@@ -28,16 +31,17 @@
* <br />
* It is <b>NOT</b> appropriate to use <b>throw new</b> so avoid doing so,
* keeping things on the stack as much as possible is very important for speed.
*/
class IOException : public Exception
{
*/
class IOException : public Exception
{
public:
/*! \brief The constructor for an IOException
* \param message The error message to throw
*/
IOException(const std::string& message) :
Exception("IOException: " + message)
{};
};
#endif
*/
IOException(const std::string& message) :
Exception("IOException: " + message)
{};
};
} // zelda
#endif

View File

@@ -17,7 +17,10 @@
#define __INVALID_OPERATION_EXCEPTION_HPP__
#include <string>
#include <Exception.hpp>
#include "Exception.hpp"
namespace zelda
{
/*! \class InvalidOperationException
* \brief An excpeption thrown on Invalid Operations calls.
@@ -39,4 +42,7 @@ public:
{
}
};
} // zelda
#endif // __INVALID_OPERATION_EXCEPTION_HPP__

36
include/MCFile.hpp Normal file
View File

@@ -0,0 +1,36 @@
// This file is part of libZelda.
//
// libZelda is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// libZelda is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libZelda. If not, see <http://www.gnu.org/licenses/>
#ifndef __MCFILE_HPP__
#define __MCFILE_HPP__
namespace zelda
{
/*! \class MCFile
* \brief The Minish Cap data container class class
*
* Contains all relevant data for a The Minish Cap save,
* file.
*/
class MCFile
{
public:
MCFile();
private:
};
} // zelda
#endif // __MCFILE_HPP__

62
include/MCFileReader.hpp Normal file
View File

@@ -0,0 +1,62 @@
// This file is part of libZelda.
//
// libZelda is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// libZelda is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libZelda. If not, see <http://www.gnu.org/licenses/>
#ifndef __MCFILEREADER_HPP__
#define __MCFILEREADER_HPP__
#include "Types.hpp"
#include "BinaryReader.hpp"
namespace zelda
{
class MCFile;
/*! \class MCFileReader
* \brief The Minish Cap Save save data reader class
*
* A Class for reading binary data from a The Minish Cap Save File,
* all work is done using a memory buffer, and not read directly from the disk.
* \sa BinaryReader
*/
class MCFileReader : public io::BinaryReader
{
public:
/*!
* \brief This constructor takes an existing buffer to read from.
*
* \param data The existing buffer
* \param length The length of the existing buffer
*/
MCFileReader(Uint8*, Uint64);
/*!
* \brief This constructor creates an instance from a file on disk.
*
* \param filename The file to create the stream from
*/
MCFileReader(const std::string&);
/*!
* \brief Reads the save data from the buffer
*
* \return MCFile* SRAM data
*/
MCFile* readFile();
};
} // zelda
#endif // __MCFILEREADER_HPP__

68
include/MCFileWriter.hpp Normal file
View File

@@ -0,0 +1,68 @@
// This file is part of libZelda.
//
// libZelda is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// libZelda is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libZelda. If not, see <http://www.gnu.org/licenses/>
#ifndef __MCFILEWRITER_HPP__
#define __MCFILEWRITER_HPP__
#include "Types.hpp"
#include "BinaryWriter.hpp"
namespace zelda
{
class MCFile;
/*! \class MCFileWriter
* \brief The Minish Cap Save save data writer class
*
* A Class for writing binary data to a The Minish Cap Save File,
* all work is done using a memory buffer, and not written directly from the disk.
* \sa BinaryWriter
*/
class MCFileWriter : public io::BinaryWriter
{
public:
/*!
* \brief This constructor takes an existing buffer to write to.
*
* \param data The existing buffer
* \param length The length of the existing buffer
*/
MCFileWriter(Uint8*, Uint64);
/*!
* \brief This constructor creates an instance from a file on disk.
*
* \param filename The file to create the stream from
*/
MCFileWriter(const std::string&);
/*!
* \brief Writes the given save data to a file on disk
*
* \param file Save data to write
*/
void writeFile(MCFile* file);
private:
Uint16 calculateSlotChecksum(Uint32 game);
Uint16 calculateChecksum(Uint8* data, Uint32 length);
Uint8* reverse(Uint8* data, Uint32 length);
void unscramble();
};
} // zelda
#endif // __MCFILEWRITER_HPP__

View File

@@ -12,6 +12,7 @@
//
// You should have received a copy of the GNU General Public License
// along with libZelda. If not, see <http://www.gnu.org/licenses/>
#ifndef __MAINPAGE_HPP__
#define __MAINPAGE_HPP__
@@ -22,7 +23,7 @@
* <br />
* libZelda provides several basic classes that can be used to read from
* and write to files, and memory, classes such as Stream, BinaryReader, BinaryWriter,
* and the currently work in progress TextStream.
* and TextStream.
* \section example_sec BinaryWriter example
* \code
* #include "BinaryWriter.hpp"

View File

@@ -17,6 +17,11 @@
#include "Types.hpp"
namespace zelda
{
namespace io
{
/*! \class Stream
* \brief Stream is the main class all streams inherit from
*
@@ -35,17 +40,6 @@ public:
//! \brief Default buffer block size.
static const Uint32 BLOCKSZ;
/*! \enum Endian
* \brief Allows the user to specify the Endianness of the stream buffer.<br />
* The proper actions are automatically taken depending on platform and
* buffer settings
*/
enum Endian
{
LittleEndian, //!< Specifies that the Stream is Little Endian (LSB)
BigEndian //!< Specifies that the Stream is Big Endian (MSB)
};
/*! \enum SeekOrigin
* \brief Specifies how to seek in a stream.
*/
@@ -56,9 +50,8 @@ public:
End //!< Tells the Stream to seek from the End of the buffer.
};
/*! \brief The default constructor
*/
*/
Stream();
/*! \brief This constructor takes an existing buffer to read from.
*
@@ -87,13 +80,27 @@ public:
* \throw IOException
*/
virtual void writeBit(bool val);
/*! \brief Writes a byte at the current position and advances the position by one byte.
* \param byte The value to write
* \throw IOException
*/
virtual void writeUByte(Uint8 byte);
/*! \brief Writes a byte at the current position and advances the position by one byte.
* \param byte The value to write
* \throw IOException
*/
virtual void writeByte(Int8 byte);
/*! \brief Writes the given buffer with the specified length, buffers can be bigger than the length
* however it's undefined behavior to try and write a buffer which is smaller than the given length.
*
* \param data The buffer to write
* \param length The amount to write
* \throw IOException
*/
virtual void writeUBytes(Uint8* data, Int64 length);
/*! \brief Writes the given buffer with the specified length, buffers can be bigger than the length
* however it's undefined behavior to try and write a buffer which is smaller than the given length.
*
@@ -212,7 +219,7 @@ public:
*
* \return Endian The current Stream Endianess
*/
Endian endianness() const;
Endian endian() const;
/*! \brief Returns whether the stream is BigEndian
@@ -235,5 +242,6 @@ protected:
Uint8* m_data; //!< The Stream buffer
bool m_autoResize; //!< Whether the stream is autoresizing
};
} // io
} // zelda
#endif // __STREAM_HPP__

View File

@@ -20,6 +20,10 @@
#include <vector>
namespace zelda
{
namespace io
{
// TODO (Phil#1#): Need to actually use AccessMode
/*! \class TextStream
* \brief A Class for reading or writing Text data.
@@ -122,28 +126,28 @@ public:
*
* \param mode The mode to set.
*/
void setAccessMode(AccessMode mode);
void setAccessMode(AccessMode mode);
/*! \brief Returns the AccessMode of the Stream.
*
* \return AccessModeThe mode to set.
*/
AccessMode accessMode() const;
AccessMode accessMode() const;
/*! \brief Sets the Textmode of the Stream.
/*! \brief Sets the Textmode of the Stream.
*
* \param mode The mode to set.
*/
void setTextMode(TextMode mode);
void setTextMode(TextMode mode);
/*! \brief Returns the TextMode of the Stream.
*
* \return TextMode The mode to set.
*/
TextMode textMode() const;
TextMode textMode() const;
bool isOpenForReading() const;
bool isOpenForWriting() const;
bool isOpenForReading() const;
bool isOpenForWriting() const;
private:
void loadLines();
std::string m_filename;
@@ -154,5 +158,6 @@ private:
Uint32 m_currentLine;
Uint32 m_startLength;
};
} // io
} // zelda
#endif

View File

@@ -12,48 +12,67 @@
//
// You should have received a copy of the GNU General Public License
// along with libZelda. If not, see <http://www.gnu.org/licenses/>
#ifndef __TYPES_HPP__
#define __TYPES_HPP__
#include <limits.h>
#ifndef __TYPES_HPP__
#define __TYPES_HPP__
// 8 bits integer types
#if UCHAR_MAX == 0xFF
typedef signed char Int8;
typedef unsigned char Uint8;
#else
#error No 8 bits integer type for this platform
#endif
// 16 bits integer types
#if USHRT_MAX == 0xFFFF
typedef signed short Int16;
typedef unsigned short Uint16;
#elif UINT_MAX == 0xFFFF
typedef signed int Int16;
typedef unsigned int Uint16;
#elif ULONG_MAX == 0xFFFF
typedef signed long Int16;
typedef unsigned long Uint16;
#else
#error No 16 bits integer type for this platform
#endif
// 32 bits integer types
#if USHRT_MAX == 0xFFFFFFFF
typedef signed short Int32;
typedef unsigned short Uint32;
#elif UINT_MAX == 0xFFFFFFFF
typedef signed int Int32;
typedef unsigned int Uint32;
#elif ULONG_MAX == 0xFFFFFFFF
typedef signed long Int32;
typedef unsigned long Uint32;
#else
#error No 32 bits integer type for this platform
#endif
typedef signed long long Int64;
typedef unsigned long long Uint64;
#endif
#include <limits.h>
/*! \enum Endian
* \brief Allows the user to specify the Endianness of data.<br />
* The proper actions are automatically taken depending on platform and
* buffer settings
*/
enum Endian
{
LittleEndian, //!< Specifies that the Stream is Little Endian (LSB)
BigEndian //!< Specifies that the Stream is Big Endian (MSB)
};
// 8 bits integer types
#if UCHAR_MAX == 0xFF
typedef signed char Int8;
typedef unsigned char Uint8;
#else
#error No 8 bits integer type for this platform
#endif
// 16 bits integer types
#if USHRT_MAX == 0xFFFF
typedef signed short Int16;
typedef unsigned short Uint16;
#elif UINT_MAX == 0xFFFF
typedef signed int Int16;
typedef unsigned int Uint16;
#elif ULONG_MAX == 0xFFFF
typedef signed long Int16;
typedef unsigned long Uint16;
#else
#error No 16 bits integer type for this platform
#endif
// 32 bits integer types
#if USHRT_MAX == 0xFFFFFFFF
typedef signed short Int32;
typedef unsigned short Uint32;
#elif UINT_MAX == 0xFFFFFFFF
typedef signed int Int32;
typedef unsigned int Uint32;
#elif ULONG_MAX == 0xFFFFFFFF
typedef signed long Int32;
typedef unsigned long Uint32;
#else
#error No 32 bits integer type for this platform
#endif
typedef signed long long Int64;
typedef unsigned long long Uint64;
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else // __cplusplus
#define NULL (void*)0
#endif
#endif // NULL
#endif

View File

@@ -20,22 +20,67 @@
#include <string>
#include <Types.hpp>
namespace zelda
{
/*!
* \brief The WiiImage class
*/
class WiiImage
{
public:
/*!
* \brief WiiImage
*/
WiiImage();
/*!
* \brief WiiImage
* \param width
* \param height
* \param data
*/
WiiImage(Uint32 width, Uint32 height, Uint8* data);
~WiiImage();
/*!
* \brief setWidth
* \param width
*/
void setWidth(const Uint32 width);
/*!
* \brief width
* \return
*/
Uint32 width() const;
/*!
* \brief setHeight
* \param height
*/
void setHeight(const Uint32 height);
/*!
* \brief height
* \return
*/
Uint32 height() const;
/*!
* \brief setData
* \param data
*/
void setData(const Uint8* data);
/*!
* \brief data
* \return
*/
Uint8* data();
/*!
* \brief toRGBA32 DOES NOT WORK!!! DO NOT USE!!!
* \return
*/
Uint8* toRGBA32();
private:
@@ -44,41 +89,156 @@ private:
Uint8* m_data;
};
/*! \class WiiBanner
* \brief Wii banner container class
*
* Contains all relevant data for a Wii banner.
*/
class WiiBanner
{
public:
enum { NoCopy = 0x00000001, Bounce = 0x00000010, NoCopyBounce = NoCopy | Bounce };
enum
{
NoCopy = 0x00000001,
Bounce = 0x00000010,
NoCopyBounce = NoCopy | Bounce
};
/*!
* \brief WiiBanner
*/
WiiBanner();
/*!
* \brief WiiBanner
* \param gameId
* \param title
* \param subtitle
* \param m_banner
* \param icons
*/
WiiBanner(Uint32 gameId, const std::string& title, const std::string& subtitle, WiiImage* m_banner, std::vector<WiiImage*> icons);
virtual ~WiiBanner();
/*!
* \brief setGameID
* \param id
*/
void setGameID(Uint64 id);
/*!
* \brief gameID
* \return
*/
Uint64 gameID() const;
/*!
* \brief setBannerImage
* \param banner
*/
void setBannerImage(WiiImage* banner);
/*!
* \brief bannerImage
* \return
*/
WiiImage* bannerImage() const;
/*!
* \brief setBannerSize
* \param size
*/
void setBannerSize(Uint32 size);
/*!
* \brief bannerSize
* \return
*/
Uint32 bannerSize() const;
/*!
* \brief setTitle
* \param title
*/
void setTitle(const std::string& title);
/*!
* \brief title
* \return
*/
std::string title() const;
void setSubtitle(const std::string& subtitle);
/*!
* \brief setSubtitle
* \param subtitle
*/
void setSubtitle(const std::string& subtitle);
/*!
* \brief subtitle
* \return
*/
std::string subtitle() const;
/*!
* \brief addIcon
* \param icon
*/
void addIcon(WiiImage* icon);
/*!
* \brief setIcon
* \param id
* \param icon
*/
void setIcon(Uint32 id, WiiImage* icon);
/*!
* \brief getIcon
* \param id
* \return
*/
WiiImage* getIcon(Uint32 id) const;
/*!
* \brief icons
* \return
*/
std::vector<WiiImage*> icons() const;
/*!
* \brief setAnimationSpeed
* \param animSpeed
*/
void setAnimationSpeed(Uint16 animSpeed);
/*!
* \brief animationSpeed
* \return
*/
Uint16 animationSpeed() const;
/*!
* \brief setPermissions
* \param permissions
*/
void setPermissions(Uint8 permissions);
/*!
* \brief permissions
* \return
*/
Uint8 permissions() const;
/*!
* \brief setFlags
* \param flags
*/
void setFlags(Uint32 flags);
/*!
* \brief flags
* \return
*/
Uint32 flags() const;
protected:
private:
@@ -92,5 +252,6 @@ private:
std::string m_title;
std::string m_subtitle;
};
} // zelda
#endif // WIIBANNER_H

View File

@@ -19,9 +19,22 @@
#include <map>
#include <Types.hpp>
namespace zelda
{
/*! \class WiiFile
* \brief Wii file container class
*
* Contains all relevant data for a file in a data.bin file.
*/
class WiiFile
{
public:
/*! \enum Permission
* \brief The Wii uses a bastardized unix permissions system so these flags
* reflect the file's individual permissions.
*/
enum Permission
{
OtherRead = 0x01,
@@ -32,11 +45,14 @@ public:
OwnerWrite = 0x20,
// Mask values;
OtherRW = (OtherRead|OtherWrite),
OtherRW = (OtherRead|OtherWrite), //!< Mask to get the Other group permissions
GroupRW = (GroupRead|GroupWrite),
OwnerRW = (OwnerRead|OwnerWrite)
};
/*!
* \brief The Type enum
*/
enum Type
{
File = 0x01,
@@ -44,29 +60,104 @@ public:
};
WiiFile();
/*!
* \brief WiiFile
* \param filename
*/
WiiFile(const std::string& filename);
/*!
* \brief WiiFile
* \param filename
* \param permissions
* \param data
* \param length
*/
WiiFile(const std::string& filename, Uint8 permissions, const Uint8* data, Uint32 length);
virtual ~WiiFile();
/*!
* \brief setFilename
* \param filename
*/
void setFilename(const std::string& filename);
/*!
* \brief filename
* \return
*/
std::string filename() const;
/*!
* \brief setData
* \param data
*/
void setData(const Uint8* data);
Uint8* data() const;
/*!
* \brief data
* \return
*/
Uint8* data() const;
/*!
* \brief setLength
* \param len
*/
void setLength(const int len);
int length() const;
/*!
* \brief length
* \return
*/
int length() const;
/*!
* \brief setPermissions
* \param permissions
*/
void setPermissions(const Uint8 permissions);
/*!
* \brief permissions
* \return
*/
Uint8 permissions() const;
/*!
* \brief setAttributes
* \param attr
*/
void setAttributes(const Uint8 attr);
/*!
* \brief attributes
* \return
*/
Uint8 attributes() const;
/*!
* \brief setType
* \param type
*/
void setType(Type type);
/*!
* \brief type
* \return
*/
Type type() const;
/*!
* \brief isDirectory
* \return
*/
bool isDirectory() const;
/*!
* \brief isFile
* \return
*/
bool isFile() const;
protected:
@@ -79,4 +170,5 @@ private:
Uint8* m_fileData;
};
} // zelda
#endif // WIIFILE_H

View File

@@ -19,25 +19,67 @@
#include <string>
#include <Types.hpp>
namespace zelda
{
class WiiFile;
class WiiBanner;
class WiiImage;
class BinaryReader;
class BinaryWriter;
/*! \class WiiSave
* \brief Wii data.bin container class
*
* Contains all relevant data for a Wii data.bin file.
*/
class WiiSave
{
public:
/*!
* \brief FileIterator
*/
typedef std::unordered_map<std::string, WiiFile*>::const_iterator FileIterator;
/*!
* \brief WiiSave
*/
WiiSave();
/*!
* \brief ~WiiSave
*/
virtual ~WiiSave();
/*!
* \brief addFile
* \param filename
* \param file
*/
void addFile(const std::string& filename, WiiFile* file);
/*!
* \brief file
* \param filename
* \return
*/
WiiFile* file(const std::string& filename) const;
/*!
* \brief fileList
* \return
*/
std::unordered_map<std::string, WiiFile*>& fileList();
/*!
* \brief setBanner
* \param banner
*/
void setBanner(WiiBanner* banner);
/*!
* \brief banner
* \return
*/
WiiBanner* banner() const;
protected:
@@ -48,4 +90,5 @@ private:
};
} // zelda
#endif // __WII__SAVE_HPP__

View File

@@ -19,17 +19,41 @@
#include <utility.hpp>
#include <BinaryReader.hpp>
namespace zelda
{
class WiiSave;
class WiiBanner;
class WiiFile;
class WiiImage;
class WiiSaveReader : public BinaryReader
/*! \class WiiSaveReader
* \brief Wii data.bin reader class
*
* A Class for reading binary data from a wii data.bin file,
* all work is done using a memory buffer, and not read directly from the disk.
* \sa BinaryReader
*/
class WiiSaveReader : public io::BinaryReader
{
public:
/*! \brief This constructor takes an existing buffer to read from.
*
* \param data The existing buffer
* \param length The length of the existing buffer
*/
WiiSaveReader(const Uint8*, Uint64);
/*! \brief This constructor creates an instance from a file on disk.
*
* \param filename The file to create the stream from
*/
WiiSaveReader(const std::string&);
/*!
* \brief readSave
* \return
*/
WiiSave* readSave();
private:
WiiBanner* readBanner();
@@ -37,4 +61,6 @@ private:
WiiImage* readImage(Uint32 width, Uint32 height);
void readCerts(Uint32 totalSize);
};
} // zelda
#endif // __WII_SAVE_READER_HPP__

View File

@@ -12,23 +12,49 @@
//
// You should have received a copy of the GNU General Public License
// along with libZelda. If not, see <http://www.gnu.org/licenses/>
#ifndef __WII_SAVE_WRITER_HPP__
#define __WII_SAVE_WRITER_HPP__
#include <Types.hpp>
#include <utility.hpp>
#include <BinaryWriter.hpp>
#include "Types.hpp"
#include "utility.hpp"
#include "BinaryWriter.hpp"
namespace zelda
{
class WiiSave;
class WiiBanner;
class WiiFile;
class WiiImage;
class WiiSaveWriter : public BinaryWriter
/*! \class WiiSaveWriter
* \brief Wii data.bin writer class
*
* A Class for writing binary data to a wii data.bin file,
* all work is done using a memory buffer, and not written directly to the disk.
* \sa BinaryReader
*/
class WiiSaveWriter : public io::BinaryWriter
{
public:
/*! \brief This constructor creates an instance from a file on disk.
*
* \param filename The file to create the stream from
*/
WiiSaveWriter(const std::string&);
/*!
* \brief writeSave
* \param save
* \param macAddress
* \param ngId
* \param ngPriv
* \param ngSig
* \param ngKeyId
* \param filepath
* \return
*/
bool writeSave(WiiSave* save, Uint8* macAddress, Uint32 ngId, Uint8* ngPriv, Uint8* ngSig, Uint32 ngKeyId, const std::string& filepath = "");
private:
@@ -38,4 +64,5 @@ private:
void writeCerts(Uint32 filesSize, Uint32 ngId, Uint8* ngPriv, Uint8* ngSig, Uint32 ngKeyId);
};
} // zelda
#endif // __WII_SAVE_WRITER_HPP__

View File

@@ -16,5 +16,3 @@ void aes_set_key(const Uint8 *key );
#endif
#endif //__AES_H_

View File

@@ -1,6 +1,8 @@
#ifndef BN_H
#define BN_H
#ifndef __DOXYGEN_IGNORE__
#include <Types.hpp>
int bn_compare(Uint8 *a, Uint8 *b, Uint32 n);
@@ -10,4 +12,5 @@ void bn_mul(Uint8 *d, Uint8 *a, Uint8 *b, Uint8 *N, Uint32 n);
void bn_exp(Uint8 *d, Uint8 *a, Uint8 *N, Uint32 n, Uint8 *e, Uint32 en);
void bn_inv(Uint8 *d, Uint8 *a, Uint8 *N, Uint32 n);
#endif // __DOXYGEN_IGNORE__
#endif // BN_H

View File

@@ -1,241 +1,244 @@
#ifndef MD5_H
#define MD5_H
#ifndef __DOXYGEN_IGNORE__
#ifdef __cplusplus
extern "C"
{
#endif
/* ========================================================================== **
*
* MD5.h
*
* Copyright:
* Copyright (C) 2003-2005 by Christopher R. Hertel
*
* Email: crh@ubiqx.mn.org
*
* $Id: MD5.h,v 0.6 2005/06/08 18:35:59 crh Exp $
*
* Modifications and additions by dimok
*
* -------------------------------------------------------------------------- **
*
* Description:
* Implements the MD5 hash algorithm, as described in RFC 1321.
*
* -------------------------------------------------------------------------- **
*
* License:
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* -------------------------------------------------------------------------- **
*
* Notes:
*
* None of this will make any sense unless you're studying RFC 1321 as you
* read the code.
*
* MD5 is described in RFC 1321.
* The MD*4* algorithm is described in RFC 1320 (that's 1321 - 1).
* MD5 is very similar to MD4, but not quite similar enough to justify
* putting the two into a single module. Besides, I wanted to add a few
* extra functions to this one to expand its usability.
*
* There are three primary motivations for this particular implementation.
* 1) Programmer's pride. I wanted to be able to say I'd done it, and I
* wanted to learn from the experience.
* 2) Portability. I wanted an implementation that I knew to be portable
* to a reasonable number of platforms. In particular, the algorithm is
* designed with little-endian platforms in mind, but I wanted an
* endian-agnostic implementation.
* 3) Compactness. While not an overriding goal, I thought it worth-while
* to see if I could reduce the overall size of the result. This is in
* keeping with my hopes that this library will be suitable for use in
* some embedded environments.
* Beyond that, cleanliness and clarity are always worth pursuing.
*
* As mentioned above, the code really only makes sense if you are familiar
* with the MD5 algorithm or are using RFC 1321 as a guide. This code is
* quirky, however, so you'll want to be reading carefully.
*
* Yeah...most of the comments are cut-and-paste from my MD4 implementation.
*
* -------------------------------------------------------------------------- **
*
* References:
* IETF RFC 1321: The MD5 Message-Digest Algorithm
* Ron Rivest. IETF, April, 1992
*
* ========================================================================== **
*/
/* -------------------------------------------------------------------------- **
* Typedefs:
*/
/* ========================================================================== **
*
* MD5.h
*
* Copyright:
* Copyright (C) 2003-2005 by Christopher R. Hertel
*
* Email: crh@ubiqx.mn.org
*
* $Id: MD5.h,v 0.6 2005/06/08 18:35:59 crh Exp $
*
* Modifications and additions by dimok
*
* -------------------------------------------------------------------------- **
*
* Description:
* Implements the MD5 hash algorithm, as described in RFC 1321.
*
* -------------------------------------------------------------------------- **
*
* License:
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* -------------------------------------------------------------------------- **
*
* Notes:
*
* None of this will make any sense unless you're studying RFC 1321 as you
* read the code.
*
* MD5 is described in RFC 1321.
* The MD*4* algorithm is described in RFC 1320 (that's 1321 - 1).
* MD5 is very similar to MD4, but not quite similar enough to justify
* putting the two into a single module. Besides, I wanted to add a few
* extra functions to this one to expand its usability.
*
* There are three primary motivations for this particular implementation.
* 1) Programmer's pride. I wanted to be able to say I'd done it, and I
* wanted to learn from the experience.
* 2) Portability. I wanted an implementation that I knew to be portable
* to a reasonable number of platforms. In particular, the algorithm is
* designed with little-endian platforms in mind, but I wanted an
* endian-agnostic implementation.
* 3) Compactness. While not an overriding goal, I thought it worth-while
* to see if I could reduce the overall size of the result. This is in
* keeping with my hopes that this library will be suitable for use in
* some embedded environments.
* Beyond that, cleanliness and clarity are always worth pursuing.
*
* As mentioned above, the code really only makes sense if you are familiar
* with the MD5 algorithm or are using RFC 1321 as a guide. This code is
* quirky, however, so you'll want to be reading carefully.
*
* Yeah...most of the comments are cut-and-paste from my MD4 implementation.
*
* -------------------------------------------------------------------------- **
*
* References:
* IETF RFC 1321: The MD5 Message-Digest Algorithm
* Ron Rivest. IETF, April, 1992
*
* ========================================================================== **
*/
/* -------------------------------------------------------------------------- **
* Typedefs:
*/
typedef struct
{
unsigned int len;
unsigned int ABCD[4];
int b_used;
unsigned char block[64];
} auth_md5Ctx;
typedef struct
{
unsigned int len;
unsigned int ABCD[4];
int b_used;
unsigned char block[64];
} auth_md5Ctx;
/* -------------------------------------------------------------------------- **
* Functions:
*/
/* -------------------------------------------------------------------------- **
* Functions:
*/
auth_md5Ctx *auth_md5InitCtx(auth_md5Ctx *ctx);
/* ------------------------------------------------------------------------ **
* Initialize an MD5 context.
*
* Input: ctx - A pointer to the MD5 context structure to be initialized.
* Contexts are typically created thusly:
* ctx = (auth_md5Ctx *)malloc( sizeof(auth_md5Ctx) );
*
* Output: A pointer to the initialized context (same as <ctx>).
*
* Notes: The purpose of the context is to make it possible to generate
* an MD5 Message Digest in stages, rather than having to pass a
* single large block to a single MD5 function. The context
* structure keeps track of various bits of state information.
*
* Once the context is initialized, the blocks of message data
* are passed to the <auth_md5SumCtx()> function. Once the
* final bit of data has been handed to <auth_md5SumCtx()> the
* context can be closed out by calling <auth_md5CloseCtx()>,
* which also calculates the final MD5 result.
*
* Don't forget to free an allocated context structure when
* you've finished using it.
*
* See Also: <auth_md5SumCtx()>, <auth_md5CloseCtx()>
*
* ------------------------------------------------------------------------ **
*/
auth_md5Ctx *auth_md5InitCtx(auth_md5Ctx *ctx);
/* ------------------------------------------------------------------------ **
* Initialize an MD5 context.
*
* Input: ctx - A pointer to the MD5 context structure to be initialized.
* Contexts are typically created thusly:
* ctx = (auth_md5Ctx *)malloc( sizeof(auth_md5Ctx) );
*
* Output: A pointer to the initialized context (same as <ctx>).
*
* Notes: The purpose of the context is to make it possible to generate
* an MD5 Message Digest in stages, rather than having to pass a
* single large block to a single MD5 function. The context
* structure keeps track of various bits of state information.
*
* Once the context is initialized, the blocks of message data
* are passed to the <auth_md5SumCtx()> function. Once the
* final bit of data has been handed to <auth_md5SumCtx()> the
* context can be closed out by calling <auth_md5CloseCtx()>,
* which also calculates the final MD5 result.
*
* Don't forget to free an allocated context structure when
* you've finished using it.
*
* See Also: <auth_md5SumCtx()>, <auth_md5CloseCtx()>
*
* ------------------------------------------------------------------------ **
*/
auth_md5Ctx *auth_md5SumCtx(auth_md5Ctx *ctx, const unsigned char *src, const int len);
/* ------------------------------------------------------------------------ **
* Build an MD5 Message Digest within the given context.
*
* Input: ctx - Pointer to the context in which the MD5 sum is being
* built.
* src - A chunk of source data. This will be used to drive
* the MD5 algorithm.
* len - The number of bytes in <src>.
*
* Output: A pointer to the updated context (same as <ctx>).
*
* See Also: <auth_md5InitCtx()>, <auth_md5CloseCtx()>, <auth_md5Sum()>
*
* ------------------------------------------------------------------------ **
*/
auth_md5Ctx *auth_md5SumCtx(auth_md5Ctx *ctx, const unsigned char *src, const int len);
/* ------------------------------------------------------------------------ **
* Build an MD5 Message Digest within the given context.
*
* Input: ctx - Pointer to the context in which the MD5 sum is being
* built.
* src - A chunk of source data. This will be used to drive
* the MD5 algorithm.
* len - The number of bytes in <src>.
*
* Output: A pointer to the updated context (same as <ctx>).
*
* See Also: <auth_md5InitCtx()>, <auth_md5CloseCtx()>, <auth_md5Sum()>
*
* ------------------------------------------------------------------------ **
*/
auth_md5Ctx *auth_md5CloseCtx(auth_md5Ctx *ctx, unsigned char *dst);
/* ------------------------------------------------------------------------ **
* Close an MD5 Message Digest context and generate the final MD5 sum.
*
* Input: ctx - Pointer to the context in which the MD5 sum is being
* built.
* dst - A pointer to at least 16 bytes of memory, which will
* receive the finished MD5 sum.
*
* Output: A pointer to the closed context (same as <ctx>).
* You might use this to free a malloc'd context structure. :)
*
* Notes: The context (<ctx>) is returned in an undefined state.
* It must be re-initialized before re-use.
*
* See Also: <auth_md5InitCtx()>, <auth_md5SumCtx()>
*
* ------------------------------------------------------------------------ **
*/
auth_md5Ctx *auth_md5CloseCtx(auth_md5Ctx *ctx, unsigned char *dst);
/* ------------------------------------------------------------------------ **
* Close an MD5 Message Digest context and generate the final MD5 sum.
*
* Input: ctx - Pointer to the context in which the MD5 sum is being
* built.
* dst - A pointer to at least 16 bytes of memory, which will
* receive the finished MD5 sum.
*
* Output: A pointer to the closed context (same as <ctx>).
* You might use this to free a malloc'd context structure. :)
*
* Notes: The context (<ctx>) is returned in an undefined state.
* It must be re-initialized before re-use.
*
* See Also: <auth_md5InitCtx()>, <auth_md5SumCtx()>
*
* ------------------------------------------------------------------------ **
*/
unsigned char * MD5(unsigned char * hash, const unsigned char *src, const int len);
/* ------------------------------------------------------------------------ **
* Compute an MD5 message digest.
*
* Input: dst - Destination buffer into which the result will be written.
* Must be 16 bytes, minimum.
* src - Source data block to be MD5'd.
* len - The length, in bytes, of the source block.
* (Note that the length is given in bytes, not bits.)
*
* Output: A pointer to <dst>, which will contain the calculated 16-byte
* MD5 message digest.
*
* Notes: This function is a shortcut. It takes a single input block.
* For more drawn-out operations, see <auth_md5InitCtx()>.
*
* This function is interface-compatible with the
* <auth_md4Sum()> function in the MD4 module.
*
* The MD5 algorithm is designed to work on data with an
* arbitrary *bit* length. Most implementations, this one
* included, handle the input data in byte-sized chunks.
*
* The MD5 algorithm does much of its work using four-byte
* words, and so can be tuned for speed based on the endian-ness
* of the host. This implementation is intended to be
* endian-neutral, which may make it a teeny bit slower than
* others. ...maybe.
*
* See Also: <auth_md5InitCtx()>
*
* ------------------------------------------------------------------------ **
*/
unsigned char * MD5(unsigned char * hash, const unsigned char *src, const int len);
/* ------------------------------------------------------------------------ **
* Compute an MD5 message digest.
*
* Input: dst - Destination buffer into which the result will be written.
* Must be 16 bytes, minimum.
* src - Source data block to be MD5'd.
* len - The length, in bytes, of the source block.
* (Note that the length is given in bytes, not bits.)
*
* Output: A pointer to <dst>, which will contain the calculated 16-byte
* MD5 message digest.
*
* Notes: This function is a shortcut. It takes a single input block.
* For more drawn-out operations, see <auth_md5InitCtx()>.
*
* This function is interface-compatible with the
* <auth_md4Sum()> function in the MD4 module.
*
* The MD5 algorithm is designed to work on data with an
* arbitrary *bit* length. Most implementations, this one
* included, handle the input data in byte-sized chunks.
*
* The MD5 algorithm does much of its work using four-byte
* words, and so can be tuned for speed based on the endian-ness
* of the host. This implementation is intended to be
* endian-neutral, which may make it a teeny bit slower than
* others. ...maybe.
*
* See Also: <auth_md5InitCtx()>
*
* ------------------------------------------------------------------------ **
*/
unsigned char * MD5fromFile(unsigned char *dst, const char *src);
/* ------------------------------------------------------------------------ **
* Compute an MD5 message digest.
*
* Input: dst - Destination buffer into which the result will be written.
* Must be 16 bytes, minimum.
* src - filepath to the file to be MD5'd.
*
* Output: A pointer to <dst>, which will contain the calculated 16-byte
* MD5 message digest.
*
* Notes: This function is a shortcut. It takes a single input block.
* For more drawn-out operations, see <auth_md5InitCtx()>.
*
* This function is interface-compatible with the
* <auth_md4Sum()> function in the MD4 module.
*
* The MD5 algorithm is designed to work on data with an
* arbitrary *bit* length. Most implementations, this one
* included, handle the input data in byte-sized chunks.
*
* The MD5 algorithm does much of its work using four-byte
* words, and so can be tuned for speed based on the endian-ness
* of the host. This implementation is intended to be
* endian-neutral, which may make it a teeny bit slower than
* others. ...maybe.
*
* See Also: <auth_md5InitCtx()>
*
* ------------------------------------------------------------------------ **
*/
unsigned char * MD5fromFile(unsigned char *dst, const char *src);
/* ------------------------------------------------------------------------ **
* Compute an MD5 message digest.
*
* Input: dst - Destination buffer into which the result will be written.
* Must be 16 bytes, minimum.
* src - filepath to the file to be MD5'd.
*
* Output: A pointer to <dst>, which will contain the calculated 16-byte
* MD5 message digest.
*
* Notes: This function is a shortcut. It takes a single input block.
* For more drawn-out operations, see <auth_md5InitCtx()>.
*
* This function is interface-compatible with the
* <auth_md4Sum()> function in the MD4 module.
*
* The MD5 algorithm is designed to work on data with an
* arbitrary *bit* length. Most implementations, this one
* included, handle the input data in byte-sized chunks.
*
* The MD5 algorithm does much of its work using four-byte
* words, and so can be tuned for speed based on the endian-ness
* of the host. This implementation is intended to be
* endian-neutral, which may make it a teeny bit slower than
* others. ...maybe.
*
* See Also: <auth_md5InitCtx()>
*
* ------------------------------------------------------------------------ **
*/
const char * MD5ToString(const unsigned char *hash, char *dst);
unsigned char * StringToMD5(const char * hash, unsigned char * dst);
const char * MD5ToString(const unsigned char *hash, char *dst);
unsigned char * StringToMD5(const char * hash, unsigned char * dst);
/* ========================================================================== */
#ifdef __cplusplus
}
#endif
#endif // __DOXYGEN_IGNORE__
#endif /* AUTH_MD5_H */

View File

@@ -1,9 +1,9 @@
#ifndef _SHA1_H_
#define _SHA1_H_
#include <Types.hpp>
#include <Types.hpp>
#ifndef __DOXYGEN_IGNORE__
#ifdef __cplusplus
extern "C" {
#endif
@@ -32,10 +32,10 @@ typedef struct SHA1Context
void SHA1Reset(SHA1Context *);
int SHA1Result(SHA1Context *);
void SHA1Input( SHA1Context *,
const unsigned char *,
unsigned);
const unsigned char *,
unsigned);
Uint8* getSha1( Uint8 * stuff, Uint32 stuff_size );
@@ -43,4 +43,5 @@ Uint8* getSha1( Uint8 * stuff, Uint32 stuff_size );
}
#endif
#endif // __DOXYGEN_IGNORE__
#endif

View File

@@ -33,292 +33,292 @@ DEALINGS IN THE SOFTWARE.
namespace utf8
{
// Base for the exceptions that may be thrown from the library
class exception : public ::std::exception {
};
// Base for the exceptions that may be thrown from the library
class exception : public ::std::exception {
};
// Exceptions that may be thrown from the library functions.
class invalid_code_point : public exception {
uint32_t cp;
public:
invalid_code_point(uint32_t cp) : cp(cp) {}
virtual const char* what() const throw() { return "Invalid code point"; }
uint32_t code_point() const {return cp;}
};
// Exceptions that may be thrown from the library functions.
class invalid_code_point : public exception {
uint32_t cp;
public:
invalid_code_point(uint32_t cp) : cp(cp) {}
virtual const char* what() const throw() { return "Invalid code point"; }
uint32_t code_point() const {return cp;}
};
class invalid_utf8 : public exception {
uint8_t u8;
public:
invalid_utf8 (uint8_t u) : u8(u) {}
virtual const char* what() const throw() { return "Invalid UTF-8"; }
uint8_t utf8_octet() const {return u8;}
};
class invalid_utf8 : public exception {
uint8_t u8;
public:
invalid_utf8 (uint8_t u) : u8(u) {}
virtual const char* what() const throw() { return "Invalid UTF-8"; }
uint8_t utf8_octet() const {return u8;}
};
class invalid_utf16 : public exception {
uint16_t u16;
public:
invalid_utf16 (uint16_t u) : u16(u) {}
virtual const char* what() const throw() { return "Invalid UTF-16"; }
uint16_t utf16_word() const {return u16;}
};
class invalid_utf16 : public exception {
uint16_t u16;
public:
invalid_utf16 (uint16_t u) : u16(u) {}
virtual const char* what() const throw() { return "Invalid UTF-16"; }
uint16_t utf16_word() const {return u16;}
};
class not_enough_room : public exception {
public:
virtual const char* what() const throw() { return "Not enough space"; }
};
class not_enough_room : public exception {
public:
virtual const char* what() const throw() { return "Not enough space"; }
};
/// The library API - functions intended to be called by the users
/// The library API - functions intended to be called by the users
template <typename octet_iterator>
octet_iterator append(uint32_t cp, octet_iterator result)
{
if (!utf8::internal::is_code_point_valid(cp))
throw invalid_code_point(cp);
template <typename octet_iterator>
octet_iterator append(uint32_t cp, octet_iterator result)
{
if (!utf8::internal::is_code_point_valid(cp))
throw invalid_code_point(cp);
if (cp < 0x80) // one octet
*(result++) = static_cast<uint8_t>(cp);
else if (cp < 0x800) { // two octets
*(result++) = static_cast<uint8_t>((cp >> 6) | 0xc0);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
}
else if (cp < 0x10000) { // three octets
*(result++) = static_cast<uint8_t>((cp >> 12) | 0xe0);
*(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
}
else { // four octets
*(result++) = static_cast<uint8_t>((cp >> 18) | 0xf0);
*(result++) = static_cast<uint8_t>(((cp >> 12) & 0x3f) | 0x80);
*(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
}
return result;
if (cp < 0x80) // one octet
*(result++) = static_cast<uint8_t>(cp);
else if (cp < 0x800) { // two octets
*(result++) = static_cast<uint8_t>((cp >> 6) | 0xc0);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
}
template <typename octet_iterator, typename output_iterator>
output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement)
{
while (start != end) {
octet_iterator sequence_start = start;
internal::utf_error err_code = utf8::internal::validate_next(start, end);
switch (err_code) {
case internal::UTF8_OK :
for (octet_iterator it = sequence_start; it != start; ++it)
*out++ = *it;
break;
case internal::NOT_ENOUGH_ROOM:
throw not_enough_room();
case internal::INVALID_LEAD:
utf8::append (replacement, out);
++start;
break;
case internal::INCOMPLETE_SEQUENCE:
case internal::OVERLONG_SEQUENCE:
case internal::INVALID_CODE_POINT:
utf8::append (replacement, out);
++start;
// just one replacement mark for the sequence
while (start != end && utf8::internal::is_trail(*start))
++start;
break;
}
}
return out;
else if (cp < 0x10000) { // three octets
*(result++) = static_cast<uint8_t>((cp >> 12) | 0xe0);
*(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
}
template <typename octet_iterator, typename output_iterator>
inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out)
{
static const uint32_t replacement_marker = utf8::internal::mask16(0xfffd);
return utf8::replace_invalid(start, end, out, replacement_marker);
else { // four octets
*(result++) = static_cast<uint8_t>((cp >> 18) | 0xf0);
*(result++) = static_cast<uint8_t>(((cp >> 12) & 0x3f) | 0x80);
*(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
}
return result;
}
template <typename octet_iterator>
uint32_t next(octet_iterator& it, octet_iterator end)
{
uint32_t cp = 0;
internal::utf_error err_code = utf8::internal::validate_next(it, end, cp);
template <typename octet_iterator, typename output_iterator>
output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out, uint32_t replacement)
{
while (start != end) {
octet_iterator sequence_start = start;
internal::utf_error err_code = utf8::internal::validate_next(start, end);
switch (err_code) {
case internal::UTF8_OK :
for (octet_iterator it = sequence_start; it != start; ++it)
*out++ = *it;
break;
case internal::NOT_ENOUGH_ROOM :
case internal::NOT_ENOUGH_ROOM:
throw not_enough_room();
case internal::INVALID_LEAD :
case internal::INCOMPLETE_SEQUENCE :
case internal::OVERLONG_SEQUENCE :
throw invalid_utf8(*it);
case internal::INVALID_CODE_POINT :
throw invalid_code_point(cp);
case internal::INVALID_LEAD:
utf8::append (replacement, out);
++start;
break;
case internal::INCOMPLETE_SEQUENCE:
case internal::OVERLONG_SEQUENCE:
case internal::INVALID_CODE_POINT:
utf8::append (replacement, out);
++start;
// just one replacement mark for the sequence
while (start != end && utf8::internal::is_trail(*start))
++start;
break;
}
return cp;
}
return out;
}
template <typename octet_iterator>
uint32_t peek_next(octet_iterator it, octet_iterator end)
{
return utf8::next(it, end);
}
template <typename octet_iterator, typename output_iterator>
inline output_iterator replace_invalid(octet_iterator start, octet_iterator end, output_iterator out)
{
static const uint32_t replacement_marker = utf8::internal::mask16(0xfffd);
return utf8::replace_invalid(start, end, out, replacement_marker);
}
template <typename octet_iterator>
uint32_t prior(octet_iterator& it, octet_iterator start)
{
// can't do much if it == start
if (it == start)
template <typename octet_iterator>
uint32_t next(octet_iterator& it, octet_iterator end)
{
uint32_t cp = 0;
internal::utf_error err_code = utf8::internal::validate_next(it, end, cp);
switch (err_code) {
case internal::UTF8_OK :
break;
case internal::NOT_ENOUGH_ROOM :
throw not_enough_room();
octet_iterator end = it;
// Go back until we hit either a lead octet or start
while (utf8::internal::is_trail(*(--it)))
if (it == start)
throw invalid_utf8(*it); // error - no lead byte in the sequence
return utf8::peek_next(it, end);
case internal::INVALID_LEAD :
case internal::INCOMPLETE_SEQUENCE :
case internal::OVERLONG_SEQUENCE :
throw invalid_utf8(*it);
case internal::INVALID_CODE_POINT :
throw invalid_code_point(cp);
}
return cp;
}
/// Deprecated in versions that include "prior"
template <typename octet_iterator>
uint32_t previous(octet_iterator& it, octet_iterator pass_start)
{
octet_iterator end = it;
while (utf8::internal::is_trail(*(--it)))
if (it == pass_start)
throw invalid_utf8(*it); // error - no lead byte in the sequence
octet_iterator temp = it;
return utf8::next(temp, end);
}
template <typename octet_iterator>
uint32_t peek_next(octet_iterator it, octet_iterator end)
{
return utf8::next(it, end);
}
template <typename octet_iterator, typename distance_type>
void advance (octet_iterator& it, distance_type n, octet_iterator end)
{
for (distance_type i = 0; i < n; ++i)
utf8::next(it, end);
}
template <typename octet_iterator>
uint32_t prior(octet_iterator& it, octet_iterator start)
{
// can't do much if it == start
if (it == start)
throw not_enough_room();
template <typename octet_iterator>
typename std::iterator_traits<octet_iterator>::difference_type
distance (octet_iterator first, octet_iterator last)
{
typename std::iterator_traits<octet_iterator>::difference_type dist;
for (dist = 0; first < last; ++dist)
utf8::next(first, last);
return dist;
}
octet_iterator end = it;
// Go back until we hit either a lead octet or start
while (utf8::internal::is_trail(*(--it)))
if (it == start)
throw invalid_utf8(*it); // error - no lead byte in the sequence
return utf8::peek_next(it, end);
}
template <typename u16bit_iterator, typename octet_iterator>
octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
{
while (start != end) {
uint32_t cp = utf8::internal::mask16(*start++);
// Take care of surrogate pairs first
if (utf8::internal::is_lead_surrogate(cp)) {
if (start != end) {
uint32_t trail_surrogate = utf8::internal::mask16(*start++);
if (utf8::internal::is_trail_surrogate(trail_surrogate))
cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
else
throw invalid_utf16(static_cast<uint16_t>(trail_surrogate));
}
/// Deprecated in versions that include "prior"
template <typename octet_iterator>
uint32_t previous(octet_iterator& it, octet_iterator pass_start)
{
octet_iterator end = it;
while (utf8::internal::is_trail(*(--it)))
if (it == pass_start)
throw invalid_utf8(*it); // error - no lead byte in the sequence
octet_iterator temp = it;
return utf8::next(temp, end);
}
template <typename octet_iterator, typename distance_type>
void advance (octet_iterator& it, distance_type n, octet_iterator end)
{
for (distance_type i = 0; i < n; ++i)
utf8::next(it, end);
}
template <typename octet_iterator>
typename std::iterator_traits<octet_iterator>::difference_type
distance (octet_iterator first, octet_iterator last)
{
typename std::iterator_traits<octet_iterator>::difference_type dist;
for (dist = 0; first < last; ++dist)
utf8::next(first, last);
return dist;
}
template <typename u16bit_iterator, typename octet_iterator>
octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
{
while (start != end) {
uint32_t cp = utf8::internal::mask16(*start++);
// Take care of surrogate pairs first
if (utf8::internal::is_lead_surrogate(cp)) {
if (start != end) {
uint32_t trail_surrogate = utf8::internal::mask16(*start++);
if (utf8::internal::is_trail_surrogate(trail_surrogate))
cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
else
throw invalid_utf16(static_cast<uint16_t>(cp));
}
// Lone trail surrogate
else if (utf8::internal::is_trail_surrogate(cp))
throw invalid_utf16(static_cast<uint16_t>(cp));
result = utf8::append(cp, result);
}
return result;
}
template <typename u16bit_iterator, typename octet_iterator>
u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
{
while (start != end) {
uint32_t cp = utf8::next(start, end);
if (cp > 0xffff) { //make a surrogate pair
*result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET);
*result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
throw invalid_utf16(static_cast<uint16_t>(trail_surrogate));
}
else
*result++ = static_cast<uint16_t>(cp);
throw invalid_utf16(static_cast<uint16_t>(cp));
}
return result;
}
// Lone trail surrogate
else if (utf8::internal::is_trail_surrogate(cp))
throw invalid_utf16(static_cast<uint16_t>(cp));
template <typename octet_iterator, typename u32bit_iterator>
octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)
result = utf8::append(cp, result);
}
return result;
}
template <typename u16bit_iterator, typename octet_iterator>
u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
{
while (start != end) {
uint32_t cp = utf8::next(start, end);
if (cp > 0xffff) { //make a surrogate pair
*result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET);
*result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
}
else
*result++ = static_cast<uint16_t>(cp);
}
return result;
}
template <typename octet_iterator, typename u32bit_iterator>
octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)
{
while (start != end)
result = utf8::append(*(start++), result);
return result;
}
template <typename octet_iterator, typename u32bit_iterator>
u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)
{
while (start != end)
(*result++) = utf8::next(start, end);
return result;
}
// The iterator class
template <typename octet_iterator>
class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t> {
octet_iterator it;
octet_iterator range_start;
octet_iterator range_end;
public:
iterator () {};
explicit iterator (const octet_iterator& octet_it,
const octet_iterator& range_start,
const octet_iterator& range_end) :
it(octet_it), range_start(range_start), range_end(range_end)
{
while (start != end)
result = utf8::append(*(start++), result);
return result;
if (it < range_start || it > range_end)
throw std::out_of_range("Invalid utf-8 iterator position");
}
template <typename octet_iterator, typename u32bit_iterator>
u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)
// the default "big three" are OK
octet_iterator base () const { return it; }
uint32_t operator * () const
{
while (start != end)
(*result++) = utf8::next(start, end);
return result;
octet_iterator temp = it;
return utf8::next(temp, range_end);
}
// The iterator class
template <typename octet_iterator>
class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t> {
octet_iterator it;
octet_iterator range_start;
octet_iterator range_end;
public:
iterator () {};
explicit iterator (const octet_iterator& octet_it,
const octet_iterator& range_start,
const octet_iterator& range_end) :
it(octet_it), range_start(range_start), range_end(range_end)
{
if (it < range_start || it > range_end)
throw std::out_of_range("Invalid utf-8 iterator position");
}
// the default "big three" are OK
octet_iterator base () const { return it; }
uint32_t operator * () const
{
octet_iterator temp = it;
return utf8::next(temp, range_end);
}
bool operator == (const iterator& rhs) const
{
if (range_start != rhs.range_start || range_end != rhs.range_end)
throw std::logic_error("Comparing utf-8 iterators defined with different ranges");
return (it == rhs.it);
}
bool operator != (const iterator& rhs) const
{
return !(operator == (rhs));
}
iterator& operator ++ ()
{
utf8::next(it, range_end);
return *this;
}
iterator operator ++ (int)
{
iterator temp = *this;
utf8::next(it, range_end);
return temp;
}
iterator& operator -- ()
{
utf8::prior(it, range_start);
return *this;
}
iterator operator -- (int)
{
iterator temp = *this;
utf8::prior(it, range_start);
return temp;
}
}; // class iterator
bool operator == (const iterator& rhs) const
{
if (range_start != rhs.range_start || range_end != rhs.range_end)
throw std::logic_error("Comparing utf-8 iterators defined with different ranges");
return (it == rhs.it);
}
bool operator != (const iterator& rhs) const
{
return !(operator == (rhs));
}
iterator& operator ++ ()
{
utf8::next(it, range_end);
return *this;
}
iterator operator ++ (int)
{
iterator temp = *this;
utf8::next(it, range_end);
return temp;
}
iterator& operator -- ()
{
utf8::prior(it, range_start);
return *this;
}
iterator operator -- (int)
{
iterator temp = *this;
utf8::prior(it, range_start);
return temp;
}
}; // class iterator
} // namespace utf8

View File

@@ -32,296 +32,296 @@ DEALINGS IN THE SOFTWARE.
namespace utf8
{
// The typedefs for 8-bit, 16-bit and 32-bit unsigned integers
// You may need to change them to match your system.
// These typedefs have the same names as ones from cstdint, or boost/cstdint
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
// The typedefs for 8-bit, 16-bit and 32-bit unsigned integers
// You may need to change them to match your system.
// These typedefs have the same names as ones from cstdint, or boost/cstdint
typedef unsigned char uint8_t;
typedef unsigned short uint16_t;
typedef unsigned int uint32_t;
// Helper code - not intended to be directly called by the library users. May be changed at any time
namespace internal
{
// Unicode constants
// Leading (high) surrogates: 0xd800 - 0xdbff
// Trailing (low) surrogates: 0xdc00 - 0xdfff
const uint16_t LEAD_SURROGATE_MIN = 0xd800u;
const uint16_t LEAD_SURROGATE_MAX = 0xdbffu;
const uint16_t TRAIL_SURROGATE_MIN = 0xdc00u;
const uint16_t TRAIL_SURROGATE_MAX = 0xdfffu;
const uint16_t LEAD_OFFSET = LEAD_SURROGATE_MIN - (0x10000 >> 10);
const uint32_t SURROGATE_OFFSET = 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN;
// Unicode constants
// Leading (high) surrogates: 0xd800 - 0xdbff
// Trailing (low) surrogates: 0xdc00 - 0xdfff
const uint16_t LEAD_SURROGATE_MIN = 0xd800u;
const uint16_t LEAD_SURROGATE_MAX = 0xdbffu;
const uint16_t TRAIL_SURROGATE_MIN = 0xdc00u;
const uint16_t TRAIL_SURROGATE_MAX = 0xdfffu;
const uint16_t LEAD_OFFSET = LEAD_SURROGATE_MIN - (0x10000 >> 10);
const uint32_t SURROGATE_OFFSET = 0x10000u - (LEAD_SURROGATE_MIN << 10) - TRAIL_SURROGATE_MIN;
// Maximum valid value for a Unicode code point
const uint32_t CODE_POINT_MAX = 0x0010ffffu;
// Maximum valid value for a Unicode code point
const uint32_t CODE_POINT_MAX = 0x0010ffffu;
template<typename octet_type>
inline uint8_t mask8(octet_type oc)
{
return static_cast<uint8_t>(0xff & oc);
template<typename octet_type>
inline uint8_t mask8(octet_type oc)
{
return static_cast<uint8_t>(0xff & oc);
}
template<typename u16_type>
inline uint16_t mask16(u16_type oc)
{
return static_cast<uint16_t>(0xffff & oc);
}
template<typename octet_type>
inline bool is_trail(octet_type oc)
{
return ((utf8::internal::mask8(oc) >> 6) == 0x2);
}
template <typename u16>
inline bool is_lead_surrogate(u16 cp)
{
return (cp >= LEAD_SURROGATE_MIN && cp <= LEAD_SURROGATE_MAX);
}
template <typename u16>
inline bool is_trail_surrogate(u16 cp)
{
return (cp >= TRAIL_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
}
template <typename u16>
inline bool is_surrogate(u16 cp)
{
return (cp >= LEAD_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
}
template <typename u32>
inline bool is_code_point_valid(u32 cp)
{
return (cp <= CODE_POINT_MAX && !utf8::internal::is_surrogate(cp));
}
template <typename octet_iterator>
inline typename std::iterator_traits<octet_iterator>::difference_type
sequence_length(octet_iterator lead_it)
{
uint8_t lead = utf8::internal::mask8(*lead_it);
if (lead < 0x80)
return 1;
else if ((lead >> 5) == 0x6)
return 2;
else if ((lead >> 4) == 0xe)
return 3;
else if ((lead >> 3) == 0x1e)
return 4;
else
return 0;
}
template <typename octet_difference_type>
inline bool is_overlong_sequence(uint32_t cp, octet_difference_type length)
{
if (cp < 0x80) {
if (length != 1)
return true;
}
template<typename u16_type>
inline uint16_t mask16(u16_type oc)
{
return static_cast<uint16_t>(0xffff & oc);
else if (cp < 0x800) {
if (length != 2)
return true;
}
template<typename octet_type>
inline bool is_trail(octet_type oc)
{
return ((utf8::internal::mask8(oc) >> 6) == 0x2);
else if (cp < 0x10000) {
if (length != 3)
return true;
}
template <typename u16>
inline bool is_lead_surrogate(u16 cp)
{
return (cp >= LEAD_SURROGATE_MIN && cp <= LEAD_SURROGATE_MAX);
}
return false;
}
template <typename u16>
inline bool is_trail_surrogate(u16 cp)
{
return (cp >= TRAIL_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
}
enum utf_error {UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT};
template <typename u16>
inline bool is_surrogate(u16 cp)
{
return (cp >= LEAD_SURROGATE_MIN && cp <= TRAIL_SURROGATE_MAX);
}
/// Helper for get_sequence_x
template <typename octet_iterator>
utf_error increase_safely(octet_iterator& it, octet_iterator end)
{
if (++it == end)
return NOT_ENOUGH_ROOM;
template <typename u32>
inline bool is_code_point_valid(u32 cp)
{
return (cp <= CODE_POINT_MAX && !utf8::internal::is_surrogate(cp));
}
if (!utf8::internal::is_trail(*it))
return INCOMPLETE_SEQUENCE;
template <typename octet_iterator>
inline typename std::iterator_traits<octet_iterator>::difference_type
sequence_length(octet_iterator lead_it)
{
uint8_t lead = utf8::internal::mask8(*lead_it);
if (lead < 0x80)
return 1;
else if ((lead >> 5) == 0x6)
return 2;
else if ((lead >> 4) == 0xe)
return 3;
else if ((lead >> 3) == 0x1e)
return 4;
else
return 0;
}
return UTF8_OK;
}
template <typename octet_difference_type>
inline bool is_overlong_sequence(uint32_t cp, octet_difference_type length)
{
if (cp < 0x80) {
if (length != 1)
return true;
}
else if (cp < 0x800) {
if (length != 2)
return true;
}
else if (cp < 0x10000) {
if (length != 3)
return true;
}
#define UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(IT, END) {utf_error ret = increase_safely(IT, END); if (ret != UTF8_OK) return ret;}
return false;
}
/// get_sequence_x functions decode utf-8 sequences of the length x
template <typename octet_iterator>
utf_error get_sequence_1(octet_iterator& it, octet_iterator end, uint32_t& code_point)
{
if (it == end)
return NOT_ENOUGH_ROOM;
enum utf_error {UTF8_OK, NOT_ENOUGH_ROOM, INVALID_LEAD, INCOMPLETE_SEQUENCE, OVERLONG_SEQUENCE, INVALID_CODE_POINT};
code_point = utf8::internal::mask8(*it);
/// Helper for get_sequence_x
template <typename octet_iterator>
utf_error increase_safely(octet_iterator& it, octet_iterator end)
{
if (++it == end)
return NOT_ENOUGH_ROOM;
return UTF8_OK;
}
if (!utf8::internal::is_trail(*it))
return INCOMPLETE_SEQUENCE;
return UTF8_OK;
}
template <typename octet_iterator>
utf_error get_sequence_2(octet_iterator& it, octet_iterator end, uint32_t& code_point)
{
if (it == end)
return NOT_ENOUGH_ROOM;
#define UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(IT, END) {utf_error ret = increase_safely(IT, END); if (ret != UTF8_OK) return ret;}
code_point = utf8::internal::mask8(*it);
/// get_sequence_x functions decode utf-8 sequences of the length x
template <typename octet_iterator>
utf_error get_sequence_1(octet_iterator& it, octet_iterator end, uint32_t& code_point)
{
if (it == end)
return NOT_ENOUGH_ROOM;
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
code_point = utf8::internal::mask8(*it);
code_point = ((code_point << 6) & 0x7ff) + ((*it) & 0x3f);
return UTF8_OK;
}
return UTF8_OK;
}
template <typename octet_iterator>
utf_error get_sequence_2(octet_iterator& it, octet_iterator end, uint32_t& code_point)
{
if (it == end)
return NOT_ENOUGH_ROOM;
code_point = utf8::internal::mask8(*it);
template <typename octet_iterator>
utf_error get_sequence_3(octet_iterator& it, octet_iterator end, uint32_t& code_point)
{
if (it == end)
return NOT_ENOUGH_ROOM;
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
code_point = utf8::internal::mask8(*it);
code_point = ((code_point << 6) & 0x7ff) + ((*it) & 0x3f);
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
return UTF8_OK;
}
code_point = ((code_point << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff);
template <typename octet_iterator>
utf_error get_sequence_3(octet_iterator& it, octet_iterator end, uint32_t& code_point)
{
if (it == end)
return NOT_ENOUGH_ROOM;
code_point = utf8::internal::mask8(*it);
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
code_point += (*it) & 0x3f;
code_point = ((code_point << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff);
return UTF8_OK;
}
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
template <typename octet_iterator>
utf_error get_sequence_4(octet_iterator& it, octet_iterator end, uint32_t& code_point)
{
if (it == end)
return NOT_ENOUGH_ROOM;
code_point += (*it) & 0x3f;
code_point = utf8::internal::mask8(*it);
return UTF8_OK;
}
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
template <typename octet_iterator>
utf_error get_sequence_4(octet_iterator& it, octet_iterator end, uint32_t& code_point)
{
if (it == end)
return NOT_ENOUGH_ROOM;
code_point = ((code_point << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff);
code_point = utf8::internal::mask8(*it);
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
code_point += (utf8::internal::mask8(*it) << 6) & 0xfff;
code_point = ((code_point << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff);
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
code_point += (*it) & 0x3f;
code_point += (utf8::internal::mask8(*it) << 6) & 0xfff;
return UTF8_OK;
}
UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR(it, end)
#undef UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR
code_point += (*it) & 0x3f;
template <typename octet_iterator>
utf_error validate_next(octet_iterator& it, octet_iterator end, uint32_t& code_point)
{
// Save the original value of it so we can go back in case of failure
// Of course, it does not make much sense with i.e. stream iterators
octet_iterator original_it = it;
return UTF8_OK;
}
uint32_t cp = 0;
// Determine the sequence length based on the lead octet
typedef typename std::iterator_traits<octet_iterator>::difference_type octet_difference_type;
const octet_difference_type length = utf8::internal::sequence_length(it);
#undef UTF8_CPP_INCREASE_AND_RETURN_ON_ERROR
template <typename octet_iterator>
utf_error validate_next(octet_iterator& it, octet_iterator end, uint32_t& code_point)
{
// Save the original value of it so we can go back in case of failure
// Of course, it does not make much sense with i.e. stream iterators
octet_iterator original_it = it;
uint32_t cp = 0;
// Determine the sequence length based on the lead octet
typedef typename std::iterator_traits<octet_iterator>::difference_type octet_difference_type;
const octet_difference_type length = utf8::internal::sequence_length(it);
// Get trail octets and calculate the code point
utf_error err = UTF8_OK;
switch (length) {
case 0:
return INVALID_LEAD;
case 1:
err = utf8::internal::get_sequence_1(it, end, cp);
break;
case 2:
err = utf8::internal::get_sequence_2(it, end, cp);
// Get trail octets and calculate the code point
utf_error err = UTF8_OK;
switch (length) {
case 0:
return INVALID_LEAD;
case 1:
err = utf8::internal::get_sequence_1(it, end, cp);
break;
case 3:
err = utf8::internal::get_sequence_3(it, end, cp);
case 2:
err = utf8::internal::get_sequence_2(it, end, cp);
break;
case 4:
err = utf8::internal::get_sequence_4(it, end, cp);
case 3:
err = utf8::internal::get_sequence_3(it, end, cp);
break;
}
case 4:
err = utf8::internal::get_sequence_4(it, end, cp);
break;
}
if (err == UTF8_OK) {
// Decoding succeeded. Now, security checks...
if (utf8::internal::is_code_point_valid(cp)) {
if (!utf8::internal::is_overlong_sequence(cp, length)){
// Passed! Return here.
code_point = cp;
++it;
return UTF8_OK;
}
else
err = OVERLONG_SEQUENCE;
if (err == UTF8_OK) {
// Decoding succeeded. Now, security checks...
if (utf8::internal::is_code_point_valid(cp)) {
if (!utf8::internal::is_overlong_sequence(cp, length)){
// Passed! Return here.
code_point = cp;
++it;
return UTF8_OK;
}
else
err = INVALID_CODE_POINT;
else
err = OVERLONG_SEQUENCE;
}
// Failure branch - restore the original value of the iterator
it = original_it;
return err;
else
err = INVALID_CODE_POINT;
}
template <typename octet_iterator>
inline utf_error validate_next(octet_iterator& it, octet_iterator end) {
uint32_t ignored;
return utf8::internal::validate_next(it, end, ignored);
}
// Failure branch - restore the original value of the iterator
it = original_it;
return err;
}
template <typename octet_iterator>
inline utf_error validate_next(octet_iterator& it, octet_iterator end) {
uint32_t ignored;
return utf8::internal::validate_next(it, end, ignored);
}
} // namespace internal
/// The library API - functions intended to be called by the users
/// The library API - functions intended to be called by the users
// Byte order mark
const uint8_t bom[] = {0xef, 0xbb, 0xbf};
// Byte order mark
const uint8_t bom[] = {0xef, 0xbb, 0xbf};
template <typename octet_iterator>
octet_iterator find_invalid(octet_iterator start, octet_iterator end)
{
octet_iterator result = start;
while (result != end) {
utf8::internal::utf_error err_code = utf8::internal::validate_next(result, end);
if (err_code != internal::UTF8_OK)
return result;
}
return result;
template <typename octet_iterator>
octet_iterator find_invalid(octet_iterator start, octet_iterator end)
{
octet_iterator result = start;
while (result != end) {
utf8::internal::utf_error err_code = utf8::internal::validate_next(result, end);
if (err_code != internal::UTF8_OK)
return result;
}
return result;
}
template <typename octet_iterator>
inline bool is_valid(octet_iterator start, octet_iterator end)
{
return (utf8::find_invalid(start, end) == end);
}
template <typename octet_iterator>
inline bool is_valid(octet_iterator start, octet_iterator end)
{
return (utf8::find_invalid(start, end) == end);
}
template <typename octet_iterator>
inline bool starts_with_bom (octet_iterator it, octet_iterator end)
{
return (
((it != end) && (utf8::internal::mask8(*it++)) == bom[0]) &&
template <typename octet_iterator>
inline bool starts_with_bom (octet_iterator it, octet_iterator end)
{
return (
((it != end) && (utf8::internal::mask8(*it++)) == bom[0]) &&
((it != end) && (utf8::internal::mask8(*it++)) == bom[1]) &&
((it != end) && (utf8::internal::mask8(*it)) == bom[2])
);
}
//Deprecated in release 2.3
template <typename octet_iterator>
inline bool is_bom (octet_iterator it)
{
return (
(utf8::internal::mask8(*it++)) == bom[0] &&
);
}
//Deprecated in release 2.3
template <typename octet_iterator>
inline bool is_bom (octet_iterator it)
{
return (
(utf8::internal::mask8(*it++)) == bom[0] &&
(utf8::internal::mask8(*it++)) == bom[1] &&
(utf8::internal::mask8(*it)) == bom[2]
);
}
);
}
} // namespace utf8
#endif // header guard

View File

@@ -32,195 +32,195 @@ DEALINGS IN THE SOFTWARE.
namespace utf8
{
namespace unchecked
{
template <typename octet_iterator>
octet_iterator append(uint32_t cp, octet_iterator result)
{
if (cp < 0x80) // one octet
*(result++) = static_cast<uint8_t>(cp);
else if (cp < 0x800) { // two octets
*(result++) = static_cast<uint8_t>((cp >> 6) | 0xc0);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
}
else if (cp < 0x10000) { // three octets
*(result++) = static_cast<uint8_t>((cp >> 12) | 0xe0);
*(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
}
else { // four octets
*(result++) = static_cast<uint8_t>((cp >> 18) | 0xf0);
*(result++) = static_cast<uint8_t>(((cp >> 12) & 0x3f)| 0x80);
*(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
}
return result;
}
namespace unchecked
{
template <typename octet_iterator>
octet_iterator append(uint32_t cp, octet_iterator result)
{
if (cp < 0x80) // one octet
*(result++) = static_cast<uint8_t>(cp);
else if (cp < 0x800) { // two octets
*(result++) = static_cast<uint8_t>((cp >> 6) | 0xc0);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
}
else if (cp < 0x10000) { // three octets
*(result++) = static_cast<uint8_t>((cp >> 12) | 0xe0);
*(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
}
else { // four octets
*(result++) = static_cast<uint8_t>((cp >> 18) | 0xf0);
*(result++) = static_cast<uint8_t>(((cp >> 12) & 0x3f)| 0x80);
*(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
*(result++) = static_cast<uint8_t>((cp & 0x3f) | 0x80);
}
return result;
}
template <typename octet_iterator>
uint32_t next(octet_iterator& it)
{
uint32_t cp = utf8::internal::mask8(*it);
typename std::iterator_traits<octet_iterator>::difference_type length = utf8::internal::sequence_length(it);
switch (length) {
case 1:
break;
case 2:
it++;
cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f);
break;
case 3:
++it;
cp = ((cp << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff);
++it;
cp += (*it) & 0x3f;
break;
case 4:
++it;
cp = ((cp << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff);
++it;
cp += (utf8::internal::mask8(*it) << 6) & 0xfff;
++it;
cp += (*it) & 0x3f;
break;
}
template <typename octet_iterator>
uint32_t next(octet_iterator& it)
{
uint32_t cp = utf8::internal::mask8(*it);
typename std::iterator_traits<octet_iterator>::difference_type length = utf8::internal::sequence_length(it);
switch (length) {
case 1:
break;
case 2:
it++;
cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f);
break;
case 3:
++it;
return cp;
cp = ((cp << 12) & 0xffff) + ((utf8::internal::mask8(*it) << 6) & 0xfff);
++it;
cp += (*it) & 0x3f;
break;
case 4:
++it;
cp = ((cp << 18) & 0x1fffff) + ((utf8::internal::mask8(*it) << 12) & 0x3ffff);
++it;
cp += (utf8::internal::mask8(*it) << 6) & 0xfff;
++it;
cp += (*it) & 0x3f;
break;
}
++it;
return cp;
}
template <typename octet_iterator>
uint32_t peek_next(octet_iterator it)
{
return utf8::unchecked::next(it);
}
template <typename octet_iterator>
uint32_t prior(octet_iterator& it)
{
while (utf8::internal::is_trail(*(--it))) ;
octet_iterator temp = it;
return utf8::unchecked::next(temp);
}
// Deprecated in versions that include prior, but only for the sake of consistency (see utf8::previous)
template <typename octet_iterator>
inline uint32_t previous(octet_iterator& it)
{
return utf8::unchecked::prior(it);
}
template <typename octet_iterator, typename distance_type>
void advance (octet_iterator& it, distance_type n)
{
for (distance_type i = 0; i < n; ++i)
utf8::unchecked::next(it);
}
template <typename octet_iterator>
typename std::iterator_traits<octet_iterator>::difference_type
distance (octet_iterator first, octet_iterator last)
{
typename std::iterator_traits<octet_iterator>::difference_type dist;
for (dist = 0; first < last; ++dist)
utf8::unchecked::next(first);
return dist;
}
template <typename u16bit_iterator, typename octet_iterator>
octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
{
while (start != end) {
uint32_t cp = utf8::internal::mask16(*start++);
// Take care of surrogate pairs first
if (utf8::internal::is_lead_surrogate(cp)) {
uint32_t trail_surrogate = utf8::internal::mask16(*start++);
cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
}
result = utf8::unchecked::append(cp, result);
}
return result;
}
template <typename octet_iterator>
uint32_t peek_next(octet_iterator it)
{
return utf8::unchecked::next(it);
template <typename u16bit_iterator, typename octet_iterator>
u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
{
while (start < end) {
uint32_t cp = utf8::unchecked::next(start);
if (cp > 0xffff) { //make a surrogate pair
*result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET);
*result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
}
else
*result++ = static_cast<uint16_t>(cp);
}
return result;
}
template <typename octet_iterator>
uint32_t prior(octet_iterator& it)
{
while (utf8::internal::is_trail(*(--it))) ;
octet_iterator temp = it;
return utf8::unchecked::next(temp);
}
template <typename octet_iterator, typename u32bit_iterator>
octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)
{
while (start != end)
result = utf8::unchecked::append(*(start++), result);
// Deprecated in versions that include prior, but only for the sake of consistency (see utf8::previous)
template <typename octet_iterator>
inline uint32_t previous(octet_iterator& it)
{
return utf8::unchecked::prior(it);
}
return result;
}
template <typename octet_iterator, typename distance_type>
void advance (octet_iterator& it, distance_type n)
{
for (distance_type i = 0; i < n; ++i)
utf8::unchecked::next(it);
}
template <typename octet_iterator, typename u32bit_iterator>
u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)
{
while (start < end)
(*result++) = utf8::unchecked::next(start);
template <typename octet_iterator>
typename std::iterator_traits<octet_iterator>::difference_type
distance (octet_iterator first, octet_iterator last)
{
typename std::iterator_traits<octet_iterator>::difference_type dist;
for (dist = 0; first < last; ++dist)
utf8::unchecked::next(first);
return dist;
}
return result;
}
template <typename u16bit_iterator, typename octet_iterator>
octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
{
while (start != end) {
uint32_t cp = utf8::internal::mask16(*start++);
// Take care of surrogate pairs first
if (utf8::internal::is_lead_surrogate(cp)) {
uint32_t trail_surrogate = utf8::internal::mask16(*start++);
cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
}
result = utf8::unchecked::append(cp, result);
}
return result;
}
// The iterator class
template <typename octet_iterator>
class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t> {
octet_iterator it;
public:
iterator () {};
explicit iterator (const octet_iterator& octet_it): it(octet_it) {}
// the default "big three" are OK
octet_iterator base () const { return it; }
uint32_t operator * () const
{
octet_iterator temp = it;
return utf8::unchecked::next(temp);
}
bool operator == (const iterator& rhs) const
{
return (it == rhs.it);
}
bool operator != (const iterator& rhs) const
{
return !(operator == (rhs));
}
iterator& operator ++ ()
{
::std::advance(it, utf8::internal::sequence_length(it));
return *this;
}
iterator operator ++ (int)
{
iterator temp = *this;
::std::advance(it, utf8::internal::sequence_length(it));
return temp;
}
iterator& operator -- ()
{
utf8::unchecked::prior(it);
return *this;
}
iterator operator -- (int)
{
iterator temp = *this;
utf8::unchecked::prior(it);
return temp;
}
}; // class iterator
template <typename u16bit_iterator, typename octet_iterator>
u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
{
while (start < end) {
uint32_t cp = utf8::unchecked::next(start);
if (cp > 0xffff) { //make a surrogate pair
*result++ = static_cast<uint16_t>((cp >> 10) + internal::LEAD_OFFSET);
*result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
}
else
*result++ = static_cast<uint16_t>(cp);
}
return result;
}
template <typename octet_iterator, typename u32bit_iterator>
octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)
{
while (start != end)
result = utf8::unchecked::append(*(start++), result);
return result;
}
template <typename octet_iterator, typename u32bit_iterator>
u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)
{
while (start < end)
(*result++) = utf8::unchecked::next(start);
return result;
}
// The iterator class
template <typename octet_iterator>
class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t> {
octet_iterator it;
public:
iterator () {};
explicit iterator (const octet_iterator& octet_it): it(octet_it) {}
// the default "big three" are OK
octet_iterator base () const { return it; }
uint32_t operator * () const
{
octet_iterator temp = it;
return utf8::unchecked::next(temp);
}
bool operator == (const iterator& rhs) const
{
return (it == rhs.it);
}
bool operator != (const iterator& rhs) const
{
return !(operator == (rhs));
}
iterator& operator ++ ()
{
::std::advance(it, utf8::internal::sequence_length(it));
return *this;
}
iterator operator ++ (int)
{
iterator temp = *this;
::std::advance(it, utf8::internal::sequence_length(it));
return temp;
}
iterator& operator -- ()
{
utf8::unchecked::prior(it);
return *this;
}
iterator operator -- (int)
{
iterator temp = *this;
utf8::unchecked::prior(it);
return temp;
}
}; // class iterator
} // namespace utf8::unchecked
} // namespace utf8::unchecked
} // namespace utf8

View File

@@ -22,17 +22,18 @@
bool isEmpty(Int8*, size_t);
unsigned short swapU16(unsigned short val );
short swap16 (short val );
short swap16 (short val );
unsigned int swapU32(unsigned int val);
int swap32 (int val );
long long swap64 (long long val);
int swap32 (int val );
long long swap64 (long long val);
float swapFloat(float val);
double swapDouble(double val);
float swapFloat(float val);
double swapDouble(double val);
bool isSystemBigEndian();
void fillRandom(Uint8 * rndArea, Uint8 count);
void yaz0Decode(Uint8* src, Uint8* dst, Uint32 uncompressedSize);
#endif