MCPcopy Create free account
hub / github.com/VitalAudio/visage / decodeBase64Data

Function decodeBase64Data

visage_utils/string_utils.cpp:48–90  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

46 }
47
48 std::unique_ptr<unsigned char[]> decodeBase64Data(const std::string& string, int& size) {
49 int groups = string.size() / 4;
50 int write_index = 0;
51
52 std::unique_ptr<unsigned char[]> result = std::make_unique<unsigned char[]>(groups * 3);
53 auto base64_value = [](char character) {
54 if (character >= 'A' && character <= 'Z')
55 return character - 'A';
56 if (character >= 'a' && character <= 'z')
57 return character - 'a' + 26;
58 if (character >= '0' && character <= '9')
59 return character - '0' + 52;
60 if (character == '+')
61 return 62;
62 if (character == '/')
63 return 63;
64 if (character == '=')
65 return 64;
66
67 return 65;
68 };
69
70 for (size_t i = 0; i + 3 < string.length(); i += 4) {
71 unsigned char value0 = base64_value(string[i]);
72 unsigned char value1 = base64_value(string[i + 1]);
73 unsigned char value2 = base64_value(string[i + 2]);
74 unsigned char value3 = base64_value(string[i + 3]);
75
76 if (value0 > 64 || value1 > 64 || value2 > 64 || value3 > 64)
77 return nullptr;
78
79 result[write_index++] = (value0 << 2) | (value1 >> 4);
80 if (value2 != 64) {
81 result[write_index++] = (value1 << 4) | (value2 >> 2);
82
83 if (value3 != 64)
84 result[write_index++] = (value2 << 6) | value3;
85 }
86 }
87
88 size = write_index;
89 return result;
90 }
91
92 String String::toLower() const {
93 std::u32string result = string_;

Callers 1

Calls 2

sizeMethod · 0.45
lengthMethod · 0.45

Tested by

no test coverage detected