1
0
mirror of synced 2024-11-15 18:53:22 +01:00

Added 'Binary' key option to all bitwise operations. Closes #398

This commit is contained in:
n1474335 2018-11-09 15:25:16 +00:00
parent 3c4893d7c7
commit 42e881326f
10 changed files with 97 additions and 34 deletions

View File

@ -9,6 +9,7 @@ import moment from "moment-timezone";
import {fromBase64} from "./lib/Base64"; import {fromBase64} from "./lib/Base64";
import {fromHex} from "./lib/Hex"; import {fromHex} from "./lib/Hex";
import {fromDecimal} from "./lib/Decimal"; import {fromDecimal} from "./lib/Decimal";
import {fromBinary} from "./lib/Binary";
/** /**
@ -298,7 +299,7 @@ class Utils {
* Accepts hex, Base64, UTF8 and Latin1 strings. * Accepts hex, Base64, UTF8 and Latin1 strings.
* *
* @param {string} str * @param {string} str
* @param {string} type - One of "Hex", "Decimal", "Base64", "UTF8" or "Latin1" * @param {string} type - One of "Binary", "Hex", "Decimal", "Base64", "UTF8" or "Latin1"
* @returns {byteArray} * @returns {byteArray}
* *
* @example * @example
@ -313,6 +314,8 @@ class Utils {
*/ */
static convertToByteArray(str, type) { static convertToByteArray(str, type) {
switch (type.toLowerCase()) { switch (type.toLowerCase()) {
case "binary":
return fromBinary(str);
case "hex": case "hex":
return fromHex(str); return fromHex(str);
case "decimal": case "decimal":
@ -333,7 +336,7 @@ class Utils {
* Accepts hex, Base64, UTF8 and Latin1 strings. * Accepts hex, Base64, UTF8 and Latin1 strings.
* *
* @param {string} str * @param {string} str
* @param {string} type - One of "Hex", "Decimal", "Base64", "UTF8" or "Latin1" * @param {string} type - One of "Binary", "Hex", "Decimal", "Base64", "UTF8" or "Latin1"
* @returns {string} * @returns {string}
* *
* @example * @example
@ -348,6 +351,8 @@ class Utils {
*/ */
static convertToByteString(str, type) { static convertToByteString(str, type) {
switch (type.toLowerCase()) { switch (type.toLowerCase()) {
case "binary":
return Utils.byteArrayToChars(fromBinary(str));
case "hex": case "hex":
return Utils.byteArrayToChars(fromHex(str)); return Utils.byteArrayToChars(fromHex(str));
case "decimal": case "decimal":

70
src/core/lib/Binary.mjs Normal file
View File

@ -0,0 +1,70 @@
/**
* Binary functions.
*
* @author n1474335 [n1474335@gmail.com]
* @copyright Crown Copyright 2018
* @license Apache-2.0
*/
import Utils from "../Utils";
/**
* Convert a byte array into a binary string.
*
* @param {Uint8Array|byteArray} data
* @param {string} [delim="Space"]
* @param {number} [padding=8]
* @returns {string}
*
* @example
* // returns "00010000 00100000 00110000"
* toHex([10,20,30]);
*
* // returns "00010000 00100000 00110000"
* toHex([10,20,30], ":");
*/
export function toBinary(data, delim="Space", padding=8) {
if (!data) return "";
delim = Utils.charRep(delim);
let output = "";
for (let i = 0; i < data.length; i++) {
output += data[i].toString(2).padStart(padding, "0") + delim;
}
if (delim.length) {
return output.slice(0, -delim.length);
} else {
return output;
}
}
/**
* Convert a binary string into a byte array.
*
* @param {string} data
* @param {string} [delim]
* @param {number} [byteLen=8]
* @returns {byteArray}
*
* @example
* // returns [10,20,30]
* fromBinary("00010000 00100000 00110000");
*
* // returns [10,20,30]
* fromBinary("00010000:00100000:00110000", "Colon");
*/
export function fromBinary(data, delim="Space", byteLen=8) {
const delimRegex = Utils.regexRep(delim);
data = data.replace(delimRegex, "");
const output = [];
for (let i = 0; i < data.length; i += byteLen) {
output.push(parseInt(data.substr(i, byteLen), 2));
}
return output;
}

View File

@ -116,3 +116,9 @@ export function sub(operand, key) {
const result = operand - key; const result = operand - key;
return (result < 0) ? 256 + result : result; return (result < 0) ? 256 + result : result;
} }
/**
* Delimiter options for bitwise operations
*/
export const BITWISE_OP_DELIMS = ["Hex", "Decimal", "Binary", "Base64", "UTF8", "Latin1"];

View File

@ -6,7 +6,7 @@
import Operation from "../Operation"; import Operation from "../Operation";
import Utils from "../Utils"; import Utils from "../Utils";
import { bitOp, add } from "../lib/BitwiseOp"; import { bitOp, add, BITWISE_OP_DELIMS } from "../lib/BitwiseOp";
/** /**
* ADD operation * ADD operation
@ -30,7 +30,7 @@ class ADD extends Operation {
"name": "Key", "name": "Key",
"type": "toggleString", "type": "toggleString",
"value": "", "value": "",
"toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"] "toggleValues": BITWISE_OP_DELIMS
} }
]; ];
} }

View File

@ -6,7 +6,7 @@
import Operation from "../Operation"; import Operation from "../Operation";
import Utils from "../Utils"; import Utils from "../Utils";
import { bitOp, and } from "../lib/BitwiseOp"; import { bitOp, and, BITWISE_OP_DELIMS } from "../lib/BitwiseOp";
/** /**
* AND operation * AND operation
@ -30,7 +30,7 @@ class AND extends Operation {
"name": "Key", "name": "Key",
"type": "toggleString", "type": "toggleString",
"value": "", "value": "",
"toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"] "toggleValues": BITWISE_OP_DELIMS
} }
]; ];
} }

View File

@ -7,6 +7,7 @@
import Operation from "../Operation"; import Operation from "../Operation";
import Utils from "../Utils"; import Utils from "../Utils";
import {BIN_DELIM_OPTIONS} from "../lib/Delim"; import {BIN_DELIM_OPTIONS} from "../lib/Delim";
import {fromBinary} from "../lib/Binary";
/** /**
* From Binary operation * From Binary operation
@ -77,15 +78,7 @@ class FromBinary extends Operation {
* @returns {byteArray} * @returns {byteArray}
*/ */
run(input, args) { run(input, args) {
const delimRegex = Utils.regexRep(args[0] || "Space"); return fromBinary(input, args[0]);
input = input.replace(delimRegex, "");
const output = [];
const byteLen = 8;
for (let i = 0; i < input.length; i += byteLen) {
output.push(parseInt(input.substr(i, byteLen), 2));
}
return output;
} }
/** /**

View File

@ -6,7 +6,7 @@
import Operation from "../Operation"; import Operation from "../Operation";
import Utils from "../Utils"; import Utils from "../Utils";
import { bitOp, or } from "../lib/BitwiseOp"; import { bitOp, or, BITWISE_OP_DELIMS } from "../lib/BitwiseOp";
/** /**
* OR operation * OR operation
@ -30,7 +30,7 @@ class OR extends Operation {
"name": "Key", "name": "Key",
"type": "toggleString", "type": "toggleString",
"value": "", "value": "",
"toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"] "toggleValues": BITWISE_OP_DELIMS
} }
]; ];
} }

View File

@ -6,7 +6,7 @@
import Operation from "../Operation"; import Operation from "../Operation";
import Utils from "../Utils"; import Utils from "../Utils";
import { bitOp, sub } from "../lib/BitwiseOp"; import { bitOp, sub, BITWISE_OP_DELIMS } from "../lib/BitwiseOp";
/** /**
* SUB operation * SUB operation
@ -30,7 +30,7 @@ class SUB extends Operation {
"name": "Key", "name": "Key",
"type": "toggleString", "type": "toggleString",
"value": "", "value": "",
"toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"] "toggleValues": BITWISE_OP_DELIMS
} }
]; ];
} }

View File

@ -7,6 +7,7 @@
import Operation from "../Operation"; import Operation from "../Operation";
import Utils from "../Utils"; import Utils from "../Utils";
import {BIN_DELIM_OPTIONS} from "../lib/Delim"; import {BIN_DELIM_OPTIONS} from "../lib/Delim";
import {toBinary} from "../lib/Binary";
/** /**
* To Binary operation * To Binary operation
@ -40,19 +41,7 @@ class ToBinary extends Operation {
* @returns {string} * @returns {string}
*/ */
run(input, args) { run(input, args) {
const delim = Utils.charRep(args[0] || "Space"), return toBinary(input, args[0]);
padding = 8;
let output = "";
for (let i = 0; i < input.length; i++) {
output += input[i].toString(2).padStart(padding, "0") + delim;
}
if (delim.length) {
return output.slice(0, -delim.length);
} else {
return output;
}
} }
/** /**

View File

@ -6,7 +6,7 @@
import Operation from "../Operation"; import Operation from "../Operation";
import Utils from "../Utils"; import Utils from "../Utils";
import { bitOp, xor } from "../lib/BitwiseOp"; import { bitOp, xor, BITWISE_OP_DELIMS } from "../lib/BitwiseOp";
/** /**
* XOR operation * XOR operation
@ -30,7 +30,7 @@ class XOR extends Operation {
"name": "Key", "name": "Key",
"type": "toggleString", "type": "toggleString",
"value": "", "value": "",
"toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"] "toggleValues": BITWISE_OP_DELIMS
}, },
{ {
"name": "Scheme", "name": "Scheme",