This repository was archived by the owner on Mar 27, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathJavyInputAppendix.js
143 lines (127 loc) · 4.31 KB
/
JavyInputAppendix.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// 这是在编译后附加到Artifact后面的额外内容
// 使得Javy可以将其编译成WASM
/*
传入的参数应当是JSON
{
"method":"", // NEXT | OLD
"inputType":"", // TEXT | UINT8
"outputType":"", // TEXT | UINT8
"input":"", //输入的数据,如果是TEXT请直接输入纯文本,如果是任意字节,请输入Base64编码字符串
"mode":"", // ENCRYPT | DECRYPT | AUTO // AUTO 仅在 method 指定 OLD 时合法
"key":"", //加密密钥,一个字符串 //如果缺省,自动使用默认值
"q":bool, //OLD模式下,决定是否添加标志位 | NEXT模式下,决定输出密文是否有标点符号
"r":number, //仅NEXT模式下需要:算法的随机程度,越大随机性越强,默认 50,最大100,超过100将会出错;
}
*/
function base64ToUint8Array(base64) {
// 将Base64字符串转换为二进制字符串
const binaryString = _atob(base64);
// 将二进制字符串转换为Uint8Array
const len = binaryString.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binaryString.charCodeAt(i);
}
return bytes;
}
function uint8ArrayToBase64(uint8Array) {
return _btoa(String.fromCharCode.apply(null, uint8Array));
}
// Read input from stdin
const Javyinput = JavyreadInput();
// Call the function with the input
const Javyresult = index(Javyinput);
// Write the result to stdout
JavywriteOutput(Javyresult);
// The main function.
function index(input) {
if(input === "ERROR"){
return "INCORRECT JSON";
}
if(input.method == "NEXT"){
if(input.inputType == "TEXT"){
let Abra = new Abracadabra(input.inputType,input.outputType);
Abra.Input_Next(input.input,input.mode,input.key,input.q,input.r);
let Output = Abra.Output();
if(input.outputType == "UINT8"){
Output = uint8ArrayToBase64(Output);
}
return Output;
}else if(input.inputType == "UINT8"){
let Abra = new Abracadabra(input.inputType,input.outputType);
let UINT8In = base64ToUint8Array(input.input);
Abra.Input_Next(UINT8In,input.mode,input.key,input.q,input.r);
let Output = Abra.Output();
if(input.outputType == "UINT8"){
Output = uint8ArrayToBase64(Output);
}
return Output;
}else{
return "ERROR inputType";
}
}else if(input.method == "OLD"){
if(input.inputType == "TEXT"){
let Abra = new Abracadabra(input.inputType,input.outputType);
Abra.Input(input.input,input.mode,input.key,input.q);
let Output = Abra.Output();
if(input.outputType == "UINT8"){
Output = uint8ArrayToBase64(Output);
}
return Output;
}else if(input.inputType == "UINT8"){
let Abra = new Abracadabra(input.inputType,input.outputType);
let UINT8In = base64ToUint8Array(input.input);
Abra.Input(UINT8In,input.mode,input.key,input.q);
let Output = Abra.Output();
if(input.outputType == "UINT8"){
Output = uint8ArrayToBase64(Output);
}
return Output;
}else{
return "ERROR inputType";
}
}else{
return "ERROR method";
}
}
// Read input from stdin
function JavyreadInput() {
const chunkSize = 1024;
const inputChunks = [];
let totalBytes = 0;
// Read all the available bytes
while (1) {
const buffer = new Uint8Array(chunkSize);
// Stdin file descriptor
const fd = 0;
const bytesRead = Javy.IO.readSync(fd, buffer);
totalBytes += bytesRead;
if (bytesRead === 0) {
break;
}
inputChunks.push(buffer.subarray(0, bytesRead));
}
// Assemble input into a single Uint8Array
const { finalBuffer } = inputChunks.reduce(
(context, chunk) => {
context.finalBuffer.set(chunk, context.bufferOffset);
context.bufferOffset += chunk.length;
return context;
},
{ bufferOffset: 0, finalBuffer: new Uint8Array(totalBytes) },
);
const InputDecoded = new TextDecoder().decode(finalBuffer);
try {
return JSON.parse(InputDecoded);;
} catch {
return "ERROR";
}
}
// Write output to stdout
function JavywriteOutput(output) {
const encodedOutput = new TextEncoder().encode(JSON.stringify(output));
const buffer = new Uint8Array(encodedOutput);
// Stdout file descriptor
const fd = 1;
Javy.IO.writeSync(fd, buffer);
}