java parses TLV format data

Keywords: Attribute encoding

TLV: TLV format data refers to data consisting of Tag, Length, Value.Specific instructions are as follows:

The tag tag attribute is bit, which is represented in hexadecimal and takes up 1-2 bytes of length.For example, "9F33" is a tag tag tag that takes up two bytes."95" is a tag tag tag that takes up one byte.If the first byte of the tag label (Note: Bytes are sorted left to right, the first byte is the leftmost byte.The same is true for bit collation.)The last four bits are "1111", which means the tag takes up two bytes, such as "9F33"; otherwise it takes up one byte, such as "95".(

The attribute of subdomain length (L itself) is also bit, accounting for 1-3 bytes.The encoding rules are as follows:
(a) When the leftmost bit bit bit (bit8) of the leftmost byte of the L field is 0, the L field occupies one byte, and its next seven bits (bit7-bit1) represent the length of the subdomain value, and binary numbers are decimal numbers of the length of the subdomain value.For example, if a domain has a value of 3 bytes, its subdomain value length is expressed as "00000011".Therefore, if the length of the subdomain value is between 1 and 127 bytes, then the L field itself takes up only one byte.(
b) When the leftmost bit bit bit of the leftmost byte in the L field (bit8) is 1, the L field occupies more than one byte, then how many bytes it occupies are represented by the decimal values of the next seven bits of the leftmost byte (that is, bit7 to bit1).For example, if the leftmost byte is 10000010, the L field has two bytes after it.Decimal of its subsequent bytes
Value represents the length of the subdomain value.For example, if the L field is "10000001 1111 1111", that means the subdomain takes up 255 bytes.Therefore, if the length of the subdomain value is 127 to 255 bytes, then the L field itself needs to take up two bytes.(

The parsing code is as follows:

  1. public class Tlv {  
  2.   
  3.     /** Subdomain Tag Label*/  
  4.     private String tag;  
  5.   
  6.     /** Length of subdomain value*/  
  7.     private int length;  
  8.   
  9.     /** Subdomain Value*/  
  10.     private String value;  
  11.   
  12.     public Tlv(String tag, int length, String value) {  
  13.         this.length = length;  
  14.         this.tag = tag;  
  15.         this.value = value;  
  16.     }  
  17.   
  18.     public String getTag() {  
  19.         return tag;  
  20.     }  
  21.   
  22.     public void setTag(String tag) {  
  23.         this.tag = tag;  
  24.     }  
  25.   
  26.     public int getLength() {  
  27.         return length;  
  28.     }  
  29.   
  30.     public void setLength(int length) {  
  31.         this.length = length;  
  32.     }  
  33.   
  34.     public String getValue() {  
  35.         return value;  
  36.     }  
  37.   
  38.     public void setValue(String value) {  
  39.         this.value = value;  
  40.     }  
  41.   
  42.     @Override  
  43.     public String toString() {  
  44.         return "tag=[" + this.tag + "]," + "length=[" + this.length + "]," + "value=[" + this.value + "]";  
  45.     }  
  46. }  

TLV parsing class:

  1. /** 
  2.  * Convert string to TLV object 
  3.  *  
  4.  */  
  5. public abstract class TlvUtils {  
  6.   
  7.     /** 
  8.      * Convert a hexadecimal string to a list of TLV objects 
  9.      * @param hexString 
  10.      * @return 
  11.      */  
  12.     public static List<Tlv> builderTlvList(String hexString) {  
  13.         List<Tlv> tlvs = new ArrayList<Tlv>();  
  14.   
  15.         int position = 0;  
  16.         while (position != StringUtils.length(hexString)) {  
  17.             String _hexTag = getTag(hexString, position);  
  18.             position += _hexTag.length();  
  19.   
  20.             LPositon l_position = getLengthAndPosition(hexString, position);  
  21.             int _vl = l_position.get_vL();  
  22.   
  23.             position = l_position.get_position();  
  24.   
  25.             String _value = StringUtils.substring(hexString, position, position + _vl * 2);  
  26.   
  27.             position = position + _value.length();  
  28.   
  29.             tlvs.add(new Tlv(_hexTag, _vl, _value));  
  30.         }  
  31.         return tlvs;  
  32.     }  
  33.       
  34.     /** 
  35.      * Convert hexadecimal string to TLV object MAP 
  36.      * @param hexString 
  37.      * @return 
  38.      */  
  39.     public static Map<String, Tlv> builderTlvMap(String hexString) {  
  40.   
  41.         Map<String, Tlv> tlvs = new HashMap<String, Tlv>();  
  42.   
  43.         int position = 0;  
  44.         while (position != hexString.length()) {  
  45.             String _hexTag = getTag(hexString, position);  
  46.               
  47.             position += _hexTag.length();  
  48.               
  49.             LPositon l_position = getLengthAndPosition(hexString, position);  
  50.               
  51.             int _vl = l_position.get_vL();  
  52.             position = l_position.get_position();  
  53.             String _value = hexString.substring(position, position + _vl * 2);  
  54.             position = position + _value.length();  
  55.               
  56.             tlvs.put(_hexTag, new Tlv(_hexTag, _vl, _value));  
  57.         }  
  58.         return tlvs;  
  59.     }  
  60.       
  61.     /** 
  62.      * Return the length of the last Value 
  63.      *  
  64.      * @param hexString 
  65.      * @param position 
  66.      * @return  
  67.      */  
  68.     private static LPositon getLengthAndPosition(String hexString, int position) {  
  69.         String firstByteString = hexString.substring(position, position + 2);  
  70.         int i = Integer.parseInt(firstByteString, 16);  
  71.         String hexLength = "";  
  72.   
  73.         if (((i >>> 7) & 1) == 0) {  
  74.             hexLength = hexString.substring(position, position + 2);  
  75.             position = position + 2;  
  76.         } else {  
  77.             //When the leftmost bit is 1, get the last 7 bits.  
  78.             int _L_Len = i & 127;  
  79.             position = position + 2;  
  80.             hexLength = hexString.substring(position, position + _L_Len * 2);  
  81.             //position represents the first byte, followed by how many bytes to represent the subsequent Value  
  82.             position = position + _L_Len * 2;  
  83.         }  
  84.         return new LPositon(Integer.parseInt(hexLength, 16), position);  
  85.   
  86.     }  
  87.   
  88.     /** 
  89.      * Get subdomain Tag Tags 
  90.      *  
  91.      * @param hexString 
  92.      * @param position 
  93.      * @return 
  94.      */  
  95.     private static String getTag(String hexString, int position) {  
  96.         String firstByte = StringUtils.substring(hexString, position, position + 2);  
  97.         int i = Integer.parseInt(firstByte, 16);  
  98.         if ((i & 0x1f) == 0x1f) {  
  99.             return hexString.substring(position, position + 4);  
  100.   
  101.         } else {  
  102.             return hexString.substring(position, position + 2);  
  103.         }  
  104.     }  
  105. }  

The LPosition class is as follows:

  1. /** 
  2.  * value Length 
  3.  */  
  4. public class LPositon {  
  5.   
  6.     private int _vL;  
  7.     private int _position;  
  8.   
  9.     public LPositon(int _vL, int position) {  
  10.         this._vL = _vL;  
  11.         this._position = position;  
  12.     }  
  13.   
  14.     public int get_vL() {  
  15.         return _vL;  
  16.     }  
  17.   
  18.     public void set_vL(int _vL) {  
  19.         this._vL = _vL;  
  20.     }  
  21.   
  22.     public int get_position() {  
  23.         return _position;  
  24.     }  
  25.   
  26.     public void set_position(int _position) {  
  27.         this._position = _position;  
  28.     }  
  29. }  


Posted by buddok on Fri, 10 May 2019 20:03:57 -0700