1use alloc::string::ToString;
2
3pub fn remove_pcap_headers(data: &[u8]) -> Result<&[u8], alloc::string::String> {
8 remove_radiotap_hdr(data)
9 .and_then(remove_80211_hdr)
10 .and_then(remove_llc_hdr)
11}
12
13#[allow(clippy::missing_errors_doc, reason = "no documentation present")]
14fn remove_radiotap_hdr(data: &[u8]) -> Result<&[u8], alloc::string::String> {
15 let radiotap_version: u8 = data[0];
24 if radiotap_version != 0 {
25 return Err(alloc::format!(
26 "Unknown header version {radiotap_version:#x}"
27 ));
28 }
29
30 let hdr_len: usize = u16::from_le_bytes([data[2], data[3]]).into();
31 let (_, remaining) = data.split_at(hdr_len);
32
33 Ok(remaining)
34}
35
36#[allow(clippy::missing_errors_doc, reason = "no documentation present")]
37fn remove_80211_hdr(data: &[u8]) -> Result<&[u8], alloc::string::String> {
38 let ieee80211_framecontrol: u8 = data[0];
55
56 let ieee80211_fc_version: u8 = ieee80211_framecontrol & 0x03; if ieee80211_fc_version != 0 {
58 return Err(
59 alloc::format!("Unknown 802.11 header version {ieee80211_fc_version}").to_string(),
60 );
61 }
62
63 let ieee80211_fc_type: u8 = (ieee80211_framecontrol & 0x0c) >> 2; if ieee80211_fc_type != 0b10 {
65 return Err(
67 alloc::format!("Unsupported 802.11 frame type {ieee80211_fc_type}").to_string(),
68 );
69 }
70
71 let ieee80211_fc_subtype: u8 = (ieee80211_framecontrol & 0xf0) >> 4; if ieee80211_fc_subtype != 0b1000 {
73 return Err(
75 alloc::format!("Unsupported 802.11 frame subtype {ieee80211_fc_type:#04x}").to_string(),
76 );
77 }
78
79 let hdr_len: usize = 26; let (_, remaining) = data.split_at(hdr_len);
81
82 Ok(remaining)
83}
84
85#[allow(clippy::missing_errors_doc, reason = "no documentation present")]
86fn remove_llc_hdr(data: &[u8]) -> Result<&[u8], alloc::string::String> {
87 let llc_type: u16 = (u16::from(data[6]) << 8) | u16::from(data[7]);
97
98 if llc_type != 0x8947 {
99 return Err(alloc::format!("Unknown LLC payload type {llc_type:#x}").to_string());
100 }
101
102 let hdr_len: usize = 8; let (_, remaining) = data.split_at(hdr_len);
104
105 Ok(remaining)
106}