Proper Base64 module
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by visionmedia · committed by visionmedia · parent 39609af · Browse files · 1 file·+61 added−64 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
1 file+61 added−64 removed
- other:+61 added−64 removed
Provenance
- Commit
4ea0e85- Tree
e55a6f2- Author
visionmedia <tj@vision-media.ca>- Committer
visionmedia <tj@vision-media.ca>- Parent
39609af
1 file changed+61 added−64 removed
modified:lib/support/base64.js+61 added-64 removed
8 unmodified lines9101112131314151617181920212223242526272829303132333435363738394041151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374424344454647484950515253545556575859606162636465666768697071727576737478758 unmodified lines * * Modified by TJ Holowaychuk for CommonJS module support. */ exports.Base64 = {/** * Base64 encode _str_. * * @param {string} str * @return {string} * @api public */
exports.encode = function(str) { var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; var encoded = []; var c = 0; while (c < str.length) { var b0 = str.charCodeAt(c++); var b1 = str.charCodeAt(c++); var b2 = str.charCodeAt(c++); var buf = (b0 << 16) + ((b1 || 0) << 8) + (b2 || 0); var i0 = (buf & (63 << 18)) >> 18; var i1 = (buf & (63 << 12)) >> 12; var i2 = isNaN(b1) ? 64 : (buf & (63 << 6)) >> 6; var i3 = isNaN(b2) ? 64 : (buf & 63); encoded[encoded.length] = chars.charAt(i0); encoded[encoded.length] = chars.charAt(i1); encoded[encoded.length] = chars.charAt(i2); encoded[encoded.length] = chars.charAt(i3); } return encoded.join('');} /** * Base64 encode _str_. * * @param {string} str * @return {string} * @api public */ encode: function(str) { var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; var encoded = []; var c = 0; while (c < str.length) { var b0 = str.charCodeAt(c++); var b1 = str.charCodeAt(c++); var b2 = str.charCodeAt(c++); var buf = (b0 << 16) + ((b1 || 0) << 8) + (b2 || 0); var i0 = (buf & (63 << 18)) >> 18; var i1 = (buf & (63 << 12)) >> 12; var i2 = isNaN(b1) ? 64 : (buf & (63 << 6)) >> 6; var i3 = isNaN(b2) ? 64 : (buf & 63); encoded[encoded.length] = chars.charAt(i0); encoded[encoded.length] = chars.charAt(i1); encoded[encoded.length] = chars.charAt(i2); encoded[encoded.length] = chars.charAt(i3); } return encoded.join(''); }, /** * Decode base64 encoded _str_. * * @param {string} str * @return {string} * @api public */ decode: function(str) { var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; var invalid = { strlen: (str.length % 4 != 0), chars: new RegExp('[^' + chars + ']').test(str), equals: (/=/.test(str) && (/=[^=]/.test(str) || /={3}/.test(str))) }; if (invalid.strlen || invalid.chars || invalid.equals) throw new Error('Invalid base64 data'); var decoded = []; var c = 0; while (c < str.length) { var i0 = chars.indexOf(str.charAt(c++)); var i1 = chars.indexOf(str.charAt(c++)); var i2 = chars.indexOf(str.charAt(c++)); var i3 = chars.indexOf(str.charAt(c++)); var buf = (i0 << 18) + (i1 << 12) + ((i2 & 63) << 6) + (i3 & 63); var b0 = (buf & (255 << 16)) >> 16; var b1 = (i2 == 64) ? -1 : (buf & (255 << 8)) >> 8; var b2 = (i3 == 64) ? -1 : (buf & 255); decoded[decoded.length] = String.fromCharCode(b0); if (b1 >= 0) decoded[decoded.length] = String.fromCharCode(b1); if (b2 >= 0) decoded[decoded.length] = String.fromCharCode(b2);/** * Decode base64 encoded _str_. * * @param {string} str * @return {string} * @api public */
exports.decode = function(str) { var chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; var invalid = { strlen: (str.length % 4 != 0), chars: new RegExp('[^' + chars + ']').test(str), equals: (/=/.test(str) && (/=[^=]/.test(str) || /={3}/.test(str))) }; if (invalid.strlen || invalid.chars || invalid.equals) throw new Error('Invalid base64 data'); var decoded = []; var c = 0; while (c < str.length) { var i0 = chars.indexOf(str.charAt(c++)); var i1 = chars.indexOf(str.charAt(c++)); var i2 = chars.indexOf(str.charAt(c++)); var i3 = chars.indexOf(str.charAt(c++)); var buf = (i0 << 18) + (i1 << 12) + ((i2 & 63) << 6) + (i3 & 63); var b0 = (buf & (255 << 16)) >> 16; var b1 = (i2 == 64) ? -1 : (buf & (255 << 8)) >> 8; var b2 = (i3 == 64) ? -1 : (buf & 255); decoded[decoded.length] = String.fromCharCode(b0); if (b1 >= 0) decoded[decoded.length] = String.fromCharCode(b1); if (b2 >= 0) decoded[decoded.length] = String.fromCharCode(b2); } return decoded.join(''); } return decoded.join('');}No newline at end of file}