aboutsummaryrefslogtreecommitdiffstats
path: root/util/stream/hex.cpp
blob: 1c05330504a273b39e01fd018a56236d179d5dc2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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);
    }
}