From 6017578964d900452e9e28e12300c0c0abf8b56f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Date: Sat, 11 Sep 2021 17:03:59 +0200 Subject: [PATCH 1/2] Add Base45 operations MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Closes #1219 Co-developed-by: Cyril Delétré --- src/core/Utils.mjs | 24 +++++++ src/core/config/Categories.json | 2 + src/core/operations/FromBase45.mjs | 78 ++++++++++++++++++++++ src/core/operations/ToBase45.mjs | 74 +++++++++++++++++++++ tests/operations/index.mjs | 1 + tests/operations/tests/Base45.mjs | 101 +++++++++++++++++++++++++++++ 6 files changed, 280 insertions(+) create mode 100644 src/core/operations/FromBase45.mjs create mode 100644 src/core/operations/ToBase45.mjs create mode 100644 tests/operations/tests/Base45.mjs diff --git a/src/core/Utils.mjs b/src/core/Utils.mjs index ed29cebad..c775aea2c 100755 --- a/src/core/Utils.mjs +++ b/src/core/Utils.mjs @@ -1206,6 +1206,30 @@ class Utils { }[token]; } + /** + * Iterate object in chunks of given size. + * + * @param {Iterable} iterable + * @param {number} chunksize + */ + static* chunked(iterable, chunksize) { + const iterator = iterable[Symbol.iterator](); + while (true) { + const res = []; + for (let i = 0; i < chunksize; i++) { + const next = iterator.next(); + if (next.done) { + break; + } + res.push(next.value); + } + if (res.length) { + yield res; + } else { + return; + } + } + } } /** diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 09ee8d15b..2cb9ab0f3 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -20,6 +20,8 @@ "From Octal", "To Base32", "From Base32", + "To Base45", + "From Base45", "To Base58", "From Base58", "To Base62", diff --git a/src/core/operations/FromBase45.mjs b/src/core/operations/FromBase45.mjs new file mode 100644 index 000000000..8e78ed0c9 --- /dev/null +++ b/src/core/operations/FromBase45.mjs @@ -0,0 +1,78 @@ +/** + * @author Thomas Weißschuh [thomas@t-8ch.de] + * @copyright Crown Copyright 2021 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; +import Utils from "../Utils.mjs"; + + +/** + * From Base45 operation + */ +class FromBase45 extends Operation { + + /** + * FromBase45 constructor + */ + constructor() { + super(); + + this.name = "From Base45"; + this.module = "Default"; + this.description = "Base45 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. The high number base results in shorter strings than with the decimal or hexadecimal system. Base45 is optimized for usage with QR codes."; + this.infoURL = "https://wikipedia.org/wiki/List_of_numeral_systems"; + this.inputType = "string"; + this.outputType = "byteArray"; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {byteArray} + */ + run(input, args) { + const alphabet = Utils.expandAlphRange("0-9A-Z $%*+-./:").join(""); + if (!input) return []; + + const res = []; + + for (const triple of Utils.chunked(input, 3)) { + triple.reverse(); + let b = 0; + for (const c of triple) { + const idx = alphabet.indexOf(c); + if (idx === -1) { + throw new OperationError(`Character not in alphabet: '${c}'`); + } + b *= 45; + b += idx; + } + + if (b > 65535) { + throw new OperationError(`Triplet too large: '${triple.join("")}'`); + } + + if (triple.length > 2) { + /** + * The last triple may only have 2 bytes so we push the MSB when we got 3 bytes + * Pushing MSB + */ + res.push(b >> 8); + } + + /** + * Pushing LSB + */ + res.push(b & 0xff); + + } + + return res; + } + +} + +export default FromBase45; diff --git a/src/core/operations/ToBase45.mjs b/src/core/operations/ToBase45.mjs new file mode 100644 index 000000000..019c3dd95 --- /dev/null +++ b/src/core/operations/ToBase45.mjs @@ -0,0 +1,74 @@ +/** + * @author Thomas Weißschuh [thomas@t-8ch.de] + * @copyright Crown Copyright 2021 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import Utils from "../Utils.mjs"; + +/** + * To Base45 operation + */ +class ToBase45 extends Operation { + + /** + * ToBase45 constructor + */ + constructor() { + super(); + + this.name = "To Base45"; + this.module = "Default"; + this.description = "Base45 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. The high number base results in shorter strings than with the decimal or hexadecimal system. Base45 is optimized for usage with QR codes."; + this.infoURL = "https://wikipedia.org/wiki/List_of_numeral_systems"; + this.inputType = "ArrayBuffer"; + this.outputType = "string"; + this.args = [ + { + name: "Alphabet", + type: "string", + value: "0-9A-Za-z" + } + ]; + } + + /** + * @param {ArrayBuffer} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + const alphabet = Utils.expandAlphRange("0-9A-Z $%*+-./:").join(""); + input = new Uint8Array(input); + if (!input) return ""; + + const res = []; + + for (const pair of Utils.chunked(input, 2)) { + let b = 0; + for (const e of pair) { + b *= 256; + b += e; + } + + let chars = 0; + do { + res.push(alphabet[b % 45]); + chars++; + b = Math.floor(b / 45); + } while (b > 0); + + if (chars < 2) { + res.push("0"); + } + } + + + return res.join(""); + + } + +} + +export default ToBase45; diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index 9add20b9b..75fe6ef9b 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -20,6 +20,7 @@ import TestRegister from "../lib/TestRegister.mjs"; import "./tests/BCD.mjs"; import "./tests/BSON.mjs"; import "./tests/BaconCipher.mjs"; +import "./tests/Base45.mjs"; import "./tests/Base58.mjs"; import "./tests/Base64.mjs"; import "./tests/Base62.mjs"; diff --git a/tests/operations/tests/Base45.mjs b/tests/operations/tests/Base45.mjs new file mode 100644 index 000000000..1e821c6d9 --- /dev/null +++ b/tests/operations/tests/Base45.mjs @@ -0,0 +1,101 @@ +/** + * Base45 tests. + * + * @author Thomas Weißschuh [thomas@t-8ch.de] + * + * @copyright Crown Copyright 2021 + * @license Apache-2.0 + */ + +import TestRegister from "../../lib/TestRegister.mjs"; + +TestRegister.addTests([ + { + name: "To Base45: nothing", + input: "", + expectedOutput: "", + recipeConfig: [ + { + op: "To Base45", + args: [], + }, + ], + }, + { + name: "To Base45: Spec encoding example 1", + input: "AB", + expectedOutput: "BB8", + recipeConfig: [ + { + op: "To Base45", + args: [], + }, + ], + }, + { + name: "To Base45: Spec encoding example 2", + input: "Hello!!", + expectedOutput: "%69 VD92EX0", + recipeConfig: [ + { + op: "To Base45", + args: [], + }, + ], + }, + { + name: "To Base45: Spec encoding example 3", + input: "base-45", + expectedOutput: "UJCLQE7W581", + recipeConfig: [ + { + op: "To Base45", + args: [], + }, + ], + }, + { + name: "From Base45: nothing", + input: "", + expectedOutput: "", + recipeConfig: [ + { + op: "From Base45", + args: [], + }, + ], + }, + { + name: "From Base45: Spec decoding example 1", + input: "QED8WEX0", + expectedOutput: "ietf!", + recipeConfig: [ + { + op: "From Base45", + args: [], + }, + ], + }, + { + name: "From Base45: Invalid character", + input: "!", + expectedOutput: "Character not in alphabet: '!'", + recipeConfig: [ + { + op: "From Base45", + args: [], + }, + ], + }, + { + name: "From Base45: Invalid triplet value", + input: "ZZZ", + expectedOutput: "Triplet too large: 'ZZZ'", + recipeConfig: [ + { + op: "From Base45", + args: [], + }, + ], + }, +]); From 7db1f394731c803c0b7f3c9c1555dc8b9bdf710e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Wei=C3=9Fschuh?= Date: Fri, 17 Sep 2021 08:32:08 +0200 Subject: [PATCH 2/2] base45: Implement highlighting --- src/core/lib/Base45.mjs | 27 +++++++++++++++++++++++++++ src/core/operations/FromBase45.mjs | 7 +++++-- src/core/operations/ToBase45.mjs | 7 +++++-- 3 files changed, 37 insertions(+), 4 deletions(-) create mode 100644 src/core/lib/Base45.mjs diff --git a/src/core/lib/Base45.mjs b/src/core/lib/Base45.mjs new file mode 100644 index 000000000..d3e279fab --- /dev/null +++ b/src/core/lib/Base45.mjs @@ -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 $%*+-./:"; diff --git a/src/core/operations/FromBase45.mjs b/src/core/operations/FromBase45.mjs index 8e78ed0c9..890d35d62 100644 --- a/src/core/operations/FromBase45.mjs +++ b/src/core/operations/FromBase45.mjs @@ -4,6 +4,7 @@ * @license Apache-2.0 */ +import {ALPHABET, highlightToBase45, highlightFromBase45} from "../lib/Base45.mjs"; import Operation from "../Operation.mjs"; import OperationError from "../errors/OperationError.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.inputType = "string"; this.outputType = "byteArray"; + + this.highlight = highlightFromBase45; + this.highlightReverse = highlightToBase45; } /** @@ -34,7 +38,6 @@ class FromBase45 extends Operation { * @returns {byteArray} */ run(input, args) { - const alphabet = Utils.expandAlphRange("0-9A-Z $%*+-./:").join(""); if (!input) return []; const res = []; @@ -43,7 +46,7 @@ class FromBase45 extends Operation { triple.reverse(); let b = 0; for (const c of triple) { - const idx = alphabet.indexOf(c); + const idx = ALPHABET.indexOf(c); if (idx === -1) { throw new OperationError(`Character not in alphabet: '${c}'`); } diff --git a/src/core/operations/ToBase45.mjs b/src/core/operations/ToBase45.mjs index 019c3dd95..d7455dcac 100644 --- a/src/core/operations/ToBase45.mjs +++ b/src/core/operations/ToBase45.mjs @@ -4,6 +4,7 @@ * @license Apache-2.0 */ +import {ALPHABET, highlightToBase45, highlightFromBase45} from "../lib/Base45.mjs"; import Operation from "../Operation.mjs"; import Utils from "../Utils.mjs"; @@ -31,6 +32,9 @@ class ToBase45 extends Operation { value: "0-9A-Za-z" } ]; + + this.highlight = highlightToBase45; + this.highlightReverse = highlightFromBase45; } /** @@ -39,7 +43,6 @@ class ToBase45 extends Operation { * @returns {string} */ run(input, args) { - const alphabet = Utils.expandAlphRange("0-9A-Z $%*+-./:").join(""); input = new Uint8Array(input); if (!input) return ""; @@ -54,7 +57,7 @@ class ToBase45 extends Operation { let chars = 0; do { - res.push(alphabet[b % 45]); + res.push(ALPHABET[b % 45]); chars++; b = Math.floor(b / 45); } while (b > 0);