Skip to content

Commit

Permalink
fixed checksum
Browse files Browse the repository at this point in the history
  • Loading branch information
cqb13 committed Jul 15, 2024
1 parent 3a08418 commit 144d7b8
Show file tree
Hide file tree
Showing 11 changed files with 8 additions and 10 deletions.
5 changes: 2 additions & 3 deletions src/calculator/create/from_8xp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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);

Expand Down
13 changes: 6 additions & 7 deletions src/calculator/create/from_txt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<u32>()
+ body_bytes.iter().map(|&byte| byte as u32).sum::<u32>();
let checksum = checksum.to_le_bytes()[0..2].to_vec();
let checksum = [checksum[0], checksum[1]];

let metadata = Metadata::new(
metadata_bytes,
0x0D,
Expand All @@ -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);

Expand Down
Binary file modified src/tests/programs/BASECONV.8xp
Binary file not shown.
Binary file modified src/tests/programs/DBD.8xp
Binary file not shown.
Binary file modified src/tests/programs/FACTOR.8xp
Binary file not shown.
Binary file modified src/tests/programs/MATHTOOL.8xp
Binary file not shown.
Binary file modified src/tests/programs/POLCONIC.8xp
Binary file not shown.
Binary file modified src/tests/programs/QUEUESIM.8xp
Binary file not shown.
Binary file modified src/tests/programs/RADICAL.8xp
Binary file not shown.
Binary file modified src/tests/programs/SPLINE.8xp
Binary file not shown.
Binary file modified src/tests/programs/TOCCATA.8xp
Binary file not shown.

0 comments on commit 144d7b8

Please sign in to comment.