1+ /*
2+ * Base 64 implementation in JavaScript
3+ * Copyright (c) 2009 Nicholas C. Zakas. All rights reserved.
4+ *
5+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6+ * of this software and associated documentation files (the "Software"), to deal
7+ * in the Software without restriction, including without limitation the rights
8+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+ * copies of the Software, and to permit persons to whom the Software is
10+ * furnished to do so, subject to the following conditions:
11+ *
12+ * The above copyright notice and this permission notice shall be included in
13+ * all copies or substantial portions of the Software.
14+ *
15+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+ * THE SOFTWARE.
22+ */
23+
24+ /**
25+ * Base64-encodes a string of text.
26+ * @param {String } text The text to encode
27+ * @return {String } The base64-encoded string.
28+ */
29+ function base64Encode ( text ) {
30+
31+ //helper function to left-pad an array with zeros
32+ function padLeft ( bits , length ) {
33+ while ( bits . length < length ) {
34+ bits . unshift ( "0" ) ;
35+ }
36+ return bits ;
37+ }
38+
39+ //helper function to right-pad an array with zeros
40+ function padRight ( bits , length ) {
41+ while ( bits . length < length ) {
42+ bits . push ( "0" ) ;
43+ }
44+ return bits ;
45+ }
46+
47+ //local variables
48+ var digits = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" ,
49+ part , index ,
50+ i = 0 , j = 0 ,
51+ padding = "" ,
52+ quantaCount ,
53+ bits = [ ] ,
54+ result = [ ] ;
55+
56+ //create an array of binary digits representing the text
57+ for ( ; i < text . length ; i ++ ) {
58+ part = text . charCodeAt ( i ) . toString ( 2 ) ;
59+ bits = bits . concat ( padLeft ( part . split ( "" ) , 8 ) ) ;
60+ }
61+
62+ //figure out how many 24-bit quanta are in the array
63+ quantaCount = Math . floor ( bits . length / 24 ) ;
64+
65+ //encode all bits
66+ encodeBits: while ( true ) {
67+
68+ //must encode one complete quanta at a time
69+ for ( i = 0 ; i < quantaCount ; i ++ ) {
70+ for ( j = 0 ; j < 4 ; j ++ ) {
71+ part = bits . splice ( 0 , 6 ) . join ( "" ) ;
72+ index = parseInt ( part , 2 ) ;
73+ result . push ( digits [ index ] ) ;
74+ }
75+ }
76+
77+ //take care of any extra bits
78+ switch ( bits . length ) {
79+ case 8 :
80+ padRight ( bits , 12 ) ;
81+ padding = "==" ;
82+ continue encodeBits;
83+ case 16 :
84+ padRight ( bits , 18 ) ;
85+ padding = "=" ;
86+ continue encodeBits;
87+ default :
88+ break encodeBits;
89+ }
90+ }
91+
92+ //add any padding to the result
93+ result . push ( padding ) ;
94+
95+ //return a string
96+ return result . join ( "" ) ;
97+
98+ }
0 commit comments