Browse Source

Merge branch 'artemisbot-feature-octal'

n1474335 8 years ago
parent
commit
87ebe77dfb

+ 4 - 3
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) {

+ 2 - 0
src/core/config/Categories.js

@@ -35,6 +35,8 @@ const Categories = [
             "From Decimal",
             "To Binary",
             "From Binary",
+            "To Octal",
+            "From Octal",
             "To Base64",
             "From Base64",
             "Show Base64 offsets",

+ 31 - 1
src/core/config/OperationConfig.js

@@ -425,7 +425,7 @@ const OperationConfig = {
         ]
     },
     "From Hex": {
-        description: "Converts a hexadecimal byte string back into a its raw value.<br><br>e.g. <code>ce 93 ce b5 ce b9 ce ac 20 cf 83 ce bf cf 85 0a</code> becomes the UTF-8 encoded string <code>Γειά σου</code>",
+        description: "Converts a hexadecimal byte string back into its raw value.<br><br>e.g. <code>ce 93 ce b5 ce b9 ce ac 20 cf 83 ce bf cf 85 0a</code> becomes the UTF-8 encoded string <code>Γειά σου</code>",
         run: ByteRepr.runFromHex,
         highlight: ByteRepr.highlightFrom,
         highlightReverse: ByteRepr.highlightTo,
@@ -454,6 +454,36 @@ const OperationConfig = {
             }
         ]
     },
+    "From Octal": {
+        description: "Converts an octal byte string back into its raw value.<br><br>e.g. <code>316 223 316 265 316 271 316 254 40 317 203 316 277 317 205</code> becomes the UTF-8 encoded string <code>Γειά σου</code>",
+        run: ByteRepr.runFromOct,
+        highlight: false,
+        highlightReverse: false,
+        inputType: "string",
+        outputType: "byteArray",
+        args: [
+            {
+                name: "Delimiter",
+                type: "option",
+                value: ByteRepr.DELIM_OPTIONS
+            }
+        ]
+    },
+    "To Octal": {
+        description: "Converts the input string to octal bytes separated by the specified delimiter.<br><br>e.g. The UTF-8 encoded string <code>Γειά σου</code> becomes <code>316 223 316 265 316 271 316 254 40 317 203 316 277 317 205</code>",
+        run: ByteRepr.runToOct,
+        highlight: false,
+        highlightReverse: false,
+        inputType: "byteArray",
+        outputType: "string",
+        args: [
+            {
+                name: "Delimiter",
+                type: "option",
+                value: ByteRepr.DELIM_OPTIONS
+            }
+        ]
+    },
     "From Charcode": {
         description: "Converts unicode character codes back into text.<br><br>e.g. <code>0393 03b5 03b9 03ac 20 03c3 03bf 03c5</code> becomes <code>Γειά σου</code>",
         run: ByteRepr.runFromCharcode,

+ 29 - 0
src/core/operations/ByteRepr.js

@@ -55,6 +55,35 @@ const ByteRepr = {
     },
 
 
+    /**
+     * To Octal 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 input.map(val => val.toString(8)).join(delim);
+    },
+
+
+    /**
+     * From Octal 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"];
+        if (input.length === 0) return [];
+        return input.split(delim).map(val => parseInt(val, 8));
+    },
+
+
     /**
      * @constant
      * @default

+ 1 - 0
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";

+ 77 - 0
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"]
+            }
+        ]
+    },
+]);