diff --git a/src/sys/misc/kernel_string_pool.c b/src/sys/misc/kernel_string_pool.c new file mode 100755 index 0000000..e85dfe7 --- /dev/null +++ b/src/sys/misc/kernel_string_pool.c @@ -0,0 +1,107 @@ +/* + "kernel_string_pool.c" + + created by: grayspace aka J. Leveille + for: UbixOS Project + date: May 11, 2002 + + purpose: to provide a mechanism for maintaining a pool of strings + for use by the kernel without unnecessary waste of memory + + NOTEs: + - for now only ASCII is supported + - done quickly, pretty hacky + + TODO: + - expand to support unicode + - use huffman encoding instead +*/ + +#include "gsdefines.h" +#include "kernel_string_pool.h" + +// returns pointer to character *after* substring read +static char * ReadSubString( char * p_dst, const BYTEg * p_substr ) +{ + char * p_retval; + char * p_curdst; + const char * p_cursubstr; + + p_retval = p_dst + (*p_substr) + 1; + p_cursubstr = (const char *) (p_substr + 1); + + do + { + *p_curdst = *p_cursubstr; + p_cursubstr++; + p_curdst++; + } + while( p_curdst < p_retval ); + + return p_retval; +} + +// returns pointer to character *after* space appended after substring read +static char * ReadSubStringAppendSpace( char * p_dst, const BYTEg * p_substr ) +{ + char * p_retval; + + p_retval = ReadSubString( p_dst, p_substr ); + *p_retval = ' '; + p_retval++; + + return p_retval; +} + +// gets the substring indicated by 'id' from the pool 'p_ksp' into 'p_dst' +// - returns pointer to 'p_dst' +char * KSTR_POOL_GetString( KSTR_POOL * p_ksp, char * p_dst, DWORDg id ) +{ + DWORDg numsubstrs; + DWORDg offset; + char * p_curdst; + const BYTEg * p_substr; + const BYTEg * p_krnstr; + + numsubstrs = (id & 0xFF); + offset = (id >> 8); + p_krnstr = p_ksp->p_krnstrs + offset; + + while( numsubstrs ) + { + // find substring + offset = (DWORDg) *p_krnstr; + p_krnstr++; + if( offset == 255 ) + { + offset += (((DWORDg) *p_krnstr)<<8); + p_krnstr++; + offset += (DWORDg) *p_krnstr; + p_krnstr++; + } + p_substr = p_ksp->p_substrs + p_ksp->p_substr_off[offset]; + + // append to destination and also append a space + p_curdst = ReadSubStringAppendSpace( p_curdst, p_substr ); + + // one less sub string + numsubstrs--; + } + + // find final substring + offset = (DWORDg) *p_krnstr; + p_krnstr++; + if( offset == 255 ) + { + offset += (((DWORDg) *p_krnstr)<<8); + p_krnstr++; + offset += (DWORDg) *p_krnstr; + p_krnstr++; + } + p_substr = p_ksp->p_substrs + p_ksp->p_substr_off[offset]; + + // append to destination + p_curdst = ReadSubString( p_curdst, p_substr ); + + return p_dst; +} diff --git a/src/sys/misc/kernel_string_pool.h b/src/sys/misc/kernel_string_pool.h new file mode 100755 index 0000000..f3e7a26 --- /dev/null +++ b/src/sys/misc/kernel_string_pool.h @@ -0,0 +1,100 @@ +/* + "kernel_string_pool.h" + + created by: grayspace aka J. Leveille + for: UbixOS Project + date: May 11, 2002 + + purpose: to provide a mechanism for maintaining a pool of strings + for use by the kernel without unnecessary waste of memory + + NOTEs: + - for now only ASCII is supported + - done quickly, pretty hacky + + TODO: + - expand to support unicode + - use huffman encoding instead +*/ + +#ifndef _KERNEL_STRING_POOL_H +#define _KERNEL_STRING_POOL_H + +/* memory FORMAT of a 'KRNSTR': (quick and dirty for now) + + an 'ID' is used to indicate a string within the pool + ---------------------------------------------------- + + 'ID' - [DWORD]: + - 'num-words' - bits [0,7] + - number of 'KRNWORD's in the string is 'num-words' + 1 + - bits [8,32] BYTE offset inside pool where the 'KRNSTR' + (kernel string) is stored + + 'KRNSTR's (kernel strings) are specified as an array of continuous BYTEs + ------------------------------------------------------------------------ + + 'KRNSTR': + - [KRNWORD] x 'num-words' (given by ID) + + 'KRNWORD': + - [BYTE] 'key1' + + - if key1 == [0,254]: + - key1 is the index of the 'sub-string' in the pool + + - otherwise key1 is an escape sequence and implies: + - [BYTE] 'key2' + - [BYTE] 'key3' + - the index of the 'sub-string' in the pool is key2*256 + key3 + 255 + + - NOTE: the reason for using two extra 8-bit keys instead of a 16-bit + value is in case we port to platform with data alignment exceptions + and this stuff is left dormant and never changed + + - NOTE: a 'space' (0x20) is implied between each 'KRNWORD', therefore + it is not currently possible to break encode string AB into substrings + A & B if AB does not contain any spaces + + - a 'sub-string' is found by using the the index of the + 'sub-string' in the pool to get an offset into an array + of continuous BYTEs via lookup + + 'sub-string's are specified as an array of continuous BYTEs + ----------------------------------------------------------- + + - a 'sub-string' is as follows: + + - [BYTE] 'numchars' [0,255] -> [1,256] + - [BYTE] x 'numchars' (the chars themselves) + + - WARNING: empty 'sub-string's are not allowed + +*/ + +/* WARNING: it is *assumed* any 'ID' given to a kernel + string pool function will be valid!! */ + +/* structure for kernel string pool */ +typedef struct tagKSTR_POOL +{ + /* pointer to where the kernel strings are stored */ + const BYTEg * p_krnstrs; + + /* pointer to where the sub strings are stored */ + const BYTEg * p_substrs; + + /* lookup table for substring offsets */ + const unsigned int * p_substr_off; +} +KSTR_POOL; + +/* the reason for this structure format is so that a tool + can be written which can generate a 'KSTR_POOL' from a configuration + file and place it into a 'C' source and header file pair as static data */ + +// gets the substring indicated by 'id' from the pool 'p_ksp' into 'p_dst' +// - returns pointer to 'p_dst' +char * KSTR_POOL_GetString( KSTR_POOL * p_ksp, char * p_dst, DWORDg id ); + +#endif /* _KERNEL_STRING_POOL_H */ \ No newline at end of file