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
160 * \brief Append a character the the vsnprintf output
162 #define PUTCH(c) do{\
164 if(pos==__maxlen){return pos;}\
165 if(__s){__s[pos++]=ch;}else{pos++;}\
167 int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args)
171 char tmpBuf[34]; // For Integers
177 //Log("vsnprintf: (__s=%p, __maxlen=%i, __format='%s', ...)", __s, __maxlen, __format);
179 while((c = *__format++) != 0)
181 // Non control character
182 if(c != '%') { PUTCH(c); continue; }
185 //Log("pos = %i", pos);
188 if(c == '%') { PUTCH('%'); continue; }
190 // Pointer - Done first for debugging
192 Uint ptr = va_arg(args, Uint);
193 PUTCH('*'); PUTCH('0'); PUTCH('x');
195 itoa(p, ptr, 16, BITS/4, '0');
200 val = va_arg(args, Uint);
201 //Log("val = %x", val);
214 if('1' <= c && c <= '9')
217 while('0' <= c && c <= '9')
225 // - Default, Long or LongLong?
227 if(c == 'l') // Long is actually the default on x86
232 val |= (Uint64)va_arg(args, Uint) << 32;
239 // - Now get the format code
245 if( (isLongLong && val >> 63) || (!isLongLong && val >> 31) ) {
249 itoa(p, val, 10, minSize, pad);
252 itoa(p, val, 10, minSize, pad);
255 itoa(p, val, 16, minSize, pad);
258 itoa(p, val, 8, minSize, pad);
261 itoa(p, val, 2, minSize, pad);
269 // String - Null Terminated Array
271 p = (char*)(Uint)val;
273 //Log("p = '%s'", p);
275 while(*p) PUTCH(*p++);
278 case 'C': // Non-Null Terminated Character Array
279 p = (char*)(Uint)val;
280 if(!p) goto printString;
281 while(minSize--) PUTCH(*p++);
292 if(__s && pos != __maxlen)
301 int sprintf(char *__s, const char *__format, ...)
306 va_start(args, __format);
307 ret = vsnprintf(__s, -1, __format, args);
314 * \fn int tolower(int c)
315 * \brief Converts a character to lower case
319 if('A' <= c && c <= 'Z')
320 return c - 'A' + 'a';
325 * \fn int strucmp(const char *Str1, const char *Str2)
326 * \brief Compare \a Str1 and \a Str2 case-insensitively
328 int strucmp(const char *Str1, const char *Str2)
330 while(*Str1 && tolower(*Str1) == tolower(*Str2))
332 return tolower(*Str1) - tolower(*Str2);
336 * \fn int strpos(const char *Str, char Ch)
337 * \brief Search a string for an ascii character
339 int strpos(const char *Str, char Ch)
342 for(pos=0;Str[pos];pos++)
344 if(Str[pos] == Ch) return pos;
350 * \fn Uint8 ByteSum(void *Ptr, int Size)
351 * \brief Adds the bytes in a memory region and returns the sum
353 Uint8 ByteSum(void *Ptr, int Size)
356 while(Size--) sum += *(Uint8*)Ptr++;
361 * \fn Uint strlen(const char *__str)
362 * \brief Get the length of string
364 Uint strlen(const char *__str)
367 while(*__str++) ret++;
372 * \fn char *strcpy(char *__str1, const char *__str2)
373 * \brief Copy a string to a new location
375 char *strcpy(char *__str1, const char *__str2)
378 *__str1++ = *__str2++;
379 *__str1 = '\0'; // Terminate String
384 * \fn char *strncpy(char *__str1, const char *__str2, size_t max)
385 * \brief Copy a string to a new location
387 char *strncpy(char *__str1, const char *__str2, size_t max)
389 while(*__str2 && max-- >= 1)
390 *__str1++ = *__str2++;
392 *__str1 = '\0'; // Terminate String
397 * \fn int strcmp(const char *str1, const char *str2)
398 * \brief Compare two strings return the difference between
399 * the first non-matching characters.
401 int strcmp(const char *str1, const char *str2)
403 while(*str1 && *str1 == *str2)
405 return *str1 - *str2;
409 * \fn int strncmp(const char *Str1, const char *Str2, size_t num)
410 * \brief Compare strings \a Str1 and \a Str2 to a maximum of \a num characters
412 int strncmp(const char *Str1, const char *Str2, size_t num)
414 if(num == 0) return 0; // TODO: Check what should officially happen here
415 while(--num && *Str1 && *Str1 == *Str2)
421 * \fn char *strdup(const char *Str)
422 * \brief Duplicates a string
424 char *strdup(const char *Str)
427 ret = malloc(strlen(Str)+1);
433 * \fn int DivUp(int num, int dem)
434 * \brief Divide two numbers, rounding up
435 * \param num Numerator
436 * \param dem Denominator
438 int DivUp(int num, int dem)
440 return (num+dem-1)/dem;
444 * \fn int strpos8(const char *str, Uint32 search)
445 * \brief Search a string for a UTF-8 character
447 int strpos8(const char *str, Uint32 Search)
451 for(pos=0;str[pos];pos++)
455 if(str[pos] == Search) return pos;
458 if(*(Uint8*)(str+pos) < 128) continue;
460 pos += ReadUTF8( (Uint8*)&str[pos], &val );
461 if(val == Search) return pos;
467 * \fn int ReadUTF8(Uint8 *str, Uint32 *Val)
468 * \brief Read a UTF-8 character from a string
470 int ReadUTF8(Uint8 *str, Uint32 *Val)
472 *Val = 0xFFFD; // Assume invalid character
475 if( !(*str & 0x80) ) {
480 // Middle of a sequence
481 if( (*str & 0xC0) == 0x80 ) {
486 if( (*str & 0xE0) == 0xC0 ) {
487 *Val = (*str & 0x1F) << 6; // Upper 6 Bits
489 if( (*str & 0xC0) != 0x80) return -1; // Validity check
490 *Val |= (*str & 0x3F); // Lower 6 Bits
495 if( (*str & 0xF0) == 0xE0 ) {
496 *Val = (*str & 0x0F) << 12; // Upper 4 Bits
498 if( (*str & 0xC0) != 0x80) return -1; // Validity check
499 *Val |= (*str & 0x3F) << 6; // Middle 6 Bits
501 if( (*str & 0xC0) != 0x80) return -1; // Validity check
502 *Val |= (*str & 0x3F); // Lower 6 Bits
507 if( (*str & 0xF1) == 0xF0 ) {
508 *Val = (*str & 0x07) << 18; // Upper 3 Bits
510 if( (*str & 0xC0) != 0x80) return -1; // Validity check
511 *Val |= (*str & 0x3F) << 12; // Middle-upper 6 Bits
513 if( (*str & 0xC0) != 0x80) return -1; // Validity check
514 *Val |= (*str & 0x3F) << 6; // Middle-lower 6 Bits
516 if( (*str & 0xC0) != 0x80) return -1; // Validity check
517 *Val |= (*str & 0x3F); // Lower 6 Bits
521 // UTF-8 Doesn't support more than four bytes
526 * \fn int WriteUTF8(Uint8 *str, Uint32 Val)
527 * \brief Write a UTF-8 character sequence to a string
529 int WriteUTF8(Uint8 *str, Uint32 Val)
539 *str = 0xC0 | (Val >> 6);
541 *str = 0x80 | (Val & 0x3F);
546 if( Val < 0x10000 ) {
547 *str = 0xE0 | (Val >> 12);
549 *str = 0x80 | ((Val >> 6) & 0x3F);
551 *str = 0x80 | (Val & 0x3F);
556 if( Val < 0x110000 ) {
557 *str = 0xF0 | (Val >> 18);
559 *str = 0x80 | ((Val >> 12) & 0x3F);
561 *str = 0x80 | ((Val >> 6) & 0x3F);
563 *str = 0x80 | (Val & 0x3F);
567 // UTF-8 Doesn't support more than four bytes
572 * \fn Uint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
573 * \brief Converts a date into an Acess Timestamp
575 Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
582 stamp += day*3600*24;
583 stamp += month*DAYS_BEFORE[month]*3600*24;
585 ((year&3) == 0 || year%100 != 0)
586 || (year%100 == 0 && ((year/100)&3) == 0)
587 ) && month > 1) // Leap year and after feb
590 stamp += ((365*4+1) * ((year-2000)&~3)) * 3600*24; // Foour Year Segments
591 stamp += ((year-2000)&3) * 365*3600*24; // Inside four year segment
599 * \brief Pseudo random number generator
600 * \note Unknown effectiveness (made up on the spot)
604 Uint old = giRandomState;
605 // Get the next state value
606 giRandomState = (RANDOM_A*giRandomState + RANDOM_C) & 0xFFFFFFFF;
607 // Check if it has changed, and if it hasn't, change it
608 if(giRandomState == old) giRandomState += RANDOM_SPRUCE;
609 return giRandomState;
612 /// \name Memory Validation
615 * \brief Checks if a string resides fully in valid memory
617 int CheckString(char *String)
620 if( MM_IsUser( (tVAddr)String ) )
625 if( !MM_IsUser( (tVAddr)String ) )
627 // Increment string pointer
632 else if( MM_GetPhysAddr( (tVAddr)String ) )
637 if( !MM_GetPhysAddr( (tVAddr)String ) )
639 // Increment string pointer
648 * \brief Check if a sized memory region is valid memory
650 int CheckMem(void *Mem, int NumBytes)
652 tVAddr addr = (tVAddr)Mem;
654 if( MM_IsUser( addr ) )
658 if( !MM_IsUser( addr ) )
664 else if( MM_GetPhysAddr( addr ) )
668 if( !MM_GetPhysAddr( addr ) )
679 * \brief Search a string array for \a Needle
680 * \note Helper function for eTplDrv_IOCtl::DRV_IOCTL_LOOKUP
682 int ModUtil_LookupString(char **Array, char *Needle)
685 if( !CheckString(Needle) ) return -1;
686 for( i = 0; Array[i]; i++ )
688 if(strcmp(Array[i], Needle) == 0) return i;
693 int ModUtil_SetIdent(char *Dest, char *Value)
695 if( !CheckMem(Dest, 32) ) return -1;
696 strncpy(Dest, Value, 32);