Skip to content

Commit b0e5916

Browse files
committed
upd ndef parser
by jaylikesbunda and Willy-JL
1 parent fc16978 commit b0e5916

File tree

1 file changed

+41
-16
lines changed
  • applications/main/nfc/plugins/supported_cards

1 file changed

+41
-16
lines changed

applications/main/nfc/plugins/supported_cards/ndef.c

+41-16
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,7 @@ static void
2727
print_data(FuriString* str, const char* prefix, const uint8_t* buf, size_t len, bool force_hex) {
2828
if(prefix) furi_string_cat_printf(str, "%s: ", prefix);
2929
if(!force_hex && is_text(buf, len)) {
30-
char* tmp = malloc(len + 1);
31-
memcpy(tmp, buf, len);
32-
tmp[len] = '\0';
33-
furi_string_cat_printf(str, "%s", tmp);
34-
free(tmp);
30+
furi_string_cat_printf(str, "%.*s", len, buf);
3531
} else {
3632
for(uint8_t i = 0; i < len; i++) {
3733
furi_string_cat_printf(str, "%02X ", buf[i]);
@@ -40,6 +36,17 @@ static void
4036
furi_string_cat(str, "\n");
4137
}
4238

39+
static inline uint8_t hex_to_int(char c) {
40+
if(c >= '0' && c <= '9') return c - '0';
41+
if(c >= 'A' && c <= 'F') return c - 'A' + 10;
42+
if(c >= 'a' && c <= 'f') return c - 'a' + 10;
43+
return 0;
44+
}
45+
46+
static char decode_char(const char* str) {
47+
return (hex_to_int(str[1]) << 4) | hex_to_int(str[2]);
48+
}
49+
4350
static void parse_ndef_uri(FuriString* str, const uint8_t* payload, uint32_t payload_len) {
4451
// https://learn.adafruit.com/adafruit-pn532-rfid-nfc/ndef#uri-records-0x55-slash-u-607763
4552
const char* prepends[] = {
@@ -88,26 +95,44 @@ static void parse_ndef_uri(FuriString* str, const uint8_t* payload, uint32_t pay
8895
size_t prepend_len = strlen(prepend);
8996

9097
size_t uri_len = prepend_len + (payload_len - 1);
91-
char* const uri_buf = malloc(uri_len);
98+
char* const uri_buf = malloc(uri_len); // const to keep the original pointer to free later
9299
memcpy(uri_buf, prepend, prepend_len);
93100
memcpy(uri_buf + prepend_len, payload + 1, payload_len - 1);
94-
char* uri = uri_buf;
101+
char* uri = uri_buf; // cursor we can iterate and shift freely
102+
103+
// Encoded chars take 3 bytes (%AB), decoded chars take 1 byte
104+
// We can decode by iterating and overwriting the same buffer
105+
size_t decoded_len = 0;
106+
for(size_t encoded_idx = 0; encoded_idx < uri_len; encoded_idx++) {
107+
if(uri[encoded_idx] == '%' && encoded_idx + 2 < uri_len) {
108+
char hi = toupper(uri[encoded_idx + 1]);
109+
char lo = toupper(uri[encoded_idx + 2]);
110+
if(((hi >= 'A' && hi <= 'F') || (hi >= '0' && hi <= '9')) &&
111+
((lo >= 'A' && lo <= 'F') || (lo >= '0' && lo <= '9'))) {
112+
uri[decoded_len++] = decode_char(&uri[encoded_idx]);
113+
encoded_idx += 2;
114+
continue;
115+
}
116+
}
117+
uri[decoded_len++] = uri[encoded_idx];
118+
}
95119

96120
const char* type = "URI";
97-
if(strncmp(uri, "http", strlen("http")) == 0) {
121+
if(strncmp(uri, "http", 4) == 0) {
98122
type = "URL";
99-
} else if(strncmp(uri, "tel:", strlen("tel:")) == 0) {
123+
} else if(strncmp(uri, "tel:", 4) == 0) {
100124
type = "Phone";
101-
uri += strlen("tel:");
102-
uri_len -= strlen("tel:");
103-
} else if(strncmp(uri, "mailto:", strlen("mailto:")) == 0) {
125+
uri += 4;
126+
decoded_len -= 4;
127+
} else if(strncmp(uri, "mailto:", 7) == 0) {
104128
type = "Mail";
105-
uri += strlen("mailto:");
106-
uri_len -= strlen("mailto:");
129+
uri += 7;
130+
decoded_len -= 7;
107131
}
108132

109133
furi_string_cat_printf(str, "%s\n", type);
110-
print_data(str, NULL, (uint8_t*)uri, uri_len, false);
134+
print_data(str, NULL, (uint8_t*)uri, decoded_len, false);
135+
111136
free(uri_buf);
112137
}
113138

@@ -477,6 +502,6 @@ static const FlipperAppPluginDescriptor ndef_plugin_descriptor = {
477502
};
478503

479504
/* Plugin entry point - must return a pointer to const descriptor */
480-
const FlipperAppPluginDescriptor* ndef_plugin_ep() {
505+
const FlipperAppPluginDescriptor* ndef_plugin_ep(void) {
481506
return &ndef_plugin_descriptor;
482507
}

0 commit comments

Comments
 (0)