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 int DivUp(int num, int dem);
32 int strpos8(const char *str, Uint32 Search);
33 int ReadUTF8(Uint8 *str, Uint32 *Val);
34 int WriteUTF8(Uint8 *str, Uint32 Val);
35 Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year);
37 int CheckString(char *String);
38 int CheckMem(void *Mem, int NumBytes);
39 int ModUtil_LookupString(char **Array, char *Needle);
40 int ModUtil_SetIdent(char *Dest, char *Value);
64 EXPORT(ModUtil_LookupString);
65 EXPORT(ModUtil_SetIdent);
68 static Uint giRandomState = RANDOM_SEED;
72 * \brief Convert a string into an integer
74 int atoi(const char *string)
78 // Clear non-numeric characters
79 while( !('0' <= *string && *string <= '9') ) string++;
90 if('0' <= *string && *string <= '9')
92 else if('A' <= *string && *string <= 'F')
93 ret += *string - 'A' + 10;
94 else if('a' <= *string && *string <= 'f')
95 ret += *string - 'a' + 10;
106 if('0' <= *string && *string <= '7')
107 ret += *string - '0';
115 for( ; '0' <= *string && *string <= '9'; string++)
118 ret += *string - '0';
124 static const char cUCDIGITS[] = "0123456789ABCDEF";
126 * \fn void itoa(char *buf, Uint num, int base, int minLength, char pad)
127 * \brief Convert an integer into a character string
129 void itoa(char *buf, Uint num, int base, int minLength, char pad)
138 if(base > 16 || base < 2) {
144 while(num > base-1) {
145 tmpBuf[pos] = cUCDIGITS[ num % base ];
146 num /= (Uint)base; // Shift `num` right 1 digit
149 tmpBuf[pos++] = cUCDIGITS[ num % base ]; // Last digit of `num`
154 while(minLength-- > 0) buf[i++] = pad;
155 while(pos-- > 0) buf[i++] = tmpBuf[pos]; // Reverse the order of characters
159 #define PUTCH(c) do{if(pos==__maxlen)break;if(__s){__s[pos++]=(c);}else{pos++;}}while(0)
160 int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args)
164 char tmpBuf[34]; // For Integers
170 while((c = *__format++) != 0)
172 // Non control character
173 if(c != '%') { PUTCH(c); continue; }
178 if(c == '%') { PUTCH('%'); continue; }
180 // Pointer - Done first for debugging
182 Uint ptr = va_arg(args, Uint);
183 PUTCH('*'); PUTCH('0'); PUTCH('x');
185 itoa(p, ptr, 16, BITS/4, '0');
190 val = va_arg(args, Uint);
202 if('1' <= c && c <= '9')
205 while('0' <= c && c <= '9')
213 // - Default, Long or LongLong?
215 if(c == 'l') // Long is actually the default on x86
220 val |= (Uint64)va_arg(args, Uint) << 32;
227 // - Now get the format code
233 if( (isLongLong && val >> 63) || (!isLongLong && val >> 31) ) {
237 itoa(p, val, 10, minSize, pad);
240 itoa(p, val, 10, minSize, pad);
243 itoa(p, val, 16, minSize, pad);
246 itoa(p, val, 8, minSize, pad);
249 itoa(p, val, 2, minSize, pad);
257 // String - Null Terminated Array
259 p = (char*)(Uint)val;
262 while(*p) PUTCH(*p++);
265 case 'C': // Non-Null Terminated Character Array
266 p = (char*)(Uint)val;
267 if(!p) goto printString;
268 while(minSize--) PUTCH(*p++);
282 if(__s && pos != __maxlen)
292 int sprintf(char *__s, const char *__format, ...)
297 va_start(args, __format);
298 ret = vsnprintf(__s, -1, __format, args);
305 * \fn int tolower(int c)
306 * \brief Converts a character to lower case
310 if('A' <= c && c <= 'Z')
311 return c - 'A' + 'a';
316 * \fn int strucmp(const char *Str1, const char *Str2)
317 * \brief Compare \a Str1 and \a Str2 case-insensitively
319 int strucmp(const char *Str1, const char *Str2)
321 while(*Str1 && tolower(*Str1) == tolower(*Str2))
323 return tolower(*Str1) - tolower(*Str2);
327 * \fn int strpos(const char *Str, char Ch)
328 * \brief Search a string for an ascii character
330 int strpos(const char *Str, char Ch)
333 for(pos=0;Str[pos];pos++)
335 if(Str[pos] == Ch) return pos;
341 * \fn Uint8 ByteSum(void *Ptr, int Size)
342 * \brief Adds the bytes in a memory region and returns the sum
344 Uint8 ByteSum(void *Ptr, int Size)
347 while(Size--) sum += *(Uint8*)Ptr++;
352 * \fn Uint strlen(const char *__str)
353 * \brief Get the length of string
355 Uint strlen(const char *__str)
358 while(*__str++) ret++;
363 * \fn char *strcpy(char *__str1, const char *__str2)
364 * \brief Copy a string to a new location
366 char *strcpy(char *__str1, const char *__str2)
369 *__str1++ = *__str2++;
370 *__str1 = '\0'; // Terminate String
375 * \fn char *strncpy(char *__str1, const char *__str2, size_t max)
376 * \brief Copy a string to a new location
378 char *strncpy(char *__str1, const char *__str2, size_t max)
380 while(*__str2 && max-- >= 1)
381 *__str1++ = *__str2++;
383 *__str1 = '\0'; // Terminate String
388 * \fn int strcmp(const char *str1, const char *str2)
389 * \brief Compare two strings return the difference between
390 * the first non-matching characters.
392 int strcmp(const char *str1, const char *str2)
394 while(*str1 && *str1 == *str2)
396 return *str1 - *str2;
400 * \fn int strncmp(const char *Str1, const char *Str2, size_t num)
401 * \brief Compare strings \a Str1 and \a Str2 to a maximum of \a num characters
403 int strncmp(const char *Str1, const char *Str2, size_t num)
405 if(num == 0) return 0; // TODO: Check what should officially happen here
406 while(--num && *Str1 && *Str1 == *Str2)
412 * \fn char *strdup(const char *Str)
413 * \brief Duplicates a string
415 char *strdup(const char *Str)
418 ret = malloc(strlen(Str)+1);
424 * \fn int DivUp(int num, int dem)
425 * \brief Divide two numbers, rounding up
426 * \param num Numerator
427 * \param dem Denominator
429 int DivUp(int num, int dem)
431 return (num+dem-1)/dem;
435 * \fn int strpos8(const char *str, Uint32 search)
436 * \brief Search a string for a UTF-8 character
438 int strpos8(const char *str, Uint32 Search)
442 for(pos=0;str[pos];pos++)
446 if(str[pos] == Search) return pos;
449 if(*(Uint8*)(str+pos) < 128) continue;
451 pos += ReadUTF8( (Uint8*)&str[pos], &val );
452 if(val == Search) return pos;
458 * \fn int ReadUTF8(Uint8 *str, Uint32 *Val)
459 * \brief Read a UTF-8 character from a string
461 int ReadUTF8(Uint8 *str, Uint32 *Val)
463 *Val = 0xFFFD; // Assume invalid character
466 if( !(*str & 0x80) ) {
471 // Middle of a sequence
472 if( (*str & 0xC0) == 0x80 ) {
477 if( (*str & 0xE0) == 0xC0 ) {
478 *Val = (*str & 0x1F) << 6; // Upper 6 Bits
480 if( (*str & 0xC0) != 0x80) return -1; // Validity check
481 *Val |= (*str & 0x3F); // Lower 6 Bits
486 if( (*str & 0xF0) == 0xE0 ) {
487 *Val = (*str & 0x0F) << 12; // Upper 4 Bits
489 if( (*str & 0xC0) != 0x80) return -1; // Validity check
490 *Val |= (*str & 0x3F) << 6; // Middle 6 Bits
492 if( (*str & 0xC0) != 0x80) return -1; // Validity check
493 *Val |= (*str & 0x3F); // Lower 6 Bits
498 if( (*str & 0xF1) == 0xF0 ) {
499 *Val = (*str & 0x07) << 18; // Upper 3 Bits
501 if( (*str & 0xC0) != 0x80) return -1; // Validity check
502 *Val |= (*str & 0x3F) << 12; // Middle-upper 6 Bits
504 if( (*str & 0xC0) != 0x80) return -1; // Validity check
505 *Val |= (*str & 0x3F) << 6; // Middle-lower 6 Bits
507 if( (*str & 0xC0) != 0x80) return -1; // Validity check
508 *Val |= (*str & 0x3F); // Lower 6 Bits
512 // UTF-8 Doesn't support more than four bytes
517 * \fn int WriteUTF8(Uint8 *str, Uint32 Val)
518 * \brief Write a UTF-8 character sequence to a string
520 int WriteUTF8(Uint8 *str, Uint32 Val)
530 *str = 0xC0 | (Val >> 6);
532 *str = 0x80 | (Val & 0x3F);
537 if( Val < 0x10000 ) {
538 *str = 0xE0 | (Val >> 12);
540 *str = 0x80 | ((Val >> 6) & 0x3F);
542 *str = 0x80 | (Val & 0x3F);
547 if( Val < 0x110000 ) {
548 *str = 0xF0 | (Val >> 18);
550 *str = 0x80 | ((Val >> 12) & 0x3F);
552 *str = 0x80 | ((Val >> 6) & 0x3F);
554 *str = 0x80 | (Val & 0x3F);
558 // UTF-8 Doesn't support more than four bytes
563 * \fn Uint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
564 * \brief Converts a date into an Acess Timestamp
566 Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
573 stamp += day*3600*24;
574 stamp += month*DAYS_BEFORE[month]*3600*24;
576 ((year&3) == 0 || year%100 != 0)
577 || (year%100 == 0 && ((year/100)&3) == 0)
578 ) && month > 1) // Leap year and after feb
581 stamp += ((365*4+1) * ((year-2000)&~3)) * 3600*24; // Foour Year Segments
582 stamp += ((year-2000)&3) * 365*3600*24; // Inside four year segment
590 * \brief Pseudo random number generator
591 * \note Unknown effectiveness (made up on the spot)
595 Uint old = giRandomState;
596 // Get the next state value
597 giRandomState = (RANDOM_A*giRandomState + RANDOM_C) & 0xFFFFFFFF;
598 // Check if it has changed, and if it hasn't, change it
599 if(giRandomState == old) giRandomState += RANDOM_SPRUCE;
600 return giRandomState;
603 /// \name Memory Validation
606 * \brief Checks if a string resides fully in valid memory
608 int CheckString(char *String)
611 if( MM_IsUser( (tVAddr)String ) )
616 if( !MM_IsUser( (tVAddr)String ) )
618 // Increment string pointer
623 else if( MM_GetPhysAddr( (tVAddr)String ) )
628 if( !MM_GetPhysAddr( (tVAddr)String ) )
630 // Increment string pointer
639 * \brief Check if a sized memory region is valid memory
641 int CheckMem(void *Mem, int NumBytes)
643 tVAddr addr = (tVAddr)Mem;
645 if( MM_IsUser( addr ) )
649 if( !MM_IsUser( addr ) )
655 else if( MM_GetPhysAddr( addr ) )
659 if( !MM_GetPhysAddr( addr ) )
670 * \brief Search a string array for \a Needle
671 * \note Helper function for eTplDrv_IOCtl::DRV_IOCTL_LOOKUP
673 int ModUtil_LookupString(char **Array, char *Needle)
676 if( !CheckString(Needle) ) return -1;
677 for( i = 0; Array[i]; i++ )
679 if(strcmp(Array[i], Needle) == 0) return i;
684 int ModUtil_SetIdent(char *Dest, char *Value)
686 if( !CheckMem(Dest, 32) ) return -1;
687 strncpy(Dest, Value, 32);