base64.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. /*jslint white: false */
  6. /*global console */
  7. var Base64 = {
  8. /* Convert data (an array of integers) to a Base64 string. */
  9. toBase64Table : 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='.split(''),
  10. base64Pad : '=',
  11. encode: function (data) {
  12. "use strict";
  13. var result = '';
  14. var toBase64Table = Base64.toBase64Table;
  15. var length = data.length;
  16. var lengthpad = (length % 3);
  17. // Convert every three bytes to 4 ascii characters.
  18. for (var i = 0; i < (length - 2); i += 3) {
  19. result += toBase64Table[data[i] >> 2];
  20. result += toBase64Table[((data[i] & 0x03) << 4) + (data[i + 1] >> 4)];
  21. result += toBase64Table[((data[i + 1] & 0x0f) << 2) + (data[i + 2] >> 6)];
  22. result += toBase64Table[data[i + 2] & 0x3f];
  23. }
  24. // Convert the remaining 1 or 2 bytes, pad out to 4 characters.
  25. var j = 0;
  26. if (lengthpad === 2) {
  27. j = length - lengthpad;
  28. result += toBase64Table[data[j] >> 2];
  29. result += toBase64Table[((data[j] & 0x03) << 4) + (data[j + 1] >> 4)];
  30. result += toBase64Table[(data[j + 1] & 0x0f) << 2];
  31. result += toBase64Table[64];
  32. } else if (lengthpad === 1) {
  33. j = length - lengthpad;
  34. result += toBase64Table[data[j] >> 2];
  35. result += toBase64Table[(data[j] & 0x03) << 4];
  36. result += toBase64Table[64];
  37. result += toBase64Table[64];
  38. }
  39. return result;
  40. },
  41. /* Convert Base64 data to a string */
  42. /* jshint -W013 */
  43. toBinaryTable : [
  44. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
  45. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
  46. -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
  47. 52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1, 0,-1,-1,
  48. -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,
  49. 15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
  50. -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
  51. 41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
  52. ],
  53. /* jshint +W013 */
  54. decode: function (data, offset) {
  55. "use strict";
  56. offset = typeof(offset) !== 'undefined' ? offset : 0;
  57. var toBinaryTable = Base64.toBinaryTable;
  58. var base64Pad = Base64.base64Pad;
  59. var result, result_length;
  60. var leftbits = 0; // number of bits decoded, but yet to be appended
  61. var leftdata = 0; // bits decoded, but yet to be appended
  62. var data_length = data.indexOf('=') - offset;
  63. if (data_length < 0) { data_length = data.length - offset; }
  64. /* Every four characters is 3 resulting numbers */
  65. result_length = (data_length >> 2) * 3 + Math.floor((data_length % 4) / 1.5);
  66. result = new Array(result_length);
  67. // Convert one by one.
  68. for (var idx = 0, i = offset; i < data.length; i++) {
  69. var c = toBinaryTable[data.charCodeAt(i) & 0x7f];
  70. var padding = (data.charAt(i) === base64Pad);
  71. // Skip illegal characters and whitespace
  72. if (c === -1) {
  73. console.error("Illegal character code " + data.charCodeAt(i) + " at position " + i);
  74. continue;
  75. }
  76. // Collect data into leftdata, update bitcount
  77. leftdata = (leftdata << 6) | c;
  78. leftbits += 6;
  79. // If we have 8 or more bits, append 8 bits to the result
  80. if (leftbits >= 8) {
  81. leftbits -= 8;
  82. // Append if not padding.
  83. if (!padding) {
  84. result[idx++] = (leftdata >> leftbits) & 0xff;
  85. }
  86. leftdata &= (1 << leftbits) - 1;
  87. }
  88. }
  89. // If there are any bits left, the base64 string was corrupted
  90. if (leftbits) {
  91. err = new Error('Corrupted base64 string');
  92. err.name = 'Base64-Error';
  93. throw err;
  94. }
  95. return result;
  96. }
  97. }; /* End of Base64 namespace */