Ejemplo n.º 1
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;
            }
        }
Ejemplo n.º 2
0
 /// <summary>Creates reader for specific byte endianness</summary>
 /// <param name="endiannes">Big or Little endian</param>
 /// <param name="input">Byte array stream</param>
 /// <returns>BinaryReader</returns>
 public static BinaryReader CreateReader(EndianHelper.Endian endiannes, MemoryStream input)
 {
     if (BitConverter.IsLittleEndian == (endiannes == EndianHelper.Endian.Little))
     {
         return(new BinaryReader(input));
     }
     else
     {
         return(new BinaryEndianReader(input, endiannes));
     }
 }
Ejemplo n.º 3
0
 /// <summary>Create instance of the binary reader specifying required endianness and input stream</summary>
 /// <param name="endianness">Required endianness</param>
 /// <param name="input">Byte input stream</param>
 public BinaryEndianReader(Stream input, EndianHelper.Endian endianness)
     : base(input)
 {
     this._endiannes = endianness;
 }