CyberChef/src/core/operations/Tidy.js

246 lines
5.3 KiB
JavaScript
Raw Normal View History

import Utils from "../Utils.js";
2016-11-28 11:42:58 +01:00
/**
* Tidy operations.
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*
* @namespace
*/
const Tidy = {
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
2017-07-24 16:55:48 +02:00
REMOVE_SPACES: true,
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
2017-07-24 16:55:48 +02:00
REMOVE_CARIAGE_RETURNS: true,
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
2017-07-24 16:55:48 +02:00
REMOVE_LINE_FEEDS: true,
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
2017-07-24 16:55:48 +02:00
REMOVE_TABS: true,
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
2017-07-24 16:55:48 +02:00
REMOVE_FORM_FEEDS: true,
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
2017-07-24 16:55:48 +02:00
REMOVE_FULL_STOPS: false,
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* Remove whitespace operation.
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
runRemoveWhitespace: function (input, args) {
2017-04-13 19:08:50 +02:00
let removeSpaces = args[0],
removeCariageReturns = args[1],
removeLineFeeds = args[2],
removeTabs = args[3],
removeFormFeeds = args[4],
removeFullStops = args[5],
2016-11-28 11:42:58 +01:00
data = input;
2017-02-09 16:09:33 +01:00
if (removeSpaces) data = data.replace(/ /g, "");
if (removeCariageReturns) data = data.replace(/\r/g, "");
if (removeLineFeeds) data = data.replace(/\n/g, "");
if (removeTabs) data = data.replace(/\t/g, "");
if (removeFormFeeds) data = data.replace(/\f/g, "");
if (removeFullStops) data = data.replace(/\./g, "");
2016-11-28 11:42:58 +01:00
return data;
},
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* Remove null bytes operation.
*
* @param {byteArray} input
2016-11-28 11:42:58 +01:00
* @param {Object[]} args
* @returns {byteArray}
2016-11-28 11:42:58 +01:00
*/
runRemoveNulls: function (input, args) {
2017-04-13 19:08:50 +02:00
const output = [];
for (let i = 0; i < input.length; i++) {
2016-11-28 11:42:58 +01:00
if (input[i] !== 0) output.push(input[i]);
}
return output;
},
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
2017-07-24 16:55:48 +02:00
APPLY_TO_EACH_LINE: false,
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
2017-07-24 16:55:48 +02:00
DROP_START: 0,
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
2017-07-24 16:55:48 +02:00
DROP_LENGTH: 5,
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* Drop bytes operation.
*
* @param {byteArray} input
2016-11-28 11:42:58 +01:00
* @param {Object[]} args
* @returns {byteArray}
2016-11-28 11:42:58 +01:00
*/
runDropBytes: function(input, args) {
2017-04-13 19:08:50 +02:00
let start = args[0],
2016-11-28 11:42:58 +01:00
length = args[1],
applyToEachLine = args[2];
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
if (start < 0 || length < 0)
throw "Error: Invalid value";
2017-02-09 16:09:33 +01:00
if (!applyToEachLine)
2016-11-28 11:42:58 +01:00
return input.slice(0, start).concat(input.slice(start+length, input.length));
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
// Split input into lines
2017-04-13 19:08:50 +02:00
let lines = [],
2017-04-13 19:31:26 +02:00
line = [],
i;
2017-02-09 16:09:33 +01:00
2017-04-13 19:31:26 +02:00
for (i = 0; i < input.length; i++) {
2016-12-14 17:39:17 +01:00
if (input[i] === 0x0a) {
2016-11-28 11:42:58 +01:00
lines.push(line);
line = [];
} else {
line.push(input[i]);
}
}
lines.push(line);
2017-02-09 16:09:33 +01:00
2017-04-13 19:08:50 +02:00
let output = [];
2016-11-28 11:42:58 +01:00
for (i = 0; i < lines.length; i++) {
output = output.concat(lines[i].slice(0, start).concat(lines[i].slice(start+length, lines[i].length)));
output.push(0x0a);
}
return output.slice(0, output.length-1);
},
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
TAKE_START: 0,
/**
* @constant
* @default
*/
TAKE_LENGTH: 5,
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* Take bytes operation.
*
* @param {byteArray} input
2016-11-28 11:42:58 +01:00
* @param {Object[]} args
* @returns {byteArray}
2016-11-28 11:42:58 +01:00
*/
runTakeBytes: function(input, args) {
2017-04-13 19:08:50 +02:00
let start = args[0],
2016-11-28 11:42:58 +01:00
length = args[1],
applyToEachLine = args[2];
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
if (start < 0 || length < 0)
throw "Error: Invalid value";
2017-02-09 16:09:33 +01:00
if (!applyToEachLine)
2016-11-28 11:42:58 +01:00
return input.slice(start, start+length);
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
// Split input into lines
2017-04-13 19:08:50 +02:00
let lines = [],
2016-11-28 11:42:58 +01:00
line = [];
2017-04-13 19:31:26 +02:00
let i;
2017-02-09 16:09:33 +01:00
2017-04-13 19:31:26 +02:00
for (i = 0; i < input.length; i++) {
2016-12-14 17:39:17 +01:00
if (input[i] === 0x0a) {
2016-11-28 11:42:58 +01:00
lines.push(line);
line = [];
} else {
line.push(input[i]);
}
}
lines.push(line);
2017-02-09 16:09:33 +01:00
2017-04-13 19:08:50 +02:00
let output = [];
2016-11-28 11:42:58 +01:00
for (i = 0; i < lines.length; i++) {
output = output.concat(lines[i].slice(start, start+length));
output.push(0x0a);
}
return output.slice(0, output.length-1);
},
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
2017-07-24 16:55:48 +02:00
PAD_POSITION: ["Start", "End"],
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
2017-07-24 16:55:48 +02:00
PAD_LENGTH: 5,
2016-11-28 11:42:58 +01:00
/**
* @constant
* @default
*/
2017-07-24 16:55:48 +02:00
PAD_CHAR: " ",
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
/**
* Pad lines operation.
*
* @param {string} input
* @param {Object[]} args
* @returns {string}
*/
runPad: function(input, args) {
2017-04-13 19:08:50 +02:00
let position = args[0],
2016-11-28 11:42:58 +01:00
len = args[1],
chr = args[2],
lines = input.split("\n"),
output = "",
i = 0;
2017-02-09 16:09:33 +01:00
2016-12-14 17:39:17 +01:00
if (position === "Start") {
2016-11-28 11:42:58 +01:00
for (i = 0; i < lines.length; i++) {
output += Utils.padLeft(lines[i], lines[i].length+len, chr) + "\n";
2016-11-28 11:42:58 +01:00
}
2016-12-14 17:39:17 +01:00
} else if (position === "End") {
2016-11-28 11:42:58 +01:00
for (i = 0; i < lines.length; i++) {
output += Utils.padRight(lines[i], lines[i].length+len, chr) + "\n";
2016-11-28 11:42:58 +01:00
}
}
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
return output.slice(0, output.length-1);
},
2017-02-09 16:09:33 +01:00
2016-11-28 11:42:58 +01:00
};
export default Tidy;