Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions tbf-parser/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1183,4 +1183,77 @@ impl TbfHeader {
_ => None,
}
}

pub fn compute_checksum(header: &[u8], new_flags: u32) -> Result<u32, TbfParseError> {
let mut checksum: u32 = 0;

let header_iter = header.chunks_exact(4);

// Iterate all chunks and XOR the chunks to compute the checksum.
for (i, chunk) in header_iter.enumerate() {
let word = if i == 2 {
new_flags
} else if i == 3 {
continue;
} else {
u32::from_le_bytes(chunk.try_into()?)
};
checksum ^= word;
}
Ok(checksum)
}

pub fn set_flags(&mut self, flags: u32, header: &[u8]) -> Result<[u8; 16], TbfParseError> {
let new_checksum = Self::compute_checksum(header, flags)?;
match self {
TbfHeader::TbfHeaderV2(hd) => {
hd.base.flags = flags;
hd.base.checksum = new_checksum;
}
TbfHeader::Padding(base) => {
base.flags = flags;
base.checksum = new_checksum;
}
}

self.serialize()

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wouldn't return anything for this function

}

pub fn serialize(&self) -> Result<[u8; 16], TbfParseError> {
match self {
TbfHeader::TbfHeaderV2(hd) => {
let base = &hd.base;
let mut bytes = [0u8; 16];
bytes[0..2].copy_from_slice(&base.version.to_le_bytes());
bytes[2..4].copy_from_slice(&base.header_size.to_le_bytes());
bytes[4..8].copy_from_slice(&base.total_size.to_le_bytes());
bytes[8..12].copy_from_slice(&base.flags.to_le_bytes());
bytes[12..16].copy_from_slice(&base.checksum.to_le_bytes());
Ok(bytes)
}
TbfHeader::Padding(base) => {
let mut bytes = [0u8; 16];
bytes[0..2].copy_from_slice(&base.version.to_le_bytes());
bytes[2..4].copy_from_slice(&base.header_size.to_le_bytes());
bytes[4..8].copy_from_slice(&base.total_size.to_le_bytes());
bytes[8..12].copy_from_slice(&base.flags.to_le_bytes());
bytes[12..16].copy_from_slice(&base.checksum.to_le_bytes());
Ok(bytes)
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactor suggestion:

let base = match self {
TbfHeader::TbfHeaderV2(hd) => &hd.base,
TbfHeader::Pading(base) => &base,
};

let bytes = ....

}
}

pub fn update_from_serialize(&mut self, bytes: &[u8]) -> Result<(), TbfParseError> {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does this function do?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the feedback! I did this function to provide a way to sync the Tbf_base with externally modified bytes without the need to re-parse. It just parses bytes back into struct, tho indeed it's better to use parse_tbf_header directly.

let new_base = TbfHeaderV2Base::try_from(&bytes[..])?;
match self {
TbfHeader::TbfHeaderV2(hd) => {
hd.base = new_base;
Ok(())
}
TbfHeader::Padding(base) => {
*base = new_base;
Ok(())
}
}
}
}
Loading