base64.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. // From: http://hg.mozilla.org/mozilla-central/raw-file/ec10630b1a54/js/src/devtools/jint/sunspider/string-base64.js
  5. import * as Log from './util/logging.js';
  6. export default {
  7. /* Convert data (an array of integers) to a Base64 string. */
  8. toBase64Table: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.split(''),
  9. base64Pad: '=',
  10. encode(data) {
  11. "use strict";
  12. let result = '';
  13. const length = data.length;
  14. const lengthpad = (length % 3);
  15. // Convert every three bytes to 4 ascii characters.
  16. for (let i = 0; i < (length - 2); i += 3) {
  17. result += this.toBase64Table[data[i] >> 2];
  18. result += this.toBase64Table[((data[i] & 0x03) << 4) + (data[i + 1] >> 4)];
  19. result += this.toBase64Table[((data[i + 1] & 0x0f) << 2) + (data[i + 2] >> 6)];
  20. result += this.toBase64Table[data[i + 2] & 0x3f];
  21. }
  22. // Convert the remaining 1 or 2 bytes, pad out to 4 characters.
  23. const j = length - lengthpad;
  24. if (lengthpad === 2) {
  25. result += this.toBase64Table[data[j] >> 2];
  26. result += this.toBase64Table[((data[j] & 0x03) << 4) + (data[j + 1] >> 4)];
  27. result += this.toBase64Table[(data[j + 1] & 0x0f) << 2];
  28. result += this.toBase64Table[64];
  29. } else if (lengthpad === 1) {
  30. result += this.toBase64Table[data[j] >> 2];
  31. result += this.toBase64Table[(data[j] & 0x03) << 4];
  32. result += this.toBase64Table[64];
  33. result += this.toBase64Table[64];
  34. }
  35. return result;
  36. },
  37. /* Convert Base64 data to a string */
  38. /* eslint-disable comma-spacing */
  39. toBinaryTable: [
  40. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
  41. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
  42. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
  43. 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1,
  44. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
  45. 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
  46. -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
  47. 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
  48. ],
  49. /* eslint-enable comma-spacing */
  50. decode(data, offset = 0) {
  51. let data_length = data.indexOf('=') - offset;
  52. if (data_length < 0) { data_length = data.length - offset; }
  53. /* Every four characters is 3 resulting numbers */
  54. const result_length = (data_length >> 2) * 3 + Math.floor((data_length % 4) / 1.5);
  55. const result = new Array(result_length);
  56. // Convert one by one.
  57. let leftbits = 0; // number of bits decoded, but yet to be appended
  58. let leftdata = 0; // bits decoded, but yet to be appended
  59. for (let idx = 0, i = offset; i < data.length; i++) {
  60. const c = this.toBinaryTable[data.charCodeAt(i) & 0x7f];
  61. const padding = (data.charAt(i) === this.base64Pad);
  62. // Skip illegal characters and whitespace
  63. if (c === -1) {
  64. Log.Error("Illegal character code " + data.charCodeAt(i) + " at position " + i);
  65. continue;
  66. }
  67. // Collect data into leftdata, update bitcount
  68. leftdata = (leftdata << 6) | c;
  69. leftbits += 6;
  70. // If we have 8 or more bits, append 8 bits to the result
  71. if (leftbits >= 8) {
  72. leftbits -= 8;
  73. // Append if not padding.
  74. if (!padding) {
  75. result[idx++] = (leftdata >> leftbits) & 0xff;
  76. }
  77. leftdata &= (1 << leftbits) - 1;
  78. }
  79. }
  80. // If there are any bits left, the base64 string was corrupted
  81. if (leftbits) {
  82. const err = new Error('Corrupted base64 string');
  83. err.name = 'Base64-Error';
  84. throw err;
  85. }
  86. return result;
  87. }
  88. }; /* End of Base64 namespace */