xpcom/base/nsUUIDGenerator.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /* -*- Mode: C++; tab-width: 50; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     2 /* This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #if defined(XP_WIN)
     7 #include <windows.h>
     8 #include <objbase.h>
     9 #elif defined(XP_MACOSX)
    10 #include <CoreFoundation/CoreFoundation.h>
    11 #else
    12 #include <stdlib.h>
    13 #include "prrng.h"
    14 #endif
    16 #include "nsUUIDGenerator.h"
    18 using namespace mozilla;
    20 NS_IMPL_ISUPPORTS(nsUUIDGenerator, nsIUUIDGenerator)
    22 nsUUIDGenerator::nsUUIDGenerator()
    23     : mLock("nsUUIDGenerator.mLock")
    24 {
    25 }
    27 nsUUIDGenerator::~nsUUIDGenerator()
    28 {
    29 }
    31 nsresult
    32 nsUUIDGenerator::Init()
    33 {
    34     // We're a service, so we're guaranteed that Init() is not going
    35     // to be reentered while we're inside Init().
    37 #if !defined(XP_WIN) && !defined(XP_MACOSX) && !defined(ANDROID)
    38     /* initialize random number generator using NSPR random noise */
    39     unsigned int seed;
    41     size_t bytes = 0;
    42     while (bytes < sizeof(seed)) {
    43         size_t nbytes = PR_GetRandomNoise(((unsigned char *)&seed)+bytes,
    44                                           sizeof(seed)-bytes);
    45         if (nbytes == 0) {
    46             return NS_ERROR_FAILURE;
    47         }
    48         bytes += nbytes;
    49     }
    51     /* Initialize a new RNG state, and immediately switch
    52      * back to the previous one -- we want to use mState
    53      * only for our own calls to random().
    54      */
    55     mSavedState = initstate(seed, mState, sizeof(mState));
    56     setstate(mSavedState);
    58     mRBytes = 4;
    59 #ifdef RAND_MAX
    60     if ((unsigned long) RAND_MAX < (unsigned long)0xffffffff)
    61         mRBytes = 3;
    62     if ((unsigned long) RAND_MAX < (unsigned long)0x00ffffff)
    63         mRBytes = 2;
    64     if ((unsigned long) RAND_MAX < (unsigned long)0x0000ffff)
    65         mRBytes = 1;
    66     if ((unsigned long) RAND_MAX < (unsigned long)0x000000ff)
    67         return NS_ERROR_FAILURE;
    68 #endif
    70 #endif /* non XP_WIN and non XP_MACOSX */
    72     return NS_OK;
    73 }
    75 NS_IMETHODIMP
    76 nsUUIDGenerator::GenerateUUID(nsID** ret)
    77 {
    78     nsID *id = static_cast<nsID*>(NS_Alloc(sizeof(nsID)));
    79     if (id == nullptr)
    80         return NS_ERROR_OUT_OF_MEMORY;
    82     nsresult rv = GenerateUUIDInPlace(id);
    83     if (NS_FAILED(rv)) {
    84         NS_Free(id);
    85         return rv;
    86     }
    88     *ret = id;
    89     return rv;
    90 }
    92 NS_IMETHODIMP
    93 nsUUIDGenerator::GenerateUUIDInPlace(nsID* id)
    94 {
    95     // The various code in this method is probably not threadsafe, so lock
    96     // across the whole method.
    97     MutexAutoLock lock(mLock);
    99 #if defined(XP_WIN)
   100     HRESULT hr = CoCreateGuid((GUID*)id);
   101     if (FAILED(hr))
   102         return NS_ERROR_FAILURE;
   103 #elif defined(XP_MACOSX)
   104     CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
   105     if (!uuid)
   106         return NS_ERROR_FAILURE;
   108     CFUUIDBytes bytes = CFUUIDGetUUIDBytes(uuid);
   109     memcpy(id, &bytes, sizeof(nsID));
   111     CFRelease(uuid);
   112 #else /* not windows or OS X; generate randomness using random(). */
   113     /* XXX we should be saving the return of setstate here and switching
   114      * back to it; instead, we use the value returned when we called
   115      * initstate, since older glibc's have broken setstate() return values
   116      */
   117 #ifndef ANDROID
   118     setstate(mState);
   119 #endif
   121     size_t bytesLeft = sizeof(nsID);
   122     while (bytesLeft > 0) {
   123 #ifdef ANDROID
   124         long rval = arc4random();
   125         const size_t mRBytes = 4;
   126 #else
   127         long rval = random();
   128 #endif
   131         uint8_t *src = (uint8_t*)&rval;
   132         // We want to grab the mRBytes least significant bytes of rval, since
   133         // mRBytes less than sizeof(rval) means the high bytes are 0.
   134 #ifdef IS_BIG_ENDIAN
   135         src += sizeof(rval) - mRBytes;
   136 #endif
   137         uint8_t *dst = ((uint8_t*) id) + (sizeof(nsID) - bytesLeft);
   138         size_t toWrite = (bytesLeft < mRBytes ? bytesLeft : mRBytes);
   139         for (size_t i = 0; i < toWrite; i++)
   140             dst[i] = src[i];
   142         bytesLeft -= toWrite;
   143     }
   145     /* Put in the version */
   146     id->m2 &= 0x0fff;
   147     id->m2 |= 0x4000;
   149     /* Put in the variant */
   150     id->m3[0] &= 0x3f;
   151     id->m3[0] |= 0x80;
   153 #ifndef ANDROID
   154     /* Restore the previous RNG state */
   155     setstate(mSavedState);
   156 #endif
   157 #endif
   159     return NS_OK;
   160 }

mercurial