Home | History | Annotate | Line # | Download | only in lib
str.c revision 1.1.1.1
      1 /*	$NetBSD: str.c,v 1.1.1.1 2014/04/01 16:16:06 jakllsch Exp $	*/
      2 
      3 /*++
      4 
      5 Copyright (c) 1998  Intel Corporation
      6 
      7 Module Name:
      8 
      9     str.c
     10 
     11 Abstract:
     12 
     13 
     14 
     15 
     16 Revision History
     17 
     18 --*/
     19 
     20 #include "lib.h"
     21 
     22 
     23 INTN
     24 StrCmp (
     25     IN CHAR16   *s1,
     26     IN CHAR16   *s2
     27     )
     28 // compare strings
     29 {
     30     return RtStrCmp(s1, s2);
     31 }
     32 
     33 INTN
     34 StrnCmp (
     35     IN CHAR16   *s1,
     36     IN CHAR16   *s2,
     37     IN UINTN    len
     38     )
     39 // compare strings
     40 {
     41     while (*s1  &&  len) {
     42         if (*s1 != *s2) {
     43             break;
     44         }
     45 
     46         s1  += 1;
     47         s2  += 1;
     48         len -= 1;
     49     }
     50 
     51     return len ? *s1 - *s2 : 0;
     52 }
     53 
     54 
     55 INTN EFIAPI
     56 LibStubStriCmp (
     57     IN EFI_UNICODE_COLLATION_INTERFACE  *This,
     58     IN CHAR16                           *s1,
     59     IN CHAR16                           *s2
     60     )
     61 {
     62     return StrCmp (s1, s2);
     63 }
     64 
     65 VOID EFIAPI
     66 LibStubStrLwrUpr (
     67     IN EFI_UNICODE_COLLATION_INTERFACE  *This,
     68     IN CHAR16                           *Str
     69     )
     70 {
     71 }
     72 
     73 INTN
     74 StriCmp (
     75     IN CHAR16   *s1,
     76     IN CHAR16   *s2
     77     )
     78 // compare strings
     79 {
     80     if (UnicodeInterface == &LibStubUnicodeInterface)
     81     	return UnicodeInterface->StriColl(UnicodeInterface, s1, s2);
     82     else
     83 	return uefi_call_wrapper(UnicodeInterface->StriColl, 3, UnicodeInterface, s1, s2);
     84 }
     85 
     86 VOID
     87 StrLwr (
     88     IN CHAR16   *Str
     89     )
     90 // lwoer case string
     91 {
     92     if (UnicodeInterface == &LibStubUnicodeInterface)
     93     	UnicodeInterface->StrLwr(UnicodeInterface, Str);
     94     else uefi_call_wrapper(UnicodeInterface->StrLwr, 2, UnicodeInterface, Str);
     95 }
     96 
     97 VOID
     98 StrUpr (
     99     IN CHAR16   *Str
    100     )
    101 // upper case string
    102 {
    103     if (UnicodeInterface == &LibStubUnicodeInterface)
    104         UnicodeInterface->StrUpr(UnicodeInterface, Str);
    105     else uefi_call_wrapper(UnicodeInterface->StrUpr, 2, UnicodeInterface, Str);
    106 }
    107 
    108 VOID
    109 StrCpy (
    110     IN CHAR16   *Dest,
    111     IN CHAR16   *Src
    112     )
    113 // copy strings
    114 {
    115     RtStrCpy (Dest, Src);
    116 }
    117 
    118 VOID
    119 StrCat (
    120     IN CHAR16   *Dest,
    121     IN CHAR16   *Src
    122     )
    123 {
    124     RtStrCat(Dest, Src);
    125 }
    126 
    127 UINTN
    128 StrLen (
    129     IN CHAR16   *s1
    130     )
    131 // string length
    132 {
    133     return RtStrLen(s1);
    134 }
    135 
    136 UINTN
    137 StrSize (
    138     IN CHAR16   *s1
    139     )
    140 // string size
    141 {
    142     return RtStrSize(s1);
    143 }
    144 
    145 CHAR16 *
    146 StrDuplicate (
    147     IN CHAR16   *Src
    148     )
    149 // duplicate a string
    150 {
    151     CHAR16      *Dest;
    152     UINTN       Size;
    153 
    154     Size = StrSize(Src);
    155     Dest = AllocatePool (Size);
    156     if (Dest) {
    157         CopyMem (Dest, Src, Size);
    158     }
    159     return Dest;
    160 }
    161 
    162 UINTN
    163 strlena (
    164     IN CHAR8    *s1
    165     )
    166 // string length
    167 {
    168     UINTN        len;
    169 
    170     for (len=0; *s1; s1+=1, len+=1) ;
    171     return len;
    172 }
    173 
    174 UINTN
    175 strcmpa (
    176     IN CHAR8    *s1,
    177     IN CHAR8    *s2
    178     )
    179 // compare strings
    180 {
    181     while (*s1) {
    182         if (*s1 != *s2) {
    183             break;
    184         }
    185 
    186         s1 += 1;
    187         s2 += 1;
    188     }
    189 
    190     return *s1 - *s2;
    191 }
    192 
    193 UINTN
    194 strncmpa (
    195     IN CHAR8    *s1,
    196     IN CHAR8    *s2,
    197     IN UINTN    len
    198     )
    199 // compare strings
    200 {
    201     while (*s1  &&  len) {
    202         if (*s1 != *s2) {
    203             break;
    204         }
    205 
    206         s1  += 1;
    207         s2  += 1;
    208         len -= 1;
    209     }
    210 
    211     return len ? *s1 - *s2 : 0;
    212 }
    213 
    214 
    215 
    216 UINTN
    217 xtoi (
    218     CHAR16  *str
    219     )
    220 // convert hex string to uint
    221 {
    222     UINTN       u;
    223     CHAR16      c;
    224 
    225     // skip preceeding white space
    226     while (*str && *str == ' ') {
    227         str += 1;
    228     }
    229 
    230     // convert hex digits
    231     u = 0;
    232     while ((c = *(str++))) {
    233         if (c >= 'a'  &&  c <= 'f') {
    234             c -= 'a' - 'A';
    235         }
    236 
    237         if ((c >= '0'  &&  c <= '9')  ||  (c >= 'A'  &&  c <= 'F')) {
    238             u = (u << 4)  |  (c - (c >= 'A' ? 'A'-10 : '0'));
    239         } else {
    240             break;
    241         }
    242     }
    243 
    244     return u;
    245 }
    246 
    247 UINTN
    248 Atoi (
    249     CHAR16  *str
    250     )
    251 // convert hex string to uint
    252 {
    253     UINTN       u;
    254     CHAR16      c;
    255 
    256     // skip preceeding white space
    257     while (*str && *str == ' ') {
    258         str += 1;
    259     }
    260 
    261     // convert digits
    262     u = 0;
    263     while ((c = *(str++))) {
    264         if (c >= '0' && c <= '9') {
    265             u = (u * 10) + c - '0';
    266         } else {
    267             break;
    268         }
    269     }
    270 
    271     return u;
    272 }
    273 
    274 BOOLEAN
    275 MetaMatch (
    276     IN CHAR16   *String,
    277     IN CHAR16   *Pattern
    278     )
    279 {
    280     CHAR16  c, p, l;
    281 
    282     for (; ;) {
    283         p = *Pattern;
    284         Pattern += 1;
    285 
    286         switch (p) {
    287         case 0:
    288             // End of pattern.  If end of string, TRUE match
    289             return *String ? FALSE : TRUE;
    290 
    291         case '*':
    292             // Match zero or more chars
    293             while (*String) {
    294                 if (MetaMatch (String, Pattern)) {
    295                     return TRUE;
    296                 }
    297                 String += 1;
    298             }
    299             return MetaMatch (String, Pattern);
    300 
    301         case '?':
    302             // Match any one char
    303             if (!*String) {
    304                 return FALSE;
    305             }
    306             String += 1;
    307             break;
    308 
    309         case '[':
    310             // Match char set
    311             c = *String;
    312             if (!c) {
    313                 return FALSE;                       // syntax problem
    314             }
    315 
    316             l = 0;
    317             while ((p = *Pattern++)) {
    318                 if (p == ']') {
    319                     return FALSE;
    320                 }
    321 
    322                 if (p == '-') {                     // if range of chars,
    323                     p = *Pattern;                   // get high range
    324                     if (p == 0 || p == ']') {
    325                         return FALSE;               // syntax problem
    326                     }
    327 
    328                     if (c >= l && c <= p) {         // if in range,
    329                         break;                      // it's a match
    330                     }
    331                 }
    332 
    333                 l = p;
    334                 if (c == p) {                       // if char matches
    335                     break;                          // move on
    336                 }
    337             }
    338 
    339             // skip to end of match char set
    340             while (p && p != ']') {
    341                 p = *Pattern;
    342                 Pattern += 1;
    343             }
    344 
    345             String += 1;
    346             break;
    347 
    348         default:
    349             c = *String;
    350             if (c != p) {
    351                 return FALSE;
    352             }
    353 
    354             String += 1;
    355             break;
    356         }
    357     }
    358 }
    359 
    360 
    361 BOOLEAN EFIAPI
    362 LibStubMetaiMatch (
    363     IN EFI_UNICODE_COLLATION_INTERFACE  *This,
    364     IN CHAR16                           *String,
    365     IN CHAR16                           *Pattern
    366     )
    367 {
    368     return MetaMatch (String, Pattern);
    369 }
    370 
    371 
    372 BOOLEAN
    373 MetaiMatch (
    374     IN CHAR16   *String,
    375     IN CHAR16   *Pattern
    376     )
    377 {
    378     if (UnicodeInterface == &LibStubUnicodeInterface)
    379     	return UnicodeInterface->MetaiMatch(UnicodeInterface, String, Pattern);
    380     else return uefi_call_wrapper(UnicodeInterface->MetaiMatch, 3, UnicodeInterface, String, Pattern);
    381 }
    382