jquery.base64.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*jslint adsafe: false, bitwise: true, browser: true, cap: false, css: false,
  2. debug: false, devel: true, eqeqeq: true, es5: false, evil: false,
  3. forin: false, fragment: false, immed: true, laxbreak: false, newcap: true,
  4. nomen: false, on: false, onevar: true, passfail: false, plusplus: true,
  5. regexp: false, rhino: true, safe: false, strict: false, sub: false,
  6. undef: true, white: false, widget: false, windows: false */
  7. /*global jQuery: false, window: false */
  8. //"use strict";
  9. /*
  10. * Original code (c) 2010 Nick Galbreath
  11. * http://code.google.com/p/stringencoders/source/browse/#svn/trunk/javascript
  12. *
  13. * jQuery port (c) 2010 Carlo Zottmann
  14. * http://github.com/carlo/jquery-base64
  15. *
  16. * Permission is hereby granted, free of charge, to any person
  17. * obtaining a copy of this software and associated documentation
  18. * files (the "Software"), to deal in the Software without
  19. * restriction, including without limitation the rights to use,
  20. * copy, modify, merge, publish, distribute, sublicense, and/or sell
  21. * copies of the Software, and to permit persons to whom the
  22. * Software is furnished to do so, subject to the following
  23. * conditions:
  24. *
  25. * The above copyright notice and this permission notice shall be
  26. * included in all copies or substantial portions of the Software.
  27. *
  28. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  29. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
  30. * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  31. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
  32. * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
  33. * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  34. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  35. * OTHER DEALINGS IN THE SOFTWARE.
  36. */
  37. /* base64 encode/decode compatible with window.btoa/atob
  38. *
  39. * window.atob/btoa is a Firefox extension to convert binary data (the "b")
  40. * to base64 (ascii, the "a").
  41. *
  42. * It is also found in Safari and Chrome. It is not available in IE.
  43. *
  44. * if (!window.btoa) window.btoa = $.base64.encode
  45. * if (!window.atob) window.atob = $.base64.decode
  46. *
  47. * The original spec's for atob/btoa are a bit lacking
  48. * https://developer.mozilla.org/en/DOM/window.atob
  49. * https://developer.mozilla.org/en/DOM/window.btoa
  50. *
  51. * window.btoa and $.base64.encode takes a string where charCodeAt is [0,255]
  52. * If any character is not [0,255], then an exception is thrown.
  53. *
  54. * window.atob and $.base64.decode take a base64-encoded string
  55. * If the input length is not a multiple of 4, or contains invalid characters
  56. * then an exception is thrown.
  57. */
  58. jQuery.base64 = ( function( $ ) {
  59. var _PADCHAR = "=",
  60. _ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
  61. _VERSION = "1.0";
  62. function _getbyte64( s, i ) {
  63. // This is oddly fast, except on Chrome/V8.
  64. // Minimal or no improvement in performance by using a
  65. // object with properties mapping chars to value (eg. 'A': 0)
  66. var idx = _ALPHA.indexOf( s.charAt( i ) );
  67. if ( idx === -1 ) {
  68. throw "Cannot decode base64";
  69. }
  70. return idx;
  71. }
  72. function _decode( s ) {
  73. var pads = 0,
  74. i,
  75. b10,
  76. imax = s.length,
  77. x = [];
  78. s = String( s );
  79. if ( imax === 0 ) {
  80. return s;
  81. }
  82. if ( imax % 4 !== 0 ) {
  83. throw "Cannot decode base64";
  84. }
  85. if ( s.charAt( imax - 1 ) === _PADCHAR ) {
  86. pads = 1;
  87. if ( s.charAt( imax - 2 ) === _PADCHAR ) {
  88. pads = 2;
  89. }
  90. // either way, we want to ignore this last block
  91. imax -= 4;
  92. }
  93. for ( i = 0; i < imax; i += 4 ) {
  94. b10 = ( _getbyte64( s, i ) << 18 ) | ( _getbyte64( s, i + 1 ) << 12 ) | ( _getbyte64( s, i + 2 ) << 6 ) | _getbyte64( s, i + 3 );
  95. x.push( String.fromCharCode( b10 >> 16, ( b10 >> 8 ) & 0xff, b10 & 0xff ) );
  96. }
  97. switch ( pads ) {
  98. case 1:
  99. b10 = ( _getbyte64( s, i ) << 18 ) | ( _getbyte64( s, i + 1 ) << 12 ) | ( _getbyte64( s, i + 2 ) << 6 );
  100. x.push( String.fromCharCode( b10 >> 16, ( b10 >> 8 ) & 0xff ) );
  101. break;
  102. case 2:
  103. b10 = ( _getbyte64( s, i ) << 18) | ( _getbyte64( s, i + 1 ) << 12 );
  104. x.push( String.fromCharCode( b10 >> 16 ) );
  105. break;
  106. }
  107. return x.join( "" );
  108. }
  109. function _getbyte( s, i ) {
  110. var x = s.charCodeAt( i );
  111. if ( x > 255 ) {
  112. throw "INVALID_CHARACTER_ERR: DOM Exception 5";
  113. }
  114. return x;
  115. }
  116. function _encode( s ) {
  117. if ( arguments.length !== 1 ) {
  118. throw "SyntaxError: exactly one argument required";
  119. }
  120. s = String( s );
  121. var i,
  122. b10,
  123. x = [],
  124. imax = s.length - s.length % 3;
  125. if ( s.length === 0 ) {
  126. return s;
  127. }
  128. for ( i = 0; i < imax; i += 3 ) {
  129. b10 = ( _getbyte( s, i ) << 16 ) | ( _getbyte( s, i + 1 ) << 8 ) | _getbyte( s, i + 2 );
  130. x.push( _ALPHA.charAt( b10 >> 18 ) );
  131. x.push( _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) );
  132. x.push( _ALPHA.charAt( ( b10 >> 6 ) & 0x3f ) );
  133. x.push( _ALPHA.charAt( b10 & 0x3f ) );
  134. }
  135. switch ( s.length - imax ) {
  136. case 1:
  137. b10 = _getbyte( s, i ) << 16;
  138. x.push( _ALPHA.charAt( b10 >> 18 ) + _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) + _PADCHAR + _PADCHAR );
  139. break;
  140. case 2:
  141. b10 = ( _getbyte( s, i ) << 16 ) | ( _getbyte( s, i + 1 ) << 8 );
  142. x.push( _ALPHA.charAt( b10 >> 18 ) + _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) + _ALPHA.charAt( ( b10 >> 6 ) & 0x3f ) + _PADCHAR );
  143. break;
  144. }
  145. return x.join( "" );
  146. }
  147. return {
  148. decode: _decode,
  149. encode: _encode,
  150. VERSION: _VERSION
  151. };
  152. }( jQuery ) );