Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
RishiRecon
GitHub Repository: RishiRecon/exploits
Path: blob/main/misc/emulator/gba/user_scripts/base64.js
28553 views
1
"use strict";
2
var 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",
3
"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",
4
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+" , "/", "="];
5
var fromBase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
6
function base64(data) {
7
try {
8
var base64 = window.btoa(data); //Use this native function when it's available, as it's a magnitude faster than the non-native code below.
9
}
10
catch (error) {
11
//Defaulting to non-native base64 encoding...
12
var base64 = "";
13
var dataLength = data.length;
14
if (dataLength > 0) {
15
var bytes = [0, 0, 0];
16
var index = 0;
17
var remainder = dataLength % 3;
18
while (data.length % 3 > 0) {
19
//Make sure we don't do fuzzy math in the next loop...
20
data[data.length] = " ";
21
}
22
while (index < dataLength) {
23
//Keep this loop small for speed.
24
bytes = [data.charCodeAt(index++) & 0xFF, data.charCodeAt(index++) & 0xFF, data.charCodeAt(index++) & 0xFF];
25
base64 += toBase64[bytes[0] >> 2] + toBase64[((bytes[0] & 0x3) << 4) | (bytes[1] >> 4)] + toBase64[((bytes[1] & 0xF) << 2) | (bytes[2] >> 6)] + toBase64[bytes[2] & 0x3F];
26
}
27
if (remainder > 0) {
28
//Fill in the padding and recalulate the trailing six-bit group...
29
base64[base64.length - 1] = "=";
30
if (remainder == 2) {
31
base64[base64.length - 2] = "=";
32
base64[base64.length - 3] = toBase64[(bytes[0] & 0x3) << 4];
33
}
34
else {
35
base64[base64.length - 2] = toBase64[(bytes[1] & 0xF) << 2];
36
}
37
}
38
}
39
}
40
return base64;
41
}
42
function base64_decode(data) {
43
try {
44
var decode64 = window.atob(data); //Use this native function when it's available, as it's a magnitude faster than the non-native code below.
45
}
46
catch (error) {
47
//Defaulting to non-native base64 decoding...
48
var decode64 = "";
49
var dataLength = data.length;
50
if (dataLength > 3 && dataLength % 4 == 0) {
51
var sixbits = [0, 0, 0, 0]; //Declare this out of the loop, to speed up the ops.
52
var index = 0;
53
while (index < dataLength) {
54
//Keep this loop small for speed.
55
sixbits = [fromBase64.indexOf(data.charAt(index++)), fromBase64.indexOf(data.charAt(index++)), fromBase64.indexOf(data.charAt(index++)), fromBase64.indexOf(data.charAt(index++))];
56
decode64 += 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]);
57
}
58
//Check for the '=' character after the loop, so we don't hose it up.
59
if (sixbits[3] >= 0x40) {
60
decode64.length -= 1;
61
if (sixbits[2] >= 0x40) {
62
decode64.length -= 1;
63
}
64
}
65
}
66
}
67
return decode64;
68
}
69
function arrayToBase64(arrayIn) {
70
var binString = "";
71
var length = arrayIn.length;
72
for (var index = 0; index < length; ++index) {
73
if (typeof arrayIn[index] == "number") {
74
binString += String.fromCharCode(arrayIn[index]);
75
}
76
}
77
return base64(binString);
78
}
79
function base64ToArray(b64String) {
80
var binString = base64_decode(b64String);
81
var outArray = [];
82
var length = binString.length;
83
for (var index = 0; index < length;) {
84
outArray.push(binString.charCodeAt(index++) & 0xFF);
85
}
86
return outArray;
87
}
88