public static P3Int16 Read(ReadOnlySpan <byte> span) { return(new P3Int16( BinaryPrimitives.ReadInt16LittleEndian(span), BinaryPrimitives.ReadInt16LittleEndian(span.Slice(2)), BinaryPrimitives.ReadInt16LittleEndian(span.Slice(4)))); }
public static void GetSetData(MutagenFrame frame, IANavigationMeshInternal item) { var parentBytes = frame.GetSpan( amount: 8, offset: 8, readSafe: true); switch (item) { case IWorldspaceNavigationMesh worldspace: { var ret = WorldspaceNavigationMeshData.CreateFromBinary(frame); ret.Parent = FormKeyBinaryTranslation.Instance.Parse(parentBytes.Slice(0, 4), frame.MetaData.MasterReferences !); ret.Coordinates = new P2Int16( BinaryPrimitives.ReadInt16LittleEndian(parentBytes.Slice(4)), BinaryPrimitives.ReadInt16LittleEndian(parentBytes.Slice(6))); worldspace.Data = ret; } break; case ICellNavigationMesh cell: { var ret = CellNavigationMeshData.CreateFromBinary(frame); ret.UnusedWorldspaceParent = FormKeyBinaryTranslation.Instance.Parse(parentBytes.Slice(0, 4), frame.MetaData.MasterReferences !); ret.Parent = FormKeyBinaryTranslation.Instance.Parse(parentBytes.Slice(4, 4), frame.MetaData.MasterReferences !); cell.Data = ret; } break; default: throw new NotImplementedException(); } }
public override bool Apply(OpCode code, int operandsize, Span <byte> operands) { if (operandsize == 4) { int val = BinaryPrimitives.ReadInt32LittleEndian(operands); if (code.Equals(OpCodes.Call) || code.Equals(OpCodes.Callvirt) || code.Equals(OpCodes.Newobj)) { var b = Info.Module.ResolveMethod(val); IL.Emit(code, b as MethodInfo); } else { IL.Emit(code, val); } } else if (operandsize == 2) { short val = BinaryPrimitives.ReadInt16LittleEndian(operands); IL.Emit(code, val); } else if (operandsize == 1) { byte val = operands[0]; IL.Emit(code, val); } else if (operandsize == 0) { IL.Emit(code); } else { return(false); } return(true); }
static bool AssemblyIfNgenIsInReleaseMode(string assemblyPath, PEReader reader) { var managedNativeHeaderDirectory = reader.PEHeaders.CorHeader.ManagedNativeHeaderDirectory; if (managedNativeHeaderDirectory.Size != 0) { var rva = managedNativeHeaderDirectory.RelativeVirtualAddress; var data = reader.GetSectionData(rva); byte[] magicBytes = data.GetContent(0, 4).ToArray(); int magic = BinaryPrimitives.ReadInt32LittleEndian(magicBytes); if (magic == CORCOMPILE_SIGNATURE) { // Extract CORCOMPILE_VERSION_INFO byte[] versionInfoHeader = data.GetContent(40, 8).ToArray(); int corVersionRva = BinaryPrimitives.ReadInt32LittleEndian(versionInfoHeader); var corVersionData = reader.GetSectionData(corVersionRva); byte[] wBuildData = corVersionData.GetContent(16, 2).ToArray(); int wBuild = BinaryPrimitives.ReadInt16LittleEndian(wBuildData); if (wBuild == 0) { return(false); } } } return(true); }
private bool IsSupportedFileFormat(ReadOnlySpan <byte> header) { short fileTypeMarker = BinaryPrimitives.ReadInt16LittleEndian(header); return(header.Length >= this.HeaderSize && (fileTypeMarker == BmpConstants.TypeMarkers.Bitmap || fileTypeMarker == BmpConstants.TypeMarkers.BitmapArray)); }
public IList <ArchiveFileInfo> Load(Stream input) { // Determine XFSA version and parser var buffer = new byte[4]; input.Position += 4; input.Read(buffer, 0, 4); var directoryEntriesOffset = BinaryPrimitives.ReadInt32LittleEndian(buffer); input.Position += 0x10; input.Read(buffer, 0, 2); var directoryEntriesCount = BinaryPrimitives.ReadInt16LittleEndian(buffer); input.Position += directoryEntriesOffset - 0x1A; var directoryDecompressedSize = Level5Compressor.PeekDecompressedSize(input); input.Position -= directoryEntriesOffset; var directoryEntrySize = directoryDecompressedSize / directoryEntriesCount; if (directoryEntrySize == _directoryEntrySizev1) { _xfsaParser = new XFSA1(); } else if (directoryEntrySize == _directoryEntrySizev2) { _xfsaParser = new XFSA2(); } else { throw new InvalidOperationException("Unknown XFSA version."); } return(_xfsaParser.Load(input)); }
/// <summary> /// Reads a 16-bit <see cref="short"/> written by <see cref="Write(short)"/>. /// </summary> /// <exception cref="EndOfMessageException"></exception> public static short ReadInt16(this IBitBuffer buffer) { Span <byte> tmp = stackalloc byte[sizeof(short)]; buffer.Read(tmp); return(BinaryPrimitives.ReadInt16LittleEndian(tmp)); }
/// <summary> /// Reads a <see cref="short"/> stored in 1 to 16 bits, written by <see cref="Write(short, int)"/>. /// </summary> /// <exception cref="EndOfMessageException"></exception> public static short ReadInt16(this IBitBuffer buffer, int bitCount) { Span <byte> tmp = stackalloc byte[sizeof(short)]; if (bitCount == tmp.Length * 8) { buffer.Read(tmp); return(BinaryPrimitives.ReadInt16LittleEndian(tmp)); } tmp.Clear(); buffer.ReadBits(tmp, bitCount, tmp.Length * 8); short value = BinaryPrimitives.ReadInt16LittleEndian(tmp); int signBit = 1 << (bitCount - 1); if ((value & signBit) == 0) { return(value); // positive } // negative unchecked { int mask = ((ushort)-1) >> (17 - bitCount); int nValue = ((ushort)value & mask) + 1; return((short)-nValue); } }
public static short ReadInt16LE(this Span <byte> span, ref int pos) { var v = BinaryPrimitives.ReadInt16LittleEndian(span.Slice(pos, 2)); pos += 2; return(v); }
private int ReadRegister(byte registerAddress, I2cDevice device) { var word = new byte[2]; device.ReadAtRegisterAddress(registerAddress, word); return(BitConverter.IsLittleEndian ? BinaryPrimitives.ReadInt16LittleEndian(word) : BinaryPrimitives.ReadInt16BigEndian(word)); }
public override bool Apply(OpCode code, int operandsize, Span <byte> operands) { if (ILHelper.IsArgS(code)) { if (operandsize == 4) { int val = BinaryPrimitives.ReadInt32LittleEndian(operands); IL.Emit(code, GetIndex(val)); } else if (operandsize == 2) { short val = BinaryPrimitives.ReadInt16LittleEndian(operands); IL.Emit(code, (short)GetIndex(val)); } else if (operandsize == 1) { byte val = operands[0]; IL.Emit(code, (byte)GetIndex(val)); } } else if (ILHelper.IsArgNotS(code)) { (OpCode code2, int val) = ILHelper.ConvertToS(code); IL.Emit(code2, (byte)GetIndex(val)); } else { return(false); } return(true); }
/// <summary> /// Reads the specified number of bits into an <see cref="short"/> without advancing the read position. /// </summary> public static short PeekInt16(this IBitBuffer buffer, int bitCount) { Span <byte> tmp = stackalloc byte[sizeof(short)]; buffer.PeekBits(tmp, bitCount, tmp.Length * 8); return(BinaryPrimitives.ReadInt16LittleEndian(tmp)); }
public short ReadInt16() { var val = BinaryPrimitives.ReadInt16LittleEndian(_buffer.Span.Slice((int)Position, 2)); Position += 2; return(val); }
private short ReadInt16(Register register) { Span <byte> val = stackalloc byte[2]; Read(register, val); return(BinaryPrimitives.ReadInt16LittleEndian(val)); }
/// <summary> /// Read int from register. /// </summary> /// <param name="register">The register.</param> /// <returns>An int.</returns> public short ReadInt16(byte register) { Span <byte> val = stackalloc byte[2]; this.Read(register, val); return(BinaryPrimitives.ReadInt16LittleEndian(val)); }
/// <summary> /// Refresh the channel statuses. /// </summary> private void RefreshChannelStatuses() { // Pause the auto-refresh to prevent possible collisions. var periodRefresh = PeriodRefresh; PeriodRefresh = 0; Span <byte> buffer = stackalloc byte[2]; _device.Read(buffer); short rawStatus = BinaryPrimitives.ReadInt16LittleEndian(buffer); bool isStatusChanged = false; for (var i = 0; i < CHANNELS_NUMBER; i++) { bool status = ((1 << i) & rawStatus) > 0; if (_statuses[(Channels)i] != status) { _statuses[(Channels)i] = status; isStatusChanged = true; } } if (isStatusChanged) { OnChannelStatusesChanged(); } // Resume the auto-refresh. PeriodRefresh = periodRefresh; }
public static short ReadInt16LittleEndian(this MemoryUnit self, int offset) { Span <byte> buf = stackalloc byte[2]; self.Read(offset, buf); return(BinaryPrimitives.ReadInt16LittleEndian(buf)); }
public short ReadInt16() { Align(2); return(IsLittleEndian ? BinaryPrimitives.ReadInt16LittleEndian(ReadBytes(2)) : BinaryPrimitives.ReadInt16BigEndian(ReadBytes(2))); }
static partial void FillBinaryBoundDataCustom(MutagenFrame frame, IPlacedObjectInternal item) { var header = frame.ReadSubrecordFrame(); if (header.Content.Length != 4) { throw new ArgumentException($"Unexpected data header length: {header.Content.Length} != 4"); } item.Unknown = BinaryPrimitives.ReadInt16LittleEndian(header.Content.Slice(2)); while (frame.Reader.TryReadSubrecord(out var subHeader)) { switch (subHeader.RecordTypeInt) { case RecordTypeInts.LNAM: item.LightingTemplate = FormKeyBinaryTranslation.Instance.Parse(frame); break; case RecordTypeInts.INAM: item.ImageSpace = FormKeyBinaryTranslation.Instance.Parse(frame); break; case RecordTypeInts.XLRM: item.LinkedRooms.Add(new FormLink <PlacedObject>(FormKeyBinaryTranslation.Instance.Parse(frame))); break; default: frame.Reader.Position -= subHeader.HeaderLength; return; } } }
public short GetShort(int offset) { AssertOffsetAndLength(offset, sizeof(short)); var span = _buffer.ReadOnlySpan.Slice(offset); return(BinaryPrimitives.ReadInt16LittleEndian(span)); }
public async Task <short> ReadInt16Async() { var buffer = await ReadBytesAsync(sizeof(short)) .ConfigureAwait(false); return(BinaryPrimitives.ReadInt16LittleEndian(buffer.Span)); }
public short ReadInt16() { Span <byte> bytes = stackalloc byte[sizeof(long)]; CopyTo <short>(bytes); return(BinaryPrimitives.ReadInt16LittleEndian(bytes)); }
public short ReadInt16() { var output = BinaryPrimitives.ReadInt16LittleEndian(Buffer.AsSpan(ReadPosition)); Position += sizeof(short); return(output); }
private async void SavePvb(IFileSystem archiveFileSystem, IList <PointMapping> mappings) { var pvbPath = archiveFileSystem .EnumeratePaths(UPath.Root, "*.pvb", SearchOption.TopDirectoryOnly, SearchTarget.File) .First(); var pvbStream = await archiveFileSystem.OpenFileAsync(pvbPath); var copy = new byte[0x10]; pvbStream.Read(copy, 0, copy.Length); // Open output var output = new MemoryStream(); await using var bw = new BinaryWriterX(output, true); // Write original data BinaryPrimitives.WriteInt16LittleEndian(copy.AsSpan(0xC), (short)mappings.Count); bw.Write(copy); var oldOffset = BinaryPrimitives.ReadInt16LittleEndian(copy.AsSpan(0x8)); copy = new byte[oldOffset - 0x10]; pvbStream.Read(copy, 0, copy.Length); bw.Write(copy); // Write compressed point mappings WriteMultipleCompressed(bw, mappings, Level5CompressionMethod.Lz10); // Set output to original file archiveFileSystem.SetFileData(pvbPath, output); }
/// <summary> /// Returns a short. /// </summary> /// <param name="index">The index.</param> /// <returns>The value.</returns> public short GetInt16(int index) { var span = this.Buffer.AsSpan(index, 2); return(this.IsLittleEndian ? BinaryPrimitives.ReadInt16LittleEndian(span) : BinaryPrimitives.ReadInt16BigEndian(span)); }
public static short ToInt16(this ReadOnlySpan <byte> b, ref int index) { ReadOnlySpan <byte> byteSpan = b.Slice(index); var result = BufferWriter.LittleEndianStorage ? BinaryPrimitives.ReadInt16LittleEndian(byteSpan) : BinaryPrimitives.ReadInt16BigEndian(byteSpan); index += sizeof(short); return(result); }
/// <summary> /// 读取数值。 /// </summary> /// <param name="buffer">当前只读字节实例。</param> /// <returns>返回数值。</returns> public static short ReadInt16(this ReadOnlySpan <byte> buffer) { if (BitConverter.IsLittleEndian) { return(BinaryPrimitives.ReadInt16BigEndian(buffer)); } return(BinaryPrimitives.ReadInt16LittleEndian(buffer)); }
private Int16 ReadInt16(Registers reg) { Span <byte> retArray = stackalloc byte[2]; _i2cDevice.WriteByte((byte)reg); _i2cDevice.Read(retArray); return(BinaryPrimitives.ReadInt16LittleEndian(retArray)); }
private int ReadRegister(byte register, I2cDevice device) { device.WriteByte(register); var word = new byte[2]; device.Read(word); return(BitConverter.IsLittleEndian ? BinaryPrimitives.ReadInt16LittleEndian(word) : BinaryPrimitives.ReadInt16BigEndian(word)); }
internal int GetNu() { if (Memory != null) { _nu = BinaryPrimitives.ReadInt16LittleEndian(Memory.AsSpan(Address + 2)) & 0xffff; } return(_nu); }