Key compression

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

Compress Keys of all sorts

This function set will both compress and decompress a key. I've seen size cut down by anywhere from 25 - 50%

<lsl> string alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZqrstuvwxyz~!@#$%^&*()_+[]\{}|;':\",./<>?ÇüéâäàçêëèïîìÄÅÉæÆôöòûùÿÖÜáíóúñÑ¿abcdefghijklmnop"; integer boundOffset = 107; string compressKey(key id){

   string str = (string)llParseString2List((string)id,["-"],[]);
   integer i;
   string output;
   integer len = llStringLength(str);
   while(i < len){
       integer val = llSubStringIndex(alphanum,llToUpper(llGetSubString(str,i,i)));
       ++i;    
       integer val2 = llSubStringIndex(alphanum,llToUpper(llGetSubString(str,i,i)));
       if(val <= 7 && val2 != 15){
           val *= 15;
           val += val2;
           output += llGetSubString(alphanum,val,val);
       }else{
           output += llGetSubString(alphanum,boundOffset + val,boundOffset + val);
           val = llSubStringIndex(alphanum,llToUpper(llGetSubString(str,i,i)));
           output += llGetSubString(alphanum,boundOffset + val,boundOffset + val);
       }
       ++i;
   }
   
   return output;

}

string decompressKey(string compressed){

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

} </lsl>


Example Output

Original Key: a80bba1c-bcc2-49a0-9142-6b1ffc4f0fb0
Compressed Key: kiBlkRlmmc.kajb}íbppmepapla
Compressed Key Length: 27
Decompressed Key: a80bba1c-bcc2-49a0-9142-6b1ffc4f0fb0

Example Output 2

Original Key: a2e76fcd-9360-4f6d-a924-000000000003
Compressed Key: kcohgpmnjdæepúkjY000003
Compressed Key Length: 23
Decompressed Key: a2e76fcd-9360-4f6d-a924-000000000003