Skip to content

Commit 602bf72

Browse files
parser refactor + bytes input fix
1 parent 006c4a2 commit 602bf72

File tree

2 files changed

+27
-15
lines changed

2 files changed

+27
-15
lines changed

gui/custom_widgets/main_context_widget.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,7 @@ def submit_input(self):
205205
user_line = self.input_widget.text()
206206
logger.debug("Sending input '%s' to inferior", user_line)
207207
user_input = b""
208+
user_echo = b""
208209
# Check if the user wants to input a byte string literal, i.e. the input is in the form: 'b"MyInput \x12\x34"'
209210
if re.match(r'^b["\'].*["\']$', user_line):
210211
# Parse the str as if it were a bytes object
@@ -214,11 +215,16 @@ def submit_input(self):
214215
logger.debug("Parsed input as bytes string, final input: %s", byte_string)
215216
# Don't pass a newline here, the user needs to specify this himself by writing '\n' at the end of his input
216217
user_input = byte_string
218+
# We want the user to get the evaluated string echoed back
219+
# However without leading b' and ending ' to avoid confusion with regular string insert
220+
user_echo = repr(byte_string)[2:][:-1].encode()
217221
else:
222+
user_echo = user_line.encode() + b"\n"
218223
user_input = user_line.encode() + b"\n"
219224
if self.inferior_attached:
220225
self.gdb_write_input.emit(user_input)
221226
else:
227+
self.update_gui.emit("main", user_echo)
222228
self.inferior_write.emit(user_input)
223229
self.input_widget.clear()
224230

gui/parser.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -79,28 +79,28 @@ def parse_ascii_control(self, token: bytes):
7979
# Colors
8080
if start == b"30m":
8181
self.parser.setTextColor(Qt.GlobalColor.black)
82-
self.parser.insertPlainText(token.replace(start, b"", 1).decode())
82+
self.insert_token(token.replace(start, b"", 1))
8383
elif start == b"31m":
8484
self.parser.setTextColor(PwndbgGuiConstants.RED)
85-
self.parser.insertPlainText(token.replace(start, b"", 1).decode())
85+
self.insert_token(token.replace(start, b"", 1))
8686
elif start == b"32m":
8787
self.parser.setTextColor(PwndbgGuiConstants.GREEN)
88-
self.parser.insertPlainText(token.replace(start, b"", 1).decode())
88+
self.insert_token(token.replace(start, b"", 1))
8989
elif start == b"33m":
9090
self.parser.setTextColor(PwndbgGuiConstants.YELLOW)
91-
self.parser.insertPlainText(token.replace(start, b"", 1).decode())
91+
self.insert_token(token.replace(start, b"", 1))
9292
elif start == b"34m":
9393
self.parser.setTextColor(PwndbgGuiConstants.LIGHT_BLUE)
94-
self.parser.insertPlainText(token.replace(start, b"", 1).decode())
94+
self.insert_token(token.replace(start, b"", 1))
9595
elif start == b"35m":
9696
self.parser.setTextColor(PwndbgGuiConstants.PURPLE)
97-
self.parser.insertPlainText(token.replace(start, b"", 1).decode())
97+
self.insert_token(token.replace(start, b"", 1))
9898
elif start == b"36m":
9999
self.parser.setTextColor(PwndbgGuiConstants.CYAN)
100-
self.parser.insertPlainText(token.replace(start, b"", 1).decode())
100+
self.insert_token(token.replace(start, b"", 1))
101101
elif start == b"37m":
102102
self.parser.setTextColor(Qt.GlobalColor.white)
103-
self.parser.insertPlainText(token.replace(start, b"", 1).decode())
103+
self.insert_token(token.replace(start, b"", 1))
104104
elif start.startswith(b"38"):
105105
# 256-bit color format: 38;5;<FG COLOR>m
106106
# RGB format: 38;2;<r>;<g>;<b>m
@@ -116,26 +116,32 @@ def parse_ascii_control(self, token: bytes):
116116
self.parser.setTextColor(QColor.fromRgb(r, g, b, 255))
117117
# Add the "m" into the start for stripping
118118
start = token[:token.index(b"m") + 1]
119-
self.parser.insertPlainText(token.replace(start, b"", 1).decode())
119+
self.insert_token(token.replace(start, b"", 1))
120120
elif start == b"39m":
121121
self.parser.setTextColor(Qt.GlobalColor.white)
122-
self.parser.insertPlainText(token.replace(start, b"").decode())
122+
self.insert_token(token.replace(start, b""))
123123
elif start == b"91m":
124124
# Bright red
125125
self.parser.setTextColor(Qt.GlobalColor.red)
126-
self.parser.insertPlainText(token.replace(start, b"").decode())
126+
self.insert_token(token.replace(start, b""))
127127
# Font
128128
elif start.startswith(b"0m"):
129129
self.reset_font()
130-
self.parser.insertPlainText(token[2:].decode())
130+
self.insert_token(token[2:])
131131
elif start.startswith(b"1m"):
132132
self.parser.setFontWeight(QFont.Weight.Bold)
133-
self.parser.insertPlainText(token[2:].decode())
133+
self.insert_token(token[2:])
134134
elif start.startswith(b"3m"):
135135
self.parser.setFontItalic(not self.parser.fontItalic())
136-
self.parser.insertPlainText(token[2:].decode())
136+
self.insert_token(token[2:])
137137
elif start.startswith(b"4m"):
138138
self.parser.setFontUnderline(not self.parser.fontUnderline())
139-
self.parser.insertPlainText(token[2:].decode())
139+
self.insert_token(token[2:])
140140
else:
141+
self.insert_token(token)
142+
143+
def insert_token(self, token: bytes):
144+
try:
141145
self.parser.insertPlainText(token.decode())
146+
except UnicodeDecodeError:
147+
self.parser.insertPlainText(repr(token))

0 commit comments

Comments
 (0)