| 24 | #include <Data/HexString.h> |
| 25 | |
| 26 | MacAddress::MacAddress(const String& s) |
| 27 | { |
| 28 | // Maximum length with 2 hex digits per octet plus optional separator |
| 29 | bool sep; |
| 30 | if(s.length() == 12) { |
| 31 | sep = false; |
| 32 | } else if(s.length() == 17) { |
| 33 | sep = true; |
| 34 | } else { |
| 35 | return; |
| 36 | } |
| 37 | auto str = s.c_str(); |
| 38 | Octets res{}; |
| 39 | for(unsigned i = 0; i < 6; ++i) { |
| 40 | if(sep && i != 5 && strchr(_F(":-.,/ "), str[2]) == nullptr) { |
| 41 | return; |
| 42 | } |
| 43 | if(!isxdigit(str[0]) || !isxdigit(str[1])) { |
| 44 | return; |
| 45 | } |
| 46 | res[i] = (unhex(str[0]) << 4) | unhex(str[1]); |
| 47 | str += 2 + unsigned(sep); |
| 48 | } |
| 49 | memcpy(octets, res, sizeof(res)); |
| 50 | } |
| 51 | |
| 52 | uint8_t& MacAddress::operator[](unsigned index) |
| 53 | { |