Rotate module converted to ESM

4 Ops:
- ROT-13
- ROT-47
- Rotate left
- Rotate right
+ module containing common functions
This commit is contained in:
Matt C 2018-04-03 22:50:26 +01:00
parent 083d2d1cc4
commit 4988ead918
9 changed files with 578 additions and 11 deletions

View file

@ -0,0 +1,110 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import Operation from "../Operation";
/**
* Default arguments for ROT13 operation
*/
const ROT13_AMOUNT = 13,
ROT13_LOWERCASE = true,
ROT13_UPPERCASE = true;
/**
* ROT13 operation.
*/
class ROT13 extends Operation {
/**
* ROT13 constructor
*/
constructor() {
super();
this.name = "ROT13";
this.module = "Default";
this.description = "A simple caesar substitution cipher which rotates alphabet characters by the specified amount (default 13).";
this.inputType = "byteArray";
this.outputType = "byteArray";
this.args = [
{
name: "Rotate lower case chars",
type: "boolean",
value: ROT13_LOWERCASE
},
{
name: "Rotate upper case chars",
type: "boolean",
value: ROT13_UPPERCASE
},
{
name: "Amount",
type: "number",
value: ROT13_AMOUNT
},
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {byteArray}
*/
run(input, args) {
const output = input,
rot13Lowercase = args[0],
rot13Upperacse = args[1];
let amount = args[2],
chr;
if (amount) {
if (amount < 0) {
amount = 26 - (Math.abs(amount) % 26);
}
for (let i = 0; i < input.length; i++) {
chr = input[i];
if (rot13Upperacse && chr >= 65 && chr <= 90) { // Upper case
chr = (chr - 65 + amount) % 26;
output[i] = chr + 65;
} else if (rot13Lowercase && chr >= 97 && chr <= 122) { // Lower case
chr = (chr - 97 + amount) % 26;
output[i] = chr + 97;
}
}
}
return output;
}
/**
* Highlight ROT13
*
* @param {Object[]} pos
* @param {number} pos[].start
* @param {number} pos[].end
* @param {Object[]} args
* @returns {Object[]} pos
*/
highlight(pos, args) {
return pos;
}
/**
* Highlight ROT13 in reverse
*
* @param {Object[]} pos
* @param {number} pos[].start
* @param {number} pos[].end
* @param {Object[]} args
* @returns {Object[]} pos
*/
highlightReverse(pos, args) {
return pos;
}
}
export default ROT13;

View file

@ -0,0 +1,93 @@
/**
* @author Matt C [matt@artemisbot.uk]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import Operation from "../Operation";
/**
* Default argument for ROT47 operation
*/
const ROT47_AMOUNT = 47;
/**
* ROT47 operation.
*/
class ROT47 extends Operation {
/**
* ROT47 constructor
*/
constructor() {
super();
this.name = "ROT47";
this.module = "Default";
this.description = "A slightly more complex variation of a caesar cipher, which includes ASCII characters from 33 '!' to 126 '~'. Default rotation: 47.";
this.inputType = "byteArray";
this.outputType = "byteArray";
this.args = [
{
name: "Amount",
type: "number",
value: ROT47_AMOUNT
},
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {byteArray}
*/
run(input, args) {
const output = input;
let amount = args[0],
chr;
if (amount) {
if (amount < 0) {
amount = 94 - (Math.abs(amount) % 94);
}
for (let i = 0; i < input.length; i++) {
chr = input[i];
if (chr >= 33 && chr <= 126) {
chr = (chr - 33 + amount) % 94;
output[i] = chr + 33;
}
}
}
return output;
}
/**
* Highlight ROT47
*
* @param {Object[]} pos
* @param {number} pos[].start
* @param {number} pos[].end
* @param {Object[]} args
* @returns {Object[]} pos
*/
highlight(pos, args) {
return pos;
}
/**
* Highlight ROT47 in reverse
*
* @param {Object[]} pos
* @param {number} pos[].start
* @param {number} pos[].end
* @param {Object[]} args
* @returns {Object[]} pos
*/
highlightReverse(pos, args) {
return pos;
}
}
export default ROT47;

View file

@ -0,0 +1,80 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import Operation from "../Operation";
import { rot, rotl, rotlCarry, ROTATE_AMOUNT, ROTATE_CARRY } from "../lib/Rotate";
/**
* Rotate left operation.
*/
class RotateLeft extends Operation {
/**
* RotateLeft constructor
*/
constructor() {
super();
this.name = "Rotate left";
this.module = "Default";
this.description = "Rotates each byte to the left by the number of bits specified, optionally carrying the excess bits over to the next byte. Currently only supports 8-bit values.";
this.inputType = "byteArray";
this.outputType = "byteArray";
this.args = [
{
name: "Amount",
type: "number",
value: ROTATE_AMOUNT
},
{
name: "Carry through",
type: "boolean",
value: ROTATE_CARRY
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {byteArray}
*/
run(input, args) {
if (args[1]) {
return rotlCarry(input, args[0]);
} else {
return rot(input, args[0], rotl);
}
}
/**
* Highlight rotate left
*
* @param {Object[]} pos
* @param {number} pos[].start
* @param {number} pos[].end
* @param {Object[]} args
* @returns {Object[]} pos
*/
highlight(pos, args) {
return pos;
}
/**
* Highlight rotate left in reverse
*
* @param {Object[]} pos
* @param {number} pos[].start
* @param {number} pos[].end
* @param {Object[]} args
* @returns {Object[]} pos
*/
highlightReverse(pos, args) {
return pos;
}
}
export default RotateLeft;

View file

@ -0,0 +1,80 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import Operation from "../Operation";
import { rot, rotr, rotrCarry, ROTATE_AMOUNT, ROTATE_CARRY } from "../lib/Rotate";
/**
* Rotate right operation.
*/
class RotateRight extends Operation {
/**
* RotateRight constructor
*/
constructor() {
super();
this.name = "Rotate right";
this.module = "Default";
this.description = "Rotates each byte to the right by the number of bits specified, optionally carrying the excess bits over to the next byte. Currently only supports 8-bit values.";
this.inputType = "byteArray";
this.outputType = "byteArray";
this.args = [
{
name: "Amount",
type: "number",
value: ROTATE_AMOUNT
},
{
name: "Carry through",
type: "boolean",
value: ROTATE_CARRY
}
];
}
/**
* @param {string} input
* @param {Object[]} args
* @returns {byteArray}
*/
run(input, args) {
if (args[1]) {
return rotrCarry(input, args[0]);
} else {
return rot(input, args[0], rotr);
}
}
/**
* Highlight rotate right
*
* @param {Object[]} pos
* @param {number} pos[].start
* @param {number} pos[].end
* @param {Object[]} args
* @returns {Object[]} pos
*/
highlight(pos, args) {
return pos;
}
/**
* Highlight rotate right in reverse
*
* @param {Object[]} pos
* @param {number} pos[].start
* @param {number} pos[].end
* @param {Object[]} args
* @returns {Object[]} pos
*/
highlightReverse(pos, args) {
return pos;
}
}
export default RotateRight;

View file

@ -10,8 +10,12 @@ import FromBase64 from "./FromBase64";
import FromHex from "./FromHex";
import Gunzip from "./Gunzip";
import Gzip from "./Gzip";
import ROT13 from "./ROT13";
import ROT47 from "./ROT47";
import RawDeflate from "./RawDeflate";
import RawInflate from "./RawInflate";
import RotateLeft from "./RotateLeft";
import RotateRight from "./RotateRight";
import ShowBase64Offsets from "./ShowBase64Offsets";
import ToBase32 from "./ToBase32";
import ToBase64 from "./ToBase64";
@ -27,8 +31,12 @@ export {
FromHex,
Gunzip,
Gzip,
ROT13,
ROT47,
RawDeflate,
RawInflate,
RotateLeft,
RotateRight,
ShowBase64Offsets,
ToBase32,
ToBase64,