Files
dobin-avred/utils.py
T
2022-06-26 11:17:22 +02:00

47 lines
1.1 KiB
Python

import json
import random
from enum import Enum
import base64
def saveMatchesToFile(filename, matches):
# convert first
results = []
for match in matches:
result = {
"start": match.begin,
"end": match.end,
}
results.append(result)
with open(filename, 'w') as outfile:
json.dump(results, outfile)
class FillType(Enum):
null = 1
space = 2
highentropy = 3
lowentropy = 4
def patchData(data: bytes, base: int, size: int, fillType: FillType=FillType.null) -> bytes:
fill = None # has to be exactly <size> bytes
if fillType is FillType.null:
fill = b"\x00" * size
elif fillType is FillType.space:
fill = b" " * size
elif fillType is FillType.highentropy:
fill = random.randbytes(size) # 3.9..
#fill = os.getrandom(size)
elif fillType is FillType.lowentropy:
temp = random.randbytes(size) # 3.9..
#temp = os.getrandom(size)
temp = base64.b64encode(temp)
fill = temp[:size]
d = bytearray(data)
d[base:base+size] = fill
data = bytes(d)
return data