guid.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. /*
  2. The MIT License (MIT)
  3. Copyright (c) 2014 Graeme Hill (http://graemehill.ca)
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. #include <cstring>
  21. #include "crossguid/guid.hpp"
  22. #ifdef GUID_LIBUUID
  23. #include <uuid/uuid.h>
  24. #endif
  25. #ifdef GUID_CFUUID
  26. #include <CoreFoundation/CFUUID.h>
  27. #endif
  28. #ifdef GUID_WINDOWS
  29. #include <objbase.h>
  30. #endif
  31. #ifdef GUID_ANDROID
  32. #include <jni.h>
  33. #include <cassert>
  34. #endif
  35. BEGIN_XG_NAMESPACE
  36. #ifdef GUID_ANDROID
  37. AndroidGuidInfo androidInfo;
  38. AndroidGuidInfo AndroidGuidInfo::fromJniEnv(JNIEnv *env)
  39. {
  40. AndroidGuidInfo info;
  41. info.env = env;
  42. auto localUuidClass = env->FindClass("java/util/UUID");
  43. info.uuidClass = (jclass)env->NewGlobalRef(localUuidClass);
  44. env->DeleteLocalRef(localUuidClass);
  45. info.newGuidMethod = env->GetStaticMethodID(
  46. info.uuidClass, "randomUUID", "()Ljava/util/UUID;");
  47. info.mostSignificantBitsMethod = env->GetMethodID(
  48. info.uuidClass, "getMostSignificantBits", "()J");
  49. info.leastSignificantBitsMethod = env->GetMethodID(
  50. info.uuidClass, "getLeastSignificantBits", "()J");
  51. info.initThreadId = std::this_thread::get_id();
  52. return info;
  53. }
  54. void initJni(JNIEnv *env)
  55. {
  56. androidInfo = AndroidGuidInfo::fromJniEnv(env);
  57. }
  58. #endif
  59. // overload << so that it's easy to convert to a string
  60. std::ostream &operator<<(std::ostream &s, const Guid &guid)
  61. {
  62. std::ios_base::fmtflags f(s.flags()); // politely don't leave the ostream in hex mode
  63. s << std::hex << std::setfill('0')
  64. << std::setw(2) << (int)guid._bytes[0]
  65. << std::setw(2) << (int)guid._bytes[1]
  66. << std::setw(2) << (int)guid._bytes[2]
  67. << std::setw(2) << (int)guid._bytes[3]
  68. << "-"
  69. << std::setw(2) << (int)guid._bytes[4]
  70. << std::setw(2) << (int)guid._bytes[5]
  71. << "-"
  72. << std::setw(2) << (int)guid._bytes[6]
  73. << std::setw(2) << (int)guid._bytes[7]
  74. << "-"
  75. << std::setw(2) << (int)guid._bytes[8]
  76. << std::setw(2) << (int)guid._bytes[9]
  77. << "-"
  78. << std::setw(2) << (int)guid._bytes[10]
  79. << std::setw(2) << (int)guid._bytes[11]
  80. << std::setw(2) << (int)guid._bytes[12]
  81. << std::setw(2) << (int)guid._bytes[13]
  82. << std::setw(2) << (int)guid._bytes[14]
  83. << std::setw(2) << (int)guid._bytes[15];
  84. s.flags(f);
  85. return s;
  86. }
  87. bool operator<(const xg::Guid &lhs, const xg::Guid &rhs)
  88. {
  89. return lhs.bytes() < rhs.bytes();
  90. }
  91. bool Guid::isValid() const
  92. {
  93. xg::Guid empty;
  94. return *this != empty;
  95. }
  96. // convert to string using std::snprintf() and std::string
  97. std::string Guid::str() const
  98. {
  99. char one[10], two[6], three[6], four[6], five[14];
  100. snprintf(one, 10, "%02x%02x%02x%02x",
  101. _bytes[0], _bytes[1], _bytes[2], _bytes[3]);
  102. snprintf(two, 6, "%02x%02x",
  103. _bytes[4], _bytes[5]);
  104. snprintf(three, 6, "%02x%02x",
  105. _bytes[6], _bytes[7]);
  106. snprintf(four, 6, "%02x%02x",
  107. _bytes[8], _bytes[9]);
  108. snprintf(five, 14, "%02x%02x%02x%02x%02x%02x",
  109. _bytes[10], _bytes[11], _bytes[12], _bytes[13], _bytes[14], _bytes[15]);
  110. const std::string sep("-");
  111. std::string out(one);
  112. out += sep + two;
  113. out += sep + three;
  114. out += sep + four;
  115. out += sep + five;
  116. return out;
  117. }
  118. // conversion operator for std::string
  119. Guid::operator std::string() const
  120. {
  121. return str();
  122. }
  123. // Access underlying bytes
  124. const std::array<unsigned char, 16>& Guid::bytes() const
  125. {
  126. return _bytes;
  127. }
  128. // create a guid from vector of bytes
  129. Guid::Guid(const std::array<unsigned char, 16> &bytes) : _bytes(bytes)
  130. { }
  131. // create a guid from vector of bytes
  132. Guid::Guid(std::array<unsigned char, 16> &&bytes) : _bytes(std::move(bytes))
  133. { }
  134. // converts a single hex char to a number (0 - 15)
  135. unsigned char hexDigitToChar(char ch)
  136. {
  137. // 0-9
  138. if (ch > 47 && ch < 58)
  139. return ch - 48;
  140. // a-f
  141. if (ch > 96 && ch < 103)
  142. return ch - 87;
  143. // A-F
  144. if (ch > 64 && ch < 71)
  145. return ch - 55;
  146. return 0;
  147. }
  148. bool isValidHexChar(char ch)
  149. {
  150. // 0-9
  151. if (ch > 47 && ch < 58)
  152. return true;
  153. // a-f
  154. if (ch > 96 && ch < 103)
  155. return true;
  156. // A-F
  157. if (ch > 64 && ch < 71)
  158. return true;
  159. return false;
  160. }
  161. // converts the two hexadecimal characters to an unsigned char (a byte)
  162. unsigned char hexPairToChar(char a, char b)
  163. {
  164. return hexDigitToChar(a) * 16 + hexDigitToChar(b);
  165. }
  166. // create empty guid
  167. Guid::Guid() : _bytes{ {0} }
  168. { }
  169. // set all bytes to zero
  170. void Guid::zeroify()
  171. {
  172. std::fill(_bytes.begin(), _bytes.end(), static_cast<unsigned char>(0));
  173. }
  174. // overload equality operator
  175. bool Guid::operator==(const Guid &other) const
  176. {
  177. return _bytes == other._bytes;
  178. }
  179. // overload inequality operator
  180. bool Guid::operator!=(const Guid &other) const
  181. {
  182. return !((*this) == other);
  183. }
  184. // member swap function
  185. void Guid::swap(Guid &other)
  186. {
  187. _bytes.swap(other._bytes);
  188. }
  189. // This is the linux friendly implementation, but it could work on other
  190. // systems that have libuuid available
  191. #ifdef GUID_LIBUUID
  192. Guid newGuid()
  193. {
  194. std::array<unsigned char, 16> data;
  195. static_assert(std::is_same<unsigned char[16], uuid_t>::value, "Wrong type!");
  196. uuid_generate(data.data());
  197. return Guid{std::move(data)};
  198. }
  199. #endif
  200. // this is the mac and ios version
  201. #ifdef GUID_CFUUID
  202. Guid newGuid()
  203. {
  204. auto newId = CFUUIDCreate(NULL);
  205. auto bytes = CFUUIDGetUUIDBytes(newId);
  206. CFRelease(newId);
  207. std::array<unsigned char, 16> byteArray =
  208. {{
  209. bytes.byte0,
  210. bytes.byte1,
  211. bytes.byte2,
  212. bytes.byte3,
  213. bytes.byte4,
  214. bytes.byte5,
  215. bytes.byte6,
  216. bytes.byte7,
  217. bytes.byte8,
  218. bytes.byte9,
  219. bytes.byte10,
  220. bytes.byte11,
  221. bytes.byte12,
  222. bytes.byte13,
  223. bytes.byte14,
  224. bytes.byte15
  225. }};
  226. return Guid{std::move(byteArray)};
  227. }
  228. #endif
  229. // obviously this is the windows version
  230. #ifdef GUID_WINDOWS
  231. Guid newGuid()
  232. {
  233. GUID newId;
  234. CoCreateGuid(&newId);
  235. std::array<unsigned char, 16> bytes =
  236. {
  237. (unsigned char)((newId.Data1 >> 24) & 0xFF),
  238. (unsigned char)((newId.Data1 >> 16) & 0xFF),
  239. (unsigned char)((newId.Data1 >> 8) & 0xFF),
  240. (unsigned char)((newId.Data1) & 0xff),
  241. (unsigned char)((newId.Data2 >> 8) & 0xFF),
  242. (unsigned char)((newId.Data2) & 0xff),
  243. (unsigned char)((newId.Data3 >> 8) & 0xFF),
  244. (unsigned char)((newId.Data3) & 0xFF),
  245. (unsigned char)newId.Data4[0],
  246. (unsigned char)newId.Data4[1],
  247. (unsigned char)newId.Data4[2],
  248. (unsigned char)newId.Data4[3],
  249. (unsigned char)newId.Data4[4],
  250. (unsigned char)newId.Data4[5],
  251. (unsigned char)newId.Data4[6],
  252. (unsigned char)newId.Data4[7]
  253. };
  254. return Guid{std::move(bytes)};
  255. }
  256. #endif
  257. // android version that uses a call to a java api
  258. #ifdef GUID_ANDROID
  259. Guid newGuid(JNIEnv *env)
  260. {
  261. assert(env != androidInfo.env || std::this_thread::get_id() == androidInfo.initThreadId);
  262. jobject javaUuid = env->CallStaticObjectMethod(
  263. androidInfo.uuidClass, androidInfo.newGuidMethod);
  264. jlong mostSignificant = env->CallLongMethod(javaUuid,
  265. androidInfo.mostSignificantBitsMethod);
  266. jlong leastSignificant = env->CallLongMethod(javaUuid,
  267. androidInfo.leastSignificantBitsMethod);
  268. std::array<unsigned char, 16> bytes =
  269. {
  270. (unsigned char)((mostSignificant >> 56) & 0xFF),
  271. (unsigned char)((mostSignificant >> 48) & 0xFF),
  272. (unsigned char)((mostSignificant >> 40) & 0xFF),
  273. (unsigned char)((mostSignificant >> 32) & 0xFF),
  274. (unsigned char)((mostSignificant >> 24) & 0xFF),
  275. (unsigned char)((mostSignificant >> 16) & 0xFF),
  276. (unsigned char)((mostSignificant >> 8) & 0xFF),
  277. (unsigned char)((mostSignificant) & 0xFF),
  278. (unsigned char)((leastSignificant >> 56) & 0xFF),
  279. (unsigned char)((leastSignificant >> 48) & 0xFF),
  280. (unsigned char)((leastSignificant >> 40) & 0xFF),
  281. (unsigned char)((leastSignificant >> 32) & 0xFF),
  282. (unsigned char)((leastSignificant >> 24) & 0xFF),
  283. (unsigned char)((leastSignificant >> 16) & 0xFF),
  284. (unsigned char)((leastSignificant >> 8) & 0xFF),
  285. (unsigned char)((leastSignificant) & 0xFF)
  286. };
  287. env->DeleteLocalRef(javaUuid);
  288. return Guid{std::move(bytes)};
  289. }
  290. Guid newGuid()
  291. {
  292. return newGuid(androidInfo.env);
  293. }
  294. #endif
  295. END_XG_NAMESPACE
  296. // Specialization for std::swap<Guid>() --
  297. // call member swap function of lhs, passing rhs
  298. namespace std
  299. {
  300. template <>
  301. void swap(xg::Guid &lhs, xg::Guid &rhs) noexcept
  302. {
  303. lhs.swap(rhs);
  304. }
  305. }