diff options
author | 2023-10-11 18:35:42 +0000 | |
---|---|---|
committer | 2023-10-12 21:27:30 +0000 | |
commit | 891f6b06b98208d4dd8c2f837b5138e7f1b2aa3b (patch) | |
tree | a01fce4a0a866a54a329c3629b9c995fa7132c72 /libs/binder/Utils.cpp | |
parent | bb07b9837531fe6cd42a1130ce09d403df331197 (diff) |
Copy HexString to libbinder
Bug: 302723053
Test: mma
Change-Id: I5c7a71a91b7dc95bfa0cd653eabe554bdd3f7812
Diffstat (limited to 'libs/binder/Utils.cpp')
-rw-r--r-- | libs/binder/Utils.cpp | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/libs/binder/Utils.cpp b/libs/binder/Utils.cpp index 0314b0fea7..47fd17dcb1 100644 --- a/libs/binder/Utils.cpp +++ b/libs/binder/Utils.cpp @@ -16,6 +16,7 @@ #include "Utils.h" +#include <android-base/logging.h> #include <string.h> namespace android { @@ -24,4 +25,22 @@ void zeroMemory(uint8_t* data, size_t size) { memset(data, 0, size); } +std::string HexString(const void* bytes, size_t len) { + CHECK(bytes != nullptr || len == 0) << bytes << " " << len; + + // b/132916539: Doing this the 'C way', std::setfill triggers ubsan implicit conversion + const uint8_t* bytes8 = static_cast<const uint8_t*>(bytes); + const char chars[] = "0123456789abcdef"; + std::string result; + result.resize(len * 2); + + for (size_t i = 0; i < len; i++) { + const auto c = bytes8[i]; + result[2 * i] = chars[c >> 4]; + result[2 * i + 1] = chars[c & 0xf]; + } + + return result; +} + } // namespace android |