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)
79 //Log("atoi: (string='%s')", string);
81 // Clear non-numeric characters
82 while( !('0' <= *string && *string <= '9') && *string != '-' ) string++;
83 if( *string == '-' ) {
85 while( !('0' <= *string && *string <= '9') ) string++;
97 if('0' <= *string && *string <= '9') {
101 else if('A' <= *string && *string <= 'F') {
103 ret += *string - 'A' + 10;
105 else if('a' <= *string && *string <= 'f') {
107 ret += *string - 'a' + 10;
115 for( ; '0' <= *string && *string <= '7'; string ++ )
118 ret += *string - '0';
124 for( ; '0' <= *string && *string <= '9'; string++)
127 ret += *string - '0';
133 //Log("atoi: RETURN %i", ret);
138 static const char cUCDIGITS[] = "0123456789ABCDEF";
140 * \fn void itoa(char *buf, Uint num, int base, int minLength, char pad)
141 * \brief Convert an integer into a character string
143 void itoa(char *buf, Uint num, int base, int minLength, char pad)
152 if(base > 16 || base < 2) {
158 while(num > base-1) {
159 tmpBuf[pos] = cUCDIGITS[ num % base ];
160 num /= (Uint)base; // Shift `num` right 1 digit
163 tmpBuf[pos++] = cUCDIGITS[ num % base ]; // Last digit of `num`
168 while(minLength-- > 0) buf[i++] = pad;
169 while(pos-- > 0) buf[i++] = tmpBuf[pos]; // Reverse the order of characters
174 * \brief Append a character the the vsnprintf output
176 #define PUTCH(c) do{\
178 if(pos==__maxlen){return pos;}\
179 if(__s){__s[pos++]=ch;}else{pos++;}\
181 int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args)
184 int minSize = 0, len;
185 char tmpBuf[34]; // For Integers
193 //Log("vsnprintf: (__s=%p, __maxlen=%i, __format='%s', ...)", __s, __maxlen, __format);
195 while((c = *__format++) != 0)
197 // Non control character
198 if(c != '%') { PUTCH(c); continue; }
201 //Log("pos = %i", pos);
204 if(c == '%') { PUTCH('%'); continue; }
206 // Pointer - Done first for debugging
208 Uint ptr = va_arg(args, Uint);
209 PUTCH('*'); PUTCH('0'); PUTCH('x');
211 itoa(p, ptr, 16, BITS/4, '0');
216 val = va_arg(args, Uint);
218 // - Padding Side Flag
234 if('1' <= c && c <= '9')
237 while('0' <= c && c <= '9')
245 // - Default, Long or LongLong?
247 if(c == 'l') // Long is actually the default on x86
252 val |= (Uint64)va_arg(args, Uint) << 32;
259 // - Now get the format code
265 if( (isLongLong && val >> 63) || (!isLongLong && val >> 31) ) {
269 itoa(p, val, 10, minSize, pad);
272 itoa(p, val, 10, minSize, pad);
275 itoa(p, val, 16, minSize, pad);
278 itoa(p, val, 8, minSize, pad);
281 itoa(p, val, 2, minSize, pad);
289 // String - Null Terminated Array
291 p = (char*)(Uint)val;
295 if( !bPadLeft ) while(len++ < minSize) PUTCH(pad);
296 while(*p) PUTCH(*p++);
297 if( bPadLeft ) while(len++ < minSize) PUTCH(pad);
300 case 'C': // Non-Null Terminated Character Array
301 p = (char*)(Uint)val;
302 if(!p) goto printString;
303 while(minSize--) PUTCH(*p++);
314 if(__s && pos != __maxlen)
323 int sprintf(char *__s, const char *__format, ...)
328 va_start(args, __format);
329 ret = vsnprintf(__s, -1, __format, args);
336 * \fn int tolower(int c)
337 * \brief Converts a character to lower case
341 if('A' <= c && c <= 'Z')
342 return c - 'A' + 'a';
347 * \fn int strucmp(const char *Str1, const char *Str2)
348 * \brief Compare \a Str1 and \a Str2 case-insensitively
350 int strucmp(const char *Str1, const char *Str2)
352 while(*Str1 && tolower(*Str1) == tolower(*Str2))
354 return tolower(*Str1) - tolower(*Str2);
358 * \fn int strpos(const char *Str, char Ch)
359 * \brief Search a string for an ascii character
361 int strpos(const char *Str, char Ch)
364 for(pos=0;Str[pos];pos++)
366 if(Str[pos] == Ch) return pos;
372 * \fn Uint8 ByteSum(void *Ptr, int Size)
373 * \brief Adds the bytes in a memory region and returns the sum
375 Uint8 ByteSum(void *Ptr, int Size)
378 while(Size--) sum += *(Uint8*)Ptr++;
383 * \fn Uint strlen(const char *__str)
384 * \brief Get the length of string
386 Uint strlen(const char *__str)
389 while(*__str++) ret++;
394 * \fn char *strcpy(char *__str1, const char *__str2)
395 * \brief Copy a string to a new location
397 char *strcpy(char *__str1, const char *__str2)
400 *__str1++ = *__str2++;
401 *__str1 = '\0'; // Terminate String
406 * \fn char *strncpy(char *__str1, const char *__str2, size_t max)
407 * \brief Copy a string to a new location
409 char *strncpy(char *__str1, const char *__str2, size_t max)
411 while(*__str2 && max-- >= 1)
412 *__str1++ = *__str2++;
414 *__str1 = '\0'; // Terminate String
419 * \fn int strcmp(const char *str1, const char *str2)
420 * \brief Compare two strings return the difference between
421 * the first non-matching characters.
423 int strcmp(const char *str1, const char *str2)
425 while(*str1 && *str1 == *str2)
427 return *str1 - *str2;
431 * \fn int strncmp(const char *Str1, const char *Str2, size_t num)
432 * \brief Compare strings \a Str1 and \a Str2 to a maximum of \a num characters
434 int strncmp(const char *Str1, const char *Str2, size_t num)
436 if(num == 0) return 0; // TODO: Check what should officially happen here
437 while(--num && *Str1 && *Str1 == *Str2)
443 * \fn char *strdup(const char *Str)
444 * \brief Duplicates a string
446 char *strdup(const char *Str)
449 ret = malloc(strlen(Str)+1);
455 * \fn int DivUp(int num, int dem)
456 * \brief Divide two numbers, rounding up
457 * \param num Numerator
458 * \param dem Denominator
460 int DivUp(int num, int dem)
462 return (num+dem-1)/dem;
466 * \fn int strpos8(const char *str, Uint32 search)
467 * \brief Search a string for a UTF-8 character
469 int strpos8(const char *str, Uint32 Search)
473 for(pos=0;str[pos];pos++)
477 if(str[pos] == Search) return pos;
480 if(*(Uint8*)(str+pos) < 128) continue;
482 pos += ReadUTF8( (Uint8*)&str[pos], &val );
483 if(val == Search) return pos;
489 * \fn int ReadUTF8(Uint8 *str, Uint32 *Val)
490 * \brief Read a UTF-8 character from a string
492 int ReadUTF8(Uint8 *str, Uint32 *Val)
494 *Val = 0xFFFD; // Assume invalid character
497 if( !(*str & 0x80) ) {
502 // Middle of a sequence
503 if( (*str & 0xC0) == 0x80 ) {
508 if( (*str & 0xE0) == 0xC0 ) {
509 *Val = (*str & 0x1F) << 6; // Upper 6 Bits
511 if( (*str & 0xC0) != 0x80) return -1; // Validity check
512 *Val |= (*str & 0x3F); // Lower 6 Bits
517 if( (*str & 0xF0) == 0xE0 ) {
518 *Val = (*str & 0x0F) << 12; // Upper 4 Bits
520 if( (*str & 0xC0) != 0x80) return -1; // Validity check
521 *Val |= (*str & 0x3F) << 6; // Middle 6 Bits
523 if( (*str & 0xC0) != 0x80) return -1; // Validity check
524 *Val |= (*str & 0x3F); // Lower 6 Bits
529 if( (*str & 0xF1) == 0xF0 ) {
530 *Val = (*str & 0x07) << 18; // Upper 3 Bits
532 if( (*str & 0xC0) != 0x80) return -1; // Validity check
533 *Val |= (*str & 0x3F) << 12; // Middle-upper 6 Bits
535 if( (*str & 0xC0) != 0x80) return -1; // Validity check
536 *Val |= (*str & 0x3F) << 6; // Middle-lower 6 Bits
538 if( (*str & 0xC0) != 0x80) return -1; // Validity check
539 *Val |= (*str & 0x3F); // Lower 6 Bits
543 // UTF-8 Doesn't support more than four bytes
548 * \fn int WriteUTF8(Uint8 *str, Uint32 Val)
549 * \brief Write a UTF-8 character sequence to a string
551 int WriteUTF8(Uint8 *str, Uint32 Val)
561 *str = 0xC0 | (Val >> 6);
563 *str = 0x80 | (Val & 0x3F);
568 if( Val < 0x10000 ) {
569 *str = 0xE0 | (Val >> 12);
571 *str = 0x80 | ((Val >> 6) & 0x3F);
573 *str = 0x80 | (Val & 0x3F);
578 if( Val < 0x110000 ) {
579 *str = 0xF0 | (Val >> 18);
581 *str = 0x80 | ((Val >> 12) & 0x3F);
583 *str = 0x80 | ((Val >> 6) & 0x3F);
585 *str = 0x80 | (Val & 0x3F);
589 // UTF-8 Doesn't support more than four bytes
594 * \fn Uint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
595 * \brief Converts a date into an Acess Timestamp
597 Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
604 stamp += day*3600*24;
605 stamp += month*DAYS_BEFORE[month]*3600*24;
607 ((year&3) == 0 || year%100 != 0)
608 || (year%100 == 0 && ((year/100)&3) == 0)
609 ) && month > 1) // Leap year and after feb
612 stamp += ((365*4+1) * ((year-2000)&~3)) * 3600*24; // Foour Year Segments
613 stamp += ((year-2000)&3) * 365*3600*24; // Inside four year segment
621 * \brief Pseudo random number generator
622 * \note Unknown effectiveness (made up on the spot)
626 Uint old = giRandomState;
627 // Get the next state value
628 giRandomState = (RANDOM_A*giRandomState + RANDOM_C) & 0xFFFFFFFF;
629 // Check if it has changed, and if it hasn't, change it
630 if(giRandomState == old) giRandomState += RANDOM_SPRUCE;
631 return giRandomState;
634 /// \name Memory Validation
637 * \brief Checks if a string resides fully in valid memory
639 int CheckString(char *String)
642 if( MM_IsUser( (tVAddr)String ) )
647 if( !MM_IsUser( (tVAddr)String ) )
649 // Increment string pointer
654 else if( MM_GetPhysAddr( (tVAddr)String ) )
659 if( !MM_GetPhysAddr( (tVAddr)String ) )
661 // Increment string pointer
670 * \brief Check if a sized memory region is valid memory
672 int CheckMem(void *Mem, int NumBytes)
674 tVAddr addr = (tVAddr)Mem;
676 if( MM_IsUser( addr ) )
680 if( !MM_IsUser( addr ) )
686 else if( MM_GetPhysAddr( addr ) )
690 if( !MM_GetPhysAddr( addr ) )
701 * \brief Search a string array for \a Needle
702 * \note Helper function for eTplDrv_IOCtl::DRV_IOCTL_LOOKUP
704 int ModUtil_LookupString(char **Array, char *Needle)
707 if( !CheckString(Needle) ) return -1;
708 for( i = 0; Array[i]; i++ )
710 if(strcmp(Array[i], Needle) == 0) return i;
715 int ModUtil_SetIdent(char *Dest, char *Value)
717 if( !CheckMem(Dest, 32) ) return -1;
718 strncpy(Dest, Value, 32);