jquery.base64.js的修正

深圳一朋友说使用jquery.base64.js时发现对于中文直接抛出异常,作者压根不处理汉字的情况,因此

对其进行修正,关键函数为:

function _decode_chars(y, x){
      while(y.length > 0){
        var ch = y[0];
        if(ch < 0x80) {
            y.shift();
            x.push(String.fromCharCode(ch));
        }else if((ch & 0x80) == 0xc0){
            if(y.length < 2) break;
            ch = y.shift();
            var ch1 = y.shift();
            x.push(String.fromCharCode( ((ch & 0x1f) << 6) + (ch1 & 0x3f)));
        }else{
            if(y.length < 3) break;
            ch = y.shift();
            var ch1 = y.shift();
            var ch2 = y.shift();
            x.push(String.fromCharCode(((ch & 0x0f) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f)));
        }    
      }
  }

function _get_chars(ch, y){
    if(ch < 0x80) y.push(ch);
    else if(ch < 0x800){
        y.push(0xc0 + ((ch >> 6) & 0x1f));
        y.push(0x80 + (ch & 0x3f));
    }else{
        y.push(0xe0 + ((ch >> 12) & 0xf));
        y.push(0x80 + ((ch >> 6) & 0x3f));
        y.push(0x80 + (ch & 0x3f));
    }
  }

完整代码

jQuery.base64 = ( function( $ ) {
  
  var _PADCHAR = "=",
    _ALPHA = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
    _VERSION = "1.1";//Mr. Ruan fix to 1.1 to support asian char(utf8)

  function _getbyte64( s, i ) {
    // This is oddly fast, except on Chrome/V8.
    // Minimal or no improvement in performance by using a
    // object with properties mapping chars to value (eg. ‘A‘: 0)

    var idx = _ALPHA.indexOf( s.charAt( i ) );

    if ( idx === -1 ) {
      throw "Cannot decode base64";
    }

    return idx;
  }
  
  function _decode_chars(y, x){
      while(y.length > 0){
        var ch = y[0];
        if(ch < 0x80) {
            y.shift();
            x.push(String.fromCharCode(ch));
        }else if((ch & 0x80) == 0xc0){
            if(y.length < 2) break;
            ch = y.shift();
            var ch1 = y.shift();
            x.push(String.fromCharCode( ((ch & 0x1f) << 6) + (ch1 & 0x3f)));
        }else{
            if(y.length < 3) break;
            ch = y.shift();
            var ch1 = y.shift();
            var ch2 = y.shift();
            x.push(String.fromCharCode(((ch & 0x0f) << 12) + ((ch1 & 0x3f) << 6) + (ch2 & 0x3f)));
        }    
      }
  }
  
  function _decode( s ) {
    var pads = 0,
      i,
      b10,
      imax = s.length,
      x = [],
      y = [];

    s = String( s );
    
    if ( imax === 0 ) {
      return s;
    }

    if ( imax % 4 !== 0 ) {
      throw "Cannot decode base64";
    }

    if ( s.charAt( imax - 1 ) === _PADCHAR ) {
      pads = 1;

      if ( s.charAt( imax - 2 ) === _PADCHAR ) {
        pads = 2;
      }

      // either way, we want to ignore this last block
      imax -= 4;
    }

    for ( i = 0; i < imax; i += 4 ) {
      var ch1 = _getbyte64( s, i );
      var ch2 = _getbyte64( s, i + 1);
      var ch3 = _getbyte64( s, i + 2);
      var ch4 = _getbyte64( s, i + 3);
      
      b10 = ( _getbyte64( s, i ) << 18 ) | ( _getbyte64( s, i + 1 ) << 12 ) | ( _getbyte64( s, i + 2 ) << 6 ) | _getbyte64( s, i + 3 );
      y.push(b10 >> 16);
      y.push((b10 >> 8) & 0xff);
      y.push(b10 & 0xff);
      _decode_chars(y, x);
    }
    switch ( pads ) {
      case 1:
        b10 = ( _getbyte64( s, i ) << 18 ) | ( _getbyte64( s, i + 1 ) << 12 ) | ( _getbyte64( s, i + 2 ) << 6 );
        y.push(b10 >> 16);
        y.push((b10 >> 8) & 0xff);
        break;

      case 2:
        b10 = ( _getbyte64( s, i ) << 18) | ( _getbyte64( s, i + 1 ) << 12 );
        y.push(b10 >> 16);
        break;
    }
    _decode_chars(y, x);
    if(y.length > 0) throw "Cannot decode base64";
    return x.join( "" );
  }
  
  
  function _get_chars(ch, y){
    if(ch < 0x80) y.push(ch);
    else if(ch < 0x800){
        y.push(0xc0 + ((ch >> 6) & 0x1f));
        y.push(0x80 + (ch & 0x3f));
    }else{
        y.push(0xe0 + ((ch >> 12) & 0xf));
        y.push(0x80 + ((ch >> 6) & 0x3f));
        y.push(0x80 + (ch & 0x3f));
    }
  }
  
  
  
  function _encode( s ) {
    if ( arguments.length !== 1 ) {
      throw "SyntaxError: exactly one argument required";
    }

    s = String( s );
    if ( s.length === 0 ) {
      return s;
    }
    
    //s = _encode_utf8(s);
    var i,
      b10,
      y = [],
      x = [],
      len = s.length;
    i = 0;
    while(i < len){
        _get_chars(s.charCodeAt(i), y);
        while(y.length >= 3){
            var ch1 = y.shift();
            var ch2 = y.shift();
            var ch3 = y.shift();
            b10 = ( ch1 << 16 ) | ( ch2 << 8 ) | ch3;
            x.push( _ALPHA.charAt( b10 >> 18 ) );
            x.push( _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) );
            x.push( _ALPHA.charAt( ( b10 >> 6 ) & 0x3f ) );
            x.push( _ALPHA.charAt( b10 & 0x3f ) );
        }
        i++;
    }
     

    switch ( y.length ) {
      case 1:
        var ch = y.shift();
        b10 = ch << 16;
        x.push( _ALPHA.charAt( b10 >> 18 ) + _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) + _PADCHAR + _PADCHAR );
        break;

      case 2:
        var ch1 = y.shift();
        var ch2 = y.shift();
        b10 = ( ch1 << 16 ) | ( ch2 << 8 );
        x.push( _ALPHA.charAt( b10 >> 18 ) + _ALPHA.charAt( ( b10 >> 12 ) & 0x3F ) + _ALPHA.charAt( ( b10 >> 6 ) & 0x3f ) + _PADCHAR );
        break;
    }

    return x.join( "" );
  }

  return {
    decode: _decode,
    encode: _encode,
    VERSION: _VERSION
  };
      
}( jQuery ) );
时间: 2024-08-16 03:14:24

