From 9cabb1126d0e6bd6ac0267e5da3d7e871df6cd2b Mon Sep 17 00:00:00 2001 From: George J Date: Sun, 6 May 2018 15:42:30 +0100 Subject: [PATCH] Adds Base85 operation to resolve #286 --- src/core/config/OperationConfig.js | 2 +- src/core/operations/Base85.js | 22 ++++++++++++---------- 2 files changed, 13 insertions(+), 11 deletions(-) diff --git a/src/core/config/OperationConfig.js b/src/core/config/OperationConfig.js index 4ca00f48..cd6cd1b8 100644 --- a/src/core/config/OperationConfig.js +++ b/src/core/config/OperationConfig.js @@ -1,8 +1,8 @@ import Arithmetic from "../operations/Arithmetic.js"; import Base from "../operations/Base.js"; import Base58 from "../operations/Base58.js"; -import Base85 from "../operations/Base85.js"; import Base64 from "../operations/Base64.js"; +import Base85 from "../operations/Base85.js"; import BCD from "../operations/BCD.js"; import BitwiseOp from "../operations/BitwiseOp.js"; import ByteRepr from "../operations/ByteRepr.js"; diff --git a/src/core/operations/Base85.js b/src/core/operations/Base85.js index 2be772c6..d8a4288b 100644 --- a/src/core/operations/Base85.js +++ b/src/core/operations/Base85.js @@ -1,6 +1,3 @@ -import Utils from "../Utils.js"; - - /** * Base85 operations. * @@ -50,7 +47,9 @@ const Base85 = { encoding = Base85._alphabetName(alphabet), result = ""; - alphabet = Utils.expandAlphRange(alphabet).join(""); + if (alphabet.length !== 85 || [].unique.call(alphabet).length !== 85) { + throw ("Alphabet must be of length 85"); + } let block; for (let i = 0; i < input.length; i += 4) { @@ -61,7 +60,7 @@ const Base85 = { ((input[i + 3] || 0)) ) >>> 0; - if (block > 0) { + if (encoding !== "Standard" || block > 0) { let digits = []; for (let j = 0; j < 5; j++) { digits.push(block % 85); @@ -73,10 +72,10 @@ const Base85 = { if (input.length < i + 4) { digits.splice(input.length - (i + 4), 4); } - + result += digits.map(digit => alphabet[digit]).join(""); } else { - if (encoding === "Standard") result += "z"; + result += (encoding === "Standard") ? "z" : null; } } @@ -98,18 +97,21 @@ const Base85 = { encoding = Base85._alphabetName(alphabet), result = []; + if (alphabet.length !== 85 || [].unique.call(alphabet).length !== 85) { + throw ("Alphabet must be of length 85"); + } + let matches = input.match(/<~(.+?)~>/); if (matches !== null) input = matches[1]; - alphabet = Utils.expandAlphRange(alphabet).join(""); - let i = 0; - let digits, block, blockBytes; + let block, blockBytes; while (i < input.length) { if (encoding === "Standard" && input[i] === "z") { result.push(0, 0, 0, 0); i++; } else { + let digits = []; digits = input .substr(i, 5) .split("")