1+ /*
2+ Copyright Vassilis Petroulias [DRDigit]
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
15+
16+ Project info and source @ http://jsbase64.codeplex.com
17+
18+ Usage: B64.encode(PlainString) returns Base64String
19+ B64.decode(Base64String) returns PlainString
20+ */
21+ var B64 = {
22+ alphabet : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=' ,
23+ lookup : null ,
24+ ie : / M S I E / . test ( navigator . userAgent ) ,
25+ ieo : / M S I E [ 6 7 ] / . test ( navigator . userAgent ) ,
26+ encode : function ( s ) {
27+ var buffer = B64 . toUtf8 ( s ) ,
28+ position = - 1 ,
29+ len = buffer . length ,
30+ nan0 , nan1 , nan2 , enc = [ , , , ] ;
31+ if ( B64 . ie ) {
32+ var result = [ ] ;
33+ while ( ++ position < len ) {
34+ nan0 = buffer [ position ] ;
35+ nan1 = buffer [ ++ position ] ;
36+ enc [ 0 ] = nan0 >> 2 ;
37+ enc [ 1 ] = ( ( nan0 & 3 ) << 4 ) | ( nan1 >> 4 ) ;
38+ if ( isNaN ( nan1 ) )
39+ enc [ 2 ] = enc [ 3 ] = 64 ;
40+ else {
41+ nan2 = buffer [ ++ position ] ;
42+ enc [ 2 ] = ( ( nan1 & 15 ) << 2 ) | ( nan2 >> 6 ) ;
43+ enc [ 3 ] = ( isNaN ( nan2 ) ) ? 64 : nan2 & 63 ;
44+ }
45+ result . push ( B64 . alphabet . charAt ( enc [ 0 ] ) , B64 . alphabet . charAt ( enc [ 1 ] ) , B64 . alphabet . charAt ( enc [ 2 ] ) , B64 . alphabet . charAt ( enc [ 3 ] ) ) ;
46+ }
47+ return result . join ( '' ) ;
48+ } else {
49+ var result = '' ;
50+ while ( ++ position < len ) {
51+ nan0 = buffer [ position ] ;
52+ nan1 = buffer [ ++ position ] ;
53+ enc [ 0 ] = nan0 >> 2 ;
54+ enc [ 1 ] = ( ( nan0 & 3 ) << 4 ) | ( nan1 >> 4 ) ;
55+ if ( isNaN ( nan1 ) )
56+ enc [ 2 ] = enc [ 3 ] = 64 ;
57+ else {
58+ nan2 = buffer [ ++ position ] ;
59+ enc [ 2 ] = ( ( nan1 & 15 ) << 2 ) | ( nan2 >> 6 ) ;
60+ enc [ 3 ] = ( isNaN ( nan2 ) ) ? 64 : nan2 & 63 ;
61+ }
62+ result += B64 . alphabet [ enc [ 0 ] ] + B64 . alphabet [ enc [ 1 ] ] + B64 . alphabet [ enc [ 2 ] ] + B64 . alphabet [ enc [ 3 ] ] ;
63+ }
64+ return result ;
65+ }
66+ } ,
67+ decode : function ( s ) {
68+ if ( s . length % 4 )
69+ throw new Error ( "InvalidCharacterError: 'B64.decode' failed: The string to be decoded is not correctly encoded." ) ;
70+ var buffer = B64 . fromUtf8 ( s ) ,
71+ position = 0 ,
72+ len = buffer . length ;
73+ if ( B64 . ieo ) {
74+ var result = [ ] ;
75+ while ( position < len ) {
76+ if ( buffer [ position ] < 128 )
77+ result . push ( String . fromCharCode ( buffer [ position ++ ] ) ) ;
78+ else if ( buffer [ position ] > 191 && buffer [ position ] < 224 )
79+ result . push ( String . fromCharCode ( ( ( buffer [ position ++ ] & 31 ) << 6 ) | ( buffer [ position ++ ] & 63 ) ) ) ;
80+ else
81+ result . push ( String . fromCharCode ( ( ( buffer [ position ++ ] & 15 ) << 12 ) | ( ( buffer [ position ++ ] & 63 ) << 6 ) | ( buffer [ position ++ ] & 63 ) ) ) ;
82+ }
83+ return result . join ( '' ) ;
84+ } else {
85+ var result = '' ;
86+ while ( position < len ) {
87+ if ( buffer [ position ] < 128 )
88+ result += String . fromCharCode ( buffer [ position ++ ] ) ;
89+ else if ( buffer [ position ] > 191 && buffer [ position ] < 224 )
90+ result += String . fromCharCode ( ( ( buffer [ position ++ ] & 31 ) << 6 ) | ( buffer [ position ++ ] & 63 ) ) ;
91+ else
92+ result += String . fromCharCode ( ( ( buffer [ position ++ ] & 15 ) << 12 ) | ( ( buffer [ position ++ ] & 63 ) << 6 ) | ( buffer [ position ++ ] & 63 ) ) ;
93+ }
94+ return result ;
95+ }
96+ } ,
97+ toUtf8 : function ( s ) {
98+ var position = - 1 ,
99+ len = s . length ,
100+ chr , buffer = [ ] ;
101+ if ( / ^ [ \x00 - \x7f ] * $ / . test ( s ) ) while ( ++ position < len )
102+ buffer . push ( s . charCodeAt ( position ) ) ;
103+ else while ( ++ position < len ) {
104+ chr = s . charCodeAt ( position ) ;
105+ if ( chr < 128 )
106+ buffer . push ( chr ) ;
107+ else if ( chr < 2048 )
108+ buffer . push ( ( chr >> 6 ) | 192 , ( chr & 63 ) | 128 ) ;
109+ else
110+ buffer . push ( ( chr >> 12 ) | 224 , ( ( chr >> 6 ) & 63 ) | 128 , ( chr & 63 ) | 128 ) ;
111+ }
112+ return buffer ;
113+ } ,
114+ fromUtf8 : function ( s ) {
115+ var position = - 1 ,
116+ len , buffer = [ ] ,
117+ enc = [ , , , ] ;
118+ if ( ! B64 . lookup ) {
119+ len = B64 . alphabet . length ;
120+ B64 . lookup = { } ;
121+ while ( ++ position < len )
122+ B64 . lookup [ B64 . alphabet . charAt ( position ) ] = position ;
123+ position = - 1 ;
124+ }
125+ len = s . length ;
126+ while ( ++ position < len ) {
127+ enc [ 0 ] = B64 . lookup [ s . charAt ( position ) ] ;
128+ enc [ 1 ] = B64 . lookup [ s . charAt ( ++ position ) ] ;
129+ buffer . push ( ( enc [ 0 ] << 2 ) | ( enc [ 1 ] >> 4 ) ) ;
130+ enc [ 2 ] = B64 . lookup [ s . charAt ( ++ position ) ] ;
131+ if ( enc [ 2 ] == 64 )
132+ break ;
133+ buffer . push ( ( ( enc [ 1 ] & 15 ) << 4 ) | ( enc [ 2 ] >> 2 ) ) ;
134+ enc [ 3 ] = B64 . lookup [ s . charAt ( ++ position ) ] ;
135+ if ( enc [ 3 ] == 64 )
136+ break ;
137+ buffer . push ( ( ( enc [ 2 ] & 3 ) << 6 ) | enc [ 3 ] ) ;
138+ }
139+ return buffer ;
140+ }
141+ } ;
0 commit comments