From 144d7b840f09b1be17ef1be7870a4053e90ffb23 Mon Sep 17 00:00:00 2001 From: cqb13 Date: Mon, 15 Jul 2024 18:15:23 -0400 Subject: [PATCH] fixed checksum --- src/calculator/create/from_8xp.rs | 5 ++--- src/calculator/create/from_txt.rs | 13 ++++++------- src/tests/programs/BASECONV.8xp | Bin 200 -> 200 bytes src/tests/programs/DBD.8xp | Bin 395 -> 395 bytes src/tests/programs/FACTOR.8xp | Bin 227 -> 227 bytes src/tests/programs/MATHTOOL.8xp | Bin 10225 -> 10225 bytes src/tests/programs/POLCONIC.8xp | Bin 352 -> 352 bytes src/tests/programs/QUEUESIM.8xp | Bin 402 -> 402 bytes src/tests/programs/RADICAL.8xp | Bin 694 -> 694 bytes src/tests/programs/SPLINE.8xp | Bin 1168 -> 1168 bytes src/tests/programs/TOCCATA.8xp | Bin 190 -> 190 bytes 11 files changed, 8 insertions(+), 10 deletions(-) diff --git a/src/calculator/create/from_8xp.rs b/src/calculator/create/from_8xp.rs index 1534b9d..4ace0d5 100644 --- a/src/calculator/create/from_8xp.rs +++ b/src/calculator/create/from_8xp.rs @@ -20,7 +20,7 @@ pub fn create_from_8xp( ProgramFileType::XPThree | ProgramFileType::XPTwo => bytes.split_at(17), ProgramFileType::TXT => return Err("TXT files cant be loaded as 8xp files".to_string()), }; - let (body_bytes, _) = bytes.split_at(bytes.len() - 2); + let (body_bytes, checksum_bytes) = bytes.split_at(bytes.len() - 2); // header translation let signature = header_bytes[0..8] @@ -131,8 +131,7 @@ pub fn create_from_8xp( let body = Body::new(body_bytes.to_vec(), translation); // checksum translation - let checksum_bytes = (body_bytes.len() as u16).to_le_bytes(); - let checksum_value = u16::from_le_bytes(checksum_bytes); + let checksum_value = u16::from_le_bytes([checksum_bytes[0], checksum_bytes[1]]); let checksum = Checksum::new(checksum_bytes.to_vec(), checksum_value); diff --git a/src/calculator/create/from_txt.rs b/src/calculator/create/from_txt.rs index bf601e5..ddb6191 100644 --- a/src/calculator/create/from_txt.rs +++ b/src/calculator/create/from_txt.rs @@ -100,6 +100,11 @@ pub fn create_from_txt( let body_length = body_bytes.len() as u16; metadata_bytes.extend(body_length.to_le_bytes()); + let checksum: u32 = metadata_bytes.iter().map(|&byte| byte as u32).sum::() + + body_bytes.iter().map(|&byte| byte as u32).sum::(); + let checksum = checksum.to_le_bytes()[0..2].to_vec(); + let checksum = [checksum[0], checksum[1]]; + let metadata = Metadata::new( metadata_bytes, 0x0D, @@ -113,13 +118,7 @@ pub fn create_from_txt( body_length, ); - let checksum_bytes = (body_bytes.len() as u16).to_le_bytes(); - - if checksum_bytes.len() != 2 { - return Err("checksum length is not 2".to_string()); - } - - let checksum = Checksum::new(checksum_bytes.to_vec(), body_bytes.len() as u16); + let checksum = Checksum::new(checksum.to_vec(), u16::from_le_bytes(checksum)); let body = Body::new(body_bytes, body_string); diff --git a/src/tests/programs/BASECONV.8xp b/src/tests/programs/BASECONV.8xp index 4a896af34bb74eddbd1fda6a75ce1003f3605ace..cd4fc1e775b6a8142de0ebe9a856d791fe6f2778 100644 GIT binary patch delta 47 zcmX@Xc!E(~ODn|F!q`nqii^RyC^fMpHANw*QX#}sHzYqlr&z&2&rr{dfq`M7`a%FW C3k<{n delta 47 zcmX@Xc!E(~ODn|F!q`nqii;tjC_lX@F;^iiGbdHSz*yhdK;O_nN5Rm{%EWA<`a%FQ CU<{W4 diff --git a/src/tests/programs/DBD.8xp b/src/tests/programs/DBD.8xp index 47c91409cc9de318b536d553e6d76c3603edc306..665605a5e4032e11132f7d8540f2f9ce687126de 100644 GIT binary patch delta 48 zcmeBX?q*ik(hBjkFm}_D;$mmDB1>f-wL#eg&Zb delta 15 Wcmez9|IvSfGNbWEmDB1>OVt54Sq1$7 diff --git a/src/tests/programs/POLCONIC.8xp b/src/tests/programs/POLCONIC.8xp index 6dfc4546e67d6575668ffc679ae5fc646a67d6b2..4828ea5709660f43e14671f286d6f525bc4c3384 100644 GIT binary patch delta 48 zcmaFB^ngiSODn|F!q`nqii^RyC^fMpHANw*QX#}sHzYqlr&z&2&rr{dfq`M8x+Nn3 DO{@&B delta 48 zcmaFB^ngiSODn|F!q`nqii;tjC_lX@F;^iiGbdHSz*yhdK;O_nN5Rm{%EWA=x+Nn3 DN7M|0 diff --git a/src/tests/programs/QUEUESIM.8xp b/src/tests/programs/QUEUESIM.8xp index 712188ca33c770d5c3b5db59dae6ba545b533472..bd82efcfc362395be42f7ddaa4e3f4573ae647bd 100644 GIT binary patch delta 48 zcmbQlJc(IdODn|F!q`nqii^RyC^fMpHANw*QX#}sHzYqlr&z&2&rr{dfq`M8dNv~f DF~|%@ delta 48 zcmbQlJc(IdODn|F!q`nqii;tjC_lX@F;^iiGbdHSz*yhdK;O_nN5Rm{%EWA=dNv~f DEAR{& diff --git a/src/tests/programs/RADICAL.8xp b/src/tests/programs/RADICAL.8xp index 9fe2828300f8bbe4b64188518bc89daa94ddbb2f..9fa4498d200fd9559c84ee46bfd04219cac9213f 100644 GIT binary patch delta 15 WcmdnSx{Y;$GNai>l?hBt;YR@{zy#(1 delta 15 WcmdnSx{Y;$GNb86l?hBtSxf*Y8w51~ diff --git a/src/tests/programs/SPLINE.8xp b/src/tests/programs/SPLINE.8xp index 312b4ab0e531e052ae666863c305c81dfc41138f..148959d96344ede2c1fbfeec121837b0102a07ab 100644 GIT binary patch delta 15 WcmbQhIe~M6GNai>l`Iw}H*NqTZUeIb delta 15 WcmbQhIe~M6GNbWEl`Iw}7Zv~_O9Q6> diff --git a/src/tests/programs/TOCCATA.8xp b/src/tests/programs/TOCCATA.8xp index e0d89fcf405bdd579ea6ced05e89d0ade6ab35ee..3034fd4edf518a2ad0ddb8c5e3904141b351f65e 100644 GIT binary patch delta 47 zcmdnTxQ|g?ODn|F!q`nqii^RyC^fMpHANw*QX#}sHzYqlr&z&2&rr{dfq`M7`g8y^ CX$+