/* * AUTHOR * N. Nielsen * * LICENSE * This software is in the public domain. * * The software is provided "as is", without warranty of any kind, * express or implied, including but not limited to the warranties * of merchantability, fitness for a particular purpose, and * noninfringement. In no event shall the author(s) be liable for any * claim, damages, or other liability, whether in an action of * contract, tort, or otherwise, arising from, out of, or in connection * with the software or the use or other dealings in the software. * * SUPPORT * Send bug reports to: */ #ifndef __RLIB_H__ #define __RLIB_H__ #ifdef __cplusplus extern "C" { #endif struct internal_state; struct r_stream; typedef unsigned char r_byte; typedef unsigned int r_uint; /* * r_replace: * Represents a replacement which was found. This structure is passed * to the r_match matching callback function below. */ typedef struct _r_replace { r_uint offset; /* The offset from the beginning of the file */ r_uint from; /* The offset from the beginning of the current buffer */ r_uint flen; /* The length of the data to be replaced */ r_byte* to; /* The data to replace */ r_uint tlen; /* The length of the replacement data */ } r_replace; /* * r_match * Callback function which receives replacements made by the script. * * stream: rlib stream * replace: The replacement. * * return 1 to accept this match, or 0 to skip. */ typedef int (*r_match)(struct r_stream* stream, r_replace* replace); /* * r_write * Callback used by the rlibRun output processed data. * * stream: rlib stream * data: The data * len: The number of bytes to write from data. */ typedef int (*r_write)(struct r_stream* stream, r_byte* data, r_uint len); /* * r_message * Callback which is sent messages from the 'message' command * in a rep script. * * stream: rlib stream * message: Null terminated message */ typedef void (*r_message)(struct r_stream* stream, const char* message); /* * r_stream * The basic data interface into rlib. */ typedef struct r_stream { /* ------ Data you supply -------------- */ r_byte* nextIn; /* The next byte to be read by rlib */ r_uint availIn; /* The number of bytes at nextIn */ r_write fWrite; /* Callback used for output */ r_match fMatch; /* Callback for confirmation or matching */ r_message fMessage; /* Callback for messages from script */ void* arg; /* Optional data for the above functions */ /* -------- Data returned -------------- */ r_uint total; /* Total replaces */ /* -------- Internal ------------------- */ struct internal_state* state; } r_stream; /* * r_script * Represents a loaded rep script along with syntax error * information. */ typedef struct _r_script { r_byte* ops; /* Compiled script */ r_uint len; /* The length of the script */ char* error; /* Syntax error details */ r_uint errline; /* Line number of syntax error */ } r_script; /* * rlibCompile * Call this function to compile a script. It will be compiled into the * r_sript structure. Be sure to zero the script structure passed. * * script: rlib script * data: rep script text. Must be null terminated. */ int rlibCompile(r_script* script, const char* data); /* * rlibDump * Write out byte codes for a script * * script: rlib script * f: Output stream to write to */ void rlibDump(r_script* script, FILE* f); /* * rlibFree * Free internal variables associated with this stream and/or * script structure. * * stream: rlib stream * script: rlib script */ void rlibFree(r_stream* stream, r_script* script); /* * rlibInit * Call this function to initialize a rlib stream. * For any of the following functions to work a stream must have been * initialized. * * stream: Pointer to a zero'd stream structure. * options: Can be any combination of the following mode values. */ int rlibInit(r_stream* stream, long options); /* Causes rlib to only output replacing text */ #define RLIB_MODE_PARSER 0x00000100 /* Causes rlib not to output anything */ #define RLIB_MODE_MATCHER 0x00000200 /* * rlibSetVar * Set a variable for use in the script. * * stream: rlib stream * var: Variable name * val: Variable value */ int rlibSetVar(r_stream* stream, const char* var, const char* val); /* * rlibRun * Run the script. rlibRun will only "eat" nextIn upto where the * last replace was found or up half whichever is more. Unless * it's the last buffer in which case it finishes up. * * stream: rlib stream * script: rlib script * done: flag whether this is the last buffer */ int rlibRun(r_stream* stream, r_script* script, int done); /* rlibClear: ----------------------------------------------------------- * Prepare and cleanup a context/stream after being run for a second * run. * * stream: rlib context/stream */ void rlibClear(r_stream* stream); /* ERROR CODES */ /* OK */ #define R_OK 0 /* Need more input */ #define R_IN 1 /* Finished processing script */ #define R_DONE 3 /* Not enough memory */ #define R_NOMEM -1 /* Syntax error in the script */ #define R_SYNTAX -2 /* Regular expression error in the script */ #define R_REGEXP -3 /* Enless loop encountered */ #define R_LOOP -4 /* User defined error from script */ #define R_USER -5 /* Read or write error */ #define R_IOERR -6 /* Invalid argument or stream data member */ #define R_INVARG -10 #ifdef __cplusplus } #endif #endif /* __RLIB_H__ */