|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: Implementation of STSTransformation |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "ststransformation.h" |
|
20 #include "javajniutils.h" |
|
21 #include "stsconstants.h" |
|
22 #include "logger.h" |
|
23 |
|
24 namespace java |
|
25 { |
|
26 namespace satsa |
|
27 { |
|
28 |
|
29 //Static function used to create the transformation object |
|
30 STSTransformation* STSTransformation::Create(JNIEnv* aJni, |
|
31 jstring aTransformation, int* errCode) |
|
32 { |
|
33 |
|
34 // Create the object |
|
35 STSTransformation* self = new STSTransformation(); |
|
36 |
|
37 if (self != NULL) |
|
38 { |
|
39 // to parse and decode the transformation string |
|
40 *errCode = self->Construct(aJni, aTransformation); |
|
41 if (*errCode != 0) |
|
42 { |
|
43 ELOG(ESATSA, "STSTransformation::Create:construct failed"); |
|
44 // An Error occured in parsing the transformation string |
|
45 return NULL; |
|
46 } |
|
47 else |
|
48 { |
|
49 // there was no error in parsing the transformation string |
|
50 return self; |
|
51 } |
|
52 } |
|
53 else |
|
54 { |
|
55 ELOG(ESATSA, "STSTransformation::Create:object creation failed"); |
|
56 *errCode = KSTSErrNoMemory; |
|
57 return NULL; |
|
58 } |
|
59 } |
|
60 |
|
61 STSTransformation::STSTransformation() |
|
62 { |
|
63 |
|
64 } |
|
65 |
|
66 STSTransformation::~STSTransformation() |
|
67 { |
|
68 |
|
69 } |
|
70 |
|
71 int STSTransformation::Construct(JNIEnv* aJni, jstring aTransformation) |
|
72 { |
|
73 LOG(ESATSA, EInfo, "STSTransformation::Construct+"); |
|
74 std::wstring algorithm; |
|
75 std::wstring mode; |
|
76 std::wstring padding; |
|
77 |
|
78 //convert the jstring to native string |
|
79 std::wstring transformation; |
|
80 try |
|
81 { |
|
82 transformation = java::util::JniUtils::jstringToWstring(aJni, |
|
83 aTransformation); |
|
84 } |
|
85 catch (...) |
|
86 { |
|
87 ELOG(ESATSA, "caught exception. Return error code"); |
|
88 return (KSTSErrNoSuchAlgorithm); |
|
89 } |
|
90 |
|
91 int index = transformation.find(STSTransformationDelim); |
|
92 if (index != wstring::npos) |
|
93 { |
|
94 // Must be in a form of algorithm/mode/padding |
|
95 // algorithm |
|
96 algorithm.assign(transformation, 0, index); |
|
97 // mode/padding |
|
98 std::wstring modeAndPadding(transformation, index + 1, wstring::npos); |
|
99 |
|
100 index = modeAndPadding.find(STSTransformationDelim); |
|
101 if (index == wstring::npos) |
|
102 { |
|
103 ELOG(ESATSA, "mode or padding is missing"); |
|
104 // There isn't delimeter, mode or padding is missing |
|
105 return (KSTSErrNoSuchAlgorithm); |
|
106 } |
|
107 |
|
108 // mode |
|
109 mode.assign(modeAndPadding, 0, index); |
|
110 if (mode.length() < 1) |
|
111 { |
|
112 ELOG(ESATSA, "mode was not found"); |
|
113 // Mode wasn't found |
|
114 return (KSTSErrNoSuchAlgorithm); |
|
115 } |
|
116 |
|
117 // padding |
|
118 padding.assign(modeAndPadding, index + 1, wstring::npos); |
|
119 if (padding.length() < 1) |
|
120 { |
|
121 ELOG(ESATSA, "padding was not found"); |
|
122 // Padding wasn't found |
|
123 return (KSTSErrNoSuchPadding); |
|
124 } |
|
125 |
|
126 } |
|
127 else |
|
128 { |
|
129 // If there is no delimeter in the string it |
|
130 // must contain only algorithm. |
|
131 algorithm.assign(transformation); |
|
132 |
|
133 // Mode and padding aren't defined |
|
134 // Use default ones |
|
135 mode.assign(STSModeECB); |
|
136 padding.assign(STSPaddingPKCS5); |
|
137 |
|
138 } |
|
139 |
|
140 // parsing over, assign the parsed tokens |
|
141 mAlgorithm = algorithm; |
|
142 mMode = mode; |
|
143 mPadding = padding; |
|
144 |
|
145 return 0; |
|
146 } |
|
147 |
|
148 const std::wstring STSTransformation::Algorithm() |
|
149 { |
|
150 LOG(ESATSA, EInfo, "STSTransformation::Algorithm+"); |
|
151 // Construction is failed if mAlgorithm is not valid |
|
152 return mAlgorithm; |
|
153 } |
|
154 |
|
155 const std::wstring STSTransformation::Mode() |
|
156 { |
|
157 LOG(ESATSA, EInfo, "STSTransformation::Mode+"); |
|
158 // Construction is failed if mMode is not valid |
|
159 return mMode; |
|
160 } |
|
161 |
|
162 const std::wstring STSTransformation::Padding() |
|
163 { |
|
164 LOG(ESATSA, EInfo, "STSTransformation::Padding+"); |
|
165 // Construction is failed if mPadding is not valid |
|
166 return mPadding; |
|
167 } |
|
168 } // namespace satsa |
|
169 } // namespace java |
|
170 |