Browse Source

remove self-executing function

if folks miss the ability to drop this into the browser, open an issue
and we’ll add a built file to the repo
master
Feross Aboukhadijeh 9 years ago
parent
commit
370cdf6eb5
  1. 228
      lib/b64.js

228
lib/b64.js

@ -1,131 +1,129 @@
;(function (exports) { 'use strict'
'use strict'
var i
var i var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
var code = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/' var lookup = []
var lookup = [] for (i = 0; i < code.length; i++) {
for (i = 0; i < code.length; i++) { lookup[i] = code[i]
lookup[i] = code[i] }
var revLookup = []
for (i = 0; i < code.length; ++i) {
revLookup[code.charCodeAt(i)] = i
}
revLookup['-'.charCodeAt(0)] = 62
revLookup['_'.charCodeAt(0)] = 63
var Arr = (typeof Uint8Array !== 'undefined')
? Uint8Array
: Array
function decode (elt) {
var v = revLookup[elt.charCodeAt(0)]
return v !== undefined ? v : -1
}
function b64ToByteArray (b64) {
var i, j, l, tmp, placeHolders, arr
if (b64.length % 4 > 0) {
throw new Error('Invalid string. Length must be a multiple of 4')
} }
var revLookup = []
for (i = 0; i < code.length; ++i) { // the number of equal signs (place holders)
revLookup[code.charCodeAt(i)] = i // if there are two placeholders, than the two characters before it
} // represent one byte
revLookup['-'.charCodeAt(0)] = 62 // if there is only one, then the three characters before it represent 2 bytes
revLookup['_'.charCodeAt(0)] = 63 // this is just a cheap hack to not do indexOf twice
var len = b64.length
placeHolders = b64.charAt(len - 2) === '=' ? 2 : b64.charAt(len - 1) === '=' ? 1 : 0
// base64 is 4/3 + up to two characters of the original data
arr = new Arr(b64.length * 3 / 4 - placeHolders)
var Arr = (typeof Uint8Array !== 'undefined') // if there are placeholders, only get up to the last complete 4 chars
? Uint8Array l = placeHolders > 0 ? b64.length - 4 : b64.length
: Array
function decode (elt) { var L = 0
var v = revLookup[elt.charCodeAt(0)]
return v !== undefined ? v : -1 function push (v) {
arr[L++] = v
} }
function b64ToByteArray (b64) { for (i = 0, j = 0; i < l; i += 4, j += 3) {
var i, j, l, tmp, placeHolders, arr tmp = (decode(b64.charAt(i)) << 18) | (decode(b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3))
push((tmp & 0xFF0000) >> 16)
if (b64.length % 4 > 0) { push((tmp & 0xFF00) >> 8)
throw new Error('Invalid string. Length must be a multiple of 4') push(tmp & 0xFF)
}
// the number of equal signs (place holders)
// if there are two placeholders, than the two characters before it
// represent one byte
// if there is only one, then the three characters before it represent 2 bytes
// this is just a cheap hack to not do indexOf twice
var len = b64.length
placeHolders = b64.charAt(len - 2) === '=' ? 2 : b64.charAt(len - 1) === '=' ? 1 : 0
// base64 is 4/3 + up to two characters of the original data
arr = new Arr(b64.length * 3 / 4 - placeHolders)
// if there are placeholders, only get up to the last complete 4 chars
l = placeHolders > 0 ? b64.length - 4 : b64.length
var L = 0
function push (v) {
arr[L++] = v
}
for (i = 0, j = 0; i < l; i += 4, j += 3) {
tmp = (decode(b64.charAt(i)) << 18) | (decode(b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3))
push((tmp & 0xFF0000) >> 16)
push((tmp & 0xFF00) >> 8)
push(tmp & 0xFF)
}
if (placeHolders === 2) {
tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4)
push(tmp & 0xFF)
} else if (placeHolders === 1) {
tmp = (decode(b64.charAt(i)) << 10) | (decode(b64.charAt(i + 1)) << 4) | (decode(b64.charAt(i + 2)) >> 2)
push((tmp >> 8) & 0xFF)
push(tmp & 0xFF)
}
return arr
} }
function encode (num) { if (placeHolders === 2) {
return lookup[num] tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4)
push(tmp & 0xFF)
} else if (placeHolders === 1) {
tmp = (decode(b64.charAt(i)) << 10) | (decode(b64.charAt(i + 1)) << 4) | (decode(b64.charAt(i + 2)) >> 2)
push((tmp >> 8) & 0xFF)
push(tmp & 0xFF)
} }
function tripletToBase64 (num) { return arr
return encode(num >> 18 & 0x3F) + encode(num >> 12 & 0x3F) + encode(num >> 6 & 0x3F) + encode(num & 0x3F) }
function encode (num) {
return lookup[num]
}
function tripletToBase64 (num) {
return encode(num >> 18 & 0x3F) + encode(num >> 12 & 0x3F) + encode(num >> 6 & 0x3F) + encode(num & 0x3F)
}
function encodeChunk (uint8, start, end) {
var temp
var output = []
for (var i = start; i < end; i += 3) {
temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])
output.push(tripletToBase64(temp))
} }
return output.join('')
}
function uint8ToBase64 (uint8) {
var i
var extraBytes = uint8.length % 3 // if we have 1 byte left, pad 2 bytes
var output = ''
var parts = []
var temp, length
var maxChunkLength = 16383 // must be multiple of 3
function encodeChunk (uint8, start, end) { // go through the array every three bytes, we'll deal with trailing stuff later
var temp
var output = [] for (i = 0, length = uint8.length - extraBytes; i < length; i += maxChunkLength) {
for (var i = start; i < end; i += 3) { parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > length ? length : (i + maxChunkLength)))
temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2])
output.push(tripletToBase64(temp))
}
return output.join('')
} }
function uint8ToBase64 (uint8) { // pad the end with zeros, but make sure to not forget the extra bytes
var i switch (extraBytes) {
var extraBytes = uint8.length % 3 // if we have 1 byte left, pad 2 bytes case 1:
var output = '' temp = uint8[uint8.length - 1]
var parts = [] output += encode(temp >> 2)
var temp, length output += encode((temp << 4) & 0x3F)
var maxChunkLength = 16383 // must be multiple of 3 output += '=='
break
// go through the array every three bytes, we'll deal with trailing stuff later case 2:
temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1])
for (i = 0, length = uint8.length - extraBytes; i < length; i += maxChunkLength) { output += encode(temp >> 10)
parts.push(encodeChunk(uint8, i, (i + maxChunkLength) > length ? length : (i + maxChunkLength))) output += encode((temp >> 4) & 0x3F)
} output += encode((temp << 2) & 0x3F)
output += '='
// pad the end with zeros, but make sure to not forget the extra bytes break
switch (extraBytes) { default:
case 1: break
temp = uint8[uint8.length - 1]
output += encode(temp >> 2)
output += encode((temp << 4) & 0x3F)
output += '=='
break
case 2:
temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1])
output += encode(temp >> 10)
output += encode((temp >> 4) & 0x3F)
output += encode((temp << 2) & 0x3F)
output += '='
break
default:
break
}
parts.push(output)
return parts.join('')
} }
exports.toByteArray = b64ToByteArray parts.push(output)
exports.fromByteArray = uint8ToBase64
}(typeof exports === 'undefined' ? (this.base64js = {}) : exports)) return parts.join('')
}
exports.toByteArray = b64ToByteArray
exports.fromByteArray = uint8ToBase64

Loading…
Cancel
Save