Skip to content

Commit e18349a

Browse files
committed
Base85 Encoder and Decoder added
1 parent e725ab2 commit e18349a

File tree

2 files changed

+77
-1
lines changed

2 files changed

+77
-1
lines changed

Base85.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { Coder } from "./Coder";
2+
3+
export class Base85Encoder implements Coder {
4+
5+
from = "text";
6+
to = "base85";
7+
8+
transform(text: string): string {
9+
const encoder = new TextEncoder();
10+
const bytes = encoder.encode(text);
11+
let base85 = "";
12+
13+
for (let i = 0; i < bytes.length; i += 4) {
14+
let chunk = 0;
15+
for (let j = 0; j < 4; j++) {
16+
chunk = (chunk << 8) | (bytes[i + j] || 0);
17+
}
18+
for (let j = 4; j >= 0; j--) {
19+
base85 += String.fromCharCode((chunk / (85 ** j) % 85) + 33);
20+
}
21+
}
22+
return base85;
23+
}
24+
25+
checkInput(text: string): boolean {
26+
// Ensure the input is a non-empty string
27+
return typeof text === "string" && text.length > 0;
28+
}
29+
}
30+
31+
export class Base85Decoder implements Coder {
32+
33+
from = "base85";
34+
to = "text";
35+
36+
transform(text: string): string {
37+
if (!this.checkInput(text)) {
38+
throw new Error("Invalid Base85 input");
39+
}
40+
41+
const decoder = new TextDecoder();
42+
const chunks = text.match(/.{1,5}/g) || [];
43+
const bytes = [];
44+
45+
for (const chunk of chunks) {
46+
let value = 0;
47+
for (let i = 0; i < chunk.length; i++) {
48+
value = value * 85 + (chunk.charCodeAt(i) - 33);
49+
}
50+
51+
for (let i = 3; i >= 0; i--) {
52+
bytes.push((value >> (i * 8)) & 0xff);
53+
}
54+
}
55+
56+
return decoder.decode(new Uint8Array(bytes).filter(byte => byte !== 0));
57+
}
58+
59+
checkInput(text: string): boolean {
60+
// Check if the input is a valid Base64 string
61+
// const base64Regex = /^[A-Za-z0-9+/=]+$/;
62+
// return typeof text === "string" && base64Regex.test(text) && text.length % 4 === 0;
63+
return true;
64+
}
65+
66+
}

main.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,23 @@ import { App, MarkdownView, Plugin, MarkdownPostProcessorContext, PluginSettingT
22

33
import { Coder } from "./Coder";
44
import { Base64Encoder, Base64Decoder } from "./Base64";
5+
import { Base85Encoder, Base85Decoder } from "./Base85";
56
import { Rot13Encoder, Rot13Decoder } from "./Rot13";
67
import { AtbashEncoder, AtbashDecoder } from "./Atbash";
78

89
export default class CoderPlugin extends Plugin {
910

1011
// List of coders
11-
coders: Coder[] = [new Base64Encoder(), new Base64Decoder(), new Rot13Encoder(), new Rot13Decoder(), new AtbashEncoder(), new AtbashDecoder()];
12+
coders: Coder[] = [
13+
new Base64Encoder(),
14+
new Base64Decoder(),
15+
new Base85Encoder(),
16+
new Base85Decoder(),
17+
new Rot13Encoder(),
18+
new Rot13Decoder(),
19+
new AtbashEncoder(),
20+
new AtbashDecoder()
21+
];
1222

1323
async onload() {
1424
this.coders.forEach(coder => {

0 commit comments

Comments
 (0)