Path: blob/main/misc/emulator/gba/user_scripts/base64.js
28553 views
"use strict";1var toBase64 = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",2"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",3"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+" , "/", "="];4var fromBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";5function base64(data) {6try {7var base64 = window.btoa(data); //Use this native function when it's available, as it's a magnitude faster than the non-native code below.8}9catch (error) {10//Defaulting to non-native base64 encoding...11var base64 = "";12var dataLength = data.length;13if (dataLength > 0) {14var bytes = [0, 0, 0];15var index = 0;16var remainder = dataLength % 3;17while (data.length % 3 > 0) {18//Make sure we don't do fuzzy math in the next loop...19data[data.length] = " ";20}21while (index < dataLength) {22//Keep this loop small for speed.23bytes = [data.charCodeAt(index++) & 0xFF, data.charCodeAt(index++) & 0xFF, data.charCodeAt(index++) & 0xFF];24base64 += toBase64[bytes[0] >> 2] + toBase64[((bytes[0] & 0x3) << 4) | (bytes[1] >> 4)] + toBase64[((bytes[1] & 0xF) << 2) | (bytes[2] >> 6)] + toBase64[bytes[2] & 0x3F];25}26if (remainder > 0) {27//Fill in the padding and recalulate the trailing six-bit group...28base64[base64.length - 1] = "=";29if (remainder == 2) {30base64[base64.length - 2] = "=";31base64[base64.length - 3] = toBase64[(bytes[0] & 0x3) << 4];32}33else {34base64[base64.length - 2] = toBase64[(bytes[1] & 0xF) << 2];35}36}37}38}39return base64;40}41function base64_decode(data) {42try {43var decode64 = window.atob(data); //Use this native function when it's available, as it's a magnitude faster than the non-native code below.44}45catch (error) {46//Defaulting to non-native base64 decoding...47var decode64 = "";48var dataLength = data.length;49if (dataLength > 3 && dataLength % 4 == 0) {50var sixbits = [0, 0, 0, 0]; //Declare this out of the loop, to speed up the ops.51var index = 0;52while (index < dataLength) {53//Keep this loop small for speed.54sixbits = [fromBase64.indexOf(data.charAt(index++)), fromBase64.indexOf(data.charAt(index++)), fromBase64.indexOf(data.charAt(index++)), fromBase64.indexOf(data.charAt(index++))];55decode64 += String.fromCharCode((sixbits[0] << 2) | (sixbits[1] >> 4)) + String.fromCharCode(((sixbits[1] & 0x0F) << 4) | (sixbits[2] >> 2)) + String.fromCharCode(((sixbits[2] & 0x03) << 6) | sixbits[3]);56}57//Check for the '=' character after the loop, so we don't hose it up.58if (sixbits[3] >= 0x40) {59decode64.length -= 1;60if (sixbits[2] >= 0x40) {61decode64.length -= 1;62}63}64}65}66return decode64;67}68function arrayToBase64(arrayIn) {69var binString = "";70var length = arrayIn.length;71for (var index = 0; index < length; ++index) {72if (typeof arrayIn[index] == "number") {73binString += String.fromCharCode(arrayIn[index]);74}75}76return base64(binString);77}78function base64ToArray(b64String) {79var binString = base64_decode(b64String);80var outArray = [];81var length = binString.length;82for (var index = 0; index < length;) {83outArray.push(binString.charCodeAt(index++) & 0xFF);84}85return outArray;86}8788