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