PDA

View Full Version : Ext.util.base64 (encode/decode)



wm003
12 May 2008, 11:37 PM
There were so many base64 versions around but i haven't found an Ext-classed version of this so here it is

call it with


var myencodedstring = Ext.util.base64.encode("Testtext"); //returns VGVzdHRleHQ=
var mydecodedstring = Ext.util.base64.decode("VGVzdHRleHQ="); //returns Testtext

Ext.util.base64 = {

base64s : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",

encode: function(decStr){
if (typeof btoa === 'function') {
return btoa(decStr);
}
var base64s = this.base64s;
var bits;
var dual;
var i = 0;
var encOut = "";
while(decStr.length >= i + 3){
bits = (decStr.charCodeAt(i++) & 0xff) <<16 | (decStr.charCodeAt(i++) & 0xff) <<8 | decStr.charCodeAt(i++) & 0xff;
encOut += base64s.charAt((bits & 0x00fc0000) >>18) + base64s.charAt((bits & 0x0003f000) >>12) + base64s.charAt((bits & 0x00000fc0) >> 6) + base64s.charAt((bits & 0x0000003f));
}
if(decStr.length -i > 0 && decStr.length -i < 3){
dual = Boolean(decStr.length -i -1);
bits = ((decStr.charCodeAt(i++) & 0xff) <<16) | (dual ? (decStr.charCodeAt(i) & 0xff) <<8 : 0);
encOut += base64s.charAt((bits & 0x00fc0000) >>18) + base64s.charAt((bits & 0x0003f000) >>12) + (dual ? base64s.charAt((bits & 0x00000fc0) >>6) : '=') + '=';
}
return(encOut);
},

decode: function(encStr){
if (typeof atob === 'function') {
return atob(encStr);
}
var base64s = this.base64s;
var bits;
var decOut = "";
var i = 0;
for(; i<encStr.length; i += 4){
bits = (base64s.indexOf(encStr.charAt(i)) & 0xff) <<18 | (base64s.indexOf(encStr.charAt(i +1)) & 0xff) <<12 | (base64s.indexOf(encStr.charAt(i +2)) & 0xff) << 6 | base64s.indexOf(encStr.charAt(i +3)) & 0xff;
decOut += String.fromCharCode((bits & 0xff0000) >>16, (bits & 0xff00) >>8, bits & 0xff);
}
if(encStr.charCodeAt(i -2) == 61){
return(decOut.substring(0, decOut.length -2));
}
else if(encStr.charCodeAt(i -1) == 61){
return(decOut.substring(0, decOut.length -1));
}
else {
return(decOut);
}
}

};

Animal
12 May 2008, 11:48 PM
FF has a built in function to do this as someone pointed out to me when I included base 64 encode functions in my Grid to Excel UX. See that thread for how to incorporate the native function if available.

wm003
12 May 2008, 11:54 PM
FF has a built in function to do this as someone pointed out to me when I included base 64 encode functions in my Grid to Excel UX. See that thread for how to incorporate the native function if available.
/:) ah, didn't know that...(that's why you posted it here...;))..I'll check your Excel-thread again (which is quite impressive btw. it's a pity, IE6/7 do not support it)

wm003
13 May 2008, 12:23 AM
FF has a built in function to do this as someone pointed out to me when I included base 64 encode functions in my Grid to Excel UX. See that thread for how to incorporate the native function if available.
Thanks, Animal. I updated the code to support that.
However, if somebody will code an own global "atob" or "btoa" function, the Ext.util.base64 will generally fail. Everybody who uses it should keep this in mind.

gounis
13 Jan 2009, 6:44 AM
Hello,

I would like to ask which is the license for using the Ext.util.base64.

Thank you in advance.

wm003
16 Jan 2009, 12:37 AM
Hello,

I would like to ask which is the license for using the Ext.util.base64.

Thank you in advance.

Free as in free beer and free speech :)

aditit
31 Aug 2009, 1:39 AM
I want to use base64.encode function .But it is not present in ext-all.js When I added code for base64.encode and called it in .js . But it says , Ext.util.base64.encode is not defined. What is the solution for it.
Thanks in advance!

