Exemplo n.º 1
0
        /// <summary>
        /// Writes data into the stream.
        /// </summary>
        /// <typeparam name="T">Type of the data</typeparam>
        /// <param name="data">Data to be written</param>
        public void Write <T>(ref T data) where T : unmanaged
        {
            Span <byte> buffer = MemoryMarshal.Cast <T, byte>(MemoryMarshal.CreateSpan(ref data, 1));

            _activeStream.Write(buffer);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Writes data to CPU mapped memory.
 /// </summary>
 /// <typeparam name="T">Type of the data being written</typeparam>
 /// <param name="va">Virtual address to write the data into</param>
 /// <param name="value">Data to be written</param>
 /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
 public void Write <T>(ulong va, T value) where T : unmanaged
 {
     Write(va, MemoryMarshal.Cast <T, byte>(MemoryMarshal.CreateSpan(ref value, 1)));
 }
Exemplo n.º 3
0
 public PinnedConvertingMemoryManager(IPinnedMemoryOwner <TFrom> rooted)
 {
     _origin = (TTo *)rooted.Origin;
     Length  = MemoryMarshal.Cast <TFrom, TTo>(rooted.Memory.Span).Length;
 }
Exemplo n.º 4
0
 /// <summary>
 /// Reads data from GPU memory.
 /// </summary>
 /// <typeparam name="T">Type of the data to be read</typeparam>
 /// <param name="address">GPU virtual address of the data</param>
 /// <returns>Data at the memory location</returns>
 public override T MemoryRead <T>(ulong address)
 {
     return(MemoryMarshal.Cast <byte, T>(_data.Span.Slice((int)address))[0]);
 }
Exemplo n.º 5
0
 /// <summary>
 /// Reads data from CPU mapped memory.
 /// </summary>
 /// <typeparam name="T">Type of the data being read</typeparam>
 /// <param name="va">Virtual address of the data in memory</param>
 /// <returns>The data</returns>
 /// <exception cref="InvalidMemoryRegionException">Throw for unhandled invalid or unmapped memory accesses</exception>
 public T Read <T>(ulong va) where T : unmanaged
 {
     return(MemoryMarshal.Cast <byte, T>(GetSpan(va, Unsafe.SizeOf <T>(), true))[0]);
 }
Exemplo n.º 6
0
 public T MemoryRead <T>(ulong address) where T : unmanaged
 {
     return(MemoryMarshal.Cast <byte, T>(new ReadOnlySpan <byte>(_data).Slice((int)address))[0]);
 }
Exemplo n.º 7
0
        public THashValue GetHashValue(string value)
        {
            ReadOnlySpan <byte> span = MemoryMarshal.Cast <char, byte>(value.AsSpan());

            return(GetHashValue(span));
        }
Exemplo n.º 8
0
        public bool Open(IFilter imageFilter)
        {
            Stream stream = imageFilter.GetDataForkStream();

            stream.Seek(0, SeekOrigin.Begin);

            if (stream.Length < 512)
            {
                return(false);
            }

            byte[] qHdrB = new byte[68];
            stream.Read(qHdrB, 0, 68);
            qHdr = Marshal.SpanToStructureLittleEndian <QedHeader>(qHdrB);

            DicConsole.DebugWriteLine("QED plugin", "qHdr.magic = 0x{0:X8}", qHdr.magic);
            DicConsole.DebugWriteLine("QED plugin", "qHdr.cluster_size = {0}", qHdr.cluster_size);
            DicConsole.DebugWriteLine("QED plugin", "qHdr.table_size = {0}", qHdr.table_size);
            DicConsole.DebugWriteLine("QED plugin", "qHdr.header_size = {0}", qHdr.header_size);
            DicConsole.DebugWriteLine("QED plugin", "qHdr.features = {0}", qHdr.features);
            DicConsole.DebugWriteLine("QED plugin", "qHdr.compat_features = {0}", qHdr.compat_features);
            DicConsole.DebugWriteLine("QED plugin", "qHdr.autoclear_features = {0}", qHdr.autoclear_features);
            DicConsole.DebugWriteLine("QED plugin", "qHdr.l1_table_offset = {0}", qHdr.l1_table_offset);
            DicConsole.DebugWriteLine("QED plugin", "qHdr.image_size = {0}", qHdr.image_size);
            DicConsole.DebugWriteLine("QED plugin", "qHdr.backing_file_offset = {0}", qHdr.backing_file_offset);
            DicConsole.DebugWriteLine("QED plugin", "qHdr.backing_file_size = {0}", qHdr.backing_file_size);

            if (qHdr.image_size <= 1)
            {
                throw new ArgumentOutOfRangeException(nameof(qHdr.image_size), "Image size is too small");
            }

            if (!IsPowerOfTwo(qHdr.cluster_size))
            {
                throw new ArgumentOutOfRangeException(nameof(qHdr.cluster_size), "Cluster size must be a power of 2");
            }

            if (qHdr.cluster_size < 4096 || qHdr.cluster_size > 67108864)
            {
                throw new ArgumentOutOfRangeException(nameof(qHdr.cluster_size),
                                                      "Cluster size must be between 4 Kbytes and 64 Mbytes");
            }

            if (!IsPowerOfTwo(qHdr.table_size))
            {
                throw new ArgumentOutOfRangeException(nameof(qHdr.table_size), "Table size must be a power of 2");
            }

            if (qHdr.table_size < 1 || qHdr.table_size > 16)
            {
                throw new ArgumentOutOfRangeException(nameof(qHdr.table_size),
                                                      "Table size must be between 1 and 16 clusters");
            }

            if ((qHdr.features & QED_FEATURE_MASK) > 0)
            {
                throw new ArgumentOutOfRangeException(nameof(qHdr.features),
                                                      $"Image uses unknown incompatible features {qHdr.features & QED_FEATURE_MASK:X}");
            }

            if ((qHdr.features & QED_FEATURE_BACKING_FILE) == QED_FEATURE_BACKING_FILE)
            {
                throw new NotImplementedException("Differencing images not yet supported");
            }

            clusterSectors = qHdr.cluster_size / 512;
            tableSize      = qHdr.cluster_size * qHdr.table_size / 8;

            DicConsole.DebugWriteLine("QED plugin", "qHdr.clusterSectors = {0}", clusterSectors);
            DicConsole.DebugWriteLine("QED plugin", "qHdr.tableSize = {0}", tableSize);

            byte[] l1TableB = new byte[tableSize * 8];
            stream.Seek((long)qHdr.l1_table_offset, SeekOrigin.Begin);
            stream.Read(l1TableB, 0, (int)tableSize * 8);
            DicConsole.DebugWriteLine("QED plugin", "Reading L1 table");
            l1Table = MemoryMarshal.Cast <byte, ulong>(l1TableB).ToArray();

            l1Mask = 0;
            int c = 0;

            clusterBits = Ctz32(qHdr.cluster_size);
            l2Mask      = (tableSize - 1) << clusterBits;
            l1Shift     = clusterBits + Ctz32(tableSize);

            for (int i = 0; i < 64; i++)
            {
                l1Mask <<= 1;

                if (c >= 64 - l1Shift)
                {
                    continue;
                }

                l1Mask += 1;
                c++;
            }

            sectorMask = 0;
            for (int i = 0; i < clusterBits; i++)
            {
                sectorMask = (sectorMask << 1) + 1;
            }

            DicConsole.DebugWriteLine("QED plugin", "qHdr.clusterBits = {0}", clusterBits);
            DicConsole.DebugWriteLine("QED plugin", "qHdr.l1Mask = {0:X}", l1Mask);
            DicConsole.DebugWriteLine("QED plugin", "qHdr.l1Shift = {0}", l1Shift);
            DicConsole.DebugWriteLine("QED plugin", "qHdr.l2Mask = {0:X}", l2Mask);
            DicConsole.DebugWriteLine("QED plugin", "qHdr.sectorMask = {0:X}", sectorMask);

            maxL2TableCache = MAX_CACHE_SIZE / tableSize;
            maxClusterCache = MAX_CACHE_SIZE / qHdr.cluster_size;

            imageStream = stream;

            sectorCache  = new Dictionary <ulong, byte[]>();
            l2TableCache = new Dictionary <ulong, ulong[]>();
            clusterCache = new Dictionary <ulong, byte[]>();

            imageInfo.CreationTime         = imageFilter.GetCreationTime();
            imageInfo.LastModificationTime = imageFilter.GetLastWriteTime();
            imageInfo.MediaTitle           = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());
            imageInfo.Sectors      = qHdr.image_size / 512;
            imageInfo.SectorSize   = 512;
            imageInfo.XmlMediaType = XmlMediaType.BlockMedia;
            imageInfo.MediaType    = MediaType.GENERIC_HDD;
            imageInfo.ImageSize    = qHdr.image_size;

            imageInfo.Cylinders       = (uint)(imageInfo.Sectors / 16 / 63);
            imageInfo.Heads           = 16;
            imageInfo.SectorsPerTrack = 63;

            return(true);
        }
Exemplo n.º 9
0
        /// <summary>
        /// Construct a gravity model.
        /// </summary>
        /// <param name="name">the name of the model.</param>
        /// <param name="path">directory for data file.</param>
        /// <param name="Nmax">if non-negative, truncate the degree of the model this value.</param>
        /// <param name="Mmax">if non-negative, truncate the order of the model this value.</param>
        /// <remarks>
        /// A filename is formed by appending ".egm" (World Gravity Model) to the <paramref name="name"/>.
        /// If <paramref name="path"/> is specified (and is non-empty), then the file is loaded from directory, <paramref name="path"/>.
        /// Otherwise the <paramref name="path"/> is given by <see cref="DefaultGravityPath"/>.
        /// <para>
        /// This file contains the metadata which specifies the properties of the model.
        /// The coefficients for the spherical harmonic sums are obtained from a file obtained by appending ".cof"
        /// to metadata file (so the filename ends in ".egm.cof").
        /// </para>
        /// <para>
        /// If <paramref name="Nmax"/> ≥ 0 and <paramref name="Mmax"/> &lt; 0, then <paramref name="Mmax"/> is set to <paramref name="Nmax"/>.
        /// After the model is loaded, the maximum degree and order of the model can be found by the <see cref="Degree"/> and <see cref="Order"/> methods.
        /// </para>
        /// </remarks>
        public GravityModel(string name, string path = "", int Nmax = -1, int Mmax = -1)
        {
            _name        = name;
            _dir         = path;
            _description = "NONE";
            _amodel      = double.NaN;
            _GMmodel     = double.NaN;
            _zeta0       = 0;
            _corrmult    = 1;
            _nmx         = -1;
            _mmx         = -1;
            _norm        = Normalization.Full;

            if (string.IsNullOrWhiteSpace(path))
            {
                _dir = DefaultGravityPath;
            }
            bool truncate = Nmax >= 0 || Mmax >= 0;

            if (truncate)
            {
                if (Nmax >= 0 && Mmax < 0)
                {
                    Mmax = Nmax;
                }
                if (Nmax < 0)
                {
                    Nmax = int.MaxValue;
                }
                if (Mmax < 0)
                {
                    Mmax = int.MaxValue;
                }
            }
            ReadMetadata(_name, ref _filename, ref _name, ref _description, ref _date, ref _amodel, ref _GMmodel,
                         ref _zeta0, ref _corrmult, ref _norm, ref _id, ref _earth);

            double[] _Cx, _Sx;
            string   coeff = _filename + ".cof";

            using (var coeffstr = File.OpenRead(coeff))
            {
                Span <byte> id = stackalloc byte[idlength_];
                if (coeffstr.Read(id) != idlength_)
                {
                    throw new GeographicException("No header in " + coeff);
                }
                if (MemoryMarshal.Cast <char, byte>(_id.AsSpan()).SequenceEqual(id))
                {
                    throw new GeographicException($"ID mismatch: {_id} vs {Encoding.ASCII.GetString(id.ToArray())}");
                }
                int N = 0, M = 0;
                if (truncate)
                {
                    N = Nmax; M = Mmax;
                }

                var scoeff = SphericalEngine.Coeff.FromStream(coeffstr, ref N, ref M, truncate);
                _Cx = new double[scoeff.Cnm.Length];
                _Sx = new double[scoeff.Snm.Length];
                scoeff.Cnm.CopyTo(_Cx);
                scoeff.Snm.CopyTo(_Sx);

                if (!(N >= 0 && M >= 0))
                {
                    throw new GeographicException("Degree and order must be at least 0");
                }
                if (_Cx[0] != 0)
                {
                    throw new GeographicException("The degree 0 term should be zero");
                }

                _Cx[0]         = 1;       // Include the 1/r term in the sum
                _gravitational = new SphericalHarmonic(_Cx, _Sx, N, N, M, _amodel, _norm);
                if (truncate)
                {
                    N = Nmax; M = Mmax;
                }


                scoeff = SphericalEngine.Coeff.FromStream(coeffstr, ref N, ref M, truncate);
                double[] _CC, _CS;
                if (N < 0)
                {
                    N   = M = 0;
                    _CC = new[] { 0d };
                }
                else
                {
                    _CC = new double[scoeff.Cnm.Length];
                    scoeff.Cnm.CopyTo(_CC);
                }

                _CS = new double[scoeff.Snm.Length];
                scoeff.Snm.CopyTo(_CS);

                _CC[0]     += _zeta0 / _corrmult;
                _correction = new SphericalHarmonic(_CC, _CS, N, N, M, 1, _norm);
                var pos = (int)coeffstr.Position;
                coeffstr.Seek(0, SeekOrigin.End);
                if (pos != coeffstr.Position)
                {
                    throw new GeographicException("Extra data in " + coeff);
                }
            }
            int nmx = _gravitational.Coefficients.Nmx;

            _nmx = Max(nmx, _correction.Coefficients.Nmx);
            _mmx = Max(_gravitational.Coefficients.Mmx,
                       _correction.Coefficients.Mmx);
            // Adjust the normalization of the normal potential to match the model.
            var mult  = _earth._GM / _GMmodel;
            var amult = Sq(_earth._a / _amodel);
            // The 0th term in _zonal should be is 1 + _dzonal0.  Instead set it to 1
            // to give exact cancellation with the (0,0) term in the model and account
            // for _dzonal0 separately.
            var _zonal = new List <double> {
                1
            };

            _dzonal0 = (_earth.MassConstant - _GMmodel) / _GMmodel;
            for (int n = 2; n <= nmx; n += 2)
            {
                // Only include as many normal zonal terms as matter.  Figuring the limit
                // in this way works because the coefficients of the normal potential
                // (which is smooth) decay much more rapidly that the corresponding
                // coefficient of the model potential (which is bumpy).  Typically this
                // goes out to n = 18.
                mult *= amult;
                double
                    r = _Cx[n],                                // the model term
                    s = -mult *_earth.Jn(n) / Sqrt(2 * n + 1), // the normal term
                    t = r - s;                                 // the difference

                if (t == r)                                    // the normal term is negligible
                {
                    break;
                }
                _zonal.Add(0);      // index = n - 1; the odd terms are 0
                _zonal.Add(s);
            }
            int nmx1 = _zonal.Count - 1;
            var za   = _zonal.ToArray();

            _disturbing = new SphericalHarmonic1(_Cx, _Sx,
                                                 _gravitational.Coefficients.N,
                                                 nmx, _gravitational.Coefficients.Mmx,
                                                 za,
                                                 za, // This is not accessed!
                                                 nmx1, nmx1, 0,
                                                 _amodel,
                                                 _norm);
        }
