index.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. 'use strict'
  2. exports.byteLength = byteLength
  3. exports.toByteArray = toByteArray
  4. exports.fromByteArray = fromByteArray
  5. var lookup = []
  6. var revLookup = []
  7. var Arr = typeof Uint8Array !== 'undefined' ? Uint8Array : Array
  8. var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  9. for (var i = 0, len = code.length; i < len; ++i) {
  10. lookup[i] = code[i]
  11. revLookup[code.charCodeAt(i)] = i
  12. }
  13. revLookup['-'.charCodeAt(0)] = 62
  14. revLookup['_'.charCodeAt(0)] = 63
  15. function placeHoldersCount (b64) {
  16. var len = b64.length
  17. if (len % 4 > 0) {
  18. throw new Error('Invalid string. Length must be a multiple of 4')
  19. }
  20. // the number of equal signs (place holders)
  21. // if there are two placeholders, than the two characters before it
  22. // represent one byte
  23. // if there is only one, then the three characters before it represent 2 bytes
  24. // this is just a cheap hack to not do indexOf twice
  25. return b64[len - 2] === '=' ? 2 : b64[len - 1] === '=' ? 1 : 0
  26. }
  27. function byteLength (b64) {
  28. // base64 is 4/3 + up to two characters of the original data
  29. return (b64.length * 3 / 4) - placeHoldersCount(b64)
  30. }
  31. function toByteArray (b64) {
  32. var i, l, tmp, placeHolders, arr
  33. var len = b64.length
  34. placeHolders = placeHoldersCount(b64)
  35. arr = new Arr((len * 3 / 4) - placeHolders)
  36. // if there are placeholders, only get up to the last complete 4 chars
  37. l = placeHolders > 0 ? len - 4 : len
  38. var L = 0
  39. for (i = 0; i < l; i += 4) {
  40. tmp = (revLookup[b64.charCodeAt(i)] << 18) | (revLookup[b64.charCodeAt(i + 1)] << 12) | (revLookup[b64.charCodeAt(i + 2)] << 6) | revLookup[b64.charCodeAt(i + 3)]
  41. arr[L++] = (tmp >> 16) & 0xFF
  42. arr[L++] = (tmp >> 8) & 0xFF
  43. arr[L++] = tmp & 0xFF
  44. }
  45. if (placeHolders === 2) {
  46. tmp = (revLookup[b64.charCodeAt(i)] << 2) | (revLookup[b64.charCodeAt(i + 1)] >> 4)
  47. arr[L++] = tmp & 0xFF
  48. } else if (placeHolders === 1) {
  49. tmp = (revLookup[b64.charCodeAt(i)] << 10) | (revLookup[b64.charCodeAt(i + 1)] << 4) | (revLookup[b64.charCodeAt(i + 2)] >> 2)
  50. arr[L++] = (tmp >> 8) & 0xFF
  51. arr[L++] = tmp & 0xFF
  52. }
  53. return arr
  54. }
  55. function tripletToBase64 (num) {
  56. return lookup[num >> 18 & 0x3F] + lookup[num >> 12 & 0x3F] + lookup[num >> 6 & 0x3F] + lookup[num & 0x3F]
  57. }
  58. function encodeChunk (uint8, start, end) {
  59. var tmp
  60. var output = []
  61. for (var i = start; i < end; i += 3) {
  62. tmp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])
  63. output.push(tripletToBase64(tmp))
  64. }
  65. return output.join('')
  66. }
  67. function fromByteArray (uint8) {
  68. var tmp
  69. var len = uint8.length
  70. var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes
  71. var output = ''
  72. var parts = []
  73. var maxChunkLength = 16383 // must be multiple of 3
  74. // go through the array every three bytes, we'll deal with trailing stuff later
  75. for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
  76. parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)))
  77. }
  78. // pad the end with zeros, but make sure to not forget the extra bytes
  79. if (extraBytes === 1) {
  80. tmp = uint8[len - 1]
  81. output += lookup[tmp >> 2]
  82. output += lookup[(tmp << 4) & 0x3F]
  83. output += '=='
  84. } else if (extraBytes === 2) {
  85. tmp = (uint8[len - 2] << 8) + (uint8[len - 1])
  86. output += lookup[tmp >> 10]
  87. output += lookup[(tmp >> 4) & 0x3F]
  88. output += lookup[(tmp << 2) & 0x3F]
  89. output += '='
  90. }
  91. parts.push(output)
  92. return parts.join('')
  93. }