#define RANDOM_SEED 0xACE55052
#define RANDOM_A 0x00731ADE
#define RANDOM_C 12345
-#define RANDOM_SPRUCE 0xf12b02b
+#define RANDOM_SPRUCE 0xf12b039
// 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
int sprintf(char *__s, const char *__format, ...);
int tolower(int c);
int strucmp(const char *Str1, const char *Str2);
+char *strchr(const char *__s, int __c);
int strpos(const char *Str, char Ch);
Uint8 ByteSum(void *Ptr, int Size);
size_t strlen(const char *__s);
char *strncpy(char *__str1, const char *__str2, size_t max);
int strcmp(const char *str1, const char *str2);
int strncmp(const char *str1, const char *str2, size_t num);
-char *strdup(const char *Str);
+char *_strdup(const char *File, int Line, const char *Str);
char **str_split(const char *__str, char __ch);
int strpos8(const char *str, Uint32 Search);
int ReadUTF8(Uint8 *str, Uint32 *Val);
int WriteUTF8(Uint8 *str, Uint32 Val);
+
int DivUp(int num, int dem);
Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year);
Uint rand(void);
+
int CheckString(char *String);
int CheckMem(void *Mem, int NumBytes);
+
int ModUtil_LookupString(char **Array, char *Needle);
int ModUtil_SetIdent(char *Dest, char *Value);
+
+ int UnHex(Uint8 *Dest, size_t DestSize, const char *SourceString);
// === EXPORTS ===
EXPORT(atoi);
EXPORT(sprintf);
EXPORT(tolower);
EXPORT(strucmp);
+EXPORT(strchr);
EXPORT(strpos);
EXPORT(ByteSum);
EXPORT(strlen);
EXPORT(strncpy);
EXPORT(strcmp);
EXPORT(strncmp);
-EXPORT(strdup);
+//EXPORT(strdup);
+EXPORT(_strdup); // Takes File/Line too
EXPORT(str_split);
EXPORT(strpos8);
EXPORT(DivUp);
EXPORT(CheckMem);
EXPORT(ModUtil_LookupString);
EXPORT(ModUtil_SetIdent);
-
-// === GLOBALS ===
-static Uint giRandomState = RANDOM_SEED;
+EXPORT(UnHex);
// === CODE ===
/**
if(pos==__maxlen){return pos;}\
if(__s){__s[pos++]=ch;}else{pos++;}\
}while(0)
+#define GETVAL() do {\
+ if(isLongLong) val = va_arg(args, Uint64);\
+ else val = va_arg(args, unsigned int);\
+ }while(0)
/**
* \brief VArg String Number Print Formatted
*/
goto printString;
}
- // Get Argument
- val = va_arg(args, Uint);
-
// - Padding Side Flag
if(c == '+') {
bPadLeft = 1;
// - Minimum length
if(c == '*') { // Dynamic length
- minSize = val;
- val = va_arg(args, Uint);
+ minSize = va_arg(args, unsigned int);
c = *__format++;
}
else if('1' <= c && c <= '9')
{
c = *__format++;
if(c == 'l') {
- #if BITS == 32
- val |= (Uint64)va_arg(args, Uint) << 32;
- #endif
c = *__format++;
isLongLong = 1;
}
{
case 'd':
case 'i':
+ GETVAL();
if( isLongLong && val >> 63 ) {
PUTCH('-');
val = -val;
itoa(p, val, 10, minSize, pad);
goto printString;
case 'u':
+ GETVAL();
itoa(p, val, 10, minSize, pad);
goto printString;
+ case 'X':
+ #if BITS == 64
+ isLongLong = 1; // TODO: Handle non-x86 64-bit archs
+ #endif
+ GETVAL();
+ itoa(p, val, 16, minSize, pad);
+ goto printString;
+
case 'x':
+ GETVAL();
itoa(p, val, 16, minSize, pad);
goto printString;
case 'o':
+ GETVAL();
itoa(p, val, 8, minSize, pad);
goto printString;
case 'b':
+ GETVAL();
itoa(p, val, 2, minSize, pad);
goto printString;
case 'B': //Boolean
+ val = va_arg(args, unsigned int);
if(val) p = "True";
else p = "False";
goto printString;
// String - Null Terminated Array
case 's':
- p = (char*)(tVAddr)val;
+ p = va_arg(args, char*); // Get Argument
printString:
if(!p) p = "(null)";
len = strlen(p);
break;
case 'C': // Non-Null Terminated Character Array
- p = (char*)(tVAddr)val;
+ p = va_arg(args, char*);
if(!p) goto printString;
while(minSize--) PUTCH(*p++);
break;
// Single Character
case 'c':
default:
+ GETVAL();
PUTCH( (Uint8)val );
break;
}
return tolower(*Str1) - tolower(*Str2);
}
+/**
+ * \brief Locate a byte in a string
+ */
+char *strchr(const char *__s, int __c)
+{
+ for( ; *__s; __s ++ )
+ {
+ if( *__s == __c ) return (char*)__s;
+ }
+ return NULL;
+}
+
/**
* \fn int strpos(const char *Str, char Ch)
* \brief Search a string for an ascii character
return *Str1-*Str2;
}
+#if 0
/**
* \fn char *strdup(const char *Str)
* \brief Duplicates a string
{
char *ret;
ret = malloc(strlen(Str)+1);
+ if( !ret ) return NULL;
strcpy(ret, Str);
return ret;
}
+#else
+
+/**
+ * \fn char *_strdup(const char *File, int Line, const char *Str)
+ * \brief Duplicates a string
+ */
+char *_strdup(const char *File, int Line, const char *Str)
+{
+ char *ret;
+ ret = Heap_Allocate(File, Line, strlen(Str)+1);
+ if( !ret ) return NULL;
+ strcpy(ret, Str);
+ return ret;
+}
+#endif
/**
* \brief Split a string using the passed character
*/
Uint rand(void)
{
- Uint old = giRandomState;
+ #if 0
+ static Uint state = RANDOM_SEED;
+ Uint old = state;
// Get the next state value
- giRandomState = (RANDOM_A*giRandomState + RANDOM_C) & 0xFFFFFFFF;
+ giRandomState = (RANDOM_A*state + RANDOM_C);
// Check if it has changed, and if it hasn't, change it
- if(giRandomState == old) giRandomState += RANDOM_SPRUCE;
- return giRandomState;
+ if(state == old) state += RANDOM_SPRUCE;
+ return state;
+ #else
+ // http://en.wikipedia.org/wiki/Xorshift
+ // 2010-10-03
+ static Uint32 x = 123456789;
+ static Uint32 y = 362436069;
+ static Uint32 z = 521288629;
+ static Uint32 w = 88675123;
+ Uint32 t;
+
+ t = x ^ (x << 11);
+ x = y; y = z; z = w;
+ return w = w ^ (w >> 19) ^ t ^ (t >> 8);
+ #endif
}
/* *
strncpy(Dest, Value, 32);
return 1;
}
+
+/**
+ * \brief Convert a string of hexadecimal digits into a byte stream
+ */
+int UnHex(Uint8 *Dest, size_t DestSize, const char *SourceString)
+{
+ int i;
+
+ for( i = 0; i < DestSize*2; i += 2 )
+ {
+ Uint8 val = 0;
+
+ if(SourceString[i] == '\0') break;
+
+ if('0' <= SourceString[i] && SourceString[i] <= '9')
+ val |= (SourceString[i]-'0') << 4;
+ else if('A' <= SourceString[i] && SourceString[i] <= 'F')
+ val |= (SourceString[i]-'A'+10) << 4;
+ else if('a' <= SourceString[i] && SourceString[i] <= 'f')
+ val |= (SourceString[i]-'a'+10) << 4;
+
+ if(SourceString[i+1] == '\0') break;
+
+ if('0' <= SourceString[i+1] && SourceString[i+1] <= '9')
+ val |= (SourceString[i+1] - '0');
+ else if('A' <= SourceString[i+1] && SourceString[i+1] <= 'F')
+ val |= (SourceString[i+1] - 'A' + 10);
+ else if('a' <= SourceString[i+1] && SourceString[i+1] <= 'f')
+ val |= (SourceString[i+1] - 'a' + 10);
+
+ Dest[i/2] = val;
+ }
+ return i/2;
+}