Skip to content

Commit

Permalink
Operations can now set options from within the worker
Browse files Browse the repository at this point in the history
  • Loading branch information
n1474335 committed Sep 19, 2017
1 parent 13f07ab commit f6b52b7
Show file tree
Hide file tree
Showing 10 changed files with 81 additions and 38 deletions.
5 changes: 4 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@

"COMPILE_TIME": false,
"COMPILE_MSG": false,
"PKG_VERSION": false
"PKG_VERSION": false,
"ENVIRONMENT_IS_WORKER": false,
"ENVIRONMENT_IS_NODE": false,
"ENVIRONMENT_IS_WEB": false
}
}
21 changes: 18 additions & 3 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,16 @@ module.exports = function (grunt) {
BUILD_CONSTANTS = {
COMPILE_TIME: JSON.stringify(compileTime),
COMPILE_MSG: JSON.stringify(grunt.option("compile-msg") || grunt.option("msg") || ""),
PKG_VERSION: JSON.stringify(pkg.version)
PKG_VERSION: JSON.stringify(pkg.version),
ENVIRONMENT_IS_WORKER: function() {
return typeof importScripts === "function";
},
ENVIRONMENT_IS_NODE: function() {
return typeof process === "object" && typeof require === "function";
},
ENVIRONMENT_IS_WEB: function() {
return typeof window === "object";
}
},
moduleEntryPoints = listEntryModules();

Expand Down Expand Up @@ -277,7 +286,10 @@ module.exports = function (grunt) {
output: {
filename: "index.js",
path: __dirname + "/build/test"
}
},
plugins: [
new webpack.DefinePlugin(BUILD_CONSTANTS)
]
},
node: {
target: "node",
Expand All @@ -288,7 +300,10 @@ module.exports = function (grunt) {
path: __dirname + "/build/node",
library: "CyberChef",
libraryTarget: "commonjs2"
}
},
plugins: [
new webpack.DefinePlugin(BUILD_CONSTANTS)
]
}
},
"webpack-dev-server": {
Expand Down
9 changes: 1 addition & 8 deletions src/core/Chef.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const Chef = function() {
* @returns {string} response.result - The output of the recipe
* @returns {string} response.type - The data type of the result
* @returns {number} response.progress - The position that we have got to in the recipe
* @returns {number} response.options - The app options object (which may have been changed)
* @returns {number} response.duration - The number of ms it took to execute the recipe
* @returns {number} response.error - The error object thrown by a failed operation (false if no error)
*/
Expand All @@ -40,12 +39,7 @@ Chef.prototype.bake = async function(inputText, recipeConfig, options, progress,
containsFc = recipe.containsFlowControl(),
error = false;

// Reset attemptHighlight flag
if (options.hasOwnProperty("attemptHighlight")) {
options.attemptHighlight = true;
}

if (containsFc) options.attemptHighlight = false;
if (containsFc && ENVIRONMENT_IS_WORKER()) self.setOption("attemptHighlight", false);

// Clean up progress
if (progress >= recipeConfig.length) {
Expand Down Expand Up @@ -86,7 +80,6 @@ Chef.prototype.bake = async function(inputText, recipeConfig, options, progress,
this.dish.get(Dish.STRING),
type: Dish.enumLookup(this.dish.type),
progress: progress,
options: options,
duration: new Date().getTime() - startTime,
error: error
};
Expand Down
32 changes: 25 additions & 7 deletions src/core/ChefWorker.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,24 @@ self.postMessage({
*/
self.addEventListener("message", function(e) {
// Handle message
switch (e.data.action) {
const r = e.data;
switch (r.action) {
case "bake":
bake(e.data.data);
bake(r.data);
break;
case "silentBake":
silentBake(e.data.data);
silentBake(r.data);
break;
case "docURL":
// Used to set the URL of the current document so that scripts can be
// imported into an inline worker.
self.docURL = e.data.data;
self.docURL = r.data;
break;
case "highlight":
calculateHighlights(
e.data.data.recipeConfig,
e.data.data.direction,
e.data.data.pos
r.data.recipeConfig,
r.data.direction,
r.data.pos
);
break;
default:
Expand Down Expand Up @@ -158,3 +159,20 @@ self.sendStatusMessage = function(msg) {
data: msg
});
};


/**
* Send an option value update to the app.
*
* @param {string} option
* @param {*} value
*/
self.setOption = function(option, value) {
self.postMessage({
action: "optionUpdate",
data: {
option: option,
value: value
}
});
};
19 changes: 14 additions & 5 deletions src/core/Utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ const Utils = {
* @returns {string}
*/
printable: function(str, preserveWs) {
if (typeof window !== "undefined" && window.app && !window.app.options.treatAsUtf8) {
if (ENVIRONMENT_IS_WEB() && window.app && !window.app.options.treatAsUtf8) {
str = Utils.byteArrayToChars(Utils.strToByteArray(str));
}

Expand Down Expand Up @@ -384,8 +384,12 @@ const Utils = {
let wordArray = CryptoJS.enc.Utf8.parse(str),
byteArray = Utils.wordArrayToByteArray(wordArray);

if (typeof window !== "undefined" && str.length !== wordArray.sigBytes) {
window.app.options.attemptHighlight = false;
if (str.length !== wordArray.sigBytes) {
if (ENVIRONMENT_IS_WORKER()) {
self.setOption("attemptHighlight", false);
} else if (ENVIRONMENT_IS_WEB()) {
window.app.options.attemptHighlight = false;
}
}
return byteArray;
},
Expand Down Expand Up @@ -448,8 +452,13 @@ const Utils = {
let wordArray = new CryptoJS.lib.WordArray.init(words, byteArray.length),
str = CryptoJS.enc.Utf8.stringify(wordArray);

if (typeof window !== "undefined" && str.length !== wordArray.sigBytes)
window.app.options.attemptHighlight = false;
if (str.length !== wordArray.sigBytes) {
if (ENVIRONMENT_IS_WORKER()) {
self.setOption("attemptHighlight", false);
} else if (ENVIRONMENT_IS_WEB()) {
window.app.options.attemptHighlight = false;
}
}
return str;
} catch (err) {
// If it fails, treat it as ANSI
Expand Down
5 changes: 3 additions & 2 deletions src/core/operations/BitwiseOp.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ const BitwiseOp = {

input = input.slice(sampleOffset, sampleOffset + sampleLength);

if (self) self.sendStatusMessage("Calculating " + Math.pow(256, keyLength) + " values...");
if (ENVIRONMENT_IS_WORKER())
self.sendStatusMessage("Calculating " + Math.pow(256, keyLength) + " values...");

/**
* Converts an integer to an array of bytes expressing that number.
Expand All @@ -156,7 +157,7 @@ const BitwiseOp = {
};

for (let key = 1, l = Math.pow(256, keyLength); key < l; key++) {
if (key % 10000 === 0 && self) {
if (key % 10000 === 0 && ENVIRONMENT_IS_WORKER()) {
self.sendStatusMessage("Calculating " + l + " values... " + Math.floor(key / l * 100) + "%");
}

Expand Down
8 changes: 3 additions & 5 deletions src/core/operations/ByteRepr.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,11 +119,11 @@ const ByteRepr = {
else if (ordinal < 4294967296) padding = 8;
else padding = 2;

if (padding > 2 && app) app.options.attemptHighlight = false;
if (padding > 2 && ENVIRONMENT_IS_WORKER()) self.setOption("attemptHighlight", false);

output += Utils.hex(ordinal, padding) + delim;
} else {
if (app) app.options.attemptHighlight = false;
if (ENVIRONMENT_IS_WORKER()) self.setOption("attemptHighlight", false);
output += ordinal.toString(base) + delim;
}
}
Expand All @@ -149,9 +149,7 @@ const ByteRepr = {
throw "Error: Base argument must be between 2 and 36";
}

if (base !== 16 && app) {
app.options.attemptHighlight = false;
}
if (base !== 16 && ENVIRONMENT_IS_WORKER()) self.setOption("attemptHighlight", false);

// Split into groups of 2 if the whole string is concatenated and
// too long to be a single character
Expand Down
2 changes: 1 addition & 1 deletion src/core/operations/Hexdump.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ const Hexdump = {
const w = (width - 13) / 4;
// w should be the specified width of the hexdump and therefore a round number
if (Math.floor(w) !== w || input.indexOf("\r") !== -1 || output.indexOf(13) !== -1) {
//TODO if (self) self.setOption("attemptHighlight", false);
if (ENVIRONMENT_IS_WORKER()) self.setOption("attemptHighlight", false);
}
return output;
},
Expand Down
3 changes: 3 additions & 0 deletions src/web/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ App.prototype.handleError = function(err) {
App.prototype.bake = function(step) {
if (this.baking) return;

// Reset attemptHighlight flag
this.options.attemptHighlight = true;

this.manager.worker.bake(
this.getInput(), // The user's input
this.getRecipeConfig(), // The configuration of the recipe
Expand Down
15 changes: 9 additions & 6 deletions src/web/WorkerWaiter.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@ WorkerWaiter.prototype.registerChefWorker = function() {
* @param {MessageEvent} e
*/
WorkerWaiter.prototype.handleChefMessage = function(e) {
switch (e.data.action) {
const r = e.data;
switch (r.action) {
case "bakeSuccess":
this.bakingComplete(e.data.data);
this.bakingComplete(r.data);
break;
case "bakeError":
this.app.handleError(e.data.data);
this.app.handleError(r.data);
this.setBakingStatus(false);
break;
case "silentBakeComplete":
Expand All @@ -55,10 +56,13 @@ WorkerWaiter.prototype.handleChefMessage = function(e) {
this.app.loaded();
break;
case "statusMessage":
this.manager.output.setStatusMsg(e.data.data);
this.manager.output.setStatusMsg(r.data);
break;
case "optionUpdate":
this.app.options[r.data.option] = r.data.value;
break;
case "highlightsCalculated":
this.manager.highlighter.displayHighlights(e.data.data.pos, e.data.data.direction);
this.manager.highlighter.displayHighlights(r.data.pos, r.data.direction);
break;
default:
console.error("Unrecognised message from ChefWorker", e);
Expand Down Expand Up @@ -104,7 +108,6 @@ WorkerWaiter.prototype.bakingComplete = function(response) {
this.app.handleError(response.error);
}

this.app.options = response.options;
this.app.dishStr = response.type === "html" ? Utils.stripHtmlTags(response.result, true) : response.result;
this.app.progress = response.progress;
this.manager.recipe.updateBreakpointIndicator(response.progress);
Expand Down

0 comments on commit f6b52b7

Please sign in to comment.