X-Git-Url: https://git.ucc.asn.au/?a=blobdiff_plain;ds=sidebyside;f=Kernel%2Flib.c;h=d3fff546a72a70c0fb1c3e8075616e19b5792943;hb=b6c3b3cf61caafbd91bbf3acc81995e472656a5b;hp=ee9f24e0f6bba679a0f977661918cd1f73b17b43;hpb=8bc40333b1401d7616b225945fee53d972c2f418;p=tpg%2Facess2.git diff --git a/Kernel/lib.c b/Kernel/lib.c index ee9f24e0..d3fff546 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"; /** @@ -63,7 +70,7 @@ int tolower(int c) * \fn int strucmp(char *Str1, char *Str2) * \brief Compare \a Str1 and \a Str2 case-insensitively */ -int strucmp(char *Str1, char *Str2) +int strucmp(const char *Str1, const char *Str2) { while(*Str1 && tolower(*Str1) == tolower(*Str2)) Str1++, Str2++; @@ -74,7 +81,7 @@ int strucmp(char *Str1, char *Str2) * \fn int strpos(char *Str, char Ch) * \brief Search a string for an ascii character */ -int strpos(char *Str, char Ch) +int strpos(const char *Str, char Ch) { int pos; for(pos=0;Str[pos];pos++) @@ -97,7 +104,7 @@ int ByteSum(void *Ptr, int Size) * \fn Uint strlen(char *__str) * \brief Get the length of string */ -Uint strlen(char *__str) +Uint strlen(const char *__str) { Uint ret = 0; while(*__str++) ret++; @@ -108,7 +115,7 @@ Uint strlen(char *__str) * \fn char *strcpy(char *__str1, char *__str2) * \brief Copy a string to a new location */ -char *strcpy(char *__str1, char *__str2) +char *strcpy(char *__str1, const char *__str2) { while(*__str2) *__str1++ = *__str2++; @@ -121,7 +128,7 @@ char *strcpy(char *__str1, char *__str2) * \brief Compare two strings return the difference between * the first non-matching characters. */ -int strcmp(char *str1, char *str2) +int strcmp(const char *str1, const char *str2) { while(*str1 && *str1 == *str2) str1++, str2++; @@ -132,18 +139,42 @@ int strcmp(char *str1, char *str2) * \fn int strncmp(char *Str1, char *Str2, size_t num) * \brief Compare strings \a Str1 and \a Str2 to a maximum of \a num characters */ -int strncmp(char *Str1, char *Str2, size_t num) +int strncmp(const char *Str1, const 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(const char *str) +{ + char *ret; + ret = malloc(strlen(str)+1); + strcpy(ret, str); + return ret; +} + +/** + * \fn int DivUp(int num, int dem) + * \brief Divide two numbers, rounding up + * \param num Numerator + * \param dem Denominator + */ +int DivUp(int num, int dem) +{ + return (num+dem-1)/dem; +} + /** * \fn int strpos8(char *str, Uint32 search) * \brief Search a string for a UTF-8 character */ -int strpos8(char *str, Uint32 Search) +int strpos8(const char *str, Uint32 Search) { int pos; Uint32 val = 0; @@ -168,6 +199,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 +209,6 @@ int ReadUTF8(Uint8 *str, Uint32 *Val) // Middle of a sequence if( (*str & 0xC0) == 0x80 ) { - *Val = -1; return 1; } @@ -217,10 +249,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 +324,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);