From 44e0d24f1bb610152eaf78965442112ff04156ec Mon Sep 17 00:00:00 2001 From: Gwilym Inzani Date: Thu, 6 Apr 2023 23:35:44 +0100 Subject: [PATCH] Pad the result --- agb-gbafix/src/main.rs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/agb-gbafix/src/main.rs b/agb-gbafix/src/main.rs index 0bade60f..b837dd92 100644 --- a/agb-gbafix/src/main.rs +++ b/agb-gbafix/src/main.rs @@ -42,8 +42,12 @@ fn write_gba_file(input: &[u8], output: &mut W) -> Result<()> { continue; } - for _ in address..section_header.sh_addr { - output.write_all(&[0])?; + if address < section_header.sh_addr { + for _ in address..section_header.sh_addr { + output.write_all(&[0])?; + } + + address = section_header.sh_addr; } let (mut data, compression) = elf_file.section_data(§ion_header)?; @@ -73,5 +77,15 @@ fn write_gba_file(input: &[u8], output: &mut W) -> Result<()> { address += data.len() as u64; } + let length = address - GBA_START_ADDRESS; + + if !length.is_power_of_two() { + let required_padding = length.next_power_of_two() - length; + + for _ in 0..required_padding { + output.write_all(&[0])?; + } + } + Ok(()) }