diff --git a/include/doorkeeper/helpers/tylermapsource.hpp b/include/doorkeeper/helpers/tylermapsource.hpp index 612f171..af25d34 100644 --- a/include/doorkeeper/helpers/tylermapsource.hpp +++ b/include/doorkeeper/helpers/tylermapsource.hpp @@ -12,6 +12,10 @@ #include #include +#if !defined(NDEBUG) && !defined(WITH_TYLERMAP_WRITER) +# define WITH_TYLERMAP_WRITER +#endif + namespace dkh { template class TylerMapSource : public dk::BaseMapSource { @@ -43,6 +47,18 @@ namespace dkh { std::unique_ptr m_stream; }; +#if defined(WITH_TYLERMAP_WRITER) + class TylerMapWriter { + public: + TylerMapWriter ( void ) = delete; + TylerMapWriter ( const TylerMapWriter& ) = delete; + TylerMapWriter ( TylerMapWriter&& ) = default; + + private: + std::unique_ptr m_stream; + }; +#endif + class InvalidMapFileException : public std::exception { }; diff --git a/src/tylermapsource.cpp b/src/tylermapsource.cpp index ce157e4..f50c68c 100644 --- a/src/tylermapsource.cpp +++ b/src/tylermapsource.cpp @@ -1,4 +1,5 @@ #include "doorkeeper/helpers/tylermapsource.hpp" +#include #if defined(__GNUC__) # include @@ -31,34 +32,94 @@ namespace dkh { namespace { const uint32_t g_signature = 0x52504B44; //DKPR - uint32_t read_uint32 (std::istream& parStream, uint32_t parMax=0xFFFFFFFF) { - uint32_t retval; -#if __BYTE_ORDER == __LITTLE_ENDIAN - parStream.read(reinterpret_cast(&retval), sizeof(uint32_t)); -#else -# error "Unknown endianness, please write the implementation" + template T lil_endian_to_machine ( T parIn ) a_pure; + template<> uint32_t lil_endian_to_machine ( uint32_t parIn ) a_pure; + template<> uint16_t lil_endian_to_machine ( uint16_t parIn ) a_pure; + template<> uint8_t lil_endian_to_machine ( uint8_t parIn ) a_pure; + template<> implem::TylerMapHeader lil_endian_to_machine ( implem::TylerMapHeader parIn ) a_pure; +#if defined(WITH_TYLERMAP_WRITER) + template T machine_to_lil_endian ( T parIn ) a_pure; + template<> uint32_t machine_to_lil_endian ( uint32_t parIn ) a_pure; + template<> uint16_t machine_to_lil_endian ( uint16_t parIn ) a_pure; + template<> uint8_t machine_to_lil_endian ( uint8_t parIn ) a_pure; + template<> implem::TylerMapHeader machine_to_lil_endian ( implem::TylerMapHeader parIn ) a_pure; #endif + + template<> uint32_t lil_endian_to_machine (uint32_t parIn) { return le32toh(parIn); } + template<> uint16_t lil_endian_to_machine (uint16_t parIn) { return le16toh(parIn); } + template<> uint8_t lil_endian_to_machine (uint8_t parIn) { return parIn; } + + template<> implem::TylerMapHeader lil_endian_to_machine (implem::TylerMapHeader parIn) { + parIn.signature = lil_endian_to_machine(parIn.signature); + parIn.file_size = lil_endian_to_machine(parIn.file_size); + parIn.version_major = lil_endian_to_machine(parIn.version_major); + parIn.version_minor = lil_endian_to_machine(parIn.version_minor); + parIn.layer_count = lil_endian_to_machine(parIn.layer_count); + parIn.map_type = lil_endian_to_machine(parIn.map_type); + parIn.map_info_type = lil_endian_to_machine(parIn.map_info_type); + parIn.dimensions = lil_endian_to_machine(parIn.dimensions); + return parIn; + } + +#if defined(WITH_TYLERMAP_WRITER) + template<> uint32_t machine_to_lil_endian (uint32_t parIn) { return htole32(parIn); } + template<> uint16_t machine_to_lil_endian (uint16_t parIn) { return htole16(parIn); } + template<> uint8_t machine_to_lil_endian (uint8_t parIn) { return parIn; } + + template<> implem::TylerMapHeader machine_to_lil_endian (implem::TylerMapHeader parIn) { + parIn.signature = machine_to_lil_endian(parIn.signature); + parIn.file_size = machine_to_lil_endian(parIn.file_size); + parIn.version_major = machine_to_lil_endian(parIn.version_major); + parIn.version_minor = machine_to_lil_endian(parIn.version_minor); + parIn.layer_count = machine_to_lil_endian(parIn.layer_count); + parIn.map_type = machine_to_lil_endian(parIn.map_type); + parIn.map_info_type = machine_to_lil_endian(parIn.map_info_type); + parIn.dimensions = machine_to_lil_endian(parIn.dimensions); + return parIn; + } +#endif + + template + T read_int (std::istream& parStream, T parMax=std::numeric_limits::max()) { + T retval; + parStream.read(reinterpret_cast(&retval), sizeof(T)); + retval = lil_endian_to_machine(retval); + if (retval > parMax) throw InvalidMapFileException(); return retval; } - std::string& read_string (std::istream& parStream, std::string& parString, uint32_t parLength) { + std::string& read_string (std::istream& parStream, std::string& parString) { + const uint32_t max_string_length = 1024 * 2; + const uint32_t len = std::min(read_int(parStream), max_string_length); std::istream_iterator itstream(parStream); - parString.resize(parLength); - std::copy_n(itstream, parLength, std::insert_iterator(parString, parString.begin())); + parString.resize(len); + std::copy_n(itstream, len, std::insert_iterator(parString, parString.begin())); return parString; } + +#if defined(WITH_TYLERMAP_WRITER) + template + void write_int (std::ostream& parStream, T parValue) { + parValue = machine_to_lil_endian(parValue); + parStream.write(reinterpret_cast(&parValue), sizeof(T)); + } + + void write_string (std::ostream& parStream, const std::string& parString) { + const uint32_t len = static_cast(parString.size()); + write_int(parStream, len); + parStream.write(parString.c_str(), parString.size()); + } +#endif } //unnamed namespace namespace implem { void read_header (std::istream& parStream, TylerMapHeader& parHeader) { -#if __BYTE_ORDER == __LITTLE_ENDIAN parStream.read(reinterpret_cast(&parHeader), sizeof(TylerMapHeader)); -#else -# error "Unknown endianness, please write the implementation" -#endif + parHeader = lil_endian_to_machine(parHeader); + if (g_signature != parHeader.signature) throw InvalidMapFileException(); } @@ -66,18 +127,13 @@ namespace dkh { void read_comments (std::istream& parStream, std::string& parVendor, CommentMap& parComments) { //Technically there is no limit on strings other than the uint32 //max value, but wth - const uint32_t max_string_length = 1024 * 2; const uint32_t max_entries = 200; - { - const uint32_t vendor_length = read_uint32(parStream, max_string_length); - read_string(parStream, parVendor, vendor_length); - } - const uint32_t list_length = read_uint32(parStream, max_entries); + read_string(parStream, parVendor); + const uint32_t list_length = read_int(parStream, max_entries); std::string full_string; for (uint32_t z = 0; z < list_length; ++z) { - const uint32_t string_length = read_uint32(parStream, max_string_length); - read_string(parStream, full_string, string_length); + read_string(parStream, full_string); auto equal_pos = full_string.find('='); if (std::string::npos == equal_pos) throw InvalidMapFileException();