Adds Base85 operation to resolve #286

This commit is contained in:
George J 2018-05-06 15:42:30 +01:00
parent d6fb200fb2
commit 9cabb1126d
2 changed files with 13 additions and 11 deletions

View file

@ -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";

View file

@ -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);
@ -76,7 +75,7 @@ const Base85 = {
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("")