xref: /DragonStub/lib/runtime/rtstr.c (revision b95f138fa1550040e68ac3dd34cfce7d148d7984)
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(RtStrCmp)
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 + Size, (Len - Size) * sizeof(CHAR16), '\0');
75     RtCopyMem(Dest, Src, Size * sizeof(CHAR16));
76 }
77 
78 #ifndef __GNUC__
79 #pragma RUNTIME_CODE(RtStpCpy)
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(RtStpnCpy)
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 + Size, (Len - Size) * sizeof(CHAR16), '\0');
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+RtStrLen(Dest), Src);
126 }
127 
128 #ifndef __GNUC__
129 #pragma RUNTIME_CODE(RtStrnCat)
130 #endif
131 VOID
132 RUNTIMEFUNCTION
133 RtStrnCat (
134     IN CHAR16   *Dest,
135     IN CONST CHAR16   *Src,
136     IN UINTN    Len
137     )
138 {
139     UINTN DestSize, Size;
140 
141     DestSize = RtStrLen(Dest);
142     Size = RtStrnLen(Src, Len);
143     RtCopyMem(Dest + DestSize, Src, Size * sizeof(CHAR16));
144     Dest[DestSize + Size] = '\0';
145 }
146 
147 #ifndef __GNUC__
148 #pragma RUNTIME_CODE(RtStrLen)
149 #endif
150 UINTN
151 RUNTIMEFUNCTION
152 RtStrLen (
153     IN CONST CHAR16   *s1
154     )
155 // string length
156 {
157     UINTN        len;
158 
159     for (len=0; *s1; s1+=1, len+=1) ;
160     return len;
161 }
162 
163 #ifndef __GNUC__
164 #pragma RUNTIME_CODE(RtStrnLen)
165 #endif
166 UINTN
167 RUNTIMEFUNCTION
168 RtStrnLen (
169     IN CONST CHAR16   *s1,
170     IN UINTN           Len
171     )
172 // string length
173 {
174     UINTN i;
175     for (i = 0; *s1 && i < Len; i++)
176         s1++;
177     return i;
178 }
179 
180 #ifndef __GNUC__
181 #pragma RUNTIME_CODE(RtStrSize)
182 #endif
183 UINTN
184 RUNTIMEFUNCTION
185 RtStrSize (
186     IN CONST CHAR16   *s1
187     )
188 // string size
189 {
190     UINTN        len;
191 
192     for (len=0; *s1; s1+=1, len+=1) ;
193     return (len + 1) * sizeof(CHAR16);
194 }
195 
196 #ifndef __GNUC__
197 #pragma RUNTIME_CODE(RtBCDtoDecimal)
198 #endif
199 UINT8
200 RUNTIMEFUNCTION
201 RtBCDtoDecimal(
202     IN  UINT8 BcdValue
203     )
204 {
205     UINTN   High, Low;
206 
207     High    = BcdValue >> 4;
208     Low     = BcdValue - (High << 4);
209 
210     return ((UINT8)(Low + (High * 10)));
211 }
212 
213 
214 #ifndef __GNUC__
215 #pragma RUNTIME_CODE(RtDecimaltoBCD)
216 #endif
217 UINT8
218 RUNTIMEFUNCTION
219 RtDecimaltoBCD (
220     IN  UINT8 DecValue
221     )
222 {
223     UINTN   High, Low;
224 
225     High    = DecValue / 10;
226     Low     = DecValue - (High * 10);
227 
228     return ((UINT8)(Low + (High << 4)));
229 }
230 
231 
232