Difference between revisions of "Key compression"

From Second Life Wiki
Jump to navigation Jump to search
(New page: '''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 = "0123456789ABCDEFGHI...)
 
(Replaced <source> with <syntaxhighlight>)
 
(8 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{Merge|Key_Compression}}
'''Compress Keys of all sorts'''
'''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%
<syntaxhighlight lang="lsl2">
 
string strFullCompression = "0123456789abcdefghijklmnopqrstuvwxyz!\"#$%&'()*+,-./:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`{|}~¡¢£¤¥¦§¨©ª«¬­®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿĀāĂ㥹ĆćĈĉĊċČčĎďĐđĒēĔĕĖėĘęĚěĜĝĞğĠġĢģĤĥĦħĨĩĪīĬĭĮįİıIJijĴĵĶķĸĹĺĻļĽľĿŀŁłŃńŅ";
<lsl>
string alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZqrstuvwxyz~!@#$%^&*()_+[]\{}|;':\",./<>?ÇüéâäàçêëèïîìÄÅÉæÆôöòûùÿÖÜáíóúñÑ¿abcdefghijklmnop";
integer boundOffset = 107;
string compressKey(key id){
string compressKey(key id){
     string str = (string)llParseString2List((string)id,["-"],[]);
     string str = (string)llParseString2List((string)id,["-"],[]);
Line 12: Line 11:
     integer len = llStringLength(str);
     integer len = llStringLength(str);
     while(i < len){
     while(i < len){
         integer val = llSubStringIndex(alphanum,llToUpper(llGetSubString(str,i,i)));
         integer val = llSubStringIndex(strFullCompression,(llGetSubString(str,i,i)));
         ++i;     
         ++i;     
         integer val2 = llSubStringIndex(alphanum,llToUpper(llGetSubString(str,i,i)));
         integer val2 = llSubStringIndex(strFullCompression,(llGetSubString(str,i,i)));
         if(val <= 7 && val2 != 15){
         val *= 16;
            val *= 15;
        val += val2;
            val += val2;
         output += llGetSubString(strFullCompression,val,val);
            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;
         ++i;
     }
     }
Line 35: Line 28:
     string output;
     string output;
     while(i < len){
     while(i < len){
         integer indx = llSubStringIndex(alphanum,llGetSubString(compressed,i,i));
         integer indx = llSubStringIndex(strFullCompression,llGetSubString(compressed,i,i));
         if(indx >= boundOffset){
         integer val1 = (integer)indx / 16;
            output += llGetSubString(alphanum,indx - boundOffset,indx - boundOffset);   
        integer val2 = indx % 16;
        }else{
        output += llGetSubString(strFullCompression,val1,val1) + llGetSubString(strFullCompression,val2,val2);         
            integer val1 = (integer)indx / 15;
            integer val2 = indx % 15;
            output += llGetSubString(alphanum,val1,val1) + llGetSubString(alphanum,val2,val2);         
        }
         ++i;     
         ++i;     
     }
     }
Line 48: Line 37:
     return llGetSubString(llInsertString(llInsertString(llInsertString(llInsertString(llInsertString(llToLower(output),12,""),8,"-"),13,"-"),18,"-"),23,"-"),0,35);
     return llGetSubString(llInsertString(llInsertString(llInsertString(llInsertString(llInsertString(llToLower(output),12,""),8,"-"),13,"-"),18,"-"),23,"-"),0,35);
}
}
</lsl>
</syntaxhighlight>
 


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


'''Example Output'''
string compressKey(key id){ 
Original Key: a80bba1c-bcc2-49a0-9142-6b1ffc4f0fb0
    id = (string)llParseString2List(id,(list)"-",[]); 
Compressed Key: kiBlkRlmmc.kajb}íbppmepapla
    integer i = llStringLength(id);   
  Compressed Key Length: 27
    string output;   
  Decompressed Key: a80bba1c-bcc2-49a0-9142-6b1ffc4f0fb0
    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);
}
</syntaxhighlight>

Latest revision as of 00:34, 14 September 2022

Compress Keys of all sorts

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);
}


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.

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

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);
}