Joedb 9.1.4
The Journal-Only Embedded Database
Loading...
Searching...
No Matches
base64.cpp
Go to the documentation of this file.
1#include "joedb/ui/base64.h"
2
3#include <stdint.h>
4
5namespace joedb
6{
7 static char const base64_codes[] =
8 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
9
10 ////////////////////////////////////////////////////////////////////////////
11 std::string base64_encode(const std::string &input)
12 ////////////////////////////////////////////////////////////////////////////
13 {
14 const size_t N = input.size() / 3;
15 const size_t remainder = input.size() % 3;
16
17 std::string result;
18 result.resize(4 * (N + static_cast<size_t>(remainder != 0)));
19
20 for (size_t i = 0; i < N; i++)
21 {
22 const uint32_t word = (uint32_t(uint8_t(input[3 * i + 0])) << 16) |
23 (uint32_t(uint8_t(input[3 * i + 1])) << 8) |
24 (uint32_t(uint8_t(input[3 * i + 2])) );
25 result[4 * i + 0] = base64_codes[(word >> 18) ];
26 result[4 * i + 1] = base64_codes[(word >> 12) & 0x3f];
27 result[4 * i + 2] = base64_codes[(word >> 6) & 0x3f];
28 result[4 * i + 3] = base64_codes[(word ) & 0x3f];
29 }
30
31 if (remainder == 2)
32 {
33 const int32_t word = (uint8_t(input[3 * N + 0]) << 10) |
34 (uint8_t(input[3 * N + 1]) << 2);
35 result[4 * N + 0] = base64_codes[(word >> 12) ];
36 result[4 * N + 1] = base64_codes[(word >> 6) & 0x3f];
37 result[4 * N + 2] = base64_codes[(word ) & 0x3f];
38 result[4 * N + 3] = '=';
39 }
40 else if (remainder == 1)
41 {
42 const int32_t word = (uint8_t(input[3 * N + 0]) << 4);
43 result[4 * N + 0] = base64_codes[(word >> 6) ];
44 result[4 * N + 1] = base64_codes[(word ) & 0x3f];
45 result[4 * N + 2] = '=';
46 result[4 * N + 3] = '=';
47 }
48
49 return result;
50 }
51}
std::string base64_encode(const std::string &input)
Definition base64.cpp:11
Definition Blob.h:7