From 2c08238259c15ea808e9009c73dbd18130756521 Mon Sep 17 00:00:00 2001 From: Varun Patil Date: Mon, 19 Oct 2020 12:04:42 +0530 Subject: [PATCH] Fix broken base64 function --- extra/js/drive.js | 20 ++++++++++++++++++++ extra/shell.html | 6 ++++-- 2 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 extra/js/drive.js diff --git a/extra/js/drive.js b/extra/js/drive.js new file mode 100644 index 0000000..54391c3 --- /dev/null +++ b/extra/js/drive.js @@ -0,0 +1,20 @@ +// https://stackoverflow.com/a/9458996 +function _bytesToBase64(bytes) { + var binary = ''; + var len = bytes.byteLength; + for (var i = 0; i < len; i++) { + binary += String.fromCharCode(bytes[i]); + } + return window.btoa(binary); +} + +// https://stackoverflow.com/a/21797381 +function _base64ToBytes(base64) { + var binary_string = window.atob(base64); + var len = binary_string.length; + var bytes = new Uint8Array(len); + for (var i = 0; i < len; i++) { + bytes[i] = binary_string.charCodeAt(i); + } + return bytes; +} diff --git a/extra/shell.html b/extra/shell.html index 6876732..ff790a8 100644 --- a/extra/shell.html +++ b/extra/shell.html @@ -3,8 +3,10 @@ + + MKXP @@ -176,7 +178,7 @@ window.saveFile = function(filename) { const buf = FS.readFile('/game/' + filename); - const b64 = btoa(String.fromCharCode.apply(null, buf)); + const b64 = _bytesToBase64(buf); localforage.setItem(namespace + filename, b64); localforage.getItem(namespace, function(err, res) { @@ -198,7 +200,7 @@ localforage.getItem(namespace + key, (err, res) => { if (err) return; - const buf = new Uint8Array(atob(res).split('').map((c) => c.charCodeAt(0))); + const buf = _base64ToBytes(res); FS.writeFile('/game/' + key, buf); }); });