pull from upstream

This commit is contained in:
d98762625 2018-04-09 11:23:18 +01:00
commit 76f27dbcdb
20 changed files with 934 additions and 318 deletions

View file

@ -0,0 +1,103 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import Operation from "../Operation";
/**
* 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: true
},
{
name: "Rotate upper case chars",
type: "boolean",
value: true
},
{
name: "Amount",
type: "number",
value: 13
},
];
}
/**
* @param {byteArray} 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,88 @@
/**
* @author Matt C [matt@artemisbot.uk]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import Operation from "../Operation";
/**
* 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: 47
},
];
}
/**
* @param {byteArray} 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,81 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import Operation from "../Operation";
import {rot, rotl, rotlCarry} 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: 1
},
{
name: "Carry through",
type: "boolean",
value: false
}
];
}
/**
* @param {byteArray} 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,81 @@
/**
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*/
import Operation from "../Operation";
import {rot, rotr, rotrCarry} 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: 1
},
{
name: "Carry through",
type: "boolean",
value: false
}
];
}
/**
* @param {byteArray} 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

@ -25,7 +25,8 @@ class Unzip extends Operation {
this.module = "Compression";
this.description = "Decompresses data using the PKZIP algorithm and displays it per file, with support for passwords.";
this.inputType = "byteArray";
this.outputType = "html";
this.outputType = "List<File>";
this.presentType = "html";
this.args = [
{
name: "Password",
@ -43,7 +44,7 @@ class Unzip extends Operation {
/**
* @param {byteArray} input
* @param {Object[]} args
* @returns {html}
* @returns {File[]}
*/
run(input, args) {
const options = {
@ -51,28 +52,22 @@ class Unzip extends Operation {
verify: args[1]
},
unzip = new Zlib.Unzip(input, options),
filenames = unzip.getFilenames(),
files = [];
filenames = unzip.getFilenames();
filenames.forEach(function(fileName) {
return filenames.map(fileName => {
const bytes = unzip.decompress(fileName);
const contents = Utils.byteArrayToUtf8(bytes);
const file = {
fileName: fileName,
size: contents.length,
};
const isDir = contents.length === 0 && fileName.endsWith("/");
if (!isDir) {
file.bytes = bytes;
file.contents = contents;
}
files.push(file);
return new File([bytes], fileName);
});
}
return Utils.displayFilesAsHTML(files);
/**
* Displays the files in HTML for web apps.
*
* @param {File[]} files
* @returns {html}
*/
async present(files) {
return await Utils.displayFilesAsHTML(files);
}
}

View file

@ -12,8 +12,12 @@ import FromHex from "./FromHex";
import Gunzip from "./Gunzip";
import Gzip from "./Gzip";
import PowerSet from "./PowerSet";
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 SetDifference from "./SetDifference";
import SetIntersection from "./SetIntersection";
import SetUnion from "./SetUnion";
@ -35,8 +39,12 @@ export {
Gunzip,
Gzip,
PowerSet,
ROT13,
ROT47,
RawDeflate,
RawInflate,
RotateLeft,
RotateRight,
SetDifference,
SetIntersection,
SetUnion,

View file

@ -1,244 +0,0 @@
/**
* Bit rotation operations.
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2016
* @license Apache-2.0
*
* @namespace
*
* @todo Support for UTF16
*/
const Rotate = {
/**
* @constant
* @default
*/
ROTATE_AMOUNT: 1,
/**
* @constant
* @default
*/
ROTATE_CARRY: false,
/**
* Runs rotation operations across the input data.
*
* @private
* @param {byteArray} data
* @param {number} amount
* @param {function} algo - The rotation operation to carry out
* @returns {byteArray}
*/
_rot: function(data, amount, algo) {
const result = [];
for (let i = 0; i < data.length; i++) {
let b = data[i];
for (let j = 0; j < amount; j++) {
b = algo(b);
}
result.push(b);
}
return result;
},
/**
* Rotate right operation.
*
* @param {byteArray} input
* @param {Object[]} args
* @returns {byteArray}
*/
runRotr: function(input, args) {
if (args[1]) {
return Rotate._rotrCarry(input, args[0]);
} else {
return Rotate._rot(input, args[0], Rotate._rotr);
}
},
/**
* Rotate left operation.
*
* @param {byteArray} input
* @param {Object[]} args
* @returns {byteArray}
*/
runRotl: function(input, args) {
if (args[1]) {
return Rotate._rotlCarry(input, args[0]);
} else {
return Rotate._rot(input, args[0], Rotate._rotl);
}
},
/**
* @constant
* @default
*/
ROT13_AMOUNT: 13,
/**
* @constant
* @default
*/
ROT13_LOWERCASE: true,
/**
* @constant
* @default
*/
ROT13_UPPERCASE: true,
/**
* ROT13 operation.
*
* @param {byteArray} input
* @param {Object[]} args
* @returns {byteArray}
*/
runRot13: function(input, args) {
let amount = args[2],
output = input,
chr,
rot13Lowercase = args[0],
rot13Upperacse = args[1];
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;
},
/**
* @constant
* @default
*/
ROT47_AMOUNT: 47,
/**
* ROT47 operation.
*
* @author Matt C [matt@artemisbot.uk]
* @param {byteArray} input
* @param {Object[]} args
* @returns {byteArray}
*/
runRot47: function(input, args) {
let amount = args[0],
output = input,
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;
},
/**
* Rotate right bitwise op.
*
* @private
* @param {byte} b
* @returns {byte}
*/
_rotr: function(b) {
const bit = (b & 1) << 7;
return (b >> 1) | bit;
},
/**
* Rotate left bitwise op.
*
* @private
* @param {byte} b
* @returns {byte}
*/
_rotl: function(b) {
const bit = (b >> 7) & 1;
return ((b << 1) | bit) & 0xFF;
},
/**
* Rotates a byte array to the right by a specific amount as a whole, so that bits are wrapped
* from the end of the array to the beginning.
*
* @private
* @param {byteArray} data
* @param {number} amount
* @returns {byteArray}
*/
_rotrCarry: function(data, amount) {
let carryBits = 0,
newByte,
result = [];
amount = amount % 8;
for (let i = 0; i < data.length; i++) {
const oldByte = data[i] >>> 0;
newByte = (oldByte >> amount) | carryBits;
carryBits = (oldByte & (Math.pow(2, amount)-1)) << (8-amount);
result.push(newByte);
}
result[0] |= carryBits;
return result;
},
/**
* Rotates a byte array to the left by a specific amount as a whole, so that bits are wrapped
* from the beginning of the array to the end.
*
* @private
* @param {byteArray} data
* @param {number} amount
* @returns {byteArray}
*/
_rotlCarry: function(data, amount) {
let carryBits = 0,
newByte,
result = [];
amount = amount % 8;
for (let i = data.length-1; i >= 0; i--) {
const oldByte = data[i];
newByte = ((oldByte << amount) | carryBits) & 0xFF;
carryBits = (oldByte >> (8-amount)) & (Math.pow(2, amount)-1);
result[i] = (newByte);
}
result[data.length-1] = result[data.length-1] | carryBits;
return result;
},
};
export default Rotate;