Skip to content

Commit

Permalink
fix hex editor find bug
Browse files Browse the repository at this point in the history
SilenZcience committed Jul 30, 2024
1 parent fd185c4 commit b8a3165
Showing 2 changed files with 36 additions and 7 deletions.
31 changes: 24 additions & 7 deletions cat_win/src/service/hexeditor.py
Original file line number Diff line number Diff line change
@@ -128,6 +128,26 @@ def _setup_file(self) -> None:
if self.debug_mode:
err_print(self.error_bar)

def _get_current_state_row(self, row: int) -> list:
"""
get the current state of the hex row
Parameters:
row (int):
the row to get
Returns:
hex_row (list):
the row in the current edited state
"""
hex_row = []
for j, byte in enumerate(self.hex_array[row]):
hex_byte = self.hex_array_edit[row][j]
if hex_byte is None:
hex_byte = byte
hex_row.append(hex_byte)
return hex_row

def getxymax(self) -> tuple:
"""
find the size of the window.
@@ -394,11 +414,8 @@ def _action_save(self) -> bool:
bytes_loaded-= HexEditor.columns
bytes_loaded+= len(self.hex_array[-1])
content = b''
for i, row in enumerate(self.hex_array):
for j, byte in enumerate(row):
hex_byte = self.hex_array_edit[i][j]
if hex_byte is None:
hex_byte = byte
for i in range(len(self.hex_array)):
for hex_byte in self._get_current_state_row(i):
try:
content += bytes.fromhex(hex_byte)
except ValueError:
@@ -464,11 +481,11 @@ def _action_find(self) -> bool:
indicates if the editor should keep running
"""
def find_bytes(row: int, col: int = 0) -> int:
search_in = ''.join(self.hex_array[row][col:])
search_in = ''.join(self._get_current_state_row(row)[col:])
search_wrap = ''
while len(self.search)-1 > len(search_wrap) and row < len(self.hex_array)-1:
row += 1
search_wrap += ''.join(self.hex_array[row])
search_wrap += ''.join(self._get_current_state_row(row))
search_in += search_wrap[:len(self.search)-1]

for i in range(0, len(search_in), 2):
12 changes: 12 additions & 0 deletions cat_win/tests/src/service/test_hexeditor.py
Original file line number Diff line number Diff line change
@@ -88,6 +88,18 @@ def test__key_dc_empty(self):
editor._key_dc(None)
self.assertEqual(editor.cpos.get_pos(), (0, 0))

@patch('cat_win.src.service.helper.iohelper.IoHelper.yield_file', IoHelperMock.yield_file_gen(b'@' * 20))
def test__get_current_state_row(self):
editor = HexEditor('', '')
self.assertListEqual(editor._get_current_state_row(0), ['40'] * 16)
self.assertListEqual(editor._get_current_state_row(1), ['40'] * 4)
editor.hex_array_edit[1][3] = '--'
self.assertListEqual(editor._get_current_state_row(0), ['40'] * 16)
self.assertListEqual(editor._get_current_state_row(1), ['40'] * 3 + ['--'])
editor.hex_array_edit[0][0] = '21'
self.assertListEqual(editor._get_current_state_row(0), ['21'] + ['40'] * 15)
self.assertListEqual(editor._get_current_state_row(1), ['40'] * 3 + ['--'])

def test__key_dc(self):
editor = HexEditor(__file__, '')
hex_array = deepcopy(editor.hex_array)

0 comments on commit b8a3165

Please sign in to comment.