Exemplo n.º 1
0
        /// <summary>Reads Single and swap bytes if needed</summary>
        /// <returns>Single</returns>
        public override Single ReadSingle()
        {
            Byte[] bytes = base.ReadBytes(sizeof(Single));
            EndianHelper.AdjustEndianness(typeof(Single), bytes, this._endiannes);

            return(BitConverter.ToSingle(bytes, 0));
        }
Exemplo n.º 2
0
        /// <summary>Reads Int64 and swap bytes if needed</summary>
        /// <returns>Int64</returns>
        public override Int64 ReadInt64()
        {
            Byte[] bytes = base.ReadBytes(sizeof(Int64));
            EndianHelper.AdjustEndianness(typeof(Int64), bytes, this._endiannes);

            return(BitConverter.ToInt64(bytes, 0));
        }
Exemplo n.º 3
0
        /// <summary>Reads unsigned Int32 and swap bytes if needed</summary>
        /// <returns>UInt32</returns>
        public override UInt32 ReadUInt32()
        {
            Byte[] bytes = base.ReadBytes(sizeof(UInt32));
            EndianHelper.AdjustEndianness(typeof(UInt32), bytes, this._endiannes);

            return(BitConverter.ToUInt32(bytes, 0));
        }
Exemplo n.º 4
0
        internal static void AdjustEndianness(Type type, Byte[] data, EndianHelper.Endian endian, Int32 startOffset = 0)
        {
            if (EndianHelper.Endianness == endian)
            {
                return;
            }

            TypeCode code = Type.GetTypeCode(type);

            switch (code)
            {
            default:
                Array.Reverse(data, startOffset, EndianHelper.SizeTypes[code]);
                break;

            case TypeCode.String:
                return;                //Ignore strings

            case TypeCode.Object:
                foreach (FieldInfo field in type.GetFields())
                {
                    Type fieldType = field.FieldType;
                    if (field.IsStatic)                   // Ignore static fields
                    {
                        continue;
                    }

                    /*else if(fieldType.IsArray)// array fields
                     * {
                     *      //handle arrays, assuming fixed length
                     *      MarshalAsAttribute[] attr = (MarshalAsAttribute[])fieldType.GetCustomAttributes(typeof(MarshalAsAttribute), false);
                     *      if(attr.Length == 0 || attr[0].SizeConst == 0)
                     *              throw new NotSupportedException("Array fields must be decorated with a MarshalAsAttribute with SizeConst specified.");
                     *
                     *      var arrayLength = attr[0].SizeConst;
                     *      var elementType = fieldType.GetElementType();
                     *      var elementSize = Marshal.SizeOf(elementType);
                     *      var arrayOffset = Marshal.OffsetOf(type, field.Name).ToInt32();
                     *
                     *      for(int i = arrayOffset; i < arrayOffset + elementSize * arrayLength; i += elementSize)
                     *      {
                     *              MaybeAdjustEndianness(elementType, data, endianness, i);
                     *      }
                     * }*/else
                    {
                        Int32 offset = Marshal.OffsetOf(type, field.Name).ToInt32();

                        Int32 fieldOffset = startOffset + offset;

                        EndianHelper.AdjustEndianness(fieldType, data, endian, fieldOffset);
                    }
                }
                break;
            }
        }
Exemplo n.º 5
0
        /// <summary>Reads decimal and swap bytes if needed</summary>
        /// <returns>Decimal</returns>
        public override Decimal ReadDecimal()
        {
            Byte[] bytes = base.ReadBytes(sizeof(Decimal));
            EndianHelper.AdjustEndianness(typeof(Decimal), bytes, this._endiannes);
            var i1 = BitConverter.ToInt32(bytes, 0);
            var i2 = BitConverter.ToInt32(bytes, 4);
            var i3 = BitConverter.ToInt32(bytes, 8);
            var i4 = BitConverter.ToInt32(bytes, 12);

            return(new Decimal(new Int32[] { i1, i2, i3, i4, }));
        }
Exemplo n.º 6
0
        /// <summary>Get structure from specific padding from the beginning of the image</summary>
        /// <typeparam name="T">Structure type</typeparam>
        /// <param name="padding">Padding from the beginning of the image</param>
        /// <returns>Readed structure from image</returns>
        public virtual T PtrToStructure <T>(UInt32 padding) where T : struct
        {
            Byte[] bytes = this.ReadBytes(padding, (UInt32)Marshal.SizeOf(typeof(T)));

            EndianHelper.AdjustEndianness(typeof(T), bytes, this.Endianness);

            GCHandle handle = GCHandle.Alloc(bytes, GCHandleType.Pinned);

            try
            {
                T result = (T)Marshal.PtrToStructure(handle.AddrOfPinnedObject(), typeof(T));
                return(result);
            } finally
            {
                handle.Free();
            }
        }