createToBase64Bin.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.createToBase64Bin = void 0;
  4. const constants_1 = require("./constants");
  5. const createToBase64Bin = (chars = constants_1.alphabet, pad = '=') => {
  6. if (chars.length !== 64)
  7. throw new Error('chars must be 64 characters long');
  8. const table = chars.split('').map((c) => c.charCodeAt(0));
  9. const table2 = [];
  10. for (const c1 of table) {
  11. for (const c2 of table) {
  12. const two = (c1 << 8) + c2;
  13. table2.push(two);
  14. }
  15. }
  16. const doAddPadding = pad.length === 1;
  17. const E = doAddPadding ? pad.charCodeAt(0) : 0;
  18. const EE = doAddPadding ? (E << 8) | E : 0;
  19. return (uint8, start, length, dest, offset) => {
  20. const extraLength = length % 3;
  21. const baseLength = length - extraLength;
  22. for (; start < baseLength; start += 3) {
  23. const o1 = uint8[start];
  24. const o2 = uint8[start + 1];
  25. const o3 = uint8[start + 2];
  26. const v1 = (o1 << 4) | (o2 >> 4);
  27. const v2 = ((o2 & 0b1111) << 8) | o3;
  28. dest.setInt32(offset, (table2[v1] << 16) + table2[v2]);
  29. offset += 4;
  30. }
  31. if (extraLength === 1) {
  32. const o1 = uint8[baseLength];
  33. if (doAddPadding) {
  34. dest.setInt32(offset, (table2[o1 << 4] << 16) + EE);
  35. offset += 4;
  36. }
  37. else {
  38. dest.setInt16(offset, table2[o1 << 4]);
  39. offset += 2;
  40. }
  41. }
  42. else if (extraLength) {
  43. const o1 = uint8[baseLength];
  44. const o2 = uint8[baseLength + 1];
  45. const v1 = (o1 << 4) | (o2 >> 4);
  46. const v2 = (o2 & 0b1111) << 2;
  47. if (doAddPadding) {
  48. dest.setInt32(offset, (table2[v1] << 16) + (table[v2] << 8) + E);
  49. offset += 4;
  50. }
  51. else {
  52. dest.setInt16(offset, table2[v1]);
  53. offset += 2;
  54. dest.setInt8(offset, table[v2]);
  55. offset += 1;
  56. }
  57. }
  58. return offset;
  59. };
  60. };
  61. exports.createToBase64Bin = createToBase64Bin;
  62. //# sourceMappingURL=createToBase64Bin.js.map