index.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. // Support decoding URL-safe base64 strings, as Node.js does.
  14. // See: https://en.wikipedia.org/wiki/Base64#URL_applications
  15. revLookup['-'.charCodeAt(0)] = 62
  16. revLookup['_'.charCodeAt(0)] = 63
  17. function getLens (b64) {
  18. var len = b64.length
  19. if (len % 4 > 0) {
  20. throw new Error('Invalid string. Length must be a multiple of 4')
  21. }
  22. // Trim off extra bytes after placeholder bytes are found
  23. // See: https://github.com/beatgammit/base64-js/issues/42
  24. var validLen = b64.indexOf('=')
  25. if (validLen === -1) validLen = len
  26. var placeHoldersLen = validLen === len
  27. ? 0
  28. : 4 - (validLen % 4)
  29. return [validLen, placeHoldersLen]
  30. }
  31. // base64 is 4/3 + up to two characters of the original data
  32. function byteLength (b64) {
  33. var lens = getLens(b64)
  34. var validLen = lens[0]
  35. var placeHoldersLen = lens[1]
  36. return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
  37. }
  38. function _byteLength (b64, validLen, placeHoldersLen) {
  39. return ((validLen + placeHoldersLen) * 3 / 4) - placeHoldersLen
  40. }
  41. function toByteArray (b64) {
  42. var tmp
  43. var lens = getLens(b64)
  44. var validLen = lens[0]
  45. var placeHoldersLen = lens[1]
  46. var arr = new Arr(_byteLength(b64, validLen, placeHoldersLen))
  47. var curByte = 0
  48. // if there are placeholders, only get up to the last complete 4 chars
  49. var len = placeHoldersLen > 0
  50. ? validLen - 4
  51. : validLen
  52. for (var i = 0; i < len; i += 4) {
  53. tmp =
  54. (revLookup[b64.charCodeAt(i)] << 18) |
  55. (revLookup[b64.charCodeAt(i + 1)] << 12) |
  56. (revLookup[b64.charCodeAt(i + 2)] << 6) |
  57. revLookup[b64.charCodeAt(i + 3)]
  58. arr[curByte++] = (tmp >> 16) & 0xFF
  59. arr[curByte++] = (tmp >> 8) & 0xFF
  60. arr[curByte++] = tmp & 0xFF
  61. }
  62. if (placeHoldersLen === 2) {
  63. tmp =
  64. (revLookup[b64.charCodeAt(i)] << 2) |
  65. (revLookup[b64.charCodeAt(i + 1)] >> 4)
  66. arr[curByte++] = tmp & 0xFF
  67. }
  68. if (placeHoldersLen === 1) {
  69. tmp =
  70. (revLookup[b64.charCodeAt(i)] << 10) |
  71. (revLookup[b64.charCodeAt(i + 1)] << 4) |
  72. (revLookup[b64.charCodeAt(i + 2)] >> 2)
  73. arr[curByte++] = (tmp >> 8) & 0xFF
  74. arr[curByte++] = tmp & 0xFF
  75. }
  76. return arr
  77. }
  78. function tripletToBase64 (num) {
  79. return lookup[num >> 18 & 0x3F] +
  80. lookup[num >> 12 & 0x3F] +
  81. lookup[num >> 6 & 0x3F] +
  82. lookup[num & 0x3F]
  83. }
  84. function encodeChunk (uint8, start, end) {
  85. var tmp
  86. var output = []
  87. for (var i = start; i < end; i += 3) {
  88. tmp =
  89. ((uint8[i] << 16) & 0xFF0000) +
  90. ((uint8[i + 1] << 8) & 0xFF00) +
  91. (uint8[i + 2] & 0xFF)
  92. output.push(tripletToBase64(tmp))
  93. }
  94. return output.join('')
  95. }
  96. function fromByteArray (uint8) {
  97. var tmp
  98. var len = uint8.length
  99. var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes
  100. var parts = []
  101. var maxChunkLength = 16383 // must be multiple of 3
  102. // go through the array every three bytes, we'll deal with trailing stuff later
  103. for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
  104. parts.push(encodeChunk(
  105. uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)
  106. ))
  107. }
  108. // pad the end with zeros, but make sure to not forget the extra bytes
  109. if (extraBytes === 1) {
  110. tmp = uint8[len - 1]
  111. parts.push(
  112. lookup[tmp >> 2] +
  113. lookup[(tmp << 4) & 0x3F] +
  114. '=='
  115. )
  116. } else if (extraBytes === 2) {
  117. tmp = (uint8[len - 2] << 8) + uint8[len - 1]
  118. parts.push(
  119. lookup[tmp >> 10] +
  120. lookup[(tmp >> 4) & 0x3F] +
  121. lookup[(tmp << 2) & 0x3F] +
  122. '='
  123. )
  124. }
  125. return parts.join('')
  126. }