use alloc::string::ToString;
pub fn remove_pcap_headers(data: &[u8]) -> Result<&[u8], alloc::string::String> {
remove_radiotap_hdr(data).and_then(remove_wlan_headers)
}
pub fn remove_wlan_headers(data: &[u8]) -> Result<&[u8], alloc::string::String> {
remove_80211_hdr(data).and_then(remove_llc_hdr)
}
#[allow(clippy::missing_errors_doc, reason = "no documentation present")]
fn remove_radiotap_hdr(data: &[u8]) -> Result<&[u8], alloc::string::String> {
let radiotap_version: u8 = data[0];
if radiotap_version != 0 {
return Err(alloc::format!(
"Unknown header version {radiotap_version:#x}"
));
}
let hdr_len: usize = u16::from_le_bytes([data[2], data[3]]).into();
let (_, remaining) = data.split_at(hdr_len);
Ok(remaining)
}
#[allow(clippy::missing_errors_doc, reason = "no documentation present")]
fn remove_80211_hdr(data: &[u8]) -> Result<&[u8], alloc::string::String> {
let ieee80211_framecontrol: u8 = data[0];
let ieee80211_fc_version: u8 = ieee80211_framecontrol & 0x03; if ieee80211_fc_version != 0 {
return Err(
alloc::format!("Unknown 802.11 header version {ieee80211_fc_version}").to_string(),
);
}
let ieee80211_fc_type: u8 = (ieee80211_framecontrol & 0x0c) >> 2; if ieee80211_fc_type != 0b10 {
return Err(
alloc::format!("Unsupported 802.11 frame type {ieee80211_fc_type}").to_string(),
);
}
let ieee80211_fc_subtype: u8 = (ieee80211_framecontrol & 0xf0) >> 4; if !(ieee80211_fc_subtype == 0b1000 || ieee80211_fc_subtype == 0b0000) {
return Err(
alloc::format!("Unsupported 802.11 frame subtype {ieee80211_fc_subtype:#04x}")
.to_string(),
);
}
let hdr_len: usize = 26; let (_, remaining) = data.split_at(hdr_len);
Ok(remaining)
}
#[allow(clippy::missing_errors_doc, reason = "no documentation present")]
fn remove_llc_hdr(data: &[u8]) -> Result<&[u8], alloc::string::String> {
let llc_type: u16 = (u16::from(data[6]) << 8) | u16::from(data[7]);
if llc_type != 0x8947 {
return Err(alloc::format!("Unknown LLC payload type {llc_type:#x}").to_string());
}
let hdr_len: usize = 8; let (_, remaining) = data.split_at(hdr_len);
Ok(remaining)
}