3 * Common Library Functions
8 #define RANDOM_SEED 0xACE55052
9 #define RANDOM_A 0x00731ADE
10 #define RANDOM_C 12345
11 #define RANDOM_SPRUCE 0xf12b039
12 // Jan Feb Mar Apr May Jun Jul Aug Sept Oct Nov Dec
13 const short DAYS_BEFORE[] = {0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
14 #define UNIX_TO_2K ((30*365*3600*24) + (7*3600*24)) //Normal years + leap years
17 int atoi(const char *string);
18 void itoa(char *buf, Uint num, int base, int minLength, char pad);
19 int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args);
20 int sprintf(char *__s, const char *__format, ...);
22 int strucmp(const char *Str1, const char *Str2);
23 int strpos(const char *Str, char Ch);
24 Uint8 ByteSum(void *Ptr, int Size);
25 size_t strlen(const char *__s);
26 char *strcpy(char *__str1, const char *__str2);
27 char *strncpy(char *__str1, const char *__str2, size_t max);
28 int strcmp(const char *str1, const char *str2);
29 int strncmp(const char *str1, const char *str2, size_t num);
30 char *strdup(const char *Str);
31 char **str_split(const char *__str, char __ch);
32 int strpos8(const char *str, Uint32 Search);
33 int ReadUTF8(Uint8 *str, Uint32 *Val);
34 int WriteUTF8(Uint8 *str, Uint32 Val);
35 int DivUp(int num, int dem);
36 Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year);
38 int CheckString(char *String);
39 int CheckMem(void *Mem, int NumBytes);
40 int ModUtil_LookupString(char **Array, char *Needle);
41 int ModUtil_SetIdent(char *Dest, char *Value);
66 EXPORT(ModUtil_LookupString);
67 EXPORT(ModUtil_SetIdent);
71 * \brief Convert a string into an integer
73 int atoi(const char *string)
78 //Log("atoi: (string='%s')", string);
80 // Clear non-numeric characters
81 while( !('0' <= *string && *string <= '9') && *string != '-' ) string++;
82 if( *string == '-' ) {
84 while( !('0' <= *string && *string <= '9') ) string++;
96 if('0' <= *string && *string <= '9') {
100 else if('A' <= *string && *string <= 'F') {
102 ret += *string - 'A' + 10;
104 else if('a' <= *string && *string <= 'f') {
106 ret += *string - 'a' + 10;
114 for( ; '0' <= *string && *string <= '7'; string ++ )
117 ret += *string - '0';
123 for( ; '0' <= *string && *string <= '9'; string++)
126 ret += *string - '0';
132 //Log("atoi: RETURN %i", ret);
137 static const char cUCDIGITS[] = "0123456789ABCDEF";
139 * \fn void itoa(char *buf, Uint num, int base, int minLength, char pad)
140 * \brief Convert an integer into a character string
142 void itoa(char *buf, Uint num, int base, int minLength, char pad)
151 if(base > 16 || base < 2) {
157 while(num > base-1) {
158 tmpBuf[pos] = cUCDIGITS[ num % base ];
159 num /= (Uint)base; // Shift `num` right 1 digit
162 tmpBuf[pos++] = cUCDIGITS[ num % base ]; // Last digit of `num`
167 while(minLength-- > 0) buf[i++] = pad;
168 while(pos-- > 0) buf[i++] = tmpBuf[pos]; // Reverse the order of characters
173 * \brief Append a character the the vsnprintf output
175 #define PUTCH(c) do{\
177 if(pos==__maxlen){return pos;}\
178 if(__s){__s[pos++]=ch;}else{pos++;}\
181 * \brief VArg String Number Print Formatted
183 int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args)
186 int minSize = 0, len;
187 char tmpBuf[34]; // For Integers
195 //Log("vsnprintf: (__s=%p, __maxlen=%i, __format='%s', ...)", __s, __maxlen, __format);
197 while((c = *__format++) != 0)
199 // Non control character
200 if(c != '%') { PUTCH(c); continue; }
203 //Log("pos = %i", pos);
206 if(c == '%') { PUTCH('%'); continue; }
208 // Pointer - Done first for debugging
210 Uint ptr = va_arg(args, Uint);
211 PUTCH('*'); PUTCH('0'); PUTCH('x');
213 itoa(p, ptr, 16, BITS/4, '0');
218 val = va_arg(args, unsigned int);
220 // - Padding Side Flag
235 if(c == '*') { // Dynamic length
237 val = va_arg(args, unsigned int);
240 else if('1' <= c && c <= '9')
243 while('0' <= c && c <= '9')
253 // - Default, Long or LongLong?
255 if(c == 'l') // Long is actually the default on x86
260 val |= (Uint64)va_arg(args, Uint) << 32;
267 // - Now get the format code
273 if( isLongLong && val >> 63 ) {
277 else if( !isLongLong && val >> 31 ) {
281 itoa(p, val, 10, minSize, pad);
284 itoa(p, val, 10, minSize, pad);
287 itoa(p, val, 16, minSize, pad);
290 itoa(p, val, 8, minSize, pad);
293 itoa(p, val, 2, minSize, pad);
301 // String - Null Terminated Array
303 p = (char*)(tVAddr)val;
307 if( !bPadLeft ) while(len++ < minSize) PUTCH(pad);
308 while(*p) PUTCH(*p++);
309 if( bPadLeft ) while(len++ < minSize) PUTCH(pad);
312 case 'C': // Non-Null Terminated Character Array
313 p = (char*)(tVAddr)val;
314 if(!p) goto printString;
315 while(minSize--) PUTCH(*p++);
326 if(__s && pos != __maxlen)
335 int sprintf(char *__s, const char *__format, ...)
340 va_start(args, __format);
341 ret = vsnprintf(__s, -1, __format, args);
348 * \fn int tolower(int c)
349 * \brief Converts a character to lower case
353 if('A' <= c && c <= 'Z')
354 return c - 'A' + 'a';
359 * \fn int strucmp(const char *Str1, const char *Str2)
360 * \brief Compare \a Str1 and \a Str2 case-insensitively
362 int strucmp(const char *Str1, const char *Str2)
364 while(*Str1 && tolower(*Str1) == tolower(*Str2))
366 return tolower(*Str1) - tolower(*Str2);
370 * \fn int strpos(const char *Str, char Ch)
371 * \brief Search a string for an ascii character
373 int strpos(const char *Str, char Ch)
376 for(pos=0;Str[pos];pos++)
378 if(Str[pos] == Ch) return pos;
384 * \fn Uint8 ByteSum(void *Ptr, int Size)
385 * \brief Adds the bytes in a memory region and returns the sum
387 Uint8 ByteSum(void *Ptr, int Size)
390 while(Size--) sum += *(Uint8*)Ptr++;
395 * \fn Uint strlen(const char *__str)
396 * \brief Get the length of string
398 Uint strlen(const char *__str)
401 while(*__str++) ret++;
406 * \fn char *strcpy(char *__str1, const char *__str2)
407 * \brief Copy a string to a new location
409 char *strcpy(char *__str1, const char *__str2)
412 *__str1++ = *__str2++;
413 *__str1 = '\0'; // Terminate String
418 * \fn char *strncpy(char *__str1, const char *__str2, size_t max)
419 * \brief Copy a string to a new location
421 char *strncpy(char *__str1, const char *__str2, size_t max)
423 while(*__str2 && max-- >= 1)
424 *__str1++ = *__str2++;
426 *__str1 = '\0'; // Terminate String
431 * \fn int strcmp(const char *str1, const char *str2)
432 * \brief Compare two strings return the difference between
433 * the first non-matching characters.
435 int strcmp(const char *str1, const char *str2)
437 while(*str1 && *str1 == *str2)
439 return *str1 - *str2;
443 * \fn int strncmp(const char *Str1, const char *Str2, size_t num)
444 * \brief Compare strings \a Str1 and \a Str2 to a maximum of \a num characters
446 int strncmp(const char *Str1, const char *Str2, size_t num)
448 if(num == 0) return 0; // TODO: Check what should officially happen here
449 while(--num && *Str1 && *Str1 == *Str2)
455 * \fn char *strdup(const char *Str)
456 * \brief Duplicates a string
458 char *strdup(const char *Str)
461 ret = malloc(strlen(Str)+1);
462 if( !ret ) return NULL;
468 * \brief Split a string using the passed character
469 * \return NULL terminated array of strings on the heap
470 * \param __str String to split
471 * \param __ch Character to split by
473 char **str_split(const char *__str, char __ch)
480 for( i = 0; __str[i]; i++ )
486 ret = malloc( sizeof(char*)*(len+1) + (i + 1) );
487 if( !ret ) return NULL;
490 start = (char *)&ret[len+1];
492 for( i = 0; __str[i]; i++ )
494 if(__str[i] == __ch) {
509 * \fn int DivUp(int num, int dem)
510 * \brief Divide two numbers, rounding up
511 * \param num Numerator
512 * \param dem Denominator
514 int DivUp(int num, int dem)
516 return (num+dem-1)/dem;
520 * \fn int strpos8(const char *str, Uint32 search)
521 * \brief Search a string for a UTF-8 character
523 int strpos8(const char *str, Uint32 Search)
527 for(pos=0;str[pos];pos++)
531 if(str[pos] == Search) return pos;
534 if(*(Uint8*)(str+pos) < 128) continue;
536 pos += ReadUTF8( (Uint8*)&str[pos], &val );
537 if(val == Search) return pos;
543 * \fn int ReadUTF8(Uint8 *str, Uint32 *Val)
544 * \brief Read a UTF-8 character from a string
546 int ReadUTF8(Uint8 *str, Uint32 *Val)
548 *Val = 0xFFFD; // Assume invalid character
551 if( !(*str & 0x80) ) {
556 // Middle of a sequence
557 if( (*str & 0xC0) == 0x80 ) {
562 if( (*str & 0xE0) == 0xC0 ) {
563 *Val = (*str & 0x1F) << 6; // Upper 6 Bits
565 if( (*str & 0xC0) != 0x80) return -1; // Validity check
566 *Val |= (*str & 0x3F); // Lower 6 Bits
571 if( (*str & 0xF0) == 0xE0 ) {
572 *Val = (*str & 0x0F) << 12; // Upper 4 Bits
574 if( (*str & 0xC0) != 0x80) return -1; // Validity check
575 *Val |= (*str & 0x3F) << 6; // Middle 6 Bits
577 if( (*str & 0xC0) != 0x80) return -1; // Validity check
578 *Val |= (*str & 0x3F); // Lower 6 Bits
583 if( (*str & 0xF1) == 0xF0 ) {
584 *Val = (*str & 0x07) << 18; // Upper 3 Bits
586 if( (*str & 0xC0) != 0x80) return -1; // Validity check
587 *Val |= (*str & 0x3F) << 12; // Middle-upper 6 Bits
589 if( (*str & 0xC0) != 0x80) return -1; // Validity check
590 *Val |= (*str & 0x3F) << 6; // Middle-lower 6 Bits
592 if( (*str & 0xC0) != 0x80) return -1; // Validity check
593 *Val |= (*str & 0x3F); // Lower 6 Bits
597 // UTF-8 Doesn't support more than four bytes
602 * \fn int WriteUTF8(Uint8 *str, Uint32 Val)
603 * \brief Write a UTF-8 character sequence to a string
605 int WriteUTF8(Uint8 *str, Uint32 Val)
615 *str = 0xC0 | (Val >> 6);
617 *str = 0x80 | (Val & 0x3F);
622 if( Val < 0x10000 ) {
623 *str = 0xE0 | (Val >> 12);
625 *str = 0x80 | ((Val >> 6) & 0x3F);
627 *str = 0x80 | (Val & 0x3F);
632 if( Val < 0x110000 ) {
633 *str = 0xF0 | (Val >> 18);
635 *str = 0x80 | ((Val >> 12) & 0x3F);
637 *str = 0x80 | ((Val >> 6) & 0x3F);
639 *str = 0x80 | (Val & 0x3F);
643 // UTF-8 Doesn't support more than four bytes
648 * \fn Uint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
649 * \brief Converts a date into an Acess Timestamp
651 Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
658 stamp += day*3600*24;
659 stamp += month*DAYS_BEFORE[month]*3600*24;
661 ((year&3) == 0 || year%100 != 0)
662 || (year%100 == 0 && ((year/100)&3) == 0)
663 ) && month > 1) // Leap year and after feb
666 stamp += ((365*4+1) * ((year-2000)&~3)) * 3600*24; // Four Year Segments
667 stamp += ((year-2000)&3) * 365*3600*24; // Inside four year segment
675 * \brief Pseudo random number generator
676 * \note Unknown effectiveness (made up on the spot)
681 static Uint state = RANDOM_SEED;
683 // Get the next state value
684 giRandomState = (RANDOM_A*state + RANDOM_C);
685 // Check if it has changed, and if it hasn't, change it
686 if(state == old) state += RANDOM_SPRUCE;
689 // http://en.wikipedia.org/wiki/Xorshift
691 static Uint32 x = 123456789;
692 static Uint32 y = 362436069;
693 static Uint32 z = 521288629;
694 static Uint32 w = 88675123;
699 return w = w ^ (w >> 19) ^ t ^ (t >> 8);
704 * \name Memory Validation
708 * \brief Checks if a string resides fully in valid memory
710 int CheckString(char *String)
712 if( !MM_GetPhysAddr( (tVAddr)String ) )
716 if( MM_IsUser( (tVAddr)String ) )
721 if( !MM_IsUser( (tVAddr)String ) )
723 // Increment string pointer
728 else if( MM_GetPhysAddr( (tVAddr)String ) )
733 if( !MM_GetPhysAddr( (tVAddr)String ) )
735 // Increment string pointer
744 * \brief Check if a sized memory region is valid memory
746 int CheckMem(void *Mem, int NumBytes)
748 tVAddr addr = (tVAddr)Mem;
750 if( MM_IsUser( addr ) )
754 if( !MM_IsUser( addr ) )
760 else if( MM_GetPhysAddr( addr ) )
764 if( !MM_GetPhysAddr( addr ) )
777 * \brief Search a string array for \a Needle
778 * \note Helper function for eTplDrv_IOCtl::DRV_IOCTL_LOOKUP
780 int ModUtil_LookupString(char **Array, char *Needle)
783 if( !CheckString(Needle) ) return -1;
784 for( i = 0; Array[i]; i++ )
786 if(strcmp(Array[i], Needle) == 0) return i;
791 int ModUtil_SetIdent(char *Dest, char *Value)
793 if( !CheckMem(Dest, 32) ) return -1;
794 strncpy(Dest, Value, 32);