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