From 343e6a41044c0bd1ece7666d69f32f8d73497df3 Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Fri, 27 Apr 2012 08:23:57 +0200 Subject: [PATCH] change signature of ykp_construct_ndef_text() include language and encoding, follow the ndef specification for encoding text in ndef. --- tests/test_ndef_construction.c | 24 +++++++++++++++++++++--- ykpers.c | 15 +++++++++++---- ykpers.h | 2 +- ykpersonalize.c | 2 +- 4 files changed, 34 insertions(+), 9 deletions(-) diff --git a/tests/test_ndef_construction.c b/tests/test_ndef_construction.c index f591018..60ff45d 100644 --- a/tests/test_ndef_construction.c +++ b/tests/test_ndef_construction.c @@ -76,20 +76,38 @@ void _test_exact_text() { YKNDEF ndef; memset(&ndef, 0, sizeof(YKNDEF)); - char text[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; - int rc = ykp_construct_ndef_text(&ndef, text); + char text[] = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + int rc = ykp_construct_ndef_text(&ndef, text, "en", false); assert(rc == 0); assert(ndef.type == 'T'); - assert(strncmp(ndef.data, text, NDEF_DATA_SIZE) == 0); + assert(ndef.data[0] == 2); + assert(strncmp(ndef.data + 1, "en", 2) == 0); + assert(strncmp(ndef.data + 3, text, NDEF_DATA_SIZE - 3) == 0); assert(ndef.len == NDEF_DATA_SIZE); } +void _test_other_lang_text() +{ + YKNDEF ndef; + memset(&ndef, 0, sizeof(YKNDEF)); + char text[] = "aaaaaaaaaaaaaaa"; + size_t text_len = strlen(text); + int rc = ykp_construct_ndef_text(&ndef, text, "sv-SE", true); + assert(rc == 0); + assert(ndef.type == 'T'); + assert(ndef.data[0] == (0x80 & 5)); + assert(strncmp(ndef.data + 1, "sv-SE", 5) == 0); + assert(strncmp(ndef.data + 6, text, text_len) == 0); + assert(ndef.len == text_len + 6); +} + int main (void) { _test_https_uri(); _test_to_long_uri(); _test_exact_uri(); _test_exact_text(); + _test_other_lang_text(); return 0; } diff --git a/ykpers.c b/ykpers.c index f913c52..de9b36c 100644 --- a/ykpers.c +++ b/ykpers.c @@ -402,15 +402,22 @@ int ykp_construct_ndef_uri(YKNDEF *ndef, const char *uri) } /* Fill in the data and len parts of the YKNDEF struct based on supplied text. */ -int ykp_construct_ndef_text(YKNDEF *ndef, const char *text) +int ykp_construct_ndef_text(YKNDEF *ndef, const char *text, const char *lang, bool isutf16) { size_t data_length = strlen(text); - if(data_length > NDEF_DATA_SIZE) { + size_t lang_length = strlen(lang); + char status = lang_length; + if(isutf16) { + status &= 0x80; + } + if((data_length + lang_length + 1) > NDEF_DATA_SIZE) { ykp_errno = YKP_EINVAL; return 1; } - memcpy(ndef->data, text, data_length); - ndef->len = data_length; + ndef->data[0] = status; + memcpy(ndef->data + 1, lang, lang_length); + memcpy(ndef->data + lang_length + 1, text, data_length); + ndef->len = data_length + lang_length + 1; ndef->type = 'T'; return 0; } diff --git a/ykpers.h b/ykpers.h index a72a3ec..4d98ddc 100644 --- a/ykpers.h +++ b/ykpers.h @@ -68,7 +68,7 @@ int ykp_HMAC_key_from_hex(YKP_CONFIG *cfg, const char *hexkey); /* Functions for constructing the YKNDEF struct before writing it to a neo */ int ykp_construct_ndef_uri(YKNDEF *ndef, const char *uri); -int ykp_construct_ndef_text(YKNDEF *ndef, const char *text); +int ykp_construct_ndef_text(YKNDEF *ndef, const char *text, const char *lang, bool isutf16); 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); diff --git a/ykpersonalize.c b/ykpersonalize.c index c5320d2..910fb43 100644 --- a/ykpersonalize.c +++ b/ykpersonalize.c @@ -233,7 +233,7 @@ int main(int argc, char **argv) if(ndef_type == 'U') { ykp_construct_ndef_uri(&ndef, ndef_string); } else if(ndef_type == 'T') { - ykp_construct_ndef_text(&ndef, ndef_string); + ykp_construct_ndef_text(&ndef, ndef_string, "en", false); } if(use_access_code) { memcpy(ndef.curAccCode, access_code, ACC_CODE_SIZE); -- 2.39.5