public unsafe void GetRow(int row, double[] array, int index) { if((uint)row >= (uint)Height) throw new ArgumentOutOfRangeException(); Utility.ValidateRange(array, index, Width); fixed(double* psrc=data, pdest=array) Unsafe.Copy(psrc+row*Width, pdest+index, Width*sizeof(double)); }
public static void Read <T>(IntPtr srcPointer, ref T value) { Unsafe.Copy(ref value, srcPointer.ToPointer()); }
private unsafe void Copy <T>(Entity entity, int index, T data) where T : struct { var destination = EntityManager.GetComponentDataRawRW(entity, index); Unsafe.Copy(destination, ref data); }
public static void Write <T>(void *dstPointer, ref T value) { Unsafe.Copy(dstPointer, ref value); }
public static void Write <T>(IntPtr dstPointer, ref T value) { Unsafe.Copy(dstPointer.ToPointer(), ref value); }
public override int Read(byte[] buffer, int offset, int count) { if (_disposed) { throw new ObjectDisposedException(this.GetType().FullName); } if (offset < 0 || buffer.Length < offset) { throw new ArgumentOutOfRangeException("offset"); } if (count < 0 || (buffer.Length - offset) < count) { throw new ArgumentOutOfRangeException("count"); } try { int readSumLength = 0; for (; ;) { if (_current == null) { _current = _queue.Dequeue(); } if (_current.Value.Array == null) { return(0); } var subCount = Math.Min(count, _current.Value.Count); readSumLength += subCount; Unsafe.Copy(_current.Value.Array, _current.Value.Offset, buffer, offset, subCount); offset += subCount; count -= subCount; if (subCount < _current.Value.Count) { _current = new ArraySegment <byte>(_current.Value.Array, _current.Value.Offset + subCount, _current.Value.Count - subCount); } else { _bufferManager.ReturnBuffer(_current.Value.Array); _current = null; } if (count == 0) { _position += readSumLength; return(readSumLength); } } } catch (Exception e) { throw new StopIoException("QueueStream Read", e); } }
public unsafe static void Main(string[] args) { var thumb = "48026c976caaf7f3a72d38c17d16ce69d04a6053".ToUpper(); var cert = LoadCertificateFromStore(thumb, false, StoreLocation.CurrentUser, StoreName.My); var privKey = cert.GetECDsaPrivateKey(); var curve = privKey.ExportParameters(true); List <byte> currentSection = null; var lines = ecdsaKeyPEM.Split('\n'); for (int i = 0; i < lines.Length; i++) { if (lines[i].StartsWith("-----BEGIN")) { //Starting a block string blockName = lines[i].Substring("-----BEGIN ".Length, lines[i].LastIndexOf("-----") - "-----BEGIN ".Length); currentSection = new List <byte>(); _sections.Add(blockName, currentSection); continue; } else if (lines[i].StartsWith("-----END")) { //ending block currentSection = null; } currentSection?.AddRange(Convert.FromBase64String(lines[i].Trim())); } IntPtr prov, key; var res = NCryptOpenStorageProvider(out prov, "Microsoft Software Key Storage Provider", 0); //ECDSA_P521 var size = sizeof(BCRYPT_ECCKEY_BLOB); var blobHeader = new BCRYPT_ECCKEY_BLOB() { Magic = KeyBlobMagicNumber.BCRYPT_ECDSA_PRIVATE_GENERIC_MAGIC, cbKey = _sections["EC PRIVATE KEY"].Count }; var desc = new BCryptBufferDesc() { cBuffers = 1 }; var buff = new BCryptBuffer() { BufferType = NCryptBufferDescriptors.NCRYPTBUFFER_ECC_CURVE_NAME, cbBuffer = "ECDSA_P521\0".Length * 2, pvBuffer = Marshal.StringToHGlobalUni("ECDSA_P521\0") }; var buffPtr = Marshal.AllocHGlobal(sizeof(BCryptBuffer)); Marshal.StructureToPtr(buff, buffPtr, true); desc.pBuffers = buffPtr; var descPtr = Marshal.AllocHGlobal(sizeof(BCryptBufferDesc)); Marshal.StructureToPtr(desc, descPtr, true); var bytes = new byte[size + blobHeader.cbKey]; _sections["EC PRIVATE KEY"].ToArray().CopyTo(bytes, size); fixed(void *blobPtr = bytes) { Unsafe.Copy(blobPtr, ref blobHeader); res = NCryptImportKey(prov, IntPtr.Zero, "ECCPRIVATEBLOB", descPtr, out key, (IntPtr)blobPtr, (uint)bytes.Length, 0); } }
public unsafe static void Xor(byte[] source1, byte[] source2, byte[] destination) { if (source1 == null) { throw new ArgumentNullException("source1"); } if (source2 == null) { throw new ArgumentNullException("source2"); } if (destination == null) { throw new ArgumentNullException("destination"); } // Zero { int targetRange = Math.Max(source1.Length, source2.Length); if (destination.Length > targetRange) { Unsafe.Zero(destination, targetRange, destination.Length - targetRange); } } if (source1.Length > source2.Length && destination.Length > source2.Length) { Unsafe.Copy(source1, source2.Length, destination, source2.Length, Math.Min(source1.Length, destination.Length) - source2.Length); } else if (source2.Length > source1.Length && destination.Length > source1.Length) { Unsafe.Copy(source2, source1.Length, destination, source1.Length, Math.Min(source2.Length, destination.Length) - source1.Length); } int length = Math.Min(Math.Min(source1.Length, source2.Length), destination.Length); fixed(byte *p_x = source1, p_y = source2) { byte *t_x = p_x, t_y = p_y; fixed(byte *p_buffer = destination) { byte *t_buffer = p_buffer; for (int i = (length / 8) - 1; i >= 0; i--, t_x += 8, t_y += 8, t_buffer += 8) { *((long *)t_buffer) = *((long *)t_x) ^ *((long *)t_y); } if ((length & 4) != 0) { *((int *)t_buffer) = *((int *)t_x) ^ *((int *)t_y); t_x += 4; t_y += 4; t_buffer += 4; } if ((length & 2) != 0) { *((short *)t_buffer) = (short)(*((short *)t_x) ^ *((short *)t_y)); t_x += 2; t_y += 2; t_buffer += 2; } if ((length & 1) != 0) { *((byte *)t_buffer) = (byte)(*((byte *)t_x) ^ *((byte *)t_y)); } } } }