mirror of
https://github.com/trussed-dev/usbd-ccid.git
synced 2026-06-20 04:16:20 -07:00
Improve usage of constants to avoid magic valuse
This commit is contained in:
+3
-1
@@ -4,6 +4,8 @@ pub const PACKET_SIZE: usize = 512;
|
||||
#[cfg(not(feature = "highspeed-usb"))]
|
||||
pub const PACKET_SIZE: usize = 64;
|
||||
|
||||
pub const CCID_HEADER_LEN: usize = 10;
|
||||
|
||||
pub const CLASS_CCID: u8 = 0x0B;
|
||||
pub const SUBCLASS_NONE: u8 = 0x0;
|
||||
|
||||
@@ -39,7 +41,7 @@ pub const MAX_IFSD: [u8; 4] = [0xfe, 0x00, 0x00, 0x00];
|
||||
// "The value shall be between 261 + 10 and 65544 + 10
|
||||
// dwMaxCCIDMsgLen 3072
|
||||
pub const MAX_MSG_LENGTH: usize = 3072;
|
||||
pub const MAX_MSG_LENGTH_LE: [u8; 4] = [0x00, 0x0C, 0x00, 0x00];
|
||||
pub const MAX_MSG_LENGTH_LE: [u8; 4] = (MAX_MSG_LENGTH as u32).to_le_bytes();
|
||||
|
||||
pub const NUM_SLOTS: u8 = 1;
|
||||
pub const MAX_BUSY_SLOTS: u8 = 1;
|
||||
|
||||
+5
-5
@@ -145,7 +145,7 @@ where
|
||||
// when certificates are transmitted, because PIV somehow uses short APDUs
|
||||
// only (can we fix this), so 255B is the maximum)
|
||||
if !self.receiving_long {
|
||||
if packet.len() < 10 {
|
||||
if packet.len() < CCID_HEADER_LEN {
|
||||
panic!("unexpected short packet");
|
||||
}
|
||||
self.ext_packet.clear();
|
||||
@@ -304,7 +304,7 @@ where
|
||||
if self.state == State::Processing {
|
||||
// Need to send a wait extension request.
|
||||
let mut packet = RawPacket::new();
|
||||
packet.resize_default(10).ok();
|
||||
packet.resize_default(CCID_HEADER_LEN).ok();
|
||||
packet[0] = 0x80;
|
||||
packet[6] = self.seq;
|
||||
|
||||
@@ -369,7 +369,7 @@ where
|
||||
panic!("No response while priming outbox");
|
||||
};
|
||||
|
||||
let chunk_size = core::cmp::min(PACKET_SIZE - 10, message.len() - self.sent);
|
||||
let chunk_size = core::cmp::min(PACKET_SIZE - CCID_HEADER_LEN, message.len() - self.sent);
|
||||
let chunk = &message[self.sent..][..chunk_size];
|
||||
self.sent += chunk_size;
|
||||
let more = self.sent < message.len();
|
||||
@@ -409,7 +409,7 @@ where
|
||||
|
||||
fn send_slot_status_ok(&mut self) {
|
||||
let mut packet = RawPacket::new();
|
||||
packet.resize_default(10).ok();
|
||||
packet.resize_default(CCID_HEADER_LEN).ok();
|
||||
packet[0] = 0x81;
|
||||
packet[6] = self.seq;
|
||||
self.send_packet_assuming_possible(packet);
|
||||
@@ -417,7 +417,7 @@ where
|
||||
|
||||
fn send_slot_status_error(&mut self, error: Error) {
|
||||
let mut packet = RawPacket::new();
|
||||
packet.resize_default(10).ok();
|
||||
packet.resize_default(CCID_HEADER_LEN).ok();
|
||||
packet[0] = 0x6c;
|
||||
packet[6] = self.seq;
|
||||
packet[7] = 1 << 6;
|
||||
|
||||
+6
-6
@@ -45,9 +45,9 @@ pub trait PacketWithData: Packet {
|
||||
fn data(&self) -> &[u8] {
|
||||
// let len = u32::from_le_bytes(self[1..5].try_into().unwrap()) as usize;
|
||||
let declared_len = u32::from_le_bytes(self[1..5].try_into().unwrap()) as usize;
|
||||
let len = core::cmp::min(MAX_MSG_LENGTH - 10, declared_len);
|
||||
let len = core::cmp::min(MAX_MSG_LENGTH - CCID_HEADER_LEN, declared_len);
|
||||
// hprintln!("delcared = {}, len = {}", declared_len, len).ok();
|
||||
&self[10..][..len]
|
||||
&self[CCID_HEADER_LEN..][..len]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,7 +76,7 @@ pub struct DataBlock<'a> {
|
||||
|
||||
impl<'a> DataBlock<'a> {
|
||||
pub fn new(seq: u8, chain: Chain, data: &'a [u8]) -> Self {
|
||||
assert!(data.len() + 10 <= PACKET_SIZE);
|
||||
assert!(data.len() + CCID_HEADER_LEN <= PACKET_SIZE);
|
||||
Self { seq, chain, data }
|
||||
}
|
||||
}
|
||||
@@ -117,7 +117,7 @@ impl From<DataBlock<'_>> for RawPacket {
|
||||
fn from(block: DataBlock<'_>) -> RawPacket {
|
||||
let mut packet = RawPacket::new();
|
||||
let len = block.data.len();
|
||||
packet.resize_default(10 + len).ok();
|
||||
packet.resize_default(CCID_HEADER_LEN + len).ok();
|
||||
packet[0] = 0x80;
|
||||
packet[1..][..4].copy_from_slice(
|
||||
&u32::try_from(len)
|
||||
@@ -133,7 +133,7 @@ impl From<DataBlock<'_>> for RawPacket {
|
||||
packet[8] = 0;
|
||||
// chain parameter
|
||||
packet[9] = block.chain as u8;
|
||||
packet[10..][..len].copy_from_slice(block.data);
|
||||
packet[CCID_HEADER_LEN..][..len].copy_from_slice(block.data);
|
||||
|
||||
packet
|
||||
}
|
||||
@@ -224,7 +224,7 @@ macro_rules! command_message {
|
||||
fn try_from(packet: ExtPacket)
|
||||
-> core::result::Result<Self, Self::Error>
|
||||
{
|
||||
if packet.len() < 10 {
|
||||
if packet.len() <CCID_HEADER_LEN {
|
||||
return Err(Error::ShortPacket);
|
||||
}
|
||||
if packet[5] != 0 {
|
||||
|
||||
Reference in New Issue
Block a user