-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
325 lines (274 loc) · 7.24 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
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
'use strict';
var Reader = require('./src/reader');
var Writer = require('./src/writer');
function extend() {
for (var i=1; i<arguments.length; i++) {
for (var key in arguments[i]) {
if (!arguments[i].hasOwnProperty(key))
continue;
arguments[0][key] = arguments[i][key];
}
}
return arguments[0];
}
function isClassType(f) {
if (typeof(f) != 'function')
return false;
return /^class\s/.test(Function.prototype.toString.call(f));
}
function getAtomicSize(typeName) {
switch (typeName) {
case 'int8':
case 'uint8':
return 1;
case 'int16BE':
case 'int16LE':
case 'uint16BE':
case 'uint16LE':
return 2;
case 'int32BE':
case 'int32LE':
case 'uint32BE':
case 'uint32LE':
case 'floatBE':
case 'floatLE':
return 4;
case 'doubleBE':
case 'doubleLE':
return 8;
default:
throw new Error('Unexpected type: ' + typeName);
}
}
class Format {
constructor() {
this.steps = [];
var scope = this;
function addStepFunctions(fnName) {
if (fnName in scope)
return;
scope[fnName] = function(name, construct) {
scope.steps.push({ type: 'data', fn: fnName, name: name, construct: construct });
return scope;
};
scope[fnName + 'array'] = function(name, length, construct) {
scope.steps.push({ type: 'array', fn: fnName, name: name, length: length, construct: construct });
return scope;
};
}
var names = Object.getOwnPropertyNames(Reader.prototype);
for (var i = 0; i < names.length; i++) {
var member = names[i];
if (member == 'constructor')
continue;
addStepFunctions(member);
}
}
text(name, byteLength, encoding, construct) {
this.steps.push({ type: 'data', fn: 'text', name: name, byteLength: byteLength, encoding: encoding, construct: construct });
return this;
}
buffer(name, length, construct) {
this.steps.push({ type: 'data', fn: 'buffer', name: name, length: length, construct: construct });
return this;
}
nest(name, format, construct) {
this.steps.push({ type: 'nest', name: name, format: format, construct: construct });
return this;
}
list(name, count, format) {
this.steps.push({ type: 'list', name: name, count: count, format: format });
return this;
}
listEof(name, format) {
this.steps.push({ type: 'listEof', name: name, format: format });
return this;
}
custom(name, callback) {
this.steps.push({ type: 'custom', name: name, callback: callback });
return this;
}
length() {
var length = 0;
for (var i = 0; i < this.steps.length; i++) {
var step = this.steps[i];
if (step.type == 'data') {
if (step.fn == 'buffer') {
if (step.length === 'eof')
throw new Error('Cannot use the predictive length() with variable length sections (.buffer() with EOF)');
length += step.length;
}
else if (step.fn == 'text') {
length += step.byteLength;
}
else {
length += getAtomicSize(step.fn);
}
continue;
}
if (step.type == 'array') {
length += step.length * getAtomicSize(step.fn);
continue;
}
if (step.type == 'list') {
length += step.count * step.format.length();
continue;
}
if (step.type == 'nest') {
length += step.format.length();
continue;
}
if (step.type == 'custom') {
throw new Error('Cannot use the predictive length() with variable length sections (.custom())');
}
if (step.type == 'listEof') {
throw new Error('Cannot use the predictive length() with variable length sections (.listEof())');
}
}
return length;
}
parse(buffer, reader) {
if (!reader)
reader = new Reader(buffer);
var result = {};
for (var i = 0; i < this.steps.length; i++) {
var step = this.steps[i];
if (step.type == 'data') {
var f = reader[step.fn];
var value;
if (step.fn == 'buffer') {
value = f.apply(reader, [step.length]);
}
else if (step.fn == 'text') {
value = f.apply(reader, [step.byteLength, step.encoding]);
}
else {
value = f.apply(reader);
}
if (step.construct) {
value = isClassType(step.construct) ? new step.construct(value) : step.construct(value);
}
result[step.name] = value;
continue;
}
if (step.type == 'array') {
var f = reader[step.fn];
var value = new Array(step.length);
for (var i = 0; i < step.length; ++i) {
value[i] = f.apply(reader);
}
if (step.construct) {
value = isClassType(step.construct) ? new step.construct(value) : step.construct(value);
}
result[step.name] = value;
continue;
}
if (step.type == 'list') {
var list = [];
for (var j = 0; j < step.count; j++) {
list.push(step.format.parse(buffer, reader));
}
result[step.name] = list;
continue;
}
if (step.type == 'listEof') {
var list = [];
while (reader.position < buffer.length) {
list.push(step.format.parse(buffer, reader));
}
result[step.name] = list;
continue;
}
if (step.type == 'nest') {
var value = step.format.parse(buffer, reader);
if (step.construct) {
value = isClassType(step.construct) ? new step.construct(value) : step.construct(value);
}
result[step.name] = value;
continue;
}
if (step.type == 'custom') {
var fmt = step.callback(result, buffer, reader);
if (!(fmt instanceof Format))
throw new Error('Error: .custom() callback must return an instance of Format');
result[step.name] = fmt.parse(buffer, reader);
continue;
}
}
return result;
}
write(data, options) {
var opt = extend({
writer: null,
blocksize: 1024
}, options);
var end = false;
if (!opt.writer) {
end = true;
opt.writer = new Writer(parseInt(opt.blocksize));
}
var writer = opt.writer;
for (var i = 0; i < this.steps.length; i++) {
var step = this.steps[i];
if (step.type == 'data') {
var f = writer[step.fn];
var value = data[step.name];
if (typeof(value) == 'object' && 'serialize' in value) {
value = value.serialize();
}
if (step.fn == 'text') {
f.apply(writer, [value, step.encoding]);
}
else {
f.apply(writer, [value]);
}
continue;
}
if (step.type == 'array') {
var f = writer[step.fn];
var value = data[step.name];
if (typeof(value) == 'object' && 'serialize' in value) {
value = value.serialize();
}
for (var i = 0; i < step.length; ++i) {
f.apply(writer, [value[i]]);
}
}
if (step.type == 'list') {
var list = data[step.name];
for (var j = 0; j < list.length; j++) {
var value = list[j];
step.format.write(value, opt);
}
continue;
}
if (step.type == 'listEof') {
var list = data[step.name];
for (var j = 0; j < list.length; j++) {
var value = list[j];
step.format.write(value, opt);
}
continue;
}
if (step.type == 'nest') {
var value = data[step.name];
if (typeof(value) == 'object' && 'serialize' in value) {
value = value.serialize();
}
step.format.write(value, opt);
continue;
}
if (step.type == 'custom') {
var fmt = step.callback(data, writer.output, writer);
if (!(fmt instanceof Format))
throw new Error('Error: .custom() callback must return an instance of Format');
fmt.write(data[step.name], opt);
continue;
}
}
if (end)
writer.end();
return writer.output;
}
}
module.exports = Format;