-
Notifications
You must be signed in to change notification settings - Fork 15
feat(types): Add TBF header serialization support with set_flags function #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
847d9c5
8430e23
208fc14
9561750
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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() | ||
| } | ||
|
|
||
| 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) | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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> { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does this function do?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(()) | ||
| } | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
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