public void Align()
        {
            using (TempFile temp = new TempFile())
            {
                Guid   guid  = Guid.NewGuid();
                byte[] bytes = guid.ToByteArray();

                File.WriteAllBytes(temp.Path, bytes);
                using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path))
                {
                    MemoryLoadedFileReader reader = new MemoryLoadedFileReader(file);

                    Assert.Equal(0, reader.Position);
                    reader.Align(2);
                    Assert.Equal(0, reader.Position);
                    reader.Align(4);
                    Assert.Equal(0, reader.Position);
                    reader.Align(8);
                    Assert.Equal(0, reader.Position);
                    reader.Position = 1;
                    reader.Align(2);
                    Assert.Equal(2, reader.Position);
                    reader.Align(4);
                    Assert.Equal(4, reader.Position);
                    reader.Align(8);
                    Assert.Equal(8, reader.Position);
                }
            }
        }
示例#2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PdbFile"/> class.
        /// </summary>
        /// <param name="path">Path to PDB file.</param>
        public PdbFile(string path)
        {
            MemoryLoadedFile file = new MemoryLoadedFile(path);

            try
            {
                Initialize(file);
            }
            catch
            {
                file.Dispose();
                throw;
            }
        }
示例#3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PdbFile"/> class.
        /// </summary>
        /// <param name="file">File loaded into memory for faster parsing.</param>
        public unsafe EmbeddedPdbFile(MemoryLoadedFile file)
        {
            PEReader peReader         = new PEReader(file.BasePointer, (int)file.Length);
            var      debugEntries     = peReader.ReadDebugDirectory();
            var      embeddedPdbEntry = debugEntries.FirstOrDefault(e => e.Type == DebugDirectoryEntryType.EmbeddedPortablePdb);

            if (embeddedPdbEntry.DataSize == 0)
            {
                throw new Exception("PDB wasn't embedded");
            }
            embeddedPdbReaderProvider = peReader.ReadEmbeddedPortablePdbDebugDirectoryData(embeddedPdbEntry);
            PdbFile   = new PdbFile(embeddedPdbReaderProvider.GetMetadataReader());
            this.file = file;
        }
        public void ReadAllBytes()
        {
            using (TempFile temp = new TempFile())
            {
                byte[] bytes = Guid.NewGuid().ToByteArray();

                File.WriteAllBytes(temp.Path, bytes);
                using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path))
                {
                    MemoryLoadedFileReader reader = new MemoryLoadedFileReader(file);

                    Assert.Equal(bytes, reader.ReadAllBytes());
                    Assert.Equal(0, reader.BytesRemaining);
                }
            }
        }
示例#5
0
        public void ReadCStringWide()
        {
            using (TempFile temp = new TempFile())
            {
                byte[] bytes = new byte[] { (byte)'T', 0, (byte)'e', 0, (byte)'s', 0, (byte)'t', 0, 0, 0 };

                File.WriteAllBytes(temp.Path, bytes);
                using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path))
                {
                    IBinaryReader reader = new MemoryLoadedFileReader(file).ReadSubstream();

                    Assert.Equal("Test", reader.ReadCStringWide().ToString());
                    Assert.Equal(0, reader.BytesRemaining);
                }
            }
        }
        public void ReadBString()
        {
            using (TempFile temp = new TempFile())
            {
                byte[] bytes = new byte[] { 4, 0, (byte)'T', (byte)'e', (byte)'s', (byte)'t' };

                File.WriteAllBytes(temp.Path, bytes);
                using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path))
                {
                    MemoryLoadedFileReader reader = new MemoryLoadedFileReader(file);

                    Assert.Equal("Test", reader.ReadBString().String);
                    Assert.Equal(0, reader.BytesRemaining);
                }
            }
        }
        public void ReadUintArray()
        {
            using (TempFile temp = new TempFile())
            {
                byte[] bytes      = new byte[] { 127, 0, 0, 0, 255, 255, 255, 255, 0, 0, 0, 1 };
                uint[] validation = new uint[] { 127, uint.MaxValue, 256 * 256 * 256U };

                File.WriteAllBytes(temp.Path, bytes);
                using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path))
                {
                    MemoryLoadedFileReader reader = new MemoryLoadedFileReader(file);

                    Assert.Equal(validation, reader.ReadUintArray(validation.Length));
                    Assert.Equal(0, reader.BytesRemaining);
                }
            }
        }
        public void ReadDouble()
        {
            using (TempFile temp = new TempFile())
            {
                double value = 42.24;
                byte[] bytes = BitConverter.GetBytes(value);

                File.WriteAllBytes(temp.Path, bytes);
                using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path))
                {
                    MemoryLoadedFileReader reader = new MemoryLoadedFileReader(file);

                    Assert.Equal(value, reader.ReadDouble());
                    Assert.Equal(0, reader.BytesRemaining);
                }
            }
        }
