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