Exemplo n.º 10
0
 public unsafe Span <T> GetDataBuffer <T>() where T : struct
 {
     return(MemoryMarshal.Cast <byte, T>(new Span <byte>(this.DataBufferPtr.ToPointer(), _byteCount)));
 }
Exemplo n.º 11
0
        private static void Compress(ReadOnlySpan <byte> data, Span <ulong> state, ulong ivLow, ulong ivHigh, ulong finalizer)
        {
            var dataAsULong = MemoryMarshal.Cast <byte, ulong>(data);
            var d0          = dataAsULong[0];
            var d1          = dataAsULong[1];
            var d2          = dataAsULong[2];
            var d3          = dataAsULong[3];
            var d4          = dataAsULong[4];
            var d5          = dataAsULong[5];
            var d6          = dataAsULong[6];
            var d7          = dataAsULong[7];
            var d8          = dataAsULong[8];
            var d9          = dataAsULong[9];
            var dA          = dataAsULong[10];
            var dB          = dataAsULong[11];
            var dC          = dataAsULong[12];
            var dD          = dataAsULong[13];
            var dE          = dataAsULong[14];
            var dF          = dataAsULong[15];
            var t0          = state[0];
            var t1          = state[1];
            var t2          = state[2];
            var t3          = state[3];
            var t4          = state[4];
            var t5          = state[5];
            var t6          = state[6];
            var t7          = state[7];
            var t8          = IV0;
            var t9          = IV1;
            var tA          = IV2;
            var tB          = IV3;
            var tC          = (IV4 ^ ivLow);
            var tD          = (IV5 ^ ivHigh);
            var tE          = finalizer;
            var tF          = IV7;

            // round 0
            t0 += t4;
            t0 += d0;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 32);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 24);
            t0 += t4;
            t0 += d1;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 16);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 63);

            t1 += t5;
            t1 += d2;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 32);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 24);
            t1 += t5;
            t1 += d3;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 16);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 63);

            t2 += t6;
            t2 += d4;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 32);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 24);
            t2 += t6;
            t2 += d5;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 16);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 63);

            t3 += t7;
            t3 += d6;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 32);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 24);
            t3 += t7;
            t3 += d7;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 16);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 63);

            t0 += t5;
            t0 += d8;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 32);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 24);
            t0 += t5;
            t0 += d9;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 16);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 63);

            t1 += t6;
            t1 += dA;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 32);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 24);
            t1 += t6;
            t1 += dB;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 16);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 63);

            t2 += t7;
            t2 += dC;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 32);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 24);
            t2 += t7;
            t2 += dD;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 16);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 63);

            t3 += t4;
            t3 += dE;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 32);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 24);
            t3 += t4;
            t3 += dF;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 16);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 63);

            // round 1
            t0 += t4;
            t0 += dE;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 32);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 24);
            t0 += t4;
            t0 += dA;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 16);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 63);

            t1 += t5;
            t1 += d4;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 32);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 24);
            t1 += t5;
            t1 += d8;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 16);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 63);

            t2 += t6;
            t2 += d9;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 32);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 24);
            t2 += t6;
            t2 += dF;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 16);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 63);

            t3 += t7;
            t3 += dD;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 32);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 24);
            t3 += t7;
            t3 += d6;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 16);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 63);

            t0 += t5;
            t0 += d1;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 32);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 24);
            t0 += t5;
            t0 += dC;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 16);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 63);

            t1 += t6;
            t1 += d0;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 32);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 24);
            t1 += t6;
            t1 += d2;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 16);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 63);

            t2 += t7;
            t2 += dB;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 32);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 24);
            t2 += t7;
            t2 += d7;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 16);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 63);

            t3 += t4;
            t3 += d5;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 32);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 24);
            t3 += t4;
            t3 += d3;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 16);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 63);

            // round 2
            t0 += t4;
            t0 += dB;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 32);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 24);
            t0 += t4;
            t0 += d8;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 16);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 63);

            t1 += t5;
            t1 += dC;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 32);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 24);
            t1 += t5;
            t1 += d0;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 16);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 63);

            t2 += t6;
            t2 += d5;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 32);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 24);
            t2 += t6;
            t2 += d2;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 16);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 63);

            t3 += t7;
            t3 += dF;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 32);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 24);
            t3 += t7;
            t3 += dD;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 16);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 63);

            t0 += t5;
            t0 += dA;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 32);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 24);
            t0 += t5;
            t0 += dE;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 16);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 63);

            t1 += t6;
            t1 += d3;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 32);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 24);
            t1 += t6;
            t1 += d6;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 16);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 63);

            t2 += t7;
            t2 += d7;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 32);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 24);
            t2 += t7;
            t2 += d1;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 16);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 63);

            t3 += t4;
            t3 += d9;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 32);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 24);
            t3 += t4;
            t3 += d4;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 16);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 63);

            // round 3
            t0 += t4;
            t0 += d7;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 32);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 24);
            t0 += t4;
            t0 += d9;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 16);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 63);

            t1 += t5;
            t1 += d3;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 32);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 24);
            t1 += t5;
            t1 += d1;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 16);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 63);

            t2 += t6;
            t2 += dD;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 32);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 24);
            t2 += t6;
            t2 += dC;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 16);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 63);

            t3 += t7;
            t3 += dB;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 32);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 24);
            t3 += t7;
            t3 += dE;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 16);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 63);

            t0 += t5;
            t0 += d2;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 32);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 24);
            t0 += t5;
            t0 += d6;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 16);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 63);

            t1 += t6;
            t1 += d5;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 32);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 24);
            t1 += t6;
            t1 += dA;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 16);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 63);

            t2 += t7;
            t2 += d4;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 32);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 24);
            t2 += t7;
            t2 += d0;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 16);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 63);

            t3 += t4;
            t3 += dF;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 32);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 24);
            t3 += t4;
            t3 += d8;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 16);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 63);

            // round 4
            t0 += t4;
            t0 += d9;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 32);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 24);
            t0 += t4;
            t0 += d0;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 16);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 63);

            t1 += t5;
            t1 += d5;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 32);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 24);
            t1 += t5;
            t1 += d7;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 16);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 63);

            t2 += t6;
            t2 += d2;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 32);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 24);
            t2 += t6;
            t2 += d4;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 16);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 63);

            t3 += t7;
            t3 += dA;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 32);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 24);
            t3 += t7;
            t3 += dF;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 16);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 63);

            t0 += t5;
            t0 += dE;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 32);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 24);
            t0 += t5;
            t0 += d1;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 16);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 63);

            t1 += t6;
            t1 += dB;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 32);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 24);
            t1 += t6;
            t1 += dC;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 16);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 63);

            t2 += t7;
            t2 += d6;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 32);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 24);
            t2 += t7;
            t2 += d8;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 16);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 63);

            t3 += t4;
            t3 += d3;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 32);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 24);
            t3 += t4;
            t3 += dD;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 16);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 63);

            // round 5
            t0 += t4;
            t0 += d2;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 32);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 24);
            t0 += t4;
            t0 += dC;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 16);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 63);

            t1 += t5;
            t1 += d6;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 32);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 24);
            t1 += t5;
            t1 += dA;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 16);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 63);

            t2 += t6;
            t2 += d0;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 32);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 24);
            t2 += t6;
            t2 += dB;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 16);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 63);

            t3 += t7;
            t3 += d8;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 32);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 24);
            t3 += t7;
            t3 += d3;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 16);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 63);

            t0 += t5;
            t0 += d4;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 32);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 24);
            t0 += t5;
            t0 += dD;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 16);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 63);

            t1 += t6;
            t1 += d7;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 32);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 24);
            t1 += t6;
            t1 += d5;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 16);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 63);

            t2 += t7;
            t2 += dF;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 32);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 24);
            t2 += t7;
            t2 += dE;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 16);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 63);

            t3 += t4;
            t3 += d1;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 32);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 24);
            t3 += t4;
            t3 += d9;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 16);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 63);

            // round 6
            t0 += t4;
            t0 += dC;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 32);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 24);
            t0 += t4;
            t0 += d5;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 16);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 63);

            t1 += t5;
            t1 += d1;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 32);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 24);
            t1 += t5;
            t1 += dF;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 16);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 63);

            t2 += t6;
            t2 += dE;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 32);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 24);
            t2 += t6;
            t2 += dD;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 16);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 63);

            t3 += t7;
            t3 += d4;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 32);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 24);
            t3 += t7;
            t3 += dA;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 16);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 63);

            t0 += t5;
            t0 += d0;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 32);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 24);
            t0 += t5;
            t0 += d7;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 16);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 63);

            t1 += t6;
            t1 += d6;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 32);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 24);
            t1 += t6;
            t1 += d3;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 16);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 63);

            t2 += t7;
            t2 += d9;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 32);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 24);
            t2 += t7;
            t2 += d2;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 16);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 63);

            t3 += t4;
            t3 += d8;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 32);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 24);
            t3 += t4;
            t3 += dB;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 16);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 63);

            // round 7
            t0 += t4;
            t0 += dD;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 32);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 24);
            t0 += t4;
            t0 += dB;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 16);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 63);

            t1 += t5;
            t1 += d7;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 32);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 24);
            t1 += t5;
            t1 += dE;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 16);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 63);

            t2 += t6;
            t2 += dC;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 32);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 24);
            t2 += t6;
            t2 += d1;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 16);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 63);

            t3 += t7;
            t3 += d3;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 32);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 24);
            t3 += t7;
            t3 += d9;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 16);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 63);

            t0 += t5;
            t0 += d5;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 32);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 24);
            t0 += t5;
            t0 += d0;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 16);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 63);

            t1 += t6;
            t1 += dF;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 32);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 24);
            t1 += t6;
            t1 += d4;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 16);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 63);

            t2 += t7;
            t2 += d8;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 32);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 24);
            t2 += t7;
            t2 += d6;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 16);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 63);

            t3 += t4;
            t3 += d2;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 32);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 24);
            t3 += t4;
            t3 += dA;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 16);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 63);

            // round 8
            t0 += t4;
            t0 += d6;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 32);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 24);
            t0 += t4;
            t0 += dF;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 16);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 63);

            t1 += t5;
            t1 += dE;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 32);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 24);
            t1 += t5;
            t1 += d9;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 16);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 63);

            t2 += t6;
            t2 += dB;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 32);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 24);
            t2 += t6;
            t2 += d3;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 16);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 63);

            t3 += t7;
            t3 += d0;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 32);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 24);
            t3 += t7;
            t3 += d8;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 16);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 63);

            t0 += t5;
            t0 += dC;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 32);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 24);
            t0 += t5;
            t0 += d2;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 16);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 63);

            t1 += t6;
            t1 += dD;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 32);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 24);
            t1 += t6;
            t1 += d7;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 16);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 63);

            t2 += t7;
            t2 += d1;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 32);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 24);
            t2 += t7;
            t2 += d4;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 16);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 63);

            t3 += t4;
            t3 += dA;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 32);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 24);
            t3 += t4;
            t3 += d5;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 16);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 63);

            // round 9
            t0 += t4;
            t0 += dA;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 32);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 24);
            t0 += t4;
            t0 += d2;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 16);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 63);

            t1 += t5;
            t1 += d8;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 32);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 24);
            t1 += t5;
            t1 += d4;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 16);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 63);

            t2 += t6;
            t2 += d7;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 32);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 24);
            t2 += t6;
            t2 += d6;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 16);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 63);

            t3 += t7;
            t3 += d1;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 32);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 24);
            t3 += t7;
            t3 += d5;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 16);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 63);

            t0 += t5;
            t0 += dF;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 32);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 24);
            t0 += t5;
            t0 += dB;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 16);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 63);

            t1 += t6;
            t1 += d9;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 32);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 24);
            t1 += t6;
            t1 += dE;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 16);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 63);

            t2 += t7;
            t2 += d3;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 32);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 24);
            t2 += t7;
            t2 += dC;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 16);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 63);

            t3 += t4;
            t3 += dD;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 32);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 24);
            t3 += t4;
            t3 += d0;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 16);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 63);

            // round 10
            t0 += t4;
            t0 += d0;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 32);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 24);
            t0 += t4;
            t0 += d1;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 16);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 63);

            t1 += t5;
            t1 += d2;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 32);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 24);
            t1 += t5;
            t1 += d3;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 16);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 63);

            t2 += t6;
            t2 += d4;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 32);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 24);
            t2 += t6;
            t2 += d5;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 16);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 63);

            t3 += t7;
            t3 += d6;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 32);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 24);
            t3 += t7;
            t3 += d7;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 16);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 63);

            t0 += t5;
            t0 += d8;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 32);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 24);
            t0 += t5;
            t0 += d9;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 16);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 63);

            t1 += t6;
            t1 += dA;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 32);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 24);
            t1 += t6;
            t1 += dB;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 16);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 63);

            t2 += t7;
            t2 += dC;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 32);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 24);
            t2 += t7;
            t2 += dD;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 16);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 63);

            t3 += t4;
            t3 += dE;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 32);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 24);
            t3 += t4;
            t3 += dF;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 16);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 63);

            // round 11
            t0 += t4;
            t0 += dE;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 32);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 24);
            t0 += t4;
            t0 += dA;
            tC ^= t0;
            tC  = Operations.RotateRight(tC, 16);
            t8 += tC;
            t4 ^= t8;
            t4  = Operations.RotateRight(t4, 63);

            t1 += t5;
            t1 += d4;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 32);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 24);
            t1 += t5;
            t1 += d8;
            tD ^= t1;
            tD  = Operations.RotateRight(tD, 16);
            t9 += tD;
            t5 ^= t9;
            t5  = Operations.RotateRight(t5, 63);

            t2 += t6;
            t2 += d9;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 32);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 24);
            t2 += t6;
            t2 += dF;
            tE ^= t2;
            tE  = Operations.RotateRight(tE, 16);
            tA += tE;
            t6 ^= tA;
            t6  = Operations.RotateRight(t6, 63);

            t3 += t7;
            t3 += dD;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 32);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 24);
            t3 += t7;
            t3 += d6;
            tF ^= t3;
            tF  = Operations.RotateRight(tF, 16);
            tB += tF;
            t7 ^= tB;
            t7  = Operations.RotateRight(t7, 63);

            t0 += t5;
            t0 += d1;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 32);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 24);
            t0 += t5;
            t0 += dC;
            tF ^= t0;
            tF  = Operations.RotateRight(tF, 16);
            tA += tF;
            t5 ^= tA;
            t5  = Operations.RotateRight(t5, 63);

            t1 += t6;
            t1 += d0;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 32);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 24);
            t1 += t6;
            t1 += d2;
            tC ^= t1;
            tC  = Operations.RotateRight(tC, 16);
            tB += tC;
            t6 ^= tB;
            t6  = Operations.RotateRight(t6, 63);

            t2 += t7;
            t2 += dB;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 32);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 24);
            t2 += t7;
            t2 += d7;
            tD ^= t2;
            tD  = Operations.RotateRight(tD, 16);
            t8 += tD;
            t7 ^= t8;
            t7  = Operations.RotateRight(t7, 63);

            t3 += t4;
            t3 += d5;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 32);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 24);
            t3 += t4;
            t3 += d3;
            tE ^= t3;
            tE  = Operations.RotateRight(tE, 16);
            t9 += tE;
            t4 ^= t9;
            t4  = Operations.RotateRight(t4, 63);

            state[0] ^= (t0 ^ t8);
            state[1] ^= (t1 ^ t9);
            state[2] ^= (t2 ^ tA);
            state[3] ^= (t3 ^ tB);
            state[4] ^= (t4 ^ tC);
            state[5] ^= (t5 ^ tD);
            state[6] ^= (t6 ^ tE);
            state[7] ^= (t7 ^ tF);
        }
