From: Klas Lindfors Date: Fri, 9 Nov 2012 14:08:50 +0000 (+0100) Subject: add ykp_ndef_as_text() to get the text back from YK_NDEF X-Git-Tag: v1.10.0~32 X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1f27b416ad023094ea65ad5307918054c1289139;p=yubikey-personalization add ykp_ndef_as_text() to get the text back from YK_NDEF --- diff --git a/libykpers-1.map b/libykpers-1.map index 6bdf7d2..0ea98e6 100644 --- a/libykpers-1.map +++ b/libykpers-1.map @@ -166,5 +166,6 @@ LIBYKPERS_1.10 { global: # Functions: yk_write_ndef2; + ykp_ndef_as_text; # Variables: } LIBYKPERS_1.9; diff --git a/ykpers.c b/ykpers.c index 52aee82..12ead7d 100644 --- a/ykpers.c +++ b/ykpers.c @@ -445,6 +445,43 @@ int ykp_construct_ndef_text(YK_NDEF *ndef, const char *text, const char *lang, b return 1; } +int ykp_ndef_as_text(YK_NDEF *ndef, char *text, size_t len) +{ + if(ndef->type == 'U') { + const char *part = NULL; + size_t offset = 0; + if(ndef->data[0] > 0) { + part = ndef_identifiers[ndef->data[0] - 1]; + offset = strlen(part); + } + if(offset + ndef->len - 1 > len) { + ykp_errno = YKP_EINVAL; + return 0; + } + if(part) { + memcpy(text, part, offset); + } + memcpy(text + offset, ndef->data + 1, ndef->len - 1); + text[ndef->len + offset] = 0; + return 1; + } + else if(ndef->type == 'T') { + unsigned char status = ndef->data[0]; + if(status & 0x80) + status ^= 0x80; + if(ndef->len - status - 1 > len) { + ykp_errno = YKP_EINVAL; + return 0; + } + memcpy(text, ndef->data + status + 1, ndef->len - status - 1); + text[ndef->len - status] = 0; + return 1; + } + else { + return 0; + } +} + int ykp_set_ndef_access_code(YK_NDEF *ndef, unsigned char *access_code) { if(ndef) diff --git a/ykpers.h b/ykpers.h index 9a142f0..4eeec6a 100644 --- a/ykpers.h +++ b/ykpers.h @@ -72,6 +72,7 @@ int ykp_free_ndef(YK_NDEF *ndef); int ykp_construct_ndef_uri(YK_NDEF *ndef, const char *uri); int ykp_construct_ndef_text(YK_NDEF *ndef, const char *text, const char *lang, bool isutf16); int ykp_set_ndef_access_code(YK_NDEF *ndef, unsigned char *access_code); +int ykp_ndef_as_text(YK_NDEF *ndef, char *text, size_t len); int ykp_set_access_code(YKP_CONFIG *cfg, unsigned char *access_code, size_t len); int ykp_set_fixed(YKP_CONFIG *cfg, unsigned char *fixed, size_t len);