Commit a2f9a603 authored by eric@webkit.org's avatar eric@webkit.org

2010-01-27 Kwang Yul Seo <skyul@company100.net>

        Reviewed by Eric Seidel.

        [BREWMP] Port WTF's randomNumber
        https://bugs.webkit.org/show_bug.cgi?id=33566

        Use GETRAND to generate 4 byte random byte sequence to implement
        weakRandomNumber. Create a secure random number generator with
        AEECLSID_RANDOM to implement randomNumber.

        * wtf/RandomNumber.cpp:
        (WTF::weakRandomNumber):
        (WTF::randomNumber):

git-svn-id: svn://svn.chromium.org/blink/trunk@53928 bbb929c8-8fbe-4397-9dbb-9b2b20218538
parent 85b92e07
2010-01-27 Kwang Yul Seo <skyul@company100.net>
Reviewed by Eric Seidel.
[BREWMP] Port WTF's randomNumber
https://bugs.webkit.org/show_bug.cgi?id=33566
Use GETRAND to generate 4 byte random byte sequence to implement
weakRandomNumber. Create a secure random number generator with
AEECLSID_RANDOM to implement randomNumber.
* wtf/RandomNumber.cpp:
(WTF::weakRandomNumber):
(WTF::randomNumber):
2010-01-27 Kwang Yul Seo <skyul@company100.net> 2010-01-27 Kwang Yul Seo <skyul@company100.net>
Reviewed by Eric Seidel. Reviewed by Eric Seidel.
......
...@@ -40,6 +40,12 @@ extern "C" { ...@@ -40,6 +40,12 @@ extern "C" {
} }
#endif #endif
#if PLATFORM(BREWMP)
#include <AEEAppGen.h>
#include <AEESource.h>
#include <AEEStdLib.h>
#endif
namespace WTF { namespace WTF {
double weakRandomNumber() double weakRandomNumber()
...@@ -47,6 +53,10 @@ double weakRandomNumber() ...@@ -47,6 +53,10 @@ double weakRandomNumber()
#if COMPILER(MSVC) && defined(_CRT_RAND_S) #if COMPILER(MSVC) && defined(_CRT_RAND_S)
// rand_s is incredibly slow on windows so we fall back on rand for Math.random // rand_s is incredibly slow on windows so we fall back on rand for Math.random
return (rand() + (rand() / (RAND_MAX + 1.0))) / (RAND_MAX + 1.0); return (rand() + (rand() / (RAND_MAX + 1.0))) / (RAND_MAX + 1.0);
#elif PLATFORM(BREWMP)
uint32_t bits;
GETRAND(reinterpret_cast<byte*>(&bits), sizeof(uint32_t));
return static_cast<double>(bits) / (static_cast<double>(std::numeric_limits<uint32_t>::max()) + 1.0);
#else #else
return randomNumber(); return randomNumber();
#endif #endif
...@@ -99,6 +109,16 @@ double randomNumber() ...@@ -99,6 +109,16 @@ double randomNumber()
// Mask off the low 53bits // Mask off the low 53bits
fullRandom &= (1LL << 53) - 1; fullRandom &= (1LL << 53) - 1;
return static_cast<double>(fullRandom)/static_cast<double>(1LL << 53); return static_cast<double>(fullRandom)/static_cast<double>(1LL << 53);
#elif PLATFORM(BREWMP)
uint32_t bits;
ISource* randomSource;
IShell* shell = reinterpret_cast<AEEApplet*>(GETAPPINSTANCE())->m_pIShell;
ISHELL_CreateInstance(shell, AEECLSID_RANDOM, reinterpret_cast<void**>(&randomSource));
ISOURCE_Read(randomSource, reinterpret_cast<char*>(&bits), 4);
ISOURCE_Release(randomSource);
return static_cast<double>(bits) / (static_cast<double>(std::numeric_limits<uint32_t>::max()) + 1.0);
#else #else
uint32_t part1 = rand() & (RAND_MAX - 1); uint32_t part1 = rand() & (RAND_MAX - 1);
uint32_t part2 = rand() & (RAND_MAX - 1); uint32_t part2 = rand() & (RAND_MAX - 1);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment