Skip to content

Commit

Permalink
#10 Fixed Last tag from a hex string is sometimes missing after parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
gloomsinger79 authored and Evgeniy Sinev committed Sep 10, 2019
1 parent 4cb2adc commit 1d887ab
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/main/java/com/payneteasy/tlv/BerTlvParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ private ParseResult parseWithResult(int aLevel, byte[] aBuf, int aOffset, int aL
if(tag.isConstructed()) {

ArrayList<BerTlv> list = new ArrayList<BerTlv>();
addChildren(aLevel, aBuf, aOffset, levelPadding, tagBytesCount, lengthBytesCount, valueLength, list);
addChildren(aLevel, aBuf, aOffset + tagBytesCount + lengthBytesCount, levelPadding, lengthBytesCount, valueLength, list);

int resultOffset = aOffset + tagBytesCount + lengthBytesCount + valueLength;
if(log.isDebugEnabled()) {
Expand All @@ -108,20 +108,19 @@ private ParseResult parseWithResult(int aLevel, byte[] aBuf, int aOffset, int aL
* @param aBuf buffer
* @param aOffset offset (first byte)
* @param levelPadding level padding (for debug)
* @param aTagBytesCount tag bytes count
* @param aDataBytesCount data bytes count
* @param valueLength length
* @param list list to add
*/
private void addChildren(int aLevel, byte[] aBuf, int aOffset, String levelPadding, int aTagBytesCount, int aDataBytesCount, int valueLength, ArrayList<BerTlv> list) {
int startPosition = aOffset + aTagBytesCount + aDataBytesCount;
private void addChildren(int aLevel, byte[] aBuf, int aOffset, String levelPadding, int aDataBytesCount, int valueLength, ArrayList<BerTlv> list) {
int startPosition = aOffset;
int len = valueLength;
while (startPosition <= aOffset + valueLength) {
while (startPosition < aOffset + valueLength ) {
ParseResult result = parseWithResult(aLevel+1, aBuf, startPosition, len);
list.add(result.tlv);

startPosition = result.offset;
len = valueLength - startPosition;
len = (aOffset + valueLength) - startPosition;

if(log.isDebugEnabled()) {
log.debug("{}level {}: adding {} with offset {}, startPosition={}, aDataBytesCount={}, valueLength={}"
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/com/payneteasy/tlv/BerTlvParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,26 @@ public void test_empty_hex() {
BerTlvs tlvs = parser.parse(bytes, 0, bytes.length);
Assert.assertEquals(0, tlvs.getList().size());
}

@Test
public void test_issue_10_last_tag() {
String hex = "BF01820114DF0105A0000000049F2201EFDF020101DF030101DF0481F8A191CB87473F29349B5D60A88B3EAEE0973AA6F1A082F358D849FDDFF9C091F899EDA9792CAF09EF28F5D22404B88A2293EEBBC1949C43BEA4D60CFD879A1539544E09E0F09F60F065B2BF2A13ECC705F3D468B9D33AE77AD9D3F19CA40F23DCF5EB7C04DC8F69EBA565B1EBCB4686CD274785530FF6F6E9EE43AA43FDB02CE00DAEC15C7B8FD6A9B394BABA419D3F6DC85E16569BE8E76989688EFEA2DF22FF7D35C043338DEAA982A02B866DE5328519EBBCD6F03CDD686673847F84DB651AB86C28CF1462562C577B853564A290C8556D818531268D25CC98A4CC6A0BDFFFDA2DCCA3A94C998559E307FDDF915006D9A987B07DDAEB3BDF050103";
byte[] bytes = HexUtil.parseHex(hex);
BerTlvParser parser = new BerTlvParser();
BerTlvs tlvs = parser.parse(bytes, 0, bytes.length);

BerTlvLogger.log(" ", tlvs, LOG);

{
BerTlv berTlv = tlvs.find(new BerTag(0xDF, 0x05));
Assert.assertNotNull(berTlv);
Assert.assertEquals("03", berTlv.getHexValue());
}

{
Assert.assertEquals(1, tlvs.getList().size());
Assert.assertTrue(tlvs.getList().get(0).isConstructed());
Assert.assertEquals(6, tlvs.getList().get(0).getValues().size());
}
}
}

0 comments on commit 1d887ab

Please sign in to comment.