Exemplo n.º 12
0
    public override void _Ready()
    {
        base._Ready();


        var mesh    = GD.Load <Mesh>("res://asset/fish/Fish1.obj");
        var shader  = GD.Load <Shader>("res://asset/fish/fish1.shader");
        var diffuse = GD.Load <Texture>("res://asset/fish/Fish1-diffuse_base.png");

        shader.SetDefaultTextureParam("texture_albedo", diffuse);


        var mm = new MultiMesh();

        mm.TransformFormat  = MultiMesh.TransformFormatEnum.Transform3d;
        mm.ColorFormat      = MultiMesh.ColorFormatEnum.None;
        mm.CustomDataFormat = MultiMesh.CustomDataFormatEnum.None;


        this.Multimesh = new MultiMesh();
        this.Multimesh.TransformFormat  = MultiMesh.TransformFormatEnum.Transform3d;
        this.Multimesh.CustomDataFormat = MultiMesh.CustomDataFormatEnum.Float;
        this.Multimesh.Mesh             = mesh;
        //set shader
        {
            var shadMat = new ShaderMaterial();
            shadMat.Shader = shader;
            shadMat.SetShaderParam("texture_albedo", diffuse);
            this.MaterialOverride = shadMat;
        }
        this.Multimesh.InstanceCount = 10000;          //need to do this after setting the TransformFormat
        //mm.InstanceCount = 1000;
        //mm.VisibleInstanceCount = -1;


        //set initial placement
        var visibleCount = this.Multimesh.VisibleInstanceCount;

        if (visibleCount == -1)
        {
            visibleCount = this.Multimesh.InstanceCount;
        }

        //this vector3 array is actually a transform array (4 vector 3's)
        xforms = this.Multimesh.TransformArray;
        //so lets cast it into that!
        var spanV3 = new Span <Vector3>(xforms);
        var spanXf = MemoryMarshal.Cast <Vector3, Transform>(spanV3);


        //put all the fish into a grid
        {
            var seperationDistance = 5;
            var dim = Math.Pow(visibleCount, 1.0 / 3);
            GD.Print($"visibleCount={visibleCount}, xformsLen={xforms.Length}, dim={dim}");
            var i = 0;
            for (var x = 0; x < dim; x++)
            {
                for (var z = 0; z < dim; z++)
                {
                    for (var y = 0; y < dim; y++)
                    {
                        if (i >= visibleCount)
                        {
                            break;                             //need this because of rounding with our dim variable.
                        }
                        var loc = new Vector3((x * seperationDistance), y * seperationDistance, z * seperationDistance);
                        spanXf[i].origin = loc;                         //loc offset 3 is the position in a transform.  we could assign directly to array like this: //xforms[(i * 4) + 3] = loc;
                        //this.Multimesh.SetInstanceTransform(i, new Transform(Basis.Identity, loc)); //instead of updating 1 at a time, we update all after the loop is done.
                        i++;
                    }
                }
            }
        }
        this.Multimesh.TransformArray = xforms;
    }
