You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RandomStream.cxx 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* 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. #include <rdr/RandomStream.h>
  19. #include <rdr/Exception.h>
  20. #include <time.h>
  21. #include <stdlib.h>
  22. #ifndef WIN32
  23. #include <unistd.h>
  24. #include <errno.h>
  25. #else
  26. #define getpid() GetCurrentProcessId()
  27. #endif
  28. using namespace rdr;
  29. const int DEFAULT_BUF_LEN = 256;
  30. unsigned int RandomStream::seed;
  31. RandomStream::RandomStream()
  32. : offset(0)
  33. {
  34. ptr = end = start = new U8[DEFAULT_BUF_LEN];
  35. #ifdef WIN32
  36. provider = 0;
  37. if (!CryptAcquireContext(&provider, 0, 0, PROV_RSA_FULL, 0)) {
  38. if (GetLastError() == NTE_BAD_KEYSET) {
  39. if (!CryptAcquireContext(&provider, 0, 0, PROV_RSA_FULL, CRYPT_NEWKEYSET)) {
  40. fprintf(stderr, "RandomStream: unable to create keyset\n");
  41. provider = 0;
  42. }
  43. } else {
  44. fprintf(stderr, "RandomStream: unable to acquire context\n");
  45. provider = 0;
  46. }
  47. }
  48. if (!provider) {
  49. #else
  50. fp = fopen("/dev/urandom", "r");
  51. if (!fp)
  52. fp = fopen("/dev/random", "r");
  53. if (!fp) {
  54. #endif
  55. fprintf(stderr,"RandomStream: warning: no OS supplied random source - using rand()\n");
  56. seed += (unsigned int) time(0) + getpid() + getpid() * 987654 + rand();
  57. srand(seed);
  58. }
  59. }
  60. RandomStream::~RandomStream() {
  61. delete [] start;
  62. #ifdef WIN32
  63. if (provider) {
  64. CryptReleaseContext(provider, 0);
  65. }
  66. #else
  67. if (fp) fclose(fp);
  68. #endif
  69. }
  70. int RandomStream::pos() {
  71. return offset + ptr - start;
  72. }
  73. int RandomStream::overrun(int itemSize, int nItems, bool wait) {
  74. if (itemSize > DEFAULT_BUF_LEN)
  75. throw Exception("RandomStream overrun: max itemSize exceeded");
  76. if (end - ptr != 0)
  77. memmove(start, ptr, end - ptr);
  78. end -= ptr - start;
  79. offset += ptr - start;
  80. ptr = start;
  81. int length = start + DEFAULT_BUF_LEN - end;
  82. #ifdef WIN32
  83. if (provider) {
  84. if (!CryptGenRandom(provider, length, (U8*)end))
  85. throw rdr::SystemException("unable to CryptGenRandom", GetLastError());
  86. end += length;
  87. #else
  88. if (fp) {
  89. int n = fread((U8*)end, length, 1, fp);
  90. if (n != 1)
  91. throw rdr::SystemException("reading /dev/urandom or /dev/random failed",
  92. errno);
  93. end += length;
  94. #endif
  95. } else {
  96. for (int i=0; i<length; i++)
  97. *(U8*)end++ = (int) (256.0*rand()/(RAND_MAX+1.0));
  98. }
  99. if (itemSize * nItems > end - ptr)
  100. nItems = (end - ptr) / itemSize;
  101. return nItems;
  102. }