Line data Source code
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 :
30 : #include "linux/handler/guid_generator.h"
31 :
32 : #include <assert.h>
33 : #include <pthread.h>
34 : #include <stdio.h>
35 : #include <stdlib.h>
36 : #include <time.h>
37 : #include <unistd.h>
38 :
39 : //
40 : // GUIDGenerator
41 : //
42 : // This class is used to generate random GUID.
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
45 : // crash to happen very offen.
46 : //
47 : class GUIDGenerator {
48 : public:
49 0 : static uint16_t BytesToUInt16(const uint8_t bytes[]) {
50 0 : return ((uint16_t) bytes[1] << 8) | ((uint16_t) bytes[0]);
51 : }
52 :
53 : // The last field in a GUID is 48 bits long so we're converting only 6 bytes
54 0 : static uint64_t BytesToUInt48(const uint8_t bytes[]) {
55 0 : return ((uint64_t) bytes[0] << 40) | ((uint64_t) bytes[1] << 32) |
56 0 : ((uint64_t) bytes[2] << 24) | ((uint64_t) bytes[3] << 16) |
57 0 : ((uint64_t) bytes[4] << 8) | (uint64_t) bytes[5];
58 : }
59 :
60 0 : static void UInt32ToBytes(uint8_t bytes[], uint32_t n) {
61 0 : bytes[0] = n & 0xff;
62 0 : bytes[1] = (n >> 8) & 0xff;
63 0 : bytes[2] = (n >> 16) & 0xff;
64 0 : bytes[3] = (n >> 24) & 0xff;
65 0 : }
66 :
67 0 : static bool CreateGUID(GUID *guid) {
68 0 : InitOnce();
69 0 : guid->data1 = random();
70 0 : guid->data2 = (uint16_t)(random());
71 0 : guid->data3 = (uint16_t)(random());
72 0 : UInt32ToBytes(&guid->data4[0], random());
73 0 : UInt32ToBytes(&guid->data4[4], random());
74 0 : return true;
75 : }
76 :
77 : private:
78 0 : static void InitOnce() {
79 0 : pthread_once(&once_control, &InitOnceImpl);
80 0 : }
81 :
82 0 : static void InitOnceImpl() {
83 0 : srandom(time(NULL));
84 0 : }
85 :
86 : static pthread_once_t once_control;
87 : };
88 :
89 : pthread_once_t GUIDGenerator::once_control = PTHREAD_ONCE_INIT;
90 :
91 0 : bool CreateGUID(GUID *guid) {
92 0 : return GUIDGenerator::CreateGUID(guid);
93 : }
94 :
95 : // Parse guid to string.
96 0 : bool GUIDToString(const GUID *guid, char *buf, size_t buf_len) {
97 : // Should allow more space the the max length of GUID.
98 0 : assert(buf_len > kGUIDStringLength);
99 0 : int num = snprintf(buf, buf_len, kGUIDFormatString,
100 0 : guid->data1, guid->data2, guid->data3,
101 0 : GUIDGenerator::BytesToUInt16(&(guid->data4[0])),
102 0 : GUIDGenerator::BytesToUInt48(&(guid->data4[2])));
103 0 : if (num != kGUIDStringLength)
104 0 : return false;
105 :
106 0 : buf[num] = '\0';
107 0 : return true;
108 : }
|