From 6c902e62e23fd6d2baf06078c94085fe755c36d6 Mon Sep 17 00:00:00 2001 From: Dan Sinclair Date: Tue, 5 Sep 2017 13:22:36 -0400 Subject: Rename fx_rand to fx_random Match FX_Random method names. Change-Id: I2d178229563881479b80e1f86975f5b165475837 Reviewed-on: https://pdfium-review.googlesource.com/13093 Reviewed-by: Tom Sepez Commit-Queue: dsinclair --- BUILD.gn | 4 +- core/fpdfapi/edit/cpdf_creator.cpp | 2 +- core/fxcrt/fx_guid.cpp | 2 +- core/fxcrt/fx_rand.cpp | 141 ------------------------------------- core/fxcrt/fx_rand.h | 18 ----- core/fxcrt/fx_random.cpp | 141 +++++++++++++++++++++++++++++++++++++ core/fxcrt/fx_random.h | 18 +++++ 7 files changed, 163 insertions(+), 163 deletions(-) delete mode 100644 core/fxcrt/fx_rand.cpp delete mode 100644 core/fxcrt/fx_rand.h create mode 100644 core/fxcrt/fx_random.cpp create mode 100644 core/fxcrt/fx_random.h diff --git a/BUILD.gn b/BUILD.gn index 3d3917fe70..e66517e1a9 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -843,8 +843,8 @@ static_library("fxcrt") { "core/fxcrt/fx_extension.h", "core/fxcrt/fx_memory.cpp", "core/fxcrt/fx_memory.h", - "core/fxcrt/fx_rand.cpp", - "core/fxcrt/fx_rand.h", + "core/fxcrt/fx_random.cpp", + "core/fxcrt/fx_random.h", "core/fxcrt/fx_safe_types.h", "core/fxcrt/fx_stream.cpp", "core/fxcrt/fx_stream.h", diff --git a/core/fpdfapi/edit/cpdf_creator.cpp b/core/fpdfapi/edit/cpdf_creator.cpp index 7d9f2d0cc8..38a21ba0cb 100644 --- a/core/fpdfapi/edit/cpdf_creator.cpp +++ b/core/fpdfapi/edit/cpdf_creator.cpp @@ -19,7 +19,7 @@ #include "core/fpdfapi/parser/cpdf_string.h" #include "core/fpdfapi/parser/fpdf_parser_decode.h" #include "core/fxcrt/fx_extension.h" -#include "core/fxcrt/fx_rand.h" +#include "core/fxcrt/fx_random.h" namespace { diff --git a/core/fxcrt/fx_guid.cpp b/core/fxcrt/fx_guid.cpp index 6f130f9d46..4d3a4e9a5f 100644 --- a/core/fxcrt/fx_guid.cpp +++ b/core/fxcrt/fx_guid.cpp @@ -7,7 +7,7 @@ #include "core/fxcrt/fx_guid.h" #include "core/fxcrt/fx_extension.h" -#include "core/fxcrt/fx_rand.h" +#include "core/fxcrt/fx_random.h" void FX_GUID_CreateV4(FX_GUID* pGUID) { FX_Random_GenerateMT((uint32_t*)pGUID, 4); diff --git a/core/fxcrt/fx_rand.cpp b/core/fxcrt/fx_rand.cpp deleted file mode 100644 index 7a0c3e4d9b..0000000000 --- a/core/fxcrt/fx_rand.cpp +++ /dev/null @@ -1,141 +0,0 @@ -// Copyright 2017 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#include "core/fxcrt/fx_rand.h" - -#include "core/fxcrt/fx_memory.h" -#include "core/fxcrt/fx_string.h" -#include "core/fxcrt/fx_system.h" - -#define MT_N 848 -#define MT_M 456 -#define MT_Matrix_A 0x9908b0df -#define MT_Upper_Mask 0x80000000 -#define MT_Lower_Mask 0x7fffffff - -#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ -#include -#else // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ -#include -#endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ - -namespace { - -struct MTContext { - MTContext() { - mti = MT_N + 1; - bHaveSeed = false; - } - - uint32_t mti; - bool bHaveSeed; - uint32_t mt[MT_N]; -}; - -#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ -bool GenerateCryptoRandom(uint32_t* pBuffer, int32_t iCount) { - HCRYPTPROV hCP = 0; - if (!::CryptAcquireContext(&hCP, nullptr, nullptr, PROV_RSA_FULL, 0) || - !hCP) { - return false; - } - ::CryptGenRandom(hCP, iCount * sizeof(uint32_t), - reinterpret_cast(pBuffer)); - ::CryptReleaseContext(hCP, 0); - return true; -} -#endif - -} // namespace - -void* FX_Random_MT_Start(uint32_t dwSeed) { - MTContext* pContext = FX_Alloc(MTContext, 1); - pContext->mt[0] = dwSeed; - uint32_t& i = pContext->mti; - uint32_t* pBuf = pContext->mt; - for (i = 1; i < MT_N; i++) - pBuf[i] = (1812433253UL * (pBuf[i - 1] ^ (pBuf[i - 1] >> 30)) + i); - - pContext->bHaveSeed = true; - return pContext; -} - -uint32_t FX_Random_MT_Generate(void* pContext) { - ASSERT(pContext); - - MTContext* pMTC = static_cast(pContext); - uint32_t v; - static uint32_t mag[2] = {0, MT_Matrix_A}; - uint32_t& mti = pMTC->mti; - uint32_t* pBuf = pMTC->mt; - if ((int)mti < 0 || mti >= MT_N) { - if (mti > MT_N && !pMTC->bHaveSeed) - return 0; - - uint32_t kk; - for (kk = 0; kk < MT_N - MT_M; kk++) { - v = (pBuf[kk] & MT_Upper_Mask) | (pBuf[kk + 1] & MT_Lower_Mask); - pBuf[kk] = pBuf[kk + MT_M] ^ (v >> 1) ^ mag[v & 1]; - } - for (; kk < MT_N - 1; kk++) { - v = (pBuf[kk] & MT_Upper_Mask) | (pBuf[kk + 1] & MT_Lower_Mask); - pBuf[kk] = pBuf[kk + (MT_M - MT_N)] ^ (v >> 1) ^ mag[v & 1]; - } - v = (pBuf[MT_N - 1] & MT_Upper_Mask) | (pBuf[0] & MT_Lower_Mask); - pBuf[MT_N - 1] = pBuf[MT_M - 1] ^ (v >> 1) ^ mag[v & 1]; - mti = 0; - } - v = pBuf[mti++]; - v ^= (v >> 11); - v ^= (v << 7) & 0x9d2c5680UL; - v ^= (v << 15) & 0xefc60000UL; - v ^= (v >> 18); - return v; -} - -void FX_Random_MT_Close(void* pContext) { - ASSERT(pContext); - FX_Free(pContext); -} - -void FX_Random_GenerateMT(uint32_t* pBuffer, int32_t iCount) { - uint32_t dwSeed; -#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ - if (!GenerateCryptoRandom(&dwSeed, 1)) - FX_Random_GenerateBase(&dwSeed, 1); -#else - FX_Random_GenerateBase(&dwSeed, 1); -#endif - void* pContext = FX_Random_MT_Start(dwSeed); - while (iCount-- > 0) - *pBuffer++ = FX_Random_MT_Generate(pContext); - - FX_Random_MT_Close(pContext); -} - -void FX_Random_GenerateBase(uint32_t* pBuffer, int32_t iCount) { -#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ - SYSTEMTIME st1, st2; - ::GetSystemTime(&st1); - do { - ::GetSystemTime(&st2); - } while (memcmp(&st1, &st2, sizeof(SYSTEMTIME)) == 0); - uint32_t dwHash1 = - FX_HashCode_GetA(CFX_ByteStringC((uint8_t*)&st1, sizeof(st1)), true); - uint32_t dwHash2 = - FX_HashCode_GetA(CFX_ByteStringC((uint8_t*)&st2, sizeof(st2)), true); - ::srand((dwHash1 << 16) | (uint32_t)dwHash2); -#else - time_t tmLast = time(nullptr); - time_t tmCur; - while ((tmCur = time(nullptr)) == tmLast) - continue; - - ::srand((tmCur << 16) | (tmLast & 0xFFFF)); -#endif - while (iCount-- > 0) - *pBuffer++ = static_cast((::rand() << 16) | (::rand() & 0xFFFF)); -} diff --git a/core/fxcrt/fx_rand.h b/core/fxcrt/fx_rand.h deleted file mode 100644 index db5f48fee3..0000000000 --- a/core/fxcrt/fx_rand.h +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright 2017 PDFium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com - -#ifndef CORE_FXCRT_FX_RAND_H_ -#define CORE_FXCRT_FX_RAND_H_ - -#include - -void* FX_Random_MT_Start(uint32_t dwSeed); -void FX_Random_MT_Close(void* pContext); -uint32_t FX_Random_MT_Generate(void* pContext); -void FX_Random_GenerateBase(uint32_t* pBuffer, int32_t iCount); -void FX_Random_GenerateMT(uint32_t* pBuffer, int32_t iCount); - -#endif // CORE_FXCRT_FX_RAND_H_ diff --git a/core/fxcrt/fx_random.cpp b/core/fxcrt/fx_random.cpp new file mode 100644 index 0000000000..4d8fc0efcf --- /dev/null +++ b/core/fxcrt/fx_random.cpp @@ -0,0 +1,141 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#include "core/fxcrt/fx_random.h" + +#include "core/fxcrt/fx_memory.h" +#include "core/fxcrt/fx_string.h" +#include "core/fxcrt/fx_system.h" + +#define MT_N 848 +#define MT_M 456 +#define MT_Matrix_A 0x9908b0df +#define MT_Upper_Mask 0x80000000 +#define MT_Lower_Mask 0x7fffffff + +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ +#include +#else // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ +#include +#endif // _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ + +namespace { + +struct MTContext { + MTContext() { + mti = MT_N + 1; + bHaveSeed = false; + } + + uint32_t mti; + bool bHaveSeed; + uint32_t mt[MT_N]; +}; + +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ +bool GenerateCryptoRandom(uint32_t* pBuffer, int32_t iCount) { + HCRYPTPROV hCP = 0; + if (!::CryptAcquireContext(&hCP, nullptr, nullptr, PROV_RSA_FULL, 0) || + !hCP) { + return false; + } + ::CryptGenRandom(hCP, iCount * sizeof(uint32_t), + reinterpret_cast(pBuffer)); + ::CryptReleaseContext(hCP, 0); + return true; +} +#endif + +} // namespace + +void* FX_Random_MT_Start(uint32_t dwSeed) { + MTContext* pContext = FX_Alloc(MTContext, 1); + pContext->mt[0] = dwSeed; + uint32_t& i = pContext->mti; + uint32_t* pBuf = pContext->mt; + for (i = 1; i < MT_N; i++) + pBuf[i] = (1812433253UL * (pBuf[i - 1] ^ (pBuf[i - 1] >> 30)) + i); + + pContext->bHaveSeed = true; + return pContext; +} + +uint32_t FX_Random_MT_Generate(void* pContext) { + ASSERT(pContext); + + MTContext* pMTC = static_cast(pContext); + uint32_t v; + static uint32_t mag[2] = {0, MT_Matrix_A}; + uint32_t& mti = pMTC->mti; + uint32_t* pBuf = pMTC->mt; + if ((int)mti < 0 || mti >= MT_N) { + if (mti > MT_N && !pMTC->bHaveSeed) + return 0; + + uint32_t kk; + for (kk = 0; kk < MT_N - MT_M; kk++) { + v = (pBuf[kk] & MT_Upper_Mask) | (pBuf[kk + 1] & MT_Lower_Mask); + pBuf[kk] = pBuf[kk + MT_M] ^ (v >> 1) ^ mag[v & 1]; + } + for (; kk < MT_N - 1; kk++) { + v = (pBuf[kk] & MT_Upper_Mask) | (pBuf[kk + 1] & MT_Lower_Mask); + pBuf[kk] = pBuf[kk + (MT_M - MT_N)] ^ (v >> 1) ^ mag[v & 1]; + } + v = (pBuf[MT_N - 1] & MT_Upper_Mask) | (pBuf[0] & MT_Lower_Mask); + pBuf[MT_N - 1] = pBuf[MT_M - 1] ^ (v >> 1) ^ mag[v & 1]; + mti = 0; + } + v = pBuf[mti++]; + v ^= (v >> 11); + v ^= (v << 7) & 0x9d2c5680UL; + v ^= (v << 15) & 0xefc60000UL; + v ^= (v >> 18); + return v; +} + +void FX_Random_MT_Close(void* pContext) { + ASSERT(pContext); + FX_Free(pContext); +} + +void FX_Random_GenerateMT(uint32_t* pBuffer, int32_t iCount) { + uint32_t dwSeed; +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ + if (!GenerateCryptoRandom(&dwSeed, 1)) + FX_Random_GenerateBase(&dwSeed, 1); +#else + FX_Random_GenerateBase(&dwSeed, 1); +#endif + void* pContext = FX_Random_MT_Start(dwSeed); + while (iCount-- > 0) + *pBuffer++ = FX_Random_MT_Generate(pContext); + + FX_Random_MT_Close(pContext); +} + +void FX_Random_GenerateBase(uint32_t* pBuffer, int32_t iCount) { +#if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ + SYSTEMTIME st1, st2; + ::GetSystemTime(&st1); + do { + ::GetSystemTime(&st2); + } while (memcmp(&st1, &st2, sizeof(SYSTEMTIME)) == 0); + uint32_t dwHash1 = + FX_HashCode_GetA(CFX_ByteStringC((uint8_t*)&st1, sizeof(st1)), true); + uint32_t dwHash2 = + FX_HashCode_GetA(CFX_ByteStringC((uint8_t*)&st2, sizeof(st2)), true); + ::srand((dwHash1 << 16) | (uint32_t)dwHash2); +#else + time_t tmLast = time(nullptr); + time_t tmCur; + while ((tmCur = time(nullptr)) == tmLast) + continue; + + ::srand((tmCur << 16) | (tmLast & 0xFFFF)); +#endif + while (iCount-- > 0) + *pBuffer++ = static_cast((::rand() << 16) | (::rand() & 0xFFFF)); +} diff --git a/core/fxcrt/fx_random.h b/core/fxcrt/fx_random.h new file mode 100644 index 0000000000..c07ef797cf --- /dev/null +++ b/core/fxcrt/fx_random.h @@ -0,0 +1,18 @@ +// Copyright 2017 PDFium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +// Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com + +#ifndef CORE_FXCRT_FX_RANDOM_H_ +#define CORE_FXCRT_FX_RANDOM_H_ + +#include + +void* FX_Random_MT_Start(uint32_t dwSeed); +void FX_Random_MT_Close(void* pContext); +uint32_t FX_Random_MT_Generate(void* pContext); +void FX_Random_GenerateBase(uint32_t* pBuffer, int32_t iCount); +void FX_Random_GenerateMT(uint32_t* pBuffer, int32_t iCount); + +#endif // CORE_FXCRT_FX_RANDOM_H_ -- cgit v1.2.3