aboutsummaryrefslogtreecommitdiffstats
path: root/util/string/hex.cpp
blob: 7b1af2c6b7447da9c148b303d8c2463c3315d1f0 (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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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;
}