Ei kuvausta

base64.js 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. 'use strict';
  2. var utils = require('./utils');
  3. var support = require('./support');
  4. // private property
  5. var _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
  6. // public method for encoding
  7. exports.encode = function(input) {
  8. var output = [];
  9. var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
  10. var i = 0, len = input.length, remainingBytes = len;
  11. var isArray = utils.getTypeOf(input) !== "string";
  12. while (i < input.length) {
  13. remainingBytes = len - i;
  14. if (!isArray) {
  15. chr1 = input.charCodeAt(i++);
  16. chr2 = i < len ? input.charCodeAt(i++) : 0;
  17. chr3 = i < len ? input.charCodeAt(i++) : 0;
  18. } else {
  19. chr1 = input[i++];
  20. chr2 = i < len ? input[i++] : 0;
  21. chr3 = i < len ? input[i++] : 0;
  22. }
  23. enc1 = chr1 >> 2;
  24. enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
  25. enc3 = remainingBytes > 1 ? (((chr2 & 15) << 2) | (chr3 >> 6)) : 64;
  26. enc4 = remainingBytes > 2 ? (chr3 & 63) : 64;
  27. output.push(_keyStr.charAt(enc1) + _keyStr.charAt(enc2) + _keyStr.charAt(enc3) + _keyStr.charAt(enc4));
  28. }
  29. return output.join("");
  30. };
  31. // public method for decoding
  32. exports.decode = function(input) {
  33. var chr1, chr2, chr3;
  34. var enc1, enc2, enc3, enc4;
  35. var i = 0, resultIndex = 0;
  36. var dataUrlPrefix = "data:";
  37. if (input.substr(0, dataUrlPrefix.length) === dataUrlPrefix) {
  38. // This is a common error: people give a data url
  39. // (data:image/png;base64,iVBOR...) with a {base64: true} and
  40. // wonders why things don't work.
  41. // We can detect that the string input looks like a data url but we
  42. // *can't* be sure it is one: removing everything up to the comma would
  43. // be too dangerous.
  44. throw new Error("Invalid base64 input, it looks like a data url.");
  45. }
  46. input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
  47. var totalLength = input.length * 3 / 4;
  48. if(input.charAt(input.length - 1) === _keyStr.charAt(64)) {
  49. totalLength--;
  50. }
  51. if(input.charAt(input.length - 2) === _keyStr.charAt(64)) {
  52. totalLength--;
  53. }
  54. if (totalLength % 1 !== 0) {
  55. // totalLength is not an integer, the length does not match a valid
  56. // base64 content. That can happen if:
  57. // - the input is not a base64 content
  58. // - the input is *almost* a base64 content, with a extra chars at the
  59. // beginning or at the end
  60. // - the input uses a base64 variant (base64url for example)
  61. throw new Error("Invalid base64 input, bad content length.");
  62. }
  63. var output;
  64. if (support.uint8array) {
  65. output = new Uint8Array(totalLength|0);
  66. } else {
  67. output = new Array(totalLength|0);
  68. }
  69. while (i < input.length) {
  70. enc1 = _keyStr.indexOf(input.charAt(i++));
  71. enc2 = _keyStr.indexOf(input.charAt(i++));
  72. enc3 = _keyStr.indexOf(input.charAt(i++));
  73. enc4 = _keyStr.indexOf(input.charAt(i++));
  74. chr1 = (enc1 << 2) | (enc2 >> 4);
  75. chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
  76. chr3 = ((enc3 & 3) << 6) | enc4;
  77. output[resultIndex++] = chr1;
  78. if (enc3 !== 64) {
  79. output[resultIndex++] = chr2;
  80. }
  81. if (enc4 !== 64) {
  82. output[resultIndex++] = chr3;
  83. }
  84. }
  85. return output;
  86. };