forked from gratajik/Hex0r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex0r.js
182 lines (141 loc) · 5.2 KB
/
hex0r.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
var BASE64_CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
var HEX = '0123456789ABCDEF';
function markup_hex0rwindow(div) {
var step = parseInt($(div).data('row-width'));
var wordSize = parseInt($(div).data('word-size'));
var rowBreak = parseInt($(div).data('row-break'));
var caption = $(div).data('caption');
var highlightsStr = $(div).data('highlights').split(',');
var trim = $(div).data('trim').toString() == "true"; ;
var base64 = $(div).data('base64').toString() == "true";
var showLineNums = $(div).data('show-line-nums').toString() == "true";
var rawData = div.text();
var offset = parseInt($(div).data('offset')) || 0;
if (trim == true) {
rawData = remove_whitespace(rawData);
}
if (base64 == true) {
rawData = base64_decode(rawData);
}
var lineData;
var highlights = [];
for (var hi = 0; hi < highlightsStr.length; hi++) {
highlights.push(highlightsStr[hi].split(":"));
}
div.text("");
div.append("<table></table>");
var skip = offset % step;
offset -= skip;
function applyHighlights(index) {
for (var idx = 0; idx < highlights.length; idx++) {
if ((index >= highlights[idx][0]) && (index <= highlights[idx][1])) {
if (index == highlights[idx][0]) {
$("table tr td:last", div).addClass("hex0rwindow_border_start");
}
if (index == highlights[idx][1]) {
$("table tr td:last", div).addClass("hex0rwindow_border_end");
}
$("table tr td:last", div).addClass("hex0rwindow_code_hi hex0rwindow_border_middle");
$("table tr td:last", div).attr("style", "backround-color: " + highlights[idx][2] + ";");
$("table tr td:last", div).attr("title", highlights[idx][3]);
runlen += 1;
}
else {
$("table tr td:last", div).addClass("hex0rwindow_code");
}
}
}
if (caption)
$("table", div).append("<caption>" + caption + "</caption>");
while (rawData.length > 0) {
lineData = rawData.slice(0, step - skip);
rawData = rawData.slice(step - skip);
if(skip > 0){
lineData = (" ").repeat(skip) + lineData;
}
$("table", div).addClass("hex0rwindow_table");
$("table", div).append("<tr></tr>").addClass("hex0rwindow");
if (showLineNums == true) {
$("table tr:last", div).append("<td>" + dec_to_hex8(offset) + " </td>");
$("table tr td:last", div).addClass("hex0rwindow_offset");
}
var runlen = 0;
for (var idxData = 0; idxData < lineData.length; idxData += wordSize) {
var num = "";
if(idxData < skip){
num = " "
} else {
for (var idxWs = 0; idxWs < wordSize; idxWs++) {
num += dec2_to_hex(lineData.charCodeAt(idxData + idxWs));
}
}
if (idxData == rowBreak - 1) {
$("table tr:last", div).append("<td>" + num + "  </td>");
applyHighlights(offset + idxData);
}
else {
$("table tr:last", div).append("<td>" + num + " </td>");
applyHighlights(offset + idxData);
}
}
var text = "";
for (var i = 0; i < lineData.length; i++) {
var cc;
if(i < skip){
text = text + " ";
continue;
} else {
cc = lineData.charCodeAt(i);
}
if ((cc >= 32) && (cc <= 126)) {
text = text + lineData.charAt(i);
}
else {
text = text + ".";
}
}
if (lineData.length < step)
$("table tr td:last", div).attr("colspan", Math.floor((step - lineData.length) / wordSize) + 1);
offset += step;
$("table tr:last", div).append("<td>" + text + "</td>");
$("table tr td:last", div).addClass("hex0rwindow_visual");
skip = 0;
}
}
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, "");
}
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;
}