@@ -1202,14 +1202,15 @@ def _decode(b):
1202
1202
del name , routine , mod
1203
1203
1204
1204
def overlap_structure (* structs ):
1205
- """overlap_structure(*structs: tuple[bytes_like]) -> bytes
1205
+ r """overlap_structure(*structs: tuple[bytes_like]) -> bytes
1206
1206
1207
1207
Unpacks bytes_like structures and overlap them up. Check the example
1208
1208
below for more detail.
1209
1209
1210
1210
Parameters:
1211
1211
structs: ``str``, ``bytes`` and ``list[int]`` are all acceptable,
1212
1212
as long as the argument is "len-able", iterable and serving ints.
1213
+ Note ``str``s will be used as ``bytes`` object.
1213
1214
1214
1215
Raises:
1215
1216
ValueError: 2 non-zero values on the same index. Carry `msg` of
@@ -1222,32 +1223,30 @@ def overlap_structure(*structs):
1222
1223
1223
1224
Examples:
1224
1225
1225
- >>> x = bytes.fromhex('41 42 43 44')
1226
- >>> y = '\0 \0 \0 \0 \0 \0 \0 \0 \0 xyz'
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)
1228
1231
>>> 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!"#$\x00 xyz'
1231
- >>> w = p32(0xbeef)
1232
- >>> overlap_structure(w, x, y, z)
1233
1232
Traceback (most recent call last):
1234
1233
...
1235
1234
ValueError: Conflict values at index 0, 1
1236
1235
"""
1237
1236
if len (structs ) == 1 :
1238
1237
return structs [0 ]
1239
1238
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 )
1241
1242
final = [0 ] * maxlen
1242
- errs = []
1243
+ errs = set ()
1243
1244
1244
- for s in structs :
1245
- if isinstance (s , str ):
1246
- s = _need_bytes (s )
1245
+ for s in itr :
1247
1246
for i , e in enumerate (s ):
1248
1247
if e > 0 :
1249
1248
if final [i ]:
1250
- errs .append (i )
1249
+ errs .add (i )
1251
1250
final [i ] = e
1252
1251
1253
1252
if errs :
0 commit comments