//From http://stackoverflow.com/questions/221925/creating-a-byte-array-from-a-stream - very useful public static byte[] ReadFully(IImageStream input) { byte[] buffer = new byte[16 * 1024]; using (MemoryStream ms = new MemoryStream()) { int read; while ((read = input.Read(buffer, 0, buffer.Length)) > 0) { ms.Write(buffer, 0, read); } return(ms.ToArray()); } }
/// <inheritdoc/> public void Read(byte[] pv, int cb, IntPtr pcbRead) { if (cb < 0) { cb = 0; } cb = stream.Read(pv, 0, cb); if (pcbRead != IntPtr.Zero) { Marshal.WriteInt32(pcbRead, cb); } }
public static byte[] ReadBodyFromRva(this MethodDef method) { if (MethodDefExt.OriginalMD == null) { MethodDefExt.OriginalMD = AssemblyDef.Load(Configuration.AssemblyFilename, (ModuleCreationOptions)null).ManifestModule as ModuleDefMD; } IImageStream fullStream = MethodDefExt.OriginalMD.MetaData.PEImage.CreateFullStream(); FileOffset fileOffset = MethodDefExt.OriginalMD.MetaData.PEImage.ToFileOffset(method.RVA); long num = MethodDefExt.OriginalMD.MetaData.PEImage.ToFileOffset((RVA)MethodDefExt.OriginalMD.TablesStream.ReadMethodRow(method.Rid + 1U).RVA) - fileOffset; byte[] buffer = new byte[100]; fullStream.Position = (long)(fileOffset + 20L); fullStream.Read(buffer, 0, buffer.Length); return(buffer); }
protected override void OnPaint(PaintEventArgs e) { EnsureFontInfo(); e.Graphics.Clear(BackColor); if (stream != null) { var currentIndexBase = scrollBar.Value * 0x10L; // https://stackoverflow.com/questions/4428335/how-to-get-the-exact-text-margins-used-by-textrenderer var gdiHeight = TextRenderer.MeasureText(e.Graphics, "W", Font, Size.Empty).Height; var padding = (int)Math.Ceiling(gdiHeight / 6f); var currentX = PAD_X + padding; var currentY = PAD_Y; int visibleLines = (ClientSize.Height - PAD_Y * 2 - 4) / charSize.Height; const string Header = " Offset 0 1 2 3 4 5 6 7 8 9 A B C D E F Ascii"; TextRenderer.DrawText(e.Graphics, Header, Font, new Point(currentX, currentY), HeaderColor, TextFormatFlags.NoPadding); currentY += charSize.Height + 2; visibleLines -= 2; var len = (int)Math.Min(visibleLines * 0x10, stream.Length - currentIndexBase); byte[] data = new byte[len]; stream.Position = currentIndexBase; stream.Read(data, 0, data.Length); int offset = 0; for (int i = 0; i < visibleLines; i++) { currentX = PAD_X + padding; if (offset < data.Length) { TextRenderer.DrawText(e.Graphics, currentIndexBase.ToString("X8"), Font, new Point(currentX, currentY), HeaderColor, TextFormatFlags.NoPadding); currentX += charSize.Width * 10; PaintLine(e.Graphics, data, currentIndexBase, offset, currentX, currentY, padding); } currentY += charSize.Height; currentIndexBase += 0x10; offset += 0x10; } currentX = PAD_X + padding; currentY += 4; TextRenderer.DrawText(e.Graphics, GetStatusText(), Font, new Point(currentX, currentY), ForeColor, TextFormatFlags.NoPadding); var borderBounds = new Rectangle(PAD_X / 2, PAD_Y / 2, (15 + 16 * 3 + 16) * charSize.Width + PAD_X, (visibleLines + 1) * charSize.Height + PAD_Y + 2); ControlPaint.DrawBorder(e.Graphics, borderBounds, BorderColor, ButtonBorderStyle.Solid); using (var pen = new Pen(BorderColor, 1)) { var hexX = borderBounds.Left + 10 * charSize.Width; e.Graphics.DrawLine(pen, hexX, borderBounds.Top, hexX, borderBounds.Bottom - 1); var ascX = borderBounds.Left + (12 + 16 * 3) * charSize.Width; e.Graphics.DrawLine(pen, ascX, borderBounds.Top, ascX, borderBounds.Bottom - 1); var hdrY = borderBounds.Top + charSize.Height + 2; e.Graphics.DrawLine(pen, borderBounds.Left, hdrY, borderBounds.Right - 1, hdrY); } } base.OnPaint(e); }