From f1a08e7fb82e813ce6985460cc2606fc7b19ae13 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 14 Mar 2023 10:15:31 +0100 Subject: SSB encoding format validation --- .../uic/barcode/ssbFrame/SsbCommonTicketPart.java | 28 ++++++++++++++--- .../java/org/uic/barcode/ssbFrame/SsbGroup.java | 21 ++++++++++++- .../java/org/uic/barcode/ssbFrame/SsbHeader.java | 18 ++++++++++- .../uic/barcode/ssbFrame/SsbNonReservation.java | 15 ++++++++- .../java/org/uic/barcode/ssbFrame/SsbPass.java | 36 +++++++++++++++++++++- .../org/uic/barcode/ssbFrame/SsbReservation.java | 27 +++++++++++++++- .../java/org/uic/barcode/ssbFrame/SsbStations.java | 26 +++++++++++++--- .../org/uic/barcode/ssbFrame/SsbTicketPart.java | 2 +- 8 files changed, 158 insertions(+), 15 deletions(-) diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java b/src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java index 0a158f3..8eef552 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java @@ -2,6 +2,7 @@ package org.uic.barcode.ssbFrame; import org.uic.barcode.asn1.uper.BitBuffer; import org.uic.barcode.asn1.uper.ByteBitBuffer; +import org.uic.barcode.ticket.EncodingFormatException; public abstract class SsbCommonTicketPart extends SsbTicketPart { @@ -46,22 +47,41 @@ public abstract class SsbCommonTicketPart extends SsbTicketPart { return offset; } - protected int encodeCommonPart(byte[] bytes, int offset) { + protected int encodeCommonPart(byte[] bytes, int offset) throws EncodingFormatException { BitBuffer bits = new ByteBitBuffer(bytes); - + + if (numberOfAdults < 0 || numberOfAdults > 99) { + throw new EncodingFormatException("SSB number of adults too big"); + } bits.putInteger(offset,7, numberOfAdults); offset = offset + 7; + + if (numberOfChildren < 0 || numberOfChildren > 99) { + throw new EncodingFormatException("SSB number of children too big"); + } bits.putInteger(offset, 7, numberOfChildren); offset = offset + 7; + bits.put(offset,specimen); offset++; + bits.putInteger(offset, 6,classCode.ordinal()); offset = offset + 6; + + if (ticketNumber.length() > 14) { + throw new EncodingFormatException("SSB Ticket Number too long"); + } bits.putChar6String(offset, 84, ticketNumber); - offset = offset + 84; - bits.putInteger(offset, 4, year); + offset = offset + 84; + + + bits.putInteger(offset, 4, (year % 10)); offset = offset + 4; + + if (day > 512) { + throw new EncodingFormatException("SSB day too long"); + } bits.putInteger(offset, 9, day); offset = offset + 9; diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbGroup.java b/src/main/java/org/uic/barcode/ssbFrame/SsbGroup.java index 1b2f6e7..7751ef6 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbGroup.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbGroup.java @@ -2,6 +2,7 @@ package org.uic.barcode.ssbFrame; import org.uic.barcode.asn1.uper.BitBuffer; import org.uic.barcode.asn1.uper.ByteBitBuffer; +import org.uic.barcode.ticket.EncodingFormatException; public class SsbGroup extends SsbCommonTicketPart { @@ -52,7 +53,7 @@ public class SsbGroup extends SsbCommonTicketPart { } @Override - protected int encodeContent(byte[] bytes, int offset) { + protected int encodeContent(byte[] bytes, int offset) throws EncodingFormatException { offset = offset + encodeCommonPart(bytes, offset); @@ -61,23 +62,41 @@ public class SsbGroup extends SsbCommonTicketPart { bits.put(offset, isReturnJourney); offset = offset++; + if (firstDayOfValidity < 0 || firstDayOfValidity > 511) { + throw new EncodingFormatException("SSB first day of validity too big"); + } bits.putInteger(offset, 9, firstDayOfValidity); offset = offset + 9; + if (lastDayOfValidity < 0 || lastDayOfValidity > 511) { + throw new EncodingFormatException("SSB last day of validity too big"); + } bits.putInteger(offset, 9, lastDayOfValidity); offset = offset + 9; offset = stations.encode(offset, bytes); + if (groupName.length() > 12) { + throw new EncodingFormatException("SSB group name too big"); + } bits.putChar6String(offset, 72,groupName); offset = offset + 72; + if (counterMarkNumber < 0 || counterMarkNumber > 246) { + throw new EncodingFormatException("SSB number of countermark too big"); + } bits.putInteger(offset, 9,counterMarkNumber); offset = offset + 9; + if (infoCode < 0 || infoCode > 9999) { + throw new EncodingFormatException("SSB info code too big"); + } bits.putInteger(offset, 14, infoCode); offset = offset + 14; + if (text.length() > 24) { + throw new EncodingFormatException("SSB text too big"); + } bits.putChar6String(offset, 144, text); offset = offset + 144; diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbHeader.java b/src/main/java/org/uic/barcode/ssbFrame/SsbHeader.java index 2ea4a51..48c8eaf 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbHeader.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbHeader.java @@ -2,6 +2,7 @@ package org.uic.barcode.ssbFrame; import org.uic.barcode.asn1.uper.BitBuffer; import org.uic.barcode.asn1.uper.ByteBitBuffer; +import org.uic.barcode.ticket.EncodingFormatException; public class SsbHeader extends SsbTicketPart { @@ -40,13 +41,28 @@ public class SsbHeader extends SsbTicketPart { } - public int encodeContent(byte[] bytes, int offset) { + public int encodeContent(byte[] bytes, int offset) throws EncodingFormatException { BitBuffer bits = new ByteBitBuffer(bytes); + if (version < 0 || version > 15) { + throw new EncodingFormatException("SSB Version too big"); + } + bits.putInteger(0, 4, version); + + if (issuer < 0 || issuer > 9999) { + throw new EncodingFormatException("SSB Issuer code too big"); + } + bits.putInteger(4, 14, issuer); + + if (keyId < 0 || keyId > 15) { + throw new EncodingFormatException("SSB Key Id too big"); + } + bits.putInteger(18, 4, keyId); + bits.putInteger(22, 5, ticketType.ordinal()); return 4 + 14 + 4 + 5; diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbNonReservation.java b/src/main/java/org/uic/barcode/ssbFrame/SsbNonReservation.java index a1fb3ae..80fc2bc 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbNonReservation.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbNonReservation.java @@ -2,6 +2,7 @@ package org.uic.barcode.ssbFrame; import org.uic.barcode.asn1.uper.BitBuffer; import org.uic.barcode.asn1.uper.ByteBitBuffer; +import org.uic.barcode.ticket.EncodingFormatException; public class SsbNonReservation extends SsbCommonTicketPart { @@ -42,7 +43,7 @@ public class SsbNonReservation extends SsbCommonTicketPart { } @Override - protected int encodeContent(byte[] bytes, int offset) { + protected int encodeContent(byte[] bytes, int offset) throws EncodingFormatException { offset = offset + encodeCommonPart(bytes, offset); @@ -51,17 +52,29 @@ public class SsbNonReservation extends SsbCommonTicketPart { bits.put(offset, isReturnJourney); offset = offset++; + if (firstDayOfValidity < 0 || firstDayOfValidity > 511) { + throw new EncodingFormatException("SSB first day of validity too big"); + } bits.putInteger(offset, 9, firstDayOfValidity); offset = offset + 9; + if (lastDayOfValidity < 0 || lastDayOfValidity > 511) { + throw new EncodingFormatException("SSB last day of validity too big"); + } bits.putInteger(offset, 9, lastDayOfValidity); offset = offset + 9; offset = stations.encode(offset, bytes); + if (infoCode < 0 || infoCode > 9999) { + throw new EncodingFormatException("SSB info code too big"); + } bits.putInteger(offset, 14, infoCode); offset = offset + 14; + if (text.length() > 37) { + throw new EncodingFormatException("SSB text too big"); + } bits.putChar6String(offset, 222, text); offset = offset + 222; diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbPass.java b/src/main/java/org/uic/barcode/ssbFrame/SsbPass.java index 3598efd..a26fb61 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbPass.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbPass.java @@ -2,6 +2,7 @@ package org.uic.barcode.ssbFrame; import org.uic.barcode.asn1.uper.BitBuffer; import org.uic.barcode.asn1.uper.ByteBitBuffer; +import org.uic.barcode.ticket.EncodingFormatException; public class SsbPass extends SsbCommonTicketPart { @@ -81,45 +82,78 @@ public class SsbPass extends SsbCommonTicketPart { } @Override - protected int encodeContent(byte[] bytes, int offset) { + protected int encodeContent(byte[] bytes, int offset) throws EncodingFormatException { offset = offset + encodeCommonPart(bytes, offset); BitBuffer bits = new ByteBitBuffer(bytes); + if (passSubType < 0 || passSubType > 3) { + throw new EncodingFormatException("SSB pass type too big"); + } bits.putInteger(offset, 2,passSubType); offset = offset + 2; + if (firstDayOfValidity < 0 || firstDayOfValidity > 511) { + throw new EncodingFormatException("SSB first day of validity too big"); + } bits.putInteger(offset, 9,firstDayOfValidity); offset = offset + 9; + if (maximumValidityDuration < 0 || maximumValidityDuration > 511) { + throw new EncodingFormatException("SSB validity duration too big"); + } bits.putInteger(offset, 9,maximumValidityDuration); offset = offset + 9; + if (numberOfTravels < 0 || numberOfTravels > 94) { + throw new EncodingFormatException("SSB number of travels too big"); + } bits.putInteger(offset, 7, numberOfTravels); offset = offset + 7; + if (country_1 < 0 || country_1 > 100) { + throw new EncodingFormatException("SSB country 1 too big"); + } bits.putInteger(offset, 7,country_1); offset = offset + 7; + if (country_2 < 0 || country_2 > 99) { + throw new EncodingFormatException("SSB country 2 too big"); + } bits.putInteger(offset, 7,country_2); offset = offset + 7; + if (country_3 < 0 || country_3 > 99) { + throw new EncodingFormatException("SSB country 3 too big"); + } bits.putInteger(offset, 7,country_3); offset = offset + 7; + if (country_4 < 0 || country_4 > 99) { + throw new EncodingFormatException("SSB country 4 too big"); + } bits.putInteger(offset, 7,country_4); offset = offset + 7; + if (country_5 < 0 || country_5 > 99) { + throw new EncodingFormatException("SSB country 5 too big"); + } bits.putInteger(offset, 7,country_5); offset = offset + 7; bits.put(offset, hasSecondPage); offset++; + if (infoCode < 0 || infoCode > 9999) { + throw new EncodingFormatException("SSB info code too big"); + } bits.putInteger(offset, 14, infoCode); offset = offset + 14; + if (text.length() > 40) { + throw new EncodingFormatException("SSB text too big"); + } bits.putChar6String(offset, 240,text); offset = offset + 240; diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java b/src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java index 73017d7..c70c2d1 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java @@ -2,6 +2,7 @@ package org.uic.barcode.ssbFrame; import org.uic.barcode.asn1.uper.BitBuffer; import org.uic.barcode.asn1.uper.ByteBitBuffer; +import org.uic.barcode.ticket.EncodingFormatException; public class SsbReservation extends SsbCommonTicketPart { @@ -81,12 +82,15 @@ public class SsbReservation extends SsbCommonTicketPart { } @Override - protected int encodeContent(byte[] bytes, int offset) { + protected int encodeContent(byte[] bytes, int offset) throws EncodingFormatException { offset = offset + encodeCommonPart(bytes, offset); BitBuffer bits = new ByteBitBuffer(bytes); + if (ticketSubType < 0 || ticketSubType > 3) { + throw new EncodingFormatException("SSB pass type too big"); + } bits.putInteger(offset, 2,ticketSubType); offset = offset + 2; @@ -103,27 +107,48 @@ public class SsbReservation extends SsbCommonTicketPart { Open Tekst 6 Bit ASCII (27 Car) 162,000 */ + if (departureDate < 0 || departureDate > 512) { + throw new EncodingFormatException("SSB departure date too big"); + } bits.putInteger(offset, 9, departureDate); offset = offset + 9; + if (departureTime < 0 || departureTime > 1440) { + throw new EncodingFormatException("SSB departure time too big"); + } bits.putInteger(offset, 11,departureTime); offset = offset + 11; + if (train.length() > 5) { + throw new EncodingFormatException("SSB train too big"); + } bits.putChar6String(offset, 30,train); offset = offset + 30; + if (coach < 0 || coach > 999) { + throw new EncodingFormatException("SSB coach too big"); + } bits.putInteger(offset, 10,coach); offset = offset + 10; + if (place.length() > 3) { + throw new EncodingFormatException("SSB coach too big"); + } bits.putChar6String(offset, 18,place); offset = offset + 18; bits.put(offset, overbooking); offset++; + if (infoCode < 0 || infoCode > 9999) { + throw new EncodingFormatException("SSB info code too big"); + } bits.putInteger(offset, 14, infoCode); offset = offset + 14; + if (text.length() > 27) { + throw new EncodingFormatException("SSB text too big"); + } bits.putChar6String(offset, 162, text); offset = offset + 162; diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbStations.java b/src/main/java/org/uic/barcode/ssbFrame/SsbStations.java index 34fbbc3..e3b7654 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbStations.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbStations.java @@ -2,6 +2,7 @@ package org.uic.barcode.ssbFrame; import org.uic.barcode.asn1.uper.BitBuffer; import org.uic.barcode.asn1.uper.ByteBitBuffer; +import org.uic.barcode.ticket.EncodingFormatException; public class SsbStations { @@ -23,9 +24,7 @@ public class SsbStations { protected String departureStationCode = " "; protected SsbStationCodeTable codeTable = SsbStationCodeTable.NRT; - - - public int encode(int offset, byte[] bytes) { + public int encode(int offset, byte[] bytes) throws EncodingFormatException { boolean isAlphaNumeric = false; @@ -42,16 +41,33 @@ public class SsbStations { offset++; if (isAlphaNumeric) { + if (departureStationCode.length() > 6) { + throw new EncodingFormatException("SSB departure station too long"); + } bits.putChar6String(offset,30, departureStationCode); offset = offset + 30; + + if (arrivalStationCode.length() > 6) { + throw new EncodingFormatException("SSB arrival station too long"); + } bits.putChar6String(offset,30, arrivalStationCode); offset = offset + 30; } else { bits.putInteger(offset, 4, codeTable.ordinal()); offset = offset + 4; - bits.putInteger(offset, 28, Integer.parseInt(departureStationCode)); + + int stationCode = Integer.parseInt(departureStationCode); + if (stationCode < 0 || stationCode > 9999999) { + throw new EncodingFormatException("SSB departure station code too long"); + } + bits.putInteger(offset, 28, stationCode); offset = offset + 28; - bits.putInteger(offset, 28, Integer.parseInt(arrivalStationCode)); + + stationCode = Integer.parseInt(arrivalStationCode); + if (stationCode < 0 || stationCode > 9999999) { + throw new EncodingFormatException("SSB arrival station code too long"); + } + bits.putInteger(offset, 28, stationCode); offset = offset + 28; } diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbTicketPart.java b/src/main/java/org/uic/barcode/ssbFrame/SsbTicketPart.java index 5583e66..717608a 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbTicketPart.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbTicketPart.java @@ -22,7 +22,7 @@ public abstract class SsbTicketPart { encodeContent(bytes, 0); } - protected abstract int encodeContent(byte[] bytes, int offset); + protected abstract int encodeContent(byte[] bytes, int offset) throws EncodingFormatException; -- cgit v1.2.3