From f29e7cff6ba09c3fa31778910b68d22dabfd8dd7 Mon Sep 17 00:00:00 2001 From: Alejandro Revilla Date: Tue, 8 Apr 2014 18:27:36 -0300 Subject: [PATCH] Bugfix in IFA_BITMAP handling third bitmap --- .../main/java/org/jpos/iso/IFA_BITMAP.java | 11 ++++++--- jpos/src/main/java/org/jpos/iso/ISOUtil.java | 6 +++++ .../java/org/jpos/iso/IFA_BITMAPTest.java | 9 ++++--- .../test/java/org/jpos/iso/ISOUtilTest.java | 24 ++++++++++++++++++- 4 files changed, 41 insertions(+), 9 deletions(-) diff --git a/jpos/src/main/java/org/jpos/iso/IFA_BITMAP.java b/jpos/src/main/java/org/jpos/iso/IFA_BITMAP.java index 624bb9e7c0..10c44ccfc6 100644 --- a/jpos/src/main/java/org/jpos/iso/IFA_BITMAP.java +++ b/jpos/src/main/java/org/jpos/iso/IFA_BITMAP.java @@ -47,8 +47,11 @@ public IFA_BITMAP(int len, String description) { * @exception ISOException */ public byte[] pack (ISOComponent c) throws ISOException { - byte[] b = ISOUtil.bitSet2byte ((BitSet) c.getValue()); - return ISOUtil.hexString(b).getBytes(); + BitSet b = (BitSet) c.getValue(); + int len = + getLength() >= 8 ? + (((b.length()+62)>>6)<<3) : getLength(); + return ISOUtil.hexString(ISOUtil.bitSet2byte (b, len)).getBytes(); } public int getMaxPackedLength() { @@ -68,8 +71,10 @@ public int unpack (ISOComponent c, byte[] b, int offset) BitSet bmap = ISOUtil.hex2BitSet (b, offset, getLength() << 3); c.setValue(bmap); len = (bmap.get(1)) ? 128 : 64; /* changed by Hani */ - if (getLength() > 16 && bmap.get(65)) + if (getLength() > 16 && bmap.get(65)) { len = 192; + bmap.clear(65); + } return (len >> 2); } public void unpack (ISOComponent c, InputStream in) diff --git a/jpos/src/main/java/org/jpos/iso/ISOUtil.java b/jpos/src/main/java/org/jpos/iso/ISOUtil.java index 2f47e7dbcb..7a9a59732b 100644 --- a/jpos/src/main/java/org/jpos/iso/ISOUtil.java +++ b/jpos/src/main/java/org/jpos/iso/ISOUtil.java @@ -698,6 +698,12 @@ public static BitSet hex2BitSet (byte[] b, int offset, int maxBits) { int len = maxBits > 64? ((Character.digit((char)b[offset],16) & 0x08) == 8 ? 128 : 64) : maxBits; + if (len > 64 && maxBits > 128 && + b.length > offset+16 && + (Character.digit((char)b[offset+16],16) & 0x08) == 8) + { + len = 192; + } BitSet bmap = new BitSet (len); for (int i=0; i> 2)], 16); diff --git a/jpos/src/test/java/org/jpos/iso/IFA_BITMAPTest.java b/jpos/src/test/java/org/jpos/iso/IFA_BITMAPTest.java index e8db79e4fe..7bbfb1be61 100644 --- a/jpos/src/test/java/org/jpos/iso/IFA_BITMAPTest.java +++ b/jpos/src/test/java/org/jpos/iso/IFA_BITMAPTest.java @@ -52,10 +52,10 @@ public void setUp() { thirtytwoBytes = new IFA_BITMAP(32,"32 byte bitmap"); fortyeightBytes = new IFA_BITMAP(48,"48 byte bitmap"); - inBytes = "8F81421FF12418F18F81421FF12418F18F81421FF12418F1".getBytes(); - sixteenByteBitMapIn32Bytes = "7F81421FF12418F18F81421FF12418F18F81421FF12418F1".getBytes(); - thirtytwoByteBitMapIn48Bytes = "8F81421FF12418F17F81421FF12418F18F81421FF12418F1".getBytes(); - sixteenByteBitMapIn48Bytes = "7F81421FF12418F17F81421FF12418F18F81421FF12418F1".getBytes(); + inBytes = "8F81421FF12418F18F81421FF12418F18081421FF12418F1".getBytes(); + sixteenByteBitMapIn32Bytes = "7F81421FF12418F18F81421FF12418F18081421FF12418F1".getBytes(); + thirtytwoByteBitMapIn48Bytes = "8F81421FF12418F17F81421FF12418F18081421FF12418F1".getBytes(); + sixteenByteBitMapIn48Bytes = "7F81421FF12418F17F81421FF12418F18081421FF12418F1".getBytes(); in = ISOUtil.hexdump(inBytes); } @@ -208,5 +208,4 @@ public void setUp() { assertEquals("48 Byte (16 bytes used) bitmap pack should reflect unpack",ISOUtil.hexString(sixteenByteBitMapIn48Bytes,0,16),ISOUtil.hexString(outBytes)); } - } diff --git a/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java b/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java index d0010f45a5..e88c5ba825 100644 --- a/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java +++ b/jpos/src/test/java/org/jpos/iso/ISOUtilTest.java @@ -276,6 +276,28 @@ public void testBitSet2byte3() throws Throwable { assertEquals("result.length", 0, result.length); } + @Test + public void testBitSetByteHexInteroperability() throws Throwable { + BitSet bs = new BitSet(); + bs.set(1); + bs.set(63); + bs.set(127); + bs.set(191); + byte[] b = ISOUtil.bitSet2byte(bs); + BitSet bs1 = ISOUtil.byte2BitSet(b, 0, 192); + BitSet bs2 = ISOUtil.hex2BitSet(ISOUtil.hexString(b).getBytes(), 0, 192); + assertEquals("BitSets should be equal", bs1, bs2); + } + + @Test + public void testBitSetByteHexInteroperability2() throws Throwable { + byte[] b = ISOUtil.hex2byte("F23C04800EE0000080000000000000000000380000000000"); + BitSet bs1 = ISOUtil.byte2BitSet(b, 0, 192); + BitSet bs2 = ISOUtil.hex2BitSet (ISOUtil.hexString(b).getBytes(), 0, 192); + assertEquals("BitSets should be equal", bs1, bs2); + assertEquals("Image matches", ISOUtil.hexString(b), ISOUtil.hexString(ISOUtil.bitSet2byte(bs1))); + } + @Test(expected = NullPointerException.class) public void testBitSet2byteThrowsNullPointerException() throws Throwable { ISOUtil.bitSet2byte(null); @@ -2466,7 +2488,7 @@ public void testHex2BitSet1() throws Throwable { public void testHex2BitSet10() throws Throwable { byte[] b = new byte[80]; BitSet result = ISOUtil.hex2BitSet(b, 0, 1000); - assertEquals("result.size()", 256, result.size()); + assertEquals("result.size()", 384, result.size()); } @Test