base64.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. (function () {
  19. var
  20. object = typeof window != 'undefined' ? window : exports,
  21. chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=',
  22. INVALID_CHARACTER_ERR = (function () {
  23. // fabricate a suitable error object
  24. try {
  25. document.createElement('$');
  26. }
  27. catch (error) {
  28. return error;
  29. }
  30. }());
  31. // encoder
  32. // [https://gist.github.com/999166] by [https://github.com/nignag]
  33. object.btoa || (
  34. object.btoa = function (input) {
  35. for (
  36. // initialize result and counter
  37. var block, charCode, idx = 0, map = chars, output = '';
  38. // if the next input index does not exist:
  39. // change the mapping table to "="
  40. // check if d has no fractional digits
  41. input.charAt(idx | 0) || (map = '=', idx % 1);
  42. // "8 - idx % 1 * 8" generates the sequence 2, 4, 6, 8
  43. output += map.charAt(63 & block >> 8 - idx % 1 * 8)
  44. ) {
  45. charCode = input.charCodeAt(idx += 3 / 4);
  46. if (charCode > 0xFF) throw INVALID_CHARACTER_ERR;
  47. block = block << 8 | charCode;
  48. }
  49. return output;
  50. });
  51. // decoder
  52. // [https://gist.github.com/1020396] by [https://github.com/atk]
  53. object.atob || (
  54. object.atob = function (input) {
  55. input = input.replace(/=+$/, '');
  56. if (input.length % 4 == 1) throw INVALID_CHARACTER_ERR;
  57. for (
  58. // initialize result and counters
  59. var bc = 0, bs, buffer, idx = 0, output = '';
  60. // get next character
  61. buffer = input.charAt(idx++);
  62. // character found in table? initialize bit storage and add its ascii value;
  63. ~buffer && (bs = bc % 4 ? bs * 64 + buffer : buffer,
  64. // and if not first of each 4 characters,
  65. // convert the first 8 bits to one ascii character
  66. bc++ % 4) ? output += String.fromCharCode(255 & bs >> (-2 * bc & 6)) : 0
  67. ) {
  68. // try to find character in table (0-63, not found => -1)
  69. buffer = chars.indexOf(buffer);
  70. }
  71. return output;
  72. });
  73. }());