#include <common.h>
// === 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
// === PROTOTYPES ===
int ReadUTF8(Uint8 *str, Uint32 *Val);
+// === GLOBALS ===
+static Uint giRandomState = RANDOM_SEED;
+
// === CODE ===
static const char cUCDIGITS[] = "0123456789ABCDEF";
/**
* \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++;
* \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++)
* \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++;
* \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++;
* \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++;
* \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 int strpos8(char *str, Uint32 search)
+ * \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(const 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;
*/
int ReadUTF8(Uint8 *str, Uint32 *Val)
{
+ *Val = 0xFFFD; // Assume invalid character
+
// ASCII
if( !(*str & 0x80) ) {
*Val = *str;
// Middle of a sequence
if( (*str & 0xC0) == 0x80 ) {
- *Val = -1;
return 1;
}
}
// 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
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);