Skip to content

Instantly share code, notes, and snippets.

@MCJack123
Last active May 6, 2018 23:44
Show Gist options
  • Save MCJack123/68a91b2b4f6c225a88b6b85d328340f1 to your computer and use it in GitHub Desktop.
Save MCJack123/68a91b2b4f6c225a88b6b85d328340f1 to your computer and use it in GitHub Desktop.
A simple ncurses-based hex editor supporting files up to 4GB
// compile with: g++ -o hexedit -std=c++11 hexedit.cpp -lncurses
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <utility>
#include <iomanip>
#include <stdint.h>
#include <math.h>
#include <ncurses.h>
#define ZERO byte(0) // quick 0x00 access
#include <vector> // for cache vector
#include <bitset> // for byte type
#include <map> // for memory map
class byte: public std::bitset<8> {
public:
using std::bitset<8>::bitset;
bool operator <(const byte &rhs) const {
return to_ulong() < rhs.to_ulong();
}
bool operator >(const byte &rhs) const {
return to_ulong() > rhs.to_ulong();
}
};
class byte16: public std::bitset<32> {
public:
using std::bitset<32>::bitset;
bool operator <(const byte16 &rhs) const {
return to_ulong() < rhs.to_ulong();
}
bool operator >(const byte16 &rhs) const {
return to_ulong() > rhs.to_ulong();
}
};
std::string intToHex( unsigned long i , size_t width )
{
std::stringstream stream;
stream << std::setfill ('0') << std::setw(width) << std::hex << i;
return stream.str();
}
char makeChar(unsigned long l) {
if (l >= 32 && l < 128) return (char)l;
else return '.';
}
int resize(int previous) {
//echo();
WINDOW * win = newwin(4 - (LINES % 2), 14 + (COLS % 2), (LINES - 4 + (LINES % 2)) / 2, (COLS - 14 - (COLS % 2)) / 2);
keypad(win, TRUE);
int h, w;
getmaxyx(win, h, w);
//nodelay(win, TRUE);
bool odd = (LINES % 2) == 1;
int textx, texty, cursorpos;
std::vector<int> text;
if (odd) {
textx = 8;
texty = 1;
} else {
textx = 2;
texty = 2;
}
std::string previoustext = std::to_string(previous);
for (int i = 0; i < previoustext.size(); i++) text.push_back(previoustext[i] - 48);
cursorpos = text.size();
if (cursorpos > 10) cursorpos = 10;
for (int j = 0; j < w; j++) {
mvwaddch(win, 0, j, '-');
mvwaddch(win, h-1, j, '-');
}
mvwaddch(win, 1, 0, '|');
mvwaddch(win, 1, w - 1, '|');
if (!odd) {
mvwaddch(win, 2, 0, '|');
mvwaddch(win, 2, w - 1, '|');
}
mvwaddstr(win, 1, 2, "Size:");
mvwaddstr(win, texty, textx, std::to_string(previous).c_str());
wrefresh(win);
bool done = false;
while (!done) {
int ch = wgetch(win);
switch (ch) {
case KEY_BACKSPACE:
if (cursorpos > 0) {
text.pop_back();
mvwaddch(win, texty, textx + --cursorpos, ' ');
wmove(win, texty, textx + cursorpos);
}
break;
case 127:
if (cursorpos > 0) {
text.pop_back();
mvwaddch(win, texty, textx + --cursorpos, ' ');
wmove(win, texty, textx + cursorpos);
}
break;
case KEY_ENTER:
done = true;
break;
case 10:
done = true;
break;
case '0':
if (cursorpos < 10) {text.push_back(0); mvwaddch(win, texty, textx + cursorpos++, '0');}
break;
case '1':
if (cursorpos < 10) {text.push_back(1); mvwaddch(win, texty, textx + cursorpos++, '1');}
break;
case '2':
if (cursorpos < 10) {text.push_back(2); mvwaddch(win, texty, textx + cursorpos++, '2');}
break;
case '3':
if (cursorpos < 10) {text.push_back(3); mvwaddch(win, texty, textx + cursorpos++, '3');}
break;
case '4':
if (cursorpos < 10) {text.push_back(4); mvwaddch(win, texty, textx + cursorpos++, '4');}
break;
case '5':
if (cursorpos < 10) {text.push_back(5); mvwaddch(win, texty, textx + cursorpos++, '5');}
break;
case '6':
if (cursorpos < 10) {text.push_back(6); mvwaddch(win, texty, textx + cursorpos++, '6');}
break;
case '7':
if (cursorpos < 10) {text.push_back(7); mvwaddch(win, texty, textx + cursorpos++, '7');}
break;
case '8':
if (cursorpos < 10) {text.push_back(8); mvwaddch(win, texty, textx + cursorpos++, '8');}
break;
case '9':
if (cursorpos < 10) {text.push_back(9); mvwaddch(win, texty, textx + cursorpos++, '9');}
break;
}
wrefresh(win);
}
int finalnum = 0;
for (int i = 0; i < text.size(); i++) finalnum += pow(10, i) * text[text.size()-i-1];
wrefresh(win);
delwin(win);
//noecho();
return finalnum;
}
std::pair<std::map<byte16, byte>, int> memEdit(std::map<byte16, byte> mem, int mapsize) {
//mapsize--;
initscr();
cbreak();
noecho();
keypad(stdscr, TRUE);
nodelay(stdscr, TRUE);
if (COLS < 47) {
nodelay(stdscr, FALSE);
keypad(stdscr, FALSE);
echo();
nocbreak();
endwin();
std::cout << "The terminal is too narrow for the editor! Exiting.\n";
return std::make_pair(mem, mapsize);
}
int mode;
bool bars;
if (COLS < 50) {mode = 0; bars = false;}
else if (COLS >= 50 && COLS < 70) {mode = 0; bars = true;}
else if (COLS >= 70 && COLS < 80) {mode = 1; bars = true;}
else if (COLS >= 80 && COLS < 83) {mode = 2; bars = false;}
else {mode = 2; bars = true;}
for (int x = 0; x < COLS; x++) {
mvaddch(0, x, '-');
mvaddch(LINES-1, x, '-');
}
for (int y = 1; y < LINES - 1; y++) {
if (bars) mvaddch(y, 0, '|');
if (mode == 2) mvaddch(y, 11+((int)bars*2), '|');
if (bars) mvaddch(y, COLS-1, '|');
if (mode > 0) mvaddch(y, COLS-18-((int)bars*2), '|');
if (mode == 2) mvaddch(y, (int)bars * 2, '0');
if (mode == 2) mvaddch(y, (int)bars * 2 + 1, 'x');
refresh();
}
unsigned int current_offset = 0x0000;
int typing = -1;
unsigned int top_offset = 0x0000;
unsigned int bottom_offset = 0x0010 * (LINES - 3);
while (true) {
for (int y = 1; y < LINES - 1; y++) {
if (mode == 2) mvaddstr(y, 2 + ((int)bars * 2), intToHex(0x0010 * (y - 1) + top_offset, 8).c_str());
for (int off = 0; off < 16; off++) {
mvaddstr(y, (13 * (mode == 2)) + (3*off) + ((int)bars * 2), intToHex((uint8_t)mem[byte16((0x0010 * (y - 1)) + off + top_offset)].to_ulong(), 2).c_str());
if (mode > 0) mvaddch(y, COLS - ((int)bars * 2) - (16-off), makeChar(mem[byte16((0x0010 * (y - 1)) + off + top_offset)].to_ulong()));
}
}
refresh();
while (true) {
bool exit = false;
const char * curhex = intToHex((uint8_t)mem[byte16(current_offset)].to_ulong(), 2).c_str();
//mvaddstr(0, 0, intToHex(bottom_offset, 4).c_str());
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), curhex[0] | A_STANDOUT);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), curhex[1] | A_STANDOUT);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()) | A_STANDOUT);
refresh();
Loop:
move(0, 0);
int ch = getch();
switch (ch) {
case KEY_UP:
if (current_offset >= 0x0010) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), curhex[0]);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), curhex[1]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
current_offset -= 0x0010;
} else break;
if (top_offset > current_offset) {
top_offset -= 0x0010;
bottom_offset -= 0x0010;
int oldx, oldy;
getyx(stdscr, oldy, oldx);
move(LINES - 1, oldx);
exit = true;
}
break;
case KEY_DOWN:
if (current_offset < mapsize - 0x0010) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), curhex[0]);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), curhex[1]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
current_offset += 0x0010;
} else break;
if (bottom_offset < current_offset) {
bottom_offset += 0x0010;
top_offset += 0x0010;
int oldx, oldy;
getyx(stdscr, oldy, oldx);
move(LINES - 1, oldx);
refresh();
exit = true;
}
break;
case KEY_LEFT:
if (current_offset > 0x0000 && current_offset % 0x0010 > 0) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), curhex[0]);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), curhex[1]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
current_offset--;
}
break;
case KEY_RIGHT:
if (current_offset < mapsize - 1 && current_offset % 16 < 15) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), curhex[0]);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), curhex[1]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
current_offset++;
}
break;
case 'r': {
int newSize = resize(mapsize);
if (newSize > mapsize) for (unsigned long i = mapsize; i < newSize; i++) mem[byte16(i)] = ZERO;
if (newSize < current_offset) {
current_offset = newSize - 1;
if (top_offset > current_offset) {
top_offset -= 0x0010;
bottom_offset -= 0x0010;
int oldx, oldy;
getyx(stdscr, oldy, oldx);
move(LINES - 1, oldx);
}
if (bottom_offset < current_offset) {
bottom_offset += 0x0010;
top_offset += 0x0010;
int oldx, oldy;
getyx(stdscr, oldy, oldx);
move(LINES - 1, oldx);
refresh();
}
}
mapsize = newSize;
exit = true;
break;
}
case '0':
if (typing == -1) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), ch | A_STANDOUT);
typing = 0;
refresh();
goto Loop;
} else {
mem[byte16(current_offset)] = byte((typing * 16) + 0);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), ch);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), intToHex(typing, 1).c_str()[0]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
typing = -1;
if (current_offset < mapsize - 1) current_offset++;
}
break;
case '1':
if (typing == -1) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), ch | A_STANDOUT);
typing = 1;
refresh();
goto Loop;
} else {
mem[byte16(current_offset)] = byte((typing * 16) + 1);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), ch);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), intToHex(typing, 1).c_str()[0]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
typing = -1;
if (current_offset < mapsize - 1) current_offset++;
}
break;
case '2':
if (typing == -1) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), ch | A_STANDOUT);
typing = 2;
refresh();
goto Loop;
} else {
mem[byte16(current_offset)] = byte((typing * 16) + 2);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), ch);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), intToHex(typing, 1).c_str()[0]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
typing = -1;
if (current_offset < mapsize - 1) current_offset++;
}
break;
case '3':
if (typing == -1) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), ch | A_STANDOUT);
typing = 3;
refresh();
goto Loop;
} else {
mem[byte16(current_offset)] = byte((typing * 16) + 3);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), ch);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), intToHex(typing, 1).c_str()[0]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
typing = -1;
if (current_offset < mapsize - 1) current_offset++;
}
break;
case '4':
if (typing == -1) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), ch | A_STANDOUT);
typing = 4;
refresh();
goto Loop;
} else {
mem[byte16(current_offset)] = byte((typing * 16) + 4);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), ch);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), intToHex(typing, 1).c_str()[0]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
typing = -1;
if (current_offset < mapsize - 1) current_offset++;
}
break;
case '5':
if (typing == -1) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), ch | A_STANDOUT);
typing = 5;
refresh();
goto Loop;
} else {
mem[byte16(current_offset)] = byte((typing * 16) + 5);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), ch);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), intToHex(typing, 1).c_str()[0]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
typing = -1;
if (current_offset < mapsize - 1) current_offset++;
}
break;
case '6':
if (typing == -1) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), ch | A_STANDOUT);
typing = 6;
refresh();
goto Loop;
} else {
mem[byte16(current_offset)] = byte((typing * 16) + 6);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), ch);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), intToHex(typing, 1).c_str()[0]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
typing = -1;
if (current_offset < mapsize - 1) current_offset++;
}
break;
case '7':
if (typing == -1) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), ch | A_STANDOUT);
typing = 7;
refresh();
goto Loop;
} else {
mem[byte16(current_offset)] = byte((typing * 16) + 7);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), ch);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), intToHex(typing, 1).c_str()[0]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
typing = -1;
if (current_offset < mapsize - 1) current_offset++;
}
break;
case '8':
if (typing == -1) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), ch | A_STANDOUT);
typing = 8;
refresh();
goto Loop;
} else {
mem[byte16(current_offset)] = byte((typing * 16) + 8);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), ch);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), intToHex(typing, 1).c_str()[0]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
typing = -1;
if (current_offset < mapsize - 1) current_offset++;
}
break;
case '9':
if (typing == -1) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), ch | A_STANDOUT);
typing = 9;
refresh();
goto Loop;
} else {
mem[byte16(current_offset)] = byte((typing * 16) + 9);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), ch);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), intToHex(typing, 1).c_str()[0]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
typing = -1;
if (current_offset < mapsize - 1) current_offset++;
}
break;
case 'a':
if (typing == -1) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), 'a' | A_STANDOUT);
typing = 10;
refresh();
goto Loop;
} else {
mem[byte16(current_offset)] = byte((typing * 16) + 10);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), 'a');
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), intToHex(typing, 1).c_str()[0]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
typing = -1;
if (current_offset < mapsize - 1) current_offset++;
}
break;
case 'b':
if (typing == -1) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), 'b' | A_STANDOUT);
typing = 11;
refresh();
goto Loop;
} else {
mem[byte16(current_offset)] = byte((typing * 16) + 11);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), 'b');
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), intToHex(typing, 1).c_str()[0]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
typing = -1;
if (current_offset < mapsize - 1) current_offset++;
}
break;
case 'c':
if (typing == -1) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), 'c' | A_STANDOUT);
typing = 12;
refresh();
goto Loop;
} else {
mem[byte16(current_offset)] = byte((typing * 16) + 12);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), 'c');
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), intToHex(typing, 1).c_str()[0]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
typing = -1;
if (current_offset < mapsize - 1) current_offset++;
}
break;
case 'd':
if (typing == -1) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), 'd' | A_STANDOUT);
typing = 13;
refresh();
goto Loop;
} else {
mem[byte16(current_offset)] = byte((typing * 16) + 13);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), 'd');
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), intToHex(typing, 1).c_str()[0]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
typing = -1;
if (current_offset < mapsize - 1) current_offset++;
}
break;
case 'e':
if (typing == -1) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), 'e' | A_STANDOUT);
typing = 14;
refresh();
goto Loop;
} else {
mem[byte16(current_offset)] = byte((typing * 16) + 14);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), 'e');
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), intToHex(typing, 1).c_str()[0]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
typing = -1;
if (current_offset < mapsize - 1) current_offset++;
}
break;
case 'f':
if (typing == -1) {
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), 'f' | A_STANDOUT);
typing = 15;
refresh();
goto Loop;
} else {
mem[byte16(current_offset)] = byte((typing * 16) + 15);
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + 1 + ((int)bars * 2) + ((mode == 2) * 13), 'f');
mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, ((current_offset % 0x0010) * 3) + ((mode == 2) * 13) + ((int)bars * 2), intToHex(typing, 1).c_str()[0]);
if (mode > 0) mvaddch(floor(current_offset / 0x0010) - (top_offset / 0x0010) + 1, COLS - ((int)bars * 2) - (16-(current_offset % 0x0010)), makeChar(mem[byte16(current_offset)].to_ulong()));
typing = -1;
if (current_offset < mapsize - 1) current_offset++;
}
break;
case 'q':
nodelay(stdscr, FALSE);
keypad(stdscr, FALSE);
echo();
nocbreak();
endwin();
return std::make_pair(mem, mapsize);
default:
goto Loop;
}
if (exit) break;
}
}
nodelay(stdscr, FALSE);
keypad(stdscr, FALSE);
echo();
nocbreak();
endwin();
return std::make_pair(mem, mapsize);
}
int main(int argc, const char * argv[]) {
if (argc < 2) {
std::cout << "Usage: " << argv[0] << " <file>\n";
return 1;
}
std::ifstream in(argv[1], std::ios::in | std::ios::binary | std::ios::ate);
int size = in.tellg();
if (size > pow(2, 32)) {
in.close();
std::cout << "The file is too big to be read by this program.\n";
return 2;
}
in.seekg(0);
std::map<byte16, byte> data;
while (!in.eof() && in.good()) data[byte16((unsigned long)in.tellg() - 1)] = byte((unsigned long)in.get());
in.close();
std::pair<std::map<byte16, byte>, int> retval;
retval = memEdit(data, size);
data = std::get<0>(retval);
std::ofstream out(argv[1], std::ios::out | std::ios::trunc);
for (unsigned long i = 0; i < std::get<1>(retval); i++) out.put((char)data[byte16(i)].to_ulong());
out.close();
return 0;
}
mode = 0, bars = false (COLS = 47-49)
-----------------------------------------------
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
-----------------------------------------------
mode = 0, bars = true (COLS = 50-69)
--------------------------------------------------
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F|
--------------------------------------------------
mode = 1, bars = true (COLS = 70-79)
----------------------------------------------------------------------
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
----------------------------------------------------------------------
mode = 2, bars = false (COLS = 80-82)
--------------------------------------------------------------------------------
0x00000000 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000010 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000020 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000030 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000040 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000050 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000060 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000070 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000080 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000090 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000100 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000110 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000120 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000130 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000140 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000150 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000160 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000170 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000180 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000190 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000200 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
0x00000210 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................
--------------------------------------------------------------------------------
mode = 2, bars = true (COLS = 83+)
-----------------------------------------------------------------------------------
| 0x00000000 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000010 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000020 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000030 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000040 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000050 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000060 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000070 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000080 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000090 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000100 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000110 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000120 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000130 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000140 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000150 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000160 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000170 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000180 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000190 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000200 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
| 0x00000210 | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F | ................ |
-----------------------------------------------------------------------------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment