From e8ec1e1364cbe865c249573c446955b03b000404 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?An=C5=BEe=20Jen=C5=A1terle?= Date: Sat, 16 Nov 2024 23:21:21 +0100 Subject: Fix SSB decoder, add real life test case --- .../java/org/uic/barcode/ssbFrame/SsbClass.java | 124 ++++++++++----------- .../uic/barcode/ssbFrame/SsbCommonTicketPart.java | 24 ++-- .../java/org/uic/barcode/ssbFrame/SsbGroup.java | 30 ++--- .../uic/barcode/ssbFrame/SsbNonReservation.java | 24 ++-- .../java/org/uic/barcode/ssbFrame/SsbNonUic.java | 4 +- .../java/org/uic/barcode/ssbFrame/SsbPass.java | 48 ++++---- .../org/uic/barcode/ssbFrame/SsbReservation.java | 36 +++--- .../uic/barcode/ssbFrame/SsbStationCodeTable.java | 14 ++- .../java/org/uic/barcode/ssbFrame/SsbStations.java | 20 ++-- .../org/uic/barcode/ssbFrame/SsbTicketType.java | 5 +- 10 files changed, 170 insertions(+), 159 deletions(-) (limited to 'src/main') diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbClass.java b/src/main/java/org/uic/barcode/ssbFrame/SsbClass.java index 48f94b8..f5257d9 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbClass.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbClass.java @@ -3,68 +3,68 @@ package org.uic.barcode.ssbFrame; public enum SsbClass { NoClass, - FIRST, + First, Second, - class_3, - class_4, - class_5, - class_6, - class_7, - class_8, - class_9, - class_10, - class_11, - class_12, - class_13, - class_14, - class_15, - class_16, - class_17, - class_18, - class_19, - class_20, - class_21, - class_22, - class_23, - class_24, - class_25, - class_26, - class_27, - class_28, - class_29, - class_30, - class_31, - class_32, - class_33, - class_34, - class_35, - class_36, - class_37, - class_38, - class_39, - class_40, - class_41, - class_42, - class_43, - class_44, - class_45, - class_46, - class_47, - class_48, - class_49, - class_50, - class_51, - class_52, - class_53, - class_54, - class_55, - class_56, - class_57, - class_58, - class_59, - class_60, - class_61, - class_62, - class_63; + Class_3, + Class_4, + Class_5, + Class_6, + Class_7, + Class_8, + Class_9, + Class_A, + Class_B, + Class_C, + Class_D, + Class_E, + Class_F, + Class_G, + Class_H, + Class_I, + Class_J, + Class_K, + Class_L, + Class_M, + Class_N, + Class_O, + Class_P, + Class_Q, + Class_R, + Class_S, + Class_T, + Class_U, + Class_V, + Class_W, + Class_X, + Class_Y, + Class_Z, + Class_36, + Class_37, + Class_38, + Class_39, + Class_40, + Class_41, + Class_42, + Class_43, + Class_44, + Class_45, + Class_46, + Class_47, + Class_48, + Class_49, + Class_50, + Class_51, + Class_52, + Class_53, + Class_54, + Class_55, + Class_56, + Class_57, + Class_58, + Class_59, + Class_60, + Class_61, + Class_62, + Class_63; } diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java b/src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java index 8eef552..eee8af0 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java @@ -30,20 +30,20 @@ public abstract class SsbCommonTicketPart extends SsbTicketPart { int offset = 27; // header offset numberOfAdults = bits.getInteger(offset, 7); - offset = offset + 7; + offset += 7; numberOfChildren = bits.getInteger(offset, 7); - offset = offset + 7; + offset += 7; specimen = bits.get(offset); offset++; int classIndex = bits.getInteger(offset, 6); classCode = SsbClass.values()[classIndex]; - offset = offset + 6; + offset += 6; ticketNumber = bits.getChar6String(offset, 84); - offset = offset + 84; + offset += 84; year = bits.getInteger(offset, 4); - offset = offset + 4; + offset += 4; day = bits.getInteger(offset, 9); - offset = offset + 9; + offset += 9; return offset; } @@ -55,35 +55,35 @@ public abstract class SsbCommonTicketPart extends SsbTicketPart { throw new EncodingFormatException("SSB number of adults too big"); } bits.putInteger(offset,7, numberOfAdults); - offset = offset + 7; + offset += 7; if (numberOfChildren < 0 || numberOfChildren > 99) { throw new EncodingFormatException("SSB number of children too big"); } bits.putInteger(offset, 7, numberOfChildren); - offset = offset + 7; + offset += 7; bits.put(offset,specimen); offset++; bits.putInteger(offset, 6,classCode.ordinal()); - offset = offset + 6; + offset += 6; if (ticketNumber.length() > 14) { throw new EncodingFormatException("SSB Ticket Number too long"); } bits.putChar6String(offset, 84, ticketNumber); - offset = offset + 84; + offset += 84; bits.putInteger(offset, 4, (year % 10)); - offset = offset + 4; + offset += 4; if (day > 512) { throw new EncodingFormatException("SSB day too long"); } bits.putInteger(offset, 9, day); - offset = offset + 9; + offset += 9; return offset; diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbGroup.java b/src/main/java/org/uic/barcode/ssbFrame/SsbGroup.java index 7751ef6..18881d2 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbGroup.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbGroup.java @@ -21,32 +21,32 @@ public class SsbGroup extends SsbCommonTicketPart { @Override protected int decodeContent(byte[] bytes, int offset) { - offset = offset + decodeCommonPart(bytes); + offset = decodeCommonPart(bytes); BitBuffer bits = new ByteBitBuffer(bytes); isReturnJourney = bits.get(offset); - offset = offset++; + offset++; firstDayOfValidity = bits.getInteger(offset, 9); - offset = offset + 9; + offset += 9; lastDayOfValidity = bits.getInteger(offset, 9); - offset = offset + 9; + offset += 9; offset = stations.decode(offset, bytes); groupName = bits.getChar6String(offset, 72); - offset = offset + 72; + offset += 72; counterMarkNumber = bits.getInteger(offset, 9); - offset = offset + 9; + offset += 9; infoCode = bits.getInteger(offset, 14); - offset = offset + 14; + offset += 14; text = bits.getChar6String(offset, 144); - offset = offset + 144; + offset += 144; return offset; @@ -55,7 +55,7 @@ public class SsbGroup extends SsbCommonTicketPart { @Override protected int encodeContent(byte[] bytes, int offset) throws EncodingFormatException { - offset = offset + encodeCommonPart(bytes, offset); + offset += encodeCommonPart(bytes, offset); BitBuffer bits = new ByteBitBuffer(bytes); @@ -66,13 +66,13 @@ public class SsbGroup extends SsbCommonTicketPart { throw new EncodingFormatException("SSB first day of validity too big"); } bits.putInteger(offset, 9, firstDayOfValidity); - offset = offset + 9; + 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 += 9; offset = stations.encode(offset, bytes); @@ -80,25 +80,25 @@ public class SsbGroup extends SsbCommonTicketPart { throw new EncodingFormatException("SSB group name too big"); } bits.putChar6String(offset, 72,groupName); - offset = offset + 72; + offset += 72; if (counterMarkNumber < 0 || counterMarkNumber > 246) { throw new EncodingFormatException("SSB number of countermark too big"); } bits.putInteger(offset, 9,counterMarkNumber); - offset = offset + 9; + offset += 9; if (infoCode < 0 || infoCode > 9999) { throw new EncodingFormatException("SSB info code too big"); } bits.putInteger(offset, 14, infoCode); - offset = offset + 14; + offset += 14; if (text.length() > 24) { throw new EncodingFormatException("SSB text too big"); } bits.putChar6String(offset, 144, text); - offset = offset + 144; + offset += 144; return offset; } diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbNonReservation.java b/src/main/java/org/uic/barcode/ssbFrame/SsbNonReservation.java index 80fc2bc..329d6ea 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbNonReservation.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbNonReservation.java @@ -17,26 +17,26 @@ public class SsbNonReservation extends SsbCommonTicketPart { @Override protected int decodeContent(byte[] bytes, int offset) { - offset = offset + decodeCommonPart(bytes); + offset = decodeCommonPart(bytes); BitBuffer bits = new ByteBitBuffer(bytes); isReturnJourney = bits.get(offset); - offset = offset++; + offset++; firstDayOfValidity = bits.getInteger(offset, 9); - offset = offset + 9; + offset += 9; lastDayOfValidity = bits.getInteger(offset, 9); - offset = offset + 9; + offset += 9; offset = stations.decode(offset, bytes); infoCode = bits.getInteger(offset, 14); - offset = offset + 14; + offset += 14; text = bits.getChar6String(offset, 222); - offset = offset + 222; + offset += 222; return offset; @@ -45,24 +45,24 @@ public class SsbNonReservation extends SsbCommonTicketPart { @Override protected int encodeContent(byte[] bytes, int offset) throws EncodingFormatException { - offset = offset + encodeCommonPart(bytes, offset); + offset = encodeCommonPart(bytes, offset); BitBuffer bits = new ByteBitBuffer(bytes); bits.put(offset, isReturnJourney); - offset = 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; + 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 += 9; offset = stations.encode(offset, bytes); @@ -70,13 +70,13 @@ public class SsbNonReservation extends SsbCommonTicketPart { throw new EncodingFormatException("SSB info code too big"); } bits.putInteger(offset, 14, infoCode); - offset = offset + 14; + offset += 14; if (text.length() > 37) { throw new EncodingFormatException("SSB text too big"); } bits.putChar6String(offset, 222, text); - offset = offset + 222; + offset += 222; return offset; diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbNonUic.java b/src/main/java/org/uic/barcode/ssbFrame/SsbNonUic.java index 1f0049e..1f9a8a4 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbNonUic.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbNonUic.java @@ -20,7 +20,7 @@ public class SsbNonUic extends SsbTicketPart { for (int i = offset; i < openDataLength; i++) { - if (bits.get(i) == false) { + if (bits.get(i) == true) { sb.append("1"); } else { sb.append("0"); @@ -46,7 +46,7 @@ public class SsbNonUic extends SsbTicketPart { for (int i = 0; i< openDataLength ; i++) { - if (i < bitString.length() && bitString.charAt(i) == '0') { + if (i < bitString.length() && bitString.charAt(i) == '1') { bits.put(offset + i, true); } else { bits.put(offset + i, false); diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbPass.java b/src/main/java/org/uic/barcode/ssbFrame/SsbPass.java index a26fb61..a18acc8 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbPass.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbPass.java @@ -38,45 +38,45 @@ public class SsbPass extends SsbCommonTicketPart { @Override protected int decodeContent(byte[] bytes, int offset) { - offset = offset + decodeCommonPart(bytes); + offset = decodeCommonPart(bytes); BitBuffer bits = new ByteBitBuffer(bytes); passSubType = bits.getInteger(offset, 2); - offset = offset + 2; + offset += 2; firstDayOfValidity = bits.getInteger(offset, 9); - offset = offset + 9; + offset += 9; maximumValidityDuration = bits.getInteger(offset, 9); - offset = offset + 9; + offset += 9; numberOfTravels = bits.getInteger(offset, 7); - offset = offset + 7; + offset += 7; country_1 = bits.getInteger(offset, 7); - offset = offset + 7; + offset += 7; country_2 = bits.getInteger(offset, 7); - offset = offset + 7; + offset += 7; country_3 = bits.getInteger(offset, 7); - offset = offset + 7; + offset += 7; country_4 = bits.getInteger(offset, 7); - offset = offset + 7; + offset += 7; country_5 = bits.getInteger(offset, 7); - offset = offset + 7; + offset += 7; hasSecondPage = bits.get(offset); offset++; infoCode = bits.getInteger(offset, 14); - offset = offset + 14; + offset += 14; text = bits.getChar6String(offset, 240); - offset = offset + 240; + offset += 240; return offset; } @@ -84,7 +84,7 @@ public class SsbPass extends SsbCommonTicketPart { @Override protected int encodeContent(byte[] bytes, int offset) throws EncodingFormatException { - offset = offset + encodeCommonPart(bytes, offset); + offset = encodeCommonPart(bytes, offset); BitBuffer bits = new ByteBitBuffer(bytes); @@ -92,55 +92,55 @@ public class SsbPass extends SsbCommonTicketPart { throw new EncodingFormatException("SSB pass type too big"); } bits.putInteger(offset, 2,passSubType); - offset = offset + 2; + 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; + offset += 9; if (maximumValidityDuration < 0 || maximumValidityDuration > 511) { throw new EncodingFormatException("SSB validity duration too big"); } bits.putInteger(offset, 9,maximumValidityDuration); - offset = offset + 9; + offset += 9; if (numberOfTravels < 0 || numberOfTravels > 94) { throw new EncodingFormatException("SSB number of travels too big"); } bits.putInteger(offset, 7, numberOfTravels); - offset = offset + 7; + 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; + 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; + 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; + 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; + 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; + offset += 7; bits.put(offset, hasSecondPage); offset++; @@ -149,13 +149,13 @@ public class SsbPass extends SsbCommonTicketPart { throw new EncodingFormatException("SSB info code too big"); } bits.putInteger(offset, 14, infoCode); - offset = offset + 14; + offset += 14; if (text.length() > 40) { throw new EncodingFormatException("SSB text too big"); } bits.putChar6String(offset, 240,text); - offset = offset + 240; + offset += 240; return offset; } diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java b/src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java index c70c2d1..7496bcc 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java @@ -33,12 +33,12 @@ public class SsbReservation extends SsbCommonTicketPart { @Override protected int decodeContent(byte[] bytes, int offset) { - offset = offset + decodeCommonPart(bytes); + offset = decodeCommonPart(bytes); BitBuffer bits = new ByteBitBuffer(bytes); ticketSubType = bits.getInteger(offset, 2); - offset = offset + 2; + offset += 2; stations = new SsbStations(); offset = stations.decode(offset, bytes); @@ -55,28 +55,28 @@ public class SsbReservation extends SsbCommonTicketPart { */ departureDate = bits.getInteger(offset, 9); - offset = offset + 9; + offset += 9; departureTime = bits.getInteger(offset, 11); - offset = offset + 11; + offset += 11; train = bits.getChar6String(offset, 30); - offset = offset + 30; + offset += 30; coach = bits.getInteger(offset, 10); - offset = offset + 10; + offset += 10; place = bits.getChar6String(offset, 18); - offset = offset + 18; + offset += 18; overbooking = bits.get(offset); offset++; infoCode = bits.getInteger(offset, 14); - offset = offset + 14; + offset += 14; text = bits.getChar6String(offset, 162); - offset = offset + 162; + offset += 162; return offset; } @@ -84,7 +84,7 @@ public class SsbReservation extends SsbCommonTicketPart { @Override protected int encodeContent(byte[] bytes, int offset) throws EncodingFormatException { - offset = offset + encodeCommonPart(bytes, offset); + offset = encodeCommonPart(bytes, offset); BitBuffer bits = new ByteBitBuffer(bytes); @@ -92,7 +92,7 @@ public class SsbReservation extends SsbCommonTicketPart { throw new EncodingFormatException("SSB pass type too big"); } bits.putInteger(offset, 2,ticketSubType); - offset = offset + 2; + offset += 2; offset = stations.encode(offset, bytes); @@ -111,31 +111,31 @@ public class SsbReservation extends SsbCommonTicketPart { throw new EncodingFormatException("SSB departure date too big"); } bits.putInteger(offset, 9, departureDate); - offset = offset + 9; + offset += 9; if (departureTime < 0 || departureTime > 1440) { throw new EncodingFormatException("SSB departure time too big"); } bits.putInteger(offset, 11,departureTime); - offset = offset + 11; + offset += 11; if (train.length() > 5) { throw new EncodingFormatException("SSB train too big"); } bits.putChar6String(offset, 30,train); - offset = offset + 30; + offset += 30; if (coach < 0 || coach > 999) { throw new EncodingFormatException("SSB coach too big"); } bits.putInteger(offset, 10,coach); - offset = offset + 10; + offset += 10; if (place.length() > 3) { throw new EncodingFormatException("SSB coach too big"); } bits.putChar6String(offset, 18,place); - offset = offset + 18; + offset += 18; bits.put(offset, overbooking); offset++; @@ -144,13 +144,13 @@ public class SsbReservation extends SsbCommonTicketPart { throw new EncodingFormatException("SSB info code too big"); } bits.putInteger(offset, 14, infoCode); - offset = offset + 14; + offset += 14; if (text.length() > 27) { throw new EncodingFormatException("SSB text too big"); } bits.putChar6String(offset, 162, text); - offset = offset + 162; + offset += 162; return offset; diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbStationCodeTable.java b/src/main/java/org/uic/barcode/ssbFrame/SsbStationCodeTable.java index 8aeaf22..bc421c2 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbStationCodeTable.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbStationCodeTable.java @@ -5,5 +5,17 @@ public enum SsbStationCodeTable { UNKNOWN_0, NRT, RESERVATION, - UNKNOWN_3; + UNKNOWN_3, + UNKNOWN_4, + UNKNOWN_5, + UNKNOWN_6, + UNKNOWN_7, + UNKNOWN_8, + UNKNOWN_9, + UNKNOWN_10, + UNKNOWN_11, + UNKNOWN_12, + UNKNOWN_13, + UNKNOWN_14, + UNKNOWN_15; } diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbStations.java b/src/main/java/org/uic/barcode/ssbFrame/SsbStations.java index e3b7654..105d821 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbStations.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbStations.java @@ -45,30 +45,30 @@ public class SsbStations { throw new EncodingFormatException("SSB departure station too long"); } bits.putChar6String(offset,30, departureStationCode); - offset = offset + 30; + offset += 30; if (arrivalStationCode.length() > 6) { throw new EncodingFormatException("SSB arrival station too long"); } bits.putChar6String(offset,30, arrivalStationCode); - offset = offset + 30; + offset += 30; } else { bits.putInteger(offset, 4, codeTable.ordinal()); - offset = offset + 4; + offset += 4; 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; + offset += 28; 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; + offset += 28; } return offset; @@ -84,16 +84,16 @@ public class SsbStations { if (isAlphaNumeric) { departureStationCode = bits.getChar6String(offset,30); - offset = offset + 30; + offset += 30; arrivalStationCode = bits.getChar6String(offset,30); - offset = offset + 30; + offset += 30; } else { codeTable = SsbStationCodeTable.values()[bits.getInteger(offset, 4)]; - offset = offset + 4; + offset += 4; departureStationCode = Integer.toString(bits.getInteger(offset, 28)); - offset = offset + 28; + offset += 28; arrivalStationCode = Integer.toString(bits.getInteger(offset, 28)); - offset = offset + 28; + offset += 28; } diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbTicketType.java b/src/main/java/org/uic/barcode/ssbFrame/SsbTicketType.java index cb60a26..5b153d6 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbTicketType.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbTicketType.java @@ -1,7 +1,7 @@ package org.uic.barcode.ssbFrame; public enum SsbTicketType { - + UIC_0_UNDEFINED, UIC_1_IRT_RES_BOA, UIC_2_NRT, UIC_3_GRP, @@ -32,8 +32,7 @@ public enum SsbTicketType { NONUIC_28_BILATERAL, NONUIC_29_BILATERAL, NONUIC_30_BILATERAL, - NONUIC_31_BILATERAL, - NONUIC_32_BILATERAL; + NONUIC_31_BILATERAL; } -- cgit v1.2.3