2 * Copyright (c) 2018 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/public-api/object/property-value.h>
23 #include <dali/public-api/object/property-array.h>
24 #include <dali-toolkit/devel-api/builder/base64-encoding.h>
25 #include <dali-toolkit/third-party/base-n/basen.hpp>
35 const int MAX_PROPERTY_STRING_LENGTH(64); // Cuts larger strings into blocks of this size.
37 bool GetStringFromProperty( const Property::Value& value, std::string& output )
39 bool extracted = false;
40 if( value.Get( output ) )
46 Property::Array* array = value.GetArray();
49 const unsigned int arraySize = array->Size();
50 for( unsigned int i = 0; i < arraySize; ++i )
53 if( array->GetElementAt( i ).Get( element ) )
60 // If property in array is anything other than a string, then it is invalid so break and clear output.
71 }//anonymous namespace
73 bool DecodeBase64PropertyData( const Property::Value& value, std::vector<uint32_t>& outputData )
76 std::string encodedString;
79 if( GetStringFromProperty( value, encodedString ) )
81 std::vector<unsigned char> outputTmpData;
82 outputTmpData.reserve( ceil( encodedString.size() * 0.75f ) );
83 bn::decode_b64( encodedString.begin(), encodedString.end(), std::back_inserter(outputTmpData) );
86 outputData.resize( outputTmpData.size() / sizeof(uint32_t) );
87 // Treat as a block of data
88 memcpy( &outputData[0], &outputTmpData[0], outputTmpData.size() );
95 void EncodeBase64PropertyData( Property::Value& value, const std::vector<uint32_t>& inputData )
97 std::ostringstream oss;
99 bn::encode_b64( reinterpret_cast<const char*>(&inputData[0]),
100 reinterpret_cast<const char*>(&inputData[0]+inputData.size()),
101 std::ostream_iterator<unsigned char>(oss, "") );
104 std::string encodedString = oss.str();
105 if( encodedString.length() > MAX_PROPERTY_STRING_LENGTH)
107 // cut string up into blocks of MAX_PROPERTY_STRING_LENGTH and store to an array
108 auto numStrings = encodedString.length() / MAX_PROPERTY_STRING_LENGTH +
109 ((encodedString.length() % MAX_PROPERTY_STRING_LENGTH) != 0);
111 Property::Array array;
112 for(auto i=0u; i<numStrings; ++i)
114 array.PushBack( encodedString.substr( i*MAX_PROPERTY_STRING_LENGTH, MAX_PROPERTY_STRING_LENGTH));
120 value = encodedString;
125 } // namespace Toolkit