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.
25 #include <uuid/uuid.h>
26 #include <FBaseUuId.h>
27 #include <FBaseResult.h>
28 #include <FBaseByteBuffer.h>
29 #include <FBaseSysLog.h>
30 #include "FBase_StringConverter.h"
31 #include <unique_ptr.h>
34 namespace Tizen { namespace Base
37 const static UUID __INVALID_UUID = {0x00000000, 0x0000, 0x0000, {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
42 uuid_clear(this->uuid);
45 UuId::UuId(const UuId& uuid)
48 uuid_copy(this->uuid, uuid.uuid);
51 UuId::UuId(const UUID& uuid)
54 memcpy(this->uuid, &uuid, sizeof(this->uuid));
56 ConvertToUuId(this->uuid);
59 UuId::UuId(const byte uuid[16])
62 uuid_copy(this->uuid, uuid);
70 operator ==(const UUID& uuid1, const UuId& uuid2)
73 memcpy(uuid, &uuid1, sizeof(uuid));
74 UuId::ConvertToUuId(uuid);
76 int ret = uuid_compare(uuid, uuid2.uuid);
88 UuId::operator ==(const UuId& uuid) const
90 int ret = uuid_compare(this->uuid, uuid.uuid);
100 operator !=(const UUID& uuid1, const UuId& uuid2)
113 UuId::operator !=(const UuId& uuid) const
126 UuId::operator =(const UuId& uuid)
130 uuid_copy(this->uuid, uuid.uuid);
136 UuId::Equals(const Object& obj) const
138 const UuId* pOther = dynamic_cast<const UuId*>(&obj);
145 if (*this == *pOther)
156 UuId::GetHashCode(void) const
158 String str = ToString();
160 return str.GetHashCode();
164 UuId::ToString(void) const
167 char tmpuuid[sizeof(UUID)];
169 unsigned short s1 = 0;
170 unsigned short s2 = 0;
175 char pChar[1 + 2 * (sizeof(unsigned long) + sizeof(unsigned short) * 2 + sizeof(unsigned char) * 8) + 4];
177 memcpy(tmpuuid, &(__uuid), sizeof(UUID));
179 memcpy(&x, tmpuuid, sizeof(unsigned long));
180 pos += sizeof(unsigned long);
182 memcpy(&s1, tmpuuid + pos, sizeof(unsigned short));
183 pos += sizeof(unsigned short);
185 memcpy(&s2, tmpuuid + pos, sizeof(unsigned short));
186 pos += sizeof(unsigned short);
188 memcpy(&c1, tmpuuid + pos, sizeof(unsigned char) * 2);
189 pos += sizeof(unsigned char) * 2;
191 memcpy(&c2, tmpuuid + pos, sizeof(unsigned char) * 6);
193 sprintf(pChar, "%.8x-%.4x-%.4x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x",
194 x, s1, s2, c1[0], c1[1], c2[0], c2[1], c2[2], c2[3], c2[4], c2[5]);
196 return String(pChar);
199 char uuid_str[36] = {0,};
201 uuid_unparse(this->uuid, uuid_str);
203 return String(uuid_str);
207 UuId::ToUUID(void) const
210 byte uuidValue[16] = {0,};
212 uuid_copy(uuidValue, this->uuid);
213 ConvertToUuId(uuidValue);
215 memcpy(&uuid, uuidValue, sizeof(UUID));
221 UuId::Parse(const String& str, UuId& uuid)
225 int length = str.GetLength();
227 SysTryReturn(NID_BASE, length == 36, E_INVALID_ARG, E_INVALID_ARG, ("[E_INVALID_ARG] The length of str(%d) MUST be 36."), length);
229 SysTryReturn(NID_BASE, !(str[8] != L'-' || str[13] != L'-' || str[18] != L'-' || str[23] != L'-')
230 , E_INVALID_ARG, E_INVALID_ARG, ("[E_INVALID_ARG] The str(%ls) is not valid UuId type"), str.GetPointer());
232 // str[i] must be one of '0~9' or 'A~F' or 'a~f'
233 for (int i = 0; i < length; i++)
237 if (i == 8 || i == 13 || i == 18 || i == 23)
243 SysLogException(NID_BASE, E_INVALID_ARG, "[E_INVALID_ARG] The str(%ls) is not valid UuId type", str.GetPointer());
244 return E_INVALID_ARG;
248 SysTryReturn(NID_BASE, (str[i] >= '0' && str[i] <= '9') || (str[i] >= 'A' && str[i] <= 'F') ||
249 (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());
252 char mbstring[length + 1];
253 wchar_t wchar_tstring[length + 1];
255 wcsncpy(wchar_tstring, str.GetPointer(), length);
256 wchar_tstring[length] = 0;
258 length = wcstombs(mbstring, wchar_tstring, length);
259 SysTryReturn(NID_BASE, length == 36, E_INVALID_ARG, E_INVALID_ARG, "[E_INVALID_ARG] The str(%ls) is not valid UuId type", str.GetPointer());
262 "%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",
263 &c[0], &c[1], &c[2], &c[3], &c[4], &c[5], &c[6], &c[7],
264 &c[8], &c[9], &c[10], &c[11],
265 &c[12], &c[13], &c[14], &c[15],
266 &c[16], &c[17], &c[18], &c[19],
267 &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 for (int i = 0; i < 32; ++i)
273 c[i] = (char) (c[i] - 'a' + (char) 10);
275 else if (c[i] >= 'A')
277 c[i] = (char) (c[i] - 'A' + (char) 10);
285 uuid.__uuid.x = c[0] * 0x10000000
293 uuid.__uuid.s1 = c[8] * 0x1000
297 uuid.__uuid.s2 = c[12] * 0x1000
302 uuid.__uuid.c[0] = c[16] * 0x10 + c[17];
303 uuid.__uuid.c[1] = c[18] * 0x10 + c[19];
304 uuid.__uuid.c[2] = c[20] * 0x10 + c[21];
305 uuid.__uuid.c[3] = c[22] * 0x10 + c[23];
306 uuid.__uuid.c[4] = c[24] * 0x10 + c[25];
307 uuid.__uuid.c[5] = c[26] * 0x10 + c[27];
308 uuid.__uuid.c[6] = c[28] * 0x10 + c[29];
309 uuid.__uuid.c[7] = c[30] * 0x10 + c[31];
314 int length = str.GetLength();
316 SysTryReturnResult(NID_BASE, length == 36, E_INVALID_ARG, "The length of str(%d) MUST be 36.", length);
318 SysTryReturnResult(NID_BASE, !(str[8] != L'-' || str[13] != L'-' || str[18] != L'-' || str[23] != L'-')
319 ,E_INVALID_ARG, "The str(%ls) is not valid UuId type.", str.GetPointer());
322 std::unique_ptr<char []> pStr(_StringConverter::CopyToCharArrayN(str));
323 SysTryReturnResult(NID_BASE, pStr != null, E_OUT_OF_MEMORY, "Memory allocation failed.");
324 int ret = uuid_parse(pStr.get(), uuidValue);
326 SysTryReturnResult(NID_BASE, ret == 0, E_INVALID_ARG, "The str(%ls) MUST consist of only alphanumeric characters and '-'.", str.GetPointer());
328 uuid_copy(uuid.uuid, uuidValue);
334 UuId::GenerateN(void)
337 UuId* pUuid = new (std::nothrow) UuId();
340 SysLogException(NID_BASE, E_OUT_OF_MEMORY, "[%s] Memory allocation failed.", GetErrorMessage(E_OUT_OF_MEMORY));
346 uuid_copy(pUuid->uuid, out);
352 UuId::GetInvalidUuId(void)
354 UuId invalidUuId(__INVALID_UUID);
359 UuId::ConvertToUuId(byte uuid[16])
363 for (int i=0; i<2; i++)