2 * Copyright (c) 2010 Nick Galbreath
3 * http://code.google.com/p/stringencoders/source/browse/#svn/trunk/javascript
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
27 /* base64 encode/decode compatible with window.btoa/atob
29 * window.atob/btoa is a Firefox extension to convert binary data (the "b")
30 * to base64 (ascii, the "a").
32 * It is also found in Safari and Chrome. It is not available in IE.
34 * if (!window.btoa) window.btoa = base64.encode
35 * if (!window.atob) window.atob = base64.decode
37 * The original spec's for atob/btoa are a bit lacking
38 * https://developer.mozilla.org/en/DOM/window.atob
39 * https://developer.mozilla.org/en/DOM/window.btoa
41 * window.btoa and base64.encode takes a string where charCodeAt is [0,255]
42 * If any character is not [0,255], then an DOMException(5) is thrown.
44 * window.atob and base64.decode take a base64-encoded string
45 * If the input length is not a multiple of 4, or contains invalid characters
46 * then an DOMException(5) is thrown.
50 base64.ALPHA = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
52 base64.makeDOMException = function() {
53 // sadly in FF,Safari,Chrome you can't make a DOMException
57 return new DOMException(DOMException.INVALID_CHARACTER_ERR);
59 // not available, just passback a duck-typed equiv
60 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Error
61 // https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Error/prototype
62 var ex = new Error("DOM Exception 5");
64 // ex.number and ex.description is IE-specific.
65 ex.code = ex.number = 5;
66 ex.name = ex.description = "INVALID_CHARACTER_ERR";
68 // Safari/Chrome output format
69 ex.toString = function() { return 'Error: ' + ex.name + ': ' + ex.message; };
74 base64.getbyte64 = function(s,i) {
75 // This is oddly fast, except on Chrome/V8.
76 // Minimal or no improvement in performance by using a
77 // object with properties mapping chars to value (eg. 'A': 0)
78 var idx = base64.ALPHA.indexOf(s.charAt(i));
80 throw base64.makeDOMException();
85 base64.decode = function(s) {
88 var getbyte64 = base64.getbyte64;
96 throw base64.makeDOMException();
100 if (s.charAt(imax - 1) === base64.PADCHAR) {
102 if (s.charAt(imax - 2) === base64.PADCHAR) {
105 // either way, we want to ignore this last block
110 for (i = 0; i < imax; i += 4) {
111 b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12) |
112 (getbyte64(s,i+2) << 6) | getbyte64(s,i+3);
113 x.push(String.fromCharCode(b10 >> 16, (b10 >> 8) & 0xff, b10 & 0xff));
118 b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12) | (getbyte64(s,i+2) << 6);
119 x.push(String.fromCharCode(b10 >> 16, (b10 >> 8) & 0xff));
122 b10 = (getbyte64(s,i) << 18) | (getbyte64(s,i+1) << 12);
123 x.push(String.fromCharCode(b10 >> 16));
129 base64.getbyte = function(s,i) {
130 var x = s.charCodeAt(i);
132 throw base64.makeDOMException();
137 base64.encode = function(s) {
138 if (arguments.length !== 1) {
139 throw new SyntaxError("Not enough arguments");
141 var padchar = base64.PADCHAR;
142 var alpha = base64.ALPHA;
143 var getbyte = base64.getbyte;
151 var imax = s.length - s.length % 3;
153 if (s.length === 0) {
156 for (i = 0; i < imax; i += 3) {
157 b10 = (getbyte(s,i) << 16) | (getbyte(s,i+1) << 8) | getbyte(s,i+2);
158 x.push(alpha.charAt(b10 >> 18));
159 x.push(alpha.charAt((b10 >> 12) & 0x3F));
160 x.push(alpha.charAt((b10 >> 6) & 0x3f));
161 x.push(alpha.charAt(b10 & 0x3f));
163 switch (s.length - imax) {
165 b10 = getbyte(s,i) << 16;
166 x.push(alpha.charAt(b10 >> 18) + alpha.charAt((b10 >> 12) & 0x3F) +
170 b10 = (getbyte(s,i) << 16) | (getbyte(s,i+1) << 8);
171 x.push(alpha.charAt(b10 >> 18) + alpha.charAt((b10 >> 12) & 0x3F) +
172 alpha.charAt((b10 >> 6) & 0x3f) + padchar);