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)
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);
217 //Log("val = %x", val);
229 if('1' <= c && c <= '9')
232 while('0' <= c && c <= '9')
240 // - Default, Long or LongLong?
242 if(c == 'l') // Long is actually the default on x86
247 val |= (Uint64)va_arg(args, Uint) << 32;
254 // - Now get the format code
260 if( (isLongLong && val >> 63) || (!isLongLong && val >> 31) ) {
264 itoa(p, val, 10, minSize, pad);
267 itoa(p, val, 10, minSize, pad);
270 itoa(p, val, 16, minSize, pad);
273 itoa(p, val, 8, minSize, pad);
276 itoa(p, val, 2, minSize, pad);
284 // String - Null Terminated Array
286 p = (char*)(Uint)val;
288 //Log("p = '%s'", p);
290 while(*p) PUTCH(*p++);
293 case 'C': // Non-Null Terminated Character Array
294 p = (char*)(Uint)val;
295 if(!p) goto printString;
296 while(minSize--) PUTCH(*p++);
307 if(__s && pos != __maxlen)
316 int sprintf(char *__s, const char *__format, ...)
321 va_start(args, __format);
322 ret = vsnprintf(__s, -1, __format, args);
329 * \fn int tolower(int c)
330 * \brief Converts a character to lower case
334 if('A' <= c && c <= 'Z')
335 return c - 'A' + 'a';
340 * \fn int strucmp(const char *Str1, const char *Str2)
341 * \brief Compare \a Str1 and \a Str2 case-insensitively
343 int strucmp(const char *Str1, const char *Str2)
345 while(*Str1 && tolower(*Str1) == tolower(*Str2))
347 return tolower(*Str1) - tolower(*Str2);
351 * \fn int strpos(const char *Str, char Ch)
352 * \brief Search a string for an ascii character
354 int strpos(const char *Str, char Ch)
357 for(pos=0;Str[pos];pos++)
359 if(Str[pos] == Ch) return pos;
365 * \fn Uint8 ByteSum(void *Ptr, int Size)
366 * \brief Adds the bytes in a memory region and returns the sum
368 Uint8 ByteSum(void *Ptr, int Size)
371 while(Size--) sum += *(Uint8*)Ptr++;
376 * \fn Uint strlen(const char *__str)
377 * \brief Get the length of string
379 Uint strlen(const char *__str)
382 while(*__str++) ret++;
387 * \fn char *strcpy(char *__str1, const char *__str2)
388 * \brief Copy a string to a new location
390 char *strcpy(char *__str1, const char *__str2)
393 *__str1++ = *__str2++;
394 *__str1 = '\0'; // Terminate String
399 * \fn char *strncpy(char *__str1, const char *__str2, size_t max)
400 * \brief Copy a string to a new location
402 char *strncpy(char *__str1, const char *__str2, size_t max)
404 while(*__str2 && max-- >= 1)
405 *__str1++ = *__str2++;
407 *__str1 = '\0'; // Terminate String
412 * \fn int strcmp(const char *str1, const char *str2)
413 * \brief Compare two strings return the difference between
414 * the first non-matching characters.
416 int strcmp(const char *str1, const char *str2)
418 while(*str1 && *str1 == *str2)
420 return *str1 - *str2;
424 * \fn int strncmp(const char *Str1, const char *Str2, size_t num)
425 * \brief Compare strings \a Str1 and \a Str2 to a maximum of \a num characters
427 int strncmp(const char *Str1, const char *Str2, size_t num)
429 if(num == 0) return 0; // TODO: Check what should officially happen here
430 while(--num && *Str1 && *Str1 == *Str2)
436 * \fn char *strdup(const char *Str)
437 * \brief Duplicates a string
439 char *strdup(const char *Str)
442 ret = malloc(strlen(Str)+1);
448 * \fn int DivUp(int num, int dem)
449 * \brief Divide two numbers, rounding up
450 * \param num Numerator
451 * \param dem Denominator
453 int DivUp(int num, int dem)
455 return (num+dem-1)/dem;
459 * \fn int strpos8(const char *str, Uint32 search)
460 * \brief Search a string for a UTF-8 character
462 int strpos8(const char *str, Uint32 Search)
466 for(pos=0;str[pos];pos++)
470 if(str[pos] == Search) return pos;
473 if(*(Uint8*)(str+pos) < 128) continue;
475 pos += ReadUTF8( (Uint8*)&str[pos], &val );
476 if(val == Search) return pos;
482 * \fn int ReadUTF8(Uint8 *str, Uint32 *Val)
483 * \brief Read a UTF-8 character from a string
485 int ReadUTF8(Uint8 *str, Uint32 *Val)
487 *Val = 0xFFFD; // Assume invalid character
490 if( !(*str & 0x80) ) {
495 // Middle of a sequence
496 if( (*str & 0xC0) == 0x80 ) {
501 if( (*str & 0xE0) == 0xC0 ) {
502 *Val = (*str & 0x1F) << 6; // Upper 6 Bits
504 if( (*str & 0xC0) != 0x80) return -1; // Validity check
505 *Val |= (*str & 0x3F); // Lower 6 Bits
510 if( (*str & 0xF0) == 0xE0 ) {
511 *Val = (*str & 0x0F) << 12; // Upper 4 Bits
513 if( (*str & 0xC0) != 0x80) return -1; // Validity check
514 *Val |= (*str & 0x3F) << 6; // Middle 6 Bits
516 if( (*str & 0xC0) != 0x80) return -1; // Validity check
517 *Val |= (*str & 0x3F); // Lower 6 Bits
522 if( (*str & 0xF1) == 0xF0 ) {
523 *Val = (*str & 0x07) << 18; // Upper 3 Bits
525 if( (*str & 0xC0) != 0x80) return -1; // Validity check
526 *Val |= (*str & 0x3F) << 12; // Middle-upper 6 Bits
528 if( (*str & 0xC0) != 0x80) return -1; // Validity check
529 *Val |= (*str & 0x3F) << 6; // Middle-lower 6 Bits
531 if( (*str & 0xC0) != 0x80) return -1; // Validity check
532 *Val |= (*str & 0x3F); // Lower 6 Bits
536 // UTF-8 Doesn't support more than four bytes
541 * \fn int WriteUTF8(Uint8 *str, Uint32 Val)
542 * \brief Write a UTF-8 character sequence to a string
544 int WriteUTF8(Uint8 *str, Uint32 Val)
554 *str = 0xC0 | (Val >> 6);
556 *str = 0x80 | (Val & 0x3F);
561 if( Val < 0x10000 ) {
562 *str = 0xE0 | (Val >> 12);
564 *str = 0x80 | ((Val >> 6) & 0x3F);
566 *str = 0x80 | (Val & 0x3F);
571 if( Val < 0x110000 ) {
572 *str = 0xF0 | (Val >> 18);
574 *str = 0x80 | ((Val >> 12) & 0x3F);
576 *str = 0x80 | ((Val >> 6) & 0x3F);
578 *str = 0x80 | (Val & 0x3F);
582 // UTF-8 Doesn't support more than four bytes
587 * \fn Uint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
588 * \brief Converts a date into an Acess Timestamp
590 Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
597 stamp += day*3600*24;
598 stamp += month*DAYS_BEFORE[month]*3600*24;
600 ((year&3) == 0 || year%100 != 0)
601 || (year%100 == 0 && ((year/100)&3) == 0)
602 ) && month > 1) // Leap year and after feb
605 stamp += ((365*4+1) * ((year-2000)&~3)) * 3600*24; // Foour Year Segments
606 stamp += ((year-2000)&3) * 365*3600*24; // Inside four year segment
614 * \brief Pseudo random number generator
615 * \note Unknown effectiveness (made up on the spot)
619 Uint old = giRandomState;
620 // Get the next state value
621 giRandomState = (RANDOM_A*giRandomState + RANDOM_C) & 0xFFFFFFFF;
622 // Check if it has changed, and if it hasn't, change it
623 if(giRandomState == old) giRandomState += RANDOM_SPRUCE;
624 return giRandomState;
627 /// \name Memory Validation
630 * \brief Checks if a string resides fully in valid memory
632 int CheckString(char *String)
635 if( MM_IsUser( (tVAddr)String ) )
640 if( !MM_IsUser( (tVAddr)String ) )
642 // Increment string pointer
647 else if( MM_GetPhysAddr( (tVAddr)String ) )
652 if( !MM_GetPhysAddr( (tVAddr)String ) )
654 // Increment string pointer
663 * \brief Check if a sized memory region is valid memory
665 int CheckMem(void *Mem, int NumBytes)
667 tVAddr addr = (tVAddr)Mem;
669 if( MM_IsUser( addr ) )
673 if( !MM_IsUser( addr ) )
679 else if( MM_GetPhysAddr( addr ) )
683 if( !MM_GetPhysAddr( addr ) )
694 * \brief Search a string array for \a Needle
695 * \note Helper function for eTplDrv_IOCtl::DRV_IOCTL_LOOKUP
697 int ModUtil_LookupString(char **Array, char *Needle)
700 if( !CheckString(Needle) ) return -1;
701 for( i = 0; Array[i]; i++ )
703 if(strcmp(Array[i], Needle) == 0) return i;
708 int ModUtil_SetIdent(char *Dest, char *Value)
710 if( !CheckMem(Dest, 32) ) return -1;
711 strncpy(Dest, Value, 32);