-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
247 lines (206 loc) · 6.57 KB
/
main.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
var stderr = "";
var Module = {
// Hook to capture the stderr stream
printErr: line => { stderr += line + '\n'; }
};
async function outguess(input_file, parameters) {
// Clear stderr
stderr = "";
let image_filename = allocateUTF8(input_file.name);
let image_data_size = input_file.size;
let image_data_ptr = Module._malloc(image_data_size);
let image_data = new Uint8Array(await input_file.arrayBuffer());
for (let i = 0; i < image_data_size; i++)
setValue(image_data_ptr + i, image_data[i], "i8");
let key_ptr = allocateUTF8(parameters.key);
let use_error_correction = parameters.error_correction ? 1 : 0;
let return_info = null;
try {
// outguess_main(char* image_filename, uint8_t* image_data, size_t image_data_size)
return_info = Module._outguess_main(image_filename, image_data_ptr, image_data_size, key_ptr, use_error_correction);
}
catch (e) {
if (e.name == "ExitStatus") {
// outguess just called "exit(1)". More information should be available on stderr,
// we can just return indicating a failure.
// emscriptem will append a debug message to stderr, warning about this exit.
// We make sure not to show this message.
let info = stderr;
info = info.substring(0, info.length - 1);
info = info.substring(0, info.lastIndexOf("\n"));
_free(image_filename);
_free(image_data_ptr);
_free(key_ptr);
return {
success: false,
output: null,
info: info
};
}
else if (e.name == "RuntimeError") {
return {
success: false,
output: null,
info: `Outguess crashed: ${e.message}.\nThis crash would probably have also occured when using the original command-line version.`
};
}
else {
// Unknown error, log it
console.warn(e);
}
}
let message_ptr = getValue(return_info, "i8*");
let message_length = getValue(return_info + 4, "i32");
let message_outguessed = message_ptr !== 0;
let message = null;
if (message_outguessed) {
message = new Uint8Array(message_length);
for (let i = 0; i < message_length; i++)
message[i] = getValue(message_ptr + i, "i8");
}
_free(image_filename);
_free(image_data_ptr);
_free(key_ptr);
if (message_outguessed)
_free(message_ptr);
_free(return_info);
return {
success: message_outguessed,
output: message,
info: stderr
};
}
// Detects whether a byte array can be interpreted as a string.
function is_printable(array) {
try {
let strict_decoder = new TextDecoder('utf8', {
fatal: true
});
strict_decoder.decode(array);
return true;
} catch (e) {
return false;
}
}
function decode_array(array) {
let decoder = new TextDecoder('utf8');
return decoder.decode(array);
}
class JOutguess {
constructor() {
// DOM elements
this.file_selector = null;
this.run_button = null;
this.download_button = null;
this.output_textarea = null;
// Parameters
this.input_file = null;
this.parameters = {
key: "Default key",
error_correction: false,
};
// Output
this.output_blob = null;
}
initialize() {
// DOM elements
this.file_selector = document.getElementById("file-selector");
this.run_button = document.getElementById("run-button");
this.download_button = document.getElementById("download-button");
this.output_textarea = document.getElementById("text-output");
this.info_textarea = document.getElementById("info-output");
this.key_input = document.getElementById("key");
this.error_correction_checkbox = document.getElementById("error-correction");
// Events
this.file_selector.addEventListener("input", () =>
this.process_input_files(this.file_selector.files)
);
document.body.addEventListener("drop", ev => {
ev.preventDefault();
let files = [];
if (ev.dataTransfer.items)
files = Array.from(ev.dataTransfer.items)
.filter(item => item.kind === "file")
.map(item => item.getAsFile());
else
files = ev.dataTransfer.files;
this.process_input_files(files);
});
document.body.addEventListener("dragover", ev => ev.preventDefault());
this.download_button.addEventListener("click", () =>
this.download_output()
);
this.run_button.addEventListener("click", async () =>
this.run_outguess()
);
this.key_input.addEventListener("change", () =>
this.parameters.key = this.key_input.value === "" ? "Default key" : this.key_input.value
);
this.error_correction_checkbox.addEventListener("change", () =>
this.parameters.error_correction = this.error_correction_checkbox.checked
);
// Initialization
this.clear_output();
this.process_input_files(this.file_selector.files);
console.log("Initialized!");
}
process_input_files(files) {
if (files.length == 0)
return;
if (files.length > 1) {
alert("Only one image file can be selected.");
return;
}
this.input_file = files[0];
this.run_button.removeAttribute("disabled");
this.clear_output();
}
async run_outguess() {
if (this.input_file === null)
return;
let result = await outguess(this.input_file, this.parameters);
this.set_info(result.info);
if (result.success)
this.set_output(result.output);
else
this.clear_output();
}
// Output
clear_output() {
this.output_textarea.setAttribute("disabled", "");
this.output_textarea.textContent = "";
this.download_button.setAttribute("disabled", "");
this.output_blob = null;
}
set_output(output) {
if (is_printable(output)) {
this.output_textarea.removeAttribute("disabled");
this.output_textarea.textContent = decode_array(output);
}
else {
this.output_textarea.setAttribute("disabled", "");
this.output_textarea.textContent = "Output isn't printable!";
}
this.download_button.removeAttribute("disabled");
this.output_blob = new Blob([output]);
}
clear_info() {
this.info_textarea.setAttribute("disable", "");
this.info_textarea.textContent = "";
}
set_info(info) {
this.info_textarea.removeAttribute("disabled");
this.info_textarea.textContent = info;
}
download_output() {
if (this.output_blob === null)
return;
let link = document.createElement("a");
link.download = "output";
link.href = URL.createObjectURL(this.output_blob);
link.click();
URL.revokeObjectURL(link.href);
}
}
const joutguess = new JOutguess;
document.addEventListener("DOMContentLoaded", () => joutguess.initialize());