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 ReadUTF8(Uint8 *str, Uint32 *Val);
20 static Uint giRandomState = RANDOM_SEED;
24 * \brief Convert a string into an integer
26 int atoi(const char *string)
30 // Clear non-numeric characters
31 while( !('0' <= *string && *string <= '9') ) string++;
42 if('0' <= *string && *string <= '9')
44 else if('A' <= *string && *string <= 'F')
45 ret += *string - 'A' + 10;
46 else if('a' <= *string && *string <= 'f')
47 ret += *string - 'a' + 10;
58 if('0' <= *string && *string <= '7')
67 for( ; '0' <= *string && *string <= '9'; string++)
76 static const char cUCDIGITS[] = "0123456789ABCDEF";
78 * \fn void itoa(char *buf, Uint num, int base, int minLength, char pad)
79 * \brief Convert an integer into a character string
81 void itoa(char *buf, Uint num, int base, int minLength, char pad)
90 if(base > 16 || base < 2) {
97 tmpBuf[pos] = cUCDIGITS[ num % base ];
98 num /= (Uint)base; // Shift `num` right 1 digit
101 tmpBuf[pos++] = cUCDIGITS[ num % base ]; // Last digit of `num`
106 while(minLength-- > 0) buf[i++] = pad;
107 while(pos-- > 0) buf[i++] = tmpBuf[pos]; // Reverse the order of characters
111 #define PUTCH(c) do{if(pos==__maxlen)break;if(__s){__s[pos++]=(c);}else{pos++;}}while(0)
112 int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args)
116 char tmpBuf[34]; // For Integers
122 while((c = *__format++) != 0)
124 // Non control character
125 if(c != '%') { PUTCH(c); continue; }
130 if(c == '%') { PUTCH('%'); continue; }
132 // Pointer - Done first for debugging
134 Uint ptr = va_arg(args, Uint);
135 PUTCH('*'); PUTCH('0'); PUTCH('x');
137 itoa(p, ptr, 16, BITS/4, '0');
142 val = va_arg(args, Uint);
154 if('1' <= c && c <= '9')
157 while('0' <= c && c <= '9')
165 // - Default, Long or LongLong?
167 if(c == 'l') // Long is actually the default on x86
172 val |= (Uint64)va_arg(args, Uint) << 32;
179 // - Now get the format code
185 if( (isLongLong && val >> 63) || (!isLongLong && val >> 31) ) {
189 itoa(p, val, 10, minSize, pad);
192 itoa(p, val, 10, minSize, pad);
195 itoa(p, val, 16, minSize, pad);
198 itoa(p, val, 8, minSize, pad);
201 itoa(p, val, 2, minSize, pad);
209 // String - Null Terminated Array
211 p = (char*)(Uint)val;
214 while(*p) PUTCH(*p++);
217 case 'C': // Non-Null Terminated Character Array
218 p = (char*)(Uint)val;
219 if(!p) goto printString;
220 while(minSize--) PUTCH(*p++);
231 if(__s && pos != __maxlen)
241 int sprintf(char *__s, const char *__format, ...)
246 va_start(args, __format);
247 ret = vsnprintf(__s, -1, __format, args);
254 * \fn int tolower(int c)
255 * \brief Converts a character to lower case
259 if('A' <= c && c <= 'Z')
260 return c - 'A' + 'a';
265 * \fn int strucmp(char *Str1, char *Str2)
266 * \brief Compare \a Str1 and \a Str2 case-insensitively
268 int strucmp(const char *Str1, const char *Str2)
270 while(*Str1 && tolower(*Str1) == tolower(*Str2))
272 return tolower(*Str1) - tolower(*Str2);
276 * \fn int strpos(const char *Str, char Ch)
277 * \brief Search a string for an ascii character
279 int strpos(const char *Str, char Ch)
282 for(pos=0;Str[pos];pos++)
284 if(Str[pos] == Ch) return pos;
290 * \fn Uint8 ByteSum(void *Ptr, int Size)
291 * \brief Adds the bytes in a memory region and returns the sum
293 Uint8 ByteSum(void *Ptr, int Size)
296 while(Size--) sum += *(Uint8*)Ptr++;
301 * \fn Uint strlen(const char *__str)
302 * \brief Get the length of string
304 Uint strlen(const char *__str)
307 while(*__str++) ret++;
312 * \fn char *strcpy(const char *__str1, const char *__str2)
313 * \brief Copy a string to a new location
315 char *strcpy(char *__str1, const char *__str2)
318 *__str1++ = *__str2++;
319 *__str1 = '\0'; // Terminate String
324 * \fn char *strcpy(const char *__str1, const char *__str2)
325 * \brief Copy a string to a new location
327 char *strncpy(char *__str1, const char *__str2, size_t max)
329 while(*__str2 && max-- >= 1)
330 *__str1++ = *__str2++;
332 *__str1 = '\0'; // Terminate String
337 * \fn int strcmp(const char *str1, const char *str2)
338 * \brief Compare two strings return the difference between
339 * the first non-matching characters.
341 int strcmp(const char *str1, const char *str2)
343 while(*str1 && *str1 == *str2)
345 return *str1 - *str2;
349 * \fn int strncmp(const char *Str1, const char *Str2, size_t num)
350 * \brief Compare strings \a Str1 and \a Str2 to a maximum of \a num characters
352 int strncmp(const char *Str1, const char *Str2, size_t num)
354 if(num == 0) return 0; // TODO: Check what should officially happen here
355 while(--num && *Str1 && *Str1 == *Str2)
361 * \fn char *strdup(const char *Str)
362 * \brief Duplicates a string
364 char *strdup(const char *Str)
367 ret = malloc(strlen(Str)+1);
373 * \fn int DivUp(int num, int dem)
374 * \brief Divide two numbers, rounding up
375 * \param num Numerator
376 * \param dem Denominator
378 int DivUp(int num, int dem)
380 return (num+dem-1)/dem;
384 * \fn int strpos8(const char *str, Uint32 search)
385 * \brief Search a string for a UTF-8 character
387 int strpos8(const char *str, Uint32 Search)
391 for(pos=0;str[pos];pos++)
395 if(str[pos] == Search) return pos;
398 if(*(Uint8*)(str+pos) < 128) continue;
400 pos += ReadUTF8( (Uint8*)&str[pos], &val );
401 if(val == Search) return pos;
407 * \fn int ReadUTF8(Uint8 *str, Uint32 *Val)
408 * \brief Read a UTF-8 character from a string
410 int ReadUTF8(Uint8 *str, Uint32 *Val)
412 *Val = 0xFFFD; // Assume invalid character
415 if( !(*str & 0x80) ) {
420 // Middle of a sequence
421 if( (*str & 0xC0) == 0x80 ) {
426 if( (*str & 0xE0) == 0xC0 ) {
427 *Val = (*str & 0x1F) << 6; // Upper 6 Bits
429 if( (*str & 0xC0) != 0x80) return -1; // Validity check
430 *Val |= (*str & 0x3F); // Lower 6 Bits
435 if( (*str & 0xF0) == 0xE0 ) {
436 *Val = (*str & 0x0F) << 12; // Upper 4 Bits
438 if( (*str & 0xC0) != 0x80) return -1; // Validity check
439 *Val |= (*str & 0x3F) << 6; // Middle 6 Bits
441 if( (*str & 0xC0) != 0x80) return -1; // Validity check
442 *Val |= (*str & 0x3F); // Lower 6 Bits
447 if( (*str & 0xF1) == 0xF0 ) {
448 *Val = (*str & 0x07) << 18; // Upper 3 Bits
450 if( (*str & 0xC0) != 0x80) return -1; // Validity check
451 *Val |= (*str & 0x3F) << 12; // Middle-upper 6 Bits
453 if( (*str & 0xC0) != 0x80) return -1; // Validity check
454 *Val |= (*str & 0x3F) << 6; // Middle-lower 6 Bits
456 if( (*str & 0xC0) != 0x80) return -1; // Validity check
457 *Val |= (*str & 0x3F); // Lower 6 Bits
461 // UTF-8 Doesn't support more than four bytes
466 * \fn int WriteUTF8(Uint8 *str, Uint32 Val)
467 * \brief Write a UTF-8 character sequence to a string
469 int WriteUTF8(Uint8 *str, Uint32 Val)
479 *str = 0xC0 | (Val >> 6);
481 *str = 0x80 | (Val & 0x3F);
486 if( Val < 0x10000 ) {
487 *str = 0xE0 | (Val >> 12);
489 *str = 0x80 | ((Val >> 6) & 0x3F);
491 *str = 0x80 | (Val & 0x3F);
496 if( Val < 0x110000 ) {
497 *str = 0xF0 | (Val >> 18);
499 *str = 0x80 | ((Val >> 12) & 0x3F);
501 *str = 0x80 | ((Val >> 6) & 0x3F);
503 *str = 0x80 | (Val & 0x3F);
507 // UTF-8 Doesn't support more than four bytes
512 * \fn Uint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
513 * \brief Converts a date into an Acess Timestamp
515 Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
522 stamp += day*3600*24;
523 stamp += month*DAYS_BEFORE[month]*3600*24;
525 ((year&3) == 0 || year%100 != 0)
526 || (year%100 == 0 && ((year/100)&3) == 0)
527 ) && month > 1) // Leap year and after feb
530 stamp += ((365*4+1) * ((year-2000)&~3)) * 3600*24; // Foour Year Segments
531 stamp += ((year-2000)&3) * 365*3600*24; // Inside four year segment
539 * \brief Pseudo random number generator
540 * \note Unknown effectiveness (made up on the spot)
544 Uint old = giRandomState;
545 // Get the next state value
546 giRandomState = (RANDOM_A*giRandomState + RANDOM_C) & 0xFFFFFFFF;
547 // Check if it has changed, and if it hasn't, change it
548 if(giRandomState == old) giRandomState += RANDOM_SPRUCE;
549 return giRandomState;
552 /// \name Memory Validation
555 * \brief Checks if a string resides fully in valid memory
557 int CheckString(char *String)
560 if( MM_IsUser( (tVAddr)String ) )
565 if( !MM_IsUser( (tVAddr)String ) )
567 // Increment string pointer
572 else if( MM_GetPhysAddr( (tVAddr)String ) )
577 if( !MM_GetPhysAddr( (tVAddr)String ) )
579 // Increment string pointer
588 * \brief Check if a sized memory region is valid memory
590 int CheckMem(void *Mem, int NumBytes)
592 tVAddr addr = (tVAddr)Mem;
594 if( MM_IsUser( addr ) )
598 if( !MM_IsUser( addr ) )
604 else if( MM_GetPhysAddr( addr ) )
608 if( !MM_GetPhysAddr( addr ) )
619 * \brief Search a string array for \a Needle
620 * \note Helper function for eTplDrv_IOCtl::DRV_IOCTL_LOOKUP
622 int ModUtil_LookupString(char **Array, char *Needle)
625 if( !CheckString(Needle) ) return -1;
626 for( i = 0; Array[i]; i++ )
628 if(strcmp(Array[i], Needle) == 0) return i;
633 int ModUtil_SetIdent(char *Dest, char *Value)
635 if( !CheckMem(Dest, 32) ) return -1;
636 strncpy(Dest, Value, 32);
651 EXPORT(ModUtil_LookupString);
652 EXPORT(ModUtil_SetIdent);