示例#9
0
        public void ReadUint()
        {
            using (TempFile temp = new TempFile())
            {
                byte[] bytes = new byte[] { 127, 0, 0, 0, 255, 255, 255, 255, 0, 0, 0, 1 };

                File.WriteAllBytes(temp.Path, bytes);
                using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path))
                {
                    IBinaryReader reader = new MemoryLoadedFileReader(file).ReadSubstream();

                    Assert.Equal(127U, reader.ReadUint());
                    Assert.Equal(uint.MaxValue, reader.ReadUint());
                    Assert.Equal(256 * 256 * 256U, reader.ReadUint());
                    Assert.Equal(0, reader.BytesRemaining);
                }
            }
        }
示例#10
0
        public void ReadInt()
        {
            using (TempFile temp = new TempFile())
            {
                byte[] bytes = new byte[] { 127, 0, 0, 0, 255, 255, 255, 255, 0, 0, 0, 1 };

                File.WriteAllBytes(temp.Path, bytes);
                using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path))
                {
                    MemoryLoadedFileReader reader = new MemoryLoadedFileReader(file);

                    Assert.Equal(127, reader.ReadInt());
                    Assert.Equal(-1, reader.ReadInt());
                    Assert.Equal(256 * 256 * 256, reader.ReadInt());
                    Assert.Equal(0, reader.BytesRemaining);
                }
            }
        }
        public unsafe void SimpleByteArray()
        {
            using (TempFile temp = new TempFile())
            {
                byte[] bytes = new byte[] { 127, 128, 129, 200 };

                File.WriteAllBytes(temp.Path, bytes);
                using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path))
                {
                    Assert.Equal(bytes.Length, file.Length);
                    Assert.NotEqual(IntPtr.Zero, new IntPtr(file.BasePointer));
                    for (int i = 0; i < bytes.Length; i++)
                    {
                        Assert.Equal(bytes[i], file.BasePointer[i]);
                    }
                }
            }
        }
示例#12
0
        public unsafe void Move()
        {
            using (TempFile temp = new TempFile())
            {
                byte[] bytes = Guid.NewGuid().ToByteArray();

                File.WriteAllBytes(temp.Path, bytes);
                using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path))
                {
                    IBinaryReader reader = new MemoryLoadedFileReader(file).ReadSubstream();

                    Assert.Equal(bytes.Length, reader.Length);
                    reader.Move((uint)bytes.Length / 2);
                    Assert.Equal(bytes.Length / 2, reader.BytesRemaining);
                    reader.Position += bytes.Length / 2;
                    Assert.Equal(0, reader.BytesRemaining);
                }
            }
        }
示例#13
0
        public void ReadByte()
        {
            using (TempFile temp = new TempFile())
            {
                byte[] bytes = new byte[] { 127, 128, 129, 200 };

                File.WriteAllBytes(temp.Path, bytes);
                using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path))
                {
                    IBinaryReader reader = new MemoryLoadedFileReader(file).ReadSubstream();

                    for (int i = 0; i < bytes.Length; i++)
                    {
                        Assert.Equal(bytes[i], reader.ReadByte());
                    }
                    Assert.Equal(0, reader.BytesRemaining);
                }
            }
        }
示例#14
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PdbFile"/> class.
        /// </summary>
        /// <param name="file">File loaded into memory for faster parsing.</param>
        public PdbFile(MemoryLoadedFile file)
        {
            Reader         = new SharpPdb.Windows.PdbFile(file);
            functionsCache = SimpleCache.CreateStruct(() =>
            {
                List <IPdbFunction> functions = new List <IPdbFunction>();

                foreach (var dbiModule in Reader.DbiStream.Modules)
                {
                    var symbolStream = dbiModule.LocalSymbolStream;

                    foreach (var kind in ManagedProcedureSymbol.Kinds)
                    {
                        foreach (ManagedProcedureSymbol procedure in symbolStream[kind])
                        {
                            functions.Add(new PdbFunction(this, procedure, dbiModule));
                        }
                    }
                }
                return(functions);
            });
            sourcesCache          = new DictionaryCache <FileChecksumSubsection, PdbSource>(checksum => new PdbSource(this, checksum));
            functionsByTokenCache = SimpleCache.CreateStruct(() => Functions.ToDictionary(f => f.Token));
            tokenRidMapCache      = SimpleCache.CreateStruct(() =>
            {
                var reader = Reader.DbiStream.GetKnownDebugStream(KnownDebugStreamIndex.TokenRidMap)?.Reader;

                if (reader == null)
                {
                    return(null);
                }

                int count          = (int)(reader.Length / 4); // 4 = sizeof(uint)
                uint[] tokenRidMap = new uint[count];

                for (int i = 0; i < count; i++)
                {
                    tokenRidMap[i] = reader.ReadUint();
                }
                return(tokenRidMap);
            });
            this.file = file;
        }