Exemplo n.º 13
0
        public bool Open(IFilter imageFilter)
        {
            Stream imageStream = imageFilter.GetDataForkStream();

            byte[] header = new byte[512];
            byte[] footer;

            imageStream.Seek(0, SeekOrigin.Begin);
            imageStream.Read(header, 0, 512);

            if (imageStream.Length % 2 == 0)
            {
                footer = new byte[512];
                imageStream.Seek(-512, SeekOrigin.End);
                imageStream.Read(footer, 0, 512);
            }
            else
            {
                footer = new byte[511];
                imageStream.Seek(-511, SeekOrigin.End);
                imageStream.Read(footer, 0, 511);
            }

            uint  headerChecksum = BigEndianBitConverter.ToUInt32(header, 0x40);
            uint  footerChecksum = BigEndianBitConverter.ToUInt32(footer, 0x40);
            ulong headerCookie   = BigEndianBitConverter.ToUInt64(header, 0);
            ulong footerCookie   = BigEndianBitConverter.ToUInt64(footer, 0);

            header[0x40] = 0;
            header[0x41] = 0;
            header[0x42] = 0;
            header[0x43] = 0;
            footer[0x40] = 0;
            footer[0x41] = 0;
            footer[0x42] = 0;
            footer[0x43] = 0;

            uint headerCalculatedChecksum = VhdChecksum(header);
            uint footerCalculatedChecksum = VhdChecksum(footer);

            AaruConsole.DebugWriteLine("VirtualPC plugin", "Header checksum = 0x{0:X8}, calculated = 0x{1:X8}",
                                       headerChecksum, headerCalculatedChecksum);

            AaruConsole.DebugWriteLine("VirtualPC plugin", "Header checksum = 0x{0:X8}, calculated = 0x{1:X8}",
                                       footerChecksum, footerCalculatedChecksum);

            byte[] usableHeader;
            uint   usableChecksum;

            if (headerCookie == IMAGE_COOKIE &&
                headerChecksum == headerCalculatedChecksum)
            {
                usableHeader   = header;
                usableChecksum = headerChecksum;
            }
            else if (footerCookie == IMAGE_COOKIE &&
                     footerChecksum == footerCalculatedChecksum)
            {
                usableHeader   = footer;
                usableChecksum = footerChecksum;
            }
            else
            {
                throw new
                      ImageNotSupportedException("(VirtualPC plugin): Both header and footer are corrupt, image cannot be opened.");
            }

            thisFooter = new HardDiskFooter
            {
                Cookie             = BigEndianBitConverter.ToUInt64(usableHeader, 0x00),
                Features           = BigEndianBitConverter.ToUInt32(usableHeader, 0x08),
                Version            = BigEndianBitConverter.ToUInt32(usableHeader, 0x0C),
                Offset             = BigEndianBitConverter.ToUInt64(usableHeader, 0x10),
                Timestamp          = BigEndianBitConverter.ToUInt32(usableHeader, 0x18),
                CreatorApplication = BigEndianBitConverter.ToUInt32(usableHeader, 0x1C),
                CreatorVersion     = BigEndianBitConverter.ToUInt32(usableHeader, 0x20),
                CreatorHostOs      = BigEndianBitConverter.ToUInt32(usableHeader, 0x24),
                OriginalSize       = BigEndianBitConverter.ToUInt64(usableHeader, 0x28),
                CurrentSize        = BigEndianBitConverter.ToUInt64(usableHeader, 0x30),
                DiskGeometry       = BigEndianBitConverter.ToUInt32(usableHeader, 0x38),
                DiskType           = BigEndianBitConverter.ToUInt32(usableHeader, 0x3C), Checksum = usableChecksum,
                UniqueId           = BigEndianBitConverter.ToGuid(usableHeader, 0x44), SavedState = usableHeader[0x54],
                Reserved           = new byte[usableHeader.Length - 0x55]
            };

            Array.Copy(usableHeader, 0x55, thisFooter.Reserved, 0, usableHeader.Length - 0x55);

            thisDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);
            thisDateTime = thisDateTime.AddSeconds(thisFooter.Timestamp);

            var sha1Ctx = new Sha1Context();

            sha1Ctx.Update(thisFooter.Reserved);

            AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.cookie = 0x{0:X8}", thisFooter.Cookie);
            AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.features = 0x{0:X8}", thisFooter.Features);
            AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.version = 0x{0:X8}", thisFooter.Version);
            AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.offset = {0}", thisFooter.Offset);

            AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.timestamp = 0x{0:X8} ({1})", thisFooter.Timestamp,
                                       thisDateTime);

            AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.creatorApplication = 0x{0:X8} (\"{1}\")",
                                       thisFooter.CreatorApplication,
                                       Encoding.ASCII.GetString(BigEndianBitConverter.GetBytes(thisFooter.
                                                                                               CreatorApplication)));

            AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.creatorVersion = 0x{0:X8}",
                                       thisFooter.CreatorVersion);

            AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.creatorHostOS = 0x{0:X8} (\"{1}\")",
                                       thisFooter.CreatorHostOs,
                                       Encoding.ASCII.GetString(BigEndianBitConverter.
                                                                GetBytes(thisFooter.CreatorHostOs)));

            AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.originalSize = {0}", thisFooter.OriginalSize);
            AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.currentSize = {0}", thisFooter.CurrentSize);

            AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.diskGeometry = 0x{0:X8} (C/H/S: {1}/{2}/{3})",
                                       thisFooter.DiskGeometry, (thisFooter.DiskGeometry & 0xFFFF0000) >> 16,
                                       (thisFooter.DiskGeometry & 0xFF00) >> 8,
                                       thisFooter.DiskGeometry & 0xFF);

            AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.diskType = 0x{0:X8}", thisFooter.DiskType);
            AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.checksum = 0x{0:X8}", thisFooter.Checksum);
            AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.uniqueId = {0}", thisFooter.UniqueId);
            AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.savedState = 0x{0:X2}", thisFooter.SavedState);
            AaruConsole.DebugWriteLine("VirtualPC plugin", "footer.reserved's SHA1 = 0x{0}", sha1Ctx.End());

            if (thisFooter.Version == VERSION1)
            {
                imageInfo.Version = "1.0";
            }
            else
            {
                throw new
                      ImageNotSupportedException($"(VirtualPC plugin): Unknown image type {thisFooter.DiskType} found. Please submit a bug with an example image.");
            }

            switch (thisFooter.CreatorApplication)
            {
            case CREATOR_QEMU:
            {
                imageInfo.Application = "QEMU";

                // QEMU always set same version
                imageInfo.ApplicationVersion = "Unknown";

                break;
            }

            case CREATOR_VIRTUAL_BOX:
            {
                imageInfo.ApplicationVersion =
                    $"{(thisFooter.CreatorVersion & 0xFFFF0000) >> 16}.{thisFooter.CreatorVersion & 0x0000FFFF:D2}";

                switch (thisFooter.CreatorHostOs)
                {
                case CREATOR_MACINTOSH:
                case CREATOR_MACINTOSH_OLD:
                    imageInfo.Application = "VirtualBox for Mac";

                    break;

                case CREATOR_WINDOWS:
                    // VirtualBox uses Windows creator for any other OS
                    imageInfo.Application = "VirtualBox";

                    break;

                default:
                    imageInfo.Application =
                        $"VirtualBox for unknown OS \"{Encoding.ASCII.GetString(BigEndianBitConverter.GetBytes(thisFooter.CreatorHostOs))}\"";

                    break;
                }

                break;
            }

            case CREATOR_VIRTUAL_SERVER:
            {
                imageInfo.Application = "Microsoft Virtual Server";

                switch (thisFooter.CreatorVersion)
                {
                case VERSION_VIRTUAL_SERVER2004:
                    imageInfo.ApplicationVersion = "2004";

                    break;

                default:
                    imageInfo.ApplicationVersion = $"Unknown version 0x{thisFooter.CreatorVersion:X8}";

                    break;
                }

                break;
            }

            case CREATOR_VIRTUAL_PC:
            {
                switch (thisFooter.CreatorHostOs)
                {
                case CREATOR_MACINTOSH:
                case CREATOR_MACINTOSH_OLD:
                    switch (thisFooter.CreatorVersion)
                    {
                    case VERSION_VIRTUAL_PC_MAC:
                        imageInfo.Application        = "Connectix Virtual PC";
                        imageInfo.ApplicationVersion = "5, 6 or 7";

                        break;

                    default:
                        imageInfo.ApplicationVersion = $"Unknown version 0x{thisFooter.CreatorVersion:X8}";

                        break;
                    }

                    break;

                case CREATOR_WINDOWS:
                    switch (thisFooter.CreatorVersion)
                    {
                    case VERSION_VIRTUAL_PC_MAC:
                        imageInfo.Application        = "Connectix Virtual PC";
                        imageInfo.ApplicationVersion = "5, 6 or 7";

                        break;

                    case VERSION_VIRTUAL_PC2004:
                        imageInfo.Application        = "Microsoft Virtual PC";
                        imageInfo.ApplicationVersion = "2004";

                        break;

                    case VERSION_VIRTUAL_PC2007:
                        imageInfo.Application        = "Microsoft Virtual PC";
                        imageInfo.ApplicationVersion = "2007";

                        break;

                    default:
                        imageInfo.ApplicationVersion = $"Unknown version 0x{thisFooter.CreatorVersion:X8}";

                        break;
                    }

                    break;

                default:
                    imageInfo.Application =
                        $"Virtual PC for unknown OS \"{Encoding.ASCII.GetString(BigEndianBitConverter.GetBytes(thisFooter.CreatorHostOs))}\"";

                    imageInfo.ApplicationVersion = $"Unknown version 0x{thisFooter.CreatorVersion:X8}";

                    break;
                }

                break;
            }

            case CREATOR_DISCIMAGECHEF:
            {
                imageInfo.Application = "DiscImageChef";

                imageInfo.ApplicationVersion =
                    $"{(thisFooter.CreatorVersion & 0xFF000000) >> 24}.{(thisFooter.CreatorVersion & 0xFF0000) >> 16}.{(thisFooter.CreatorVersion & 0xFF00) >> 8}.{thisFooter.CreatorVersion & 0xFF}";
            }

            break;

            case CREATOR_AARU:
            {
                imageInfo.Application = "Aaru";

                imageInfo.ApplicationVersion =
                    $"{(thisFooter.CreatorVersion & 0xFF000000) >> 24}.{(thisFooter.CreatorVersion & 0xFF0000) >> 16}.{(thisFooter.CreatorVersion & 0xFF00) >> 8}.{thisFooter.CreatorVersion & 0xFF}";
            }

            break;

            default:
            {
                imageInfo.Application =
                    $"Unknown application \"{Encoding.ASCII.GetString(BigEndianBitConverter.GetBytes(thisFooter.CreatorHostOs))}\"";

                imageInfo.ApplicationVersion = $"Unknown version 0x{thisFooter.CreatorVersion:X8}";

                break;
            }
            }

            thisFilter           = imageFilter;
            imageInfo.ImageSize  = thisFooter.CurrentSize;
            imageInfo.Sectors    = thisFooter.CurrentSize / 512;
            imageInfo.SectorSize = 512;

            imageInfo.CreationTime         = imageFilter.GetCreationTime();
            imageInfo.LastModificationTime = thisDateTime;
            imageInfo.MediaTitle           = Path.GetFileNameWithoutExtension(imageFilter.GetFilename());

            imageInfo.Cylinders       = (thisFooter.DiskGeometry & 0xFFFF0000) >> 16;
            imageInfo.Heads           = (thisFooter.DiskGeometry & 0xFF00) >> 8;
            imageInfo.SectorsPerTrack = thisFooter.DiskGeometry & 0xFF;

            if (thisFooter.DiskType == TYPE_DYNAMIC ||
                thisFooter.DiskType == TYPE_DIFFERENCING)
            {
                imageStream.Seek((long)thisFooter.Offset, SeekOrigin.Begin);
                byte[] dynamicBytes = new byte[1024];
                imageStream.Read(dynamicBytes, 0, 1024);

                uint dynamicChecksum = BigEndianBitConverter.ToUInt32(dynamicBytes, 0x24);

                dynamicBytes[0x24] = 0;
                dynamicBytes[0x25] = 0;
                dynamicBytes[0x26] = 0;
                dynamicBytes[0x27] = 0;

                uint dynamicChecksumCalculated = VhdChecksum(dynamicBytes);

                AaruConsole.DebugWriteLine("VirtualPC plugin",
                                           "Dynamic header checksum = 0x{0:X8}, calculated = 0x{1:X8}", dynamicChecksum,
                                           dynamicChecksumCalculated);

                if (dynamicChecksum != dynamicChecksumCalculated)
                {
                    throw new
                          ImageNotSupportedException("(VirtualPC plugin): Both header and footer are corrupt, image cannot be opened.");
                }

                thisDynamic = new DynamicDiskHeader
                {
                    LocatorEntries = new ParentLocatorEntry[8], Reserved2 = new byte[256]
                };

                for (int i = 0; i < 8; i++)
                {
                    thisDynamic.LocatorEntries[i] = new ParentLocatorEntry();
                }

                thisDynamic.Cookie          = BigEndianBitConverter.ToUInt64(dynamicBytes, 0x00);
                thisDynamic.DataOffset      = BigEndianBitConverter.ToUInt64(dynamicBytes, 0x08);
                thisDynamic.TableOffset     = BigEndianBitConverter.ToUInt64(dynamicBytes, 0x10);
                thisDynamic.HeaderVersion   = BigEndianBitConverter.ToUInt32(dynamicBytes, 0x18);
                thisDynamic.MaxTableEntries = BigEndianBitConverter.ToUInt32(dynamicBytes, 0x1C);
                thisDynamic.BlockSize       = BigEndianBitConverter.ToUInt32(dynamicBytes, 0x20);
                thisDynamic.Checksum        = dynamicChecksum;
                thisDynamic.ParentId        = BigEndianBitConverter.ToGuid(dynamicBytes, 0x28);
                thisDynamic.ParentTimestamp = BigEndianBitConverter.ToUInt32(dynamicBytes, 0x38);
                thisDynamic.Reserved        = BigEndianBitConverter.ToUInt32(dynamicBytes, 0x3C);
                thisDynamic.ParentName      = Encoding.BigEndianUnicode.GetString(dynamicBytes, 0x40, 512);

                for (int i = 0; i < 8; i++)
                {
                    thisDynamic.LocatorEntries[i].PlatformCode =
                        BigEndianBitConverter.ToUInt32(dynamicBytes, 0x240 + 0x00 + (24 * i));

                    thisDynamic.LocatorEntries[i].PlatformDataSpace =
                        BigEndianBitConverter.ToUInt32(dynamicBytes, 0x240 + 0x04 + (24 * i));

                    thisDynamic.LocatorEntries[i].PlatformDataLength =
                        BigEndianBitConverter.ToUInt32(dynamicBytes, 0x240 + 0x08 + (24 * i));

                    thisDynamic.LocatorEntries[i].Reserved =
                        BigEndianBitConverter.ToUInt32(dynamicBytes, 0x240 + 0x0C + (24 * i));

                    thisDynamic.LocatorEntries[i].PlatformDataOffset =
                        BigEndianBitConverter.ToUInt64(dynamicBytes, 0x240 + 0x10 + (24 * i));
                }

                Array.Copy(dynamicBytes, 0x300, thisDynamic.Reserved2, 0, 256);

                parentDateTime = new DateTime(2000, 1, 1, 0, 0, 0, DateTimeKind.Utc);
                parentDateTime = parentDateTime.AddSeconds(thisDynamic.ParentTimestamp);

                sha1Ctx = new Sha1Context();
                sha1Ctx.Update(thisDynamic.Reserved2);

                AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.cookie = 0x{0:X8}", thisDynamic.Cookie);
                AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.dataOffset = {0}", thisDynamic.DataOffset);
                AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.tableOffset = {0}", thisDynamic.TableOffset);

                AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.headerVersion = 0x{0:X8}",
                                           thisDynamic.HeaderVersion);

                AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.maxTableEntries = {0}",
                                           thisDynamic.MaxTableEntries);

                AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.blockSize = {0}", thisDynamic.BlockSize);
                AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.checksum = 0x{0:X8}", thisDynamic.Checksum);
                AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.parentID = {0}", thisDynamic.ParentId);

                AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.parentTimestamp = 0x{0:X8} ({1})",
                                           thisDynamic.ParentTimestamp, parentDateTime);

                AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.reserved = 0x{0:X8}", thisDynamic.Reserved);

                for (int i = 0; i < 8; i++)
                {
                    AaruConsole.DebugWriteLine("VirtualPC plugin",
                                               "dynamic.locatorEntries[{0}].platformCode = 0x{1:X8} (\"{2}\")", i,
                                               thisDynamic.LocatorEntries[i].PlatformCode,
                                               Encoding.ASCII.GetString(BigEndianBitConverter.GetBytes(thisDynamic.
                                                                                                       LocatorEntries
                                                                                                       [i].
                                                                                                       PlatformCode)));

                    AaruConsole.DebugWriteLine("VirtualPC plugin",
                                               "dynamic.locatorEntries[{0}].platformDataSpace = {1}", i,
                                               thisDynamic.LocatorEntries[i].PlatformDataSpace);

                    AaruConsole.DebugWriteLine("VirtualPC plugin",
                                               "dynamic.locatorEntries[{0}].platformDataLength = {1}", i,
                                               thisDynamic.LocatorEntries[i].PlatformDataLength);

                    AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.locatorEntries[{0}].reserved = 0x{1:X8}", i,
                                               thisDynamic.LocatorEntries[i].Reserved);

                    AaruConsole.DebugWriteLine("VirtualPC plugin",
                                               "dynamic.locatorEntries[{0}].platformDataOffset = {1}", i,
                                               thisDynamic.LocatorEntries[i].PlatformDataOffset);
                }

                AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.parentName = \"{0}\"", thisDynamic.ParentName);
                AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.reserved2's SHA1 = 0x{0}", sha1Ctx.End());

                if (thisDynamic.HeaderVersion != VERSION1)
                {
                    throw new
                          ImageNotSupportedException($"(VirtualPC plugin): Unknown image type {thisFooter.DiskType} found. Please submit a bug with an example image.");
                }

                DateTime startTime = DateTime.UtcNow;

                blockAllocationTable = new uint[thisDynamic.MaxTableEntries];

                // How many sectors uses the BAT
                int batSectorCount = (int)Math.Ceiling(((double)thisDynamic.MaxTableEntries * 4) / 512);

                byte[] bat = new byte[thisDynamic.MaxTableEntries * 4];
                imageStream.Seek((long)thisDynamic.TableOffset, SeekOrigin.Begin);
                imageStream.Read(bat, 0, batSectorCount);

                ReadOnlySpan <byte> span = bat;

                blockAllocationTable =
                    MemoryMarshal.Cast <byte, uint>(span).Slice(0, (int)thisDynamic.MaxTableEntries).ToArray();

                for (int i = 0; i < blockAllocationTable.Length; i++)
                {
                    blockAllocationTable[i] = Swapping.Swap(blockAllocationTable[i]);
                }

                DateTime endTime = DateTime.UtcNow;

                AaruConsole.DebugWriteLine("VirtualPC plugin", "Filling the BAT took {0} seconds",
                                           (endTime - startTime).TotalSeconds);

                bitmapSize = (uint)Math.Ceiling((double)thisDynamic.BlockSize / 512

                                                // 1 bit per sector on the bitmap
                                                / 8

                                                // and aligned to 512 byte boundary
                                                / 512);

                AaruConsole.DebugWriteLine("VirtualPC plugin", "Bitmap is {0} sectors", bitmapSize);
            }

            imageInfo.XmlMediaType = XmlMediaType.BlockMedia;

            switch (thisFooter.DiskType)
            {
            case TYPE_FIXED:
            case TYPE_DYNAMIC:
            {
                // Nothing to do here, really.
                return(true);
            }

            case TYPE_DIFFERENCING:
            {
                locatorEntriesData = new byte[8][];

                for (int i = 0; i < 8; i++)
                {
                    if (thisDynamic.LocatorEntries[i].PlatformCode != 0x00000000)
                    {
                        locatorEntriesData[i] = new byte[thisDynamic.LocatorEntries[i].PlatformDataLength];
                        imageStream.Seek((long)thisDynamic.LocatorEntries[i].PlatformDataOffset, SeekOrigin.Begin);

                        imageStream.Read(locatorEntriesData[i], 0,
                                         (int)thisDynamic.LocatorEntries[i].PlatformDataLength);

                        switch (thisDynamic.LocatorEntries[i].PlatformCode)
                        {
                        case PLATFORM_CODE_WINDOWS_ABSOLUTE:
                        case PLATFORM_CODE_WINDOWS_RELATIVE:
                            AaruConsole.DebugWriteLine("VirtualPC plugin",
                                                       "dynamic.locatorEntries[{0}] = \"{1}\"", i,
                                                       Encoding.ASCII.GetString(locatorEntriesData[i]));

                            break;

                        case PLATFORM_CODE_WINDOWS_ABSOLUTE_U:
                        case PLATFORM_CODE_WINDOWS_RELATIVE_U:
                            AaruConsole.DebugWriteLine("VirtualPC plugin",
                                                       "dynamic.locatorEntries[{0}] = \"{1}\"", i,
                                                       Encoding.BigEndianUnicode.
                                                       GetString(locatorEntriesData[i]));

                            break;

                        case PLATFORM_CODE_MACINTOSH_URI:
                            AaruConsole.DebugWriteLine("VirtualPC plugin",
                                                       "dynamic.locatorEntries[{0}] = \"{1}\"", i,
                                                       Encoding.UTF8.GetString(locatorEntriesData[i]));

                            break;

                        default:
                            AaruConsole.DebugWriteLine("VirtualPC plugin", "dynamic.locatorEntries[{0}] =", i);
                            PrintHex.PrintHexArray(locatorEntriesData[i], 64);

                            break;
                        }
                    }
                }

                int    currentLocator = 0;
                bool   locatorFound   = false;
                string parentPath     = null;

                while (!locatorFound &&
                       currentLocator < 8)
                {
                    switch (thisDynamic.LocatorEntries[currentLocator].PlatformCode)
                    {
                    case PLATFORM_CODE_WINDOWS_ABSOLUTE:
                    case PLATFORM_CODE_WINDOWS_RELATIVE:
                        parentPath = Encoding.ASCII.GetString(locatorEntriesData[currentLocator]);

                        break;

                    case PLATFORM_CODE_WINDOWS_ABSOLUTE_U:
                    case PLATFORM_CODE_WINDOWS_RELATIVE_U:
                        parentPath = Encoding.BigEndianUnicode.GetString(locatorEntriesData[currentLocator]);

                        break;

                    case PLATFORM_CODE_MACINTOSH_URI:
                        parentPath =
                            Uri.UnescapeDataString(Encoding.UTF8.GetString(locatorEntriesData[currentLocator]));

                        if (parentPath.StartsWith("file://localhost", StringComparison.InvariantCulture))
                        {
                            parentPath = parentPath.Remove(0, 16);
                        }
                        else
                        {
                            AaruConsole.DebugWriteLine("VirtualPC plugin",
                                                       "Unsupported protocol classified found in URI parent path: \"{0}\"",
                                                       parentPath);

                            parentPath = null;
                        }

                        break;
                    }

                    if (parentPath != null)
                    {
                        AaruConsole.DebugWriteLine("VirtualPC plugin", "Possible parent path: \"{0}\"", parentPath);

                        IFilter parentFilter =
                            new FiltersList().GetFilter(Path.Combine(imageFilter.GetParentFolder(), parentPath));

                        if (parentFilter != null)
                        {
                            locatorFound = true;
                        }

                        if (!locatorFound)
                        {
                            parentPath = null;
                        }
                    }

                    currentLocator++;
                }

                if (!locatorFound)
                {
                    throw new
                          FileNotFoundException("(VirtualPC plugin): Cannot find parent file for differencing disk image");
                }

                {
                    parentImage = new Vhd();

                    IFilter parentFilter =
                        new FiltersList().GetFilter(Path.Combine(imageFilter.GetParentFolder(), parentPath));

                    if (parentFilter == null)
                    {
                        throw new ImageNotSupportedException("(VirtualPC plugin): Cannot find parent image filter");
                    }

                    /*                            PluginBase plugins = new PluginBase();
                     *                          plugins.RegisterAllPlugins();
                     *                          if (!plugins.ImagePluginsList.TryGetValue(Name.ToLower(), out parentImage))
                     *                              throw new SystemException("(VirtualPC plugin): Unable to open myself");*/

                    if (!parentImage.Identify(parentFilter))
                    {
                        throw new
                              ImageNotSupportedException("(VirtualPC plugin): Parent image is not a Virtual PC disk image");
                    }

                    if (!parentImage.Open(parentFilter))
                    {
                        throw new ImageNotSupportedException("(VirtualPC plugin): Cannot open parent disk image");
                    }

                    // While specification says that parent and child disk images should contain UUID relationship
                    // in reality it seems that old differencing disk images stored a parent UUID that, nonetheless
                    // the parent never stored itself. So the only real way to know that images are related is
                    // because the parent IS found and SAME SIZE. Ugly...
                    // More funny even, tested parent images show an empty host OS, and child images a correct one.
                    if (parentImage.Info.Sectors != imageInfo.Sectors)
                    {
                        throw new
                              ImageNotSupportedException("(VirtualPC plugin): Parent image is of different size");
                    }
                }

                return(true);
            }

            case TYPE_DEPRECATED1:
            case TYPE_DEPRECATED2:
            case TYPE_DEPRECATED3:
            {
                throw new
                      ImageNotSupportedException("(VirtualPC plugin): Deprecated image type found. Please submit a bug with an example image.");
            }

            default:
            {
                throw new
                      ImageNotSupportedException($"(VirtualPC plugin): Unknown image type {thisFooter.DiskType} found. Please submit a bug with an example image.");
            }
            }
        }
