-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathBER.java
More file actions
120 lines (105 loc) · 2.96 KB
/
Copy pathBER.java
File metadata and controls
120 lines (105 loc) · 2.96 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
package javaforce.net;
import java.util.*;
/** BER Encoding.
*
* Encoding = TLV (tag/length/value)
*
* @author pquiring
*/
public class BER implements SubPacket {
private byte[] type;
private Packet data = new Packet();
public int getSize() {
return type.length + 1 + getLengthBytes(data.offset) + data.offset;
}
public int getDataSize() {
return getSize();
}
public void read(Packet packet) throws Exception {
//TODO
}
public void write(Packet packet) throws Exception {
//write type
packet.write(type);
//write length
writeLength(packet, data.offset);
//write data
packet.write(data.data, 0, data.offset);
}
private byte getLengthBytes(int length) {
if (length < 256) {
return 1;
} else if (length < 65536) {
return 2;
} else if (length < 16777215) {
return 3;
} else {
return 4;
}
}
private void writeLength(Packet packet, int length) throws Exception {
byte lenSize = getLengthBytes(length);
packet.writeByte((byte)(0x80 + lenSize));
switch (lenSize) {
case 1: packet.writeByte((byte)length); break;
case 2: packet.writeShort((short)length); break;
case 3: packet.writeInt24(length); break;
case 4: packet.writeInt(length); break;
}
}
/** Set Type.
*
* @param type = BER Type
*/
public void setType(byte[] type) {
setType(type, 0, type.length);
}
/** Set Type.
*
* [0] = CCF11111 //CC=1 -> APPLICATION defined, F=1 -> constructed
* [1] = 1TTTTTTT //0x80 = more bytes (7bits Type)
* [n] = 0TTTTTTT //0x00 = last byte (7bits Type)
* Type = TT..TT
*
* @param type = BER Type
*/
public void setType(byte[] type, int type_offset, int type_length) {
this.type = Arrays.copyOfRange(type, type_offset, type_offset + type_length);
}
/** Append 8bit String (max length = 255). */
public void appendString(byte[] str) throws Exception {
data.writeByte((byte)0x04); //type
int length = str.length;
if (length > 255) {
writeLength(data, length);
} else {
data.writeByte((byte)length); //length
}
data.write(str);
}
/** Append boolean. */
public void appendBoolean(boolean value) throws Exception {
data.writeByte((byte)0x01); //type
data.writeByte((byte)0x01); //length
if (value) {
data.writeByte((byte)(byte)0xff); //true
} else {
data.writeByte((byte)0x00); //false
}
}
public void appendSequence(int length) throws Exception {
data.writeByte((byte)0x30); //type
data.writeByte((byte)(byte)length); //length
}
public void appendInteger(int value) throws Exception {
data.writeByte((byte)0x02); //type
byte bytes = getLengthBytes(value);
data.writeByte((byte)bytes); //length
switch (bytes) {
case 1: data.writeByte((byte)value); break;
case 2: data.writeShort((short)value); break;
case 3: data.writeInt24(value); break;
case 4: data.writeInt(value); break;
}
}
}