2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 // Licensed under the Apache License, Version 2.0 (the License);
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
19 * @file FSecCert_Base64.cpp
20 * @brief This file contains implementation of Base64 Encode/Decode functions.
28 #include <unique_ptr.h>
32 #include <FBaseString.h>
33 #include <FBase_StringConverter.h>
34 #include <FBaseSysLog.h>
35 #include <FBaseByteBuffer.h>
36 #include <FBaseResult.h>
37 #include <FBaseUtilStringUtil.h>
38 #include "FSecCert_Base64.h"
40 using namespace Tizen::Base;
41 using namespace Tizen::Io;
43 namespace Tizen { namespace Security { namespace Cert
47 _Base64::Decode(char* pIn, int inSize, byte* pOut, int& outSize)
52 SysTryReturn(NID_SEC_CERT, pIn != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input argument, null buffer passed.");
53 SysTryReturn(NID_SEC_CERT, inSize > 0, -1, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input argument, input size must be greater than zero.");
55 SysTryReturn(NID_SEC_CERT, pOut != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input argument, out buffer is passed null.");
57 if (pIn[inSize - 1] != '\0' && pIn[inSize] != '\0')
59 std::unique_ptr< char[] > pTmp(new (std::nothrow) char[inSize + 1]);
60 SysTryReturn(NID_SEC_CERT, pTmp != null, -1, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] Failed to allocate memory.");
62 memcpy(pTmp.get(), pIn, inSize);
65 encodedStr.Append(pTmp.get());
69 encodedStr.Append(pIn);
72 std::unique_ptr< ByteBuffer > pDecodedBuffer(Tizen::Base::Utility::StringUtil::DecodeBase64StringN(encodedStr));
73 SysTryReturn(NID_SEC_CERT, pDecodedBuffer != null, -1, E_SYSTEM, "[E_SYSTEM] Failed to perform base64 decoding.");
75 if (outSize > pDecodedBuffer->GetRemaining())
77 outSize = pDecodedBuffer->GetRemaining();
80 memcpy(pOut, pDecodedBuffer->GetPointer(), outSize);
86 _Base64::Encode(byte* pIn, int inSize, char* pOut, int outSize)
89 String encodedStr(_MAX_CERTIFICATE_SIZE);
92 SysTryReturnResult(NID_SEC_CERT, pIn != null, E_INVALID_ARG, "Invalid input argument, input buffer is null.");
93 SysTryReturnResult(NID_SEC_CERT, inSize > 0, E_INVALID_ARG, "Invalid input argument, input size is less than zero.");
94 SysTryReturnResult(NID_SEC_CERT, pOut != null, E_INVALID_ARG, "Invalid input argument.");
96 r = buffer.Construct(inSize);
97 SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
99 r = buffer.SetArray(pIn, 0, inSize);
100 SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_SYSTEM, "Failed to set bytes for encoding.");
104 r = Tizen::Base::Utility::StringUtil::EncodeToBase64String(buffer, encodedStr);
105 SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_SYSTEM, "Failed to encode base 64 data.");
107 std::unique_ptr< char[] > pEncodedBuffer(Tizen::Base::_StringConverter::CopyToCharArrayN(encodedStr));
108 SysTryReturn(NID_SEC_CERT, pEncodedBuffer != null, GetLastResult(), GetLastResult(), "[%s] Failed to convert string to char buffer.", GetErrorMessage(GetLastResult()));
110 if (outSize > encodedStr.GetLength())
112 strcpy(pOut, pEncodedBuffer.get());
116 memcpy(pOut, pEncodedBuffer.get(), outSize);
119 outSize = encodedStr.GetLength();
125 _Base64::GetEncodedSize(int length)
128 SysTryReturn(NID_SEC_CERT, length > 0, -1, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input argument, length is less than zero.");
130 return (length + 2) / 3 * 4;
134 _Base64::GetDecodedSize(int length)
137 SysTryReturn(NID_SEC_CERT, length > 0, -1, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input argument, length is less than zero.");
139 return (length + 3) / 4 * 3;
142 } } } //Tizen::Security::Cert