|
| 1 | +use anyhow::{anyhow, Result}; |
| 2 | + |
| 3 | +// References: |
| 4 | +// |
| 5 | +// NDEF parser: https://github.com/TapTrack/NdefLibrary |
| 6 | +// NDEF message spec: https://www.netes.com.tr/netes/dosyalar/dosya/B6159F60458582512B16EF1263ADE707.pdf |
| 7 | +// Mifare reader: https://github.com/hackeriet/pyhackeriet |
| 8 | +// Mifare specs: https://www.puntoflotante.net/TUTORIAL-RFID-ISO-14443A-TAGS-13.56-MHZ.htm |
| 9 | + |
| 10 | +#[derive(PartialEq)] |
| 11 | +enum NdefMessageParserState { |
| 12 | + Init, |
| 13 | + Length, |
| 14 | + Value, |
| 15 | +} |
| 16 | + |
| 17 | +pub struct NdefMessageParser { |
| 18 | + state: NdefMessageParserState, |
| 19 | + length: i32, |
| 20 | + pub data: Vec<u8>, |
| 21 | +} |
| 22 | + |
| 23 | +impl NdefMessageParser { |
| 24 | + pub fn new() -> Self { |
| 25 | + Self { |
| 26 | + state: NdefMessageParserState::Init, |
| 27 | + length: -1, |
| 28 | + data: Vec::new(), |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + pub fn add_data(&mut self, data: &[u8]) { |
| 33 | + for byte in data { |
| 34 | + match self.state { |
| 35 | + NdefMessageParserState::Init => { |
| 36 | + if *byte == 0x00 { |
| 37 | + continue; |
| 38 | + } |
| 39 | + |
| 40 | + if *byte == 0x03 { |
| 41 | + self.state = NdefMessageParserState::Length; |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + NdefMessageParserState::Length => { |
| 46 | + if self.length == -1 { |
| 47 | + if *byte == 0xff { |
| 48 | + self.length = -2; |
| 49 | + } else { |
| 50 | + self.length = *byte as i32; |
| 51 | + self.state = NdefMessageParserState::Value; |
| 52 | + } |
| 53 | + |
| 54 | + continue; |
| 55 | + } |
| 56 | + |
| 57 | + if self.length == -2 { |
| 58 | + self.length = *byte as i32; |
| 59 | + } else { |
| 60 | + self.length = (self.length << 8) & *byte as i32; |
| 61 | + self.state = NdefMessageParserState::Value; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + NdefMessageParserState::Value => { |
| 66 | + self.data.push(*byte); |
| 67 | + |
| 68 | + if self.data.len() as i32 == self.length { |
| 69 | + return; |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + pub fn is_done(&self) -> bool { |
| 77 | + self.data.len() as i32 == self.length |
| 78 | + } |
| 79 | + |
| 80 | + pub fn has_started(&self) -> bool { |
| 81 | + self.state != NdefMessageParserState::Init |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +pub struct NdefTextRecord { |
| 86 | + _id: Vec<u8>, |
| 87 | + value: Vec<u8>, |
| 88 | +} |
| 89 | + |
| 90 | +impl NdefTextRecord { |
| 91 | + pub fn text(&self) -> Result<String> { |
| 92 | + if self.value.is_empty() { |
| 93 | + return Ok(String::new()); |
| 94 | + } |
| 95 | + |
| 96 | + let code_length = self.value[0] & 0b00111111; |
| 97 | + Ok(String::from_utf8( |
| 98 | + self.value[(code_length as usize + 1)..].to_vec(), |
| 99 | + )?) |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +pub fn parse_ndef_text_record(data: &[u8]) -> Result<NdefTextRecord> { |
| 104 | + let record_length = data.len(); |
| 105 | + let mut index = 0; |
| 106 | + |
| 107 | + if record_length <= index { |
| 108 | + return Err(anyhow!("Flags missing")); |
| 109 | + } |
| 110 | + |
| 111 | + let flags = data[index]; |
| 112 | + |
| 113 | + let _is_message_begin = (flags & 0b10000000) != 0; |
| 114 | + let _is_message_end = (flags & 0b01000000) != 0; |
| 115 | + let is_chunked = (flags & 0b00100000) != 0; |
| 116 | + let is_short_record = (flags & 0b00010000) != 0; |
| 117 | + let has_id_length = (flags & 0b00001000) != 0; |
| 118 | + let type_name_format = flags & 0b00000111; |
| 119 | + |
| 120 | + if is_chunked { |
| 121 | + return Err(anyhow!("Chunked records are not supported")); |
| 122 | + } |
| 123 | + |
| 124 | + index += 1; |
| 125 | + |
| 126 | + if record_length <= index { |
| 127 | + return Err(anyhow!("Type length missing")); |
| 128 | + } |
| 129 | + |
| 130 | + let type_length = data[index]; |
| 131 | + |
| 132 | + index += 1; |
| 133 | + |
| 134 | + let payload_length = if is_short_record { |
| 135 | + if record_length <= index { |
| 136 | + return Err(anyhow!("Payload length missing")); |
| 137 | + } |
| 138 | + |
| 139 | + let payload_length_index = index; |
| 140 | + index += 1; |
| 141 | + data[payload_length_index] as u32 |
| 142 | + } else { |
| 143 | + if record_length <= index + 3 { |
| 144 | + return Err(anyhow!("Payload length missing")); |
| 145 | + } |
| 146 | + |
| 147 | + let payload_length_index = index; |
| 148 | + index += 4; |
| 149 | + u32::from_be_bytes(data[payload_length_index..(payload_length_index + 4)].try_into()?) |
| 150 | + }; |
| 151 | + |
| 152 | + let id_length = if has_id_length { |
| 153 | + if record_length <= index { |
| 154 | + return Err(anyhow!("ID length missing")); |
| 155 | + } |
| 156 | + |
| 157 | + let id_length_index = index; |
| 158 | + index += 1; |
| 159 | + data[id_length_index] |
| 160 | + } else { |
| 161 | + 0 |
| 162 | + }; |
| 163 | + |
| 164 | + let payload_type = if type_length > 0 { |
| 165 | + let type_index = index; |
| 166 | + index += type_length as usize; |
| 167 | + |
| 168 | + if record_length < type_index + type_length as usize { |
| 169 | + return Err(anyhow!("Type missing")); |
| 170 | + } |
| 171 | + |
| 172 | + data[type_index..(type_index + type_length as usize)].to_vec() |
| 173 | + } else { |
| 174 | + vec![] |
| 175 | + }; |
| 176 | + |
| 177 | + let payload_id = if id_length > 0 { |
| 178 | + let id_index = index; |
| 179 | + index += id_length as usize; |
| 180 | + |
| 181 | + if record_length < id_index + id_length as usize { |
| 182 | + return Err(anyhow!("ID missing")); |
| 183 | + } |
| 184 | + |
| 185 | + data[id_index..(id_index + id_length as usize)].to_vec() |
| 186 | + } else { |
| 187 | + vec![] |
| 188 | + }; |
| 189 | + |
| 190 | + let payload_value = if payload_length > 0 { |
| 191 | + let value_index = index; |
| 192 | + |
| 193 | + if record_length < value_index + payload_length as usize { |
| 194 | + return Err(anyhow!("Payload missing")); |
| 195 | + } |
| 196 | + |
| 197 | + data[value_index..(value_index + payload_length as usize)].to_vec() |
| 198 | + } else { |
| 199 | + vec![] |
| 200 | + }; |
| 201 | + |
| 202 | + if type_name_format != 1 { |
| 203 | + return Err(anyhow!("Not a well known payload type")); |
| 204 | + } |
| 205 | + |
| 206 | + if payload_type != [0x54] { |
| 207 | + return Err(anyhow!("Not a text record")); |
| 208 | + } |
| 209 | + |
| 210 | + Ok(NdefTextRecord { |
| 211 | + _id: payload_id, |
| 212 | + value: payload_value, |
| 213 | + }) |
| 214 | +} |
0 commit comments