Skip to content

Commit 13f07ab

Browse files
committed
Highlighting now works with the web worker
1 parent 8c960f0 commit 13f07ab

File tree

10 files changed

+200
-95
lines changed

10 files changed

+200
-95
lines changed

src/core/Chef.js

+34
Original file line numberDiff line numberDiff line change
@@ -123,4 +123,38 @@ Chef.prototype.silentBake = function(recipeConfig) {
123123
return new Date().getTime() - startTime;
124124
};
125125

126+
127+
/**
128+
* Calculates highlight offsets if possible.
129+
*
130+
* @param {Object[]} recipeConfig
131+
* @param {string} direction
132+
* @param {Object} pos - The position object for the highlight.
133+
* @param {number} pos.start - The start offset.
134+
* @param {number} pos.end - The end offset.
135+
* @returns {Object}
136+
*/
137+
Chef.prototype.calculateHighlights = function(recipeConfig, direction, pos) {
138+
const recipe = new Recipe(recipeConfig);
139+
const highlights = recipe.generateHighlightList();
140+
141+
if (!highlights) return false;
142+
143+
for (let i = 0; i < highlights.length; i++) {
144+
// Remove multiple highlights before processing again
145+
pos = [pos[0]];
146+
147+
const func = direction === "forward" ? highlights[i].f : highlights[i].b;
148+
149+
if (typeof func == "function") {
150+
pos = func(pos, highlights[i].args);
151+
}
152+
}
153+
154+
return {
155+
pos: pos,
156+
direction: direction
157+
};
158+
};
159+
126160
export default Chef;

src/core/ChefWorker.js

