divine 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.gitignore +30 -0
- data/README.md +57 -0
- data/Rakefile +45 -18
- data/lib/divine.rb +3 -3
- data/lib/divine/code_generators/code_generator.rb +112 -14
- data/lib/divine/code_generators/java.rb +144 -80
- data/lib/divine/code_generators/javascript.rb +192 -104
- data/lib/divine/code_generators/ruby.rb +133 -60
- data/lib/divine/dsl.rb +70 -12
- data/lib/divine/version.rb +1 -1
- data/test/basic_complex_test/js_test/js_testBasic.js +1 -1
- data/test/basic_complex_test/js_test/js_testComplex.js +1 -1
- data/test/basic_complex_test/ruby_test/ruby_test.rb +10 -10
- data/test/binaryTree_test/js_test/js_test.js +1 -1
- data/test/binaryTree_test/ruby_test/ruby_test.rb +4 -4
- data/test/complex_test/js_test/js_test.js +3 -2
- data/test/ipv6_test/java_test/JavaTest.java +4 -6
- data/test/ipv6_test/js_test/js_test.js +1 -1
- data/test/signed_float_test/ruby_test/ruby_test.rb +36 -0
- data/test/signed_float_test/signed_float_test.rb +14 -0
- data/test/signed_int_test/java_test/JavaTest.java +83 -0
- data/test/signed_int_test/js_test/js_test.js +55 -0
- data/test/signed_int_test/ruby_test/ruby_test.rb +37 -0
- data/test/signed_int_test/signed_int_test.rb +15 -0
- data/test/unify_test/unify_test.rb +24 -13
- metadata +14 -38
- data/test/basic_complex_test/java_test/test_babel.java +0 -368
- data/test/basic_complex_test/js_test/test_babel.js +0 -523
- data/test/basic_complex_test/ruby_test/test_babel.rb +0 -368
- data/test/binaryTree_test/java_test/test_binaryTree.java +0 -301
- data/test/binaryTree_test/js_test/test_binaryTree.js +0 -447
- data/test/binaryTree_test/ruby_test/test_binaryTree.rb +0 -303
- data/test/complex_test/java_test/test_complex.java +0 -331
- data/test/complex_test/js_test/test_complex.js +0 -478
- data/test/complex_test/ruby_test/test_complex.rb +0 -330
- data/test/ipv6_test/java_test/junit.jar +0 -0
- data/test/ipv6_test/java_test/test_ipv6.java +0 -270
- data/test/ipv6_test/js_test/test_ipv6.js +0 -409
- data/test/ipv6_test/ruby_test/test_ipv6.rb +0 -267
- data/test/unify_test/java_test/test_unify.java +0 -171
- data/test/unify_test/js_test/js_test.js +0 -56
- data/test/unify_test/js_test/test_unify.js +0 -326
- data/test/unify_test/ruby_test/ruby_test.rb +0 -35
- data/test/unify_test/ruby_test/test_unify.rb +0 -187
@@ -1,330 +0,0 @@
|
|
1
|
-
module BabelTest
|
2
|
-
|
3
|
-
|
4
|
-
class BabelBase < Object
|
5
|
-
public
|
6
|
-
def serialize
|
7
|
-
out = []
|
8
|
-
serialize_internal(out)
|
9
|
-
out.flatten.pack("C*")
|
10
|
-
end
|
11
|
-
|
12
|
-
protected
|
13
|
-
### Read methods ###
|
14
|
-
def read_int8(data)
|
15
|
-
data.getbyte
|
16
|
-
end
|
17
|
-
|
18
|
-
def read_int16(data)
|
19
|
-
(data.getbyte << 8) | read_int8(data)
|
20
|
-
end
|
21
|
-
|
22
|
-
def read_int24(data)
|
23
|
-
(data.getbyte << 16) | read_int16(data)
|
24
|
-
end
|
25
|
-
|
26
|
-
def read_int32(data)
|
27
|
-
(data.getbyte << 24) | read_int24(data)
|
28
|
-
end
|
29
|
-
|
30
|
-
def read_bool(data)
|
31
|
-
read_int8(data) == 1
|
32
|
-
end
|
33
|
-
|
34
|
-
def read_string(data)
|
35
|
-
data.read(read_int16(data)).force_encoding('UTF-8')
|
36
|
-
end
|
37
|
-
|
38
|
-
def read_binary(data)
|
39
|
-
data.read(read_int32(data))
|
40
|
-
end
|
41
|
-
|
42
|
-
def read_short_binary(data)
|
43
|
-
data.read(read_int8(data))
|
44
|
-
end
|
45
|
-
|
46
|
-
def read_ip_number(data)
|
47
|
-
ips = read_short_binary(data)
|
48
|
-
if ips.size == 4
|
49
|
-
read_ipv4_number(ips)
|
50
|
-
else
|
51
|
-
read_ipv6_number(ips)
|
52
|
-
end
|
53
|
-
end
|
54
|
-
|
55
|
-
def read_ipv4_number(ips)
|
56
|
-
ips.bytes.to_a.join('.')
|
57
|
-
end
|
58
|
-
|
59
|
-
def read_ipv6_number(ips)
|
60
|
-
ipv6 = []
|
61
|
-
ips.bytes.each_slice(2) do |t|
|
62
|
-
fst = t[0]
|
63
|
-
lst = t[1]
|
64
|
-
tmp = ""
|
65
|
-
tmp = fst.to_s 16 if fst != 0
|
66
|
-
if fst != 0 and lst < 10
|
67
|
-
tmp << "0#{lst.to_s 16}"
|
68
|
-
elsif fst != 0 and lst > 10 or fst == 0 and lst > 0
|
69
|
-
tmp << lst.to_s(16)
|
70
|
-
end
|
71
|
-
ipv6.push(tmp)
|
72
|
-
end
|
73
|
-
ipv6.join(':').gsub(/:{2,}/, "::")
|
74
|
-
end
|
75
|
-
|
76
|
-
### Write methods ###
|
77
|
-
def write_int8(v, out)
|
78
|
-
v = v.to_i
|
79
|
-
raise_error "Too large int8 number: #{v}" if v > 0xFF # Max 255
|
80
|
-
raise_error "a negative number passed to int8 number: #{v}" if v < 0
|
81
|
-
out << v
|
82
|
-
end
|
83
|
-
|
84
|
-
def write_int16(v, out)
|
85
|
-
v = v.to_i
|
86
|
-
raise_error "Too large int16 number: #{v}" if v > 0xFFFF # Max 65.535
|
87
|
-
raise_error "a negative number passed to int16 number: #{v}" if v < 0
|
88
|
-
write_int8( v >> 8 & 0xFF, out)
|
89
|
-
write_int8( v & 0xFF, out)
|
90
|
-
end
|
91
|
-
|
92
|
-
def write_int24(v, out)
|
93
|
-
v = v.to_i
|
94
|
-
raise_error "Too large int24 number: #{v}" if v > 0xFFFFFF # Max 16.777.215
|
95
|
-
raise_error "a negative number passed to int24 number: #{v}" if v < 0 # In Case added to ruby declaration
|
96
|
-
write_int8( v >> 16 & 0xFF, out)
|
97
|
-
write_int16( v & 0xFFFF, out)
|
98
|
-
end
|
99
|
-
|
100
|
-
def write_int32(v, out)
|
101
|
-
v = v.to_i
|
102
|
-
raise_error "Too large int32 number: #{v}" if v > 0xFFFFFFFF # Max 4.294.967.295
|
103
|
-
raise_error "a negative number passed to int32 number: #{v}" if v < 0
|
104
|
-
write_int8( v >> 24 & 0xFF, out)
|
105
|
-
write_int24( v & 0xFFFFFF, out)
|
106
|
-
end
|
107
|
-
|
108
|
-
def write_bool(v, out)
|
109
|
-
write_int8(v ? 1 : 0, out)
|
110
|
-
end
|
111
|
-
|
112
|
-
def write_string(v, out)
|
113
|
-
s = force_to_utf8_string(v)
|
114
|
-
raise_error "Too large string: #{s.bytesize} bytes" if s.bytesize > 0xFFFF
|
115
|
-
write_int16(s.bytesize, out)
|
116
|
-
out << s.bytes.to_a
|
117
|
-
end
|
118
|
-
|
119
|
-
def write_binary(v, out)
|
120
|
-
if v.is_a?(Array)
|
121
|
-
raise_error "Too large binary: #{v.size} (#{v.class.name})" unless v.size < 0xFFFFFFFF
|
122
|
-
write_int32(v.size, out)
|
123
|
-
v.each do |x|
|
124
|
-
write_int8(x, out)
|
125
|
-
end
|
126
|
-
elsif v.is_a?(String)
|
127
|
-
raise_error "Too large binary: #{v.size} (#{v.class.name})" unless v.size < 0xFFFFFFFF
|
128
|
-
write_int32(v.size, out)
|
129
|
-
out << v.bytes.to_a
|
130
|
-
else
|
131
|
-
raise_error "Unsupported binary 'nil'" if v == nil
|
132
|
-
raise_error "Unsupported binary of type '#{v.class.name}'"
|
133
|
-
end
|
134
|
-
end
|
135
|
-
|
136
|
-
def write_16_binary(v, out)
|
137
|
-
if v.is_a?(Array)
|
138
|
-
raise_error "Too large 16 binary: #{v.size} (#{v.class.name})" unless v.size*2 < 0xFF
|
139
|
-
write_int8(v.size * 2, out) # IPv6 consists of 8 parts each of them has zise of 2 bytes
|
140
|
-
v.each do |x|
|
141
|
-
write_int16(x, out)
|
142
|
-
end
|
143
|
-
else
|
144
|
-
raise_error "Unsupported binary 'nil'" if v == nil
|
145
|
-
raise_error "Unsupported binary of type '#{v.class.name}'"
|
146
|
-
end
|
147
|
-
end
|
148
|
-
|
149
|
-
def write_short_binary(v, out)
|
150
|
-
if v.is_a?(Array)
|
151
|
-
raise_error "Too large short_binary: #{v.size} (#{v.class.name})" unless v.size < 0xFF
|
152
|
-
write_int8(v.size, out)
|
153
|
-
v.each do |x|
|
154
|
-
write_int8(x, out)
|
155
|
-
end
|
156
|
-
elsif v.is_a?(String)
|
157
|
-
raise_error "To large short_binary: #{v.size} (#{v.class.name})" unless v.size < 0xFF
|
158
|
-
write_int8(v.size, out)
|
159
|
-
out << v.bytes.to_a
|
160
|
-
else
|
161
|
-
raise_error "Unsupported binary 'nil'" if v == nil
|
162
|
-
raise_error "Unsupported binary of type '#{v.class.name}'"
|
163
|
-
end
|
164
|
-
end
|
165
|
-
|
166
|
-
def write_ip_number(v, out)
|
167
|
-
if v.is_a?(Array)
|
168
|
-
if v.size == 4
|
169
|
-
write_ipv4_number(v, out);
|
170
|
-
else
|
171
|
-
write_ipv6_number(v, out);
|
172
|
-
end
|
173
|
-
elsif v.is_a?(String)
|
174
|
-
if v.include?":"
|
175
|
-
write_ipv6_number(v, out);
|
176
|
-
else
|
177
|
-
write_ipv4_number(v, out);
|
178
|
-
end
|
179
|
-
else
|
180
|
-
raise_error "Unknown IP number '#{v}'"
|
181
|
-
end
|
182
|
-
end
|
183
|
-
|
184
|
-
def write_ipv4_number(v,out)
|
185
|
-
if v.is_a?(Array)
|
186
|
-
raise_error "Unknown IP v4 number #{v}" unless v.size == 0 || v.size == 4 # Only IPv4 for now
|
187
|
-
write_short_binary(v, out)
|
188
|
-
elsif v.is_a?(String)
|
189
|
-
ss = v.split(/\./).map do |s|
|
190
|
-
s.to_i
|
191
|
-
end
|
192
|
-
write_ipv4_number(ss, out)
|
193
|
-
else
|
194
|
-
raise_error "Unknown IP number '#{v}'"
|
195
|
-
end
|
196
|
-
end
|
197
|
-
|
198
|
-
def write_ipv6_number(v, out)
|
199
|
-
if v.is_a?(Array)
|
200
|
-
write_16_binary(v, out)
|
201
|
-
elsif v.is_a?(String)
|
202
|
-
v = v.gsub(" ","") + " " # Temporary: To avoid the split problem when we have : at the end of "v"
|
203
|
-
raise_error "Unknown IPv6 number #{v}" unless v.strip.empty? ||
|
204
|
-
v.strip.match(/[^:0-9a-f]+/i) == nil && #Should not contains numbers or letters 0-9a-f
|
205
|
-
v.strip.match(/[0-9a-f]+/i) != nil && #Should contains numbers or letters 0-9a-f
|
206
|
-
v.match(":{3,}") == nil &&
|
207
|
-
v.split("::").size <= 2
|
208
|
-
ss = v.split(/:/).map do |s|
|
209
|
-
s = s.strip
|
210
|
-
raise_error "Unknown IPv6 Group #{s}" unless s.size <= 4
|
211
|
-
s.to_i 16
|
212
|
-
end
|
213
|
-
ss = [] if v.strip.empty?
|
214
|
-
raise_error "Unknown IPv6 number #{v}" unless (!v.include?("::") && ss.size == 0 || ss.size == 8) ||
|
215
|
-
(v.include?("::") && ss.size > 2 && ss.size < 8)
|
216
|
-
write_ipv6_number(ss, out)
|
217
|
-
else
|
218
|
-
raise_error "Unknown IPv6 number '#{v}'"
|
219
|
-
end
|
220
|
-
end
|
221
|
-
|
222
|
-
private
|
223
|
-
def raise_error(msg)
|
224
|
-
raise "[#{self.class.name}] #{msg}"
|
225
|
-
end
|
226
|
-
|
227
|
-
def force_to_utf8_string(string)
|
228
|
-
if string.encoding != Encoding::UTF_8
|
229
|
-
string = string.encode(Encoding::UTF_8)
|
230
|
-
end
|
231
|
-
return string
|
232
|
-
end
|
233
|
-
end
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
class Complex < BabelBase
|
238
|
-
attr_accessor :list1
|
239
|
-
|
240
|
-
def initialize()
|
241
|
-
super
|
242
|
-
@list1 ||= []
|
243
|
-
end
|
244
|
-
|
245
|
-
def serialize_internal(out)
|
246
|
-
print "+"
|
247
|
-
# Serialize list 'list1'
|
248
|
-
write_int32(list1.size, out)
|
249
|
-
list1.each do |var_100|
|
250
|
-
write_int32(var_100.size, out)
|
251
|
-
var_100.each_pair do |var_101, var_102|
|
252
|
-
write_string(var_101, out)
|
253
|
-
write_int32(var_102.size, out)
|
254
|
-
var_102.each do |var_103|
|
255
|
-
var_103.serialize_internal(out)
|
256
|
-
end
|
257
|
-
end
|
258
|
-
end
|
259
|
-
end
|
260
|
-
|
261
|
-
def deserialize(data)
|
262
|
-
print "-"
|
263
|
-
# Deserialize list 'list1'
|
264
|
-
@list1 = []
|
265
|
-
var_104 = read_int32(data)
|
266
|
-
(1..var_104).each do
|
267
|
-
var_105 = {}
|
268
|
-
var_106 = read_int32(data)
|
269
|
-
(1..var_106).each do
|
270
|
-
var_107 = read_string(data)
|
271
|
-
var_108 = []
|
272
|
-
var_109 = read_int32(data)
|
273
|
-
(1..var_109).each do
|
274
|
-
var_10a = IPList.new
|
275
|
-
var_10a.deserialize(data)
|
276
|
-
var_108 << var_10a
|
277
|
-
end
|
278
|
-
var_105[var_107] = var_108
|
279
|
-
end
|
280
|
-
@list1 << var_105
|
281
|
-
end
|
282
|
-
end
|
283
|
-
end
|
284
|
-
|
285
|
-
|
286
|
-
|
287
|
-
class IPList < BabelBase
|
288
|
-
attr_accessor :list1, :list2
|
289
|
-
|
290
|
-
def initialize()
|
291
|
-
super
|
292
|
-
@list1 ||= []
|
293
|
-
@list2 ||= []
|
294
|
-
end
|
295
|
-
|
296
|
-
def serialize_internal(out)
|
297
|
-
print "+"
|
298
|
-
# Serialize list 'list1'
|
299
|
-
write_int32(list1.size, out)
|
300
|
-
list1.each do |var_10b|
|
301
|
-
write_ip_number(var_10b, out)
|
302
|
-
end
|
303
|
-
# Serialize list 'list2'
|
304
|
-
write_int32(list2.size, out)
|
305
|
-
list2.each do |var_10c|
|
306
|
-
write_ip_number(var_10c, out)
|
307
|
-
end
|
308
|
-
end
|
309
|
-
|
310
|
-
def deserialize(data)
|
311
|
-
print "-"
|
312
|
-
# Deserialize list 'list1'
|
313
|
-
@list1 = []
|
314
|
-
var_10d = read_int32(data)
|
315
|
-
(1..var_10d).each do
|
316
|
-
var_10e = read_ip_number(data)
|
317
|
-
@list1 << var_10e
|
318
|
-
end
|
319
|
-
# Deserialize list 'list2'
|
320
|
-
@list2 = []
|
321
|
-
var_10f = read_int32(data)
|
322
|
-
(1..var_10f).each do
|
323
|
-
var_110 = read_ip_number(data)
|
324
|
-
@list2 << var_110
|
325
|
-
end
|
326
|
-
end
|
327
|
-
end
|
328
|
-
|
329
|
-
|
330
|
-
end
|
Binary file
|
@@ -1,270 +0,0 @@
|
|
1
|
-
import java.io.ByteArrayInputStream;
|
2
|
-
import java.io.ByteArrayOutputStream;
|
3
|
-
import java.io.IOException;
|
4
|
-
import java.util.ArrayList;
|
5
|
-
import java.util.HashMap;
|
6
|
-
import java.util.regex.Pattern;
|
7
|
-
import java.nio.charset.Charset;
|
8
|
-
|
9
|
-
abstract class BabelBase {
|
10
|
-
private static final Charset UTF8 = Charset.forName("UTF-8");
|
11
|
-
|
12
|
-
public byte[] serialize() throws IOException {
|
13
|
-
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
14
|
-
serializeInternal(baos);
|
15
|
-
baos.close();
|
16
|
-
return baos.toByteArray();
|
17
|
-
}
|
18
|
-
|
19
|
-
abstract void serializeInternal(ByteArrayOutputStream baos) throws IOException;
|
20
|
-
|
21
|
-
abstract void deserialize(ByteArrayInputStream baos) throws IOException;
|
22
|
-
|
23
|
-
protected int readInt8(ByteArrayInputStream data) {
|
24
|
-
return data.read() & 0xff;
|
25
|
-
}
|
26
|
-
|
27
|
-
protected int readInt16(ByteArrayInputStream data) {
|
28
|
-
return (data.read() << 8) | readInt8(data);
|
29
|
-
}
|
30
|
-
|
31
|
-
protected int readInt24(ByteArrayInputStream data) {
|
32
|
-
return (data.read() << 16) | readInt16(data);
|
33
|
-
}
|
34
|
-
|
35
|
-
protected long readInt32(ByteArrayInputStream data) {
|
36
|
-
return (data.read() << 24) | readInt24(data);
|
37
|
-
}
|
38
|
-
|
39
|
-
protected boolean readBool(ByteArrayInputStream data) {
|
40
|
-
return readInt8(data) == 1;
|
41
|
-
}
|
42
|
-
|
43
|
-
protected String readString(ByteArrayInputStream data) throws IOException {
|
44
|
-
// Force utf8
|
45
|
-
return new String(readBytes(readInt16(data), data), UTF8);
|
46
|
-
}
|
47
|
-
|
48
|
-
private byte[] readBytes(int size, ByteArrayInputStream data) throws IOException {
|
49
|
-
byte[] bs = new byte[size];
|
50
|
-
data.read(bs);
|
51
|
-
return bs;
|
52
|
-
}
|
53
|
-
|
54
|
-
protected byte[] readBinary(ByteArrayInputStream data) throws IOException {
|
55
|
-
long c = readInt32(data);
|
56
|
-
if (c > Integer.MAX_VALUE) {
|
57
|
-
throw new IndexOutOfBoundsException("Binary data to big for java");
|
58
|
-
}
|
59
|
-
return readBytes((int) c, data);
|
60
|
-
}
|
61
|
-
|
62
|
-
protected byte[] readShortBinary(ByteArrayInputStream data) throws IOException {
|
63
|
-
return readBytes(readInt8(data), data);
|
64
|
-
}
|
65
|
-
|
66
|
-
protected String readIpNumber(ByteArrayInputStream data) throws IOException {
|
67
|
-
byte[] ips = readShortBinary(data);
|
68
|
-
if(ips.length == 4){
|
69
|
-
return readIpv4Number(ips);
|
70
|
-
} else{
|
71
|
-
return readIpv6Number(ips);
|
72
|
-
}
|
73
|
-
}
|
74
|
-
|
75
|
-
protected String readIpv4Number(byte[] ips){
|
76
|
-
String ip = "";
|
77
|
-
for (byte b : ips) {
|
78
|
-
if (ip.length() > 0) {
|
79
|
-
ip += ".";
|
80
|
-
}
|
81
|
-
ip += (b & 0xFF);
|
82
|
-
}
|
83
|
-
return ip;
|
84
|
-
}
|
85
|
-
|
86
|
-
protected String readIpv6Number(byte[] ips) throws IOException {
|
87
|
-
String ip = "";
|
88
|
-
int part1, part2;
|
89
|
-
for (int i = 0; i < ips.length; i+=2) {
|
90
|
-
part1 = ips[i] & 0xFF;
|
91
|
-
part2 = ips[i+1] & 0xFF;
|
92
|
-
ip += part1 == 0? "" : Integer.toHexString(part1);
|
93
|
-
ip += (part1 == 0 && part2 == 0)? "" : (part2 < 10 && part1 != 0? "0" + Integer.toHexString(part2): Integer.toHexString(part2));
|
94
|
-
if (i < ips.length-2) {
|
95
|
-
ip += ":";
|
96
|
-
}
|
97
|
-
}
|
98
|
-
ip = ip.replaceAll(":{3,}", "::");
|
99
|
-
return ip;
|
100
|
-
}
|
101
|
-
|
102
|
-
protected void writeInt8(int v, ByteArrayOutputStream out) {
|
103
|
-
if (v > 0xFF) { // Max 255
|
104
|
-
raiseError("Too large int8 number: " + v);
|
105
|
-
}else if(v < 0){
|
106
|
-
raiseError("a negative number passed to int8 number: " + v);
|
107
|
-
}
|
108
|
-
out.write(v);
|
109
|
-
}
|
110
|
-
|
111
|
-
protected void writeInt16(int v, ByteArrayOutputStream out) {
|
112
|
-
if (v > 0xFFFF) { // Max 65.535
|
113
|
-
raiseError("Too large int16 number: " + v);
|
114
|
-
}else if(v < 0){
|
115
|
-
raiseError("a negative number passed to int16 number: " + v);
|
116
|
-
}
|
117
|
-
writeInt8(v >> 8 & 0xFF, out);
|
118
|
-
writeInt8(v & 0xFF, out);
|
119
|
-
}
|
120
|
-
|
121
|
-
protected void writeInt24(int v, ByteArrayOutputStream out) {
|
122
|
-
if (v > 0xFFFFFF) { // Max 16.777.215
|
123
|
-
raiseError("Too large int24 number: " + v);
|
124
|
-
}else if(v < 0){ // In Case added to Java declaration
|
125
|
-
raiseError("a negative number passed to int24 number: " + v);
|
126
|
-
}
|
127
|
-
writeInt8(v >> 16 & 0xFF, out);
|
128
|
-
writeInt16(v & 0xFFFF, out);
|
129
|
-
}
|
130
|
-
|
131
|
-
protected void writeInt32(long v, ByteArrayOutputStream out) {
|
132
|
-
if (v > 0xFFFFFFFFL) { // Max 4.294.967.295
|
133
|
-
raiseError("Too large int32 number: " + v);
|
134
|
-
}else if(v < 0){
|
135
|
-
raiseError("a negative number passed to int32 number: " + v);
|
136
|
-
}
|
137
|
-
writeInt8((int) ((v >> 24) & 0xFF), out);
|
138
|
-
writeInt24((int) (v & 0xFFFFFF), out);
|
139
|
-
}
|
140
|
-
|
141
|
-
protected void writeBool(boolean v, ByteArrayOutputStream out) {
|
142
|
-
writeInt8(v ? 1 : 0, out);
|
143
|
-
}
|
144
|
-
|
145
|
-
protected void writeString(String v, ByteArrayOutputStream out) throws IOException {
|
146
|
-
byte[] bs = v.getBytes(UTF8);
|
147
|
-
if (bs.length > 0xFFFF) {
|
148
|
-
raiseError("Too large string: " + bs.length + " bytes");
|
149
|
-
}
|
150
|
-
writeInt16(bs.length, out);
|
151
|
-
out.write(bs);
|
152
|
-
}
|
153
|
-
|
154
|
-
protected void writeBinary(byte[] v, ByteArrayOutputStream out) throws IOException {
|
155
|
-
if (v.length > 0xFFFFFFFFL) {
|
156
|
-
raiseError("Too large binary: " + v.length + " bytes");
|
157
|
-
}
|
158
|
-
writeInt32(v.length, out);
|
159
|
-
out.write(v);
|
160
|
-
}
|
161
|
-
|
162
|
-
|
163
|
-
protected void write16Binary(int[] v, ByteArrayOutputStream out) throws IOException {
|
164
|
-
if (v.length > 0xFF) {
|
165
|
-
raiseError("Too large 16_binary: " + (v.length*2) + " bytes");
|
166
|
-
}
|
167
|
-
writeInt8(v.length*2, out);
|
168
|
-
for(int i = 0; i < v.length; i++){
|
169
|
-
this.writeInt16(v[i], out);
|
170
|
-
}
|
171
|
-
}
|
172
|
-
|
173
|
-
protected void writeShortBinary(byte[] v, ByteArrayOutputStream out) throws IOException {
|
174
|
-
if (v.length > 0xFF) {
|
175
|
-
raiseError("Too large short_binary: " + v.length + " bytes");
|
176
|
-
}
|
177
|
-
writeInt8(v.length, out);
|
178
|
-
out.write(v);
|
179
|
-
}
|
180
|
-
|
181
|
-
protected void writeIpNumber(String v, ByteArrayOutputStream out) throws IOException {
|
182
|
-
if(v.contains(":")){
|
183
|
-
writeIpv6Number( v, out);
|
184
|
-
}else{
|
185
|
-
writeIpv4Number( v, out);
|
186
|
-
}
|
187
|
-
}
|
188
|
-
|
189
|
-
protected void writeIpv4Number(String v, ByteArrayOutputStream out) throws IOException {
|
190
|
-
byte[] ss = new byte[0];
|
191
|
-
if(!v.isEmpty()){
|
192
|
-
String[] bs = v.split("\\.");
|
193
|
-
ss = new byte[bs.length];
|
194
|
-
for (int i = 0; i < bs.length; i++) {
|
195
|
-
ss[i] = (byte) (Integer.parseInt(bs[i]) & 0xFF);
|
196
|
-
}
|
197
|
-
}
|
198
|
-
if (ss.length == 0 || ss.length == 4) {
|
199
|
-
writeShortBinary(ss, out);
|
200
|
-
} else {
|
201
|
-
raiseError("Unknown IP v4 number " + v); // Only IPv4 for now
|
202
|
-
}
|
203
|
-
}
|
204
|
-
|
205
|
-
protected void writeIpv6Number(String v, ByteArrayOutputStream out)
|
206
|
-
throws IOException {
|
207
|
-
v = v.replaceAll(" ", "") + " "; // Temporary: To avoid the split problem when we have : at the
|
208
|
-
// end of "v"
|
209
|
-
int[] ss = new int[0];
|
210
|
-
boolean contains_ipv6_letters = Pattern.compile("[0-9a-f]+").matcher(
|
211
|
-
v.trim().toLowerCase()).find();
|
212
|
-
boolean contains_other_letters = Pattern.compile("[^:0-9a-f]+")
|
213
|
-
.matcher(v.trim().toLowerCase()).find();
|
214
|
-
// make sure of v must have only one "::" and no more than two of ":".
|
215
|
-
// e.g. 1::1::1 & 1:::1:205
|
216
|
-
if (!v.trim().isEmpty() && v.split(":{3,}").length == 1
|
217
|
-
&& v.split(":{2}").length <= 2 && !contains_other_letters
|
218
|
-
&& contains_ipv6_letters) {
|
219
|
-
String[] bs = v.split(":");
|
220
|
-
ss = new int[bs.length];
|
221
|
-
for (int i = 0; i < bs.length; i++) {
|
222
|
-
String s = bs[i].trim();
|
223
|
-
if (s.length() <= 4) // to avoid such number 0125f
|
224
|
-
ss[i] = Integer.parseInt(
|
225
|
-
(s.isEmpty() ? "0" : bs[i].trim()), 16);
|
226
|
-
else
|
227
|
-
raiseError("Unknown IPv6 Group " + i + " which is " + s);
|
228
|
-
}
|
229
|
-
}
|
230
|
-
// Check for make sure of the size of the IP groups in case "::" is used
|
231
|
-
// [> 2 & < 8]or not [must == 8]
|
232
|
-
if (!contains_other_letters
|
233
|
-
&& (!v.contains("::") && ss.length == 0 || ss.length == 8)
|
234
|
-
|| (v.contains("::") && ss.length > 2 && ss.length < 8)) {
|
235
|
-
write16Binary(ss, out);
|
236
|
-
} else {
|
237
|
-
raiseError("Unknown IP v6 number " + v);
|
238
|
-
}
|
239
|
-
}
|
240
|
-
|
241
|
-
protected void raiseError(String msg) {
|
242
|
-
throw new IllegalArgumentException("[" + this.getClass().getCanonicalName() + "] " + msg);
|
243
|
-
}
|
244
|
-
}
|
245
|
-
|
246
|
-
|
247
|
-
class IPV6 extends BabelBase {
|
248
|
-
public ArrayList<String> list1 = new ArrayList<String>();
|
249
|
-
|
250
|
-
@Override
|
251
|
-
void serializeInternal(ByteArrayOutputStream baos) throws IOException {
|
252
|
-
// Serialize list 'list1'
|
253
|
-
writeInt32(list1.size(), baos);
|
254
|
-
for(int var_101=0; var_101<list1.size(); var_101++) {
|
255
|
-
String var_100 = list1.get(var_101);
|
256
|
-
writeIpNumber(var_100, baos);
|
257
|
-
}
|
258
|
-
}
|
259
|
-
|
260
|
-
@Override
|
261
|
-
public void deserialize(ByteArrayInputStream bais) throws IOException {
|
262
|
-
// Deserialize list 'list1'
|
263
|
-
this.list1 = new ArrayList<String>();
|
264
|
-
int var_102 = (int)this.readInt32(bais);
|
265
|
-
for(int var_104=0; var_104<var_102; var_104++) {
|
266
|
-
String var_103 = readIpNumber(bais);
|
267
|
-
this.list1.add(var_103);
|
268
|
-
}
|
269
|
-
}
|
270
|
-
}
|