forked from node-ebml/node-ebml
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.js
More file actions
183 lines (162 loc) · 5.14 KB
/
Copy pathexample.js
File metadata and controls
183 lines (162 loc) · 5.14 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
const EBML = require('./index.js'); //You will use require('universal-ebml')
const util = require('util');
//Define document schema
let _the_schema = {
"RootTag": {
"tag": "1e544945", //Hex tags should be lowercase only
"type": "m",
"description": "Root tag"
},
"SubTag": {
"context": ["RootTag"], //The tag can be only a direct child of RootTag
"tag": "80",
"type": "m",
"description": "Sub tag"
},
"StringVal": {
"context": ["SubTag", "ValueList"], //The tag can be only a direct child of SubTag or StringList
"tag": "80", //The tags can duplicate as long as they can be differentiated by parent contexts
"type": "8",
"description": "Utf8 string"
},
"ValueList": {
"context": ["SubTag"], //The tag can be only a direct child of SubTag
"tag": "81",
"type": "m",
"description": "String list"
},
"IntVal": {
"context": ["^SubTag"], //The tag can be only a direct or indirect child of SubTag
"tag": "a0",
"type": "i",
"description": "Int value"
},
"UIntVal": {
"context": ["^SubTag"], //The tag can be only a direct or indirect child of SubTag
"tag": "a2",
"type": "u",
"description": "UInt value"
},
"DateVal": {
"context": ["^SubTag"], //The tag can be only a direct or indirect child of SubTag
"tag": "a1",
"type": "d",
"description": "Date value"
}
};
let schema = new EBML.Schema(_the_schema);
//Create our document for serializing to EBML
let doc = {
name: "RootTag",
children: [
{
name: "SubTag",
children: [
{
name: "StringVal",
data: EBML.tools.toBinary("Some string", "8")
},
{
name: "ValueList",
children: [
{
name: "StringVal",
data: EBML.tools.toBinary("Another string", "8")
},
{
name: "IntVal",
data: EBML.tools.toBinary(42, "i")
},
{
name: "IntVal",
data: EBML.tools.toBinary(-42, "i")
},
{
name: "UIntVal",
data: EBML.tools.toBinary(554, "u")
},
]
}
]
},
{
name: "SubTag",
children: [
{
name: "StringVal",
data: EBML.tools.toBinary("Yet another string", "8")
},
{
name: "DateVal",
data: EBML.tools.toBinary(new Date("2018-07-27T15:58:00"), "d")
},
{
name: "UIntVal",
data: EBML.tools.toBinary(-554, "u")
},
]
}
]
};
//Let us encode our doc
function encode(root, schema) {
let encoder = new EBML.Encoder(null, schema);
let encodedDoc;
encoder.on("data", chunk => {
encodedDoc = chunk;
});
function encodeInner(node){
let info = schema.findTagByName(node.name);
if(info.type === 'm'){
encoder.write(['start', node]);
for(let i=0; i<node.children.length; ++i){
encodeInner(node.children[i]);
}
encoder.write(['end', node]);
}else{
encoder.write(['tag', node]);
}
}
encodeInner(root);
return encodedDoc;
}
let encodedDoc = encode(doc, schema);
//Now we have encoded DOC into EBML
console.log("EBML encoded doc: " + encodedDoc.toString('hex'));
//Let us now decode it back
function decode(buffer, schema) {
let decoder = new EBML.Decoder(null, schema);
let stack = [{children: []}];
decoder.on('data', function(chunk) {
if(chunk[0] === 'start') {
let item = stack[stack.length - 1];
let data = chunk[1];
let item1 = {
name: data.name,
type: data.type,
children: []
};
item.children.push(item1);
stack.push(item1);
}else if(chunk[0] === 'tag') {
let item = stack[stack.length - 1];
let data = chunk[1];
let item1 = {
name: data.name,
type: data.type,
data: data.data,
value: data.value
};
item.children.push(item1);
}else{ //chunk[0] === 'end'
stack.pop();
}
});
decoder.write(buffer);
if(decoder._tag_stack.length)
throw new Error('Failed to parse: ' + buffer.toString('hex'));
return stack[0].children[0];
}
let new_doc = decode(encodedDoc, schema);
//Now we have decoded original doc
console.log("Decoded doc: ", util.inspect(new_doc, {showHidden: false, depth: null}));