Skip to content

Commit 0426d49

Browse files
committed
Resolve feature regression in hexdump(..., cyclic=True)
This code wasnt updated when the code was changed to operate on file descriptors. Additionally, there was a functional regression, that the first line would be skipped. Fixes: #836 Caused by: #695 (d65991d)
1 parent 54e7228 commit 0426d49

1 file changed

Lines changed: 32 additions & 10 deletions

File tree

pwnlib/util/fiddling.py

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,22 @@ def hexdump_iter(fd, width=16, skip=True, hexii=False, begin=0, style=None,
608608
spacer = ' '
609609
marker = (style.get('marker') or (lambda s:s))('│')
610610

611+
# Total length of the input stream
612+
total = 0
613+
614+
if hasattr(fd, 'len'):
615+
total = fd.len
616+
else:
617+
# Save the current file offset
618+
cur = fd.seek(0, os.SEEK_CUR)
619+
620+
# Determine the total size of the file
621+
fd.seek(0, os.SEEK_END)
622+
total = fd.tell()
623+
624+
# Restore the file offset
625+
fd.seek(cur, os.SEEK_SET)
626+
611627
if hexii:
612628
column_sep = ''
613629
line_fmt = '%%(offset)08x %%(hexbytes)-%is│' % (len(column_sep)+(width*byte_width))
@@ -628,7 +644,7 @@ def style_byte(b):
628644
cache = [style_byte(chr(b)) for b in range(256)]
629645

630646
if cyclic:
631-
update_cyclic_pregenerated(len(s))
647+
update_cyclic_pregenerated(total)
632648

633649
numb = 0
634650
while True:
@@ -637,19 +653,25 @@ def style_byte(b):
637653
if chunk == '':
638654
break
639655
numb += len(chunk)
656+
640657
# If this chunk is the same as the last unique chunk,
641658
# use a '*' instead.
642-
if skip and \
643-
(last_unique == chunk or \
644-
(cyclic and sequential_lines(last_unique, chunk))):
659+
if skip and last_unique:
660+
same_as_last_line = (last_unique == chunk)
661+
lines_are_sequential = (cyclic and sequential_lines(last_unique, chunk))
645662
last_unique = chunk
646-
if not skipping:
647-
yield '*'
648-
skipping = True
649-
continue
650663

651-
# Chunk is unique, save for next iteration
652-
last_unique = chunk
664+
if same_as_last_line or lines_are_sequential:
665+
666+
# If we have not already printed a "*", do so
667+
if not skipping:
668+
yield '*'
669+
skipping = True
670+
671+
# Move on to the next chunk
672+
continue
673+
674+
# Chunk is unique, no longer skipping
653675
skipping = False
654676

655677
# Generate contents for line

0 commit comments

Comments
 (0)