openpgp_card/ocard/data/
extended_length_info.rs

1// SPDX-FileCopyrightText: 2021 Heiko Schaefer <heiko@schaefer.name>
2// SPDX-License-Identifier: MIT OR Apache-2.0
3
4//! 4.1.3.1 Extended length information
5//! (Introduced in V3.0)
6
7use std::convert::TryFrom;
8
9use nom::{bytes::complete::tag, number::complete as number, sequence};
10
11use crate::ocard::data::{complete, ExtendedLengthInfo};
12
13fn parse(input: &[u8]) -> nom::IResult<&[u8], (u16, u16)> {
14    let (input, (_, cmd, _, resp)) = nom::combinator::all_consuming(sequence::tuple((
15        tag([0x2, 0x2]),
16        number::be_u16,
17        tag([0x2, 0x2]),
18        number::be_u16,
19    )))(input)?;
20
21    Ok((input, (cmd, resp)))
22}
23
24impl ExtendedLengthInfo {
25    pub fn max_command_bytes(&self) -> u16 {
26        self.max_command_bytes
27    }
28
29    pub fn max_response_bytes(&self) -> u16 {
30        self.max_response_bytes
31    }
32}
33
34impl TryFrom<&[u8]> for ExtendedLengthInfo {
35    type Error = crate::Error;
36
37    fn try_from(input: &[u8]) -> Result<Self, Self::Error> {
38        let eli = complete(parse(input))?;
39
40        Ok(Self {
41            max_command_bytes: eli.0,
42            max_response_bytes: eli.1,
43        })
44    }
45}
46
47#[cfg(test)]
48mod test {
49    use super::*;
50
51    #[test]
52    fn test_floss34() {
53        let data = [0x2, 0x2, 0x8, 0x0, 0x2, 0x2, 0x8, 0x0];
54
55        let eli =
56            ExtendedLengthInfo::try_from(&data[..]).expect("failed to parse extended length info");
57
58        assert_eq!(
59            eli,
60            ExtendedLengthInfo {
61                max_command_bytes: 2048,
62                max_response_bytes: 2048,
63            },
64        );
65    }
66}