From fc6a4042f7fca1828f0b8e267cfd660e6fe1d40a Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Tue, 22 Nov 2022 14:04:27 +0100 Subject: support for SSB barcodes SSB frame implenmentation including decoding, encoding, signing and verification --- .../org/uic/barcode/ssbFrame/SsbReservation.java | 208 +++++++++++++++++++++ 1 file changed, 208 insertions(+) create mode 100644 src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java (limited to 'src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java') diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java b/src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java new file mode 100644 index 0000000..c7f520d --- /dev/null +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java @@ -0,0 +1,208 @@ +package org.uic.barcode.ssbFrame; + +import org.uic.barcode.asn1.uper.BitBuffer; +import org.uic.barcode.asn1.uper.ByteBitBuffer; + +public class SsbReservation extends SsbCommonTicketPart { + + private SsbStations stations = new SsbStations(); + + private int ticketSubType = 0; + + private int departureDate = 0; + + private int departureTime = 0; + + private String train = null; + + private int coach = 0; + + private String place = null; + + private boolean overbooking = false; + + private int infoCode = 0; + + private String text = null; + + + + + + @Override + protected void decodeContent(byte[] bytes) { + + int offset = decodeCommonPart(bytes); + + BitBuffer bits = new ByteBitBuffer(bytes); + + ticketSubType = bits.getInteger(offset, 2); + offset = offset + 4; + + stations = new SsbStations(); + stations.decode(offset, bytes); + + /* + * Departure date : First day of validity from the issuing date Num (<367) 9,000 + Departure Time Num (<1440) 11,000 + Train number AlphaNum + 5 Car 30,000 + Coach number Num (< 999) 10,000 + Seat/berth number 3 AlphaNum 18,000 + Overbooking indicator Bit Flag 1,000 + Information Messages Num (< 9999) 14,000 + Open Tekst 6 Bit ASCII (27 Car) 162,000 + */ + + departureDate = bits.getInteger(offset, 9); + offset = offset + 9; + + departureTime = bits.getInteger(offset, 11); + offset = offset + 11; + + train = bits.getChar6String(offset, 30); + offset = offset +30; + + coach = bits.getInteger(offset, 10); + offset = offset + 10; + + place = bits.getChar6String(offset, 18); + offset = offset + 18; + + overbooking = bits.get(offset); + offset++; + + infoCode = bits.getInteger(offset, 14); + offset = offset + 14; + + text = bits.getChar6String(offset, 162); + offset = offset + 162; + } + + @Override + protected void encodeContent(byte[] bytes) { + + int offset = encodeCommonPart(bytes); + + BitBuffer bits = new ByteBitBuffer(bytes); + + bits.putInteger(offset, 2,ticketSubType); + offset = offset + 4; + + offset = stations.encode(offset, bytes); + + /* + * Departure date : First day of validity from the issuing date Num (<367) 9,000 + Departure Time Num (<1440) 11,000 + Train number AlphaNum + 5 Car 30,000 + Coach number Num (< 999) 10,000 + Seat/berth number 3 AlphaNum 18,000 + Overbooking indicator Bit Flag 1,000 + Information Messages Num (< 9999) 14,000 + Open Tekst 6 Bit ASCII (27 Car) 162,000 + */ + + bits.putInteger(offset, 9, departureDate); + offset = offset + 9; + + bits.putInteger(offset, 11,departureTime); + offset = offset + 11; + + bits.putChar6String(offset, 30,train); + offset = offset +30; + + bits.putInteger(offset, 10,coach); + offset = offset + 10; + + bits.putChar6String(offset, 18,place); + offset = offset + 18; + + bits.put(offset, overbooking); + offset++; + + bits.putInteger(offset, 14, infoCode); + offset = offset + 14; + + bits.putChar6String(offset, 162, text); + offset = offset + 162; + + } + + public SsbStations getStations() { + return stations; + } + + public int getTicketSubType() { + return ticketSubType; + } + + public void setTicketSubType(int ticketSubType) { + this.ticketSubType = ticketSubType; + } + + public int getDepartureDate() { + return departureDate; + } + + public void setDepartureDate(int departureDate) { + this.departureDate = departureDate; + } + + public int getDepartureTime() { + return departureTime; + } + + public void setDepartureTime(int departureTime) { + this.departureTime = departureTime; + } + + public String getTrain() { + return train; + } + + public void setTrain(String train) { + this.train = train; + } + + public int getCoach() { + return coach; + } + + public void setCoach(int coach) { + this.coach = coach; + } + + public String getPlace() { + return place; + } + + public void setPlace(String place) { + this.place = place; + } + + public boolean isOverbooking() { + return overbooking; + } + + public void setOverbooking(boolean overbooking) { + this.overbooking = overbooking; + } + + public int getInfoCode() { + return infoCode; + } + + public void setInfoCode(int infoCode) { + this.infoCode = infoCode; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + + +} -- cgit v1.2.3 From 7ec06722923d96d2e51300bafb44b920ca341d58 Mon Sep 17 00:00:00 2001 From: CGantert345 <57003061+CGantert345@users.noreply.github.com> Date: Fri, 10 Mar 2023 16:49:58 +0100 Subject: ssb unit tests --- .../org/uic/barcode/ssbFrame/SsbReservation.java | 24 +++++++++++++--------- 1 file changed, 14 insertions(+), 10 deletions(-) (limited to 'src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java') diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java b/src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java index c7f520d..73017d7 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java @@ -30,17 +30,17 @@ public class SsbReservation extends SsbCommonTicketPart { @Override - protected void decodeContent(byte[] bytes) { + protected int decodeContent(byte[] bytes, int offset) { - int offset = decodeCommonPart(bytes); + offset = offset + decodeCommonPart(bytes); BitBuffer bits = new ByteBitBuffer(bytes); ticketSubType = bits.getInteger(offset, 2); - offset = offset + 4; + offset = offset + 2; stations = new SsbStations(); - stations.decode(offset, bytes); + offset = stations.decode(offset, bytes); /* * Departure date : First day of validity from the issuing date Num (<367) 9,000 @@ -60,7 +60,7 @@ public class SsbReservation extends SsbCommonTicketPart { offset = offset + 11; train = bits.getChar6String(offset, 30); - offset = offset +30; + offset = offset + 30; coach = bits.getInteger(offset, 10); offset = offset + 10; @@ -76,17 +76,19 @@ public class SsbReservation extends SsbCommonTicketPart { text = bits.getChar6String(offset, 162); offset = offset + 162; + + return offset; } @Override - protected void encodeContent(byte[] bytes) { + protected int encodeContent(byte[] bytes, int offset) { - int offset = encodeCommonPart(bytes); + offset = offset + encodeCommonPart(bytes, offset); BitBuffer bits = new ByteBitBuffer(bytes); bits.putInteger(offset, 2,ticketSubType); - offset = offset + 4; + offset = offset + 2; offset = stations.encode(offset, bytes); @@ -108,7 +110,7 @@ public class SsbReservation extends SsbCommonTicketPart { offset = offset + 11; bits.putChar6String(offset, 30,train); - offset = offset +30; + offset = offset + 30; bits.putInteger(offset, 10,coach); offset = offset + 10; @@ -117,7 +119,7 @@ public class SsbReservation extends SsbCommonTicketPart { offset = offset + 18; bits.put(offset, overbooking); - offset++; + offset++; bits.putInteger(offset, 14, infoCode); offset = offset + 14; @@ -125,6 +127,8 @@ public class SsbReservation extends SsbCommonTicketPart { bits.putChar6String(offset, 162, text); offset = offset + 162; + return offset; + } public SsbStations getStations() { -- cgit v1.2.3 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 --- .../org/uic/barcode/ssbFrame/SsbReservation.java | 27 +++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'src/main/java/org/uic/barcode/ssbFrame/SsbReservation.java') 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; -- cgit v1.2.3