UUID.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. //
  2. // UUID.cpp
  3. //
  4. // $Id: //poco/1.4/Foundation/src/UUID.cpp#2 $
  5. //
  6. // Library: Foundation
  7. // Package: UUID
  8. // Module: UUID
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #include "Poco/UUID.h"
  16. #include "Poco/ByteOrder.h"
  17. #include "Poco/Exception.h"
  18. #include <algorithm>
  19. #include <cstring>
  20. namespace Poco {
  21. UUID::UUID():
  22. _timeLow(0),
  23. _timeMid(0),
  24. _timeHiAndVersion(0),
  25. _clockSeq(0)
  26. {
  27. std::memset(_node, 0, sizeof(_node));
  28. }
  29. UUID::UUID(const UUID& uuid):
  30. _timeLow(uuid._timeLow),
  31. _timeMid(uuid._timeMid),
  32. _timeHiAndVersion(uuid._timeHiAndVersion),
  33. _clockSeq(uuid._clockSeq)
  34. {
  35. std::memcpy(_node, uuid._node, sizeof(_node));
  36. }
  37. UUID::UUID(const std::string& uuid)
  38. {
  39. parse(uuid);
  40. }
  41. UUID::UUID(const char* uuid)
  42. {
  43. poco_check_ptr (uuid);
  44. parse(std::string(uuid));
  45. }
  46. UUID::UUID(UInt32 timeLow, UInt32 timeMid, UInt32 timeHiAndVersion, UInt16 clockSeq, UInt8 node[]):
  47. _timeLow(timeLow),
  48. _timeMid(timeMid),
  49. _timeHiAndVersion(timeHiAndVersion),
  50. _clockSeq(clockSeq)
  51. {
  52. std::memcpy(_node, node, sizeof(_node));
  53. }
  54. UUID::UUID(const char* bytes, Version version)
  55. {
  56. UInt32 i32;
  57. UInt16 i16;
  58. std::memcpy(&i32, bytes, sizeof(i32));
  59. _timeLow = ByteOrder::fromNetwork(i32);
  60. bytes += sizeof(i32);
  61. std::memcpy(&i16, bytes, sizeof(i16));
  62. _timeMid = ByteOrder::fromNetwork(i16);
  63. bytes += sizeof(i16);
  64. std::memcpy(&i16, bytes, sizeof(i16));
  65. _timeHiAndVersion = ByteOrder::fromNetwork(i16);
  66. bytes += sizeof(i16);
  67. std::memcpy(&i16, bytes, sizeof(i16));
  68. _clockSeq = ByteOrder::fromNetwork(i16);
  69. bytes += sizeof(i16);
  70. std::memcpy(_node, bytes, sizeof(_node));
  71. _timeHiAndVersion &= 0x0FFF;
  72. _timeHiAndVersion |= (version << 12);
  73. _clockSeq &= 0x3FFF;
  74. _clockSeq |= 0x8000;
  75. }
  76. UUID::~UUID()
  77. {
  78. }
  79. UUID& UUID::operator = (const UUID& uuid)
  80. {
  81. if (&uuid != this)
  82. {
  83. _timeLow = uuid._timeLow;
  84. _timeMid = uuid._timeMid;
  85. _timeHiAndVersion = uuid._timeHiAndVersion;
  86. _clockSeq = uuid._clockSeq;
  87. std::memcpy(_node, uuid._node, sizeof(_node));
  88. }
  89. return *this;
  90. }
  91. void UUID::swap(UUID& uuid)
  92. {
  93. std::swap(_timeLow, uuid._timeLow);
  94. std::swap(_timeMid, uuid._timeMid);
  95. std::swap(_timeHiAndVersion, uuid._timeHiAndVersion);
  96. std::swap(_clockSeq, uuid._clockSeq);
  97. std::swap_ranges(_node, _node + 6, &uuid._node[0]);
  98. }
  99. void UUID::parse(const std::string& uuid)
  100. {
  101. if (!tryParse(uuid))
  102. throw SyntaxException(uuid);
  103. }
  104. bool UUID::tryParse(const std::string& uuid)
  105. {
  106. if (uuid.size() < 32)
  107. return false;
  108. bool haveHyphens = false;
  109. if (uuid[8] == '-' && uuid[13] == '-' && uuid[18] == '-' && uuid[23] == '-')
  110. {
  111. if (uuid.size() >= 36)
  112. haveHyphens = true;
  113. else
  114. return false;
  115. }
  116. std::string::const_iterator it = uuid.begin();
  117. _timeLow = 0;
  118. for (int i = 0; i < 8; ++i)
  119. {
  120. _timeLow = (_timeLow << 4) | nibble(*it++);
  121. }
  122. if (haveHyphens) ++it;
  123. _timeMid = 0;
  124. for (int i = 0; i < 4; ++i)
  125. {
  126. _timeMid = (_timeMid << 4) | nibble(*it++);
  127. }
  128. if (haveHyphens) ++it;
  129. _timeHiAndVersion = 0;
  130. for (int i = 0; i < 4; ++i)
  131. {
  132. _timeHiAndVersion = (_timeHiAndVersion << 4) | nibble(*it++);
  133. }
  134. if (haveHyphens) ++it;
  135. _clockSeq = 0;
  136. for (int i = 0; i < 4; ++i)
  137. {
  138. _clockSeq = (_clockSeq << 4) | nibble(*it++);
  139. }
  140. if (haveHyphens) ++it;
  141. for (int i = 0; i < 6; ++i)
  142. {
  143. _node[i] = (nibble(*it++) << 4) | nibble(*it++) ;
  144. }
  145. return true;
  146. }
  147. std::string UUID::toString() const
  148. {
  149. std::string result;
  150. result.reserve(36);
  151. appendHex(result, _timeLow);
  152. result += '-';
  153. appendHex(result, _timeMid);
  154. result += '-';
  155. appendHex(result, _timeHiAndVersion);
  156. result += '-';
  157. appendHex(result, _clockSeq);
  158. result += '-';
  159. for (int i = 0; i < sizeof(_node); ++i)
  160. appendHex(result, _node[i]);
  161. return result;
  162. }
  163. void UUID::copyFrom(const char* buffer)
  164. {
  165. UInt32 i32;
  166. UInt16 i16;
  167. std::memcpy(&i32, buffer, sizeof(i32));
  168. _timeLow = ByteOrder::fromNetwork(i32);
  169. buffer += sizeof(i32);
  170. std::memcpy(&i16, buffer, sizeof(i16));
  171. _timeMid = ByteOrder::fromNetwork(i16);
  172. buffer += sizeof(i16);
  173. std::memcpy(&i16, buffer, sizeof(i16));
  174. _timeHiAndVersion = ByteOrder::fromNetwork(i16);
  175. buffer += sizeof(i16);
  176. std::memcpy(&i16, buffer, sizeof(i16));
  177. _clockSeq = ByteOrder::fromNetwork(i16);
  178. buffer += sizeof(i16);
  179. std::memcpy(_node, buffer, sizeof(_node));
  180. }
  181. void UUID::copyTo(char* buffer) const
  182. {
  183. UInt32 i32 = ByteOrder::toNetwork(_timeLow);
  184. std::memcpy(buffer, &i32, sizeof(i32));
  185. buffer += sizeof(i32);
  186. UInt16 i16 = ByteOrder::toNetwork(_timeMid);
  187. std::memcpy(buffer, &i16, sizeof(i16));
  188. buffer += sizeof(i16);
  189. i16 = ByteOrder::toNetwork(_timeHiAndVersion);
  190. std::memcpy(buffer, &i16, sizeof(i16));
  191. buffer += sizeof(i16);
  192. i16 = ByteOrder::toNetwork(_clockSeq);
  193. std::memcpy(buffer, &i16, sizeof(i16));
  194. buffer += sizeof(i16);
  195. std::memcpy(buffer, _node, sizeof(_node));
  196. }
  197. int UUID::variant() const
  198. {
  199. int v = _clockSeq >> 13;
  200. if ((v & 6) == 6)
  201. return v;
  202. else if (v & 4)
  203. return 2;
  204. else
  205. return 0;
  206. }
  207. int UUID::compare(const UUID& uuid) const
  208. {
  209. if (_timeLow != uuid._timeLow) return _timeLow < uuid._timeLow ? -1 : 1;
  210. if (_timeMid != uuid._timeMid) return _timeMid < uuid._timeMid ? -1 : 1;
  211. if (_timeHiAndVersion != uuid._timeHiAndVersion) return _timeHiAndVersion < uuid._timeHiAndVersion ? -1 : 1;
  212. if (_clockSeq != uuid._clockSeq) return _clockSeq < uuid._clockSeq ? -1 : 1;
  213. for (int i = 0; i < sizeof(_node); ++i)
  214. {
  215. if (_node[i] < uuid._node[i])
  216. return -1;
  217. else if (_node[i] > uuid._node[i])
  218. return 1;
  219. }
  220. return 0;
  221. }
  222. void UUID::appendHex(std::string& str, UInt8 n)
  223. {
  224. static const char* digits = "0123456789abcdef";
  225. str += digits[(n >> 4) & 0xF];
  226. str += digits[n & 0xF];
  227. }
  228. void UUID::appendHex(std::string& str, UInt16 n)
  229. {
  230. appendHex(str, UInt8(n >> 8));
  231. appendHex(str, UInt8(n & 0xFF));
  232. }
  233. void UUID::appendHex(std::string& str, UInt32 n)
  234. {
  235. appendHex(str, UInt16(n >> 16));
  236. appendHex(str, UInt16(n & 0xFFFF));
  237. }
  238. UInt8 UUID::nibble(char hex)
  239. {
  240. if (hex >= 'a' && hex <= 'f')
  241. return UInt8(hex - 'a' + 10);
  242. else if (hex >= 'A' && hex <= 'F')
  243. return UInt8(hex - 'A' + 10);
  244. else if (hex >= '0' && hex <= '9')
  245. return UInt8(hex - '0');
  246. else
  247. return UInt8(0);
  248. }
  249. void UUID::fromNetwork()
  250. {
  251. _timeLow = ByteOrder::fromNetwork(_timeLow);
  252. _timeMid = ByteOrder::fromNetwork(_timeMid);
  253. _timeHiAndVersion = ByteOrder::fromNetwork(_timeHiAndVersion);
  254. _clockSeq = ByteOrder::fromNetwork(_clockSeq);
  255. }
  256. void UUID::toNetwork()
  257. {
  258. _timeLow = ByteOrder::toNetwork(_timeLow);
  259. _timeMid = ByteOrder::toNetwork(_timeMid);
  260. _timeHiAndVersion = ByteOrder::toNetwork(_timeHiAndVersion);
  261. _clockSeq = ByteOrder::toNetwork(_clockSeq);
  262. }
  263. namespace
  264. {
  265. static UUID uuidNull;
  266. static UUID uuidDNS("6ba7b810-9dad-11d1-80b4-00c04fd430c8");
  267. static UUID uuidURI("6ba7b811-9dad-11d1-80b4-00c04fd430c8");
  268. static UUID uuidOID("6ba7b812-9dad-11d1-80b4-00c04fd430c8");
  269. static UUID uuidX500("6ba7b814-9dad-11d1-80b4-00c04fd430c8");
  270. }
  271. const UUID& UUID::null()
  272. {
  273. return uuidNull;
  274. }
  275. const UUID& UUID::dns()
  276. {
  277. return uuidDNS;
  278. }
  279. const UUID& UUID::uri()
  280. {
  281. return uuidURI;
  282. }
  283. const UUID& UUID::oid()
  284. {
  285. return uuidOID;
  286. }
  287. const UUID& UUID::x500()
  288. {
  289. return uuidX500;
  290. }
  291. } // namespace Poco