Home | History | Annotate | Line # | Download | only in runtime
      1 /*	$NetBSD: rtstr.c,v 1.1.1.3 2021/09/30 18:50:09 jmcneill Exp $	*/
      2 
      3 /*++
      4 
      5 Copyright (c) 1998  Intel Corporation
      6 
      7 Module Name:
      8 
      9     str.c
     10 
     11 Abstract:
     12 
     13     String runtime functions
     14 
     15 
     16 Revision History
     17 
     18 --*/
     19 
     20 #include "lib.h"
     21 
     22 #ifndef __GNUC__
     23 #pragma RUNTIME_CODE(RtStrCmp)
     24 #endif
     25 INTN
     26 RUNTIMEFUNCTION
     27 RtStrCmp (
     28     IN CONST CHAR16   *s1,
     29     IN CONST CHAR16   *s2
     30     )
     31 // compare strings
     32 {
     33     while (*s1) {
     34         if (*s1 != *s2) {
     35             break;
     36         }
     37 
     38         s1 += 1;
     39         s2 += 1;
     40     }
     41 
     42     return *s1 - *s2;
     43 }
     44 
     45 #ifndef __GNUC__
     46 #pragma RUNTIME_CODE(RtStrCpy)
     47 #endif
     48 VOID
     49 RUNTIMEFUNCTION
     50 RtStrCpy (
     51     IN CHAR16   *Dest,
     52     IN CONST CHAR16   *Src
     53     )
     54 // copy strings
     55 {
     56     while (*Src) {
     57         *(Dest++) = *(Src++);
     58     }
     59     *Dest = 0;
     60 }
     61 
     62 #ifndef __GNUC__
     63 #pragma RUNTIME_CODE(RtStrnCpy)
     64 #endif
     65 VOID
     66 RUNTIMEFUNCTION
     67 RtStrnCpy (
     68     IN CHAR16   *Dest,
     69     IN CONST CHAR16   *Src,
     70     IN UINTN     Len
     71     )
     72 // copy strings
     73 {
     74     UINTN Size = RtStrnLen(Src, Len);
     75     if (Size != Len)
     76         RtSetMem(Dest + Size, (Len - Size) * sizeof(CHAR16), '\0');
     77     RtCopyMem(Dest, Src, Size * sizeof(CHAR16));
     78 }
     79 
     80 #ifndef __GNUC__
     81 #pragma RUNTIME_CODE(RtStpCpy)
     82 #endif
     83 CHAR16 *
     84 RUNTIMEFUNCTION
     85 RtStpCpy (
     86     IN CHAR16   *Dest,
     87     IN CONST CHAR16   *Src
     88     )
     89 // copy strings
     90 {
     91     while (*Src) {
     92         *(Dest++) = *(Src++);
     93     }
     94     *Dest = 0;
     95     return Dest;
     96 }
     97 
     98 #ifndef __GNUC__
     99 #pragma RUNTIME_CODE(RtStpnCpy)
    100 #endif
    101 CHAR16 *
    102 RUNTIMEFUNCTION
    103 RtStpnCpy (
    104     IN CHAR16   *Dest,
    105     IN CONST CHAR16   *Src,
    106     IN UINTN     Len
    107     )
    108 // copy strings
    109 {
    110     UINTN Size = RtStrnLen(Src, Len);
    111     if (Size != Len)
    112         RtSetMem(Dest + Size, (Len - Size) * sizeof(CHAR16), '\0');
    113     RtCopyMem(Dest, Src, Size * sizeof(CHAR16));
    114     return Dest + Size;
    115 }
    116 
    117 #ifndef __GNUC__
    118 #pragma RUNTIME_CODE(RtStrCat)
    119 #endif
    120 VOID
    121 RUNTIMEFUNCTION
    122 RtStrCat (
    123     IN CHAR16   *Dest,
    124     IN CONST CHAR16   *Src
    125     )
    126 {
    127     RtStrCpy(Dest+RtStrLen(Dest), Src);
    128 }
    129 
    130 #ifndef __GNUC__
    131 #pragma RUNTIME_CODE(RtStrnCat)
    132 #endif
    133 VOID
    134 RUNTIMEFUNCTION
    135 RtStrnCat (
    136     IN CHAR16   *Dest,
    137     IN CONST CHAR16   *Src,
    138     IN UINTN    Len
    139     )
    140 {
    141     UINTN DestSize, Size;
    142 
    143     DestSize = RtStrLen(Dest);
    144     Size = RtStrnLen(Src, Len);
    145     RtCopyMem(Dest + DestSize, Src, Size * sizeof(CHAR16));
    146     Dest[DestSize + Size] = '\0';
    147 }
    148 
    149 #ifndef __GNUC__
    150 #pragma RUNTIME_CODE(RtStrLen)
    151 #endif
    152 UINTN
    153 RUNTIMEFUNCTION
    154 RtStrLen (
    155     IN CONST CHAR16   *s1
    156     )
    157 // string length
    158 {
    159     UINTN        len;
    160 
    161     for (len=0; *s1; s1+=1, len+=1) ;
    162     return len;
    163 }
    164 
    165 #ifndef __GNUC__
    166 #pragma RUNTIME_CODE(RtStrnLen)
    167 #endif
    168 UINTN
    169 RUNTIMEFUNCTION
    170 RtStrnLen (
    171     IN CONST CHAR16   *s1,
    172     IN UINTN           Len
    173     )
    174 // string length
    175 {
    176     UINTN i;
    177     for (i = 0; *s1 && i < Len; i++)
    178         s1++;
    179     return i;
    180 }
    181 
    182 #ifndef __GNUC__
    183 #pragma RUNTIME_CODE(RtStrSize)
    184 #endif
    185 UINTN
    186 RUNTIMEFUNCTION
    187 RtStrSize (
    188     IN CONST CHAR16   *s1
    189     )
    190 // string size
    191 {
    192     UINTN        len;
    193 
    194     for (len=0; *s1; s1+=1, len+=1) ;
    195     return (len + 1) * sizeof(CHAR16);
    196 }
    197 
    198 #ifndef __GNUC__
    199 #pragma RUNTIME_CODE(RtBCDtoDecimal)
    200 #endif
    201 UINT8
    202 RUNTIMEFUNCTION
    203 RtBCDtoDecimal(
    204     IN  UINT8 BcdValue
    205     )
    206 {
    207     UINTN   High, Low;
    208 
    209     High    = BcdValue >> 4;
    210     Low     = BcdValue - (High << 4);
    211 
    212     return ((UINT8)(Low + (High * 10)));
    213 }
    214 
    215 
    216 #ifndef __GNUC__
    217 #pragma RUNTIME_CODE(RtDecimaltoBCD)
    218 #endif
    219 UINT8
    220 RUNTIMEFUNCTION
    221 RtDecimaltoBCD (
    222     IN  UINT8 DecValue
    223     )
    224 {
    225     UINTN   High, Low;
    226 
    227     High    = DecValue / 10;
    228     Low     = DecValue - (High * 10);
    229 
    230     return ((UINT8)(Low + (High << 4)));
    231 }
    232 
    233 
    234