CRLF
[ghsmtp.git] / Pill.cpp
blob11c1ba16d529867f67553ffe5ba7debc124fa599
1 #include "Pill.hpp"
3 #include <limits>
5 Pill::Pill()
7 using s_t = decltype(s_);
9 std::uniform_int_distribution<s_t> uni_dist;
10 s_ = uni_dist(rng_);
12 auto resp{b32_ndigits_};
13 b32_str_[resp] = '\0';
15 // <http://philzimmermann.com/docs/human-oriented-base-32-encoding.txt>
17 constexpr char b32_charset[]{"ybndrfg8ejkmcpqxot1uwisza345h769"};
19 auto const os{reinterpret_cast<const unsigned char*>(&s_)};
20 auto osp{os + sizeof(s_)};
21 auto x{0ul};
23 switch ((osp - os) % 5) { // Duff's device
24 case 0:
25 do {
26 x = *--osp;
27 b32_str_[--resp] = b32_charset[x % 32];
28 x /= 32;
29 [[fallthrough]];
31 case 4:
32 x |= (static_cast<unsigned long>(*--osp)) << 3;
33 b32_str_[--resp] = b32_charset[x % 32];
34 x /= 32;
35 b32_str_[--resp] = b32_charset[x % 32];
36 x /= 32;
37 [[fallthrough]];
39 case 3:
40 x |= (static_cast<unsigned long>(*--osp)) << 1;
41 b32_str_[--resp] = b32_charset[x % 32];
42 x /= 32;
43 [[fallthrough]];
45 case 2:
46 x |= (static_cast<unsigned long>(*--osp)) << 4;
47 b32_str_[--resp] = b32_charset[x % 32];
48 x /= 32;
49 b32_str_[--resp] = b32_charset[x % 32];
50 x /= 32;
51 [[fallthrough]];
53 case 1:
54 x |= (static_cast<unsigned long>(*--osp)) << 2;
55 b32_str_[--resp] = b32_charset[x % 32];
56 x /= 32;
57 b32_str_[--resp] = b32_charset[x];
58 } while (osp > os);