-
Notifications
You must be signed in to change notification settings - Fork 12
/
drop.js
1407 lines (1192 loc) · 36.2 KB
/
drop.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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*jslint bitwise: true */
///////////////////////////////////////////////////////////////////////////////
// Globals
///////////////////////////////////////////////////////////////////////////////
var data;
var file;
var filename;
var hexii;
var colorHex;
var reader;
var arrayBuffer;
var MAX_FILE_SIZE = 10 * 1024 * 1024; // 10MB
var LINES_TO_DISPLAY = 200;
var FONT_HEIGHT = 15;
var BYTES_PER_LINE = 16;
var NUM_BYTES_PER_DISPLAY = BYTES_PER_LINE * LINES_TO_DISPLAY;
var isValueElementSet = false;
var addressString = "";
var hexString = "";
var asciiString = "";
var clickedNode;
var selectData = [];
var selectedNodes = [];
var selectStart = 0;
var selectEnd = 0;
var selectedNode = null;
var hexDumpStart;
var hexDumpEnd;
var editor;
var parser;
var treedata = [];
var expectedOffset = 0; // for parse tree
var lastHexDumpPosition = 0;
var gotoLocation = 0;
var scrollNeeded = false;
///////////////////////////////////////////////////////////////////////////////
// Utility functions
///////////////////////////////////////////////////////////////////////////////
var hexArray = ["0", "1", "2", "3",
"4", "5", "6", "7",
"8", "9", "A", "B",
"C", "D", "E", "F"
];
var displayableAscii = [
".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".",
".",
".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".", ".",
".",
" ", "!", "\"", "#", "$", "%", "&", "\'", "(", ")", "*", "+", ",",
"-", ".", "\/",
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "<", "=",
">", "?",
"@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N",
"O",
"P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "[", "\", "]",
"^", "_",
".", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n",
"o",
"p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "{", "|", "}", "~",
"."
];
function convertToHex(dec) {
var decToHex = hexArray[(dec & 0xf0) >> 4] + hexArray[(dec & 0x0f)];
return (decToHex);
}
function convertToHexWord(dec) {
var decToHex =
hexArray[(dec & 0xf0000000) >> 0x1c] + hexArray[(dec & 0x0f000000) >>
0x18] +
hexArray[(dec & 0x00f00000) >> 0x14] + hexArray[(dec & 0x000f0000) >>
0x0f] +
hexArray[(dec & 0x0000f000) >> 0x0c] + hexArray[(dec & 0x00000f00) >>
0x08] +
hexArray[(dec & 0x000000f0) >> 0x04] + hexArray[(dec & 0x0000000f) >>
0x00];
return (decToHex);
}
function addHexIdentifier(value) {
return value + "h";
}
function intToHex(val, addIdentifier, pad) {
addIdentifier = (typeof addIdentifier === "undefined") ? true :
addIdentifier;
pad = (typeof pad === "undefined") ? true : pad;
// Convert value to hex
var str = String(val.toString(16));
// Pad with 0's
if (pad === true) {
while (str.length < 8) {
str = '0' + str;
}
} else {
charsToAdd = 8 - str.length;
while (charsToAdd > 0) {
str = ' ' + str;
charsToAdd = charsToAdd - 1;
}
}
if (addIdentifier) {
return addHexIdentifier(str);
} else {
return str;
}
}
function hexToInt(str) {
str = str.replace('h', '');
return parseInt(str, 16);
}
function dispAscii(val) {
if (val > 127) {
return '.';
}
return displayableAscii[val];
}
function isDisplayable(val) {
if (val > 127) {
return false;
}
if (val === 0x2e) {
return true; // real period
}
if (displayableAscii[val] === '.') {
return false;
}
return true;
}
function str2ArrayBuffer(str) {
arrayBuffer = new ArrayBuffer(str.length);
var bufView = new Uint8Array(arrayBuffer);
for (var i = 0, strLen = str.length; i < strLen; i++) {
bufView[i] = str.charCodeAt(i);
}
var tmp = arrayBuffer.byteLength;
return bufView;
}
function strToArray(str) {
a = [];
for (var i = 0; i < str.length; i++) {
a.push(str.charCodeAt(i));
}
return a;
}
function startsWith(haystack, needle) {
if (haystack.length < needle.length) return false;
for (var i = 0; i < needle.length; i++) {
if (haystack[i] != needle[i]) return false;
}
return true;
}
function showDialog(str, title, okBtn) {
$("#dialog-message").html(str);
if (okBtn) {
$("#dialog-message").dialog({
title: title,
modal: true,
disabled: false,
buttons: {
Ok: function() {
$(this).dialog("close");
}
}
});
} else {
$("#dialog-message").dialog({
title: title,
modal: true,
disabled: false
});
}
$("#dialog-message").dialog("enable");
$("#dialog-message").dialog("open");
}
function removeDialog() {
$("#dialog-message").dialog("close");
}
function showError(str) {
$("#dialog-message").html(
"<span class=\"ui-icon ui-icon-alert\" style=\"float: left; margin: 0 7px 50px 0;\"></span>" +
str);
$("#dialog-message").dialog({
title: "Error",
modal: true,
disabled: false,
buttons: {
Ok: function() {
$(this).dialog("close");
}
}
});
$("#dialog-message").dialog("enable");
$("#dialog-message").dialog("open");
}
function snapSelectionToWord() {
// Copied from http://jsfiddle.net/rrvw4/23/
var sel;
// Check for existence of window.getSelection() and that it has a
// modify() method. IE 9 has both selection APIs but no modify() method.
if (window.getSelection && (sel = window.getSelection()).modify) {
sel = window.getSelection();
if (!sel.isCollapsed) {
// Detect if selection is backwards
var range = document.createRange();
range.setStart(sel.anchorNode, sel.anchorOffset);
range.setEnd(sel.focusNode, sel.focusOffset);
var backwards = range.collapsed;
range.detach();
// modify() works on the focus of the selection
var endNode = sel.focusNode,
endOffset = sel.focusOffset;
sel.collapse(sel.anchorNode, sel.anchorOffset);
var direction = [];
if (backwards) {
direction = ['backward', 'forward'];
} else {
direction = ['forward', 'backward'];
}
sel.modify("move", direction[0], "character");
sel.modify("move", direction[1], "word");
sel.extend(endNode, endOffset);
sel.modify("extend", direction[1], "character");
sel.modify("extend", direction[0], "word");
}
} else if ((sel = document.selection) && sel.type != "Control") {
var textRange = sel.createRange();
if (textRange.text) {
textRange.expand("word");
// Move the end back to not include the word's trailing space(s),
// if necessary
while (/\s$/.test(textRange.text)) {
textRange.moveEnd("character", -1);
}
textRange.select();
}
}
}
function selectText(element) {
var doc = document,
text = doc.getElementById(element),
range, selection;
if (doc.body.createTextRange) {
range = document.body.createTextRange();
range.moveToElementText(text);
range.select();
} else if (window.getSelection) {
selection = window.getSelection();
range = document.createRange();
range.selectNodeContents(text);
selection.removeAllRanges();
selection.addRange(range);
}
}
///////////////////////////////////////////////////////////////////////////////
// Strings view
///////////////////////////////////////////////////////////////////////////////
function SetStrings() {
var stringsData = [];
var str = [];
var minLength = 4;
var startOffset = 0;
var isUnicode = false;
// TODO make sure this can handle unicode
// check 41 00 41 42 43 44 00 -> ABCD
for (var i = 0; i < data.length; i++) {
if (isUnicode && data[i] === 0 && i - startOffset % 2 == 1) {
// no op
} else if (isDisplayable(data[i])) {
str.push(dispAscii(data[i]));
} else if (data[i] === 0 && i - startOffset == 1) {
isUnicode = true;
} else {
if (str.length >= minLength) {
var uOrA = "A";
if (isUnicode) uOrA = "U";
stringsData.push("<a class=\"stringFound\" href=\"#" + intToHex(
startOffset) + "\">" + intToHex(startOffset) + " " + uOrA + " " +
str.join("") + "</a><br>");
}
str = [];
startOffset = i + 1;
isUnicode = false;
}
}
$('#strings').html(stringsData.join(""));
$('.stringFound').click(function(e) {
e.preventDefault();
$("#accordion").accordion("activate", 0);
gotoLocation = this.href.split('#')[1].replace('h', '');
gotoLocation = hexToInt(gotoLocation);
scrollToByte(gotoLocation);
return false;
});
}
///////////////////////////////////////////////////////////////////////////////
// File reading
///////////////////////////////////////////////////////////////////////////////
function handleFileSelect(evt) {
evt.stopPropagation();
evt.preventDefault();
var files; // FileList
if (evt.dataTransfer) {
files = evt.dataTransfer.files;
} else {
files = evt.target.files;
}
file = files[0]; // File object
if (file.size > MAX_FILE_SIZE) {
showError(
"File is too large.<br>IceBuddha currently only accepts files under 10MB."
);
return;
}
showDialog("Loading " + file.name + " (" + file.size + " bytes)",
"Loading...", false);
createTemplate(file.name, file.size);
reader = new FileReader();
reader.onloadend = handleFinishedRead;
readFileSlice(0, MAX_FILE_SIZE);
}
function handleDragOver(evt) {
evt.stopPropagation();
evt.preventDefault();
evt.dataTransfer.dropEffect = 'copy';
}
function readFileSlice(start, end) {
if (file === null) return;
// Determine how much to read
var blob;
if (file.slice) {
blob = file.slice(start, end);
} else if (file.webkitSlice) {
blob = file.webkitSlice(start, end);
} else if (file.mozSlice) {
blob = file.mozSlice(start, end);
}
reader.readAsArrayBuffer(blob);
}
function handleFinishedRead(evt) {
if (evt.target.readyState == FileReader.DONE) {
var length = evt.target.result.byteLength;
arrayBuffer = evt.target.result;
data = new Uint8Array(arrayBuffer, 0, length);
displayHexDump(0);
SetParseTree(ChooseParseScript());
SetStrings();
removeDialog();
}
}
function ChooseParseScript() {
parseScript = "unknown.py";
if (startsWith(data, strToArray("MZ"))) {
parseScript = "pe.py";
} else if (startsWith(data, strToArray("GIF"))) {
parseScript = "gif.py";
} else if (startsWith(data, [0xfe, 0xed, 0xfa, 0xce]) ||
startsWith(data, [0xce, 0xfa, 0xed, 0xfe]) ||
startsWith(data, [0xfe, 0xed, 0xfa, 0xcf]) ||
startsWith(data, [0xcf, 0xfa, 0xed, 0xfe]) ||
startsWith(data, [0xca, 0xfe, 0xba, 0xbe])
) {
parseScript = "mach_o.py";
}
var filetype = parseScript.split('.')[0];
$('#parseScriptSelection').text(filetype);
return parseScript;
}
function onOddRow(offset) {
return ((offset >> 4) % 2) == 1;
}
function displayHexDump(position) {
lastHexDumpPosition = position;
var output = [""];
var address = [""];
var hex = [""];
var ascii = [""];
length = NUM_BYTES_PER_DISPLAY;
if (position + length > data.length) {
length = data.length - position;
}
bytesAbove = position;
if (bytesAbove > NUM_BYTES_PER_DISPLAY * 0.25) {
bytesAbove = NUM_BYTES_PER_DISPLAY * 0.25;
}
hexDumpStart = position - bytesAbove;
hexDumpEnd = position + length;
var column = 0;
var lineIsZeroes = true;
var prevLineZeroes = true;
var i;
for (i = hexDumpStart; i < hexDumpEnd; i++) {
// Show address
if (column === 0) {
prevLineZeroes = lineIsZeroes;
lineIsZeroes = true;
// Check if this line is all zeroes
for (var zeroChecki = i; zeroChecki < hexDumpEnd && zeroChecki < i + 16; zeroChecki++) {
if (data[zeroChecki] !== 0) {
lineIsZeroes = false;
break;
}
}
if (hexii == 1 && lineIsZeroes) {
address.push(
"<div class=\"zeroline\"> </div>"
);
hex.push("<div class=\"zeroline\">");
} else {
address.push("<i class=\"");
if (onOddRow(i) && hexii != 1) {
address.push("alt_row");
}
address.push("\">");
var pad = true;
if (hexii == 1 && !prevLineZeroes) {
pad = false;
}
address.push(intToHex(i, false, pad));
address.push(" </i><br>\n");
}
}
// Show value
hex.push("<i id=\"h");
hex.push(i);
hex.push("\" class=\"hex");
if (onOddRow(i) && hexii != 1) {
hex.push(" alt_row");
}
hex.push(" v" + convertToHex(data[i]));
hex.push("\">");
if (hexii === 0) {
hex.push(hexArray[(data[i] & 0xf0) >> 4]);
hex.push(hexArray[(data[i] & 0x0f)]);
} else {
if (data[i] === 0) {
hex.push(" ");
} else if (data[i] == 0xff) {
hex.push("##");
} else {
if (dispAscii(data[i]) == "." && data[i] != 0x2e) {
hex.push(hexArray[(data[i] & 0xf0) >> 4]);
hex.push(hexArray[(data[i] & 0x0f)]);
} else {
hex.push("<i class=\"asciiPeriod\">.</i>");
hex.push(dispAscii(data[i]));
}
}
}
if (column == 7 || column == 15) {
hex.push(" ");
}
hex.push(" </i>");
// Show ascii
if (hexii === 0) {
ascii.push("<i id=\"a");
ascii.push(i);
ascii.push("\" class=\"ascii");
if (onOddRow(i)) {
ascii.push(" alt_row");
}
ascii.push(" v" + convertToHex(data[i]));
ascii.push("\">");
ascii.push(dispAscii(data[i]));
ascii.push("</i>");
}
// Add extra formatting
column++;
if (column % 16 === 0) {
if (hexii == 1 && lineIsZeroes) {
hex.push("</div>\n");
} else {
hex.push("<br>\n");
}
ascii.push("<br>\n");
column = 0;
}
}
// Add some formatting for data < 16 bytes
if (hexDumpEnd - hexDumpStart < 16) {
for (i = hexDumpEnd; i < 16; i++) {
// Show value
hex.push("<i class=\"hex");
if (((i >> 4) % 2) == 1) {
hex.push(" alt_row");
}
hex.push("\">");
hex.push(" ");
if (i % 15 === 0 || i % 8 === 0) {
hex.push(" ");
}
hex.push(" </i>");
}
}
hex.push("<i class=\"hex\">]</i>");
// Show last line
if (hexii == 1 && prevLineZeroes) {
address.push("<i>");
address.push(intToHex(i, true, prevLineZeroes));
address.push(" </i><br>\n");
}
// Set html
addressString = address.join("");
hexString = hex.join("");
asciiString = ascii.join("");
footer = "";
if (position + NUM_BYTES_PER_DISPLAY < data.length) {
footer = "<footer>Loading more data...</footer>";
}
$('#byte_content').html(getByteContentHTML(addressString, hexString +
footer, asciiString, position - bytesAbove));
// Add right-click menu
$("#hexCell").contextMenu({
menu: 'hexContextMenu',
onSelect: function(e) {
hexId = e.target.closest('#hexCell i.hex').attr('id');
if (e.action == "Download") {
// Download file
var bb = new BlobBuilder();
bb.append(arrayBuffer);
var blob = bb.getBlob("application/octet-stream");
saveAs(blob, filename);
} else {
showDialog("The item's action is: " + e.action + "\nTarget:" +
hexId, "Click detected", true);
}
}
});
$('#byte_content').unbind('scroll', outOfRangeScrollHandler);
// On refresh, scroll to the correct place
$('#byte_content').scrollTo($("#h" + position), 1, {
onAfter: function() {
//
// After getting to the location, set events to cause data refreshes on scrolls
//
// Scroll up
if (position - NUM_BYTES_PER_DISPLAY * 0.25 > 0) {
scrollPointOffsetUp = position - NUM_BYTES_PER_DISPLAY * 0.25;
$scrollPointUp = $('#h' + scrollPointOffsetUp);
opts = {
offset: 0,
context: '#byte_content'
};
$scrollPointUp.waypoint(function(event, direction) {
if (direction === 'up') {
if (mouseIsDown) return;
// Upwards scroll event triggered
$scrollPointUp.waypoint('destroy');
$scrollPointUp.detach();
displayHexDump(scrollPointOffsetUp);
}
}, opts);
}
// Scroll down
if (position + NUM_BYTES_PER_DISPLAY * 0.75 < data.length) {
scrollPointOffsetDown = position + (NUM_BYTES_PER_DISPLAY *
0.75);
$scrollPointDown = $('#h' + scrollPointOffsetDown);
opts = {
offset: '100%',
context: '#byte_content'
};
$scrollPointDown.waypoint(function(event, direction) {
if (direction === 'down') {
if (mouseIsDown) return;
// Downward scroll event triggered
$scrollPointDown.waypoint('destroy');
$scrollPointDown.detach();
displayHexDump(scrollPointOffsetDown);
}
}, opts);
}
// If the user grabs the scroll bar, make sure refresh the screen
$('#byte_content').bind('scroll', outOfRangeScrollHandler);
}
});
$("#asciiCell").mouseover(mouseoverBytes).mouseout(mouseoutBytes);
$("#hexCell").mouseover(mouseoverBytes).mouseout(mouseoutBytes);
$("#hexCell").mouseup(snapSelectionToWord);
$("#addressCell").mouseup(snapSelectionToWord);
if (!isValueElementSet) {
SetValueElement(0);
}
reHighlite();
setHexColor();
}
var outOfRangeScrollHandler = function() {
scrollPos = $('#byte_content').scrollTop();
topOfContent = $('#byteFillerAbove').height();
contentHeight = FONT_HEIGHT * LINES_TO_DISPLAY;
if ((scrollPos < topOfContent - (FONT_HEIGHT * 1)) ||
(scrollPos > (topOfContent + contentHeight) + (FONT_HEIGHT * 1))) {
scrollLocation = (scrollPos / FONT_HEIGHT) * BYTES_PER_LINE;
if (mouseIsDown) {
// Wait for the scroll to finish
scrollNeeded = true;
return;
}
scrollToByte(scrollLocation);
}
};
var mouseIsDown = false;
$(document).mousedown(function() {
mouseIsDown = true;
});
$(document).mouseup(function() {
mouseIsDown = false;
if (scrollNeeded) {
outOfRangeScrollHandler();
}
});
function getByteContentHTML(address, hex, ascii, start) {
output = [];
if (!data) return;
// Calculate size of the scroll view and any filling that should be added before the hexdump
// for smoother looking auto-scrolling
tableHeight = data.length / BYTES_PER_LINE * FONT_HEIGHT;
preHeight = start / BYTES_PER_LINE * FONT_HEIGHT;
tableHeightStyle = "style=\"min-height:" + tableHeight + "px; height:" +
tableHeight + "px; border-spacing: 0px;\"";
preHeightStyle = "style=\"min-height:" + preHeight + "px; height:" +
preHeight + "px;\"";
output.push("<table border=0 cellpadding=0 cellspacing=0 " +
tableHeightStyle + " id=\"byteScrollableArea\">");
output.push("<tr " + preHeightStyle + "><td " + preHeightStyle +
" id=\"byteFillerAbove\"><td><td></tr>");
output.push("<tr>");
output.push(
"<td id=\"addressCell\" style=\"padding: 0 0 0 0;\" class=\"address\">"
);
output.push(address);
output.push("</td><td id=\"hexCell\" style=\"padding: 0 0 0 0;\">");
output.push(hex);
output.push("</td><td id=\"asciiCell\">");
output.push(ascii);
output.push("</td></tr></table>");
ret = output.join("");
return ret;
}
function setHexColor() {
var i;
if (colorHex == 1) {
for (i = 0; i <= 0xff; i++) {
if (i === 0x00) {
$(".v00").addClass("hexColor0");
} else if (dispAscii(i) == "." && i != 0x2e) {
$(".v" + hexArray[(i & 0xf0) >> 4] + "" + hexArray[(i & 0x0f)]).addClass(
"hexColorNonAscii");
}
}
} else {
for (i = 0; i <= 0xff; i++) {
if (i === 0x00) {
$(".v00").removeClass("hexColor0");
} else if (dispAscii(i) == "." && i != 0x2e) {
$(".v" + hexArray[(i & 0xf0) >> 4] + "" + hexArray[(i & 0x0f)]).removeClass(
"hexColorNonAscii");
}
}
}
}
function createTemplate(fileName, fileSize) {
filename = fileName;
var output = [];
// Set defaults for new file read
isValueElementSet = false;
addressString = "";
hexString = "";
asciiString = "";
// Set byte content
output = [];
output.push("<div id=\"accordion\">");
output.push("<h3><strong>" + escape(fileName) + "</strong> - " + fileSize +
" bytes</h3>");
output.push("<div id=\"fileParsing\">");
output.push("<table border=0 cellpadding=0 cellspacing=0>\n");
output.push(" <tr><td width=650px>\n");
output.push(" <div id=\"byte_content\">");
output.push(getByteContentHTML("", "", "", 0));
output.push(" </div>\n");
output.push(
" <td style=\"height:100%\"><table border=0 cellpadding=0 cellspacing=0 style=\"height:208\">\n"
);
output.push(" <tr><td id=\"value\">");
output.push(
" <tr><td id=\"goto\">Go to<br><input id=\"gotoInput\" value=\"0000000h\"></td>"
);
output.push("</table></table>\n");
output.push(
"<div id=\"parseTreeEnvelope\"><div id=\"parsetree\"></div></div>\n");
output.push("</div>");
output.push("<h3>Parse as: <i id=\"parseScriptSelection\">unknown</i></h3>");
output.push("<div id=\"editor\"></div>");
output.push("<h3>Strings</h3>");
output.push("<div id=\"strings\"></div>");
output.push("</div>");
// Right-click menu
output.push(
"<div id=\"hexContextMenu\" style=\"display: none;\">\n" +
"<ul>" +
"<li id=\"Download\"><a href=\"#Download\">Download</a></li>" +
"<li id=\"Edit\"><a href=\"#Edit\">Edit</a></li>" +
"</ul>" +
"</div>");
output.push(
"<div id=\"parseTreeContextMenu\" style=\"display: none;\">\n" +
"<ul>" +
"<li id=\"Colorize\"><a href=\"#Colorize\">Colorize</a></li>" +
"<li id=\"Goto\"><a href=\"#Goto\">Goto</a></li>" +
"<li id=\"CompressChildren\"><a href=\"#CompressChildren\">Compress children</a></li>" +
"<li id=\"ExpandChildren\"><a href=\"#ExpandChildren\">Expand children</a></li>" +
"<li id=\"DownloadParse\"><a href=\"#DownloadParse\">Download parsed data</a></li>" +
"</ul>" +
"</div>");
$('#content').html(output.join(""));
$("#accordion").accordion({
clearStyle: true,
autoHeight: false,
beforeActivate: function(event, ui) {
if (ui.newHeader[0].id == 'ui-accordion-accordion-header-0') {
// If we are showing the hexdump view, recreate it before it is displayed
ParseInstructions(editor.getSession().getValue());
}
},
activate: function(event, ui) {
if (ui.newHeader[0].id == 'ui-accordion-accordion-header-1') {
// If we are showing the ACE editor, tell it to refresh after the accordion
// expands
editor.renderer.onResize(true);
editor.renderer.updateFull(force = true);
}
},
});
/////////////////////////////////////////////////////////////////////////////
//
// Set appmenu
//
$("#appmenu").html(
'<input type="file" id="fileSelect"/> <input type="checkbox" id="hexii"/>HexII <input type="checkbox" id="colorHex"/>Color hex'
);
/////////////////////////////////////////////////////////////////////////////
//
// Check settings
//
//
// hexii
//
hexii = 0;
$('#hexii').change(function() {
if ($(this).is(":checked")) {
hexii = 1;
} else {
hexii = 0;
}
if (filename !== undefined) {
displayHexDump(0);
}
$.cookie('hexii', hexii);
});
if ($.cookie('hexii') !== undefined) {
hexii = $.cookie('hexii');
if (hexii == 1) hexii = 1;
else hexii = 0;
}
if ($_GET('hexii')) {
var hexiiParam = $_GET('hexii');
if (hexiiParam == 1) hexii = 1;
else hexii = 0;
}
if (hexii == 1) {
$("#hexii").prop("checked", true);
}
//
// colorHex
//
colorHex = 0;
$('#colorHex').change(function() {
if ($(this).is(":checked")) {
colorHex = 1;
} else {
colorHex = 0;
}
$.cookie('colorHex', colorHex);
setHexColor();
});
if ($.cookie('colorHex') !== undefined) {
colorHex = $.cookie('colorHex');
if (colorHex == 1) colorHex = 1;
else colorHex = 0;
}
if ($_GET('colorhex')) {
var colorhexParam = $_GET('colorhex');
if (colorhexParam == 1) colorHex = 1;
else colorHex = 0;
}
if (colorHex == 1) {
$("#colorHex").prop("checked", true);
}
setHexColor();
/////////////////////////////////////////////////////////////////////////////
//
// Set hot-keys
//
// ctrl+g: Moves to the goto input box
$(document).bind('keydown', 'ctrl+g', function() {
var text_input = $('#gotoInput');
text_input.focus();
text_input.select();
return false;
});
/////////////////////////////////////////////////////////////////////////////
// Goto input
$('#gotoInput').keypress(function(e) {
var code = (e.keyCode ? e.keyCode : e.which);
// Remove the error styling on any typing
$('#gotoInput').removeClass("InputError");
if (code == 13) {
try {
var input = $('#gotoInput').val();
// Convert to javascript
input = input.replace(/([0-9a-zA-Z]+)h/g, "0x$1");
input = "gotoLocation=" + input;
// Eval it
var gotoFunc = new Function(input); // jshint ignore:line
gotoFunc();
if (gotoLocation < 0) gotoLocation = data.length + gotoLocation;
scrollToByte(gotoLocation);
SetValueElement(gotoLocation);
selectText('h' + gotoLocation);
e.preventDefault();
} catch (exception) {
$('#gotoInput').addClass("InputError");
}
}
});
/////////////////////////////////////////////////////////////////////////////
$('#byte_content').scrollTo(0); // Start at top
// hack for chrome to force scrolling
$('#byte_content').scroll(function() {
mouseIsDown = false;
if (scrollNeeded) {
outOfRangeScrollHandler();
}
});
$addressCell = $('#addressCell');
$hexCell = $('#hexCell');
$asciiCell = $('#asciiCell');
}