hexedit.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* hexedit.c - Hexadecimal file editor
  2. *
  3. * Copyright 2015 Rob Landley <rob@landley.net>
  4. *
  5. * No standard.
  6. USE_HEXEDIT(NEWTOY(hexedit, "<1>1r", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_LOCALE))
  7. config HEXEDIT
  8. bool "hexedit"
  9. default y
  10. help
  11. usage: hexedit FILE
  12. Hexadecimal file editor/viewer. All changes are written to disk immediately.
  13. -r Read only (display but don't edit)
  14. Keys:
  15. Arrows Move left/right/up/down by one line/column
  16. PgUp/PgDn Move up/down by one page
  17. Home/End Start/end of line (start/end of file with ctrl)
  18. 0-9, a-f Change current half-byte to hexadecimal value
  19. ^J or : Jump (+/- for relative offset, otherwise absolute address)
  20. ^F or / Find string (^G/n: next, ^D/p: previous match)
  21. u Undo
  22. x Toggle bw/color display
  23. q/^C/^Q/Esc Quit
  24. */
  25. #define FOR_hexedit
  26. #include "toys.h"
  27. GLOBALS(
  28. char *data, *search, keybuf[16], input[80];
  29. long long len, base, pos;
  30. int numlen, undo, undolen, mode;
  31. unsigned rows, cols;
  32. )
  33. #define UNDO_LEN (sizeof(toybuf)/(sizeof(long long)+1))
  34. static void show_error(char *what)
  35. {
  36. printf("\e[%dH\e[41m\e[37m\e[K\e[1m%s\e[0m", TT.rows+1, what);
  37. xflush(1);
  38. msleep(500);
  39. }
  40. // TODO: support arrow keys, insertion, and scrolling (and reuse in vi)
  41. static int prompt(char *prompt, char *initial_value)
  42. {
  43. int yes = 0, key, len = strlen(initial_value);
  44. strcpy(TT.input, initial_value);
  45. while (1) {
  46. printf("\e[%dH\e[K\e[1m%s: \e[0m%s\e[?25h", TT.rows+1, prompt, TT.input);
  47. xflush(1);
  48. key = scan_key(TT.keybuf, -1);
  49. if (key < 0 || key == 27) break;
  50. if (key == '\r') {
  51. yes = len; // Hitting enter with no input counts as cancellation.
  52. break;
  53. }
  54. if (key == 0x7f && (len > 0)) TT.input[--len] = 0;
  55. else if (key == 'U'-'@') while (len > 0) TT.input[--len] = 0;
  56. else if (key >= ' ' && key < 0x7f && len < sizeof(TT.input))
  57. TT.input[len++] = key;
  58. }
  59. printf("\e[?25l");
  60. return yes;
  61. }
  62. // Render all characters printable, using color to distinguish.
  63. static void draw_char(int ch)
  64. {
  65. if (ch >= ' ' && ch < 0x7f) {
  66. putchar(ch);
  67. return;
  68. }
  69. if (TT.mode) {
  70. if (ch>127) {
  71. printf("\e[2m");
  72. ch &= 127;
  73. }
  74. if (ch<32 || ch==127) {
  75. printf("\e[7m");
  76. if (ch==127) ch = 32;
  77. else ch += 64;
  78. }
  79. xputc(ch);
  80. } else {
  81. if (ch < ' ') printf("\e[31m%c", ch + '@');
  82. else printf("\e[35m?");
  83. }
  84. printf("\e[0m");
  85. }
  86. static void draw_status(void)
  87. {
  88. char line[80];
  89. printf("\e[%dH\e[K", TT.rows+1);
  90. snprintf(line, sizeof(line), "\"%s\"%s, %#llx/%#llx", *toys.optargs,
  91. FLAG(r) ? " [readonly]" : "", TT.pos, TT.len);
  92. draw_trim(line, -1, TT.cols);
  93. }
  94. static void draw_byte(int byte)
  95. {
  96. if (byte) printf("%02x", byte);
  97. else printf("\e[2m00\e[0m");
  98. }
  99. static void draw_line(long long yy)
  100. {
  101. int x, xx = 16;
  102. yy = (TT.base+yy)*16;
  103. if (yy+xx>=TT.len) xx = TT.len-yy;
  104. if (yy<TT.len) {
  105. printf("\r\e[%dm%0*llx\e[0m ", 33*!TT.mode, TT.numlen, yy);
  106. for (x=0; x<xx; x++) {
  107. putchar(' ');
  108. draw_byte(TT.data[yy+x]);
  109. }
  110. printf("%*s", 2+3*(16-xx), "");
  111. for (x=0; x<xx; x++) draw_char(TT.data[yy+x]);
  112. printf("%*s", 16-xx, "");
  113. }
  114. printf("\e[K");
  115. }
  116. static void draw_page(void)
  117. {
  118. int y;
  119. for (y = 0; y<TT.rows; y++) {
  120. printf(y ? "\r\n" : "\e[H");
  121. draw_line(y);
  122. }
  123. draw_status();
  124. }
  125. // side: 0 = editing left, 1 = editing right, 2 = clear, 3 = read only
  126. static void highlight(int xx, int yy, int side)
  127. {
  128. char cc = TT.data[16*(TT.base+yy)+xx];
  129. int i;
  130. // Display cursor in hex area.
  131. printf("\e[%u;%uH\e[%dm", yy+1, TT.numlen+3*(xx+1), 7*(side!=2));
  132. if (side>1) draw_byte(cc);
  133. else for (i=0; i<2;) {
  134. if (side==i) printf("\e[32m");
  135. printf("%x", (cc>>(4*(1&++i)))&15);
  136. }
  137. // Display cursor in text area.
  138. printf("\e[7m\e[%u;%uH"+4*(side==2), yy+1, 1+TT.numlen+17*3+xx);
  139. draw_char(cc);
  140. }
  141. static void find_next(int pos)
  142. {
  143. char *p;
  144. p = memmem(TT.data+pos, TT.len-pos, TT.search, strlen(TT.search));
  145. if (p) TT.pos = p - TT.data;
  146. else show_error("No match!");
  147. }
  148. static void find_prev(int pos)
  149. {
  150. size_t len = strlen(TT.search);
  151. for (; pos >= 0; pos--) {
  152. if (!memcmp(TT.data+pos, TT.search, len)) {
  153. TT.pos = pos;
  154. return;
  155. }
  156. }
  157. show_error("No match!");
  158. }
  159. void hexedit_main(void)
  160. {
  161. long long y;
  162. int x, i, side = 0, key, fd;
  163. // Terminal setup
  164. TT.cols = 80;
  165. TT.rows = 24;
  166. terminal_size(&TT.cols, &TT.rows);
  167. if (TT.rows) TT.rows--;
  168. xsignal(SIGWINCH, generic_signal);
  169. sigatexit(tty_sigreset);
  170. dprintf(1, "\e[0m\e[?25l");
  171. xset_terminal(1, 1, 0, 0);
  172. if (access(*toys.optargs, W_OK)) toys.optflags |= FLAG_r;
  173. fd = xopen(*toys.optargs, FLAG(r) ? O_RDONLY : O_RDWR);
  174. if ((TT.len = fdlength(fd))<1) error_exit("bad length");
  175. if (sizeof(long)==32 && TT.len>SIZE_MAX) TT.len = SIZE_MAX;
  176. // count file length hex in digits, rounded up to multiple of 4
  177. for (TT.pos = TT.len, TT.numlen = 0; TT.pos; TT.pos >>= 4, TT.numlen++);
  178. TT.numlen += (4-TT.numlen)&3;
  179. TT.data=xmmap(0, TT.len, PROT_READ|(PROT_WRITE*!FLAG(r)), MAP_SHARED, fd, 0);
  180. close(fd);
  181. draw_page();
  182. for (;;) {
  183. // Scroll display if necessary
  184. if (TT.pos<0) TT.pos = 0;
  185. if (TT.pos>=TT.len) TT.pos = TT.len-1;
  186. x = TT.pos&15;
  187. y = TT.pos/16;
  188. // scroll up
  189. while (y<TT.base) {
  190. if (TT.base-y>(TT.rows/2)) {
  191. TT.base = y;
  192. draw_page();
  193. } else {
  194. TT.base--;
  195. printf("\e[H\e[1L");
  196. draw_line(0);
  197. }
  198. }
  199. // scroll down
  200. while (y>=TT.base+TT.rows) {
  201. if (y-(TT.base+TT.rows)>(TT.rows/2)) {
  202. TT.base = y-TT.rows-1;
  203. draw_page();
  204. } else {
  205. TT.base++;
  206. printf("\e[H\e[1M\e[%uH", TT.rows);
  207. draw_line(TT.rows-1);
  208. }
  209. }
  210. draw_status();
  211. y -= TT.base;
  212. // Display cursor and flush output
  213. highlight(x, y, FLAG(r) ? 3 : side);
  214. xflush(1);
  215. // Wait for next key
  216. key = scan_key(TT.keybuf, -1);
  217. // Window resized?
  218. if (key == -3) {
  219. toys.signal = 0;
  220. terminal_size(&TT.cols, &TT.rows);
  221. if (TT.rows) TT.rows--;
  222. draw_page();
  223. continue;
  224. }
  225. if (key == 'x') {
  226. TT.mode = !TT.mode;
  227. printf("\e[0m");
  228. draw_page();
  229. continue;
  230. }
  231. // Various popular ways to quit...
  232. if (key==-1||key==('C'-'@')||key==('Q'-'@')||key==27||key=='q') break;
  233. highlight(x, y, 2);
  234. if (key == ('J'-'@') || key == ':' || key == '-' || key == '+') {
  235. // Jump (relative or absolute)
  236. char initial[2] = {}, *s = 0;
  237. long long val;
  238. if (key == '-' || key == '+') *initial = key;
  239. if (!prompt("Jump to", initial)) continue;
  240. val = estrtol(TT.input, &s, 0);
  241. if (!errno && s && !*s) {
  242. if (*TT.input == '-' || *TT.input == '+') TT.pos += val;
  243. else TT.pos = val;
  244. }
  245. continue;
  246. } else if (key == ('F'-'@') || key == '/') { // Find
  247. if (!prompt("Find", TT.search ? TT.search : "")) continue;
  248. // TODO: parse hex escapes in input, and record length to support \0
  249. free(TT.search);
  250. TT.search = xstrdup(TT.input);
  251. find_next(TT.pos);
  252. } else if (TT.search && (key == ('G'-'@') || key == 'n')) { // Find next
  253. if (TT.pos < TT.len) find_next(TT.pos+1);
  254. } else if (TT.search && (key == ('D'-'@') || key == 'p')) { // Find previous
  255. if (TT.pos > 0) find_prev(TT.pos-1);
  256. }
  257. // Remove cursor
  258. highlight(x, y, 2);
  259. // Hex digit?
  260. if (key>='a' && key<='f') key-=32;
  261. if (!FLAG(r) && ((key>='0' && key<='9') || (key>='A' && key<='F'))) {
  262. if (!side) {
  263. long long *ll = (long long *)toybuf;
  264. ll[TT.undo] = TT.pos;
  265. toybuf[(sizeof(long long)*UNDO_LEN)+TT.undo++] = TT.data[TT.pos];
  266. if (TT.undolen < UNDO_LEN) TT.undolen++;
  267. TT.undo %= UNDO_LEN;
  268. }
  269. i = key - '0';
  270. if (i>9) i -= 7;
  271. TT.data[TT.pos] &= 15<<(4*side);
  272. TT.data[TT.pos] |= i<<(4*!side);
  273. if (++side==2) {
  274. highlight(x, y, side);
  275. side = 0;
  276. ++TT.pos;
  277. }
  278. } else side = 0;
  279. if (key=='u') {
  280. if (TT.undolen) {
  281. long long *ll = (long long *)toybuf;
  282. TT.undolen--;
  283. if (!TT.undo) TT.undo = UNDO_LEN;
  284. TT.pos = ll[--TT.undo];
  285. TT.data[TT.pos] = toybuf[sizeof(long long)*UNDO_LEN+TT.undo];
  286. }
  287. }
  288. if (key>=256) {
  289. key -= 256;
  290. if (key==KEY_UP) TT.pos -= 16;
  291. else if (key==KEY_DOWN) TT.pos += 16;
  292. else if (key==KEY_RIGHT) {
  293. if (TT.pos<TT.len) TT.pos++;
  294. } else if (key==KEY_LEFT) {
  295. if (TT.pos>0) TT.pos--;
  296. } else if (key==KEY_PGUP) {
  297. TT.pos -= 16*TT.rows;
  298. if (TT.pos < 0) TT.pos = 0;
  299. TT.base = TT.pos/16;
  300. draw_page();
  301. } else if (key==KEY_PGDN) {
  302. TT.pos += 16*TT.rows;
  303. if (TT.pos > TT.len-1) TT.pos = TT.len-1;
  304. TT.base = TT.pos/16;
  305. draw_page();
  306. } else if (key==KEY_HOME) TT.pos &= ~0xf;
  307. else if (key==KEY_END) TT.pos |= 0xf;
  308. else if (key==(KEY_CTRL|KEY_HOME)) TT.pos = 0;
  309. else if (key==(KEY_CTRL|KEY_END)) TT.pos = TT.len-1;
  310. }
  311. }
  312. munmap(TT.data, TT.len);
  313. tty_reset();
  314. }