Changeset 68
- Timestamp:
- 07/20/06 13:09:10 (2 years ago)
- Files:
-
- branches/experimental/jsolait/jsolait.js (modified) (9 diffs)
- branches/experimental/jsolait/jsolait.wsf (modified) (6 diffs)
- branches/experimental/jsolait/lib/codecs.js (modified) (2 diffs)
- branches/experimental/jsolait/lib/crypto.js (modified) (2 diffs)
- branches/experimental/jsolait/lib/dom.js (modified) (2 diffs)
- branches/experimental/jsolait/lib/forms.js (modified) (2 diffs)
- branches/experimental/jsolait/lib/itertools.js (moved) (moved from branches/experimental/jsolait/lib/iter.js) (1 diff)
- branches/experimental/jsolait/lib/jsonrpc.js (modified) (1 diff)
- branches/experimental/jsolait/lib/lang.js (modified) (1 diff)
- branches/experimental/jsolait/lib/net/sockets.js (modified) (1 diff)
- branches/experimental/jsolait/lib/operators.js (modified) (2 diffs)
- branches/experimental/jsolait/lib/sets.js (modified) (2 diffs)
- branches/experimental/jsolait/lib/strings.js (modified) (2 diffs)
- branches/experimental/jsolait/lib/testing.js (modified) (2 diffs)
- branches/experimental/jsolait/lib/urllib.js (modified) (2 diffs)
- branches/experimental/jsolait/lib/xml.js (modified) (2 diffs)
- branches/experimental/jsolait/lib/xmlrpc.js (modified) (2 diffs)
- branches/experimental/test/test.js (modified) (1 diff)
- branches/experimental/test/test_codecs.js (modified) (1 diff)
- branches/experimental/test/test_core.js (modified) (1 diff)
- branches/experimental/test/test_crypto.js (modified) (1 diff)
- branches/experimental/test/test_iter.js (modified) (1 diff)
- branches/experimental/test/test_jsonrpc.js (modified) (1 diff)
- branches/experimental/test/test_sets.js (modified) (1 diff)
- branches/experimental/test/test_strings.js (modified) (1 diff)
- branches/experimental/test/test_testing.js (modified) (1 diff)
- branches/experimental/test/test_urllib.js (modified) (1 diff)
- branches/experimental/test/test_xmlrpc.js (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
branches/experimental/jsolait/jsolait.js
r67 r68 1 /* 2 Copyright (c) 2003-2006 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 The main jsolait script. 23 It provides the core functionalities for creating classes, modules and for importing modules. 24 25 @author Jan-Klaas Kollhof 26 @version 3.0 27 @lastchangedby $LastChangedBy$ 28 @lastchangeddate $Date$ 29 @revision $Revision$ 30 **/ 31 1 32 jsolait=(function(){ 2 33 jsolait = {}; … … 69 100 __str__ : function(){ 70 101 return "[class %s]".format(this.__name__); 102 }, 103 __create__:function(initArgs){ 104 var obj = new this(Class); 105 if(obj.__init__){ 106 obj.__init__.apply(obj, initArgs); 107 } 108 return obj; 71 109 } 72 110 }; … … 238 276 @return The error trace. 239 277 **/ 240 publ. __str__=function(indent){278 publ.toTraceString=function(indent){ 241 279 indent = indent==null ? 0 : indent; 242 280 … … 268 306 publ.__source__; 269 307 publ.__sourceURI__; 270 publ.Exception;271 308 272 309 publ.__init__=function(name, source, sourceURI){ … … 275 312 this.__source__ = source; 276 313 this.__sourceURI__ = sourceURI; 277 this.Exception = Class(jsolait.Exception, new Function());278 this.Exception.prototype.module = this;279 314 }; 280 315 … … 469 504 /** 470 505 Initializes a new CreateModuleFailed Exception. 471 @param module The module.506 @param module The module. 472 507 @param trace The error cousing this Exception. 473 508 **/ 474 509 publ.__init__=function(module, trace){ 475 supr.__init__.call(this, "Failed to create the module for%s".format(module), trace);510 supr.__init__.call(this, "Failed to create module %s".format(module), trace); 476 511 this.failedModule = module; 477 512 }; … … 484 519 485 520 var privateScope={imprt: function(imp){ 486 var s = arguments.callee.scope;487 jsolait.__imprt__(imp, s);488 }};521 var s = arguments.callee.scope; 522 jsolait.__imprt__(imp, s); 523 }}; 489 524 privateScope.imprt.scope=privateScope; 525 privateScope.Exception = Class(jsolait.Exception, new Function()); 526 privateScope.Exception.prototype.module = newMod; 490 527 491 528 try{//to run the module source … … 498 535 applyNames(privateScope); 499 536 537 jsolait.modules[name] = newMod; 500 538 return newMod; 501 539 }; … … 967 1005 return jsolait; 968 1006 }()); 969 970 971 var print = function(){972 var a=[]973 for(var i=0;i<arguments.length;i++){974 a.push(arguments[i]);975 }976 WScript.echo(a.join(" "));977 }978 979 980 981 f=function(){982 try{983 imprt("test")984 print(test.__sourceURI__, test.__source__)985 test.x();986 }catch(e){987 print(e)988 }989 990 };991 992 var fs= new ActiveXObject("Scripting.FileSystemObject");993 994 jsolait.baseURI = 'file://' + fs.getParentFolderName(WScript.scriptFullName);995 jsolait.moduleSearchURIs=[jsolait.baseURI];996 var s = String(f).replace("function(){", "").slice(0,-1)997 jsolait.createModuleFromSource('',s)998 branches/experimental/jsolait/jsolait.wsf
r65 r68 5 5 JavaScript O Lait for WSH. 6 6 7 Copyright (c) 2005 Jan-Klaas Kollhof7 Copyright (c) 2005-2006 Jan-Klaas Kollhof 8 8 9 9 This file is part of jsolait … … 25 25 26 26 <runtime> 27 <named name="compile" helpstring="Specifies to run a the lang.parser on it." many="false" required="0" />28 27 <unnamed name="filename" helpstring="The scriptfiles to run." many="false" required="1" /> 29 28 <named name="script-args" helpstring="Specifies arguments to pass to the module/script being run. This may be text or a JSON encoded object" many="false" required="0" /> … … 34 33 <script language="JavaScript"> <![CDATA[ 35 34 36 Module("jsolaitws", "0.0.1", function(mod){ 37 35 jsolait.Module("jsolaitws", function(publ,priv,__builtin__){with(publ){with(priv){with(__builtin__){ 36 __version__ = "$Revision$" 37 __sourceURI__ = 'file://' + WScript.scriptFullName; 38 jsolait.__sourceURI__ = 'file://' + WScript.scriptFullName.slice(0,-3) + "js"; 39 38 40 var fs= new ActiveXObject("Scripting.FileSystemObject"); 41 ///The location where jsolait is installed. 42 jsolait.baseURI = 'file://' + fs.getParentFolderName(WScript.scriptFullName); 43 jsolait.moduleSearchURIs = ["%(baseURI)s/lib"]; 44 39 45 var wshShell= new ActiveXObject("WScript.Shell"); 40 46 var ForReading = 1, ForWriting = 2; 41 47 42 print = function(m){48 var print = __builtin__.print = function(m){ 43 49 var s=[]; 44 50 for(var i=0;i<arguments.length;i++){ … … 49 55 } 50 56 51 pprint=function(m, indent){57 var pprint= __builtin__.pprint=function(m, indent){ 52 58 var m = m.split("\n"); 53 59 … … 74 80 75 81 Error.prototype.toString=function(){ 76 return this.name +": " + this.message; 77 } 78 79 LogError=1; 80 LogWarn=2; 81 LogInfo=4; 82 Error.prototype.toTraceString=function(){ 83 return this.message 82 return this.name +": " + this.message; 84 83 } 85 84 86 log=function(msg, level){ 87 level = level==null?LogInfo:level; 88 if(typeof msg !="string"){ 89 level = LogError; 90 91 msg = msg.toTraceString(); 92 } 85 publ.run=function(){ 93 86 94 if(level & log.level){95 print(msg);96 }97 }98 log.level = LogInfo | LogWarn | LogError;99 100 mod.run=function(){101 mod.__sourceURI__ = 'file://' + WScript.scriptFullName;102 jsolait.__sourceURI__ = 'file://' + WScript.scriptFullName.slice(0,-3) + "js";103 104 ///The location where jsolait is installed.105 jsolait.baseURI = 'file://' + fs.getParentFolderName(WScript.scriptFullName);106 107 87 if (WScript.arguments.unnamed.length==0){ 108 88 WScript.Arguments.ShowUsage(); … … 126 106 } 127 107 128 129 if(WScript.arguments.named.exists("compile")){ 130 var lang = imprt('lang'); 131 var s = jsolait.loadURI(fileName); 132 var p = new lang.Parser(s); 108 if(fileName.toLowerCase() != jsolait.__sourceURI__.toLowerCase() && fileName.toLowerCase() != __sourceURI__.toLowerCase()){ 109 var src = jsolait.loadURI(fileName); 110 var modl = jsolait.createModuleFromSource("__main__", src, fileName); 111 112 if(typeof modl.__main__=='function'){ 113 //todo find arguments 114 if(WScript.arguments.named.exists("script-args")){ 115 var args = WScript.arguments.named.Item("script-args"); 116 switch(args.slice(0,1)){ 117 case "{": case "'": case "[": case '"': 118 break; 119 default: 120 args = repr(args); 121 } 122 f=new Function( "return " + args); 123 args = [f()]; 124 }else{ 125 var args =[]; 126 } 133 127 134 try{ 135 p.parseStatements(p.next()); 136 }catch(e){ 137 var l=p.getPosition(); 138 throw fileName + '(' + (l[0] ) + ',' +l[1] + ') ' + e + ' near:\n' + p._working.slice(0,200); 139 } 140 141 }else{ 142 try{//load the script if it is not the main jsolait or jsolaitws module that has already been loaded. 143 if(fileName.toLowerCase() != jsolait.__sourceURI__.toLowerCase() && 144 fileName.toLowerCase() != mod.__sourceURI__.toLowerCase()){ 145 var src = jsolait.loadURI(fileName); 146 src = 'Module.currentURI="%s";\n%s\nModule.currentURI=null;\n'.format(src.__sourceURI__.replace(/\\/g, '\\\\'), src); 147 var f=new Function("",src); //todo should it use globalEval ? 148 f(); 149 } 150 }catch(e){ 151 log(e); 152 return; 153 } 154 155 //if the loaded file contained a module then run it's __main__ method 156 for(var mn in jsolait.modules){ 157 var modl =jsolait.modules[mn]; 158 if(modl.__sourceURI__.toLowerCase() == fileName.toLowerCase()){ 159 if(modl.__main__){ 160 //todo find arguments 161 if(WScript.arguments.named.exists("script-args")){ 162 var args = WScript.arguments.named.Item("script-args"); 163 switch(args.slice(0,1)){ 164 case "{": case "'": case "[": case '"': 165 break; 166 default: 167 args = repr(args); 168 } 169 f=new Function( "return " + args); 170 args = [f()]; 171 }else{ 172 var args =[]; 173 } 174 175 try{ 176 modl.__main__.apply(modl,args); 177 }catch(e){ 178 throw new mod.Exception("runing %s __main__() failed\n".format(modl),e) 179 } 180 } 181 return; 128 try{ 129 modl.__main__.apply(modl,args); 130 }catch(e){ 131 throw new Exception("runing %s __main__() failed\n".format(modl),e) 182 132 } 183 133 } 184 134 } 185 } 186 } );135 }; 136 }}}}); 187 137 188 138 try{ 189 imprt("jsolaitws").run();139 jsolait.loadModule("jsolaitws").run(); 190 140 }catch(e){ 191 log(e,LogError);141 WScript.echo(""+e); 192 142 } 193 143 branches/experimental/jsolait/lib/codecs.js
r44 r68 1 1 /* 2 Copyright (c) 2004-200 5Jan-Klaas Kollhof2 Copyright (c) 2004-2006 Jan-Klaas Kollhof 3 3 4 4 This file is part of the JavaScript o lait library(jsolait). … … 28 28 @lastchangeddate $Date$ 29 29 */ 30 Module("codecs", "$Revision$", function(mod){ 31 32 /** 33 Returns all all available encoders. 34 @return An array of encoder names. 35 **/ 36 mod.listEncoders=function(){ 37 var c=[]; 38 for(var attr in String.prototype){ 39 if(attr.slice(0, 7) == "encode_"){ 40 c.push(attr.slice(7)); 30 __version__ = "$Revision$"; 31 32 /** 33 Returns all all available encoders. 34 @return An array of encoder names. 35 **/ 36 publ.listEncoders=function(){ 37 var c=[]; 38 for(var attr in String.prototype){ 39 if(attr.slice(0, 7) == "encode_"){ 40 c.push(attr.slice(7)); 41 } 42 } 43 return c; 44 }; 45 /** 46 Returns all all available decoders. 47 @return An array of decoder names. 48 **/ 49 publ.listDecoders=function(){ 50 var c=[]; 51 for(var attr in String.prototype){ 52 if(attr.slice(0, 7) == "decode_"){ 53 c.push(attr.slice(7)); 54 } 55 } 56 return c; 57 }; 58 59 /** 60 Decodes an encoded string. 61 Parameters but the codec parameter are forwardet to the codec. 62 @param codec The codec to use. 63 **/ 64 String.prototype.decode = function(codec){ 65 var n ="decode_" + codec; 66 if(String.prototype[n]){ 67 var args=[]; 68 for(var i=1;i<arguments.length;i++){ 69 args[i-1] = arguments[i]; 70 } 71 return String.prototype[n].apply(this, args); 72 }else{ 73 throw new Exception("Decoder '%s' not found.".format(codec)); 74 } 75 }; 76 77 /** 78 Encodes a string. 79 Parameters but the codec parameter are forwardet to the codec. 80 @param codec The codec to use. 81 **/ 82 String.prototype.encode = function(codec){ 83 var n ="encode_" + codec; 84 if(String.prototype[n]){ 85 var args=[]; 86 for(var i=1;i<arguments.length;i++){ 87 args[i-1] = arguments[i]; 88 } 89 return String.prototype[n].apply(this, args); 90 }else{ 91 throw new Exception("Ecnoder '%s' not found.".format(codec)); 92 } 93 }; 94 95 /** 96 Decodes a Base64 encoded string to a byte string. 97 **/ 98 String.prototype.decode_base64=function(){ 99 if((this.length % 4) == 0){ 100 if(typeof(atob) != "undefined"){//try using mozillas builtin codec 101 return atob(this); 102 }else{ 103 var nBits; 104 //create a result buffer, this is much faster than having strings concatinated. 105 var sDecoded = new Array(this.length / 4); 106 var base64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; 107 for(var i=0; i < this.length; i+=4){ 108 nBits = (base64.indexOf(this.charAt(i)) & 0xff) << 18 | 109 (base64.indexOf(this.charAt(i+1)) & 0xff) << 12 | 110 (base64.indexOf(this.charAt(i+2)) & 0xff) << 6 | 111 base64.indexOf(this.charAt(i+3)) & 0xff; 112 sDecoded[i] = String.fromCharCode((nBits & 0xff0000) >> 16, (nBits & 0xff00) >> 8, nBits & 0xff); 41 113 } 42 } 43 return c; 44 }; 45 /** 46 Returns all all available decoders. 47 @return An array of decoder names. 48 **/ 49 mod.listDecoders=function(){ 50 var c=[]; 51 for(var attr in String.prototype){ 52 if(attr.slice(0, 7) == "decode_"){ 53 c.push(attr.slice(7)); 54 } 55 } 56 return c; 57 }; 58 59 /** 60 Decodes an encoded string. 61 Parameters but the codec parameter are forwardet to the codec. 62 @param codec The codec to use. 63 **/ 64 String.prototype.decode = function(codec){ 65 var n ="decode_" + codec; 66 if(String.prototype[n]){ 67 var args=[]; 68 for(var i=1;i<arguments.length;i++){ 69 args[i-1] = arguments[i]; 70 } 71 return String.prototype[n].apply(this, args); 114 //make sure padding chars are left out. 115 sDecoded[sDecoded.length-1] = sDecoded[sDecoded.length-1].substring(0, 3 - ((this.charCodeAt(i - 2) == 61) ? 2 : (this.charCodeAt(i - 1) == 61 ? 1 : 0))); 116 return sDecoded.join(""); 117 } 118 }else{ 119 throw new Exception("String length must be divisible by 4."); 120 } 121 }; 122 123 /** 124 Encodes a string using Base64. 125 **/ 126 String.prototype.encode_base64=function(){ 127 if(typeof(btoa) != "undefined"){//try using mozillas builtin codec 128 return btoa(this); 129 }else{ 130 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', 131 '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', 132 '0','1','2','3','4','5','6','7','8','9','+','/']; 133 var sbin; 134 var pad=0; 135 var s="" + this; 136 if((s.length % 3) == 1){ 137 s+=String.fromCharCode(0); 138 s+=String.fromCharCode(0); 139 pad=2; 140 }else if((s.length % 3) == 2){ 141 s+=String.fromCharCode(0); 142 pad=1; 143 } 144 //create a result buffer, this is much faster than having strings concatinated. 145 var rslt=new Array(s.length / 3); 146 var ri=0; 147 for(var i=0;i<s.length; i+=3){ 148 sbin=((s.charCodeAt(i) & 0xff) << 16) | ((s.charCodeAt(i+1) & 0xff ) << 8) | (s.charCodeAt(i+2) & 0xff); 149 rslt[ri] = (base64[(sbin >> 18) & 0x3f] + base64[(sbin >> 12) & 0x3f] + base64[(sbin >>6) & 0x3f] + base64[sbin & 0x3f]); 150 ri++; 151 } 152 if(pad>0){ 153 rslt[rslt.length-1] = rslt[rslt.length-1].substr(0, 4 - pad) + ((pad==2) ? "==" : (pad==1) ? "=" : ""); 154 } 155 return rslt.join(""); 156 } 157 }; 158 159 /** 160 Decodes a URI using decodeURIComponent. 161 **/ 162 String.prototype.decode_uri=function(){ 163 return decodeURIComponent(this); 164 }; 165 166 /** 167 Encodes a URI using encodeURIComponent. 168 **/ 169 String.prototype.encode_uri=function(){ 170 return encodeURIComponent(this); 171 }; 172 173 174 String.prototype.encode_lzw=function(){ 175 var dict = {}; 176 var data = (this + "").split(""); 177 var out=[]; 178 var currChar; 179 var phrase = data[0]; 180 var code = 256; 181 182 for(var i=1;i<data.length;i++){ 183 currChar = data[i]; 184 if(dict[phrase + currChar] != null){ 185 phrase += currChar; 72 186 }else{ 73 throw new mod.Exception("Decoder '%s' not found.".format(codec)); 74 } 75 }; 76 77 /** 78 Encodes a string. 79 Parameters but the codec parameter are forwardet to the codec. 80 @param codec The codec to use. 81 **/ 82 String.prototype.encode = function(codec){ 83 var n ="encode_" + codec; 84 if(String.prototype[n]){ 85 var args=[]; 86 for(var i=1;i<arguments.length;i++){ 87 args[i-1] = arguments[i]; 88 } 89 return String.prototype[n].apply(this, args); 187 out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0)); 188 dict[phrase + currChar] = code; 189 code++; 190 phrase=currChar; 191 } 192 } 193 out.push( phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0)); 194 for(var i=0;i<out.length;i++){ 195 196 out[i] = String.fromCharCode(out[i]); 197 } 198 return out.join(""); 199 }; 200 201 String.prototype.decode_lzw=function(){ 202 var dict={}; 203 var data = (this + "").split(""); 204 var currChar = data[0]; 205 var oldPhrase = currChar; 206 var out = [currChar]; 207 var code = 256; 208 var phrase; 209 for(var i=1;i<data.length;i++){ 210 var currCode = data[i].charCodeAt(0); 211 if(currCode < 256){ 212 phrase = data[i]; 90 213 }else{ 91 throw new mod.Exception("Ecnoder '%s' not found.".format(codec)); 92 } 93 }; 94 95 /** 96 Decodes a Base64 encoded string to a byte string. 97 **/ 98 String.prototype.decode_base64=function(){ 99 if((this.length % 4) == 0){ 100 if(typeof(atob) != "undefined"){//try using mozillas builtin codec 101 return atob(this); 102 }else{ 103 var nBits; 104 //create a result buffer, this is much faster than having strings concatinated. 105 var sDecoded = new Array(this.length / 4); 106 var base64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='; 107 for(var i=0; i < this.length; i+=4){ 108 nBits = (base64.indexOf(this.charAt(i)) & 0xff) << 18 | 109 (base64.indexOf(this.charAt(i+1)) & 0xff) << 12 | 110 (base64.indexOf(this.charAt(i+2)) & 0xff) << 6 | 111 base64.indexOf(this.charAt(i+3)) & 0xff; 112 sDecoded[i] = String.fromCharCode((nBits & 0xff0000) >> 16, (nBits & 0xff00) >> 8, nBits & 0xff); 113 } 114 //make sure padding chars are left out. 115 sDecoded[sDecoded.length-1] = sDecoded[sDecoded.length-1].substring(0, 3 - ((this.charCodeAt(i - 2) == 61) ? 2 : (this.charCodeAt(i - 1) == 61 ? 1 : 0))); 116 return sDecoded.join(""); 117 } 118 }else{ 119 throw new mod.Exception("String length must be divisible by 4."); 120 } 121 }; 122 123 /** 124 Encodes a string using Base64. 125 **/ 126 String.prototype.encode_base64=function(){ 127 if(typeof(btoa) != "undefined"){//try using mozillas builtin codec 128 return btoa(this); 129 }else{ 130 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', 131 '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', 132 '0','1','2','3','4','5','6','7','8','9','+','/']; 133 var sbin; 134 var pad=0; 135 var s="" + this; 136 if((s.length % 3) == 1){ 137 s+=String.fromCharCode(0); 138 s+=String.fromCharCode(0); 139 pad=2; 140 }else if((s.length % 3) == 2){ 141 s+=String.fromCharCode(0); 142 pad=1; 143 } 144 //create a result buffer, this is much faster than having strings concatinated. 145 var rslt=new Array(s.length / 3); 146 var ri=0; 147 for(var i=0;i<s.length; i+=3){ 148 sbin=((s.charCodeAt(i) & 0xff) << 16) | ((s.charCodeAt(i+1) & 0xff ) << 8) | (s.charCodeAt(i+2) & 0xff); 149 rslt[ri] = (base64[(sbin >> 18) & 0x3f] + base64[(sbin >> 12) & 0x3f] + base64[(sbin >>6) & 0x3f] + base64[sbin & 0x3f]); 150 ri++; 151 } 152 if(pad>0){ 153 rslt[rslt.length-1] = rslt[rslt.length-1].substr(0, 4 - pad) + ((pad==2) ? "==" : (pad==1) ? "=" : ""); 154 } 155 return rslt.join(""); 156 } 157 }; 158 159 /** 160 Decodes a URI using decodeURIComponent. 161 **/ 162 String.prototype.decode_uri=function(){ 163 return decodeURIComponent(this); 164 }; 165 166 /** 167 Encodes a URI using encodeURIComponent. 168 **/ 169 String.prototype.encode_uri=function(){ 170 return encodeURIComponent(this); 171 }; 172 173 174 String.prototype.encode_lzw=function(){ 175 var dict = {}; 176 var data = (this + "").split(""); 177 var out=[]; 178 var currChar; 179 var phrase = data[0]; 180 var code = 256; 181 182 for(var i=1;i<data.length;i++){ 183 currChar = data[i]; 184 if(dict[phrase + currChar] != null){ 185 phrase += currChar; 186 }else{ 187 out.push(phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0)); 188 dict[phrase + currChar] = code; 189 code++; 190 phrase=currChar; 191 } 192 } 193 out.push( phrase.length > 1 ? dict[phrase] : phrase.charCodeAt(0)); 194 for(var i=0;i<out.length;i++){ 195 196 out[i] = String.fromCharCode(out[i]); 197 } 198 return out.join(""); 199 }; 200 201 String.prototype.decode_lzw=function(){ 202 var dict={}; 203 var data = (this + "").split(""); 204 var currChar = data[0]; 205 var oldPhrase = currChar; 206 var out = [currChar]; 207 var code = 256; 208 var phrase; 209 for(var i=1;i<data.length;i++){ 210 var currCode = data[i].charCodeAt(0); 211 if(currCode < 256){ 212 phrase = data[i]; 213 }else{ 214 phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar); 215 } 216 out.push(phrase); 217 currChar = phrase.charAt(0); 218 dict[code] = oldPhrase + currChar; 219 code ++; 220 oldPhrase = phrase; 221 } 222 return out.join(""); 223 }; 224 225 }); 214 phrase = dict[currCode] ? dict[currCode] : (oldPhrase + currChar); 215 } 216 out.push(phrase); 217 currChar = phrase.charAt(0); 218 dict[code] = oldPhrase + currChar; 219 code ++; 220 oldPhrase = phrase; 221 } 222 return out.join(""); 223 }; 224 branches/experimental/jsolait/lib/crypto.js
r5 r68 1 1 /* 2 Copyright (c) 2003 Jan-Klaas Kollhof2 Copyright (c) 2003-2006 Jan-Klaas Kollhof 3 3 4 4 This file is part of the JavaScript o lait library(jsolait). … … 27 27 @lastchangeddate $Date$ 28 28 */ 29 Module("crypto", "$Revision$", function(mod){ 30 /** 31 Returns all all available encrypters. 32 @return An array of encrypters names.33 **/34 mod.listEncrypters=function(){ 35 var c=[]; 36 for(var attr in String.prototype){37 if(attr.slice(0, 8) == "encrypt_"){38 c.push(attr.slice(8));39 }29 __version__ = "$Revision$"; 30 31 /** 32 Returns all all available encrypters. 33 @return An array of encrypters names. 34 **/ 35 publ.listEncrypters=function(){ 36 var c=[]; 37 for(var attr in String.prototype){ 38 if(attr.slice(0, 8) == "encrypt_"){ 39 c.push(attr.slice(8)); 40 40 } 41 return c;42 };43 /** 44 Returns all all available decrypters. 45 @return An array of decrypters names.46 **/47 mod.listDecrypters=function(){ 48 var c=[]; 49 for(var attr in String.prototype){50 if(attr.slice(0, 8) == "decrypt_"){51 c.push(attr.slice(8));52 }41 } 42 return c; 43 }; 44 /** 45 Returns all all available decrypters. 46 @return An array of decrypters names. 47 **/ 48 publ.listDecrypters=function(){ 49 var c=[]; 50 for(var attr in String.prototype){ 51 if(attr.slice(0, 8) == "decrypt_"){ 52 c.push(attr.slice(8)); 53 53 } 54 return c; 55 }; 54 } 55 return c; 56 }; 57 58 /** 59 Encrypts a string. 60 Parameters but the crypdec parameter are forwardet to the crypdec. 61 @param codec The codec to use. 62 **/ 63 String.prototype.encrypt=function(crydec){ 64 var n = "encrypt_" + crydec; 65 if(String.prototype[n]){ 66 var args=[]; 67 for(var i=1;i<arguments.length;i++){ 68 args[i-1] = arguments[i]; 69 } 70 return String.prototype[n].apply(this, args); 71 }else{ 72 throw new Exception("Decrypter '%s' not found.".format(crydec)); 73 } 74 }; 75 /** 76 Decrypts a string. 77 Parameters but the crypdec parameter are forwardet to the crypdec. 78 @param codec The codec to use. 79 **/ 80 String.prototype.decrypt=function(crydec){ 81 var n = "decrypt_" + crydec; 82 if(String.prototype[n]){ 83 var args=[]; 84 for(var i=1;i<arguments.length;i++){ 85 args[i-1] = arguments[i]; 86 } 87 return String.prototype[n].apply(this, args); 88 }else{ 89 throw new Exception("Encrypter '%s' not found.".format(crydec)); 90 } 91 }; 92 93 /** 94 Encrypts a string using XOR. 95 The whole String will be XORed with the key. 96 If the key is shorter than the String then it will be multiplied to fit the length of the String. 97 @param key The key to use for encryption. 98 **/ 99 String.prototype.encrypt_xor=function(key){ 100 var e=new Array(this.length); 101 var l=key.length; 102 for(var i=0;i<this.length;i++){ 103 e[i] = String.fromCharCode(this.charCodeAt(i) ^ key.charCodeAt(i % l)); 104 } 105 return e.join(""); 106 }; 107 /** 108 Decrypts a string using XOR. 109 Since XORing is symetric it is the same as the encrypter. 110 @param key The key to use for decryption. 111 **/ 112 String.prototype.decrypt_xor=String.prototype.encrypt_xor; 113 /** 114 Encrypts a string using the ARC4 algorithm. 115 @param key The key to use for encryption. 116 **/ 117 String.prototype.encrypt_rc4=function(key){ 118 //generate substitution box 119 var sbox = new Array(256); 120 for (var i=0; i<256; i++){ 121 sbox[i]=i; 122 } 56 123 57 /** 58 Encrypts a string. 59 Parameters but the crypdec parameter are forwardet to the crypdec. 60 @param codec The codec to use. 61 **/ 62 String.prototype.encrypt=function(crydec){ 63 var n = "encrypt_" + crydec; 64 if(String.prototype[n]){ 65 var args=[]; 66 for(var i=1;i<arguments.length;i++){ 67 args[i-1] = arguments[i]; 68 } 69 return String.prototype[n].apply(this, args); 70 }else{ 71 throw new mod.Exception("Decrypter '%s' not found.".format(crydec)); 72 } 73 }; 74 /** 75 Decrypts a string. 76 Parameters but the crypdec parameter are forwardet to the crypdec. 77 @param codec The codec to use. 78 **/ 79 String.prototype.decrypt=function(crydec){ 80 var n = "decrypt_" + crydec; 81 if(String.prototype[n]){ 82 var args=[]; 83 for(var i=1;i<arguments.length;i++){ 84 args[i-1] = arguments[i]; 85 } 86 return String.prototype[n].apply(this, args); 87 }else{ 88 throw new mod.Exception("Encrypter '%s' not found.".format(crydec)); 89 } 90 }; 124 //swap things around 125 var j=0; 126 for (var i=0; i < 256; i++) { 127 j = (j + sbox[i] + key.charCodeAt(i % key.length)) % 256; 128 var tmp = sbox[i]; 129 sbox[i] = sbox[j]; 130 sbox[j] = tmp; 131 } 91 132 92 /** 93 Encrypts a string using XOR. 94 The whole String will be XORed with the key. 95 If the key is shorter than the String then it will be multiplied to fit the length of the String. 96 @param key The key to use for encryption. 97 **/ 98 String.prototype.encrypt_xor=function(key){ 99 var e=new Array(this.length); 100 var l=key.length; 101 for(var i=0;i<this.length;i++){ 102 e[i] = String.fromCharCode(this.charCodeAt(i) ^ key.charCodeAt(i % l)); 103 } 104 return e.join(""); 105 }; 106 /** 107 Decrypts a string using XOR. 108 Since XORing is symetric it is the same as the encrypter. 109 @param key The key to use for decryption. 110 **/ 111 String.prototype.decrypt_xor=String.prototype.encrypt_xor; 112 /** 113 Encrypts a string using the ARC4 algorithm. 114 @param key The key to use for encryption. 115 **/ 116 String.prototype.encrypt_rc4=function(key){ 117 //generate substitution box 118 var sbox = new Array(256); 119 for (var i=0; i<256; i++){ 120 sbox[i]=i; 121 } 122 123 //swap things around 124 var j=0; 125 for (var i=0; i < 256; i++) { 126 j = (j + sbox[i] + key.charCodeAt(i % key.length)) % 256; 127 var tmp = sbox[i]; 128 sbox[i] = sbox[j]; 129 sbox[j] = tmp; 130 } 131 132 //calculate the result 133 var i=256; 134 var j=256; 135 var rslt=new Array(this.length); 136 for (var k=0; k < this.length; k++) { 137 i = (i + 1) % 256; 138 j = (j + sbox[i]) % 256; 139 var tmp = sbox[i]; 140 sbox[i] = sbox[j]; 141 sbox[j] = tmp; 142 t = (sbox[i] + sbox[j]) % 256; 143 rslt[k] = String.fromCharCode(this.charCodeAt(k) ^ sbox[t]); 144 } 145 return rslt.join(""); 146 }; 147 /** 148 Decrypts a string using the ARC4 algorithm. 149 Since it is symetric it is the same as the encrypter. 150 @param key The key to use for decryption. 151 **/ 152 String.prototype.decrypt_rc4=Stri
