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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/nsprpub/pr/src/md/os2/os2rng.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,78 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +
    1.10 +#define INCL_DOS
    1.11 +#define INCL_DOSERRORS
    1.12 +#include <os2.h>
    1.13 +#include <stdlib.h>
    1.14 +#include <time.h>
    1.15 +#include "primpl.h"
    1.16 +
    1.17 +static BOOL clockTickTime(unsigned long *phigh, unsigned long *plow)
    1.18 +{
    1.19 +    APIRET rc = NO_ERROR;
    1.20 +    QWORD qword = {0,0};
    1.21 +
    1.22 +    rc = DosTmrQueryTime(&qword);
    1.23 +    if (rc != NO_ERROR)
    1.24 +       return FALSE;
    1.25 +
    1.26 +    *phigh = qword.ulHi;
    1.27 +    *plow  = qword.ulLo;
    1.28 +
    1.29 +    return TRUE;
    1.30 +}
    1.31 +
    1.32 +extern PRSize _PR_MD_GetRandomNoise(void *buf, PRSize size )
    1.33 +{
    1.34 +    unsigned long high = 0;
    1.35 +    unsigned long low  = 0;
    1.36 +    clock_t val = 0;
    1.37 +    int n = 0;
    1.38 +    int nBytes = 0;
    1.39 +    time_t sTime;
    1.40 +
    1.41 +    if (size <= 0)
    1.42 +       return 0;
    1.43 +
    1.44 +    clockTickTime(&high, &low);
    1.45 +
    1.46 +    /* get the maximally changing bits first */
    1.47 +    nBytes = sizeof(low) > size ? size : sizeof(low);
    1.48 +    memcpy(buf, &low, nBytes);
    1.49 +    n += nBytes;
    1.50 +    size -= nBytes;
    1.51 +
    1.52 +    if (size <= 0)
    1.53 +       return n;
    1.54 +
    1.55 +    nBytes = sizeof(high) > size ? size : sizeof(high);
    1.56 +    memcpy(((char *)buf) + n, &high, nBytes);
    1.57 +    n += nBytes;
    1.58 +    size -= nBytes;
    1.59 +
    1.60 +    if (size <= 0)
    1.61 +       return n;
    1.62 +
    1.63 +    /* get the number of milliseconds that have elapsed since application started */
    1.64 +    val = clock();
    1.65 +
    1.66 +    nBytes = sizeof(val) > size ? size : sizeof(val);
    1.67 +    memcpy(((char *)buf) + n, &val, nBytes);
    1.68 +    n += nBytes;
    1.69 +    size -= nBytes;
    1.70 +
    1.71 +    if (size <= 0)
    1.72 +       return n;
    1.73 +
    1.74 +    /* get the time in seconds since midnight Jan 1, 1970 */
    1.75 +    time(&sTime);
    1.76 +    nBytes = sizeof(sTime) > size ? size : sizeof(sTime);
    1.77 +    memcpy(((char *)buf) + n, &sTime, nBytes);
    1.78 +    n += nBytes;
    1.79 +
    1.80 +    return n;
    1.81 +}

mercurial