Skip to content

Commit

Permalink
Apply suggestions from code review
Browse files Browse the repository at this point in the history
  • Loading branch information
Arusekk committed Apr 20, 2022
1 parent b94d802 commit de3162e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 9 deletions.
12 changes: 5 additions & 7 deletions pwnlib/util/fiddling.py
Expand Up @@ -1002,16 +1002,14 @@ def js_escape(data, padding=context.cyclic_alphabet[0:1], **kwargs):
"""
data = packing._need_bytes(data)

padding = packing._need_bytes(padding)
if len(padding) != 1:
raise ValueError("Padding must be a single byte")
padding = packing._need_bytes(padding)

if len(data) % 2:
data += padding[0:1]

if isinstance(data, six.string_types):
# Give Python 2 an iterable of ints, similar to how a Python 3 bytes works
data = map(ord, data)
data = bytearray(data)

if context.endian == 'little':
return ''.join('%u{a:02x}{b:02x}'.format(a=a, b=b) for b, a in iters.group(2, data))
Expand All @@ -1038,7 +1036,7 @@ def js_unescape(s, **kwargs):
b'\xde\xad\xbe\xef'
>>> js_unescape('abc%u4141123')
b'abcAA123'
b'a\x00b\x00c\x00AA1\x002\x003\x00'
>>> data = b'abcdABCD1234!@#$\x00\x01\x02\x03\x80\x81\x82\x83'
>>> js_unescape(js_escape(data)) == data
Expand All @@ -1060,7 +1058,7 @@ def js_unescape(s, **kwargs):
Traceback (most recent call last):
ValueError: Bad % token: %zz
"""
s = packing._need_text(s)
s = packing._decode(s)
res = []
p = 0
while p < len(s):
Expand All @@ -1082,7 +1080,7 @@ def js_unescape(s, **kwargs):
else:
raise ValueError('Bad %% token: %s' % s[p:p+3])
else:
res.append(packing._encode(s[p]))
res.append(packing.p16(ord(s[p])))
p += 1

return b''.join(res)
3 changes: 1 addition & 2 deletions pwnlib/util/packing.py
@@ -1,6 +1,6 @@
# -*- coding: utf-8 -*-
r"""
Module for packing and unpacking integers and data.
Module for packing and unpacking integers.
Simplifies access to the standard ``struct.pack`` and ``struct.unpack``
functions, and also adds support for packing/unpacking arbitrary-width
Expand Down Expand Up @@ -35,7 +35,6 @@

import collections
import six
import string
import struct
import sys
import warnings
Expand Down

0 comments on commit de3162e

Please sign in to comment.