Skip to content

Commit

Permalink
packing.fit() should handle extremely large offsets as if they are cy…
Browse files Browse the repository at this point in the history
…clic patterns
  • Loading branch information
zachriggle committed Jan 7, 2017
1 parent 0c8006c commit d0267f3
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions pwnlib/util/packing.py
Original file line number Diff line number Diff line change
Expand Up @@ -595,6 +595,8 @@ def fit(pieces=None, **kwargs):
>>> fit({ 8: [0x41414141, 0x42424242],
... 20: 'CCCC'})
'aaaabaaaAAAABBBBeaaaCCCC'
>>> fit({ 0x61616162: 'X'})
'aaaaX'
"""
# HACK: To avoid circular imports we need to delay the import of `cyclic`
Expand All @@ -620,15 +622,21 @@ def fit(pieces=None, **kwargs):
if not pieces:
return ''.join(filler.next() for f in range(length))

def fill(out, value):
while value not in out:
out += filler.next()
return out, out.index(value)

# convert str keys to offsets
# convert large int keys to offsets
pieces_ = dict()
for k, v in pieces.items():
if isinstance(k, (int, long)):
pass
# cyclic() generally starts with 'aaaa'
if k >= 0x61616161:
out, k = fill(out, pack(k))
elif isinstance(k, str):
while k not in out:
out += filler.next()
k = out.index(k)
out, k = fill(out, k)
else:
raise TypeError("fit(): offset must be of type int or str, but got '%s'" % type(k))
pieces_[k] = v
Expand Down

0 comments on commit d0267f3

Please sign in to comment.