line ending normalization

This commit is contained in:
Mark Sturgill 2014-02-18 10:23:15 -07:00
parent 57a73805bb
commit 0bd09dbeac

View file

@ -1,466 +1,466 @@
/*************************************************************************** /***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify * This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by * it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or * the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version. * (at your option) any later version.
* *
***************************************************************************/ ***************************************************************************/
using System; using System;
using System.IO; using System.IO;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Security.Cryptography; using System.Security.Cryptography;
using System.Threading; using System.Threading;
namespace Server { namespace Server {
/// <summary> /// <summary>
/// Handles random number generation. /// Handles random number generation.
/// </summary> /// </summary>
public static class RandomImpl { public static class RandomImpl {
private static readonly IRandomImpl _Random; private static readonly IRandomImpl _Random;
static RandomImpl() { static RandomImpl() {
if ( Core.Unix ) { if ( Core.Unix ) {
_Random = new CSPRandom(); _Random = new CSPRandom();
} else if (Core.Is64Bit && File.Exists("rdrand64.dll")) { } else if (Core.Is64Bit && File.Exists("rdrand64.dll")) {
_Random = new RDRand64(); _Random = new RDRand64();
} else if ( !Core.Is64Bit && File.Exists("rdrand32.dll") ) { } else if ( !Core.Is64Bit && File.Exists("rdrand32.dll") ) {
_Random = new RDRand32(); _Random = new RDRand32();
} else { } else {
_Random = new CSPRandom(); _Random = new CSPRandom();
} }
if (_Random is IHardwareRNG) { if (_Random is IHardwareRNG) {
if (!((IHardwareRNG)_Random).IsSupported()) { if (!((IHardwareRNG)_Random).IsSupported()) {
_Random = new CSPRandom(); _Random = new CSPRandom();
} }
} }
} }
public static bool IsHardwareRNG { public static bool IsHardwareRNG {
get { return _Random is IHardwareRNG; } get { return _Random is IHardwareRNG; }
} }
public static Type Type { public static Type Type {
get { return _Random.GetType(); } get { return _Random.GetType(); }
} }
public static int Next(int c) { public static int Next(int c) {
return _Random.Next(c); return _Random.Next(c);
} }
public static bool NextBool() { public static bool NextBool() {
return _Random.NextBool(); return _Random.NextBool();
} }
public static void NextBytes(byte[] b) { public static void NextBytes(byte[] b) {
_Random.NextBytes(b); _Random.NextBytes(b);
} }
public static double NextDouble() { public static double NextDouble() {
return _Random.NextDouble(); return _Random.NextDouble();
} }
} }
public interface IRandomImpl { public interface IRandomImpl {
int Next(int c); int Next(int c);
bool NextBool(); bool NextBool();
void NextBytes(byte[] b); void NextBytes(byte[] b);
double NextDouble(); double NextDouble();
} }
public interface IHardwareRNG { public interface IHardwareRNG {
bool IsSupported(); bool IsSupported();
} }
public sealed class SimpleRandom : IRandomImpl { public sealed class SimpleRandom : IRandomImpl {
private Random m_Random = new Random(); private Random m_Random = new Random();
public SimpleRandom() { public SimpleRandom() {
} }
public int Next(int c) { public int Next(int c) {
int r; int r;
lock (m_Random) lock (m_Random)
r = m_Random.Next(c); r = m_Random.Next(c);
return r; return r;
} }
public bool NextBool() { public bool NextBool() {
return NextDouble() >= .5; return NextDouble() >= .5;
} }
public void NextBytes(byte[] b) { public void NextBytes(byte[] b) {
lock (m_Random) lock (m_Random)
m_Random.NextBytes(b); m_Random.NextBytes(b);
} }
public double NextDouble() { public double NextDouble() {
double r; double r;
lock (m_Random) lock (m_Random)
r = m_Random.NextDouble(); r = m_Random.NextDouble();
return r; return r;
} }
} }
public sealed class CSPRandom : IRandomImpl { public sealed class CSPRandom : IRandomImpl {
private RNGCryptoServiceProvider _CSP = new RNGCryptoServiceProvider(); private RNGCryptoServiceProvider _CSP = new RNGCryptoServiceProvider();
private static int BUFFER_SIZE = 0x4000; private static int BUFFER_SIZE = 0x4000;
private static int LARGE_REQUEST = 0x40; private static int LARGE_REQUEST = 0x40;
private byte[] _Working = new byte[BUFFER_SIZE]; private byte[] _Working = new byte[BUFFER_SIZE];
private byte[] _Buffer = new byte[BUFFER_SIZE]; private byte[] _Buffer = new byte[BUFFER_SIZE];
private int _Index = 0; private int _Index = 0;
private object _sync = new object(); private object _sync = new object();
private ManualResetEvent _filled = new ManualResetEvent(false); private ManualResetEvent _filled = new ManualResetEvent(false);
public CSPRandom() { public CSPRandom() {
_CSP.GetBytes(_Working); _CSP.GetBytes(_Working);
ThreadPool.QueueUserWorkItem(new WaitCallback(Fill)); ThreadPool.QueueUserWorkItem(new WaitCallback(Fill));
} }
private void CheckSwap(int c) { private void CheckSwap(int c) {
if (_Index + c < BUFFER_SIZE) if (_Index + c < BUFFER_SIZE)
return; return;
_filled.WaitOne(); _filled.WaitOne();
byte[] b = _Working; byte[] b = _Working;
_Working = _Buffer; _Working = _Buffer;
_Buffer = b; _Buffer = b;
_Index = 0; _Index = 0;
_filled.Reset(); _filled.Reset();
ThreadPool.QueueUserWorkItem(new WaitCallback(Fill)); ThreadPool.QueueUserWorkItem(new WaitCallback(Fill));
} }
private void Fill(object o) { private void Fill(object o) {
lock (_CSP) lock (_CSP)
_CSP.GetBytes(_Buffer); _CSP.GetBytes(_Buffer);
_filled.Set(); _filled.Set();
} }
private void _GetBytes(byte[] b) { private void _GetBytes(byte[] b) {
int c = b.Length; int c = b.Length;
lock (_sync) { lock (_sync) {
CheckSwap(c); CheckSwap(c);
Buffer.BlockCopy(_Working, _Index, b, 0, c); Buffer.BlockCopy(_Working, _Index, b, 0, c);
_Index += c; _Index += c;
} }
} }
private void _GetBytes(byte[] b, int offset, int count) { private void _GetBytes(byte[] b, int offset, int count) {
lock (_sync) { lock (_sync) {
CheckSwap(count); CheckSwap(count);
Buffer.BlockCopy(_Working, _Index, b, offset, count); Buffer.BlockCopy(_Working, _Index, b, offset, count);
_Index += count; _Index += count;
} }
} }
public int Next(int c) { public int Next(int c) {
return (int)(c * NextDouble()); return (int)(c * NextDouble());
} }
public bool NextBool() { public bool NextBool() {
return (NextByte() & 1) == 1; return (NextByte() & 1) == 1;
} }
private byte NextByte() { private byte NextByte() {
lock (_sync) { lock (_sync) {
CheckSwap(1); CheckSwap(1);
return _Working[_Index++]; return _Working[_Index++];
} }
} }
public void NextBytes(byte[] b) { public void NextBytes(byte[] b) {
int c = b.Length; int c = b.Length;
if (c >= LARGE_REQUEST) { if (c >= LARGE_REQUEST) {
lock (_CSP) lock (_CSP)
_CSP.GetBytes(b); _CSP.GetBytes(b);
return; return;
} }
_GetBytes(b); _GetBytes(b);
} }
public unsafe double NextDouble() { public unsafe double NextDouble() {
byte[] b = new byte[8]; byte[] b = new byte[8];
if (BitConverter.IsLittleEndian) { if (BitConverter.IsLittleEndian) {
b[7] = 0; b[7] = 0;
_GetBytes(b, 0, 7); _GetBytes(b, 0, 7);
} else { } else {
b[0] = 0; b[0] = 0;
_GetBytes(b, 1, 7); _GetBytes(b, 1, 7);
} }
ulong r = 0; ulong r = 0;
fixed(byte* buf = b) fixed(byte* buf = b)
r = *(ulong*)(&buf[0]) >> 3; r = *(ulong*)(&buf[0]) >> 3;
/* double: 53 bits of significand precision /* double: 53 bits of significand precision
* ulong.MaxValue >> 11 = 9007199254740991 * ulong.MaxValue >> 11 = 9007199254740991
* 2^53 = 9007199254740992 * 2^53 = 9007199254740992
*/ */
return (double)r / 9007199254740992; return (double)r / 9007199254740992;
} }
} }
public sealed class RDRand32 : IRandomImpl, IHardwareRNG { public sealed class RDRand32 : IRandomImpl, IHardwareRNG {
internal class SafeNativeMethods { internal class SafeNativeMethods {
[DllImport("rdrand32")] [DllImport("rdrand32")]
internal static extern RDRandError rdrand_32(ref uint rand, bool retry); internal static extern RDRandError rdrand_32(ref uint rand, bool retry);
[DllImport("rdrand32")] [DllImport("rdrand32")]
internal static extern RDRandError rdrand_get_bytes(int n, byte[] buffer); internal static extern RDRandError rdrand_get_bytes(int n, byte[] buffer);
} }
private static int BUFFER_SIZE = 0x10000; private static int BUFFER_SIZE = 0x10000;
private static int LARGE_REQUEST = 0x40; private static int LARGE_REQUEST = 0x40;
private byte[] _Working = new byte[BUFFER_SIZE]; private byte[] _Working = new byte[BUFFER_SIZE];
private byte[] _Buffer = new byte[BUFFER_SIZE]; private byte[] _Buffer = new byte[BUFFER_SIZE];
private int _Index = 0; private int _Index = 0;
private object _sync = new object(); private object _sync = new object();
private ManualResetEvent _filled = new ManualResetEvent(false); private ManualResetEvent _filled = new ManualResetEvent(false);
public RDRand32() { public RDRand32() {
SafeNativeMethods.rdrand_get_bytes(BUFFER_SIZE, _Working); SafeNativeMethods.rdrand_get_bytes(BUFFER_SIZE, _Working);
ThreadPool.QueueUserWorkItem(new WaitCallback(Fill)); ThreadPool.QueueUserWorkItem(new WaitCallback(Fill));
} }
public bool IsSupported() { public bool IsSupported() {
uint r = 0; uint r = 0;
return SafeNativeMethods.rdrand_32(ref r, true) == RDRandError.Success; return SafeNativeMethods.rdrand_32(ref r, true) == RDRandError.Success;
} }
private void CheckSwap(int c) { private void CheckSwap(int c) {
if (_Index + c < BUFFER_SIZE) if (_Index + c < BUFFER_SIZE)
return; return;
_filled.WaitOne(); _filled.WaitOne();
byte[] b = _Working; byte[] b = _Working;
_Working = _Buffer; _Working = _Buffer;
_Buffer = b; _Buffer = b;
_Index = 0; _Index = 0;
_filled.Reset(); _filled.Reset();
ThreadPool.QueueUserWorkItem(new WaitCallback(Fill)); ThreadPool.QueueUserWorkItem(new WaitCallback(Fill));
} }
private void Fill(object o) { private void Fill(object o) {
SafeNativeMethods.rdrand_get_bytes(BUFFER_SIZE, _Buffer); SafeNativeMethods.rdrand_get_bytes(BUFFER_SIZE, _Buffer);
_filled.Set(); _filled.Set();
} }
private void _GetBytes(byte[] b) { private void _GetBytes(byte[] b) {
int c = b.Length; int c = b.Length;
lock (_sync) { lock (_sync) {
CheckSwap(c); CheckSwap(c);
Buffer.BlockCopy(_Working, _Index, b, 0, c); Buffer.BlockCopy(_Working, _Index, b, 0, c);
_Index += c; _Index += c;
} }
} }
private void _GetBytes(byte[] b, int offset, int count) { private void _GetBytes(byte[] b, int offset, int count) {
lock (_sync) { lock (_sync) {
CheckSwap(count); CheckSwap(count);
Buffer.BlockCopy(_Working, _Index, b, offset, count); Buffer.BlockCopy(_Working, _Index, b, offset, count);
_Index += count; _Index += count;
} }
} }
public int Next(int c) { public int Next(int c) {
return (int)(c * NextDouble()); return (int)(c * NextDouble());
} }
public bool NextBool() { public bool NextBool() {
return (NextByte() & 1) == 1; return (NextByte() & 1) == 1;
} }
private byte NextByte() { private byte NextByte() {
lock (_sync) { lock (_sync) {
CheckSwap(1); CheckSwap(1);
return _Working[_Index++]; return _Working[_Index++];
} }
} }
public void NextBytes(byte[] b) { public void NextBytes(byte[] b) {
int c = b.Length; int c = b.Length;
if (c >= LARGE_REQUEST) { if (c >= LARGE_REQUEST) {
SafeNativeMethods.rdrand_get_bytes(c, b); SafeNativeMethods.rdrand_get_bytes(c, b);
return; return;
} }
_GetBytes(b); _GetBytes(b);
} }
public unsafe double NextDouble() { public unsafe double NextDouble() {
byte[] b = new byte[8]; byte[] b = new byte[8];
if (BitConverter.IsLittleEndian) { if (BitConverter.IsLittleEndian) {
b[7] = 0; b[7] = 0;
_GetBytes(b, 0, 7); _GetBytes(b, 0, 7);
} else { } else {
b[0] = 0; b[0] = 0;
_GetBytes(b, 1, 7); _GetBytes(b, 1, 7);
} }
ulong r = 0; ulong r = 0;
fixed(byte* buf = b) fixed(byte* buf = b)
r = *(ulong*)(&buf[0]) >> 3; r = *(ulong*)(&buf[0]) >> 3;
/* double: 53 bits of significand precision /* double: 53 bits of significand precision
* ulong.MaxValue >> 11 = 9007199254740991 * ulong.MaxValue >> 11 = 9007199254740991
* 2^53 = 9007199254740992 * 2^53 = 9007199254740992
*/ */
return (double)r / 9007199254740992; return (double)r / 9007199254740992;
} }
} }
public sealed class RDRand64 : IRandomImpl, IHardwareRNG { public sealed class RDRand64 : IRandomImpl, IHardwareRNG {
internal class SafeNativeMethods { internal class SafeNativeMethods {
[DllImport("rdrand64")] [DllImport("rdrand64")]
internal static extern RDRandError rdrand_64(ref ulong rand, bool retry); internal static extern RDRandError rdrand_64(ref ulong rand, bool retry);
[DllImport("rdrand64")] [DllImport("rdrand64")]
internal static extern RDRandError rdrand_get_bytes(int n, byte[] buffer); internal static extern RDRandError rdrand_get_bytes(int n, byte[] buffer);
} }
private static int BUFFER_SIZE = 0x10000; private static int BUFFER_SIZE = 0x10000;
private static int LARGE_REQUEST = 0x40; private static int LARGE_REQUEST = 0x40;
private byte[] _Working = new byte[BUFFER_SIZE]; private byte[] _Working = new byte[BUFFER_SIZE];
private byte[] _Buffer = new byte[BUFFER_SIZE]; private byte[] _Buffer = new byte[BUFFER_SIZE];
private int _Index = 0; private int _Index = 0;
private object _sync = new object(); private object _sync = new object();
private ManualResetEvent _filled = new ManualResetEvent(false); private ManualResetEvent _filled = new ManualResetEvent(false);
public RDRand64() { public RDRand64() {
SafeNativeMethods.rdrand_get_bytes(BUFFER_SIZE, _Working); SafeNativeMethods.rdrand_get_bytes(BUFFER_SIZE, _Working);
ThreadPool.QueueUserWorkItem(new WaitCallback(Fill)); ThreadPool.QueueUserWorkItem(new WaitCallback(Fill));
} }
public bool IsSupported() { public bool IsSupported() {
ulong r = 0; ulong r = 0;
return SafeNativeMethods.rdrand_64(ref r, true) == RDRandError.Success; return SafeNativeMethods.rdrand_64(ref r, true) == RDRandError.Success;
} }
private void CheckSwap(int c) { private void CheckSwap(int c) {
if (_Index + c < BUFFER_SIZE) if (_Index + c < BUFFER_SIZE)
return; return;
_filled.WaitOne(); _filled.WaitOne();
byte[] b = _Working; byte[] b = _Working;
_Working = _Buffer; _Working = _Buffer;
_Buffer = b; _Buffer = b;
_Index = 0; _Index = 0;
_filled.Reset(); _filled.Reset();
ThreadPool.QueueUserWorkItem(new WaitCallback(Fill)); ThreadPool.QueueUserWorkItem(new WaitCallback(Fill));
} }
private void Fill(object o) { private void Fill(object o) {
SafeNativeMethods.rdrand_get_bytes(BUFFER_SIZE, _Buffer); SafeNativeMethods.rdrand_get_bytes(BUFFER_SIZE, _Buffer);
_filled.Set(); _filled.Set();
} }
private void _GetBytes(byte[] b) { private void _GetBytes(byte[] b) {
int c = b.Length; int c = b.Length;
lock (_sync) { lock (_sync) {
CheckSwap(c); CheckSwap(c);
Buffer.BlockCopy(_Working, _Index, b, 0, c); Buffer.BlockCopy(_Working, _Index, b, 0, c);
_Index += c; _Index += c;
} }
} }
private void _GetBytes(byte[] b, int offset, int count) { private void _GetBytes(byte[] b, int offset, int count) {
lock (_sync) { lock (_sync) {
CheckSwap(count); CheckSwap(count);
Buffer.BlockCopy(_Working, _Index, b, offset, count); Buffer.BlockCopy(_Working, _Index, b, offset, count);
_Index += count; _Index += count;
} }
} }
public int Next(int c) { public int Next(int c) {
return (int)(c * NextDouble()); return (int)(c * NextDouble());
} }
public bool NextBool() { public bool NextBool() {
return (NextByte() & 1) == 1; return (NextByte() & 1) == 1;
} }
private byte NextByte() { private byte NextByte() {
lock (_sync) { lock (_sync) {
CheckSwap(1); CheckSwap(1);
return _Working[_Index++]; return _Working[_Index++];
} }
} }
public void NextBytes(byte[] b) { public void NextBytes(byte[] b) {
int c = b.Length; int c = b.Length;
if (c >= LARGE_REQUEST) { if (c >= LARGE_REQUEST) {
SafeNativeMethods.rdrand_get_bytes(c, b); SafeNativeMethods.rdrand_get_bytes(c, b);
return; return;
} }
_GetBytes(b); _GetBytes(b);
} }
public unsafe double NextDouble() { public unsafe double NextDouble() {
byte[] b = new byte[8]; byte[] b = new byte[8];
if (BitConverter.IsLittleEndian) { if (BitConverter.IsLittleEndian) {
b[7] = 0; b[7] = 0;
_GetBytes(b, 0, 7); _GetBytes(b, 0, 7);
} else { } else {
b[0] = 0; b[0] = 0;
_GetBytes(b, 1, 7); _GetBytes(b, 1, 7);
} }
ulong r = 0; ulong r = 0;
fixed(byte* buf = b) fixed(byte* buf = b)
r = *(ulong*)(&buf[0]) >> 3; r = *(ulong*)(&buf[0]) >> 3;
/* double: 53 bits of significand precision /* double: 53 bits of significand precision
* ulong.MaxValue >> 11 = 9007199254740991 * ulong.MaxValue >> 11 = 9007199254740991
* 2^53 = 9007199254740992 * 2^53 = 9007199254740992
*/ */
return (double)r / 9007199254740992; return (double)r / 9007199254740992;
} }
} }
public enum RDRandError : int { public enum RDRandError : int {
Unknown = -4, Unknown = -4,
Unsupported = -3, Unsupported = -3,
Supported = -2, Supported = -2,
NotReady = -1, NotReady = -1,
Failure = 0, Failure = 0,
Success = 1, Success = 1,
} }
} }