summaryrefslogtreecommitdiffstats
path: root/util/stream/hex.cpp
diff options
context:
space:
mode:
authorDevtools Arcadia <[email protected]>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <[email protected]>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/stream/hex.cpp
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/stream/hex.cpp')
-rw-r--r--util/stream/hex.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/util/stream/hex.cpp b/util/stream/hex.cpp
new file mode 100644
index 00000000000..1c05330504a
--- /dev/null
+++ b/util/stream/hex.cpp
@@ -0,0 +1,30 @@
+#include "hex.h"
+
+#include "output.h"
+#include <util/string/hex.h>
+
+void HexEncode(const void* in, size_t len, IOutputStream& out) {
+ static const size_t NUM_OF_BYTES = 32;
+ char buffer[NUM_OF_BYTES * 2];
+
+ auto current = static_cast<const char*>(in);
+ for (size_t take = 0; len; current += take, len -= take) {
+ take = Min(NUM_OF_BYTES, len);
+ HexEncode(current, take, buffer);
+ out.Write(buffer, take * 2);
+ }
+}
+
+void HexDecode(const void* in, size_t len, IOutputStream& out) {
+ Y_ENSURE(!(len & 1), TStringBuf("Odd buffer length passed to HexDecode"));
+
+ static const size_t NUM_OF_BYTES = 32;
+ char buffer[NUM_OF_BYTES];
+
+ auto current = static_cast<const char*>(in);
+ for (size_t take = 0; len; current += take, len -= take) {
+ take = Min(NUM_OF_BYTES * 2, len);
+ HexDecode(current, take, buffer);
+ out.Write(buffer, take / 2);
+ }
+}