Skip to content

feat(types): Add TBF header serialization support with set_flags function#109

Open
thoguii wants to merge 4 commits into
WyliodrinEmbeddedIoT:mainfrom
thoguii:tbf-header-set-flags
Open

feat(types): Add TBF header serialization support with set_flags function#109
thoguii wants to merge 4 commits into
WyliodrinEmbeddedIoT:mainfrom
thoguii:tbf-header-set-flags

Conversation

@thoguii

@thoguii thoguii commented Oct 1, 2025

Copy link
Copy Markdown

Pull Request Overview

TODO or Help Wanted

Checks

Using Rust tooling

  • Ran cargo fmt
  • Ran cargo clippy
  • Ran cargo test
  • Ran cargo build

Features tested:

  • feature on board

GitHub Issue

This pull request closes #117

Comment thread tbf-parser/src/types.rs Outdated
Comment on lines +1223 to +1242
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 = ....

Comment thread tbf-parser/src/types.rs Outdated
}
}

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.

Comment thread tbf-parser/src/types.rs Outdated
}
}

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

@eva-cosma eva-cosma left a comment

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.

Looks really good! Two observations though:

  • Could you write some documentation for the pub fn functions you wrote?
  • Could write some tests for your functions?

feat(tests/serialization): add tests for TBF header serialization

@eva-cosma eva-cosma left a comment

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 appreciate the enthusiasm with which you write the tests, but I feel like some of these overlap with eacother, or multiple could be merged into a single test.

I did not comment all of the tests, I'm going to leave it as an exercise for you to try to merge as many / deduplicate them.

Comment thread tbf-parser/tests/serialization.rs Outdated
}

#[test]
fn footer_sha256() {

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.

Since we're essentialy just testing the serialization of the first 16 bytes, having a footer does not make this test any different from the simple_tbf test

Comment thread tbf-parser/tests/serialization.rs Outdated
// Serialization

#[test]
fn simple_tbf() {

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.

Suggested change
fn simple_tbf() {
fn serialize_identical_with_original() {

Comment thread tbf-parser/tests/serialization.rs Outdated
}

#[test]
fn footer_rsa4096() {

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.

Same as above, footer does not affect header base serialization

Comment thread tbf-parser/src/types.rs

pub fn set_flags(&mut self, flags: u32, header: &[u8]) -> Result<[u8; 16], TbfParseError> {
/// Sets the flag field and updates the checksum, it modifies the state accordingly
pub fn set_flags(&mut self, flags: u32, header: &[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.

Besides this function, I would create a

  • set_enabled
  • set_sticky

and anything else the flags can be. Just so that the end-user of this crate does not need to know which bit is the 'enabled' or 'sticky' bit

Comment thread tbf-parser/tests/serialization.rs Outdated
let mut header = parse_tbf_header(&buffer[0..header_len as usize], 2).unwrap();

assert!(header.enabled());
assert_eq!(header.get_package_name().unwrap(), "_heart");

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.

package name not relevant for this test

Comment thread tbf-parser/tests/serialization.rs Outdated
}

#[test]
fn enable_disable_footer_sha256() {

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.

Same as above, footer does not affect header base serialization

Comment thread tbf-parser/tests/serialization.rs Outdated
}

#[test]
fn padding_header2() {

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.

Suggested change
fn padding_header2() {
fn padding_header_set_flags() {

fix(tests/serialization): removed redundant tests and make enable and sticky tests explicit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

tbf-parser: Add serialization

2 participants