wm003
31 Aug 2009, 3:10 AM
I want to use base64.encode function .But it is not present in ext-all.js When I added code for base64.encode and called it in .js . But it says , Ext.util.base64.encode is not defined. What is the solution for it.
Thanks in advance!

did you really copy the whole code from post #1? Or just the code within the encode-function? To use "Ext.util.base64.encode" you need to copy the whole code.

PranKe01
16 Dec 2009, 1:37 AM
Hi guys,

this function isn't in version 3.0 of ExtJS. I included the code myself but I get an error when using this function with a Word-File :( It says "INVALID_CHARACTER_ERR: DOM Exception 5" in this line:

return btoa(decStr);
Commenting the following lines

if (typeof btoa === 'function') {
return btoa(decStr);
}
throws an error "TypeError: Value undefined does not allow function calls." at this line:

bits = (decStr.charCodeAt(i++) & 0xff) <<16 | (decStr.charCodeAt(i++) & 0xff) <<8 | decStr.charCodeAt(i++) & 0xff;

I'm using this function with Adobe Air and I want to code ANY file with base64. First I create a air.ByteArray using stream.readBytes and then give that ByteArray to this function.

Does anyone know another way in JS to code any file to base64?

Thanks a lot!

PranKe01
22 Dec 2009, 2:41 PM
After hours of searching I finally found THE solution! None of the implementations workes with binary files or Word-files etc. But that code works like a charm :) Should be implemented in ExtJS!


var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

var encode64 = function(input) {
input = escape(input);
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;

do {
chr1 = input.charCodeAt(i++);
chr2 = input.charCodeAt(i++);
chr3 = input.charCodeAt(i++);

enc1 = chr1 >> 2;
enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
enc4 = chr3 & 63;

if (isNaN(chr2)) {
enc3 = enc4 = 64;
} else if (isNaN(chr3)) {
enc4 = 64;
}

output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + keyStr.charAt(enc3) + keyStr.charAt(enc4);
chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);

return output;
}

var decode64 = function(input) {
var output = "";
var chr1, chr2, chr3 = "";
var enc1, enc2, enc3, enc4 = "";
var i = 0;

var base64test = /[^A-Za-z0-9\+\/\=]/g;
if (base64test.exec(input)) {
Ext.Msg.alert("Warning", "The document contains invalid characters. Errors can occur when decoding it.");
}
input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

do {
enc1 = keyStr.indexOf(input.charAt(i++));
enc2 = keyStr.indexOf(input.charAt(i++));
enc3 = keyStr.indexOf(input.charAt(i++));
enc4 = keyStr.indexOf(input.charAt(i++));

chr1 = (enc1 << 2) | (enc2 >> 4);
chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
chr3 = ((enc3 & 3) << 6) | enc4;

output = output + String.fromCharCode(chr1);

if (enc3 != 64) {
output = output + String.fromCharCode(chr2);
}
if (enc4 != 64) {
output = output + String.fromCharCode(chr3);
}

chr1 = chr2 = chr3 = "";
enc1 = enc2 = enc3 = enc4 = "";
} while (i < input.length);

return unescape(output);
};

PranKe01
11 Jan 2010, 4:22 AM
Hm - the code doesn't seem to work like I thought. I am creating a ByteArray of a word-file and giving it to the encode function. When I decode it and writing the bytearray back to the hdd, the size is bigger and the file cannot be opened :-/

What am I doing wrong?! Isn't it possible to encode a ByteArray to base64? How would you encode an exe-file to base64?

PranKe01
25 Jan 2010, 6:26 AM
Hi guys,

I rewrote the function, so that I could encode and decode ByteArrays. The code can be found here: http://www.extjs.com/forum/showthread.php?p=430635#post430635

hieu79vn
17 Aug 2012, 12:08 AM
Hi Animal
Could you tell me Where to find the base 64 encode funtion in Extjs?

Thank you

Misiu
28 Dec 2012, 5:48 AM
Base64 is currently not included in ExtJS SDK.
But there are many ready-to-use extensions, just search forum :)