X-Git-Url: http://wien.tomnetworks.com/gitweb/?a=blobdiff_plain;f=mono%2Fmetadata%2Frand.c;h=6edc3f2e33579b74e3037dc13c2cf3ae760b7390;hb=aa3de31b70d1c5826d67147696423cdd9c334d41;hp=28e4b8716022d292b3251a7b774e2b5ccbdf43a8;hpb=93703b4ef8bdcf1d6cf336e14f534454221730c5;p=mono.git diff --git a/mono/metadata/rand.c b/mono/metadata/rand.c index 28e4b871602..6edc3f2e335 100644 --- a/mono/metadata/rand.c +++ b/mono/metadata/rand.c @@ -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 @@ -15,13 +15,18 @@ #include #include #include +#ifdef HAVE_UNISTD_H #include +#endif +#ifdef HAVE_STRING_H +#include +#endif #include #include #include -#if !defined(PLATFORM_WIN32) +#if !defined(__native_client__) && !defined(HOST_WIN32) && defined (HAVE_SYS_UN_H) #include #include #include @@ -39,8 +44,8 @@ get_entropy_from_server (const char *path, guchar *buf, int len) ret = -1; else { egd_addr.sun_family = AF_UNIX; - strncpy (egd_addr.sun_path, path, MONO_SIZEOF_SUNPATH - 1); - egd_addr.sun_path [MONO_SIZEOF_SUNPATH-1] = '\0'; + strncpy (egd_addr.sun_path, path, sizeof(egd_addr.sun_path) - 1); + egd_addr.sun_path [sizeof(egd_addr.sun_path)-1] = '\0'; ret = connect (file, (struct sockaddr *)&egd_addr, sizeof(egd_addr)); } if (ret == -1) { @@ -92,7 +97,7 @@ get_entropy_from_server (const char *path, guchar *buf, int len) } #endif -#if defined (PLATFORM_WIN32) +#if defined (HOST_WIN32) #include #include @@ -175,6 +180,58 @@ ves_icall_System_Security_Cryptography_RNGCryptoServiceProvider_RngClose (gpoint CryptReleaseContext ((HCRYPTPROV) handle, 0); } +#elif defined (__native_client__) || !defined (HAVE_SYS_UN_H) + +#include + +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