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++;}\
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, Uint);
220 // - Padding Side Flag
237 val = va_arg(args, Uint);
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) || (!isLongLong && val >> 31) ) {
277 itoa(p, val, 10, minSize, pad);
280 itoa(p, val, 10, minSize, pad);
283 itoa(p, val, 16, minSize, pad);
286 itoa(p, val, 8, minSize, pad);
289 itoa(p, val, 2, minSize, pad);
297 // String - Null Terminated Array
299 p = (char*)(Uint)val;
303 if( !bPadLeft ) while(len++ < minSize) PUTCH(pad);
304 while(*p) PUTCH(*p++);
305 if( bPadLeft ) while(len++ < minSize) PUTCH(pad);
308 case 'C': // Non-Null Terminated Character Array
309 p = (char*)(Uint)val;
310 if(!p) goto printString;
311 //while(minSize--) PUTCH(*p++);
312 while(minSize--) PUTCH(*p++);
323 if(__s && pos != __maxlen)
332 int sprintf(char *__s, const char *__format, ...)
337 va_start(args, __format);
338 ret = vsnprintf(__s, -1, __format, args);
345 * \fn int tolower(int c)
346 * \brief Converts a character to lower case
350 if('A' <= c && c <= 'Z')
351 return c - 'A' + 'a';
356 * \fn int strucmp(const char *Str1, const char *Str2)
357 * \brief Compare \a Str1 and \a Str2 case-insensitively
359 int strucmp(const char *Str1, const char *Str2)
361 while(*Str1 && tolower(*Str1) == tolower(*Str2))
363 return tolower(*Str1) - tolower(*Str2);
367 * \fn int strpos(const char *Str, char Ch)
368 * \brief Search a string for an ascii character
370 int strpos(const char *Str, char Ch)
373 for(pos=0;Str[pos];pos++)
375 if(Str[pos] == Ch) return pos;
381 * \fn Uint8 ByteSum(void *Ptr, int Size)
382 * \brief Adds the bytes in a memory region and returns the sum
384 Uint8 ByteSum(void *Ptr, int Size)
387 while(Size--) sum += *(Uint8*)Ptr++;
392 * \fn Uint strlen(const char *__str)
393 * \brief Get the length of string
395 Uint strlen(const char *__str)
398 while(*__str++) ret++;
403 * \fn char *strcpy(char *__str1, const char *__str2)
404 * \brief Copy a string to a new location
406 char *strcpy(char *__str1, const char *__str2)
409 *__str1++ = *__str2++;
410 *__str1 = '\0'; // Terminate String
415 * \fn char *strncpy(char *__str1, const char *__str2, size_t max)
416 * \brief Copy a string to a new location
418 char *strncpy(char *__str1, const char *__str2, size_t max)
420 while(*__str2 && max-- >= 1)
421 *__str1++ = *__str2++;
423 *__str1 = '\0'; // Terminate String
428 * \fn int strcmp(const char *str1, const char *str2)
429 * \brief Compare two strings return the difference between
430 * the first non-matching characters.
432 int strcmp(const char *str1, const char *str2)
434 while(*str1 && *str1 == *str2)
436 return *str1 - *str2;
440 * \fn int strncmp(const char *Str1, const char *Str2, size_t num)
441 * \brief Compare strings \a Str1 and \a Str2 to a maximum of \a num characters
443 int strncmp(const char *Str1, const char *Str2, size_t num)
445 if(num == 0) return 0; // TODO: Check what should officially happen here
446 while(--num && *Str1 && *Str1 == *Str2)
452 * \fn char *strdup(const char *Str)
453 * \brief Duplicates a string
455 char *strdup(const char *Str)
458 ret = malloc(strlen(Str)+1);
464 * \brief Split a string using the passed character
465 * \return NULL terminated array of strings on the heap
466 * \param __str String to split
467 * \param __ch Character to split by
469 char **str_split(const char *__str, char __ch)
476 for( i = 0; __str[i]; i++ )
482 ret = malloc( sizeof(char*)*(len+1) + (i + 1) );
483 if( !ret ) return NULL;
486 start = (char *)&ret[len+1];
488 for( i = 0; __str[i]; i++ )
490 if(__str[i] == __ch) {
505 * \fn int DivUp(int num, int dem)
506 * \brief Divide two numbers, rounding up
507 * \param num Numerator
508 * \param dem Denominator
510 int DivUp(int num, int dem)
512 return (num+dem-1)/dem;
516 * \fn int strpos8(const char *str, Uint32 search)
517 * \brief Search a string for a UTF-8 character
519 int strpos8(const char *str, Uint32 Search)
523 for(pos=0;str[pos];pos++)
527 if(str[pos] == Search) return pos;
530 if(*(Uint8*)(str+pos) < 128) continue;
532 pos += ReadUTF8( (Uint8*)&str[pos], &val );
533 if(val == Search) return pos;
539 * \fn int ReadUTF8(Uint8 *str, Uint32 *Val)
540 * \brief Read a UTF-8 character from a string
542 int ReadUTF8(Uint8 *str, Uint32 *Val)
544 *Val = 0xFFFD; // Assume invalid character
547 if( !(*str & 0x80) ) {
552 // Middle of a sequence
553 if( (*str & 0xC0) == 0x80 ) {
558 if( (*str & 0xE0) == 0xC0 ) {
559 *Val = (*str & 0x1F) << 6; // Upper 6 Bits
561 if( (*str & 0xC0) != 0x80) return -1; // Validity check
562 *Val |= (*str & 0x3F); // Lower 6 Bits
567 if( (*str & 0xF0) == 0xE0 ) {
568 *Val = (*str & 0x0F) << 12; // Upper 4 Bits
570 if( (*str & 0xC0) != 0x80) return -1; // Validity check
571 *Val |= (*str & 0x3F) << 6; // Middle 6 Bits
573 if( (*str & 0xC0) != 0x80) return -1; // Validity check
574 *Val |= (*str & 0x3F); // Lower 6 Bits
579 if( (*str & 0xF1) == 0xF0 ) {
580 *Val = (*str & 0x07) << 18; // Upper 3 Bits
582 if( (*str & 0xC0) != 0x80) return -1; // Validity check
583 *Val |= (*str & 0x3F) << 12; // Middle-upper 6 Bits
585 if( (*str & 0xC0) != 0x80) return -1; // Validity check
586 *Val |= (*str & 0x3F) << 6; // Middle-lower 6 Bits
588 if( (*str & 0xC0) != 0x80) return -1; // Validity check
589 *Val |= (*str & 0x3F); // Lower 6 Bits
593 // UTF-8 Doesn't support more than four bytes
598 * \fn int WriteUTF8(Uint8 *str, Uint32 Val)
599 * \brief Write a UTF-8 character sequence to a string
601 int WriteUTF8(Uint8 *str, Uint32 Val)
611 *str = 0xC0 | (Val >> 6);
613 *str = 0x80 | (Val & 0x3F);
618 if( Val < 0x10000 ) {
619 *str = 0xE0 | (Val >> 12);
621 *str = 0x80 | ((Val >> 6) & 0x3F);
623 *str = 0x80 | (Val & 0x3F);
628 if( Val < 0x110000 ) {
629 *str = 0xF0 | (Val >> 18);
631 *str = 0x80 | ((Val >> 12) & 0x3F);
633 *str = 0x80 | ((Val >> 6) & 0x3F);
635 *str = 0x80 | (Val & 0x3F);
639 // UTF-8 Doesn't support more than four bytes
644 * \fn Uint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
645 * \brief Converts a date into an Acess Timestamp
647 Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
654 stamp += day*3600*24;
655 stamp += month*DAYS_BEFORE[month]*3600*24;
657 ((year&3) == 0 || year%100 != 0)
658 || (year%100 == 0 && ((year/100)&3) == 0)
659 ) && month > 1) // Leap year and after feb
662 stamp += ((365*4+1) * ((year-2000)&~3)) * 3600*24; // Foour Year Segments
663 stamp += ((year-2000)&3) * 365*3600*24; // Inside four year segment
671 * \brief Pseudo random number generator
672 * \note Unknown effectiveness (made up on the spot)
676 Uint old = giRandomState;
677 // Get the next state value
678 giRandomState = (RANDOM_A*giRandomState + RANDOM_C) & 0xFFFFFFFF;
679 // Check if it has changed, and if it hasn't, change it
680 if(giRandomState == old) giRandomState += RANDOM_SPRUCE;
681 return giRandomState;
684 /// \name Memory Validation
687 * \brief Checks if a string resides fully in valid memory
689 int CheckString(char *String)
692 if( MM_IsUser( (tVAddr)String ) )
697 if( !MM_IsUser( (tVAddr)String ) )
699 // Increment string pointer
704 else if( MM_GetPhysAddr( (tVAddr)String ) )
709 if( !MM_GetPhysAddr( (tVAddr)String ) )
711 // Increment string pointer
720 * \brief Check if a sized memory region is valid memory
722 int CheckMem(void *Mem, int NumBytes)
724 tVAddr addr = (tVAddr)Mem;
726 if( MM_IsUser( addr ) )
730 if( !MM_IsUser( addr ) )
736 else if( MM_GetPhysAddr( addr ) )
740 if( !MM_GetPhysAddr( addr ) )
751 * \brief Search a string array for \a Needle
752 * \note Helper function for eTplDrv_IOCtl::DRV_IOCTL_LOOKUP
754 int ModUtil_LookupString(char **Array, char *Needle)
757 if( !CheckString(Needle) ) return -1;
758 for( i = 0; Array[i]; i++ )
760 if(strcmp(Array[i], Needle) == 0) return i;
765 int ModUtil_SetIdent(char *Dest, char *Value)
767 if( !CheckMem(Dest, 32) ) return -1;
768 strncpy(Dest, Value, 32);