|
| 1 | +;(function () { |
| 2 | + |
| 3 | + var object = |
| 4 | + typeof exports != 'undefined' ? exports : |
| 5 | + typeof self != 'undefined' ? self : // #8: web workers |
| 6 | + $.global; // #31: ExtendScript |
| 7 | + |
| 8 | + var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; |
| 9 | + |
| 10 | + function InvalidCharacterError(message) { |
| 11 | + this.message = message; |
| 12 | + } |
| 13 | + InvalidCharacterError.prototype = new Error; |
| 14 | + InvalidCharacterError.prototype.name = 'InvalidCharacterError'; |
| 15 | + |
| 16 | + // encoder |
| 17 | + // [https://gist.github.com/999166] by [https://github.com/nignag] |
| 18 | + object.btoa || ( |
| 19 | + object.btoa = function (input) { |
| 20 | + var str = String(input); |
| 21 | + for ( |
| 22 | + // initialize result and counter |
| 23 | + var block, charCode, idx = 0, map = chars, output = ''; |
| 24 | + // if the next str index does not exist: |
| 25 | + // change the mapping table to "=" |
| 26 | + // check if d has no fractional digits |
| 27 | + str.charAt(idx | 0) || (map = '=', idx % 1); |
| 28 | + // "8 - idx % 1 * 8" generates the sequence 2, 4, 6, 8 |
| 29 | + output += map.charAt(63 & block >> 8 - idx % 1 * 8) |
| 30 | + ) { |
| 31 | + charCode = str.charCodeAt(idx += 3/4); |
| 32 | + if (charCode > 0xFF) { |
| 33 | + throw new InvalidCharacterError("'btoa' failed: The string to be encoded contains characters outside of the Latin1 range."); |
| 34 | + } |
| 35 | + block = block << 8 | charCode; |
| 36 | + } |
| 37 | + return output; |
| 38 | + }); |
| 39 | + |
| 40 | + // decoder |
| 41 | + // [https://gist.github.com/1020396] by [https://github.com/atk] |
| 42 | + object.atob || ( |
| 43 | + object.atob = function (input) { |
| 44 | + var str = String(input).replace(/[=]+$/, ''); // #31: ExtendScript bad parse of /= |
| 45 | + if (str.length % 4 == 1) { |
| 46 | + throw new InvalidCharacterError("'atob' failed: The string to be decoded is not correctly encoded."); |
| 47 | + } |
| 48 | + for ( |
| 49 | + // initialize result and counters |
| 50 | + var bc = 0, bs, buffer, idx = 0, output = ''; |
| 51 | + // get next character |
| 52 | + buffer = str.charAt(idx++); |
| 53 | + // character found in table? initialize bit storage and add its ascii value; |
| 54 | + ~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer, |
| 55 | + // and if not first of each 4 characters, |
| 56 | + // convert the first 8 bits to one ascii character |
| 57 | + bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0 |
| 58 | + ) { |
| 59 | + // try to find character in table (0-63, not found => -1) |
| 60 | + buffer = chars.indexOf(buffer); |
| 61 | + } |
| 62 | + return output; |
| 63 | + }); |
| 64 | + |
| 65 | +}()); |
0 commit comments