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 --- .../uic/barcode/ssbFrame/SsbCommonTicketPart.java | 132 +++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java (limited to 'src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java') diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java b/src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java new file mode 100644 index 0000000..9512fc1 --- /dev/null +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java @@ -0,0 +1,132 @@ +package org.uic.barcode.ssbFrame; + +import org.uic.barcode.asn1.uper.BitBuffer; +import org.uic.barcode.asn1.uper.ByteBitBuffer; + +public abstract class SsbCommonTicketPart extends SsbTicketPart { + + /* + Number of adult passengers Num (<100) 7,000 + Number of child passengers Num (<100) 7,000 + "specimen" code Bit Flag 1,000 + Class of travel Lookup of 64 options 6,000 + TCN: Issuing unique Ticket number 14 AlphaNum 84,000 + Year of issue Num (0..9) 4,000 + Issuing day, from first of January Num (<512) 9,000 + */ + + protected int numberOfAdults = 0; + protected int numberOfChildren = 0; + protected boolean specimen = true; + protected SsbClass classCode = null; + protected String ticketNumber = null; + protected int year = 0; + protected int day = 0; + + protected int decodeCommonPart(byte[] bytes) { + + BitBuffer bits = new ByteBitBuffer(bytes); + + int offset = 27; // header offset + numberOfAdults = bits.getInteger(offset, 7); + offset = offset + 7; + numberOfChildren = bits.getInteger(offset, 7); + offset = offset + 7; + specimen = bits.get(offset); + offset++; + int classIndex = bits.getInteger(offset, 6); + classCode = SsbClass.values()[classIndex]; + offset = offset + 6; + ticketNumber = bits.getChar6String(offset, 84); + offset = offset + 84; + year = bits.getInteger(offset, 4); + offset = offset + 4; + day = bits.getInteger(offset, 9); + offset = offset + 9; + return offset; + } + + protected int encodeCommonPart(byte[] bytes) { + + BitBuffer bits = new ByteBitBuffer(bytes); + + int offset = 27; // header offset + bits.putInteger(offset,7, numberOfAdults); + offset = offset + 7; + bits.putInteger(numberOfChildren,offset, 7); + offset = offset + 7; + bits.put(offset,specimen); + offset++; + bits.putInteger(offset, 6,classCode.ordinal()); + offset = offset + 6; + bits.putChar6String(offset, 84, ticketNumber); + offset = offset + 84; + bits.putInteger(offset, 4, year); + offset = offset + 4; + bits.putInteger(offset, 9, day); + offset = offset + 9; + + return offset; + + + } + + public int getNumberOfAdults() { + return numberOfAdults; + } + + public void setNumberOfAdults(int numberOfAdults) { + this.numberOfAdults = numberOfAdults; + } + + public int getNumberOfChildren() { + return numberOfChildren; + } + + public void setNumberOfChildren(int numberOfChildren) { + this.numberOfChildren = numberOfChildren; + } + + public boolean isSpecimen() { + return specimen; + } + + public void setSpecimen(boolean specimen) { + this.specimen = specimen; + } + + public SsbClass getClassCode() { + return classCode; + } + + public void setClassCode(SsbClass classCode) { + this.classCode = classCode; + } + + public String getTicketNumber() { + return ticketNumber; + } + + public void setTicketNumber(String ticketNumber) { + this.ticketNumber = ticketNumber; + } + + public int getYear() { + return year; + } + + public void setYear(int year) { + this.year = year; + } + + public int getDay() { + return day; + } + + public void setDay(int day) { + this.day = day; + } + + + +} -- 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 --- src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java') diff --git a/src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java b/src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java index 9512fc1..0a158f3 100644 --- a/src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java +++ b/src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java @@ -46,14 +46,13 @@ public abstract class SsbCommonTicketPart extends SsbTicketPart { return offset; } - protected int encodeCommonPart(byte[] bytes) { + protected int encodeCommonPart(byte[] bytes, int offset) { BitBuffer bits = new ByteBitBuffer(bytes); - - int offset = 27; // header offset + bits.putInteger(offset,7, numberOfAdults); offset = offset + 7; - bits.putInteger(numberOfChildren,offset, 7); + bits.putInteger(offset, 7, numberOfChildren); offset = offset + 7; bits.put(offset,specimen); offset++; -- 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 --- .../uic/barcode/ssbFrame/SsbCommonTicketPart.java | 28 ++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) (limited to 'src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java') 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; -- cgit v1.2.3