2 // Copyright (c) 2012 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.
18 * @file FSecCert_Base64.cpp
19 * @brief This file contains implementation of Base64 Encode/Decode functions.
27 #include <unique_ptr.h>
31 #include <FBaseString.h>
32 #include <FBase_StringConverter.h>
33 #include <FBaseSysLog.h>
34 #include <FBaseByteBuffer.h>
35 #include <FBaseResult.h>
36 #include <FBaseUtilStringUtil.h>
37 #include "FSecCert_Base64.h"
39 using namespace Tizen::Base;
40 using namespace Tizen::Io;
42 namespace Tizen { namespace Security { namespace Cert
46 _Base64::Decode(char* pIn, int inSize, byte* pOut, int& outSize)
51 SysTryReturn(NID_SEC_CERT, pIn != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input argument, null buffer passed.");
52 SysTryReturn(NID_SEC_CERT, inSize > 0, -1, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input argument, input size must be greater than zero.");
54 SysTryReturn(NID_SEC_CERT, pOut != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input argument, out buffer is passed null.");
56 if (pIn[inSize - 1] != '\0' && pIn[inSize] != '\0')
58 std::unique_ptr< char[] > pTmp(new (std::nothrow) char[inSize + 1]);
59 SysTryReturn(NID_SEC_CERT, pTmp != null, -1, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
61 memcpy(pTmp.get(), pIn, inSize);
64 encodedStr.Append(pTmp.get());
68 encodedStr.Append(pIn);
71 std::unique_ptr< ByteBuffer > pDecodedBuffer(Tizen::Base::Utility::StringUtil::DecodeBase64StringN(encodedStr));
72 SysTryReturn(NID_SEC_CERT, pDecodedBuffer != null, -1, E_SYSTEM, "[E_SYSTEM] Failed to perform base64 decoding.");
74 if (outSize > pDecodedBuffer->GetRemaining())
76 outSize = pDecodedBuffer->GetRemaining();
79 memcpy(pOut, pDecodedBuffer->GetPointer(), outSize);
85 _Base64::Encode(byte* pIn, int inSize, char* pOut, int outSize)
88 String encodedStr(_MAX_CERTIFICATE_SIZE);
91 SysTryReturnResult(NID_SEC_CERT, pIn != null, E_INVALID_ARG, "Invalid input argument, input buffer is null.");
92 SysTryReturnResult(NID_SEC_CERT, inSize > 0, E_INVALID_ARG, "Invalid input argument, input size is less than zero.");
93 SysTryReturnResult(NID_SEC_CERT, pOut != null, E_INVALID_ARG, "Invalid input argument.");
95 r = buffer.Construct(inSize);
96 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
98 r = buffer.SetArray(pIn, 0, inSize);
99 SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_SYSTEM, "Failed to set bytes for encoding.");
103 r = Tizen::Base::Utility::StringUtil::EncodeToBase64String(buffer, encodedStr);
104 SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_SYSTEM, "Failed to encode base 64 data.");
106 std::unique_ptr< char[] > pEncodedBuffer(Tizen::Base::_StringConverter::CopyToCharArrayN(encodedStr));
107 SysTryReturn(NID_SEC_CERT, pEncodedBuffer != null, GetLastResult(), GetLastResult(), "[%s] Failed to convert string to char buffer.", GetErrorMessage(GetLastResult()));
109 if (outSize > encodedStr.GetLength())
111 strcpy(pOut, pEncodedBuffer.get());
115 memcpy(pOut, pEncodedBuffer.get(), outSize);
118 outSize = encodedStr.GetLength();
124 _Base64::GetEncodedSize(int length)
127 SysTryReturn(NID_SEC_CERT, length > 0, -1, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input argument, length is less than zero.");
129 return (length + 2) / 3 * 4;
133 _Base64::GetDecodedSize(int length)
136 SysTryReturn(NID_SEC_CERT, length > 0, -1, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input argument, length is less than zero.");
138 return (length + 3) / 4 * 3;
141 } } } //Tizen::Security::Cert