X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;f=Kernel%2Flib.c;h=3399d3d85ca7af8fb496117c03f09d7eb4c8e170;hb=ba8c9b788dd5b91dd172d2bbf07ae94e660ffd7d;hp=ee9f24e0f6bba679a0f977661918cd1f73b17b43;hpb=8bc40333b1401d7616b225945fee53d972c2f418;p=tpg%2Facess2.git diff --git a/Kernel/lib.c b/Kernel/lib.c index ee9f24e0..3399d3d8 100644 --- a/Kernel/lib.c +++ b/Kernel/lib.c @@ -5,6 +5,10 @@ #include // === CONSTANTS === +#define RANDOM_SEED 0xACE55052 +#define RANDOM_A 0x00731ADE +#define RANDOM_C 12345 +#define RANDOM_SPRUCE 0xf12b02b // Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec const short DAYS_BEFORE[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; #define UNIX_TO_2K ((30*365*3600*24) + (7*3600*24)) //Normal years + leap years @@ -12,6 +16,9 @@ const short DAYS_BEFORE[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 3 // === PROTOTYPES === int ReadUTF8(Uint8 *str, Uint32 *Val); +// === GLOBALS === +static Uint giRandomState = RANDOM_SEED; + // === CODE === static const char cUCDIGITS[] = "0123456789ABCDEF"; /** @@ -134,11 +141,24 @@ int strcmp(char *str1, char *str2) */ int strncmp(char *Str1, char *Str2, size_t num) { - while(num-- && *Str1 && *Str1 == *Str2) + if(num == 0) return 0; // TODO: Check what should officially happen here + while(--num && *Str1 && *Str1 == *Str2) Str1++, Str2++; return *Str1-*Str2; } +/** + * \fn char *strdup(char *str) + * \brief Duplicates a string + */ +char *strdup(char *str) +{ + char *ret; + ret = malloc(strlen(str)+1); + strcpy(ret, str); + return ret; +} + /** * \fn int strpos8(char *str, Uint32 search) * \brief Search a string for a UTF-8 character @@ -168,6 +188,8 @@ int strpos8(char *str, Uint32 Search) */ int ReadUTF8(Uint8 *str, Uint32 *Val) { + *Val = 0xFFFD; // Assume invalid character + // ASCII if( !(*str & 0x80) ) { *Val = *str; @@ -176,7 +198,6 @@ int ReadUTF8(Uint8 *str, Uint32 *Val) // Middle of a sequence if( (*str & 0xC0) == 0x80 ) { - *Val = -1; return 1; } @@ -217,10 +238,55 @@ int ReadUTF8(Uint8 *str, Uint32 *Val) } // UTF-8 Doesn't support more than four bytes - *Val = -1; return 4; } +/** + * \fn int WriteUTF8(Uint8 *str, Uint32 Val) + * \brief Write a UTF-8 character sequence to a string + */ +int WriteUTF8(Uint8 *str, Uint32 Val) +{ + // ASCII + if( Val < 128 ) { + *str = Val; + return 1; + } + + // Two Byte + if( Val < 0x8000 ) { + *str = 0xC0 | (Val >> 6); + str ++; + *str = 0x80 | (Val & 0x3F); + return 2; + } + + // Three Byte + if( Val < 0x10000 ) { + *str = 0xE0 | (Val >> 12); + str ++; + *str = 0x80 | ((Val >> 6) & 0x3F); + str ++; + *str = 0x80 | (Val & 0x3F); + return 3; + } + + // Four Byte + if( Val < 0x110000 ) { + *str = 0xF0 | (Val >> 18); + str ++; + *str = 0x80 | ((Val >> 12) & 0x3F); + str ++; + *str = 0x80 | ((Val >> 6) & 0x3F); + str ++; + *str = 0x80 | (Val & 0x3F); + return 4; + } + + // UTF-8 Doesn't support more than four bytes + return 0; +} + /** * \fn Uint64 timestamp(int sec, int mins, int hrs, int day, int month, int year) * \brief Converts a date into an Acess Timestamp @@ -247,5 +313,20 @@ Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year) return stamp * 1000; } +/** + * \fn Uint rand() + * \brief Pseudo random number generator + * \note Unknown effectiveness (made up on the spot) + */ +Uint rand() +{ + Uint old = giRandomState; + // Get the next state value + giRandomState = (RANDOM_A*giRandomState + RANDOM_C) & 0xFFFFFFFF; + // Check if it has changed, and if it hasn't, change it + if(giRandomState == old) giRandomState += RANDOM_SPRUCE; + return giRandomState; +} + EXPORT(timestamp); EXPORT(ReadUTF8);