CyberChef/src/web/InputWaiter.js

219 lines
5.7 KiB
JavaScript
Raw Normal View History

import Utils from "../core/Utils.js";
2016-11-28 11:42:58 +01:00
/**
* Waiter to handle events related to the input.
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*
* @constructor
* @param {App} app - The main view object for CyberChef.
2016-11-28 11:42:58 +01:00
* @param {Manager} manager - The CyberChef event manager.
*/
2017-04-13 19:08:50 +02:00
const InputWaiter = function(app, manager) {
2016-11-28 11:42:58 +01:00
this.app = app;
this.manager = manager;
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
// Define keys that don't change the input so we don't have to autobake when they are pressed
this.badKeys = [
2016-11-28 11:42:58 +01:00
16, //Shift
17, //Ctrl
18, //Alt
19, //Pause
20, //Caps
27, //Esc
2016-12-14 17:39:17 +01:00
33, 34, 35, 36, //PgUp, PgDn, End, Home
37, 38, 39, 40, //Directional
2016-11-28 11:42:58 +01:00
44, //PrntScrn
2016-12-14 17:39:17 +01:00
91, 92, //Win
2016-11-28 11:42:58 +01:00
93, //Context
2016-12-14 17:39:17 +01:00
112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, //F1-12
2016-11-28 11:42:58 +01:00
144, //Num
145, //Scroll
];
};
/**
* Gets the user's input from the input textarea.
*
* @returns {string}
*/
InputWaiter.prototype.get = function() {
return document.getElementById("input-text").value;
};
/**
* Sets the input in the input textarea.
*
* @param {string} input
*
* @fires Manager#statechange
*/
InputWaiter.prototype.set = function(input) {
document.getElementById("input-text").value = input;
window.dispatchEvent(this.manager.statechange);
};
/**
* Displays information about the input.
*
* @param {number} length - The length of the current input string
* @param {number} lines - The number of the lines in the current input string
*/
InputWaiter.prototype.setInputInfo = function(length, lines) {
2017-04-13 19:08:50 +02:00
let width = length.toString().length;
2016-11-28 11:42:58 +01:00
width = width < 2 ? 2 : width;
2017-02-09 16:09:33 +01:00
2017-04-13 19:08:50 +02:00
const lengthStr = Utils.pad(length.toString(), width, " ").replace(/ /g, "&nbsp;");
const linesStr = Utils.pad(lines.toString(), width, " ").replace(/ /g, "&nbsp;");
2017-02-09 16:09:33 +01:00
document.getElementById("input-info").innerHTML = "length: " + lengthStr + "<br>lines: " + linesStr;
2016-11-28 11:42:58 +01:00
};
/**
* Handler for input scroll events.
* Scrolls the highlighter pane to match the input textarea position and updates history state.
*
* @param {event} e
*
* @fires Manager#statechange
*/
InputWaiter.prototype.inputChange = function(e) {
2016-11-28 11:42:58 +01:00
// Remove highlighting from input and output panes as the offsets might be different now
this.manager.highlighter.removeHighlights();
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
// Reset recipe progress as any previous processing will be redundant now
this.app.progress = 0;
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
// Update the input metadata info
2017-05-03 01:40:39 +02:00
const inputText = this.get();
const lines = inputText.count("\n") + 1;
2017-02-09 16:09:33 +01:00
this.setInputInfo(inputText.length, lines);
2017-02-09 16:09:33 +01:00
if (this.badKeys.indexOf(e.keyCode) < 0) {
2016-11-28 11:42:58 +01:00
// Fire the statechange event as the input has been modified
window.dispatchEvent(this.manager.statechange);
}
};
/**
* Handler for input dragover events.
* Gives the user a visual cue to show that items can be dropped here.
*
* @param {event} e
*/
InputWaiter.prototype.inputDragover = function(e) {
2016-11-28 11:42:58 +01:00
// This will be set if we're dragging an operation
if (e.dataTransfer.effectAllowed === "move")
return false;
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
e.stopPropagation();
e.preventDefault();
e.target.classList.add("dropping-file");
};
/**
* Handler for input dragleave events.
* Removes the visual cue.
*
* @param {event} e
*/
InputWaiter.prototype.inputDragleave = function(e) {
2016-11-28 11:42:58 +01:00
e.stopPropagation();
e.preventDefault();
e.target.classList.remove("dropping-file");
};
/**
* Handler for input drop events.
* Loads the dragged data into the input textarea.
*
* @param {event} e
*/
InputWaiter.prototype.inputDrop = function(e) {
2016-11-28 11:42:58 +01:00
// This will be set if we're dragging an operation
if (e.dataTransfer.effectAllowed === "move")
return false;
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
e.stopPropagation();
e.preventDefault();
2017-02-09 16:09:33 +01:00
2017-05-03 01:40:39 +02:00
const el = e.target;
const file = e.dataTransfer.files[0];
const text = e.dataTransfer.getData("Text");
const reader = new FileReader();
let inputCharcode = "";
let offset = 0;
const CHUNK_SIZE = 20480; // 20KB
2017-02-09 16:09:33 +01:00
2017-04-13 19:08:50 +02:00
const setInput = function() {
const recipeConfig = this.app.getRecipeConfig();
if (!recipeConfig[0] || recipeConfig[0].op !== "From Hex") {
2017-07-24 16:55:48 +02:00
recipeConfig.unshift({op: "From Hex", args: ["Space"]});
this.app.setRecipeConfig(recipeConfig);
2016-11-28 11:42:58 +01:00
}
2017-08-09 21:09:23 +02:00
this.set(inputCharcode);
2017-02-09 16:09:33 +01:00
el.classList.remove("loadingFile");
2016-11-28 11:42:58 +01:00
}.bind(this);
2017-02-09 16:09:33 +01:00
2017-04-13 19:08:50 +02:00
const seek = function() {
2016-11-28 11:42:58 +01:00
if (offset >= file.size) {
setInput();
2016-11-28 11:42:58 +01:00
return;
}
el.value = "Processing... " + Math.round(offset / file.size * 100) + "%";
2017-04-13 19:08:50 +02:00
const slice = file.slice(offset, offset + CHUNK_SIZE);
2016-11-28 11:42:58 +01:00
reader.readAsArrayBuffer(slice);
2016-12-14 17:39:17 +01:00
};
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
reader.onload = function(e) {
2017-04-13 19:08:50 +02:00
const data = new Uint8Array(reader.result);
inputCharcode += Utils.toHexFast(data);
2016-11-28 11:42:58 +01:00
offset += CHUNK_SIZE;
seek();
2016-12-14 17:39:17 +01:00
};
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
el.classList.remove("dropping-file");
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
if (file) {
el.classList.add("loadingFile");
2016-11-28 11:42:58 +01:00
seek();
} else if (text) {
this.set(text);
}
};
/**
* Handler for clear IO events.
* Resets the input, output and info areas.
*
* @fires Manager#statechange
*/
InputWaiter.prototype.clearIoClick = function() {
this.manager.highlighter.removeHighlights();
2016-11-28 11:42:58 +01:00
document.getElementById("input-text").value = "";
document.getElementById("output-text").value = "";
document.getElementById("input-info").innerHTML = "";
document.getElementById("output-info").innerHTML = "";
document.getElementById("input-selection-info").innerHTML = "";
document.getElementById("output-selection-info").innerHTML = "";
window.dispatchEvent(this.manager.statechange);
};
export default InputWaiter;