From d29b5d3bc6e27277a79dcb7fd7f2617dccade0c7 Mon Sep 17 00:00:00 2001 From: Leonard Lesinski <84378319+Le0X8@users.noreply.github.com> Date: Mon, 25 Nov 2024 15:38:19 +0100 Subject: [PATCH 1/2] fix: Fixed padding length calculation --- src/msbt/create.rs | 6 +++--- src/msbt/hashtables.rs | 13 +++++++++++-- tests/msbt.rs | 12 ++++++++++++ tests/samples/AN_3P_Fu.msbt | Bin 0 -> 16464 bytes 4 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 tests/samples/AN_3P_Fu.msbt diff --git a/src/msbt/create.rs b/src/msbt/create.rs index 0b1fb0b..0f9a54c 100644 --- a/src/msbt/create.rs +++ b/src/msbt/create.rs @@ -1,4 +1,4 @@ -use super::MsbtFileSource; +use super::{hashtables::padding_len, MsbtFileSource}; use dh::{recommended::*, Readable, Writable}; use std::io::Result; @@ -58,7 +58,7 @@ pub fn create<'a>( let pos = target.pos()?; let labels_size = pos - table_pos; target.write_u32le_at(labels_size_pos, labels_size as u32)?; - target.write_bytes(&vec![0xab; 16 - pos as usize % 16])?; + target.write_bytes(&vec![0xab; padding_len(pos) as usize])?; // attributes section if has_attr_section { @@ -137,6 +137,6 @@ pub fn create<'a>( let pos = target.pos()?; let text_size = pos - table_pos; target.write_u32le_at(text_size_pos, text_size as u32)?; - target.write_bytes(&vec![0xab; 16 - pos as usize % 16])?; + target.write_bytes(&vec![0xab; padding_len(pos) as usize])?; Ok(()) } diff --git a/src/msbt/hashtables.rs b/src/msbt/hashtables.rs index 6d971f6..01ddcf8 100644 --- a/src/msbt/hashtables.rs +++ b/src/msbt/hashtables.rs @@ -1,6 +1,15 @@ use dh::{recommended::*, Readable}; use std::{collections::HashMap, io::Result}; +pub fn padding_len(pos: u64) -> u64 { + let res = pos % 16; + if res == 0 { + 0 + } else { + 16 - res + } +} + // https://zeldamods.org/wiki/Msbt#Labels_Section pub fn parse_label_block( reader: &mut dyn Readable, @@ -33,7 +42,7 @@ pub fn parse_label_block( reader.to(table_start + size as u64)?; let pos = reader.pos()?; - reader.jump(16 - pos as i64 % 16)?; + reader.jump(padding_len(pos) as i64)?; Ok(labels) } @@ -45,7 +54,7 @@ pub fn parse_attr_block(reader: &mut dyn Readable, big_endian: bool) -> Result<( let start = reader.pos()?; reader.jump(size as i64)?; let pos = reader.pos()?; - reader.jump(16 - pos as i64 % 16)?; + reader.jump(padding_len(pos) as i64)?; Ok((start, size as u64)) } diff --git a/tests/msbt.rs b/tests/msbt.rs index 819ddca..34d2b06 100644 --- a/tests/msbt.rs +++ b/tests/msbt.rs @@ -37,6 +37,18 @@ fn msbt_secretary() { assert!(string.starts_with("Sure thing!")); } +#[test] +fn msbt_an_3p_fu() { + let mut reader = dh::file::open_r("tests/samples/AN_3P_Fu.msbt").unwrap(); + let metadata = msbt::metadata(&mut reader).unwrap(); + assert_eq!(metadata.files.len(), 56); + + let mut target = dh::data::write_empty(); + msbt::extract(&mut reader, &mut target, &metadata.files[31]).unwrap(); + let string = String::from_utf8(dh::data::close(target)).unwrap(); + assert!(string.starts_with("<{0003,0016,}>Oh!<{0007,0000,14000000}> That might work!")); +} + #[test] fn custom_msbt() { let mut file0 = dh::data::read_ref(b"Hello, world!"); diff --git a/tests/samples/AN_3P_Fu.msbt b/tests/samples/AN_3P_Fu.msbt new file mode 100644 index 0000000000000000000000000000000000000000..a0b57f0c551070475a54465b57ba4b3d41ebe29a GIT binary patch literal 16464 zcmc(mOKe@$6^4(ofpAG(ptPkarPl?Tv?Q*dc}Pf*G(4h)5+$ag5)&tWK^!~vBW@}N z9WX?ws!)Y0ggRh=3PT1RFzA3mRl$r=#Sm4fV%Px#MEigH-^=@)>+9g$r0R{X*SY7O zyZ72_{p(+AuXFO$>in6tnPW@;`Le3|`unS@I`ydjY`#t&J30EvH+4rKqx*vL0@t>oz4mSf+v9RHT(#J4Rc@3B1i9m_*|EDwLz^2ohO zPK@le9NlL*_9M&jA6rg5WI4It^56l>L&KJbe`0xLB+1c{+bl<0mSeYDj_@OoL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Nf_I0-XbGIu|DS@bdqQh+ uEf+iQvaYaP-73zQ;2)-|*GVv^>nr%PtjcJxPNf6hxmJNB{cqW~-~KNcTYBRF literal 0 HcmV?d00001 From 0fcbd86bba92945543a263be67ff075c8237009f Mon Sep 17 00:00:00 2001 From: Leonard Lesinski <84378319+Le0X8@users.noreply.github.com> Date: Mon, 25 Nov 2024 15:38:43 +0100 Subject: [PATCH 2/2] chore: Updated version --- Cargo.lock | 2 +- Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 26affba..23105ed 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,7 +4,7 @@ version = 3 [[package]] name = "acridotheres-3ds" -version = "0.2.0" +version = "0.2.1" dependencies = [ "dh", ] diff --git a/Cargo.toml b/Cargo.toml index c1320d2..c1aaeb8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "acridotheres-3ds" -version = "0.2.0" +version = "0.2.1" edition = "2021" description = "Nintendo 3DS-specific file formats for Acridotheres" license = "MIT"