Skip to content

Commit

Permalink
Fix some unicode problems in readline (#1029)
Browse files Browse the repository at this point in the history
Technically this introduces issues with e.g. 'unhex ffdf | python foo.py' if
foo.py uses .interactive().  However, that was already broken for other reasons.

Fixes #1004
  • Loading branch information
zachriggle authored Sep 17, 2017
1 parent b62804e commit ebbe917
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions pwnlib/term/readline.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from __future__ import absolute_import

from pwnlib.term import keyconsts as kc
Expand All @@ -7,7 +9,7 @@

cursor = text.reverse

buffer_left, buffer_right = u'', u''
buffer_left, buffer_right = '', ''
saved_buffer = None
history = []
history_idx = None
Expand All @@ -29,6 +31,14 @@

tabs = 0

def force_to_bytes(data):
if isinstance(data, bytes):
return data
try:
return data.encode('utf-8')
except Exception:
return data.encode('latin-1')

def set_completer(completer):
global complete_hook, suggest_hook
if completer is None:
Expand Down Expand Up @@ -80,7 +90,7 @@ def handle_keypress(trace):

def clear():
global buffer_left, buffer_right, history_idx, search_idx
buffer_left, buffer_right = u'', u''
buffer_left, buffer_right = '', ''
history_idx = None
search_idx = None
redisplay()
Expand Down Expand Up @@ -141,7 +151,7 @@ def cancel_search(*_):
def commit_search():
global search_idx
if search_idx is not None and search_results:
set_buffer(history[search_results[search_idx][0]], u'')
set_buffer(history[search_results[search_idx][0]], '')
search_idx = None
redisplay()

Expand Down Expand Up @@ -169,7 +179,7 @@ def update_search_results():
def search_history(*_):
global buffer_left, buffer_right, history_idx, search_idx
if search_idx is None:
buffer_left, buffer_right = buffer_left + buffer_right, u''
buffer_left, buffer_right = buffer_left + buffer_right, ''
history_idx = None
search_idx = 0
update_search_results()
Expand All @@ -187,7 +197,7 @@ def history_prev(*_):
history_idx = -1
if history_idx < len(history) - 1:
history_idx += 1
set_buffer(history[history_idx], u'')
set_buffer(history[history_idx], '')

def history_next(*_):
global history_idx, saved_buffer
Expand All @@ -200,7 +210,7 @@ def history_next(*_):
saved_buffer = None
else:
history_idx -= 1
set_buffer(history[history_idx], u'')
set_buffer(history[history_idx], '')

def backward_char(*_):
global buffer_left, buffer_right
Expand Down Expand Up @@ -319,11 +329,11 @@ def forward_word(*_):

def go_beginning(*_):
commit_search()
set_buffer(u'', buffer_left + buffer_right)
set_buffer('', buffer_left + buffer_right)

def go_end(*_):
commit_search()
set_buffer(buffer_left + buffer_right, u'')
set_buffer(buffer_left + buffer_right, '')

keymap = km.Keymap({
'<nomatch>' : self_insert,
Expand Down Expand Up @@ -382,10 +392,10 @@ def readline(_size = None, prompt = '', float = True, priority = 10):
if eof:
return ''
else:
buffer = (buffer_left + buffer_right).encode('utf-8')
buffer = (buffer_left + buffer_right)
if buffer:
history.insert(0, buffer)
return buffer + '\n'
return force_to_bytes(buffer) + '\n'
except KeyboardInterrupt:
control_c()
finally:
Expand Down

0 comments on commit ebbe917

Please sign in to comment.