Clean up and handle endianness.
This commit is contained in:
parent
f1a6cac62b
commit
12db1d9638
2 changed files with 93 additions and 21 deletions
|
@ -12,6 +12,10 @@
|
||||||
#include <ciso646>
|
#include <ciso646>
|
||||||
#include <exception>
|
#include <exception>
|
||||||
|
|
||||||
|
#if !defined(NDEBUG) && !defined(WITH_TYLERMAP_WRITER)
|
||||||
|
# define WITH_TYLERMAP_WRITER
|
||||||
|
#endif
|
||||||
|
|
||||||
namespace dkh {
|
namespace dkh {
|
||||||
template <typename T, uint32_t D>
|
template <typename T, uint32_t D>
|
||||||
class TylerMapSource : public dk::BaseMapSource<T, D> {
|
class TylerMapSource : public dk::BaseMapSource<T, D> {
|
||||||
|
@ -43,6 +47,18 @@ namespace dkh {
|
||||||
std::unique_ptr<std::istream> m_stream;
|
std::unique_ptr<std::istream> m_stream;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#if defined(WITH_TYLERMAP_WRITER)
|
||||||
|
class TylerMapWriter {
|
||||||
|
public:
|
||||||
|
TylerMapWriter ( void ) = delete;
|
||||||
|
TylerMapWriter ( const TylerMapWriter& ) = delete;
|
||||||
|
TylerMapWriter ( TylerMapWriter&& ) = default;
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::unique_ptr<std::ostream> m_stream;
|
||||||
|
};
|
||||||
|
#endif
|
||||||
|
|
||||||
class InvalidMapFileException : public std::exception {
|
class InvalidMapFileException : public std::exception {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include "doorkeeper/helpers/tylermapsource.hpp"
|
#include "doorkeeper/helpers/tylermapsource.hpp"
|
||||||
|
#include <limits>
|
||||||
|
|
||||||
#if defined(__GNUC__)
|
#if defined(__GNUC__)
|
||||||
# include <endian.h>
|
# include <endian.h>
|
||||||
|
@ -31,34 +32,94 @@ namespace dkh {
|
||||||
namespace {
|
namespace {
|
||||||
const uint32_t g_signature = 0x52504B44; //DKPR
|
const uint32_t g_signature = 0x52504B44; //DKPR
|
||||||
|
|
||||||
uint32_t read_uint32 (std::istream& parStream, uint32_t parMax=0xFFFFFFFF) {
|
template <typename T> T lil_endian_to_machine ( T parIn ) a_pure;
|
||||||
uint32_t retval;
|
template<> uint32_t lil_endian_to_machine ( uint32_t parIn ) a_pure;
|
||||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
template<> uint16_t lil_endian_to_machine ( uint16_t parIn ) a_pure;
|
||||||
parStream.read(reinterpret_cast<char*>(&retval), sizeof(uint32_t));
|
template<> uint8_t lil_endian_to_machine ( uint8_t parIn ) a_pure;
|
||||||
#else
|
template<> implem::TylerMapHeader lil_endian_to_machine ( implem::TylerMapHeader parIn ) a_pure;
|
||||||
# error "Unknown endianness, please write the implementation"
|
#if defined(WITH_TYLERMAP_WRITER)
|
||||||
|
template <typename T> 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
|
#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 <typename T>
|
||||||
|
T read_int (std::istream& parStream, T parMax=std::numeric_limits<T>::max()) {
|
||||||
|
T retval;
|
||||||
|
parStream.read(reinterpret_cast<char*>(&retval), sizeof(T));
|
||||||
|
retval = lil_endian_to_machine(retval);
|
||||||
|
|
||||||
if (retval > parMax)
|
if (retval > parMax)
|
||||||
throw InvalidMapFileException();
|
throw InvalidMapFileException();
|
||||||
|
|
||||||
return retval;
|
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<uint32_t>(parStream), max_string_length);
|
||||||
std::istream_iterator<char> itstream(parStream);
|
std::istream_iterator<char> itstream(parStream);
|
||||||
parString.resize(parLength);
|
parString.resize(len);
|
||||||
std::copy_n(itstream, parLength, std::insert_iterator<std::string>(parString, parString.begin()));
|
std::copy_n(itstream, len, std::insert_iterator<std::string>(parString, parString.begin()));
|
||||||
return parString;
|
return parString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if defined(WITH_TYLERMAP_WRITER)
|
||||||
|
template <typename T>
|
||||||
|
void write_int (std::ostream& parStream, T parValue) {
|
||||||
|
parValue = machine_to_lil_endian(parValue);
|
||||||
|
parStream.write(reinterpret_cast<const char*>(&parValue), sizeof(T));
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_string (std::ostream& parStream, const std::string& parString) {
|
||||||
|
const uint32_t len = static_cast<uint32_t>(parString.size());
|
||||||
|
write_int<uint32_t>(parStream, len);
|
||||||
|
parStream.write(parString.c_str(), parString.size());
|
||||||
|
}
|
||||||
|
#endif
|
||||||
} //unnamed namespace
|
} //unnamed namespace
|
||||||
|
|
||||||
namespace implem {
|
namespace implem {
|
||||||
void read_header (std::istream& parStream, TylerMapHeader& parHeader) {
|
void read_header (std::istream& parStream, TylerMapHeader& parHeader) {
|
||||||
#if __BYTE_ORDER == __LITTLE_ENDIAN
|
|
||||||
parStream.read(reinterpret_cast<char*>(&parHeader), sizeof(TylerMapHeader));
|
parStream.read(reinterpret_cast<char*>(&parHeader), sizeof(TylerMapHeader));
|
||||||
#else
|
parHeader = lil_endian_to_machine(parHeader);
|
||||||
# error "Unknown endianness, please write the implementation"
|
|
||||||
#endif
|
|
||||||
if (g_signature != parHeader.signature)
|
if (g_signature != parHeader.signature)
|
||||||
throw InvalidMapFileException();
|
throw InvalidMapFileException();
|
||||||
}
|
}
|
||||||
|
@ -66,18 +127,13 @@ namespace dkh {
|
||||||
void read_comments (std::istream& parStream, std::string& parVendor, CommentMap& parComments) {
|
void read_comments (std::istream& parStream, std::string& parVendor, CommentMap& parComments) {
|
||||||
//Technically there is no limit on strings other than the uint32
|
//Technically there is no limit on strings other than the uint32
|
||||||
//max value, but wth
|
//max value, but wth
|
||||||
const uint32_t max_string_length = 1024 * 2;
|
|
||||||
const uint32_t max_entries = 200;
|
const uint32_t max_entries = 200;
|
||||||
|
|
||||||
{
|
read_string(parStream, parVendor);
|
||||||
const uint32_t vendor_length = read_uint32(parStream, max_string_length);
|
const uint32_t list_length = read_int<uint32_t>(parStream, max_entries);
|
||||||
read_string(parStream, parVendor, vendor_length);
|
|
||||||
}
|
|
||||||
const uint32_t list_length = read_uint32(parStream, max_entries);
|
|
||||||
std::string full_string;
|
std::string full_string;
|
||||||
for (uint32_t z = 0; z < list_length; ++z) {
|
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);
|
||||||
read_string(parStream, full_string, string_length);
|
|
||||||
auto equal_pos = full_string.find('=');
|
auto equal_pos = full_string.find('=');
|
||||||
if (std::string::npos == equal_pos)
|
if (std::string::npos == equal_pos)
|
||||||
throw InvalidMapFileException();
|
throw InvalidMapFileException();
|
||||||
|
|
Loading…
Add table
Reference in a new issue