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