MCPcopy Create free account
hub / github.com/bcndev/bytecoin / decode

Function decode

src/common/Base64.cpp:67–106  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

65}
66
67bool decode(const std::string &in, BinaryArray *ret) {
68 // Make sure the *intended* string length is a multiple of 4
69 size_t encoded_size = in.size();
70
71 while ((encoded_size % 4) != 0)
72 ++encoded_size;
73
74 const size_t N = in.size();
75 ret->clear();
76 ret->reserve(3 * encoded_size / 4);
77
78 for (size_t i = 0; i < encoded_size; i += 4) {
79 // Get values for each group of four base 64 characters
80 const uint8_t b4_0 =
81 (static_cast<uint8_t>(in[i + 0]) <= 'z') ? from_base64[static_cast<uint8_t>(in[i + 0])] : uint8_t(0xff);
82 const uint8_t b4_1 = (i + 1 < N && static_cast<uint8_t>(in[i + 1]) <= 'z')
83 ? from_base64[static_cast<uint8_t>(in[i + 1])]
84 : uint8_t(0xff);
85 const uint8_t b4_2 = (i + 2 < N && static_cast<uint8_t>(in[i + 2]) <= 'z')
86 ? from_base64[static_cast<uint8_t>(in[i + 2])]
87 : uint8_t(0xff);
88 const uint8_t b4_3 = (i + 3 < N && static_cast<uint8_t>(in[i + 3]) <= 'z')
89 ? from_base64[static_cast<uint8_t>(in[i + 3])]
90 : uint8_t(0xff);
91
92 // Transform into a group of three bytes
93 const uint8_t b3_0 = ((b4_0 & 0x3f) << 2) + ((b4_1 & 0x30) >> 4);
94 const uint8_t b3_1 = ((b4_1 & 0x0f) << 4) + ((b4_2 & 0x3c) >> 2);
95 const uint8_t b3_2 = ((b4_2 & 0x03) << 6) + ((b4_3 & 0x3f) >> 0);
96
97 // Add the byte to the return value if it isn't part of an '=' character (indicated by 0xff)
98 if (b4_1 != 0xff)
99 ret->push_back(b3_0);
100 if (b4_2 != 0xff)
101 ret->push_back(b3_1);
102 if (b4_3 != 0xff)
103 ret->push_back(b3_2);
104 }
105 return true; // TODO - find decoder which returns false on invalid data
106}
107}} // namespace common::base64

Callers

nothing calls this directly

Calls 2

sizeMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected