source: Dev/branches/rest-dojo-ui/client/dojox/encoding/base64.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

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