1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
//! Bounded sizing for AtomicReadFile ACKs only; never copies storage payloads.
use super::*;
fn read_ack_octet_string_len(len: usize) -> Option<usize> {
let wire_len = u32::try_from(len).ok()?;
let prefix = match wire_len {
0..=4 => 1,
5..=253 => 2,
254..=65535 => 4,
_ => 6,
};
len.checked_add(prefix)
}
impl AtomicReadFileAck {
/// Exact logical service encoding length if it fits `limit` and wire lengths.
///
/// Borrows payloads without encoding or copying them and stops summing as
/// soon as the cap is exceeded. APDU/NPDU headers are not included.
pub fn encoded_len_bounded(&self, limit: usize) -> Option<usize> {
fn add(total: &mut usize, amount: usize, limit: usize) -> Option<()> {
*total = total.checked_add(amount)?;
(*total <= limit).then_some(())
}
// Application Boolean plus opening and closing CHOICE tags.
let mut total = 0;
add(&mut total, 3, limit)?;
match &self.access {
FileReadAckMethod::Stream {
file_start_position,
file_data,
} => {
add(
&mut total,
1 + primitives::signed_len(*file_start_position) as usize,
limit,
)?;
add(
&mut total,
read_ack_octet_string_len(file_data.len())?,
limit,
)?;
}
FileReadAckMethod::Record {
file_start_record,
returned_record_count,
file_record_data,
} => {
u32::try_from(file_record_data.len()).ok()?;
add(
&mut total,
1 + primitives::signed_len(*file_start_record) as usize,
limit,
)?;
add(
&mut total,
1 + primitives::unsigned_len(u64::from(*returned_record_count)) as usize,
limit,
)?;
for record in file_record_data {
add(&mut total, read_ack_octet_string_len(record.len())?, limit)?;
}
}
}
Some(total)
}
}
#[cfg(test)]
#[path = "file_ack_size_tests.rs"]
mod tests;