NDEF Url Record Prefixes

3 minute read

Introduction

Today, we regularly spoiled by very cheap, compact memory to the point where 16 gigabytes on a microSD card the size of a postage stamp is not only unremarkable, but has become expected; however, in NFC chips, cost, small die sizes and low power usage are of critical importance. Therefore, there is very little memory on most NFC chips — an NXP MIFARE Ultralight only has 48 bytes of read/write memory. Even for a relatively simple dataset like a single URL, 48 bytes does not go very far. That means if you want to be able to encode anything useful, the data format on the tag must be extremely compact. In order to achieve this, the NDEF (NFC Data Exchange Format) protocol was developed, and it includes some interesting features to maximize its storage efficiency.

URI Records

NFC tags are often used to provide a point that users can tap with their phone to open a web link relevant to the content. Unfortunately, many links on the modern Internet are very long and would have trouble fitting on a cheap low-capacity tag. Therefore, the NDEF URI Record specification includes a single byte preceding the link that is used to specify a prefix that the parser should prepend to the content in order to recover the full link. For example, the URL https://www.youtube.com/watch?v=lXqJN1PyD0E takes up 43 bytes on its own. Once you add the 4 bytes of overhead from a minimal NDEF record, you’re up to a theoretical minimum of 47 bytes, which is dangerously close to the 48 byte limit on an NXP MIFARE Ultralight. Luckily, the NDEF standard specifies the prefix code 0x02 for “https://www.”, which allows us to reduce the content length from 47 bytes to 32 — one byte for the prefix code and 31 for “youtube.com/watch?v=lXqJN1PyD0E”. For consistency reasons, this prefix byte is always present, even when the prefix isn’t used by the URL, so if you actually tried to encode https://www.youtube.com/watch?v=lXqJN1PyD0E onto a tag without using the “https://www.” prefix code, you’d have to specify the code corresponding to no prefix, meaning that such an NDEF message would actually take up 48 bytes – exactly the limit for a MIFARE Ultralight.

How Does This Impact Me?

At the time of writing, the NDEF standard specifies 36 different prefix codes corresponding to many common protocols such as “https://”, “https://www.”, “mailto:”, and “ftp://” as well as several less well-known protocol prefixes such as “irdaobex://” and “urn:epc:raw:”. As the overhead from the prefix code is always present, it is advantageous to make use of URIs that can be abbreviated using one of the prefixes. Additionally, it should be remembered that, with proper use of the prefix code, URIs starting with “https://www.” and “https://” will actually take up the same amount of space, so abbreviating by dropping the “www.” doesn’t save any memory on the tag itself.

Luke Van Oort

Chief Developer, fan of immutable data structures and functional reactive programming. Guitar noodler and cycling enthusiast.

Share on: LinkedIn, Twitter, Google+