3 Modification information for LGPL compliance
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
7 r56989 - 2010-06-16 13:01:33 -0700 (Wed, 16 Jun 2010) - kjing - defunt "Mango" svn dev branch before github cutover
9 r55980 - 2010-04-19 13:31:28 -0700 (Mon, 19 Apr 2010) - kjing - create Mango (6.1) based on windex
11 r51719 - 2009-10-22 10:18:00 -0700 (Thu, 22 Oct 2009) - mitani - Converted to Build 3 tags and updated the build system
13 r51634 - 2009-10-19 13:32:22 -0700 (Mon, 19 Oct 2009) - mitani - Windex is the branch for Sugar Sales 1.0 development
15 r50375 - 2009-08-24 18:07:43 -0700 (Mon, 24 Aug 2009) - dwong - branch kobe2 from tokyo r50372
17 r42807 - 2008-12-29 11:16:59 -0800 (Mon, 29 Dec 2008) - dwong - Branch from trunk/sugarcrm r42806 to branches/tokyo/sugarcrm
19 r4085 - 2005-04-13 17:30:42 -0700 (Wed, 13 Apr 2005) - robert - adding meeting scheduler and accept/decline
25 Copyright (c) 2004 Jan-Klaas Kollhof
27 This file is part of the JavaScript o lait library(jsolait).
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.
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.
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
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.
50 Module("codecs", "0.1.2", function(mod){
52 Returns all all available encoders.
53 @return An array of encoder names.
55 mod.listEncoders=function(){
57 for(var attr in String.prototype){
58 if(attr.slice(0, 7) == "encode_"){
59 c.push(attr.slice(7));
65 Returns all all available decoders.
66 @return An array of decoder names.
68 mod.listDecoders=function(){
70 for(var attr in String.prototype){
71 if(attr.slice(0, 7) == "decode_"){
72 c.push(attr.slice(7));
79 Decodes an encoded string.
80 Parameters but the codec parameter are forwardet to the codec.
81 @param codec The codec to use.
83 String.prototype.decode = function(codec){
84 var n ="decode_" + codec;
85 if(String.prototype[n]){
87 for(var i=1;i<arguments.length;i++){
88 args[i-1] = arguments[i];
90 return String.prototype[n].apply(this, args);
92 throw new mod.Exception("Decoder '%s' not found.".format(codec));
98 Parameters but the codec parameter are forwardet to the codec.
99 @param codec The codec to use.
101 String.prototype.encode = function(codec){
102 var n ="encode_" + codec;
103 if(String.prototype[n]){
105 for(var i=1;i<arguments.length;i++){
106 args[i-1] = arguments[i];
108 return String.prototype[n].apply(this, args);
110 throw new mod.Exception("Ecnoder '%s' not found.".format(codec));
115 Decodes a Base64 encoded string to a byte string.
117 String.prototype.decode_base64=function(){
118 if((this.length % 4) == 0){
119 if(typeof(atob) != "undefined"){//try using mozillas builtin codec
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);
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("");
138 throw new mod.Exception("String length must be divisible by 4.");
143 Encodes a string using Base64.
145 String.prototype.encode_base64=function(){
146 if(typeof(btoa) != "undefined"){//try using mozillas builtin codec
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','+','/'];
155 if((s.length % 3) == 1){
156 s+=String.fromCharCode(0);
157 s+=String.fromCharCode(0);
159 }else if((s.length % 3) == 2){
160 s+=String.fromCharCode(0);
163 //create a result buffer, this is much faster than having strings concatinated.
164 var rslt=new Array(s.length / 3);
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]);
172 rslt[rslt.length-1] = rslt[rslt.length-1].substr(0, 4-pad) + ((pad==2) ? "==" : (pad==1) ? "=" : "");
174 return rslt.join("");
179 Decodes a URI using decodeURI.
181 String.prototype.decode_uri=function(){
182 return decodeURI(this);
186 Encodes a URI using encodeURI.
188 String.prototype.encode_uri=function(){
189 return encodeURI(this);