EntropySource.cs

Posted on June 28, 2020Tags , , ,   Leave a comment on EntropySource.cs

Using PC Data Creates a Strong Entropy Source

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading;
public class EntropySource
{
    private const int Sha3BitSize           = 256;
    private const int Sha3BitBufferMoveSize = Sha3BitSize >> 3;
    private const int CpuDataLength         = 256;
    private const int JitterDataSize        = 256;
    [DllImport("kernel32.dll")]
    private static extern int GetTickCount();
    [DllImport("kernel32.dll")]
    internal static extern bool QueryPerformanceCounter(out long lpPerformanceCount);
    [DllImport("kernel32.dll")]
    internal static extern bool QueryPerformanceFrequency(out long lpFrequency);
    public static byte[] GetBuffer(int bufferSize)
    {
        var rhb = new byte[0];
        var rho = new byte[bufferSize];
        var EntropyThread = new Thread(() =>
        {
            var el        = new List<string>();
            var hashBytes = new HashSet<byte[]>(new ArrayComparer());
            var sw        = new Stopwatch();
            sw.Start();
            AddLocalUserData(hashBytes);
            AddProcessInformation(hashBytes, el);
            AddJitterInformation(hashBytes);
            GetWmiInfor("Win32_PerfRawData_PerfOS_Processor",              hashBytes);
            GetWmiInfor("Win32_PerfRawData_PerfOS_System",                 hashBytes);
            GetWmiInfor("Win32_PerfRawData_Counters_ProcessorInformation", hashBytes);
            GetWmiInfor("Win32_PerfRawData_Counters_Synchronization",      hashBytes);
            GetWmiInfor("Win32_PerfRawData_PerfDisk_LogicalDisk",          hashBytes);
            GetWmiInfor("Win32_PerfRawData_PerfDisk_PhysicalDisk",         hashBytes);
            GetWmiInfor("Win32_PerfFormattedData_PerfOS_Cache",            hashBytes);
            GetWmiInfor("Win32_PerfFormattedData_PerfOS_Memory",           hashBytes);
            GetWmiInfor("Win32_PerfFormattedData_PerfOS_Objects",          hashBytes);
            CpuInfo(hashBytes);
            sw.Stop();
            hashBytes.Add(sw.Elapsed.Ticks.GetBytes());
            rhb = HashBytesToArray(hashBytes);
            AddHistogramInforamation(rhb, hashBytes);
            rhb = HashBytesToArray(hashBytes);
            hashBytes.Clear();
            var div = bufferSize / Sha3BitBufferMoveSize;
            if (div * Sha3BitBufferMoveSize < bufferSize)
                div++;
            var segSize = rhb.Length / div;
            var aseg    = new byte[segSize];
            for (var i = 0; i < div; ++i)
            {
                Buffer.BlockCopy(rhb,                                            i * segSize, aseg, 0,                         segSize);
                Buffer.BlockCopy(new SHA3Managed(Sha3BitSize).ComputeHash(aseg), 0,           rho,  i * Sha3BitBufferMoveSize, Sha3BitBufferMoveSize);
            }
        }) {Priority = ThreadPriority.Highest};
        EntropyThread.Start();
        EntropyThread.Join();
        return rho;
    }
    private static void GetWmiInfor(string key, HashSet<byte[]> hashBytes)
    {
        var wmi = new WMIServices();
        wmi.GetProperties(key);
        foreach (var v in wmi.EntriesList)
        foreach (var p in v.Value)
            hashBytes.Add(p.GetBytesObject());
    }
    private static void CpuInfo(HashSet<byte[]> hashBytes)
    {
        var lhashBytes = new HashSet<byte[]>();
        var dump       = CpuTotalPc.CPULoad;
        do
        {
            var l = (byte) CpuTotalPc.CPULoad;
            if (l != 0)
                lhashBytes.Add(new[] {l});
            Thread.Sleep(1);
        } while (lhashBytes.Count < CpuDataLength);
        foreach (var b in lhashBytes)
            hashBytes.Add(b);
    }
    private static void AddHistogramInforamation(byte[] rhb, HashSet<byte[]> hashBytes)
    {
        var FFTArray = Histogram(rhb);
        for (var i = 0; i < FFTArray.Length; ++i)
            hashBytes.Add(FFTArray[i].GetBytes());
    }
    private static void AddJitterInformation(HashSet<byte[]> hashBytes)
    {
        hashBytes.Add(new JitterEx(JitterDataSize).GetBuffer());
    }
    private static void AddProcessInformation(HashSet<byte[]> hashBytes, List<string> el)
    {
        foreach (var p in Process.GetProcesses())
        {
            try
            {
                hashBytes.Add(p.Id.GetBytes());
            }
            catch (Exception ex)
            {
                el.Add($"{ex.Message} hashBytes.Add(p.Id.GetBytes())");
            }
            try
            {
                hashBytes.Add(p.HandleCount.GetBytes());
            }
            catch (Exception ex)
            {
                el.Add($"{ex.Message} hashBytes.Add(p.HandleCount.GetBytes())");
            }
            try
            {
                hashBytes.Add(p.NonpagedSystemMemorySize64.GetBytes());
            }
            catch (Exception ex)
            {
                el.Add($"{ex.Message} hashBytes.Add(p.NonpagedSystemMemorySize64.GetBytes())");
            }
            try
            {
                hashBytes.Add(p.PagedMemorySize64.GetBytes());
            }
            catch (Exception ex)
            {
                el.Add($"{ex.Message} hashBytes.Add(p.PagedMemorySize64.GetBytes())");
            }
            try
            {
                hashBytes.Add(p.PagedSystemMemorySize64.GetBytes());
            }
            catch (Exception ex)
            {
                el.Add($"{ex.Message} hashBytes.Add(p.PagedSystemMemorySize64.GetBytes())");
            }
            try
            {
                hashBytes.Add(p.PeakPagedMemorySize64.GetBytes());
            }
            catch (Exception ex)
            {
                el.Add($"{ex.Message} hashBytes.Add(p.PeakPagedMemorySize64.GetBytes())");
            }
            try
            {
                hashBytes.Add(p.PeakVirtualMemorySize64.GetBytes());
            }
            catch (Exception ex)
            {
                el.Add($"{ex.Message} hashBytes.Add(p.PeakVirtualMemorySize64.GetBytes())");
            }
            try
            {
                hashBytes.Add(p.PeakWorkingSet64.GetBytes());
            }
            catch (Exception ex)
            {
                el.Add($"{ex.Message} hashBytes.Add(p.PeakWorkingSet64.GetBytes()");
            }
            try
            {
                hashBytes.Add(p.PrivateMemorySize64.GetBytes());
            }
            catch (Exception ex)
            {
                el.Add($"{ex.Message} hashBytes.Add(p.PrivateMemorySize64.GetBytes())");
            }
            try
            {
                hashBytes.Add(p.ProcessName.GetBytes());
            }
            catch (Exception ex)
            {
                el.Add($"{ex.Message} hashBytes.Add(p.ProcessName.GetBytes())");
            }
            try
            {
                hashBytes.Add(p.Threads.Count.GetBytes());
            }
            catch (Exception ex)
            {
                el.Add($"{ex.Message} hashBytes.Add(p.Threads.Count.GetBytes())");
            }
            try
            {
                hashBytes.Add(p.SessionId.GetBytes());
            }
            catch (Exception ex)
            {
                el.Add($"{ex.Message} hashBytes.Add(p.SessionId.GetBytes())");
            }
            for (var i = 0; i < p.Threads.Count; i++)
            {
                try
                {
                    hashBytes.Add(p.Threads[i].CurrentPriority.GetBytes());
                }
                catch (Exception ex)
                {
                    el.Add($"{ex.Message} hashBytes.Add(p.Threads[i].CurrentPriority.GetBytes())");
                }
                try
                {
                    hashBytes.Add(p.Threads[i].Id.GetBytes());
                }
                catch (Exception ex)
                {
                    el.Add($"{ex.Message} hashBytes.Add(p.Threads[i].Id.GetBytes())");
                }
                try
                {
                    hashBytes.Add(p.Threads[i].StartAddress.ToString().GetBytes());
                }
                catch (Exception ex)
                {
                    el.Add($"{ex.Message} hashBytes.Add(p.Threads[i].StartAddress.ToString().GetBytes())");
                }
            }
            try
            {
                hashBytes.Add(p.VirtualMemorySize64.GetBytes());
            }
            catch (Exception ex)
            {
                el.Add($"{ex.Message} hashBytes.Add(p.VirtualMemorySize64.GetBytes()))");
            }
            try
            {
                hashBytes.Add(p.WorkingSet64.GetBytes());
            }
            catch (Exception ex)
            {
                el.Add($"{ex.Message} hashBytes.Add(p.WorkingSet64.GetBytes()))");
            }
        }
    }
    private static void AddLocalUserData(HashSet<byte[]> hashBytes)
    {
        hashBytes.Add(Process.GetCurrentProcess().Id.GetBytes());
        hashBytes.Add(Thread.CurrentThread.ManagedThreadId.GetBytes());
        hashBytes.Add(GetTickCount().GetBytes());
        hashBytes.Add(Process.GetProcesses().Length.GetBytes());
        QueryPerformanceFrequency(out var freq);
        hashBytes.Add(freq.GetBytes());
        for (var i = 0; i < 128; i++)
        {
            QueryPerformanceCounter(out var query);
            hashBytes.Add(query.GetBytes());
        }
        hashBytes.Add(DateTime.Now.Ticks.GetBytes());
        hashBytes.Add(Environment.UserName.GetBytes());
        hashBytes.Add(Environment.MachineName.GetBytes());
        hashBytes.Add(Environment.OSVersion.ToString().GetBytes());
        hashBytes.Add(Environment.ProcessorCount.GetBytes());
        hashBytes.Add(Environment.UserDomainName.GetBytes());
        hashBytes.Add(Environment.StackTrace.GetBytes());
        hashBytes.Add(Environment.GetLogicalDrives().GetBytes());
        hashBytes.Add(MemoryInfo.GetValues().GetBytesObjectSerial());
        hashBytes.Add(MemoryInfo.GetSystemPerformanceInformation().GetBytesObjectSerial());
    }
    private static byte[] HashBytesToArray(HashSet<byte[]> hashBytes)
    {
        byte[] rhb;
        var    ptr   = 0;
        var    count = 0;
        var    arr   = hashBytes.ToArray();
        foreach (var hba in arr)
            count += hba.Length;
        hashBytes.Add(count.GetBytes());
        arr   =  hashBytes.ToArray();
        count += arr.Last().Length;
        rhb   =  new byte[count];
        foreach (var hba in arr)
        {
            Buffer.BlockCopy(hba, 0, rhb, ptr, hba.Length);
            ptr += hba.Length;
        }
        return rhb;
    }
    private static int[] Histogram(IEnumerable<byte> ba)
    {
        var map = new int[256];
        foreach (var i in ba)
            map[i]++;
        return map;
    }
}

