]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - jssource/src_files/include/jsolait/lib/codecs.js
Release 6.2.1
[Github/sugarcrm.git] / jssource / src_files / include / jsolait / lib / codecs.js
1 /*
2
3 Modification information for LGPL compliance
4
5 r56990 - 2010-06-16 13:05:36 -0700 (Wed, 16 Jun 2010) - kjing - snapshot "Mango" svn branch to a new one for GitHub sync
6
7 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
8
9 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
10
11 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3  tags and updated the build system 
12
13 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
14
15 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
16
17 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
18
19 r4085 - 2005-04-13 17:30:42 -0700 (Wed, 13 Apr 2005) - robert - adding meeting scheduler and accept/decline
20
21
22 */
23
24 /*
25   Copyright (c) 2004 Jan-Klaas Kollhof
26   
27   This file is part of the JavaScript o lait library(jsolait).
28   
29   jsolait is free software; you can redistribute it and/or modify
30   it under the terms of the GNU Lesser General Public License as published by
31   the Free Software Foundation; either version 2.1 of the License, or
32   (at your option) any later version.
33  
34   This software is distributed in the hope that it will be useful,
35   but WITHOUT ANY WARRANTY; without even the implied warranty of
36   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
37   GNU Lesser General Public License for more details.
38  
39   You should have received a copy of the GNU Lesser General Public License
40   along with this software; if not, write to the Free Software
41   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
42 */
43
44
45 /**
46     Codecs for encoding decoding Strings.
47     To add new encoders simply create new String.prototype.encode_yourCodec methods.
48     To add new decoders simply create new String.prototype.decode_yourCodec methods.
49 */
50 Module("codecs", "0.1.2", function(mod){
51     /**
52         Returns all all available encoders.
53         @return  An array of encoder names.
54     */
55     mod.listEncoders=function(){
56         var c=[];
57         for(var attr in String.prototype){
58             if(attr.slice(0, 7) == "encode_"){
59                 c.push(attr.slice(7));
60             }
61         }
62         return c;
63     }
64     /**
65         Returns all all available decoders.
66         @return  An array of decoder names.
67     */
68     mod.listDecoders=function(){
69         var c=[];
70         for(var attr in String.prototype){
71             if(attr.slice(0, 7) == "decode_"){
72                 c.push(attr.slice(7));
73             }
74         }
75         return c;
76     }
77     
78     /**
79         Decodes an encoded string.
80         Parameters but the codec parameter are forwardet to the codec.
81         @param codec  The codec to use.
82     */
83     String.prototype.decode = function(codec){
84         var n ="decode_" + codec;
85         if(String.prototype[n]){
86             var args=[];
87             for(var i=1;i<arguments.length;i++){
88                 args[i-1] = arguments[i];
89             }
90             return String.prototype[n].apply(this, args);
91         }else{
92             throw new mod.Exception("Decoder '%s' not found.".format(codec));
93         }
94     }
95     
96     /**
97         Encodes a string.
98         Parameters but the codec parameter are forwardet to the codec.
99         @param codec  The codec to use.
100     */
101     String.prototype.encode = function(codec){
102         var n ="encode_" + codec;
103         if(String.prototype[n]){
104             var args=[];
105             for(var i=1;i<arguments.length;i++){
106                 args[i-1] = arguments[i];
107             }
108             return String.prototype[n].apply(this, args);
109         }else{
110             throw new mod.Exception("Ecnoder '%s' not found.".format(codec));
111         }
112     }
113     
114     /**
115         Decodes a Base64 encoded string to a byte string.
116     */
117     String.prototype.decode_base64=function(){
118          if((this.length % 4) == 0){
119              if(typeof(atob) != "undefined"){//try using mozillas builtin codec
120                  return atob(this);
121              }else{
122                  var nBits;
123                  //create a result buffer, this is much faster than having strings concatinated.
124                  var sDecoded = new Array(this.length /4);
125                  var base64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
126                  for(var i=0; i < this.length; i+=4){
127                      nBits = (base64.indexOf(this.charAt(i))   & 0xff) << 18 |
128                                 (base64.indexOf(this.charAt(i+1)) & 0xff) << 12 |
129                                 (base64.indexOf(this.charAt(i+2)) & 0xff) <<  6 |
130                                 base64.indexOf(this.charAt(i+3)) & 0xff;
131                     sDecoded[i] = String.fromCharCode((nBits & 0xff0000) >> 16, (nBits & 0xff00) >> 8, nBits & 0xff);
132                 }
133                 //make sure padding chars are left out.
134                 sDecoded[sDecoded.length-1] = sDecoded[sDecoded.length-1].substring(0, 3 - ((this.charCodeAt(i - 2) == 61) ? 2 : (this.charCodeAt(i - 1) == 61 ? 1 : 0)));
135                 return sDecoded.join("");
136              }
137          }else{
138              throw new mod.Exception("String length must be divisible by 4.");
139          }
140     }
141     
142     /**
143         Encodes a string using Base64.
144     */
145     String.prototype.encode_base64=function(){
146         if(typeof(btoa) != "undefined"){//try using mozillas builtin codec
147             return btoa(this);
148         }else{
149             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',
150                                 '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',
151                                 '0','1','2','3','4','5','6','7','8','9','+','/'];
152             var sbin;
153             var pad=0;
154             var s="" + this;
155             if((s.length % 3) == 1){
156                 s+=String.fromCharCode(0);
157                 s+=String.fromCharCode(0);
158                 pad=2;
159             }else if((s.length % 3) == 2){
160                 s+=String.fromCharCode(0);
161                 pad=1;
162             }
163             //create a result buffer, this is much faster than having strings concatinated.
164             var rslt=new Array(s.length / 3);
165             var ri=0;
166             for(var i=0;i<s.length; i+=3){
167                 sbin=((s.charCodeAt(i) & 0xff) << 16) | ((s.charCodeAt(i+1) & 0xff ) << 8) | (s.charCodeAt(i+2) & 0xff);    
168                 rslt[ri] = (base64[(sbin >> 18) & 0x3f] + base64[(sbin >> 12) & 0x3f] + base64[(sbin >>6) & 0x3f] + base64[sbin & 0x3f]);
169                 ri++;
170             }
171             if(pad>0){
172                 rslt[rslt.length-1] = rslt[rslt.length-1].substr(0, 4-pad) +  ((pad==2) ? "==" : (pad==1) ? "=" : "");
173             }
174             return rslt.join("");
175         }
176     }
177
178     /**
179         Decodes a URI using decodeURI.
180     */
181     String.prototype.decode_uri=function(){
182         return decodeURI(this);
183     }
184     
185     /**
186         Encodes a URI using encodeURI.
187     */
188     String.prototype.encode_uri=function(){
189         return encodeURI(this);
190     }
191 })