divine 0.0.1 → 0.0.2
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/LICENSE.txt +2 -1
- data/Rakefile +49 -0
- data/lib/divine/code_generators/code_generator.rb +29 -6
- data/lib/divine/code_generators/java.rb +121 -14
- data/lib/divine/code_generators/javascript.rb +100 -11
- data/lib/divine/code_generators/ruby.rb +89 -4
- data/lib/divine/version.rb +1 -1
- data/test/basic_complex_test/basic_complex_test.rb +34 -0
- data/test/basic_complex_test/java_test/JavaTest.java +96 -0
- data/test/basic_complex_test/java_test/test_babel.java +368 -0
- data/test/basic_complex_test/js_test/js_testBasic.js +58 -0
- data/test/basic_complex_test/js_test/js_testComplex.js +68 -0
- data/test/basic_complex_test/js_test/test_babel.js +523 -0
- data/test/basic_complex_test/ruby_test/ruby_test.rb +68 -0
- data/test/basic_complex_test/ruby_test/test_babel.rb +368 -0
- data/test/binaryTree_test/binaryTree_test.rb +19 -0
- data/test/binaryTree_test/java_test/JavaTest.java +114 -0
- data/test/binaryTree_test/java_test/test_binaryTree.java +301 -0
- data/test/binaryTree_test/js_test/js_test.js +76 -0
- data/test/binaryTree_test/js_test/test_binaryTree.js +447 -0
- data/test/binaryTree_test/ruby_test/ruby_test.rb +68 -0
- data/test/binaryTree_test/ruby_test/test_binaryTree.rb +303 -0
- data/test/complex_test/complex_test.rb +23 -0
- data/test/complex_test/java_test/JavaTest.java +126 -0
- data/test/complex_test/java_test/test_complex.java +331 -0
- data/test/complex_test/js_test/js_test.js +69 -0
- data/test/complex_test/js_test/test_complex.js +478 -0
- data/test/complex_test/ruby_test/ruby_test.rb +55 -0
- data/test/complex_test/ruby_test/test_complex.rb +330 -0
- data/test/ipv6_test/ipv6_test.rb +14 -0
- data/test/ipv6_test/java_test/JavaTest.java +77 -0
- data/test/ipv6_test/java_test/junit.jar +0 -0
- data/test/ipv6_test/java_test/test_ipv6.java +270 -0
- data/test/ipv6_test/js_test/js_test.js +60 -0
- data/test/ipv6_test/js_test/test_ipv6.js +409 -0
- data/test/ipv6_test/ruby_test/ruby_test.rb +42 -0
- data/test/ipv6_test/ruby_test/test_ipv6.rb +267 -0
- data/test/java_lib/junit.jar +0 -0
- data/test/unify_test/java_test/test_unify.java +171 -0
- data/test/unify_test/js_test/js_test.js +56 -0
- data/test/unify_test/js_test/test_unify.js +326 -0
- data/test/unify_test/ruby_test/ruby_test.rb +35 -0
- data/test/unify_test/ruby_test/test_unify.rb +187 -0
- data/test/unify_test/unify_test.rb +17 -0
- metadata +77 -3
@@ -0,0 +1,56 @@
|
|
1
|
+
eval(require('fs').readFileSync('./test_unify.js', 'utf8'));
|
2
|
+
var fs = require('fs');
|
3
|
+
var assert = require('assert');
|
4
|
+
|
5
|
+
var obj_ser = buildObject();
|
6
|
+
|
7
|
+
var ca = obj_ser.serialize();
|
8
|
+
serialize(ca);
|
9
|
+
var read = deserialize();
|
10
|
+
|
11
|
+
var obj_deser = new UnifySer();
|
12
|
+
obj_deser.deserialize(new BabelDataReader(read));
|
13
|
+
compare(obj_ser, obj_deser);
|
14
|
+
|
15
|
+
function buildObject(){
|
16
|
+
var obj = new UnifySer();
|
17
|
+
//obj.i8 = 15;
|
18
|
+
//obj.i32 = 154210145;
|
19
|
+
//obj.i16 = 15485;
|
20
|
+
obj.str = "What is This?";
|
21
|
+
//obj.ip = "";
|
22
|
+
obj.map1[2] = 15;
|
23
|
+
obj.map1[3] = 16;
|
24
|
+
return obj;
|
25
|
+
}
|
26
|
+
|
27
|
+
function compare(obj1, obj2){
|
28
|
+
console.log(obj2);
|
29
|
+
console.log(obj1);
|
30
|
+
assert.equal(obj1.map1[2], obj2.map1[2]);
|
31
|
+
assert.equal(obj1.str, obj2.str);
|
32
|
+
}
|
33
|
+
|
34
|
+
function serialize(obj){
|
35
|
+
var bBuffer = new Buffer(obj);
|
36
|
+
fs.writeFileSync(__dirname + '/bin.babel.js', bBuffer, function (err) {
|
37
|
+
if (err) {
|
38
|
+
return console.log(err);
|
39
|
+
}
|
40
|
+
});
|
41
|
+
}
|
42
|
+
|
43
|
+
function deserialize(){
|
44
|
+
var data = fs.readFileSync(__dirname + '/bin.babel.js');
|
45
|
+
data = toArray(data);
|
46
|
+
return data;
|
47
|
+
|
48
|
+
}
|
49
|
+
|
50
|
+
function toArray(buffer) {
|
51
|
+
var view = new Uint8Array(buffer.length);
|
52
|
+
for (var i = 0; i < buffer.length; ++i) {
|
53
|
+
view[i] = buffer[i];
|
54
|
+
}
|
55
|
+
return view;
|
56
|
+
}
|
@@ -0,0 +1,326 @@
|
|
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
|
+
ip = "";
|
85
|
+
for (i = 0, len = ip_array.length; i < len; i++) {
|
86
|
+
b = ip_array[i];
|
87
|
+
if (ip.length > 0) {
|
88
|
+
ip += ".";
|
89
|
+
}
|
90
|
+
ip += b.toString();
|
91
|
+
}
|
92
|
+
return ip;
|
93
|
+
};
|
94
|
+
|
95
|
+
BabelHelper.prototype.read_string = function (data) {
|
96
|
+
return this.decode_utf8(data.read(this.read_int16(data)))
|
97
|
+
};
|
98
|
+
|
99
|
+
BabelHelper.prototype.write_int8 = function (v, out) {
|
100
|
+
if (v > 0xFF) // Max 255
|
101
|
+
this.raise_error("Too large int8 number: " + v);
|
102
|
+
out.writeByte(v);
|
103
|
+
}
|
104
|
+
|
105
|
+
BabelHelper.prototype.write_int16 = function (v, out) {
|
106
|
+
if (v > 0xFFFF) // Max 65.535
|
107
|
+
this.raise_error("Too large int16 number: " + v);
|
108
|
+
this.write_int8(v >> 8 & 0xFF, out);
|
109
|
+
this.write_int8(v & 0xFF, out);
|
110
|
+
}
|
111
|
+
|
112
|
+
BabelHelper.prototype.write_int24 = function (v, out) {
|
113
|
+
if (v > 0xFFFFFF) // Max 16.777.215
|
114
|
+
this.raise_error("Too large int24 number: " + v);
|
115
|
+
this.write_int8(v >> 16 & 0xFF, out);
|
116
|
+
this.write_int16(v & 0xFFFF, out);
|
117
|
+
}
|
118
|
+
|
119
|
+
BabelHelper.prototype.write_int32 = function (v, out) {
|
120
|
+
if (v > 0xFFFFFFFF) // Max 4.294.967.295
|
121
|
+
this.raise_error("Too large int32 number: " + v);
|
122
|
+
this.write_int8(v >> 24 & 0xFF, out);
|
123
|
+
this.write_int24(v & 0xFFFFFF, out);
|
124
|
+
}
|
125
|
+
|
126
|
+
BabelHelper.prototype.write_bool = function (v, out) {
|
127
|
+
this.write_int8(v ? 1 : 0, out)
|
128
|
+
}
|
129
|
+
|
130
|
+
BabelHelper.prototype.write_bool = function (v, out) {
|
131
|
+
this.write_int8(v ? 1 : 0, out)
|
132
|
+
}
|
133
|
+
|
134
|
+
BabelHelper.prototype.write_string = function (v, out) {
|
135
|
+
var s = this.encode_utf8(v);
|
136
|
+
if (s.length > 0xFFFF) this.raise_error("Too large string: " + s.length + " bytes");
|
137
|
+
this.write_int16(s.length, out);
|
138
|
+
out.write(s);
|
139
|
+
}
|
140
|
+
|
141
|
+
BabelHelper.prototype.write_binary = function (v, out) {
|
142
|
+
if ((v instanceof Array) || (v instanceof Uint8Array)) {
|
143
|
+
if (v.length > 0xFFFFFFFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
|
144
|
+
this.write_int32(v.length, out)
|
145
|
+
out.write(v);
|
146
|
+
} else if (v.constructor == String) {
|
147
|
+
if (v.length > 0xFFFFFFFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
|
148
|
+
this.write_int32(v.length, out)
|
149
|
+
out.write(v);
|
150
|
+
} else if (v == null) {
|
151
|
+
this.raise_error("Unsupported binary 'null'");
|
152
|
+
} else {
|
153
|
+
this.raise_error("Unsupported binary of type '" + v.constructor.name + "'");
|
154
|
+
}
|
155
|
+
}
|
156
|
+
|
157
|
+
BabelHelper.prototype.write_short_binary = function (v, out) {
|
158
|
+
if ((v instanceof Array) || (v instanceof Uint8Array)) {
|
159
|
+
if (v.length > 0xFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
|
160
|
+
this.write_int8(v.length, out)
|
161
|
+
out.write(v);
|
162
|
+
} else if (v.constructor == String) {
|
163
|
+
if (v.length > 0xFF) this.raise_error("Too large binary: " + v.length + " (" + v.constructor.name + ")");
|
164
|
+
this.write_int8(v.length, out)
|
165
|
+
out.write(v);
|
166
|
+
} else if (v == null) {
|
167
|
+
this.raise_error("Unsupported binary 'null'");
|
168
|
+
} else {
|
169
|
+
this.raise_error("Unsupported binary of type '" + v.constructor.name + "'");
|
170
|
+
}
|
171
|
+
}
|
172
|
+
|
173
|
+
BabelHelper.prototype.write_ip_number = function (v, out) {
|
174
|
+
if ((v instanceof Array) || (v instanceof Uint8Array)) {
|
175
|
+
if (v.length != 4 && v.length != 0) this.raise_error("Unknown IP v4 number " + v);
|
176
|
+
this.write_short_binary(v, out)
|
177
|
+
} else if (v.constructor == String) {
|
178
|
+
var ss = [];
|
179
|
+
if (v.length > 0) {
|
180
|
+
ss = v.split(".").map(Number);
|
181
|
+
}
|
182
|
+
this.write_ip_number(ss, out);
|
183
|
+
} else {
|
184
|
+
this.raise_error("Unknown IP number '" + v + "'");
|
185
|
+
}
|
186
|
+
}
|
187
|
+
|
188
|
+
BabelHelper.prototype.encode_utf8 = function (str) {
|
189
|
+
var utf8 = [];
|
190
|
+
var chr, next_chr;
|
191
|
+
var x, y, z;
|
192
|
+
for (var i = 0; i < str.length; i++) {
|
193
|
+
chr = str.charCodeAt(i);
|
194
|
+
if ((chr & 0xFF80) == 0) {
|
195
|
+
utf8.push(chr);
|
196
|
+
} else {
|
197
|
+
if ((chr & 0xFC00) == 0xD800) {
|
198
|
+
next_chr = str.charCodeAt(i + 1);
|
199
|
+
if ((next_chr & 0xFC00) == 0xDC00) {
|
200
|
+
// UTF-16 surrogate pair
|
201
|
+
chr = (((chr & 0x03FF) << 10) | (next_chr & 0X3FF)) + 0x10000;
|
202
|
+
i++;
|
203
|
+
} else {
|
204
|
+
this.raise_error("Error decoding surrogate pair: " + chr + ", " + next_chr);
|
205
|
+
}
|
206
|
+
}
|
207
|
+
x = chr & 0xFF;
|
208
|
+
y = chr & 0xFF00;
|
209
|
+
z = chr & 0xFF0000;
|
210
|
+
|
211
|
+
if (chr <= 0x0007FF) {
|
212
|
+
utf8.push(0xC0 | (y >> 6) | (x >> 6));
|
213
|
+
utf8.push(0x80 | (x & 63));
|
214
|
+
} else if (chr <= 0x00FFFF) {
|
215
|
+
utf8.push(0xe0 | (y >> 12));
|
216
|
+
utf8.push(0x80 | ((y >> 6) & 63) | (x >> 6));
|
217
|
+
utf8.push(0x80 | (x & 63));
|
218
|
+
} else if (chr <= 0x10FFFF) {
|
219
|
+
utf8.push(0xF0 | (z >> 18));
|
220
|
+
utf8.push(0x80 | ((z >> 12) & 63) | (y >> 12));
|
221
|
+
utf8.push(0x80 | ((y >> 6) & 63) | (x >> 6));
|
222
|
+
utf8.push(0x80 | (x & 63));
|
223
|
+
} else {
|
224
|
+
this.raise_error("Error encoding to UTF8: " + chr + " is greater than U+10FFFF");
|
225
|
+
}
|
226
|
+
}
|
227
|
+
}
|
228
|
+
return utf8;
|
229
|
+
}
|
230
|
+
|
231
|
+
BabelHelper.prototype.decode_utf8 = function (utf8_data) {
|
232
|
+
var str = "";
|
233
|
+
var chr, b2, b3, b4;
|
234
|
+
for (var i = 0; i < utf8_data.length; i++) {
|
235
|
+
chr = utf8_data[i];
|
236
|
+
if ((chr & 0x80) == 0x00) {}
|
237
|
+
else if ((chr & 0xF8) == 0xF0) {
|
238
|
+
// 4 bytes: U+10000 - U+10FFFF
|
239
|
+
b2 = utf8_data[i + 1];
|
240
|
+
b3 = utf8_data[i + 2];
|
241
|
+
b4 = utf8_data[i + 3];
|
242
|
+
if ((b2 & 0xc0) == 0x80 && (b3 & 0xC0) == 0x80 && (b4 & 0xC0) == 0x80) {
|
243
|
+
chr = (chr & 7) << 18 | (b2 & 63) << 12 | (b3 & 63) << 6 | (b4 & 63);
|
244
|
+
i += 3;
|
245
|
+
} else {
|
246
|
+
this.raise_error("Error decoding from UTF8: " + chr + "," + b2 + "," + b3 + "," + b4);
|
247
|
+
}
|
248
|
+
} else if ((chr & 0xF0) == 0xE0) {
|
249
|
+
// 3 bytes: U+0800 - U+FFFF
|
250
|
+
b2 = utf8_data[i + 1];
|
251
|
+
b3 = utf8_data[i + 2];
|
252
|
+
if ((b2 & 0xC0) == 0x80 && (b3 & 0xC0) == 0x80) {
|
253
|
+
chr = (chr & 15) << 12 | (b2 & 63) << 6 | (b3 & 63);
|
254
|
+
i += 2;
|
255
|
+
} else {
|
256
|
+
this.raise_error("Error decoding from UTF8: " + chr + "," + b2 + "," + b3);
|
257
|
+
}
|
258
|
+
} else if ((chr & 0xE0) == 0xC0) {
|
259
|
+
// 2 bytes: U+0080 - U+07FF
|
260
|
+
b2 = utf8_data[i + 1];
|
261
|
+
if ((b2 & 0xC0) == 0x80) {
|
262
|
+
chr = (chr & 31) << 6 | (b2 & 63);
|
263
|
+
i += 1;
|
264
|
+
} else {
|
265
|
+
this.raise_error("Error decoding from UTF8: " + chr + "," + b2);
|
266
|
+
}
|
267
|
+
} else {
|
268
|
+
// 80-BF: Second, third, or fourth byte of a multi-byte sequence
|
269
|
+
// F5-FF: Start of 4, 5, or 6 byte sequence
|
270
|
+
this.raise_error("Error decoding from UTF8: " + chr + " encountered not in multi-byte sequence");
|
271
|
+
}
|
272
|
+
if (chr <= 0xFFFF) {
|
273
|
+
str += String.fromCharCode(chr);
|
274
|
+
} else if (chr > 0xFFFF && chr <= 0x10FFFF) {
|
275
|
+
// Must be encoded into UTF-16 surrogate pair.
|
276
|
+
chr -= 0x10000;
|
277
|
+
str += (String.fromCharCode(0xD800 | (chr >> 10)) + String.fromCharCode(0xDC00 | (chr & 1023)));
|
278
|
+
} else {
|
279
|
+
this.raise_error("Error encoding surrogate pair: " + chr + " is greater than U+10ffff");
|
280
|
+
}
|
281
|
+
}
|
282
|
+
return str;
|
283
|
+
}
|
284
|
+
|
285
|
+
BabelHelper.prototype.raise_error = function (msg) {
|
286
|
+
throw "[" + this.constructor.name + "] " + msg;
|
287
|
+
}
|
288
|
+
|
289
|
+
|
290
|
+
// ------------------------------------------------------------ UnifySer
|
291
|
+
function UnifySer() {
|
292
|
+
BabelHelper.call(this);
|
293
|
+
this.str = "";
|
294
|
+
this.map1 = {};
|
295
|
+
}
|
296
|
+
|
297
|
+
// Inherit BabelHelper
|
298
|
+
UnifySer.prototype = new BabelHelper();
|
299
|
+
|
300
|
+
// Correct the constructor pointer because it points to BabelHelper
|
301
|
+
UnifySer.prototype.constructor = UnifySer;
|
302
|
+
|
303
|
+
// Define the methods of UnifySer
|
304
|
+
UnifySer.prototype.deserialize = function (data) {
|
305
|
+
this.str = this.read_string(data);
|
306
|
+
// Deserialize map 'map1'
|
307
|
+
this.map1 = {};
|
308
|
+
var var_100 = this.read_int32(data);
|
309
|
+
for(var var_103=0; var_103<var_100; var_103++) {
|
310
|
+
var var_101 = this.read_int8(data);
|
311
|
+
var var_102 = this.read_int32(data);
|
312
|
+
this.map1[var_101] = var_102;
|
313
|
+
}
|
314
|
+
}
|
315
|
+
|
316
|
+
UnifySer.prototype.serialize_internal = function(out) {
|
317
|
+
this.write_string(this.str, out);
|
318
|
+
// Serialize map 'map1'
|
319
|
+
var var_104 = Object.keys(this.map1).length;
|
320
|
+
this.write_int32(var_104, out);
|
321
|
+
for(var var_105 in this.map1) {
|
322
|
+
var var_106 = this.map1[var_105];
|
323
|
+
this.write_int8(var_105, out)
|
324
|
+
this.write_int32(var_106, out)
|
325
|
+
}
|
326
|
+
}
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require_relative 'test_unify.rb'
|
2
|
+
require 'minitest/autorun'
|
3
|
+
|
4
|
+
class TestBabelTestBasic < MiniTest::Unit::TestCase
|
5
|
+
|
6
|
+
def test_unify
|
7
|
+
obj_ser = BabelTest::UnifySer.new
|
8
|
+
obj_ser.map1 = {2 => 15, 3 => 16}
|
9
|
+
obj_ser.str = "What is This?"
|
10
|
+
#ser = obj.serialize
|
11
|
+
#serialize(ser)
|
12
|
+
res = deserialize()
|
13
|
+
|
14
|
+
obj_deser = BabelTest::UnifySer.new
|
15
|
+
obj_deser.deserialize res
|
16
|
+
compare(obj_ser, obj_deser)
|
17
|
+
|
18
|
+
end
|
19
|
+
|
20
|
+
def compare(obj_ser, obj_deser)
|
21
|
+
puts obj_deser.str
|
22
|
+
assert obj_ser.map1 == obj_deser.map1
|
23
|
+
end
|
24
|
+
|
25
|
+
def serialize(data)
|
26
|
+
File.open("bin.babel.rb", "w+b") do |f|
|
27
|
+
f.write(data)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def deserialize()
|
32
|
+
mem_buf = File.new('bin.babel.js').binmode
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
@@ -0,0 +1,187 @@
|
|
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
|
+
read_short_binary(data).bytes.to_a.join('.')
|
48
|
+
end
|
49
|
+
|
50
|
+
### Write methods ###
|
51
|
+
def write_int8(v, out)
|
52
|
+
v = v.to_i
|
53
|
+
raise_error "Too large int8 number: #{v}" if v > 0xFF # Max 255
|
54
|
+
out << v
|
55
|
+
end
|
56
|
+
|
57
|
+
def write_int16(v, out)
|
58
|
+
v = v.to_i
|
59
|
+
raise_error "Too large int16 number: #{v}" if v > 0xFFFF # Max 65.535
|
60
|
+
write_int8( v >> 8 & 0xFF, out)
|
61
|
+
write_int8( v & 0xFF, out)
|
62
|
+
end
|
63
|
+
|
64
|
+
def write_int24(v, out)
|
65
|
+
v = v.to_i
|
66
|
+
raise_error "Too large int24 number: #{v}" if v > 0xFFFFFF # Max 16.777.215
|
67
|
+
write_int8( v >> 16 & 0xFF, out)
|
68
|
+
write_int16( v & 0xFFFF, out)
|
69
|
+
end
|
70
|
+
|
71
|
+
def write_int32(v, out)
|
72
|
+
v = v.to_i
|
73
|
+
raise_error "Too large int32 number: #{v}" if v > 0xFFFFFFFF # Max 4.294.967.295
|
74
|
+
write_int8( v >> 24 & 0xFF, out)
|
75
|
+
write_int24( v & 0xFFFFFF, out)
|
76
|
+
end
|
77
|
+
|
78
|
+
def write_bool(v, out)
|
79
|
+
write_int8(v ? 1 : 0, out)
|
80
|
+
end
|
81
|
+
|
82
|
+
def write_string(v, out)
|
83
|
+
s = force_to_utf8_string(v)
|
84
|
+
raise_error "Too large string: #{s.bytesize} bytes" if s.bytesize > 0xFFFF
|
85
|
+
write_int16(s.bytesize, out)
|
86
|
+
out << s.bytes.to_a
|
87
|
+
end
|
88
|
+
|
89
|
+
def write_binary(v, out)
|
90
|
+
if v.is_a?(Array)
|
91
|
+
raise_error "Too large binary: #{v.size} (#{v.class.name})" unless v.size < 0xFFFFFFFF
|
92
|
+
write_int32(v.size, out)
|
93
|
+
v.each do |x|
|
94
|
+
write_int8(x, out)
|
95
|
+
end
|
96
|
+
elsif v.is_a?(String)
|
97
|
+
raise_error "Too large binary: #{v.size} (#{v.class.name})" unless v.size < 0xFFFFFFFF
|
98
|
+
write_int32(v.size, out)
|
99
|
+
out << v.bytes.to_a
|
100
|
+
else
|
101
|
+
raise_error "Unsupported binary 'nil'" if v == nil
|
102
|
+
raise_error "Unsupported binary of type '#{v.class.name}'"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def write_short_binary(v, out)
|
107
|
+
if v.is_a?(Array)
|
108
|
+
raise_error "Too large short_binary: #{v.size} (#{v.class.name})" unless v.size < 0xFF
|
109
|
+
write_int8(v.size, out)
|
110
|
+
v.each do |x|
|
111
|
+
write_int8(x, out)
|
112
|
+
end
|
113
|
+
elsif v.is_a?(String)
|
114
|
+
raise_error "To large short_binary: #{v.size} (#{v.class.name})" unless v.size < 0xFF
|
115
|
+
write_int8(v.size, out)
|
116
|
+
out << v.bytes.to_a
|
117
|
+
else
|
118
|
+
raise_error "Unsupported binary 'nil'" if v == nil
|
119
|
+
raise_error "Unsupported binary of type '#{v.class.name}'"
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def write_ip_number(v, out)
|
124
|
+
if v.is_a?(Array)
|
125
|
+
raise_error "Unknown IP v4 number #{v}" unless v.size == 0 || v.size == 4 # Only IPv4 for now
|
126
|
+
write_short_binary(v, out)
|
127
|
+
elsif v.is_a?(String)
|
128
|
+
ss = v.split(/\./).map do |s|
|
129
|
+
s.to_i
|
130
|
+
end
|
131
|
+
write_ip_number(ss, out)
|
132
|
+
else
|
133
|
+
raise_error "Unknown IP number '#{v}'"
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
private
|
138
|
+
def raise_error(msg)
|
139
|
+
raise "[#{self.class.name}] #{msg}"
|
140
|
+
end
|
141
|
+
|
142
|
+
def force_to_utf8_string(string)
|
143
|
+
if string.encoding != Encoding::UTF_8
|
144
|
+
string = string.encode(Encoding::UTF_8)
|
145
|
+
end
|
146
|
+
return string
|
147
|
+
end
|
148
|
+
end
|
149
|
+
|
150
|
+
|
151
|
+
|
152
|
+
class UnifySer < BabelBase
|
153
|
+
attr_accessor :str, :map1
|
154
|
+
|
155
|
+
def initialize()
|
156
|
+
super
|
157
|
+
@str ||= ""
|
158
|
+
@map1 ||= {}
|
159
|
+
end
|
160
|
+
|
161
|
+
def serialize_internal(out)
|
162
|
+
print "+"
|
163
|
+
write_string(str, out)
|
164
|
+
# Serialize map 'map1'
|
165
|
+
write_int32(map1.size, out)
|
166
|
+
map1.each_pair do |var_100, var_101|
|
167
|
+
write_int8(var_100, out)
|
168
|
+
write_int32(var_101, out)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
def deserialize(data)
|
173
|
+
print "-"
|
174
|
+
@str = read_string(data)
|
175
|
+
# Deserialize map 'map1'
|
176
|
+
@map1 = {}
|
177
|
+
var_102 = read_int32(data)
|
178
|
+
(1..var_102).each do
|
179
|
+
var_103 = read_int8(data)
|
180
|
+
var_104 = read_int32(data)
|
181
|
+
@map1[var_103] = var_104
|
182
|
+
end
|
183
|
+
end
|
184
|
+
end
|
185
|
+
|
186
|
+
|
187
|
+
end
|