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.2KB

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