#include "doorkeeper/helpers/tylermapsource.hpp" #include #if defined(__GNUC__) # include #else # error "Unsupported compiler" #endif #include #include //Vorbis comment specification: https://www.xiph.org/vorbis/doc/v-comment.html //The comment header logically is a list of eight-bit-clean vectors; the number //of vectors is bounded to 2^32-1 and the length of each vector is limited to //2^32-1 bytes. The vector length is encoded; the vector contents themselves //are not null terminated. In addition to the vector list, there is a single //vector for vendor name (also 8 bit clean, length encoded in 32 bits). For //example, the 1.0 release of libvorbis set the vendor string to "Xiph.Org //libVorbis I 20020717". // //The comment header is decoded as follows: // // 1) [vendor_length] = read an unsigned integer of 32 bits // 2) [vendor_string] = read a UTF-8 vector as [vendor_length] octets // 3) [user_comment_list_length] = read an unsigned integer of 32 bits // 4) iterate [user_comment_list_length] times { // 5) [length] = read an unsigned integer of 32 bits // 6) this iteration's user comment = read a UTF-8 vector as [length] octets // } namespace dkh { namespace { const uint32_t g_signature = 0x52504B44; //DKPR 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) { 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(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) { parStream.read(reinterpret_cast(&parHeader), sizeof(TylerMapHeader)); parHeader = lil_endian_to_machine(parHeader); if (g_signature != parHeader.signature) throw InvalidMapFileException(); } 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_entries = 200; 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) { read_string(parStream, full_string); auto equal_pos = full_string.find('='); if (std::string::npos == equal_pos) throw InvalidMapFileException(); parComments[full_string.substr(0, equal_pos)] = full_string.substr(equal_pos + 1); } } } //namespace implem } //namespace dkh