[483] | 1 | define(["dojo/_base/lang"], function(lang) { |
---|
| 2 | |
---|
| 3 | var base64 = lang.getObject("dojox.encoding.base64", true); |
---|
| 4 | |
---|
| 5 | var p="="; |
---|
| 6 | var tab="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
---|
| 7 | |
---|
| 8 | base64.encode=function(/* byte[] */ba){ |
---|
| 9 | // summary: |
---|
| 10 | // Encode an array of bytes as a base64-encoded string |
---|
| 11 | var s=[], l=ba.length; |
---|
| 12 | var rm=l%3; |
---|
| 13 | var x=l-rm; |
---|
| 14 | for (var i=0; i<x;){ |
---|
| 15 | var t=ba[i++]<<16|ba[i++]<<8|ba[i++]; |
---|
| 16 | s.push(tab.charAt((t>>>18)&0x3f)); |
---|
| 17 | s.push(tab.charAt((t>>>12)&0x3f)); |
---|
| 18 | s.push(tab.charAt((t>>>6)&0x3f)); |
---|
| 19 | s.push(tab.charAt(t&0x3f)); |
---|
| 20 | } |
---|
| 21 | // deal with trailers, based on patch from Peter Wood. |
---|
| 22 | switch(rm){ |
---|
| 23 | case 2:{ |
---|
| 24 | var t=ba[i++]<<16|ba[i++]<<8; |
---|
| 25 | s.push(tab.charAt((t>>>18)&0x3f)); |
---|
| 26 | s.push(tab.charAt((t>>>12)&0x3f)); |
---|
| 27 | s.push(tab.charAt((t>>>6)&0x3f)); |
---|
| 28 | s.push(p); |
---|
| 29 | break; |
---|
| 30 | } |
---|
| 31 | case 1:{ |
---|
| 32 | var t=ba[i++]<<16; |
---|
| 33 | s.push(tab.charAt((t>>>18)&0x3f)); |
---|
| 34 | s.push(tab.charAt((t>>>12)&0x3f)); |
---|
| 35 | s.push(p); |
---|
| 36 | s.push(p); |
---|
| 37 | break; |
---|
| 38 | } |
---|
| 39 | } |
---|
| 40 | return s.join(""); // string |
---|
| 41 | }; |
---|
| 42 | |
---|
| 43 | base64.decode=function(/* string */str){ |
---|
| 44 | // summary: |
---|
| 45 | // Convert a base64-encoded string to an array of bytes |
---|
| 46 | var s=str.split(""), out=[]; |
---|
| 47 | var l=s.length; |
---|
| 48 | while(s[--l]==p){ } // strip off trailing padding |
---|
| 49 | for (var i=0; i<l;){ |
---|
| 50 | var t=tab.indexOf(s[i++])<<18; |
---|
| 51 | if(i<=l){ t|=tab.indexOf(s[i++])<<12 }; |
---|
| 52 | if(i<=l){ t|=tab.indexOf(s[i++])<<6 }; |
---|
| 53 | if(i<=l){ t|=tab.indexOf(s[i++]) }; |
---|
| 54 | out.push((t>>>16)&0xff); |
---|
| 55 | out.push((t>>>8)&0xff); |
---|
| 56 | out.push(t&0xff); |
---|
| 57 | } |
---|
| 58 | // strip off any null bytes |
---|
| 59 | while(out[out.length-1]==0){ out.pop(); } |
---|
| 60 | return out; // byte[] |
---|
| 61 | }; |
---|
| 62 | |
---|
| 63 | return base64; |
---|
| 64 | }); |
---|