MCPcopy Create free account
hub / github.com/aseprite/laf / encode_base64

Function encode_base64

base/base64.cpp:57–101  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

55}
56
57void encode_base64(const char* input, size_t n, std::string& output)
58{
59 const size_t size = 4 * int(std::ceil(n / 3.0)); // Estimate encoded string size
60 output.resize(size);
61
62 auto outIt = output.begin();
63 auto outEnd = output.end();
64 uint8_t next = 0;
65 size_t i = 0;
66 size_t j = 0;
67 for (; i < n; ++i, ++input) {
68 auto inputValue = *input;
69 switch (j) {
70 case 0:
71 *outIt = base64Char((inputValue & 0b11111100) >> 2);
72 ++outIt;
73 next |= (inputValue & 0b00000011) << 4;
74 ++j;
75 break;
76 case 1:
77 *outIt = base64Char(((inputValue & 0b11110000) >> 4) | next);
78 ++outIt;
79 next = (inputValue & 0b00001111) << 2;
80 ++j;
81 break;
82 case 2:
83 *outIt = base64Char(((inputValue & 0b11000000) >> 6) | next);
84 ++outIt;
85 *outIt = base64Char(inputValue & 0b00111111);
86 ++outIt;
87 next = 0;
88 j = 0;
89 break;
90 }
91 }
92
93 if (outIt != outEnd) {
94 *outIt = base64Char(next);
95 ++outIt;
96 for (; outIt != outEnd; ++outIt)
97 *outIt = '='; // Padding
98 }
99}
100
101void decode_base64(const char* input, size_t n, buffer& output)
102{
103 size_t size = 3 * int(std::ceil(n / 4.0)); // Estimate decoded buffer size
104 output.resize(size);

Callers

nothing calls this directly

Calls 3

base64CharFunction · 0.85
beginMethod · 0.45
endMethod · 0.45

Tested by

no test coverage detected