xref: /netbsd-src/sys/external/bsd/gnu-efi/dist/lib/runtime/rtstr.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: rtstr.c,v 1.1.1.2 2018/08/16 18:17:47 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(RtAcquireLock)
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 + Len, '\0', (Len - Size) * sizeof(CHAR16));
77     RtCopyMem(Dest, Src, Size * sizeof(CHAR16));
78 }
79 
80 #ifndef __GNUC__
81 #pragma RUNTIME_CODE(RtStrCpy)
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(RtStrnCpy)
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 + Len, '\0', (Len - Size) * sizeof(CHAR16));
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+StrLen(Dest), Src);
128 }
129 
130 #ifndef __GNUC__
131 #pragma RUNTIME_CODE(RtStrCat)
132 #endif
133 VOID
134 RUNTIMEFUNCTION
135 RtStrnCat (
136     IN CHAR16   *Dest,
137     IN CONST CHAR16   *Src,
138     IN UINTN    Len
139     )
140 {
141     RtStrnCpy(Dest+StrLen(Dest), Src, Len);
142 }
143 
144 #ifndef __GNUC__
145 #pragma RUNTIME_CODE(RtStrLen)
146 #endif
147 UINTN
148 RUNTIMEFUNCTION
149 RtStrLen (
150     IN CONST CHAR16   *s1
151     )
152 // string length
153 {
154     UINTN        len;
155 
156     for (len=0; *s1; s1+=1, len+=1) ;
157     return len;
158 }
159 
160 #ifndef __GNUC__
161 #pragma RUNTIME_CODE(RtStrnLen)
162 #endif
163 UINTN
164 RUNTIMEFUNCTION
165 RtStrnLen (
166     IN CONST CHAR16   *s1,
167     IN UINTN           Len
168     )
169 // copy strings
170 {
171     UINTN i;
172     for (i = 0; *s1 && i < Len; i++)
173 	    s1++;
174     return i;
175 }
176 
177 #ifndef __GNUC__
178 #pragma RUNTIME_CODE(RtStrSize)
179 #endif
180 UINTN
181 RUNTIMEFUNCTION
182 RtStrSize (
183     IN CONST CHAR16   *s1
184     )
185 // string size
186 {
187     UINTN        len;
188 
189     for (len=0; *s1; s1+=1, len+=1) ;
190     return (len + 1) * sizeof(CHAR16);
191 }
192 
193 #ifndef __GNUC__
194 #pragma RUNTIME_CODE(RtBCDtoDecimal)
195 #endif
196 UINT8
197 RUNTIMEFUNCTION
198 RtBCDtoDecimal(
199     IN  UINT8 BcdValue
200     )
201 {
202     UINTN   High, Low;
203 
204     High    = BcdValue >> 4;
205     Low     = BcdValue - (High << 4);
206 
207     return ((UINT8)(Low + (High * 10)));
208 }
209 
210 
211 #ifndef __GNUC__
212 #pragma RUNTIME_CODE(RtDecimaltoBCD)
213 #endif
214 UINT8
215 RUNTIMEFUNCTION
216 RtDecimaltoBCD (
217     IN  UINT8 DecValue
218     )
219 {
220     UINTN   High, Low;
221 
222     High    = DecValue / 10;
223     Low     = DecValue - (High * 10);
224 
225     return ((UINT8)(Low + (High << 4)));
226 }
227 
228 
229