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 Arithmetic from "../operations/Arithmetic.js";
import Base from "../operations/Base.js"; import Base from "../operations/Base.js";
import Base58 from "../operations/Base58.js"; import Base58 from "../operations/Base58.js";
import Base85 from "../operations/Base85.js";
import Base64 from "../operations/Base64.js"; import Base64 from "../operations/Base64.js";
import Base85 from "../operations/Base85.js";
import BCD from "../operations/BCD.js"; import BCD from "../operations/BCD.js";
import BitwiseOp from "../operations/BitwiseOp.js"; import BitwiseOp from "../operations/BitwiseOp.js";
import ByteRepr from "../operations/ByteRepr.js"; import ByteRepr from "../operations/ByteRepr.js";

View file

@ -1,6 +1,3 @@
import Utils from "../Utils.js";
/** /**
* Base85 operations. * Base85 operations.
* *
@ -50,7 +47,9 @@ const Base85 = {
encoding = Base85._alphabetName(alphabet), encoding = Base85._alphabetName(alphabet),
result = ""; result = "";
alphabet = Utils.expandAlphRange(alphabet).join(""); if (alphabet.length !== 85 || [].unique.call(alphabet).length !== 85) {
throw ("Alphabet must be of length 85");
}
let block; let block;
for (let i = 0; i < input.length; i += 4) { for (let i = 0; i < input.length; i += 4) {
@ -61,7 +60,7 @@ const Base85 = {
((input[i + 3] || 0)) ((input[i + 3] || 0))
) >>> 0; ) >>> 0;
if (block > 0) { if (encoding !== "Standard" || block > 0) {
let digits = []; let digits = [];
for (let j = 0; j < 5; j++) { for (let j = 0; j < 5; j++) {
digits.push(block % 85); digits.push(block % 85);
@ -73,10 +72,10 @@ const Base85 = {
if (input.length < i + 4) { if (input.length < i + 4) {
digits.splice(input.length - (i + 4), 4); digits.splice(input.length - (i + 4), 4);
} }
result += digits.map(digit => alphabet[digit]).join(""); result += digits.map(digit => alphabet[digit]).join("");
} else { } else {
if (encoding === "Standard") result += "z"; result += (encoding === "Standard") ? "z" : null;
} }
} }
@ -98,18 +97,21 @@ const Base85 = {
encoding = Base85._alphabetName(alphabet), encoding = Base85._alphabetName(alphabet),
result = []; result = [];
if (alphabet.length !== 85 || [].unique.call(alphabet).length !== 85) {
throw ("Alphabet must be of length 85");
}
let matches = input.match(/<~(.+?)~>/); let matches = input.match(/<~(.+?)~>/);
if (matches !== null) input = matches[1]; if (matches !== null) input = matches[1];
alphabet = Utils.expandAlphRange(alphabet).join("");
let i = 0; let i = 0;
let digits, block, blockBytes; let block, blockBytes;
while (i < input.length) { while (i < input.length) {
if (encoding === "Standard" && input[i] === "z") { if (encoding === "Standard" && input[i] === "z") {
result.push(0, 0, 0, 0); result.push(0, 0, 0, 0);
i++; i++;
} else { } else {
let digits = [];
digits = input digits = input
.substr(i, 5) .substr(i, 5)
.split("") .split("")