public static void PromptSearchString() { if (FilePanel.CurrentPosition >= FilePanel.FileSize - FilePanel.BufferSize) { InfoPanel.Message("Already at the end of the file."); return; } if (FilePanel.BufferSize >= FilePanel.FileSize) { InfoPanel.Message("Not possible."); return; } _lastString = GetUserInput("Find data:", suggestion: _lastString); if (_lastString == null || _lastString.Length == 0) { FilePanel.Update(); InfoPanel.Message("Canceled."); return; } FilePanel.Update(); InfoPanel.Message("Searching..."); long t = Finder.FindString( _lastString, FilePanel.FileIO, FilePanel.File, FilePanel.CurrentPosition + 1 ); switch (t) { case -1: InfoPanel.Message($"No results."); break; case -2: InfoPanel.Message($"Position out of bound."); break; case -3: InfoPanel.Message($"File not found!"); break; default: MainApp.Goto(t); InfoPanel.Message($"Found at {t:X2}h."); break; } }
public static void PromptOffset() { string c = GetUserInput("Hex, Dec, Oct?"); if (c == null || c.Length < 1) { InfoPanel.Message("Canceled."); FilePanel.Update(); return; } switch (c[0]) { case 'H': case 'h': MainApp.OffsetView = OffsetView.Hex; OffsetPanel.Update(); InfoPanel.Update(); break; case 'O': case 'o': MainApp.OffsetView = OffsetView.Oct; OffsetPanel.Update(); InfoPanel.Update(); break; case 'D': case 'd': MainApp.OffsetView = OffsetView.Dec; OffsetPanel.Update(); InfoPanel.Update(); break; default: InfoPanel.Message("Invalid view mode!"); break; } FilePanel.Update(); }
public static void Initialize() { MenuItem[] mainItems = { new MenuItem("File", new MenuItem("Dump", () => { Exit(); InfoPanel.Message("Dumping..."); Dumper.Dump(FilePanel.File.FullName, MainApp.BytesPerRow, MainApp.OffsetView); InfoPanel.Message("Done"); }), new MenuItem(), new MenuItem("Exit", () => { inMenu = false; MainApp.Exit(); }) ), /* * new MenuItem("Edit", null, * new MenuItem("Test") * ),*/ new MenuItem("Search", new MenuItem("Find byte...", () => { Exit(); Dialog.PromptFindByte(); }), new MenuItem("Find ASCII string...", () => { Exit(); Dialog.PromptSearchString(); }), new MenuItem(), new MenuItem("Goto...", () => { Exit(); Dialog.PromptGoto(); }) ), new MenuItem("View", new MenuItem("Offset view...", () => { Exit(); Dialog.PromptOffset(); }), new MenuItem(), new MenuItem("File info", () => { Exit(); InfoPanel.DisplayFileInfo(); }), new MenuItem(), new MenuItem("Refresh", () => { Exit(); FilePanel.Refresh(); }) ), /* * new MenuItem("Tools", null, * new MenuItem("Test") * ),*/ #if DEBUG new MenuItem("Debug", new MenuItem("Show Test Window", () => { Exit(); new Window("Test", new Control[] { new Label("Hello World!") }).Show(); }), new MenuItem("Goto", () => { Exit(); new Window("Goto", new Control[] { new Label("Hello World!", 1, 1), new Button("OK", 12, 3, action: () => { MainApp.Goto(0xdd);}) }).Show(); }), new MenuItem("Preferences...", () => { Exit(); new Window("Test", 50, 6, new Control[] { new Label("Setting 1:", 1, 1), new Button("OK", 12, 3) }).Show(); }) ), #endif new MenuItem("?", new MenuItem("About", () => { Exit(); Dialog.GenerateWindow( title: "About", text: $"{Program.Name} v{Program.Version}\nCopyright (c) 2015-2017 dd86k", width: 36, height: 5 ); }) ) }; // Make an array for each, remember that arrays are REFERENCED. _pos = new int[mainItems.Length]; _miw = new int[mainItems.Length]; MenuItems = new List <MenuItem>(mainItems.Length); MenuItems.AddRange(mainItems); _barlength = 0; // Get menubar's length with items for (int i = 0; i < MenuItems.Count; ++i) { MenuItem item = MenuItems[i]; _pos[i] = _barlength; _barlength += item.Text.Length + 2; int max = 0; // Get longuest string in each submenus for (int si = 0; si < item.Items.Count; si++) { MenuItem subitem = MenuItems[i].Items[si]; if (!subitem.IsSeparator) { int len = subitem.Text.Length; if (len > max) { max = len; } } } _miw[i] = max; } Draw(); }
public static void PromptFindByte() { if (FilePanel.CurrentPosition + FilePanel.BufferSize >= FilePanel.FileSize) { InfoPanel.Message("Already at the end of the file."); return; } long t = GetNumberFromUser("Find byte:", suggestion: $"0x{_lastByte:X2}"); if (t == -1) { FilePanel.Update(); InfoPanel.Message("Canceled."); return; } if (t < 0 || t > byte.MaxValue) { FilePanel.Update(); InfoPanel.Message("A value between 0 and 255 is required."); } else { FilePanel.Update(); InfoPanel.Message("Searching..."); long p = Finder.FindByte( _lastByte = (byte)t, FilePanel.FileIO, FilePanel.File, FilePanel.CurrentPosition + 1 ); if (p > 0) { MainApp.Goto(--p); if (p > uint.MaxValue) { InfoPanel.Message($"Found {t:X2} at {p:X16}"); } else { InfoPanel.Message($"Found {t:X2} at {p:X8}"); } } else { switch (p) { case -1: InfoPanel.Message($"No results."); break; case -2: InfoPanel.Message($"Position out of bound."); break; case -3: InfoPanel.Message($"File not found!"); break; default: InfoPanel.Message($"Unknown error occurred. (0x{p:X2})"); break; } } } }
/// <summary> /// Find a string of data with a given position. /// </summary> /// <param name="data">Data as a string.</param> /// <param name="pos">Starting position.</param> /// <returns><see cref="FindResult"/></returns> /// <remarks> /// How does this work? /// Search every character, if the first one seems to be right, /// read the data and compare it. /// </remarks> public static long FindString(string data, FileStream io, FileInfo file, long pos = 0) { if (!file.Exists) { return(-3); } if (pos < 0 || pos > file.Length) { return(-2); } if (string.IsNullOrWhiteSpace(data)) { return(-4); } io.Position = pos; byte[] b = new byte[data.Length]; bool c = true; if (data.StartsWith("/") && data.EndsWith("/")) { RegexOptions rf = RegexOptions.Compiled | RegexOptions.ECMAScript | RegexOptions.CultureInvariant; Regex r = new Regex(data.Trim(new char[] { '/' }), rf); InfoPanel.Message("Searching with regex..."); while (c) { if (io.Position + data.Length > io.Length) { c = false; } if (r.IsMatch(char.ToString((char)io.ReadByte()))) { if (data.Length == 1) { return(io.Position - 1); } else { io.Position--; io.Read(b, 0, b.Length); if (r.IsMatch(Encoding.ASCII.GetString(b))) { return(io.Position - data.Length); } } } } } else // Non-regex { while (c) { if (io.Position + data.Length > io.Length) { c = false; } if (data[0] == (char)io.ReadByte()) { if (data.Length == 1) { return(io.Position - 1); } else { io.Position--; io.Read(b, 0, b.Length); if (data == Encoding.ASCII.GetString(b)) { return(io.Position - data.Length); } } } } } io.Position = pos - 1; return(-1); }