----------------------------------------------------------------------- -- SciTEHexEdit: A Self-Contained Primitive Hex Editor for SciTE ------------------------------------------------------------------------ -- Copyright 2005-2006 by Kein-Hong Man -- small correction by mozers local VERSION, REVDATE = "0.10", "20061011" -- version information -- -- See SciTEHexEdit.txt for usage instructions. -- Development platform: WinXP SciTE 1.71 (Mingw) ------------------------------------------------------------------------ -- Permission to use, copy, modify, and distribute this software and -- its documentation for any purpose and without fee is hereby granted, -- provided that the above copyright notice appear in all copies and -- that both that copyright notice and this permission notice appear -- in supporting documentation. -- -- KEIN-HONG MAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, -- INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN -- NO EVENT SHALL KEIN-HONG MAN BE LIABLE FOR ANY SPECIAL, INDIRECT OR -- CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS -- OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, -- NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION -- WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. ------------------------------------------------------------------------ -- QUICKSTART -- * Load this script and call HexEditor() or HexEditorNew() ------------------------------------------------------------------------ -- TODO -- * Improve file size limit warning. What is the best approach? -- * Search and replace function. This may change the file size. -- * Allow file size change, e.g. append new data to end of a file. -- * Parameter window to open a file in hex mode. -- * Undo and redo, might be possible to use < and >. -- * Load-on-demand may be better than loading everything in at once. -- * Console string arguments currently has a simple escape mechanism. -- * IEEE single and double conversion for Number command. ------------------------------------------------------------------------ -- TECHNICAL NOTES -- * For safety, a backup file is always created. -- * Up-down movement can sometimes jump columns, hard to fix perfectly. -- * Error message display to console/output window still a bit messy. ------------------------------------------------------------------------ ------------------------------------------------------------------------ -- constants and primitives ------------------------------------------------------------------------ local SIZELIMIT = 100 -- maximum size allowable (bytes) local BLOCKSIZE = 1048576 -- = SIZELIMIT * BLOCKSIZE local PAGESIZE = 256 -- # of bytes on screen at once local PAGE1K = 1024 -- # of bytes for bigger jumps local NONPRINTING = 32 -- ASCII substitute for control chars local MONOSPACE = nil -- monospace property override -- e.g. "font:Courier New,size:10" local CONSOLEPROMPT = "> " -- default console prompt local SIG = "SciTEHexEdit" local string, math, table = string, math, table local MSG = { -- mostly textual data ------------------------------------------------------------------------ -- edit window elements -- TODO: R (Replace) ------------------------------------------------------------------------ Header = [[ . SciTE Hex Editor . ver VERSION . REVDATE . ]], Footer = [[ | / (FirstPg) [ (Prev1K) - (PrevPg) + (NextPg) ] (Next1K) * (LastPg) | | . or ` (Console Mode) N (Find Next) H (Help Screen) | ]], FootWinLn = [[ +--------------------------------------------------------------------+ ]], Buttons = [[ | Refresh | Revert | FirstPg | PrevPg | NextPg | LastPg | Console | Help | ]], ButtonLn = 2, EditWinLn = [[ +--------+-------------------------------------------------+----------------+ ]], EditLegend = [[ | Offset | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ASCII view | ]], ------------------------------------------------------------------------ -- console start message -- TODO: -- replace Search and replace. Replacement can either be a -- literal string (single quotes) or a hex number. ------------------------------------------------------------------------ ConsoleStart = [[ SciTE Hex Editor Console Enter single commands at prompt line only. Most commands closes the console window on successful execution. Adding a "-" as a suffix will stop the console window from closing, e.g. "save-". Commands (which are case insensitive) are: cls Reset console to start condition. info Display information about current file. goto Go to position, can use hex. Example: goto 1234 load Load file. Example: load foo.txt save [] Save file. "saveas" is an alias. Example: save revert Reload the file and lose all current changes. search Search. Can be hex number (OxDEADBEEF), literal string ('FOO'), or Lua regexp ("^The%s"). number Converts a hex number into decimal, or a decimal number into various forms. Also works with strings. help Show help window. Pressing H returns to here. exit Return to edit window. "close" is an alias. ]], ------------------------------------------------------------------------ -- help screen message ------------------------------------------------------------------------ HelpText = [[ Help for SciTE Hex Editor ------------------------- http://luaforge.net/projects/sl-hexedit/ SciTEHexEdit is operated mainly through keypresses. The "buttons" are operated by double-clicking or by pressing [Space] with the caret on the desired button. Other commands are accessed through a console window, which is enabled using a period (.) or a backtick (`). Other keypress commands are listed at the bottom of the edit window. When initializing SciTEHexEdit to open the file currently viewed, the hex edit view is automatically positioned where the caret was in the normal SciTE edit view. SciTEHexEdit always loads the file in question, but if the buffer is untitled and unsaved, its current contents is used. All three columns or windows of the main edit view can be modified: (a) Offset window: pressing a valid digit changes the address of the page in view, and the editor will jump to the block in question. (b) Hex data window: pressing a valid digit changes data in the file. To make the changes permanent, save the file. To retrieve original values, use the "Revert" button or the "revert" console command. (c) ASCII data window: pressing a character changes data in the file. Changes can be saved or dropped. Not all characters can be entered, because some keypresses are handled only by SciTE itself. Also, [Enter] moves one line down in the edit windows, and [Space] moves the caret to the next byte in the hex data window. See SciTEHexEdit.txt for further information. More Lua extension scripts can be found at http://lua-users.org/wiki/SciteScripts Press H again to return to the previous screen...]], } ------------------------------------------------------------------------ -- user interface data ------------------------------------------------------------------------ local KeyMap = { -- keymap translation ["+"] = "NextPg", ["-"] = "PrevPg", ["]"] = "Next1K", ["["] = "Prev1K", ["/"] = "FirstPg", ["*"] = "LastPg", ["."] = "Console", ["`"] = "Console", ["H"] = "Help", ["h"] = "Help", ["N"] = "FindNext", ["n"] = "FindNext", --TODO ["R"] = "Replace", ["r"] = "Replace", } local WIN = { -- window has fixed bounds POS = { x1 = 2, y1 = 7, x2 = 9, y2 = 22, }, HEX = { x1 = 11, y1 = 7, x2 = 59, y2 = 22, }, ASC = { x1 = 61, y1 = 7, x2 = 76, y2 = 22, }, } ------------------------------------------------------------------------ -- colour scheme for edit window and character set -- * possible properties: Back, Bold, Case, EOLFilled, Font, Fore, -- Italic, Size, Underline, Visible (and possibly more...) ------------------------------------------------------------------------ local ColourScheme = { [ 8] = {Fore = "000000", Bold = true}, -- title [16] = {Fore = "800000",}, -- button bar [17] = {Fore = "FFFFFF", Back = "800000"}, -- button text [18] = {Fore = "000000", Bold = true}, -- edit box header [19] = {Fore = "000080"}, -- position pane [20] = {Fore = "808000",}, -- information box [21] = {Fore = "808000", Bold = true}, -- save file info -- data highlighting, unchanged [ 1] = {Fore = "800080",}, -- control chars [ 2] = {Fore = "333399",}, -- symbols [ 3] = {Fore = "008080",}, -- numbers [ 4] = {Fore = "000000",}, -- alphabets [ 5] = {Fore = "008000",}, -- extended chars [ 6] = {Fore = "C0C0C0",}, -- past-EOF [ 7] = {Fore = "000000",}, -- ASCII pane -- data highlighting, changed [ 9] = {Fore = "800080", Back = "FFFF00"}, -- control chars [10] = {Fore = "333399", Back = "FFFF00"}, -- symbols [11] = {Fore = "008080", Back = "FFFF00"}, -- numbers [12] = {Fore = "000000", Back = "FFFF00"}, -- alphabets [13] = {Fore = "008000", Back = "FFFF00"}, -- extended chars [14] = {Fore = "C0C0C0", Back = "FFFF00"}, -- past-EOF [15] = {Fore = "000000", Back = "FFFF00"}, -- ASCII pane } -- byte value colour coding local CharColours = { -- {,,