Exemplo n.º 14
0
 public static UnicodeTrieHeader Parse(ReadOnlySpan <byte> data)
 => MemoryMarshal.Cast <byte, UnicodeTrieHeader>(data)[0];
Exemplo n.º 15
0
 /// <summary>
 /// Copy image pixels to <paramref name="destination"/>.
 /// </summary>
 /// <param name="destination">The <see cref="Span{T}"/> of <see cref="byte"/> to copy image pixels to.</param>
 public void CopyPixelDataTo(Span <byte> destination) => this.GetPixelMemoryGroup().CopyTo(MemoryMarshal.Cast <byte, TPixel>(destination));
Exemplo n.º 16
0
        /// <summary>
        /// Writes the color palette to the stream. The color palette has 4 bytes for each entry.
        /// </summary>
        /// <typeparam name="TPixel">The type of the pixel.</typeparam>
        /// <param name="stream">The <see cref="Stream"/> to write to.</param>
        /// <param name="quantizedColorPalette">The color palette from the quantized image.</param>
        /// <param name="colorPalette">A temporary byte span to write the color palette to.</param>
        private void WriteColorPalette <TPixel>(Stream stream, ReadOnlySpan <TPixel> quantizedColorPalette, Span <byte> colorPalette)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            int quantizedColorBytes = quantizedColorPalette.Length * 4;

            PixelOperations <TPixel> .Instance.ToBgra32(this.configuration, quantizedColorPalette, MemoryMarshal.Cast <byte, Bgra32>(colorPalette.Slice(0, quantizedColorBytes)));

            Span <uint> colorPaletteAsUInt = MemoryMarshal.Cast <byte, uint>(colorPalette);

            for (int i = 0; i < colorPaletteAsUInt.Length; i++)
            {
                colorPaletteAsUInt[i] = colorPaletteAsUInt[i] & 0x00FFFFFF; // Padding byte, always 0.
            }

            stream.Write(colorPalette);
        }
Exemplo n.º 17
0
        /// <summary>
        /// Updates host shaders based on the guest GPU state.
        /// </summary>
        /// <param name="state">Current GPU state</param>
        private void UpdateShaderState(GpuState state)
        {
            ShaderAddresses addresses = new ShaderAddresses();

            Span <ShaderAddresses> addressesSpan = MemoryMarshal.CreateSpan(ref addresses, 1);

            Span <ulong> addressesArray = MemoryMarshal.Cast <ShaderAddresses, ulong>(addressesSpan);

            ulong baseAddress = state.Get <GpuVa>(MethodOffset.ShaderBaseAddress).Pack();

            for (int index = 0; index < 6; index++)
            {
                var shader = state.Get <ShaderState>(MethodOffset.ShaderState, index);

                if (!shader.UnpackEnable() && index != 1)
                {
                    continue;
                }

                addressesArray[index] = baseAddress + shader.Offset;
            }

            ShaderBundle gs = ShaderCache.GetGraphicsShader(state, addresses);

            _vsUsesInstanceId = gs.Shaders[0]?.Info.UsesInstanceId ?? false;

            int storageBufferBindingsCount = 0;
            int uniformBufferBindingsCount = 0;

            for (int stage = 0; stage < Constants.ShaderStages; stage++)
            {
                ShaderProgramInfo info = gs.Shaders[stage]?.Info;

                _currentProgramInfo[stage] = info;

                if (info == null)
                {
                    TextureManager.SetGraphicsTextures(stage, Array.Empty <TextureBindingInfo>());
                    TextureManager.SetGraphicsImages(stage, Array.Empty <TextureBindingInfo>());
                    BufferManager.SetGraphicsStorageBufferBindings(stage, null);
                    BufferManager.SetGraphicsUniformBufferBindings(stage, null);
                    continue;
                }

                var textureBindings = new TextureBindingInfo[info.Textures.Count];

                for (int index = 0; index < info.Textures.Count; index++)
                {
                    var descriptor = info.Textures[index];

                    Target target = ShaderTexture.GetTarget(descriptor.Type);

                    textureBindings[index] = new TextureBindingInfo(
                        target,
                        descriptor.Binding,
                        descriptor.CbufSlot,
                        descriptor.HandleIndex,
                        descriptor.Flags);
                }

                TextureManager.SetGraphicsTextures(stage, textureBindings);

                var imageBindings = new TextureBindingInfo[info.Images.Count];

                for (int index = 0; index < info.Images.Count; index++)
                {
                    var descriptor = info.Images[index];

                    Target target = ShaderTexture.GetTarget(descriptor.Type);
                    Format format = ShaderTexture.GetFormat(descriptor.Format);

                    imageBindings[index] = new TextureBindingInfo(
                        target,
                        format,
                        descriptor.Binding,
                        descriptor.CbufSlot,
                        descriptor.HandleIndex,
                        descriptor.Flags);
                }

                TextureManager.SetGraphicsImages(stage, imageBindings);

                BufferManager.SetGraphicsStorageBufferBindings(stage, info.SBuffers);
                BufferManager.SetGraphicsUniformBufferBindings(stage, info.CBuffers);

                if (info.SBuffers.Count != 0)
                {
                    storageBufferBindingsCount = Math.Max(storageBufferBindingsCount, info.SBuffers.Max(x => x.Binding) + 1);
                }

                if (info.CBuffers.Count != 0)
                {
                    uniformBufferBindingsCount = Math.Max(uniformBufferBindingsCount, info.CBuffers.Max(x => x.Binding) + 1);
                }
            }

            BufferManager.SetGraphicsStorageBufferBindingsCount(storageBufferBindingsCount);
            BufferManager.SetGraphicsUniformBufferBindingsCount(uniformBufferBindingsCount);

            _context.Renderer.Pipeline.SetProgram(gs.HostProgram);
        }
