예제 #1
0
 public static P3Int16 Read(ReadOnlySpan <byte> span)
 {
     return(new P3Int16(
                BinaryPrimitives.ReadInt16LittleEndian(span),
                BinaryPrimitives.ReadInt16LittleEndian(span.Slice(2)),
                BinaryPrimitives.ReadInt16LittleEndian(span.Slice(4))));
 }
예제 #2
0
            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();
                }
            }
예제 #3
0
 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);
        }
예제 #5
0
        private bool IsSupportedFileFormat(ReadOnlySpan <byte> header)
        {
            short fileTypeMarker = BinaryPrimitives.ReadInt16LittleEndian(header);

            return(header.Length >= this.HeaderSize &&
                   (fileTypeMarker == BmpConstants.TypeMarkers.Bitmap || fileTypeMarker == BmpConstants.TypeMarkers.BitmapArray));
        }
예제 #6
0
파일: Xfsa.cs 프로젝트: cinnature/Kuriimu2
        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));
        }
예제 #7
0
        /// <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));
        }
예제 #8
0
        /// <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);
            }
        }
예제 #9
0
        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));
        }
예제 #11
0
        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);
        }
예제 #12
0
        /// <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));
        }
예제 #13
0
        public short ReadInt16()
        {
            var val = BinaryPrimitives.ReadInt16LittleEndian(_buffer.Span.Slice((int)Position, 2));

            Position += 2;
            return(val);
        }
예제 #14
0
파일: Hts221.cs 프로젝트: zheng1748/iot
        private short ReadInt16(Register register)
        {
            Span <byte> val = stackalloc byte[2];

            Read(register, val);
            return(BinaryPrimitives.ReadInt16LittleEndian(val));
        }
예제 #15
0
        /// <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));
        }
예제 #16
0
        /// <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;
        }
예제 #17
0
        public static short ReadInt16LittleEndian(this MemoryUnit self, int offset)
        {
            Span <byte> buf = stackalloc byte[2];

            self.Read(offset, buf);
            return(BinaryPrimitives.ReadInt16LittleEndian(buf));
        }
예제 #18
0
파일: CDRReader.cs 프로젝트: atolab/cscdr
 public short ReadInt16()
 {
     Align(2);
     return(IsLittleEndian
         ? BinaryPrimitives.ReadInt16LittleEndian(ReadBytes(2))
         : BinaryPrimitives.ReadInt16BigEndian(ReadBytes(2)));
 }
예제 #19
0
            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;
                    }
                }
            }
예제 #20
0
        public short GetShort(int offset)
        {
            AssertOffsetAndLength(offset, sizeof(short));
            var span = _buffer.ReadOnlySpan.Slice(offset);

            return(BinaryPrimitives.ReadInt16LittleEndian(span));
        }
예제 #21
0
        public async Task <short> ReadInt16Async()
        {
            var buffer = await ReadBytesAsync(sizeof(short))
                         .ConfigureAwait(false);

            return(BinaryPrimitives.ReadInt16LittleEndian(buffer.Span));
        }
예제 #22
0
        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);
        }
예제 #25
0
        /// <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));
        }
예제 #26
0
        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);
        }
예제 #27
0
 /// <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));
 }
예제 #28
0
        private Int16 ReadInt16(Registers reg)
        {
            Span <byte> retArray = stackalloc byte[2];

            _i2cDevice.WriteByte((byte)reg);
            _i2cDevice.Read(retArray);
            return(BinaryPrimitives.ReadInt16LittleEndian(retArray));
        }
예제 #29
0
        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));
        }
예제 #30
0
 internal int GetNu()
 {
     if (Memory != null)
     {
         _nu = BinaryPrimitives.ReadInt16LittleEndian(Memory.AsSpan(Address + 2)) & 0xffff;
     }
     return(_nu);
 }