Source: dependencies/resource-loader/lib/b64.js

dependencies/resource-loader/lib/b64.js

  1. 'use strict';
  2. exports.__esModule = true;
  3. exports.encodeBinary = encodeBinary;
  4. var _keyStr = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  5. /**
  6. * Encodes binary into base64.
  7. *
  8. * @param {string} input The input data to encode.
  9. * @returns {string} The encoded base64 string
  10. */
  11. function encodeBinary(input) {
  12. var output = '';
  13. var inx = 0;
  14. while (inx < input.length) {
  15. // Fill byte buffer array
  16. var bytebuffer = [0, 0, 0];
  17. var encodedCharIndexes = [0, 0, 0, 0];
  18. for (var jnx = 0; jnx < bytebuffer.length; ++jnx) {
  19. if (inx < input.length) {
  20. // throw away high-order byte, as documented at:
  21. // https://developer.mozilla.org/En/Using_XMLHttpRequest#Handling_binary_data
  22. bytebuffer[jnx] = input.charCodeAt(inx++) & 0xff;
  23. } else {
  24. bytebuffer[jnx] = 0;
  25. }
  26. }
  27. // Get each encoded character, 6 bits at a time
  28. // index 1: first 6 bits
  29. encodedCharIndexes[0] = bytebuffer[0] >> 2;
  30. // index 2: second 6 bits (2 least significant bits from input byte 1 + 4 most significant bits from byte 2)
  31. encodedCharIndexes[1] = (bytebuffer[0] & 0x3) << 4 | bytebuffer[1] >> 4;
  32. // index 3: third 6 bits (4 least significant bits from input byte 2 + 2 most significant bits from byte 3)
  33. encodedCharIndexes[2] = (bytebuffer[1] & 0x0f) << 2 | bytebuffer[2] >> 6;
  34. // index 3: forth 6 bits (6 least significant bits from input byte 3)
  35. encodedCharIndexes[3] = bytebuffer[2] & 0x3f;
  36. // Determine whether padding happened, and adjust accordingly
  37. var paddingBytes = inx - (input.length - 1);
  38. switch (paddingBytes) {
  39. case 2:
  40. // Set last 2 characters to padding char
  41. encodedCharIndexes[3] = 64;
  42. encodedCharIndexes[2] = 64;
  43. break;
  44. case 1:
  45. // Set last character to padding char
  46. encodedCharIndexes[3] = 64;
  47. break;
  48. default:
  49. break; // No padding - proceed
  50. }
  51. // Now we will grab each appropriate character out of our keystring
  52. // based on our index array and append it to the output string
  53. for (var _jnx = 0; _jnx < encodedCharIndexes.length; ++_jnx) {
  54. output += _keyStr.charAt(encodedCharIndexes[_jnx]);
  55. }
  56. }
  57. return output;
  58. }
  59. // Backwards compat
  60. if (typeof module !== 'undefined') {
  61. module.exports.default = encodeBinary; // eslint-disable-line no-undef
  62. }
  63. //# sourceMappingURL=b64.js.map