Skip to content

Commit

Permalink
Add method to resize section payload
Browse files Browse the repository at this point in the history
  • Loading branch information
Zedeldi committed Dec 3, 2024
1 parent 1775f9b commit 7d90bd7
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions igelfs/models/section.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,23 @@ def split_into_sections(data: bytes, pad: bool = False) -> list[bytes]:
sections[-1] = sections[-1].ljust(IGF_SECT_DATA_LEN, b"\x00")
return sections

def resize(self) -> None:
"""
Resize payload of Section instance to correct size and update CRC.
This will append null bytes or destructively truncate the payload.
"""
payload_size = IGF_SECT_DATA_LEN
if self.partition:
payload_size -= self.partition.get_actual_size()
if self.hash:
payload_size -= self.hash.get_actual_size()
if (difference := len(self.data) - payload_size) < 0:
self.data += bytes(abs(difference))
else:
self.data = self.data[:payload_size]
self.update_crc()

def zero(self) -> None:
"""Set payload of Section instance to null bytes and update CRC."""
self.data = b"\x00" * len(self.data)
Expand Down

0 comments on commit 7d90bd7

Please sign in to comment.