No, you cannot expect that each tag consists of two bytes (although most tags). Tag-Length-Value (TLV) structures in EMV follow ASN.1 encoding rules (Basic Encoding Rules, BER). For more information, see the following documents:
The latter is a really good introduction that helped me get started.
The structure of a TLV (data object) consists of a tag value, a length value, and a data payload (value):
+ ----------- + ----------- + ----------- +
| Tag | Length | Value |
| (N Bytes) | (M Bytes) | (L bytes) |
+ ----------- + ----------- + ----------- +
PDOL (and any other list of data objects, DOL) contains a tag and part of the length of one or more such data objects. Similarly, PDOL-related data contains values โโof the DOs parts referenced by PDOL. Both tags and length can consist of one or more bytes.
For part of the tag, the rules are something like this (for more details, see the links above):
- If the lower 5 bits of the first byte of the tag are unique (
tag[0] & 0x01F == 0x01F ), then the tag consists of at least two bytes. - If the upper bit of the next byte of the tag is one (
tag[i] & 0x080 == 0x080 ), then the tag consists of one more byte. This is repeated for each subsequent byte.
For part of the length, the rules are approximately as follows (for more details, see the links above):
- If the upper bit of the first byte of length is zero (
length[0] & 0x080 == 0 ), the remaining seven bits encode the length value ( length[0] & 0x07F ). - If the top bit of the first byte of length is one (
length[0] & 0x080 == 0x080 ), the remaining seven bits encode the number of remaining bytes of length ( length[0] & 0x07F ). The remaining bytes represent the length value as an unsigned integer with the MSB.
source share