diff --git a/src/core/Utils.mjs b/src/core/Utils.mjs index 26debee3..55acd3dd 100755 --- a/src/core/Utils.mjs +++ b/src/core/Utils.mjs @@ -9,6 +9,7 @@ import moment from "moment-timezone"; import {fromBase64} from "./lib/Base64"; import {fromHex} from "./lib/Hex"; import {fromDecimal} from "./lib/Decimal"; +import {fromBinary} from "./lib/Binary"; /** @@ -298,7 +299,7 @@ class Utils { * Accepts hex, Base64, UTF8 and Latin1 strings. * * @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} * * @example @@ -313,6 +314,8 @@ class Utils { */ static convertToByteArray(str, type) { switch (type.toLowerCase()) { + case "binary": + return fromBinary(str); case "hex": return fromHex(str); case "decimal": @@ -333,7 +336,7 @@ class Utils { * Accepts hex, Base64, UTF8 and Latin1 strings. * * @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} * * @example @@ -348,6 +351,8 @@ class Utils { */ static convertToByteString(str, type) { switch (type.toLowerCase()) { + case "binary": + return Utils.byteArrayToChars(fromBinary(str)); case "hex": return Utils.byteArrayToChars(fromHex(str)); case "decimal": diff --git a/src/core/lib/Binary.mjs b/src/core/lib/Binary.mjs new file mode 100644 index 00000000..1b1d0fac --- /dev/null +++ b/src/core/lib/Binary.mjs @@ -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; +} + diff --git a/src/core/lib/BitwiseOp.mjs b/src/core/lib/BitwiseOp.mjs index dbac3c5a..84a7834b 100644 --- a/src/core/lib/BitwiseOp.mjs +++ b/src/core/lib/BitwiseOp.mjs @@ -116,3 +116,9 @@ export function sub(operand, key) { const result = operand - key; return (result < 0) ? 256 + result : result; } + + +/** + * Delimiter options for bitwise operations + */ +export const BITWISE_OP_DELIMS = ["Hex", "Decimal", "Binary", "Base64", "UTF8", "Latin1"]; diff --git a/src/core/operations/ADD.mjs b/src/core/operations/ADD.mjs index 7550e8e6..dc593940 100644 --- a/src/core/operations/ADD.mjs +++ b/src/core/operations/ADD.mjs @@ -6,7 +6,7 @@ import Operation from "../Operation"; import Utils from "../Utils"; -import { bitOp, add } from "../lib/BitwiseOp"; +import { bitOp, add, BITWISE_OP_DELIMS } from "../lib/BitwiseOp"; /** * ADD operation @@ -30,7 +30,7 @@ class ADD extends Operation { "name": "Key", "type": "toggleString", "value": "", - "toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"] + "toggleValues": BITWISE_OP_DELIMS } ]; } diff --git a/src/core/operations/AND.mjs b/src/core/operations/AND.mjs index 68cddcfa..1fa84074 100644 --- a/src/core/operations/AND.mjs +++ b/src/core/operations/AND.mjs @@ -6,7 +6,7 @@ import Operation from "../Operation"; import Utils from "../Utils"; -import { bitOp, and } from "../lib/BitwiseOp"; +import { bitOp, and, BITWISE_OP_DELIMS } from "../lib/BitwiseOp"; /** * AND operation @@ -30,7 +30,7 @@ class AND extends Operation { "name": "Key", "type": "toggleString", "value": "", - "toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"] + "toggleValues": BITWISE_OP_DELIMS } ]; } diff --git a/src/core/operations/FromBinary.mjs b/src/core/operations/FromBinary.mjs index 7ae0cc19..a7ced7e3 100644 --- a/src/core/operations/FromBinary.mjs +++ b/src/core/operations/FromBinary.mjs @@ -7,6 +7,7 @@ import Operation from "../Operation"; import Utils from "../Utils"; import {BIN_DELIM_OPTIONS} from "../lib/Delim"; +import {fromBinary} from "../lib/Binary"; /** * From Binary operation @@ -77,15 +78,7 @@ class FromBinary extends Operation { * @returns {byteArray} */ run(input, args) { - const delimRegex = Utils.regexRep(args[0] || "Space"); - 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; + return fromBinary(input, args[0]); } /** diff --git a/src/core/operations/OR.mjs b/src/core/operations/OR.mjs index be64cd8e..30948379 100644 --- a/src/core/operations/OR.mjs +++ b/src/core/operations/OR.mjs @@ -6,7 +6,7 @@ import Operation from "../Operation"; import Utils from "../Utils"; -import { bitOp, or } from "../lib/BitwiseOp"; +import { bitOp, or, BITWISE_OP_DELIMS } from "../lib/BitwiseOp"; /** * OR operation @@ -30,7 +30,7 @@ class OR extends Operation { "name": "Key", "type": "toggleString", "value": "", - "toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"] + "toggleValues": BITWISE_OP_DELIMS } ]; } diff --git a/src/core/operations/SUB.mjs b/src/core/operations/SUB.mjs index c3134aad..f2374488 100644 --- a/src/core/operations/SUB.mjs +++ b/src/core/operations/SUB.mjs @@ -6,7 +6,7 @@ import Operation from "../Operation"; import Utils from "../Utils"; -import { bitOp, sub } from "../lib/BitwiseOp"; +import { bitOp, sub, BITWISE_OP_DELIMS } from "../lib/BitwiseOp"; /** * SUB operation @@ -30,7 +30,7 @@ class SUB extends Operation { "name": "Key", "type": "toggleString", "value": "", - "toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"] + "toggleValues": BITWISE_OP_DELIMS } ]; } diff --git a/src/core/operations/ToBinary.mjs b/src/core/operations/ToBinary.mjs index 1ef86ebb..af9087d1 100644 --- a/src/core/operations/ToBinary.mjs +++ b/src/core/operations/ToBinary.mjs @@ -7,6 +7,7 @@ import Operation from "../Operation"; import Utils from "../Utils"; import {BIN_DELIM_OPTIONS} from "../lib/Delim"; +import {toBinary} from "../lib/Binary"; /** * To Binary operation @@ -40,19 +41,7 @@ class ToBinary extends Operation { * @returns {string} */ run(input, args) { - const delim = Utils.charRep(args[0] || "Space"), - 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; - } + return toBinary(input, args[0]); } /** diff --git a/src/core/operations/XOR.mjs b/src/core/operations/XOR.mjs index ae1c2154..3bd508fc 100644 --- a/src/core/operations/XOR.mjs +++ b/src/core/operations/XOR.mjs @@ -6,7 +6,7 @@ import Operation from "../Operation"; import Utils from "../Utils"; -import { bitOp, xor } from "../lib/BitwiseOp"; +import { bitOp, xor, BITWISE_OP_DELIMS } from "../lib/BitwiseOp"; /** * XOR operation @@ -30,7 +30,7 @@ class XOR extends Operation { "name": "Key", "type": "toggleString", "value": "", - "toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"] + "toggleValues": BITWISE_OP_DELIMS }, { "name": "Scheme",