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 4a896af..cd4fc1e 100644 Binary files a/src/tests/programs/BASECONV.8xp and b/src/tests/programs/BASECONV.8xp differ diff --git a/src/tests/programs/DBD.8xp b/src/tests/programs/DBD.8xp index 47c9140..665605a 100644 Binary files a/src/tests/programs/DBD.8xp and b/src/tests/programs/DBD.8xp differ diff --git a/src/tests/programs/FACTOR.8xp b/src/tests/programs/FACTOR.8xp index 6778cbd..6cd42e2 100644 Binary files a/src/tests/programs/FACTOR.8xp and b/src/tests/programs/FACTOR.8xp differ diff --git a/src/tests/programs/MATHTOOL.8xp b/src/tests/programs/MATHTOOL.8xp index 1c49dd6..693aca2 100644 Binary files a/src/tests/programs/MATHTOOL.8xp and b/src/tests/programs/MATHTOOL.8xp differ diff --git a/src/tests/programs/POLCONIC.8xp b/src/tests/programs/POLCONIC.8xp index 6dfc454..4828ea5 100644 Binary files a/src/tests/programs/POLCONIC.8xp and b/src/tests/programs/POLCONIC.8xp differ diff --git a/src/tests/programs/QUEUESIM.8xp b/src/tests/programs/QUEUESIM.8xp index 712188c..bd82efc 100644 Binary files a/src/tests/programs/QUEUESIM.8xp and b/src/tests/programs/QUEUESIM.8xp differ diff --git a/src/tests/programs/RADICAL.8xp b/src/tests/programs/RADICAL.8xp index 9fe2828..9fa4498 100644 Binary files a/src/tests/programs/RADICAL.8xp and b/src/tests/programs/RADICAL.8xp differ diff --git a/src/tests/programs/SPLINE.8xp b/src/tests/programs/SPLINE.8xp index 312b4ab..148959d 100644 Binary files a/src/tests/programs/SPLINE.8xp and b/src/tests/programs/SPLINE.8xp differ diff --git a/src/tests/programs/TOCCATA.8xp b/src/tests/programs/TOCCATA.8xp index e0d89fc..3034fd4 100644 Binary files a/src/tests/programs/TOCCATA.8xp and b/src/tests/programs/TOCCATA.8xp differ