[dali_2.3.20] Merge branch 'devel/master'
[platform/core/uifw/dali-toolkit.git] / dali-toolkit / devel-api / builder / base64-encoding.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  *
16  */
17
18 // EXTERNAL INCLUDES
19 #include <iterator>
20 #include <sstream>
21
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>
26
27 namespace Dali
28 {
29 namespace Toolkit
30 {
31 namespace
32 {
33 const int MAX_PROPERTY_STRING_LENGTH(64); // Cuts larger strings into blocks of this size.
34
35 bool GetStringFromProperty(const Property::Value& value, std::string& output)
36 {
37   bool extracted = false;
38   if(value.Get(output))
39   {
40     extracted = true;
41   }
42   else
43   {
44     const Property::Array* array = value.GetArray();
45     if(array)
46     {
47       const unsigned int arraySize = array->Size();
48       for(unsigned int i = 0; i < arraySize; ++i)
49       {
50         std::string element;
51         if(array->GetElementAt(i).Get(element))
52         {
53           extracted = true;
54           output += element;
55         }
56         else
57         {
58           // If property in array is anything other than a string, then it is invalid so break and clear output.
59           output.clear();
60           extracted = false;
61           break;
62         }
63       }
64     }
65   }
66   return extracted;
67 }
68
69 } //anonymous namespace
70
71 bool DecodeBase64PropertyData(const Property::Value& value, std::vector<uint32_t>& outputData)
72 {
73   bool        decoded = false;
74   std::string encodedString;
75
76   if(GetStringFromProperty(value, encodedString))
77   {
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));
81
82     outputData.clear();
83     outputData.resize(outputTmpData.size() / sizeof(uint32_t));
84     // Treat as a block of data
85     memcpy(&outputData[0], &outputTmpData[0], outputTmpData.size());
86
87     decoded = true;
88   }
89   return decoded;
90 }
91
92 void EncodeBase64PropertyData(Property::Value& value, const std::vector<uint32_t>& inputData)
93 {
94   std::ostringstream oss;
95
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, ""));
99
100   std::string encodedString = oss.str();
101   if(encodedString.length() > MAX_PROPERTY_STRING_LENGTH)
102   {
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);
106
107     Property::Array array;
108     for(auto i = 0u; i < numStrings; ++i)
109     {
110       array.PushBack(encodedString.substr(i * MAX_PROPERTY_STRING_LENGTH, MAX_PROPERTY_STRING_LENGTH));
111     }
112     value = array;
113   }
114   else
115   {
116     value = encodedString;
117   }
118 }
119
120 } // namespace Toolkit
121
122 } // namespace Dali