divine 0.0.2 → 0.0.3
Sign up to get free protection for your applications and to get access to all the features.
- 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,368 +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 Entry < BabelBase
|
238
|
-
|
239
|
-
def serialize_internal(out)
|
240
|
-
print "+"
|
241
|
-
end
|
242
|
-
|
243
|
-
def deserialize(data)
|
244
|
-
print "-"
|
245
|
-
end
|
246
|
-
end
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
class TestBasic < BabelBase
|
251
|
-
attr_accessor :i8, :i16, :i32, :str, :ip, :guid
|
252
|
-
|
253
|
-
def initialize()
|
254
|
-
super
|
255
|
-
@i8 ||= 0
|
256
|
-
@i16 ||= 0
|
257
|
-
@i32 ||= 0
|
258
|
-
@str ||= ""
|
259
|
-
@ip ||= ""
|
260
|
-
@guid ||= []
|
261
|
-
end
|
262
|
-
|
263
|
-
def serialize_internal(out)
|
264
|
-
print "+"
|
265
|
-
write_int8(i8, out)
|
266
|
-
write_int16(i16, out)
|
267
|
-
write_int32(i32, out)
|
268
|
-
write_string(str, out)
|
269
|
-
write_ip_number(ip, out)
|
270
|
-
write_binary(guid, out)
|
271
|
-
end
|
272
|
-
|
273
|
-
def deserialize(data)
|
274
|
-
print "-"
|
275
|
-
@i8 = read_int8(data)
|
276
|
-
@i16 = read_int16(data)
|
277
|
-
@i32 = read_int32(data)
|
278
|
-
@str = read_string(data)
|
279
|
-
@ip = read_ip_number(data)
|
280
|
-
@guid = read_binary(data)
|
281
|
-
end
|
282
|
-
end
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
class TestComplex < BabelBase
|
287
|
-
attr_accessor :list1, :list2, :map1, :map2
|
288
|
-
|
289
|
-
def initialize()
|
290
|
-
super
|
291
|
-
@list1 ||= []
|
292
|
-
@list2 ||= []
|
293
|
-
@map1 ||= {}
|
294
|
-
@map2 ||= {}
|
295
|
-
end
|
296
|
-
|
297
|
-
def serialize_internal(out)
|
298
|
-
print "+"
|
299
|
-
# Serialize list 'list1'
|
300
|
-
write_int32(list1.size, out)
|
301
|
-
list1.each do |var_100|
|
302
|
-
write_int32(var_100, out)
|
303
|
-
end
|
304
|
-
# Serialize list 'list2'
|
305
|
-
write_int32(list2.size, out)
|
306
|
-
list2.each do |var_101|
|
307
|
-
write_int8(var_101, out)
|
308
|
-
end
|
309
|
-
# Serialize map 'map1'
|
310
|
-
write_int32(map1.size, out)
|
311
|
-
map1.each_pair do |var_102, var_103|
|
312
|
-
write_int8(var_102, out)
|
313
|
-
write_int32(var_103, out)
|
314
|
-
end
|
315
|
-
# Serialize map 'map2'
|
316
|
-
write_int32(map2.size, out)
|
317
|
-
map2.each_pair do |var_104, var_105|
|
318
|
-
write_string(var_104, out)
|
319
|
-
write_int32(var_105.size, out)
|
320
|
-
var_105.each do |var_106|
|
321
|
-
var_106.serialize_internal(out)
|
322
|
-
end
|
323
|
-
end
|
324
|
-
end
|
325
|
-
|
326
|
-
def deserialize(data)
|
327
|
-
print "-"
|
328
|
-
# Deserialize list 'list1'
|
329
|
-
@list1 = []
|
330
|
-
var_107 = read_int32(data)
|
331
|
-
(1..var_107).each do
|
332
|
-
var_108 = read_int32(data)
|
333
|
-
@list1 << var_108
|
334
|
-
end
|
335
|
-
# Deserialize list 'list2'
|
336
|
-
@list2 = []
|
337
|
-
var_109 = read_int32(data)
|
338
|
-
(1..var_109).each do
|
339
|
-
var_10a = read_int8(data)
|
340
|
-
@list2 << var_10a
|
341
|
-
end
|
342
|
-
# Deserialize map 'map1'
|
343
|
-
@map1 = {}
|
344
|
-
var_10b = read_int32(data)
|
345
|
-
(1..var_10b).each do
|
346
|
-
var_10c = read_int8(data)
|
347
|
-
var_10d = read_int32(data)
|
348
|
-
@map1[var_10c] = var_10d
|
349
|
-
end
|
350
|
-
# Deserialize map 'map2'
|
351
|
-
@map2 = {}
|
352
|
-
var_10e = read_int32(data)
|
353
|
-
(1..var_10e).each do
|
354
|
-
var_10f = read_string(data)
|
355
|
-
var_110 = []
|
356
|
-
var_111 = read_int32(data)
|
357
|
-
(1..var_111).each do
|
358
|
-
var_112 = Entry.new
|
359
|
-
var_112.deserialize(data)
|
360
|
-
var_110 << var_112
|
361
|
-
end
|
362
|
-
@map2[var_10f] = var_110
|
363
|
-
end
|
364
|
-
end
|
365
|
-
end
|
366
|
-
|
367
|
-
|
368
|
-
end
|
@@ -1,301 +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 BinaryTree extends BabelBase {
|
248
|
-
public ArrayList<Node> root_node = new ArrayList<Node>();
|
249
|
-
|
250
|
-
@Override
|
251
|
-
void serializeInternal(ByteArrayOutputStream baos) throws IOException {
|
252
|
-
// Serialize list 'root_node'
|
253
|
-
writeInt32(root_node.size(), baos);
|
254
|
-
for(int var_101=0; var_101<root_node.size(); var_101++) {
|
255
|
-
Node var_100 = root_node.get(var_101);
|
256
|
-
var_100.serializeInternal(baos);
|
257
|
-
}
|
258
|
-
}
|
259
|
-
|
260
|
-
@Override
|
261
|
-
public void deserialize(ByteArrayInputStream bais) throws IOException {
|
262
|
-
// Deserialize list 'root_node'
|
263
|
-
this.root_node = new ArrayList<Node>();
|
264
|
-
int var_102 = (int)this.readInt32(bais);
|
265
|
-
for(int var_104=0; var_104<var_102; var_104++) {
|
266
|
-
Node var_103 = new Node();
|
267
|
-
var_103.deserialize(bais);
|
268
|
-
this.root_node.add(var_103);
|
269
|
-
}
|
270
|
-
}
|
271
|
-
}
|
272
|
-
|
273
|
-
|
274
|
-
class Node extends BabelBase {
|
275
|
-
public long i32 = 0L;
|
276
|
-
public ArrayList<Node> next_node = new ArrayList<Node>();
|
277
|
-
|
278
|
-
@Override
|
279
|
-
void serializeInternal(ByteArrayOutputStream baos) throws IOException {
|
280
|
-
writeInt32(this.i32, baos);
|
281
|
-
// Serialize list 'next_node'
|
282
|
-
writeInt32(next_node.size(), baos);
|
283
|
-
for(int var_106=0; var_106<next_node.size(); var_106++) {
|
284
|
-
Node var_105 = next_node.get(var_106);
|
285
|
-
var_105.serializeInternal(baos);
|
286
|
-
}
|
287
|
-
}
|
288
|
-
|
289
|
-
@Override
|
290
|
-
public void deserialize(ByteArrayInputStream bais) throws IOException {
|
291
|
-
this.i32 = readInt32(bais);
|
292
|
-
// Deserialize list 'next_node'
|
293
|
-
this.next_node = new ArrayList<Node>();
|
294
|
-
int var_107 = (int)this.readInt32(bais);
|
295
|
-
for(int var_109=0; var_109<var_107; var_109++) {
|
296
|
-
Node var_108 = new Node();
|
297
|
-
var_108.deserialize(bais);
|
298
|
-
this.next_node.add(var_108);
|
299
|
-
}
|
300
|
-
}
|
301
|
-
}
|