Welcome to the Second Life Forums Archive

These forums are CLOSED. Please visit the new forums HERE

Adler32 CRC (hash) and char -> ASCII functions

Huns Valen
Don't PM me here.
Join date: 3 May 2003
Posts: 2,749
05-23-2006 21:16
This code runs a cyclic redundancy check (CRC) on a string that you supply. This is useful for generating hash values. One time it returned a negative value, so I guess it can overflow sometimes! Keep this in mind if you use it to generate checksums for use with externally hosted logic. This function may actually need a long (64-bit) integer to avoid overflowing, but these are not currently available in LSL.

CODE
// Get the Adler32 CRC (i.e. hash value) of a string of characters.
// Adapted from code found at php.net/crc32 and released into the public domain by Huns Valen.
// It is not as reliable as a full CRC32, but it does run faster!

string ASCIndex;
initAsc() {
// This is Xylor's char array from XyText
ASCIndex = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`";
ASCIndex += "abcdefghijklmnopqrstuvwxyz{|}~";
ASCIndex += "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
}

integer asc(string char) {
return llSubStringIndex(ASCIndex, char) + 32;
}

integer getAdler32CRC(string str) {
integer crc;
integer i;
integer j = llStringLength(str);
integer low = 1;
integer high = 0;
integer char;
for(i=0; i<j; i++) {
char = asc(llGetSubString(str, i, i));
low = (low + char) % 65521;
high = (high + low) % 65521;
}
crc = high * 65536 + low;
return crc;
}

default {
state_entry() {
initAsc();
float startTime = llGetTime();
llOwnerSay("Computing Adler32 CRC on a big string, please wait...");
integer CRC = getAdler32CRC("Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.");
llOwnerSay("Computed Adler32 CRC on 'Lorem ipsum...' in " + (string)(llGetTime() - startTime) + " seconds. The CRC value is " + (string)CRC + ".");
}
}
Nada Epoch
The Librarian
Join date: 4 Nov 2002
Posts: 1,423
Discussion Thread
05-27-2006 16:16
/54/f2/109802/1.html
_____________________
i've got nothing. ;)