-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.html
319 lines (305 loc) · 8.54 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
<html>
<head>
<title>CBOR debugger</title>
</head>
<script>
var hexDigits = '0123456789ABCDEF';
var byteToHex = [];
var hexToByte = {};
for (var byteValue = 0; byteValue < 256; byteValue++) {
var hexValue = hexDigits.charAt(byteValue>>4) + hexDigits.charAt(byteValue&0x0f);
byteToHex[byteValue] = hexValue;
hexToByte[hexValue] = byteValue;
}
function hexToUtf8(hexString) {
var escaped = "";
for (var i = 0; i < hexString.length; i += 2) {
escaped += '%' + hexString.substring(i, i + 2);
}
try {
return decodeURIComponent(escaped);
} catch (e) {
throw new Error('UTF-8 decode error');
}
}
function cborHexDecodeType(hex, pos) {
var firstByte = hexToByte[hex.substring(pos, pos += 2)];
var majorType = firstByte>>5, value = firstByte&0x1f;
//annotate(pos - 2, 'type ' + majorType + ', additional ' + value);
if (value === 31) {
// Special value for indefinite-length arrays/objects
value = null;
} else if (value > 27) {
throw new Error('Unrecognised extra value: ' + value);
} else if (value >= 24) {
var extraBytes = {
24: 0,
25: 1,
26: 3,
27: 7
}[value];
if (majorType == 7) {
value = 'float:' + hex.substring(pos, pos + 2 + 2*extraBytes);
pos += 2 + 2*extraBytes;
} else {
//annotate(pos, extraBytes + ' extra byte(s)');
value = hexToByte[hex.substring(pos, pos += 2)];
// Decode as big-endian unsigned int
while (extraBytes--) {
value = value*256 + hexToByte[hex.substring(pos, pos += 2)];
}
//annotate(null, 'value: ' + value);
}
}
return {
type: majorType,
value: value,
nextPos: pos
}
}
function cborHexDecode(hex, pos) {
if (pos >= hex.length) throw new TypeError('Unexpected end of input');
annotate(pos, '');
var majorType = cborHexDecodeType(hex, pos);
pos = majorType.nextPos;
if (majorType.type === 0) {
annotate(null, 'positive integer: ' + majorType.value);
return {
value: majorType.value,
nextPos: pos
};
} else if (majorType.type === 1) {
annotate(null, 'negative integer: ' + (-1 - majorType.value));
return {
value: -1 -majorType.value,
nextPos: pos
};
} else if (majorType.type === 2) {
annotate(null, 'byte sequence (length ' + majorType.value + ')');
indent(1, pos)
// Byte sequence
var length = majorType.value;
var byteHex = hex.substring(pos, pos + length*2);
annotate(pos, 'binary');
indent(-1, pos+length*2);
return {
value: hex.substring(pos, pos + length*2),
nextPos: pos + length*2
};
} else if (majorType.type === 3) {
// UTF-8 string
annotate(null, 'UTF-8 string (length ' + majorType.value + ')');
indent(1, pos);
var length = majorType.value;
var stringHex = hex.substring(pos, pos + length*2);
try {
annotate(pos, '<span class="literal">' + JSON.stringify(hexToUtf8(stringHex)).replace(/</g, '<') + '</span>');
} catch (e) {
// nothing
}
indent(-1, pos + length*2);
return {
value: hexToUtf8(stringHex),
nextPos: pos + length*2
};
} else if (majorType.type === 4 || majorType.type === 5) {
// Array
var length = (majorType.value == null) ? '???' : majorType.value;
if (majorType.type === 4) {
annotate(null, '<span class="composite">Array (length ' + length + ')</span>');
} else {
annotate(null, '<span class="composite">Object (' + length + ' pairs)</span>');
}
indent(1, pos);
var length = majorType.value;
var result = [];
if (length !== null) {
if (majorType.type === 5) {
// Want key/value pairs
length *= 2;
}
for (var i = 0; i < length; i++) {
var item = cborHexDecode(hex, pos);
result.push(item.value);
pos = item.nextPos;
}
} else {
var breakCode = byteToHex[0xff];
while (hex.substring(pos, pos + 2) !== breakCode) {
var item = cborHexDecode(hex, pos);
result.push(item.value);
pos = item.nextPos;
}
annotate(pos, '<span class="break-code">break code</span>');
pos += 2; // Break code
}
if (majorType.type === 5) {
var obj = {};
for (var i = 0; i < result.length; i += 2) {
obj[result[i]] = result[i + 1];
}
result = obj;
}
indent(-1, pos);
return {
value: result,
nextPos: pos
};
} else if (majorType.type === 6) {
annotate(null, 'Semantic annotation: ' + majorType.value);
indent(1, pos);
var item = cborHexDecode(hex, pos);
indent(-1, item.nextPos);
// Format-specific enhancements
if (majorType.value === 0 && typeof item.value === 'string') {
item.value = new Date(item.value); // date-time string
} else if (majorType.value === 1 && typeof item.value === 'number') {
item.value = new Date(item.value*1000); // UTC seconds
}
return {
value: item.value,
nextPos: item.nextPos
};
} else if (majorType.type === 7) {
if (typeof majorType.value == 'string') {
annotate(null, majorType.value);
return {value: false, nextPos: pos};
} else if (majorType.value === 20) {
annotate(null, '<span class="literal">false</span>');
return {value: false, nextPos: pos};
} else if (majorType.value === 21) {
annotate(null, '<span class="literal">true</span>');
return {value: true, nextPos: pos};
} else if (majorType.value === 22) {
annotate(null, '<span class="literal">null</span>');
return {value: null, nextPos: pos};
} else if (majorType.value === 23) {
annotate(null, '<span class="literal">undefined</span>');
return {value: undefined, nextPos: pos};
}
throw new Error('Unknown simple value: ' + majorType.value);
}
}
</script>
<style>
body {
background-color: #048;
color: #EEE;
font-family: "Verdana", sans-serif;
}
#cbor-hex {
width: 100%;
height: 8em;
font-family: monospace;
margin: 0;
border-radius: 5px;
padding: 0.4em;
background-color: #FCFEFF;
border: 2px solid #000;
}
#go-button {
display: block;
width: 100%;
border-radius: 5px;
height: 2em;
margin-top: 0.5em;
background-color: #DEF;
font-size: 0.9em;
border: 2px solid #000;
}
#go-button:hover {
background-color: #FFF;
cursor: pointer;
}
#result {
padding: 0.5em;
margin: 3px;
margin-top: 1em;
border-radius: 5px;
background-color: #195792;
}
#result .invisible {
color: #195792;
}
.hex {
}
.comment {
font-family: Verdana, sans-serif;
font-size: 0.8em;
font-style: italic;
color: #8BD;
margin-left: 3em;
}
.error {
color: #F66;
text-shadow: 0px 0px 2px #000;
}
.break-code {
color: #FB8;
}
.composite {
color: #8F8;
}
.literal {
color: #FF8;
}
.indent {
padding-left: 4ex;
}
</style>
<body>
<p>Paste your CBOR (as hexidecimal):</p>
<textarea id="cbor-hex">bf61610161629f0203ffff</textarea>
<input type="button" id="go-button" value="GO">
<pre id="result">
</pre>
<script>
var hexInput = document.getElementById('cbor-hex');
var goButton = document.getElementById('go-button');
var result = document.getElementById('result');
var decoded, annotate, indent;
goButton.onclick = function () {
var htmlLines = [];
var hex = hexInput.value = hexInput.value.replace(/0x/, '').replace(/\/\/[^\n]*/g, '').replace(/[^0-9a-fA-F]/g, '').toUpperCase();
var lastPos = 0, lastLabel = '';
annotate = function (pos, label) {
pos = pos || lastPos;
if (pos === lastPos) {
lastLabel = lastLabel + (lastLabel && label? ', ' : '') + label;
return;
}
var hextract = hex.substring(lastPos, pos);
line = '<div class="line">';
line += '<span class="hex">' + hextract + '</span> <span class="invisible">//</span> <span class="comment">' + lastLabel + '</span>';
line += '</div>';
htmlLines.push(line);
lastPos = pos;
lastLabel = label;
};
indent = function (amount, pos) {
annotate(pos, '');
while (amount > 0) {
htmlLines.push('<div class="indent">');
amount--;
}
while (amount < 0) {
htmlLines.push('</div>');
amount++;
}
};
try {
decoded = cborHexDecode(hex, 0);
annotate(hex.length, 'end of input');
result.innerHTML = htmlLines.join('');
} catch (e) {
annotate(lastPos, '<span class="error">' + e.message + '</span>');
annotate(lastPos + 2, 'end of input');
result.innerHTML = htmlLines.join('');
throw e;
}
console.log(decoded);
};
goButton.onclick();
</script>
</body>
</html>