Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

base64.js 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. /* -*- Mode: js; js-indent-level: 2; -*- */
  2. /*
  3. * Copyright 2011 Mozilla Foundation and contributors
  4. * Licensed under the New BSD license. See LICENSE or:
  5. * http://opensource.org/licenses/BSD-3-Clause
  6. */
  7. var intToCharMap = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'.split('');
  8. /**
  9. * Encode an integer in the range of 0 to 63 to a single base 64 digit.
  10. */
  11. exports.encode = function (number) {
  12. if (0 <= number && number < intToCharMap.length) {
  13. return intToCharMap[number];
  14. }
  15. throw new TypeError("Must be between 0 and 63: " + number);
  16. };
  17. /**
  18. * Decode a single base 64 character code digit to an integer. Returns -1 on
  19. * failure.
  20. */
  21. exports.decode = function (charCode) {
  22. var bigA = 65; // 'A'
  23. var bigZ = 90; // 'Z'
  24. var littleA = 97; // 'a'
  25. var littleZ = 122; // 'z'
  26. var zero = 48; // '0'
  27. var nine = 57; // '9'
  28. var plus = 43; // '+'
  29. var slash = 47; // '/'
  30. var littleOffset = 26;
  31. var numberOffset = 52;
  32. // 0 - 25: ABCDEFGHIJKLMNOPQRSTUVWXYZ
  33. if (bigA <= charCode && charCode <= bigZ) {
  34. return (charCode - bigA);
  35. }
  36. // 26 - 51: abcdefghijklmnopqrstuvwxyz
  37. if (littleA <= charCode && charCode <= littleZ) {
  38. return (charCode - littleA + littleOffset);
  39. }
  40. // 52 - 61: 0123456789
  41. if (zero <= charCode && charCode <= nine) {
  42. return (charCode - zero + numberOffset);
  43. }
  44. // 62: +
  45. if (charCode == plus) {
  46. return 62;
  47. }
  48. // 63: /
  49. if (charCode == slash) {
  50. return 63;
  51. }
  52. // Invalid base64 digit.
  53. return -1;
  54. };