Skip to content

CommandBuilder::serialize_into does not return length of data #33

@dishmaker

Description

@dishmaker

Problem

Currently, to get the resulting buffer after encoding CommandBuilder, length needs to be calculated separately.

let len = builder.required_len();
builder.serialize_into(&mut &mut buf[..]).unwrap();
&buf[..len]

Solution

It would be nice if serialize_into returned length too:

let len = builder.serialize_into(&mut &mut buf[..]).unwrap();
&buf[..len]

Proposal

Even better - let's encode directly to slice like der::Encode:
https://docs.rs/der/0.8.0-rc.10/der/trait.Encode.html#method.encode_to_slice

fn encode_to_slice<'a>(&self, buf: &'a mut [u8]) -> Result<&'a [u8]>
[Spoiler] Encoding with iso7816 0.2.0
use iso7816::command::{CommandBuilder, ExpectedLen, class::Class};

fn main() {
    let mut buf = [0u8; 16];
    let read_binary = {
        let builder = CommandBuilder::<()>::new(
            Class::from_byte(0x00).unwrap(),
            iso7816::Instruction::ReadBinary,
            0x00,
            0x00,
            (),
            ExpectedLen::Ne(65535),
        )
        .force_extended();
        let len = builder.required_len();
        builder.serialize_into(&mut &mut buf[..]).unwrap();
        &buf[..len]
    };

    println!("read binary (extended) C-APDU: {:?}", read_binary);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions