Fix memory leaks in the Security namespace
[platform/framework/native/appfw.git] / src / security / pkcs / FSecPkcsInitialVector.cpp
1 ///
2 // Open Service Platform
3 // Copyright (c) 2013 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                FSecPkcsInitialVector.cpp
20  * @brief               This is the implementation file for InitialVector class.
21  *
22  * This header file contains the implementation of InitialVector class.
23  *
24  */
25
26 #include <unique_ptr.h>
27 #include <FBaseErrors.h>
28 #include <FBaseByteBuffer.h>
29 #include <FBaseResult.h>
30 #include <FBaseSysLog.h>
31 #include <FSecPkcsInitialVector.h>
32 #include "FSecPkcs_InitialVectorImpl.h"
33
34 using namespace Tizen::Base;
35
36 namespace Tizen { namespace Security { namespace Pkcs
37 {
38
39 InitialVector::InitialVector(void)
40         : __pInitialVectorImpl(null)
41 {
42
43 }
44
45 InitialVector::~InitialVector(void)
46 {
47         delete __pInitialVectorImpl;
48 }
49
50 result
51 InitialVector::Construct(const Tizen::Base::ByteBuffer& initialVector)
52 {
53         result r = E_SUCCESS;
54
55         SysAssertf(__pInitialVectorImpl == null,
56                            "Already constructed! Calling Construct() twice or more on a same instance is not allowed for this class");
57
58         __pInitialVectorImpl = new (std::nothrow) _InitialVectorImpl();
59         SysTryReturnResult(NID_SEC_CRYPTO, __pInitialVectorImpl != null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory in insufficient.");
60
61         r = __pInitialVectorImpl->Construct(initialVector);
62         SysTryCatch(NID_SEC_CRYPTO, !IsFailed(r), , r, "[%s] Propagated.", GetErrorMessage(r));
63
64         return r;
65
66 CATCH:
67
68         delete __pInitialVectorImpl;
69         __pInitialVectorImpl = null;
70
71         return r;
72
73 }
74
75 IAlgorithmParameters*
76 InitialVector::CloneN(void) const
77 {
78         result r = E_SUCCESS;
79
80         ClearLastResult();
81
82         SysAssertf(__pInitialVectorImpl != null, "Not yet constructed. Reconstructor the object.");
83         SysAssertf(__pInitialVectorImpl->GetInitialVector().GetRemaining() > 0, "Not yet constructed. Construct () should be called before use.");
84
85         std::unique_ptr< InitialVector > pInitialVectorObj(new (std::nothrow) InitialVector());
86         SysTryReturn(NID_SEC_CRYPTO, pInitialVectorObj != null, null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
87
88         r = pInitialVectorObj->Construct(__pInitialVectorImpl->GetInitialVector());
89         SysTryReturn(NID_SEC_CRYPTO, !IsFailed(r), null, E_OUT_OF_MEMORY, "[E_OUT_OF_MEMORY] The memory is insufficient.");
90
91         return pInitialVectorObj.release();
92 }
93
94 PkcsAlgorithmParameterType
95 InitialVector::GetType(void) const
96 {
97         SysAssertf(__pInitialVectorImpl != null, "Not yet constructed. Reconstructor the object.");
98
99         ClearLastResult();
100         return __pInitialVectorImpl->GetType();
101 }
102
103 const ByteBuffer&
104 InitialVector::GetInitialVector(void) const
105 {
106
107         ClearLastResult();
108
109         SysAssertf(__pInitialVectorImpl != null, "Not yet constructed. Reconstructor the object.");
110
111         return __pInitialVectorImpl->GetInitialVector();
112
113 }
114
115 bool
116 InitialVector::Equals(const Object& obj) const
117 {
118         SysAssertf(__pInitialVectorImpl != null, "Not yet constructed. Reconstructor the object.");
119
120         const InitialVector* pOther = dynamic_cast< const InitialVector* >(&obj);
121         if (pOther == null)
122         {
123                 return false;
124         }
125
126         return __pInitialVectorImpl->Equals(*pOther->__pInitialVectorImpl);
127 }
128
129 int
130 InitialVector::GetHashCode(void) const
131 {
132         SysAssertf(__pInitialVectorImpl != null, "Not yet constructed. Reconstructor the object.");
133
134         return __pInitialVectorImpl->GetHashCode();
135 }
136
137 ByteBuffer*
138 InitialVector::GetEncodedDataN(void) const
139 {
140         SysAssertf(__pInitialVectorImpl != null, "Not yet constructed. Reconstructor the object.");
141
142         result r = E_SUCCESS;
143
144         std::unique_ptr< ByteBuffer > pEncInitialVector(__pInitialVectorImpl->GetEncodedDataN());
145         SysTryReturn(NID_IO, pEncInitialVector != null, null, GetLastResult(), "[%s] Propagated.", GetErrorMessage(r));
146
147         return pEncInitialVector.release();
148
149 }
150
151
152 } } }