-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
157 lines (112 loc) · 3.94 KB
/
index.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
const fs = require("fs");
const NODE_ESC = 0xFD;
const NODE_INIT = 0xFE;
const NODE_TERM = 0xFF;
exports.read = function readOTBM(__INFILE__) {
var Node = function (data, children) {
data = this.removeEscapeCharacters(data);
this.group = data.readUInt8(0);
this.flags = data.readUInt32LE(1);
let attributeData = data.slice(5);
while (attributeData.length) {
const attrType = attributeData.readUInt8(0);
const dataLen = attributeData.readUInt16LE(1);
if (attrType === 0x10 && dataLen === 2) {
this.sid = attributeData.readUInt16LE(3);
}
if (attrType === 0x11 && dataLen === 2) {
this.cid = attributeData.readUInt16LE(3);
}
if (attributeData.length < dataLen) {
console.log("ERROR");
console.log(attributeData.length, dataLen);
console.log(data);
console.log(attributeData);
console.log(attributeData.toString());
process.abort();
return;
}
attributeData = attributeData.slice(dataLen + 3);
}
if (children.length) {
this.setChildren(children);
}
};
Node.prototype.removeEscapeCharacters = function (nodeData) {
/* FUNCTION removeEscapeCharacter
* Removes 0xFD escape character from the byte string
*/
var iEsc = 0;
var index;
while (true) {
// Find the next escape character
index = nodeData.slice(++iEsc).indexOf(NODE_ESC);
// No more: stop iteration
if (index === -1) {
return nodeData;
}
iEsc = iEsc + index;
// Remove the character from the buffer
nodeData = Buffer.concat([
nodeData.slice(0, iEsc),
nodeData.slice(iEsc + 1)
]);
}
};
Node.prototype.setChildren = function (children) {
this.children = children;
};
function readASCIIString16LE(data) {
/* FUNCTION readASCIIString16LE
* Reads a string of N bytes with its length
* deteremined by the value of its first two bytes
*/
return data.slice(2, 2 + data.readUInt16LE(0)).toString("ASCII");
}
function readNode(data) {
/* FUNCTION readNode
* Recursively parses OTBM nodal tree structure
*/
// Cut off the initializing 0xFE identifier
data = data.slice(1);
var i = 0;
var children = new Array();
var nodeData = null;
var child;
// Start reading the array
while (i < data.length) {
var cByte = data.readUInt8(i);
// Data belonging to the parent node, between 0xFE and (OxFE || 0xFF)
if (nodeData === null && (cByte === NODE_INIT || cByte === NODE_TERM)) {
nodeData = data.slice(0, i);
}
// Escape character: skip reading this and following byte
if (cByte === NODE_ESC) {
i = i + 2;
continue;
}
// A new node is started within another node: recursion
if (cByte === NODE_INIT) {
child = readNode(data.slice(i));
children.push(child.node);
// Skip index over full child length
i = i + 2 + child.i;
continue;
}
// Node termination
if (cByte === NODE_TERM) {
return {
"node": new Node(nodeData, children),
"i": i
}
}
i++;
}
}
const data = fs.readFileSync(__INFILE__);
if (data.readUInt8(10) !== 1 && data.readUInt16LE(11) !== 140) {
console.log('wrong file');
return;
}
return readNode(data.slice(4)).node;
};