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.
Files changed (44) hide show
  1. data/.gitignore +30 -0
  2. data/README.md +57 -0
  3. data/Rakefile +45 -18
  4. data/lib/divine.rb +3 -3
  5. data/lib/divine/code_generators/code_generator.rb +112 -14
  6. data/lib/divine/code_generators/java.rb +144 -80
  7. data/lib/divine/code_generators/javascript.rb +192 -104
  8. data/lib/divine/code_generators/ruby.rb +133 -60
  9. data/lib/divine/dsl.rb +70 -12
  10. data/lib/divine/version.rb +1 -1
  11. data/test/basic_complex_test/js_test/js_testBasic.js +1 -1
  12. data/test/basic_complex_test/js_test/js_testComplex.js +1 -1
  13. data/test/basic_complex_test/ruby_test/ruby_test.rb +10 -10
  14. data/test/binaryTree_test/js_test/js_test.js +1 -1
  15. data/test/binaryTree_test/ruby_test/ruby_test.rb +4 -4
  16. data/test/complex_test/js_test/js_test.js +3 -2
  17. data/test/ipv6_test/java_test/JavaTest.java +4 -6
  18. data/test/ipv6_test/js_test/js_test.js +1 -1
  19. data/test/signed_float_test/ruby_test/ruby_test.rb +36 -0
  20. data/test/signed_float_test/signed_float_test.rb +14 -0
  21. data/test/signed_int_test/java_test/JavaTest.java +83 -0
  22. data/test/signed_int_test/js_test/js_test.js +55 -0
  23. data/test/signed_int_test/ruby_test/ruby_test.rb +37 -0
  24. data/test/signed_int_test/signed_int_test.rb +15 -0
  25. data/test/unify_test/unify_test.rb +24 -13
  26. metadata +14 -38
  27. data/test/basic_complex_test/java_test/test_babel.java +0 -368
  28. data/test/basic_complex_test/js_test/test_babel.js +0 -523
  29. data/test/basic_complex_test/ruby_test/test_babel.rb +0 -368
  30. data/test/binaryTree_test/java_test/test_binaryTree.java +0 -301
  31. data/test/binaryTree_test/js_test/test_binaryTree.js +0 -447
  32. data/test/binaryTree_test/ruby_test/test_binaryTree.rb +0 -303
  33. data/test/complex_test/java_test/test_complex.java +0 -331
  34. data/test/complex_test/js_test/test_complex.js +0 -478
  35. data/test/complex_test/ruby_test/test_complex.rb +0 -330
  36. data/test/ipv6_test/java_test/junit.jar +0 -0
  37. data/test/ipv6_test/java_test/test_ipv6.java +0 -270
  38. data/test/ipv6_test/js_test/test_ipv6.js +0 -409
  39. data/test/ipv6_test/ruby_test/test_ipv6.rb +0 -267
  40. data/test/unify_test/java_test/test_unify.java +0 -171
  41. data/test/unify_test/js_test/js_test.js +0 -56
  42. data/test/unify_test/js_test/test_unify.js +0 -326
  43. data/test/unify_test/ruby_test/ruby_test.rb +0 -35
  44. data/test/unify_test/ruby_test/test_unify.rb +0 -187
