public void ReadByteReadByte_213()
 {
     var reader = new BinaryStreamReader(new MemoryStream(new byte[] { 0, 213 }), new byte[20]);
     reader.ReadByte();
     byte value = reader.ReadByte();
     Assert.AreEqual(213, value);
 }
Esempio n. 2
0
        public void ReadByteReadByte_213()
        {
            var reader = new BinaryStreamReader(new MemoryStream(new byte[] { 0, 213 }), new byte[20]);

            reader.ReadByte();
            byte value = reader.ReadByte();

            Assert.AreEqual(213, value);
        }
Esempio n. 3
0
 public static RefType Read(BinaryStreamReader signatureBlobReader)
 {
     var leadByte = (ElementType)signatureBlobReader.ReadByte();
     if (leadByte == ElementType.ByRef)
     {
         Type t = Type.Read(leadByte, signatureBlobReader);
         return new ByRef
         {
             Type = t
         };
     }
     else if (leadByte == ElementType.TypedByRef)
     {
         return TypedByRef.Instance;
     }
     else if (leadByte == ElementType.Void)
     {
         return Void.Instance;
     }
     else
     {
         Type t = Type.Read(leadByte, signatureBlobReader);
         return new DirectType
         {
             Type = t
         };
     }
 }
Esempio n. 4
0
        public static Param Read(BinaryStreamReader signatureBlobReader)
        {
            ElementType leadByte;

            var customMods = CustomMod.ReadCustomModArray(out leadByte, signatureBlobReader);

            switch (leadByte)
            {
                case ElementType.ByRef:
                    leadByte = (ElementType)signatureBlobReader.ReadByte();
                    return new ByRef
                    {
                        Type = Type.Read(leadByte, signatureBlobReader)
                    };

                case ElementType.TypedByRef:
                    return new TypedByRef();

                default:
                    return new DirectType
                    {
                        Type = Type.Read(leadByte, signatureBlobReader)
                    };
            }
        }
Esempio n. 5
0
        public void ShortThenExtend_ReadFixedZeroFilledString_StreamTooShort()
        {
            var stream = new GradualReadMemoryStream(Encoding.ASCII.GetBytes("*ABCD"), 1);
            var reader = new BinaryStreamReader(stream, new byte[20]);

            reader.ReadByte();
            reader.ReadFixedZeroFilledAsciiString(20);
        }
