Last active
September 17, 2020 07:49
-
-
Save FelixWolf/c3e4f0cc3e004c018a4eab9d5a946f8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
list Base64ToBytes(string base64){ | |
list result = []; | |
integer i; | |
integer l = llStringLength(base64); | |
for(;i<l;i+=4){ | |
integer bytes = (llBase64ToInteger(llGetSubString(base64, i,i+4))>>8)&0xFFFFFF; | |
result += [bytes>>16, (bytes>>8)&0xFF, bytes&0xFF]; | |
} | |
//TODO: Potential Speedup | |
//Instead of doing string comparison, consider checking length of input | |
//EG: Use l%3, if 0 = No pad, 1 = Two pad, 2 = One pad. | |
integer padLen = 1; | |
if(llGetSubString(base64, -2,-1) == "==") padLen = 3; | |
else if(llGetSubString(base64, -1,-1) == "=") padLen = 2; | |
return llList2List(result, 0, (3*(l/4))-padLen); | |
} | |
string BytesToBase64(list bytes){ | |
string result = ""; | |
integer i; | |
integer l = llGetListLength(bytes); | |
for(;i<l;i+=3){ | |
result += llGetSubString(llIntegerToBase64(((llList2Integer(bytes, i)&0xFF)<<24) | ((llList2Integer(bytes, i+1)&0xFF)<<16) | ((llList2Integer(bytes, i+2)&0xFF)<<8)), 0, 3); | |
} | |
//There is probably a better way to do padding. | |
return llGetSubString(result, 0, llFloor(((l<<2)|2)/3)-1) + llList2String(["","=","=="],l%3); | |
} | |
list Int32ToBytes(list array, integer bigEndian){ | |
list result = []; | |
integer i; | |
integer l = llGetListLength(array); | |
for(;i<l;i++){ | |
integer byte = llList2Integer(array, i); | |
if(bigEndian) | |
result += [(byte>>24)&0xFF,(byte>>16)&0xFF,(byte>>8)&0xFF,byte&0xFF]; | |
else | |
result += [byte&0xFF, (byte>>8)&0xFF, (byte>>16)&0xFF, (byte>>24)&0xFF]; | |
} | |
return result; | |
} | |
list BytesToInt32(list array, integer bigEndian){ | |
list result = []; | |
integer i; | |
integer l = llGetListLength(array); | |
for(;i<l;i+=4){ | |
integer byte = llList2Integer(array, i); | |
if(bigEndian) | |
result += [((llList2Integer(array, i)&0xFF)<<24) | ((llList2Integer(array, i + 1)&0xFF)<<16) | ((llList2Integer(array, i + 2)&0xFF)<<8) | (llList2Integer(array, i + 3)&0xFF)]; | |
else | |
result += [(llList2Integer(array, i)&0xFF) | ((llList2Integer(array, i + 1)&0xFF)<<8) | ((llList2Integer(array, i + 2)&0xFF)<<16) | ((llList2Integer(array, i + 3)&0xFF)<<24)]; | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment