-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathdecoder.js
More file actions
645 lines (563 loc) · 13.7 KB
/
decoder.js
File metadata and controls
645 lines (563 loc) · 13.7 KB
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
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
/**!
* hessian.js - lib/v1/decoder.js
* Copyright(c) 2014
* MIT Licensed
*
* Authors:
* dead_horse <dead_horse@qq.com> (http://deadhorse.me)
* fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
*/
'use strict';
var debug = require('debug')('hessian:v1:decoder');
var ByteBuffer = require('byte');
var is = require('is-type-of');
var utils = require('../utils');
var object = require('../object');
var JavaExceptionError = object.JavaExceptionError;
var BYTE_CODES = {};
function Decoder(buf) {
this.byteBuffer = buf ? ByteBuffer.wrap(buf) : null;
this.refMap = {};
this.refId = 0;
this.BYTE_CODES = BYTE_CODES;
}
/**
* prototype of Decoder
*/
var proto = Decoder.prototype;
proto.throwError = function (method, code) {
throw new TypeError('hessian ' + method + ' error, unexpect code: 0x' + code.toString(16));
};
proto._addRef = function (obj) {
this.refMap[this.refId++] = obj;
};
/**
* init from a buffer
* @param {Buffer} buf
* @api public
*/
proto.init = function (buf) {
this.byteBuffer = ByteBuffer.wrap(buf);
return this;
};
/**
* clean the decoder
* @api public
*/
proto.clean = function () {
this.byteBuffer = new ByteBuffer();
this.refMap = {};
this.refId = 0;
return this;
};
/**
* check if the label match the method
* @api private
*/
proto._checkLabel = function (method, label) {
var l = this.byteBuffer.getChar();
var labelIsOk = l === label || label.indexOf(l) >= 0;
if (!labelIsOk) {
throw new TypeError('hessian ' + method + ' only accept label `' + label +
'` but got unexpect label `' + l + '`');
}
return l;
};
proto.handleType = function(type, val, withType) {
return withType ? {$class: object.DEFAULT_CLASSNAME[type], $: val} : val;
};
/**
* read a null from buffer
*
* v1.0
* ```
* null ::= N(x4e)
* ```
*
* @return {Null}
* @api public
*/
proto.readNull = function () {
this._checkLabel('readNull', 'N');
return null;
};
utils.addByteCodes(BYTE_CODES, [
0x4e,
], 'readNull');
/**
* read a boolean from buffer
*
* v1.0
* ```
* boolean ::= T(x54)
* ::= F(x46)
* ```
*
* @return {Boolean}
* @api public
*/
proto.readBool = function (withType) {
var label = this._checkLabel('readBool', ['T', 'F']);
var val = label === 'T';
return this.handleType('boolean', val, withType);
};
utils.addByteCodes(BYTE_CODES, [
0x54,
0x46,
], 'readBool');
/**
* read a int from buffer
*
* v1.0
* ```
* int ::= I(x49) b32 b24 b16 b8
* ```
*
* @return {Number}
* @api public
*/
proto.readInt = function (withType) {
this._checkLabel('readInt', 'I');
var val = this.byteBuffer.getInt();
return this.handleType('int', val, withType);
};
utils.addByteCodes(BYTE_CODES, [
0x49
], 'readInt');
/**
* read a long from buffer
*
* v1.0
* ```
* long ::= L(x4c) b64 b56 b48 b40 b32 b24 b16 b8
* ```
*
* @return {Number}
* @api public
*/
proto.readLong = function (withType) {
this._checkLabel('readLong', 'L');
var val = utils.handleLong(this.byteBuffer.getLong());
return this.handleType('long', val, withType);
};
utils.addByteCodes(BYTE_CODES, [
0x4c
], 'readLong');
/**
* read a double from buffer
*
* v1.0
* ```
* double ::= D(x44) b64 b56 b48 b40 b32 b24 b16 b8
* ```
*
* @return {Number}
* @api public
*/
proto.readDouble = function (withType) {
this._checkLabel('readDouble', 'D');
var val = this.byteBuffer.getDouble();
return this.handleType('double', val, withType);
};
utils.addByteCodes(BYTE_CODES, [
0x44
], 'readDouble');
/**
* read a date from buffer
*
* v1.0
* ```
* date ::= d(x64) b64 b56 b48 b40 b32 b24 b16 b8
* ```
* Date represented by a 64-bit long of milliseconds since Jan 1 1970 00:00H, UTC.
*
* @return {Date}
* @api public
*/
proto.readDate = function (withType) {
this._checkLabel('readDate', 'd');
var date = utils.handleLong(this.byteBuffer.getLong());
debug('read a date with milliEpoch: %d', date);
var val = new Date(date);
return this.handleType('date', val, withType);
};
utils.addByteCodes(BYTE_CODES, [
0x64
], 'readDate');
/**
* read bytes from buffer
*
* v1.0
* ```
* binary ::= (b(x62) b16 b8 binary-data)* B(x42) b16 b8 binary-data
* ```
* Binary data is encoded in chunks.
* 'B' represents the final chunk and
* 'b' represents any initial chunk. Each chunk has a 16-bit length value.
*
* @return {Buffer}
* @api public
*/
proto.readBytes = function () {
var label = this._checkLabel('readBytes', ['b', 'B']);
var bufs = [];
var length = 0;
// get all trunk start with 'b'
while (label === 'b') {
length = this.byteBuffer.getUInt16();
bufs.push(this.byteBuffer.read(length));
label = this._checkLabel('readBytes', ['b', 'B']);
}
// get the last trunk start with 'B'
length = this.byteBuffer.getUInt16();
bufs.push(this.byteBuffer.read(length));
return Buffer.concat(bufs);
};
utils.addByteCodes(BYTE_CODES, [
0x62,
0x42
], 'readBytes');
proto._readUTF8String = function (len) {
if (!is.number(len)) {
len = this.byteBuffer.getUInt16();
}
var startPos = this.byteBuffer.position();
var head;
var l;
debug('read utf8 string tunk, chars length: %d', len);
if (len === 0) {
return '';
}
while (len--) {
head = this.byteBuffer.get();
l = utils.lengthOfUTF8(head);
this.byteBuffer.skip(l - 1);
}
debug('get string trunk. start position: %d, byte length: %d',
startPos, this.byteBuffer.position() - startPos);
return this.byteBuffer.getRawString(startPos, this.byteBuffer.position() - startPos);
};
/**
* read a string from buffer
*
* The length is the number of characters, which may be different than the number of bytes.
*
* v1.0
* ```
* string ::= (s(x73) b16 b8 utf-8-data)* S(x53) b16 b8 utf-8-data
* ```
*
* @return {String}
* @api public
*/
proto.readString = function (withType) {
var str = '';
var code = this.byteBuffer.get();
// get all trunk start with 's'
while (code === 0x73) {
str += this._readUTF8String();
code = this.byteBuffer.get();
}
if (code === 0x53) {
// x53 ('S') represents the final chunk
debug('read last trunk of string');
str += this._readUTF8String();
} else {
// error format
throw new TypeError('hessian readString error, unexpect string code: 0x' + code.toString(16));
}
return this.handleType('string', str, withType);
};
utils.addByteCodes(BYTE_CODES, [
0x73,
0x53
], 'readString');
/**
* v1.0
* ```
* t(x74) b16 b8 type-string
* ```
*
* @param {Boolean} skip skip type, if true, will return empty string
* @return {String} type string
*/
proto.readType = function (skip) {
this._checkLabel('readType', 't');
var typeLength = this.byteBuffer.getUInt16();
if (skip) {
this.byteBuffer.skip(typeLength);
debug('ignore type, skip %d bytes', typeLength);
return '';
} else {
var type = this.byteBuffer.readRawString(typeLength);
debug('get type: %s', type);
return type;
}
};
utils.addByteCodes(BYTE_CODES, [
0x74,
], 'readType');
proto.readLength = function () {
this._checkLabel('readLength', 'l'); // x6c
var len = this.byteBuffer.getUInt();
debug('read length: %s', len);
return len;
};
utils.addByteCodes(BYTE_CODES, [
0x6c,
], 'readLength');
/**
* A sparse array, hessian v1.0
* http://hessian.caucho.com/doc/hessian-1.0-spec.xtp#map
*/
proto._readSparseObject = function (withType) {
var obj = {};
var label = this.byteBuffer.getChar(this.byteBuffer.position());
while (label !== 'z') {
debug('sparse array label: %s', label);
var key = this.read(withType);
var val = this.read(withType);
obj[key] = val;
label = this.byteBuffer.getChar(this.byteBuffer.position());
}
// skip 'z' char
this.byteBuffer.skip(1);
// default type info
if (withType) {
return {
$class: object.DEFAULT_CLASSNAME.map,
$: obj
};
}
return obj;
};
/**
* read an object from buffer
*
* v1.0
* ```
* map ::= M(x4d) t b16 b8 type-string (object, object)* z
* ```
*
* @param {Boolean} withType if need retain the type info
* @return {Object}
* @api public
*/
proto.readObject = function (withType) {
this._checkLabel('readObject', 'M');
debug('start read an object');
var typeLabel = this.byteBuffer.getChar(this.byteBuffer.position());
if (typeLabel !== 't') {
debug('read sparse object, start label: %s', typeLabel);
return this._readSparseObject(withType);
}
var type = this.readType(false) || object.DEFAULT_CLASSNAME.map;
// if object is 'java.util.HashMap', type will be ''
var result = {
$class: type,
$: {}
};
this._addRef(result);
// get
var label = this.byteBuffer.getChar();
var key;
while (label !== 'z') {
this.byteBuffer.position(this.byteBuffer.position() - 1);
key = this.read();
var value = this.read(withType);
label = this.byteBuffer.getChar();
// property name will auto transfer to a String type.
debug('read object prop: %j with type: %s', key, withType);
if (!/^this\$\d+$/.test(key)) {
result.$[key] = value;
}
}
debug('read object finish');
// java.lang.NoClassDefFoundError
if (/Exception$/.test(type) || /^java\.lang\.\w+Error$/.test(type)) {
result.$ = new JavaExceptionError(result, withType);
}
return withType ? result : result.$;
};
utils.addByteCodes(BYTE_CODES, [
0x4d,
], 'readObject');
/**
* read an map from buffer
*
* v1.0
* ```
* map ::= M t b16 b8 type-string (object, object)* z
* ```
*
* @param {Boolean} withType if need retain the type info
* @return {Object}
* @api public
*/
proto.readMap = proto.readObject;
/**
* anonymous variable-length list = {0, "foobar"}
* http://hessian.caucho.com/doc/hessian-1.0-spec.xtp#list
*/
proto._readNoLengthArray = function (withType, type) {
var arr = [];
var label = this.byteBuffer.getChar(this.byteBuffer.position());
while (label !== 'z') {
debug('no length array item#%d label: %s', arr.length, label);
arr.push(this.read(withType));
label = this.byteBuffer.getChar(this.byteBuffer.position());
}
// skip 'z' char
this.byteBuffer.skip(1);
arr = withType
? { $class: type, $: arr }
: arr;
return arr;
};
/**
* read an array from buffer
*
* v1.0
* ```
* list ::= V(x56) type? length? object* z
* ```
*
* @param {Boolean} withType if need retain the type info
* @return {Array}
* @api public
*/
proto.readArray = function (withType) {
debug('start read an array');
this._checkLabel('readArray', 'V');
var type = '';
var typeLabel = this.byteBuffer.getChar(this.byteBuffer.position());
if (typeLabel === 't') {
type = this.readType(!withType);
}
type = type || object.DEFAULT_CLASSNAME.list;
var lengthLabel = this.byteBuffer.getChar(this.byteBuffer.position());
if (lengthLabel !== 'l') {
debug('read no length array, start label: %s', typeLabel);
return this._readNoLengthArray(withType, type);
}
// if object is 'java.util.ArrayList', type will be ''
var realResult = [];
var result = realResult;
if (withType) {
result = {
$class: type,
$: realResult
};
}
this._addRef(result);
var len = this.readLength();
while (len--) {
realResult.push(this.read(withType));
}
var endLabel = this.byteBuffer.getChar();
if (endLabel !== 'z') {
throw new TypeError('hessian readArray error, unexpect end label: ' + endLabel);
}
debug('read array finished with a length of %d', realResult.length);
return result;
};
utils.addByteCodes(BYTE_CODES, [
0x56,
], 'readArray');
proto.readList = proto.readArray;
/**
* Get a object by ref id
*
* @return {Object}
*/
proto.readRef = function (withType) {
var rid = this.readRefId();
var obj = this.refMap[rid];
if (!withType && obj && utils.hasOwnProperty(obj, '$')) {
obj = obj.$;
}
return obj;
};
/**
* v1.0
* ```
* ref ::= R(x52) b32 b24 b16 b8
* ```
*
* @return {Number}
*/
proto.readRefId = function (withType) {
this._checkLabel('readRef', 'R');
return this.byteBuffer.getInt();
};
utils.addByteCodes(BYTE_CODES, [
0x52,
], 'readRef');
/**
* read an call from buffer
*
* v1.0
* ```
* call ::= c(x63) x01 x00 header* m b16 b8 method-string (object)* z
* ```
*
* @return {Object}
* @api public
*/
proto.readCall = function(withType) {
this._checkLabel('readCall', 'c');
if (this.byteBuffer.get() !== 0x01 || this.byteBuffer.get() !== 0x00) {
new TypeError('hessian call error, expect code: 0x01 0x00');
}
var header = {};
var args = [];
var result = {header: header, arguments: args};
while (this.byteBuffer.getChar(this.byteBuffer.position()) === 'H') {
this.byteBuffer.skip(1); //skip 'H' char
var key = this.byteBuffer.readRawString(this.byteBuffer.getUInt16());
header[key] = this.read(withType);
}
this._checkLabel('readCall.method', 'm');
var methodLength = this.byteBuffer.getUInt16();
result.method = this.byteBuffer.readRawString(methodLength);
var label = this.byteBuffer.getChar(this.byteBuffer.position());
while (label !== 'z') {
args.push(this.read(withType));
label = this.byteBuffer.getChar(this.byteBuffer.position());
}
// skip 'z' char
this.byteBuffer.skip(1);
return result;
};
utils.addByteCodes(BYTE_CODES, [
0x63,
], 'readCall');
/**
* read any thing
*
* @param {Boolean} withType if need retain the type info
* @api public
*/
proto.read = function (withType) {
var pos = this.byteBuffer.position();
var code = this.byteBuffer.get(pos);
var method = this.BYTE_CODES[code];
if (debug.enabled) {
debug('read position: %s, code: 0x%s, method: %s', pos, code.toString(16), method);
}
if (!method) {
throw new Error('hessian read got an unexpect code: 0x' + code.toString(16));
}
return this[method](withType);
};
/**
* set or get decoder byteBuffer position
*/
proto.position = function (num) {
if (is.number(num)) {
this.byteBuffer.position(num);
return this;
}
return this.byteBuffer.position();
};
module.exports = Decoder;