@@ -1,331 +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 Complex extends BabelBase {
248
- public ArrayList<HashMap<String, ArrayList<IPList>>> list1 = new ArrayList<HashMap<String, ArrayList<IPList>>>();
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
- HashMap<String, ArrayList<IPList>> var_100 = list1.get(var_101);
256
- writeInt32(var_100.size(), baos);
257
- for(String var_102 : var_100.keySet()) {
258
- ArrayList<IPList> var_103 = var_100.get(var_102);
259
- writeString(var_102, baos);
260
- writeInt32(var_103.size(), baos);
261
- for(int var_105=0; var_105<var_103.size(); var_105++) {
262
- IPList var_104 = var_103.get(var_105);
263
- var_104.serializeInternal(baos);
264
- }
265
- }
266
- }
267
- }
268
-
269
- @Override
270
- public void deserialize(ByteArrayInputStream bais) throws IOException {
271
- // Deserialize list 'list1'
272
- this.list1 = new ArrayList<HashMap<String, ArrayList<IPList>>>();
273
- int var_106 = (int)this.readInt32(bais);
274
- for(int var_108=0; var_108<var_106; var_108++) {
275
- HashMap<String, ArrayList<IPList>> var_107 = new HashMap<String, ArrayList<IPList>>();
276
- int var_109 = (int)readInt32(bais);
277
- for(int var_10c=0; var_10c<var_109; var_10c++) {
278
- String var_10a = readString(bais);
279
- ArrayList<IPList> var_10b = new ArrayList<IPList>();
280
- int var_10d = (int)this.readInt32(bais);
281
- for(int var_10f=0; var_10f<var_10d; var_10f++) {
282
- IPList var_10e = new IPList();
283
- var_10e.deserialize(bais);
284
- var_10b.add(var_10e);
285
- }
286
- var_107.put(var_10a, var_10b);
287
- }
288
- this.list1.add(var_107);
289
- }
290
- }
291
- }
292
-
293
-
294
- class IPList extends BabelBase {
295
- public ArrayList<String> list1 = new ArrayList<String>();
296
- public ArrayList<String> list2 = new ArrayList<String>();
297
-
298
- @Override
299
- void serializeInternal(ByteArrayOutputStream baos) throws IOException {
300
- // Serialize list 'list1'
301
- writeInt32(list1.size(), baos);
302
- for(int var_111=0; var_111<list1.size(); var_111++) {
303
- String var_110 = list1.get(var_111);
304
- writeIpNumber(var_110, baos);
305
- }
306
- // Serialize list 'list2'
307
- writeInt32(list2.size(), baos);
308
- for(int var_113=0; var_113<list2.size(); var_113++) {
309
- String var_112 = list2.get(var_113);
310
- writeIpNumber(var_112, baos);
311
- }
312
- }
313
-
314
- @Override
315
- public void deserialize(ByteArrayInputStream bais) throws IOException {
316
- // Deserialize list 'list1'
317
- this.list1 = new ArrayList<String>();
318
- int var_114 = (int)this.readInt32(bais);
319
- for(int var_116=0; var_116<var_114; var_116++) {
320
- String var_115 = readIpNumber(bais);
321
- this.list1.add(var_115);
322
- }
323
- // Deserialize list 'list2'
324
- this.list2 = new ArrayList<String>();
325
- int var_117 = (int)this.readInt32(bais);
326
- for(int var_119=0; var_119<var_117; var_119++) {
327
- String var_118 = readIpNumber(bais);
328
- this.list2.add(var_118);
329
- }
330
- }
331
- }
@@ -1,478 +0,0 @@
1
- // ------------------------------------------------------------ BabelDataReader
2
- function BabelDataReader(data) {
3
- this.data = data;
4
- this.index = 0;
5
- }
6
-
7
- BabelDataReader.prototype.getbyte = function () {
8
- return this.data[this.index++];
9
- };
10
-
11
- BabelDataReader.prototype.read = function (items) {
12
- var from = this.index;
13
- this.index += items
14
- return this.data.subarray(from, this.index)
15
- };
16
-
17
-
18
- // ------------------------------------------------------------ BabelDataWriter
19
- function BabelDataWriter(data) {
20
- this.data = data;
21
- this.index = 0;
22
- this.data = new Uint8Array(4096);
23
- }
24
-
25
- BabelDataWriter.prototype._realloc = function (size) {
26
- size = size || 4096;
27
- var old_data = this.data;
28
- this.data = new Uint8Array(Math.max(size, 4096) + this.data.length);
29
- this.data.set(old_data, 0);
30
- };
31
-
32
- BabelDataWriter.prototype.writeByte = function (a_byte) {
33
- if (this.index + 1 >= this.data.length) this._realloc();
34
- this.data[this.index++] = a_byte;
35
- };
36
-
37
- BabelDataWriter.prototype.write = function (bytes) {
38
- if (this.index + bytes.length >= this.data.length) this._realloc(bytes.length);
39
- this.data.set(bytes, this.index);
40
- this.index += bytes.length;
41
- };
42
-
43
- BabelDataWriter.prototype.get_data = function () {
44
- return this.data.subarray(0, this.index);
45
- };
46
-
47
-
48
- // ------------------------------------------------------------ BabelHelper
49
- function BabelHelper() {}
50
-
51
- BabelHelper.prototype.serialize = function () {
52
- var out = new BabelDataWriter();
53
- this.serialize_internal(out);
54
- return out.get_data();
55
- }
56
-
57
- BabelHelper.prototype.read_int8 = function (data) {
58
- return data.getbyte();
59
- };
60
-
61
- BabelHelper.prototype.read_int16 = function (data) {
62
- return (data.getbyte() << 8) | this.read_int8(data);
63
- };
64
-
65
- BabelHelper.prototype.read_int24 = function (data) {
66
- return (data.getbyte() << 16) | this.read_int16(data);
67
- };
68
-
69
- BabelHelper.prototype.read_int32 = function (data) {
70
- // return (data.getbyte() << 24) | this.read_int24(data); // See notes about numbers above
71
- return (data.getbyte() * (256*256*256)) + this.read_int24(data);
72
- };
73
-
74
- BabelHelper.prototype.read_binary = function (data) {
75
- return data.read(this.read_int32(data));
76
- };
77
-
78
- BabelHelper.prototype.read_short_binary = function (data) {
79
- return data.read(this.read_int8(data));
80
- };
81
-
82
- BabelHelper.prototype.read_ip_number = function (data) {
83
- var ip_array = this.read_short_binary(data);
84
- if(ip_array.length == 4){
85
- return this.read_ipv4_number(ip_array);
86
- }else{
87
- return this.read_ipv6_number(ip_array);
88
- }
89
- };
90
-
91
- BabelHelper.prototype.read_ipv4_number = function (ip_array) {
92
- ip = "";
93
- for (i = 0, len = ip_array.length; i < len; i++) {
94
- b = ip_array[i];
95
- if (ip.length > 0) {
96
- ip += ".";
97
- }
98
- ip += b.toString();
99
- }
100
- return ip;
101
- };
102
- BabelHelper.prototype.read_ipv6_number = function (ip_array) {
103
- var ip = "";
104
- var part1, part2;
105
- for (i = 0, len = ip_array.length; i < len; i+=2) {
106
- part1 = ip_array[i];
107
- part2 = ip_array[i+1];
108
- ip += part1 == 0? "" : part1.toString(16);
109
- ip += (part1 == 0 && part2 == 0)? "" : (part2 < 10 && part1 != 0? "0" + part2.toString(16): part2.toString(16));
110
- if (i < ip_array.length-2)
111
- ip += ":";
112
- }
113
- ip = ip.replace(/:{3,}/g, "::");
114
- return ip;
115
- };
116
-
117
- BabelHelper.prototype.read_string = function (data) {
118
- return this.decode_utf8(data.read(this.read_int16(data)))
119
- };
120
-
121
- BabelHelper.prototype.write_int8 = function (v, out) {
122
- if (v > 0xFF) // Max 255
123
- this.raise_error("Too large int8 number: " + v);
124
- if(v < 0)
125
- this.raise_error("a negative number passed to int8 number: " + v);
126
- out.writeByte(v);
127
- }
128
-
129
- BabelHelper.prototype.write_int16 = function (v, out) {
130
- if (v > 0xFFFF) // Max 65.535
131
- this.raise_error("Too large int16 number: " + v);
132
- if(v < 0)
133
- this.raise_error("a negative number passed to int16 number: " + v);
134
- this.write_int8(v >> 8 & 0xFF, out);
135
- this.write_int8(v & 0xFF, out);
136
- }
137
-
138
- BabelHelper.prototype.write_int24 = function (v, out) {
139
- if (v > 0xFFFFFF) // Max 16.777.215
140
- this.raise_error("Too large int24 number: " + v);
141
- if (v < 0) // In Case added to JavaScript declaration
142
- this.raise_error("a negative number passed to int24 number: " + v);
143
- this.write_int8(v >> 16 & 0xFF, out);
144
- this.write_int16(v & 0xFFFF, out);
145
- }
146
-
147
- BabelHelper.prototype.write_int32 = function (v, out) {
148
- if (v > 0xFFFFFFFF) // Max 4.294.967.295
149
- this.raise_error("Too large int32 number: " + v);
150
- if(v < 0)
151
- this.raise_error("a negative number passed to int32 number: " + v);
152
- this.write_int8(v >> 24 & 0xFF, out);
153
- this.write_int24(v & 0xFFFFFF, out);
154
- }
155
-
156
- BabelHelper.prototype.write_bool = function (v, out) {
157
- this.write_int8(v ? 1 : 0, out)
158
- }
159
-
160
- BabelHelper.prototype.write_string = function (v, out) {
161
- var s = this.encode_utf8(v);
162
- if (s.length > 0xFFFF) this.raise_error("Too large string: " + s.length + " bytes");
163
- this.write_int16(s.length, out);
164
- out.write(s);
165
- }
166
-
167
- BabelHelper.prototype.write_binary = function (v, out) {
168
- if ((v instanceof Array) || (v instanceof Uint8Array)) {
169
- if (v.length > 0xFFFFFFFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
170
- this.write_int32(v.length, out)
171
- out.write(v);
172
- } else if (v.constructor == String) {
173
- if (v.length > 0xFFFFFFFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
174
- this.write_int32(v.length, out)
175
- out.write(v);
176
- } else if (v == null) {
177
- this.raise_error("Unsupported binary 'null'");
178
- } else {
179
- this.raise_error("Unsupported binary of type '" + v.constructor.name + "'");
180
- }
181
- }
182
-
183
- BabelHelper.prototype.write_16_binary = function (v, out) {
184
- if ((v instanceof Array) || (v instanceof Uint8Array)) {
185
- if (v.length > 0xFF) this.raise_error("Too large 16 binary: " + v.length*2 + " (" + v.constructor.name + ")");
186
- this.write_int8(v.length * 2, out)
187
- for (i = 0, len = v.length; i < len; i++) {
188
- this.write_int16(v[i], out);
189
- }
190
-
191
- } else if (v == null) {
192
- this.raise_error("Unsupported binary 'null'");
193
- } else {
194
- this.raise_error("Unsupported binary of type '" + v.constructor.name + "'");
195
- }
196
- }
197
-
198
- BabelHelper.prototype.write_short_binary = function (v, out) {
199
- if ((v instanceof Array) || (v instanceof Uint8Array)) {
200
- if (v.length > 0xFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
201
- this.write_int8(v.length, out)
202
- out.write(v);
203
- } else if (v.constructor == String) {
204
- if (v.length > 0xFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
205
- this.write_int8(v.length, out)
206
- out.write(v);
207
- } else if (v == null) {
208
- this.raise_error("Unsupported binary 'null'");
209
- } else {
210
- this.raise_error("Unsupported binary of type '" + v.constructor.name + "'");
211
- }
212
- }
213
-
214
- BabelHelper.prototype.write_ip_number = function (v, out) {
215
- if ((v instanceof Array) || (v instanceof Uint8Array)){
216
- if(v.length == 4){
217
- this.write_ipv4_number(v, out);
218
- }else{
219
- this.write_ipv6_number(v, out);
220
- }
221
- }else if(v.constructor == String){
222
- if(/:/g.test(v)){
223
- this.write_ipv6_number(v, out);
224
- }else{
225
- this.write_ipv4_number(v, out);
226
- }
227
- }else{
228
- this.raise_error("Unknown IP number '" + v + "'");
229
- }
230
- }
231
-
232
- BabelHelper.prototype.write_ipv4_number = function (v, out) {
233
- if ((v instanceof Array) || (v instanceof Uint8Array)) {
234
- if (v.length != 4 && v.length != 0) this.raise_error("Unknown IP v4 number " + v);
235
- this.write_short_binary(v, out)
236
- } else if (v.constructor == String) {
237
- var ss = [];
238
- if (v.length > 0) {
239
- ss = v.split(".").map(Number);
240
- }
241
- this.write_ipv4_number(ss, out);
242
- } else {
243
- this.raise_error("Unknown IP number '" + v + "'");
244
- }
245
- };
246
-
247
- BabelHelper.prototype.write_ipv6_number = function (v, out) {
248
- if ((v instanceof Array) || (v instanceof Uint8Array)) {
249
- this.write_16_binary(v, out)
250
- } else if (v.constructor == String) {
251
- var ss = [];
252
- var contains_ipv6_letters = /[0-9a-f]+/gi.test(v);
253
- var contains_other_letters = /[^:0-9a-f]+/gi.test(v);
254
- if (v.length > 0 && v.split(/:{3,}/g).length == 1 && v.split(/:{2}/g).length <= 2 && !contains_other_letters &&
255
- contains_ipv6_letters) {
256
- v = v.replace(/ /g, "");
257
- ss = v.split(":").map(function (t){
258
- if (t.length > 4)
259
- new BabelHelper().raise_error("Unknown IP Group number '" + t + "'");
260
- if (t.length == 0)
261
- t = "0";
262
- return parseInt(t, 16);
263
- });
264
- }
265
- if (!contains_other_letters &&
266
- ( !/::/g.test(v) && ss.length == 0 || ss.length == 8) || ( /::/g.test(v) && ss.length > 2 && ss.length < 8) ) {
267
- this.write_ipv6_number(ss, out);
268
- } else {
269
- this.raise_error("Unknown IP v6 number '" + v + "'");
270
- }
271
-
272
- } else {
273
- this.raise_error("Unknown IP v6 number '" + v + "'");
274
- }
275
- }
276
-
277
- BabelHelper.prototype.encode_utf8 = function (str) {
278
- var utf8 = [];
279
- var chr, next_chr;
280
- var x, y, z;
281
- for (var i = 0; i < str.length; i++) {
282
- chr = str.charCodeAt(i);
283
- if ((chr & 0xFF80) == 0) {
284
- utf8.push(chr);
285
- } else {
286
- if ((chr & 0xFC00) == 0xD800) {
287
- next_chr = str.charCodeAt(i + 1);
288
- if ((next_chr & 0xFC00) == 0xDC00) {
289
- // UTF-16 surrogate pair
290
- chr = (((chr & 0x03FF) << 10) | (next_chr & 0X3FF)) + 0x10000;
291
- i++;
292
- } else {
293
- this.raise_error("Error decoding surrogate pair: " + chr + ", " + next_chr);
294
- }
295
- }
296
- x = chr & 0xFF;
297
- y = chr & 0xFF00;
298
- z = chr & 0xFF0000;
299
-
300
- if (chr <= 0x0007FF) {
301
- utf8.push(0xC0 | (y >> 6) | (x >> 6));
302
- utf8.push(0x80 | (x & 63));
303
- } else if (chr <= 0x00FFFF) {
304
- utf8.push(0xe0 | (y >> 12));
305
- utf8.push(0x80 | ((y >> 6) & 63) | (x >> 6));
306
- utf8.push(0x80 | (x & 63));
307
- } else if (chr <= 0x10FFFF) {
308
- utf8.push(0xF0 | (z >> 18));
309
- utf8.push(0x80 | ((z >> 12) & 63) | (y >> 12));
310
- utf8.push(0x80 | ((y >> 6) & 63) | (x >> 6));
311
- utf8.push(0x80 | (x & 63));
312
- } else {
313
- this.raise_error("Error encoding to UTF8: " + chr + " is greater than U+10FFFF");
314
- }
315
- }
316
- }
317
- return utf8;
318
- }
319
-
320
- BabelHelper.prototype.decode_utf8 = function (utf8_data) {
321
- var str = "";
322
- var chr, b2, b3, b4;
323
- for (var i = 0; i < utf8_data.length; i++) {
324
- chr = utf8_data[i];
325
- if ((chr & 0x80) == 0x00) {}
326
- else if ((chr & 0xF8) == 0xF0) {
327
- // 4 bytes: U+10000 - U+10FFFF
328
- b2 = utf8_data[i + 1];
329
- b3 = utf8_data[i + 2];
330
- b4 = utf8_data[i + 3];
331
- if ((b2 & 0xc0) == 0x80 && (b3 & 0xC0) == 0x80 && (b4 & 0xC0) == 0x80) {
332
- chr = (chr & 7) << 18 | (b2 & 63) << 12 | (b3 & 63) << 6 | (b4 & 63);
333
- i += 3;
334
- } else {
335
- this.raise_error("Error decoding from UTF8: " + chr + "," + b2 + "," + b3 + "," + b4);
336
- }
337
- } else if ((chr & 0xF0) == 0xE0) {
338
- // 3 bytes: U+0800 - U+FFFF
339
- b2 = utf8_data[i + 1];
340
- b3 = utf8_data[i + 2];
341
- if ((b2 & 0xC0) == 0x80 && (b3 & 0xC0) == 0x80) {
342
- chr = (chr & 15) << 12 | (b2 & 63) << 6 | (b3 & 63);
343
- i += 2;
344
- } else {
345
- this.raise_error("Error decoding from UTF8: " + chr + "," + b2 + "," + b3);
346
- }
347
- } else if ((chr & 0xE0) == 0xC0) {
348
- // 2 bytes: U+0080 - U+07FF
349
- b2 = utf8_data[i + 1];
350
- if ((b2 & 0xC0) == 0x80) {
351
- chr = (chr & 31) << 6 | (b2 & 63);
352
- i += 1;
353
- } else {
354
- this.raise_error("Error decoding from UTF8: " + chr + "," + b2);
355
- }
356
- } else {
357
- // 80-BF: Second, third, or fourth byte of a multi-byte sequence
358
- // F5-FF: Start of 4, 5, or 6 byte sequence
359
- this.raise_error("Error decoding from UTF8: " + chr + " encountered not in multi-byte sequence");
360
- }
361
- if (chr <= 0xFFFF) {
362
- str += String.fromCharCode(chr);
363
- } else if (chr > 0xFFFF && chr <= 0x10FFFF) {
364
- // Must be encoded into UTF-16 surrogate pair.
365
- chr -= 0x10000;
366
- str += (String.fromCharCode(0xD800 | (chr >> 10)) + String.fromCharCode(0xDC00 | (chr & 1023)));
367
- } else {
368
- this.raise_error("Error encoding surrogate pair: " + chr + " is greater than U+10ffff");
369
- }
370
- }
371
- return str;
372
- }
373
-
374
- BabelHelper.prototype.raise_error = function (msg) {
375
- throw "[" + this.constructor.name + "] " + msg;
376
- }
377
-
378
-
379
- // ------------------------------------------------------------ Complex
380
- function Complex() {
381
- BabelHelper.call(this);
382
- this.list1 = [];
383
- }
384
-
385
- // Inherit BabelHelper
386
- Complex.prototype = new BabelHelper();
387
-
388
- // Correct the constructor pointer because it points to BabelHelper
389
- Complex.prototype.constructor = Complex;
390
-
391
- // Define the methods of Complex
392
- Complex.prototype.deserialize = function (data) {
393
- // Deserialize list 'list1'
394
- this.list1 = [];
395
- var var_100 = this.read_int32(data);
396
- for(var var_102=0; var_102<var_100; var_102++) {
397
- var var_101 = {};
398
- var var_103 = this.read_int32(data);
399
- for(var var_106=0; var_106<var_103; var_106++) {
400
- var var_104 = this.read_string(data);
401
- var var_105 = [];
402
- var var_107 = this.read_int32(data);
403
- for(var var_109=0; var_109<var_107; var_109++) {
404
- var var_108 = new IPList();
405
- var_108.deserialize(data);
406
- var_105.push(var_108);
407
- }
408
- var_101[var_104] = var_105;
409
- }
410
- this.list1.push(var_101);
411
- }
412
- }
413
-
414
- Complex.prototype.serialize_internal = function(out) {
415
- // Serialize list 'list1'
416
- this.write_int32(this.list1.length, out);
417
- for(var var_10b=0; var_10b<this.list1.length; var_10b++) {
418
- var var_10a = this.list1[var_10b];
419
- var var_10c = Object.keys(var_10a).length;
420
- this.write_int32(var_10c, out);
421
- for(var var_10d in var_10a) {
422
- var var_10e = var_10a[var_10d];
423
- this.write_string(var_10d, out)
424
- this.write_int32(var_10e.length, out);
425
- for(var var_111=0; var_111<var_10e.length; var_111++) {
426
- var var_110 = var_10e[var_111];
427
- var_110.serialize_internal(out)
428
- }
429
- }
430
- }
431
- }
432
-
433
-
434
- // ------------------------------------------------------------ IPList
435
- function IPList() {
436
- BabelHelper.call(this);
437
- this.list1 = [];
438
- this.list2 = [];
439
- }
440
-
441
- // Inherit BabelHelper
442
- IPList.prototype = new BabelHelper();
443
-
444
- // Correct the constructor pointer because it points to BabelHelper
445
- IPList.prototype.constructor = IPList;
446
-
447
- // Define the methods of IPList
448
- IPList.prototype.deserialize = function (data) {
449
- // Deserialize list 'list1'
450
- this.list1 = [];
451
- var var_112 = this.read_int32(data);
452
- for(var var_114=0; var_114<var_112; var_114++) {
453
- var var_113 = this.read_ip_number(data);
454
- this.list1.push(var_113);
455
- }
456
- // Deserialize list 'list2'
457
- this.list2 = [];
458
- var var_115 = this.read_int32(data);
459
- for(var var_117=0; var_117<var_115; var_117++) {
460
- var var_116 = this.read_ip_number(data);
461
- this.list2.push(var_116);
462
- }
463
- }
464
-
465
- IPList.prototype.serialize_internal = function(out) {
466
- // Serialize list 'list1'
467
- this.write_int32(this.list1.length, out);
468
- for(var var_119=0; var_119<this.list1.length; var_119++) {
469
- var var_118 = this.list1[var_119];
470
- this.write_ip_number(var_118, out)
471
- }
472
- // Serialize list 'list2'
473
- this.write_int32(this.list2.length, out);
474
- for(var var_11b=0; var_11b<this.list2.length; var_11b++) {
475
- var var_11a = this.list2[var_11b];
476
- this.write_ip_number(var_11a, out)
477
- }
478
- }