X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali-toolkit%2Fdevel-api%2Fbuilder%2Fbase64-encoding.cpp;h=17b823ad17a65694a7da533e51da30f0444f7f8b;hb=c70520ff81c009acafc2a2cfd1ac38adb64ffcae;hp=2de438aaac5144604ececd70e6aec3bebb053d9a;hpb=06cd3bf8b892c26f369aae331b16fba1547ed823;p=platform%2Fcore%2Fuifw%2Fdali-toolkit.git diff --git a/dali-toolkit/devel-api/builder/base64-encoding.cpp b/dali-toolkit/devel-api/builder/base64-encoding.cpp index 2de438a..17b823a 100644 --- a/dali-toolkit/devel-api/builder/base64-encoding.cpp +++ b/dali-toolkit/devel-api/builder/base64-encoding.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2020 Samsung Electronics Co., Ltd. + * Copyright (c) 2023 Samsung Electronics Co., Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -75,12 +75,13 @@ bool DecodeBase64PropertyData(const Property::Value& value, std::vector outputTmpData; - outputTmpData.reserve(ceil(encodedString.size() * 0.75f)); + std::vector outputTmpData; + // Output required at least ceil(length * 3 / 4) + outputData.reserve((encodedString.size() * 3 + 3) / 4); bn::decode_b64(encodedString.begin(), encodedString.end(), std::back_inserter(outputTmpData)); outputData.clear(); - uint32_t outputSize = outputTmpData.size() / sizeof(uint32_t) + static_cast(!!(outputTmpData.size() % sizeof(uint32_t))); + uint32_t outputSize = (outputTmpData.size() + sizeof(uint32_t) - 1) / sizeof(uint32_t); outputData.resize(outputSize); // Treat as a block of data memcpy(&outputData[0], &outputTmpData[0], outputTmpData.size()); @@ -97,7 +98,8 @@ bool DecodeBase64PropertyData(const Property::Value& value, std::vector if(GetStringFromProperty(value, encodedString)) { - outputData.reserve(ceil(encodedString.size() * 0.75f)); + // Output required at least ceil(length * 3 / 4) + outputData.reserve((encodedString.size() * 3 + 3) / 4); bn::decode_b64(encodedString.begin(), encodedString.end(), std::back_inserter(outputData)); decoded = true; @@ -105,6 +107,15 @@ bool DecodeBase64PropertyData(const Property::Value& value, std::vector return decoded; } +bool DecodeBase64FromString(const std::string_view& encodedString, std::vector& outputData) +{ + // Output required at least ceil(length * 3 / 4) + outputData.reserve((encodedString.size() * 3 + 3) >> 2); + bn::decode_b64(encodedString.begin(), encodedString.end(), std::back_inserter(outputData)); + + return true; // Always success. +} + void EncodeBase64PropertyData(Property::Value& value, const std::vector& inputData) { std::ostringstream oss; @@ -114,11 +125,61 @@ void EncodeBase64PropertyData(Property::Value& value, const std::vector(oss, "")); std::string encodedString = oss.str(); + + // Add padding + int paddingLength = (4 - (encodedString.length() % 4)) % 4; + if(paddingLength > 0) + { + while(paddingLength--) + { + oss << '='; + } + encodedString = oss.str(); + } + + if(encodedString.length() > MAX_PROPERTY_STRING_LENGTH) + { + // cut string up into blocks of MAX_PROPERTY_STRING_LENGTH and store to an array + auto numStrings = (encodedString.length() + MAX_PROPERTY_STRING_LENGTH - 1) / MAX_PROPERTY_STRING_LENGTH; + + Property::Array array; + for(auto i = 0u; i < numStrings; ++i) + { + array.PushBack(encodedString.substr(i * MAX_PROPERTY_STRING_LENGTH, MAX_PROPERTY_STRING_LENGTH)); + } + value = array; + } + else + { + value = encodedString; + } +} + +void EncodeBase64PropertyData(Property::Value& value, const std::vector& inputData) +{ + std::ostringstream oss; + + bn::encode_b64(reinterpret_cast(&inputData[0]), + reinterpret_cast(&inputData[0] + inputData.size()), + std::ostream_iterator(oss, "")); + + std::string encodedString = oss.str(); + + // Add padding + int paddingLength = (4 - (encodedString.length() % 4)) % 4; + if(paddingLength > 0) + { + while(paddingLength--) + { + oss << '='; + } + encodedString = oss.str(); + } + if(encodedString.length() > MAX_PROPERTY_STRING_LENGTH) { // cut string up into blocks of MAX_PROPERTY_STRING_LENGTH and store to an array - auto numStrings = encodedString.length() / MAX_PROPERTY_STRING_LENGTH + - ((encodedString.length() % MAX_PROPERTY_STRING_LENGTH) != 0); + auto numStrings = (encodedString.length() + MAX_PROPERTY_STRING_LENGTH - 1) / MAX_PROPERTY_STRING_LENGTH; Property::Array array; for(auto i = 0u; i < numStrings; ++i)