diff --git a/src/core/Utils.mjs b/src/core/Utils.mjs index 78d4d97d..65ed07fb 100755 --- a/src/core/Utils.mjs +++ b/src/core/Utils.mjs @@ -8,6 +8,7 @@ import utf8 from "utf8"; import moment from "moment-timezone"; import {fromBase64} from "./lib/Base64"; import {fromHex} from "./lib/Hex"; +import {fromDecimal} from "./lib/Decimal"; /** @@ -297,7 +298,7 @@ class Utils { * Accepts hex, Base64, UTF8 and Latin1 strings. * * @param {string} str - * @param {string} type - One of "Hex", "Base64", "UTF8" or "Latin1" + * @param {string} type - One of "Hex", "Decimal", "Base64", "UTF8" or "Latin1" * @returns {byteArray} * * @example @@ -314,6 +315,8 @@ class Utils { switch (type.toLowerCase()) { case "hex": return fromHex(str); + case "decimal": + return fromDecimal(str); case "base64": return fromBase64(str, null, "byteArray"); case "utf8": diff --git a/src/core/lib/Decimal.mjs b/src/core/lib/Decimal.mjs new file mode 100644 index 00000000..846b2a94 --- /dev/null +++ b/src/core/lib/Decimal.mjs @@ -0,0 +1,37 @@ +/** + * Decimal functions. + * + * @author n1474335 [n1474335@gmail.com] + * @copyright Crown Copyright 2018 + * @license Apache-2.0 + */ + +import Utils from "../Utils"; + + +/** + * Convert a string of decimal values into a byte array. + * + * @param {string} data + * @param {string} [delim] + * @returns {byteArray} + * + * @example + * // returns [10,20,30] + * fromDecimal("10 20 30"); + * + * // returns [10,20,30] + * fromDecimal("10:20:30", "Colon"); + */ +export function fromDecimal(data, delim="Auto") { + delim = Utils.charRep(delim); + const output = []; + let byteStr = data.split(delim); + if (byteStr[byteStr.length-1] === "") + byteStr = byteStr.slice(0, byteStr.length-1); + + for (let i = 0; i < byteStr.length; i++) { + output[i] = parseInt(byteStr[i], 10); + } + return output; +} diff --git a/src/core/lib/Hex.mjs b/src/core/lib/Hex.mjs index 538ba44b..760d84b5 100644 --- a/src/core/lib/Hex.mjs +++ b/src/core/lib/Hex.mjs @@ -1,5 +1,5 @@ /** - * Byte representation functions. + * Hexadecimal functions. * * @author n1474335 [n1474335@gmail.com] * @copyright Crown Copyright 2016 @@ -83,8 +83,7 @@ export function toHexFast(data) { * // returns [10,20,30] * fromHex("0a:14:1e", "Colon"); */ -export function fromHex(data, delim, byteLen=2) { - delim = delim || "Auto"; +export function fromHex(data, delim="Auto", byteLen=2) { if (delim !== "None") { const delimRegex = delim === "Auto" ? /[^a-f\d]/gi : Utils.regexRep(delim); data = data.replace(delimRegex, ""); diff --git a/src/core/operations/ADD.mjs b/src/core/operations/ADD.mjs index 7e6ad9d5..40329494 100644 --- a/src/core/operations/ADD.mjs +++ b/src/core/operations/ADD.mjs @@ -29,7 +29,7 @@ class ADD extends Operation { "name": "Key", "type": "toggleString", "value": "", - "toggleValues": ["Hex", "Base64", "UTF8", "Latin1"] + "toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"] } ]; } diff --git a/src/core/operations/AND.mjs b/src/core/operations/AND.mjs index 4a062725..30022ab9 100644 --- a/src/core/operations/AND.mjs +++ b/src/core/operations/AND.mjs @@ -29,7 +29,7 @@ class AND extends Operation { "name": "Key", "type": "toggleString", "value": "", - "toggleValues": ["Hex", "Base64", "UTF8", "Latin1"] + "toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"] } ]; } diff --git a/src/core/operations/FromDecimal.mjs b/src/core/operations/FromDecimal.mjs index 48667148..f3c117ff 100644 --- a/src/core/operations/FromDecimal.mjs +++ b/src/core/operations/FromDecimal.mjs @@ -5,8 +5,8 @@ */ import Operation from "../Operation"; -import Utils from "../Utils"; import {DELIM_OPTIONS} from "../lib/Delim"; +import {fromDecimal} from "../lib/Decimal"; /** * From Decimal operation @@ -71,16 +71,7 @@ class FromDecimal extends Operation { * @returns {byteArray} */ run(input, args) { - const delim = Utils.charRep(args[0]), - output = []; - let byteStr = input.split(delim); - if (byteStr[byteStr.length-1] === "") - byteStr = byteStr.slice(0, byteStr.length-1); - - for (let i = 0; i < byteStr.length; i++) { - output[i] = parseInt(byteStr[i], 10); - } - return output; + return fromDecimal(input, args[0]); } } diff --git a/src/core/operations/FromHex.mjs b/src/core/operations/FromHex.mjs index 45ea9955..3c846824 100644 --- a/src/core/operations/FromHex.mjs +++ b/src/core/operations/FromHex.mjs @@ -86,7 +86,7 @@ class FromHex extends Operation { * @returns {byteArray} */ run(input, args) { - const delim = args[0] || "Space"; + const delim = args[0] || "Auto"; return fromHex(input, delim, 2); } diff --git a/src/core/operations/OR.mjs b/src/core/operations/OR.mjs index 33bb2f63..cd3b757e 100644 --- a/src/core/operations/OR.mjs +++ b/src/core/operations/OR.mjs @@ -29,7 +29,7 @@ class OR extends Operation { "name": "Key", "type": "toggleString", "value": "", - "toggleValues": ["Hex", "Base64", "UTF8", "Latin1"] + "toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"] } ]; } diff --git a/src/core/operations/SUB.mjs b/src/core/operations/SUB.mjs index 79ce95d0..5fef815d 100644 --- a/src/core/operations/SUB.mjs +++ b/src/core/operations/SUB.mjs @@ -29,7 +29,7 @@ class SUB extends Operation { "name": "Key", "type": "toggleString", "value": "", - "toggleValues": ["Hex", "Base64", "UTF8", "Latin1"] + "toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"] } ]; } diff --git a/src/core/operations/XOR.mjs b/src/core/operations/XOR.mjs index ae35eab7..c3d14a7a 100644 --- a/src/core/operations/XOR.mjs +++ b/src/core/operations/XOR.mjs @@ -29,7 +29,7 @@ class XOR extends Operation { "name": "Key", "type": "toggleString", "value": "", - "toggleValues": ["Hex", "Base64", "UTF8", "Latin1"] + "toggleValues": ["Hex", "Decimal", "Base64", "UTF8", "Latin1"] }, { "name": "Scheme",