ByteArraySegment.cs

Posted on June 17, 2020June 17, 2020Tags , , ,   Leave a comment on ByteArraySegment.cs

Segment a byte array to 16, 32, 64 bit Arrays

using System;
using System.Collections.Generic;
using System.Numerics;
public class ByteArraySegment
{
    public IEnumerable<T[]> SegmentEnum<T>(T[] buffer, int size)
    {
        var idx = 0;
        var ptr = buffer.Length;
        while (ptr >= size)
        {
            var array = new T[size];
            Buffer.BlockCopy(buffer, idx, array, 0, size);
            ptr -= size;
            idx += size;
            yield return array;
        }
    }
    public List<byte[]> SegmentList(byte[] buffer, int size)
    {
        var list = new List<byte[]>(buffer.Length >> 2);
        var idx  = 0;
        var ptr  = buffer.Length;
        while (ptr >= size)
        {
            var _result = new byte[size];
            Buffer.BlockCopy(buffer, idx, _result, 0, size);
            list.Add(_result);
            ptr -= size;
            idx += size;
        }
        return list;
    }
    public char[] SegmentChar(byte[] buffer)
    {
        var len = buffer.Length;
        if (len % 2 != 0)
            throw new Exception("Buffer must be a multiple of 2.");
        var array = new char[len >> 1];
        Buffer.BlockCopy(buffer, 0, array, 0, len);
        return array;
    }
    public short[] Segment16(byte[] buffer)
    {
        var len = buffer.Length;
        if (len % 2 != 0)
            throw new Exception("Buffer must be a multiple of 2.");
        var array = new short[len >> 1];
        Buffer.BlockCopy(buffer, 0, array, 0, len);
        return array;
    }
    public ushort[] SegmentU16(byte[] buffer)
    {
        var len = buffer.Length;
        if (len % 2 != 0)
            throw new Exception("Buffer must be a multiple of 2.");
        var array = new ushort[len >> 1];
        Buffer.BlockCopy(buffer, 0, array, 0, len);
        return array;
    }
    public int[] Segment32(byte[] buffer)
    {
        var len = buffer.Length;
        if (len % 4 != 0)
            throw new Exception("Buffer must be a multiple of 4.");
        var array = new int[len >> 2];
        Buffer.BlockCopy(buffer, 0, array, 0, len);
        return array;
    }
    public float[] SegmentFloat(byte[] buffer)
    {
        var len = buffer.Length;
        if (len % 4 != 0)
            throw new Exception("Buffer must be a multiple of 4.");
        var array = new float[len >> 2];
        Buffer.BlockCopy(buffer, 0, array, 0, len);
        return array;
    }
    public uint[] SegmentU32(byte[] buffer)
    {
        var len = buffer.Length;
        if (len % 4 != 0)
            throw new Exception("Buffer must be a multiple of 4.");
        var array = new uint[len >> 2];
        Buffer.BlockCopy(buffer, 0, array, 0, len);
        return array;
    }
    public long[] Segment64(byte[] buffer)
    {
        var len = buffer.Length;
        if (len % 8 != 0)
            throw new Exception("Buffer must be a multiple of 8.");
        var array = new long[len >> 3];
        Buffer.BlockCopy(buffer, 0, array, 0, len);
        return array;
    }
    public double[] SegmentDouble(byte[] buffer)
    {
        var len = buffer.Length;
        if (len % 8 != 0)
            throw new Exception("Buffer must be a multiple of 8.");
        var array = new double[len >> 3];
        Buffer.BlockCopy(buffer, 0, array, 0, len);
        return array;
    }
    public ulong[] SegmentU64(byte[] buffer)
    {
        var len = buffer.Length;
        if (len % 8 != 0)
            throw new Exception("Buffer must be a multiple of 8.");
        var array = new ulong[len >> 3];
        Buffer.BlockCopy(buffer, 0, array, 0, len);
        return array;
    }
    public BigInteger[] SegmentBI(byte[] buffer, int size)
    {
        var idx  = 0;
        var ptr  = buffer.Length;
        var bi   = new BigInteger[buffer.Length / size];
        var ptr1 = 0;
        while (ptr >= size)
        {
            var array = new byte[size];
            Buffer.BlockCopy(buffer, idx, array, 0, size);
            ptr      -= size;
            idx      += size;
            bi[ptr1++] =  new BigInteger(array);
        }
        return bi;
    }
}

