Key compression

From Second Life Wiki
Revision as of 18:13, 25 February 2008 by Tmyclyk Dmytryk (talk | contribs)
Jump to navigation Jump to search

Compress Keys of all sorts

<lsl> string strFullCompression = "0123456789abcdefghijklmnopqrstuvwxyz!\"#$%&'()*+,-./:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`{|}~¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅ"; string compressKey(key id){

   string str = (string)llParseString2List((string)id,["-"],[]);
   integer i;
   string output;
   integer len = llStringLength(str);
   while(i < len){
       integer val = llSubStringIndex(strFullCompression,(llGetSubString(str,i,i)));
       ++i;    
       integer val2 = llSubStringIndex(strFullCompression,(llGetSubString(str,i,i)));
       val *= 16;
       val += val2;
       output += llGetSubString(strFullCompression,val,val);
       ++i;
   }
   
   return output;

}

string decompressKey(string compressed){

   integer i;
   integer len = llStringLength(compressed);
   string output;
   while(i < len){
       integer indx = llSubStringIndex(strFullCompression,llGetSubString(compressed,i,i));
       integer val1 = (integer)indx / 16;
       integer val2 = indx % 16;
       output += llGetSubString(strFullCompression,val1,val1) + llGetSubString(strFullCompression,val2,val2);        
       ++i;    
   }
   
   return llGetSubString(llInsertString(llInsertString(llInsertString(llInsertString(llInsertString(llToLower(output),12,""),8,"-"),13,"-"),18,"-"),23,"-"),0,35);

} </lsl>


This code is modified by SiRiS Asturias for efficiency, I have both here because on the mono they're both giving consistent but different outputs.. It's for reference. <lsl> string compressKey(key id){

   id = (string)llParseString2List(id,(list)"-",[]);   
   integer i = llStringLength(id);    
   string output;    
   while(i--){     
       integer val = llSubStringIndex(strFullCompression,llGetSubString(id,i,i)) + (llSubStringIndex(strFullCompression,llGetSubString(id,i,i--)) << 4 );   
       output += llGetSubString(strFullCompression,val,val);     
       //++i;   
   }    
   return output;

} string decompressKey(string compressed){

   integer i = llStringLength(compressed);  
   string output;     while(i--){      
       integer indx = llSubStringIndex(strFullCompression,llGetSubString(compressed,i,i)); 
       integer val1 = (integer)(indx >> 4);   
       integer val2 = indx % 16;      
       output += llGetSubString(strFullCompression,val2,val2) + llGetSubString(strFullCompression,val1,val1);   
   }     
   return llGetSubString(llInsertString(llInsertString(llInsertString(llInsertString(output,8,"-"),13,"-"),18,"-"),23,"-"),0,35);

} </lsl>