x86_64 fixes
[tpg/acess2.git] / Kernel / lib.c
1 /*
2  * Acess2
3  * Common Library Functions
4  */
5 #include <acess.h>
6
7 // === CONSTANTS ===
8 #define RANDOM_SEED     0xACE55052
9 #define RANDOM_A        0x00731ADE
10 #define RANDOM_C        12345
11 #define RANDOM_SPRUCE   0xf12b039
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
15
16 // === PROTOTYPES ===
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, ...);
21  int    tolower(int c);
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 *File, int Line, const char *Str);
31 char    **str_split(const char *__str, char __ch);
32  int    strpos8(const char *str, Uint32 Search);
33  int    ReadUTF8(Uint8 *str, Uint32 *Val);
34  int    WriteUTF8(Uint8 *str, Uint32 Val);
35  int    DivUp(int num, int dem);
36 Sint64  timestamp(int sec, int mins, int hrs, int day, int month, int year);
37 Uint    rand(void);
38  int    CheckString(char *String);
39  int    CheckMem(void *Mem, int NumBytes);
40  int    ModUtil_LookupString(char **Array, char *Needle);
41  int    ModUtil_SetIdent(char *Dest, char *Value);
42
43 // === EXPORTS ===
44 EXPORT(atoi);
45 EXPORT(itoa);
46 EXPORT(vsnprintf);
47 EXPORT(sprintf);
48 EXPORT(tolower);
49 EXPORT(strucmp);
50 EXPORT(strpos);
51 EXPORT(ByteSum);
52 EXPORT(strlen);
53 EXPORT(strcpy);
54 EXPORT(strncpy);
55 EXPORT(strcmp);
56 EXPORT(strncmp);
57 //EXPORT(strdup);
58 EXPORT(_strdup);        // Takes File/Line too
59 EXPORT(str_split);
60 EXPORT(strpos8);
61 EXPORT(DivUp);
62 EXPORT(ReadUTF8);
63 EXPORT(WriteUTF8);
64 EXPORT(timestamp);
65 EXPORT(CheckString);
66 EXPORT(CheckMem);
67 EXPORT(ModUtil_LookupString);
68 EXPORT(ModUtil_SetIdent);
69
70 // === CODE ===
71 /**
72  * \brief Convert a string into an integer
73  */
74 int atoi(const char *string)
75 {
76          int    ret = 0;
77          int    bNeg = 0;
78         
79         //Log("atoi: (string='%s')", string);
80         
81         // Clear non-numeric characters
82         while( !('0' <= *string && *string <= '9') && *string != '-' )  string++;
83         if( *string == '-' ) {
84                 bNeg = 1;
85                 while( !('0' <= *string && *string <= '9') )    string++;
86         }
87         
88         if(*string == '0')
89         {
90                 string ++;
91                 if(*string == 'x')
92                 {
93                         // Hex
94                         string ++;
95                         for( ;; string ++ )
96                         {
97                                 if('0' <= *string && *string <= '9') {
98                                         ret *= 16;
99                                         ret += *string - '0';
100                                 }
101                                 else if('A' <= *string && *string <= 'F') {
102                                         ret *= 16;
103                                         ret += *string - 'A' + 10;
104                                 }
105                                 else if('a' <= *string && *string <= 'f') {
106                                         ret *= 16;
107                                         ret += *string - 'a' + 10;
108                                 }
109                                 else
110                                         break;
111                         }
112                 }
113                 else    // Octal
114                 {
115                         for( ; '0' <= *string && *string <= '7'; string ++ )
116                         {
117                                 ret *= 8;
118                                 ret += *string - '0';
119                         }
120                 }
121         }
122         else    // Decimal
123         {
124                 for( ; '0' <= *string && *string <= '9'; string++)
125                 {
126                         ret *= 10;
127                         ret += *string - '0';
128                 }
129         }
130         
131         if(bNeg)        ret = -ret;
132         
133         //Log("atoi: RETURN %i", ret);
134         
135         return ret;
136 }
137
138 static const char cUCDIGITS[] = "0123456789ABCDEF";
139 /**
140  * \fn void itoa(char *buf, Uint num, int base, int minLength, char pad)
141  * \brief Convert an integer into a character string
142  */
143 void itoa(char *buf, Uint num, int base, int minLength, char pad)
144 {
145         char    tmpBuf[BITS];
146          int    pos=0, i;
147
148         // Sanity check
149         if(!buf)        return;
150         
151         // Sanity Check
152         if(base > 16 || base < 2) {
153                 buf[0] = 0;
154                 return;
155         }
156         
157         // Convert 
158         while(num > base-1) {
159                 tmpBuf[pos] = cUCDIGITS[ num % base ];
160                 num /= (Uint)base;              // Shift `num` right 1 digit
161                 pos++;
162         }
163         tmpBuf[pos++] = cUCDIGITS[ num % base ];                // Last digit of `num`
164         
165         // Put in reverse
166         i = 0;
167         minLength -= pos;
168         while(minLength-- > 0)  buf[i++] = pad;
169         while(pos-- > 0)                buf[i++] = tmpBuf[pos]; // Reverse the order of characters
170         buf[i] = 0;
171 }
172
173 /**
174  * \brief Append a character the the vsnprintf output
175  */
176 #define PUTCH(c)        do{\
177         char ch=(c);\
178         if(pos==__maxlen){return pos;}\
179         if(__s){__s[pos++]=ch;}else{pos++;}\
180         }while(0)
181 #define GETVAL()        do {\
182         if(isLongLong)  val = va_arg(args, Uint64);\
183         else    val = va_arg(args, unsigned int);\
184         }while(0)
185 /**
186  * \brief VArg String Number Print Formatted
187  */
188 int vsnprintf(char *__s, size_t __maxlen, const char *__format, va_list args)
189 {
190         char    c, pad = ' ';
191          int    minSize = 0, len;
192         char    tmpBuf[34];     // For Integers
193         char    *p = NULL;
194          int    isLongLong = 0;
195         Uint64  val;
196         size_t  pos = 0;
197         // Flags
198          int    bPadLeft = 0;
199         
200         //Log("vsnprintf: (__s=%p, __maxlen=%i, __format='%s', ...)", __s, __maxlen, __format);
201         
202         while((c = *__format++) != 0)
203         {
204                 // Non control character
205                 if(c != '%') { PUTCH(c); continue; }
206                 
207                 c = *__format++;
208                 //Log("pos = %i", pos);
209                 
210                 // Literal %
211                 if(c == '%') { PUTCH('%'); continue; }
212                 
213                 // Pointer - Done first for debugging
214                 if(c == 'p') {
215                         Uint    ptr = va_arg(args, Uint);
216                         PUTCH('*');     PUTCH('0');     PUTCH('x');
217                         p = tmpBuf;
218                         itoa(p, ptr, 16, BITS/4, '0');
219                         goto printString;
220                 }
221                 
222                 // - Padding Side Flag
223                 if(c == '+') {
224                         bPadLeft = 1;
225                         c = *__format++;
226                 }
227                 
228                 // - Padding
229                 if(c == '0') {
230                         pad = '0';
231                         c = *__format++;
232                 }
233                 else
234                         pad = ' ';
235                 
236                 // - Minimum length
237                 if(c == '*') {  // Dynamic length
238                         minSize = va_arg(args, unsigned int);
239                         c = *__format++;
240                 }
241                 else if('1' <= c && c <= '9')
242                 {
243                         minSize = 0;
244                         while('0' <= c && c <= '9')
245                         {
246                                 minSize *= 10;
247                                 minSize += c - '0';
248                                 c = *__format++;
249                         }
250                 }
251                 else
252                         minSize = 1;
253                 
254                 // - Default, Long or LongLong?
255                 isLongLong = 0;
256                 if(c == 'l')    // Long is actually the default on x86
257                 {
258                         c = *__format++;
259                         if(c == 'l') {
260                                 c = *__format++;
261                                 isLongLong = 1;
262                         }
263                 }
264                 
265                 // - Now get the format code
266                 p = tmpBuf;
267                 switch(c)
268                 {
269                 case 'd':
270                 case 'i':
271                         GETVAL();
272                         if( isLongLong && val >> 63 ) {
273                                 PUTCH('-');
274                                 val = -val;
275                         }
276                         else if( !isLongLong && val >> 31 ) {
277                                 PUTCH('-');
278                                 val = -(Sint32)val;
279                         }
280                         itoa(p, val, 10, minSize, pad);
281                         goto printString;
282                 case 'u':
283                         GETVAL();
284                         itoa(p, val, 10, minSize, pad);
285                         goto printString;
286                 case 'X':
287                         #if BITS == 64
288                         isLongLong = 1; // TODO: Handle non-x86 64-bit archs
289                         #endif
290                         GETVAL();
291                         itoa(p, val, 16, minSize, pad);
292                         goto printString;
293                         
294                 case 'x':
295                         GETVAL();
296                         itoa(p, val, 16, minSize, pad);
297                         goto printString;
298                 case 'o':
299                         GETVAL();
300                         itoa(p, val, 8, minSize, pad);
301                         goto printString;
302                 case 'b':
303                         GETVAL();
304                         itoa(p, val, 2, minSize, pad);
305                         goto printString;
306
307                 case 'B':       //Boolean
308                         val = va_arg(args, unsigned int);
309                         if(val) p = "True";
310                         else    p = "False";
311                         goto printString;
312                 
313                 // String - Null Terminated Array
314                 case 's':
315                         p = va_arg(args, char*);        // Get Argument
316                 printString:
317                         if(!p)          p = "(null)";
318                         len = strlen(p);
319                         if( !bPadLeft ) while(len++ < minSize)  PUTCH(pad);
320                         while(*p)       PUTCH(*p++);
321                         if( bPadLeft )  while(len++ < minSize)  PUTCH(pad);
322                         break;
323                 
324                 case 'C':       // Non-Null Terminated Character Array
325                         p = va_arg(args, char*);
326                         if(!p)  goto printString;
327                         while(minSize--)        PUTCH(*p++);
328                         break;
329                 
330                 // Single Character
331                 case 'c':
332                 default:
333                         GETVAL();
334                         PUTCH( (Uint8)val );
335                         break;
336                 }
337         }
338         
339         if(__s && pos != __maxlen)
340                 __s[pos] = '\0';
341         
342         return pos;
343 }
344 #undef PUTCH
345
346 /**
347  */
348 int sprintf(char *__s, const char *__format, ...)
349 {
350         va_list args;
351          int    ret;
352         
353         va_start(args, __format);
354         ret = vsnprintf(__s, -1, __format, args);
355         va_end(args);
356         
357         return ret;
358 }
359
360 /**
361  * \fn int tolower(int c)
362  * \brief Converts a character to lower case
363  */
364 int tolower(int c)
365 {
366         if('A' <= c && c <= 'Z')
367                 return c - 'A' + 'a';
368         return c;
369 }
370
371 /**
372  * \fn int strucmp(const char *Str1, const char *Str2)
373  * \brief Compare \a Str1 and \a Str2 case-insensitively
374  */
375 int strucmp(const char *Str1, const char *Str2)
376 {
377         while(*Str1 && tolower(*Str1) == tolower(*Str2))
378                 Str1++, Str2++;
379         return tolower(*Str1) - tolower(*Str2);
380 }
381
382 /**
383  * \fn int strpos(const char *Str, char Ch)
384  * \brief Search a string for an ascii character
385  */
386 int strpos(const char *Str, char Ch)
387 {
388          int    pos;
389         for(pos=0;Str[pos];pos++)
390         {
391                 if(Str[pos] == Ch)      return pos;
392         }
393         return -1;
394 }
395
396 /**
397  * \fn Uint8 ByteSum(void *Ptr, int Size)
398  * \brief Adds the bytes in a memory region and returns the sum
399  */
400 Uint8 ByteSum(void *Ptr, int Size)
401 {
402         Uint8   sum = 0;
403         while(Size--)   sum += *(Uint8*)Ptr++;
404         return sum;
405 }
406
407 /**
408  * \fn Uint strlen(const char *__str)
409  * \brief Get the length of string
410  */
411 Uint strlen(const char *__str)
412 {
413         Uint    ret = 0;
414         while(*__str++) ret++;
415         return ret;
416 }
417
418 /**
419  * \fn char *strcpy(char *__str1, const char *__str2)
420  * \brief Copy a string to a new location
421  */
422 char *strcpy(char *__str1, const char *__str2)
423 {
424         while(*__str2)
425                 *__str1++ = *__str2++;
426         *__str1 = '\0'; // Terminate String
427         return __str1;
428 }
429
430 /**
431  * \fn char *strncpy(char *__str1, const char *__str2, size_t max)
432  * \brief Copy a string to a new location
433  */
434 char *strncpy(char *__str1, const char *__str2, size_t max)
435 {
436         while(*__str2 && max-- >= 1)
437                 *__str1++ = *__str2++;
438         if(max)
439                 *__str1 = '\0'; // Terminate String
440         return __str1;
441 }
442
443 /**
444  * \fn int strcmp(const char *str1, const char *str2)
445  * \brief Compare two strings return the difference between
446  *        the first non-matching characters.
447  */
448 int strcmp(const char *str1, const char *str2)
449 {
450         while(*str1 && *str1 == *str2)
451                 str1++, str2++;
452         return *str1 - *str2;
453 }
454
455 /**
456  * \fn int strncmp(const char *Str1, const char *Str2, size_t num)
457  * \brief Compare strings \a Str1 and \a Str2 to a maximum of \a num characters
458  */
459 int strncmp(const char *Str1, const char *Str2, size_t num)
460 {
461         if(num == 0)    return 0;       // TODO: Check what should officially happen here
462         while(--num && *Str1 && *Str1 == *Str2)
463                 Str1++, Str2++;
464         return *Str1-*Str2;
465 }
466
467 #if 0
468 /**
469  * \fn char *strdup(const char *Str)
470  * \brief Duplicates a string
471  */
472 char *strdup(const char *Str)
473 {
474         char    *ret;
475         ret = malloc(strlen(Str)+1);
476         if( !ret )      return NULL;
477         strcpy(ret, Str);
478         return ret;
479 }
480 #endif
481
482 /**
483  * \fn char *_strdup(const char *File, int Line, const char *Str)
484  * \brief Duplicates a string
485  */
486 char *_strdup(const char *File, int Line, const char *Str)
487 {
488         char    *ret;
489         ret = Heap_Allocate(File, Line, strlen(Str)+1);
490         if( !ret )      return NULL;
491         strcpy(ret, Str);
492         return ret;
493 }
494
495 /**
496  * \brief Split a string using the passed character
497  * \return NULL terminated array of strings on the heap
498  * \param __str String to split
499  * \param __ch  Character to split by
500  */
501 char **str_split(const char *__str, char __ch)
502 {
503          int    i, j;
504          int    len = 1;
505         char    **ret;
506         char    *start;
507         
508         for( i = 0; __str[i]; i++ )
509         {
510                 if(__str[i] == __ch)
511                         len ++;
512         }
513         
514         ret = malloc( sizeof(char*)*(len+1) + (i + 1) );
515         if( !ret )      return NULL;
516         
517         j = 1;
518         start = (char *)&ret[len+1];
519         ret[0] = start;
520         for( i = 0; __str[i]; i++ )
521         {
522                 if(__str[i] == __ch) {
523                         *start++ = '\0';
524                         ret[j++] = start;
525                 }
526                 else {
527                         *start++ = __str[i]; 
528                 }
529         }
530         *start = '\0';
531         ret[j] = NULL;
532         
533         return ret;
534 }
535
536 /**
537  * \fn int DivUp(int num, int dem)
538  * \brief Divide two numbers, rounding up
539  * \param num   Numerator
540  * \param dem   Denominator
541  */
542 int DivUp(int num, int dem)
543 {
544         return (num+dem-1)/dem;
545 }
546
547 /**
548  * \fn int strpos8(const char *str, Uint32 search)
549  * \brief Search a string for a UTF-8 character
550  */
551 int strpos8(const char *str, Uint32 Search)
552 {
553          int    pos;
554         Uint32  val = 0;
555         for(pos=0;str[pos];pos++)
556         {
557                 // ASCII Range
558                 if(Search < 128) {
559                         if(str[pos] == Search)  return pos;
560                         continue;
561                 }
562                 if(*(Uint8*)(str+pos) < 128)    continue;
563                 
564                 pos += ReadUTF8( (Uint8*)&str[pos], &val );
565                 if(val == Search)       return pos;
566         }
567         return -1;
568 }
569
570 /**
571  * \fn int ReadUTF8(Uint8 *str, Uint32 *Val)
572  * \brief Read a UTF-8 character from a string
573  */
574 int ReadUTF8(Uint8 *str, Uint32 *Val)
575 {
576         *Val = 0xFFFD;  // Assume invalid character
577         
578         // ASCII
579         if( !(*str & 0x80) ) {
580                 *Val = *str;
581                 return 1;
582         }
583         
584         // Middle of a sequence
585         if( (*str & 0xC0) == 0x80 ) {
586                 return 1;
587         }
588         
589         // Two Byte
590         if( (*str & 0xE0) == 0xC0 ) {
591                 *Val = (*str & 0x1F) << 6;      // Upper 6 Bits
592                 str ++;
593                 if( (*str & 0xC0) != 0x80)      return -1;      // Validity check
594                 *Val |= (*str & 0x3F);  // Lower 6 Bits
595                 return 2;
596         }
597         
598         // Three Byte
599         if( (*str & 0xF0) == 0xE0 ) {
600                 *Val = (*str & 0x0F) << 12;     // Upper 4 Bits
601                 str ++;
602                 if( (*str & 0xC0) != 0x80)      return -1;      // Validity check
603                 *Val |= (*str & 0x3F) << 6;     // Middle 6 Bits
604                 str ++;
605                 if( (*str & 0xC0) != 0x80)      return -1;      // Validity check
606                 *Val |= (*str & 0x3F);  // Lower 6 Bits
607                 return 3;
608         }
609         
610         // Four Byte
611         if( (*str & 0xF1) == 0xF0 ) {
612                 *Val = (*str & 0x07) << 18;     // Upper 3 Bits
613                 str ++;
614                 if( (*str & 0xC0) != 0x80)      return -1;      // Validity check
615                 *Val |= (*str & 0x3F) << 12;    // Middle-upper 6 Bits
616                 str ++;
617                 if( (*str & 0xC0) != 0x80)      return -1;      // Validity check
618                 *Val |= (*str & 0x3F) << 6;     // Middle-lower 6 Bits
619                 str ++;
620                 if( (*str & 0xC0) != 0x80)      return -1;      // Validity check
621                 *Val |= (*str & 0x3F);  // Lower 6 Bits
622                 return 4;
623         }
624         
625         // UTF-8 Doesn't support more than four bytes
626         return 4;
627 }
628
629 /**
630  * \fn int WriteUTF8(Uint8 *str, Uint32 Val)
631  * \brief Write a UTF-8 character sequence to a string
632  */
633 int WriteUTF8(Uint8 *str, Uint32 Val)
634 {
635         // ASCII
636         if( Val < 128 ) {
637                 *str = Val;
638                 return 1;
639         }
640         
641         // Two Byte
642         if( Val < 0x8000 ) {
643                 *str = 0xC0 | (Val >> 6);
644                 str ++;
645                 *str = 0x80 | (Val & 0x3F);
646                 return 2;
647         }
648         
649         // Three Byte
650         if( Val < 0x10000 ) {
651                 *str = 0xE0 | (Val >> 12);
652                 str ++;
653                 *str = 0x80 | ((Val >> 6) & 0x3F);
654                 str ++;
655                 *str = 0x80 | (Val & 0x3F);
656                 return 3;
657         }
658         
659         // Four Byte
660         if( Val < 0x110000 ) {
661                 *str = 0xF0 | (Val >> 18);
662                 str ++;
663                 *str = 0x80 | ((Val >> 12) & 0x3F);
664                 str ++;
665                 *str = 0x80 | ((Val >> 6) & 0x3F);
666                 str ++;
667                 *str = 0x80 | (Val & 0x3F);
668                 return 4;
669         }
670         
671         // UTF-8 Doesn't support more than four bytes
672         return 0;
673 }
674
675 /**
676  * \fn Uint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
677  * \brief Converts a date into an Acess Timestamp
678  */
679 Sint64 timestamp(int sec, int mins, int hrs, int day, int month, int year)
680 {
681         Sint64  stamp;
682         stamp = sec;
683         stamp += mins*60;
684         stamp += hrs*3600;
685         
686         stamp += day*3600*24;
687         stamp += month*DAYS_BEFORE[month]*3600*24;
688         if(     (
689                 ((year&3) == 0 || year%100 != 0)
690                 || (year%100 == 0 && ((year/100)&3) == 0)
691                 ) && month > 1) // Leap year and after feb
692                 stamp += 3600*24;
693         
694         stamp += ((365*4+1) * ((year-2000)&~3)) * 3600*24;      // Four Year Segments
695         stamp += ((year-2000)&3) * 365*3600*24; // Inside four year segment
696         stamp += UNIX_TO_2K;
697         
698         return stamp * 1000;
699 }
700
701 /**
702  * \fn Uint rand()
703  * \brief Pseudo random number generator
704  * \note Unknown effectiveness (made up on the spot)
705  */
706 Uint rand(void)
707 {
708         #if 0
709         static Uint     state = RANDOM_SEED;
710         Uint    old = state;
711         // Get the next state value
712         giRandomState = (RANDOM_A*state + RANDOM_C);
713         // Check if it has changed, and if it hasn't, change it
714         if(state == old)        state += RANDOM_SPRUCE;
715         return state;
716         #else
717         // http://en.wikipedia.org/wiki/Xorshift
718         // 2010-10-03
719         static Uint32   x = 123456789;
720         static Uint32   y = 362436069;
721         static Uint32   z = 521288629;
722         static Uint32   w = 88675123; 
723         Uint32  t;
724  
725         t = x ^ (x << 11);
726         x = y; y = z; z = w;
727         return w = w ^ (w >> 19) ^ t ^ (t >> 8); 
728         #endif
729 }
730
731 /* *
732  * \name Memory Validation
733  * \{
734  */
735 /**
736  * \brief Checks if a string resides fully in valid memory
737  */
738 int CheckString(char *String)
739 {
740         if( !MM_GetPhysAddr( (tVAddr)String ) )
741                 return 0;
742         
743         // Check 1st page
744         if( MM_IsUser( (tVAddr)String ) )
745         {
746                 // Traverse String
747                 while( *String )
748                 {
749                         if( !MM_IsUser( (tVAddr)String ) )
750                                 return 0;
751                         // Increment string pointer
752                         String ++;
753                 }
754                 return 1;
755         }
756         else if( MM_GetPhysAddr( (tVAddr)String ) )
757         {
758                 // Traverse String
759                 while( *String )
760                 {
761                         if( !MM_GetPhysAddr( (tVAddr)String ) )
762                                 return 0;
763                         // Increment string pointer
764                         String ++;
765                 }
766                 return 1;
767         }
768         return 0;
769 }
770
771 /**
772  * \brief Check if a sized memory region is valid memory
773  */
774 int CheckMem(void *Mem, int NumBytes)
775 {
776         tVAddr  addr = (tVAddr)Mem;
777         
778         if( MM_IsUser( addr ) )
779         {
780                 while( NumBytes-- )
781                 {
782                         if( !MM_IsUser( addr ) )
783                                 return 0;
784                         addr ++;
785                 }
786                 return 1;
787         }
788         else if( MM_GetPhysAddr( addr ) )
789         {
790                 while( NumBytes-- )
791                 {
792                         if( !MM_GetPhysAddr( addr ) )
793                                 return 0;
794                         addr ++;
795                 }
796                 return 1;
797         }
798         return 0;
799 }
800 /* *
801  * \}
802  */
803
804 /**
805  * \brief Search a string array for \a Needle
806  * \note Helper function for eTplDrv_IOCtl::DRV_IOCTL_LOOKUP
807  */
808 int ModUtil_LookupString(char **Array, char *Needle)
809 {
810          int    i;
811         if( !CheckString(Needle) )      return -1;
812         for( i = 0; Array[i]; i++ )
813         {
814                 if(strcmp(Array[i], Needle) == 0)       return i;
815         }
816         return -1;
817 }
818
819 int ModUtil_SetIdent(char *Dest, char *Value)
820 {
821         if( !CheckMem(Dest, 32) )       return -1;
822         strncpy(Dest, Value, 32);
823         return 1;
824 }

UCC git Repository :: git.ucc.asn.au