mirror of
https://github.com/gchq/CyberChef
synced 2025-01-04 00:38:41 +00:00
base45: Implement highlighting
This commit is contained in:
parent
6017578964
commit
7db1f39473
3 changed files with 37 additions and 4 deletions
27
src/core/lib/Base45.mjs
Normal file
27
src/core/lib/Base45.mjs
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
/**
|
||||||
|
* Base45 resources.
|
||||||
|
*
|
||||||
|
* @author Thomas Weißschuh [thomas@t-8ch.de]
|
||||||
|
* @copyright Crown Copyright 2021
|
||||||
|
* @license Apache-2.0
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Highlight to Base45
|
||||||
|
*/
|
||||||
|
export function highlightToBase45(pos, args) {
|
||||||
|
pos[0].start = Math.floor(pos[0].start / 2) * 3;
|
||||||
|
pos[0].end = Math.ceil(pos[0].end / 2) * 3;
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Highlight from Base45
|
||||||
|
*/
|
||||||
|
export function highlightFromBase45(pos, args) {
|
||||||
|
pos[0].start = Math.floor(pos[0].start / 3) * 2;
|
||||||
|
pos[0].end = Math.ceil(pos[0].end / 3) * 2;
|
||||||
|
return pos;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ $%*+-./:";
|
|
@ -4,6 +4,7 @@
|
||||||
* @license Apache-2.0
|
* @license Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import {ALPHABET, highlightToBase45, highlightFromBase45} from "../lib/Base45.mjs";
|
||||||
import Operation from "../Operation.mjs";
|
import Operation from "../Operation.mjs";
|
||||||
import OperationError from "../errors/OperationError.mjs";
|
import OperationError from "../errors/OperationError.mjs";
|
||||||
import Utils from "../Utils.mjs";
|
import Utils from "../Utils.mjs";
|
||||||
|
@ -26,6 +27,9 @@ class FromBase45 extends Operation {
|
||||||
this.infoURL = "https://wikipedia.org/wiki/List_of_numeral_systems";
|
this.infoURL = "https://wikipedia.org/wiki/List_of_numeral_systems";
|
||||||
this.inputType = "string";
|
this.inputType = "string";
|
||||||
this.outputType = "byteArray";
|
this.outputType = "byteArray";
|
||||||
|
|
||||||
|
this.highlight = highlightFromBase45;
|
||||||
|
this.highlightReverse = highlightToBase45;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -34,7 +38,6 @@ class FromBase45 extends Operation {
|
||||||
* @returns {byteArray}
|
* @returns {byteArray}
|
||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
const alphabet = Utils.expandAlphRange("0-9A-Z $%*+-./:").join("");
|
|
||||||
if (!input) return [];
|
if (!input) return [];
|
||||||
|
|
||||||
const res = [];
|
const res = [];
|
||||||
|
@ -43,7 +46,7 @@ class FromBase45 extends Operation {
|
||||||
triple.reverse();
|
triple.reverse();
|
||||||
let b = 0;
|
let b = 0;
|
||||||
for (const c of triple) {
|
for (const c of triple) {
|
||||||
const idx = alphabet.indexOf(c);
|
const idx = ALPHABET.indexOf(c);
|
||||||
if (idx === -1) {
|
if (idx === -1) {
|
||||||
throw new OperationError(`Character not in alphabet: '${c}'`);
|
throw new OperationError(`Character not in alphabet: '${c}'`);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@
|
||||||
* @license Apache-2.0
|
* @license Apache-2.0
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import {ALPHABET, highlightToBase45, highlightFromBase45} from "../lib/Base45.mjs";
|
||||||
import Operation from "../Operation.mjs";
|
import Operation from "../Operation.mjs";
|
||||||
import Utils from "../Utils.mjs";
|
import Utils from "../Utils.mjs";
|
||||||
|
|
||||||
|
@ -31,6 +32,9 @@ class ToBase45 extends Operation {
|
||||||
value: "0-9A-Za-z"
|
value: "0-9A-Za-z"
|
||||||
}
|
}
|
||||||
];
|
];
|
||||||
|
|
||||||
|
this.highlight = highlightToBase45;
|
||||||
|
this.highlightReverse = highlightFromBase45;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -39,7 +43,6 @@ class ToBase45 extends Operation {
|
||||||
* @returns {string}
|
* @returns {string}
|
||||||
*/
|
*/
|
||||||
run(input, args) {
|
run(input, args) {
|
||||||
const alphabet = Utils.expandAlphRange("0-9A-Z $%*+-./:").join("");
|
|
||||||
input = new Uint8Array(input);
|
input = new Uint8Array(input);
|
||||||
if (!input) return "";
|
if (!input) return "";
|
||||||
|
|
||||||
|
@ -54,7 +57,7 @@ class ToBase45 extends Operation {
|
||||||
|
|
||||||
let chars = 0;
|
let chars = 0;
|
||||||
do {
|
do {
|
||||||
res.push(alphabet[b % 45]);
|
res.push(ALPHABET[b % 45]);
|
||||||
chars++;
|
chars++;
|
||||||
b = Math.floor(b / 45);
|
b = Math.floor(b / 45);
|
||||||
} while (b > 0);
|
} while (b > 0);
|
||||||
|
|
Loading…
Reference in a new issue