|
| 1 | +/* |
| 2 | + * Copyright (c) 2013 Kevin van Zonneveld (http://kvz.io) |
| 3 | + * and Contributors (http://phpjs.org/authors) |
| 4 | + * |
| 5 | + * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 6 | + * this software and associated documentation files (the "Software"), to deal in |
| 7 | + * the Software without restriction, including without limitation the rights to |
| 8 | + * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
| 9 | + * of the Software, and to permit persons to whom the Software is furnished to do |
| 10 | + * so, subject to the following conditions: |
| 11 | + * |
| 12 | + * The above copyright notice and this permission notice shall be included in all |
| 13 | + * 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 THE |
| 21 | + * SOFTWARE. |
| 22 | + */ |
| 23 | + |
| 24 | +(function() { |
| 25 | + |
| 26 | + var object = typeof exports !== 'undefined' ? exports : this; // For eventual node.js environment support |
| 27 | + |
| 28 | + function base64_encode(data) { |
| 29 | + // discuss at: http://phpjs.org/functions/base64_encode/ |
| 30 | + // original by: Tyler Akins (http://rumkin.com) |
| 31 | + // improved by: Bayron Guevara |
| 32 | + // improved by: Thunder.m |
| 33 | + // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) |
| 34 | + // improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) |
| 35 | + // improved by: Rafał Kukawski (http://kukawski.pl) |
| 36 | + // bugfixed by: Pellentesque Malesuada |
| 37 | + // example 1: base64_encode('Kevin van Zonneveld'); |
| 38 | + // returns 1: 'S2V2aW4gdmFuIFpvbm5ldmVsZA==' |
| 39 | + // example 2: base64_encode('a'); |
| 40 | + // returns 2: 'YQ==' |
| 41 | + // example 3: base64_encode('✓ à la mode'); |
| 42 | + // returns 3: '4pyTIMOgIGxhIG1vZGU=' |
| 43 | + |
| 44 | + var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; |
| 45 | + var o1, o2, o3, h1, h2, h3, h4, bits, i = 0, |
| 46 | + ac = 0, |
| 47 | + enc = '', |
| 48 | + tmp_arr = []; |
| 49 | + |
| 50 | + if (!data) { |
| 51 | + return data; |
| 52 | + } |
| 53 | + |
| 54 | + data = unescape(encodeURIComponent(data)); |
| 55 | + |
| 56 | + do { |
| 57 | + // pack three octets into four hexets |
| 58 | + o1 = data.charCodeAt(i++); |
| 59 | + o2 = data.charCodeAt(i++); |
| 60 | + o3 = data.charCodeAt(i++); |
| 61 | + |
| 62 | + bits = o1 << 16 | o2 << 8 | o3; |
| 63 | + |
| 64 | + h1 = bits >> 18 & 0x3f; |
| 65 | + h2 = bits >> 12 & 0x3f; |
| 66 | + h3 = bits >> 6 & 0x3f; |
| 67 | + h4 = bits & 0x3f; |
| 68 | + |
| 69 | + // use hexets to index into b64, and append result to encoded string |
| 70 | + tmp_arr[ac++] = b64.charAt(h1) + b64.charAt(h2) + b64.charAt(h3) + b64.charAt(h4); |
| 71 | + } while (i < data.length); |
| 72 | + |
| 73 | + enc = tmp_arr.join(''); |
| 74 | + |
| 75 | + var r = data.length % 3; |
| 76 | + |
| 77 | + return (r ? enc.slice(0, r - 3) : enc) + '==='.slice(r || 3); |
| 78 | + } |
| 79 | + |
| 80 | + object.base64encode = base64_encode; |
| 81 | + |
| 82 | +}()); |
0 commit comments