MCPcopy Create free account
hub / github.com/KebsCS/KBotExt / Base64

Class Base64

KBotExt/base64.h:5–108  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3#include <string>
4
5class Base64
6{
7 std::string base64_chars =
8 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
9 "abcdefghijklmnopqrstuvwxyz"
10 "0123456789+/";
11
12public:
13 static bool is_base64(const unsigned char c)
14 {
15 return isalnum(c) || c == '+' || c == '/';
16 }
17
18 std::string Encode(const unsigned char* bytes_to_encode, unsigned int in_len)
19 {
20 std::string ret;
21 int i = 0;
22 unsigned char char_array_3[3];
23 unsigned char char_array_4[4];
24
25 while (in_len--)
26 {
27 char_array_3[i++] = *(bytes_to_encode++);
28 if (i == 3)
29 {
30 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
31 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
32 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
33 char_array_4[3] = char_array_3[2] & 0x3f;
34
35 for (i = 0; i < 4; i++)
36 ret += base64_chars[char_array_4[i]];
37 i = 0;
38 }
39 }
40
41 if (i)
42 {
43 int j;
44 for (j = i; j < 3; j++)
45 char_array_3[j] = '\0';
46
47 char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
48 char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
49 char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
50 char_array_4[3] = char_array_3[2] & 0x3f;
51
52 for (j = 0; j < i + 1; j++)
53 ret += base64_chars[char_array_4[j]];
54
55 while (i++ < 3)
56 ret += '=';
57 }
58
59 return ret;
60 }
61
62 std::string Decode(const std::string& encoded_string)

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected