Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(910)

Side by Side Diff: obsolete/breakpad/common/linux/guid_creator.cc

Issue 10928195: First round of dead file removal (Closed) Base URL: https://github.com/samclegg/nativeclient-sdk.git@master
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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 <cassert>
31 #include <cstdio>
32 #include <cstdlib>
33 #include <ctime>
34 #include <unistd.h>
35
36 #include "common/linux/guid_creator.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 GUIDGenerator() {
49 srandom(time(NULL));
50 }
51
52 static u_int32_t BytesToUInt32(const u_int8_t bytes[]) {
53 return ((u_int32_t) bytes[0]
54 | ((u_int32_t) bytes[1] << 8)
55 | ((u_int32_t) bytes[2] << 16)
56 | ((u_int32_t) bytes[3] << 24));
57 }
58
59 static void UInt32ToBytes(u_int8_t bytes[], u_int32_t n) {
60 bytes[0] = n & 0xff;
61 bytes[1] = (n >> 8) & 0xff;
62 bytes[2] = (n >> 16) & 0xff;
63 bytes[3] = (n >> 24) & 0xff;
64 }
65
66 bool CreateGUID(GUID *guid) const {
67 guid->data1 = random();
68 guid->data2 = (u_int16_t)(random());
69 guid->data3 = (u_int16_t)(random());
70 UInt32ToBytes(&guid->data4[0], random());
71 UInt32ToBytes(&guid->data4[4], random());
72 return true;
73 }
74 };
75
76 // Guid generator.
77 const GUIDGenerator kGuidGenerator;
78
79 bool CreateGUID(GUID *guid) {
80 return kGuidGenerator.CreateGUID(guid);
81 }
82
83 // Parse guid to string.
84 bool GUIDToString(const GUID *guid, char *buf, int buf_len) {
85 // Should allow more space the the max length of GUID.
86 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 GUIDGenerator::BytesToUInt32(&(guid->data4[4])));
91 if (num != kGUIDStringLength)
92 return false;
93
94 buf[num] = '\0';
95 return true;
96 }
OLDNEW
« no previous file with comments | « obsolete/breakpad/common/linux/guid_creator.h ('k') | obsolete/breakpad/common/linux/http_upload.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698