-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbilbinary.js
413 lines (348 loc) · 10.7 KB
/
bilbinary.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* -*- indent-tabs-mode: nil; js-indent-level: 2 -*-
*
* Copyright (c) 2017 Sony Global Education, Inc.
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
const { compactify, uncompactify } = require('./compactify');
const dict = require('./keyword_dict.json').map;
const idict = require('./insn_dict.json').map;
const BT_NUMBER = 0x01; // floating point number
const BT_KEYWORD = 0x02; // keyword
const BT_OBJECT = 0x03; // json object
const BT_ARRAY = 0x04; // json array
const BT_INT8 = 0x05; // 8 bit integer
const BT_INT16 = 0x06; // 16 bit integer
const BT_INT32 = 0x07; // 32 bit integer
const BT_INT8ARRAY = 0x08; // 8 bit integer array
const byteLength = obj => Buffer.byteLength(obj, 'utf-8');
const { keyLength, keyEncode } = (() => {
return {
keyLength: key => key ? 2 : 0,
keyEncode: (b, key, offset) => {
if (key) {
let code = dict[key];
if (!code) {
code = parseInt(key);
if (`${code}` !== key)
throw new Error(`Failed to convert key ${key} to code`);
}
if (code > 65535)
throw new Error(`Code ${code} for keyword ${key} too big`);
b.writeUInt16LE(code, offset);
}
}
};
})();
const pack_key = (b, key, type) => {
let offset = 0;
b.writeUInt8(type, offset);
offset += 1;
keyEncode(b, key, offset);
offset += keyLength(key);
return offset;
};
const header_size = key => 1 + keyLength(key); // type followed by key
const elist_nul = false;
const elist_hsize = 2;
const elist_size = (s) => {
if (elist_nul)
return s + 1;
return s;
};
const check_offset = (tag, key, obj, b, offset) => {
if (b.length != offset) {
const msg = `${obj} for ${key}: ${b.length} != ${offset}`;
throw new Error(`Failed to pack ${tag}: ${msg}`);
}
};
const pack_number = (key, obj) => {
let b = Buffer.allocUnsafe(header_size(key) + 4);
let offset = pack_key(b, key, BT_NUMBER);
b.writeFloatLE(obj, offset);
offset += 4;
check_offset('number', key, obj, b, offset);
return b;
};
const pack_keyword = (key, obj) => {
let b = Buffer.allocUnsafe(header_size(key) + 2);
let offset = pack_key(b, key, BT_KEYWORD);
const code = key === 'name' ? idict[obj] : dict[obj];
if (!code)
throw new Error(`Failed to convert value ${obj} to code`);
b.writeUInt16LE(code, offset);
offset += 2;
check_offset('keyword', key, obj, b, offset);
return b;
};
const pack_elist = (bs) => {
const hsize = elist_hsize;
const bslen = bs.reduce((acc, x) => acc + x.length, 0);
let b = Buffer.allocUnsafe(elist_size(hsize + bslen));
let offset = bs.reduce((acc, x) => {
x.copy(b, acc);
return acc + x.length;
}, hsize);
if (elist_nul) {
b.writeUInt8(0, offset);
offset += 1;
}
if (hsize === 4)
b.writeUInt32LE(offset, 0);
else {
if (offset > 65535)
throw new Error(`offset ${offset} too large`);
b.writeUInt16LE(offset, 0);
}
return b;
}
const pack_document = (obj, array) => {
const bs = Object.keys(obj).map(key => pack_keyvalue(key, obj[key]));
return pack_elist(bs);
};
const pack_object = (type, key, obj, array) => {
const doc = pack_document(obj, array);
let b = Buffer.allocUnsafe(header_size(key) + doc.length);
const offset = pack_key(b, key, type);
doc.copy(b, offset);
return b;
};
const pack_array = (key, obj) => {
// return pack_object(BT_ARRAY, key, obj.reduce((acc, x, idx) => {
// acc[`${idx}`] = x;
// return acc;
// }, {}), true);
const bs = obj.map(x => pack_keyvalue(null, x));
const doc = pack_elist(bs);
const type = BT_ARRAY;
let b = Buffer.allocUnsafe(header_size(key) + doc.length);
const offset = pack_key(b, key, type);
doc.copy(b, offset);
return b;
};
const pack_int8array = (key, obj) => {
const bs = [new Buffer(obj)];
const doc = pack_elist(bs);
const type = BT_INT8ARRAY;
let b = Buffer.allocUnsafe(header_size(key) + doc.length);
const offset = pack_key(b, key, type);
doc.copy(b, offset);
return b;
};
const { int8_p, int16_p, int32_p } = (() => {
return [
{ name: 'int8_p', min: -128, max: 127 },
{ name: 'int16_p', min: -32768, max: 32767 },
{ name: 'int32_p', min: -2147483648, max: 2147483647 }
].reduce((acc, x) => {
acc[x.name] = obj => {
return Number.isInteger(obj) && obj >= x.min && obj <= x.max;
};
return acc;
}, {});
})();
const int8array_p = obj => obj instanceof Array && obj.every(int8_p);
const pack_int8 = (key, obj) => {
let b = Buffer.allocUnsafe(header_size(key) + 1);
let offset = pack_key(b, key, BT_INT8);
b.writeInt8(obj, offset);
offset += 1;
check_offset('int8', key, obj, b, offset);
return b;
};
const pack_int16 = (key, obj) => {
let b = Buffer.allocUnsafe(header_size(key) + 2);
let offset = pack_key(b, key, BT_INT16);
b.writeInt16LE(obj, offset);
offset += 2;
check_offset('int16', key, obj, b, offset);
return b;
};
const pack_int32 = (key, obj) => {
let b = Buffer.allocUnsafe(header_size(key) + 4);
let offset = pack_key(b, key, BT_INT32);
b.writeInt32LE(obj, offset);
offset += 4;
check_offset('int32', key, obj, b, offset);
return b;
};
const pack_keyvalue = (key, obj) => {
if (typeof obj === 'number') {
if (int8_p(obj))
return pack_int8(key, obj);
if (int16_p(obj))
return pack_int16(key, obj);
// if (int32_p(obj))
// return pack_int32(key, obj);
return pack_number(key, obj);
}
if (typeof obj === 'string')
return pack_keyword(key, obj);
if (int8array_p(obj) && obj.length > 0)
return pack_int8array(key, obj);
if (obj instanceof Array)
return pack_array(key, obj);
if (obj instanceof Object)
return pack_object(BT_OBJECT, key, obj, false);
throw new Error(`Unsupported value: ${obj} for key ${key}`);
};
const read_esize = (b, offset) => {
const hsize = elist_hsize;
if (offset + hsize > b.length)
throw new Error(`Buffer too small: ${offset + hsize} > ${b.length}`);
const esize = hsize === 4 ? b.readUInt32LE(offset) : b.readUInt16LE(offset);
if (offset + esize > b.length)
throw new Error(`Element size too large: ${offset + esize} > ${b.length}`);
if (esize < hsize)
throw new Error(`Element size too small: ${esize} < ${hsize}`);
return { esize, hsize };
};
const unpack_elist = (b, offset, obj, unpack) => {
let { hsize, esize } = read_esize(b, offset);
if (elist_nul)
throw new Error(`Elist with terminating NUL is not supported yet`);
esize += offset;
offset += hsize;
while (offset < esize)
offset = unpack(b, offset, obj);
return offset;
}
const unpack_object = (b, offset, push) => {
let obj = {};
offset = unpack_elist(b, offset, obj, unpack_keyvalue);
push(obj);
return offset;
}
const unpack_array = (b, offset, push) => {
let obj = [];
offset = unpack_elist(b, offset, obj, unpack_value);
push(obj);
return offset;
}
const unpack_int8array = (b, offset, push) => {
let obj = [];
let { hsize, esize } = read_esize(b, offset);
esize += offset;
offset += hsize;
while (offset < esize) {
obj.push(b.readInt8(offset));
offset++;
}
push(obj);
return offset;
}
const invert_dict = (dict) => {
return Object.keys(dict).reduce((acc, key) => {
acc[dict[key]] = key;
return acc;
}, {});
};
const dict_inv = invert_dict(dict);
const idict_inv = invert_dict(idict);
const unpack_element = (type, b, offset, push) => {
switch (type) {
case BT_INT8:
if (b.length < 1)
throw new Error(`Buffer too small for INT8: ${b}`);
push(b.readInt8(offset));
return offset + 1;
case BT_INT16:
if (b.length < 2)
throw new Error(`Buffer too small for INT16: ${b}`);
push(b.readInt16LE(offset));
return offset + 2;
case BT_NUMBER:
if (b.length < 4)
throw new Error(`Buffer too small for Number: ${b}`);
push(b.readFloatLE(offset));
return offset + 4;
case BT_KEYWORD:
if (b.length < 2)
throw new Error(`Buffer too small for Keyword: ${b}`);
push(b.readInt16LE(offset));
return offset + 2;
case BT_ARRAY:
return unpack_array(b, offset, push);
case BT_INT8ARRAY:
return unpack_int8array(b, offset, push);
case BT_OBJECT:
return unpack_object(b, offset, push);
default:
throw new Error(`Unsupported type: ${type}`);
}
};
const unpack_value = (b, offset, obj) => {
if (b.length < 1)
throw new Error(`Buffer too small: ${b}`);
const type = b[offset];
const push = (v) => {
if (type === BT_KEYWORD) {
if (!dict_inv[v])
throw new Error(`Unknown keyword code: ${v}`);
v = dict_inv[v];
}
obj.push(v);
};
return unpack_element(type, b, offset + 1, push);
};
const unpack_keyvalue = (b, offset, obj) => {
if (b.length < 3)
throw new Error(`Buffer too small: ${b}`);
const type = b[offset];
const keyCode = b.readUInt16LE(offset + 1);
const key = dict_inv[keyCode];
if (!key)
throw new Error(`Unknown key code: ${keyCode}`);
const push = (v) => {
if (type === BT_KEYWORD) {
const dict = key === 'name' ? idict_inv : dict_inv;
if (!dict[v])
throw new Error(`Unknown keyword code: ${v}`);
v = dict[v];
}
obj[key] = v;
};
return unpack_element(type, b, offset + 3, push);
};
const unpack_document = (b) => {
let obj = {};
unpack_elist(b, 0, obj, unpack_keyvalue);
return obj;
};
const serialize = (obj) => {
//process.stderr.write(JSON.stringify(obj));
return pack_document(obj, false);
};
const deserialize = (b) => {
//process.stderr.write(JSON.stringify(obj));
return unpack_document(b);
};
function Translator(scripts)
{
this.translate = () => serialize(compactify(scripts));
this.serialize = (script) => serialize(script);
this.deserialize = (b) => deserialize(b);
}
module.exports = {
translator: function(scripts) {
return new Translator(scripts);
}
};