-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBase16.cpp
More file actions
69 lines (44 loc) · 1.19 KB
/
Copy pathBase16.cpp
File metadata and controls
69 lines (44 loc) · 1.19 KB
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
64
65
66
using namespace std;
#include "Base16.hpp"
char Base16::getKey(const uint8_t i) const{
return keys.at(i);
}
uint8_t Base16::getCode(const char c) const{
size_t pos = keys.find(c);
if(pos == string::npos)
throw runtime_error("Invalid base16 character");
return pos;
}
pair<char, char> Base16::encode(const uint8_t src) const{
uint8_t greater = (src & 0xF0) >> 4;
uint8_t lower = src & 0x0F;
return pair<char, char>(getKey(greater), getKey(lower));
}
uint8_t Base16::decode(const pair<char, char> &src) const{
uint8_t res = 0;
res |= getCode(src.first) << 4;
res |= getCode(src.second);
return res;
}
string Base16::encode(const Buffer &src) const{
string res;
for(size_t i = 0; i < src.getSize(); ++i){
pair<char, char> keys = encode(src[i]);
res += keys.first;
res += keys.second;
}
return res;
}
Buffer Base16::decode(const string &src) const{
if(src.length() % 2)
throw runtime_error("src length is not even");
Buffer res(src.length() / 2);
for(size_t i = 0; i < src.length(); i += 2){
char greater = src.at(i);
char lower = src.at(i + 1);
pair<char, char> current(greater, lower);
uint8_t val = decode(current);
res[i / 2] = val;
}
return res;
}