2 // Open Service Platform
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
20 * @brief This is the implementation file for UuId class.
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>
35 namespace Tizen { namespace Base
38 const static UUID __INVALID_UUID = {0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
43 uuid_clear(this->uuid);
46 UuId::UuId(const UuId& uuid)
49 uuid_copy(this->uuid, uuid.uuid);
52 UuId::UuId(const UUID& uuid)
55 memcpy(this->uuid, &uuid, sizeof(this->uuid));
57 ConvertToUuId(this->uuid);
60 UuId::UuId(const byte uuid[16])
63 uuid_copy(this->uuid, uuid);
71 operator ==(const UUID& uuid1, const UuId& uuid2)
74 memcpy(uuid, &uuid1, sizeof(uuid));
75 UuId::ConvertToUuId(uuid);
77 int ret = uuid_compare(uuid, uuid2.uuid);
89 UuId::operator ==(const UuId& uuid) const
91 int ret = uuid_compare(this->uuid, uuid.uuid);
101 operator !=(const UUID& uuid1, const UuId& uuid2)
114 UuId::operator !=(const UuId& uuid) const
127 UuId::operator =(const UuId& uuid)
131 uuid_copy(this->uuid, uuid.uuid);
137 UuId::Equals(const Object& obj) const
139 const UuId* pOther = dynamic_cast<const UuId*>(&obj);
146 if (*this == *pOther)
157 UuId::GetHashCode(void) const
159 String str = ToString();
161 return str.GetHashCode();
165 UuId::ToString(void) const
168 char tmpuuid[sizeof(UUID)];
170 unsigned short s1 = 0;
171 unsigned short s2 = 0;
176 char pChar[1 + 2 * (sizeof(unsigned long) + sizeof(unsigned short) * 2 + sizeof(unsigned char) * 8) + 4];
178 memcpy(tmpuuid, &(__uuid), sizeof(UUID));
180 memcpy(&x, tmpuuid, sizeof(unsigned long));
181 pos += sizeof(unsigned long);
183 memcpy(&s1, tmpuuid + pos, sizeof(unsigned short));
184 pos += sizeof(unsigned short);
186 memcpy(&s2, tmpuuid + pos, sizeof(unsigned short));
187 pos += sizeof(unsigned short);
189 memcpy(&c1, tmpuuid + pos, sizeof(unsigned char) * 2);
190 pos += sizeof(unsigned char) * 2;
192 memcpy(&c2, tmpuuid + pos, sizeof(unsigned char) * 6);
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]);
197 return String(pChar);
200 char uuid_str[36] = {0,};
202 uuid_unparse(this->uuid, uuid_str);
204 return String(uuid_str);
208 UuId::ToUUID(void) const
211 byte uuidValue[16] = {0,};
213 uuid_copy(uuidValue, this->uuid);
214 ConvertToUuId(uuidValue);
216 memcpy(&uuid, uuidValue, sizeof(UUID));
222 UuId::Parse(const String& str, UuId& uuid)
226 int length = str.GetLength();
228 SysTryReturn(NID_BASE, length == 36, E_INVALID_ARG, E_INVALID_ARG, ("[E_INVALID_ARG] The length of str(%d) MUST be 36."), length);
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());
233 // str[i] must be one of '0~9' or 'A~F' or 'a~f'
234 for (int i = 0; i < length; i++)
238 if (i == 8 || i == 13 || i == 18 || i == 23)
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;
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());
253 char mbstring[length + 1];
254 wchar_t wchar_tstring[length + 1];
256 wcsncpy(wchar_tstring, str.GetPointer(), length);
257 wchar_tstring[length] = 0;
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());
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]);
270 for (int i = 0; i < 32; ++i)
274 c[i] = (char) (c[i] - 'a' + (char) 10);
276 else if (c[i] >= 'A')
278 c[i] = (char) (c[i] - 'A' + (char) 10);
286 uuid.__uuid.x = c[0] * 0x10000000
294 uuid.__uuid.s1 = c[8] * 0x1000
298 uuid.__uuid.s2 = c[12] * 0x1000
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];
315 int length = str.GetLength();
317 SysTryReturnResult(NID_BASE, length == 36, E_INVALID_ARG, "The length of str(%d) MUST be 36.", length);
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());
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);
327 SysTryReturnResult(NID_BASE, ret == 0, E_INVALID_ARG, "The str(%ls) MUST consist of only alphanumeric characters and '-'.", str.GetPointer());
329 uuid_copy(uuid.uuid, uuidValue);
335 UuId::GenerateN(void)
338 UuId* pUuid = new (std::nothrow) UuId();
341 SysLogException(NID_BASE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
347 uuid_copy(pUuid->uuid, out);
353 UuId::GetInvalidUuId(void)
355 UuId invalidUuId(__INVALID_UUID);
360 UuId::ConvertToUuId(byte uuid[16])
364 for (int i=0; i<2; i++)