Writer.cs

Posted on June 10, 2020June 11, 2020Tags , ,   Leave a comment on Writer.cs

Simplified Binary Writer

Example Code:

using System;
using System.IO;
using System.Numerics;
using System.Text;
/// <inheritdoc />
/// <summary>
///     Alternate Binary Writer created to enhance usability and ease coding complexity.
/// </summary>
public class Writer : IDisposable
{
    /// <inheritdoc />
    /// <summary>
    ///     Initializes a new instance of the Writer class based on a specified path name, a default FileMode of Create, and
    ///     defaulting to UTF-8 encoding.
    /// </summary>
    public Writer(string path) : this(path, FileMode.Create, new UTF8Encoding(false, true))
    {
    }
    /// <inheritdoc />
    /// <summary>
    ///     Initializes a new instance of the Writer class based on a specified path name, a specified FileMode and defaulting
    ///     to UTF-8 encoding.
    /// </summary>
    public Writer(string path, FileMode fm) : this(path, fm, new UTF8Encoding(false, true))
    {
    }
    /// <summary>
    ///     Initializes a new instance of the Writer class based on a specified path name, a specified FileMode and a specified
    ///     encoding.
    /// </summary>
    public Writer(string path, FileMode fm, Encoding encoding)
    {
        if (string.IsNullOrEmpty(path))
            throw new Exception($"Path {path} cannot be null or empty.");
        BaseMode = fm;
        try
        {
            if (File.Exists(path))
                File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Normal);
            BaseStream = new FileStream(path, BaseMode, FileAccess.Write);
            if (BaseStream?.CanWrite == true)
                BaseWriter = new BinaryWriter(BaseStream, encoding, true);
            else throw new Exception($"The FileStream for path:{path} is null.");
        }
        catch (Exception e)
        {
            throw new Exception($"Error: {e.Message}");
        }
    }
    public Writer(Stream stream) : this(stream, new UTF8Encoding(false, true))
    {
    }
    public Writer(Stream strm, Encoding encoding)
    {
        try
        {
            if (strm?.CanWrite == true)
                BaseWriter = new BinaryWriter(strm, encoding, true);
            else throw new Exception("The Stream is null.");
        }
        catch (Exception e)
        {
            throw new Exception($"Error: {e.Message}");
        }
    }
    /// <summary>
    ///     Expose the underlying BinaryWriter
    /// </summary>
    public BinaryWriter BaseWriter { get; }
    /// <summary>
    ///     Expose the underlying FileMode
    /// </summary>
    public FileMode BaseMode { get; }
    /// <summary>
    ///     Expose the underlying FileStream
    /// </summary>
    public FileStream BaseStream { get; }
    /// <summary>
    ///     Each time a primitive is written there is one additional integer written to signify its type.
    ///     This can be used to compute the final size of the stream bytes needed.
    /// </summary>
    public static int PrimitiveOverHead => sizeof(int);
    /// <summary>
    ///     Each time a array is written there are two additional integers written one to signify its type and the other its
    ///     size.
    ///     This can be used to compute the final size of the stream bytes needed.
    /// </summary>
    public static int ArrayOverHead => sizeof(int) * 2;
    public void Dispose()
    {
        if (BaseStream == null) return;
        BaseStream.Flush();
        BaseStream.Dispose();
    }
    /// <summary>
    ///     Sets the position to offset relative to origin within the stream.
    /// </summary>
    public long Seek(int offset, SeekOrigin origin)
    {
        return BaseStream.Seek(offset, origin);
    }
    public void WriteBool(bool value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.Boolean);
        BaseWriter.Write(value);
    }
    public void WriteChar(char value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.Character);
        BaseWriter.Write(value);
    }
    public void WriteByte(byte value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.Byte);
        BaseWriter.Write(value);
    }
    public void WriteSByte(sbyte value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.ShortByte);
        BaseWriter.Write(value);
    }
    public void WriteShort(short value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.Short);
        BaseWriter.Write(value);
    }
    public void WriteUShort(ushort value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.UnsignedShort);
        BaseWriter.Write(value);
    }
    public void WriteInt(int value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.Integer);
        BaseWriter.Write(value);
    }
    public void WriteUInt(uint value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.UnsignedInteger);
        BaseWriter.Write(value);
    }
    public void WriteLOng(long value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.Long);
        BaseWriter.Write(value);
    }
    public void WriteULong(ulong value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.UnsignedLong);
        BaseWriter.Write(value);
    }
    public void WriteString(string value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.String);
        BaseWriter.Write(value);
    }
    public void WriteFloat(float value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.Float);
        BaseWriter.Write(value);
    }
    public void WriteDouble(double value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.Double);
        BaseWriter.Write(value);
    }
    public void WriteDecimal(decimal value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.Decimal);
        BaseWriter.Write(value);
    }
    public void WriteBigInteger(BigInteger value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.BigInteger);
        WriteBytes(value.ToByteArray());
    }
    public void WriteBigRational(BigRational value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.BigRational);
        var Num = value.Numerator.ToByteArray();
        var Den = value.Denominator.ToByteArray();
        WriteBytes(Num);
        WriteBytes(Den);
    }
    public void WriteBools(bool[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.BooleanArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteChars(char[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.CharacterArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteBytes(byte[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.ByteArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteSBytes(sbyte[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.ShortByteArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteShorts(short[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.ShortArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteUShorts(ushort[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.UnsignedShortArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteInts(int[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.IntegerArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteUInts(uint[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.UnsignedIntegerArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteLongs(long[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.LongArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteULongs(ulong[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.UnsignedLongArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteStrings(string[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.StringArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteFloats(float[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.FloatArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteDoubles(double[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.DoubleArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteDecimals(decimal[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.DecimalArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            BaseWriter.Write(v);
    }
    public void WriteBigIntegers(BigInteger[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.BigIntegerArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            WriteBigInteger(v);
    }
    public void WriteBigRationals(BigRational[] value)
    {
        BaseWriter.Write((int) Rwtypes.Readerwritertypes.BigRationalArray);
        var len = value.Length;
        BaseWriter.Write(len);
        foreach (var v in value)
            WriteBigRational(v);
    }
}