Skip to content

Commit

Permalink
fix: Fix mod.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
redpeacock78 committed Dec 29, 2021
1 parent af8ffb7 commit 3c6fb67
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions mod.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
// deno-lint-ignore-file camelcase no-inferrable-types
// Base85 Encode
export function base85encode(input: Uint8Array): string {
const bit: string = [...input].map((u: number): string =>
Number(u).toString(2).padStart(8, "0")
).join("");
const bit: string = [...input].map((u: number): string => Number(u).toString(2).padStart(8, "0")).join("");
const n: number = 32;
const mod: number = bit.length % n;
let padding_bit: string = bit;
Expand All @@ -12,19 +10,16 @@ export function base85encode(input: Uint8Array): string {
padding_bit = `${padding_bit}00`;
}
}
const base: number[][] = padding_bit.match(/.{32}/g)!.map(
(i: string): number[] => {
const con: number = 85;
const dec: number = parseInt(i, 2);
const a: number = dec % con;
const b: number = ((dec - a) / con) % con;
const c: number = ((dec - (a + b * con)) / con ** 2) % con;
const d: number = ((dec - (a + b * con + c * con ** 2)) / con ** 3) % con;
const e: number =
((dec - (a + b * con + c * con ** 2 + d * con ** 3)) / con ** 4) % con;
return [e, d, c, b, a];
},
);
const base: number[][] = padding_bit.match(/.{32}/g)!.map((i: string): number[] => {
const con: number = 85;
const dec: number = parseInt(i, 2);
const a: number = dec % con;
const b: number = ((dec - a) / con) % con;
const c: number = ((dec - (a + b * con)) / con ** 2) % con;
const d: number = ((dec - (a + b * con + c * con ** 2)) / con ** 3) % con;
const e: number = ((dec - (a + b * con + c * con ** 2 + d * con ** 3)) / con ** 4) % con;
return [e, d, c, b, a];
});
const result: string = base
.flat()
.map((i: number): string => String.fromCharCode(i + 33))
Expand All @@ -36,9 +31,8 @@ export function base85encode(input: Uint8Array): string {

// Base85 Decode
export function base85decode(str: string): Uint8Array {
if (str.match(/^<~/) && str.match(/~>$/)) {
const replaced: string = str.replace(/^<~/g, "").replace(/~>$/g, "")
.replace(/z/g, "!!!!!");
if (str.match(/^<~/) || str.match(/~>$/)) {
const replaced: string = str.replace(/^<~/g, "").replace(/~>$/g, "").replace(/z/g, "!!!!!");
const n: number = 5;
const mod: number = replaced.length % n;
const diff: number = n - mod;
Expand All @@ -56,9 +50,7 @@ export function base85decode(str: string): Uint8Array {
.map((i: string): number[] => {
return i
.match(/./g)!
.map((i: string, n: number): number =>
(i!.charCodeAt(0) - 33) * 85 ** (4 - n)
)
.map((i: string, n: number): number => (i!.charCodeAt(0) - 33) * 85 ** (4 - n))
.reduce((sum: number, elm: number): number => sum + elm + 0)
.toString(2)
.padStart(32, "0")
Expand Down

0 comments on commit 3c6fb67

Please sign in to comment.