2 * Copyright (c) 2023 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<uint8_t> outputTmpData;
79 // Output required at least ceil(length * 3 / 4)
80 outputData.reserve((encodedString.size() * 3 + 3) / 4);
81 bn::decode_b64(encodedString.begin(), encodedString.end(), std::back_inserter(outputTmpData));
84 uint32_t outputSize = (outputTmpData.size() + sizeof(uint32_t) - 1) / sizeof(uint32_t);
85 outputData.resize(outputSize);
86 // Treat as a block of data
87 memcpy(&outputData[0], &outputTmpData[0], outputTmpData.size());
94 bool DecodeBase64PropertyData(const Property::Value& value, std::vector<uint8_t>& outputData)
97 std::string encodedString;
99 if(GetStringFromProperty(value, encodedString))
101 // Output required at least ceil(length * 3 / 4)
102 outputData.reserve((encodedString.size() * 3 + 3) / 4);
103 bn::decode_b64(encodedString.begin(), encodedString.end(), std::back_inserter(outputData));
110 bool DecodeBase64FromString(const std::string_view& encodedString, std::vector<uint8_t>& outputData)
112 // Output required at least ceil(length * 3 / 4)
113 outputData.reserve((encodedString.size() * 3 + 3) >> 2);
114 bn::decode_b64(encodedString.begin(), encodedString.end(), std::back_inserter(outputData));
116 return true; // Always success.
119 void EncodeBase64PropertyData(Property::Value& value, const std::vector<uint32_t>& inputData)
121 std::ostringstream oss;
123 bn::encode_b64(reinterpret_cast<const char*>(&inputData[0]),
124 reinterpret_cast<const char*>(&inputData[0] + inputData.size()),
125 std::ostream_iterator<unsigned char>(oss, ""));
127 std::string encodedString = oss.str();
130 int paddingLength = (4 - (encodedString.length() % 4)) % 4;
131 if(paddingLength > 0)
133 while(paddingLength--)
137 encodedString = oss.str();
140 if(encodedString.length() > MAX_PROPERTY_STRING_LENGTH)
142 // cut string up into blocks of MAX_PROPERTY_STRING_LENGTH and store to an array
143 auto numStrings = (encodedString.length() + MAX_PROPERTY_STRING_LENGTH - 1) / MAX_PROPERTY_STRING_LENGTH;
145 Property::Array array;
146 for(auto i = 0u; i < numStrings; ++i)
148 array.PushBack(encodedString.substr(i * MAX_PROPERTY_STRING_LENGTH, MAX_PROPERTY_STRING_LENGTH));
154 value = encodedString;
158 void EncodeBase64PropertyData(Property::Value& value, const std::vector<uint8_t>& inputData)
160 std::ostringstream oss;
162 bn::encode_b64(reinterpret_cast<const uint8_t*>(&inputData[0]),
163 reinterpret_cast<const uint8_t*>(&inputData[0] + inputData.size()),
164 std::ostream_iterator<char>(oss, ""));
166 std::string encodedString = oss.str();
169 int paddingLength = (4 - (encodedString.length() % 4)) % 4;
170 if(paddingLength > 0)
172 while(paddingLength--)
176 encodedString = oss.str();
179 if(encodedString.length() > MAX_PROPERTY_STRING_LENGTH)
181 // cut string up into blocks of MAX_PROPERTY_STRING_LENGTH and store to an array
182 auto numStrings = (encodedString.length() + MAX_PROPERTY_STRING_LENGTH - 1) / MAX_PROPERTY_STRING_LENGTH;
184 Property::Array array;
185 for(auto i = 0u; i < numStrings; ++i)
187 array.PushBack(encodedString.substr(i * MAX_PROPERTY_STRING_LENGTH, MAX_PROPERTY_STRING_LENGTH));
193 value = encodedString;
197 } // namespace Toolkit