Dominic Tarr
11 years ago
1 changed files with 108 additions and 98 deletions
@ -1,103 +1,113 @@ |
|||||
(function (exports) { |
(function (exports) { |
||||
'use strict'; |
'use strict'; |
||||
|
|
||||
var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; |
var lookup = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'; |
||||
|
|
||||
function b64ToByteArray(b64) { |
function decode (c) { |
||||
var i, j, l, tmp, placeHolders, arr; |
return indexOf(lookup, c) |
||||
|
} |
||||
if (b64.length % 4 > 0) { |
|
||||
throw 'Invalid string. Length must be a multiple of 4'; |
function b64ToByteArray(b64) { |
||||
} |
var i, j, l, tmp, placeHolders, arr; |
||||
|
|
||||
// the number of equal signs (place holders)
|
if (b64.length % 4 > 0) { |
||||
// if there are two placeholders, than the two characters before it
|
throw 'Invalid string. Length must be a multiple of 4'; |
||||
// 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
|
// the number of equal signs (place holders)
|
||||
placeHolders = indexOf(b64, '='); |
// if there are two placeholders, than the two characters before it
|
||||
placeHolders = placeHolders > 0 ? b64.length - placeHolders : 0; |
// represent one byte
|
||||
|
// if there is only one, then the three characters before it represent 2 bytes
|
||||
// base64 is 4/3 + up to two characters of the original data
|
// this is just a cheap hack to not do indexOf twice
|
||||
arr = [];//new Uint8Array(b64.length * 3 / 4 - placeHolders);
|
placeHolders = indexOf(b64, '='); |
||||
|
placeHolders = placeHolders > 0 ? b64.length - placeHolders : 0; |
||||
// if there are placeholders, only get up to the last complete 4 chars
|
|
||||
l = placeHolders > 0 ? b64.length - 4 : b64.length; |
// base64 is 4/3 + up to two characters of the original data
|
||||
|
arr = new Uint8Array(b64.length * 3 / 4 - placeHolders); |
||||
for (i = 0, j = 0; i < l; i += 4, j += 3) { |
|
||||
tmp = (indexOf(lookup, b64.charAt(i)) << 18) | (indexOf(lookup, b64.charAt(i + 1)) << 12) | (indexOf(lookup, b64.charAt(i + 2)) << 6) | indexOf(lookup, b64.charAt(i + 3)); |
// if there are placeholders, only get up to the last complete 4 chars
|
||||
arr.push((tmp & 0xFF0000) >> 16); |
l = placeHolders > 0 ? b64.length - 4 : b64.length; |
||||
arr.push((tmp & 0xFF00) >> 8); |
|
||||
arr.push(tmp & 0xFF); |
var L = 0 |
||||
} |
|
||||
|
function push (v) { |
||||
if (placeHolders === 2) { |
arr[L++] = v |
||||
tmp = (indexOf(lookup, b64.charAt(i)) << 2) | (indexOf(lookup, b64.charAt(i + 1)) >> 4); |
} |
||||
arr.push(tmp & 0xFF); |
|
||||
} else if (placeHolders === 1) { |
for (i = 0, j = 0; i < l; i += 4, j += 3) { |
||||
tmp = (indexOf(lookup, b64.charAt(i)) << 10) | (indexOf(lookup, b64.charAt(i + 1)) << 4) | (indexOf(lookup, b64.charAt(i + 2)) >> 2); |
tmp = (decode(b64.charAt(i)) << 18) | (indexOf(lookup, b64.charAt(i + 1)) << 12) | (decode(b64.charAt(i + 2)) << 6) | decode(b64.charAt(i + 3)); |
||||
arr.push((tmp >> 8) & 0xFF); |
push((tmp & 0xFF0000) >> 16); |
||||
arr.push(tmp & 0xFF); |
push((tmp & 0xFF00) >> 8); |
||||
} |
push(tmp & 0xFF); |
||||
|
} |
||||
return arr; |
|
||||
} |
if (placeHolders === 2) { |
||||
|
tmp = (decode(b64.charAt(i)) << 2) | (decode(b64.charAt(i + 1)) >> 4); |
||||
function uint8ToBase64(uint8) { |
push(tmp & 0xFF); |
||||
var i, |
} else if (placeHolders === 1) { |
||||
extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes
|
tmp = (indexOf(lookup, b64.charAt(i)) << 10) | (indexOf(lookup, b64.charAt(i + 1)) << 4) | (indexOf(lookup, b64.charAt(i + 2)) >> 2); |
||||
output = "", |
push((tmp >> 8) & 0xFF); |
||||
temp, length; |
push(tmp & 0xFF); |
||||
|
} |
||||
function tripletToBase64 (num) { |
|
||||
return lookup.charAt(num >> 18 & 0x3F) + lookup.charAt(num >> 12 & 0x3F) + lookup.charAt(num >> 6 & 0x3F) + lookup.charAt(num & 0x3F); |
return arr; |
||||
}; |
} |
||||
|
|
||||
// go through the array every three bytes, we'll deal with trailing stuff later
|
function uint8ToBase64(uint8) { |
||||
for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) { |
var i, |
||||
temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]); |
extraBytes = uint8.length % 3, // if we have 1 byte left, pad 2 bytes
|
||||
output += tripletToBase64(temp); |
output = "", |
||||
} |
temp, length; |
||||
|
|
||||
// pad the end with zeros, but make sure to not forget the extra bytes
|
function tripletToBase64 (num) { |
||||
switch (extraBytes) { |
return lookup.charAt(num >> 18 & 0x3F) + lookup.charAt(num >> 12 & 0x3F) + lookup.charAt(num >> 6 & 0x3F) + lookup.charAt(num & 0x3F); |
||||
case 1: |
}; |
||||
temp = uint8[uint8.length - 1]; |
|
||||
output += lookup.charAt(temp >> 2); |
// go through the array every three bytes, we'll deal with trailing stuff later
|
||||
output += lookup.charAt((temp << 4) & 0x3F); |
for (i = 0, length = uint8.length - extraBytes; i < length; i += 3) { |
||||
output += '=='; |
temp = (uint8[i] << 16) + (uint8[i + 1] << 8) + (uint8[i + 2]); |
||||
break; |
output += tripletToBase64(temp); |
||||
case 2: |
} |
||||
temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]); |
|
||||
output += lookup.charAt(temp >> 10); |
// pad the end with zeros, but make sure to not forget the extra bytes
|
||||
output += lookup.charAt((temp >> 4) & 0x3F); |
switch (extraBytes) { |
||||
output += lookup.charAt((temp << 2) & 0x3F); |
case 1: |
||||
output += '='; |
temp = uint8[uint8.length - 1]; |
||||
break; |
output += lookup.charAt(temp >> 2); |
||||
} |
output += lookup.charAt((temp << 4) & 0x3F); |
||||
|
output += '=='; |
||||
return output; |
break; |
||||
} |
case 2: |
||||
|
temp = (uint8[uint8.length - 2] << 8) + (uint8[uint8.length - 1]); |
||||
module.exports.toByteArray = b64ToByteArray; |
output += lookup.charAt(temp >> 10); |
||||
module.exports.fromByteArray = uint8ToBase64; |
output += lookup.charAt((temp >> 4) & 0x3F); |
||||
|
output += lookup.charAt((temp << 2) & 0x3F); |
||||
|
output += '='; |
||||
|
break; |
||||
|
} |
||||
|
|
||||
|
return output; |
||||
|
} |
||||
|
|
||||
|
module.exports.toByteArray = b64ToByteArray; |
||||
|
module.exports.fromByteArray = uint8ToBase64; |
||||
}()); |
}()); |
||||
|
|
||||
function indexOf (arr, elt /*, from*/) { |
function indexOf (arr, elt /*, from*/) { |
||||
var len = arr.length; |
var len = arr.length; |
||||
|
|
||||
var from = Number(arguments[1]) || 0; |
var from = Number(arguments[1]) || 0; |
||||
from = (from < 0) |
from = (from < 0) |
||||
? Math.ceil(from) |
? Math.ceil(from) |
||||
: Math.floor(from); |
: Math.floor(from); |
||||
if (from < 0) |
if (from < 0) |
||||
from += len; |
from += len; |
||||
|
|
||||
for (; from < len; from++) { |
for (; from < len; from++) { |
||||
if ((typeof arr === 'string' && arr.charAt(from) === elt) || |
if ((typeof arr === 'string' && arr.charAt(from) === elt) || |
||||
(typeof arr !== 'string' && arr[from] === elt)) { |
(typeof arr !== 'string' && arr[from] === elt)) { |
||||
return from; |
return from; |
||||
} |
} |
||||
} |
} |
||||
return -1; |
return -1; |
||||
} |
} |
||||
|
Loading…
Reference in new issue