Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add TLVList builder pattern #205

Merged
merged 12 commits into from
Feb 15, 2019
Merged

Add TLVList builder pattern #205

merged 12 commits into from
Feb 15, 2019

Conversation

demsey
Copy link
Contributor

@demsey demsey commented Feb 14, 2019

Last commit implements builder pattern for TLVList. So is possible parse/unpack TLV byte sequences that are not compliant with ISO/IEC 7816-4 BER-TLV rules. e.g: Visa Base1 Datasets (which are fixed 2 bytes long)

    TLVList tl = TLVList.createInstance()
                        .fixedLengthSize(2)
                        .build()

After that using tl is as usual: unpack, append, pack, etc.
It is also possible to create TLVList with a fixed tag size:

    TLVList tl = TLVList.createInstance()
                        .fixedTagSize(2)
                        .build()

and even mixing them together.

e.g: when fixed length size 1 is used then length is encoded i 1 byte (0..255) unlike at ISO/IEC 7816-4 only (0..127)

The changes affected only TLVList and TLVMsg (and corresponding jUnit test). They are never used in jPOS itself.

API changes:

  • remove TLVMsg()
  • remove TLVMsg.setTag(int)
  • remove TLVMsg.setValue(byte[] value)

At problems use just TLVList.append(tag, bytes)

  • change TLVList.unpack to throws only one unchecked IllegalArgumentException instead ISOException and extra undocumented BufferUnderflowException
  • deprecate TLVMsg(int, byte) - use TLVList.append(int, byte) instead.

Most of these changes were necessary to ensure consistency in the situation of the availability of various types of TLVList (which was previously not necessary)

In addition 2427a9f introduces verification of created tag identifiers.
So now it is not possible to create an illegal (in ISO/IEC 7816-4 BER-TLV mode) FF, 00, '3F', etc. tags. Using them resulted in the creation of an unparsable byte sequence.

 * Indicates throw BufferUnderflowException and IndexOutOfBoundsException
   by unpack and index ethods
 * Add missing jUnit test with BufferUnderflowException
The intention is to not promote the use of TLVMsg outside.
  * make TLVMsg immutable

No args constructor TLVMsg() creates tags with id 0x00 which is invalid as BER-TLV
id and leads to produce of an incorrect TLV encoded bytes.

Tag (TLVMsg) should be created when its id and value are known,
e.g. from TLVList by createTag(int, byte[])
Add verification tag number in accordance with ISO/IEC 7816
Now is possible to create TLV engine that is not compliant with ISO/IEC
7816-4 BER-TLV rules, but it is usable in some situations e.g. parsing
Visa Base1 Datasets (which are fixed 2 bytes long) e.g.:

  TLVList tl = TLVList.createInstance()
                      .fixedLengthSize(2)
                      .build()

After that using tl is as usual: unpack, append, pack, etc.
@ar
Copy link
Member

ar commented Feb 15, 2019

Awesome work @demsey as usual!

@ar ar merged commit 292b62b into jpos:master Feb 15, 2019
@demsey demsey deleted the tlv-builder branch February 15, 2019 10:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants