andre@0: /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ andre@0: /* This Source Code Form is subject to the terms of the Mozilla Public andre@0: * License, v. 2.0. If a copy of the MPL was not distributed with this andre@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ andre@0: andre@0: #include andre@0: #include andre@0: #include andre@0: #include andre@0: #include andre@0: #include andre@0: #include andre@0: andre@0: static BOOL andre@0: CurrentClockTickTime(LPDWORD lpdwHigh, LPDWORD lpdwLow) andre@0: { andre@0: LARGE_INTEGER liCount; andre@0: andre@0: if (!QueryPerformanceCounter(&liCount)) andre@0: return FALSE; andre@0: andre@0: *lpdwHigh = liCount.u.HighPart; andre@0: *lpdwLow = liCount.u.LowPart; andre@0: return TRUE; andre@0: } andre@0: andre@0: extern PRSize _PR_MD_GetRandomNoise( void *buf, PRSize size ) andre@0: { andre@0: DWORD dwHigh, dwLow, dwVal; andre@0: size_t n = 0; andre@0: size_t nBytes; andre@0: time_t sTime; andre@0: andre@0: if (size <= 0) andre@0: return 0; andre@0: andre@0: CurrentClockTickTime(&dwHigh, &dwLow); andre@0: andre@0: // get the maximally changing bits first andre@0: nBytes = sizeof(dwLow) > size ? size : sizeof(dwLow); andre@0: memcpy((char *)buf, &dwLow, nBytes); andre@0: n += nBytes; andre@0: size -= nBytes; andre@0: andre@0: if (size <= 0) andre@0: return n; andre@0: andre@0: nBytes = sizeof(dwHigh) > size ? size : sizeof(dwHigh); andre@0: memcpy(((char *)buf) + n, &dwHigh, nBytes); andre@0: n += nBytes; andre@0: size -= nBytes; andre@0: andre@0: if (size <= 0) andre@0: return n; andre@0: andre@0: // get the number of milliseconds that have elapsed since Windows started andre@0: dwVal = GetTickCount(); andre@0: andre@0: nBytes = sizeof(dwVal) > size ? size : sizeof(dwVal); andre@0: memcpy(((char *)buf) + n, &dwVal, nBytes); andre@0: n += nBytes; andre@0: size -= nBytes; andre@0: andre@0: if (size <= 0) andre@0: return n; andre@0: andre@0: // get the time in seconds since midnight Jan 1, 1970 andre@0: time(&sTime); andre@0: nBytes = sizeof(sTime) > size ? size : sizeof(sTime); andre@0: memcpy(((char *)buf) + n, &sTime, nBytes); andre@0: n += nBytes; andre@0: andre@0: return n; andre@0: } andre@0: