forked from crabapples-h/navicat-keygen-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBase32.hpp
134 lines (110 loc) · 3.78 KB
/
Base32.hpp
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#pragma once
#include <stdint.h>
#include <string>
#include <vector>
[[nodiscard]]
inline std::string base32_encode(const void* lpBinary, size_t cbBinary) {
static const std::string::value_type Alphabet[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";
static constexpr std::string::value_type PaddingChar = '=';
std::string szBase32;
if (auto pbBinary = reinterpret_cast<const uint8_t*>(lpBinary); cbBinary) {
szBase32.reserve((cbBinary * 8 + 4) / 5);
uint8_t Idx = 0;
uint8_t BitsLeft = 8;
for (size_t i = 0; i < cbBinary;) {
if (BitsLeft < 5) {
Idx = pbBinary[i] << (5 - BitsLeft);
++i;
if (i != cbBinary) {
Idx |= pbBinary[i] >> (3 + BitsLeft);
}
Idx &= 0x1F;
BitsLeft += 3;
} else {
Idx = pbBinary[i] >> (BitsLeft - 5);
Idx &= 0x1F;
BitsLeft -= 5;
}
szBase32.append(1, Alphabet[Idx]);
if (BitsLeft == 0) {
BitsLeft = 8;
++i;
}
}
if (szBase32.length() % 8) {
size_t Padding = 8 - szBase32.length() % 8;
szBase32.append(Padding, PaddingChar);
}
}
return szBase32;
}
[[nodiscard]]
inline std::string base32_encode(const std::vector<uint8_t>& Binary) {
return base32_encode(Binary.data(), Binary.size());
}
[[nodiscard]]
inline std::string base32_encode(const std::initializer_list<uint8_t>& Binary) {
return base32_encode(Binary.begin(), Binary.size());
}
[[nodiscard]]
inline std::vector<uint8_t> base32_decode(std::string_view szBase32) {
static constexpr std::string::value_type PaddingChar = '=';
std::vector<uint8_t> Binary;
if (szBase32.length()) {
Binary.reserve((szBase32.length() * 5 + 7) / 8);
uint8_t Byte = 0;
uint8_t BitsNeed = 8;
for (size_t i = 0; i < szBase32.length(); ++i) {
uint8_t Idx;
if ('A' <= szBase32[i] && szBase32[i] <= 'Z') {
Idx = szBase32[i] - 'A';
} else if ('a' <= szBase32[i] && szBase32[i] <= 'z') {
Idx = szBase32[i] - 'a';
} else if ('2' <= szBase32[i] && szBase32[i] <= '7') {
Idx = szBase32[i] - '2' + 26;
} else if (szBase32[i] == PaddingChar) {
for (size_t j = i + 1; j < szBase32.length(); ++j) {
if (szBase32[j] != PaddingChar) {
throw std::invalid_argument("base32_decode: invalid padding schema.");
}
}
break;
} else {
throw std::invalid_argument("base32_decode: non-Base32 character is detected.");
}
if (BitsNeed >= 5) {
Byte |= Idx;
BitsNeed -= 5;
Byte <<= BitsNeed;
} else {
Byte |= Idx >> (5 - BitsNeed);
Binary.push_back(Byte);
BitsNeed += 3;
Byte = Idx << BitsNeed;
if (BitsNeed > 5) {
Byte >>= BitsNeed - 5;
}
}
}
switch (BitsNeed) {
case 1:
case 2:
case 3:
throw std::invalid_argument("base32_decode: base32 string is corrupted.");
case 4:
case 5:
case 6:
case 7:
if (Byte != 0) {
throw std::invalid_argument("base32_decode: base32 string is corrupted.");
}
break;
case 0:
case 8:
break;
default:
__builtin_unreachable();
}
}
return Binary;
}