Exemplo n.º 18
0
        /// <summary>Read an array of structures from RIFF chunk</summary>
        public static void read <T>(this iRiffChunk chunk, T[] arr) where T : unmanaged
        {
            var span = MemoryMarshal.Cast <T, byte>(arr.AsSpan());

            chunk.read(span);
        }
Exemplo n.º 19
0
        public THashValue CombineHashValue(THashValue hash, string value)
        {
            ReadOnlySpan <byte> span = MemoryMarshal.Cast <char, byte>(value.AsSpan());

            return(CombineHashValue(hash, span));
        }
Exemplo n.º 20
0
        /// <summary>
        /// Given the metadata for an event and an event payload, decode and deserialize the event payload.
        /// </summary>
        internal static object[] DecodePayload(ref EventSource.EventMetadata metadata, ReadOnlySpan <byte> payload)
        {
            ParameterInfo[] parameters    = metadata.Parameters;
            object[]        decodedFields = new object[parameters.Length];
            for (int i = 0; i < parameters.Length; i++)
            {
                // It is possible that an older version of the event was emitted.
                // If this happens, the payload might be missing arguments at the end.
                // We can just leave these unset.
                if (payload.Length <= 0)
                {
                    break;
                }

                Type parameterType = parameters[i].ParameterType;
                if (parameterType == typeof(IntPtr))
                {
                    if (IntPtr.Size == 8)
                    {
                        decodedFields[i] = (IntPtr)BinaryPrimitives.ReadInt64LittleEndian(payload);
                    }
                    else
                    {
                        decodedFields[i] = (IntPtr)BinaryPrimitives.ReadInt32LittleEndian(payload);
                    }
                    payload = payload.Slice(IntPtr.Size);
                }
                else if (parameterType == typeof(int))
                {
                    decodedFields[i] = BinaryPrimitives.ReadInt32LittleEndian(payload);
                    payload          = payload.Slice(sizeof(int));
                }
                else if (parameterType == typeof(uint))
                {
                    decodedFields[i] = BinaryPrimitives.ReadUInt32LittleEndian(payload);
                    payload          = payload.Slice(sizeof(uint));
                }
                else if (parameterType == typeof(long))
                {
                    decodedFields[i] = BinaryPrimitives.ReadInt64LittleEndian(payload);
                    payload          = payload.Slice(sizeof(long));
                }
                else if (parameterType == typeof(ulong))
                {
                    decodedFields[i] = BinaryPrimitives.ReadUInt64LittleEndian(payload);
                    payload          = payload.Slice(sizeof(ulong));
                }
                else if (parameterType == typeof(byte))
                {
                    decodedFields[i] = MemoryMarshal.Read <byte>(payload);
                    payload          = payload.Slice(sizeof(byte));
                }
                else if (parameterType == typeof(sbyte))
                {
                    decodedFields[i] = MemoryMarshal.Read <sbyte>(payload);
                    payload          = payload.Slice(sizeof(sbyte));
                }
                else if (parameterType == typeof(short))
                {
                    decodedFields[i] = BinaryPrimitives.ReadInt16LittleEndian(payload);
                    payload          = payload.Slice(sizeof(short));
                }
                else if (parameterType == typeof(ushort))
                {
                    decodedFields[i] = BinaryPrimitives.ReadUInt16LittleEndian(payload);
                    payload          = payload.Slice(sizeof(ushort));
                }
                else if (parameterType == typeof(float))
                {
                    decodedFields[i] = BitConverter.Int32BitsToSingle(BinaryPrimitives.ReadInt32LittleEndian(payload));
                    payload          = payload.Slice(sizeof(float));
                }
                else if (parameterType == typeof(double))
                {
                    decodedFields[i] = BitConverter.Int64BitsToDouble(BinaryPrimitives.ReadInt64LittleEndian(payload));
                    payload          = payload.Slice(sizeof(double));
                }
                else if (parameterType == typeof(bool))
                {
                    // The manifest defines a bool as a 32bit type (WIN32 BOOL), not 1 bit as CLR Does.
                    decodedFields[i] = (BinaryPrimitives.ReadInt32LittleEndian(payload) == 1);
                    payload          = payload.Slice(sizeof(int));
                }
                else if (parameterType == typeof(Guid))
                {
                    const int sizeOfGuid = 16;
                    decodedFields[i] = new Guid(payload.Slice(0, sizeOfGuid));
                    payload          = payload.Slice(sizeOfGuid);
                }
                else if (parameterType == typeof(char))
                {
                    decodedFields[i] = (char)BinaryPrimitives.ReadUInt16LittleEndian(payload);
                    payload          = payload.Slice(sizeof(char));
                }
                else if (parameterType == typeof(string))
                {
                    // Try to find null terminator (0x00) from the byte span
                    // NOTE: we do this by hand instead of using IndexOf because payload may be unaligned due to
                    // mixture of different types being stored in the same buffer. (see eventpipe.cpp:CopyData)
                    int byteCount = -1;
                    for (int j = 1; j < payload.Length; j += 2)
                    {
                        if (payload[j - 1] == (byte)(0) && payload[j] == (byte)(0))
                        {
                            byteCount = j + 1;
                            break;
                        }
                    }

                    ReadOnlySpan <char> charPayload;
                    if (byteCount < 0)
                    {
                        charPayload = MemoryMarshal.Cast <byte, char>(payload);
                        payload     = default;
                    }
                    else
                    {
                        charPayload = MemoryMarshal.Cast <byte, char>(payload.Slice(0, byteCount - 2));
                        payload     = payload.Slice(byteCount);
                    }
                    decodedFields[i] = BitConverter.IsLittleEndian ? new string(charPayload) : Encoding.Unicode.GetString(MemoryMarshal.Cast <char, byte>(charPayload));
                }
                else
                {
                    Debug.Fail("Unsupported type encountered.");
                }
            }

            return(decodedFields);
        }
Exemplo n.º 21
0
        /// <summary>
        /// Performs actual copy of the inline data after the transfer is finished.
        /// </summary>
        private void FinishTransfer()
        {
            var memoryManager = _channel.MemoryManager;

            var data = MemoryMarshal.Cast <int, byte>(_buffer).Slice(0, _size);

            if (_isLinear && _lineCount == 1)
            {
                memoryManager.WriteTrackedResource(_dstGpuVa, data);
                _context.AdvanceSequence();
            }
            else
            {
                var dstCalculator = new OffsetCalculator(
                    _dstWidth,
                    _dstHeight,
                    _dstStride,
                    _isLinear,
                    _dstGobBlocksInY,
                    1);

                int srcOffset = 0;

                for (int y = _dstY; y < _dstY + _lineCount; y++)
                {
                    int x1      = _dstX;
                    int x2      = _dstX + _lineLengthIn;
                    int x1Round = BitUtils.AlignUp(_dstX, 16);
                    int x2Trunc = BitUtils.AlignDown(x2, 16);

                    int x = x1;

                    if (x1Round <= x2)
                    {
                        for (; x < x1Round; x++, srcOffset++)
                        {
                            int dstOffset = dstCalculator.GetOffset(x, y);

                            ulong dstAddress = _dstGpuVa + (uint)dstOffset;

                            memoryManager.Write(dstAddress, data[srcOffset]);
                        }
                    }

                    for (; x < x2Trunc; x += 16, srcOffset += 16)
                    {
                        int dstOffset = dstCalculator.GetOffset(x, y);

                        ulong dstAddress = _dstGpuVa + (uint)dstOffset;

                        memoryManager.Write(dstAddress, MemoryMarshal.Cast <byte, Vector128 <byte> >(data.Slice(srcOffset, 16))[0]);
                    }

                    for (; x < x2; x++, srcOffset++)
                    {
                        int dstOffset = dstCalculator.GetOffset(x, y);

                        ulong dstAddress = _dstGpuVa + (uint)dstOffset;

                        memoryManager.Write(dstAddress, data[srcOffset]);
                    }
                }

                _context.AdvanceSequence();
            }

            _finished = true;
        }
