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;
23 static const char cUCDIGITS[] = "0123456789ABCDEF";
25 * \fn static void itoa(char *buf, Uint num, int base, int minLength, char pad)
26 * \brief Convert an integer into a character string
28 void itoa(char *buf, Uint num, int base, int minLength, char pad)
37 if(base > 16 || base < 2) {
44 tmpBuf[pos] = cUCDIGITS[ num % base ];
45 num /= (Uint)base; // Shift `num` right 1 digit
48 tmpBuf[pos++] = cUCDIGITS[ num % base ]; // Last digit of `num`
53 while(minLength-- > 0) buf[i++] = pad;
54 while(pos-- > 0) buf[i++] = tmpBuf[pos]; // Reverse the order of characters
58 #define PUTCH(c) do{if(pos==__maxlen)break;if(__s){__s[pos++]=(c);}else{pos++;}}while(0)
59 int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args)
63 char tmpBuf[34]; // For Integers
69 while((c = *__format++) != 0)
71 // Non control character
72 if(c != '%') { PUTCH(c); continue; }
77 if(c == '%') { PUTCH('%'); continue; }
79 // Pointer - Done first for debugging
81 Uint ptr = va_arg(args, Uint);
82 PUTCH('*'); PUTCH('0'); PUTCH('x');
84 itoa(p, ptr, 16, BITS/4, '0');
89 val = va_arg(args, Uint);
101 if('1' <= c && c <= '9')
104 while('0' <= c && c <= '9')
112 // - Default, Long or LongLong?
114 if(c == 'l') // Long is actually the default on x86
119 val |= (Uint64)va_arg(args, Uint) << 32;
126 // - Now get the format code
132 if( (isLongLong && val >> 63) || (!isLongLong && val >> 31) ) {
136 itoa(p, val, 10, minSize, pad);
139 itoa(p, val, 10, minSize, pad);
142 itoa(p, val, 16, minSize, pad);
145 itoa(p, val, 8, minSize, pad);
148 itoa(p, val, 2, minSize, pad);
156 // String - Null Terminated Array
158 p = (char*)(Uint)val;
161 while(*p) PUTCH(*p++);
164 case 'C': // Non-Null Terminated Character Array
165 p = (char*)(Uint)val;
166 if(!p) goto printString;
167 while(minSize--) PUTCH(*p++);
178 if(__s && pos != __maxlen)
188 int sprintf(char *__s, const char *__format, ...)
193 va_start(args, __format);
194 ret = vsnprintf(__s, -1, __format, args);
201 * \fn int tolower(int c)
202 * \brief Converts a character to lower case
206 if('A' <= c && c <= 'Z')
207 return c - 'A' + 'a';
212 * \fn int strucmp(char *Str1, char *Str2)
213 * \brief Compare \a Str1 and \a Str2 case-insensitively
215 int strucmp(const char *Str1, const char *Str2)
217 while(*Str1 && tolower(*Str1) == tolower(*Str2))
219 return tolower(*Str1) - tolower(*Str2);
223 * \fn int strpos(const char *Str, char Ch)
224 * \brief Search a string for an ascii character
226 int strpos(const char *Str, char Ch)
229 for(pos=0;Str[pos];pos++)
231 if(Str[pos] == Ch) return pos;
237 * \fn Uint8 ByteSum(void *Ptr, int Size)
238 * \brief Adds the bytes in a memory region and returns the sum
240 Uint8 ByteSum(void *Ptr, int Size)
243 while(Size--) sum += *(Uint8*)Ptr++;
248 * \fn Uint strlen(const char *__str)
249 * \brief Get the length of string
251 Uint strlen(const char *__str)
254 while(*__str++) ret++;
259 * \fn char *strcpy(const char *__str1, const char *__str2)
260 * \brief Copy a string to a new location
262 char *strcpy(char *__str1, const char *__str2)
265 *__str1++ = *__str2++;
266 *__str1 = '\0'; // Terminate String
271 * \fn char *strcpy(const char *__str1, const char *__str2)
272 * \brief Copy a string to a new location
274 char *strncpy(char *__str1, const char *__str2, size_t max)
276 while(*__str2 && max-- >= 1)
277 *__str1++ = *__str2++;
279 *__str1 = '\0'; // Terminate String
284 * \fn int strcmp(const char *str1, const char *str2)
285 * \brief Compare two strings return the difference between
286 * the first non-matching characters.
288 int strcmp(const char *str1, const char *str2)
290 while(*str1 && *str1 == *str2)
292 return *str1 - *str2;
296 * \fn int strncmp(const char *Str1, const char *Str2, size_t num)
297 * \brief Compare strings \a Str1 and \a Str2 to a maximum of \a num characters
299 int strncmp(const char *Str1, const char *Str2, size_t num)
301 if(num == 0) return 0; // TODO: Check what should officially happen here
302 while(--num && *Str1 && *Str1 == *Str2)
308 * \fn char *strdup(const char *Str)
309 * \brief Duplicates a string
311 char *strdup(const char *Str)
314 ret = malloc(strlen(Str)+1);
320 * \fn int DivUp(int num, int dem)
321 * \brief Divide two numbers, rounding up
322 * \param num Numerator
323 * \param dem Denominator
325 int DivUp(int num, int dem)
327 return (num+dem-1)/dem;
331 * \fn int strpos8(const char *str, Uint32 search)
332 * \brief Search a string for a UTF-8 character
334 int strpos8(const char *str, Uint32 Search)
338 for(pos=0;str[pos];pos++)
342 if(str[pos] == Search) return pos;
345 if(*(Uint8*)(str+pos) < 128) continue;
347 pos += ReadUTF8( (Uint8*)&str[pos], &val );
348 if(val == Search) return pos;
354 * \fn int ReadUTF8(Uint8 *str, Uint32 *Val)
355 * \brief Read a UTF-8 character from a string
357 int ReadUTF8(Uint8 *str, Uint32 *Val)
359 *Val = 0xFFFD; // Assume invalid character
362 if( !(*str & 0x80) ) {
367 // Middle of a sequence
368 if( (*str & 0xC0) == 0x80 ) {
373 if( (*str & 0xE0) == 0xC0 ) {
374 *Val = (*str & 0x1F) << 6; // Upper 6 Bits
376 if( (*str & 0xC0) != 0x80) return -1; // Validity check
377 *Val |= (*str & 0x3F); // Lower 6 Bits
382 if( (*str & 0xF0) == 0xE0 ) {
383 *Val = (*str & 0x0F) << 12; // Upper 4 Bits
385 if( (*str & 0xC0) != 0x80) return -1; // Validity check
386 *Val |= (*str & 0x3F) << 6; // Middle 6 Bits
388 if( (*str & 0xC0) != 0x80) return -1; // Validity check
389 *Val |= (*str & 0x3F); // Lower 6 Bits
394 if( (*str & 0xF1) == 0xF0 ) {
395 *Val = (*str & 0x07) << 18; // Upper 3 Bits
397 if( (*str & 0xC0) != 0x80) return -1; // Validity check
398 *Val |= (*str & 0x3F) << 12; // Middle-upper 6 Bits
400 if( (*str & 0xC0) != 0x80) return -1; // Validity check
401 *Val |= (*str & 0x3F) << 6; // Middle-lower 6 Bits
403 if( (*str & 0xC0) != 0x80) return -1; // Validity check
404 *Val |= (*str & 0x3F); // Lower 6 Bits
408 // UTF-8 Doesn't support more than four bytes
413 * \fn int WriteUTF8(Uint8 *str, Uint32 Val)
414 * \brief Write a UTF-8 character sequence to a string
416 int WriteUTF8(Uint8 *str, Uint32 Val)
426 *str = 0xC0 | (Val >> 6);
428 *str = 0x80 | (Val & 0x3F);
433 if( Val < 0x10000 ) {
434 *str = 0xE0 | (Val >> 12);
436 *str = 0x80 | ((Val >> 6) & 0x3F);
438 *str = 0x80 | (Val & 0x3F);
443 if( Val < 0x110000 ) {
444 *str = 0xF0 | (Val >> 18);
446 *str = 0x80 | ((Val >> 12) & 0x3F);
448 *str = 0x80 | ((Val >> 6) & 0x3F);
450 *str = 0x80 | (Val & 0x3F);
454 // UTF-8 Doesn't support more than four bytes
459 * \fn Uint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
460 * \brief Converts a date into an Acess Timestamp
462 Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
469 stamp += day*3600*24;
470 stamp += month*DAYS_BEFORE[month]*3600*24;
472 ((year&3) == 0 || year%100 != 0)
473 || (year%100 == 0 && ((year/100)&3) == 0)
474 ) && month > 1) // Leap year and after feb
477 stamp += ((365*4+1) * ((year-2000)&~3)) * 3600*24; // Foour Year Segments
478 stamp += ((year-2000)&3) * 365*3600*24; // Inside four year segment
486 * \brief Pseudo random number generator
487 * \note Unknown effectiveness (made up on the spot)
491 Uint old = giRandomState;
492 // Get the next state value
493 giRandomState = (RANDOM_A*giRandomState + RANDOM_C) & 0xFFFFFFFF;
494 // Check if it has changed, and if it hasn't, change it
495 if(giRandomState == old) giRandomState += RANDOM_SPRUCE;
496 return giRandomState;
499 /// \name Memory Validation
502 * \brief Checks if a string resides fully in valid memory
504 int CheckString(char *String)
507 if( MM_IsUser( (tVAddr)String ) )
512 if( !MM_IsUser( (tVAddr)String ) )
514 // Increment string pointer
519 else if( MM_GetPhysAddr( (tVAddr)String ) )
524 if( !MM_GetPhysAddr( (tVAddr)String ) )
526 // Increment string pointer
535 * \brief Check if a sized memory region is valid memory
537 int CheckMem(void *Mem, int NumBytes)
539 tVAddr addr = (tVAddr)Mem;
541 if( MM_IsUser( addr ) )
545 if( !MM_IsUser( addr ) )
551 else if( MM_GetPhysAddr( addr ) )
555 if( !MM_GetPhysAddr( addr ) )
566 * \brief Search a string array for \a Needle
567 * \note Helper function for eTplDrv_IOCtl::DRV_IOCTL_LOOKUP
569 int ModUtil_LookupString(char **Array, char *Needle)
572 if( !CheckString(Needle) ) return -1;
573 for( i = 0; Array[i]; i++ )
575 if(strcmp(Array[i], Needle) == 0) return i;
580 int ModUtil_SetIdent(char *Dest, char *Value)
582 if( !CheckMem(Dest, 32) ) return -1;
583 strncpy(Dest, Value, 32);
598 EXPORT(ModUtil_LookupString);
599 EXPORT(ModUtil_SetIdent);