Esempio n. 6
0
        public void ShortThenExtend_ReadFixedZeroFilledString_Alphabet()
        {
            var stream = new GradualReadMemoryStream(Encoding.ASCII.GetBytes("*ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1);
            var reader = new BinaryStreamReader(stream, new byte[20]);

            reader.ReadByte();
            string str = reader.ReadFixedZeroFilledAsciiString(26);

            Assert.AreEqual("ABCDEFGHIJKLMNOPQRSTUVWXYZ", str);
        }
Esempio n. 7
0
        public void Stream34Buffer20_ReadByte_Seek1To34()
        {
            var reader = new BinaryStreamReader(new MemoryStream(new byte[34]), new byte[20]);

            reader.ReadByte();
            for (int i = 1; i <= 34; i++)
            {
                reader.Position++;
                Assert.AreEqual(i + 1, reader.Position);
            }
        }
Esempio n. 8
0
        public void Read(BinaryStreamReader signatureBlobReader)
        {
            byte genericInst = signatureBlobReader.ReadByte();
            if (genericInst != 0x0a)
                throw new BadImageFormatException("Invalid leading byte in MethodSpec: " + genericInst + ".");

            uint? genArgCount = signatureBlobReader.ReadCompressedUInt32();
            if(genArgCount==null)
                throw new BadImageFormatException("Null value for MethodSpec.GenArgCount is not supported.");

            this.GenArgs = new TypeReference[genArgCount.Value];
        }
Esempio n. 9
0
        public void ShortStream_ReadByte_ExtendStream_Seek34To1()
        {
            var stream = new MemoryStream();

            stream.SetLength(7);
            var reader = new BinaryStreamReader(stream, new byte[20]);

            reader.ReadByte();
            stream.SetLength(34);
            for (int i = 34; i >= 1; i--)
            {
                reader.Position = i;
                Assert.AreEqual(i, reader.Position);
            }
        }
Esempio n. 10
0
        static void Main(string[] args)
        {
            var pe = new PEFile();
            var originalBytes = Properties.Resources.console_anycpu;
            var reader = new BinaryStreamReader(new MemoryStream(originalBytes), new byte[1024]);
            pe.ReadFrom(reader);

            using (var output = File.Create("console.anycpu.exe"))
            {
                var writer = new BinaryStreamWriter(output);
                pe.WriteTo(writer);

                while (reader.Position < originalBytes.Length)
                {
                    writer.WriteByte(reader.ReadByte());
                }
            }
        }
Esempio n. 11
0
        private static string ReadAlignedNameString(BinaryStreamReader reader)
        {
            var bytes = new List<byte>();
            while (true)
            {
                var b = reader.ReadByte();
                if (b == 0)
                    break;

                bytes.Add(b);
            }

            int skipCount = -1 + ((bytes.Count + 4) & ~3) - bytes.Count;

            reader.Position += skipCount;

            return Encoding.UTF8.GetString(bytes.ToArray(), 0, bytes.Count);
        }
Esempio n. 12
0
        public void ShortStream_ReadByte_ExtendStream_SeekReadInt64()
        {
            var stream = new MemoryStream();

            stream.SetLength(7);
            stream.Position = 6;
            stream.WriteByte(108);
            stream.Position = 0;

            var reader = new BinaryStreamReader(stream, new byte[8]);

            reader.ReadByte();
            stream.SetLength(34);
            reader.Position = 6;

            long value = reader.ReadInt64();

            Assert.AreEqual(108, value);
        }
Esempio n. 13
0
        public static MethodSig Read(BinaryStreamReader signatureBlobReader)
        {
            var callingConvention = (CallingConventions)signatureBlobReader.ReadByte();

            MethodSig result;
            switch (callingConvention & ~CallingConventions.HasThis & ~CallingConventions.ExplicitThis)
            {
                case CallingConventions.Default:
                    result = new Default();
                    break;

                case CallingConventions.C:
                    result = new C();
                    break;

                case CallingConventions.StdCall:
                    result = new StdCall();
                    break;

                case CallingConventions.FastCall:
                    result = new FastCall();
                    break;

                case CallingConventions.VarArg:
                    result = new VarArg();
                    break;

                case CallingConventions.Generic:
                    {
                        var typed = new Generic();
                        typed.ReadDetails(signatureBlobReader);
                        result = typed;
                    }
                    break;

                default:
                    throw new BadImageFormatException("Invalid calling convention byte "+callingConvention+".");
            }

            result.ReadParameters(signatureBlobReader);

            return result;
        }
Esempio n. 14
0
        public static CustomMod[] ReadCustomModArray(out ElementType leadByte, BinaryStreamReader signatureBlobReader)
        {
            List<CustomMod> customMods = null;

            leadByte = (ElementType)signatureBlobReader.ReadByte();
            while (true)
            {
                var cmod = CustomMod.Read(signatureBlobReader, leadByte);
                if (cmod == null)
                    break;

                if (customMods == null)
                    customMods = new List<CustomMod>();

                customMods.Add(cmod);
            }

            if (customMods == null)
                return null;
            else
                return customMods.ToArray();
        }
        public void ShortStream_ReadByte_ExtendStream_SeekReadInt64()
        {
            var stream = new MemoryStream();
            stream.SetLength(7);
            stream.Position = 6;
            stream.WriteByte(108);
            stream.Position = 0;

            var reader = new BinaryStreamReader(stream, new byte[8]);
            reader.ReadByte();
            stream.SetLength(34);
            reader.Position = 6;

            long value = reader.ReadInt64();
            Assert.AreEqual(108, value);
        }
 public void ShortThenExtend_ReadFixedZeroFilledString_Alphabet()
 {
     var stream = new GradualReadMemoryStream(Encoding.ASCII.GetBytes("*ABCDEFGHIJKLMNOPQRSTUVWXYZ"), 1);
     var reader = new BinaryStreamReader(stream, new byte[20]);
     reader.ReadByte();
     string str = reader.ReadFixedZeroFilledAsciiString(26);
     Assert.AreEqual("ABCDEFGHIJKLMNOPQRSTUVWXYZ", str);
 }
Esempio n. 17
0
        public void Empty_ReadByte()
        {
            var reader = new BinaryStreamReader(new MemoryStream(), new byte[20]);

            reader.ReadByte();
        }
 public void ShortThenExtend_ReadFixedZeroFilledString_StreamTooShort()
 {
     var stream = new GradualReadMemoryStream(Encoding.ASCII.GetBytes("*ABCD"), 1);
     var reader = new BinaryStreamReader(stream, new byte[20]);
     reader.ReadByte();
     reader.ReadFixedZeroFilledAsciiString(20);
 }
 public void Stream34Buffer20_ReadByte_Seek1To34()
 {
     var reader = new BinaryStreamReader(new MemoryStream(new byte[34]), new byte[20]);
     reader.ReadByte();
     for (int i = 1; i <= 34; i++)
     {
         reader.Position++;
         Assert.AreEqual(i + 1, reader.Position);
     }
 }
 public void Empty_ReadByte()
 {
     var reader = new BinaryStreamReader(new MemoryStream(), new byte[20]);
     reader.ReadByte();
 }
Esempio n. 21
0
        private static string ReadAsciiZ(BinaryStreamReader reader)
        {
            var result = new StringBuilder();
            while (true)
            {
                byte b = reader.ReadByte();
                if (b == 0)
                    break;

                const int maxLength = 512;

                if (result.Length > maxLength)
                    throw new InvalidOperationException("String is too long, for safety reasons the size limit for ASCIIZ strings is set to " + maxLength + ".");

                result.Append((char)b);
            }

            return result.ToString();
        }
Esempio n. 22
0
        void ReadOptionalHeader(BinaryStreamReader reader)
        {
            if (this.OptionalHeader == null)
                this.OptionalHeader = new OptionalHeader();

            var peMagic = (PEMagic)reader.ReadInt16();

            if (peMagic != PEMagic.NT32
                && peMagic != PEMagic.NT64)
                throw new BadImageFormatException("Unsupported PE magic value " + peMagic + ".");

            this.OptionalHeader.PEMagic = peMagic;

            this.OptionalHeader.MajorLinkerVersion = reader.ReadByte();
            this.OptionalHeader.MinorLinkerVersion = reader.ReadByte();
            this.OptionalHeader.SizeOfCode = reader.ReadUInt32();
            this.OptionalHeader.SizeOfInitializedData = reader.ReadUInt32();
            this.OptionalHeader.SizeOfUninitializedData = reader.ReadUInt32();
            this.OptionalHeader.AddressOfEntryPoint = reader.ReadUInt32();
            this.OptionalHeader.BaseOfCode = reader.ReadUInt32();

            if (peMagic == PEMagic.NT32)
            {
                this.OptionalHeader.BaseOfData = reader.ReadUInt32();
                this.OptionalHeader.ImageBase = reader.ReadUInt32();
            }
            else
            {
                this.OptionalHeader.ImageBase = reader.ReadUInt64();
            }

            this.OptionalHeader.SectionAlignment = reader.ReadUInt32();
            this.OptionalHeader.FileAlignment = reader.ReadUInt32();
            this.OptionalHeader.MajorOperatingSystemVersion = reader.ReadUInt16();
            this.OptionalHeader.MinorOperatingSystemVersion = reader.ReadUInt16();
            this.OptionalHeader.MajorImageVersion = reader.ReadUInt16();
            this.OptionalHeader.MinorImageVersion = reader.ReadUInt16();
            this.OptionalHeader.MajorSubsystemVersion = reader.ReadUInt16();
            this.OptionalHeader.MinorSubsystemVersion = reader.ReadUInt16();
            this.OptionalHeader.Win32VersionValue = reader.ReadUInt32();
            this.OptionalHeader.SizeOfImage = reader.ReadUInt32();
            this.OptionalHeader.SizeOfHeaders = reader.ReadUInt32();
            this.OptionalHeader.CheckSum = reader.ReadUInt32();
            this.OptionalHeader.Subsystem = (Subsystem)reader.ReadUInt16();
            this.OptionalHeader.DllCharacteristics = (DllCharacteristics)reader.ReadUInt16();

            if (peMagic == PEMagic.NT32)
            {
                this.OptionalHeader.SizeOfStackReserve = reader.ReadUInt32();
                this.OptionalHeader.SizeOfStackCommit = reader.ReadUInt32();
                this.OptionalHeader.SizeOfHeapReserve = reader.ReadUInt32();
                this.OptionalHeader.SizeOfHeapCommit = reader.ReadUInt32();
            }
            else
            {
                this.OptionalHeader.SizeOfStackReserve = reader.ReadUInt64();
                this.OptionalHeader.SizeOfStackCommit = reader.ReadUInt64();
                this.OptionalHeader.SizeOfHeapReserve = reader.ReadUInt64();
                this.OptionalHeader.SizeOfHeapCommit = reader.ReadUInt64();
            }

            this.OptionalHeader.LoaderFlags = reader.ReadUInt32();
            this.OptionalHeader.NumberOfRvaAndSizes = reader.ReadUInt32();

            if (this.OptionalHeader.DataDirectories == null
                || this.OptionalHeader.DataDirectories.Length != this.OptionalHeader.NumberOfRvaAndSizes)
                this.OptionalHeader.DataDirectories = new DataDirectory[this.OptionalHeader.NumberOfRvaAndSizes];

            for (int i = 0; i < this.OptionalHeader.DataDirectories.Length; i++)
            {
                var dd = new DataDirectory();
                dd.Read(reader);
                this.OptionalHeader.DataDirectories[i] = dd;
            }
        }
 public void ShortStream_ReadByte_ExtendStream_Seek34To1()
 {
     var stream = new MemoryStream();
     stream.SetLength(7);
     var reader = new BinaryStreamReader(stream, new byte[20]);
     reader.ReadByte();
     stream.SetLength(34);
     for (int i = 34; i >= 1; i--)
     {
         reader.Position = i;
         Assert.AreEqual(i, reader.Position);
     }
 }
Esempio n. 24
0
        public static Type Read(ElementType leadByte, BinaryStreamReader signatureBlobReader)
        {
            switch (leadByte)
            {
                case ElementType.End:
                    break;
                case ElementType.Void:
                    break;

                case ElementType.Boolean: return Boolean.Instance;
                case ElementType.Char: return Char.Instance;
                case ElementType.I1: return I1.Instance;
                case ElementType.U1: return U1.Instance;
                case ElementType.I2: return I2.Instance;
                case ElementType.U2: return U2.Instance;
                case ElementType.I4: return I4.Instance;
                case ElementType.U4: return U4.Instance;
                case ElementType.I8: return I8.Instance;
                case ElementType.U8: return U8.Instance;
                case ElementType.R4: return R4.Instance;
                case ElementType.R8: return R8.Instance;

                case ElementType.String: return String.Instance;

                case ElementType.Ptr:
                    ElementType ptrLeadByte;
                    var ptrCustomMod = CustomMod.ReadCustomModArray(out ptrLeadByte, signatureBlobReader);
                    Ptr resultPtr;
                    if (ptrLeadByte == ElementType.Void)
                    {
                        resultPtr = Ptr.Void.Instance;
                    }
                    else
                    {
                        Type ptrType = Type.Read(ptrLeadByte, signatureBlobReader);
                        resultPtr = new Ptr.Type
                        {
                            PtrType = ptrType
                        };
                    }
                    resultPtr.CustomMods = ptrCustomMod;
                    return resultPtr;

                case ElementType.ByRef:
                    break;

                case ElementType.ValueType:
                    var valueTypeTypeDefOrRefOrSpecEncoded = signatureBlobReader.ReadTypeDefOrRefOrSpecEncoded();
                    return new ValueType { TypeDefOrRefEncoded = valueTypeTypeDefOrRefOrSpecEncoded };

                case ElementType.Class:
                    var classTypeDefOrRefOrSpecEncoded = signatureBlobReader.ReadTypeDefOrRefOrSpecEncoded();
                    return new Class { TypeDefOrRefEncoded = classTypeDefOrRefOrSpecEncoded };

                case ElementType.Var:
                    uint? varNumber = signatureBlobReader.ReadCompressedUInt32();
                    return new Var
                    {
                        Number = varNumber
                    };

                case ElementType.Array:
                    // TODO: implement ArrayShape (ECMA-335 §23.2.13)
                    throw new NotImplementedException("TODO: implement ArrayShape (ECMA-335 §23.2.13).");

                case ElementType.GenericInst:
                    var genericLeadByte = (ElementType)signatureBlobReader.ReadByte();
                    GenericInst genericInst;
                    switch (genericLeadByte)
                    {
                        case ElementType.Class:
                            genericInst = new GenericInst.Class();
                            break;

                        case ElementType.ValueType:
                            genericInst = new GenericInst.ValueType();
                            break;

                        default:
                            throw new BadImageFormatException("Invalid lead byte for "+ElementType.GenericInst+" class/valuetype value.");
                    }

                    var typeDefOrRefOrSpecEncoded = signatureBlobReader.ReadTypeDefOrRefOrSpecEncoded();
                    genericInst.TypeDefOrRefOrSpecEncoded = typeDefOrRefOrSpecEncoded;

                    uint? genArgCount = signatureBlobReader.ReadCompressedUInt32();
                    if (genArgCount == null)
                        throw new BadImageFormatException("Invalid null value for GenArgCount value in " + ElementType.GenericInst+".");

                    var genericInstTypes = new Type[genArgCount.Value];
                    for(int i = 0; i<genericInstTypes.Length; i++)
                    {
                        var genericInstTypeLeadByte = (ElementType)signatureBlobReader.ReadByte();
                        genericInstTypes[i] = Type.Read(genericInstTypeLeadByte, signatureBlobReader);
                    }

                    genericInst.Types = genericInstTypes;
                    return genericInst;

                case ElementType.TypedByRef:
                    break;

                case ElementType.I: return I.Instance;
                case ElementType.U: return U.Instance;

                case ElementType.FnPtr:
                    var methodDefSig = MethodSig.Read(signatureBlobReader);
                    return new FnPtr
                    {
                        MethodDefSig = methodDefSig
                    };

                case ElementType.Object: return Object.Instance;

                case ElementType.SZArray:
                    ElementType szArrayLeadByte;
                    var szArrayCustomMod = CustomMod.ReadCustomModArray(out szArrayLeadByte, signatureBlobReader);
                    var szArrayType = Type.Read(szArrayLeadByte, signatureBlobReader);
                    return new SZArray
                    {
                        CustomMods = szArrayCustomMod,
                        Type = szArrayType
                    };

                case ElementType.MVar:
                    uint? mvarNumber = signatureBlobReader.ReadCompressedUInt32();
                    return new MVar
                    {
                        Number = mvarNumber
                    };

                case ElementType.CMod_ReqD:
                    break;

                case ElementType.CMod_Opt:
                    break;
                case ElementType.Internal:
                    break;
                case ElementType.Modifier:
                    break;
                case ElementType.Sentinel:
                    break;
                case ElementType.Pinned:
                    break;
                case ElementType.R4_Hfa:
                    break;
                case ElementType.R8_Hfa:
                    break;
                case ElementType.ArgumentType_:
                    break;
                case ElementType.CustomAttribute_BoxedObject_:
                    break;
                case ElementType.CustomAttribute_Field_:
                    break;
                case ElementType.CustomAttribute_Property_:
                    break;
                case ElementType.CustomAttribute_Enum_:
                    break;
                default:
                    break;
            }

            throw new BadImageFormatException("Invalid lead byte "+leadByte+".");
        }