レポジトリ種類: SVN
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <set>
#include <sstream>
#include "hexeditor.hh"
HexEditor::HexEditor(const std::string &filename)
: curPos(0), dpOffset(0), bpr(16),
statusMode(Status_Normal), modified(false),
running(true),
lastSearchDir(Direction_Forward) {
// ncurses
initscr();
clearok(stdscr, TRUE);
cbreak();
noecho();
keypad(stdscr, TRUE);
start_color();
init_pair(1, COLOR_BLACK, COLOR_WHITE); // カーソル
init_pair(2, COLOR_BLACK, COLOR_GREEN); // 変更
init_pair(3, COLOR_BLACK, COLOR_MAGENTA); // 普通
init_pair(4, COLOR_BLACK, COLOR_CYAN); // コマンド
init_pair(5, COLOR_BLACK, COLOR_YELLOW); // 検索
init_pair(6, COLOR_BLACK, COLOR_YELLOW); // 検索ハイライト
init_pair(7, COLOR_BLACK, COLOR_RED); // エラー
init_pair(8, COLOR_WHITE, COLOR_BLACK); // デフォルト
refresh();
// ファイルをバッファーに読み込む
std::ifstream file(filename, std::ios::binary);
if (!file) {
endwin();
throw std::runtime_error("ファイルを開くに失敗");
}
buf.assign((std::istreambuf_iterator<char>(file)), {});
file.close();
if (buf.empty()) {
endwin();
throw std::runtime_error("ファイルが空です");
}
fname = filename;
// ウィンドウを作成する
getmaxyx(stdscr, rows, cols);
if (rows < 3 || cols < 20) {
endwin();
throw std::runtime_error("ターミナルが小さ過ぎます");
}
bpr = std::min<size_t>(16, (cols / 2 - 10) / 4);
hexPanel = newwin(rows - 2, cols / 2, 0, 0);
asciiPanel = newwin(rows - 2, cols / 2, 0, cols / 2);
scrollok(hexPanel, TRUE);
scrollok(asciiPanel, TRUE);
wattrset(hexPanel, COLOR_PAIR(8));
wattrset(asciiPanel, COLOR_PAIR(8));
}
HexEditor::~HexEditor() {
delwin(hexPanel);
delwin(asciiPanel);
endwin();
}
void HexEditor::highlightcol(size_t i, size_t row, uint8_t byte) {
wattron(hexPanel, COLOR_PAIR(1));
mvwprintw(hexPanel, row, 10 + i * 3, "%02x", byte);
wattroff(hexPanel, COLOR_PAIR(1));
wattron(asciiPanel, COLOR_PAIR(1));
mvwaddch(asciiPanel, row, i, std::isprint(byte) ? byte : '.');
wattroff(asciiPanel, COLOR_PAIR(1));
}
void HexEditor::statusbar() {
int colorPair;
switch (statusMode) {
case Status_Replace: colorPair = 2; break;
case Status_Normal: colorPair = 3; break;
case Status_Command: colorPair = 4; break;
case Status_Search: colorPair = 5; break;
case Status_Error: colorPair = 7; break;
}
std::string status;
if (statusMode == Status_Normal || statusMode == Status_Error) {
status = "FILE: " + fname;
status += " | OFFSET: 0x" + std::to_string(curPos);
status += " | FILE SIZE: " + std::to_string(buf.size()) + " B";
if (!lastSearch.empty()) {
status += " | SEARCH: " + lastSearch;
} else if (!lastHexSearch.empty()) {
std::ostringstream hexSearch;
for (size_t i = 0; i < lastHexSearch.size(); ++i) {
if (i > 0) hexSearch << " ";
hexSearch << std::hex << std::setfill('0') << std::setw(2)
<< (int)lastHexSearch[i];
}
status += " | SEARCH: " + hexSearch.str();
}
status += (modified ? " [+]" : "");
} else if (statusMode == Status_Command) {
status = ":" + statusText;
} else if (statusMode == Status_Search) {
status = (lastSearchDir == Direction_Forward ? "/" : "?") + statusText;
} else if (statusMode == Status_Replace) {
status = "-- REPLACE --";
}
wattron(stdscr, COLOR_PAIR(colorPair));
mvprintw(rows - 1, 0, "%s", status.c_str());
for (int i = status.length(); i < cols; ++i) {
mvaddch(rows - 1, i, ' ');
}
wattroff(stdscr, COLOR_PAIR(colorPair));
}
void HexEditor::render() {
werase(hexPanel);
werase(asciiPanel);
getmaxyx(stdscr, rows, cols);
size_t maxRows = rows - 2;
std::set<size_t> matchBytes;
if (!lastSearch.empty()) {
for (size_t i = 0; i <= buf.size() - lastSearch.size(); ++i) {
bool match = true;
for (size_t j = 0; j < lastSearch.size(); ++j) {
if (buf[i + j] != static_cast<uint8_t>(lastSearch[j])) {
match = false;
break;
}
}
if (match) {
for (size_t j = 0; j < lastSearch.size(); ++j) {
matchBytes.insert(i + j);
}
}
}
} else if (!lastHexSearch.empty()) {
for (size_t i = 0; i <= buf.size() - lastHexSearch.size(); ++i) {
bool match = true;
for (size_t j = 0; j < lastHexSearch.size(); ++j) {
if (buf[i + j] != lastHexSearch[j]) {
match = false;
break;
}
}
if (match) {
for (size_t j = 0; j < lastHexSearch.size(); ++j) {
matchBytes.insert(i + j);
}
}
}
}
// HEXとASCIIの表示
for (size_t row = 0; row < maxRows; ++row) {
size_t offset = dpOffset + row * bpr;
if (offset >= buf.size()) break;
std::ostringstream hexLine, asciiLine;
hexLine << std::hex << std::setfill('0') << std::setw(8) << offset << ": ";
mvwprintw(hexPanel, row, 0, "%s", hexLine.str().c_str());
// HEXとASCIIのデータ
for (size_t i = 0; i < bpr && (offset + i) < buf.size(); ++i) {
uint8_t byte = buf[offset + i];
bool isMatch = matchBytes.count(offset + i) > 0;
if (offset + i == curPos) {
highlightcol(i, row, byte);
} else if (isMatch) {
wattron(hexPanel, COLOR_PAIR(6));
mvwprintw(hexPanel, row, 10 + i * 3, "%02x", byte);
wattroff(hexPanel, COLOR_PAIR(6));
wattron(asciiPanel, COLOR_PAIR(6));
mvwaddch(asciiPanel, row, i, std::isprint(byte) ? byte : '.');
wattroff(asciiPanel, COLOR_PAIR(6));
} else {
wattron(hexPanel, COLOR_PAIR(8));
mvwprintw(hexPanel, row, 10 + i * 3, "%02x", byte);
wattroff(hexPanel, COLOR_PAIR(8));
wattron(asciiPanel, COLOR_PAIR(8));
mvwaddch(asciiPanel, row, i, std::isprint(byte) ? byte : '.');
wattroff(asciiPanel, COLOR_PAIR(8));
}
}
}
statusbar();
redrawwin(hexPanel);
redrawwin(asciiPanel);
wrefresh(hexPanel);
wrefresh(asciiPanel);
refresh();
}
void HexEditor::findNextMatch() {
if (lastSearch.empty() && lastHexSearch.empty()) return;
size_t searchSize = lastSearch.empty() ? lastHexSearch.size() : lastSearch.size();
size_t startPos = curPos + 1;
for (size_t i = startPos; i <= buf.size() - searchSize; ++i) {
bool match = true;
if (!lastSearch.empty()) {
for (size_t j = 0; j < lastSearch.size(); ++j) {
if (buf[i + j] != static_cast<uint8_t>(lastSearch[j])) {
match = false;
break;
}
}
} else {
for (size_t j = 0; j < lastHexSearch.size(); ++j) {
if (buf[i + j] != lastHexSearch[j]) {
match = false;
break;
}
}
}
if (match) {
curPos = i;
dpOffset = curPos - (curPos % bpr);
render();
return;
}
}
for (size_t i = 0; i < startPos && i <= buf.size() - searchSize; ++i) {
bool match = true;
if (!lastSearch.empty()) {
for (size_t j = 0; j < lastSearch.size(); ++j) {
if (buf[i + j] != static_cast<uint8_t>(lastSearch[j])) {
match = false;
break;
}
}
} else {
for (size_t j = 0; j < lastHexSearch.size(); ++j) {
if (buf[i + j] != lastHexSearch[j]) {
match = false;
break;
}
}
}
if (match) {
curPos = i;
dpOffset = curPos - (curPos % bpr);
render();
return;
}
}
}
void HexEditor::findPrevMatch() {
if (lastSearch.empty() && lastHexSearch.empty()) return;
size_t searchSize = lastSearch.empty() ? lastHexSearch.size() : lastSearch.size();
size_t startPos = curPos > 0 ? curPos - 1 : buf.size() - searchSize;
for (size_t i = startPos + 1; i > 0; --i) {
size_t pos = i - 1;
if (pos > buf.size() - searchSize) continue;
bool match = true;
if (!lastSearch.empty()) {
for (size_t j = 0; j < lastSearch.size(); ++j) {
if (buf[pos + j] != static_cast<uint8_t>(lastSearch[j])) {
match = false;
break;
}
}
} else {
for (size_t j = 0; j < lastHexSearch.size(); ++j) {
if (buf[pos + j] != lastHexSearch[j]) {
match = false;
break;
}
}
}
if (match) {
curPos = pos;
dpOffset = curPos - (curPos % bpr);
render();
return;
}
}
for (size_t i = buf.size() - searchSize; i > startPos; --i) {
bool match = true;
if (!lastSearch.empty()) {
for (size_t j = 0; j < lastSearch.size(); ++j) {
if (buf[i + j] != static_cast<uint8_t>(lastSearch[j])) {
match = false;
break;
}
}
} else {
for (size_t j = 0; j < lastHexSearch.size(); ++j) {
if (buf[i + j] != lastHexSearch[j]) {
match = false;
break;
}
}
}
if (match) {
curPos = i;
dpOffset = curPos - (curPos % bpr);
render();
return;
}
}
}
void HexEditor::handleCommand() {
statusMode = Status_Command;
statusText.clear();
render();
int ch;
while ((ch = getch()) != '\n' && ch != 27) {
if (ch == KEY_BACKSPACE || ch == 127) {
if (!statusText.empty()) {
statusText.pop_back();
}
} else if (ch >= 32 && ch <= 126) { // 書き込める文字
statusText += ch;
}
render();
}
if (ch == 27) { // Esc
statusMode = Status_Normal;
statusText.clear();
render();
return;
}
bool isCommand = false;
if (statusText == "w") {
handleSave();
isCommand = true;
} else if (statusText == "q") {
handleQuit(false);
isCommand = true;
} else if (statusText == "wq") {
handleSave();
handleQuit(false);
isCommand = true;
} else if (statusText == "q!") {
handleQuit(true);
isCommand = true;
} else if (statusText == "wq!") {
handleSave();
handleQuit(true);
isCommand = true;
} else if (statusText == "noh") {
lastSearch = "";
isCommand = true;
} else {
statusMode = Status_Error;
statusText.clear();
}
if (isCommand) {
statusMode = Status_Normal;
statusText.clear();
}
render();
}
void HexEditor::handleQuit(bool force) {
if (force || (!force && !modified)) {
running = false;
} else {
statusMode = Status_Error;
statusText.clear();
render();
}
}
void HexEditor::handleSave() {
std::ofstream file(fname, std::ios::binary);
if (file) {
file.write(reinterpret_cast<const char *>(buf.data()), buf.size());
file.close();
modified = false;
}
}
void HexEditor::handleSearch() {
statusMode = Status_Search;
lastSearchDir = Direction_Forward;
statusText.clear();
render();
int ch;
while ((ch = getch()) != '\n' && ch != 27) {
if (ch == KEY_BACKSPACE || ch == 127) {
if (!statusText.empty()) statusText.pop_back();
} else if (ch >= 32 && ch <= 126) { // 書き込める文字
statusText += ch;
}
render();
}
if (ch == 27) { // Esc
statusMode = Status_Normal;
statusText.clear();
render();
return;
}
lastSearch.clear();
lastHexSearch.clear();
matchOff.clear();
std::istringstream iss(statusText);
std::string hexByte;
bool isHexSearch = true;
std::vector<uint8_t> hexSearch;
while (iss >> hexByte) {
if (hexByte.size() != 2 || !std::all_of(hexByte.begin(), hexByte.end(), [](char c) { return std::isxdigit(c); })) {
isHexSearch = false;
break;
}
try {
hexSearch.push_back(static_cast<uint8_t>(std::stoul(hexByte, nullptr, 16)));
} catch (...) {
isHexSearch = false;
break;
}
}
if (isHexSearch && !hexSearch.empty()) { // HEX検索
lastHexSearch = hexSearch;
for (size_t i = curPos + 1; i <= buf.size() - lastHexSearch.size(); ++i) {
bool match = true;
for (size_t j = 0; j < lastHexSearch.size(); ++j) {
if (buf[i + j] != lastHexSearch[j]) {
match = false;
break;
}
}
if (match) {
matchOff.push_back(i);
curPos = i;
dpOffset = curPos - (curPos % bpr);
break;
}
}
} else { // ASCII検索
lastSearch = statusText;
// Find matches and store start offsets
for (size_t i = curPos + 1; i <= buf.size() - lastSearch.size(); ++i) {
bool match = true;
for (size_t j = 0; j < lastSearch.size(); ++j) {
if (buf[i + j] != static_cast<uint8_t>(lastSearch[j])) {
match = false;
break;
}
}
if (match) {
matchOff.push_back(i);
curPos = i;
dpOffset = curPos - (curPos % bpr);
break;
}
}
}
statusMode = Status_Normal;
statusText.clear();
render();
}
void HexEditor::handleReverseSearch() {
statusMode = Status_Search;
lastSearchDir = Direction_Reverse;
statusText.clear();
render();
int ch;
while ((ch = getch()) != '\n' && ch != 27) {
if (ch == KEY_BACKSPACE || ch == 127) {
if (!statusText.empty()) statusText.pop_back();
} else if (ch >= 32 && ch <= 126) {
statusText += ch;
}
render();
}
if (ch == 27) { // Esc
statusMode = Status_Normal;
statusText.clear();
render();
return;
}
lastSearch.clear();
lastHexSearch.clear();
matchOff.clear();
std::istringstream iss(statusText);
std::string hexByte;
bool isHexSearch = true;
std::vector<uint8_t> hexSearch;
while (iss >> hexByte) {
if (hexByte.size() != 2 || !std::all_of(hexByte.begin(), hexByte.end(), [](char c) { return std::isxdigit(c); })) {
isHexSearch = false;
break;
}
try {
hexSearch.push_back(static_cast<uint8_t>(std::stoul(hexByte, nullptr, 16)));
} catch (...) {
isHexSearch = false;
break;
}
}
if (isHexSearch && !hexSearch.empty()) {
lastHexSearch = hexSearch;
size_t startPos = curPos > 0 ? curPos - 1 : buf.size() - lastHexSearch.size();
for (size_t i = startPos + 1; i > 0; --i) {
size_t pos = i - 1;
if (pos > buf.size() - lastHexSearch.size()) continue;
bool match = true;
for (size_t j = 0; j < lastHexSearch.size(); ++j) {
if (buf[pos + j] != lastHexSearch[j]) {
match = false;
break;
}
}
if (match) {
matchOff.push_back(pos);
curPos = pos;
dpOffset = curPos - (curPos % bpr);
break;
}
}
} else {
lastSearch = statusText;
size_t startPos = curPos > 0 ? curPos - 1 : buf.size() - lastSearch.size();
for (size_t i = startPos + 1; i > 0; --i) {
size_t pos = i - 1;
if (pos > buf.size() - lastSearch.size()) continue;
bool match = true;
for (size_t j = 0; j < lastSearch.size(); ++j) {
if (buf[pos + j] != static_cast<uint8_t>(lastSearch[j])) {
match = false;
break;
}
}
if (match) {
matchOff.push_back(pos);
curPos = pos;
dpOffset = curPos - (curPos % bpr);
break;
}
}
}
statusMode = Status_Normal;
statusText.clear();
render();
}
void HexEditor::handleReplace() {
statusMode = Status_Replace;
statusText.clear();
render();
std::string hexInput;
int ch;
while ((ch = getch()) != 27) {
if (std::isxdigit(ch) && hexInput.size() < 2) {
hexInput += ch;
statusText = "REPLACE: " + hexInput;
render();
}
if (hexInput.size() == 2) {
uint8_t byte = std::stoul(hexInput, nullptr, 16);
if (curPos < buf.size()) {
undoStack.push_back({curPos, buf[curPos], byte});
redoStack.clear();
buf[curPos] = byte;
modified = true;
curPos = std::min(curPos + 1, buf.size() - 1);
}
hexInput.clear();
statusText.clear();
render();
}
}
statusMode = Status_Normal;
statusText.clear();
render();
}
void HexEditor::undo() {
if (undoStack.empty()) return;
Edit edit = undoStack.back();
undoStack.pop_back();
redoStack.push_back({edit.offset, edit.newByte, edit.oldByte});
buf[edit.offset] = edit.oldByte;
modified = true;
curPos = edit.offset;
dpOffset = curPos - (curPos % bpr);
render();
}
void HexEditor::redo() {
if (redoStack.empty()) return;
Edit edit = redoStack.back();
redoStack.pop_back();
undoStack.push_back({edit.offset, edit.newByte, edit.oldByte});
buf[edit.offset] = edit.newByte;
modified = true;
curPos = edit.offset;
dpOffset = curPos - (curPos % bpr);
render();
}
void HexEditor::input() {
int ch;
while (running) {
if (statusMode != Status_Normal && statusMode != Status_Error) continue;
ch = getch();
if ((ch == 'j' || ch == KEY_DOWN) && curPos + bpr < buf.size()) {
curPos += bpr; // 下
if (statusMode == Status_Error) statusMode = Status_Normal;
} else if ((ch == 'k' || ch == KEY_UP) && curPos >= bpr) {
curPos -= bpr; // 上
if (statusMode == Status_Error) statusMode = Status_Normal;
} else if ((ch == 'l' || ch == KEY_RIGHT) && curPos + 1 < buf.size()) {
curPos += 1; // 右
if (statusMode == Status_Error) statusMode = Status_Normal;
} else if ((ch == 'h' || ch == KEY_LEFT) && curPos > 0) {
curPos -= 1; // 左
if (statusMode == Status_Error) statusMode = Status_Normal;
} else if (ch == 'i') {
if (curPos - 1 > 0) curPos--;
handleReplace();
} else if (ch == 'r') {
handleReplace();
} else if (ch == 'a') {
if (curPos + 1 < buf.size()) curPos++;
handleReplace();
} else if (ch == ':') {
handleCommand();
} else if (ch == '/') {
handleSearch();
} else if (ch == '?') {
handleReverseSearch();
} else if (ch == 'n') {
findNextMatch();
} else if (ch == 'N') {
findPrevMatch();
} else if (ch == 'u') {
undo();
} else if (ch == 'R') {
redo();
} else if (ch == KEY_HOME) {
curPos = 0; // ファイルの一番上
if (statusMode == Status_Error) statusMode = Status_Normal;
} else if (ch == 'g') {
if (getch() == 'g') {
curPos = 0; // ファイルの一番上
if (statusMode == Status_Error) statusMode = Status_Normal;
}
} else if (ch == 'G' || ch == KEY_END) {
curPos = buf.size() - 1; // ファイルの一番下
if (statusMode == Status_Error) statusMode = Status_Normal;
} else if (ch == 'Z') {
int next = getch();
if (next == 'Q') {
handleQuit(true);
break;
} else if (next == 'Z') {
handleSave();
handleQuit(true);
break;
} else if (next == 'S') {
handleSave();
if (statusMode == Status_Error) statusMode = Status_Normal;
}
}
// 画面の動き
getmaxyx(stdscr, rows, cols);
if (curPos < dpOffset) {
dpOffset = curPos - (curPos % bpr);
} else if (curPos >= dpOffset + (rows - 2) * bpr) {
dpOffset = curPos - ((rows - 3) * bpr);
}
render();
}
}
void HexEditor::run() {
render();
input();
}