示例#15
0
        public unsafe void Duplicate()
        {
            using (TempFile temp = new TempFile())
            {
                byte[] bytes = Guid.NewGuid().ToByteArray();

                File.WriteAllBytes(temp.Path, bytes);
                using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path))
                {
                    IBinaryReader reader = new MemoryLoadedFileReader(file).ReadSubstream();
                    reader.Position = 5;
                    IBinaryReader reader2 = reader.Duplicate();

                    Assert.Equal(reader.Position, reader2.Position);
                    reader.Position++;
                    Assert.NotEqual(reader.Position, reader2.Position);
                    reader2.Position++;
                    Assert.Equal(reader.Position, reader2.Position);
                    reader2.Position++;
                    Assert.NotEqual(reader.Position, reader2.Position);
                }
            }
        }
示例#16
0
        public unsafe void ReadBytes()
        {
            using (TempFile temp = new TempFile())
            {
                byte[] bytes = Guid.NewGuid().ToByteArray();

                File.WriteAllBytes(temp.Path, bytes);
                using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path))
                {
                    IBinaryReader reader = new MemoryLoadedFileReader(file).ReadSubstream();

                    byte[] readBytes = new byte[reader.BytesRemaining];

                    fixed(byte *b = readBytes)
                    {
                        reader.ReadBytes(b, (uint)readBytes.Length);
                    }

                    Assert.Equal(bytes, readBytes);
                    Assert.Equal(0, reader.BytesRemaining);
                }
            }
        }
        public unsafe void ReadDecimal()
        {
            using (TempFile temp = new TempFile())
            {
                decimal[] values = new decimal[] { new decimal(42.24) };
                byte[]    bytes  = new byte[16];

                fixed(decimal *d = values)
                fixed(byte *b = bytes)
                {
                    MemoryBuffer.MemCpy(b, d, 16);
                }

                File.WriteAllBytes(temp.Path, bytes);
                using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path))
                {
                    MemoryLoadedFileReader reader = new MemoryLoadedFileReader(file);

                    Assert.Equal(values[0], reader.ReadDecimal());
                    Assert.Equal(0, reader.BytesRemaining);
                }
            }
        }
        public void ReadSubstream()
        {
            using (TempFile temp = new TempFile())
            {
                Guid   guid  = Guid.NewGuid();
                byte[] bytes = guid.ToByteArray();

                File.WriteAllBytes(temp.Path, bytes);
                using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path))
                {
                    MemoryLoadedFileReader reader  = new MemoryLoadedFileReader(file);
                    IBinaryReader          reader2 = reader.ReadSubstream();

                    Assert.Equal(0, reader2.Position);
                    Assert.Equal(reader.Length, reader2.Length);
                    Assert.Equal(0, reader.BytesRemaining);
                    reader2.Position = 1;
                    Assert.Equal(1, reader2.Position);
                    Assert.Equal(0, reader.BytesRemaining);
                    Assert.NotEqual(reader.Position, reader2.Position);
                }
            }
        }
示例#19
0
        /// <summary>
        /// Opens PDB file from the specified path. It supports both Portable PDB and Windows PDB format.
        /// </summary>
        /// <param name="path">Path to the PDB file.</param>
        public static IPdbFile OpenPdb(string path)
        {
            // Load file into memory.
            MemoryLoadedFile file = new MemoryLoadedFile(path);

            // Check if it is Portable PDB file.
            try
            {
                return(new Portable.PdbFile(file));
            }
            catch
            {
            }

            // Check if it is Windows PDB file.
            try
            {
                return(new Windows.PdbFile(file));
            }
            catch
            {
            }

            // Check if it is Portable PDB file that is embedded in the assembly.
            try
            {
                return(new Portable.EmbeddedPdbFile(file));
            }
            catch
            {
            }

            // Unload file from memory.
            file.Dispose();
            return(null);
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="MemoryLoadedFileReader"/> class.
 /// </summary>
 /// <param name="file">The <see cref="MemoryLoadedFile"/> as stream.</param>
 public MemoryLoadedFileReader(MemoryLoadedFile file)
     : base(file.BasePointer, file.Length)
 {
     File = file;
 }
示例#21
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PdbFile"/> class.
 /// </summary>
 /// <param name="file">File loaded into memory for faster parsing.</param>
 public unsafe PdbFile(MemoryLoadedFile file)
     : this(new MetadataReader(file.BasePointer, (int)file.Length))
 {
     this.file = file;
 }
示例#22
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PdbFile"/> class.
 /// </summary>
 /// <param name="file">File loaded into memory. Note that file will be closed when instance of this type is disposed.</param>
 public PdbFile(MemoryLoadedFile file)
 {
     Initialize(file);
 }
示例#23
0
 private void Initialize(MemoryLoadedFile file)
 {
     File = file;
     Initialize(new MemoryLoadedFileReader(file));
 }
示例#24
0
 /// <summary>
 /// Initializes a new instance of the <see cref="PdbFileReader"/> class.
 /// </summary>
 /// <param name="file">File loaded into memory. Note that file will be closed when instance of this type is disposed.</param>
 public PdbFileReader(MemoryLoadedFile file)
     : this(new PdbFile(file))
 {
 }