Skip to content
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

Create standard binary format encapsulating constant size data #3142

Closed
tiziano88 opened this issue Aug 9, 2022 · 0 comments
Closed

Create standard binary format encapsulating constant size data #3142

tiziano88 opened this issue Aug 9, 2022 · 0 comments

Comments

@tiziano88
Copy link
Collaborator

To represent a constant size byte array, we currently use a pair of fields embedded in an object / message, e.g.:

/// body (may include padding 0s)
pub body: alloc::vec::Vec<u8>,
/// The effective length of the body, excluding any padding contained
pub length: u64,

I think it would be useful to define a simple packed representation of a padded byte array once and for all, and write packers / unpackers for each language.

The simplest solution would be to just concatenate the actual length (as a little endian 32 bit unsigned int), with the actual data, and then finally any remaining padding.

For instance, the input vector (as hex string)

001122334455

would be encoded as a 8-byte padded vector as:

actual length (little endian, 4 bytes)
|        actual data (6 bytes)
|        |
|        |            padding (2 bytes)
|        |            |
06000000 001122334455 0000

Note that 8 bytes refers to the length of the padded data, without the prefix, so this vector is ultimately 12 bytes in total.

Then for instance we could have the following methods in Rust:

fn pad(actual_data: &[u8]) -> Vec<u8> {
  ...
}

fn unpad(padded_data: &[u8]) -> &[u8] {
  ...
}

Note that these operate on single vectors, therefore there is no need to assemble parameters from different fields in a struct / message (unlike the current mechanism).

Note also that unpad can return a reference to a subslice of the padded data, without reallocations.

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

No branches or pull requests

1 participant