#include "doorkeeper/helpers/tylermapsource.hpp" #include #include #include #include #include #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<> uint64_t lil_endian_to_machine ( uint64_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; template<> implem::TylerMapLayerHeader lil_endian_to_machine ( implem::TylerMapLayerHeader parIn ) a_pure; #if defined(WITH_TYLERMAP_WRITER) template T machine_to_lil_endian ( T parIn ) a_pure; template<> uint64_t machine_to_lil_endian ( uint64_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; template<> implem::TylerMapLayerHeader machine_to_lil_endian ( implem::TylerMapLayerHeader parIn ) a_pure; #endif template<> uint64_t lil_endian_to_machine (uint64_t parIn) { return le64toh(parIn); } 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; } bool isdigit (char parChar) a_pure a_flatten; 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.hashing_algorithm = lil_endian_to_machine(parIn.hashing_algorithm); parIn.dimensions = lil_endian_to_machine(parIn.dimensions); parIn.dimensions_start = lil_endian_to_machine(parIn.dimensions_start); parIn.comment_start = lil_endian_to_machine(parIn.comment_start); parIn.layer_start = lil_endian_to_machine(parIn.layer_start); return parIn; } template<> implem::TylerMapLayerHeader lil_endian_to_machine (implem::TylerMapLayerHeader parIn) { parIn.layer_length = lil_endian_to_machine(parIn.layer_length); parIn.layer_format = lil_endian_to_machine(parIn.layer_format); return parIn; } #if defined(WITH_TYLERMAP_WRITER) template<> uint64_t machine_to_lil_endian (uint64_t parIn) { return htole64(parIn); } 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.hashing_algorithm = machine_to_lil_endian(parIn.hashing_algorithm); parIn.dimensions = machine_to_lil_endian(parIn.dimensions); parIn.dimensions_start = machine_to_lil_endian(parIn.dimensions_start); parIn.comment_start = machine_to_lil_endian(parIn.comment_start); parIn.layer_start = machine_to_lil_endian(parIn.layer_start); return parIn; } template<> implem::TylerMapLayerHeader machine_to_lil_endian (implem::TylerMapLayerHeader parIn) { parIn.layer_length = machine_to_lil_endian(parIn.layer_length); parIn.layer_format = machine_to_lil_endian(parIn.layer_format); 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 bool isdigit (char parChar) { return std::isdigit(parChar); } } //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); } } void read_layer_header (std::istream& parStream, TylerMapLayerHeader& parHeader) { parStream.read(reinterpret_cast(&parHeader), sizeof(TylerMapLayerHeader)); parHeader = lil_endian_to_machine(parHeader); } void read_dimensions (std::istream& parStream, uint32_t parDimensions, std::vector& parMap, std::vector& parTile) { parMap.reserve(parMap.size() + parDimensions); for (uint32_t z = 0; z < parDimensions; ++z) { const auto dim = read_int(parStream); parMap.push_back(dim); } parTile.reserve(parTile.size() + parDimensions); for (uint32_t z = 0; z < parDimensions; ++z) { const auto dim = read_int(parStream); parTile.push_back(dim); } } void extract_array_from_comments (std::vector& parOut, const std::string& parPrefix, const std::map& parMap) { typedef decltype(std::distance(parPrefix.begin(), parPrefix.end())) diff_type; using std::size_t; using boost::lexical_cast; parOut.clear(); for (const auto& itcurr : parMap) { const std::string& key = itcurr.first; if (parPrefix.size() + 3 > key.size()) continue; if (not std::equal(parPrefix.begin(), parPrefix.end(), key.begin())) continue; if (key[parPrefix.size()] != '[') continue; auto it_past_index = std::find_if_not(key.begin() + parPrefix.size() + 1, key.end(), &isdigit); if (it_past_index == key.end()) continue; if (*it_past_index != ']') continue; if (std::distance(key.begin(), it_past_index) + 1 != static_cast(key.size())) continue; boost::string_ref index_part(key); const auto dist = std::distance(key.begin(), it_past_index); DK_ASSERT(dist > static_cast(parPrefix.size() + 1)); index_part = index_part.substr(parPrefix.size() + 1, dist - parPrefix.size() - 1); const size_t index = lexical_cast(index_part); if (index >= parMap.size()) continue; if (parOut.size() <= index) parOut.resize(index + 1); parOut[index] = itcurr.second; } } } //namespace implem } //namespace dkh