base64 decoding for embedding image onto the src

This commit is contained in:
minjaesong
2020-06-03 23:22:34 +09:00
parent e59c507ea0
commit 55b167a66b
9 changed files with 105 additions and 453 deletions

View File

@@ -64,4 +64,48 @@ system.maxmem = function() {
system.halt = function() {
exit();
};
Object.freeze(system);
Object.freeze(system);
// some utilities functions
var base64 = {};
base64._lookup = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
base64._revLookup = {"=":0,"A":0,"B":1,"C":2,"D":3,"E":4,"F":5,"G":6,"H":7,"I":8,"J":9,"K":10,"L":11,"M":12,"N":13,"O":14,"P":15,"Q":16,"R":17,"S":18,"T":19,"U":20,"V":21,"W":22,"X":23,"Y":24,"Z":25,"a":26,"b":27,"c":28,"d":29,"e":30,"f":31,"g":32,"h":33,"i":34,"j":35,"k":36,"l":37,"m":38,"n":39,"o":40,"p":41,"q":42,"r":43,"s":44,"t":45,"u":46,"v":47,"w":48,"x":49,"y":50,"z":51,"0":52,"1":53,"2":54,"3":55,"4":56,"5":57,"6":58,"7":59,"8":60,"9":61,"+":62,"/":63};
base64.atobarr = function(base64str) {
var ret = [];
for (var i = 0; i < base64str.length; i += 4) {
var bits = (base64._revLookup[base64str.charAt(i)] << 18) | (base64._revLookup[base64str.charAt(i+1)] << 12) | (base64._revLookup[base64str.charAt(i+2)] << 6) | (base64._revLookup[base64str.charAt(i+3)]);
var pads = (base64str.charAt(i+2) == "=") ? 2 : ((base64str.charAt(i+3) == "=") ? 1 : 0);
ret.push((bits >> 16) & 255);
if (pads <= 1) ret.push((bits >> 8) & 255);
if (pads == 0) ret.push(bits & 255);
}
return ret;
};
base64.atob = function(base64str) {
var ret = "";
for (var i = 0; i < base64str.length; i += 4) {
var bits = (base64._revLookup[base64str.charAt(i)] << 18) | (base64._revLookup[base64str.charAt(i+1)] << 12) | (base64._revLookup[base64str.charAt(i+2)] << 6) | (base64._revLookup[base64str.charAt(i+3)]);
var pads = (base64str.charAt(i+2) == "=") ? 2 : ((base64str.charAt(i+3) == "=") ? 1 : 0);
ret += String.fromCharCode((bits >> 16) & 255);
if (pads <= 1) ret += String.fromCharCode((bits >> 8) & 255);
if (pads == 0) ret += String.fromCharCode(bits & 255);
}
return ret;
};
base64.btoa = function(bytes) {
if (typeof bytes == "string") {
for (var i = 0; i < bytes.length; i += 3) {
}
}
else if (Array.isArray(bytes)) {
for (var i = 0; i < bytes.length; i += 3) {
}
}
else {
throw "Unknown byte representation (with typeof "+typeof bytes+")";
}
};
Object.freeze(base64);