2005-11-22 00:01:45 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 50; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2005-11-22 00:01:45 +03:00
|
|
|
|
2006-04-07 01:48:47 +04:00
|
|
|
#if defined(XP_WIN)
|
2005-11-22 00:01:45 +03:00
|
|
|
#include <windows.h>
|
2006-02-19 10:43:19 +03:00
|
|
|
#include <objbase.h>
|
2006-04-07 01:48:47 +04:00
|
|
|
#elif defined(XP_MACOSX)
|
|
|
|
#include <CoreFoundation/CoreFoundation.h>
|
2005-11-22 00:01:45 +03:00
|
|
|
#else
|
|
|
|
#include <stdlib.h>
|
2006-04-07 01:48:47 +04:00
|
|
|
#include "prrng.h"
|
2005-11-22 00:01:45 +03:00
|
|
|
#endif
|
|
|
|
|
2011-04-01 03:57:38 +04:00
|
|
|
#include "nsUUIDGenerator.h"
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-03-31 23:51:19 +04:00
|
|
|
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
using namespace mozilla;
|
|
|
|
|
2013-07-19 06:31:26 +04:00
|
|
|
NS_IMPL_ISUPPORTS1(nsUUIDGenerator, nsIUUIDGenerator)
|
2005-11-22 00:01:45 +03:00
|
|
|
|
|
|
|
nsUUIDGenerator::nsUUIDGenerator()
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
: mLock("nsUUIDGenerator.mLock")
|
2005-11-22 00:01:45 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2006-05-15 02:41:47 +04:00
|
|
|
nsUUIDGenerator::~nsUUIDGenerator()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2005-11-22 00:01:45 +03:00
|
|
|
nsresult
|
|
|
|
nsUUIDGenerator::Init()
|
|
|
|
{
|
2006-05-15 02:41:47 +04:00
|
|
|
// We're a service, so we're guaranteed that Init() is not going
|
|
|
|
// to be reentered while we're inside Init().
|
|
|
|
|
2010-06-01 23:02:42 +04:00
|
|
|
#if !defined(XP_WIN) && !defined(XP_MACOSX) && !defined(ANDROID)
|
2005-11-22 00:01:45 +03:00
|
|
|
/* initialize random number generator using NSPR random noise */
|
|
|
|
unsigned int seed;
|
|
|
|
|
2012-08-09 11:10:11 +04:00
|
|
|
size_t bytes = 0;
|
2005-11-22 00:01:45 +03:00
|
|
|
while (bytes < sizeof(seed)) {
|
2012-08-09 11:10:11 +04:00
|
|
|
size_t nbytes = PR_GetRandomNoise(((unsigned char *)&seed)+bytes,
|
2005-11-22 00:01:45 +03:00
|
|
|
sizeof(seed)-bytes);
|
|
|
|
if (nbytes == 0) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
bytes += nbytes;
|
|
|
|
}
|
|
|
|
|
2006-08-14 22:53:34 +04:00
|
|
|
/* Initialize a new RNG state, and immediately switch
|
|
|
|
* back to the previous one -- we want to use mState
|
|
|
|
* only for our own calls to random().
|
|
|
|
*/
|
2006-08-15 04:52:21 +04:00
|
|
|
mSavedState = initstate(seed, mState, sizeof(mState));
|
|
|
|
setstate(mSavedState);
|
2005-11-22 00:01:45 +03:00
|
|
|
|
|
|
|
mRBytes = 4;
|
|
|
|
#ifdef RAND_MAX
|
|
|
|
if ((unsigned long) RAND_MAX < (unsigned long)0xffffffff)
|
|
|
|
mRBytes = 3;
|
|
|
|
if ((unsigned long) RAND_MAX < (unsigned long)0x00ffffff)
|
|
|
|
mRBytes = 2;
|
|
|
|
if ((unsigned long) RAND_MAX < (unsigned long)0x0000ffff)
|
|
|
|
mRBytes = 1;
|
|
|
|
if ((unsigned long) RAND_MAX < (unsigned long)0x000000ff)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
#endif
|
|
|
|
|
2006-04-07 01:48:47 +04:00
|
|
|
#endif /* non XP_WIN and non XP_MACOSX */
|
2005-11-22 00:01:45 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUUIDGenerator::GenerateUUID(nsID** ret)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsID *id = static_cast<nsID*>(NS_Alloc(sizeof(nsID)));
|
2012-07-30 18:20:58 +04:00
|
|
|
if (id == nullptr)
|
2005-11-22 00:01:45 +03:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsresult rv = GenerateUUIDInPlace(id);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_Free(id);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
*ret = id;
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsUUIDGenerator::GenerateUUIDInPlace(nsID* id)
|
|
|
|
{
|
2006-05-15 02:41:47 +04:00
|
|
|
// The various code in this method is probably not threadsafe, so lock
|
|
|
|
// across the whole method.
|
Rollup of bug 645263 and bug 646259: Switch to mozilla:: sync primitives. r=cjones,dbaron,doublec,ehsan src=bsmedberg
Bug 645263, part 0: Count sync primitive ctor/dtors. r=dbaron
Bug 645263, part 1: Migrate content/media to mozilla:: sync primitives. r=doublec
Bug 645263, part 2: Migrate modules/plugin to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 3: Migrate nsComponentManagerImpl to mozilla:: sync primitives. sr=bsmedberg
Bug 645263, part 4: Migrate everything else to mozilla:: sync primitives. r=dbaron
Bug 645263, part 5: Remove nsAutoLock.*. sr=bsmedberg
Bug 645263, part 6: Make editor test be nicer to deadlock detector. r=ehsan
Bug 645263, part 7: Disable tracemalloc backtraces for xpcshell tests. r=dbaron
Bug 646259: Fix nsCacheService to use a CondVar for notifying. r=cjones
2011-04-01 08:29:02 +04:00
|
|
|
MutexAutoLock lock(mLock);
|
2009-02-11 02:10:07 +03:00
|
|
|
|
2011-05-01 22:59:24 +04:00
|
|
|
#if defined(XP_WIN)
|
2005-11-22 00:01:45 +03:00
|
|
|
HRESULT hr = CoCreateGuid((GUID*)id);
|
2012-08-16 15:58:11 +04:00
|
|
|
if (FAILED(hr))
|
2005-11-22 00:01:45 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2006-04-07 01:48:47 +04:00
|
|
|
#elif defined(XP_MACOSX)
|
|
|
|
CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault);
|
|
|
|
if (!uuid)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
CFUUIDBytes bytes = CFUUIDGetUUIDBytes(uuid);
|
|
|
|
memcpy(id, &bytes, sizeof(nsID));
|
|
|
|
|
|
|
|
CFRelease(uuid);
|
|
|
|
#else /* not windows or OS X; generate randomness using random(). */
|
2006-08-15 04:52:21 +04:00
|
|
|
/* XXX we should be saving the return of setstate here and switching
|
|
|
|
* back to it; instead, we use the value returned when we called
|
|
|
|
* initstate, since older glibc's have broken setstate() return values
|
|
|
|
*/
|
2010-06-01 23:02:42 +04:00
|
|
|
#ifndef ANDROID
|
2006-08-15 04:52:21 +04:00
|
|
|
setstate(mState);
|
2010-06-01 23:02:42 +04:00
|
|
|
#endif
|
2006-08-14 22:53:34 +04:00
|
|
|
|
2012-08-09 11:10:11 +04:00
|
|
|
size_t bytesLeft = sizeof(nsID);
|
2005-11-22 00:01:45 +03:00
|
|
|
while (bytesLeft > 0) {
|
2010-06-01 23:02:42 +04:00
|
|
|
#ifdef ANDROID
|
|
|
|
long rval = arc4random();
|
2013-05-24 21:56:45 +04:00
|
|
|
const size_t mRBytes = 4;
|
2010-06-01 23:02:42 +04:00
|
|
|
#else
|
2005-11-22 00:01:45 +03:00
|
|
|
long rval = random();
|
2010-06-01 23:02:42 +04:00
|
|
|
#endif
|
|
|
|
|
2005-11-22 00:01:45 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t *src = (uint8_t*)&rval;
|
2006-06-15 08:40:21 +04:00
|
|
|
// We want to grab the mRBytes least significant bytes of rval, since
|
|
|
|
// mRBytes less than sizeof(rval) means the high bytes are 0.
|
|
|
|
#ifdef IS_BIG_ENDIAN
|
2005-11-22 00:01:45 +03:00
|
|
|
src += sizeof(rval) - mRBytes;
|
|
|
|
#endif
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t *dst = ((uint8_t*) id) + (sizeof(nsID) - bytesLeft);
|
2012-08-09 11:10:11 +04:00
|
|
|
size_t toWrite = (bytesLeft < mRBytes ? bytesLeft : mRBytes);
|
|
|
|
for (size_t i = 0; i < toWrite; i++)
|
2005-11-22 00:01:45 +03:00
|
|
|
dst[i] = src[i];
|
|
|
|
|
|
|
|
bytesLeft -= toWrite;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Put in the version */
|
|
|
|
id->m2 &= 0x0fff;
|
|
|
|
id->m2 |= 0x4000;
|
|
|
|
|
|
|
|
/* Put in the variant */
|
|
|
|
id->m3[0] &= 0x3f;
|
|
|
|
id->m3[0] |= 0x80;
|
2006-08-14 22:53:34 +04:00
|
|
|
|
2010-06-01 23:02:42 +04:00
|
|
|
#ifndef ANDROID
|
2006-08-14 22:53:34 +04:00
|
|
|
/* Restore the previous RNG state */
|
2006-08-15 04:52:21 +04:00
|
|
|
setstate(mSavedState);
|
2010-06-01 23:02:42 +04:00
|
|
|
#endif
|
2005-11-22 00:01:45 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|