NaCl runtime fixes
[mono.git] / mono / metadata / rand.c
index 4dfae9a948c1bbda3e8b5bdaa44faca8a1e3fad9..4ed1203f85df3390b9d75bd5c597ec955c0c6dea 100644 (file)
@@ -6,8 +6,8 @@
  *      Patrik Torstensson (p@rxc.se)
  *     Sebastien Pouliot (sebastien@ximian.com)
  *
- * (C) 2001 Ximian, Inc.
- * Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
+ * Copyright 2001-2003 Ximian, Inc (http://www.ximian.com)
+ * Copyright 2004-2009 Novell, Inc (http://www.novell.com)
  */
 
 #include <config.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
+#ifdef HAVE_UNISTD_H
 #include <unistd.h>
+#endif
+#ifdef HAVE_STRING_H
+#include <string.h>
+#endif
 
 #include <mono/metadata/object.h>
 #include <mono/metadata/rand.h>
 #include <mono/metadata/exception.h>
 
-#if !defined(PLATFORM_WIN32)
+#if !defined(__native_client__) && !defined(HOST_WIN32)
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <errno.h>
@@ -92,10 +97,10 @@ get_entropy_from_server (const char *path, guchar *buf, int len)
 }
 #endif
 
-#if defined (PLATFORM_WIN32)
+#if defined (HOST_WIN32)
 
 #include <windows.h>
-#include <WinCrypt.h>
+#include <wincrypt.h>
 
 #ifndef PROV_INTEL_SEC
 #define PROV_INTEL_SEC         22
@@ -175,6 +180,58 @@ ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngClose (gpoint
        CryptReleaseContext ((HCRYPTPROV) handle, 0);
 }
 
+#elif defined (__native_client__)
+
+#include <time.h>
+
+MonoBoolean
+ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngOpen (void)
+{
+       srand (time (NULL));
+       return TRUE;
+}
+
+gpointer
+ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngInitialize (MonoArray *seed)
+{
+       return -1;
+}
+
+gpointer 
+ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngGetBytes (gpointer handle, MonoArray *arry)
+{      
+       guint32 len = mono_array_length (arry);
+       guchar *buf = mono_array_addr (arry, guchar, 0);
+
+       /* Read until the buffer is filled. This may block if using NAME_DEV_RANDOM. */
+       gint count = 0;
+       gint err;
+
+       do {
+               if (len - count >= sizeof (long))
+               {
+                       *(long*)buf = rand();
+                       count += sizeof (long);
+               }
+               else if (len - count >= sizeof (short))
+               {
+                       *(short*)buf = rand();
+                       count += sizeof (short);
+               }
+               else if (len - count >= sizeof (char))
+               {
+                       *buf = rand();
+                       count += sizeof (char);
+               }
+       } while (count < len);
+
+       return (gpointer)-1L;
+}
+
+void
+ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngClose (gpointer handle) 
+{
+}
 #else
 
 #ifndef NAME_DEV_URANDOM
@@ -229,16 +286,20 @@ ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngGetBytes (gpo
                        return NULL; 
                get_entropy_from_server (socket_path, mono_array_addr (arry, guchar, 0), mono_array_length (arry));
                return (gpointer) -1;
-       }
-       else {
+       } else {
                /* Read until the buffer is filled. This may block if using NAME_DEV_RANDOM. */
                gint count = 0;
                gint err;
 
                do {
                        err = read (file, buf + count, len - count);
+                       if (err < 0) {
+                               if (errno == EINTR)
+                                       continue;
+                               break;
+                       }
                        count += err;
-               } while (err >= 0 && count < len);
+               } while (count < len);
 
                if (err < 0) {
                        g_warning("Entropy error! Error in read (%s).", strerror (errno));