2018-05-16 22:51:04 +02:00
|
|
|
/**
|
|
|
|
* @author n1474335 [n1474335@gmail.com]
|
|
|
|
* @copyright Crown Copyright 2016
|
|
|
|
* @license Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2019-07-09 13:23:59 +02:00
|
|
|
import Operation from "../Operation.mjs";
|
|
|
|
import OperationError from "../errors/OperationError.mjs";
|
|
|
|
import Utils from "../Utils.mjs";
|
|
|
|
import {fromHex} from "../lib/Hex.mjs";
|
2018-05-16 22:51:04 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Change IP format operation
|
|
|
|
*/
|
|
|
|
class ChangeIPFormat extends Operation {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ChangeIPFormat constructor
|
|
|
|
*/
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
|
|
|
|
this.name = "Change IP format";
|
2018-08-19 23:58:31 +02:00
|
|
|
this.module = "Default";
|
2018-05-16 22:51:04 +02:00
|
|
|
this.description = "Convert an IP address from one format to another, e.g. <code>172.20.23.54</code> to <code>ac141736</code>";
|
|
|
|
this.inputType = "string";
|
|
|
|
this.outputType = "string";
|
|
|
|
this.args = [
|
|
|
|
{
|
|
|
|
"name": "Input format",
|
|
|
|
"type": "option",
|
2019-08-24 02:14:44 +02:00
|
|
|
"value": ["Dotted Decimal", "Decimal", "Octal", "Hex"]
|
2018-05-16 22:51:04 +02:00
|
|
|
},
|
|
|
|
{
|
|
|
|
"name": "Output format",
|
|
|
|
"type": "option",
|
2019-08-24 02:14:44 +02:00
|
|
|
"value": ["Dotted Decimal", "Decimal", "Octal", "Hex"]
|
2018-05-16 22:51:04 +02:00
|
|
|
}
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {string} input
|
|
|
|
* @param {Object[]} args
|
|
|
|
* @returns {string}
|
|
|
|
*/
|
|
|
|
run(input, args) {
|
2018-05-29 01:48:30 +02:00
|
|
|
const [inFormat, outFormat] = args,
|
2018-05-16 22:51:04 +02:00
|
|
|
lines = input.split("\n");
|
|
|
|
let output = "",
|
|
|
|
j = 0;
|
|
|
|
|
|
|
|
for (let i = 0; i < lines.length; i++) {
|
|
|
|
if (lines[i] === "") continue;
|
|
|
|
let baIp = [];
|
|
|
|
let octets;
|
|
|
|
|
|
|
|
if (inFormat === outFormat) {
|
|
|
|
output += lines[i] + "\n";
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert to byte array IP from input format
|
|
|
|
switch (inFormat) {
|
|
|
|
case "Dotted Decimal":
|
|
|
|
octets = lines[i].split(".");
|
|
|
|
for (j = 0; j < octets.length; j++) {
|
|
|
|
baIp.push(parseInt(octets[j], 10));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case "Decimal":
|
2019-08-24 02:14:44 +02:00
|
|
|
baIp = this.fromNumber(lines[i].toString(), 10);
|
|
|
|
break;
|
|
|
|
case "Octal":
|
|
|
|
baIp = this.fromNumber(lines[i].toString(), 8);
|
2018-05-16 22:51:04 +02:00
|
|
|
break;
|
|
|
|
case "Hex":
|
|
|
|
baIp = fromHex(lines[i]);
|
|
|
|
break;
|
|
|
|
default:
|
2018-05-29 01:48:30 +02:00
|
|
|
throw new OperationError("Unsupported input IP format");
|
2018-05-16 22:51:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
let ddIp;
|
|
|
|
let decIp;
|
|
|
|
let hexIp;
|
|
|
|
|
|
|
|
// Convert byte array IP to output format
|
|
|
|
switch (outFormat) {
|
|
|
|
case "Dotted Decimal":
|
|
|
|
ddIp = "";
|
|
|
|
for (j = 0; j < baIp.length; j++) {
|
|
|
|
ddIp += baIp[j] + ".";
|
|
|
|
}
|
|
|
|
output += ddIp.slice(0, ddIp.length-1) + "\n";
|
|
|
|
break;
|
|
|
|
case "Decimal":
|
|
|
|
decIp = ((baIp[0] << 24) | (baIp[1] << 16) | (baIp[2] << 8) | baIp[3]) >>> 0;
|
|
|
|
output += decIp.toString() + "\n";
|
|
|
|
break;
|
2019-08-24 02:14:44 +02:00
|
|
|
case "Octal":
|
|
|
|
decIp = ((baIp[0] << 24) | (baIp[1] << 16) | (baIp[2] << 8) | baIp[3]) >>> 0;
|
|
|
|
output += "0" + decIp.toString(8) + "\n";
|
|
|
|
break;
|
2018-05-16 22:51:04 +02:00
|
|
|
case "Hex":
|
|
|
|
hexIp = "";
|
|
|
|
for (j = 0; j < baIp.length; j++) {
|
|
|
|
hexIp += Utils.hex(baIp[j]);
|
|
|
|
}
|
|
|
|
output += hexIp + "\n";
|
|
|
|
break;
|
|
|
|
default:
|
2018-05-29 01:48:30 +02:00
|
|
|
throw new OperationError("Unsupported output IP format");
|
2018-05-16 22:51:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return output.slice(0, output.length-1);
|
|
|
|
}
|
|
|
|
|
2019-08-24 02:14:44 +02:00
|
|
|
/**
|
|
|
|
* Constructs an array of IP address octets from a numerical value.
|
|
|
|
* @param {string} value The value of the IP address
|
|
|
|
* @param {number} radix The numeral system to be used
|
|
|
|
* @returns {number[]}
|
|
|
|
*/
|
|
|
|
fromNumber(value, radix) {
|
|
|
|
const decimal = parseInt(value, radix);
|
|
|
|
const baIp = [];
|
|
|
|
baIp.push(decimal >> 24 & 255);
|
|
|
|
baIp.push(decimal >> 16 & 255);
|
|
|
|
baIp.push(decimal >> 8 & 255);
|
|
|
|
baIp.push(decimal & 255);
|
|
|
|
return baIp;
|
|
|
|
}
|
|
|
|
|
2018-05-16 22:51:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export default ChangeIPFormat;
|