forked from Lekensteyn/hex-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhex-viewer.js
432 lines (379 loc) · 15.4 KB
/
hex-viewer.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
var HexViewer = (function (id) {
'use strict';
/* note: change in CSS too */
var COLUMNS = 16;
/* prefs */
var autoCenter = true;
// .hex-viewer
// + .bits-n-bytes
// + .hexasc
// + .hex
// + .asc
// + .bits
// + .annotations
var container = document.getElementById(id);
var bnbPanel = document.createElement('div');
var hexascPanel = document.createElement('div');
var hexPanel = document.createElement('div');
var bitPanel = document.createElement('div');
var ascPanel = document.createElement('div');
var annoPanel = document.createElement('div');
container.className = 'hex-viewer';
bnbPanel.className = 'bits-n-bytes';
hexascPanel.className = 'hexasc';
hexPanel.className = 'hex';
ascPanel.className = 'asc';
bitPanel.className = 'bits';
annoPanel.className = 'annotations';
[hexPanel, ascPanel].forEach(function (panel) {
hexascPanel.appendChild(panel);
});
[hexascPanel, bitPanel].forEach(function (panel) {
bnbPanel.appendChild(panel);
});
[bnbPanel, annoPanel].forEach(function (panel) {
container.appendChild(panel);
});
// ByteView of the data buffer
var byteView;
// contains HTMLElements for the hex and ascii boxes
var hexBoxes = [];
var ascBoxes = [];
// contains HTMLElements for annotations
var annoLines = [];
// annotations object
var annotations;
function annosIntoView() {
var elms = annoPanel.querySelectorAll('.hover-before, .hover, .hover-after');
if (elms.length) {
centerElement(elms[0], elms[elms.length - 1]);
}
}
function bytesIntoView(begin, end) {
begin = Math.min(ascBoxes.length - 1, begin);
end = Math.min(ascBoxes.length - 1, end);
centerElement(ascBoxes[begin], ascBoxes[end]);
}
function hiliteAnnotBits(annots, begin_i, end_i) {
var annot_i = begin_i;
var beginByte = Math.floor(annots[begin_i].offset / 8);
var endByte = Math.ceil((annots[end_i].offset + annots[end_i].length) / 8);
endByte = Math.min(endByte, byteView.byteLength);
bitPanel.innerHTML = '';
var bitFragment = document.createDocumentFragment();
var in_slice = false, slice = -1;
for (var b = beginByte; b < endByte; ++b) {
var byteData = byteView[b];
for (var bi = 7; bi >= 0; --bi) {
if (8 * b + (7 - bi) == annots[annot_i].offset) {
in_slice = true;
++slice;
}
var bitBox = document.createElement('span');
if (in_slice) {
bitBox.className = slice % 2 ? 'odd' : 'even';
}
bitBox.textContent = byteData & (1 << bi) ? 1 : 0;
bitFragment.appendChild(bitBox);
// check following bit
var endBit = annots[annot_i].offset + annots[annot_i].length;
if (in_slice && 8 * b + (8 - bi) == endBit) {
in_slice = false;
if (annot_i < end_i) {
++annot_i;
}
}
}
}
bitPanel.appendChild(bitFragment);
}
// highlight the bytes that share a bit with the slice (if the annotation
// falls outside the slice, extend the slice)
function hiliteBits(offset_bit, bits, baseCls) {
var byteRange;
if (!baseCls)
baseCls = 'hover';
var annots;
// indexes of annotation elements just before or after selection
var prev_anno_i = -1, next_anno_i = end + 1;
if (annotations) {
annots = annotations.getAnnotations();
// annotation indexes
var i, begin = Infinity, end = annots.length - 1;
// find begin of annotations
for (i = 0; i < annots.length; ++i) {
// if the annotation starts in this slice
if (annots[i].offset >= offset_bit) {
prev_anno_i = i - 1;
next_anno_i = prev_anno_i + 1;
// check if annotation is actually in the slice (i.e. if the
// annotation starts before the end of the slice)
if (annots[i].offset < offset_bit + bits) {
begin = i;
}
break;
}
}
// if there are annotations in range, find the end
if (begin <= end) {
for (; i < annots.length; ++i) {
// if the annotation ends past the end
if (annots[i].offset + annots[i].length > offset_bit + bits) {
// end is last annotation that is contained in slice
end = i - 1;
next_anno_i = i;
break;
}
}
offset_bit = annots[begin].offset;
bits = annots[end].offset - offset_bit + annots[end].length;
// TODO: consider marking on hover too
if (baseCls === 'selected') {
hiliteAnnotBits(annots, begin, end);
}
}
// mark previous, "selected" and next annotations
if (prev_anno_i >= 0) {
annoLines[prev_anno_i].classList.add(baseCls + '-before');
}
for (i = begin; i <= end; ++i) {
annoLines[i].classList.add(baseCls);
}
if (next_anno_i < annots.length) {
annoLines[next_anno_i].classList.add(baseCls + '-after');
}
}
// selected bits range
hiliteBytesBits(offset_bit, bits, baseCls);
// mark bytes before and after selection
if (annotations) {
var annot;
if (prev_anno_i >= 0) {
annot = annots[prev_anno_i];
hiliteBytesBits(annot.offset, annot.length, baseCls + '-before');
}
if (next_anno_i < annots.length) {
annot = annots[next_anno_i];
hiliteBytesBits(annot.offset, annot.length, baseCls + '-after');
}
}
}
function hiliteBytesBits(offset_bit, bits, className) {
var begin = offset_bit / 8;
// annotations may be longer than the bytes
var end_bit = Math.min(8 * ascBoxes.length, offset_bit + bits);
if (offset_bit >= end_bit) {
return;
}
if (offset_bit % 8) {
begin = Math.floor(begin);
ascBoxes[begin].classList.add('partial');
hexBoxes[begin].classList.add('partial');
}
for (var byte_no = begin; byte_no < end_bit / 8; ++byte_no) {
ascBoxes[byte_no].classList.add(className);
hexBoxes[byte_no].classList.add(className);
}
if (end_bit % 8) {
var end = Math.floor(end_bit / 8);
ascBoxes[end].classList.add('partial');
hexBoxes[end].classList.add('partial');
}
}
// returns begin byte and length (in bytes) of the element (byte in boxes or
// annotations). null if the elm element is invalid.
function findElmBytes(src_boxes, elm) {
if (src_boxes) {
var byte_no = src_boxes.indexOf(elm);
if (byte_no == -1) {
return null;
}
// find first annotation that contains (part of) this byte
if (annotations) {
var annots = annotations.getAnnotations();
for (var i = 0; i < annots.length; ++i) {
var annot = annots[i];
var abegin = annot.offset;
var aend = abegin + annot.length;
if (8 * byte_no < aend && abegin < 8 * (byte_no + 1)) {
var range = Annotations.annot_to_byterange(annot);
return [range[0], range[1] - range[0]];
}
}
}
// fallback to the whole byte
return [byte_no, 1];
} else {
if (!elm.classList.contains('line')) {
return null;
}
var begin = Math.floor(elm.dataset.offset / 8);
var len = elm.dataset.byteEnd - elm.dataset.byteStart;
return [begin, len];
}
}
// generated listener callback to highlight boxes and annotations
function addSelecter(src_boxes) {
return function (ev) {
var byteInfo = findElmBytes(src_boxes, ev.target);
if (!byteInfo) {
return;
}
hiliteBits(8 * byteInfo[0], 8 * byteInfo[1]);
if (autoCenter) {
if (src_boxes) { // bytes
annosIntoView();
} else { // annotations
bytesIntoView(byteInfo[0], byteInfo[0] + byteInfo[1]);
}
}
};
}
function clearSelecter() {
removeSelectionClasses('hover');
}
function removeSelectionClasses(base) {
[base, base + '-before', base + '-after'].forEach(function (className) {
var affected = container.getElementsByClassName(className);
for (var i = affected.length - 1; i >= 0; --i) {
var classList = affected[i].classList;
// HACK: drop 'partial' if (1) the class to be removed is the
// selection or (2) the class to be removed is not the selection
// and the box is not part of a selection
if (base === 'selected' || !classList.contains('selected')) {
classList.remove('partial');
}
classList.remove(className);
}
});
}
function togglePermSelect(boxes) {
return function (ev) {
var byteInfo = findElmBytes(boxes, ev.target);
if (!byteInfo) {
return;
}
var isSelected = ev.target.classList.contains('selected');
removeSelectionClasses('selected');
if (!isSelected) {
hiliteBits(8 * byteInfo[0], 8 * byteInfo[1], 'selected');
if (boxes) { // bytes
annosIntoView();
} else { // annotations
bytesIntoView(byteInfo[0], byteInfo[0] + byteInfo[1]);
}
autoCenter = false;
} else {
// TODO: if 'partial' is kept, then the refresh is not needed
hiliteBits(8 * byteInfo[0], 8 * byteInfo[1], 'hover');
// no selection, no bits to highlight
bitPanel.innerHTML = '';
// no selection - feel free to center!
autoCenter = true;
}
};
}
hexPanel.addEventListener('mouseover', addSelecter(hexBoxes));
ascPanel.addEventListener('mouseover', addSelecter(ascBoxes));
annoPanel.addEventListener('mouseover', addSelecter(null));
hexPanel.addEventListener('mouseout', clearSelecter);
ascPanel.addEventListener('mouseout', clearSelecter);
annoPanel.addEventListener('mouseout', clearSelecter);
hexPanel.addEventListener('click', togglePermSelect(hexBoxes));
ascPanel.addEventListener('click', togglePermSelect(ascBoxes));
annoPanel.addEventListener('click', togglePermSelect(null));
function to_ascii(b) {
if (b >= 0x20 && b < 0x7F) {
return String.fromCharCode(b);
}
// not a printable ASCII character, ignore.
return '.';
}
// center elements on screen if not already visible. elm and bottomElm must
// have the same parent, elm must be located before bottomElm.
function centerElement(elm, bottomElm) {
var par = elm.parentNode;
if (!bottomElm) bottomElm = elm;
var parRect = par.getBoundingClientRect();
var topRect = elm.getBoundingClientRect();
var botRect = bottomElm.getBoundingClientRect();
var topDiff = topRect.top - parRect.top;
var botDiff = botRect.bottom - parRect.bottom;
// topDiff is negative if element is clipped at top by its parent
if (topDiff < 0) {
// so scroll up a little bit (remove the diff)
par.scrollTop += topDiff;
} else if (botDiff > 0) {
// botDiff is positive if the element bottom gets past its parent
// bottom, so scroll down
par.scrollTop += botDiff;
}
}
// Loads an ArrayBuffer into the page (hex, ascii)
function loadData(buffer) {
// bytes per line
byteView = new Uint8Array(buffer);
hexPanel.innerHTML = '';
ascPanel.innerHTML = '';
// cannot simply overwrite array as addSelector encapsulated it
hexBoxes.splice(0, hexBoxes.length);
ascBoxes.splice(0, ascBoxes.length);
// appending directly slows down
var hexFragment = document.createDocumentFragment();
var ascFragment = document.createDocumentFragment();
// length of biggest line number (in hex)
//var lineno_length = byteView.byteLength.toString(16).length;
var lineno_length = 7; // hard-coded 8 (including colon) in CSS
var lineno_pad = '0'.repeat(lineno_length);
for (var i = 0; i < byteView.byteLength; i += COLUMNS) {
var hexLine = document.createElement('div');
var ascLine = document.createElement('div');
hexLine.className = 'line';
ascLine.className = 'line';
hexLine.dataset.lineNo =
(lineno_pad + i.toString(16)).substr(-lineno_length);
var cols = Math.min(COLUMNS, byteView.byteLength - i);
for (var j = 0; j < cols; ++j) {
var hexBox = document.createElement('span');
var ascBox = document.createElement('span');
var b = byteView[i + j];
hexBox.textContent = ('0' + b.toString(16)).substr(-2);
ascBox.textContent = to_ascii(b);
hexBoxes.push(hexBox);
ascBoxes.push(ascBox);
hexLine.appendChild(hexBox);
ascLine.appendChild(ascBox);
}
hexFragment.appendChild(hexLine);
ascFragment.appendChild(ascLine);
}
hexPanel.appendChild(hexFragment);
ascPanel.appendChild(ascFragment);
}
/* annotations related stuff */
// annots is an Annotations instance
function setAnnotations(annots) {
annotations = annots;
annoPanel.innerHTML = '';
annoLines = [];
var annFragment = document.createDocumentFragment();
annots.getAnnotations().forEach(function (annot) {
var line = document.createElement('div');
line.className = 'line';
line.dataset.offset = annot.offset;
line.dataset.length = annot.length;
var byteRange = Annotations.annot_to_byterange(annot);
line.dataset.byteStart = byteRange[0];
line.dataset.byteEnd = byteRange[1];
line.textContent = annot.name + ': ' + annot.desc;
annoLines.push(line);
annFragment.appendChild(line);
});
annoPanel.appendChild(annFragment);
}
// exports
this.loadData = loadData;
this.handleFilePicker = handleFilePicker;
this.setAnnotations = setAnnotations;
});
/* vim: set sw=4 et ts=4: */