LZW LSL Helpers

From Second Life Wiki
Jump to: navigation, search

LZW LSL Helpers

Description

The following LSL code contains helper-functions for using the LZW engine by Haravikk Mistral. These functions are all individually documented. For examples on how to use them you should view the examples page.

To use, you may simply paste the following code at the top of your script(s). Remember to remove any functions/constants that you do not use in order to conserve memory.

Script

// These variables are used to build communications. Commands are sent as 
// combined bits in the integer argument of a link-message, and are 
// recovered using masks, you may wish to read about bit-masks before 
// editing these values. These are used so the string argument is 
// kept free for data only.
//
// Commands take the following form (in hex):
//      0xFFMMIOvv
// Where the letters are:
//      F   Filter, used to quickly determine if a message is for us.
//      C   Command; encrypt/decrypt etc.
//      I   Type of data provided (hex, base64, etc.).
//      O   Desired type of data to be returned (hex, base64, etc.), 
//          this is unused in replies as the reply's value for I will 
//          be the request's value for O.
//      v   Variable, depends on mode.
 
// This mask allows the filter byte to be retrieved quickly
integer LSLLZW_FILTER_MASK      = 0xFF000000;
// This mask allows the mask byte to be retrieved quickly
integer LSLLZW_COMMAND_MASK     = 0x00FF0000;
// This mask allows the input type to be retrieved quickly
integer LSLLZW_INPUT_TYPE_MASK  = 0x0000F000;
// This mask allows the output type to be retireved quickly
integer LSLLZW_OUTPUT_TYPE_MASK = 0x00000F00;
// This mask allows the variable to retrieved quickly
integer LSLLZW_VARIABLE_MASK    = 0x000000FF;
// How many bits right variable must be shifted
integer LSLLZW_VARIABLE_SHIFT   = 0;
 
// A request
integer LSLLZW_FILTER_REQUEST   = 0x83000000;
// A reply
integer LSLLZW_FILTER_REPLY     = 0x84000000;
 
// An error occurred
integer LSLLZW_COMMAND_ERROR    = 0x00000000;
// Prime engine with key
integer LSLLZW_COMMAND_COMPRESS = 0x00010000;
// Compress message using expanded key
integer LSLLZW_COMMAND_DECOMPRESS = 0x00020000;
 
// Input type is hex
integer LSLLZW_INPUT_HEX        = 0x00000000;
// Input type is base64
integer LSLLZW_INPUT_BASE64     = 0x00001000;
 
// Output type is hex
integer LSLLZW_OUTPUT_HEX       = 0x00000000;
// Output type is base64
integer LSLLZW_OUTPUT_BASE64    = 0x00000100;
 
// The following extra variables are used to track our messages
key     requestID               = NULL_KEY;
 
// Sends hexadecimal data and gets compressed hexadecimal data back
lslLZWCompressHexToHex(integer targetLink, string hexData, key id) {
    llMessageLinked(
        targetLink,
        LSLLZW_FILTER_REQUEST | LSLLZW_COMMAND_COMPRESS | 
            LSLLZW_INPUT_HEX | LSLLZW_OUTPUT_HEX,
        (hexData = "") + hexData,
        requestID = id
    );
}
 
// Sends hexadecimal data and gets compressed base64 data back
lslLZWCompressHexToBase64(integer targetLink, string hexData, key id) {
    llMessageLinked(
        targetLink,
        LSLLZW_FILTER_REQUEST | LSLLZW_COMMAND_COMPRESS | 
            LSLLZW_INPUT_HEX | LSLLZW_OUTPUT_BASE64,
        (hexData = "") + hexData,
        requestID = id
    );
}
 
// Send base64 data and gets compressed hexadecimal data back
lslLZWCompressBase64ToHex(integer targetLink, string b64Data, key id) {
    llMessageLinked(
        targetLink,
        LSLLZW_FILTER_REQUEST | LSLLZW_COMMAND_COMPRESS | 
            LSLLZW_INPUT_BASE64 | LSLLZW_OUTPUT_HEX,
        (b64Data = "") + b64Data,
        requestID = id
    );    
}
 
// Send base64 data and gets compressed hexadecimal data back
lslLZWCompressBase64ToBase64(integer targetLink, string b64Data, key id) {
    llMessageLinked(
        targetLink,
        LSLLZW_FILTER_REQUEST | LSLLZW_COMMAND_COMPRESS | 
            LSLLZW_INPUT_BASE64 | LSLLZW_OUTPUT_BASE64,
        (b64Data = "") + b64Data,
        requestID = id
    );    
}
 
// Sends hexadecimal data and gets decompressed hexadecimal data back
lslLZWDecompressHexToHex(integer targetLink, string hexData, key id) {
    llMessageLinked(
        targetLink,
        LSLLZW_FILTER_REQUEST | LSLLZW_COMMAND_DECOMPRESS | 
            LSLLZW_INPUT_HEX | LSLLZW_OUTPUT_HEX,
        (hexData = "") + hexData,
        requestID = id
    );
}
 
// Sends hexadecimal data and gets decompressed base64 data back
lslLZWDecompressHexToBase64(integer targetLink, string hexData, key id) {
    llMessageLinked(
        targetLink,
        LSLLZW_FILTER_REQUEST | LSLLZW_COMMAND_DECOMPRESS | 
            LSLLZW_INPUT_HEX | LSLLZW_OUTPUT_BASE64,
        (hexData = "") + hexData,
        requestID = id
    );
}
 
// Send base64 data and gets decompressed hexadecimal data back
lslLZWDecompressBase64ToHex(integer targetLink, string b64Data, key id) {
    llMessageLinked(
        targetLink,
        LSLLZW_FILTER_REQUEST | LSLLZW_COMMAND_DECOMPRESS | 
            LSLLZW_INPUT_BASE64 | LSLLZW_OUTPUT_HEX,
        (b64Data = "") + b64Data,
        requestID = id
    );    
}
 
// Send base64 data and gets decompressed hexadecimal data back
lslLZWDecompressBase64ToBase64(integer targetLink, string b64Data, key id) {
    llMessageLinked(
        targetLink,
        LSLLZW_FILTER_REQUEST | LSLLZW_COMMAND_DECOMPRESS | 
            LSLLZW_INPUT_BASE64 | LSLLZW_OUTPUT_BASE64,
        (b64Data = "") + b64Data,
        requestID = id
    );    
}
 
// Tests to see if a message is a reply or not (TRUE/FALSE)
integer lslLZWIsReply(integer int, key id) {
    return (
        ((int & LSLLZW_FILTER_MASK) == LSLLZW_FILTER_REPLY) && 
        (id == requestID)
    );
}
 
// Grabs the mode of this reply. Should be one of the LSLLZW_COMMAND_* constants
integer lslLZWGetReplyMode(integer int) {
    return (int & LSLLZW_COMMAND_MASK);
}
 
// Grabs the data type of this reply. Should be one of the LSLLZW_INPUT_* 
// constants.
integer lslLZWGetReplyDataType(integer int) {
    return (int & LSLLZW_INPUT_TYPE_MASK);
}