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

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

mercurial