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 "common/linux/guid_creator.h"
31 :
32 : #include <assert.h>
33 : #include <stdio.h>
34 : #include <stdlib.h>
35 : #include <time.h>
36 : #include <unistd.h>
37 :
38 : //
39 : // GUIDGenerator
40 : //
41 : // This class is used to generate random GUID.
42 : // Currently use random number to generate a GUID since Linux has
43 : // no native GUID generator. This should be OK since we don't expect
44 : // crash to happen very offen.
45 : //
46 : class GUIDGenerator {
47 : public:
48 1464 : GUIDGenerator() {
49 1464 : srandom(time(NULL));
50 1464 : }
51 :
52 5588 : static u_int32_t BytesToUInt32(const u_int8_t bytes[]) {
53 5588 : return ((u_int32_t) bytes[0]
54 5588 : | ((u_int32_t) bytes[1] << 8)
55 5588 : | ((u_int32_t) bytes[2] << 16)
56 16764 : | ((u_int32_t) bytes[3] << 24));
57 : }
58 :
59 5588 : static void UInt32ToBytes(u_int8_t bytes[], u_int32_t n) {
60 5588 : bytes[0] = n & 0xff;
61 5588 : bytes[1] = (n >> 8) & 0xff;
62 5588 : bytes[2] = (n >> 16) & 0xff;
63 5588 : bytes[3] = (n >> 24) & 0xff;
64 5588 : }
65 :
66 2794 : bool CreateGUID(GUID *guid) const {
67 2794 : guid->data1 = random();
68 2794 : guid->data2 = (u_int16_t)(random());
69 2794 : guid->data3 = (u_int16_t)(random());
70 2794 : UInt32ToBytes(&guid->data4[0], random());
71 2794 : UInt32ToBytes(&guid->data4[4], random());
72 2794 : return true;
73 : }
74 : };
75 :
76 : // Guid generator.
77 1464 : const GUIDGenerator kGuidGenerator;
78 :
79 2794 : bool CreateGUID(GUID *guid) {
80 2794 : return kGuidGenerator.CreateGUID(guid);
81 : }
82 :
83 : // Parse guid to string.
84 2794 : bool GUIDToString(const GUID *guid, char *buf, int buf_len) {
85 : // Should allow more space the the max length of GUID.
86 2794 : assert(buf_len > kGUIDStringLength);
87 : int num = snprintf(buf, buf_len, kGUIDFormatString,
88 : guid->data1, guid->data2, guid->data3,
89 : GUIDGenerator::BytesToUInt32(&(guid->data4[0])),
90 2794 : GUIDGenerator::BytesToUInt32(&(guid->data4[4])));
91 2794 : if (num != kGUIDStringLength)
92 0 : return false;
93 :
94 2794 : buf[num] = '\0';
95 2794 : return true;
96 4392 : }
|