3 * Common Library Functions
8 #define RANDOM_SEED 0xACE55052
9 #define RANDOM_A 0x00731ADE
10 #define RANDOM_C 12345
11 #define RANDOM_SPRUCE 0xf12b02b
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);
70 static Uint giRandomState = RANDOM_SEED;
74 * \brief Convert a string into an integer
76 int atoi(const char *string)
81 //Log("atoi: (string='%s')", string);
83 // Clear non-numeric characters
84 while( !('0' <= *string && *string <= '9') && *string != '-' ) string++;
85 if( *string == '-' ) {
87 while( !('0' <= *string && *string <= '9') ) string++;
99 if('0' <= *string && *string <= '9') {
101 ret += *string - '0';
103 else if('A' <= *string && *string <= 'F') {
105 ret += *string - 'A' + 10;
107 else if('a' <= *string && *string <= 'f') {
109 ret += *string - 'a' + 10;
117 for( ; '0' <= *string && *string <= '7'; string ++ )
120 ret += *string - '0';
126 for( ; '0' <= *string && *string <= '9'; string++)
129 ret += *string - '0';
135 //Log("atoi: RETURN %i", ret);
140 static const char cUCDIGITS[] = "0123456789ABCDEF";
142 * \fn void itoa(char *buf, Uint num, int base, int minLength, char pad)
143 * \brief Convert an integer into a character string
145 void itoa(char *buf, Uint num, int base, int minLength, char pad)
154 if(base > 16 || base < 2) {
160 while(num > base-1) {
161 tmpBuf[pos] = cUCDIGITS[ num % base ];
162 num /= (Uint)base; // Shift `num` right 1 digit
165 tmpBuf[pos++] = cUCDIGITS[ num % base ]; // Last digit of `num`
170 while(minLength-- > 0) buf[i++] = pad;
171 while(pos-- > 0) buf[i++] = tmpBuf[pos]; // Reverse the order of characters
176 * \brief Append a character the the vsnprintf output
178 #define PUTCH(c) do{\
180 if(pos==__maxlen){return pos;}\
181 if(__s){__s[pos++]=ch;}else{pos++;}\
184 * \brief VArg String Number Print Formatted
186 int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args)
189 int minSize = 0, len;
190 char tmpBuf[34]; // For Integers
198 //Log("vsnprintf: (__s=%p, __maxlen=%i, __format='%s', ...)", __s, __maxlen, __format);
200 while((c = *__format++) != 0)
202 // Non control character
203 if(c != '%') { PUTCH(c); continue; }
206 //Log("pos = %i", pos);
209 if(c == '%') { PUTCH('%'); continue; }
211 // Pointer - Done first for debugging
213 Uint ptr = va_arg(args, Uint);
214 PUTCH('*'); PUTCH('0'); PUTCH('x');
216 itoa(p, ptr, 16, BITS/4, '0');
221 val = va_arg(args, Uint);
223 // - Padding Side Flag
238 if(c == '*') { // Dynamic length
240 val = va_arg(args, Uint);
243 else if('1' <= c && c <= '9')
246 while('0' <= c && c <= '9')
256 // - Default, Long or LongLong?
258 if(c == 'l') // Long is actually the default on x86
263 val |= (Uint64)va_arg(args, Uint) << 32;
270 // - Now get the format code
276 if( isLongLong && val >> 63 ) {
280 else if( !isLongLong && val >> 31 ) {
284 itoa(p, val, 10, minSize, pad);
287 itoa(p, val, 10, minSize, pad);
290 itoa(p, val, 16, minSize, pad);
293 itoa(p, val, 8, minSize, pad);
296 itoa(p, val, 2, minSize, pad);
304 // String - Null Terminated Array
306 p = (char*)(tVAddr)val;
310 if( !bPadLeft ) while(len++ < minSize) PUTCH(pad);
311 while(*p) PUTCH(*p++);
312 if( bPadLeft ) while(len++ < minSize) PUTCH(pad);
315 case 'C': // Non-Null Terminated Character Array
316 p = (char*)(tVAddr)val;
317 if(!p) goto printString;
318 while(minSize--) PUTCH(*p++);
329 if(__s && pos != __maxlen)
338 int sprintf(char *__s, const char *__format, ...)
343 va_start(args, __format);
344 ret = vsnprintf(__s, -1, __format, args);
351 * \fn int tolower(int c)
352 * \brief Converts a character to lower case
356 if('A' <= c && c <= 'Z')
357 return c - 'A' + 'a';
362 * \fn int strucmp(const char *Str1, const char *Str2)
363 * \brief Compare \a Str1 and \a Str2 case-insensitively
365 int strucmp(const char *Str1, const char *Str2)
367 while(*Str1 && tolower(*Str1) == tolower(*Str2))
369 return tolower(*Str1) - tolower(*Str2);
373 * \fn int strpos(const char *Str, char Ch)
374 * \brief Search a string for an ascii character
376 int strpos(const char *Str, char Ch)
379 for(pos=0;Str[pos];pos++)
381 if(Str[pos] == Ch) return pos;
387 * \fn Uint8 ByteSum(void *Ptr, int Size)
388 * \brief Adds the bytes in a memory region and returns the sum
390 Uint8 ByteSum(void *Ptr, int Size)
393 while(Size--) sum += *(Uint8*)Ptr++;
398 * \fn Uint strlen(const char *__str)
399 * \brief Get the length of string
401 Uint strlen(const char *__str)
404 while(*__str++) ret++;
409 * \fn char *strcpy(char *__str1, const char *__str2)
410 * \brief Copy a string to a new location
412 char *strcpy(char *__str1, const char *__str2)
415 *__str1++ = *__str2++;
416 *__str1 = '\0'; // Terminate String
421 * \fn char *strncpy(char *__str1, const char *__str2, size_t max)
422 * \brief Copy a string to a new location
424 char *strncpy(char *__str1, const char *__str2, size_t max)
426 while(*__str2 && max-- >= 1)
427 *__str1++ = *__str2++;
429 *__str1 = '\0'; // Terminate String
434 * \fn int strcmp(const char *str1, const char *str2)
435 * \brief Compare two strings return the difference between
436 * the first non-matching characters.
438 int strcmp(const char *str1, const char *str2)
440 while(*str1 && *str1 == *str2)
442 return *str1 - *str2;
446 * \fn int strncmp(const char *Str1, const char *Str2, size_t num)
447 * \brief Compare strings \a Str1 and \a Str2 to a maximum of \a num characters
449 int strncmp(const char *Str1, const char *Str2, size_t num)
451 if(num == 0) return 0; // TODO: Check what should officially happen here
452 while(--num && *Str1 && *Str1 == *Str2)
458 * \fn char *strdup(const char *Str)
459 * \brief Duplicates a string
461 char *strdup(const char *Str)
464 ret = malloc(strlen(Str)+1);
470 * \brief Split a string using the passed character
471 * \return NULL terminated array of strings on the heap
472 * \param __str String to split
473 * \param __ch Character to split by
475 char **str_split(const char *__str, char __ch)
482 for( i = 0; __str[i]; i++ )
488 ret = malloc( sizeof(char*)*(len+1) + (i + 1) );
489 if( !ret ) return NULL;
492 start = (char *)&ret[len+1];
494 for( i = 0; __str[i]; i++ )
496 if(__str[i] == __ch) {
511 * \fn int DivUp(int num, int dem)
512 * \brief Divide two numbers, rounding up
513 * \param num Numerator
514 * \param dem Denominator
516 int DivUp(int num, int dem)
518 return (num+dem-1)/dem;
522 * \fn int strpos8(const char *str, Uint32 search)
523 * \brief Search a string for a UTF-8 character
525 int strpos8(const char *str, Uint32 Search)
529 for(pos=0;str[pos];pos++)
533 if(str[pos] == Search) return pos;
536 if(*(Uint8*)(str+pos) < 128) continue;
538 pos += ReadUTF8( (Uint8*)&str[pos], &val );
539 if(val == Search) return pos;
545 * \fn int ReadUTF8(Uint8 *str, Uint32 *Val)
546 * \brief Read a UTF-8 character from a string
548 int ReadUTF8(Uint8 *str, Uint32 *Val)
550 *Val = 0xFFFD; // Assume invalid character
553 if( !(*str & 0x80) ) {
558 // Middle of a sequence
559 if( (*str & 0xC0) == 0x80 ) {
564 if( (*str & 0xE0) == 0xC0 ) {
565 *Val = (*str & 0x1F) << 6; // Upper 6 Bits
567 if( (*str & 0xC0) != 0x80) return -1; // Validity check
568 *Val |= (*str & 0x3F); // Lower 6 Bits
573 if( (*str & 0xF0) == 0xE0 ) {
574 *Val = (*str & 0x0F) << 12; // Upper 4 Bits
576 if( (*str & 0xC0) != 0x80) return -1; // Validity check
577 *Val |= (*str & 0x3F) << 6; // Middle 6 Bits
579 if( (*str & 0xC0) != 0x80) return -1; // Validity check
580 *Val |= (*str & 0x3F); // Lower 6 Bits
585 if( (*str & 0xF1) == 0xF0 ) {
586 *Val = (*str & 0x07) << 18; // Upper 3 Bits
588 if( (*str & 0xC0) != 0x80) return -1; // Validity check
589 *Val |= (*str & 0x3F) << 12; // Middle-upper 6 Bits
591 if( (*str & 0xC0) != 0x80) return -1; // Validity check
592 *Val |= (*str & 0x3F) << 6; // Middle-lower 6 Bits
594 if( (*str & 0xC0) != 0x80) return -1; // Validity check
595 *Val |= (*str & 0x3F); // Lower 6 Bits
599 // UTF-8 Doesn't support more than four bytes
604 * \fn int WriteUTF8(Uint8 *str, Uint32 Val)
605 * \brief Write a UTF-8 character sequence to a string
607 int WriteUTF8(Uint8 *str, Uint32 Val)
617 *str = 0xC0 | (Val >> 6);
619 *str = 0x80 | (Val & 0x3F);
624 if( Val < 0x10000 ) {
625 *str = 0xE0 | (Val >> 12);
627 *str = 0x80 | ((Val >> 6) & 0x3F);
629 *str = 0x80 | (Val & 0x3F);
634 if( Val < 0x110000 ) {
635 *str = 0xF0 | (Val >> 18);
637 *str = 0x80 | ((Val >> 12) & 0x3F);
639 *str = 0x80 | ((Val >> 6) & 0x3F);
641 *str = 0x80 | (Val & 0x3F);
645 // UTF-8 Doesn't support more than four bytes
650 * \fn Uint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
651 * \brief Converts a date into an Acess Timestamp
653 Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
660 stamp += day*3600*24;
661 stamp += month*DAYS_BEFORE[month]*3600*24;
663 ((year&3) == 0 || year%100 != 0)
664 || (year%100 == 0 && ((year/100)&3) == 0)
665 ) && month > 1) // Leap year and after feb
668 stamp += ((365*4+1) * ((year-2000)&~3)) * 3600*24; // Four Year Segments
669 stamp += ((year-2000)&3) * 365*3600*24; // Inside four year segment
677 * \brief Pseudo random number generator
678 * \note Unknown effectiveness (made up on the spot)
682 Uint old = giRandomState;
683 // Get the next state value
684 giRandomState = (RANDOM_A*giRandomState + RANDOM_C) & 0xFFFFFFFF;
685 // Check if it has changed, and if it hasn't, change it
686 if(giRandomState == old) giRandomState += RANDOM_SPRUCE;
687 return giRandomState;
690 /// \name Memory Validation
693 * \brief Checks if a string resides fully in valid memory
695 int CheckString(char *String)
697 if( !MM_GetPhysAddr( (tVAddr)String ) )
701 if( MM_IsUser( (tVAddr)String ) )
706 if( !MM_IsUser( (tVAddr)String ) )
708 // Increment string pointer
713 else if( MM_GetPhysAddr( (tVAddr)String ) )
718 if( !MM_GetPhysAddr( (tVAddr)String ) )
720 // Increment string pointer
729 * \brief Check if a sized memory region is valid memory
731 int CheckMem(void *Mem, int NumBytes)
733 tVAddr addr = (tVAddr)Mem;
735 if( MM_IsUser( addr ) )
739 if( !MM_IsUser( addr ) )
745 else if( MM_GetPhysAddr( addr ) )
749 if( !MM_GetPhysAddr( addr ) )
760 * \brief Search a string array for \a Needle
761 * \note Helper function for eTplDrv_IOCtl::DRV_IOCTL_LOOKUP
763 int ModUtil_LookupString(char **Array, char *Needle)
766 if( !CheckString(Needle) ) return -1;
767 for( i = 0; Array[i]; i++ )
769 if(strcmp(Array[i], Needle) == 0) return i;
774 int ModUtil_SetIdent(char *Dest, char *Value)
776 if( !CheckMem(Dest, 32) ) return -1;
777 strncpy(Dest, Value, 32);