From b8ce10ae96b4fb43475a2b0b3d8dd2e6a9b33bdd Mon Sep 17 00:00:00 2001 From: Matt C Date: Tue, 14 Mar 2017 21:26:10 +0000 Subject: [PATCH 1/4] Adds Octal functions - To Octal - From Octal --- src/js/config/Categories.js | 2 ++ src/js/config/OperationConfig.js | 32 +++++++++++++++++++++++++++++++- src/js/core/Utils.js | 32 ++++++++++++++++++++++++++++++++ src/js/operations/ByteRepr.js | 31 +++++++++++++++++++++++++++++++ 4 files changed, 96 insertions(+), 1 deletion(-) diff --git a/src/js/config/Categories.js b/src/js/config/Categories.js index 3a84b0ef..397a4c56 100755 --- a/src/js/config/Categories.js +++ b/src/js/config/Categories.js @@ -35,6 +35,8 @@ var Categories = [ "From Decimal", "To Binary", "From Binary", + "To Octal", + "From Octal", "To Base64", "From Base64", "Show Base64 offsets", diff --git a/src/js/config/OperationConfig.js b/src/js/config/OperationConfig.js index e8e3a881..153a48ee 100755 --- a/src/js/config/OperationConfig.js +++ b/src/js/config/OperationConfig.js @@ -392,7 +392,7 @@ var OperationConfig = { ] }, "From Hex": { - description: "Converts a hexadecimal byte string back into a its raw value.

e.g. ce 93 ce b5 ce b9 ce ac 20 cf 83 ce bf cf 85 0a becomes the UTF-8 encoded string Γειά σου", + description: "Converts a hexadecimal byte string back into its raw value.

e.g. ce 93 ce b5 ce b9 ce ac 20 cf 83 ce bf cf 85 0a becomes the UTF-8 encoded string Γειά σου", run: ByteRepr.runFromHex, highlight: ByteRepr.highlightFrom, highlightReverse: ByteRepr.highlightTo, @@ -421,6 +421,36 @@ var OperationConfig = { } ] }, + "From Octal": { + description: "Converts a octal byte string back into its raw value.

e.g. 316 223 316 265 316 271 316 254 40 317 203 316 277 317 205 becomes the UTF-8 encoded string Γειά σου", + run: ByteRepr.runFromOct, + highlight: false, + highlightReverse: false, + inputType: "string", + outputType: "byteArray", + args: [ + { + name: "Delimiter", + type: "option", + value: ByteRepr.OCT_DELIM_OPTIONS + } + ] + }, + "To Octal": { + description: "Converts the input string to octal bytes separated by the specified delimiter.

e.g. The UTF-8 encoded string Γειά σου becomes 316 223 316 265 316 271 316 254 40 317 203 316 277 317 205", + run: ByteRepr.runToOct, + highlight: false, + highlightReverse: false, + inputType: "byteArray", + outputType: "string", + args: [ + { + name: "Delimiter", + type: "option", + value: ByteRepr.OCT_DELIM_OPTIONS + } + ] + }, "From Charcode": { description: "Converts unicode character codes back into text.

e.g. 0393 03b5 03b9 03ac 20 03c3 03bf 03c5 becomes Γειά σου", run: ByteRepr.runFromCharcode, diff --git a/src/js/core/Utils.js b/src/js/core/Utils.js index 9ef0b2b1..23e6221f 100755 --- a/src/js/core/Utils.js +++ b/src/js/core/Utils.js @@ -828,6 +828,38 @@ var Utils = { }, + /** + * Convert an byte array into a octal string + * + * @author Matt C [matt@artemisbot.pw] + * @param {byteArray} data + * @param {string} [delim] + * @returns {string} + * + */ + toOct: function(data, delim) { + var output = ""; + delim = delim || "Space"; + data.map(val => output += (parseInt(Utils.bin(val), 2).toString(8) + delim)); + return output.slice(0, -delim.length); + }, + + + /** + * Convert an Octal string into a byte array. + * + * @author Matt C [matt@artemisbot.pw] + * @param {string} data + * @param {string} [delim] + * @returns {byteArray} + * + */ + fromOct: function(data, delim) { + delim = delim || "Space"; + return data.split(delim).map(val => parseInt(val, 8)); + }, + + /** * Parses CSV data and returns it as a two dimensional array or strings. * diff --git a/src/js/operations/ByteRepr.js b/src/js/operations/ByteRepr.js index 2130409d..5a1fdb93 100755 --- a/src/js/operations/ByteRepr.js +++ b/src/js/operations/ByteRepr.js @@ -21,6 +21,11 @@ var ByteRepr = { * @default */ HEX_DELIM_OPTIONS: ["Space", "Comma", "Semi-colon", "Colon", "Line feed", "CRLF", "0x", "\\x", "None"], + /** + * @constant + * @default + */ + OCT_DELIM_OPTIONS: ["Space", "Comma", "Semi-colon", "Colon", "Line feed", "CRLF"], /** * @constant * @default @@ -53,6 +58,32 @@ var ByteRepr = { }, + /** + * To Oct operation. + * + * @author Matt C [matt@artemisbot.pw] + * @param {byteArray} input + * @param {Object[]} args + * @returns {string} + */ + runToOct: function(input, args) { + var delim = Utils.charRep[args[0] || "Space"]; + return Utils.toOct(input, delim, 2); + }, + + /** + * From Oct operation. + * + * @author Matt C [matt@artemisbot.pw] + * @param {string} input + * @param {Object[]} args + * @returns {byteArray} + */ + runFromOct: function(input, args) { + var delim = Utils.charRep[args[0] || "Space"]; + return Utils.fromOct(input, delim); + }, + /** * @constant * @default From 96e40a6479ec37b0a180439230794cdd6932fd7c Mon Sep 17 00:00:00 2001 From: Matt C Date: Mon, 27 Mar 2017 20:30:32 +0000 Subject: [PATCH 2/4] Made requested changes. --- src/js/config/OperationConfig.js | 4 ++-- src/js/core/Utils.js | 32 -------------------------------- src/js/operations/ByteRepr.js | 13 +++++-------- 3 files changed, 7 insertions(+), 42 deletions(-) diff --git a/src/js/config/OperationConfig.js b/src/js/config/OperationConfig.js index 153a48ee..4957610e 100755 --- a/src/js/config/OperationConfig.js +++ b/src/js/config/OperationConfig.js @@ -432,7 +432,7 @@ var OperationConfig = { { name: "Delimiter", type: "option", - value: ByteRepr.OCT_DELIM_OPTIONS + value: ByteRepr.DELIM_OPTIONS } ] }, @@ -447,7 +447,7 @@ var OperationConfig = { { name: "Delimiter", type: "option", - value: ByteRepr.OCT_DELIM_OPTIONS + value: ByteRepr.DELIM_OPTIONS } ] }, diff --git a/src/js/core/Utils.js b/src/js/core/Utils.js index c9ce1b0b..405249a1 100755 --- a/src/js/core/Utils.js +++ b/src/js/core/Utils.js @@ -828,38 +828,6 @@ var Utils = { }, - /** - * Convert an byte array into a octal string - * - * @author Matt C [matt@artemisbot.pw] - * @param {byteArray} data - * @param {string} [delim] - * @returns {string} - * - */ - toOct: function(data, delim) { - var output = ""; - delim = delim || "Space"; - data.map(val => output += (parseInt(Utils.bin(val), 2).toString(8) + delim)); - return output.slice(0, -delim.length); - }, - - - /** - * Convert an Octal string into a byte array. - * - * @author Matt C [matt@artemisbot.pw] - * @param {string} data - * @param {string} [delim] - * @returns {byteArray} - * - */ - fromOct: function(data, delim) { - delim = delim || "Space"; - return data.split(delim).map(val => parseInt(val, 8)); - }, - - /** * Parses CSV data and returns it as a two dimensional array or strings. * diff --git a/src/js/operations/ByteRepr.js b/src/js/operations/ByteRepr.js index 5a1fdb93..8bdf398f 100755 --- a/src/js/operations/ByteRepr.js +++ b/src/js/operations/ByteRepr.js @@ -21,11 +21,6 @@ var ByteRepr = { * @default */ HEX_DELIM_OPTIONS: ["Space", "Comma", "Semi-colon", "Colon", "Line feed", "CRLF", "0x", "\\x", "None"], - /** - * @constant - * @default - */ - OCT_DELIM_OPTIONS: ["Space", "Comma", "Semi-colon", "Colon", "Line feed", "CRLF"], /** * @constant * @default @@ -67,8 +62,10 @@ var ByteRepr = { * @returns {string} */ runToOct: function(input, args) { - var delim = Utils.charRep[args[0] || "Space"]; - return Utils.toOct(input, delim, 2); + var delim = Utils.charRep[args[0] || "Space"], + output = ""; + input.map(val => output += (parseInt(Utils.bin(val), 2).toString(8) + delim)); + return output.slice(0, -delim.length); }, /** @@ -81,7 +78,7 @@ var ByteRepr = { */ runFromOct: function(input, args) { var delim = Utils.charRep[args[0] || "Space"]; - return Utils.fromOct(input, delim); + return input.split(delim).map(val => parseInt(val, 8)); }, /** From 7ab6c887748140452c80f2692e229c83b9d23c8a Mon Sep 17 00:00:00 2001 From: Matt C Date: Tue, 28 Mar 2017 16:21:57 +0000 Subject: [PATCH 3/4] Made fixes so that tests might work --- src/js/operations/ByteRepr.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/js/operations/ByteRepr.js b/src/js/operations/ByteRepr.js index 8bdf398f..e9d6e2c4 100755 --- a/src/js/operations/ByteRepr.js +++ b/src/js/operations/ByteRepr.js @@ -62,10 +62,8 @@ var ByteRepr = { * @returns {string} */ runToOct: function(input, args) { - var delim = Utils.charRep[args[0] || "Space"], - output = ""; - input.map(val => output += (parseInt(Utils.bin(val), 2).toString(8) + delim)); - return output.slice(0, -delim.length); + var delim = Utils.charRep[args[0] || "Space"]; + return input.map(val => parseInt(Utils.bin(val), 2).toString(8)).join(delim); }, /** @@ -78,6 +76,7 @@ var ByteRepr = { */ runFromOct: function(input, args) { var delim = Utils.charRep[args[0] || "Space"]; + if (input.length === 0) return []; return input.split(delim).map(val => parseInt(val, 8)); }, From 5f85bff315d9b7bdedb2e65e2f3db1b95def722e Mon Sep 17 00:00:00 2001 From: n1474335 Date: Wed, 5 Apr 2017 22:00:06 +0100 Subject: [PATCH 4/4] Fixed tests for octal operations. --- src/core/Utils.js | 7 +-- src/core/config/OperationConfig.js | 2 +- src/core/operations/ByteRepr.js | 8 ++-- test/index.js | 1 + test/tests/operations/ByteRepr.js | 77 ++++++++++++++++++++++++++++++ 5 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 test/tests/operations/ByteRepr.js diff --git a/src/core/Utils.js b/src/core/Utils.js index 3794d972..2e4b354b 100755 --- a/src/core/Utils.js +++ b/src/core/Utils.js @@ -212,7 +212,7 @@ const Utils = { * @returns {string} */ printable: function(str, preserveWs) { - if (window && window.app && !window.app.options.treatAsUtf8) { + if (typeof window !== "undefined" && window.app && !window.app.options.treatAsUtf8) { str = Utils.byteArrayToChars(Utils.strToByteArray(str)); } @@ -388,8 +388,9 @@ const Utils = { var wordArray = CryptoJS.enc.Utf8.parse(str), byteArray = Utils.wordArrayToByteArray(wordArray); - if (window && str.length !== wordArray.sigBytes) + if (typeof window !== "undefined" && str.length !== wordArray.sigBytes) { window.app.options.attemptHighlight = false; + } return byteArray; }, @@ -440,7 +441,7 @@ const Utils = { var wordArray = new CryptoJS.lib.WordArray.init(words, byteArray.length), str = CryptoJS.enc.Utf8.stringify(wordArray); - if (window && str.length !== wordArray.sigBytes) + if (typeof window !== "undefined" && str.length !== wordArray.sigBytes) window.app.options.attemptHighlight = false; return str; } catch (err) { diff --git a/src/core/config/OperationConfig.js b/src/core/config/OperationConfig.js index f1fc336f..16f6f1b7 100755 --- a/src/core/config/OperationConfig.js +++ b/src/core/config/OperationConfig.js @@ -455,7 +455,7 @@ const OperationConfig = { ] }, "From Octal": { - description: "Converts a octal byte string back into its raw value.

e.g. 316 223 316 265 316 271 316 254 40 317 203 316 277 317 205 becomes the UTF-8 encoded string Γειά σου", + description: "Converts an octal byte string back into its raw value.

e.g. 316 223 316 265 316 271 316 254 40 317 203 316 277 317 205 becomes the UTF-8 encoded string Γειά σου", run: ByteRepr.runFromOct, highlight: false, highlightReverse: false, diff --git a/src/core/operations/ByteRepr.js b/src/core/operations/ByteRepr.js index c12d9847..950492ac 100755 --- a/src/core/operations/ByteRepr.js +++ b/src/core/operations/ByteRepr.js @@ -56,7 +56,7 @@ const ByteRepr = { /** - * To Oct operation. + * To Octal operation. * * @author Matt C [matt@artemisbot.pw] * @param {byteArray} input @@ -65,11 +65,12 @@ const ByteRepr = { */ runToOct: function(input, args) { var delim = Utils.charRep[args[0] || "Space"]; - return input.map(val => parseInt(Utils.bin(val), 2).toString(8)).join(delim); + return input.map(val => val.toString(8)).join(delim); }, + /** - * From Oct operation. + * From Octal operation. * * @author Matt C [matt@artemisbot.pw] * @param {string} input @@ -82,6 +83,7 @@ const ByteRepr = { return input.split(delim).map(val => parseInt(val, 8)); }, + /** * @constant * @default diff --git a/test/index.js b/test/index.js index fba3e43a..13d82b10 100644 --- a/test/index.js +++ b/test/index.js @@ -12,6 +12,7 @@ import "babel-polyfill"; import TestRegister from "./TestRegister.js"; import "./tests/operations/Base58.js"; +import "./tests/operations/ByteRepr.js"; import "./tests/operations/Compress.js"; import "./tests/operations/FlowControl.js"; import "./tests/operations/MorseCode.js"; diff --git a/test/tests/operations/ByteRepr.js b/test/tests/operations/ByteRepr.js new file mode 100644 index 00000000..533e7a2f --- /dev/null +++ b/test/tests/operations/ByteRepr.js @@ -0,0 +1,77 @@ +/** + * ByteRepr tests. + * + * @author Matt C [matt@artemisbot.pw] + * @copyright Crown Copyright 2017 + * @license Apache-2.0 + */ +import TestRegister from "../../TestRegister.js"; + +TestRegister.addTests([ + { + name: "To Octal: nothing", + input: "", + expectedOutput: "", + recipeConfig: [ + { + "op": "To Octal", + "args": ["Space"] + } + ] + }, + { + name: "From Octal: nothing", + input: "", + expectedOutput: "", + recipeConfig: [ + { + "op": "From Octal", + "args": ["Space"] + } + ] + }, + { + name: "To Octal: hello world", + input: "hello world", // [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100], + expectedOutput: "150 145 154 154 157 40 167 157 162 154 144", + recipeConfig: [ + { + "op": "To Octal", + "args": ["Space"] + } + ] + }, + { + name: "From Octal: hello world", + input: "150 145 154 154 157 40 167 157 162 154 144", + expectedOutput: "hello world", + recipeConfig: [ + { + "op": "From Octal", + "args": ["Space"] + } + ] + }, + { + name: "To Octal: Γειά σου", + input: "Γειά σου", //[206,147,206,181,206,185,206,172,32,207,131,206,191,207,133], + expectedOutput: "316 223 316 265 316 271 316 254 40 317 203 316 277 317 205", + recipeConfig: [ + { + "op": "To Octal", + "args": ["Space"] + } + ] + }, + { + name: "From Octal: Γειά σου", + input: "316 223 316 265 316 271 316 254 40 317 203 316 277 317 205", + expectedOutput: "Γειά σου", + recipeConfig: [ + { + "op": "From Octal", + "args": ["Space"] + } + ] + }, +]); \ No newline at end of file