Skip to content

Commit af43ad3

Browse files
committed
Added base64.js for browsers which don't support atob()
1 parent 6c415ef commit af43ad3

File tree

3 files changed

+227
-2
lines changed

3 files changed

+227
-2
lines changed

test/base64.js

+216
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/*
2+
Copyright (c) 2008 Fred Palmer fred.palmer_at_gmail.com
3+
4+
Permission is hereby granted, free of charge, to any person
5+
obtaining a copy of this software and associated documentation
6+
files (the "Software"), to deal in the Software without
7+
restriction, including without limitation the rights to use,
8+
copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the
10+
Software is furnished to do so, subject to the following
11+
conditions:
12+
13+
The above copyright notice and this permission notice shall be
14+
included in all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23+
OTHER DEALINGS IN THE SOFTWARE.
24+
*/
25+
function StringBuffer()
26+
{
27+
this.buffer = [];
28+
}
29+
30+
StringBuffer.prototype.append = function append(string)
31+
{
32+
this.buffer.push(string);
33+
return this;
34+
};
35+
36+
StringBuffer.prototype.toString = function toString()
37+
{
38+
return this.buffer.join("");
39+
};
40+
41+
var Base64 =
42+
{
43+
codex : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
44+
45+
encode : function (input)
46+
{
47+
var output = new StringBuffer();
48+
49+
var enumerator = new Utf8EncodeEnumerator(input);
50+
while (enumerator.moveNext())
51+
{
52+
var chr1 = enumerator.current;
53+
54+
enumerator.moveNext();
55+
var chr2 = enumerator.current;
56+
57+
enumerator.moveNext();
58+
var chr3 = enumerator.current;
59+
60+
var enc1 = chr1 >> 2;
61+
var enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
62+
var enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
63+
var enc4 = chr3 & 63;
64+
65+
if (isNaN(chr2))
66+
{
67+
enc3 = enc4 = 64;
68+
}
69+
else if (isNaN(chr3))
70+
{
71+
enc4 = 64;
72+
}
73+
74+
output.append(this.codex.charAt(enc1) + this.codex.charAt(enc2) + this.codex.charAt(enc3) + this.codex.charAt(enc4));
75+
}
76+
77+
return output.toString();
78+
},
79+
80+
decode : function (input)
81+
{
82+
var output = new StringBuffer();
83+
84+
var enumerator = new Base64DecodeEnumerator(input);
85+
while (enumerator.moveNext())
86+
{
87+
var charCode = enumerator.current;
88+
89+
if (charCode < 128)
90+
output.append(String.fromCharCode(charCode));
91+
else if ((charCode > 191) && (charCode < 224))
92+
{
93+
enumerator.moveNext();
94+
var charCode2 = enumerator.current;
95+
96+
output.append(String.fromCharCode(((charCode & 31) << 6) | (charCode2 & 63)));
97+
}
98+
else
99+
{
100+
enumerator.moveNext();
101+
var charCode2 = enumerator.current;
102+
103+
enumerator.moveNext();
104+
var charCode3 = enumerator.current;
105+
106+
output.append(String.fromCharCode(((charCode & 15) << 12) | ((charCode2 & 63) << 6) | (charCode3 & 63)));
107+
}
108+
}
109+
110+
return output.toString();
111+
}
112+
}
113+
114+
115+
function Utf8EncodeEnumerator(input)
116+
{
117+
this._input = input;
118+
this._index = -1;
119+
this._buffer = [];
120+
}
121+
122+
Utf8EncodeEnumerator.prototype =
123+
{
124+
current: Number.NaN,
125+
126+
moveNext: function()
127+
{
128+
if (this._buffer.length > 0)
129+
{
130+
this.current = this._buffer.shift();
131+
return true;
132+
}
133+
else if (this._index >= (this._input.length - 1))
134+
{
135+
this.current = Number.NaN;
136+
return false;
137+
}
138+
else
139+
{
140+
var charCode = this._input.charCodeAt(++this._index);
141+
142+
// "\r\n" -> "\n"
143+
//
144+
if ((charCode == 13) && (this._input.charCodeAt(this._index + 1) == 10))
145+
{
146+
charCode = 10;
147+
this._index += 2;
148+
}
149+
150+
if (charCode < 128)
151+
{
152+
this.current = charCode;
153+
}
154+
else if ((charCode > 127) && (charCode < 2048))
155+
{
156+
this.current = (charCode >> 6) | 192;
157+
this._buffer.push((charCode & 63) | 128);
158+
}
159+
else
160+
{
161+
this.current = (charCode >> 12) | 224;
162+
this._buffer.push(((charCode >> 6) & 63) | 128);
163+
this._buffer.push((charCode & 63) | 128);
164+
}
165+
166+
return true;
167+
}
168+
}
169+
}
170+
171+
function Base64DecodeEnumerator(input)
172+
{
173+
this._input = input;
174+
this._index = -1;
175+
this._buffer = [];
176+
}
177+
178+
Base64DecodeEnumerator.prototype =
179+
{
180+
current: 64,
181+
182+
moveNext: function()
183+
{
184+
if (this._buffer.length > 0)
185+
{
186+
this.current = this._buffer.shift();
187+
return true;
188+
}
189+
else if (this._index >= (this._input.length - 1))
190+
{
191+
this.current = 64;
192+
return false;
193+
}
194+
else
195+
{
196+
var enc1 = Base64.codex.indexOf(this._input.charAt(++this._index));
197+
var enc2 = Base64.codex.indexOf(this._input.charAt(++this._index));
198+
var enc3 = Base64.codex.indexOf(this._input.charAt(++this._index));
199+
var enc4 = Base64.codex.indexOf(this._input.charAt(++this._index));
200+
201+
var chr1 = (enc1 << 2) | (enc2 >> 4);
202+
var chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
203+
var chr3 = ((enc3 & 3) << 6) | enc4;
204+
205+
this.current = chr1;
206+
207+
if (enc3 != 64)
208+
this._buffer.push(chr2);
209+
210+
if (enc4 != 64)
211+
this._buffer.push(chr3);
212+
213+
return true;
214+
}
215+
}
216+
};

test/benchmark.html

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@
1616
<script src="../source/rom.js" type="text/javascript" charset="utf-8"></script>
1717
<script src="../source/ui.js" type="text/javascript" charset="utf-8"></script>
1818

19+
<script src="base64.js" type="text/javascript" charset="utf-8"></script>
1920
<script src="benchmark.js" type="text/javascript" charset="utf-8"></script>
2021
<script type="text/javascript" charset="utf-8">
2122
$(function() {
2223
var currentRepeat = -1;
23-
var repeatCount = 10;
24+
var repeatCount = 1;
2425
var frameCount = 100;
2526
var results = [];
2627
var i;

0 commit comments

Comments
 (0)