base64-arraybuffer.umd.js 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. /*
  2. * base64-arraybuffer 1.0.2 <https://github.com/niklasvh/base64-arraybuffer>
  3. * Copyright (c) 2022 Niklas von Hertzen <https://hertzen.com>
  4. * Released under MIT License
  5. */
  6. (function (global, factory) {
  7. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  8. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  9. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global['base64-arraybuffer'] = {}));
  10. }(this, (function (exports) { 'use strict';
  11. var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  12. // Use a lookup table to find the index.
  13. var lookup = typeof Uint8Array === 'undefined' ? [] : new Uint8Array(256);
  14. for (var i = 0; i < chars.length; i++) {
  15. lookup[chars.charCodeAt(i)] = i;
  16. }
  17. var encode = function (arraybuffer) {
  18. var bytes = new Uint8Array(arraybuffer), i, len = bytes.length, base64 = '';
  19. for (i = 0; i < len; i += 3) {
  20. base64 += chars[bytes[i] >> 2];
  21. base64 += chars[((bytes[i] & 3) << 4) | (bytes[i + 1] >> 4)];
  22. base64 += chars[((bytes[i + 1] & 15) << 2) | (bytes[i + 2] >> 6)];
  23. base64 += chars[bytes[i + 2] & 63];
  24. }
  25. if (len % 3 === 2) {
  26. base64 = base64.substring(0, base64.length - 1) + '=';
  27. }
  28. else if (len % 3 === 1) {
  29. base64 = base64.substring(0, base64.length - 2) + '==';
  30. }
  31. return base64;
  32. };
  33. var decode = function (base64) {
  34. var bufferLength = base64.length * 0.75, len = base64.length, i, p = 0, encoded1, encoded2, encoded3, encoded4;
  35. if (base64[base64.length - 1] === '=') {
  36. bufferLength--;
  37. if (base64[base64.length - 2] === '=') {
  38. bufferLength--;
  39. }
  40. }
  41. var arraybuffer = new ArrayBuffer(bufferLength), bytes = new Uint8Array(arraybuffer);
  42. for (i = 0; i < len; i += 4) {
  43. encoded1 = lookup[base64.charCodeAt(i)];
  44. encoded2 = lookup[base64.charCodeAt(i + 1)];
  45. encoded3 = lookup[base64.charCodeAt(i + 2)];
  46. encoded4 = lookup[base64.charCodeAt(i + 3)];
  47. bytes[p++] = (encoded1 << 2) | (encoded2 >> 4);
  48. bytes[p++] = ((encoded2 & 15) << 4) | (encoded3 >> 2);
  49. bytes[p++] = ((encoded3 & 3) << 6) | (encoded4 & 63);
  50. }
  51. return arraybuffer;
  52. };
  53. exports.decode = decode;
  54. exports.encode = encode;
  55. Object.defineProperty(exports, '__esModule', { value: true });
  56. })));
  57. //# sourceMappingURL=base64-arraybuffer.umd.js.map