| 1 | /************************************************************************************** |
| 2 | # 076 License |
| 3 | |
| 4 | Copyright (c) 2026 テクニカル諏訪子 |
| 5 | |
| 6 | Permission is hereby granted to any person obtaining a copy of the software |
| 7 | Hexagon (the "Software") to use, modify, merge, copy, publish, distribute, |
| 8 | sublicense, and/or sell copies of the Software, subject to the following conditions: |
| 9 | |
| 10 | 1. **Origin Attribution**: |
| 11 | - You must not misrepresent the origin of the Software; you must not claim |
| 12 | you created the original Software. |
| 13 | - If the Software is used in a product, you must either: |
| 14 | a. Provide clear attribution in the product's documentation, user interface, |
| 15 | or other visible areas, **OR** |
| 16 | b. Pay the original developers a fee they specify in writing. |
| 17 | 2. **Usage Restriction**: |
| 18 | - The Software, or any derivative works, dependencies, or libraries |
| 19 | incorporating it, must not be used for censorship or to suppress freedom of |
| 20 | speech, expression, or creativity. Prohibited uses include, but are not |
| 21 | limited to: |
| 22 | - Censorship of so-called "hate speech", visuals, non-mainstream opinions, |
| 23 | ideas, or objective reality. |
| 24 | - Tools or systems designed to restrict access to information or |
| 25 | artistic works. |
| 26 | 3. **Notice Preservation**: |
| 27 | - This license and the above copyright notice must remain intact in all copies |
| 28 | of the source code. |
| 29 | |
| 30 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 31 | INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A |
| 32 | PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 33 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF |
| 34 | CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE |
| 35 | OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 36 | **************************************************************************************/ |
| 37 | #pragma once |
| 38 | |
| 39 | #ifdef _WIN32 |
| 40 | #include <pdcurses.h> |
| 41 | #else |
| 42 | #include <ncurses.h> |
| 43 | #endif |
| 44 | |
| 45 | #include <string> |
| 46 | #include <vector> |
| 47 | |
| 48 | class HexEditor { |
| 49 | private: |
| 50 | enum StatusMode : uint8_t { |
| 51 | Status_Normal, |
| 52 | Status_Command, |
| 53 | Status_Search, |
| 54 | Status_Replace, |
| 55 | Status_Error, |
| 56 | }; |
| 57 | |
| 58 | enum SearchDirection : uint8_t { |
| 59 | Direction_Forward, |
| 60 | Direction_Reverse, |
| 61 | }; |
| 62 | |
| 63 | struct Edit { |
| 64 | size_t offset; |
| 65 | uint8_t oldByte; |
| 66 | uint8_t newByte; |
| 67 | }; |
| 68 | |
| 69 | struct FileSignature { |
| 70 | std::vector<uint8_t> signature; |
| 71 | std::string type; |
| 72 | }; |
| 73 | |
| 74 | size_t curPos; |
| 75 | size_t dpOffset; |
| 76 | size_t bpr; |
| 77 | StatusMode statusMode; |
| 78 | bool modified; |
| 79 | bool running; |
| 80 | std::string lastSearch; |
| 81 | bool isMouse = false; |
| 82 | bool hasFile = false; |
| 83 | |
| 84 | WINDOW *hexPanel; |
| 85 | WINDOW *asciiPanel; |
| 86 | std::vector<uint8_t> buf; |
| 87 | std::string fname; |
| 88 | int rows, cols; |
| 89 | std::string statusText; |
| 90 | std::vector<uint8_t> lastHexSearch; |
| 91 | SearchDirection lastSearchDir; |
| 92 | std::vector<size_t> matchOff; |
| 93 | std::vector<Edit> undoStack; |
| 94 | std::vector<Edit> redoStack; |
| 95 | |
| 96 | size_t headerLen; |
| 97 | std::string headerType; |
| 98 | static const std::vector<FileSignature> signatures; |
| 99 | |
| 100 | void render(); |
| 101 | void input(); |
| 102 | void highlightcol(size_t i, size_t row, uint8_t byte); |
| 103 | void topbar(); |
| 104 | void statusbar(); |
| 105 | void handleCommand(); |
| 106 | void handleOpen(const std::string &path); |
| 107 | void handleSave(); |
| 108 | void handleQuit(bool force); |
| 109 | void handleSearch(); |
| 110 | void handleReverseSearch(); |
| 111 | void findNextMatch(); |
| 112 | void findPrevMatch(); |
| 113 | void handleReplace(); |
| 114 | void handleMouse(); |
| 115 | void undo(); |
| 116 | void redo(); |
| 117 | |
| 118 | public: |
| 119 | HexEditor(const std::string &filename); |
| 120 | ~HexEditor(); |
| 121 | |
| 122 | void run(); |
| 123 | }; |