-
Notifications
You must be signed in to change notification settings - Fork 3
/
Pill.cpp
58 lines (47 loc) · 1.37 KB
/
Pill.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
#include "Pill.hpp"
Pill::Pill()
{
using s_t = decltype(s_);
std::uniform_int_distribution<s_t> uni_dist;
s_ = uni_dist(rng_);
auto resp{b32_ndigits_};
b32_str_[resp] = '\0';
// <http://philzimmermann.com/docs/human-oriented-base-32-encoding.txt>
constexpr char b32_charset[]{"ybndrfg8ejkmcpqxot1uwisza345h769"};
auto const os{reinterpret_cast<const unsigned char*>(&s_)};
auto osp{os + sizeof(s_)};
auto x{0ul};
switch ((osp - os) % 5) { // Duff's device
case 0:
do {
x = *--osp;
b32_str_[--resp] = b32_charset[x % 32];
x /= 32;
[[fallthrough]];
case 4:
x |= (static_cast<unsigned long>(*--osp)) << 3;
b32_str_[--resp] = b32_charset[x % 32];
x /= 32;
b32_str_[--resp] = b32_charset[x % 32];
x /= 32;
[[fallthrough]];
case 3:
x |= (static_cast<unsigned long>(*--osp)) << 1;
b32_str_[--resp] = b32_charset[x % 32];
x /= 32;
[[fallthrough]];
case 2:
x |= (static_cast<unsigned long>(*--osp)) << 4;
b32_str_[--resp] = b32_charset[x % 32];
x /= 32;
b32_str_[--resp] = b32_charset[x % 32];
x /= 32;
[[fallthrough]];
case 1:
x |= (static_cast<unsigned long>(*--osp)) << 2;
b32_str_[--resp] = b32_charset[x % 32];
x /= 32;
b32_str_[--resp] = b32_charset[x];
} while (osp > os);
}
}