[dali_2.2.12] 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     uint32_t outputSize = outputTmpData.size() / sizeof(uint32_t) + static_cast<uint32_t>(!!(outputTmpData.size() % sizeof(uint32_t)));
84     outputData.resize(outputSize);
85     // Treat as a block of data
86     memcpy(&outputData[0], &outputTmpData[0], outputTmpData.size());
87
88     decoded = true;
89   }
90   return decoded;
91 }
92
93 bool DecodeBase64PropertyData(const Property::Value& value, std::vector<uint8_t>& outputData)
94 {
95   bool        decoded = false;
96   std::string encodedString;
97
98   if(GetStringFromProperty(value, encodedString))
99   {
100     outputData.reserve(ceil(encodedString.size() * 0.75f));
101     bn::decode_b64(encodedString.begin(), encodedString.end(), std::back_inserter(outputData));
102
103     decoded = true;
104   }
105   return decoded;
106 }
107
108 void EncodeBase64PropertyData(Property::Value& value, const std::vector<uint32_t>& inputData)
109 {
110   std::ostringstream oss;
111
112   bn::encode_b64(reinterpret_cast<const char*>(&inputData[0]),
113                  reinterpret_cast<const char*>(&inputData[0] + inputData.size()),
114                  std::ostream_iterator<unsigned char>(oss, ""));
115
116   std::string encodedString = oss.str();
117   if(encodedString.length() > MAX_PROPERTY_STRING_LENGTH)
118   {
119     // cut string up into blocks of MAX_PROPERTY_STRING_LENGTH and store to an array
120     auto numStrings = encodedString.length() / MAX_PROPERTY_STRING_LENGTH +
121                       ((encodedString.length() % MAX_PROPERTY_STRING_LENGTH) != 0);
122
123     Property::Array array;
124     for(auto i = 0u; i < numStrings; ++i)
125     {
126       array.PushBack(encodedString.substr(i * MAX_PROPERTY_STRING_LENGTH, MAX_PROPERTY_STRING_LENGTH));
127     }
128     value = array;
129   }
130   else
131   {
132     value = encodedString;
133   }
134 }
135
136 } // namespace Toolkit
137
138 } // namespace Dali