summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/uic/barcode/ssbFrame/SsbHeader.java
blob: 48c8eaf6b5c1db15a06251324f45777626bce6ba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
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 {
	
	private int version = 3;
	private SsbTicketType ticketType = null;
	private int keyId = 0;
	private int issuer = 0;
	
	/*
	        Version	             Num 0-4 Bits
			Issuer code	         Num  14 Bits
			ID					 Num   4 Bits
			Ticket type code	 Num   5 Bits
	 */

	public SsbHeader(int version, SsbTicketType type,  int keyId, int issuer) {
		this.issuer = issuer;
		this.keyId = keyId;
		this.ticketType = type;
		this.version = version;
	}
	
	public SsbHeader() {		
	}

	public int decodeContent(byte[] headerData, int offset) {
		
		BitBuffer bits = new ByteBitBuffer(headerData);

		version = bits.getInteger(0, 4);
		issuer = bits.getInteger(4, 14);
		keyId = bits.getInteger(18, 4);
		ticketType = SsbTicketType.values()[bits.getInteger(22, 5)];
		
		return 4 + 14 + 4 + 5;

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



	public int getVersion() {
		return version;
	}



	public void setVersion(int version) {
		this.version = version;
	}



	public SsbTicketType getTicketType() {
		return ticketType;
	}



	public void setTicketType(SsbTicketType ticketType) {
		this.ticketType = ticketType;
	}



	public int getKeyId() {
		return keyId;
	}



	public void setKeyId(int keyId) {
		this.keyId = keyId;
	}



	public int getIssuer() {
		return issuer;
	}



	public void setIssuer(int issuer) {
		this.issuer = issuer;
	}



	
	
	
	
}