PageRenderTime 303ms CodeModel.GetById 142ms app.highlight 13ms RepoModel.GetById 98ms app.codeStats 0ms

/examples/jsonrpc/public/services/phpolait/jsolait/lib/codecs.js

http://pyjamas.googlecode.com/
JavaScript | 103 lines | 103 code | 0 blank | 0 comment | 25 complexity | 14b6a710c54b8369b79f456e4e28d155 MD5 | raw file
  1Module("codecs", "0.1.2", function(mod){
  2mod.listEncoders=function(){
  3var c=[];
  4for(var attr in String.prototype){
  5if(attr.slice(0, 7) == "encode_"){
  6c.push(attr.slice(7));
  7}
  8}
  9return c;
 10}
 11mod.listDecoders=function(){
 12var c=[];
 13for(var attr in String.prototype){
 14if(attr.slice(0, 7) == "decode_"){
 15c.push(attr.slice(7));
 16}
 17}
 18return c;
 19}
 20String.prototype.decode = function(codec){
 21var n ="decode_" + codec;
 22if(String.prototype[n]){
 23var args=[];
 24for(var i=1;i<arguments.length;i++){
 25args[i-1] = arguments[i];
 26}
 27return String.prototype[n].apply(this, args);
 28}else{
 29throw new mod.Exception("Decoder '%s' not found.".format(codec));
 30}
 31}
 32String.prototype.encode = function(codec){
 33var n ="encode_" + codec;
 34if(String.prototype[n]){
 35var args=[];
 36for(var i=1;i<arguments.length;i++){
 37args[i-1] = arguments[i];
 38}
 39return String.prototype[n].apply(this, args);
 40}else{
 41throw new mod.Exception("Ecnoder '%s' not found.".format(codec));
 42}
 43}
 44String.prototype.decode_base64=function(){
 45if((this.length % 4) == 0){
 46if(typeof(atob) != "undefined"){
 47return atob(this);
 48}else{
 49var nBits;
 50var sDecoded = new Array(this.length /4);
 51var base64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
 52for(var i=0; i < this.length; i+=4){
 53nBits = (base64.indexOf(this.charAt(i))   & 0xff) << 18 |
 54(base64.indexOf(this.charAt(i+1)) & 0xff) << 12 |
 55(base64.indexOf(this.charAt(i+2)) & 0xff) <<  6 |
 56base64.indexOf(this.charAt(i+3)) & 0xff;
 57sDecoded[i] = String.fromCharCode((nBits & 0xff0000) >> 16, (nBits & 0xff00) >> 8, nBits & 0xff);
 58}
 59sDecoded[sDecoded.length-1] = sDecoded[sDecoded.length-1].substring(0, 3 - ((this.charCodeAt(i - 2) == 61) ? 2 : (this.charCodeAt(i - 1) == 61 ? 1 : 0)));
 60return sDecoded.join("");
 61}
 62}else{
 63throw new mod.Exception("String length must be divisible by 4.");
 64}
 65}
 66String.prototype.encode_base64=function(){
 67if(typeof(btoa) != "undefined"){
 68return btoa(this);
 69}else{
 70var base64 = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
 71'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
 72'0','1','2','3','4','5','6','7','8','9','+','/'];
 73var sbin;
 74var pad=0;
 75var s="" + this;
 76if((s.length % 3) == 1){
 77s+=String.fromCharCode(0);
 78s+=String.fromCharCode(0);
 79pad=2;
 80}else if((s.length % 3) == 2){
 81s+=String.fromCharCode(0);
 82pad=1;
 83}
 84var rslt=new Array(s.length / 3);
 85var ri=0;
 86for(var i=0;i<s.length; i+=3){
 87sbin=((s.charCodeAt(i) & 0xff) << 16) | ((s.charCodeAt(i+1) & 0xff ) << 8) | (s.charCodeAt(i+2) & 0xff);    
 88rslt[ri] = (base64[(sbin >> 18) & 0x3f] + base64[(sbin >> 12) & 0x3f] + base64[(sbin >>6) & 0x3f] + base64[sbin & 0x3f]);
 89ri++;
 90}
 91if(pad>0){
 92rslt[rslt.length-1] = rslt[rslt.length-1].substr(0, 4-pad) +  ((pad==2) ? "==" : (pad==1) ? "=" : "");
 93}
 94return rslt.join("");
 95}
 96}
 97String.prototype.decode_uri=function(){
 98return decodeURI(this);
 99}
100String.prototype.encode_uri=function(){
101return encodeURI(this);
102}
103})