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
274 if( (isLongLong && val >> 63) || (!isLongLong && val >> 31) ) {
276 if( (Sint)val < 0 ) {
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*)(Uint)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*)(Uint)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);
467 * \brief Split a string using the passed character
468 * \return NULL terminated array of strings on the heap
469 * \param __str String to split
470 * \param __ch Character to split by
472 char **str_split(const char *__str, char __ch)
479 for( i = 0; __str[i]; i++ )
485 ret = malloc( sizeof(char*)*(len+1) + (i + 1) );
486 if( !ret ) return NULL;
489 start = (char *)&ret[len+1];
491 for( i = 0; __str[i]; i++ )
493 if(__str[i] == __ch) {
508 * \fn int DivUp(int num, int dem)
509 * \brief Divide two numbers, rounding up
510 * \param num Numerator
511 * \param dem Denominator
513 int DivUp(int num, int dem)
515 return (num+dem-1)/dem;
519 * \fn int strpos8(const char *str, Uint32 search)
520 * \brief Search a string for a UTF-8 character
522 int strpos8(const char *str, Uint32 Search)
526 for(pos=0;str[pos];pos++)
530 if(str[pos] == Search) return pos;
533 if(*(Uint8*)(str+pos) < 128) continue;
535 pos += ReadUTF8( (Uint8*)&str[pos], &val );
536 if(val == Search) return pos;
542 * \fn int ReadUTF8(Uint8 *str, Uint32 *Val)
543 * \brief Read a UTF-8 character from a string
545 int ReadUTF8(Uint8 *str, Uint32 *Val)
547 *Val = 0xFFFD; // Assume invalid character
550 if( !(*str & 0x80) ) {
555 // Middle of a sequence
556 if( (*str & 0xC0) == 0x80 ) {
561 if( (*str & 0xE0) == 0xC0 ) {
562 *Val = (*str & 0x1F) << 6; // Upper 6 Bits
564 if( (*str & 0xC0) != 0x80) return -1; // Validity check
565 *Val |= (*str & 0x3F); // Lower 6 Bits
570 if( (*str & 0xF0) == 0xE0 ) {
571 *Val = (*str & 0x0F) << 12; // Upper 4 Bits
573 if( (*str & 0xC0) != 0x80) return -1; // Validity check
574 *Val |= (*str & 0x3F) << 6; // Middle 6 Bits
576 if( (*str & 0xC0) != 0x80) return -1; // Validity check
577 *Val |= (*str & 0x3F); // Lower 6 Bits
582 if( (*str & 0xF1) == 0xF0 ) {
583 *Val = (*str & 0x07) << 18; // Upper 3 Bits
585 if( (*str & 0xC0) != 0x80) return -1; // Validity check
586 *Val |= (*str & 0x3F) << 12; // Middle-upper 6 Bits
588 if( (*str & 0xC0) != 0x80) return -1; // Validity check
589 *Val |= (*str & 0x3F) << 6; // Middle-lower 6 Bits
591 if( (*str & 0xC0) != 0x80) return -1; // Validity check
592 *Val |= (*str & 0x3F); // Lower 6 Bits
596 // UTF-8 Doesn't support more than four bytes
601 * \fn int WriteUTF8(Uint8 *str, Uint32 Val)
602 * \brief Write a UTF-8 character sequence to a string
604 int WriteUTF8(Uint8 *str, Uint32 Val)
614 *str = 0xC0 | (Val >> 6);
616 *str = 0x80 | (Val & 0x3F);
621 if( Val < 0x10000 ) {
622 *str = 0xE0 | (Val >> 12);
624 *str = 0x80 | ((Val >> 6) & 0x3F);
626 *str = 0x80 | (Val & 0x3F);
631 if( Val < 0x110000 ) {
632 *str = 0xF0 | (Val >> 18);
634 *str = 0x80 | ((Val >> 12) & 0x3F);
636 *str = 0x80 | ((Val >> 6) & 0x3F);
638 *str = 0x80 | (Val & 0x3F);
642 // UTF-8 Doesn't support more than four bytes
647 * \fn Uint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
648 * \brief Converts a date into an Acess Timestamp
650 Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
657 stamp += day*3600*24;
658 stamp += month*DAYS_BEFORE[month]*3600*24;
660 ((year&3) == 0 || year%100 != 0)
661 || (year%100 == 0 && ((year/100)&3) == 0)
662 ) && month > 1) // Leap year and after feb
665 stamp += ((365*4+1) * ((year-2000)&~3)) * 3600*24; // Foour Year Segments
666 stamp += ((year-2000)&3) * 365*3600*24; // Inside four year segment
674 * \brief Pseudo random number generator
675 * \note Unknown effectiveness (made up on the spot)
679 Uint old = giRandomState;
680 // Get the next state value
681 giRandomState = (RANDOM_A*giRandomState + RANDOM_C) & 0xFFFFFFFF;
682 // Check if it has changed, and if it hasn't, change it
683 if(giRandomState == old) giRandomState += RANDOM_SPRUCE;
684 return giRandomState;
687 /// \name Memory Validation
690 * \brief Checks if a string resides fully in valid memory
692 int CheckString(char *String)
695 if( MM_IsUser( (tVAddr)String ) )
700 if( !MM_IsUser( (tVAddr)String ) )
702 // Increment string pointer
707 else if( MM_GetPhysAddr( (tVAddr)String ) )
712 if( !MM_GetPhysAddr( (tVAddr)String ) )
714 // Increment string pointer
723 * \brief Check if a sized memory region is valid memory
725 int CheckMem(void *Mem, int NumBytes)
727 tVAddr addr = (tVAddr)Mem;
729 if( MM_IsUser( addr ) )
733 if( !MM_IsUser( addr ) )
739 else if( MM_GetPhysAddr( addr ) )
743 if( !MM_GetPhysAddr( addr ) )
754 * \brief Search a string array for \a Needle
755 * \note Helper function for eTplDrv_IOCtl::DRV_IOCTL_LOOKUP
757 int ModUtil_LookupString(char **Array, char *Needle)
760 if( !CheckString(Needle) ) return -1;
761 for( i = 0; Array[i]; i++ )
763 if(strcmp(Array[i], Needle) == 0) return i;
768 int ModUtil_SetIdent(char *Dest, char *Value)
770 if( !CheckMem(Dest, 32) ) return -1;
771 strncpy(Dest, Value, 32);