diff --git a/libwvdrmengine/cdm/core/test/reboot_test.cpp b/libwvdrmengine/cdm/core/test/reboot_test.cpp index 5b9a2280..58d736e1 100644 --- a/libwvdrmengine/cdm/core/test/reboot_test.cpp +++ b/libwvdrmengine/cdm/core/test/reboot_test.cpp @@ -13,8 +13,8 @@ #include "log.h" using wvutil::a2b_hex; -using wvutil::b2a_hex; using wvutil::FileSystem; +using wvutil::unlimited_b2a_hex; namespace wvcdm { FileSystem* RebootTest::file_system_; @@ -33,11 +33,11 @@ std::string EncodeString(const std::string& data) { // If there are any unprintable characters, except whitespace, or if we // close a brace before we open it, then just use hex. if (!printable || braces_count < 0) { - return "0x" + b2a_hex(data) + ","; + return "0x" + unlimited_b2a_hex(data) + ","; } } // If we left any braces open, then use hex. - if (braces_count != 0) return "0x" + b2a_hex(data) + ","; + if (braces_count != 0) return "0x" + unlimited_b2a_hex(data) + ","; return "{" + data + "},"; } @@ -51,12 +51,12 @@ std::string EncodeKey(const std::string& data) { } // When decoding, we assume that a key starting with "0x" is in hex. So we // can't have any keys that start with "0x". - if (data.substr(0, 2) == "0x") return "0x" + b2a_hex(data) + ":"; + if (data.substr(0, 2) == "0x") return "0x" + unlimited_b2a_hex(data) + ":"; // If the key is just is not printable, or if it has unmatched braces, then // we use hex. Otherwise, we surround the whole string with braces. for (size_t i = 0; i < data.length(); i++) { if (!isprint(data[i]) || (data[i] == ':')) { - return "0x" + b2a_hex(data) + ":"; + return "0x" + unlimited_b2a_hex(data) + ":"; } } return data + ":"; @@ -247,6 +247,10 @@ TEST_F(RebootTest, TestDumpUtil) { // This key looks like it might be hex. It should show up as hex in the // save file. map1["0x_bad_key_00"] = "value is ok"; + std::string big_string = "start with something {binary"; + // Double big_string 8 times, i.e. times 256, so it's bigger than 2k: + for (int i = 0; i < 8; i++) big_string = big_string + big_string; + map1["big_file"] = big_string; const std::string dump2 = DumpData(map1); std::map map3; EXPECT_TRUE(ParseDump(dump2, &map3)); diff --git a/libwvdrmengine/cdm/util/include/string_conversions.h b/libwvdrmengine/cdm/util/include/string_conversions.h index 471d2fe3..e5944bd6 100644 --- a/libwvdrmengine/cdm/util/include/string_conversions.h +++ b/libwvdrmengine/cdm/util/include/string_conversions.h @@ -20,10 +20,15 @@ CORE_UTIL_EXPORT std::vector a2b_hex(const std::string& label, const std::string& b); CORE_UTIL_EXPORT std::string a2bs_hex(const std::string& b); -// Binary to ASCII hex conversion. +// Binary to ASCII hex conversion. The default versions limit output to 2k to +// protect us from log spam. The unlimited version has no length limit. CORE_UTIL_EXPORT std::string b2a_hex(const std::vector& b); +CORE_UTIL_EXPORT std::string unlimited_b2a_hex(const std::vector& b); CORE_UTIL_EXPORT std::string b2a_hex(const std::string& b); +CORE_UTIL_EXPORT std::string unlimited_b2a_hex(const std::string& b); CORE_UTIL_EXPORT std::string HexEncode(const uint8_t* bytes, size_t size); +CORE_UTIL_EXPORT std::string UnlimitedHexEncode(const uint8_t* bytes, + size_t size); // Base64 encoding/decoding. // Converts binary data into the ASCII Base64 character set and vice diff --git a/libwvdrmengine/cdm/util/src/string_conversions.cpp b/libwvdrmengine/cdm/util/src/string_conversions.cpp index 9fd3251b..37ac2ef3 100644 --- a/libwvdrmengine/cdm/util/src/string_conversions.cpp +++ b/libwvdrmengine/cdm/util/src/string_conversions.cpp @@ -201,20 +201,34 @@ std::string b2a_hex(const std::vector& byte) { return HexEncode(byte.data(), byte.size()); } +std::string unlimited_b2a_hex(const std::vector& byte) { + if (byte.empty()) return ""; + return UnlimitedHexEncode(byte.data(), byte.size()); +} + std::string b2a_hex(const std::string& byte) { if (byte.empty()) return ""; return HexEncode(reinterpret_cast(byte.data()), byte.length()); } +std::string unlimited_b2a_hex(const std::string& byte) { + if (byte.empty()) return ""; + return UnlimitedHexEncode(reinterpret_cast(byte.data()), + byte.length()); +} + std::string HexEncode(const uint8_t* in_buffer, size_t size) { - static const char kHexChars[] = "0123456789ABCDEF"; - if (size == 0) return ""; constexpr unsigned int kMaxSafeSize = 2048; if (size > kMaxSafeSize) size = kMaxSafeSize; + return UnlimitedHexEncode(in_buffer, size); +} + +std::string UnlimitedHexEncode(const uint8_t* in_buffer, size_t size) { + static const char kHexChars[] = "0123456789ABCDEF"; + if (size == 0) return ""; // Each input byte creates two output hex characters. std::string out_buffer(size * 2, '\0'); - for (unsigned int i = 0; i < size; ++i) { char byte = in_buffer[i]; out_buffer[(i << 1)] = kHexChars[(byte >> 4) & 0xf];