public unsafe static ulong GetUInt64(this ReadableBuffer buffer) { byte *addr; ulong value; int consumed, len = buffer.Length; if (buffer.IsSingleSpan) { // It fits! addr = (byte *)buffer.FirstSpan.UnsafePointer; } else if (len < 128) // REVIEW: What's a good number { var data = stackalloc byte[len]; buffer.CopyTo(new Span <byte>(data, len)); addr = data; // memory allocated via stackalloc is valid and // intact until the end of the method; we don't need to worry about scope } else { // Heap allocated copy to parse into array (should be rare) var arr = buffer.ToArray(); if (!InvariantParser.TryParse(arr, 0, FormattingData.InvariantUtf8, Format.Parsed.HexUppercase, out value, out consumed)) { throw new InvalidOperationException(); } return(value); } if (!InvariantParser.TryParse(addr, 0, len, FormattingData.InvariantUtf8, Format.Parsed.HexUppercase, out value, out consumed)) { throw new InvalidOperationException(); } return(value); }
/// <summary> /// Decodes the utf8 encoded bytes in the <see cref="ReadableBuffer"/> into a <see cref="string"/> /// </summary> /// <param name="buffer">The buffer to decode</param> public static string GetUtf8String(this ReadableBuffer buffer) { if (buffer.IsEmpty) { return(null); } // Assign 'textSpan' to something formally stack-referring. // The default classification is "returnable, not referring to stack", we want the opposite in this case. ReadOnlySpan <byte> textSpan = stackalloc byte[0]; if (buffer.IsSingleSpan) { textSpan = buffer.First.Span; } else if (buffer.Length < 128) // REVIEW: What's a good number { Span <byte> destination = stackalloc byte[128]; buffer.CopyTo(destination); // We are able to cast because buffer.Length < 128 textSpan = destination.Slice(0, (int)buffer.Length); } else { // Heap allocated copy to parse into array (should be rare) textSpan = new ReadOnlySpan <byte>(buffer.ToArray()); } return(new Utf8String(textSpan).ToString()); }
/// <summary> /// Decodes the utf8 encoded bytes in the <see cref="ReadableBuffer"/> into a <see cref="string"/> /// </summary> /// <param name="buffer">The buffer to decode</param> public static unsafe string GetUtf8String(this ReadableBuffer buffer) { if (buffer.IsEmpty) { return(null); } ReadOnlySpan <byte> textSpan; if (buffer.IsSingleSpan) { textSpan = buffer.First.Span; } else if (buffer.Length < 128) // REVIEW: What's a good number { var data = stackalloc byte[128]; var destination = new Span <byte>(data, 128); buffer.CopyTo(destination); textSpan = destination.Slice(0, buffer.Length); } else { // Heap allocated copy to parse into array (should be rare) textSpan = new ReadOnlySpan <byte>(buffer.ToArray()); } return(new Utf8String(textSpan).ToString()); }
public unsafe static uint GetUInt32(this ReadableBuffer buffer) { ReadOnlySpan <byte> textSpan; if (buffer.IsSingleSpan) { // It fits! textSpan = buffer.FirstSpan; } else if (buffer.Length < 128) // REVIEW: What's a good number { var data = stackalloc byte[128]; var destination = new Span <byte>(data, 128); buffer.CopyTo(destination); textSpan = destination.Slice(0, buffer.Length); } else { // Heap allocated copy to parse into array (should be rare) textSpan = new ReadOnlySpan <byte>(buffer.ToArray()); } uint value; var utf8Buffer = new Utf8String(textSpan); if (!InvariantParser.TryParse(utf8Buffer, out value)) { throw new InvalidOperationException(); } return(value); }
public unsafe static uint GetUInt32(this ReadableBuffer buffer) { var textSpan = default(ReadOnlySpan <byte>); if (buffer.IsSingleSpan) { // It fits! var span = buffer.FirstSpan; textSpan = new ReadOnlySpan <byte>(span.Array, span.Offset, span.Length); } else if (buffer.Length < 128) // REVIEW: What's a good number { var target = stackalloc byte[128]; buffer.CopyTo(target, length: 128); textSpan = new ReadOnlySpan <byte>(target, buffer.Length); } else { // Heap allocated copy to parse into array (should be rare) textSpan = new ReadOnlySpan <byte>(buffer.ToArray()); } uint value; var utf8Buffer = new Utf8String(textSpan); if (!InvariantParser.TryParse(utf8Buffer, out value)) { throw new InvalidOperationException(); } return(value); }
public static Span <byte> ToSpan(this ReadableBuffer buffer) { if (buffer.IsSingleSpan) { return(buffer.First.Span); } return(buffer.ToArray()); }
public override void SetClientRandom(ReadableBuffer readableBuffer) { if (readableBuffer.Length != Hello.RandomLength) { Alerts.AlertException.ThrowAlert(Alerts.AlertLevel.Fatal, Alerts.AlertDescription.illegal_parameter, "Invalid client random length"); } _clientRandom = readableBuffer.ToArray(); }
private ReadOnlySpan <byte> ConvertBufferToSpan(ReadableBuffer buffer) { if (buffer.IsSingleSpan) { return(buffer.First.Span); } return(buffer.ToArray()); }
public void SetHeader(ref ReadableBuffer key, ref ReadableBuffer value) { string headerKey = GetHeaderKey(ref key); _headers[headerKey] = new HeaderValue { Raw = value.ToArray() }; }
/// <summary> /// Parses a <see cref="ulong"/> from the specified <see cref="ReadableBuffer"/> /// </summary> /// <param name="buffer">The <see cref="ReadableBuffer"/> to parse</param> public unsafe static ulong GetUInt64(this ReadableBuffer buffer) { byte *addr; ulong value; int consumed, len = buffer.Length; if (buffer.IsSingleSpan) { // It fits! void *pointer; ArraySegment <byte> data; if (buffer.First.TryGetPointer(out pointer)) { if (!PrimitiveParser.TryParseUInt64((byte *)pointer, 0, len, EncodingData.InvariantUtf8, Format.Parsed.HexUppercase, out value, out consumed)) { throw new InvalidOperationException(); } } else if (buffer.First.TryGetArray(out data)) { if (!PrimitiveParser.TryParseUInt64(data.Array, 0, EncodingData.InvariantUtf8, Format.Parsed.HexUppercase, out value, out consumed)) { throw new InvalidOperationException(); } } else { throw new InvalidOperationException(); } } else if (len < 128) // REVIEW: What's a good number { var data = stackalloc byte[len]; buffer.CopyTo(new Span <byte>(data, len)); addr = data; if (!PrimitiveParser.TryParseUInt64(addr, 0, len, EncodingData.InvariantUtf8, Format.Parsed.HexUppercase, out value, out consumed)) { throw new InvalidOperationException(); } } else { // Heap allocated copy to parse into array (should be rare) var arr = buffer.ToArray(); if (!PrimitiveParser.TryParseUInt64(arr, 0, EncodingData.InvariantUtf8, Format.Parsed.HexUppercase, out value, out consumed)) { throw new InvalidOperationException(); } return(value); } return(value); }
private static Leto.Alerts.AlertException CheckForAlert(ReadableBuffer result) { var returnMessage = new Span <byte>(result.ToArray()); var header = returnMessage.Read <Leto.RecordLayer.RecordHeader>(); Assert.Equal(Leto.RecordLayer.RecordType.Alert, header.RecordType); var exception = new Leto.Alerts.AlertException(returnMessage.Slice(Marshal.SizeOf <Leto.RecordLayer.RecordHeader>())); return(exception); }
public byte[] GetBytes() { int len = GetPayloadLength(); if (len == 0) { return(NilBytes); } ApplyMask(); return(_buffer.ToArray()); }
public byte[] GetBytes() { int len = GetTotalBytes(); if (len == 0) { return(NilBytes); } ApplyMask(); return(buffer.ToArray()); }
/// <summary> /// Parses a <see cref="ulong"/> from the specified <see cref="ReadableBuffer"/> /// </summary> /// <param name="buffer">The <see cref="ReadableBuffer"/> to parse</param> public unsafe static ulong GetUInt64(this ReadableBuffer buffer) { byte *addr; ulong value; var len = buffer.Length; if (buffer.IsSingleSpan) { // It fits! fixed(byte *source = &buffer.First.Span.DangerousGetPinnableReference()) { // We are able to cast because IsSingleSpan and span size is int if (!PrimitiveParser.InvariantUtf8.TryParseUInt64(source, (int)len, out value)) { ThrowInvalidOperation(); } } } else if (len < 128) // REVIEW: What's a good number { // We are able to cast because len < 128 var length = (int)len; var data = stackalloc byte[length]; buffer.CopyTo(new Span <byte>(data, length)); addr = data; if (!PrimitiveParser.InvariantUtf8.TryParseUInt64(addr, length, out value)) { throw new InvalidOperationException(); } } else { // Heap allocated copy to parse into array (should be rare) var arr = buffer.ToArray(); if (!PrimitiveParser.InvariantUtf8.TryParseUInt64(arr, out value)) { throw new InvalidOperationException(); } return(value); } return(value); }