MCPcopy Create free account
hub / github.com/apache/trafficserver / EncodeBase64

Function EncodeBase64

lib/yamlcpp/src/binary.cpp:9–45  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
8
9std::string EncodeBase64(const unsigned char *data, std::size_t size) {
10 const char PAD = '=';
11
12 std::string ret;
13 ret.resize(4 * size / 3 + 3);
14 char *out = &ret[0];
15
16 std::size_t chunks = size / 3;
17 std::size_t remainder = size % 3;
18
19 for (std::size_t i = 0; i < chunks; i++, data += 3) {
20 *out++ = encoding[data[0] >> 2];
21 *out++ = encoding[((data[0] & 0x3) << 4) | (data[1] >> 4)];
22 *out++ = encoding[((data[1] & 0xf) << 2) | (data[2] >> 6)];
23 *out++ = encoding[data[2] & 0x3f];
24 }
25
26 switch (remainder) {
27 case 0:
28 break;
29 case 1:
30 *out++ = encoding[data[0] >> 2];
31 *out++ = encoding[((data[0] & 0x3) << 4)];
32 *out++ = PAD;
33 *out++ = PAD;
34 break;
35 case 2:
36 *out++ = encoding[data[0] >> 2];
37 *out++ = encoding[((data[0] & 0x3) << 4) | (data[1] >> 4)];
38 *out++ = encoding[((data[1] & 0xf) << 2)];
39 *out++ = PAD;
40 break;
41 }
42
43 ret.resize(out - &ret[0]);
44 return ret;
45}
46
47static const unsigned char decoding[] = {
48 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,

Callers 2

WriteBinaryFunction · 0.85
encodeMethod · 0.85

Calls 1

resizeMethod · 0.45

Tested by

no test coverage detected