-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.js
485 lines (462 loc) · 14.1 KB
/
parser.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
var Application = {};
function IOError(message) {
this.message = message;
this.name = "IOError";
}
function ByteStringParser (bytes) {
this.bytes = bytes;
var buffer = new ArrayBuffer(bytes.length);
for (var i = 0; i < bytes.length; i++) {
buffer[i] = bytes[i];
}
this.data = new DataView(buffer);
this.pos = 0;
}
function Renderer (name, desc, func) {
this.name = name;
this.desc = desc;
this.func = func;
}
function Parser (name, desc, func) {
this.name = name;
this.desc = desc;
this.func = func;
}
var parsers = [];
var renders = [];
ByteStringParser.prototype.decode0 = function (array) {
return new TextDecoder("UTF-8").decode(array);
};
ByteStringParser.prototype.decode1 = function (array) {
var encodedString = String.fromCharCode.apply(null, array ),
decodedString = decodeURIComponent(escape(encodedString));
return decodedString;
};
ByteStringParser.prototype.decode2 = function (array) {
var out, i, len, c;
var char2, char3;
out = "";
len = array.length;
i = 0;
while(i < len) {
c = array[i++];
switch(c >> 4)
{
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
// 0xxxxxxx
out += String.fromCharCode(c);
break;
case 12: case 13:
// 110x xxxx 10xx xxxx
char2 = array[i++];
out += String.fromCharCode(((c & 0x1F) << 6) | (char2 & 0x3F));
break;
case 14:
// 1110 xxxx 10xx xxxx 10xx xxxx
char2 = array[i++];
char3 = array[i++];
out += String.fromCharCode(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
}
}
return out;
};
ByteStringParser.prototype.decode = function (array) {
try {
return this.decode0(array);
} catch (err) {
console.warn(err);
try {
return this.decode1(array);
} catch (err){
console.warn(err);
return this.decode2(array);
}
}
};
ByteStringParser.prototype.types = ["boolean", "int8", "uint8", "int32", "uint32", "float", "string", "array", "dict"];
/**
* Read one byte from the array.
* If increment is <code>false</code>, the current position on the buffer will not be increased.
* If offset is defined, the offset is added to the current position before reading. This does not affect the increment.
* @throws IOError if you reached the end of the buffer already.
* @param increment if given and not <code>false</code>: <code>this.pos = this.pos + 1</code>
* @param offset if given: <code>return this.bytes[this.pos + offset]</code>
* @returns {int} byte
*/
ByteStringParser.prototype.read = function (increment, offset) {
increment = (increment === undefined || increment === null ? true : increment);
offset = (offset === undefined || offset === null ? 0 : offset);
if ((this.pos + offset) >= this.bytes.length) { //
throw new IOError("IOError: Over the end of the buffer.");
}
var byte = this.bytes[this.pos + offset];
if (increment === true) {
this.pos++;
}
return byte;
};
ByteStringParser.prototype.next_int = function(values_to_read) {
var int = 0;
for (var i = 0; i < values_to_read; i++) {
int += (this.read(false, i) << (8 * i));
}
return int;
};
ByteStringParser.prototype.new_byte = function(byte) {
byte = (byte !== undefined ? byte : this.read());
var obj = $("<div>");
obj.addClass("byte");
obj.data("byte", byte);
obj.text(Application.render(byte));
return obj;
};
/**
* Generates a red byte like object, which shows read errors.
* @param byte
* @returns {*|jQuery|HTMLElement}
*/
ByteStringParser.prototype.new_failed_byte = function(byte) {
var obj = $("<div>");
obj.addClass("byte").addClass("failed");
obj.data("byte", byte);
var str;
if (byte === undefined) {
str = Application.render(0);
obj.text(Application.repeat("-", str.length));
} else {
str = Application.render(byte);
}
obj.text(str);
return obj;
};
ByteStringParser.prototype.new_failed_rest = function(error_text) {
error_text = error_text || "Parse Error";
var obj = $("<div>");
obj.addClass("failed").addClass("element");
var obj_part = $("<div>");
obj_part.addClass("part");
var obj_part_bytes = $("<div>");
obj_part_bytes.addClass("bytes");
while (this.pos < this.bytes.length) {
obj_part_bytes.append(this.new_failed_byte(this.read()))
}
var obj_part_caption = $("<div>");
obj_part_caption.addClass("caption");
obj_part_caption.text(error_text);
var obj_part_calculated = $("<div>");
obj_part_calculated.addClass("calculated");
obj_part_calculated.text("???");
obj_part.append(obj_part_bytes);
obj_part.append(obj_part_caption);
obj_part.append(obj_part_calculated);
obj.append(obj_part);
return obj;
};
ByteStringParser.prototype.new_obj_bytes = function () {
var obj = $("<div>");
obj.addClass("bytes");
return obj;
};
ByteStringParser.prototype.new_something = function(type, values_to_read, calculated, name) {
if (name === undefined || name === null) {
name = type;
}
var obj = $("<div>");
obj.addClass(type).addClass("part");
var bytes_obj = this.new_obj_bytes();
for(var i = 0; i < values_to_read; i++)
{
try {
bytes_obj.append(this.new_byte());
} catch (e) {
if (e instanceof IOError) {
bytes_obj.append(this.new_failed_byte());
break;
}
}
}
obj.append(bytes_obj);
var label = $("<div>");
label.addClass("caption");
label.text(name);
obj.append(label);
var value = $("<div>");
value.addClass("calculated");
value.text(calculated === undefined ? "-" : calculated);
obj.append(value);
return obj;
};
ByteStringParser.prototype.new_boolean = function() {
var obj = $("<div>");
obj.addClass("bool");
var bool = this.next_int(1);
obj.append(this.new_something("value", 1, (bool == 0 ? "false" : "true")));
//elem.append(obj);
return obj;
};
ByteStringParser.prototype.new_intX = function(values_to_read) {
var obj = $("<div>");
obj.addClass("int" + (8*values_to_read));
var int = this.next_int(values_to_read);
obj.append(this.new_something("value", values_to_read, int));
return obj;
};
ByteStringParser.prototype.new_int8 = function() {
return this.new_intX(1);
};
ByteStringParser.prototype.new_int32 = function() {
return this.new_intX(4);
};
ByteStringParser.prototype.new_float = function() {
var obj = $("<div>");
obj.addClass("float");
//var float = Application.parseFloat();
//var int = this.next_int(values_to_read);
obj.append(this.new_something("value", values_to_read, "little Endian"));
return obj;
};
/**
*
* @param is_key false/undefined (default): Add a normal String element. true: omit the outer object, only add the part.
* @returns {*|jQuery|HTMLElement}
*/
ByteStringParser.prototype.new_string = function(is_key) {
var obj_string = $("<div>");
obj_string.addClass("string");
var obj = $("<div>");
obj.addClass("part");
obj.addClass(is_key === true ? "key" : "value");
var int_parts = [];
var bytes_obj = this.new_obj_bytes();
while (this.read(false) != 0) {
int_parts[int_parts.length] = this.read(false);
bytes_obj.append(this.new_byte());
}
var uin8array = new Uint8Array(int_parts.length);
for (var i = 0; i < int_parts.length; i++) {
uin8array[i] = int_parts[i];
}
var str = this.decode(uin8array);
bytes_obj.append(this.new_byte()); // the skippend "\0"
obj.append(bytes_obj);
var label = $("<div>");
label.addClass("caption");
label.text(is_key === true ? "key" : "string");
obj.append(label);
var value = $("<div>");
value.addClass("calculated");
value.text(str);
obj.append(value);
if (is_key === true) {
return obj;
}
obj_string.append(obj);
return obj_string;
};
ByteStringParser.prototype.new_list = function() {
var obj = $("<div>");
obj.addClass("list");
var count = this.next_int(2);
obj.append(this.new_something("count", 2, count, "count"));
for (var i = 0; i < count; i++) {
var elem = this.new_id();
obj.append(elem);
}
return obj;
};
ByteStringParser.prototype.new_dict = function() {
var obj = $("<div>");
obj.addClass("dict");
var count = this.next_int(2);
obj.append(this.new_something("count", 2, count, "add count"));
var i, elem;
for (i = 0; i < count; i++) {
elem = this.new_id();
obj.append(elem);
var key = this.new_string(true);
obj.append(key);
}
count = this.next_int(2);
obj.append(this.new_something("count", 2, count, "del. count"));
for (i = 0; i < count; i++) {
elem = this.new_id();
obj.append(elem);
}
return obj;
};
ByteStringParser.prototype.new_id = function () {
var id_int = this.next_int(4);
var id_obj = this.new_something("id", 4, id_int);
id_obj.data("value", id_int);
//id_obj.onclick("ByteStringParser.show_element($(this).data(\"value\"))");
return id_obj
};
ByteStringParser.prototype.new_whatever = function() {
var type = this.read(false);
var type_obj = this.new_something("type", 1, this.types[type]);
var id_obj = this.new_id();
switch (type) {
case 0: // BOOLEAN
obj = this.new_boolean();
break;
case 1:case 2: // INT8
obj = this.new_int8();
break;
case 3:case 4: // INT32
obj = this.new_int32();
break;
case 6: // STRING
obj = this.new_string();
break;
case 7: // LIST
obj = this.new_list();
break;
case 8: // DICT
obj = this.new_dict();
break;
default:
var obj = $("<div>");
obj.text("nope");
break;
}
obj.addClass("element");
obj.prepend(id_obj);
obj.prepend(type_obj);
return obj;
};
/*
APPLICATION STUFF
*/
Application.load_parser = function () {
var select = $("#parsers");
for(var i = 0; i < parsers.length; i++) {
var parser = parsers[i];
if (!parser instanceof Parser) {
continue;
}
var option = $("<option/>")
.val(i)
.text(parser.name)
.attr("title", parser.desc)
.data("func", parser.func);
if (i == 0) {
option.attr('selected',true);
}
select.append(option);
}
};
Application.load_render = function () {
var select = $("#renderers");
for(var i = 0; i < renders.length; i++) {
var parser = renders[i];
if (!parser instanceof Renderer) {
continue;
}
var option = $("<option/>")
.val(i)
.text(parser.name)
.attr("title", parser.desc)
.data("func", parser.func);
if (i == 0) {
option.attr('selected',true);
}
select.append(option);
}
};
Application.get_parser = function () {
return parsers[$("#parsers").val() || 0];
};
Application.get_render = function () {
return renders[$("#renderers").val() || 0] || function (byte) {return ""+byte};
};
Application.parse = function (string) {
return this.get_parser().func(string);
};
Application.render = function (byte) {
return this.get_render().func(byte);
};
Application.parseFloat = function (str, radix) {
var parts = str.split(".");
if ( parts.length > 1 )
{
return parseInt(parts[0], radix) + parseInt(parts[1], radix) / Math.pow(radix, parts[1].length);
}
return parseInt(parts[0], radix);
};
Application.repeat = function (str, n) {
return (new Array(n + 1)).join(str);
};
if (String.prototype.repeat) {
Application.repeat = function (str, n) {
return str.repeat(n);
}
}
parsers[parsers.length] = new Parser(
"json list", "uses the build in json parser. Is capable of hex in a format of 0x2E.", JSON.parse
);
parsers[parsers.length] = new Parser(
"hex blob", "all non-hex character are simply ignored.", function (string) {
string = string.replace(/[^0-9a-f]+/gi, '');
if(string.length % 2 === 1) {throw "not an even number of digits..."}
var array = new Uint8Array(string.length/2);
for (var i = 0; i < string.length-1; i += 2) {
array[i/2] = parseInt(string.substring(i, i+2), 16);
}
return array;
}
);
renders[renders.length] = new Renderer(
"decimal", "negative possible.", function (byte) {return byte}
);
var to_positive_hex= function (byte) {
var string = "0" + ((byte >>> 0).toString(16));
return string.substring(string.length-2);
};
renders[renders.length] = new Renderer(
"positive", "always positive", function (byte) {
var hex=to_positive_hex(byte);
return parseInt(hex, 16);
}
);
renders[renders.length] = new Renderer(
"hex", "without the 0x.. part.", to_positive_hex
);
function onload() {
Application.load_parser();
Application.load_render();
}
function func_render() {
var input = document.getElementById("input").value;
var the_list = Application.parse(input);
var renderer = new ByteStringParser(the_list);
var output = $("#output");
output.empty();
var did_fail = false;
var elem_start = 0;
while (renderer.pos < renderer.bytes.length) {
try {
elem_start = renderer.pos;
output.append(renderer.new_whatever());
} catch (e) {
if (e instanceof IOError) {
did_fail = true;
renderer.pos = elem_start;
break;
}
}
}
if (did_fail) {
output.append(renderer.new_failed_rest())
}
}
function func_use_single_row () {
var output = $("#output");
if($("#use_single_row").is(":checked")) {
output.addClass("single_row");
} else {
output.removeClass("single_row");
}
}