+26
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ self.addEventListener("message", function(e) {
5353
// imported into an inline worker.
5454
self.docURL = e.data.data;
5555
break;
56+
case "highlight":
57+
calculateHighlights(
58+
e.data.data.recipeConfig,
59+
e.data.data.direction,
60+
e.data.data.pos
61+
);
62+
break;
5663
default:
5764
break;
5865
}
@@ -121,6 +128,25 @@ function loadRequiredModules(recipeConfig) {
121128
}
122129

123130

131+
/**
132+
* Calculates highlight offsets if possible.
133+
*
134+
* @param {Object[]} recipeConfig
135+
* @param {string} direction
136+
* @param {Object} pos - The position object for the highlight.
137+
* @param {number} pos.start - The start offset.
138+
* @param {number} pos.end - The end offset.
139+
*/
140+
function calculateHighlights(recipeConfig, direction, pos) {
141+
pos = self.chef.calculateHighlights(recipeConfig, direction, pos);
142+
143+
self.postMessage({
144+
action: "highlightsCalculated",
145+
data: pos
146+
});
147+
}
148+
149+
124150
/**
125151
* Send status update to the app.
126152
*

src/core/Operation.js

+8
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@ Operation.prototype._parseConfig = function(operationConfig) {
5454
const ingredient = new Ingredient(ingredientConfig);
5555
this.addIngredient(ingredient);
5656
}
57+
58+
if (this.highlight === "func") {
59+
this.highlight = OpModules[this.module][`${this.name}-highlight`];
60+
}
61+
62+
if (this.highlightReverse === "func") {
63+
this.highlightReverse = OpModules[this.module][`${this.name}-highlightReverse`];
64+
}
5765
};
5866

5967

src/core/Recipe.js

+33
Original file line numberDiff line numberDiff line change
@@ -215,4 +215,37 @@ Recipe.prototype.fromString = function(recipeStr) {
215215
this._parseConfig(recipeConfig);
216216
};
217217

218+
219+
/**
220+
* Generates a list of all the highlight functions assigned to operations in the recipe, if the
221+
* entire recipe supports highlighting.
222+
*
223+
* @returns {Object[]} highlights
224+
* @returns {function} highlights[].f
225+
* @returns {function} highlights[].b
226+
* @returns {Object[]} highlights[].args
227+
*/
228+
Recipe.prototype.generateHighlightList = function() {
229+
const highlights = [];
230+
231+
for (let i = 0; i < this.opList.length; i++) {
232+
let op = this.opList[i];
233+
if (op.isDisabled()) continue;
234+
235+
// If any breakpoints are set, do not attempt to highlight
236+
if (op.isBreakpoint()) return false;
237+
238+
// If any of the operations do not support highlighting, fail immediately.
239+
if (op.highlight === false || op.highlight === undefined) return false;
240+
241+
highlights.push({
242+
f: op.highlight,
243+
b: op.highlightReverse,
244+
args: op.getIngValues()
245+
});
246+
}
247+
248+
return highlights;
249+
};
250+
218251
export default Recipe;

src/core/config/OperationConfig.js

+23-23
Original file line numberDiff line numberDiff line change
@@ -184,8 +184,8 @@ const OperationConfig = {
184184
"From Base64": {
185185
module: "Default",
186186
description: "Base64 is a notation for encoding arbitrary byte data using a restricted set of symbols that can be conveniently used by humans and processed by computers.<br><br>This operation decodes data from an ASCII Base64 string back into its raw format.<br><br>e.g. <code>aGVsbG8=</code> becomes <code>hello</code>",
187-
highlight: Base64.highlightFrom,
188-
highlightReverse: Base64.highlightTo,
187+
highlight: "func",
188+
highlightReverse: "func",
189189
inputType: "string",
190190
outputType: "byteArray",
191191
args: [
@@ -195,7 +195,7 @@ const OperationConfig = {
195195
value: Base64.ALPHABET_OPTIONS
196196
},
197197
{
198-
name: "Remove non&#8209;alphabet chars",
198+
name: "Remove non-alphabet chars",
199199
type: "boolean",
200200
value: Base64.REMOVE_NON_ALPH_CHARS
201201
}
@@ -204,8 +204,8 @@ const OperationConfig = {
204204
"To Base64": {
205205
module: "Default",
206206
description: "Base64 is a notation for encoding arbitrary byte data using a restricted set of symbols that can be conveniently used by humans and processed by computers.<br><br>This operation encodes data in an ASCII Base64 string.<br><br>e.g. <code>hello</code> becomes <code>aGVsbG8=</code>",
207-
highlight: Base64.highlightTo,
208-
highlightReverse: Base64.highlightFrom,
207+
highlight: "func",
208+
highlightReverse: "func",
209209
inputType: "byteArray",
210210
outputType: "string",
211211
args: [
@@ -228,7 +228,7 @@ const OperationConfig = {
228228
value: Base58.ALPHABET_OPTIONS
229229
},
230230
{
231-
name: "Remove non&#8209;alphabet chars",
231+
name: "Remove non-alphabet chars",
232232
type: "boolean",
233233
value: Base58.REMOVE_NON_ALPH_CHARS
234234
}
@@ -259,7 +259,7 @@ const OperationConfig = {
259259
value: Base64.BASE32_ALPHABET
260260
},
261261
{
262-
name: "Remove non&#8209;alphabet chars",
262+
name: "Remove non-alphabet chars",
263263
type: "boolean",
264264
value: Base64.REMOVE_NON_ALPH_CHARS
265265
}
@@ -446,8 +446,8 @@ const OperationConfig = {
446446
"From Hex": {
447447
module: "Default",
448448
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>",
449-
highlight: ByteRepr.highlightFrom,
450-
highlightReverse: ByteRepr.highlightTo,
449+
highlight: "func",
450+
highlightReverse: "func",
451451
inputType: "string",
452452
outputType: "byteArray",
453453
args: [
@@ -461,8 +461,8 @@ const OperationConfig = {
461461
"To Hex": {
462462
module: "Default",
463463
description: "Converts the input string to hexadecimal bytes separated by the specified delimiter.<br><br>e.g. The UTF-8 encoded string <code>Γειά σου</code> becomes <code>ce 93 ce b5 ce b9 ce ac 20 cf 83 ce bf cf 85 0a</code>",
464-
highlight: ByteRepr.highlightTo,
465-
highlightReverse: ByteRepr.highlightFrom,
464+
highlight: "func",
465+
highlightReverse: "func",
466466
inputType: "byteArray",
467467
outputType: "string",
468468
args: [
@@ -506,8 +506,8 @@ const OperationConfig = {
506506
"From Charcode": {
507507
module: "Default",
508508
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>",
509-
highlight: ByteRepr.highlightFrom,
510-
highlightReverse: ByteRepr.highlightTo,
509+
highlight: "func",
510+
highlightReverse: "func",
511511
inputType: "string",
512512
outputType: "byteArray",
513513
args: [
@@ -527,8 +527,8 @@ const OperationConfig = {
527527
"To Charcode": {
528528
module: "Default",
529529
description: "Converts text to its unicode character code equivalent.<br><br>e.g. <code>Γειά σου</code> becomes <code>0393 03b5 03b9 03ac 20 03c3 03bf 03c5</code>",
530-
highlight: ByteRepr.highlightTo,
531-
highlightReverse: ByteRepr.highlightFrom,
530+
highlight: "func",
531+
highlightReverse: "func",
532532
inputType: "string",
533533
outputType: "string",
534534
args: [
@@ -547,8 +547,8 @@ const OperationConfig = {
547547
"From Binary": {
548548
module: "Default",
549549
description: "Converts a binary string back into its raw form.<br><br>e.g. <code>01001000 01101001</code> becomes <code>Hi</code>",
550-
highlight: ByteRepr.highlightFromBinary,
551-
highlightReverse: ByteRepr.highlightToBinary,
550+
highlight: "func",
551+
highlightReverse: "func",
552552
inputType: "string",
553553
outputType: "byteArray",
554554
args: [
@@ -562,8 +562,8 @@ const OperationConfig = {
562562
"To Binary": {
563563
module: "Default",
564564
description: "Displays the input data as a binary string.<br><br>e.g. <code>Hi</code> becomes <code>01001000 01101001</code>",
565-
highlight: ByteRepr.highlightToBinary,
566-
highlightReverse: ByteRepr.highlightFromBinary,
565+
highlight: "func",
566+
highlightReverse: "func",
567567
inputType: "byteArray",
568568
outputType: "string",
569569
args: [
@@ -603,17 +603,17 @@ const OperationConfig = {
603603
"From Hexdump": {
604604
module: "Default",
605605
description: "Attempts to convert a hexdump back into raw data. This operation supports many different hexdump variations, but probably not all. Make sure you verify that the data it gives you is correct before continuing analysis.",
606-
highlight: Hexdump.highlightFrom,
607-
highlightReverse: Hexdump.highlightTo,
606+
highlight: "func",
607+
highlightReverse: "func",
608608
inputType: "string",
609609
outputType: "byteArray",
610610
args: []
611611
},
612612
"To Hexdump": {
613613
module: "Default",
614614
description: "Creates a hexdump of the input data, displaying both the hexadecimal values of each byte and an ASCII representation alongside.",
615-
highlight: Hexdump.highlightTo,
616-
highlightReverse: Hexdump.highlightFrom,
615+
highlight: "func",
616+
highlightReverse: "func",
617617
inputType: "byteArray",
618618
outputType: "string",
619619
args: [

src/core/config/modules/Default.js

+28
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,34 @@ OpModules.Default = {
158158
"Conditional Jump": FlowControl.runCondJump,
159159
"Return": FlowControl.runReturn,
160160
"Comment": FlowControl.runComment,
161+
162+
163+
/*
164+
Highlighting functions.
165+
166+
This is a temporary solution as highlighting should be entirely
167+
overhauled at some point.
168+
*/
169+
"From Base64-highlight": Base64.highlightFrom,
170+
"From Base64-highlightReverse": Base64.highlightTo,
171+
"To Base64-highlight": Base64.highlightTo,
172+
"To Base64-highlightReverse": Base64.highlightFrom,
173+
"From Hex-highlight": ByteRepr.highlightFrom,
174+
"From Hex-highlightReverse": ByteRepr.highlightTo,
175+
"To Hex-highlight": ByteRepr.highlightTo,
176+
"To Hex-highlightReverse": ByteRepr.highlightFrom,
177+
"From Charcode-highlight": ByteRepr.highlightFrom,
178+
"From Charcode-highlightReverse": ByteRepr.highlightTo,
179+
"To Charcode-highlight": ByteRepr.highlightTo,
180+
"To Charcode-highlightReverse": ByteRepr.highlightFrom,
181+
"From Binary-highlight": ByteRepr.highlightFromBinary,
182+
"From Binary-highlightReverse": ByteRepr.highlightToBinary,
183+
"To Binary-highlight": ByteRepr.highlightToBinary,
184+
"To Binary-highlightReverse": ByteRepr.highlightFromBinary,
185+
"From Hexdump-highlight": Hexdump.highlightFrom,
186+
"From Hexdump-highlightReverse": Hexdump.highlightTo,
187+
"To Hexdump-highlight": Hexdump.highlightTo,
188+
"To Hexdump-highlightReverse": Hexdump.highlightFrom,
161189
};
162190

163191
export default OpModules;

src/core/operations/Hexdump.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const Hexdump = {
9292
const w = (width - 13) / 4;
9393
// w should be the specified width of the hexdump and therefore a round number
9494
if (Math.floor(w) !== w || input.indexOf("\r") !== -1 || output.indexOf(13) !== -1) {
95-
if (self) self.setOption("attemptHighlight", false);
95+
//TODO if (self) self.setOption("attemptHighlight", false);
9696
}
9797
return output;
9898
},

0 commit comments

Comments
 (0)