annotate base64.js @ 110:2f621b00747e

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