N_SE-53194 : fix AppControl::Stop() without listener
[platform/framework/native/appfw.git] / src / base / FBaseUuId.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                FBaseUuId.cpp
19  * @brief               This is the implementation file for UuId class.
20  */
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <wchar.h>
24 #include <uuid/uuid.h>
25 #include <FBaseUuId.h>
26 #include <FBaseResult.h>
27 #include <FBaseByteBuffer.h>
28 #include <FBaseSysLog.h>
29 #include <unique_ptr.h>
30 #include "FBase_StringConverter.h"
31
32 namespace Tizen { namespace Base
33 {
34
35 const static UUID __INVALID_UUID = {0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
36
37 UuId::UuId(void)
38         : __pUuIdImpl(null)
39 {
40         uuid_clear(this->uuid);
41 }
42
43 UuId::UuId(const UuId& uuid)
44         : __pUuIdImpl(null)
45 {
46         uuid_copy(this->uuid, uuid.uuid);
47 }
48
49 UuId::UuId(const UUID& uuid)
50         : __pUuIdImpl(null)
51 {
52         memcpy(this->uuid, &uuid, sizeof(this->uuid));
53
54         ConvertToUuId(this->uuid);
55 }
56
57 UuId::UuId(const byte uuid[16])
58         : __pUuIdImpl(null)
59 {
60         uuid_copy(this->uuid, uuid);
61 }
62
63 UuId::~UuId(void)
64 {
65 }
66
67 bool
68 operator ==(const UUID& uuid1, const UuId& uuid2)
69 {
70         byte uuid[16] = {0, };
71         memcpy(uuid, &uuid1, sizeof(uuid));
72         UuId::ConvertToUuId(uuid);
73
74         int ret = uuid_compare(uuid, uuid2.uuid);
75         if (ret == 0)
76         {
77                 return true;
78         }
79         else
80         {
81                 return false;
82         }
83 }
84
85 bool
86 UuId::operator ==(const UuId& uuid) const
87 {
88         int ret = uuid_compare(this->uuid, uuid.uuid);
89         if (ret == 0)
90         {
91                 return true;
92         }
93
94         return false;
95 }
96
97 bool
98 operator !=(const UUID& uuid1, const UuId& uuid2)
99 {
100         if (uuid1 == uuid2)
101         {
102                 return false;
103         }
104         else
105         {
106                 return true;
107         }
108 }
109
110 bool
111 UuId::operator !=(const UuId& uuid) const
112 {
113         if (*this == uuid)
114         {
115                 return false;
116         }
117         else
118         {
119                 return true;
120         }
121 }
122
123 UuId&
124 UuId::operator =(const UuId& uuid)
125 {
126         if (&uuid != this)
127         {
128                 uuid_copy(this->uuid, uuid.uuid);
129         }
130         return (*this);
131 }
132
133 bool
134 UuId::Equals(const Object& obj) const
135 {
136         const UuId* pOther = dynamic_cast< const UuId* >(&obj);
137
138         if (pOther == null)
139         {
140                 return false;
141         }
142
143         if (*this == *pOther)
144         {
145                 return true;
146         }
147         else
148         {
149                 return false;
150         }
151 }
152
153 int
154 UuId::GetHashCode(void) const
155 {
156         String str = ToString();
157
158         return str.GetHashCode();
159 }
160
161 String
162 UuId::ToString(void) const
163 {
164         /*
165         char tmpuuid[sizeof(UUID)];
166         unsigned int x = 0;
167         unsigned short s1 = 0;
168         unsigned short s2 = 0;
169         unsigned char c1[2];
170         unsigned char c2[6];
171         int pos = 0;
172
173         char pChar[1 + 2 * (sizeof(unsigned long) + sizeof(unsigned short) * 2 + sizeof(unsigned char) * 8) + 4];
174
175         memcpy(tmpuuid, &(__uuid), sizeof(UUID));
176
177         memcpy(&x, tmpuuid, sizeof(unsigned long));
178         pos += sizeof(unsigned long);
179
180         memcpy(&s1, tmpuuid + pos, sizeof(unsigned short));
181         pos += sizeof(unsigned short);
182
183         memcpy(&s2, tmpuuid + pos, sizeof(unsigned short));
184         pos += sizeof(unsigned short);
185
186         memcpy(&c1, tmpuuid + pos, sizeof(unsigned char) * 2);
187         pos += sizeof(unsigned char) * 2;
188
189         memcpy(&c2, tmpuuid + pos, sizeof(unsigned char) * 6);
190
191         sprintf(pChar, "%.8x-%.4x-%.4x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x",
192                         x, s1, s2, c1[0], c1[1], c2[0], c2[1], c2[2], c2[3], c2[4], c2[5]);
193
194         return String(pChar);
195         */
196
197         char uuid_str[36] = {0, };
198
199         uuid_unparse(this->uuid, uuid_str);
200
201         return String(uuid_str);
202 }
203
204 UUID
205 UuId::ToUUID(void) const
206 {
207         UUID uuid;
208         byte uuidValue[16] = {0, };
209
210         uuid_copy(uuidValue, this->uuid);
211         ConvertToUuId(uuidValue);
212
213         memcpy(&uuid, uuidValue, sizeof(UUID));
214
215         return uuid;
216 }
217
218 result
219 UuId::Parse(const String& str, UuId& uuid)
220 {
221         /*
222         char c[32] = {0};
223         int length = str.GetLength();
224
225         SysTryReturn(NID_BASE, length == 36, E_INVALID_ARG, E_INVALID_ARG, ("[E_INVALID_ARG] The length of str(%d) MUST be 36."), length);
226
227         SysTryReturn(NID_BASE, !(str[8] != L'-' || str[13] != L'-' || str[18] != L'-' || str[23] != L'-')
228                           , E_INVALID_ARG, E_INVALID_ARG, ("[E_INVALID_ARG] The str(%ls) is not valid UuId type"), str.GetPointer());
229
230         // str[i] must be one of '0~9' or 'A~F' or 'a~f'
231         for (int i = 0; i < length; i++)
232         {
233                 if (str[i] == L'-')
234                 {
235                         if (i == 8 || i == 13 || i == 18 || i == 23)
236                         {
237                                 continue;
238                         }
239                         else
240                         {
241                                 SysLogException(NID_BASE, E_INVALID_ARG, "[E_INVALID_ARG] The str(%ls) is not valid UuId type", str.GetPointer());
242                                 return E_INVALID_ARG;
243                         }
244                 }
245
246                 SysTryReturn(NID_BASE, (str[i] >= '0' && str[i] <= '9') || (str[i] >= 'A' && str[i] <= 'F') ||
247                                    (str[i] >= 'a' && str[i] <= 'f'), E_INVALID_ARG, E_INVALID_ARG, ("[E_INVALID_ARG] The str(%ls) MUST consist of only alphanumeric characters and '-'."), str.GetPointer());
248         }
249
250         char mbstring[length + 1];
251         wchar_t wchar_tstring[length + 1];
252
253         wcsncpy(wchar_tstring, str.GetPointer(), length);
254         wchar_tstring[length] = 0;
255
256         length = wcstombs(mbstring, wchar_tstring, length);
257         SysTryReturn(NID_BASE, length == 36, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The str(%ls) is not valid UuId type", str.GetPointer());
258
259         sscanf(mbstring,
260                    "%c%c%c%c%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c-%c%c%c%c%c%c%c%c%c%c%c%c",
261                    &c[0], &c[1], &c[2], &c[3], &c[4], &c[5], &c[6], &c[7],
262                    &c[8], &c[9], &c[10], &c[11],
263                    &c[12], &c[13], &c[14], &c[15],
264                    &c[16], &c[17], &c[18], &c[19],
265                    &c[20], &c[21], &c[22], &c[23], &c[24], &c[25], &c[26], &c[27], &c[28], &c[29], &c[30], &c[31]);
266
267         for (int i = 0; i < 32; ++i)
268         {
269                 if (c[i] >= 'a')
270                 {
271                         c[i] = (char) (c[i] - 'a' + (char) 10);
272                 }
273                 else if (c[i] >= 'A')
274                 {
275                         c[i] = (char) (c[i] - 'A' + (char) 10);
276                 }
277                 else
278                 {
279                         c[i] -= '0';
280                 }
281         }
282
283         uuid.__uuid.x = c[0] * 0x10000000
284                                         + c[1] * 0x1000000
285                                         + c[2] * 0x100000
286                                         + c[3] * 0x10000
287                                         + c[4] * 0x1000
288                                         + c[5] * 0x100
289                                         + c[6] * 0x10
290                                         + c[7];
291         uuid.__uuid.s1 = c[8] * 0x1000
292                                          + c[9] * 0x100
293                                          + c[10] * 0x10
294                                          + c[11];
295         uuid.__uuid.s2 = c[12] * 0x1000
296                                          + c[13] * 0x100
297                                          + c[14] * 0x10
298                                          + c[15];
299
300         uuid.__uuid.c[0] = c[16] * 0x10 + c[17];
301         uuid.__uuid.c[1] = c[18] * 0x10 + c[19];
302         uuid.__uuid.c[2] = c[20] * 0x10 + c[21];
303         uuid.__uuid.c[3] = c[22] * 0x10 + c[23];
304         uuid.__uuid.c[4] = c[24] * 0x10 + c[25];
305         uuid.__uuid.c[5] = c[26] * 0x10 + c[27];
306         uuid.__uuid.c[6] = c[28] * 0x10 + c[29];
307         uuid.__uuid.c[7] = c[30] * 0x10 + c[31];
308
309         return E_SUCCESS;
310         */
311
312         int length = str.GetLength();
313
314         SysTryReturnResult(NID_BASE, length == 36, E_INVALID_ARG, "The length of str(%d) MUST be 36.", length);
315
316         SysTryReturnResult(NID_BASE, !(str[8] != L'-' || str[13] != L'-' || str[18] != L'-' || str[23] != L'-')
317                 , E_INVALID_ARG, "The str(%ls) is not valid UuId type.", str.GetPointer());
318
319         uuid_t uuidValue;
320         std::unique_ptr< char[] > pStr(_StringConverter::CopyToCharArrayN(str));
321         SysTryReturnResult(NID_BASE, pStr != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
322         int ret = uuid_parse(pStr.get(), uuidValue);
323
324         SysTryReturnResult(NID_BASE, ret == 0, E_INVALID_ARG, "The str(%ls) MUST consist of only alphanumeric characters and '-'.", str.GetPointer());
325
326         uuid_copy(uuid.uuid, uuidValue);
327
328         return E_SUCCESS;
329 }
330
331 UuId*
332 UuId::GenerateN(void)
333 {
334         uuid_t out;
335         UuId* pUuid = new (std::nothrow) UuId();
336         if (pUuid == null)
337         {
338                 SysLogException(NID_BASE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
339                 return null;
340         }
341
342         uuid_generate(out);
343
344         uuid_copy(pUuid->uuid, out);
345
346         return pUuid;
347 }
348
349 UuId
350 UuId::GetInvalidUuId(void)
351 {
352         UuId invalidUuId(__INVALID_UUID);
353         return invalidUuId;
354 }
355
356 void
357 UuId::ConvertToUuId(byte uuid[16])
358 {
359         byte value = 0x00;
360
361         for (int i = 0; i < 2; i++)
362         {
363                 value = uuid[i];
364                 uuid[i] = uuid[3 - i];
365                 uuid[3 - i] = value;
366         }
367
368         value = uuid[4];
369         uuid[4] = uuid[5];
370         uuid[5] = value;
371
372         value = uuid[6];
373         uuid[6] = uuid[7];
374         uuid[7] = value;
375 }
376
377 }} //Tizen::Base