-
-
Notifications
You must be signed in to change notification settings - Fork 55
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
Limiting the size of a compound structure / enum that can be browsed by the reader #488
Comments
I've found a solution, by implementing a custom reader :
I use it this way :
|
I think it could be nice to be able to :
|
Hey! I added the bytes support for arbitrary bytes number from any token in this MR: #489 I solved your issue with the use of use deku::prelude::*;
fn main() {
env_logger::init();
#[derive(Debug, PartialEq)]
#[deku_derive(DekuRead)]
struct Data {
#[deku(read_all)]
pub blocks: Vec<Block>,
}
#[derive(Debug, PartialEq)]
#[deku_derive(DekuRead)]
struct Block {
pub size: u8,
pub id: u8,
#[deku(ctx = "*id, *size as usize -1")] // size-1 to remove the ID size
pub data: BlockType,
}
#[derive(Debug, PartialEq)]
#[deku_derive(DekuRead)]
#[deku(ctx = "id: u8, size: usize", id = "id")]
enum BlockType {
#[deku(id = 1)]
Block1(#[deku(ctx = "size")] Block1),
#[deku(id = 2)]
Block2(Block2),
}
#[derive(Debug, PartialEq)]
#[deku_derive(DekuRead)]
#[deku(ctx = "size: usize")]
struct Block1 {
pub field1_size: u8,
#[deku(bytes = "*field1_size as usize")]
pub field1: u16,
#[deku(skip, cond = "size <= 4")]
pub field2_size: Option<u8>,
#[deku(skip, cond = "size <= 4", bytes = "field2_size.unwrap() as usize")]
pub field2: Option<u64>,
}
#[derive(Debug, PartialEq)]
#[deku_derive(DekuRead)]
struct Block2;
let buffer = &[
// 1st Block, type 1
4, // Size: 4
1, // ID: 1
2, 0x12, 0x34, // Field 1, size 2
// No Field 2
// 2nd Block, type 1
6, // Size: Y
1, // ID: 1
1, 0x56, // Field 1, size 1 (casted into u16)
2, 0x78, 0x9A, // Field 2, size 2
];
let (_rest, val) = Data::from_bytes((buffer, 0)).unwrap();
assert_eq!(val.blocks.len(), 2);
assert_eq!(val.blocks[0].size, 4);
assert_eq!(val.blocks[1].size, 6);
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
My input data is quite complex, but always has a format where structures are preceded by a byte giving the length of the structure to come.
Within a compound structure, the last fields are sometimes not supplied. The only way to know that reading is complete is to know that all the bytes indicated upstream have been consumed, and that the next structure has started.
Here is an example :
How could I handle this case ? Is there a way to handle the second field of the first block ?
The text was updated successfully, but these errors were encountered: