Repository navigation
Expand file tree
/
Copy pathTagImpl.java
More file actions
164 lines (142 loc) · 4.7 KB
/
Copy pathTagImpl.java
File metadata and controls
164 lines (142 loc) · 4.7 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
* Copyright 2010 sasc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package sasc.iso7816;
import java.util.Arrays;
import sasc.util.Util;
/**
*
* @author sasc
*/
public class TagImpl implements Tag {
byte[] idBytes;
String name;
String description;
TagValueType tagValueType;
Class tagClass;
TagType type;
public TagImpl(String id, TagValueType tagValueType, String name, String description) {
build(Util.fromHexString(id), tagValueType, name, description);
}
public TagImpl(byte[] idBytes, TagValueType tagValueType, String name, String description) {
build(idBytes, tagValueType, name, description);
}
private void build(byte[] idBytes, TagValueType tagValueType, String name, String description) {
if(idBytes == null){
throw new IllegalArgumentException("Param id cannot be null");
}
if(idBytes.length == 0) {
throw new IllegalArgumentException("Param id cannot be empty");
}
if(tagValueType == null){
throw new IllegalArgumentException("Param tagValueType cannot be null");
}
this.idBytes = idBytes;
this.name = name!=null?name:"";
this.description = description!=null?description:"";
this.tagValueType = tagValueType;
if(Util.isBitSet(this.idBytes[0], 6)){
this.type = TagType.CONSTRUCTED;
} else {
this.type = TagType.PRIMITIVE;
}
//Bits 8 and 7 of the first byte of the tag field indicate a class.
//The value 00 indicates a data object of the universal class.
//The value 01 indicates a data object of the application class.
//The value 10 indicates a data object of the context-specific class.
//The value 11 indicates a data object of the private class.
byte classValue = (byte)(this.idBytes[0] >>> 6 & 0x03);
switch(classValue){
case (byte)0x00:
tagClass = Class.UNIVERSAL;
break;
case (byte)0x01:
tagClass = Class.APPLICATION;
break;
case (byte)0x02:
tagClass = Class.CONTEXT_SPECIFIC;
break;
case (byte)0x03:
tagClass = Class.PRIVATE;
break;
default:
throw new RuntimeException("UNEXPECTED TAG CLASS: "+Util.byte2BinaryLiteral(classValue) + " " + Util.byteArrayToHexString(this.idBytes) + " " + name);
}
}
@Override
public boolean isConstructed() {
return type == TagType.CONSTRUCTED;
}
@Override
public byte[] getTagBytes() {
return idBytes;
}
@Override
public String getName() {
return name;
}
@Override
public String getDescription(){
return description;
}
@Override
public TagValueType getTagValueType() {
return tagValueType;
}
@Override
public TagType getType() {
return type;
}
@Override
public Class getTagClass(){
return tagClass;
}
@Override
public boolean equals(Object other){
if(!(other instanceof Tag)) return false;
Tag that = (Tag)other;
if(this.getTagBytes().length != that.getTagBytes().length) return false;
return Arrays.equals(this.getTagBytes(), that.getTagBytes());
}
@Override
public int hashCode() {
int hash = 3;
hash = 59 * hash + Arrays.hashCode(this.idBytes);
return hash;
}
@Override
public int getNumTagBytes() {
return idBytes.length;
}
@Override
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("Tag[");
sb.append(Util.byteArrayToHexString(getTagBytes()));
sb.append("] Name=");
sb.append(getName());
sb.append(", TagType=");
sb.append(getType());
sb.append(", ValueType=");
sb.append(getTagValueType());
sb.append(", Class=");
sb.append(tagClass);
return sb.toString();
}
public static void main(String[] args) throws Exception {
TagImpl tag = new TagImpl("bf0c", TagValueType.BINARY, "", "");
System.out.println(tag);
}
}