forked from nmcveity/HexViewJS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhexview.js
183 lines (144 loc) · 4.78 KB
/
hexview.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
var HEX = '0123456789ABCDEF'
function dec2_to_hex(dec)
{
if (dec < 0)
dec = 0
if (dec > 255)
dec = 255
return HEX.charAt(Math.floor(dec / 16)) + HEX.charAt(dec % 16)
}
function dec_to_hex8(dec)
{
var str = ""
for (var i = 3; i >= 0; i--)
{
str += dec2_to_hex((dec >> (i*8)) & 255)
}
return str;
}
function remove_whitespace(str)
{
return str.replace(/\n/g, "")
.replace(/\t/g, "")
.replace(/ /g, "")
.replace(/\r/g, "")
}
var BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
function base64_decode(encoded)
{
var decoded = ""
for (var i = 0; i < encoded.length; i += 4)
{
var ch0 = encoded.charAt(i+0)
var ch1 = encoded.charAt(i+1)
var ch2 = encoded.charAt(i+2)
var ch3 = encoded.charAt(i+3)
var index0 = BASE64_CHARS.indexOf(ch0);
var index1 = BASE64_CHARS.indexOf(ch1);
var index2 = BASE64_CHARS.indexOf(ch2);
var index3 = BASE64_CHARS.indexOf(ch3);
decoded += String.fromCharCode((index0 << 2) | (index1 >> 4))
decoded += String.fromCharCode(((index1 & 15) << 4) | (index2 >> 2))
decoded += String.fromCharCode(((index2 & 3) << 6) | index3)
}
return decoded
}
function markup_hexviewwindow(div, index)
{
var bin_data = base64_decode(remove_whitespace(div.text()))
var line_data
var title = div.attr("title")
var highlights_str = $("form#hexviewwindow_params input[name='highlights']", div).attr("value").split(',')
var highlights = []
for (var i = 0; i < highlights_str.length; i++)
{
highlights.push(highlights_str[i].split(":"))
}
var params = title.split(':')
var step = parseInt($("form#hexviewwindow_params input[name='row_width']", div).attr("value"))
var word_size = parseInt($("form#hexviewwindow_params input[name='word_size']", div).attr("value"))
var row_break = parseInt($("form#hexviewwindow_params input[name='row_break']", div).attr("value"))
var caption = $("form#hexviewwindow_params input[name='caption']", div).attr("value")
div.text("")
div.append("<table></table>")
var offset = 0
function apply_highlights(index)
{
for (var j = 0; j < highlights.length; j++)
{
if ((index >= highlights[j][0]) && (index <= highlights[j][1]))
{
if (index == highlights[j][0])
{
$("table tr td:last", div).addClass("hexviewerwindow_border_start")
}
if (index == highlights[j][1])
{
$("table tr td:last", div).addClass("hexviewerwindow_border_end")
}
$("table tr td:last", div).addClass("hexviewerwindow_code_hi hexviewerwindow_border_middle")
$("table tr td:last", div).attr("style", "background-color: " + highlights[j][2] + ";")
$("table tr td:last", div).attr("title", highlights[j][3])
runlen += 1
}
else
{
$("table tr td:last", div).addClass("hexviewerwindow_code")
}
}
}
if (caption)
$("table", div).append("<caption>" + caption + "</caption>")
while (bin_data.length > 0)
{
line_data = bin_data.slice(0, step)
bin_data = bin_data.slice(step)
$("table", div).addClass("hexviewerwindow_table")
$("table", div).append("<tr></tr>").addClass("hexviewerwindow")
$("table tr:last", div).append("<td>" + dec_to_hex8(offset) + " </td>")
/*$("table tr:last", div).append("<td>0x" + dec_to_hex8(offset) + "</td>")*/
$("table tr td:last", div).addClass("hexviewerwindow_offset")
var runlen = 0
for (var i = 0; i < line_data.length; i += word_size)
{
var num = ""
for (var j = 0; j < word_size; j++)
{
num += dec2_to_hex(line_data.charCodeAt(i+j))
}
if (i == row_break - 1)
{
$("table tr:last", div).append("<td>" + num + "  </td>")
apply_highlights(offset+i)
}
else
{
$("table tr:last", div).append("<td>" + num + " </td>")
apply_highlights(offset+i)
}
}
var text = ""
for (var i = 0; i < line_data.length; i++)
{
var cc = line_data.charCodeAt(i)
if ((cc >= 32) && (cc <= 126))
{
text = text + line_data.charAt(i)
}
else
{
text = text + "."
}
}
if (line_data.length < step)
$("table tr td:last", div).attr("colspan", Math.floor((step - line_data.length) / word_size) + 1)
offset += step
$("table tr:last", div).append("<td>" + text + "</td>")
$("table tr td:last", div).addClass("hexviewerwindow_visual")
}
}
$(document).ready(function () {
$("div.hexviewwindow").each(function (index) {
markup_hexviewwindow($(this), index)
})
})