-
Notifications
You must be signed in to change notification settings - Fork 13
/
index.html
598 lines (564 loc) · 16.8 KB
/
index.html
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
<!DOCTYPE html>
<html lang="en">
<!--
This is a simple tool to visual msgpack data.
I haven't bothered with using jQuery etc..
because I wanted to keep the whole thing a
single file.
Also, it's not cross browser compatible so suck it!
-->
<head>
<meta charset="utf-8" />
<title>
msgpack visualizer
</title>
<style>
html, body, div, span,
h1, h2, p, a, form, label {
margin: 0;
padding: 0;
border: 0;
font-size: 100%;
font: inherit;
vertical-align: baseline;
}
body {
line-height: 1;
padding: 0px 8px;
font-family: sans-serif;
}
h1 {
font-size: 48px;
margin-bottom: 8px;
}
h2 {
font-size: 16px;
margin-bottom: 8px;
}
#datadisplay {
width: 400px;
height: 100%;
overflow-y:scroll;
font-family: monospace;
vertical-align: top;
float: left;
}
#datadisplay a {
margin-right: 8px;
text-decoration: none;
color: #333;
text-transform: uppercase;
float: left;
}
#datadisplay a.highlight {
color: #33F;
}
#output {
margin-left: 440px;
overflow-y:scroll;
}
#json-obj.beautiful {
font-family: monospace;
white-space: pre;
}
</style>
<!-- Using the awesome base64 -> ArrayBuffer lib by Daniel Guerrero -->
<script type="text/javascript">
/*
https://raw.github.com/danguer/blog-examples/master/js/base64-binary.js
Copyright (c) 2011, Daniel Guerrero
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* Neither the name of the Daniel Guerrero nor the
names of its contributors may be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL DANIEL GUERRERO BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* Uses the new array typed in javascript to binary base64 encode/decode
* at the moment just decodes a binary base64 encoded
* into either an ArrayBuffer (decodeArrayBuffer)
* or into an Uint8Array (decode)
*
* References:
* https://developer.mozilla.org/en/JavaScript_typed_arrays/ArrayBuffer
* https://developer.mozilla.org/en/JavaScript_typed_arrays/Uint8Array
*/
var Base64Binary = {
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
/* will return a Uint8Array type */
decodeArrayBuffer: function(input) {
var bytes = Math.ceil((3 * input.length) / 4.0);
var ab = new ArrayBuffer(bytes);
this.decode(input, ab);
return ab;
},
decode: function(input, arrayBuffer) {
//get last chars to see if are valid
var lkey1 = this._keyStr.indexOf(input.charAt(input.length - 1));
var lkey2 = this._keyStr.indexOf(input.charAt(input.length - 1));
var bytes = Math.ceil((3 * input.length) / 4.0);
if (lkey1 == 64) bytes--; //padding chars, so skip
if (lkey2 == 64) bytes--; //padding chars, so skip
var uarray;
var chr1, chr2, chr3;
var enc1, enc2, enc3, enc4;
var i = 0;
var j = 0;
if (arrayBuffer) uarray = new Uint8Array(arrayBuffer);
else uarray = new Uint8Array(bytes);
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
for (i = 0; i < bytes; i += 3) {
//get the 3 octects in 4 ascii chars
enc1 = this._keyStr.indexOf(input.charAt(j++));
enc2 = this._keyStr.indexOf(input.charAt(j++));
enc3 = this._keyStr.indexOf(input.charAt(j++));
enc4 = this._keyStr.indexOf(input.charAt(j++));
chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;
uarray[i] = chr1;
if (enc3 != 64) uarray[i + 1] = chr2;
if (enc4 != 64) uarray[i + 2] = chr3;
}
return uarray;
}
}
function highlight(elems, className) {
elems.forEach(function(elem) {
elem.className = className;
});
}
function onPositionHover() {
var allElemsNodeList = datadisplay.getElementsByTagName("a");
var allElems = [];
for(var i=0, ii=allElemsNodeList.length; i<ii; i++) {
allElems.push(allElemsNodeList[i]);
}
highlight(allElems, "");
var coords = this.rel.split(",");
var start = parseInt(coords[0], 10);
var end = start + parseInt(coords[1], 10) - 1;
highlight(allElems.filter(function(elem) {
var indx = parseInt(elem.rel, 10);
return indx >= start && indx <= end;
}), "highlight");
}
// Logger
var logoutput;
var datadisplay;
var jsonobj;
function errlog(str) {
var div = document.createElement("div");
div.innerText = str;
logoutput.appendChild(div);
}
function log(position, length, str) {
var div = document.createElement("div");
var a = document.createElement("a");
a.href = "#";
a.rel = position + "," + length;
a.innerHTML = "position " + position + ": ";
a.addEventListener("mouseover", onPositionHover);
div.appendChild(a);
var span = document.createElement("span");
span.innerText = str;
div.appendChild(span);
logoutput.appendChild(div);
}
function clear() {
logoutput.innerHTML = "";
datadisplay.innerHTML = "";
jsonobj.innerHTML = "";
}
// adds a byteval to the list
function displayByte(byteVal, indx) {
var byteStr = byteVal.toString(16);
if(byteStr.length == 1) {
byteStr = "0" + byteStr;
}
var a = document.createElement("a");
a.href = "#";
a.id = "byte_" + indx;
a.rel = indx;
a.innerHTML = byteStr;
datadisplay.appendChild(a);
}
// http://jsfromhell.com/geral/utf-8
function bufToString(buf) {
var uint8Buff = new Uint8Array(buf);
var byteCount = uint8Buff.byteLength;
var output = "";
var a, b;
for (var i = 0; i < byteCount; i++) {
a = uint8Buff[i];
if (a & 0x80) {
b = uint8Buff[i + 1];
if (((a & 0xfc) == 0xc0) && ((b & 0xc0) == 0x80)) {
output += String.fromCharCode(((a & 0x03) << 6) + (b & 0x3f));
} else {
output += String.fromCharCode(128);
i++;
}
} else {
output += String.fromCharCode(a);
}
}
return output;
}
// javascript msgpack parsing taken from
// https://github.com/creationix/msgpack-js/blob/master/msgpack.js
// and then heavily tweaked to my needs
function decode(dataView) {
var offset = 0;
function map(length) {
var value = {};
for (var i = 0; i < length; i++) {
var key = parse();
value[key] = parse();
}
return value;
}
function raw(length) {
var value = bufToString(dataView.buffer.slice(offset, offset + length));
offset += length;
return value;
}
function array(length) {
var value = new Array(length);
for (var i = 0; i < length; i++) {
value[i] = parse();
}
return value;
}
function parse() {
var type = dataView.getUint8(offset);
var value, length;
switch (type) {
// nil
case 0xc0:
log(offset, 1, "NULL");
offset++;
return null;
// false
case 0xc2:
log(offset, 1, "false");
offset++;
return false;
// true
case 0xc3:
log(offset, 1, "true");
offset++;
return true;
// bin 8
case 0xc4:
length = dataView.getUint8(offset + 1);
var startOffset = offset;
offset += 2;
var result = raw(length);
log(startOffset, offset - startOffset, "bin 8 marker with " + length + " items");
return result;
// bin 16
case 0xc5:
length = dataView.getUint16(offset + 1);
var startOffset = offset;
offset += 3;
var result = raw(length);
log(startOffset, offset - startOffset, "bin 16 marker with " + length + " items");
return result;
// bin 32
case 0xc6:
length = dataView.getUint32(offset + 1);
var startOffset = offset;
offset += 5;
var result = raw(length);
log(startOffset, offset - startOffset, "bin 32 marker with " + length + " items");
return result;
// float
case 0xca:
value = dataView.getFloat32(offset + 1);
log(offset, 5, "float value " + value);
offset += 5;
return value;
// double
case 0xcb:
value = dataView.getFloat64(offset + 1);
log(offset, 9, "double value " + value);
offset += 9;
return value;
// uint8
case 0xcc:
value = dataView.getUint8(offset + 1);
log(offset, 2, "uint8 value " + value);
offset += 2;
return value;
// uint 16
case 0xcd:
value = dataView.getUint16(offset + 1);
log(offset, 3, "uint16 value " + value);
offset += 3;
return value;
// uint 32
case 0xce:
value = dataView.getUint32(offset + 1);
log(offset, 5, "uint32 value " + value);
offset += 5;
return value;
// uint64
case 0xcf:
// value = buffer.readUInt64BE(offset + 1);
log(offset, 9, "uint64 marker - cannot parse uint64 to javascript, setting to Infinity");
offset += 9;
return Infinity;
// int 8
case 0xd0:
value = dataView.getInt8(offset + 1);
log(offset, 2, "int8 value " + value);
offset += 2;
return value;
// int 16
case 0xd1:
value = dataView.getInt16(offset + 1);
log(offset, 3, "int16 value " + value);
offset += 3;
return value;
// int 32
case 0xd2:
value = dataView.getInt32(offset + 1);
log(offset, 5, "int32 value " + value);
offset += 5;
return value;
// int 64
case 0xd3:
log(offset, 9, "int64 marker - cannot parse uint64 to javascript, setting to Infinity");
offset += 9;
return Infinity;
// map 16
case 0xde:
length = dataView.getUint16(offset + 1);
var startOffset = offset;
offset += 3;
var result = map(length);
log(startOffset, offset - startOffset, "map16 marker with " + length + " items");
return result;
// map 32
case 0xdf:
length = dataView.getUint32(offset + 1);
var startOffset = offset;
offset += 5;
var result = map(length);
log(startOffset, offset - startOffset, "map32 marker with " + length + " items");
return result;
// array 16
case 0xdc:
length = dataView.getUint16(offset + 1);
var startOffset = offset;
offset += 3;
var result = array(length);
log(startOffset, offset - startOffset, "array16 marker with " + length + " items");
return result;
// array 32
case 0xdd:
length = dataView.getUint32(offset + 1);
var startOffset = offset;
offset += 5;
var result = array(length);
log(startOffset, offset - startOffset, "array32 marker with " + length + " items");
return result;
// raw 8
case 0xd9:
length = dataView.getUint8(offset + 1);
var startOffset = offset;
offset += 2;
var result = raw(length);
log(startOffset, offset - startOffset, "raw8 marker - " + result);
return result;
// raw 16
case 0xda:
length = dataView.getUint16(offset + 1);
var startOffset = offset;
offset += 3;
var result = raw(length);
log(startOffset, offset - startOffset, "raw16 marker - " + result);
return result;
// raw 32
case 0xdb:
length = dataView.getUint32(offset + 1);
var startOffset = offset;
offset += 5;
var result = raw(length);
log(startOffset, offset - startOffset, "raw32 marker - " + result);
return result;
}
// FixRaw
if ((type & 0xe0) === 0xa0) {
length = type & 0x1f;
var startOffset = offset;
offset++;
var result = raw(length);
log(startOffset, offset - startOffset, "fixed length raw marker - " + result);
return result;
}
// FixMap
if ((type & 0xf0) === 0x80) {
length = type & 0x0f;
var startOffset = offset;
offset++;
var result = map(length);
log(startOffset, offset - startOffset, "fixed length map marker with " + length + " items");
return result;
}
// FixArray
if ((type & 0xf0) === 0x90) {
length = type & 0x0f;
var startOffset = offset;
offset++;
var result = array(length);
log(startOffset, offset - startOffset, "fixed length array marker with " + length + " items");
return result;
}
// Positive FixNum
if ((type & 0x80) === 0x00) {
log(offset, 1, "postive fix length number with value " + type);
offset++;
return type;
}
// Negative Fixnum
if ((type & 0xe0) === 0xe0) {
value = dataView.getInt8(offset);
log(offset, 1, "negative fix length number with value " + value);
offset++;
return value;
}
throw new Error("Unknown type 0x" + type.toString(16));
}
var value = parse();
if (offset !== dataView.byteLength) {
var overflow = dataView.byteLength - offset;
errlog(overflow + " trailing bytes");
var remainBytes = dataView.buffer.slice(offset, overflow);
errlog("trailing chars: " + raw(overflow));
// if (remainBytes.byteLength) {
// setTimeout(function() {
// try {
// errlog("attempting to parse trailing chars");
// errlog(decode(new DataView(remainBytes)));
// } catch (e) {
// errlog("Error parsing input: " + e.message);
// e.stack && errlog(e.stack);
// }
// }, 0)
// }
}
return value;
}
function indent(){
var jsonobj = document.getElementById("json-obj");
var chk = document.getElementById('chk_beautify');
if (chk.checked){
if (!jsonobj.className.match(/beautiful/)) {
jsonobj.className += ' beautiful';
}
} else {
jsonobj.className = jsonobj.className.replace(' beautiful', '');
}
}
var outputscroller;
function parseBase64() {
var wh = window.innerHeight;
if (!logoutput) {
logoutput = document.getElementById("log");
}
if (!jsonobj) {
jsonobj = document.getElementById("json-obj");
}
if(!datadisplay) {
datadisplay = document.getElementById("datadisplay");
datadisplay.style.height = (wh - 110) + "px";
}
if(!outputscroller) {
outputscroller = document.getElementById("output");
outputscroller.style.height = (wh - 110) + "px";
}
clear();
var input = document.getElementById("data").value;
var buff = null;
try {
buff = Base64Binary.decodeArrayBuffer(input);
var dataView = new DataView(buff);
for(var i=0, ii=dataView.byteLength; i<ii; i++) {
var byteVal = dataView.getUint8(i);
displayByte(byteVal, i);
}
jsonobj.innerHTML = JSON.stringify(decode(dataView), null, 2);
indent();
} catch (e) {
errlog("Error parsing input: " + e.message);
e.stack && errlog(e.stack);
}
}
function getQSParams() {
return window.location.search
.substr(1)
.split('&')
.reduce(function(result, item) {
var parts = item.split('=');
result[parts[0]] = decodeURIComponent(parts[1]);
return result;
}, { });
}
function checkUrl() {
var params = getQSParams();
// Accept base64 data
if (params['base64'] !== undefined) {
document.getElementById("data").value = params['base64'];
parseBase64();
}
};
</script>
</head>
<body id="home" onload="checkUrl()">
<a href="https://github.com/sugendran/msgpack-visualizer"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_gray_6d6d6d.png" alt="Fork me on GitHub"></a>
<div id="header">
<h1>
msgpack visualizer
</h1>
<h2>
Created by sugendran in an attempt to find out why things are breaking.
It's not pretty, but it works :)
</h2>
</div>
<div id="content">
<div>
<form action="javascript:parseBase64()">
<label for="data">Base64: </label>
<input type="text" id="data" style="width:60vw" value="gaVoZWxsb6V3b3JsZA==" />
<button onclick="parseBase64()">Parse</button>
<label><input type="checkbox" id="chk_beautify" onchange="indent()"> indent</label>
</form>
</div>
<div>
<div id="datadisplay"></div>
<div id="output">
<div id="json-obj" style="margin-bottom: 16px;"></div>
<div id="log"></div>
</div>
</div>
</div>
</body>
</html>