NaCl runtime fixes
[mono.git] / mono / metadata / rand.c
index 935325ed198af50cf485a5e6d1ad57cdc56fac4c..4ed1203f85df3390b9d75bd5c597ec955c0c6dea 100644 (file)
 #include <mono/metadata/rand.h>
 #include <mono/metadata/exception.h>
 
-#if defined(__native_client__)
-#include <errno.h>
-
-static void
-get_entropy_from_server (const char *path, guchar *buf, int len)
-{
-    return;
-}
-
-#else /* defined(__native_client__) */
-
-#if !defined(HOST_WIN32)
+#if !defined(__native_client__) && !defined(HOST_WIN32)
 #include <sys/socket.h>
 #include <sys/un.h>
 #include <errno.h>
@@ -107,7 +96,6 @@ get_entropy_from_server (const char *path, guchar *buf, int len)
     close (file);
 }
 #endif
-#endif /* __native_client__ */
 
 #if defined (HOST_WIN32)
 
@@ -192,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