-
Notifications
You must be signed in to change notification settings - Fork 0
/
decode.js
337 lines (336 loc) · 8.58 KB
/
decode.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
const fs = require('fs');
const DATATYPES = {
NIL: 'NIL',
BOOL: 'BOOL',
UINT: 'UINT',
INT: 'INT',
FLOAT: 'FLOAT',
STR: 'STR',
BIN: 'BIN',
ARRAY: 'ARRAY',
MAP: 'MAP',
TIMESTAMP: 'TIMESTAMP'
}
const DATATYPE_LENGTH = {
BIN:{
0xc4: 1,
0xc5: 2,
0xc6: 4,
},
UINT:{
0xcc: 1,
0xcd: 2,
0xce: 4,
0xcf: 8,
},
INT:{
0xd0: 1,
0xd1: 2,
0xd2: 4,
0xd3: 8,
},
FLOAT:{
0xca: 4,
0xcb: 8,
},
STR:{
0xd9: 1,
0xda: 2,
0xdb: 4
},
MAP: {
0xde: 2,
0xdf: 4,
},
ARRAY: {
0xdc: 2,
0xdd: 4
}
};
const BYTE_TO_DATATYPE = {
0x0c : DATATYPES.NIL,
0xc2: DATATYPES.BOOL,
0xc3: DATATYPES.BOOL,
0xc4: DATATYPES.BIN,
0xc5: DATATYPES.BIN,
0xc6: DATATYPES.BIN,
0xca: DATATYPES.FLOAT,
0xcb: DATATYPES.FLOAT,
0xcc: DATATYPES.UINT,
0xcd: DATATYPES.UINT,
0xce: DATATYPES.UINT,
0xcf: DATATYPES.UINT,
0xd0: DATATYPES.INT,
0xd1: DATATYPES.INT,
0xd2: DATATYPES.INT,
0xd3: DATATYPES.INT,
0xd9: DATATYPES.STR,
0xda: DATATYPES.STR,
0xdb: DATATYPES.STR,
0xdc: DATATYPES.ARRAY,
0xdd: DATATYPES.ARRAY,
0xde: DATATYPES.MAP,
0xdf: DATATYPES.MAP
}
class Decoder {
constructor() {
this.isDebugMode = false;
this.output = undefined;
this.stack = [];
this.offset = -1;
}
debug(...args){
if(this.isDebugMode){
console.log(...args);
}
}
setDebugMode(value){
this.isDebugMode = value;
}
decode(rawBuffer){
this.resetState();
this.buffer = rawBuffer;
this.readDataType();
return this.output;
}
resetState(){
this.output = undefined;
this.buffer = undefined;
this.stack = [];
this.offset = -1;
}
getObjectAtPath(pathArr){
//if no path returns root object
let targetObj = this.output;
let i = 0;
if(!pathArr || pathArr.length == 0) return targetObj;
while(pathArr[i]){
const currLevelKey = pathArr[i];
if(currLevelKey) targetObj = targetObj[currLevelKey];
i++;
}
this.debug('getObjectAtPath', pathArr);
return targetObj;
}
getCurrStackPath(){
if(this.stack.length < 2) return false;
return this.stack.slice(1).map(level => level.path);
}
getObjectAtCurrStackPath() {
const currPath = this.getCurrStackPath();
return this.getObjectAtPath(currPath);
}
readStringAtPosition(firstByte) {
this.debug('readStringAtPosition',firstByte);
let strLen = (firstByte & 0x1f);
const multiByteLength = DATATYPE_LENGTH.STR[firstByte];
if (multiByteLength) {
strLen = this.readMultiByteUInt(multiByteLength);
}
let str = '';
for(let i=0; i < strLen; i++){
this.offset++;
str += String.fromCharCode(this.buffer[this.offset]);
}
return str;
}
addMapKey(keyStr) {
this.debug('addMapKey', keyStr);
const stack = this.getCurrStack();
const parentObj = this.getObjectAtCurrStackPath();
//init a new key at parentObj to initial undefined value
parentObj[keyStr] = undefined;
stack.nextKey = false;
stack.lastKey = keyStr;
}
addMapValue(value) {
this.debug('addMapValue', value);
const stack = this.getCurrStack();
const parentObj = this.getObjectAtCurrStackPath();
//set value for lastKey at parentObj
parentObj[stack.lastKey] = value;
stack.length--;
stack.nextKey = true;
if (stack.length === 0) {
stack.nextKey = false;
this.resetCompletedStacks();
}
}
addArrayValue(value) {
this.debug('addArrayValue', value);
const stack = this.getCurrStack();
const parentObj = this.getObjectAtCurrStackPath();
//set value for lastKey at parentObj
parentObj.push(value);
stack.length--;
if (stack.length === 0) {
this.resetCompletedStacks();
}
}
readMultiByteUInt(bytesLength){
let length = 0;
this.offset++; //increment offset to the start of the int bytes
if(bytesLength === 1){
length = this.buffer.readUInt8(this.offset);
}
if(bytesLength === 2){
this.offset++; //increment twice since it's a 16bit uint
length = this.buffer.readUInt16BE(this.offset)
}
if(bytesLength === 4){
this.offset += 2;
length = this.buffer.readUInt32BE(this.offset);
}
this.debug('read multi byte uint:', bytesLength, 'value: ',length);
return length;
}
readMultiByteInt(bytesLength){
let length = 0;
this.offset++; //increment offset to the start of the int bytes
if(bytesLength === 1){
length = this.buffer.readInt8(this.offset);
}
if(bytesLength === 2){
this.offset++; //increment twice since it's a 16bit uint
length = this.buffer.readInt16BE(this.offset)
}
if(bytesLength === 4){
this.offset += 2;
length = this.buffer.readInt32BE(this.offset);
}
this.debug('read multi byte int:', bytesLength, 'value: ',length);
return length;
}
readDataType(){
this.offset++;
const currType = this.buffer[this.offset];
this.debug('readDataType', this.offset, 'stack', JSON.stringify(this.stack));
if(this.offset === this.buffer.byteLength){
return this.endOfData();
}
//parse MAP
if( BYTE_TO_DATATYPE[currType] === DATATYPES.MAP || (currType >>> 4) === 0x08 ){
this.readMapDataType(currType)
}
//parse STR
if( BYTE_TO_DATATYPE[currType] === DATATYPES.STR || (currType >>> 5) === 0x05) {
// this.readStringDataType(currType);
this.addDataAtCurrentStack(DATATYPES.STR, currType);
}
//parse ARRAY
if( BYTE_TO_DATATYPE[currType] === DATATYPES.ARRAY || (currType >>> 4) === 0x09){
this.readArrayDataType(currType);
}
return this.readDataType();
}
readMapDataType(firstByte){
this.debug('readDataType - map detected');
const isRoot = this.stack.length === 0;
const mapPath = !isRoot && this.getCurrStack().lastKey;
let mapLength = (firstByte & 0x0f);
const multiByteLength = DATATYPE_LENGTH.MAP[firstByte];
if (multiByteLength) {
mapLength = this.readMultiByteUInt(multiByteLength);
}
if (isRoot) {
this.output = {};
} else {
const parentNode = this.getObjectAtCurrStackPath();
parentNode[mapPath] = {};
this.getCurrStack().nextKey = true;
}
this.stack.push({
root: isRoot,
path: mapPath,
type:'map',
length: mapLength,
nextKey: true,
lastKey: undefined
});
//handle empty map case
if(mapLength === 0){
// all parents stack with one child element
// can now be marked as completed since the
// leaf is the empty map we just added on the stack
this.resetCompletedStacks();
}
}
readArrayDataType(firstByte){
this.debug('readArrayDataType');
const isRoot = this.stack.length === 0;
const arrPath = !isRoot && this.getCurrStack().lastKey;
let arrLength = (firstByte & 0x0f);
const multiByteLength = DATATYPE_LENGTH.ARRAY[firstByte];
if (multiByteLength) {
arrLength = this.readMultiByteUInt(multiByteLength);
}
if (isRoot) {
this.output = [];
} else {
const parentNode = this.getObjectAtCurrStackPath();
parentNode[arrPath] = [];
if(this.getCurrStack().type == 'map'){
this.getCurrStack().nextKey = true;
}
}
this.stack.push({
root: isRoot,
path: arrPath,
type:'array',
length: arrLength,
});
//handle empty map case
if(arrLength === 0) {
this.resetCompletedStacks();
}
}
addDataAtCurrentStack(type, firstByte){
this.debug('addDataAtCurrentStack');
let data = null;
if (type === DATATYPES.STR) {
data = this.readStringAtPosition(firstByte);
}
const stack = this.getCurrStack();
if (!stack) {
this.output = data;
}
if (stack.type === 'map'){
stack.nextKey ?
this.addMapKey(data):
this.addMapValue(data);
}
if (stack.type === 'array') {
this.addArrayValue(data);
}
}
getCurrStack(){
return this.stack[this.stack.length-1];
}
isStackRoot(){
return this.stack.length === 1;
}
decrementParentStack() {
this.debug('decrement parent stack');
if(this.isStackRoot()) return false;
let parentDepth = this.stack.length - 2;
this.stack[parentDepth].length--;
}
resetCompletedStacks(){
this.decrementParentStack();
this.debug('reset parent stacks');
let depth = this.stack.length-1;
while(this.stack[depth]) {
const nStack = this.stack[depth];
if(nStack.length <= 1 && !this.stack[depth].root){
this.debug('remove stack @ depth',depth,'path:',this.stack.path)
this.stack.pop()
}
depth--;
}
}
endOfData() {
this.debug('endOfData');
return this.output;
}
}
module.exports = Decoder;