|
| 1 | +async function getLCP(go) { |
| 2 | + const WASM_URL = "lcp.wasm"; |
| 3 | + |
| 4 | + let instance; |
| 5 | + |
| 6 | + if ("instantiateStreaming" in WebAssembly) { |
| 7 | + instance = ( |
| 8 | + await WebAssembly.instantiateStreaming(fetch(WASM_URL), go.importObject) |
| 9 | + ).instance; |
| 10 | + } else { |
| 11 | + const res = await fetch(WASM_URL); |
| 12 | + if (!res.ok) throw new Error(`unexpected status code: ${res.status}`); |
| 13 | + instance = ( |
| 14 | + await WebAssembly.instantiate(await res.arrayBuffer(), go.importObject) |
| 15 | + ).instance; |
| 16 | + } |
| 17 | + |
| 18 | + go.run(instance); |
| 19 | + |
| 20 | + return instance; |
| 21 | +} |
| 22 | + |
| 23 | +/** |
| 24 | + * @param {WebAssembly.Instance} lcp |
| 25 | + * @param {string} s |
| 26 | + * @returns {number} The memory address |
| 27 | + */ |
| 28 | +function newGoString(lcp, s) { |
| 29 | + const data = new TextEncoder().encode(s); |
| 30 | + return newGoBytes(lcp, data); |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * @param {WebAssembly.Instance} lcp |
| 35 | + * @param {Uint8Array} data |
| 36 | + * @returns {number} The memory address |
| 37 | + */ |
| 38 | +function newGoBytes(lcp, data) { |
| 39 | + const { newBytes } = lcp.exports; |
| 40 | + const addr = newBytes(data.length); |
| 41 | + // Read *after* calling newBytes |
| 42 | + const u8mem = new Uint8Array(lcp.exports.memory.buffer); |
| 43 | + |
| 44 | + for (let i = 0; i < data.length; ++i) u8mem[addr + i] = data[i]; |
| 45 | + |
| 46 | + return addr; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * @param {WebAssembly.Instance} lcp |
| 51 | + * @param {number} ptr |
| 52 | + * @returns {Uint8Array | null} |
| 53 | + */ |
| 54 | +function bytesFromGo(lcp, ptr) { |
| 55 | + const { memory, bytesSize } = lcp.exports; |
| 56 | + const size = bytesSize(ptr); |
| 57 | + if (!size) return null; |
| 58 | + |
| 59 | + return new Uint8Array(memory.buffer).subarray(ptr, ptr + size); |
| 60 | +} |
| 61 | + |
| 62 | +/** |
| 63 | + * @param {WebAssembly.Instance} lcp |
| 64 | + * @param {File} file |
| 65 | + * @param {string} key |
| 66 | + */ |
| 67 | +async function decrypt(lcp, file, key) { |
| 68 | + const { decrypt, freeBytes } = lcp.exports; |
| 69 | + const fileData = new Uint8Array(await file.arrayBuffer()); |
| 70 | + console.log(fileData); |
| 71 | + const goFile = newGoBytes(lcp, fileData); |
| 72 | + const goKey = newGoString(lcp, key); |
| 73 | + let goOut; |
| 74 | + |
| 75 | + try { |
| 76 | + goOut = decrypt(goFile, goKey); |
| 77 | + const decryptedData = bytesFromGo(lcp, goOut); |
| 78 | + if (!decryptedData) throw new Error("no decrypted data"); |
| 79 | + const blob = new Blob([decryptedData], { type: "application/epub+zip" }); |
| 80 | + const link = document.createElement("a"); |
| 81 | + link.href = URL.createObjectURL(blob); |
| 82 | + link.download = `decrypted.${file.name}`; |
| 83 | + document.body.appendChild(link); |
| 84 | + link.click(); |
| 85 | + document.body.removeChild(link); |
| 86 | + URL.revokeObjectURL(link.href); |
| 87 | + } finally { |
| 88 | + freeBytes(goFile); |
| 89 | + freeBytes(goKey); |
| 90 | + if (goOut) freeBytes(goOut); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +async function main() { |
| 95 | + const go = new Go(); |
| 96 | + const lcp = await getLCP(go); |
| 97 | + |
| 98 | + const submitButton = document.getElementById("button-submit"); |
| 99 | + |
| 100 | + const resetSubmitButton = () => { |
| 101 | + submitButton.removeAttribute("disabled"); |
| 102 | + submitButton.innerText = "Decrypt"; |
| 103 | + }; |
| 104 | + |
| 105 | + let busy = false; |
| 106 | + resetSubmitButton(); |
| 107 | + |
| 108 | + document.getElementById("file-form").addEventListener("submit", (ev) => { |
| 109 | + ev.preventDefault(); |
| 110 | + const formData = new FormData(ev.target); |
| 111 | + const file = formData.get("file"); |
| 112 | + const key = formData.get("key"); |
| 113 | + |
| 114 | + if ( |
| 115 | + busy || |
| 116 | + !file || |
| 117 | + !(file instanceof File) || |
| 118 | + !key || |
| 119 | + typeof key !== "string" |
| 120 | + ) |
| 121 | + return; |
| 122 | + |
| 123 | + busy = true; |
| 124 | + submitButton.setAttribute("disabled", "1"); |
| 125 | + submitButton.innerText = "Decrypting..."; |
| 126 | + |
| 127 | + decrypt(lcp, file, key) |
| 128 | + .catch((error) => { |
| 129 | + console.log(error); |
| 130 | + alert("There was an error decrypting the file"); |
| 131 | + }) |
| 132 | + .finally(() => { |
| 133 | + busy = false; |
| 134 | + resetSubmitButton(); |
| 135 | + }); |
| 136 | + }); |
| 137 | +} |
| 138 | + |
| 139 | +main(); |
0 commit comments