base64.js 2.8 KB

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