Skip to content

Commit b5a1ca8

Browse files
committed
Add proxy of filepointer; more robust overlap_structure
1 parent 958a7a9 commit b5a1ca8

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

pwnlib/filepointer.py

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# This is a fallback support for filepointer mudule
2+
from pwnlib.file.filepointer import *

pwnlib/util/packing.py

+13-14
Original file line numberDiff line numberDiff line change
@@ -1202,14 +1202,15 @@ def _decode(b):
12021202
del name, routine, mod
12031203

12041204
def overlap_structure(*structs):
1205-
"""overlap_structure(*structs: tuple[bytes_like]) -> bytes
1205+
r"""overlap_structure(*structs: tuple[bytes_like]) -> bytes
12061206
12071207
Unpacks bytes_like structures and overlap them up. Check the example
12081208
below for more detail.
12091209
12101210
Parameters:
12111211
structs: ``str``, ``bytes`` and ``list[int]`` are all acceptable,
12121212
as long as the argument is "len-able", iterable and serving ints.
1213+
Note ``str``s will be used as ``bytes`` object.
12131214
12141215
Raises:
12151216
ValueError: 2 non-zero values on the same index. Carry `msg` of
@@ -1222,32 +1223,30 @@ def overlap_structure(*structs):
12221223
12231224
Examples:
12241225
1225-
>>> x = bytes.fromhex('41 42 43 44')
1226-
>>> y = '\0\0\0\0\0\0\0\0\0xyz'
1227-
>>> z = [0, 0, 0, 0, 33, 34, 35, 36]
1226+
>>> x = p32(0x7ffffe30)
1227+
>>> y = [0, 0, 0, 0, 33, 34, 35, 36]
1228+
>>> overlap_structure(x, y)
1229+
b'0\xfe\xff\x7f!"#$'
1230+
>>> z = p32(0xbeef)
12281231
>>> overlap_structure(x, y, z)
1229-
<stdin>:1: BytesWarning: Text is not bytes; assuming ASCII, no guarantees. See https://docs.pwntools.com/#bytes
1230-
b'ABCD!"#$\x00xyz'
1231-
>>> w = p32(0xbeef)
1232-
>>> overlap_structure(w, x, y, z)
12331232
Traceback (most recent call last):
12341233
...
12351234
ValueError: Conflict values at index 0, 1
12361235
"""
12371236
if len(structs) == 1:
12381237
return structs[0]
12391238

1240-
maxlen = max(len(e) for e in structs)
1239+
# convert str to bytes first to calc accurate length
1240+
itr = [_need_bytes(s) if isinstance(s, str) else s for s in structs]
1241+
maxlen = max(len(e) for e in itr)
12411242
final = [0] * maxlen
1242-
errs = []
1243+
errs = set()
12431244

1244-
for s in structs:
1245-
if isinstance(s, str):
1246-
s = _need_bytes(s)
1245+
for s in itr:
12471246
for i, e in enumerate(s):
12481247
if e > 0:
12491248
if final[i]:
1250-
errs.append(i)
1249+
errs.add(i)
12511250
final[i] = e
12521251

12531252
if errs:

0 commit comments

Comments
 (0)