uuid.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. #ifndef CRASHPAD_UTIL_MISC_UUID_H_
  15. #define CRASHPAD_UTIL_MISC_UUID_H_
  16. #include <stdint.h>
  17. #include <string>
  18. #include "base/strings/string16.h"
  19. #include "base/strings/string_piece.h"
  20. #include "build/build_config.h"
  21. #if defined(OS_WIN)
  22. #include <rpc.h>
  23. #endif
  24. namespace crashpad {
  25. //! \brief A universally unique identifier (%UUID).
  26. //!
  27. //! An alternate term for %UUID is “globally unique identifier” (GUID), used
  28. //! primarily by Microsoft.
  29. //!
  30. //! A %UUID is a unique 128-bit number specified by RFC 4122.
  31. //!
  32. //! This is a POD structure.
  33. struct UUID {
  34. bool operator==(const UUID& that) const;
  35. bool operator!=(const UUID& that) const { return !operator==(that); }
  36. //! \brief Initializes the %UUID to zero.
  37. void InitializeToZero();
  38. //! \brief Initializes the %UUID from a sequence of bytes.
  39. //!
  40. //! \a bytes is taken as a %UUID laid out in big-endian format in memory. On
  41. //! little-endian machines, appropriate byte-swapping will be performed to
  42. //! initialize an object’s data members.
  43. //!
  44. //! \param[in] bytes A buffer of exactly 16 bytes that will be assigned to the
  45. //! %UUID.
  46. void InitializeFromBytes(const uint8_t* bytes);
  47. //! \brief Initializes the %UUID from a RFC 4122 §3 formatted string.
  48. //!
  49. //! \param[in] string A string of the form
  50. //! `"00112233-4455-6677-8899-aabbccddeeff"`.
  51. //!
  52. //! \return `true` if the string was formatted correctly and the object has
  53. //! been initialized with the data. `false` if the string could not be
  54. //! parsed, with the object state untouched.
  55. bool InitializeFromString(const base::StringPiece& string);
  56. //! \brief Initializes the %UUID using a standard system facility to generate
  57. //! the value.
  58. //!
  59. //! \return `true` if the %UUID was initialized correctly, `false` otherwise
  60. //! with a message logged.
  61. bool InitializeWithNew();
  62. #if defined(OS_WIN) || DOXYGEN
  63. //! \brief Initializes the %UUID from a system `UUID` or `GUID` structure.
  64. //!
  65. //! \param[in] system_uuid A system `UUID` or `GUID` structure.
  66. void InitializeFromSystemUUID(const ::UUID* system_uuid);
  67. #endif // OS_WIN
  68. //! \brief Formats the %UUID per RFC 4122 §3.
  69. //!
  70. //! \return A string of the form `"00112233-4455-6677-8899-aabbccddeeff"`.
  71. std::string ToString() const;
  72. #if defined(OS_WIN) || DOXYGEN
  73. //! \brief The same as ToString, but returned as a string16.
  74. base::string16 ToString16() const;
  75. #endif // OS_WIN
  76. // These fields are laid out according to RFC 4122 §4.1.2.
  77. uint32_t data_1;
  78. uint16_t data_2;
  79. uint16_t data_3;
  80. uint8_t data_4[2];
  81. uint8_t data_5[6];
  82. };
  83. } // namespace crashpad
  84. #endif // CRASHPAD_UTIL_MISC_UUID_H_