forked from Lekensteyn/hex-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhex-viewer.html
136 lines (123 loc) · 3.6 KB
/
hex-viewer.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
<!doctype html>
<html>
<head>
<title>Hex viewer</title>
<link rel="stylesheet" type="text/less" href="style.less">
<script src="less.js"></script>
<script>
// fallback if a local less.js file cannot be loaded.
window.less || document.write('<script src="' +
'https://cdnjs.cloudflare.com/ajax/libs/less.js/1.6.1/less.min.js"><\/script>');
window.less || alert('hex-viewer: compile style.less manually or find a less.js');
</script>
</head>
<body>
<div id="page">
<div id="hex-viewer"></div>
<p class="controls">
<label for="file-picker">Open binary</label>
<input type="file" id="file-picker">
<label for="file-picker2">Open annotations</label>
<input type="file" id="file-picker2">
</p>
</div>
<script src="polyfill.js"></script>
<script src="annotations.js"></script>
<script src="hex-viewer.js"></script>
<script>
var hexViewer = new HexViewer('hex-viewer');
// end core, begin params
var testData = array_to_arraybuffer(function () {
// fill array with 0x00 .. 0xff
var a = [];
for (var i = 0; i < 0x100; i++)
a[i] = i;
return a;
}());
try {
loadArrayBuffer('test.bin', hexViewer.loadData.bind(hexViewer));
} catch (ex) {
console.log('Failed to load test.bin: ', ex);
hexViewer.loadData(testData);
}
try {
loadString('test.txt', setAnnotations);
} catch (ex) {
console.log('Failed to load annotations from test.txt: ', ex);
}
function setAnnotations(text) {
try {
var annots = Annotations.loadFieldsizeFormat(text);
} catch (ex) {
alert("Annotations parse error: " + ex);
return;
}
hexViewer.setAnnotations(annots);
}
handleFilePicker('file-picker', hexViewer.loadData.bind(hexViewer));
handleFilePicker('file-picker2', setAnnotations, 'Text');
function handleFilePicker(id, loadData, type) {
type = type || 'ArrayBuffer';
var fileInput = document.getElementById(id);
fileInput.addEventListener('change', function () {
if (this.files.length > 0) {
handleFile(this.files[0]);
}
});
function handleFile(file) {
var reader = new FileReader();
reader.onload = function () {
loadData(this.result);
};
reader['readAs' + type](file);
}
}
function array_to_arraybuffer (arr) {
var buffer = new ArrayBuffer(arr.length);
var byteView = new Uint8Array(buffer);
arr.forEach(function (b, i) {
byteView[i] = b;
});
return buffer;
}
function loadString(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.onload = function () {
if (xhr.responseText) {
var len = xhr.responseText.length;
if (len > 1024 * 1024 &&
!confirm('Continue processing ' + len + ' chars?')) {
console.log('Aborted processing of ' + url);
return;
}
callback(xhr.responseText);
} else {
console.log('Cannot load text for ' + url);
}
};
xhr.send(null);
}
function loadArrayBuffer(url, callback) {
var xhr = new XMLHttpRequest();
xhr.open('get', url, true);
xhr.responseType = 'arraybuffer';
xhr.onload = function () {
if (xhr.response) {
var len = xhr.response.byteLength;
if (len > 1024 * 1024 &&
!confirm('Continue processing ' + len + ' bytes?')) {
console.log('Aborted processing of ' + url);
return;
}
callback(xhr.response);
} else {
console.log('Cannot load ArrayBuffer for ' + url);
}
};
xhr.send(null);
}
</script>
</body>
</html>
<!-- vim: set sw=4 et ts=4: -->