X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Flib.c;h=db0145057cd18b754b4d028e2b2a79a89d3a1877;hb=c2adb8d03edd3bcf25eb2fd9a90ed74200ca78fe;hp=3d91c71c742b59f2cf3eee6a50bb6409c83e131f;hpb=77ed20ce9d7e25654215980d0f89e63b8dd366f0;p=tpg%2Facess2.git diff --git a/Kernel/lib.c b/Kernel/lib.c index 3d91c71c..db014505 100644 --- a/Kernel/lib.c +++ b/Kernel/lib.c @@ -2,7 +2,7 @@ * Acess2 * Common Library Functions */ -#include +#include // === CONSTANTS === #define RANDOM_SEED 0xACE55052 @@ -92,12 +92,12 @@ int strpos(const char *Str, char Ch) } /** - * \fn int ByteSum(void *Ptr, int Size) + * \fn Uint8 ByteSum(void *Ptr, int Size) * \brief Adds the bytes in a memory region and returns the sum */ -int ByteSum(void *Ptr, int Size) +Uint8 ByteSum(void *Ptr, int Size) { - int sum = 0; + Uint8 sum = 0; while(Size--) sum += *(Uint8*)Ptr++; return sum; } @@ -341,5 +341,95 @@ Uint rand() return giRandomState; } +/// \name Memory Validation +/// \{ +/** + * \brief Checks if a string resides fully in valid memory + */ +int CheckString(char *String) +{ + // Check 1st page + if( MM_IsUser( (tVAddr)String ) ) + { + // Traverse String + while( *String ) + { + if( !MM_IsUser( (tVAddr)String ) ) + return 0; + // Increment string pointer + String ++; + } + return 1; + } + else if( MM_GetPhysAddr( (tVAddr)String ) ) + { + // Traverse String + while( *String ) + { + if( !MM_GetPhysAddr( (tVAddr)String ) ) + return 0; + // Increment string pointer + String ++; + } + return 1; + } + return 0; +} + +/** + * \brief Check if a sized memory region is valid memory + */ +int CheckMem(void *Mem, int NumBytes) +{ + tVAddr addr = (tVAddr)Mem; + + if( MM_IsUser( addr ) ) + { + while( NumBytes-- ) + { + if( !MM_IsUser( addr ) ) + return 0; + addr ++; + } + return 1; + } + else if( MM_GetPhysAddr( addr ) ) + { + while( NumBytes-- ) + { + if( !MM_GetPhysAddr( addr ) ) + return 0; + addr ++; + } + return 1; + } + return 0; +} +/// \} + +/** + * \brief Search a string array for \a Needle + * \note Helper function for eTplDrv_IOCtl::DRV_IOCTL_LOOKUP + */ +int LookupString(char **Array, char *Needle) +{ + int i; + for( i = 0; Array[i]; i++ ) + { + if(strcmp(Array[i], Needle) == 0) return i; + } + return -1; +} + +EXPORT(strlen); +EXPORT(strdup); +EXPORT(strcmp); +EXPORT(strncmp); +EXPORT(strcpy); +//EXPORT(strncpy); + EXPORT(timestamp); EXPORT(ReadUTF8); +EXPORT(CheckMem); +EXPORT(CheckString); +EXPORT(LookupString);