Repositorio del curso CCOM4030 el semestre B91 del proyecto Paz para la Mujer

index.js 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. var i
  53. for (i = 0; i < len; i += 4) {
  54. tmp =
  55. (revLookup[b64.charCodeAt(i)] << 18) |
  56. (revLookup[b64.charCodeAt(i + 1)] << 12) |
  57. (revLookup[b64.charCodeAt(i + 2)] << 6) |
  58. revLookup[b64.charCodeAt(i + 3)]
  59. arr[curByte++] = (tmp >> 16) & 0xFF
  60. arr[curByte++] = (tmp >> 8) & 0xFF
  61. arr[curByte++] = tmp & 0xFF
  62. }
  63. if (placeHoldersLen === 2) {
  64. tmp =
  65. (revLookup[b64.charCodeAt(i)] << 2) |
  66. (revLookup[b64.charCodeAt(i + 1)] >> 4)
  67. arr[curByte++] = tmp & 0xFF
  68. }
  69. if (placeHoldersLen === 1) {
  70. tmp =
  71. (revLookup[b64.charCodeAt(i)] << 10) |
  72. (revLookup[b64.charCodeAt(i + 1)] << 4) |
  73. (revLookup[b64.charCodeAt(i + 2)] >> 2)
  74. arr[curByte++] = (tmp >> 8) & 0xFF
  75. arr[curByte++] = tmp & 0xFF
  76. }
  77. return arr
  78. }
  79. function tripletToBase64 (num) {
  80. return lookup[num >> 18 & 0x3F] +
  81. lookup[num >> 12 & 0x3F] +
  82. lookup[num >> 6 & 0x3F] +
  83. lookup[num & 0x3F]
  84. }
  85. function encodeChunk (uint8, start, end) {
  86. var tmp
  87. var output = []
  88. for (var i = start; i < end; i += 3) {
  89. tmp =
  90. ((uint8[i] << 16) & 0xFF0000) +
  91. ((uint8[i + 1] << 8) & 0xFF00) +
  92. (uint8[i + 2] & 0xFF)
  93. output.push(tripletToBase64(tmp))
  94. }
  95. return output.join('')
  96. }
  97. function fromByteArray (uint8) {
  98. var tmp
  99. var len = uint8.length
  100. var extraBytes = len % 3 // if we have 1 byte left, pad 2 bytes
  101. var parts = []
  102. var maxChunkLength = 16383 // must be multiple of 3
  103. // go through the array every three bytes, we'll deal with trailing stuff later
  104. for (var i = 0, len2 = len - extraBytes; i < len2; i += maxChunkLength) {
  105. parts.push(encodeChunk(
  106. uint8, i, (i + maxChunkLength) > len2 ? len2 : (i + maxChunkLength)
  107. ))
  108. }
  109. // pad the end with zeros, but make sure to not forget the extra bytes
  110. if (extraBytes === 1) {
  111. tmp = uint8[len - 1]
  112. parts.push(
  113. lookup[tmp >> 2] +
  114. lookup[(tmp << 4) & 0x3F] +
  115. '=='
  116. )
  117. } else if (extraBytes === 2) {
  118. tmp = (uint8[len - 2] << 8) + uint8[len - 1]
  119. parts.push(
  120. lookup[tmp >> 10] +
  121. lookup[(tmp >> 4) & 0x3F] +
  122. lookup[(tmp << 2) & 0x3F] +
  123. '='
  124. )
  125. }
  126. return parts.join('')
  127. }