/*
- * Copyright (c) 2020 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2023 Samsung Electronics Co., Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
if(GetStringFromProperty(value, encodedString))
{
- std::vector<unsigned char> outputTmpData;
- outputTmpData.reserve(ceil(encodedString.size() * 0.75f));
+ std::vector<uint8_t> outputTmpData;
+ // Output required at least ceil(length * 3 / 4)
+ outputData.reserve((encodedString.size() * 3 + 3) / 4);
bn::decode_b64(encodedString.begin(), encodedString.end(), std::back_inserter(outputTmpData));
outputData.clear();
- outputData.resize(outputTmpData.size() / sizeof(uint32_t));
+ uint32_t outputSize = (outputTmpData.size() + sizeof(uint32_t) - 1) / sizeof(uint32_t);
+ outputData.resize(outputSize);
// Treat as a block of data
memcpy(&outputData[0], &outputTmpData[0], outputTmpData.size());
return decoded;
}
+bool DecodeBase64PropertyData(const Property::Value& value, std::vector<uint8_t>& outputData)
+{
+ bool decoded = false;
+ std::string encodedString;
+
+ if(GetStringFromProperty(value, encodedString))
+ {
+ // Output required at least ceil(length * 3 / 4)
+ outputData.reserve((encodedString.size() * 3 + 3) / 4);
+ bn::decode_b64(encodedString.begin(), encodedString.end(), std::back_inserter(outputData));
+
+ decoded = true;
+ }
+ return decoded;
+}
+
+bool DecodeBase64FromString(const std::string_view& encodedString, std::vector<uint8_t>& outputData)
+{
+ // Output required at least ceil(length * 3 / 4)
+ outputData.reserve((encodedString.size() * 3 + 3) >> 2);
+ bn::decode_b64(encodedString.begin(), encodedString.end(), std::back_inserter(outputData));
+
+ return true; // Always success.
+}
+
void EncodeBase64PropertyData(Property::Value& value, const std::vector<uint32_t>& inputData)
{
std::ostringstream oss;
std::ostream_iterator<unsigned char>(oss, ""));
std::string encodedString = oss.str();
+
+ // Add padding
+ int paddingLength = (4 - (encodedString.length() % 4)) % 4;
+ if(paddingLength > 0)
+ {
+ while(paddingLength--)
+ {
+ oss << '=';
+ }
+ encodedString = oss.str();
+ }
+
+ if(encodedString.length() > MAX_PROPERTY_STRING_LENGTH)
+ {
+ // cut string up into blocks of MAX_PROPERTY_STRING_LENGTH and store to an array
+ auto numStrings = (encodedString.length() + MAX_PROPERTY_STRING_LENGTH - 1) / MAX_PROPERTY_STRING_LENGTH;
+
+ Property::Array array;
+ for(auto i = 0u; i < numStrings; ++i)
+ {
+ array.PushBack(encodedString.substr(i * MAX_PROPERTY_STRING_LENGTH, MAX_PROPERTY_STRING_LENGTH));
+ }
+ value = array;
+ }
+ else
+ {
+ value = encodedString;
+ }
+}
+
+void EncodeBase64PropertyData(Property::Value& value, const std::vector<uint8_t>& inputData)
+{
+ std::ostringstream oss;
+
+ bn::encode_b64(reinterpret_cast<const uint8_t*>(&inputData[0]),
+ reinterpret_cast<const uint8_t*>(&inputData[0] + inputData.size()),
+ std::ostream_iterator<char>(oss, ""));
+
+ std::string encodedString = oss.str();
+
+ // Add padding
+ int paddingLength = (4 - (encodedString.length() % 4)) % 4;
+ if(paddingLength > 0)
+ {
+ while(paddingLength--)
+ {
+ oss << '=';
+ }
+ encodedString = oss.str();
+ }
+
if(encodedString.length() > MAX_PROPERTY_STRING_LENGTH)
{
// cut string up into blocks of MAX_PROPERTY_STRING_LENGTH and store to an array
- auto numStrings = encodedString.length() / MAX_PROPERTY_STRING_LENGTH +
- ((encodedString.length() % MAX_PROPERTY_STRING_LENGTH) != 0);
+ auto numStrings = (encodedString.length() + MAX_PROPERTY_STRING_LENGTH - 1) / MAX_PROPERTY_STRING_LENGTH;
Property::Array array;
for(auto i = 0u; i < numStrings; ++i)