2 * Copyright (c) 2020 Samsung Electronics Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <dali-toolkit/devel-api/builder/base64-encoding.h>
23 #include <dali/public-api/object/property-array.h>
24 #include <dali/public-api/object/property-value.h>
25 #include <dali-toolkit/third-party/base-n/basen.hpp>
33 const int MAX_PROPERTY_STRING_LENGTH(64); // Cuts larger strings into blocks of this size.
35 bool GetStringFromProperty(const Property::Value& value, std::string& output)
37 bool extracted = false;
44 const Property::Array* array = value.GetArray();
47 const unsigned int arraySize = array->Size();
48 for(unsigned int i = 0; i < arraySize; ++i)
51 if(array->GetElementAt(i).Get(element))
58 // If property in array is anything other than a string, then it is invalid so break and clear output.
69 } //anonymous namespace
71 bool DecodeBase64PropertyData(const Property::Value& value, std::vector<uint32_t>& outputData)
74 std::string encodedString;
76 if(GetStringFromProperty(value, encodedString))
78 std::vector<unsigned char> outputTmpData;
79 outputTmpData.reserve(ceil(encodedString.size() * 0.75f));
80 bn::decode_b64(encodedString.begin(), encodedString.end(), std::back_inserter(outputTmpData));
83 outputData.resize(outputTmpData.size() / sizeof(uint32_t));
84 // Treat as a block of data
85 memcpy(&outputData[0], &outputTmpData[0], outputTmpData.size());
92 void EncodeBase64PropertyData(Property::Value& value, const std::vector<uint32_t>& inputData)
94 std::ostringstream oss;
96 bn::encode_b64(reinterpret_cast<const char*>(&inputData[0]),
97 reinterpret_cast<const char*>(&inputData[0] + inputData.size()),
98 std::ostream_iterator<unsigned char>(oss, ""));
100 std::string encodedString = oss.str();
101 if(encodedString.length() > MAX_PROPERTY_STRING_LENGTH)
103 // cut string up into blocks of MAX_PROPERTY_STRING_LENGTH and store to an array
104 auto numStrings = encodedString.length() / MAX_PROPERTY_STRING_LENGTH +
105 ((encodedString.length() % MAX_PROPERTY_STRING_LENGTH) != 0);
107 Property::Array array;
108 for(auto i = 0u; i < numStrings; ++i)
110 array.PushBack(encodedString.substr(i * MAX_PROPERTY_STRING_LENGTH, MAX_PROPERTY_STRING_LENGTH));
116 value = encodedString;
120 } // namespace Toolkit