WEB开发网
开发学院网页设计JavaScript 利用LZ78对Javascript脚本进行压缩 阅读

利用LZ78对Javascript脚本进行压缩

 2010-09-14 13:18:50 来源:WEB开发网   
核心提示:functionBytesToBits(len,num){varo="";while(len--){if(num&0x01){o="1"+o;}else{o="0"+o;}num>>=1;}returno;}functionBitsToBytes(

functionBytesToBits(len,num){
  varo="";
  while(len--){
    if(num&0x01){
      o="1"+o;
    }else{
      o="0"+o;
    }
    num>>=1;
  }
  returno;
}
functionBitsToBytes(i){
 o=42;
 if(i.charAt(0)=='1')
   o+=32;
 if(i.charAt(1)=='1')
   o+=16;
 if(i.charAt(2)=='1')
   o+=8;
 if(i.charAt(3)=='1')
   o+=4;
 if(i.charAt(4)=='1')
   o+=2;
 if(i.charAt(5)=='1')
   o+=1;
 if(o>=92)
   o++;
 returnString.fromCharCode(o);
}
functionCompressConfirm(){
 if(confirm("Areyousurethatyouwanttodothis? Itcantakealongtime!")){
   CompressCode();
 }
}
functionCompressCode(){
  varletter_codes=newArray();
  varcode_len=1;
  varcode_next=0;
  varcode_len_trigger=1;
  vart_out="";
  vart_bits="";
  vart_in=document.daForm.Orig.value;
  varprog=document.daForm.Progress;
  varlast_code;
  varsearch_value;
  varmincode=256,maxcode=-1;
  for(vari=0;i<t_in.length;i++){
    varc=t_in.charCodeAt(i);
    if(mincode>c){
      mincode=c;
    }
    if(maxcode<c){
      maxcode=c;
    }
  }
  for(vari=mincode;i<=maxcode;i++){
    if(code_next==code_len_trigger){
      code_len++;
      code_len_trigger*=2;
    }
    letter_codes[String.fromCharCode(i)]=code_next++;
  }
  last_code="";
  search_value="";
  for(vari=0;i<t_in.length;i++){
    search_value+=t_in.charAt(i);
    if(i%250==0){
      prog.value="Encoding: "+i+"of"+t_in.length+"("+Math.floor(100*i/t_in.length)+"%)";
    }
    //TODO: Possiblyadd"&&i!=t_in.length-1
    //Thatwaythechunkaftertheforloopdoesnotneedtoexist
    if(letter_codes[search_value]!==undefined  ){
      last_code=letter_codes[search_value];
    }else{
      if(code_next==code_len_trigger){
        code_len++;
        code_len_trigger<<=1;
      }
      letter_codes[search_value]=code_next++;
      t_bits+=BytesToBits(code_len,last_code);
      while(t_bits.length>=6){
        t_out+=BitsToBytes(t_bits);
        t_bits=t_bits.slice(6,t_bits.length);
      }
      search_value=t_in.charAt(i);
      last_code=letter_codes[search_value];
    }
  }
  if(code_next==code_len_trigger){
    code_len++;
    code_len_trigger<<=1;
  }
  t_bits+=BytesToBits(code_len,last_code);
  while(t_bits.length){
    t_out+=BitsToBytes(t_bits);
    t_bits=t_bits.slice(6,t_bits.length);
  }
  document.daForm.Comp.value=WrapInJS(t_out);
  prog.value="Done="+t_in.length+"to"+document.daForm.Comp.value.length+
    "="+(t_in.length-document.daForm.Comp.value.length)+"bytesofsavings"
}
functionCreatePopup(str)
{
  ShowMeWindow=window.open("","","location=no,directories=no,menubar=no,"+
    "resizable=yes,scrollbars=yes,status=yes,toolbar=no,width=300,height=240");
  ShowMeWindow.document.write(str);
  ShowMeWindow.document.close();
}
functionWrapInJS(str,mincode,maxcode)
{
  varout='<sc'+'riptlanguage="JavaScript">'+"n";
  out+='d="'
  while(str.length>80)
  {
    out+=str.slice(0,80)+""n+"";
    str=str.slice(80,str.length);
  }
  out+=str+"";n";
  out+="varl,x,b,o,t,c,s,r,p,e;l=c=p=-1;x=b=e=0;o='';t=newArray();s=r=1;n";
  out+="functiong(){if(!b){if(x>=d.length)return-1;l=d.charCodeAt(x);n";
  out+="if(l>92)l--;x++;b=6;}return(l>>--b)&1;}n";
  out+="functiona(){varb,n=0,c=0;for(;n<s;n++)c=c*2+(b=g());return(b<0)?-1:c;}n";
  out+="for(vari=y;i<=z;i++){if(e+1==r){s++;r*=2;}t[e++]=String.fromCharCode(i);}n";
  out+="while((c=a())>=0){if(p>=0)t[e++]=t[p]+t[c].charAt(0);o+=t[c];p=c;n";
  out+="if(t.length+1==r){r*=2;s++;}}n";
  out+="document.write(o);n";
  returnout;
}

1 2  下一页

Tags:利用 LZ Javascript

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接