| OLD | NEW |
| 1 // Copyright 2014 PDFium Authors. All rights reserved. | 1 // Copyright 2014 PDFium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com | 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com |
| 6 | 6 |
| 7 #include "../../include/fxcrt/fx_basic.h" | 7 #include "../../include/fxcrt/fx_basic.h" |
| 8 #include "../../include/fxcrt/fx_ext.h" | 8 #include "../../include/fxcrt/fx_ext.h" |
| 9 #include "extension.h" | 9 #include "extension.h" |
| 10 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 10 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 11 #include <wincrypt.h> | 11 #include <wincrypt.h> |
| 12 #else | 12 #else |
| 13 #include <ctime> | 13 #include <ctime> |
| 14 #endif | 14 #endif |
| 15 | 15 |
| 16 IFX_FileStream* FX_CreateFileStream(const FX_CHAR* filename, FX_DWORD dwModes) | 16 IFX_FileStream* FX_CreateFileStream(const FX_CHAR* filename, FX_DWORD dwModes) { |
| 17 { | 17 IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create(); |
| 18 IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create(); | 18 if (!pFA) { |
| 19 if (!pFA) { | 19 return NULL; |
| 20 return NULL; | 20 } |
| 21 } | 21 if (!pFA->Open(filename, dwModes)) { |
| 22 if (!pFA->Open(filename, dwModes)) { | 22 pFA->Release(); |
| 23 pFA->Release(); | 23 return NULL; |
| 24 return NULL; | 24 } |
| 25 } | 25 return new CFX_CRTFileStream(pFA); |
| 26 return new CFX_CRTFileStream(pFA); | |
| 27 } | 26 } |
| 28 IFX_FileStream* FX_CreateFileStream(const FX_WCHAR* filename, FX_DWORD dwModes) | 27 IFX_FileStream* FX_CreateFileStream(const FX_WCHAR* filename, |
| 29 { | 28 FX_DWORD dwModes) { |
| 30 IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create(); | 29 IFXCRT_FileAccess* pFA = FXCRT_FileAccess_Create(); |
| 31 if (!pFA) { | 30 if (!pFA) { |
| 32 return NULL; | 31 return NULL; |
| 33 } | 32 } |
| 34 if (!pFA->Open(filename, dwModes)) { | 33 if (!pFA->Open(filename, dwModes)) { |
| 35 pFA->Release(); | 34 pFA->Release(); |
| 36 return NULL; | 35 return NULL; |
| 37 } | 36 } |
| 38 return new CFX_CRTFileStream(pFA); | 37 return new CFX_CRTFileStream(pFA); |
| 39 } | 38 } |
| 40 IFX_FileWrite* FX_CreateFileWrite(const FX_CHAR* filename) | 39 IFX_FileWrite* FX_CreateFileWrite(const FX_CHAR* filename) { |
| 41 { | 40 return FX_CreateFileStream(filename, FX_FILEMODE_Truncate); |
| 42 return FX_CreateFileStream(filename, FX_FILEMODE_Truncate); | |
| 43 } | 41 } |
| 44 IFX_FileWrite* FX_CreateFileWrite(const FX_WCHAR* filename) | 42 IFX_FileWrite* FX_CreateFileWrite(const FX_WCHAR* filename) { |
| 45 { | 43 return FX_CreateFileStream(filename, FX_FILEMODE_Truncate); |
| 46 return FX_CreateFileStream(filename, FX_FILEMODE_Truncate); | |
| 47 } | 44 } |
| 48 IFX_FileRead* FX_CreateFileRead(const FX_CHAR* filename) | 45 IFX_FileRead* FX_CreateFileRead(const FX_CHAR* filename) { |
| 49 { | 46 return FX_CreateFileStream(filename, FX_FILEMODE_ReadOnly); |
| 50 return FX_CreateFileStream(filename, FX_FILEMODE_ReadOnly); | |
| 51 } | 47 } |
| 52 IFX_FileRead* FX_CreateFileRead(const FX_WCHAR* filename) | 48 IFX_FileRead* FX_CreateFileRead(const FX_WCHAR* filename) { |
| 53 { | 49 return FX_CreateFileStream(filename, FX_FILEMODE_ReadOnly); |
| 54 return FX_CreateFileStream(filename, FX_FILEMODE_ReadOnly); | |
| 55 } | 50 } |
| 56 IFX_MemoryStream* FX_CreateMemoryStream(uint8_t* pBuffer, size_t dwSize, FX_BOOL
bTakeOver) | 51 IFX_MemoryStream* FX_CreateMemoryStream(uint8_t* pBuffer, |
| 57 { | 52 size_t dwSize, |
| 58 return new CFX_MemoryStream(pBuffer, dwSize, bTakeOver); | 53 FX_BOOL bTakeOver) { |
| 54 return new CFX_MemoryStream(pBuffer, dwSize, bTakeOver); |
| 59 } | 55 } |
| 60 IFX_MemoryStream* FX_CreateMemoryStream(FX_BOOL bConsecutive) | 56 IFX_MemoryStream* FX_CreateMemoryStream(FX_BOOL bConsecutive) { |
| 61 { | 57 return new CFX_MemoryStream(bConsecutive); |
| 62 return new CFX_MemoryStream(bConsecutive); | |
| 63 } | 58 } |
| 64 #ifdef __cplusplus | 59 #ifdef __cplusplus |
| 65 extern "C" { | 60 extern "C" { |
| 66 #endif | 61 #endif |
| 67 FX_FLOAT FXSYS_tan(FX_FLOAT a) | 62 FX_FLOAT FXSYS_tan(FX_FLOAT a) { |
| 68 { | 63 return (FX_FLOAT)tan(a); |
| 69 return (FX_FLOAT)tan(a); | |
| 70 } | 64 } |
| 71 FX_FLOAT FXSYS_logb(FX_FLOAT b, FX_FLOAT x) | 65 FX_FLOAT FXSYS_logb(FX_FLOAT b, FX_FLOAT x) { |
| 72 { | 66 return FXSYS_log(x) / FXSYS_log(b); |
| 73 return FXSYS_log(x) / FXSYS_log(b); | |
| 74 } | 67 } |
| 75 FX_FLOAT FXSYS_strtof(const FX_CHAR* pcsStr, int32_t iLength, int32_t *pUsedLen) | 68 FX_FLOAT FXSYS_strtof(const FX_CHAR* pcsStr, |
| 76 { | 69 int32_t iLength, |
| 77 FXSYS_assert(pcsStr != NULL); | 70 int32_t* pUsedLen) { |
| 78 if (iLength < 0) { | 71 FXSYS_assert(pcsStr != NULL); |
| 79 iLength = (int32_t)FXSYS_strlen(pcsStr); | 72 if (iLength < 0) { |
| 73 iLength = (int32_t)FXSYS_strlen(pcsStr); |
| 74 } |
| 75 CFX_WideString ws = CFX_WideString::FromLocal(pcsStr, iLength); |
| 76 return FXSYS_wcstof(ws.c_str(), iLength, pUsedLen); |
| 77 } |
| 78 FX_FLOAT FXSYS_wcstof(const FX_WCHAR* pwsStr, |
| 79 int32_t iLength, |
| 80 int32_t* pUsedLen) { |
| 81 FXSYS_assert(pwsStr != NULL); |
| 82 if (iLength < 0) { |
| 83 iLength = (int32_t)FXSYS_wcslen(pwsStr); |
| 84 } |
| 85 if (iLength == 0) { |
| 86 return 0.0f; |
| 87 } |
| 88 int32_t iUsedLen = 0; |
| 89 FX_BOOL bNegtive = FALSE; |
| 90 switch (pwsStr[iUsedLen]) { |
| 91 case '-': |
| 92 bNegtive = TRUE; |
| 93 case '+': |
| 94 iUsedLen++; |
| 95 break; |
| 96 } |
| 97 FX_FLOAT fValue = 0.0f; |
| 98 while (iUsedLen < iLength) { |
| 99 FX_WCHAR wch = pwsStr[iUsedLen]; |
| 100 if (wch >= L'0' && wch <= L'9') { |
| 101 fValue = fValue * 10.0f + (wch - L'0'); |
| 102 } else { |
| 103 break; |
| 80 } | 104 } |
| 81 CFX_WideString ws = CFX_WideString::FromLocal(pcsStr, iLength); | 105 iUsedLen++; |
| 82 return FXSYS_wcstof(ws.c_str(), iLength, pUsedLen); | 106 } |
| 107 if (iUsedLen < iLength && pwsStr[iUsedLen] == L'.') { |
| 108 FX_FLOAT fPrecise = 0.1f; |
| 109 while (++iUsedLen < iLength) { |
| 110 FX_WCHAR wch = pwsStr[iUsedLen]; |
| 111 if (wch >= L'0' && wch <= L'9') { |
| 112 fValue += (wch - L'0') * fPrecise; |
| 113 fPrecise *= 0.1f; |
| 114 } else { |
| 115 break; |
| 116 } |
| 117 } |
| 118 } |
| 119 if (pUsedLen) { |
| 120 *pUsedLen = iUsedLen; |
| 121 } |
| 122 return bNegtive ? -fValue : fValue; |
| 83 } | 123 } |
| 84 FX_FLOAT FXSYS_wcstof(const FX_WCHAR* pwsStr, int32_t iLength, int32_t *pUsedLen
) | 124 FX_WCHAR* FXSYS_wcsncpy(FX_WCHAR* dstStr, |
| 85 { | 125 const FX_WCHAR* srcStr, |
| 86 FXSYS_assert(pwsStr != NULL); | 126 size_t count) { |
| 87 if (iLength < 0) { | 127 FXSYS_assert(dstStr != NULL && srcStr != NULL && count > 0); |
| 88 iLength = (int32_t)FXSYS_wcslen(pwsStr); | 128 for (size_t i = 0; i < count; ++i) |
| 129 if ((dstStr[i] = srcStr[i]) == L'\0') { |
| 130 break; |
| 89 } | 131 } |
| 90 if (iLength == 0) { | 132 return dstStr; |
| 91 return 0.0f; | 133 } |
| 134 int32_t FXSYS_wcsnicmp(const FX_WCHAR* s1, const FX_WCHAR* s2, size_t count) { |
| 135 FXSYS_assert(s1 != NULL && s2 != NULL && count > 0); |
| 136 FX_WCHAR wch1 = 0, wch2 = 0; |
| 137 while (count-- > 0) { |
| 138 wch1 = (FX_WCHAR)FXSYS_tolower(*s1++); |
| 139 wch2 = (FX_WCHAR)FXSYS_tolower(*s2++); |
| 140 if (wch1 != wch2) { |
| 141 break; |
| 92 } | 142 } |
| 93 int32_t iUsedLen = 0; | 143 } |
| 94 FX_BOOL bNegtive = FALSE; | 144 return wch1 - wch2; |
| 95 switch (pwsStr[iUsedLen]) { | 145 } |
| 96 case '-': | 146 int32_t FXSYS_strnicmp(const FX_CHAR* s1, const FX_CHAR* s2, size_t count) { |
| 97 bNegtive = TRUE; | 147 FXSYS_assert(s1 != NULL && s2 != NULL && count > 0); |
| 98 case '+': | 148 FX_CHAR ch1 = 0, ch2 = 0; |
| 99 iUsedLen++; | 149 while (count-- > 0) { |
| 100 break; | 150 ch1 = (FX_CHAR)FXSYS_tolower(*s1++); |
| 151 ch2 = (FX_CHAR)FXSYS_tolower(*s2++); |
| 152 if (ch1 != ch2) { |
| 153 break; |
| 101 } | 154 } |
| 102 FX_FLOAT fValue = 0.0f; | 155 } |
| 103 while (iUsedLen < iLength) { | 156 return ch1 - ch2; |
| 104 FX_WCHAR wch = pwsStr[iUsedLen]; | 157 } |
| 105 if (wch >= L'0' && wch <= L'9') { | 158 FX_DWORD FX_HashCode_String_GetA(const FX_CHAR* pStr, |
| 106 fValue = fValue * 10.0f + (wch - L'0'); | 159 int32_t iLength, |
| 107 } else { | 160 FX_BOOL bIgnoreCase) { |
| 108 break; | 161 FXSYS_assert(pStr != NULL); |
| 109 } | 162 if (iLength < 0) { |
| 110 iUsedLen++; | 163 iLength = (int32_t)FXSYS_strlen(pStr); |
| 164 } |
| 165 const FX_CHAR* pStrEnd = pStr + iLength; |
| 166 FX_DWORD dwHashCode = 0; |
| 167 if (bIgnoreCase) { |
| 168 while (pStr < pStrEnd) { |
| 169 dwHashCode = 31 * dwHashCode + FXSYS_tolower(*pStr++); |
| 111 } | 170 } |
| 112 if (iUsedLen < iLength && pwsStr[iUsedLen] == L'.') { | 171 } else { |
| 113 FX_FLOAT fPrecise = 0.1f; | 172 while (pStr < pStrEnd) { |
| 114 while (++iUsedLen < iLength) { | 173 dwHashCode = 31 * dwHashCode + *pStr++; |
| 115 FX_WCHAR wch = pwsStr[iUsedLen]; | |
| 116 if (wch >= L'0' && wch <= L'9') { | |
| 117 fValue += (wch - L'0') * fPrecise; | |
| 118 fPrecise *= 0.1f; | |
| 119 } else { | |
| 120 break; | |
| 121 } | |
| 122 } | |
| 123 } | 174 } |
| 124 if (pUsedLen) { | 175 } |
| 125 *pUsedLen = iUsedLen; | 176 return dwHashCode; |
| 177 } |
| 178 FX_DWORD FX_HashCode_String_GetW(const FX_WCHAR* pStr, |
| 179 int32_t iLength, |
| 180 FX_BOOL bIgnoreCase) { |
| 181 FXSYS_assert(pStr != NULL); |
| 182 if (iLength < 0) { |
| 183 iLength = (int32_t)FXSYS_wcslen(pStr); |
| 184 } |
| 185 const FX_WCHAR* pStrEnd = pStr + iLength; |
| 186 FX_DWORD dwHashCode = 0; |
| 187 if (bIgnoreCase) { |
| 188 while (pStr < pStrEnd) { |
| 189 dwHashCode = 1313 * dwHashCode + FXSYS_tolower(*pStr++); |
| 126 } | 190 } |
| 127 return bNegtive ? -fValue : fValue; | 191 } else { |
| 128 } | 192 while (pStr < pStrEnd) { |
| 129 FX_WCHAR* FXSYS_wcsncpy(FX_WCHAR* dstStr, const FX_WCHAR* srcStr, size_t count) | 193 dwHashCode = 1313 * dwHashCode + *pStr++; |
| 130 { | |
| 131 FXSYS_assert(dstStr != NULL && srcStr != NULL && count > 0); | |
| 132 for (size_t i = 0; i < count; ++i) | |
| 133 if ((dstStr[i] = srcStr[i]) == L'\0') { | |
| 134 break; | |
| 135 } | |
| 136 return dstStr; | |
| 137 } | |
| 138 int32_t FXSYS_wcsnicmp(const FX_WCHAR* s1, const FX_WCHAR* s2, size_t count) | |
| 139 { | |
| 140 FXSYS_assert(s1 != NULL && s2 != NULL && count > 0); | |
| 141 FX_WCHAR wch1 = 0, wch2 = 0; | |
| 142 while (count-- > 0) { | |
| 143 wch1 = (FX_WCHAR)FXSYS_tolower(*s1++); | |
| 144 wch2 = (FX_WCHAR)FXSYS_tolower(*s2++); | |
| 145 if (wch1 != wch2) { | |
| 146 break; | |
| 147 } | |
| 148 } | 194 } |
| 149 return wch1 - wch2; | 195 } |
| 150 } | 196 return dwHashCode; |
| 151 int32_t FXSYS_strnicmp(const FX_CHAR* s1, const FX_CHAR* s2, size_t count) | |
| 152 { | |
| 153 FXSYS_assert(s1 != NULL && s2 != NULL && count > 0); | |
| 154 FX_CHAR ch1 = 0, ch2 = 0; | |
| 155 while (count-- > 0) { | |
| 156 ch1 = (FX_CHAR)FXSYS_tolower(*s1++); | |
| 157 ch2 = (FX_CHAR)FXSYS_tolower(*s2++); | |
| 158 if (ch1 != ch2) { | |
| 159 break; | |
| 160 } | |
| 161 } | |
| 162 return ch1 - ch2; | |
| 163 } | |
| 164 FX_DWORD FX_HashCode_String_GetA(const FX_CHAR* pStr, int32_t iLength, FX_BOOL b
IgnoreCase) | |
| 165 { | |
| 166 FXSYS_assert(pStr != NULL); | |
| 167 if (iLength < 0) { | |
| 168 iLength = (int32_t)FXSYS_strlen(pStr); | |
| 169 } | |
| 170 const FX_CHAR* pStrEnd = pStr + iLength; | |
| 171 FX_DWORD dwHashCode = 0; | |
| 172 if (bIgnoreCase) { | |
| 173 while (pStr < pStrEnd) { | |
| 174 dwHashCode = 31 * dwHashCode + FXSYS_tolower(*pStr++); | |
| 175 } | |
| 176 } else { | |
| 177 while (pStr < pStrEnd) { | |
| 178 dwHashCode = 31 * dwHashCode + *pStr ++; | |
| 179 } | |
| 180 } | |
| 181 return dwHashCode; | |
| 182 } | |
| 183 FX_DWORD FX_HashCode_String_GetW(const FX_WCHAR* pStr, int32_t iLength, FX_BOOL
bIgnoreCase) | |
| 184 { | |
| 185 FXSYS_assert(pStr != NULL); | |
| 186 if (iLength < 0) { | |
| 187 iLength = (int32_t)FXSYS_wcslen(pStr); | |
| 188 } | |
| 189 const FX_WCHAR* pStrEnd = pStr + iLength; | |
| 190 FX_DWORD dwHashCode = 0; | |
| 191 if (bIgnoreCase) { | |
| 192 while (pStr < pStrEnd) { | |
| 193 dwHashCode = 1313 * dwHashCode + FXSYS_tolower(*pStr++); | |
| 194 } | |
| 195 } else { | |
| 196 while (pStr < pStrEnd) { | |
| 197 dwHashCode = 1313 * dwHashCode + *pStr ++; | |
| 198 } | |
| 199 } | |
| 200 return dwHashCode; | |
| 201 } | 197 } |
| 202 #ifdef __cplusplus | 198 #ifdef __cplusplus |
| 203 } | 199 } |
| 204 #endif | 200 #endif |
| 205 #ifdef __cplusplus | 201 #ifdef __cplusplus |
| 206 extern "C" { | 202 extern "C" { |
| 207 #endif | 203 #endif |
| 208 void* FX_Random_MT_Start(FX_DWORD dwSeed) | 204 void* FX_Random_MT_Start(FX_DWORD dwSeed) { |
| 209 { | 205 FX_LPMTRANDOMCONTEXT pContext = FX_Alloc(FX_MTRANDOMCONTEXT, 1); |
| 210 FX_LPMTRANDOMCONTEXT pContext = FX_Alloc(FX_MTRANDOMCONTEXT, 1); | 206 pContext->mt[0] = dwSeed; |
| 211 pContext->mt[0] = dwSeed; | 207 FX_DWORD& i = pContext->mti; |
| 212 FX_DWORD &i = pContext->mti; | 208 FX_DWORD* pBuf = pContext->mt; |
| 213 FX_DWORD* pBuf = pContext->mt; | 209 for (i = 1; i < MT_N; i++) { |
| 214 for (i = 1; i < MT_N; i ++) { | 210 pBuf[i] = (1812433253UL * (pBuf[i - 1] ^ (pBuf[i - 1] >> 30)) + i); |
| 215 pBuf[i] = (1812433253UL * (pBuf[i - 1] ^ (pBuf[i - 1] >> 30)) + i); | 211 } |
| 212 pContext->bHaveSeed = TRUE; |
| 213 return pContext; |
| 214 } |
| 215 FX_DWORD FX_Random_MT_Generate(void* pContext) { |
| 216 FXSYS_assert(pContext != NULL); |
| 217 FX_LPMTRANDOMCONTEXT pMTC = (FX_LPMTRANDOMCONTEXT)pContext; |
| 218 FX_DWORD v; |
| 219 static FX_DWORD mag[2] = {0, MT_Matrix_A}; |
| 220 FX_DWORD& mti = pMTC->mti; |
| 221 FX_DWORD* pBuf = pMTC->mt; |
| 222 if ((int)mti < 0 || mti >= MT_N) { |
| 223 if (mti > MT_N && !pMTC->bHaveSeed) { |
| 224 return 0; |
| 216 } | 225 } |
| 217 pContext->bHaveSeed = TRUE; | 226 FX_DWORD kk; |
| 218 return pContext; | 227 for (kk = 0; kk < MT_N - MT_M; kk++) { |
| 228 v = (pBuf[kk] & MT_Upper_Mask) | (pBuf[kk + 1] & MT_Lower_Mask); |
| 229 pBuf[kk] = pBuf[kk + MT_M] ^ (v >> 1) ^ mag[v & 1]; |
| 230 } |
| 231 for (; kk < MT_N - 1; kk++) { |
| 232 v = (pBuf[kk] & MT_Upper_Mask) | (pBuf[kk + 1] & MT_Lower_Mask); |
| 233 pBuf[kk] = pBuf[kk + (MT_M - MT_N)] ^ (v >> 1) ^ mag[v & 1]; |
| 234 } |
| 235 v = (pBuf[MT_N - 1] & MT_Upper_Mask) | (pBuf[0] & MT_Lower_Mask); |
| 236 pBuf[MT_N - 1] = pBuf[MT_M - 1] ^ (v >> 1) ^ mag[v & 1]; |
| 237 mti = 0; |
| 238 } |
| 239 v = pBuf[mti++]; |
| 240 v ^= (v >> 11); |
| 241 v ^= (v << 7) & 0x9d2c5680UL; |
| 242 v ^= (v << 15) & 0xefc60000UL; |
| 243 v ^= (v >> 18); |
| 244 return v; |
| 219 } | 245 } |
| 220 FX_DWORD FX_Random_MT_Generate(void* pContext) | 246 void FX_Random_MT_Close(void* pContext) { |
| 221 { | 247 FXSYS_assert(pContext != NULL); |
| 222 FXSYS_assert(pContext != NULL); | 248 FX_Free(pContext); |
| 223 FX_LPMTRANDOMCONTEXT pMTC = (FX_LPMTRANDOMCONTEXT)pContext; | |
| 224 FX_DWORD v; | |
| 225 static FX_DWORD mag[2] = {0, MT_Matrix_A}; | |
| 226 FX_DWORD &mti = pMTC->mti; | |
| 227 FX_DWORD* pBuf = pMTC->mt; | |
| 228 if ((int)mti < 0 || mti >= MT_N) { | |
| 229 if (mti > MT_N && !pMTC->bHaveSeed) { | |
| 230 return 0; | |
| 231 } | |
| 232 FX_DWORD kk; | |
| 233 for (kk = 0; kk < MT_N - MT_M; kk ++) { | |
| 234 v = (pBuf[kk] & MT_Upper_Mask) | (pBuf[kk + 1] & MT_Lower_Mask); | |
| 235 pBuf[kk] = pBuf[kk + MT_M] ^ (v >> 1) ^ mag[v & 1]; | |
| 236 } | |
| 237 for (; kk < MT_N - 1; kk ++) { | |
| 238 v = (pBuf[kk] & MT_Upper_Mask) | (pBuf[kk + 1] & MT_Lower_Mask); | |
| 239 pBuf[kk] = pBuf[kk + (MT_M - MT_N)] ^ (v >> 1) ^ mag[v & 1]; | |
| 240 } | |
| 241 v = (pBuf[MT_N - 1] & MT_Upper_Mask) | (pBuf[0] & MT_Lower_Mask); | |
| 242 pBuf[MT_N - 1] = pBuf[MT_M - 1] ^ (v >> 1) ^ mag[v & 1]; | |
| 243 mti = 0; | |
| 244 } | |
| 245 v = pBuf[mti ++]; | |
| 246 v ^= (v >> 11); | |
| 247 v ^= (v << 7) & 0x9d2c5680UL; | |
| 248 v ^= (v << 15) & 0xefc60000UL; | |
| 249 v ^= (v >> 18); | |
| 250 return v; | |
| 251 } | 249 } |
| 252 void FX_Random_MT_Close(void* pContext) | 250 void FX_Random_GenerateMT(FX_DWORD* pBuffer, int32_t iCount) { |
| 253 { | 251 FX_DWORD dwSeed; |
| 254 FXSYS_assert(pContext != NULL); | 252 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 255 FX_Free(pContext); | 253 if (!FX_GenerateCryptoRandom(&dwSeed, 1)) { |
| 254 FX_Random_GenerateBase(&dwSeed, 1); |
| 255 } |
| 256 #else |
| 257 FX_Random_GenerateBase(&dwSeed, 1); |
| 258 #endif |
| 259 void* pContext = FX_Random_MT_Start(dwSeed); |
| 260 while (iCount-- > 0) { |
| 261 *pBuffer++ = FX_Random_MT_Generate(pContext); |
| 262 } |
| 263 FX_Random_MT_Close(pContext); |
| 256 } | 264 } |
| 257 void FX_Random_GenerateMT(FX_DWORD* pBuffer, int32_t iCount) | 265 void FX_Random_GenerateBase(FX_DWORD* pBuffer, int32_t iCount) { |
| 258 { | |
| 259 FX_DWORD dwSeed; | |
| 260 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 266 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 261 if (!FX_GenerateCryptoRandom(&dwSeed, 1)) { | 267 SYSTEMTIME st1, st2; |
| 262 FX_Random_GenerateBase(&dwSeed, 1); | 268 ::GetSystemTime(&st1); |
| 263 } | 269 do { |
| 270 ::GetSystemTime(&st2); |
| 271 } while (FXSYS_memcmp(&st1, &st2, sizeof(SYSTEMTIME)) == 0); |
| 272 FX_DWORD dwHash1 = |
| 273 FX_HashCode_String_GetA((const FX_CHAR*)&st1, sizeof(st1), TRUE); |
| 274 FX_DWORD dwHash2 = |
| 275 FX_HashCode_String_GetA((const FX_CHAR*)&st2, sizeof(st2), TRUE); |
| 276 ::srand((dwHash1 << 16) | (FX_DWORD)dwHash2); |
| 264 #else | 277 #else |
| 265 FX_Random_GenerateBase(&dwSeed, 1); | 278 time_t tmLast = time(NULL), tmCur; |
| 279 while ((tmCur = time(NULL)) == tmLast) |
| 280 ; |
| 281 ::srand((tmCur << 16) | (tmLast & 0xFFFF)); |
| 266 #endif | 282 #endif |
| 267 void* pContext = FX_Random_MT_Start(dwSeed); | 283 while (iCount-- > 0) { |
| 268 while (iCount -- > 0) { | 284 *pBuffer++ = (FX_DWORD)((::rand() << 16) | (::rand() & 0xFFFF)); |
| 269 *pBuffer ++ = FX_Random_MT_Generate(pContext); | 285 } |
| 270 } | |
| 271 FX_Random_MT_Close(pContext); | |
| 272 } | |
| 273 void FX_Random_GenerateBase(FX_DWORD* pBuffer, int32_t iCount) | |
| 274 { | |
| 275 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | |
| 276 SYSTEMTIME st1, st2; | |
| 277 ::GetSystemTime(&st1); | |
| 278 do { | |
| 279 ::GetSystemTime(&st2); | |
| 280 } while (FXSYS_memcmp(&st1, &st2, sizeof(SYSTEMTIME)) == 0); | |
| 281 FX_DWORD dwHash1 = FX_HashCode_String_GetA((const FX_CHAR*)&st1, sizeof(st1)
, TRUE); | |
| 282 FX_DWORD dwHash2 = FX_HashCode_String_GetA((const FX_CHAR*)&st2, sizeof(st2)
, TRUE); | |
| 283 ::srand((dwHash1 << 16) | (FX_DWORD)dwHash2); | |
| 284 #else | |
| 285 time_t tmLast = time(NULL), tmCur; | |
| 286 while ((tmCur = time(NULL)) == tmLast); | |
| 287 ::srand((tmCur << 16) | (tmLast & 0xFFFF)); | |
| 288 #endif | |
| 289 while (iCount -- > 0) { | |
| 290 *pBuffer ++ = (FX_DWORD)((::rand() << 16) | (::rand() & 0xFFFF)); | |
| 291 } | |
| 292 } | 286 } |
| 293 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 287 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 294 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount) | 288 FX_BOOL FX_GenerateCryptoRandom(FX_DWORD* pBuffer, int32_t iCount) { |
| 295 { | 289 HCRYPTPROV hCP = NULL; |
| 296 HCRYPTPROV hCP = NULL; | 290 if (!::CryptAcquireContext(&hCP, NULL, NULL, PROV_RSA_FULL, 0) || |
| 297 if (!::CryptAcquireContext(&hCP, NULL, NULL, PROV_RSA_FULL, 0) || hCP == NUL
L) { | 291 hCP == NULL) { |
| 298 return FALSE; | 292 return FALSE; |
| 299 } | 293 } |
| 300 ::CryptGenRandom(hCP, iCount * sizeof(FX_DWORD), (uint8_t*)pBuffer); | 294 ::CryptGenRandom(hCP, iCount * sizeof(FX_DWORD), (uint8_t*)pBuffer); |
| 301 ::CryptReleaseContext(hCP, 0); | 295 ::CryptReleaseContext(hCP, 0); |
| 302 return TRUE; | 296 return TRUE; |
| 303 } | 297 } |
| 304 #endif | 298 #endif |
| 305 void FX_Random_GenerateCrypto(FX_DWORD* pBuffer, int32_t iCount) | 299 void FX_Random_GenerateCrypto(FX_DWORD* pBuffer, int32_t iCount) { |
| 306 { | |
| 307 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ | 300 #if _FXM_PLATFORM_ == _FXM_PLATFORM_WINDOWS_ |
| 308 FX_GenerateCryptoRandom(pBuffer, iCount); | 301 FX_GenerateCryptoRandom(pBuffer, iCount); |
| 309 #else | 302 #else |
| 310 FX_Random_GenerateBase(pBuffer, iCount); | 303 FX_Random_GenerateBase(pBuffer, iCount); |
| 311 #endif | 304 #endif |
| 312 } | 305 } |
| 313 #ifdef __cplusplus | 306 #ifdef __cplusplus |
| 314 } | 307 } |
| 315 #endif | 308 #endif |
| OLD | NEW |