PageRenderTime 51ms CodeModel.GetById 40ms app.highlight 8ms RepoModel.GetById 2ms app.codeStats 0ms

/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
 30#include "common/linux/guid_creator.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//
 47class GUIDGenerator {
 48 public:
 49  static u_int32_t BytesToUInt32(const u_int8_t bytes[]) {
 50    return ((u_int32_t) bytes[0]
 51            | ((u_int32_t) bytes[1] << 8)
 52            | ((u_int32_t) bytes[2] << 16)
 53            | ((u_int32_t) bytes[3] << 24));
 54  }
 55
 56  static void UInt32ToBytes(u_int8_t bytes[], u_int32_t n) {
 57    bytes[0] = n & 0xff;
 58    bytes[1] = (n >> 8) & 0xff;
 59    bytes[2] = (n >> 16) & 0xff;
 60    bytes[3] = (n >> 24) & 0xff;
 61  }
 62
 63  static bool CreateGUID(GUID *guid) {
 64    InitOnce();
 65    guid->data1 = random();
 66    guid->data2 = (u_int16_t)(random());
 67    guid->data3 = (u_int16_t)(random());
 68    UInt32ToBytes(&guid->data4[0], random());
 69    UInt32ToBytes(&guid->data4[4], random());
 70    return true;
 71  }
 72
 73 private:
 74  static void InitOnce() {
 75    pthread_once(&once_control, &InitOnceImpl);
 76  }
 77
 78  static void InitOnceImpl() {
 79    srandom(time(NULL));
 80  }
 81
 82  static pthread_once_t once_control;
 83};
 84
 85pthread_once_t GUIDGenerator::once_control = PTHREAD_ONCE_INIT;
 86
 87bool CreateGUID(GUID *guid) {
 88  return GUIDGenerator::CreateGUID(guid);
 89}
 90
 91// Parse guid to string.
 92bool GUIDToString(const GUID *guid, char *buf, int buf_len) {
 93  // Should allow more space the the max length of GUID.
 94  assert(buf_len > kGUIDStringLength);
 95  int num = snprintf(buf, buf_len, kGUIDFormatString,
 96                     guid->data1, guid->data2, guid->data3,
 97                     GUIDGenerator::BytesToUInt32(&(guid->data4[0])),
 98                     GUIDGenerator::BytesToUInt32(&(guid->data4[4])));
 99  if (num != kGUIDStringLength)
100    return false;
101
102  buf[num] = '\0';
103  return true;
104}