-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathbinarypack.js
334 lines (311 loc) · 10.1 KB
/
binarypack.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
var BufferCursor = require('buffercursor');
var BufferBuilder = require('./bufferbuilder');
BinaryPack = {
unpack: function(data){
var unpacker = new BinaryPack.Unpacker(data);
return unpacker.unpack();
},
pack: function(data){
var packer = new BinaryPack.Packer();
var buffer = packer.pack(data);
return buffer;
}
};
BinaryPack.Unpacker = function(buffer){
this.cursor = new BufferCursor(buffer);
this.length = buffer.length;
}
BinaryPack.Unpacker.prototype.unpack = function(){
var type = this.cursor.readUInt8();
if (type < 0x80){
var positive_fixnum = type;
return positive_fixnum;
} else if ((type ^ 0xe0) < 0x20){
var negative_fixnum = (type ^ 0xe0) - 0x20;
return negative_fixnum;
}
var size;
if ((size = type ^ 0xa0) <= 0x0f){
return this.readraw(size);
} else if ((size = type ^ 0xb0) <= 0x0f){
return this.readstring(size);
} else if ((size = type ^ 0x90) <= 0x0f){
return this.readarray(size);
} else if ((size = type ^ 0x80) <= 0x0f){
return this.readmap(size);
}
switch(type){
case 0xc0:
return null;
case 0xc1:
return undefined;
case 0xc2:
return false;
case 0xc3:
return true;
case 0xca:
return this.cursor.readFloatBE();
case 0xcb:
return this.cursor.readDoubleBE();
case 0xcc:
return this.cursor.readUInt8();
case 0xcd:
return this.cursor.readUInt16BE();
case 0xce:
return this.cursor.readUInt32BE();
case 0xcf:
return this.readUInt64();
case 0xd0:
return this.cursor.readInt8();
case 0xd1:
return this.cursor.readInt16BE();
case 0xd2:
return this.cursor.readInt32BE();
case 0xd3:
return this.readInt64();
case 0xd4:
return undefined;
case 0xd5:
return undefined;
case 0xd6:
return undefined;
case 0xd7:
return undefined;
case 0xd8:
size = this.cursor.readUInt16BE();
return this.readstring(size);
case 0xd9:
size = this.cursor.readUInt32BE();
return this.readstring(size);
case 0xda:
size = this.cursor.readUInt16BE();
return this.readraw(size);
case 0xdb:
size = this.cursor.readUInt32BE();
return this.readraw(size);
case 0xdc:
size = this.cursor.readUInt16BE();
return this.readarray(size);
case 0xdd:
size = this.cursor.readUInt32BE();
return this.readarray(size);
case 0xde:
size = this.cursor.readUInt16BE();
return this.readmap(size);
case 0xdf:
size = this.cursor.readUInt32BE();
return this.readmap(size);
}
}
BinaryPack.Unpacker.prototype.readUInt64 = function(){
var bytes = this.cursor.slice(8);
return ((((((bytes.buffer[0] * 256 +
bytes.buffer[1]) * 256 +
bytes.buffer[2]) * 256 +
bytes.buffer[3]) * 256 +
bytes.buffer[4]) * 256 +
bytes.buffer[5]) * 256 +
bytes.buffer[6]) * 256 +
bytes.buffer[7];
}
BinaryPack.Unpacker.prototype.readInt64 = function(){
var uint64 = this.readUInt64();
return (uint64 < Math.pow(2, 63) ) ? uint64 : uint64 - Math.pow(2, 64);
}
BinaryPack.Unpacker.prototype.readraw = function(size){
return this.cursor.slice(size).buffer;
}
BinaryPack.Unpacker.prototype.readstring = function(size){
return this.cursor.toString('utf8', size);
}
BinaryPack.Unpacker.prototype.readarray = function(size){
var objects = new Array(size);
for(var i = 0; i < size ; i++){
objects[i] = this.unpack();
}
return objects;
}
BinaryPack.Unpacker.prototype.readmap = function(size){
var map = {};
for(var i = 0; i < size ; i++){
var key = this.unpack();
var value = this.unpack();
map[key] = value;
}
return map;
}
BinaryPack.Packer = function(){
this.bufferBuilder = new BufferBuilder();
}
BinaryPack.Packer.prototype.pack = function(value){
var type = typeof(value);
if (type == 'string'){
this.pack_string(value);
} else if (type == 'number'){
if (Math.floor(value) === value){
this.pack_integer(value);
} else{
this.pack_double(value);
}
} else if (type == 'boolean'){
if (value === true){
this.bufferBuilder.append(0xc3, 2);
} else if (value === false){
this.bufferBuilder.append(0xc2, 2);
}
} else if (type == 'undefined'){
this.bufferBuilder.append(0xc0, 2);
} else if (type == 'object'){
if (value === null){
this.bufferBuilder.append(0xc0, 2);
} else {
var constructor = value.constructor;
if (constructor == Array){
this.pack_array(value);
} else if (Buffer.isBuffer(value)) {
this.pack_bin(value);
} else if (constructor == Object){
this.pack_object(value);
} else if (constructor == Date){
this.pack_string(value.toString());
} else if (typeof value.toBinaryPack == 'function'){
this.bufferBuilder.append(value.toBinaryPack(), 1);
} else {
throw new Error('Type "' + constructor.toString() + '" not yet supported');
}
}
} else {
throw new Error('Type "' + type + '" not yet supported');
}
return this.bufferBuilder.getBuffer();
}
BinaryPack.Packer.prototype.pack_bin = function(blob){
var length = blob.length || blob.byteLength || blob.size;
if (length <= 0x0f){
this.bufferBuilder.append(0xa0 + length, 2);
} else if (length <= 0xffff){
this.bufferBuilder.append(0xda, 2) ;
this.bufferBuilder.append(length, 3);
} else if (length <= 0xffffffff){
this.bufferBuilder.append(0xdb, 2);
this.bufferBuilder.append(length, 4);
} else{
throw new Error('Invalid length');
return;
}
this.bufferBuilder.append(blob, 1);
}
BinaryPack.Packer.prototype.pack_string = function(str){
var length = Buffer.byteLength(str);
if (length <= 0x0f){
this.bufferBuilder.append(0xb0 + length, 2);
} else if (length <= 0xffff){
this.bufferBuilder.append(0xd8, 2) ;
this.bufferBuilder.append(length, 3);
} else if (length <= 0xffffffff){
this.bufferBuilder.append(0xd9, 2);
this.bufferBuilder.append(length, 4);
} else{
throw new Error('Invalid length');
return;
}
this.bufferBuilder.append(str, 0);
}
BinaryPack.Packer.prototype.pack_array = function(ary){
var length = ary.length;
if (length <= 0x0f){
this.bufferBuilder.append(0x90 + length, 2);
} else if (length <= 0xffff){
this.bufferBuilder.append(0xdc, 2)
this.bufferBuilder.append(length, 3);
} else if (length <= 0xffffffff){
this.bufferBuilder.append(0xdd, 2);
this.bufferBuilder.append(length, 4);
} else{
throw new Error('Invalid length');
}
for(var i = 0; i < length ; i++){
this.pack(ary[i]);
}
}
BinaryPack.Packer.prototype.pack_integer = function(num){
if ( -0x20 <= num && num <= 0x7f){
this.bufferBuilder.append(num & 0xff, 2);
} else if (0x00 <= num && num <= 0xff){
this.bufferBuilder.append(0xcc, 2);
this.bufferBuilder.append(num, 2);
} else if (-0x80 <= num && num <= 0x7f){
this.bufferBuilder.append(0xd0, 2);
this.bufferBuilder.append(num, 5);
} else if ( 0x0000 <= num && num <= 0xffff){
this.bufferBuilder.append(0xcd, 2);
this.bufferBuilder.append(num, 3);
} else if (-0x8000 <= num && num <= 0x7fff){
this.bufferBuilder.append(0xd1, 2);
this.bufferBuilder.append(num, 6);
} else if ( 0x00000000 <= num && num <= 0xffffffff){
this.bufferBuilder.append(0xce, 2);
this.bufferBuilder.append(num, 4);
} else if (-0x80000000 <= num && num <= 0x7fffffff){
this.bufferBuilder.append(0xd2, 2);
this.bufferBuilder.append(num, 7);
} else if (-0x8000000000000000 <= num && num <= 0x7FFFFFFFFFFFFFFF){
this.bufferBuilder.append(0xd3, 2);
this.pack_int64(num);
} else if (0x0000000000000000 <= num && num <= 0xFFFFFFFFFFFFFFFF){
this.bufferBuilder.append(0xcf);
this.pack_uint64(num);
} else{
throw new Error('Invalid integer');
}
}
BinaryPack.Packer.prototype.pack_double = function(num){
this.bufferBuilder.append(0xcb, 2);
this.bufferBuilder.append(num, 9);
}
BinaryPack.Packer.prototype.pack_object = function(obj){
var keys = Object.keys(obj);
var length = keys.length;
if (length <= 0x0f){
this.bufferBuilder.append(0x80 + length, 2);
} else if (length <= 0xffff){
this.bufferBuilder.append(0xde, 2);
this.bufferBuilder.append(length, 3);
} else if (length <= 0xffffffff){
this.bufferBuilder.append(0xdf, 2);
this.bufferBuilder.append(length, 4);
} else{
throw new Error('Invalid length');
}
for(var prop in obj){
if (obj.hasOwnProperty(prop)){
this.pack(prop);
this.pack(obj[prop]);
}
}
}
BinaryPack.Packer.prototype.pack_int64 = function(num){
var high = Math.floor(num / Math.pow(2, 32));
var low = num % Math.pow(2, 32);
this.bufferBuilder.append((high & 0xff000000) >>> 24, 2);
this.bufferBuilder.append((high & 0x00ff0000) >>> 16, 2);
this.bufferBuilder.append((high & 0x0000ff00) >>> 8, 2);
this.bufferBuilder.append((high & 0x000000ff), 2);
this.bufferBuilder.append((low & 0xff000000) >>> 24, 2);
this.bufferBuilder.append((low & 0x00ff0000) >>> 16, 2);
this.bufferBuilder.append((low & 0x0000ff00) >>> 8, 2);
this.bufferBuilder.append((low & 0x000000ff), 2);
}
BinaryPack.Packer.prototype.pack_uint64 = function(num){
var high = num / Math.pow(2, 32);
var low = num % Math.pow(2, 32);
this.bufferBuilder.append((high & 0xff000000) >>> 24, 2);
this.bufferBuilder.append((high & 0x00ff0000) >>> 16, 2);
this.bufferBuilder.append((high & 0x0000ff00) >>> 8, 2);
this.bufferBuilder.append((high & 0x000000ff), 2);
this.bufferBuilder.append((low & 0xff000000) >>> 24, 2);
this.bufferBuilder.append((low & 0x00ff0000) >>> 16, 2);
this.bufferBuilder.append((low & 0x0000ff00) >>> 8, 2);
this.bufferBuilder.append((low & 0x000000ff), 2);
}
module.exports = BinaryPack;