- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
module Bert {
export class Decoder {
private b: ArrayBuffer;
private d: DataView;
private i: number = 0;
result: any;
constructor(packet: ArrayBuffer) {
this.b = packet;
this.d = new DataView(packet);
if (131 == this.d.getUint8(this.i++)) {
this.result = this.decode();
} else {
throw 'Not BERT';
}
}
decode() {
var tag = this.d.getUint8(this.i++);
var r: any;
switch (tag) {
case 100: r = this.decodeAtom(); break; // latin1 atom
case 107: r = this.decodeString(); break; // utf8 string
case 109: r = this.decodeBinary(); break; // utf8 binary string
case 115: r = this.decodeSmallAtom(); break; // latin1 atom
case 118: r = this.decodeAtom(); break; // utf8 atom
case 119: r = this.decodeSmallAtom(); break; // utf8 atom
}
return r;
}
decodeAtom() {
var length = this.d.getUint16(this.i);
this.i += 2;
var dec = new Utf8.Decoder(this.b.slice(this.i, this.i + length));
this.i += length;
return dec.result;
}
decodeSmallAtom() {
var length = this.d.getUint8(this.i++);
var dec = new Utf8.Decoder(this.b.slice(this.i, this.i + length));
this.i += length;
return dec.result;
}
decodeString() {
var length = this.d.getUint16(this.i);
this.i += 2;
var dec = new Utf8.Decoder(this.b.slice(this.i, this.i + length));
this.i += length;
return dec.result;
}
decodeBinary() {
var length = this.d.getUint32(this.i);
this.i += 4;
var dec = new Utf8.Decoder(this.b.slice(this.i, this.i + length));
this.i += length;
return dec.result;
}
...
}
}