blob: 7f62e091f988e45bd554f8ea74b749a8648fd93c [file] [log] [blame]
Constantin Kaplinsky47ed8d32004-10-08 09:43:57 +00001/* Copyright (C) 2002-2003 RealVNC Ltd. All Rights Reserved.
2 *
3 * This is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This software is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this software; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
16 * USA.
17 */
18
19#include <rdr/RandomStream.h>
20#include <rdr/Exception.h>
21#include <time.h>
22#include <stdlib.h>
23#ifndef WIN32
24#include <unistd.h>
25#include <errno.h>
26#else
27#define getpid() GetCurrentProcessId()
28#endif
29
30using namespace rdr;
31
32const int DEFAULT_BUF_LEN = 256;
33
34unsigned int RandomStream::seed;
35
36RandomStream::RandomStream()
37 : offset(0)
38{
39 ptr = end = start = new U8[DEFAULT_BUF_LEN];
40
41#ifdef WIN32
42 provider = 0;
43 if (!CryptAcquireContext(&provider, 0, 0, PROV_RSA_FULL, 0)) {
44 if (GetLastError() == NTE_BAD_KEYSET) {
45 if (!CryptAcquireContext(&provider, 0, 0, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
46 fprintf(stderr, "RandomStream: unable to create keyset\n");
47 provider = 0;
48 }
49 } else {
50 fprintf(stderr, "RandomStream: unable to acquire context\n");
51 provider = 0;
52 }
53 }
54 if (!provider) {
55#else
56 fp = fopen("/dev/urandom", "r");
57 if (!fp)
58 fp = fopen("/dev/random", "r");
59 if (!fp) {
60#endif
61 fprintf(stderr,"RandomStream: warning: no OS supplied random source - using rand()\n");
62 seed += (unsigned int) time(0) + getpid() + getpid() * 987654 + rand();
63 srand(seed);
64 }
65}
66
67RandomStream::~RandomStream() {
68 delete [] start;
69
70#ifdef WIN32
71 if (provider) {
72 CryptReleaseContext(provider, 0);
73 }
74#else
75 if (fp) fclose(fp);
76#endif
77}
78
79int RandomStream::pos() {
80 return offset + ptr - start;
81}
82
83int RandomStream::overrun(int itemSize, int nItems, bool wait) {
84 if (itemSize > DEFAULT_BUF_LEN)
85 throw Exception("RandomStream overrun: max itemSize exceeded");
86
87 if (end - ptr != 0)
88 memmove(start, ptr, end - ptr);
89
90 end -= ptr - start;
91 offset += ptr - start;
92 ptr = start;
93
94 int length = start + DEFAULT_BUF_LEN - end;
95
96#ifdef WIN32
97 if (provider) {
98 if (!CryptGenRandom(provider, length, (U8*)end))
99 throw rdr::SystemException("unable to CryptGenRandom", GetLastError());
100 end += length;
101#else
102 if (fp) {
103 int n = fread((U8*)end, length, 1, fp);
104 if (n != 1)
105 throw rdr::SystemException("reading /dev/urandom or /dev/random failed",
106 errno);
107 end += length;
108#endif
109 } else {
110 for (int i=0; i<length; i++)
111 *(U8*)end++ = (int) (256.0*rand()/(RAND_MAX+1.0));
112 }
113
114 if (itemSize * nItems > end - ptr)
115 nItems = (end - ptr) / itemSize;
116
117 return nItems;
118}