Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite elf serialization from xor to BytesIO #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions makeelf/elfstruct.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from makeelf.type.uint16 import uint16
from makeelf.type.uint32 import uint32
import makeelf.utils
from io import BytesIO

## \class ELFCLASS
# \brief File class
Expand Down Expand Up @@ -884,8 +885,17 @@ def __bytes__(self):
end_of_file = sorted(headers.keys())[-1]
end_of_file += len(headers[end_of_file])

b = BytesIO(bytes(end_of_file))

regions = list()

# check if two half segments [x1, x2) and [y1; y2) intersect
def segments_intersect(x, y):
x1, x2 = x
y1, y2 = y
return x2 > y1 and y2 > x1

# create and populate buffer
b = bytes(end_of_file)
for off in headers:
# TODO: there's something wrong, when hdr is not bytes, but only
# simulates it
Expand All @@ -899,12 +909,20 @@ def __bytes__(self):
hdr = bytes(hdr)
size = len(hdr)

region = (off, off + size)
if any(segments_intersect(x, region) for x in regions):
raise Exception('Attempt to write at intersecting parts of a file')
regions.append(region)

b.seek(off)
b.write(hdr)

# expand to file size
aligned = align(bytes(off) + hdr, end_of_file)
#aligned = align(bytes(off) + hdr, end_of_file)

# xor into b
b = makeelf.utils.bytes_xor(b, aligned)
return b
#b = makeelf.utils.bytes_xor(b, aligned)
return b.getvalue()

##
# \brief Deserialization of object
Expand Down