Skip to content

Commit

Permalink
Merge pull request #90 from craftbyte/master
Browse files Browse the repository at this point in the history
Fix SSB decoder, add real life test case
  • Loading branch information
CGantert345 authored Nov 25, 2024
2 parents 15c13a2 + 8563469 commit 79c96b4
Show file tree
Hide file tree
Showing 13 changed files with 330 additions and 180 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>org.uic.barcode</groupId>
<artifactId>org.uic.barcode</artifactId>
<version>1.4.9</version>
<version>1.4.10</version>
<packaging>jar</packaging>
<name>UIC barcode</name>
<description>Encoding and decoding of Aztec barcode content according to UIC IRS 90918-9</description>
Expand Down
124 changes: 62 additions & 62 deletions src/main/java/org/uic/barcode/ssbFrame/SsbClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,68 +3,68 @@
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;

}
24 changes: 12 additions & 12 deletions src/main/java/org/uic/barcode/ssbFrame/SsbCommonTicketPart.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,20 @@ protected int decodeCommonPart(byte[] bytes) {

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;
}

Expand All @@ -55,35 +55,35 @@ protected int encodeCommonPart(byte[] bytes, int offset) throws EncodingFormatEx
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;

Expand Down
30 changes: 15 additions & 15 deletions src/main/java/org/uic/barcode/ssbFrame/SsbGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -55,7 +55,7 @@ protected int decodeContent(byte[] bytes, int offset) {
@Override
protected int encodeContent(byte[] bytes, int offset) throws EncodingFormatException {

offset = offset + encodeCommonPart(bytes, offset);
offset += encodeCommonPart(bytes, offset);

BitBuffer bits = new ByteBitBuffer(bytes);

Expand All @@ -66,39 +66,39 @@ protected int encodeContent(byte[] bytes, int offset) throws EncodingFormatExcep
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);

if (groupName.length() > 12) {
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;
}
Expand Down
24 changes: 12 additions & 12 deletions src/main/java/org/uic/barcode/ssbFrame/SsbNonReservation.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -45,38 +45,38 @@ protected int decodeContent(byte[] bytes, int offset) {
@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);

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() > 37) {
throw new EncodingFormatException("SSB text too big");
}
bits.putChar6String(offset, 222, text);
offset = offset + 222;
offset += 222;

return offset;

Expand Down
Loading

0 comments on commit 79c96b4

Please sign in to comment.