From 3f0e6a043d2eb7e463e7e86fcd7a4665b801c8f1 Mon Sep 17 00:00:00 2001 From: chochos Date: Thu, 14 Jun 2012 18:26:36 +0000 Subject: [PATCH] Improve parsing of invalid data, throwing more specific messages git-svn-id: https://j8583.svn.sourceforge.net/svnroot/j8583/j8583@108 db60a039-f72e-0410-b1d2-baa000846441 --- .../parse/AlphaNumericFieldParseInfo.java | 5 ++--- .../solab/iso8583/parse/AlphaParseInfo.java | 6 ++++++ .../solab/iso8583/parse/AmountParseInfo.java | 2 +- .../solab/iso8583/parse/BinaryParseInfo.java | 2 +- .../solab/iso8583/parse/Date10ParseInfo.java | 2 +- .../solab/iso8583/parse/Date4ParseInfo.java | 2 +- .../solab/iso8583/parse/DateExpParseInfo.java | 2 +- .../solab/iso8583/parse/LlbinParseInfo.java | 17 ++++++++++++----- .../solab/iso8583/parse/LllbinParseInfo.java | 16 +++++++++++----- .../solab/iso8583/parse/LllvarParseInfo.java | 19 ++++++++++++------- .../solab/iso8583/parse/LlvarParseInfo.java | 19 ++++++++++++------- .../solab/iso8583/parse/NumericParseInfo.java | 6 ++++++ .../solab/iso8583/parse/TimeParseInfo.java | 12 ++++++++---- 13 files changed, 74 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/solab/iso8583/parse/AlphaNumericFieldParseInfo.java b/src/main/java/com/solab/iso8583/parse/AlphaNumericFieldParseInfo.java index 2073881..8d3c816 100644 --- a/src/main/java/com/solab/iso8583/parse/AlphaNumericFieldParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/AlphaNumericFieldParseInfo.java @@ -37,9 +37,8 @@ public abstract class AlphaNumericFieldParseInfo extends FieldParseInfo { public IsoValue parse(byte[] buf, int pos, CustomField custom) throws ParseException, UnsupportedEncodingException { if (pos < 0) { - throw new ParseException(String.format("Invalid position %d", pos), pos); - } - if (pos+length > buf.length) { + throw new ParseException(String.format("Invalid ALPHA/NUM position %d", pos), pos); + } else if (pos+length > buf.length) { throw new ParseException(String.format("Insufficient data for %s field of length %d, pos %d", type, length, pos), pos); } diff --git a/src/main/java/com/solab/iso8583/parse/AlphaParseInfo.java b/src/main/java/com/solab/iso8583/parse/AlphaParseInfo.java index 58b51b6..d6794ca 100644 --- a/src/main/java/com/solab/iso8583/parse/AlphaParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/AlphaParseInfo.java @@ -37,6 +37,12 @@ public class AlphaParseInfo extends AlphaNumericFieldParseInfo { public IsoValue parseBinary(byte[] buf, int pos, CustomField custom) throws ParseException, UnsupportedEncodingException { + if (pos < 0) { + throw new ParseException(String.format("Invalid bin ALPHA position %d", pos), pos); + } else if (pos+length > buf.length) { + throw new ParseException(String.format("Insufficient data for bin %s field of length %d, pos %d", + type, length, pos), pos); + } if (custom == null) { return new IsoValue(type, new String(buf, pos, length, getCharacterEncoding()), length, null); } else { diff --git a/src/main/java/com/solab/iso8583/parse/AmountParseInfo.java b/src/main/java/com/solab/iso8583/parse/AmountParseInfo.java index 77c4e71..26337d7 100644 --- a/src/main/java/com/solab/iso8583/parse/AmountParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/AmountParseInfo.java @@ -38,7 +38,7 @@ public class AmountParseInfo extends FieldParseInfo { public IsoValue parse(byte[] buf, int pos, CustomField custom) throws ParseException { if (pos < 0) { - throw new ParseException(String.format("Invalid position %d", pos), pos); + throw new ParseException(String.format("Invalid AMOUNT position %d", pos), pos); } if (pos+12 > buf.length) { throw new ParseException(String.format("Insufficient data for AMOUNT field, pos %d", pos), pos); diff --git a/src/main/java/com/solab/iso8583/parse/BinaryParseInfo.java b/src/main/java/com/solab/iso8583/parse/BinaryParseInfo.java index f11a6e0..e5e3ed3 100644 --- a/src/main/java/com/solab/iso8583/parse/BinaryParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/BinaryParseInfo.java @@ -41,7 +41,7 @@ public class BinaryParseInfo extends FieldParseInfo { public IsoValue parse(byte[] buf, int pos, CustomField custom) throws ParseException, UnsupportedEncodingException { if (pos < 0) { - throw new ParseException(String.format("Invalid position %d", pos), pos); + throw new ParseException(String.format("Invalid BINARY position %d", pos), pos); } if (pos+(length*2) > buf.length) { throw new ParseException(String.format("Insufficient data for BINARY field of length %d, pos %d", diff --git a/src/main/java/com/solab/iso8583/parse/Date10ParseInfo.java b/src/main/java/com/solab/iso8583/parse/Date10ParseInfo.java index 39acedd..4fc2818 100644 --- a/src/main/java/com/solab/iso8583/parse/Date10ParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/Date10ParseInfo.java @@ -45,7 +45,7 @@ public class Date10ParseInfo extends FieldParseInfo { public IsoValue parse(byte[] buf, int pos, CustomField custom) throws ParseException { if (pos < 0) { - throw new ParseException(String.format("Invalid position %d", pos), pos); + throw new ParseException(String.format("Invalid DATE10 position %d", pos), pos); } if (pos+10 > buf.length) { throw new ParseException(String.format("Insufficient data for DATE10 field, pos %d", pos), pos); diff --git a/src/main/java/com/solab/iso8583/parse/Date4ParseInfo.java b/src/main/java/com/solab/iso8583/parse/Date4ParseInfo.java index 686e40b..cad87e3 100644 --- a/src/main/java/com/solab/iso8583/parse/Date4ParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/Date4ParseInfo.java @@ -39,7 +39,7 @@ public class Date4ParseInfo extends FieldParseInfo { @Override public IsoValue parse(byte[] buf, int pos, CustomField custom) throws ParseException { if (pos < 0) { - throw new ParseException(String.format("Invalid position %d", pos), pos); + throw new ParseException(String.format("Invalid DATE4 position %d", pos), pos); } if (pos+4 > buf.length) { throw new ParseException(String.format("Insufficient data for DATE4 field, pos %d", pos), pos); diff --git a/src/main/java/com/solab/iso8583/parse/DateExpParseInfo.java b/src/main/java/com/solab/iso8583/parse/DateExpParseInfo.java index e329ed8..a647959 100644 --- a/src/main/java/com/solab/iso8583/parse/DateExpParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/DateExpParseInfo.java @@ -39,7 +39,7 @@ public class DateExpParseInfo extends FieldParseInfo { @Override public IsoValue parse(byte[] buf, int pos, CustomField custom) throws ParseException { if (pos < 0) { - throw new ParseException(String.format("Invalid position %d", pos), pos); + throw new ParseException(String.format("Invalid DATE_EXP position %d", pos), pos); } if (pos+4 > buf.length) { throw new ParseException(String.format("Insufficient data for DATE_EXP field, pos %d", pos), pos); diff --git a/src/main/java/com/solab/iso8583/parse/LlbinParseInfo.java b/src/main/java/com/solab/iso8583/parse/LlbinParseInfo.java index f212dda..65ed3c7 100644 --- a/src/main/java/com/solab/iso8583/parse/LlbinParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/LlbinParseInfo.java @@ -39,13 +39,15 @@ public class LlbinParseInfo extends FieldParseInfo { @Override public IsoValue parse(byte[] buf, int pos, CustomField custom) throws ParseException { if (pos < 0) { - throw new ParseException(String.format("Invalid position %d", pos), pos); + throw new ParseException(String.format("Invalid LLBIN position %d", pos), pos); + } else if (pos+2 > buf.length) { + throw new ParseException("Insufficient LLBIN header", pos); } length = ((buf[pos] - 48) * 10) + (buf[pos + 1] - 48); if (length < 0) { throw new ParseException(String.format("Invalid LLBIN length %d pos %d", length, pos), pos); } - if (pos+2 > buf.length || length+pos+2 > buf.length) { + if (length+pos+2 > buf.length) { throw new ParseException(String.format("Insufficient data for LLBIN field, pos %d (LEN states '%s')", pos, new String(buf, pos, 2)), pos); } byte[] binval = length == 0 ? new byte[0] : HexCodec.hexDecode(new String(buf, pos + 2, length)); @@ -64,12 +66,17 @@ public class LlbinParseInfo extends FieldParseInfo { @Override public IsoValue parseBinary(byte[] buf, int pos, CustomField custom) throws ParseException { + if (pos < 0) { + throw new ParseException(String.format("Invalid bin LLBIN position %d", pos), pos); + } else if (pos+1 > buf.length) { + throw new ParseException("Insufficient bin LLBIN header", pos); + } length = (((buf[pos] & 0xf0) >> 4) * 10) + (buf[pos] & 0x0f); if (length < 0) { - throw new ParseException(String.format("Invalid LLBIN length %d pos %d", length, pos), pos); + throw new ParseException(String.format("Invalid bin LLBIN length %d pos %d", length, pos), pos); } - if (pos+1 > buf.length || length+pos+1 > buf.length) { - throw new ParseException(String.format("Insufficient data for LLBIN field, pos %d", pos), pos); + if (length+pos+1 > buf.length) { + throw new ParseException(String.format("Insufficient data for bin LLBIN field, pos %d", pos), pos); } byte[] _v = new byte[length]; System.arraycopy(buf, pos+1, _v, 0, length); diff --git a/src/main/java/com/solab/iso8583/parse/LllbinParseInfo.java b/src/main/java/com/solab/iso8583/parse/LllbinParseInfo.java index 210b367..57c4fee 100644 --- a/src/main/java/com/solab/iso8583/parse/LllbinParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/LllbinParseInfo.java @@ -39,7 +39,9 @@ public class LllbinParseInfo extends FieldParseInfo { @Override public IsoValue parse(byte[] buf, int pos, CustomField custom) throws ParseException { if (pos < 0) { - throw new ParseException(String.format("Invalid position %d", pos), pos); + throw new ParseException(String.format("Invalid LLLBIN position %d", pos), pos); + } else if (pos+3 > buf.length) { + throw new ParseException("Insufficient LLLBIN header", pos); } if (!(Character.isDigit(buf[pos]) && Character.isDigit(buf[pos+1]) && Character.isDigit(buf[pos+2]))) { throw new ParseException(String.format("Invalid LLLBIN length '%s' pos %d", new String(buf, pos, 3), pos), pos); @@ -47,8 +49,7 @@ public class LllbinParseInfo extends FieldParseInfo { length = ((buf[pos] - 48) * 100) + ((buf[pos + 1] - 48) * 10) + (buf[pos + 2] - 48); if (length < 0) { throw new ParseException(String.format("Invalid LLLBIN length %d pos %d", length, pos), pos); - } - if (length+pos+3 > buf.length) { + } else if (length+pos+3 > buf.length) { throw new ParseException(String.format("Insufficient data for LLLBIN field, pos %d", pos), pos); } byte[] binval = length == 0 ? new byte[0] : HexCodec.hexDecode(new String(buf, pos + 3, length)); @@ -68,12 +69,17 @@ public class LllbinParseInfo extends FieldParseInfo { @Override public IsoValue parseBinary(byte[] buf, int pos, CustomField custom) throws ParseException { + if (pos < 0) { + throw new ParseException(String.format("Invalid bin LLLBIN position %d", pos), pos); + } else if (pos+3 > buf.length) { + throw new ParseException("Insufficient bin LLLBIN header", pos); + } length = ((buf[pos] & 0x0f) * 100) + (((buf[pos + 1] & 0xf0) >> 4) * 10) + (buf[pos + 1] & 0x0f); if (length < 0) { - throw new ParseException(String.format("Invalid LLLBIN length %d pos %d", length, pos), pos); + throw new ParseException(String.format("Invalid bin LLLBIN length %d pos %d", length, pos), pos); } if (length+pos+2 > buf.length) { - throw new ParseException(String.format("Insufficient data for LLLBIN field, pos %d", pos), pos); + throw new ParseException(String.format("Insufficient data for bin LLLBIN field, pos %d", pos), pos); } byte[] _v = new byte[length]; System.arraycopy(buf, pos+2, _v, 0, length); diff --git a/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java b/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java index 1162bac..2b5b438 100644 --- a/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/LllvarParseInfo.java @@ -39,7 +39,9 @@ public class LllvarParseInfo extends FieldParseInfo { public IsoValue parse(byte[] buf, int pos, CustomField custom) throws ParseException, UnsupportedEncodingException { if (pos < 0) { - throw new ParseException(String.format("Invalid position %d", pos), pos); + throw new ParseException(String.format("Invalid LLLVAR position %d", pos), pos); + } else if (pos+3 > buf.length) { + throw new ParseException(String.format("Insufficient data for LLLVAR header, pos %d", pos), pos); } if (!(Character.isDigit(buf[pos]) && Character.isDigit(buf[pos+1]) && Character.isDigit(buf[pos+2]))) { throw new ParseException(String.format("Invalid LLLVAR length '%s' pos %d", @@ -48,8 +50,7 @@ public class LllvarParseInfo extends FieldParseInfo { length = ((buf[pos] - 48) * 100) + ((buf[pos + 1] - 48) * 10) + (buf[pos + 2] - 48); if (length < 0) { throw new ParseException(String.format("Invalid LLLVAR length %d pos %d", length, pos), pos); - } - if (length+pos+3 > buf.length) { + } else if (length+pos+3 > buf.length) { throw new ParseException(String.format("Insufficient data for LLLVAR field, pos %d", pos), pos); } String _v = length == 0 ? "" : new String(buf, pos + 3, length, getCharacterEncoding()); @@ -70,12 +71,16 @@ public class LllvarParseInfo extends FieldParseInfo { public IsoValue parseBinary(byte[] buf, int pos, CustomField custom) throws ParseException, UnsupportedEncodingException { + if (pos < 0) { + throw new ParseException(String.format("Invalid bin LLLVAR position %d", pos), pos); + } else if (pos+3 > buf.length) { + throw new ParseException(String.format("Insufficient data for bin LLLVAR header, pos %d", pos), pos); + } length = ((buf[pos] & 0x0f) * 100) + (((buf[pos + 1] & 0xf0) >> 4) * 10) + (buf[pos + 1] & 0x0f); if (length < 0) { - throw new ParseException(String.format("Invalid LLLVAR length %d pos %d", length, pos), pos); - } - if (length+pos+2 > buf.length) { - throw new ParseException(String.format("Insufficient data for LLLVAR field, pos %d", pos), pos); + throw new ParseException(String.format("Invalid bin LLLVAR length %d pos %d", length, pos), pos); + } else if (length+pos+2 > buf.length) { + throw new ParseException(String.format("Insufficient data for bin LLLVAR field, pos %d", pos), pos); } if (custom == null) { return new IsoValue(type, new String(buf, pos + 2, length, getCharacterEncoding()), null); diff --git a/src/main/java/com/solab/iso8583/parse/LlvarParseInfo.java b/src/main/java/com/solab/iso8583/parse/LlvarParseInfo.java index 435f6f6..c7cdc21 100644 --- a/src/main/java/com/solab/iso8583/parse/LlvarParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/LlvarParseInfo.java @@ -38,13 +38,14 @@ public class LlvarParseInfo extends FieldParseInfo { public IsoValue parse(byte[] buf, int pos, CustomField custom) throws ParseException, UnsupportedEncodingException { if (pos < 0) { - throw new ParseException(String.format("Invalid position %d", pos), pos); + throw new ParseException(String.format("Invalid LLVAR position %d", pos), pos); + } else if (pos+2 > buf.length) { + throw new ParseException(String.format("Insufficient data for LLVAR header, pos %d", pos), pos); } length = ((buf[pos] - 48) * 10) + (buf[pos + 1] - 48); if (length < 0) { throw new ParseException(String.format("Invalid LLVAR length %d pos %d", length, pos), pos); - } - if (pos+2 > buf.length || length+pos+2 > buf.length) { + } else if (length+pos+2 > buf.length) { throw new ParseException(String.format("Insufficient data for LLVAR field, pos %d", pos), pos); } String _v = length == 0 ? "" : new String(buf, pos + 2, length, getCharacterEncoding()); @@ -68,13 +69,17 @@ public class LlvarParseInfo extends FieldParseInfo { public IsoValue parseBinary(byte[] buf, int pos, CustomField custom) throws ParseException, UnsupportedEncodingException { - + if (pos < 0) { + throw new ParseException(String.format("Invalid bin LLVAR position %d", pos), pos); + } else if (pos+1 > buf.length) { + throw new ParseException(String.format("Insufficient data for bin LLVAR header, pos %d", pos), pos); + } length = (((buf[pos] & 0xf0) >> 4) * 10) + (buf[pos] & 0x0f); if (length < 0) { - throw new ParseException(String.format("Invalid LLVAR length %d pos %d", length, pos), pos); + throw new ParseException(String.format("Invalid bin LLVAR length %d pos %d", length, pos), pos); } - if (pos+1 > buf.length || length+pos+1 > buf.length) { - throw new ParseException(String.format("Insufficient data for LLVAR field, pos %d", pos), pos); + if (length+pos+1 > buf.length) { + throw new ParseException(String.format("Insufficient data for bin LLVAR field, pos %d", pos), pos); } if (custom == null) { return new IsoValue(type, new String(buf, pos + 1, length, getCharacterEncoding()), null); diff --git a/src/main/java/com/solab/iso8583/parse/NumericParseInfo.java b/src/main/java/com/solab/iso8583/parse/NumericParseInfo.java index 8824b2f..e7c36f1 100644 --- a/src/main/java/com/solab/iso8583/parse/NumericParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/NumericParseInfo.java @@ -36,6 +36,12 @@ public class NumericParseInfo extends AlphaNumericFieldParseInfo { } public IsoValue parseBinary(byte[] buf, int pos, CustomField custom) throws ParseException { + if (pos < 0) { + throw new ParseException(String.format("Invalid bin NUMERIC position %d", pos), pos); + } else if (pos+length > buf.length) { + throw new ParseException(String.format("Insufficient data for bin %s field of length %d, pos %d", + type, length, pos), pos); + } //A long covers up to 18 digits if (length < 19) { long l = 0; diff --git a/src/main/java/com/solab/iso8583/parse/TimeParseInfo.java b/src/main/java/com/solab/iso8583/parse/TimeParseInfo.java index d1c7ae7..d5ac5ad 100644 --- a/src/main/java/com/solab/iso8583/parse/TimeParseInfo.java +++ b/src/main/java/com/solab/iso8583/parse/TimeParseInfo.java @@ -40,9 +40,8 @@ public class TimeParseInfo extends FieldParseInfo { @Override public IsoValue parse(byte[] buf, int pos, CustomField custom) throws ParseException { if (pos < 0) { - throw new ParseException(String.format("Invalid position %d", pos), pos); - } - if (pos+6 > buf.length) { + throw new ParseException(String.format("Invalid TIME position %d", pos), pos); + } else if (pos+6 > buf.length) { throw new ParseException(String.format("Insufficient data for TIME field, pos %d", pos), pos); } Calendar cal = Calendar.getInstance(); @@ -54,9 +53,14 @@ public class TimeParseInfo extends FieldParseInfo { @Override public IsoValue parseBinary(byte[] buf, int pos, CustomField custom) throws ParseException { + if (pos < 0) { + throw new ParseException(String.format("Invalid bin TIME position %d", pos), pos); + } else if (pos+3 > buf.length) { + throw new ParseException(String.format("Insufficient data for bin TIME field, pos %d", pos), pos); + } int[] tens = new int[3]; int start = 0; - for (int i = pos; i < pos + tens.length; i++) { + for (int i = pos; i < pos + 3; i++) { tens[start++] = (((buf[i] & 0xf0) >> 4) * 10) + (buf[i] & 0x0f); } Calendar cal = Calendar.getInstance();