jquery.base64.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. jQuery.base64 = ( function( $ ) {
  2. var _PADCHAR = "=",
  3. _ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
  4. _VERSION = "1.1";//Mr. Ruan fix to 1.1 to support asian char(utf8)
  5. function _getbyte64( s, i ) {
  6. // This is oddly fast, except on Chrome/V8.
  7. // Minimal or no improvement in performance by using a
  8. // object with properties mapping chars to value (eg. 'A': 0)
  9. var idx = _ALPHA.indexOf( s.charAt( i ) );
  10. if ( idx === -1 ) {
  11. throw "Cannot decode base64";
  12. }
  13. return idx;
  14. }
  15. function _decode_chars(y, x){
  16. while(y.length > 0){
  17. var ch = y[0];
  18. if(ch < 0x80) {
  19. y.shift();
  20. x.push(String.fromCharCode(ch));
  21. }else if((ch & 0x80) == 0xc0){
  22. if(y.length < 2) break;
  23. ch = y.shift();
  24. var ch1 = y.shift();
  25. x.push(String.fromCharCode( ((ch & 0x1f) << 6) + (ch1 & 0x3f)));
  26. }else{
  27. if(y.length < 3) break;
  28. ch = y.shift();
  29. var ch1 = y.shift();
  30. var ch2 = y.shift();
  31. x.push(String.fromCharCode(((ch & 0x0f) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f)));
  32. }
  33. }
  34. }
  35. function _decode( s ) {
  36. var pads = 0,
  37. i,
  38. b10,
  39. imax = s.length,
  40. x = [],
  41. y = [];
  42. s = String( s );
  43. if ( imax === 0 ) {
  44. return s;
  45. }
  46. if ( imax % 4 !== 0 ) {
  47. throw "Cannot decode base64";
  48. }
  49. if ( s.charAt( imax - 1 ) === _PADCHAR ) {
  50. pads = 1;
  51. if ( s.charAt( imax - 2 ) === _PADCHAR ) {
  52. pads = 2;
  53. }
  54. // either way, we want to ignore this last block
  55. imax -= 4;
  56. }
  57. for ( i = 0; i < imax; i += 4 ) {
  58. var ch1 = _getbyte64( s, i );
  59. var ch2 = _getbyte64( s, i + 1);
  60. var ch3 = _getbyte64( s, i + 2);
  61. var ch4 = _getbyte64( s, i + 3);
  62. b10 = ( _getbyte64( s, i ) << 18 ) | ( _getbyte64( s, i + 1 ) << 12 ) | ( _getbyte64( s, i + 2 ) << 6 ) | _getbyte64( s, i + 3 );
  63. y.push(b10 >> 16);
  64. y.push((b10 >> 8) & 0xff);
  65. y.push(b10 & 0xff);
  66. _decode_chars(y, x);
  67. }
  68. switch ( pads ) {
  69. case 1:
  70. b10 = ( _getbyte64( s, i ) << 18 ) | ( _getbyte64( s, i + 1 ) << 12 ) | ( _getbyte64( s, i + 2 ) << 6 );
  71. y.push(b10 >> 16);
  72. y.push((b10 >> 8) & 0xff);
  73. break;
  74. case 2:
  75. b10 = ( _getbyte64( s, i ) << 18) | ( _getbyte64( s, i + 1 ) << 12 );
  76. y.push(b10 >> 16);
  77. break;
  78. }
  79. _decode_chars(y, x);
  80. if(y.length > 0) throw "Cannot decode base64";
  81. return x.join( "" );
  82. }
  83. function _get_chars(ch, y){
  84. if(ch < 0x80) y.push(ch);
  85. else if(ch < 0x800){
  86. y.push(0xc0 + ((ch >> 6) & 0x1f));
  87. y.push(0x80 + (ch & 0x3f));
  88. }else{
  89. y.push(0xe0 + ((ch >> 12) & 0xf));
  90. y.push(0x80 + ((ch >> 6) & 0x3f));
  91. y.push(0x80 + (ch & 0x3f));
  92. }
  93. }
  94. function _encode( s ) {
  95. if ( arguments.length !== 1 ) {
  96. throw "SyntaxError: exactly one argument required";
  97. }
  98. s = String( s );
  99. if ( s.length === 0 ) {
  100. return s;
  101. }
  102. //s = _encode_utf8(s);
  103. var i,
  104. b10,
  105. y = [],
  106. x = [],
  107. len = s.length;
  108. i = 0;
  109. while(i < len){
  110. _get_chars(s.charCodeAt(i), y);
  111. while(y.length >= 3){
  112. var ch1 = y.shift();
  113. var ch2 = y.shift();
  114. var ch3 = y.shift();
  115. b10 = ( ch1 << 16 ) | ( ch2 << 8 ) | ch3;
  116. x.push( _ALPHA.charAt( b10 >> 18 ) );
  117. x.push( _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) );
  118. x.push( _ALPHA.charAt( ( b10 >> 6 ) & 0x3f ) );
  119. x.push( _ALPHA.charAt( b10 & 0x3f ) );
  120. }
  121. i++;
  122. }
  123. switch ( y.length ) {
  124. case 1:
  125. var ch = y.shift();
  126. b10 = ch << 16;
  127. x.push( _ALPHA.charAt( b10 >> 18 ) + _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) + _PADCHAR + _PADCHAR );
  128. break;
  129. case 2:
  130. var ch1 = y.shift();
  131. var ch2 = y.shift();
  132. b10 = ( ch1 << 16 ) | ( ch2 << 8 );
  133. x.push( _ALPHA.charAt( b10 >> 18 ) + _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) + _ALPHA.charAt( ( b10 >> 6 ) & 0x3f ) + _PADCHAR );
  134. break;
  135. }
  136. return x.join( "" );
  137. }
  138. return {
  139. decode: _decode,
  140. encode: _encode,
  141. VERSION: _VERSION
  142. };
  143. }( jQuery ) );