jquery.base64.js的修正的相关文章

通过jquery.cookie.js实现记住用户名、密码登录功能

<!doctype html>   <html xmlns="http://www.w3.org/1999/xhtml">   <head>   <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />   <title>无标题文档</title>   <script src="

js效果笔记:怎样实现图片的懒加载以及jquery.lazyload.js的使用

在项目中有时候会用到图片的延迟加载,那么延迟加载的好处是啥呢? 我觉得主要包括两点吧,第一是在包含很多大图片长页面中延迟加载图片可以加快页面加载速度:第二是帮助降低服务器负担. 下面介绍一下常用的延迟加载插件jquery.lazyload.js以及怎样实现一个延迟加载的插件. 一:jquery.lazyload.js插件 lazyload是jQuery写的延迟加载插件,在浏览器可视区域外的图片不会被载入, 直到用户将页面滚动到它们所在的位置. 这与图片预加载的处理方式正好是相反的. 实现原理 首

jquery.validation.js 表单验证

jquery.validation.js 表单验证 官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 一导入js库 <script src="../js/jquery.js" type="text/javascript"></script> <script src="../js/jq

jQuery验证控件jquery.validate.js使用说明+中文API

官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载自:http://blog.sina.com.cn/s/blog_608475eb0100h3h1.html 一导入js库<script src="../js/jquery.js" type="text/javascript"></script>

jquery.cycle.js简单用法实例

样式: a{text-decoration: none;} *{margin:0; padding:0;} /*容器设置*/ .player { width:216px; height:248px; background:url(http://i2.itc.cn/20120117/2cc0_da8f6c82_c8da_693d_7714_9533a013006a_3.jpg) no-repeat; background-color:#ede1d1; position:relative; padd

jQuery验证控件jquery.validate.js使用说明

官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation jQuery plugin: Validation 使用说明 转载自:http://blog.sina.com.cn/s/blog_608475eb0100h3h1.html 转载自:http://blog.csdn.net/windxxf/article/details/7520340,中文API请参考此处内容 一导入js库<script src="../

jQuery验证空间jquery.validate.js使用说明+中文API

--------转载自http://www.cnblogs.com/hejunrex/archive/2011/11/17/2252193.html jQuery plugin: Validation 使用说明 官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 转载自:http://blog.sina.com.cn/s/blog_608475eb0100h3h1.html 一.导入js库 <script src=

jquery.validation.js 使用

引用文件: "~/assets/global/plugins/jquery-validation/js/jquery.validate.min.js", "~/assets/global/plugins/jquery-validation/js/jquery.metadata.js", "~/assets/global/plugins/jquery-validation/js/localization/messages_cn.js", jquer

jQuery表单验证插件——jquery.validate.js

官网地址:http://bassistance.de/jquery-plugins/jquery-plugin-validation 一.导入js库 1 <script src="../js/jquery.js" type="text/javascript"></script> 2 <script src="../js/jquery.validate.js" type="text/javascript&qu