aboutsummaryrefslogtreecommitdiffstats
path: root/util/string/hex.cpp
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/string/hex.cpp
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/string/hex.cpp')
-rw-r--r--util/string/hex.cpp63
1 files changed, 63 insertions, 0 deletions
diff --git a/util/string/hex.cpp b/util/string/hex.cpp
new file mode 100644
index 0000000000..667397987f
--- /dev/null
+++ b/util/string/hex.cpp
@@ -0,0 +1,63 @@
+#include "hex.h"
+
+const char* const Char2DigitTable = ("\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
+ "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
+ "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
+ "\x00\x01\x02\x03\x04\x05\x06\x07\x08\x09\xff\xff\xff\xff\xff\xff" //0-9
+ "\xff\x0a\x0b\x0c\x0d\x0e\x0f\xff\xff\xff\xff\xff\xff\xff\xff\xff" //A-Z
+ "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
+ "\xff\x0a\x0b\x0c\x0d\x0e\x0f\xff\xff\xff\xff\xff\xff\xff\xff\xff" //a-z
+ "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
+ "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
+ "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
+ "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
+ "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
+ "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
+ "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
+ "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
+ "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff");
+
+char* HexEncode(const void* in, size_t len, char* out) {
+ const unsigned char* b = (const unsigned char*)in;
+ const unsigned char* e = b + len;
+
+ while (b != e) {
+ *out++ = DigitToChar(*b / 16);
+ *out++ = DigitToChar(*b++ % 16);
+ }
+
+ return out;
+}
+
+void* HexDecode(const void* in, size_t len, void* ptr) {
+ const char* b = (const char*)in;
+ const char* e = b + len;
+ Y_ENSURE(!(len & 1), TStringBuf("Odd buffer length passed to HexDecode"));
+
+ char* out = (char*)ptr;
+
+ while (b != e) {
+ *out++ = (char)String2Byte(b);
+ b += 2;
+ }
+
+ return out;
+}
+
+TString HexEncode(const void* in, size_t len) {
+ TString ret;
+
+ ret.ReserveAndResize(len << 1);
+ HexEncode(in, len, ret.begin());
+
+ return ret;
+}
+
+TString HexDecode(const void* in, size_t len) {
+ TString ret;
+
+ ret.ReserveAndResize(len >> 1);
+ HexDecode(in, len, ret.begin());
+
+ return ret;
+}