From a2b51fe212980dc4117e7368240dddf523d9f367 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Sun, 3 Apr 2022 18:10:00 +0800 Subject: [PATCH] optimize libmd5.nas & test/md5.nas --- README.md | 9 +- module/libmd5.nas | 12 ++- test/md5.nas | 225 +++++++++++++++++++++++--------------------- test/md5compare.nas | 46 ++++----- 4 files changed, 157 insertions(+), 135 deletions(-) diff --git a/README.md b/README.md index df3f2cf..b9fdf6f 100644 --- a/README.md +++ b/README.md @@ -124,7 +124,7 @@ in __`linux/macOS/Unix`__: ## __How to Use__ -First we should learn how to write a program using this language, +First we should learn how to write and run a program using this language, click to see the [__tutorial__](#tutorial). Input this command to run scripts __directly__: @@ -184,6 +184,13 @@ If your system is __`Windows`__ and you want to output unicode,please use this c > chcp 65001 +or you could write this in your nasal code: + +```javascript +if(os.platform()=="windows") + system("chcp 65001"); +``` + ## __Tutorial__ Nasal is really __easy__ to learn. diff --git a/module/libmd5.nas b/module/libmd5.nas index ca24f8a..8eb68bb 100644 --- a/module/libmd5.nas +++ b/module/libmd5.nas @@ -1,8 +1,10 @@ import("lib.nas"); -var md5=func(str){ +var md5=func(){ var lib=dylib.dlopen("./module/libmd5"~(os.platform()=="windows"?".dll":".so")); - var res=dylib.dlcall(dylib.dlsym(lib,"nas_md5"),str); - dylib.dlclose(lib); - return res; -} \ No newline at end of file + var sym=dylib.dlsym(lib,"nas_md5"); + var call=dylib.dlcall; + return func(s){ + return call(sym,s); + }; +}(); \ No newline at end of file diff --git a/test/md5.nas b/test/md5.nas index 9aa7de5..59db46f 100644 --- a/test/md5.nas +++ b/test/md5.nas @@ -1,44 +1,39 @@ import("lib.nas"); - var check=func(x){ + if(x<0x100000000) + return x; return x-int(x/0x100000000)*0x100000000; } var u32_bits_and=func(x,y){ - x=check(x); - y=check(y); + (x,y)=(check(x),check(y)); var (res,op)=(0,1); for(var i=0;i<32;i+=1){ var (tmpx,tmpy)=(x-int(x/2)*2,y-int(y/2)*2); res+=op*(tmpx==1 and tmpy==1); - x=int(x/2); - y=int(y/2); + (x,y)=(int(x/2),int(y/2)); op*=2; } return res; } var u32_bits_or=func(x,y){ - x=check(x); - y=check(y); + (x,y)=(check(x),check(y)); var (res,op)=(0,1); for(var i=0;i<32;i+=1){ var (tmpx,tmpy)=(x-int(x/2)*2,y-int(y/2)*2); res+=op*(tmpx==1 or tmpy==1); - x=int(x/2); - y=int(y/2); + (x,y)=(int(x/2),int(y/2)); op*=2; } return res; } var u32_bits_xor=func(x,y){ - x=check(x); - y=check(y); + (x,y)=(check(x),check(y)); var (res,op)=(0,1); for(var i=0;i<32;i+=1){ var (tmpx,tmpy)=(x-int(x/2)*2,y-int(y/2)*2); res+=op*(tmpx!=tmpy); - x=int(x/2); - y=int(y/2); + (x,y)=(int(x/2),int(y/2)); op*=2; } return res; @@ -54,22 +49,25 @@ var u32_bits_not=func(x){ return res; } -var hex32str=func(num){ +var hex32str=func(){ var ch=["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f"]; - var res=""; - for(var i=0;i<4;i+=1){ - var tmp=""; - for(var j=0;j<2;j+=1){ - tmp=ch[u32_bits_and(num,0x0f)]~tmp; - num=int(num/16); - } - res~=tmp; + var tbl=[]; + setsize(tbl,256); + for(var i=0;i<16;i+=1){ + for(var j=0;j<16;j+=1) + tbl[i*16+j]=ch[i]~ch[j]; } - return res; -} - -var _md5=func(s){ + return func(num){ + var res=""; + for(var i=0;i<4;i+=1){ + res~=tbl[u32_bits_and(num,0xff)]; + num=int(num/256); + } + return res; + }; +}(); +var _md5=func(){ var K=[ 0xd76aa478, 0xe8c7b756, 0x242070db, 0xc1bdceee, 0xf57c0faf, 0x4787c62a, 0xa8304613, 0xfd469501, 0x698098d8, 0x8b44f7af, 0xffff5bb1, 0x895cd7be, 0x6b901122, 0xfd987193, 0xa679438e, 0x49b40821, @@ -135,92 +133,105 @@ var _md5=func(s){ u32_bits_or(x,u32_bits_not(z)) ); } + var functions=[ + F,F,F,F,F,F,F,F,F,F,F,F,F,F,F,F, + G,G,G,G,G,G,G,G,G,G,G,G,G,G,G,G, + H,H,H,H,H,H,H,H,H,H,H,H,H,H,H,H, + I,I,I,I,I,I,I,I,I,I,I,I,I,I,I,I + ]; - var len=size(s)*8; - var res=[]; - setsize(res,len); - var v=[128,64,32,16,8,4,2,1]; - for(var i=0;i448){ - append(res,1); - mod+=1; - # 512+448=960 - for(;mod<960;mod+=1) - append(res,0); - } - var (tmp,cnt,t)=([],0,0); - foreach(var i;res){ - if(!cnt) t=i; - else t=t*2+i; - cnt+=1; - if(cnt==8){ - cnt=0; - append(tmp,t); + # +------len------+--1~512--+--64--+ + # | text | fill | size | + # +---------------+---------+------+ N*512 + var mod=size(res)-int(size(res)/512)*512; + if(mod==448){ + var i=size(res); + setsize(res,i+512); + res[i]=1; + for(i+=1;i448){ + var i=size(res); + setsize(res,int(i/512)*512+960); # 512+448=960 + res[i]=1; + for(i+=1;i16 uint32=>64 byte=>512 bit - # because using double to discribe number - # this may only work when string's length is under 1<<51 - tmp=[]; - setsize(tmp,size(res)/4); - for(var i=0;i0;i-=1){ + tmp[tmp_size-4-i]=int(lower32-int(lower32/256)*256); + lower32=int(lower32/256); + } + for(var i=4;i>0;i-=1){ + tmp[tmp_size-i]=int(higher32-int(higher32/256)*256); + higher32=int(higher32/256); + } + res=tmp; + + # 1 block=>16 uint32=>64 byte=>512 bit + # because using double to discribe number + # this may only work when string's length is under 1<<51 + tmp=[]; + setsize(tmp,size(res)/4); + for(var i=0;i","!","@","#","$","%", "^","&","*","(",")","-","=","\\","|","[","]","{","}","`"," ","\t","?" ]; - var (prt,lastpercent,percent)=("",0,0); - for(var i=1;i<=total;i+=1){ - var s=""; - for(var j=0;j=2){ + prt~="#"; + lastpercent=percent; + } + var tmp=prt; + for(var spc=size(prt);spc<50;spc+=1) + tmp~=" "; + print(" |",tmp,"| ",percent,"% (",i,"/",total,")\t",res," \r"); } - var res=md5(s); - if(cmp(res,_md5(s))){ - die("error: "~str(i)); - } - percent=int(i/total*100); - if(percent-lastpercent>=2){ - prt~="#"; - lastpercent=percent; - } - var tmp=prt; - for(var spc=size(prt);spc<50;spc+=1) - tmp~=" "; - print(" |",tmp,"| ",percent,"% (",i,"/",total,")\t",res," \r"); - } - print('\n'); -} + print('\n'); + }; +}(); var filechecksum=func(){ var getname=func(s){