2 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 * @brief This is the implementation file for UuId class.
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"
32 namespace Tizen { namespace Base
35 const static UUID __INVALID_UUID = {0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
40 uuid_clear(this->uuid);
43 UuId::UuId(const UuId& uuid)
46 uuid_copy(this->uuid, uuid.uuid);
49 UuId::UuId(const UUID& uuid)
52 memcpy(this->uuid, &uuid, sizeof(this->uuid));
54 ConvertToUuId(this->uuid);
57 UuId::UuId(const byte uuid[16])
60 uuid_copy(this->uuid, uuid);
68 operator ==(const UUID& uuid1, const UuId& uuid2)
70 byte uuid[16] = {0, };
71 memcpy(uuid, &uuid1, sizeof(uuid));
72 UuId::ConvertToUuId(uuid);
74 int ret = uuid_compare(uuid, uuid2.uuid);
86 UuId::operator ==(const UuId& uuid) const
88 int ret = uuid_compare(this->uuid, uuid.uuid);
98 operator !=(const UUID& uuid1, const UuId& uuid2)
111 UuId::operator !=(const UuId& uuid) const
124 UuId::operator =(const UuId& uuid)
128 uuid_copy(this->uuid, uuid.uuid);
134 UuId::Equals(const Object& obj) const
136 const UuId* pOther = dynamic_cast< const UuId* >(&obj);
143 if (*this == *pOther)
154 UuId::GetHashCode(void) const
156 String str = ToString();
158 return str.GetHashCode();
162 UuId::ToString(void) const
165 char tmpuuid[sizeof(UUID)];
167 unsigned short s1 = 0;
168 unsigned short s2 = 0;
173 char pChar[1 + 2 * (sizeof(unsigned long) + sizeof(unsigned short) * 2 + sizeof(unsigned char) * 8) + 4];
175 memcpy(tmpuuid, &(__uuid), sizeof(UUID));
177 memcpy(&x, tmpuuid, sizeof(unsigned long));
178 pos += sizeof(unsigned long);
180 memcpy(&s1, tmpuuid + pos, sizeof(unsigned short));
181 pos += sizeof(unsigned short);
183 memcpy(&s2, tmpuuid + pos, sizeof(unsigned short));
184 pos += sizeof(unsigned short);
186 memcpy(&c1, tmpuuid + pos, sizeof(unsigned char) * 2);
187 pos += sizeof(unsigned char) * 2;
189 memcpy(&c2, tmpuuid + pos, sizeof(unsigned char) * 6);
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]);
194 return String(pChar);
197 char uuid_str[36] = {0, };
199 uuid_unparse(this->uuid, uuid_str);
201 return String(uuid_str);
205 UuId::ToUUID(void) const
208 byte uuidValue[16] = {0, };
210 uuid_copy(uuidValue, this->uuid);
211 ConvertToUuId(uuidValue);
213 memcpy(&uuid, uuidValue, sizeof(UUID));
219 UuId::Parse(const String& str, UuId& uuid)
223 int length = str.GetLength();
225 SysTryReturn(NID_BASE, length == 36, E_INVALID_ARG, E_INVALID_ARG, ("[E_INVALID_ARG] The length of str(%d) MUST be 36."), length);
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());
230 // str[i] must be one of '0~9' or 'A~F' or 'a~f'
231 for (int i = 0; i < length; i++)
235 if (i == 8 || i == 13 || i == 18 || i == 23)
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;
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());
250 char mbstring[length + 1];
251 wchar_t wchar_tstring[length + 1];
253 wcsncpy(wchar_tstring, str.GetPointer(), length);
254 wchar_tstring[length] = 0;
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());
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]);
267 for (int i = 0; i < 32; ++i)
271 c[i] = (char) (c[i] - 'a' + (char) 10);
273 else if (c[i] >= 'A')
275 c[i] = (char) (c[i] - 'A' + (char) 10);
283 uuid.__uuid.x = c[0] * 0x10000000
291 uuid.__uuid.s1 = c[8] * 0x1000
295 uuid.__uuid.s2 = c[12] * 0x1000
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];
312 int length = str.GetLength();
314 SysTryReturnResult(NID_BASE, length == 36, E_INVALID_ARG, "The length of str(%d) MUST be 36.", length);
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());
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);
324 SysTryReturnResult(NID_BASE, ret == 0, E_INVALID_ARG, "The str(%ls) MUST consist of only alphanumeric characters and '-'.", str.GetPointer());
326 uuid_copy(uuid.uuid, uuidValue);
332 UuId::GenerateN(void)
335 UuId* pUuid = new (std::nothrow) UuId();
338 SysLogException(NID_BASE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
344 uuid_copy(pUuid->uuid, out);
350 UuId::GetInvalidUuId(void)
352 UuId invalidUuId(__INVALID_UUID);
357 UuId::ConvertToUuId(byte uuid[16])
361 for (int i = 0; i < 2; i++)
364 uuid[i] = uuid[3 - i];