/// <inheritdoc /> public ValueTask InvokeAsync(TiffImageDecoderContext context, ITiffImageDecoderPipelineNode next) { if (context is null) { throw new ArgumentNullException(nameof(context)); } if (next is null) { throw new ArgumentNullException(nameof(next)); } int bytesPerScanline = (context.SourceImageSize.Width + 1) / 2; Memory <byte> source = context.UncompressedData.Slice(context.SourceReadOffset.Y * bytesPerScanline); ReadOnlySpan <byte> sourceSpan = source.Span; using TiffPixelBufferWriter <TiffGray8> writer = context.GetWriter <TiffGray8>(); int xOffset = context.SourceReadOffset.X; int rows = context.ReadSize.Height; for (int row = 0; row < rows; row++) { using TiffPixelSpanHandle <TiffGray8> pixelSpanHandle = writer.GetRowSpan(row); ReadOnlySpan <byte> bitsSpan = sourceSpan.Slice(xOffset >> 1); // xOffset / 2 Span <byte> rowDestinationSpan = MemoryMarshal.AsBytes(pixelSpanHandle.GetSpan()); int bitsOffset = xOffset & 1; // xOffset % 2 int sourceIndex = 0; int destinationIndex = 0; int remainingWidth = context.ReadSize.Width; byte bits; if (bitsOffset > 0) { remainingWidth--; bits = bitsSpan[sourceIndex++]; bits = (byte)(bits & 0xf); rowDestinationSpan[destinationIndex++] = (byte)(bits << 4 | bits); } // manual loop unrolling for (; (remainingWidth >> 4) > 0; remainingWidth -= 16) // for (; remainingWidth >= 16; remainingWidth -= 16) { bits = bitsSpan[sourceIndex++]; rowDestinationSpan[destinationIndex++] = (byte)(bits & 0xf0 | bits >> 4); rowDestinationSpan[destinationIndex++] = (byte)(bits << 4 | bits & 0xf); bits = bitsSpan[sourceIndex++]; rowDestinationSpan[destinationIndex++] = (byte)(bits & 0xf0 | bits >> 4); rowDestinationSpan[destinationIndex++] = (byte)(bits << 4 | bits & 0xf); bits = bitsSpan[sourceIndex++]; rowDestinationSpan[destinationIndex++] = (byte)(bits & 0xf0 | bits >> 4); rowDestinationSpan[destinationIndex++] = (byte)(bits << 4 | bits & 0xf); bits = bitsSpan[sourceIndex++]; rowDestinationSpan[destinationIndex++] = (byte)(bits & 0xf0 | bits >> 4); rowDestinationSpan[destinationIndex++] = (byte)(bits << 4 | bits & 0xf); bits = bitsSpan[sourceIndex++]; rowDestinationSpan[destinationIndex++] = (byte)(bits & 0xf0 | bits >> 4); rowDestinationSpan[destinationIndex++] = (byte)(bits << 4 | bits & 0xf); bits = bitsSpan[sourceIndex++]; rowDestinationSpan[destinationIndex++] = (byte)(bits & 0xf0 | bits >> 4); rowDestinationSpan[destinationIndex++] = (byte)(bits << 4 | bits & 0xf); bits = bitsSpan[sourceIndex++]; rowDestinationSpan[destinationIndex++] = (byte)(bits & 0xf0 | bits >> 4); rowDestinationSpan[destinationIndex++] = (byte)(bits << 4 | bits & 0xf); bits = bitsSpan[sourceIndex++]; rowDestinationSpan[destinationIndex++] = (byte)(bits & 0xf0 | bits >> 4); rowDestinationSpan[destinationIndex++] = (byte)(bits << 4 | bits & 0xf); } for (; (remainingWidth >> 1) > 0; remainingWidth -= 2) // for (; remainingWidth >= 2; remainingWidth -= 2) { bits = bitsSpan[sourceIndex++]; rowDestinationSpan[destinationIndex++] = (byte)(bits & 0xf0 | bits >> 4); rowDestinationSpan[destinationIndex++] = (byte)(bits << 4 | bits & 0xf); } if (remainingWidth != 0) { bits = bitsSpan[sourceIndex++]; rowDestinationSpan[destinationIndex++] = (byte)(bits & 0xf0 | bits >> 4); } sourceSpan = sourceSpan.Slice(bytesPerScanline); } return(next.RunAsync(context)); }
public static bool TryWriteUtf8Bytes(ref ResizableMemory <byte> writer, ReadOnlySpan <byte> value) { int i = 0; int start = 0; for (; i < value.Length; i++) { byte b = value[i]; // Special chars switch (b) { case (byte)'"': case (byte)'\\': //case (byte)'/': if (i != start) { int bytes = i - start; var buffer = writer.RequestSpan(bytes); value.Slice(start, bytes).CopyTo(buffer); writer.Advance(bytes); } writer.Push((byte)'\\'); writer.Push(b); start = i + 1; continue; } if (b < 32) // Control codes { if (i != start) { int bytes = i - start; var buffer = writer.RequestSpan(bytes); value.Slice(start, bytes).CopyTo(buffer); writer.Advance(bytes); } switch (b) { case (byte)'\b': writer.Push((byte)'\\'); writer.Push((byte)'b'); break; case (byte)'\f': writer.Push((byte)'\\'); writer.Push((byte)'f'); break; case (byte)'\n': writer.Push((byte)'\\'); writer.Push((byte)'n'); break; case (byte)'\r': writer.Push((byte)'\\'); writer.Push((byte)'r'); break; case (byte)'\t': writer.Push((byte)'\\'); writer.Push((byte)'t'); break; default: var escape = writer.RequestSpan(6); escape[0] = (byte)'\\'; escape[1] = (byte)'u'; escape[2] = (byte)'0'; escape[3] = (byte)'0'; escape[4] = ToHex((byte)(b >> 4)); escape[5] = ToHex((byte)(b & 0xF)); writer.Advance(6); break; } start = i + 1; } else if (b >= 192) // Multi-byte chars { if (i != start) { int bytes = i - start; var buffer = writer.RequestSpan(bytes); value.Slice(start, i - start).CopyTo(buffer); writer.Advance(bytes); } int seqStart = i; int length = 1; for (; length < 4 && i < value.Length - 1 && value[i + 1] >= 128; length++, i++) { } Span <ushort> utf16Value = stackalloc ushort[2]; if (Encodings.Utf8.ToUtf16(value.Slice(seqStart, length), MemoryMarshal.AsBytes(utf16Value), out _, out int bytesWritten) != OperationStatus.Done) { return(false); } for (int j = 0; j < bytesWritten / 2; j++) { var buffer2 = writer.RequestSpan(6); buffer2[0] = (byte)'\\'; buffer2[1] = (byte)'u'; buffer2[2] = ToHex((byte)((utf16Value[j] >> 12) & 0xF)); buffer2[3] = ToHex((byte)((utf16Value[j] >> 8) & 0xF)); buffer2[4] = ToHex((byte)((utf16Value[j] >> 4) & 0xF)); buffer2[5] = ToHex((byte)(utf16Value[j] & 0xF)); writer.Advance(6); } start = i + 1; } else if (b >= 128) // Multi-byte chars out of sequence { return(false); } } // Append last part to builder if (i != start) { int bytes = i - start; var buffer = writer.RequestSpan(bytes); value.Slice(start, bytes).CopyTo(buffer); writer.Advance(bytes); } return(true); }
public void SetHeaderHash() { Span <InnerHeader> spanHeader = MemoryMarshal.CreateSpan(ref this, 1); HeaderHash = XXHash128.ComputeHash(MemoryMarshal.AsBytes(spanHeader).Slice(0, Unsafe.SizeOf <InnerHeader>() - Unsafe.SizeOf <Hash128>())); }
public void EncodeAllUnicodeCodePoints(bool useUtf8Encoder) { Text.Encoding systemEncoder = useUtf8Encoder ? Text.Encoding.UTF8 : Text.Encoding.Unicode; const uint maximumValidCodePoint = 0x10FFFF; uint[] codePoints = new uint[maximumValidCodePoint + 1]; var plainText = new StringBuilder(); for (int i = 0; i <= maximumValidCodePoint; i++) { if (!EncodingHelper.IsValidScalarValue((uint)i)) { codePoints[i] = 0; // skip unsupported characters plainText.Append((char)0); } else { codePoints[i] = (uint)i; if (i > 0xFFFF) { plainText.Append(char.ConvertFromUtf32(i)); } else { plainText.Append((char)i); } } } ReadOnlySpan <uint> allCodePoints = codePoints; Span <byte> buffer = new byte[4 * (maximumValidCodePoint + 1)]; int written; int consumed; if (useUtf8Encoder) { Assert.Equal(Buffers.OperationStatus.Done, TextEncodings.Utf32.ToUtf8(MemoryMarshal.AsBytes(allCodePoints), buffer, out consumed, out written)); } else { Assert.Equal(Buffers.OperationStatus.Done, TextEncodings.Utf32.ToUtf16(MemoryMarshal.AsBytes(allCodePoints), buffer, out consumed, out written)); } buffer = buffer.Slice(0, written); string unicodeString = plainText.ToString(); ReadOnlySpan <char> characters = unicodeString.AsSpan(); int byteCount = systemEncoder.GetByteCount(unicodeString); byte[] expectedBytes = new byte[byteCount]; systemEncoder.GetBytes(characters.ToArray(), 0, characters.Length, expectedBytes, 0); Assert.Equal(expectedBytes.Length, buffer.Length); Assert.True(buffer.SequenceEqual(expectedBytes), "Bad output from system encoding comparison"); }
public static TransferCoding GetFinalTransferCoding(StringValues transferEncoding) { const ulong chunkedStart = 0x006b_006e_0075_0068; // 4 chars "hunk" const uint chunkedEnd = 0x0064_0065; // 2 chars "ed" var transferEncodingOptions = TransferCoding.None; var transferEncodingCount = transferEncoding.Count; for (var i = 0; i < transferEncodingCount; i++) { var values = transferEncoding[i].AsSpan(); while (values.Length > 0) { int offset; char c = '\0'; // Skip any spaces and empty values. for (offset = 0; offset < values.Length; offset++) { c = values[offset]; if (c != ' ' && c != ',') { break; } } // Skip last read char. offset++; if ((uint)offset > (uint)values.Length) { // Consumed entire string, move to next. break; } // Remove leading spaces or empty values. values = values.Slice(offset); offset = 0; var byteValue = MemoryMarshal.AsBytes(values); if (ToLowerCase(c) == 'c' && TryReadLowerCaseUInt64(byteValue, out var result64) && result64 == chunkedStart) { offset += sizeof(ulong) / 2; byteValue = byteValue.Slice(sizeof(ulong)); if (TryReadLowerCaseUInt32(byteValue, out var result32) && result32 == chunkedEnd) { offset += sizeof(uint) / 2; transferEncodingOptions = TransferCoding.Chunked; } } if ((uint)offset >= (uint)values.Length) { // Consumed entire string, move to next string. break; } else { values = values.Slice(offset); } for (offset = 0; offset < values.Length; offset++) { c = values[offset]; if (c == ',') { break; } else if (c != ' ') { // Value contains extra chars; Chunked is not the matched one. transferEncodingOptions = TransferCoding.Other; } } if ((uint)offset >= (uint)values.Length) { // Consumed entire string, move to next string. break; } else if (c == ',') { // Consumed value, move to next value. offset++; if ((uint)offset >= (uint)values.Length) { // Consumed entire string, move to next string. break; } else { values = values.Slice(offset); continue; } } } } return(transferEncodingOptions); }
internal ModelMesh(W3dMesh w3dMesh, AssetLoadContext loadContext) { SetNameAndInstanceId("W3DMesh", w3dMesh.Header.MeshName); W3dShaderMaterial w3dShaderMaterial; ShaderResourcesBase shaderResources; if (w3dMesh.MaterialPasses.Count == 1 && w3dMesh.MaterialPasses[0].ShaderMaterialIds != null) { if (w3dMesh.MaterialPasses[0].ShaderMaterialIds.Items.Length > 1) { throw new NotSupportedException(); } var shaderMaterialID = w3dMesh.MaterialPasses[0].ShaderMaterialIds.Items[0]; w3dShaderMaterial = w3dMesh.ShaderMaterials.Items[(int)shaderMaterialID]; var effectName = w3dShaderMaterial.Header.TypeName.Replace(".fx", string.Empty); shaderResources = loadContext.ShaderResources.GetShaderMaterialResources(effectName); } else { w3dShaderMaterial = null; shaderResources = loadContext.ShaderResources.FixedFunction; } _depthPipeline = loadContext.ShaderResources.MeshDepth.Pipeline; MeshParts = new List <ModelMeshPart>(); if (w3dShaderMaterial != null) { MeshParts.Add(CreateModelMeshPartShaderMaterial( loadContext, w3dMesh, w3dMesh.MaterialPasses[0], w3dShaderMaterial, (ShaderMaterialShaderResources)shaderResources)); } else { var vertexMaterials = CreateMaterials(w3dMesh); var shadingConfigurations = new FixedFunctionShaderResources.ShadingConfiguration[w3dMesh.Shaders.Items.Count]; for (var i = 0; i < shadingConfigurations.Length; i++) { shadingConfigurations[i] = CreateShadingConfiguration(w3dMesh.Shaders.Items[i]); } for (var i = 0; i < w3dMesh.MaterialPasses.Count; i++) { CreateModelMeshPartsFixedFunction( loadContext, w3dMesh, w3dMesh.MaterialPasses[i], vertexMaterials, shadingConfigurations, MeshParts); } } _boundingBox = new AxisAlignedBoundingBox( w3dMesh.Header.Min, w3dMesh.Header.Max); _shaderSet = shaderResources.ShaderSet; Skinned = w3dMesh.IsSkinned; Hidden = w3dMesh.Header.Attributes.HasFlag(W3dMeshFlags.Hidden); CameraOriented = (w3dMesh.Header.Attributes & W3dMeshFlags.GeometryTypeMask) == W3dMeshFlags.GeometryTypeCameraOriented; _vertexBuffer = AddDisposable(loadContext.GraphicsDevice.CreateStaticBuffer( MemoryMarshal.AsBytes(new ReadOnlySpan <MeshShaderResources.MeshVertex.Basic>(CreateVertices(w3dMesh, w3dMesh.IsSkinned))), BufferUsage.VertexBuffer)); _indexBuffer = AddDisposable(loadContext.GraphicsDevice.CreateStaticBuffer( CreateIndices(w3dMesh), BufferUsage.IndexBuffer)); var hasHouseColor = w3dMesh.Header.MeshName.StartsWith("HOUSECOLOR"); _meshConstantsResourceSet = loadContext.ShaderResources.Mesh.GetCachedMeshResourceSet( Skinned, hasHouseColor); PostInitialize(loadContext); }
public static bool TryToBase62Chars(ReadOnlySpan <byte> bytes, Span <char> chars, out int charsWritten, Base62Alphabet alphabet = null) { var charsAsBytes = MemoryMarshal.AsBytes(chars); return(TryToBase62Chars(bytes, charsAsBytes, shiftCount: 1, out charsWritten, alphabet)); }
public static ReadOnlySpan <byte> AsBytes <T>(this ReadOnlySpan <T> src) where T : struct => MemoryMarshal.AsBytes(src);
// compute a keccak hash (md) of given byte length from "in" public static void ComputeHash(Span <byte> input, Span <byte> output) { if (output.Length <= 0 || output.Length > STATE_SIZE) { throw new ArgumentException("Bad keccak use"); } byte[] stateArray = _arrayPool.Rent(STATE_SIZE); byte[] tempArray = _arrayPool.Rent(TEMP_BUFF_SIZE); try { Span <ulong> state = MemoryMarshal.Cast <byte, ulong>(stateArray.AsSpan(0, STATE_SIZE)); Span <byte> temp = tempArray.AsSpan(0, TEMP_BUFF_SIZE); state.Clear(); temp.Clear(); int roundSize = STATE_SIZE == output.Length ? HASH_DATA_AREA : STATE_SIZE - (2 * output.Length); int roundSizeU64 = roundSize / 8; var inputLength = input.Length; int i; for (; inputLength >= roundSize; inputLength -= roundSize, input = input.Slice(roundSize)) { var input64 = MemoryMarshal.Cast <byte, ulong>(input); for (i = 0; i < roundSizeU64; i++) { state[i] ^= input64[i]; } KeccakF(state, ROUNDS); } // last block and padding if (inputLength >= TEMP_BUFF_SIZE || inputLength > roundSize || roundSize - inputLength + inputLength + 1 >= TEMP_BUFF_SIZE || roundSize == 0 || roundSize - 1 >= TEMP_BUFF_SIZE || roundSizeU64 * 8 > TEMP_BUFF_SIZE) { throw new ArgumentException("Bad keccak use"); } input.Slice(0, inputLength).CopyTo(temp); temp[inputLength++] = 1; temp[roundSize - 1] |= 0x80; var tempU64 = MemoryMarshal.Cast <byte, ulong>(temp); for (i = 0; i < roundSizeU64; i++) { state[i] ^= tempU64[i]; } KeccakF(state, ROUNDS); MemoryMarshal.AsBytes(state).Slice(0, output.Length).CopyTo(output); } finally { _arrayPool.Return(stateArray); _arrayPool.Return(tempArray); } }
public object ReadUtf16(Type type, ReadOnlySpan <char> data, ValueConverter converter = null) => ReadUtf16(type, MemoryMarshal.AsBytes(data), converter);
public object ReadUtf16(Type type, string data, ValueConverter converter = null) => ReadUtf16(type, MemoryMarshal.AsBytes(data.AsSpan()), converter);
public T ReadUtf16 <T>(string data, ValueConverter <T> converter = null) => ReadUtf16(MemoryMarshal.AsBytes(data.AsSpan()), converter);
public T ReadUtf16 <T>(ReadOnlySpan <char> data, ValueConverter <T> converter = null) => ReadUtf16(MemoryMarshal.AsBytes(data), converter);
/// <summary> /// Displays a series of prebuild characters including the dot or not /// You can build your won characters with the primitives like Bottom, Top, Dot /// </summary> /// <param name="rawData">The Character to display</param> public void Display(ReadOnlySpan <Character> rawData) { Display(MemoryMarshal.AsBytes(rawData)); }
public uint256(string hexString) { if (hexString is null) { throw new ArgumentNullException(nameof(hexString)); } //account for 0x prefix if (hexString.Length < ExpectedSize * 2) { throw new FormatException($"Invalid Hex String, the hex string should be {ExpectedSize * 2} chars long or {(ExpectedSize * 2) + 4} if prefixed with 0x."); } ReadOnlySpan <char> hexAsSpan = (hexString[0] == '0' && (hexString[1] == 'x' || hexString[1] == 'X')) ? hexString.Trim().AsSpan(2) : hexString.Trim().AsSpan(); if (hexAsSpan.Length != ExpectedSize * 2) { throw new FormatException($"Invalid Hex String, the hex string should be {ExpectedSize * 2} chars long or {(ExpectedSize * 2) + 4} if prefixed with 0x."); } Span <byte> dst = MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref this.part1, ExpectedSize / sizeof(ulong))); int i = hexAsSpan.Length - 1; int j = 0; while (i > 0) { char c = hexAsSpan[i--]; if (c >= '0' && c <= '9') { dst[j] = (byte)(c - '0'); } else if (c >= 'a' && c <= 'f') { dst[j] = (byte)(c - ('a' - 10)); } else if (c >= 'A' && c <= 'F') { dst[j] = (byte)(c - ('A' - 10)); } else { throw new ArgumentException("Invalid nibble: " + c); } c = hexAsSpan[i--]; if (c >= '0' && c <= '9') { dst[j] |= (byte)((c - '0') << 4); } else if (c >= 'a' && c <= 'f') { dst[j] |= (byte)((c - ('a' - 10)) << 4); } else if (c >= 'A' && c <= 'F') { dst[j] |= (byte)((c - ('A' - 10)) << 4); } else { throw new ArgumentException("Invalid nibble: " + c); } j++; } }
public static Span <byte> AsBytes <T>(this Span <T> span) where T : struct { return(MemoryMarshal.AsBytes(span)); }
public ReadOnlySpan <byte> ToReadOnlySpan() { return(MemoryMarshal.AsBytes(MemoryMarshal.CreateReadOnlySpan(ref this.part1, ExpectedSize / sizeof(ulong)))); }
public Span <byte> GetSpan() => MemoryMarshal.AsBytes(new Span <Address>(new[] { this }));
private int WritePropertyNameIndented(ref ReadOnlySpan <char> escapedPropertyName) { int idx = 0; if (_currentDepth < 0) { while (_buffer.Length <= idx) { GrowAndEnsure(); } _buffer[idx++] = JsonConstants.ListSeperator; } if (_tokenType != JsonTokenType.None) { WriteNewLine(ref idx); } int indent = Indentation; while (true) { bool result = JsonWriterHelper.TryWriteIndentation(_buffer.Slice(idx), indent, out int bytesWritten); idx += bytesWritten; if (result) { break; } indent -= bytesWritten; AdvanceAndGrow(idx); idx = 0; } while (_buffer.Length <= idx) { AdvanceAndGrow(idx); idx = 0; } _buffer[idx++] = JsonConstants.Quote; ReadOnlySpan <byte> byteSpan = MemoryMarshal.AsBytes(escapedPropertyName); int partialConsumed = 0; while (true) { OperationStatus status = Buffers.Text.TextEncodings.Utf16.ToUtf8(byteSpan.Slice(partialConsumed), _buffer.Slice(idx), out int consumed, out int written); idx += written; if (status == OperationStatus.Done) { break; } partialConsumed += consumed; AdvanceAndGrow(idx); idx = 0; } while (_buffer.Length <= idx) { AdvanceAndGrow(idx); idx = 0; } _buffer[idx++] = JsonConstants.Quote; while (_buffer.Length <= idx) { AdvanceAndGrow(idx); idx = 0; } _buffer[idx++] = JsonConstants.KeyValueSeperator; while (_buffer.Length <= idx) { AdvanceAndGrow(idx); idx = 0; } _buffer[idx++] = JsonConstants.Space; return(idx); }
internal static ProcessInfo[] GetProcessInfos(int?processIdFilter = null) { ProcessInfo[] processInfos; // Start with the default buffer size. int bufferSize = DefaultCachedBufferSize; // Get the cached buffer. long[]? buffer = Interlocked.Exchange(ref CachedBuffer, null); try { while (true) { if (buffer == null) { // Allocate buffer of longs since some platforms require the buffer to be 64-bit aligned. buffer = new long[(bufferSize + 7) / 8]; } uint requiredSize = 0; unsafe { // Note that the buffer will contain pointers to itself and it needs to be pinned while it is being processed // by GetProcessInfos below fixed(long *bufferPtr = buffer) { uint status = Interop.NtDll.NtQuerySystemInformation( Interop.NtDll.SystemProcessInformation, bufferPtr, (uint)(buffer.Length * sizeof(long)), &requiredSize); if (status != Interop.NtDll.STATUS_INFO_LENGTH_MISMATCH) { // see definition of NT_SUCCESS(Status) in SDK if ((int)status < 0) { throw new InvalidOperationException(SR.CouldntGetProcessInfos, new Win32Exception((int)status)); } // Parse the data block to get process information processInfos = GetProcessInfos(MemoryMarshal.AsBytes <long>(buffer), processIdFilter); break; } } } buffer = null; bufferSize = GetNewBufferSize(bufferSize, (int)requiredSize); } } finally { // Cache the final buffer for use on the next call. Interlocked.Exchange(ref CachedBuffer, buffer); } return(processInfos); }
public void BruteTestingRoundtripEncodeDecodeAllUnicodeCodePoints(bool useUtf8Encoder) { const int maximumValidCodePoint = 0x10FFFF; uint[] expectedCodePoints = new uint[maximumValidCodePoint + 1]; for (uint i = 0; i <= maximumValidCodePoint; i++) { if (!EncodingHelper.IsValidScalarValue(i)) { expectedCodePoints[i] = 0; // skip unsupported code points. } else { expectedCodePoints[i] = i; } } ReadOnlySpan <uint> allCodePoints = expectedCodePoints; Span <byte> buffer = new byte[4 * (maximumValidCodePoint + 1)]; int consumed; int written; if (useUtf8Encoder) { Assert.Equal(OperationStatus.Done, TextEncodings.Utf32.ToUtf8(MemoryMarshal.AsBytes(allCodePoints), buffer, out consumed, out written)); } else { Assert.Equal(OperationStatus.Done, TextEncodings.Utf32.ToUtf16(MemoryMarshal.AsBytes(allCodePoints), buffer, out consumed, out written)); } Assert.Equal(MemoryMarshal.AsBytes(allCodePoints).Length, consumed); buffer = buffer.Slice(0, written); Span <uint> utf32 = new uint[maximumValidCodePoint + 1]; if (useUtf8Encoder) { Assert.Equal(OperationStatus.Done, TextEncodings.Utf8.ToUtf32(buffer, MemoryMarshal.AsBytes(utf32), out consumed, out written)); } else { Assert.Equal(OperationStatus.Done, TextEncodings.Utf16.ToUtf32(buffer, MemoryMarshal.AsBytes(utf32), out consumed, out written)); } Assert.Equal(buffer.Length, consumed); Assert.Equal((maximumValidCodePoint + 1) * sizeof(uint), written); Assert.True(allCodePoints.SequenceEqual(utf32), "Bad output from round-trip"); }
/// <inheritdoc /> public ValueTask InvokeAsync(TiffImageDecoderContext context, ITiffImageDecoderPipelineNode next) { if (context is null) { throw new ArgumentNullException(nameof(context)); } if (next is null) { throw new ArgumentNullException(nameof(next)); } int bytesPerScanline = (context.SourceImageSize.Width + 7) / 8; Memory <byte> source = context.UncompressedData.Slice(context.SourceReadOffset.Y * bytesPerScanline); ReadOnlySpan <byte> sourceSpan = source.Span; using TiffPixelBufferWriter <TiffMask> writer = context.GetWriter <TiffMask>(); int xOffset = context.SourceReadOffset.X; int rows = context.ReadSize.Height; for (int row = 0; row < rows; row++) { using TiffPixelSpanHandle <TiffMask> pixelSpanHandle = writer.GetRowSpan(row); ReadOnlySpan <byte> bitsSpan = sourceSpan.Slice(xOffset >> 3); // xOffset / 8 Span <byte> rowDestinationSpan = MemoryMarshal.AsBytes(pixelSpanHandle.GetSpan()); int bitsOffset = xOffset & 7; // xOffset % 8 int sourceIndex = 0; int destinationIndex = 0; int remainingWidth = context.ReadSize.Width; byte bits; if (bitsOffset > 0) { remainingWidth -= 8 - bitsOffset; bits = MaybeReverseBits(bitsSpan[sourceIndex++]); for (; bitsOffset < 8; bitsOffset++) { bool isSet = (bits >> (7 - bitsOffset) & 1) != 0; rowDestinationSpan[destinationIndex++] = isSet ? (byte)255 : (byte)0; } } while (remainingWidth >= 8) { bits = (bitsSpan[sourceIndex++]); bool bit0 = (bits >> 7 & 1) != 0; bool bit1 = (bits >> 6 & 1) != 0; bool bit2 = (bits >> 5 & 1) != 0; bool bit3 = (bits >> 4 & 1) != 0; bool bit4 = (bits >> 3 & 1) != 0; bool bit5 = (bits >> 2 & 1) != 0; bool bit6 = (bits >> 1 & 1) != 0; bool bit7 = (bits & 1) != 0; rowDestinationSpan[destinationIndex++] = bit0 ? (byte)255 : (byte)0; rowDestinationSpan[destinationIndex++] = bit1 ? (byte)255 : (byte)0; rowDestinationSpan[destinationIndex++] = bit2 ? (byte)255 : (byte)0; rowDestinationSpan[destinationIndex++] = bit3 ? (byte)255 : (byte)0; rowDestinationSpan[destinationIndex++] = bit4 ? (byte)255 : (byte)0; rowDestinationSpan[destinationIndex++] = bit5 ? (byte)255 : (byte)0; rowDestinationSpan[destinationIndex++] = bit6 ? (byte)255 : (byte)0; rowDestinationSpan[destinationIndex++] = bit7 ? (byte)255 : (byte)0; remainingWidth -= 8; } if (remainingWidth > 0) { bits = MaybeReverseBits(bitsSpan[sourceIndex++]); for (; remainingWidth > 0; remainingWidth--) { bool isSet = (bits & 0b10000000) != 0; rowDestinationSpan[destinationIndex++] = isSet ? (byte)255 : (byte)0; bits = (byte)(bits << 1); } } sourceSpan = sourceSpan.Slice(bytesPerScanline); } return(next.RunAsync(context)); }
public static ConnectionOptions ParseConnection(HttpHeaders headers) { // Keep-alive const ulong lowerCaseKeep = 0x0000_0020_0020_0020; // Don't lowercase hyphen const ulong keepAliveStart = 0x002d_0070_0065_0065; // 4 chars "eep-" const ulong keepAliveMiddle = 0x0076_0069_006c_0061; // 4 chars "aliv" const ushort keepAliveEnd = 0x0065; // 1 char "e" // Upgrade const ulong upgradeStart = 0x0061_0072_0067_0070; // 4 chars "pgra" const uint upgradeEnd = 0x0065_0064; // 2 chars "de" // Close const ulong closeEnd = 0x0065_0073_006f_006c; // 4 chars "lose" var connection = headers.HeaderConnection; var connectionCount = connection.Count; if (connectionCount == 0) { return(ConnectionOptions.None); } var connectionOptions = ConnectionOptions.None; if (connectionCount == 1) { // "keep-alive" is the only value that will be repeated over // many requests on the same connection; on the first request // we will have switched it for the readonly static value; // so we can ptentially short-circuit parsing and use ReferenceEquals. if (ReferenceEquals(connection.ToString(), KeepAlive)) { return(ConnectionOptions.KeepAlive); } } for (var i = 0; i < connectionCount; i++) { var value = connection[i].AsSpan(); while (value.Length > 0) { int offset; char c = '\0'; // Skip any spaces and empty values. for (offset = 0; offset < value.Length; offset++) { c = value[offset]; if (c != ' ' && c != ',') { break; } } // Skip last read char. offset++; if ((uint)offset > (uint)value.Length) { // Consumed enitre string, move to next. break; } // Remove leading spaces or empty values. value = value.Slice(offset); c = ToLowerCase(c); var byteValue = MemoryMarshal.AsBytes(value); offset = 0; var potentialConnectionOptions = ConnectionOptions.None; if (c == 'k' && byteValue.Length >= (2 * sizeof(ulong) + sizeof(ushort))) { if (ReadLowerCaseUInt64(byteValue, lowerCaseKeep) == keepAliveStart) { offset += sizeof(ulong) / 2; byteValue = byteValue.Slice(sizeof(ulong)); if (ReadLowerCaseUInt64(byteValue) == keepAliveMiddle) { offset += sizeof(ulong) / 2; byteValue = byteValue.Slice(sizeof(ulong)); if (ReadLowerCaseUInt16(byteValue) == keepAliveEnd) { offset += sizeof(ushort) / 2; potentialConnectionOptions = ConnectionOptions.KeepAlive; } } } } else if (c == 'u' && byteValue.Length >= (sizeof(ulong) + sizeof(uint))) { if (ReadLowerCaseUInt64(byteValue) == upgradeStart) { offset += sizeof(ulong) / 2; byteValue = byteValue.Slice(sizeof(ulong)); if (ReadLowerCaseUInt32(byteValue) == upgradeEnd) { offset += sizeof(uint) / 2; potentialConnectionOptions = ConnectionOptions.Upgrade; } } } else if (c == 'c' && byteValue.Length >= sizeof(ulong)) { if (ReadLowerCaseUInt64(byteValue) == closeEnd) { offset += sizeof(ulong) / 2; potentialConnectionOptions = ConnectionOptions.Close; } } if ((uint)offset >= (uint)value.Length) { // Consumed enitre string, move to next string. connectionOptions |= potentialConnectionOptions; break; } else { value = value.Slice(offset); } for (offset = 0; offset < value.Length; offset++) { c = value[offset]; if (c == ',') { break; } else if (c != ' ') { // Value contains extra chars; this is not the matched one. potentialConnectionOptions = ConnectionOptions.None; } } if ((uint)offset >= (uint)value.Length) { // Consumed enitre string, move to next string. connectionOptions |= potentialConnectionOptions; break; } else if (c == ',') { // Consumed value corretly. connectionOptions |= potentialConnectionOptions; // Skip comma. offset++; if ((uint)offset >= (uint)value.Length) { // Consumed enitre string, move to next string. break; } else { // Move to next value. value = value.Slice(offset); } } } } // If Connection is a single value, switch it for the interned value // in case the connection is long lived if (connectionOptions == ConnectionOptions.Upgrade) { headers.HeaderConnection = ConnectionValueUpgrade; } else if (connectionOptions == ConnectionOptions.KeepAlive) { headers.HeaderConnection = ConnectionValueKeepAlive; } else if (connectionOptions == ConnectionOptions.Close) { headers.HeaderConnection = ConnectionValueClose; } return(connectionOptions); }
internal static void WriteStruct <T>(this Process process, IntPtr address, T @struct, bool protectedPages = false) where T : unmanaged { process.WriteSpan(address, MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref @struct, 1)), protectedPages); }
private static async Task TestValidConversionAsync <T>(TiffFieldReader fieldReader, TiffImageFileDirectoryEntry entry, string methodName, T[] refData) where T : unmanaged { MethodInfo method1 = typeof(TiffFieldReader).GetMethod(methodName, new Type[] { typeof(TiffImageFileDirectoryEntry), typeof(bool), typeof(CancellationToken) }); MethodInfo method2 = typeof(TiffFieldReader).GetMethod(methodName, new Type[] { typeof(TiffImageFileDirectoryEntry), typeof(int), typeof(bool), typeof(CancellationToken) }); MethodInfo method3 = typeof(TiffFieldReader).GetMethod(methodName, new Type[] { typeof(TiffImageFileDirectoryEntry), typeof(int), typeof(Memory <T>), typeof(bool), typeof(CancellationToken) }); Assert.NotNull(method1); Assert.NotNull(method2); Assert.NotNull(method3); var delegate1 = (Func <TiffImageFileDirectoryEntry, bool, CancellationToken, ValueTask <TiffValueCollection <T> > >)method1.CreateDelegate(typeof(Func <TiffImageFileDirectoryEntry, bool, CancellationToken, ValueTask <TiffValueCollection <T> > >), fieldReader); var delegate2 = (Func <TiffImageFileDirectoryEntry, int, bool, CancellationToken, ValueTask <TiffValueCollection <T> > >)method2.CreateDelegate(typeof(Func <TiffImageFileDirectoryEntry, int, bool, CancellationToken, ValueTask <TiffValueCollection <T> > >), fieldReader); var delegate3 = (Func <TiffImageFileDirectoryEntry, int, Memory <T>, bool, CancellationToken, ValueTask>)method3.CreateDelegate(typeof(Func <TiffImageFileDirectoryEntry, int, Memory <T>, bool, CancellationToken, ValueTask>), fieldReader); // Test basic overload TiffValueCollection <T> testData = await delegate1(entry, false, default); Assert.True(MemoryMarshal.AsBytes(refData.AsSpan()).SequenceEqual(MemoryMarshal.AsBytes(testData.ToArray().AsSpan()))); // Test overload with sizeLimit argument int sizeLimit = refData.Length / 2; testData = await delegate2(entry, sizeLimit, false, default); Assert.Equal(sizeLimit, testData.Count); Assert.True(MemoryMarshal.AsBytes(refData.AsSpan(0, sizeLimit)).SequenceEqual(MemoryMarshal.AsBytes(testData.ToArray().AsSpan()))); // Test overload with external buffer int offset = refData.Length / 4; T[] testArray = new T[sizeLimit]; await delegate3(entry, offset, testArray, false, default); Assert.True(MemoryMarshal.AsBytes(refData.AsSpan(offset, sizeLimit)).SequenceEqual(MemoryMarshal.AsBytes(testArray.AsSpan()))); // Test invalid parameter Assert.Throws <ArgumentOutOfRangeException>("offset", () => delegate3(entry, -1, new T[entry.ValueCount], false, default)); Assert.Throws <ArgumentOutOfRangeException>("offset", () => delegate3(entry, (int)(entry.ValueCount + 1), new T[1], false, default)); Assert.Throws <ArgumentOutOfRangeException>("destination", () => delegate3(entry, 0, new T[entry.ValueCount + 1], false, default)); // Now do it again with the sync version if (methodName.EndsWith("Async", StringComparison.Ordinal)) { methodName = methodName.Substring(0, methodName.Length - 5); MethodInfo method4 = typeof(TiffFieldReader).GetMethod(methodName, new Type[] { typeof(TiffImageFileDirectoryEntry), typeof(bool) }); MethodInfo method5 = typeof(TiffFieldReader).GetMethod(methodName, new Type[] { typeof(TiffImageFileDirectoryEntry), typeof(int), typeof(bool) }); MethodInfo method6 = typeof(TiffFieldReader).GetMethod(methodName, new Type[] { typeof(TiffImageFileDirectoryEntry), typeof(int), typeof(Memory <T>), typeof(bool) }); Assert.NotNull(method4); Assert.NotNull(method5); Assert.NotNull(method6); var delegate4 = (Func <TiffImageFileDirectoryEntry, bool, TiffValueCollection <T> >)method4.CreateDelegate(typeof(Func <TiffImageFileDirectoryEntry, bool, TiffValueCollection <T> >), fieldReader); var delegate5 = (Func <TiffImageFileDirectoryEntry, int, bool, TiffValueCollection <T> >)method5.CreateDelegate(typeof(Func <TiffImageFileDirectoryEntry, int, bool, TiffValueCollection <T> >), fieldReader); var delegate6 = (Action <TiffImageFileDirectoryEntry, int, Memory <T>, bool>)method6.CreateDelegate(typeof(Action <TiffImageFileDirectoryEntry, int, Memory <T>, bool>), fieldReader); // Test basic overload testData = delegate4(entry, false); Assert.True(MemoryMarshal.AsBytes(refData.AsSpan()).SequenceEqual(MemoryMarshal.AsBytes(testData.ToArray().AsSpan()))); // Test overload with sizeLimit argument sizeLimit = refData.Length / 2; testData = delegate5(entry, sizeLimit, false); Assert.Equal(sizeLimit, testData.Count); Assert.True(MemoryMarshal.AsBytes(refData.AsSpan(0, sizeLimit)).SequenceEqual(MemoryMarshal.AsBytes(testData.ToArray().AsSpan()))); // Test overload with external buffer offset = refData.Length / 4; testArray = new T[sizeLimit]; delegate6(entry, offset, testArray, false); Assert.True(MemoryMarshal.AsBytes(refData.AsSpan(offset, sizeLimit)).SequenceEqual(MemoryMarshal.AsBytes(testArray.AsSpan()))); // Test invalid parameter Assert.Throws <ArgumentOutOfRangeException>("offset", () => delegate6(entry, -1, new T[entry.ValueCount], false)); Assert.Throws <ArgumentOutOfRangeException>("offset", () => delegate6(entry, (int)(entry.ValueCount + 1), new T[1], false)); Assert.Throws <ArgumentOutOfRangeException>("destination", () => delegate6(entry, 0, new T[entry.ValueCount + 1], false)); } }
/// <summary> /// Converts bool array to an array of integers. /// </summary> /// <param name="array">The bool array.</param> /// <param name="dest">The destination array of integers.</param> public static void ConvertToIntArray(Span <bool> array, Span <sbyte> dest) => MemoryMarshal.AsBytes(array).CopyTo(MemoryMarshal.AsBytes(dest));
public static string ToAnsiString(this ReadOnlySpan <sbyte> source) => System.Text.Encoding.Default.GetString(MemoryMarshal.AsBytes(source.Slice(0, source.GetAnsiStringLength())));
/// <summary> /// Returns the a copy of the image pixels as a byte array in row-major order. /// </summary> /// <typeparam name="TPixel">The pixel format.</typeparam> /// <param name="source">The source image</param> /// <returns>A copy of the pixel data as bytes from this frame.</returns> /// <exception cref="ArgumentNullException">Thrown if the stream is null.</exception> public static byte[] SavePixelData <TPixel>(this ImageFrame <TPixel> source) where TPixel : struct, IPixel <TPixel> => MemoryMarshal.AsBytes(source.GetPixelSpan()).ToArray();
public bool IsHeaderValid() { Span <InnerHeader> spanHeader = MemoryMarshal.CreateSpan(ref this, 1); return(XXHash128.ComputeHash(MemoryMarshal.AsBytes(spanHeader).Slice(0, Unsafe.SizeOf <InnerHeader>() - Unsafe.SizeOf <Hash128>())) == HeaderHash); }
public Buffer(Clyde clyde, BufferTarget type, BufferUsageHint usage, Span <T> initialize, string name = null) : base(clyde, type, usage, MemoryMarshal.AsBytes(initialize), name) { }