MCPcopy Create free account
hub / github.com/Snapchat/Valdi / base64ToBinaryInternal

Function base64ToBinaryInternal

libs/utils/src/utils/encoding/Base64Utils.cpp:42–68  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

40}
41
42bool base64ToBinaryInternal(const char* encodedString, size_t inSize, std::vector<uint8_t>* ret) {
43 // Strip out newlines since the decoder returns an error code (0) if it finds them within the encoded string.
44 std::string noNewlines(encodedString, inSize);
45 boost::algorithm::replace_all(noNewlines, "\n", "");
46 boost::algorithm::replace_all(noNewlines, "\r", "");
47
48 // Determine the max array length we'll need given the string length
49 size_t maxOutSize = 0;
50 if (EVP_DecodedLength(&maxOutSize, noNewlines.length()) != 1 || maxOutSize == 0) {
51 return false;
52 }
53
54 ret->resize(maxOutSize);
55 size_t outSize = 0;
56 if (EVP_DecodeBase64(reinterpret_cast<uint8_t*>(ret->data()),
57 &outSize,
58 maxOutSize,
59 reinterpret_cast<const uint8_t*>(noNewlines.c_str()),
60 noNewlines.length()) != 1) {
61 // make it empty to indicate error
62 ret->resize(0);
63 return false;
64 }
65
66 ret->resize(outSize);
67 return true;
68}
69
70std::vector<uint8_t> base64ToBinary(std::string_view base64) {
71 std::vector<uint8_t> decodedData;

Callers 1

base64ToBinaryFunction · 0.85

Calls 3

lengthMethod · 0.45
resizeMethod · 0.45
dataMethod · 0.45

Tested by

no test coverage detected