-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
shifts.html
573 lines (492 loc) · 23.5 KB
/
shifts.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
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
<!doctype html>
<html>
<head>
<title>Byte shift and constant-xor report</title>
<style>
#txt,#dec,#hex{ width:300px; }
Xtable{ display: inline-block; }
table, th, td {
xborder: 1px solid black;
}
table{
background-color: #DFDFDF;
border-radius: 0.5em;
padding:0.5em;
border: 0;
border-collapse: collapse;
border-spacing: 0 5px;
}
tbody{ font-family: "Lucida Console", Monaco, monospace; }
/* fix header border */
thead{
font-weight: bold;
border-bottom: 1px solid black;
text-align: center;
}
thead tr th {
border-bottom: 1px solid red;
border-collapse: separate;
border-spacing: 5px 5px;
}
tbody tr#first td {
border-top: 3px solid #4d4d4d;
border-collapse: separate;
border-spacing: 5px 5px;
}
/* fix column borders */
table { border: none; border-collapse: collapse; }
table td { border-left: 1px dashed #000; }
table td:first-child { border-left: none; }
tbody tr td{ padding-left:0.5em; padding-right: 0.5em; }
td{ white-space: pre; }
/* column colors */
thead tr.result-column-header td:nth-child(1){ background-color: rgba(255, 223, 211,0.5); }
thead tr td:nth-child(2){ background-color: rgba(223, 255, 211,0.5); }
thead tr td:nth-child(3){ background-color: rgba(211, 223, 255,0.5); }
tbody tr td:nth-child(1){ background-color: #FFDFD3; }
tbody tr:nth-child(even) > td:nth-child(1) { background-color: #FFEFE3; }
tbody tr td:nth-child(2){ background-color: #DFFFD3; }
tbody tr:nth-child(even) > td:nth-child(2) { background-color: #EFFFE3; }
tbody tr td:nth-child(3){ background-color: #D3DFFF; }
tbody tr:nth-child(even) > td:nth-child(3) { background-color: #E3EFFF; }
/* controls colors */
#controls{
background-color: skyblue;
width: max-content;
padding:10px;
border-radius: 0.5em;
line-height:1.5em;
}
</style>
<script>
//Polyfills for outdated browsers
if (!String.prototype.trim) {
String.prototype.trim = function () {
return this.replace(/^\s+|\s+$/g, '');
};
}
if (!Array.prototype.join) {
Array.prototype.join = function (delim) {
var out = "";
for (var i = 0; i < this.length; i++) {
if (out.length > 0)
out += delim;
out += this[i];
}
return out;
};
}
if (!String.prototype.padStart) {
String.prototype.padStart = function padStart(targetLength, padString) {
targetLength = targetLength >> 0; //floor if number or convert non-number to 0;
padString = String(typeof padString !== 'undefined' ? padString : ' ');
if (this.length > targetLength) {
return String(this);
} else {
targetLength = targetLength - this.length;
if (targetLength > padString.length) {
padString += padString.repeat(targetLength / padString.length); //append to original to ensure we are longer than needed
}
return padString.slice(0, targetLength) + String(this);
}
};
}
var OperationTypes = {
CHAR_FUNCTION: 0,
STR_FUNCTION: 1
};
var OperationRangeTypes = {
create: function (id, displayName, min, max) {
return {
id: id,
displayName: displayName,
min: min,
max: max
};
}
};
var Operations = {
create: function (id, displayName, funcType, rangeType, apply) {
return {
id: id,
displayName: displayName,
funcType: funcType,
rangeType: rangeType,
apply: apply
};
}
};
var StringUtils = {
entityMap: {
"&": "&",
"<": "<",
">": ">",
'"': '"',
"'": ''',
"/": '/'
},
charMap: {
0x00: "NUL",
0x07: "BEL",
0x08: "BS",
0x09: "Tab",
0x1B: "Esc",
0xa0: "NBSP",
0x0d: "CR",
0x0a: "LF",
0x7F: "Del"
},
cleanUnprintableChars: function (s) {
return s.replace(/[^\x20-\x7E\xA0-\xFF]/g, ".");
},
filterPrintable: function (s) {
for (var i = 0; i < s.length; i++) {
var a = s.charCodeAt(i);
if (!charfilter(a))
return false;
}
return true;
},
toCharCodes: function (s) {
var o = [];
for (var i = 0; i < s.length; i++) {
var a = s.charCodeAt(i);
o.push(a);
}
return o;
},
fromCharCodes: function (arrCodes) {
for (var i = 0; i < arrCodes.length; i++)
arrCodes[i] = +arrCodes[i];//convert to number
if (Object.apply) {
return String.fromCharCode.apply(null, arrCodes);
} else {
var o = "";
for (var i = 0; i < arrCodes.length; i++)
o += String.fromCharCode(parseInt(arrCodes[i]));
return o;
}
},
applyCharFunction: function (s, charFunction, n) {
let o = "";
for (let i = 0; i < s.length; i++) {
let a = charFunction(s.charCodeAt(i), n);
o += String.fromCharCode(a);
}
return o;
},
applyStrFunction: function (s, strFunction, n) {
let charCodes = [];
let hex = [];
let binary = [];
for (let i = 0; i < s.length; i++) {
let a = s.charCodeAt(i);
charCodes.push(a);
}
hex = ShiftsTool.decArrayToHexArray(charCodes);
binary = ShiftsTool.decArrayToBinArray(charCodes);
return strFunction(s, n, charCodes, hex, binary);
},
allCharsMatchFilter: function (s, charFilter) {
for (var i = 0; i < s.length; i++) {
var a = s.charCodeAt(i);
if (!charFilter(a))
return false;
}
return true;
},
isCharPrintable: function (c) {
return !((c < 0x20 || c > 0x7E) && (c !== 0x0A && c !== 0x0D));
},
createPrintableCharString: function (c) {
var sc = String.fromCharCode(c);
var printable = StringUtils.isCharPrintable(c);
if (typeof StringUtils.charMap[c] !== "undefined") {
sc = StringUtils.charMap[c];
printable = true;
}
if (!printable)
return c;
return c + "(" + StringUtils.escapeHTML(sc) + ")";
},
allCharsPrintable: function (s) {
return StringUtils.allCharsMatchFilter(s, StringUtils.isCharPrintable);
},
escapeHTML: function (string) {
return String(string).replace(/[&<>"'\/]/g, function (s) {
return StringUtils.entityMap[s];
});
},
stripWS: function (str) {
return str.replace(/\s/g, '');
}
};
var ShiftsTool = {
operationRanges: {
0: OperationRangeTypes.create("bytes", "Bytes", 0, 255),
1: OperationRangeTypes.create("bitshifts", "Bit Shifts", 0, 7),
2: OperationRangeTypes.create("lrbitshifts", "Bit Shifts", -7, 7),
get: function (i) {
return this[i];
}
},
operations: [
Operations.create("bshift", "Constant Byte Shift (modular addition)", OperationTypes.CHAR_FUNCTION, 0, function (c, n) {
return (c + n) % 256;
}),
Operations.create("cxor", "Constant Byte XOR", OperationTypes.CHAR_FUNCTION, 0, function (c, n) {
return c ^ n;
}),
Operations.create("bbshift", "Byte Bit-Shift", OperationTypes.CHAR_FUNCTION, 2, function (c, n) {
var newchar = c;
if(n<0) newchar<<= (-n);
else newchar>>=n;
return newchar&0xff;
}),
Operations.create("brotate", "Byte Bit-Rotation", OperationTypes.CHAR_FUNCTION, 1, function (c, n) {
var newchar = c;
for (var i = 0; i < n; i++) {
var bit = newchar & 1;
var shiftedchar = newchar >> 1;
var rotatedbit = bit << 7;
var rotatedchar = rotatedbit | shiftedchar;
newchar = rotatedchar;
}
return newchar;
}),
Operations.create("sbrotate", "Full String Bit-Rotation", OperationTypes.STR_FUNCTION, 2, function (s, n, charCodes, hex, binary) {
let binaryString = binary.join("");
if(n<0){
n=-n;
for(let i=0; i<n; i++){
let bit = binaryString.substr(0,1);
binaryString = binaryString.substr(1) + bit;
}
}else{
for(let i=0; i<n; i++){
let bit = binaryString.substr(-1,1);
binaryString = bit + binaryString.substr(0,binaryString.length-1);
}
}
binary = binaryString.match(/.{1,8}/g);
charCodes = ShiftsTool.binArrayToDecArray(binary);
return StringUtils.fromCharCodes(charCodes);
}),
Operations.create("cand", "Constant Byte AND", OperationTypes.CHAR_FUNCTION, 0, function (c, n) {
return c & n;
}),
Operations.create("cor", "Constant Byte OR", OperationTypes.CHAR_FUNCTION, 0, function (c, n) {
return c | n;
}),
Operations.create("cxnor", "Constant Byte XNOR", OperationTypes.CHAR_FUNCTION, 0, function (c, n) {
return (c ^ n) ^ 0xFF;
}),
Operations.create("cnand", "Constant Byte NAND", OperationTypes.CHAR_FUNCTION, 0, function (c, n) {
return (c & n) ^ 0xFF;
})
],
tryAllRange: function (operationFuncType, operationRangeType, s, operFunc, t) {
//var t = document.getElementById("tbl");
var flt = document.getElementById("flt").checked;
console.log(operationRangeType, operationRangeType.min, operationRangeType.max);
for (var n = operationRangeType.min; n <= operationRangeType.max; n++) {
var strOut = "";
if(operationFuncType===OperationTypes.CHAR_FUNCTION){
strOut = StringUtils.applyCharFunction(s, operFunc, n);;
}
if(operationFuncType===OperationTypes.STR_FUNCTION){
strOut = StringUtils.applyStrFunction(s, operFunc, n);;
}
var cleanedOut = StringUtils.cleanUnprintableChars(strOut);
var hexOut = this.decArrayToHexArray(StringUtils.toCharCodes(strOut)).join(" ");
if (flt)
if (!StringUtils.allCharsPrintable(strOut))
continue;
//n - f - d
var newRow = t.insertRow();
var cellKey = newRow.insertCell(0);
var cellOutString = newRow.insertCell(1);
var cellOutHex = newRow.insertCell(2);
var charString = StringUtils.createPrintableCharString(n);
cellKey.innerHTML = charString;
cellOutString.innerHTML = StringUtils.escapeHTML(cleanedOut);
cellOutHex.innerHTML = hexOut;
}
},
clearreport: function (parentElem) {
//var Parent = document.getElementById('tbl');
console.log("clearreport", parentElem);
while (parentElem.hasChildNodes())
parentElem.removeChild(parentElem.firstChild);
},
report_op_elem: function (resultsElem, op, s) {
var row, cell;
var tableElem = document.createElement("table");
tableElem.id = "results_" + op.id;
var thead = tableElem.createTHead();
row = thead.insertRow();
row.classList.add('result-type-header');
cell = row.insertCell(0);
cell.colSpan = 3;
var operationRangeType = this.operationRanges.get(op.rangeType);
//cell.innerHTML = "<b>"+StringUtils.escapeHTML(op.displayName)+" - All "+StringUtils.escapeHTML(operationType.displayName)+"</b>";
cell.innerHTML = StringUtils.escapeHTML(op.displayName) + " - All " + StringUtils.escapeHTML(operationRangeType.displayName);
var row2 = thead.insertRow();
row2.classList.add('result-column-header');
cell = row2.insertCell(0);
cell.innerHTML = "Key Value";
cell = row2.insertCell(1);
cell.innerHTML = "Decoded Text";
cell = row2.insertCell(2);
cell.innerHTML = "Decoded Hex Values";
var tbody = tableElem.createTBody();
this.tryAllRange(op.funcType,operationRangeType, s, op.apply, tbody);
resultsElem.appendChild(tableElem);
},
report_op: function (op, s) {
var resultContainer = document.getElementById("results");
this.clearreport(resultContainer);
this.report_op_elem(resultContainer, op, s);
},
report_all_ops: function (s) {
var resultContainer = document.getElementById("results");
this.clearreport(resultContainer);
for (var i = 0; i < this.operations.length; i++) {
this.report_op_elem(resultContainer, this.operations[i], s);
}
},
//-------------------------------------
radixArrayToDecArray: function (radixArray, radix) {
var decArray = [];
for (var i = 0; i < radixArray.length; i++) {
var charCode = parseInt(radixArray[i], radix);
decArray.push(charCode);
}
return decArray;
},
decArrayToRadixArray: function (decArray, radix, minZeroPadLength) {
let radixArray = [];
for (var i = 0; i < decArray.length; i++) {
let radixString = Number(+decArray[i]).toString(radix).padStart(minZeroPadLength,"0");
radixArray.push(radixString);
}
return radixArray;
},
decArrayToBinArray: function(decArray){
return this.decArrayToRadixArray(decArray,2,8);
},
binArrayToDecArray: function(binArray){
return this.radixArrayToDecArray(binArray,2);
},
decArrayToHexArray: function (decArray) {
return this.decArrayToRadixArray(decArray,16,2);
},
hexArrayToDecArray: function (hexArray) {
return this.radixArrayToDecArray(hexArray,16);
},
report_update_inputs_dec: function (decArray) {
var inpt = document.getElementById("txt");
var inpd = document.getElementById("dec");
var inph = document.getElementById("hex");
var hexArray = this.decArrayToHexArray(decArray);
var text = StringUtils.fromCharCodes(decArray);
inpd.value = decArray.join(" ");
inpt.value = text;
inph.value = hexArray.join(" ");
var result = {text: text, hex: hexArray, dec: decArray};
console.log(result)
return result;
},
report_update_inputs_txt: function (s) {
var decArray = StringUtils.toCharCodes(s);
return this.report_update_inputs_dec(decArray);
},
report_update_inputs_hex: function (hexArray) {
var decArray = this.hexArrayToDecArray(hexArray);
return this.report_update_inputs_dec(decArray);
},
report_op_from_select: function (s) {
var op = this.getSelectedOperationType();
console.log("report_op_from_select", op, s);
this.report_op(op, s);
},
report_txt: function () {
var input = document.getElementById("txt");
var values = this.report_update_inputs_txt(input.value);
this.report_op_from_select(values.text);
},
report_dec: function () {
var input = document.getElementById("dec");
var values = this.report_update_inputs_dec(input.value.split(" "));
this.report_op_from_select(values.text);
},
report_hex: function () {
var input = document.getElementById("hex");
var hexArray = StringUtils.stripWS(input.value).match(/.{1,2}/g);
var values = this.report_update_inputs_hex(hexArray);
this.report_op_from_select(values.text);
},
report_unk: function () {
//var inpt = document.getElementById("txt");
var inpd = document.getElementById("dec");
var inph = document.getElementById("hex");
if (inph.value !== "")
return this.report_hex();
if (inpd.value !== "")
return this.report_dec();
this.report_txt();
},
getSelectedOperationType: function () {
var opSelect = document.getElementById('operation-type');
var opId = opSelect.value;
for (var i = 0; i < this.operations.length; i++) {
var op = this.operations[i];
if (op.id === opId)
return op;
}
return null;
},
fillOperationOptions: function () {
var opSelect = document.getElementById('operation-type');
for (var i = 0; i < this.operations.length; i++) {
var op = this.operations[i];
var option = document.createElement('option');
option.value = op.id;
option.innerHTML = StringUtils.escapeHTML(op.displayName);
opSelect.appendChild(option);
}
},
init: function () {
this.fillOperationOptions();
}
};
function docReady(fn) {
// see if DOM is already available
if (document.readyState === "complete" || document.readyState === "interactive") {
// call on next available tick
setTimeout(fn, 1);
} else {
document.addEventListener("DOMContentLoaded", fn);
}
}
docReady(function () {
ShiftsTool.init();
})
</script>
</head>
<body>
<h2>Byte Shifts/Xor Report v2.1</h2>
<hr>
<div id="controls">
<input type="text" id="txt" placeholder="Text"><input type="submit" value="Generate Report" onclick="ShiftsTool.report_txt()"> <i>OR</i><br>
<input type="text" id="dec" placeholder="Decimal values"><input type="submit" value="Generate Report" onclick="ShiftsTool.report_dec()"> <i>OR</i><br>
<input type="text" id="hex" placeholder="Hexadecimal values"><input type="submit" value="Generate Report" onclick="ShiftsTool.report_hex()"> <br>
<select id="operation-type" onchange="ShiftsTool.report_unk()">
</select><br>
<input type="checkbox" id="flt" onclick="ShiftsTool.report_unk()"> Filter strings with unprintable chars.
</div>
<hr>
<div id="results">
</div>
</body>
</html>