LCOV - app.info - toolkit/crashreporter/google-breakpad/src/common/linux/guid_creator.cc

LCOV - code coverage report
| Current view: | directory - toolkit/crashreporter/google-breakpad/src/common/linux - guid_creator.cc (source / functions) |  | Found | Hit | Coverage |
| Test: | app.info | Lines: | 32 | 31 | 96.9 %
| Date: | 2012-04-21 | Functions: | 9 | 8 | 88.9 %

           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            1396 :   GUIDGenerator() {
          49            1396 :     srandom(time(NULL));
          50            1396 :   }
          51                 :
          52            5504 :   static u_int32_t BytesToUInt32(const u_int8_t bytes[]) {
          53            5504 :     return ((u_int32_t) bytes[0]
          54            5504 :             | ((u_int32_t) bytes[1] << 8)
          55            5504 :             | ((u_int32_t) bytes[2] << 16)
          56           16512 :             | ((u_int32_t) bytes[3] << 24));
          57                 :   }
          58                 :
          59            5504 :   static void UInt32ToBytes(u_int8_t bytes[], u_int32_t n) {
          60            5504 :     bytes[0] = n & 0xff;
          61            5504 :     bytes[1] = (n >> 8) & 0xff;
          62            5504 :     bytes[2] = (n >> 16) & 0xff;
          63            5504 :     bytes[3] = (n >> 24) & 0xff;
          64            5504 :   }
          65                 :
          66            2752 :   bool CreateGUID(GUID *guid) const {
          67            2752 :     guid->data1 = random();
          68            2752 :     guid->data2 = (u_int16_t)(random());
          69            2752 :     guid->data3 = (u_int16_t)(random());
          70            2752 :     UInt32ToBytes(&guid->data4[0], random());
          71            2752 :     UInt32ToBytes(&guid->data4[4], random());
          72            2752 :     return true;
          73                 :   }
          74                 : };
          75                 :
          76                 : // Guid generator.
          77            1396 : const GUIDGenerator kGuidGenerator;
          78                 :
          79            2752 : bool CreateGUID(GUID *guid) {
          80            2752 :   return kGuidGenerator.CreateGUID(guid);
          81                 : }
          82                 :
          83                 : // Parse guid to string.
          84            2752 : bool GUIDToString(const GUID *guid, char *buf, int buf_len) {
          85                 :   // Should allow more space the the max length of GUID.
          86            2752 :   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            2752 :                      GUIDGenerator::BytesToUInt32(&(guid->data4[4])));
          91            2752 :   if (num != kGUIDStringLength)
          92               0 :     return false;
          93                 :
          94            2752 :   buf[num] = '\0';
          95            2752 :   return true;
          96            4188 : }

Generated by: LCOV version 1.7