nsprpub/pr/src/md/os2/os2rng.c

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6
michael@0 7 #define INCL_DOS
michael@0 8 #define INCL_DOSERRORS
michael@0 9 #include <os2.h>
michael@0 10 #include <stdlib.h>
michael@0 11 #include <time.h>
michael@0 12 #include "primpl.h"
michael@0 13
michael@0 14 static BOOL clockTickTime(unsigned long *phigh, unsigned long *plow)
michael@0 15 {
michael@0 16 APIRET rc = NO_ERROR;
michael@0 17 QWORD qword = {0,0};
michael@0 18
michael@0 19 rc = DosTmrQueryTime(&qword);
michael@0 20 if (rc != NO_ERROR)
michael@0 21 return FALSE;
michael@0 22
michael@0 23 *phigh = qword.ulHi;
michael@0 24 *plow = qword.ulLo;
michael@0 25
michael@0 26 return TRUE;
michael@0 27 }
michael@0 28
michael@0 29 extern PRSize _PR_MD_GetRandomNoise(void *buf, PRSize size )
michael@0 30 {
michael@0 31 unsigned long high = 0;
michael@0 32 unsigned long low = 0;
michael@0 33 clock_t val = 0;
michael@0 34 int n = 0;
michael@0 35 int nBytes = 0;
michael@0 36 time_t sTime;
michael@0 37
michael@0 38 if (size <= 0)
michael@0 39 return 0;
michael@0 40
michael@0 41 clockTickTime(&high, &low);
michael@0 42
michael@0 43 /* get the maximally changing bits first */
michael@0 44 nBytes = sizeof(low) > size ? size : sizeof(low);
michael@0 45 memcpy(buf, &low, nBytes);
michael@0 46 n += nBytes;
michael@0 47 size -= nBytes;
michael@0 48
michael@0 49 if (size <= 0)
michael@0 50 return n;
michael@0 51
michael@0 52 nBytes = sizeof(high) > size ? size : sizeof(high);
michael@0 53 memcpy(((char *)buf) + n, &high, nBytes);
michael@0 54 n += nBytes;
michael@0 55 size -= nBytes;
michael@0 56
michael@0 57 if (size <= 0)
michael@0 58 return n;
michael@0 59
michael@0 60 /* get the number of milliseconds that have elapsed since application started */
michael@0 61 val = clock();
michael@0 62
michael@0 63 nBytes = sizeof(val) > size ? size : sizeof(val);
michael@0 64 memcpy(((char *)buf) + n, &val, nBytes);
michael@0 65 n += nBytes;
michael@0 66 size -= nBytes;
michael@0 67
michael@0 68 if (size <= 0)
michael@0 69 return n;
michael@0 70
michael@0 71 /* get the time in seconds since midnight Jan 1, 1970 */
michael@0 72 time(&sTime);
michael@0 73 nBytes = sizeof(sTime) > size ? size : sizeof(sTime);
michael@0 74 memcpy(((char *)buf) + n, &sTime, nBytes);
michael@0 75 n += nBytes;
michael@0 76
michael@0 77 return n;
michael@0 78 }

mercurial