[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/phpgwapi/js/jsolait/lib/ -> codecs.js (source)

   1  /*
   2    Copyright (c) 2004 Jan-Klaas Kollhof
   3    
   4    This file is part of the JavaScript o lait library(jsolait).
   5    
   6    jsolait is free software; you can redistribute it and/or modify
   7    it under the terms of the GNU Lesser General Public License as published by
   8    the Free Software Foundation; either version 2.1 of the License, or
   9    (at your option) any later version.
  10   
  11    This software is distributed in the hope that it will be useful,
  12    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14    GNU Lesser General Public License for more details.
  15   
  16    You should have received a copy of the GNU Lesser General Public License
  17    along with this software; if not, write to the Free Software
  18    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19  */
  20  
  21  
  22  /**
  23      Codecs for encoding decoding Strings.
  24      To add new encoders simply create new String.prototype.encode_yourCodec methods.
  25      To add new decoders simply create new String.prototype.decode_yourCodec methods.
  26  */
  27  Module("codecs", "0.1.2", function(mod){
  28      /**
  29          Returns all all available encoders.
  30          @return  An array of encoder names.
  31      */
  32      mod.listEncoders=function(){
  33          var c=[];
  34          for(var attr in String.prototype){
  35              if(attr.slice(0, 7) == "encode_"){
  36                  c.push(attr.slice(7));
  37              }
  38          }
  39          return c;
  40      }
  41      /**
  42          Returns all all available decoders.
  43          @return  An array of decoder names.
  44      */
  45      mod.listDecoders=function(){
  46          var c=[];
  47          for(var attr in String.prototype){
  48              if(attr.slice(0, 7) == "decode_"){
  49                  c.push(attr.slice(7));
  50              }
  51          }
  52          return c;
  53      }
  54      
  55      /**
  56          Decodes an encoded string.
  57          Parameters but the codec parameter are forwardet to the codec.
  58          @param codec  The codec to use.
  59      */
  60      String.prototype.decode = function(codec){
  61          var n ="decode_" + codec;
  62          if(String.prototype[n]){
  63              var args=[];
  64              for(var i=1;i<arguments.length;i++){
  65                  args[i-1] = arguments[i];
  66              }
  67              return String.prototype[n].apply(this, args);
  68          }else{
  69              throw new mod.Exception("Decoder '%s' not found.".format(codec));
  70          }
  71      }
  72      
  73      /**
  74          Encodes a string.
  75          Parameters but the codec parameter are forwardet to the codec.
  76          @param codec  The codec to use.
  77      */
  78      String.prototype.encode = function(codec){
  79          var n ="encode_" + codec;
  80          if(String.prototype[n]){
  81              var args=[];
  82              for(var i=1;i<arguments.length;i++){
  83                  args[i-1] = arguments[i];
  84              }
  85              return String.prototype[n].apply(this, args);
  86          }else{
  87              throw new mod.Exception("Ecnoder '%s' not found.".format(codec));
  88          }
  89      }
  90      
  91      /**
  92          Decodes a Base64 encoded string to a byte string.
  93      */
  94      String.prototype.decode_base64=function(){
  95           if((this.length % 4) == 0){
  96               if(typeof(atob) != "undefined"){//try using mozillas builtin codec
  97                   return atob(this);
  98               }else{
  99                   var nBits;
 100                   //create a result buffer, this is much faster than having strings concatinated.
 101                   var sDecoded = new Array(this.length /4);
 102                   var base64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
 103                   for(var i=0; i < this.length; i+=4){
 104                       nBits = (base64.indexOf(this.charAt(i))   & 0xff) << 18 |
 105                                  (base64.indexOf(this.charAt(i+1)) & 0xff) << 12 |
 106                                  (base64.indexOf(this.charAt(i+2)) & 0xff) <<  6 |
 107                                  base64.indexOf(this.charAt(i+3)) & 0xff;
 108                      sDecoded[i] = String.fromCharCode((nBits & 0xff0000) >> 16, (nBits & 0xff00) >> 8, nBits & 0xff);
 109                  }
 110                  //make sure padding chars are left out.
 111                  sDecoded[sDecoded.length-1] = sDecoded[sDecoded.length-1].substring(0, 3 - ((this.charCodeAt(i - 2) == 61) ? 2 : (this.charCodeAt(i - 1) == 61 ? 1 : 0)));
 112                  return sDecoded.join("");
 113               }
 114           }else{
 115               throw new mod.Exception("String length must be divisible by 4.");
 116           }
 117      }
 118      
 119      /**
 120          Encodes a string using Base64.
 121      */
 122      String.prototype.encode_base64=function(){
 123          if(typeof(btoa) != "undefined"){//try using mozillas builtin codec
 124              return btoa(this);
 125          }else{
 126              var 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',
 127                                  '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',
 128                                  '0','1','2','3','4','5','6','7','8','9','+','/'];
 129              var sbin;
 130              var pad=0;
 131              var s="" + this;
 132              if((s.length % 3) == 1){
 133                  s+=String.fromCharCode(0);
 134                  s+=String.fromCharCode(0);
 135                  pad=2;
 136              }else if((s.length % 3) == 2){
 137                  s+=String.fromCharCode(0);
 138                  pad=1;
 139              }
 140              //create a result buffer, this is much faster than having strings concatinated.
 141              var rslt=new Array(s.length / 3);
 142              var ri=0;
 143              for(var i=0;i<s.length; i+=3){
 144                  sbin=((s.charCodeAt(i) & 0xff) << 16) | ((s.charCodeAt(i+1) & 0xff ) << 8) | (s.charCodeAt(i+2) & 0xff);    
 145                  rslt[ri] = (base64[(sbin >> 18) & 0x3f] + base64[(sbin >> 12) & 0x3f] + base64[(sbin >>6) & 0x3f] + base64[sbin & 0x3f]);
 146                  ri++;
 147              }
 148              if(pad>0){
 149                  rslt[rslt.length-1] = rslt[rslt.length-1].substr(0, 4-pad) +  ((pad==2) ? "==" : (pad==1) ? "=" : "");
 150              }
 151              return rslt.join("");
 152          }
 153      }
 154  
 155      /**
 156          Decodes a URI using decodeURI.
 157      */
 158      String.prototype.decode_uri=function(){
 159          return decodeURI(this);
 160      }
 161      
 162      /**
 163          Encodes a URI using encodeURI.
 164      */
 165      String.prototype.encode_uri=function(){
 166          return encodeURI(this);
 167      }
 168  })


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7