uuid.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright 2014 The Crashpad Authors. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #if !defined(__STDC_FORMAT_MACROS)
  15. #define __STDC_FORMAT_MACROS
  16. #endif
  17. #include "util/misc/uuid.h"
  18. #include <inttypes.h>
  19. #include <stddef.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <type_traits>
  23. #include "base/logging.h"
  24. #include "base/rand_util.h"
  25. #include "base/strings/stringprintf.h"
  26. #include "base/strings/utf_string_conversions.h"
  27. #include "base/sys_byteorder.h"
  28. #if defined(OS_MACOSX)
  29. #include <uuid/uuid.h>
  30. #endif // OS_MACOSX
  31. namespace crashpad {
  32. static_assert(sizeof(UUID) == 16, "UUID must be 16 bytes");
  33. static_assert(std::is_pod<UUID>::value, "UUID must be POD");
  34. bool UUID::operator==(const UUID& that) const {
  35. return memcmp(this, &that, sizeof(*this)) == 0;
  36. }
  37. void UUID::InitializeToZero() {
  38. memset(this, 0, sizeof(*this));
  39. }
  40. void UUID::InitializeFromBytes(const uint8_t* bytes) {
  41. memcpy(this, bytes, sizeof(*this));
  42. data_1 = base::NetToHost32(data_1);
  43. data_2 = base::NetToHost16(data_2);
  44. data_3 = base::NetToHost16(data_3);
  45. }
  46. bool UUID::InitializeFromString(const base::StringPiece& string) {
  47. if (string.length() != 36)
  48. return false;
  49. UUID temp;
  50. static constexpr char kScanFormat[] =
  51. "%08" SCNx32 "-%04" SCNx16 "-%04" SCNx16
  52. "-%02" SCNx8 "%02" SCNx8
  53. "-%02" SCNx8 "%02" SCNx8 "%02" SCNx8 "%02" SCNx8 "%02" SCNx8 "%02" SCNx8;
  54. int rv = sscanf(string.data(),
  55. kScanFormat,
  56. &temp.data_1,
  57. &temp.data_2,
  58. &temp.data_3,
  59. &temp.data_4[0],
  60. &temp.data_4[1],
  61. &temp.data_5[0],
  62. &temp.data_5[1],
  63. &temp.data_5[2],
  64. &temp.data_5[3],
  65. &temp.data_5[4],
  66. &temp.data_5[5]);
  67. if (rv != 11)
  68. return false;
  69. *this = temp;
  70. return true;
  71. }
  72. bool UUID::InitializeWithNew() {
  73. #if defined(OS_MACOSX)
  74. uuid_t uuid;
  75. uuid_generate(uuid);
  76. InitializeFromBytes(uuid);
  77. return true;
  78. #elif defined(OS_WIN) || defined(OS_LINUX) || defined(OS_ANDROID)
  79. // Linux does not provide a UUID generator in a widely-available system
  80. // library. uuid_generate() from libuuid is not available everywhere.
  81. // On Windows, do not use UuidCreate() to avoid a dependency on rpcrt4, so
  82. // that this function is usable early in DllMain().
  83. base::RandBytes(this, sizeof(*this));
  84. // Set six bits per RFC 4122 §4.4 to identify this as a pseudo-random UUID.
  85. data_3 = (4 << 12) | (data_3 & 0x0fff); // §4.1.3
  86. data_4[0] = 0x80 | (data_4[0] & 0x3f); // §4.1.1
  87. return true;
  88. #else
  89. #error Port.
  90. #endif // OS_MACOSX
  91. }
  92. #if defined(OS_WIN)
  93. void UUID::InitializeFromSystemUUID(const ::UUID* system_uuid) {
  94. static_assert(sizeof(::UUID) == sizeof(UUID),
  95. "unexpected system uuid size");
  96. static_assert(offsetof(::UUID, Data1) == offsetof(UUID, data_1),
  97. "unexpected system uuid layout");
  98. memcpy(this, system_uuid, sizeof(*this));
  99. }
  100. #endif // OS_WIN
  101. std::string UUID::ToString() const {
  102. return base::StringPrintf("%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
  103. data_1,
  104. data_2,
  105. data_3,
  106. data_4[0],
  107. data_4[1],
  108. data_5[0],
  109. data_5[1],
  110. data_5[2],
  111. data_5[3],
  112. data_5[4],
  113. data_5[5]);
  114. }
  115. #if defined(OS_WIN)
  116. base::string16 UUID::ToString16() const {
  117. return base::UTF8ToUTF16(ToString());
  118. }
  119. #endif // OS_WIN
  120. } // namespace crashpad