public void HeaderChunk_ComesFirst() { // arrange var testFile = TestFile.Create(TestImages.Png.PngWithMetadata); using Image <Rgba32> input = testFile.CreateRgba32Image(); using var memStream = new MemoryStream(); // act input.Save(memStream, PngEncoder); // assert memStream.Position = 0; Span <byte> bytesSpan = memStream.ToArray().AsSpan(8); // Skip header. BinaryPrimitives.ReadInt32BigEndian(bytesSpan.Slice(0, 4)); var type = (PngChunkType)BinaryPrimitives.ReadInt32BigEndian(bytesSpan.Slice(4, 4)); Assert.Equal(PngChunkType.Header, type); }
public int ReadInt32() { if (CurrentType != AmqpToken.Array || _itemType != AmqpType.Int) { throw new InvalidOperationException(); } if (_currentLength < sizeof(int)) { throw new InvalidOperationException(); } int value = BinaryPrimitives.ReadInt32BigEndian(_buffer.Slice(_current)); _current += sizeof(int); _currentLength -= sizeof(int); if (_currentLength == 0) { CurrentType = AmqpToken.ArrayEnd; } return(value); }
// Only called from the IPv6Helper, only parse the canonical format internal static int ParseHostNumber(ReadOnlySpan <char> str, int start, int end) { Span <byte> numbers = stackalloc byte[NumberOfLabels]; for (int i = 0; i < numbers.Length; ++i) { int b = 0; char ch; for (; (start < end) && (ch = str[start]) != '.' && ch != ':'; ++start) { b = (b * 10) + ch - '0'; } numbers[i] = (byte)b; ++start; } return(BinaryPrimitives.ReadInt32BigEndian(numbers)); }
public static void RegisterAllBigEndian() { Register(BinaryPrimitives.ReadInt16BigEndian, BinaryPrimitives.WriteInt16BigEndian); Register(BinaryPrimitives.ReadInt32BigEndian, BinaryPrimitives.WriteInt32BigEndian); Register(BinaryPrimitives.ReadInt64BigEndian, BinaryPrimitives.WriteInt64BigEndian); Register(BinaryPrimitives.ReadUInt16BigEndian, BinaryPrimitives.WriteUInt16BigEndian); Register(BinaryPrimitives.ReadUInt32BigEndian, BinaryPrimitives.WriteUInt32BigEndian); Register(BinaryPrimitives.ReadInt64BigEndian, BinaryPrimitives.WriteInt64BigEndian); Register(BinaryPrimitives.ReadUInt64BigEndian, BinaryPrimitives.WriteUInt64BigEndian); Register( src => BitConverter.Int32BitsToSingle(BinaryPrimitives.ReadInt32BigEndian(src)), (dst, val) => BinaryPrimitives.WriteInt32BigEndian(dst, BitConverter.SingleToInt32Bits(val))); Register( src => BitConverter.Int64BitsToDouble(BinaryPrimitives.ReadInt64BigEndian(src)), (dst, val) => BinaryPrimitives.WriteInt64BigEndian(dst, BitConverter.DoubleToInt64Bits(val))); IsAlreadySet = true; Endianness = Endianness.BigEndian; }
private async ValueTask ReadFrameAsync(CancellationToken cancellationToken) { await InnerTransport.ReadAllAsync(HeaderBuf, 0, HeaderSize, cancellationToken); int size = BinaryPrimitives.ReadInt32BigEndian(HeaderBuf); if ((0 > size) || (size > Configuration.MaxFrameSize)) // size must be in the range 0 to allowed max { throw new TTransportException(TTransportException.ExceptionType.Unknown, $"Maximum frame size exceeded ({size} bytes)"); } UpdateKnownMessageSize(size + HeaderSize); ReadBuffer.SetLength(size); ReadBuffer.Seek(0, SeekOrigin.Begin); ArraySegment <byte> bufSegment; ReadBuffer.TryGetBuffer(out bufSegment); await InnerTransport.ReadAllAsync(bufSegment.Array, 0, size, cancellationToken); }
public void Chunk_ComesBeforePlteAndIDat(object chunkTypeObj) { // arrange var chunkType = (PngChunkType)chunkTypeObj; var testFile = TestFile.Create(TestImages.Png.PngWithMetadata); using Image <Rgba32> input = testFile.CreateRgba32Image(); using var memStream = new MemoryStream(); // act input.Save(memStream, PngEncoder); // assert memStream.Position = 0; Span <byte> bytesSpan = memStream.ToArray().AsSpan(8); // Skip header. bool palFound = false; bool dataFound = false; while (bytesSpan.Length > 0) { int length = BinaryPrimitives.ReadInt32BigEndian(bytesSpan.Slice(0, 4)); var type = (PngChunkType)BinaryPrimitives.ReadInt32BigEndian(bytesSpan.Slice(4, 4)); if (chunkType == type) { Assert.False(palFound || dataFound, $"{chunkType} chunk should come before data and palette chunk"); } switch (type) { case PngChunkType.Data: dataFound = true; break; case PngChunkType.Palette: palFound = true; break; } bytesSpan = bytesSpan.Slice(4 + 4 + length + 4); } }
private object ReadValueFromStream(Stream stream, Type propertyType, Type typeSerializerType) { if (typeSerializerType != null) { var typeSerializer = this.GetTypeSerializer(typeSerializerType); return(typeSerializer.ReadObject(stream, propertyType)); } if (propertyType == typeof(byte)) { return(stream.ReadByte()); } else if (propertyType == typeof(short)) { return(BinaryPrimitives.ReadInt16BigEndian(this.ReadBytes(stream, sizeof(short)))); } else if (propertyType == typeof(ushort)) { return(BinaryPrimitives.ReadUInt16BigEndian(this.ReadBytes(stream, sizeof(ushort)))); } else if (propertyType == typeof(int)) { return(BinaryPrimitives.ReadInt32BigEndian(this.ReadBytes(stream, sizeof(int)))); } else if (propertyType == typeof(uint)) { return(BinaryPrimitives.ReadUInt32BigEndian(this.ReadBytes(stream, sizeof(uint)))); } else if (propertyType == typeof(long)) { return(BinaryPrimitives.ReadInt64BigEndian(this.ReadBytes(stream, sizeof(long)))); } else if (propertyType == typeof(ulong)) { return(BinaryPrimitives.ReadUInt64BigEndian(this.ReadBytes(stream, sizeof(ulong)))); } else { throw new InvalidOperationException($"Cannot read type {propertyType.FullName} from stream without explicit serializer."); } }
public bool TryGetInt32Array(Span <int> buffer, out int written) { if (CurrentType != AmqpToken.Array || _itemType != AmqpType.Int) { throw new InvalidOperationException(); } written = _currentLength / sizeof(int); if (buffer.Length < written) { return(false); } var data = _buffer.Slice(_current, _currentLength); for (int i = 0; i < written; i++) { buffer[i] = BinaryPrimitives.ReadInt32BigEndian(data); data = data.Slice(sizeof(int)); } return(true); }
private static RtcpReceptionReport ParseRtcpReceptionReport(Span <byte> bytes, ref int idx) { var receptionReport = new RtcpReceptionReport(); receptionReport.SynchronizationSource = BinaryPrimitives.ReadUInt32BigEndian(bytes.Slice(idx)); idx += 4; receptionReport.FractionLost = bytes[idx]; idx += 1; receptionReport.CumulativeNumberOfPacketsLost = (uint)(bytes[idx] << 16 | bytes[idx + 1] << 8 | bytes[idx + 2]); idx += 3; receptionReport.ExtendedHighestSequenceNumberReceived = BinaryPrimitives.ReadUInt32BigEndian(bytes.Slice(idx)); idx += 4; receptionReport.InterarrivalJitter = BinaryPrimitives.ReadInt32BigEndian(bytes.Slice(idx)); idx += 4; receptionReport.LastSRTimestamp = BinaryPrimitives.ReadUInt32BigEndian(bytes.Slice(idx)); idx += 4; receptionReport.DelaySinceLastSR = BinaryPrimitives.ReadUInt32BigEndian(bytes.Slice(idx)); idx += 4; return(receptionReport); }
protected static int GetIterations(ReadOnlyMemory <byte> param, int defCount) { int iterCount = defCount; if (param.Length > 0) { if (param.Length % 4 != 0) { throw new ArgumentException("Invalid param to str2Key"); } iterCount = BinaryPrimitives.ReadInt32BigEndian(param.Span.Slice(param.Length - 4)); } if (iterCount == 0) { iterCount = int.MaxValue; } return(iterCount); }
public static bool Test(ReadOnlySpan <byte> header) { if (header.Length < HeaderSize) { return(false); } // TODO: check byte per byte instead? if (BinaryPrimitives.ReadInt32BigEndian(header) != 0x38425053) // "8BPS" { return(false); } if (BinaryPrimitives.ReadInt16BigEndian(header.Slice(sizeof(int))) != 1) { return(false); } return(true); }
public void Initialize() { if (!this.initialized) { Span <byte> buffer = stackalloc byte[4]; this.stream.Seek(0, SeekOrigin.Begin); this.stream.Read(buffer); Debug.Assert(buffer.SequenceEqual(Header)); this.stream.Read(buffer); var version = BinaryPrimitives.ReadInt32BigEndian(buffer); Debug.Assert(version == 2); for (int i = 1; i <= 256; i++) { this.stream.ReadAll(buffer); this.fanoutTable[i] = BinaryPrimitives.ReadInt32BigEndian(buffer); } } }
public override async ValueTask <HPacket> ReceivePacketAsync(HNode node) { using IMemoryOwner <byte> lengthOwner = MemoryPool <byte> .Shared.Rent(4); int lengthReceived = 0; do { lengthReceived = await node.ReceiveAsync(lengthOwner.Memory.Slice(0, 4)).ConfigureAwait(false); }while (lengthReceived == 0); if (lengthReceived != 4) { node.Dispose(); } int length = BinaryPrimitives.ReadInt32BigEndian(lengthOwner.Memory.Span); using IMemoryOwner <byte> bodyOwner = MemoryPool <byte> .Shared.Rent(length); Memory <byte> body = bodyOwner.Memory.Slice(0, length); int received = 0; do { received += await node.ReceiveAsync(bodyOwner.Memory.Slice(received, length - received)); }while (received != length); var packetData = new byte[sizeof(int) + length]; BinaryPrimitives.WriteInt32BigEndian(packetData, length); Buffer.BlockCopy(bodyOwner.Memory.ToArray(), 0, packetData, 4, length); if (node.IsWebSocket && node.Decrypter != null) { node.Decrypter.Process(packetData.AsSpan().Slice(5, 1)); node.Decrypter.Process(packetData.AsSpan().Slice(4, 1)); } return(CreatePacket(packetData)); }
public int GetOrCreate(ReadOnlySpan <byte> value) { var size = 1 + value.Length; //Todo: faire pool avec taille exacte using var array = MemoryPool <byte> .Shared.Rent(size); var valueBytes = array.Memory.Slice(0, size).Span; valueBytes[0] = (byte)RocksDbStorage.StoreType.Alias; value.CopyTo(valueBytes.Slice(1)); var exising = Storage.Database.Get(valueBytes); if (exising != null) { return(BinaryPrimitives.ReadInt32BigEndian(exising)); } lock (this) { exising = Storage.Database.Get(valueBytes); if (exising != null) { return(BinaryPrimitives.ReadInt32BigEndian(exising)); } WriteBatch b = new(); Storage.IncrementCounterValue(b, RocksDbStorage.Counter.LastAlias, Span <byte> .Empty); Storage.Database.Write(b); Span <byte> idBytes = stackalloc byte[4]; var id = (int)Storage.GetCounterValue(RocksDbStorage.Counter.LastAlias, Span <byte> .Empty); BinaryPrimitives.WriteInt32BigEndian(idBytes, id); Storage.Database.Put(valueBytes, idBytes); return(id); } }
eNaluAction iVideoTrackReader.writeNextNalu(EncodedBuffer dest) { if (EOF) { return(eNaluAction.EOF); } int naluLength; Span <byte> naluPayload; lock (clusters.syncRoot) { // Seek to the blob seek(ref readerState); // Read 4-bytes NALU length. Unlike mpeg4, MKV header never says it's 4 bytes, i.e. just guessing here. Span <byte> naluLengthSpan = stackalloc byte[4]; read(ref readerState, naluLengthSpan); naluLength = BinaryPrimitives.ReadInt32BigEndian(naluLengthSpan); Debug.Assert(readerState.bytesLeft >= naluLength); // Write NALU start code to mapped memory EmulationPrevention.writeStartCode4(dest.span, 0); // Write the payload naluPayload = dest.span.Slice(4, naluLength); read(ref readerState, naluPayload); } dest.setLength(naluLength + 4); // Parse the payload. The shared memory mapped by the driver is both readable and writeable. eNaluAction act = setBufferMetadata(dest, naluPayload); if (readerState.bytesLeft <= 0) { advance(); } return(act); }
public static bool TryRead <TSequence>(ref BufferReader <TSequence> reader, out int value, bool littleEndian = false) where TSequence : ISequence <ReadOnlyMemory <byte> > { var unread = reader.UnreadSegment; if (littleEndian) { if (BinaryPrimitives.TryReadInt32LittleEndian(unread, out value)) { reader.Advance(sizeof(int)); return(true); } } else if (BinaryPrimitives.TryReadInt32BigEndian(unread, out value)) { reader.Advance(sizeof(int)); return(true); } Span <byte> tempSpan = stackalloc byte[4]; var copied = BufferReader.Peek(reader, tempSpan); if (copied < 4) { value = default; return(false); } if (littleEndian) { value = BinaryPrimitives.ReadInt32LittleEndian(tempSpan); } else { value = BinaryPrimitives.ReadInt32BigEndian(tempSpan); } reader.Advance(sizeof(int)); return(true); }
public void ExcludeFilter_WithNone_DoesNotExcludeChunks() { // arrange var testFile = TestFile.Create(TestImages.Png.PngWithMetadata); using Image <Rgba32> input = testFile.CreateRgba32Image(); using var memStream = new MemoryStream(); var encoder = new PngEncoder() { ChunkFilter = PngChunkFilter.None, TextCompressionThreshold = 8 }; var expectedChunkTypes = new List <PngChunkType>() { PngChunkType.Header, PngChunkType.Gamma, PngChunkType.Palette, PngChunkType.InternationalText, PngChunkType.Text, PngChunkType.CompressedText, PngChunkType.Exif, PngChunkType.Physical, PngChunkType.Data, PngChunkType.End, }; // act input.Save(memStream, encoder); memStream.Position = 0; Span <byte> bytesSpan = memStream.ToArray().AsSpan(8); // Skip header. while (bytesSpan.Length > 0) { int length = BinaryPrimitives.ReadInt32BigEndian(bytesSpan.Slice(0, 4)); var chunkType = (PngChunkType)BinaryPrimitives.ReadInt32BigEndian(bytesSpan.Slice(4, 4)); Assert.True(expectedChunkTypes.Contains(chunkType), $"{chunkType} chunk should have been present"); bytesSpan = bytesSpan.Slice(4 + 4 + length + 4); } }
private bool ParseVBRI(out VBRInfo info) { info = new VBRInfo(); info.Channels = Channels; info.SampleRate = SampleRate; info.SampleCount = SampleCount; // VBRI is "fixed" size... Yay. :) Span <byte> buf = stackalloc byte[26]; if (Read(36, buf) != 26) { return(false); } int version = BinaryPrimitives.ReadInt16BigEndian(buf.Slice(4)); info.VBRDelay = BinaryPrimitives.ReadInt16BigEndian(buf.Slice(6)); info.VBRQuality = BinaryPrimitives.ReadInt16BigEndian(buf.Slice(8)); info.VBRBytes = BinaryPrimitives.ReadInt32BigEndian(buf.Slice(10)); info.VBRFrames = BinaryPrimitives.ReadInt32BigEndian(buf.Slice(14)); // TOC // entries int tocEntries = BinaryPrimitives.ReadInt16BigEndian(buf.Slice(18)); int tocScale = BinaryPrimitives.ReadInt16BigEndian(buf.Slice(20)); int tocEntrySize = BinaryPrimitives.ReadInt16BigEndian(buf.Slice(22)); int tocFramesPerEntry = BinaryPrimitives.ReadInt16BigEndian(buf.Slice(24)); int tocSize = tocEntries * tocEntrySize; var toc = new byte[tocSize]; if (Read(62, toc) != tocSize) { return(false); } return(true); }
public bool TryRead(out int value, bool littleEndian = false) { var unread = Unread; if (littleEndian) { if (BinaryPrimitives.TryReadInt32LittleEndian(unread, out value)) { Advance(sizeof(int)); return(true); } } else if (BinaryPrimitives.TryReadInt32BigEndian(unread, out value)) { Advance(sizeof(int)); return(true); } Span <byte> span = stackalloc byte[4]; var copied = CopyTo(this, span); if (copied < 4) { value = default; return(false); } if (littleEndian) { value = BinaryPrimitives.ReadInt32LittleEndian(span); } else { value = BinaryPrimitives.ReadInt32BigEndian(span); } Advance(sizeof(int)); return(true); }
private static void SetDataProtectionValue(KeyBag item, string blockIdentifier, ReadOnlySpan <byte> value) { if (item.DataProtection == null) { item.DataProtection = new DataProtectionKeyData(); } switch (blockIdentifier) { case KeyBagConstants.DpwtTag: item.DataProtection.Dpwt = BinaryPrimitives.ReadInt32BigEndian(value); break; case KeyBagConstants.DpicTag: item.DataProtection.Dpic = BinaryPrimitives.ReadInt32BigEndian(value); break; case KeyBagConstants.DpslTag: item.DataProtection.Dpsl = value.ToArray(); break; default: throw new InvalidDataException($"Unexpected block identifier \"{blockIdentifier}\""); } }
private void ProcessWindowUpdateFrame(FrameHeader frameHeader) { Debug.Assert(frameHeader.Type == FrameType.WindowUpdate); if (frameHeader.Length != FrameHeader.WindowUpdateLength) { throw new Http2ProtocolException(Http2ProtocolErrorCode.FrameSizeError); } int amount = BinaryPrimitives.ReadInt32BigEndian(_incomingBuffer.ActiveSpan) & 0x7FFFFFFF; Debug.Assert(amount >= 0); if (amount == 0) { throw new Http2ProtocolException(Http2ProtocolErrorCode.ProtocolError); } _incomingBuffer.Discard(frameHeader.Length); if (frameHeader.StreamId == 0) { _connectionWindow.AdjustCredit(amount); } else { Http2Stream http2Stream = GetStream(frameHeader.StreamId); if (http2Stream == null) { // Don't wait for completion, which could happen asynchronously. Task ignored = SendRstStreamAsync(frameHeader.StreamId, Http2ProtocolErrorCode.StreamClosed); return; } http2Stream.OnWindowUpdate(amount); } }
int ReadEndianBytes(bool isLittleEndian, byte[] bytes) { return(isLittleEndian ? BinaryPrimitives.ReadInt32LittleEndian(bytes) : BinaryPrimitives.ReadInt32BigEndian(bytes)); }
private static PngChunkType GetType(string text) { return((PngChunkType)BinaryPrimitives.ReadInt32BigEndian(Encoding.ASCII.GetBytes(text))); }
private static IEnumerable <(string, string[])> ProcessAtoms(Memory <byte> buffer) { var cursor = 0; var filename = default(string); while (cursor < buffer.Length) { var atom = new MP4Atom(buffer.Span.Slice(cursor)); cursor += atom.Size; if (atom.Name == "moov" || atom.Name == "udta") { foreach (var(fn, str) in ProcessAtoms(atom.Buffer)) { yield return(fn, str); } continue; } if (atom.Name == "meta") { filename = ProcessAtomsButDumber(atom.Buffer); } if (atom.Name != "Xtra") { continue; // moov -> udta -> Xtra } if (atom.Buffer.Length < 0x1F) { Console.Out.WriteLine("\nReplay is f****d\n"); continue; } var localCursor = 0; var blockSize = BinaryPrimitives.ReadInt32BigEndian(atom.Buffer.Span); if (blockSize != atom.Buffer.Length) { Console.Out.WriteLine("\nReplay has a lot of data?\n"); } localCursor += 4; var blockNameLength = BinaryPrimitives.ReadInt32BigEndian(atom.Buffer.Span.Slice(localCursor)); if (blockNameLength == 0) { continue; } localCursor += 4; var name = Encoding.ASCII.GetString(atom.Buffer.Span.Slice(localCursor, blockNameLength).ToArray()); localCursor += blockNameLength; if (name != "WM/EncodingSettings") { Console.Out.WriteLine("\nReplay is f****d\n"); continue; } var settingCount = BinaryPrimitives.ReadInt32BigEndian(atom.Buffer.Span.Slice(localCursor)); localCursor += 4; for (var i = 0; i < settingCount; ++i) { var encodedSettingLength = BinaryPrimitives.ReadInt32BigEndian(atom.Buffer.Span.Slice(localCursor)); if (encodedSettingLength == 0) { continue; } var type = BinaryPrimitives.ReadInt16BigEndian(atom.Buffer.Span.Slice(localCursor + 4)); if (type != 8) { Console.Out.WriteLine("\nNot Type 8?\n"); } var data = atom.Buffer.Span.Slice(localCursor + 6, encodedSettingLength - 6); var b64Str = Encoding.Unicode.GetString(data.ToArray()); localCursor += encodedSettingLength; yield return(filename, b64Str.Split(':')); } } }
public int ReadVirtualInt32() { return(BinaryPrimitives.ReadInt32BigEndian(GetVirtualReadOnlySpan(4))); }
public static int ToInt32(this ReadOnlySpan <byte> b, ref int index) { ReadOnlySpan <byte> byteSpan = b.Slice(index); var result = BufferWriter.LittleEndianStorage ? BinaryPrimitives.ReadInt32LittleEndian(byteSpan) : BinaryPrimitives.ReadInt32BigEndian(byteSpan); index += sizeof(int); return(result); }
public static float ToSingle(this ReadOnlySpan <byte> b, ref int index) { ReadOnlySpan <byte> byteSpan = b.Slice(index); var result = BitConverter.Int32BitsToSingle(BufferWriter.LittleEndianStorage ? BinaryPrimitives.ReadInt32LittleEndian(byteSpan) : BinaryPrimitives.ReadInt32BigEndian(byteSpan)); index += sizeof(float); return(result); }
private static int ReadHeader(byte[] buffer) { return(BinaryPrimitives.ReadInt32BigEndian(buffer)); }
/// <summary> /// Reads a <see cref="Int32" /> from a buffer. /// </summary> /// <param name="buffer">The buffer.</param> /// <param name="useNbo">If <c>true</c> will make most significant byte first.</param> /// <returns></returns> public static int ToInt32(ReadOnlySpan <byte> buffer, bool useNbo) { return(useNbo ? BinaryPrimitives.ReadInt32BigEndian(buffer) : BinaryPrimitives.ReadInt32LittleEndian(buffer)); }