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
59 * \fn int tolower(int c)
60 * \brief Converts a character to lower case
64 if('A' <= c && c <= 'Z')
70 * \fn int strucmp(char *Str1, char *Str2)
71 * \brief Compare \a Str1 and \a Str2 case-insensitively
73 int strucmp(const char *Str1, const char *Str2)
75 while(*Str1 && tolower(*Str1) == tolower(*Str2))
77 return tolower(*Str1) - tolower(*Str2);
81 * \fn int strpos(const char *Str, char Ch)
82 * \brief Search a string for an ascii character
84 int strpos(const char *Str, char Ch)
87 for(pos=0;Str[pos];pos++)
89 if(Str[pos] == Ch) return pos;
95 * \fn int ByteSum(void *Ptr, int Size)
96 * \brief Adds the bytes in a memory region and returns the sum
98 int ByteSum(void *Ptr, int Size)
101 while(Size--) sum += *(Uint8*)Ptr++;
106 * \fn Uint strlen(const char *__str)
107 * \brief Get the length of string
109 Uint strlen(const char *__str)
112 while(*__str++) ret++;
117 * \fn char *strcpy(const char *__str1, const char *__str2)
118 * \brief Copy a string to a new location
120 char *strcpy(char *__str1, const char *__str2)
123 *__str1++ = *__str2++;
124 *__str1 = '\0'; // Terminate String
129 * \fn int strcmp(const char *str1, const char *str2)
130 * \brief Compare two strings return the difference between
131 * the first non-matching characters.
133 int strcmp(const char *str1, const char *str2)
135 while(*str1 && *str1 == *str2)
137 return *str1 - *str2;
141 * \fn int strncmp(const char *Str1, const char *Str2, size_t num)
142 * \brief Compare strings \a Str1 and \a Str2 to a maximum of \a num characters
144 int strncmp(const char *Str1, const char *Str2, size_t num)
146 if(num == 0) return 0; // TODO: Check what should officially happen here
147 while(--num && *Str1 && *Str1 == *Str2)
153 * \fn char *strdup(const char *Str)
154 * \brief Duplicates a string
156 char *strdup(const char *Str)
159 ret = malloc(strlen(Str)+1);
165 * \fn int DivUp(int num, int dem)
166 * \brief Divide two numbers, rounding up
167 * \param num Numerator
168 * \param dem Denominator
170 int DivUp(int num, int dem)
172 return (num+dem-1)/dem;
176 * \fn int strpos8(const char *str, Uint32 search)
177 * \brief Search a string for a UTF-8 character
179 int strpos8(const char *str, Uint32 Search)
183 for(pos=0;str[pos];pos++)
187 if(str[pos] == Search) return pos;
190 if(*(Uint8*)(str+pos) < 128) continue;
192 pos += ReadUTF8( (Uint8*)&str[pos], &val );
193 if(val == Search) return pos;
199 * \fn int ReadUTF8(Uint8 *str, Uint32 *Val)
200 * \brief Read a UTF-8 character from a string
202 int ReadUTF8(Uint8 *str, Uint32 *Val)
204 *Val = 0xFFFD; // Assume invalid character
207 if( !(*str & 0x80) ) {
212 // Middle of a sequence
213 if( (*str & 0xC0) == 0x80 ) {
218 if( (*str & 0xE0) == 0xC0 ) {
219 *Val = (*str & 0x1F) << 6; // Upper 6 Bits
221 if( (*str & 0xC0) != 0x80) return -1; // Validity check
222 *Val |= (*str & 0x3F); // Lower 6 Bits
227 if( (*str & 0xF0) == 0xE0 ) {
228 *Val = (*str & 0x0F) << 12; // Upper 4 Bits
230 if( (*str & 0xC0) != 0x80) return -1; // Validity check
231 *Val |= (*str & 0x3F) << 6; // Middle 6 Bits
233 if( (*str & 0xC0) != 0x80) return -1; // Validity check
234 *Val |= (*str & 0x3F); // Lower 6 Bits
239 if( (*str & 0xF1) == 0xF0 ) {
240 *Val = (*str & 0x07) << 18; // Upper 3 Bits
242 if( (*str & 0xC0) != 0x80) return -1; // Validity check
243 *Val |= (*str & 0x3F) << 12; // Middle-upper 6 Bits
245 if( (*str & 0xC0) != 0x80) return -1; // Validity check
246 *Val |= (*str & 0x3F) << 6; // Middle-lower 6 Bits
248 if( (*str & 0xC0) != 0x80) return -1; // Validity check
249 *Val |= (*str & 0x3F); // Lower 6 Bits
253 // UTF-8 Doesn't support more than four bytes
258 * \fn int WriteUTF8(Uint8 *str, Uint32 Val)
259 * \brief Write a UTF-8 character sequence to a string
261 int WriteUTF8(Uint8 *str, Uint32 Val)
271 *str = 0xC0 | (Val >> 6);
273 *str = 0x80 | (Val & 0x3F);
278 if( Val < 0x10000 ) {
279 *str = 0xE0 | (Val >> 12);
281 *str = 0x80 | ((Val >> 6) & 0x3F);
283 *str = 0x80 | (Val & 0x3F);
288 if( Val < 0x110000 ) {
289 *str = 0xF0 | (Val >> 18);
291 *str = 0x80 | ((Val >> 12) & 0x3F);
293 *str = 0x80 | ((Val >> 6) & 0x3F);
295 *str = 0x80 | (Val & 0x3F);
299 // UTF-8 Doesn't support more than four bytes
304 * \fn Uint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
305 * \brief Converts a date into an Acess Timestamp
307 Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
314 stamp += day*3600*24;
315 stamp += month*DAYS_BEFORE[month]*3600*24;
317 ((year&3) == 0 || year%100 != 0)
318 || (year%100 == 0 && ((year/100)&3) == 0)
319 ) && month > 1) // Leap year and after feb
322 stamp += ((365*4+1) * ((year-2000)&~3)) * 3600*24; // Foour Year Segments
323 stamp += ((year-2000)&3) * 365*3600*24; // Inside four year segment
331 * \brief Pseudo random number generator
332 * \note Unknown effectiveness (made up on the spot)
336 Uint old = giRandomState;
337 // Get the next state value
338 giRandomState = (RANDOM_A*giRandomState + RANDOM_C) & 0xFFFFFFFF;
339 // Check if it has changed, and if it hasn't, change it
340 if(giRandomState == old) giRandomState += RANDOM_SPRUCE;
341 return giRandomState;
344 /// \name Memory Validation
347 * \brief Checks if a string resides fully in valid memory
349 int CheckString(char *String)
352 if( MM_IsUser( (tVAddr)String ) )
357 if( !MM_IsUser( (tVAddr)String ) )
359 // Increment string pointer
364 else if( MM_GetPhysAddr( (tVAddr)String ) )
369 if( !MM_GetPhysAddr( (tVAddr)String ) )
371 // Increment string pointer
380 * \brief Check if a sized memory region is valid memory
382 int CheckMem(void *Mem, int NumBytes)
384 tVAddr addr = (tVAddr)Mem;
386 if( MM_IsUser( addr ) )
390 if( !MM_IsUser( addr ) )
396 else if( MM_GetPhysAddr( addr ) )
400 if( !MM_GetPhysAddr( addr ) )
411 * \brief Search a string array for \a Needle
412 * \note Helper function for eTplDrv_IOCtl::DRV_IOCTL_LOOKUP
414 int LookupString(char **Array, char *Needle)
417 for( i = 0; Array[i]; i++ )
419 if(strcmp(Array[i], Needle) == 0) return i;
435 EXPORT(LookupString);