Analysis of BER-TLV in financial system, the realization of changing, adding and deleting tag

Source: Internet
Author: User

BER-TLV is a common data exchange protocol in financial systems, TLV is short for (tag-length-value), its specific norms have a clear definition in ISO7616-4. At present, we briefly describe the TLV encoding rules. All the data below are represented in hexadecimal encoding.

Tag domain:

The first byte encoding of the tag field

Second Byte encoding of the tag field

Note that the sixth bit of the first byte is divided into basic data objects and structure data objects. Let's briefly explain the basic data objects and Structure Data Objects. Here are two simple examples. The following are all TLV-encoded data streams:

70069f3803010203

9f3803010203

We can see that the value field of the 70 tag is also composed of TLV structures.

The value range of the 9f38 tag is composed of simple data (010203) and does not need to conform to the TLV structure encoding.

Based on this principle, TLV can be designed as a multi-layer nested relationship.

For example:

Bytes

If the resolution is nested, it can be expressed as follows (indicated by indentation)

6f (TAG)

30 (LEN domain of 6f)

Values (Value Field of 6f)

84 (TAG)

0e (LEN domain of 84)

3250da-92e5359532e4444463031 (Value Field of 84)

A5 (TAG)

1e (LEN domain of A5)

Bf0c1b61194f08a0000003330101500a50109f43204445424954870101 (Value Field of A5)

Bf0c (TAG)

1B (LEN domain of bfoc)

61194f08a0000003330101500a50109f43204445424954870101 (Value Field of bfoc)

61 (TAG)

19 (LEN domain of 61)

4f08a0000003330101500a50109f43204445424954870101 (Value Field of 61)

4f (TAG)

08 (4f Len domain)

A000000333010101 (4f value field)

50 (TAG)

0a (50 Len domains)

50347f43204445424954 (Value Field of 50)

87 (TAG)

01 (LEN domain in 87)

01 (Value Field of 87)

After talking about the tag field, the following briefly describes the length (LEN) field. The length field is much simpler than the tag field, so the original ISO text can clearly explain the problem.

 

After finishing the basic structure of TLV, let's talk about the implementation of the current design. At present, there are many TLV codes implemented on the Internet, but many of them are based on resolution, and no modification is found.

Because TVL has a nested structure, a multi-layer nested TLV structure is changed, which may cause a series of TLV structure Len fields and value fields to be modified. Take the above 6ftlv structure as an example. If the underlying 87 is changed, the 50, 4f, and 84 of the simple structure do not need to be changed, it contains the TLV structure of all its nested structures and needs to be changed (6f \ A5 \ bfoc \ 61). Unfortunately, I have encountered the situation that I need to change this value multiple times, especially if the length is changed, the length of the entire TLV structure needs to be recalculated, which is painful. Therefore, the following pattern is designed.

The above is a brief introduction to TLV and some causes and consequences. below is the implementation.

========================================================== ======================================

From the analysis above, we can see that for a composite TLV structure, it can be abstracted as its value domain is its underlying TLV structure. A simple TLV mechanism can be abstracted into a non-lower TLV structure. For a TLV data string, it can be abstracted into multiple complex TLV organizations or the beginning and end of a simple TLV structure. Each TLV segment is of the same level.

This abstraction is not like a binary tree. Therefore, we should consider using a binary tree to implement this structure. Currently, the nodes on the left are defined as their brothers (at the same level ), the node on the right is his son (his subordinates). Therefore, the structure of the tree abstracted from 6f is shown as follows:

Therefore, if we need a simple structure value, we only need to put the value directly on the node. If we need a complex structure value, you only need to traverse its son in the left and right (directly connect a simple mechanism to a TLV string, and add its own tag and length to the previous string if it is a complex structure) to obtain its value.

If you want to add a TLV structure, for example, you need to insert another A5 with the value 9f380101. Before the original 84 and A5, I need to construct the A5 into a string first, then, the left subtree is connected to the new A5, and the old A5 is changed to the left subtree of the new A5, as shown in the following figure:

Of course, if it is a simple structure, it is directly the insertion of the node of the tree. I will not talk about it here.

Of course, deletion is the reverse operation of insert, which is not mentioned here. (Hey hey, you're too lazy)

Finally, the output of the program can be displayed in XML or JSON format. As the current project is selected, the column is represented in JSON format:

Before insertion:

[   {      "funccode" : 0   },   [      {         "6F" : "840E325041592E5359532E4444463031A51EBF0C1B61194F08A000000333010101500A50424F43204445424954870101",         "tlvstr" : "6F30840E325041592E5359532E4444463031A51EBF0C1B61194F08A000000333010101500A50424F43204445424954870101"      },      [         {            "84" : "325041592E5359532E4444463031",            "tlvstr" : "840E325041592E5359532E4444463031"         },         {            "A5" : "BF0C1B61194F08A000000333010101500A50424F43204445424954870101",            "tlvstr" : "A51EBF0C1B61194F08A000000333010101500A50424F43204445424954870101"         },         [            {               "BF0C" : "61194F08A000000333010101500A50424F43204445424954870101",               "tlvstr" : "BF0C1B61194F08A000000333010101500A50424F43204445424954870101"            },            [               {                  "61" : "4F08A000000333010101500A50424F43204445424954870101",                  "tlvstr" : "61194F08A000000333010101500A50424F43204445424954870101"               },               [                  {                     "4F" : "A000000333010101",                     "tlvstr" : "4F08A000000333010101"                  },                  {                     "50" : "50424F43204445424954",                     "tlvstr" : "500A50424F43204445424954"                  },                  {                     "87" : "01",                     "tlvstr" : "870101"                  }               ]            ]         ]      ]   ]]

After insertion

[   [      {         "6F" : "840E325041592E5359532E4444463031A5049F380101A51EBF0C1B61194F08A000000333010101500A50424F43204445424954870101",         "tlvstr" : "6F36840E325041592E5359532E4444463031A5049F380101A51EBF0C1B61194F08A000000333010101500A50424F43204445424954870101"      },      [         {            "84" : "325041592E5359532E4444463031",            "tlvstr" : "840E325041592E5359532E4444463031"         },         {            "A5" : "9F380101",            "tlvstr" : "A5049F380101"         },         [            {               "9F38" : "01",               "tlvstr" : "9F380101"            }         ],         {            "A5" : "BF0C1B61194F08A000000333010101500A50424F43204445424954870101",            "tlvstr" : "A51EBF0C1B61194F08A000000333010101500A50424F43204445424954870101"         },         [            {               "BF0C" : "61194F08A000000333010101500A50424F43204445424954870101",               "tlvstr" : "BF0C1B61194F08A000000333010101500A50424F43204445424954870101"            },            [               {                  "61" : "4F08A000000333010101500A50424F43204445424954870101",                  "tlvstr" : "61194F08A000000333010101500A50424F43204445424954870101"               },               [                  {                     "4F" : "A000000333010101",                     "tlvstr" : "4F08A000000333010101"                  },                  {                     "50" : "50424F43204445424954",                     "tlvstr" : "500A50424F43204445424954"                  },                  {                     "87" : "01",                     "tlvstr" : "870101"                  }               ]            ]         ]      ]   ],   {      "funccode" : 0   }]

 

Analysis of BER-TLV in financial system, the realization of changing, adding and deleting tag

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.