/thirdparty/breakpad/common/linux/guid_creator.cc

http://github.com/tomahawk-player/tomahawk · C++ · 104 lines · 54 code · 12 blank · 38 comment · 2 complexity · 79abb725c7793389867e811092583df4 MD5 · raw file

  1. // Copyright (c) 2006, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. #include "common/linux/guid_creator.h"
  30. #include <assert.h>
  31. #include <pthread.h>
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <time.h>
  35. #include <unistd.h>
  36. //
  37. // GUIDGenerator
  38. //
  39. // This class is used to generate random GUID.
  40. // Currently use random number to generate a GUID since Linux has
  41. // no native GUID generator. This should be OK since we don't expect
  42. // crash to happen very offen.
  43. //
  44. class GUIDGenerator {
  45. public:
  46. static u_int32_t BytesToUInt32(const u_int8_t bytes[]) {
  47. return ((u_int32_t) bytes[0]
  48. | ((u_int32_t) bytes[1] << 8)
  49. | ((u_int32_t) bytes[2] << 16)
  50. | ((u_int32_t) bytes[3] << 24));
  51. }
  52. static void UInt32ToBytes(u_int8_t bytes[], u_int32_t n) {
  53. bytes[0] = n & 0xff;
  54. bytes[1] = (n >> 8) & 0xff;
  55. bytes[2] = (n >> 16) & 0xff;
  56. bytes[3] = (n >> 24) & 0xff;
  57. }
  58. static bool CreateGUID(GUID *guid) {
  59. InitOnce();
  60. guid->data1 = random();
  61. guid->data2 = (u_int16_t)(random());
  62. guid->data3 = (u_int16_t)(random());
  63. UInt32ToBytes(&guid->data4[0], random());
  64. UInt32ToBytes(&guid->data4[4], random());
  65. return true;
  66. }
  67. private:
  68. static void InitOnce() {
  69. pthread_once(&once_control, &InitOnceImpl);
  70. }
  71. static void InitOnceImpl() {
  72. srandom(time(NULL));
  73. }
  74. static pthread_once_t once_control;
  75. };
  76. pthread_once_t GUIDGenerator::once_control = PTHREAD_ONCE_INIT;
  77. bool CreateGUID(GUID *guid) {
  78. return GUIDGenerator::CreateGUID(guid);
  79. }
  80. // Parse guid to string.
  81. bool GUIDToString(const GUID *guid, char *buf, int buf_len) {
  82. // Should allow more space the the max length of GUID.
  83. assert(buf_len > kGUIDStringLength);
  84. int num = snprintf(buf, buf_len, kGUIDFormatString,
  85. guid->data1, guid->data2, guid->data3,
  86. GUIDGenerator::BytesToUInt32(&(guid->data4[0])),
  87. GUIDGenerator::BytesToUInt32(&(guid->data4[4])));
  88. if (num != kGUIDStringLength)
  89. return false;
  90. buf[num] = '\0';
  91. return true;
  92. }