Exemplo n.º 22
0
        /// <summary>
        /// Deserialize object of given type
        /// </summary>
        /// <param name="t">Target deserialized type</param>
        /// <param name="stream">Stream to read from</param>
        /// <returns>Deserialized object</returns>
        /// <exception cref="ArgumentNullException"><paramref name="t"/> or <paramref name="stream"/> are null</exception>
        public static object Deserialize(Type t, Stream stream)
        {
            if (t == null)
            {
                throw new ArgumentNullException(nameof(t));
            }
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }

            if (t == typeof(sbyte))
            {
                return((sbyte)stream.ReadByteOrThrow());
            }
            if (t == typeof(byte))
            {
                return(stream.ReadByteOrThrow());
            }
            if (t == typeof(short))
            {
                return(stream.ReadS16());
            }
            if (t == typeof(ushort))
            {
                return(stream.ReadU16());
            }
            if (t == typeof(int))
            {
                return(stream.ReadS32());
            }
            if (t == typeof(uint))
            {
                return(stream.ReadU32());
            }
            if (t == typeof(long))
            {
                return(stream.ReadS64());
            }
            if (t == typeof(ulong))
            {
                return(stream.ReadU64());
            }
            if (t == typeof(float))
            {
                return(stream.ReadSingle());
            }
            if (t == typeof(double))
            {
                return(stream.ReadDouble());
            }
            if (!t.IsValueType && stream.ReadByteOrThrow() == 0)
            {
                return(null);
            }
            if (t.IsEnum)
            {
                return(Deserialize(t.GetEnumUnderlyingType(), stream));
            }
            if (t == typeof(string))
            {
                return(stream.ReadCString());
            }

            if (t.IsArray)
            {
                var count = stream.ReadS32();
                var t2    = t.GetElementType() ??
                            throw new Exception($"Element type for array not found in object of type {t}");
                var vArray = Array.CreateInstance(t2, count);
                if (vArray is sbyte[] vArrayS8)
                {
                    stream.Read(MemoryMarshal.Cast <sbyte, byte>(vArrayS8));
                }
                else if (vArray is byte[] vArrayU8)
                {
                    stream.Read(vArrayU8);
                }
                else if (vArray is short[] vArrayS16)
                {
                    if (BitConverter.IsLittleEndian)
                    {
                        stream.Read(MemoryMarshal.Cast <short, byte>(vArrayS16));
                    }
                    else
                    {
                        for (var i = 0; i < count; i++)
                        {
                            vArrayS16.SetValue(stream.ReadS16(), i);
                        }
                    }
                }
                else if (vArray is ushort[] vArrayU16)
                {
                    if (BitConverter.IsLittleEndian)
                    {
                        stream.Read(MemoryMarshal.Cast <ushort, byte>(vArrayU16));
                    }
                    else
                    {
                        for (var i = 0; i < count; i++)
                        {
                            vArrayU16.SetValue(stream.ReadU16(), i);
                        }
                    }
                }
                else if (vArray is int[] vArrayS32)
                {
                    if (BitConverter.IsLittleEndian)
                    {
                        stream.Read(MemoryMarshal.Cast <int, byte>(vArrayS32));
                    }
                    else
                    {
                        for (var i = 0; i < count; i++)
                        {
                            vArrayS32.SetValue(stream.ReadS32(), i);
                        }
                    }
                }
                else if (vArray is uint[] vArrayU32)
                {
                    if (BitConverter.IsLittleEndian)
                    {
                        stream.Read(MemoryMarshal.Cast <uint, byte>(vArrayU32));
                    }
                    else
                    {
                        for (var i = 0; i < count; i++)
                        {
                            vArrayU32.SetValue(stream.ReadU32(), i);
                        }
                    }
                }
                else if (vArray is long[] vArrayS64)
                {
                    if (BitConverter.IsLittleEndian)
                    {
                        stream.Read(MemoryMarshal.Cast <long, byte>(vArrayS64));
                    }
                    else
                    {
                        for (var i = 0; i < count; i++)
                        {
                            vArrayS64.SetValue(stream.ReadS64(), i);
                        }
                    }
                }
                else if (vArray is ulong[] vArrayU64)
                {
                    if (BitConverter.IsLittleEndian)
                    {
                        stream.Read(MemoryMarshal.Cast <ulong, byte>(vArrayU64));
                    }
                    else
                    {
                        for (var i = 0; i < count; i++)
                        {
                            vArrayU64.SetValue(stream.ReadU64(), i);
                        }
                    }
                }
                else if (vArray is float[] vArraySingle)
                {
                    stream.Read(MemoryMarshal.Cast <float, byte>(vArraySingle));
                }
                else if (vArray is double[] vArrayDouble)
                {
                    stream.Read(MemoryMarshal.Cast <double, byte>(vArrayDouble));
                }
                else
                {
                    for (var i = 0; i < count; i++)
                    {
                        vArray.SetValue(Deserialize(t2, stream), i);
                    }
                }

                return(vArray);
            }

            var res = Activator.CreateInstance(t);

            if (IsListType(t))
            {
                var vList = res as IList;
                Debug.Assert(vList != null, nameof(vList) + " != null");
                var count = stream.ReadS32();
                var t2    = t.GetGenericArguments()[0];
                for (var i = 0; i < count; i++)
                {
                    vList.Add(Deserialize(t2, stream));
                }

                return(vList);
            }

            if (IsDictionaryType(t))
            {
                var vDict = res as IDictionary;
                Debug.Assert(vDict != null, nameof(vDict) + " != null");
                var count    = stream.ReadS32();
                var typeArgs = t.GetGenericArguments();
                var t2       = typeArgs[0];
                var t3       = typeArgs[1];
                for (var i = 0; i < count; i++)
                {
                    vDict.Add(Deserialize(t2, stream), Deserialize(t3, stream));
                }

                return(vDict);
            }

            var members = t.GetMembers();

            if (t.GetCustomAttribute <CzCustomSerializeMembersAttribute>() != null)
            {
                var dict = CustomSerDict.GetOrAdd(t, GenerateCustomSerializeDict);

                int tag;
                do
                {
                    tag = stream.ReadS32();
                    if (tag != -1)
                    {
                        TryDeserializeMember(dict[tag], stream, res);
                    }
                } while (tag != -1);
            }
            else
            {
                foreach (var m in members)
                {
                    TryDeserializeMember(m, stream, res);
                }
            }

            return(res);
        }
Exemplo n.º 23
0
        /// <summary>
        /// Ensures that the texture bindings are visible to the host GPU.
        /// Note: this actually performs the binding using the host graphics API.
        /// </summary>
        /// <param name="pool">The current texture pool</param>
        /// <param name="stage">The shader stage using the textures to be bound</param>
        /// <param name="stageIndex">The stage number of the specified shader stage</param>
        private void CommitTextureBindings(TexturePool pool, ShaderStage stage, int stageIndex)
        {
            if (_textureBindings[stageIndex] == null)
            {
                return;
            }

            for (int index = 0; index < _textureBindings[stageIndex].Length; index++)
            {
                TextureBindingInfo binding = _textureBindings[stageIndex][index];

                int packedId;

                if (binding.IsBindless)
                {
                    ulong address;

                    var bufferManager = _context.Methods.BufferManager;

                    if (_isCompute)
                    {
                        address = bufferManager.GetComputeUniformBufferAddress(binding.CbufSlot);
                    }
                    else
                    {
                        address = bufferManager.GetGraphicsUniformBufferAddress(stageIndex, binding.CbufSlot);
                    }

                    packedId = MemoryMarshal.Cast <byte, int>(_context.PhysicalMemory.GetSpan(address + (ulong)binding.CbufOffset * 4, 4))[0];
                }
                else
                {
                    packedId = ReadPackedId(stageIndex, binding.Handle);
                }

                int textureId = UnpackTextureId(packedId);
                int samplerId;

                if (_samplerIndex == SamplerIndex.ViaHeaderIndex)
                {
                    samplerId = textureId;
                }
                else
                {
                    samplerId = UnpackSamplerId(packedId);
                }

                Texture texture = pool.Get(textureId);

                ITexture hostTexture = texture?.GetTargetTexture(binding.Target);

                if (_textureState[stageIndex][index].Texture != hostTexture || _rebind)
                {
                    _textureState[stageIndex][index].Texture = hostTexture;

                    _context.Renderer.Pipeline.SetTexture(CurrentShaderMeta().GetTextureUnit(stage, index), hostTexture);
                }

                Sampler sampler = _samplerPool.Get(samplerId);

                ISampler hostSampler = sampler?.HostSampler;

                if (_textureState[stageIndex][index].Sampler != hostSampler || _rebind)
                {
                    _textureState[stageIndex][index].Sampler = hostSampler;

                    _context.Renderer.Pipeline.SetSampler(CurrentShaderMeta().GetTextureUnit(stage, index), hostSampler);
                }
            }
        }
Exemplo n.º 24
0
        /// <summary>
        /// Serialize object to stream
        /// </summary>
        /// <param name="stream">Stream to write to</param>
        /// <param name="value">Object to serialize</param>
        /// <exception cref="ArgumentNullException"><paramref name="stream"/> is null</exception>
        public static void Serialize(Stream stream, object value)
        {
            if (stream == null)
            {
                throw new ArgumentNullException(nameof(stream));
            }
            if (value == null)
            {
                stream.WriteByte(0);
                return;
            }

            switch (value)
            {
            case sbyte vSByte:
                stream.WriteByte((byte)vSByte);
                return;

            case byte vByte:
                stream.WriteByte(vByte);
                return;

            case short vShort:
                vShort.WriteTo(stream);
                return;

            case ushort vUShort:
                vUShort.WriteTo(stream);
                return;

            case int vInt:
                vInt.WriteTo(stream);
                return;

            case uint vUInt:
                vUInt.WriteTo(stream);
                return;

            case long vLong:
                vLong.WriteTo(stream);
                return;

            case ulong vULong:
                vULong.WriteTo(stream);
                return;

            case float vFloat:
                vFloat.WriteTo(stream);
                return;

            case double vDouble:
                vDouble.WriteTo(stream);
                return;
            }

            var t = value.GetType();

            if (!t.IsValueType)
            {
                stream.WriteByte(1);
            }

            if (t.IsEnum)
            {
                // ReSharper disable once TailRecursiveCall
                Serialize(stream, Convert.ChangeType(value, t.GetEnumUnderlyingType(), null));
                return;
            }

            if (value is string vString)
            {
                var arr = TextEncoding.GetBytes(vString);
                stream.Write(arr, 0, arr.Length);
                stream.WriteByte(0);
                return;
            }

            if (value is Array vArray)
            {
                vArray.Length.WriteTo(stream);
                if (vArray is sbyte[] vArrayS8)
                {
                    stream.Write(MemoryMarshal.Cast <sbyte, byte>(vArrayS8));
                }
                else if (vArray is byte[] vArrayU8)
                {
                    stream.Write(vArrayU8, 0, vArray.Length);
                }
                else if (vArray is short[] vArrayS16)
                {
                    if (BitConverter.IsLittleEndian)
                    {
                        stream.Write(MemoryMarshal.Cast <short, byte>(vArrayS16));
                    }
                    else
                    {
                        foreach (var o in vArrayS16)
                        {
                            o.WriteTo(stream);
                        }
                    }
                }
                else if (vArray is ushort[] vArrayU16)
                {
                    if (BitConverter.IsLittleEndian)
                    {
                        stream.Write(MemoryMarshal.Cast <ushort, byte>(vArrayU16));
                    }
                    else
                    {
                        foreach (var o in vArrayU16)
                        {
                            o.WriteTo(stream);
                        }
                    }
                }
                else if (vArray is int[] vArrayS32)
                {
                    if (BitConverter.IsLittleEndian)
                    {
                        stream.Write(MemoryMarshal.Cast <int, byte>(vArrayS32));
                    }
                    else
                    {
                        foreach (var o in vArrayS32)
                        {
                            o.WriteTo(stream);
                        }
                    }
                }
                else if (vArray is uint[] vArrayU32)
                {
                    if (BitConverter.IsLittleEndian)
                    {
                        stream.Write(MemoryMarshal.Cast <uint, byte>(vArrayU32));
                    }
                    else
                    {
                        foreach (var o in vArrayU32)
                        {
                            o.WriteTo(stream);
                        }
                    }
                }
                else if (vArray is long[] vArrayS64)
                {
                    if (BitConverter.IsLittleEndian)
                    {
                        stream.Write(MemoryMarshal.Cast <long, byte>(vArrayS64));
                    }
                    else
                    {
                        foreach (var o in vArrayS64)
                        {
                            o.WriteTo(stream);
                        }
                    }
                }
                else if (vArray is ulong[] vArrayU64)
                {
                    if (BitConverter.IsLittleEndian)
                    {
                        stream.Write(MemoryMarshal.Cast <ulong, byte>(vArrayU64));
                    }
                    else
                    {
                        foreach (var o in vArrayU64)
                        {
                            o.WriteTo(stream);
                        }
                    }
                }
                else if (vArray is float[] vArraySingle)
                {
                    stream.Write(MemoryMarshal.Cast <float, byte>(vArraySingle));
                }
                else if (vArray is double[] vArrayDouble)
                {
                    stream.Write(MemoryMarshal.Cast <double, byte>(vArrayDouble));
                }
                else
                {
                    foreach (var o in vArray)
                    {
                        Serialize(stream, o);
                    }
                }

                return;
            }

            if (IsList(value))
            {
                var vList = value as IList;
                Debug.Assert(vList != null, nameof(vList) + " != null");
                vList.Count.WriteTo(stream);
                foreach (var o in vList)
                {
                    Serialize(stream, o);
                }
                return;
            }

            if (IsDictionary(value))
            {
                var vDict = value as IDictionary;
                Debug.Assert(vDict != null, nameof(vDict) + " != null");
                vDict.Count.WriteTo(stream);
                foreach (var o in vDict.Keys)
                {
                    Serialize(stream, o);
                    Serialize(stream, vDict[o]);
                }

                return;
            }

            var members = t.GetMembers();

            if (t.GetCustomAttribute <CzCustomSerializeMembersAttribute>() != null)
            {
                foreach (var m in members)
                {
                    if (m.GetCustomAttribute <CzSerializeAttribute>() is CzSerializeAttribute attrib)
                    {
                        TrySerializeMember(value, m, stream, attrib.Tag);
                    }
                }

                (-1).WriteTo(stream);
            }
            else
            {
                foreach (var m in members)
                {
                    TrySerializeMember(value, m, stream);
                }
            }
        }
Exemplo n.º 25
0
 /// <summary>
 /// Reads data from CPU mapped memory, with read tracking
 /// </summary>
 /// <typeparam name="T">Type of the data being read</typeparam>
 /// <param name="va">Virtual address of the data in memory</param>
 /// <returns>The data</returns>
 public T ReadTracked <T>(ulong va) where T : unmanaged
 {
     SignalMemoryTracking(va, (ulong)Unsafe.SizeOf <T>(), false);
     return(MemoryMarshal.Cast <byte, T>(GetSpan(va, Unsafe.SizeOf <T>()))[0]);
 }
