From 131e2acf135fd2f94209f47c811c87dfcf6930e6 Mon Sep 17 00:00:00 2001 From: Klas Lindfors Date: Wed, 25 Apr 2012 14:13:02 +0200 Subject: [PATCH] function for constructing ndef struct from uri --- ykpers.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) diff --git a/ykpers.c b/ykpers.c index 59b3454..802aa71 100644 --- a/ykpers.c +++ b/ykpers.c @@ -78,6 +78,45 @@ static const YK_CONFIG default_config2 = { 0 /* crc */ }; +/* From nfcforum-ts-rtd-uri-1.0.pdf */ +static char *ndef_identifiers[] = { + "http://www.", + "https://www.", + "http://", + "https://", + "tel:", + "mailto:", + "ftp://anonymous:anonymous@", + "ftp://ftp.", + "ftps://", + "sftp://", + "smb://", + "nfs://", + "ftp://", + "dav://", + "news:", + "telnet://", + "imap:", + "rtsp://", + "urn:", + "pop:", + "sip:", + "sips:", + "tftp:", + "btspp://", + "btl2cap://", + "btgoep://", + "tcpobex://", + "irdaobex://", + "file://", + "urn:epc:id:", + "urn:epc:tag:", + "urn:epc:pat:", + "urn:epc:raw:", + "urn:epc:", + "urn:nfc:" +}; + YKP_CONFIG *ykp_create_config(void) { YKP_CONFIG *cfg = malloc(sizeof(YKP_CONFIG)); @@ -332,6 +371,33 @@ int ykp_AES_key_from_passphrase(YKP_CONFIG *cfg, const char *passphrase, return 0; } +/* Fill in the data and len parts of the YKNDEF struct based on supplied uri. */ +int ykp_construct_ndef(YKNDEF *ndef, const char *uri) +{ + int num_identifiers = sizeof(ndef_identifiers) / sizeof(char*); + int index = 0; + for(; index < num_identifiers; index++) { + size_t len = strlen(ndef_identifiers[index]); + if(strncmp(uri, ndef_identifiers[index], len) == 0) { + uri += len; + break; + } + } + if(index > num_identifiers) { + ndef->data[0] = 0; + } else { + ndef->data[0] = index + 1; + } + size_t data_length = strlen(uri); + if(data_length + 1 > NDEF_DATA_SIZE) { + ykp_errno = YKP_EINVAL; + return 1; + } + memcpy(ndef->data + 1, uri, data_length); + ndef->len = data_length + 1; + return 0; +} + static bool vcheck_all(const YKP_CONFIG *cfg) { return true; -- 2.39.5