2012-07-13 18 views
6

Sono nuovo di Java, quindi mi piacerebbe utilizzare la soluzione standard per, penso, il compito standard. La lunghezza di tag e valori non è nota.Esiste un parser Java per BER-TLV?

+0

potrebbe essere questo quello che ti serve? https://github.com/VakhoQ/tlv-encoder – grep

risposta

0

Ho trovato le classi Javacard per BER TLV. Spero che questi aiuti

3

Il tutorial in here fornisce suggerimenti su come analizzare BER-TLV. Utilizzando JACCAL

+0

questo è fantastico! – null

1

ho fatto un semplice parser sulla base delle informazioni fornite qui: http://www.codeproject.com/Articles/669147/Simple-TLV-Parser

non so se questo supporto di codice tutti gli standard, ma funziona per me.

public static Map<String, String> parseTLV(String tlv) { 
    if (tlv == null || tlv.length()%2!=0) { 
     throw new RuntimeException("Invalid tlv, null or odd length"); 
    } 
    HashMap<String, String> hashMap = new HashMap<String, String>(); 
    for (int i=0; i<tlv.length();) { 
     try { 
      String key = tlv.substring(i, i=i+2); 

      if ((Integer.parseInt(key,16) & 0x1F) == 0x1F) { 
       // extra byte for TAG field 
       key += tlv.substring(i, i=i+2); 
      } 
      String len = tlv.substring(i, i=i+2); 
      int length = Integer.parseInt(len,16); 

      if (length > 127) { 
       // more than 1 byte for lenth 
       int bytesLength = length-128; 
       len = tlv.substring(i, i=i+(bytesLength*2)); 
       length = Integer.parseInt(len,16); 
      } 
      length*=2; 

      String value = tlv.substring(i, i=i+length); 
      //System.out.println(key+" = "+value); 
      hashMap.put(key, value); 
     } catch (NumberFormatException e) { 
      throw new RuntimeException("Error parsing number",e); 
     } catch (IndexOutOfBoundsException e) { 
      throw new RuntimeException("Error processing field",e); 
     } 
    } 

    return hashMap; 
} 
+0

simple-tlv è diverso da ber-tlv e un semplice parser tlv si bloccherà spesso sui dati ber-tvl –

0

È possibile utilizzare questo parser BER-TLV: source code on git o download jar.
Esempi:

come analizzare

byte[] bytes = HexUtil.parseHex("50045649534157131000023100000033D44122011003400000481F"); 
BerTlvParser parser = new BerTlvParser(LOG); 
BerTlvs tlvs = parser.parse(bytes, 0, bytes.length); 

come costruire

byte[] bytes = new BerTlvBuilder() 
       .addHex(new BerTag(0x50), "56495341") 
       .addHex(new BerTag(0x57), "1000023100000033D44122011003400000481F") 
       .buildArray(); 
0

potrebbe essere this free library può essere utile per voi. Ho usato questo per l'analisi semplice di TLV. Comunque è con la licenza MIT e puoi modificarlo.

https://github.com/VakhoQ/tlv-encoder