OLD | NEW |
1 // Copyright (c) 2006, Google Inc. | 1 // Copyright (c) 2006, Google Inc. |
2 // All rights reserved. | 2 // All rights reserved. |
3 // | 3 // |
4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
6 // met: | 6 // met: |
7 // | 7 // |
8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
(...skipping 28 matching lines...) Expand all Loading... |
39 // | 39 // |
40 // GUIDGenerator | 40 // GUIDGenerator |
41 // | 41 // |
42 // This class is used to generate random GUID. | 42 // This class is used to generate random GUID. |
43 // Currently use random number to generate a GUID since Linux has | 43 // Currently use random number to generate a GUID since Linux has |
44 // no native GUID generator. This should be OK since we don't expect | 44 // no native GUID generator. This should be OK since we don't expect |
45 // crash to happen very offen. | 45 // crash to happen very offen. |
46 // | 46 // |
47 class GUIDGenerator { | 47 class GUIDGenerator { |
48 public: | 48 public: |
49 static u_int32_t BytesToUInt32(const u_int8_t bytes[]) { | 49 static uint32_t BytesToUInt32(const uint8_t bytes[]) { |
50 return ((u_int32_t) bytes[0] | 50 return ((uint32_t) bytes[0] |
51 | ((u_int32_t) bytes[1] << 8) | 51 | ((uint32_t) bytes[1] << 8) |
52 | ((u_int32_t) bytes[2] << 16) | 52 | ((uint32_t) bytes[2] << 16) |
53 | ((u_int32_t) bytes[3] << 24)); | 53 | ((uint32_t) bytes[3] << 24)); |
54 } | 54 } |
55 | 55 |
56 static void UInt32ToBytes(u_int8_t bytes[], u_int32_t n) { | 56 static void UInt32ToBytes(uint8_t bytes[], uint32_t n) { |
57 bytes[0] = n & 0xff; | 57 bytes[0] = n & 0xff; |
58 bytes[1] = (n >> 8) & 0xff; | 58 bytes[1] = (n >> 8) & 0xff; |
59 bytes[2] = (n >> 16) & 0xff; | 59 bytes[2] = (n >> 16) & 0xff; |
60 bytes[3] = (n >> 24) & 0xff; | 60 bytes[3] = (n >> 24) & 0xff; |
61 } | 61 } |
62 | 62 |
63 static bool CreateGUID(GUID *guid) { | 63 static bool CreateGUID(GUID *guid) { |
64 InitOnce(); | 64 InitOnce(); |
65 guid->data1 = random(); | 65 guid->data1 = random(); |
66 guid->data2 = (u_int16_t)(random()); | 66 guid->data2 = (uint16_t)(random()); |
67 guid->data3 = (u_int16_t)(random()); | 67 guid->data3 = (uint16_t)(random()); |
68 UInt32ToBytes(&guid->data4[0], random()); | 68 UInt32ToBytes(&guid->data4[0], random()); |
69 UInt32ToBytes(&guid->data4[4], random()); | 69 UInt32ToBytes(&guid->data4[4], random()); |
70 return true; | 70 return true; |
71 } | 71 } |
72 | 72 |
73 private: | 73 private: |
74 static void InitOnce() { | 74 static void InitOnce() { |
75 pthread_once(&once_control, &InitOnceImpl); | 75 pthread_once(&once_control, &InitOnceImpl); |
76 } | 76 } |
77 | 77 |
(...skipping 17 matching lines...) Expand all Loading... |
95 int num = snprintf(buf, buf_len, kGUIDFormatString, | 95 int num = snprintf(buf, buf_len, kGUIDFormatString, |
96 guid->data1, guid->data2, guid->data3, | 96 guid->data1, guid->data2, guid->data3, |
97 GUIDGenerator::BytesToUInt32(&(guid->data4[0])), | 97 GUIDGenerator::BytesToUInt32(&(guid->data4[0])), |
98 GUIDGenerator::BytesToUInt32(&(guid->data4[4]))); | 98 GUIDGenerator::BytesToUInt32(&(guid->data4[4]))); |
99 if (num != kGUIDStringLength) | 99 if (num != kGUIDStringLength) |
100 return false; | 100 return false; |
101 | 101 |
102 buf[num] = '\0'; | 102 buf[num] = '\0'; |
103 return true; | 103 return true; |
104 } | 104 } |
OLD | NEW |