Exemplo n.º 26
0
        public ShaderHeader(ReadOnlySpan <byte> code)
        {
            ReadOnlySpan <int> header = MemoryMarshal.Cast <byte, int>(code);

            int commonWord0 = header[0];
            int commonWord1 = header[1];
            int commonWord2 = header[2];
            int commonWord3 = header[3];
            int commonWord4 = header[4];

            SphType = commonWord0.Extract(0, 5);
            Version = commonWord0.Extract(5, 5);

            Stage = (ShaderStage)commonWord0.Extract(10, 4);

            // Invalid.
            if (Stage == ShaderStage.Compute)
            {
                Stage = ShaderStage.Vertex;
            }

            MrtEnable = commonWord0.Extract(14);

            KillsPixels = commonWord0.Extract(15);

            DoesGlobalStore = commonWord0.Extract(16);

            SassVersion = commonWord0.Extract(17, 4);

            DoesLoadOrStore = commonWord0.Extract(26);
            DoesFp64        = commonWord0.Extract(27);

            StreamOutMask = commonWord0.Extract(28, 4);

            ShaderLocalMemoryLowSize = commonWord1.Extract(0, 24);

            PerPatchAttributeCount = commonWord1.Extract(24, 8);

            ShaderLocalMemoryHighSize = commonWord2.Extract(0, 24);

            ThreadsPerInputPrimitive = commonWord2.Extract(24, 8);

            ShaderLocalMemoryCrsSize = commonWord3.Extract(0, 24);

            OutputTopology = (OutputTopology)commonWord3.Extract(24, 4);

            MaxOutputVertexCount = commonWord4.Extract(0, 12);

            StoreReqStart = commonWord4.Extract(12, 8);
            StoreReqEnd   = commonWord4.Extract(24, 8);

            int type2OmapTarget = header[18];
            int type2Omap       = header[19];

            OmapTargets = new OutputMapTarget[8];

            for (int offset = 0; offset < OmapTargets.Length * 4; offset += 4)
            {
                OmapTargets[offset >> 2] = new OutputMapTarget(
                    type2OmapTarget.Extract(offset + 0),
                    type2OmapTarget.Extract(offset + 1),
                    type2OmapTarget.Extract(offset + 2),
                    type2OmapTarget.Extract(offset + 3));
            }

            OmapSampleMask = type2Omap.Extract(0);
            OmapDepth      = type2Omap.Extract(1);
        }
Exemplo n.º 27
0
        public void Heightmap()
        {
            Span <byte> span = new byte[128];

            span.Fill(0xFF);

            Heightmap map = new Heightmap
            {
                XMin         = 20,
                XMax         = 180,
                XSpacing     = 40,
                YMin         = 50,
                YMax         = 150,
                YSpacing     = 50,
                Radius       = 0,
                NumX         = 3,
                NumY         = 4,
                ZCoordinates = new float[] {
                    10, 20, 30,
                    40, 50, 60,
                    70, 80, 90,
                    100, 110, 120
                }
            };

            int bytesWritten = Writer.WriteHeightMap(span, map);

            Assert.AreEqual(80, bytesWritten);

            // Header
            float xMin = MemoryMarshal.Read <float>(span);

            Assert.AreEqual(20, xMin, 0.0001);
            float xMax = MemoryMarshal.Read <float>(span.Slice(4, 4));

            Assert.AreEqual(180, xMax, 0.0001);
            float xSpacing = MemoryMarshal.Read <float>(span.Slice(8, 4));

            Assert.AreEqual(40, xSpacing, 0.0001);
            float yMin = MemoryMarshal.Read <float>(span.Slice(12, 4));

            Assert.AreEqual(50, yMin, 0.0001);
            float yMax = MemoryMarshal.Read <float>(span.Slice(16, 4));

            Assert.AreEqual(150, yMax, 0.0001);
            float ySpacing = MemoryMarshal.Read <float>(span.Slice(20, 4));

            Assert.AreEqual(50, ySpacing, 0.0001);
            float radius = MemoryMarshal.Read <float>(span.Slice(24, 4));

            Assert.AreEqual(0, radius, 0.0001);
            ushort numX = MemoryMarshal.Read <ushort>(span.Slice(28, 2));

            Assert.AreEqual(3, numX);
            ushort numY = MemoryMarshal.Read <ushort>(span.Slice(30, 2));

            Assert.AreEqual(4, numY);

            // Points
            Span <float> zCoordinates = MemoryMarshal.Cast <byte, float>(span.Slice(32));

            for (int i = 0; i < map.ZCoordinates.Length; i++)
            {
                Assert.AreEqual(zCoordinates[i], 10 * i + 10, 0.0001);
            }
        }
Exemplo n.º 28
0
        internal Interop.Crypto.X509VerifyStatusCode FindChainViaAia(
            ref List <X509Certificate2> downloadedCerts)
        {
            IntPtr lastCert = IntPtr.Zero;
            SafeX509StoreCtxHandle storeCtx = _storeCtx;

            Interop.Crypto.X509VerifyStatusCode statusCode =
                Interop.Crypto.X509VerifyStatusCode.X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT;

            while (!IsCompleteChain(statusCode))
            {
                using (SafeX509Handle currentCert = Interop.Crypto.X509StoreCtxGetCurrentCert(storeCtx))
                {
                    IntPtr currentHandle = currentCert.DangerousGetHandle();

                    // No progress was made, give up.
                    if (currentHandle == lastCert)
                    {
                        break;
                    }

                    lastCert = currentHandle;

                    ArraySegment <byte> authorityInformationAccess =
                        OpenSslX509CertificateReader.FindFirstExtension(
                            currentCert,
                            Oids.AuthorityInformationAccess);

                    if (authorityInformationAccess.Count == 0)
                    {
                        break;
                    }

                    X509Certificate2 downloaded = DownloadCertificate(
                        authorityInformationAccess,
                        ref _remainingDownloadTime);

                    // The AIA record is contained in a public structure, so no need to clear it.
                    CryptoPool.Return(authorityInformationAccess.Array, clearSize: 0);

                    if (downloaded == null)
                    {
                        break;
                    }

                    if (downloadedCerts == null)
                    {
                        downloadedCerts = new List <X509Certificate2>();
                    }

                    AddToStackAndUpRef(downloaded.Handle, _untrustedLookup);
                    downloadedCerts.Add(downloaded);

                    Interop.Crypto.X509StoreCtxRebuildChain(storeCtx);
                    statusCode = Interop.Crypto.X509StoreCtxGetError(storeCtx);
                }
            }

            if (statusCode == Interop.Crypto.X509VerifyStatusCode.X509_V_OK && downloadedCerts != null)
            {
                using (SafeX509StackHandle chainStack = Interop.Crypto.X509StoreCtxGetChain(_storeCtx))
                {
                    int           chainSize     = Interop.Crypto.GetX509StackFieldCount(chainStack);
                    Span <IntPtr> tempChain     = stackalloc IntPtr[DefaultChainCapacity];
                    byte[]        tempChainRent = null;

                    if (chainSize <= tempChain.Length)
                    {
                        tempChain = tempChain.Slice(0, chainSize);
                    }
                    else
                    {
                        int targetSize = checked (chainSize * IntPtr.Size);
                        tempChainRent = CryptoPool.Rent(targetSize);
                        tempChain     = MemoryMarshal.Cast <byte, IntPtr>(tempChainRent.AsSpan(0, targetSize));
                    }

                    for (int i = 0; i < chainSize; i++)
                    {
                        tempChain[i] = Interop.Crypto.GetX509StackField(chainStack, i);
                    }

                    // In the average case we never made it here.
                    //
                    // Given that we made it here, in the average remaining case
                    // we are doing a one item for which will match in the second position
                    // of an (on-average) 3 item collection.
                    //
                    // The only case where this loop really matters is if downloading the
                    // certificate made an alternate chain better, which may have resulted in
                    // an extra download and made the first one not be involved any longer. In
                    // that case, it's a 2 item for loop matching against a three item set.
                    //
                    // So N*M is well contained.
                    for (int i = downloadedCerts.Count - 1; i >= 0; i--)
                    {
                        X509Certificate2 downloadedCert = downloadedCerts[i];

                        if (!tempChain.Contains(downloadedCert.Handle))
                        {
                            downloadedCert.Dispose();
                            downloadedCerts.RemoveAt(i);
                        }
                    }

                    if (downloadedCerts.Count == 0)
                    {
                        downloadedCerts = null;
                    }

                    if (tempChainRent != null)
                    {
                        CryptoPool.Return(tempChainRent);
                    }
                }
            }

            return(statusCode);
        }
Exemplo n.º 29
0
 public override Span <TTo> GetSpan() => MemoryMarshal.Cast <TFrom, TTo>(_unrooted.Memory.Span);
Exemplo n.º 30
0
        //
        // Internal
        //

        internal static void HChaCha20(ReadOnlySpan <byte> key, ReadOnlySpan <byte> nonce, Span <byte> outKey)
        {
            // TODO: Could there be any unaligned memory access problems?
            var  keyU32 = MemoryMarshal.Cast <byte, uint>(key);
            uint x00    = ChaCha20.InitialState0;
            uint x01    = ChaCha20.InitialState1;
            uint x02    = ChaCha20.InitialState2;
            uint x03    = ChaCha20.InitialState3;
            uint x04    = keyU32[0];
            uint x05    = keyU32[1];
            uint x06    = keyU32[2];
            uint x07    = keyU32[3];
            uint x08    = keyU32[4];
            uint x09    = keyU32[5];
            uint x10    = keyU32[6];
            uint x11    = keyU32[7];

            // TODO: Could there be any unaligned memory access problems?
            var  nonceU32 = MemoryMarshal.Cast <byte, uint>(nonce);
            uint x12      = nonceU32[0];
            uint x13      = nonceU32[1];
            uint x14      = nonceU32[2];
            uint x15      = nonceU32[3];

            // TODO: This is exact the same code as in ChaCha20.ChaChaCore. Could we DRY this up?
            for (var i = 0; i < ChaCha20.Rounds; i += 2)
            {
                x00 += x04;
                x12  = ChaCha20.RotateLeft(x12 ^ x00, 16);
                x08 += x12;
                x04  = ChaCha20.RotateLeft(x04 ^ x08, 12);
                x00 += x04;
                x12  = ChaCha20.RotateLeft(x12 ^ x00, 8);
                x08 += x12;
                x04  = ChaCha20.RotateLeft(x04 ^ x08, 7);
                x01 += x05;
                x13  = ChaCha20.RotateLeft(x13 ^ x01, 16);
                x09 += x13;
                x05  = ChaCha20.RotateLeft(x05 ^ x09, 12);
                x01 += x05;
                x13  = ChaCha20.RotateLeft(x13 ^ x01, 8);
                x09 += x13;
                x05  = ChaCha20.RotateLeft(x05 ^ x09, 7);
                x02 += x06;
                x14  = ChaCha20.RotateLeft(x14 ^ x02, 16);
                x10 += x14;
                x06  = ChaCha20.RotateLeft(x06 ^ x10, 12);
                x02 += x06;
                x14  = ChaCha20.RotateLeft(x14 ^ x02, 8);
                x10 += x14;
                x06  = ChaCha20.RotateLeft(x06 ^ x10, 7);
                x03 += x07;
                x15  = ChaCha20.RotateLeft(x15 ^ x03, 16);
                x11 += x15;
                x07  = ChaCha20.RotateLeft(x07 ^ x11, 12);
                x03 += x07;
                x15  = ChaCha20.RotateLeft(x15 ^ x03, 8);
                x11 += x15;
                x07  = ChaCha20.RotateLeft(x07 ^ x11, 7);
                x00 += x05;
                x15  = ChaCha20.RotateLeft(x15 ^ x00, 16);
                x10 += x15;
                x05  = ChaCha20.RotateLeft(x05 ^ x10, 12);
                x00 += x05;
                x15  = ChaCha20.RotateLeft(x15 ^ x00, 8);
                x10 += x15;
                x05  = ChaCha20.RotateLeft(x05 ^ x10, 7);
                x01 += x06;
                x12  = ChaCha20.RotateLeft(x12 ^ x01, 16);
                x11 += x12;
                x06  = ChaCha20.RotateLeft(x06 ^ x11, 12);
                x01 += x06;
                x12  = ChaCha20.RotateLeft(x12 ^ x01, 8);
                x11 += x12;
                x06  = ChaCha20.RotateLeft(x06 ^ x11, 7);
                x02 += x07;
                x13  = ChaCha20.RotateLeft(x13 ^ x02, 16);
                x08 += x13;
                x07  = ChaCha20.RotateLeft(x07 ^ x08, 12);
                x02 += x07;
                x13  = ChaCha20.RotateLeft(x13 ^ x02, 8);
                x08 += x13;
                x07  = ChaCha20.RotateLeft(x07 ^ x08, 7);
                x03 += x04;
                x14  = ChaCha20.RotateLeft(x14 ^ x03, 16);
                x09 += x14;
                x04  = ChaCha20.RotateLeft(x04 ^ x09, 12);
                x03 += x04;
                x14  = ChaCha20.RotateLeft(x14 ^ x03, 8);
                x09 += x14;
                x04  = ChaCha20.RotateLeft(x04 ^ x09, 7);
            }

            var outKeyU32 = MemoryMarshal.Cast <byte, uint>(outKey);

            outKeyU32[0] = x00;
            outKeyU32[1] = x01;
            outKeyU32[2] = x02;
            outKeyU32[3] = x03;
            outKeyU32[4] = x12;
            outKeyU32[5] = x13;
            outKeyU32[6] = x14;
            outKeyU32[7] = x15;
        }