From c460c2bf6b2ce1db33a524176dbe09509f4d5acd Mon Sep 17 00:00:00 2001 From: n1474335 Date: Fri, 7 Jul 2017 13:23:58 +0000 Subject: [PATCH] Replaced hexToByteArray with fromHex and byteArrayToHex with toHex. Switched displayFilesAsHTML operation to use template strings and introduced markup formatting method. --- src/core/Ingredient.js | 2 +- src/core/Utils.js | 150 ++++++++++++------------------- src/core/operations/BitwiseOp.js | 4 +- src/core/operations/IP.js | 4 +- src/web/OutputWaiter.js | 1 + 5 files changed, 62 insertions(+), 99 deletions(-) diff --git a/src/core/Ingredient.js b/src/core/Ingredient.js index 543f732b..e8d8a8cc 100755 --- a/src/core/Ingredient.js +++ b/src/core/Ingredient.js @@ -73,7 +73,7 @@ Ingredient.prepare = function(data, type) { case "byteArray": if (typeof data == "string") { data = data.replace(/\s+/g, ""); - return Utils.hexToByteArray(data); + return Utils.fromHex(data); } else { return data; } diff --git a/src/core/Utils.js b/src/core/Utils.js index 3af4ca5b..8bfef0a8 100755 --- a/src/core/Utils.js +++ b/src/core/Utils.js @@ -340,50 +340,6 @@ const Utils = { }, - /** - * Translates a hex string into an array of bytes. - * - * @param {string} hexStr - * @returns {byteArray} - * - * @example - * // returns [0xfe, 0x09, 0xa7] - * Utils.hexToByteArray("fe09a7"); - */ - hexToByteArray: function(hexStr) { - // TODO: Handle errors i.e. input string is not hex - if (!hexStr) return []; - hexStr = hexStr.replace(/\s+/g, ""); - const byteArray = []; - for (let i = 0; i < hexStr.length; i += 2) { - byteArray.push(parseInt(hexStr.substr(i, 2), 16)); - } - return byteArray; - }, - - - /** - * Translates an array of bytes to a hex string. - * - * @param {byteArray} byteArray - * @param {string} [delim=" "] - * @returns {string} - * - * @example - * // returns "fe09a7" - * Utils.byteArrayToHex([0xfe, 0x09, 0xa7], ""); - */ - byteArrayToHex: function(byteArray, delim) { - if (!byteArray) return ""; - delim = typeof delim === "undefined" ? " " : delim; - let hexStr = ""; - for (let i = 0; i < byteArray.length; i++) { - hexStr += Utils.hex(byteArray[i]) + delim; - } - return hexStr.slice(0, hexStr.length - delim.length); - }, - - /** * Converts a string to a byte array. * Treats the string as UTF-8 if any values are over 255. @@ -828,7 +784,7 @@ const Utils = { if (removeScriptAndStyle) { htmlStr = htmlStr.replace(/<(script|style)[^>]*>.*<\/(script|style)>/gmi, ""); } - return htmlStr.replace(/<[^>\n]+>/g, ""); + return htmlStr.replace(/<[^>]+>/g, ""); }, @@ -946,16 +902,19 @@ const Utils = { * @returns {html} */ displayFilesAsHTML: function(files) { + /* and used to denote newlines and spaces in HTML markup. + * If a non-html operation is used, all markup will be removed but these + * whitespace chars will remain for formatting purposes. + */ + const formatDirectory = function(file) { - const html = "
" + - "" + - "
"; + const html = `
+ +
`; return html; }; @@ -966,52 +925,52 @@ const Utils = { ); const blobUrl = URL.createObjectURL(blob); - const downloadAnchorElem = "\u21B4"; + const viewFileElem = ``; - const expandFileContentsElem = "🔍"; + const downloadFileElem = `💾`; - const hexFileData = Utils.byteArrayToHex(new Uint8Array(file.bytes)); + const hexFileData = Utils.toHexFast(new Uint8Array(file.bytes)); - const switchFileDataToInput = ""; + const switchToInputElem = ``; - const html = "
" + - "" + - "
" + - "
" + - "
" + Utils.escapeHtml(file.contents) + "
" + - "
" + - "
"; + const html = `
+ +
+
+
${Utils.escapeHtml(file.contents)}
+
+
+
`; return html; }; - let html = "
" + - files.length + - " file(s) found
\n"; + let html = `
+ ${files.length} file(s) found +
`; + files.forEach(function(file, i) { if (typeof file.contents !== "undefined") { html += formatFile(file, i); @@ -1019,7 +978,10 @@ const Utils = { html += formatDirectory(file); } }); - return html; + + return html.replace(/(?:(
(?:\n|.)*<\/pre>)|\s{2,})/g, "$1") // Remove whitespace from markup
+            .replace(//g, "\n") // Replace  with newlines
+            .replace(//g, " "); // Replace  with spaces
     },
 
 
diff --git a/src/core/operations/BitwiseOp.js b/src/core/operations/BitwiseOp.js
index 7a4675f6..5b7fb741 100755
--- a/src/core/operations/BitwiseOp.js
+++ b/src/core/operations/BitwiseOp.js
@@ -142,12 +142,12 @@ const BitwiseOp = {
 
 
         for (let key = 1, l = Math.pow(256, keyLength); key < l; key++) {
-            result = BitwiseOp._bitOp(input, Utils.hexToByteArray(key.toString(16)), BitwiseOp._xor, nullPreserving, differential);
+            result = BitwiseOp._bitOp(input, Utils.fromHex(key.toString(16)), BitwiseOp._xor, nullPreserving, differential);
             resultUtf8 = Utils.byteArrayToUtf8(result);
             if (crib !== "" && resultUtf8.search(regex) === -1) continue;
             if (printKey) output += "Key = " + Utils.hex(key, (2*keyLength)) + ": ";
             if (outputHex)
-                output += Utils.byteArrayToHex(result) + "\n";
+                output += Utils.toHex(result) + "\n";
             else
                 output += Utils.printable(resultUtf8, false) + "\n";
             if (printKey) output += "\n";
diff --git a/src/core/operations/IP.js b/src/core/operations/IP.js
index 8a852789..4c3eb5de 100755
--- a/src/core/operations/IP.js
+++ b/src/core/operations/IP.js
@@ -283,7 +283,7 @@ const IP = {
                     baIp.push(decimal & 255);
                     break;
                 case "Hex":
-                    baIp = Utils.hexToByteArray(lines[i]);
+                    baIp = Utils.fromHex(lines[i]);
                     break;
                 default:
                     throw "Unsupported input IP format";
@@ -516,7 +516,7 @@ const IP = {
             "Destination IP address" + IP._ipv4ToStr(dstIP) + "";
 
         if (ihl > 5) {
-            output += "Options" + Utils.byteArrayToHex(options) + "";
+            output += "Options" + Utils.toHex(options) + "";
         }
 
         return output + "";
diff --git a/src/web/OutputWaiter.js b/src/web/OutputWaiter.js
index 3523db18..ee5d57ca 100755
--- a/src/web/OutputWaiter.js
+++ b/src/web/OutputWaiter.js
@@ -172,6 +172,7 @@ OutputWaiter.prototype.undoSwitchClick = function() {
  * Moves a files data for items created via Utils.displayFilesAsHTML to the input.
  */
 OutputWaiter.prototype.fileSwitch = function(e) {
+    e.preventDefault();
     this.switchOrigData = this.manager.input.get();
     this.app.setInput(e.target.getAttribute("fileValue"));
     document.getElementById("undo-switch").disabled = false;