Merge "Beautified source code of appfw/src/base/utility" into tizen_2.2
[platform/framework/native/appfw.git] / src / security / cert / FSecCert_Base64.cpp
1 //
2 // Copyright (c) 2012 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  * @file                FSecCert_Base64.cpp
19  * @brief               This file contains implementation of Base64 Encode/Decode functions.
20 */
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <error.h>
25 #include <memory.h>
26 #include <new>
27 #include <unique_ptr.h>
28 #include <sys/stat.h>
29 #include <assert.h>
30 #include <dirent.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"
38
39 using namespace Tizen::Base;
40 using namespace Tizen::Io;
41
42 namespace Tizen { namespace Security { namespace Cert
43 {
44
45 int
46 _Base64::Decode(char* pIn, int inSize, byte* pOut, int& outSize)
47 {
48         String encodedStr;
49
50         ClearLastResult();
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.");
53
54         SysTryReturn(NID_SEC_CERT, pOut != null, -1, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input argument, out buffer is passed null.");
55
56         if (pIn[inSize - 1] != '\0' && pIn[inSize] != '\0')
57         {
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.");
60
61                 memcpy(pTmp.get(), pIn, inSize);
62                 pTmp[inSize] = '\0';
63
64                 encodedStr.Append(pTmp.get());
65         }
66         else
67         {
68                 encodedStr.Append(pIn);
69         }
70
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.");
73
74         if (outSize > pDecodedBuffer->GetRemaining())
75         {
76                 outSize = pDecodedBuffer->GetRemaining();
77         }
78
79         memcpy(pOut, pDecodedBuffer->GetPointer(), outSize);
80
81         return outSize;
82 }
83
84 result
85 _Base64::Encode(byte* pIn, int inSize, char* pOut, int outSize)
86 {
87         result r = E_SUCCESS;
88         String encodedStr(_MAX_CERTIFICATE_SIZE);
89         ByteBuffer buffer;
90
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.");
94
95         r = buffer.Construct(inSize);
96         SysTryReturn(NID_SEC_CERT, !IsFailed(r), r, r, "[%s] Propagated.", GetErrorMessage(r));
97
98         r = buffer.SetArray(pIn, 0, inSize);
99         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_SYSTEM, "Failed to set bytes for encoding.");
100
101         buffer.Flip();
102
103         r = Tizen::Base::Utility::StringUtil::EncodeToBase64String(buffer, encodedStr);
104         SysTryReturnResult(NID_SEC_CERT, !IsFailed(r), E_SYSTEM, "Failed to encode base 64 data.");
105
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()));
108
109         if (outSize > encodedStr.GetLength())
110         {
111                 strcpy(pOut, pEncodedBuffer.get());
112         }
113         else
114         {
115                 memcpy(pOut, pEncodedBuffer.get(), outSize);
116         }
117
118         outSize = encodedStr.GetLength();
119
120         return E_SUCCESS;
121 }
122
123 int
124 _Base64::GetEncodedSize(int length)
125 {
126         ClearLastResult();
127         SysTryReturn(NID_SEC_CERT, length > 0, -1, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input argument, length is less than zero.");
128
129         return (length + 2) / 3 * 4;
130 }
131
132 int
133 _Base64::GetDecodedSize(int length)
134 {
135         ClearLastResult();
136         SysTryReturn(NID_SEC_CERT, length > 0, -1, E_INVALID_ARG, "[E_INVALID_ARG] Invalid input argument, length is less than zero.");
137
138         return (length + 3) / 4 * 3;
139 }
140
141 } } } //Tizen::Security::Cert