Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tubes with context.encoding #1317

Merged
merged 10 commits into from
Jun 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions pwnlib/context/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,6 +349,7 @@ class ContextType(object):
'cyclic_size': 4,
'delete_corefiles': False,
'device': os.getenv('ANDROID_SERIAL', None) or None,
'encoding': 'auto',
'endian': 'little',
'gdbinit': "",
'kernel': None,
Expand Down Expand Up @@ -812,6 +813,36 @@ def bytes(self):
def bytes(self, value):
self.bits = value*8

@_validator
def encoding(self, charset):
if charset == 'auto':
return charset

if ( b'aA'.decode(charset) != 'aA'
or 'aA'.encode(charset) != b'aA'):
raise ValueError('Strange encoding!')

return charset

def _encode(self, s):
if isinstance(s, (bytes, bytearray)):
return s # already bytes

if self.encoding == 'auto':
try:
return s.encode('latin1')
except UnicodeEncodeError:
return s.encode('utf-8', 'surrogateescape')
return s.encode(self.encoding)

def _decode(self, b):
if self.encoding == 'auto':
try:
return b.decode('utf-8')
except UnicodeDecodeError:
return b.decode('latin1')
return b.decode(self.encoding)

@_validator
def endian(self, endianness):
"""
Expand Down
Loading