-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base32Code.cpp
166 lines (150 loc) · 3.69 KB
/
Base32Code.cpp
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
* Copyright (c) 2018 Johan Andersson <nilsjohanandersson@gmail.com>
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "Base32Code.h"
#include <algorithm>
#include <cctype>
#include <cassert>
#include <stdexcept>
namespace base32 {
const char Base32Code::Digits[32] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q',
'R', 'S', 'T', 'V', 'W', 'X', 'Y', 'Z',
};
Base32Code::Base32Code(std::string str) : str_(std::move(str))
{
for (char& c : str_)
{
c = Base32Code::Canonical(c);
if (c == Base32Code::InvalidDigit()) {
throw std::invalid_argument("Invalid Base32 digit");
}
}
}
char Base32Code::Digit(const char value)
{
if (value < 0 || sizeof(Digits) <= value)
{
return InvalidDigit();
}
return Digits[value];
}
signed char Base32Code::Value(const char digit)
{
const char canonical = Canonical(digit);
if (InvalidDigit() == canonical)
{
return InvalidValue();
}
const char* it = std::find(std::cbegin(Digits), std::cend(Digits), canonical);
const auto value = it - std::begin(Digits);
assert(0 <= value && value < 32);
return static_cast<signed char>(value);
}
char Base32Code::Canonical(const char digit)
{
switch (digit)
{
case '0': // zero
case 'o': // lower case oh
case 'O': // upper case oh
return '0';
case '1': // one
case 'i': // lower case eye
case 'I': // upper case eye
case 'l': // lower case ell
case 'L': // upper case ell
return '1';
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
case 'A':
case 'B':
case 'C':
case 'D':
case 'E':
case 'F':
case 'G':
case 'H':
// 1
case 'J':
case 'K':
// 1
case 'M':
case 'N':
// 0
case 'P':
case 'Q':
case 'R':
case 'S':
case 'T':
// no U
case 'V':
case 'W':
case 'X':
case 'Y':
case 'Z':
return digit;
case 'a':
case 'b':
case 'c':
case 'd':
case 'e':
case 'f':
case 'g':
case 'h':
// 1
case 'j':
case 'k':
// 1
case 'm':
case 'n':
// 0
case 'p':
case 'q':
case 'r':
case 's':
case 't':
// no u
case 'v':
case 'w':
case 'x':
case 'y':
case 'z':
return static_cast<char>(std::toupper(digit));
default:
return InvalidDigit();
}
}
bool Base32Code::IsValid(const std::string& str)
{
return std::all_of(str.begin(), str.end(), [](auto c) { return IsValid(c); });
}
} // namespace base32