878ddf1f |
1 | /** @file\r |
2 | Unicode string primatives.\r |
3 | \r |
4 | Copyright (c) 2006, Intel Corporation<BR>\r |
5 | All rights reserved. This program and the accompanying materials\r |
6 | are licensed and made available under the terms and conditions of the BSD License\r |
7 | which accompanies this distribution. The full text of the license may be found at\r |
8 | http://opensource.org/licenses/bsd-license.php\r |
9 | \r |
10 | THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r |
11 | WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r |
12 | \r |
13 | Module Name: String.c\r |
14 | \r |
15 | **/\r |
16 | \r |
17 | /**\r |
18 | Copies one Null-terminated Unicode string to another Null-terminated Unicode\r |
19 | string and returns the new Unicode string.\r |
20 | \r |
21 | This function copies the contents of the Unicode string Source to the Unicode\r |
22 | string Destination, and returns Destination. If Source and Destination\r |
23 | overlap, then the results are undefined.\r |
24 | \r |
25 | If Destination is NULL, then ASSERT().\r |
26 | If Source is NULL, then ASSERT().\r |
27 | If Source and Destination overlap, then ASSERT().\r |
28 | If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r |
29 | PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r |
30 | \r |
31 | @param Destination Pointer to a Null-terminated Unicode string.\r |
32 | @param Source Pointer to a Null-terminated Unicode string.\r |
33 | \r |
34 | @return Destiantion\r |
35 | \r |
36 | **/\r |
37 | CHAR16 *\r |
38 | EFIAPI\r |
39 | StrCpy (\r |
40 | OUT CHAR16 *Destination,\r |
41 | IN CONST CHAR16 *Source\r |
42 | )\r |
43 | {\r |
44 | CHAR16 *ReturnValue;\r |
45 | \r |
46 | //\r |
47 | // Destination cannot be NULL\r |
48 | //\r |
49 | ASSERT (Destination != NULL);\r |
50 | \r |
51 | //\r |
52 | // Destination and source cannot overlap\r |
53 | //\r |
54 | ASSERT ((UINTN)(Destination - Source) > StrLen (Source));\r |
55 | ASSERT ((UINTN)(Source - Destination) > StrLen (Source));\r |
56 | \r |
57 | ReturnValue = Destination;\r |
58 | while (*Source) {\r |
59 | *(Destination++) = *(Source++);\r |
60 | }\r |
61 | *Destination = 0;\r |
62 | return ReturnValue;\r |
63 | }\r |
64 | \r |
65 | /**\r |
66 | Copies one Null-terminated Unicode string with a maximum length to another\r |
67 | Null-terminated Unicode string with a maximum length and returns the new\r |
68 | Unicode string.\r |
69 | \r |
70 | This function copies the contents of the Unicode string Source to the Unicode\r |
71 | string Destination, and returns Destination. At most, Length Unicode\r |
72 | characters are copied from Source to Destination. If Length is 0, then\r |
73 | Destination is returned unmodified. If Length is greater that the number of\r |
74 | Unicode characters in Source, then Destination is padded with Null Unicode\r |
75 | characters. If Source and Destination overlap, then the results are\r |
76 | undefined.\r |
77 | \r |
78 | If Destination is NULL, then ASSERT().\r |
79 | If Source is NULL, then ASSERT().\r |
80 | If Source and Destination overlap, then ASSERT().\r |
81 | If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r |
82 | PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r |
83 | \r |
84 | @param Destination Pointer to a Null-terminated Unicode string.\r |
85 | @param Source Pointer to a Null-terminated Unicode string.\r |
86 | @param Length Maximum number of Unicode characters to copy.\r |
87 | \r |
88 | @return Destination\r |
89 | \r |
90 | **/\r |
91 | CHAR16 *\r |
92 | EFIAPI\r |
93 | StrnCpy (\r |
94 | OUT CHAR16 *Destination,\r |
95 | IN CONST CHAR16 *Source,\r |
96 | IN UINTN Length\r |
97 | )\r |
98 | {\r |
99 | CHAR16 *ReturnValue;\r |
100 | \r |
101 | if (Length == 0) {\r |
102 | return Destination;\r |
103 | }\r |
104 | \r |
105 | //\r |
106 | // Destination cannot be NULL if Length is not zero\r |
107 | //\r |
108 | ASSERT (Destination != NULL);\r |
109 | \r |
110 | //\r |
111 | // Destination and source cannot overlap\r |
112 | // Q: Does Source have to be NULL-terminated?\r |
113 | //\r |
114 | ASSERT ((UINTN)(Destination - Source) > StrLen (Source));\r |
115 | ASSERT ((UINTN)(Source - Destination) >= Length);\r |
116 | \r |
117 | ReturnValue = Destination;\r |
118 | \r |
6cfb0c24 |
119 | while ((*Source != L'\0') && (Length > 0)) {\r |
878ddf1f |
120 | *(Destination++) = *(Source++);\r |
121 | Length--;\r |
122 | }\r |
123 | \r |
124 | ZeroMem (Destination, Length * sizeof (*Destination));\r |
125 | return ReturnValue;\r |
126 | }\r |
127 | \r |
128 | /**\r |
129 | Returns the length of a Null-terminated Unicode string.\r |
130 | \r |
131 | This function returns the number of Unicode characters in the Null-terminated\r |
132 | Unicode string specified by String.\r |
133 | \r |
134 | If String is NULL, then ASSERT().\r |
135 | If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r |
136 | PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r |
137 | \r |
138 | @param String Pointer to a Null-terminated Unicode string.\r |
139 | \r |
140 | @return The length of String.\r |
141 | \r |
142 | **/\r |
143 | UINTN\r |
144 | EFIAPI\r |
145 | StrLen (\r |
146 | IN CONST CHAR16 *String\r |
147 | )\r |
148 | {\r |
149 | UINTN Length;\r |
150 | \r |
151 | ASSERT (String != NULL);\r |
152 | \r |
153 | for (Length = 0; *String != L'\0'; String++, Length++) {\r |
154 | //\r |
155 | // If PcdMaximumUnicodeStringLength is not zero,\r |
156 | // length should not more than PcdMaximumUnicodeStringLength\r |
157 | //\r |
dc530c7b |
158 | if (PcdGet32 (PcdMaximumUnicodeStringLength) != 0) {\r |
159 | ASSERT (Length < PcdGet32 (PcdMaximumUnicodeStringLength));\r |
878ddf1f |
160 | }\r |
161 | }\r |
162 | return Length;\r |
163 | }\r |
164 | \r |
165 | /**\r |
166 | Returns the size of a Null-terminated Unicode string in bytes, including the\r |
167 | Null terminator.\r |
168 | \r |
169 | This function returns the size, in bytes, of the Null-terminated Unicode\r |
170 | string specified by String.\r |
171 | \r |
172 | If String is NULL, then ASSERT().\r |
173 | If PcdMaximumUnicodeStringLength is not zero, and String contains more than\r |
174 | PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r |
175 | \r |
176 | @param String Pointer to a Null-terminated Unicode string.\r |
177 | \r |
178 | @return The size of String.\r |
179 | \r |
180 | **/\r |
181 | UINTN\r |
182 | EFIAPI\r |
183 | StrSize (\r |
184 | IN CONST CHAR16 *String\r |
185 | )\r |
186 | {\r |
187 | return (StrLen (String) + 1) * sizeof (*String);\r |
188 | }\r |
189 | \r |
190 | /**\r |
191 | Compares two Null-terminated Unicode strings, and returns the difference\r |
192 | between the first mismatched Unicode characters.\r |
193 | \r |
194 | This function compares the Null-terminated Unicode string FirstString to the\r |
195 | Null-terminated Unicode string SecondString. If FirstString is identical to\r |
196 | SecondString, then 0 is returned. Otherwise, the value returned is the first\r |
197 | mismatched Unicode character in SecondString subtracted from the first\r |
198 | mismatched Unicode character in FirstString.\r |
199 | \r |
200 | If FirstString is NULL, then ASSERT().\r |
201 | If SecondString is NULL, then ASSERT().\r |
202 | If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more\r |
203 | than PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r |
204 | If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more\r |
205 | than PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r |
206 | \r |
207 | @param FirstString Pointer to a Null-terminated Unicode string.\r |
208 | @param SecondString Pointer to a Null-terminated Unicode string.\r |
209 | \r |
210 | @retval 0 FirstString is identical to SecondString.\r |
211 | @retval !=0 FirstString is not identical to SecondString.\r |
212 | \r |
213 | **/\r |
214 | INTN\r |
215 | EFIAPI\r |
216 | StrCmp (\r |
217 | IN CONST CHAR16 *FirstString,\r |
218 | IN CONST CHAR16 *SecondString\r |
219 | )\r |
220 | {\r |
221 | //\r |
222 | // ASSERT both strings are less long than PcdMaximumUnicodeStringLength\r |
223 | //\r |
224 | ASSERT (StrSize (FirstString) != 0);\r |
225 | ASSERT (StrSize (SecondString) != 0);\r |
226 | \r |
227 | while ((*FirstString != L'\0') && (*FirstString == *SecondString)) {\r |
228 | FirstString++;\r |
229 | SecondString++;\r |
230 | }\r |
231 | return *FirstString - *SecondString;\r |
232 | }\r |
233 | \r |
234 | /**\r |
235 | Compares two Null-terminated Unicode strings with maximum lengths, and\r |
236 | returns the difference between the first mismatched Unicode characters.\r |
237 | \r |
238 | This function compares the Null-terminated Unicode string FirstString to the\r |
239 | Null-terminated Unicode string SecondString. At most, Length Unicode\r |
240 | characters will be compared. If Length is 0, then 0 is returned. If\r |
241 | FirstString is identical to SecondString, then 0 is returned. Otherwise, the\r |
242 | value returned is the first mismatched Unicode character in SecondString\r |
243 | subtracted from the first mismatched Unicode character in FirstString.\r |
244 | \r |
245 | If FirstString is NULL, then ASSERT().\r |
246 | If SecondString is NULL, then ASSERT().\r |
247 | If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more\r |
248 | than PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r |
249 | If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more\r |
250 | than PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r |
251 | \r |
252 | @param FirstString Pointer to a Null-terminated Unicode string.\r |
253 | @param SecondString Pointer to a Null-terminated Unicode string.\r |
254 | @param Length Maximum number of Unicode characters to compare.\r |
255 | \r |
256 | @retval 0 FirstString is identical to SecondString.\r |
257 | @retval !=0 FirstString is not identical to SecondString.\r |
258 | \r |
259 | **/\r |
260 | INTN\r |
261 | EFIAPI\r |
262 | StrnCmp (\r |
263 | IN CONST CHAR16 *FirstString,\r |
264 | IN CONST CHAR16 *SecondString,\r |
265 | IN UINTN Length\r |
266 | )\r |
267 | {\r |
268 | if (Length == 0) {\r |
269 | return 0;\r |
270 | }\r |
271 | \r |
272 | //\r |
273 | // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.\r |
274 | // Length tests are performed inside StrLen().\r |
275 | //\r |
276 | ASSERT (StrSize (FirstString) != 0);\r |
277 | ASSERT (StrSize (SecondString) != 0);\r |
278 | \r |
279 | while ((*FirstString != L'\0') &&\r |
267f7f10 |
280 | (*FirstString == *SecondString) &&\r |
878ddf1f |
281 | (Length > 1)) {\r |
282 | FirstString++;\r |
283 | SecondString++;\r |
284 | Length--;\r |
285 | }\r |
286 | \r |
287 | return *FirstString - *SecondString;\r |
288 | }\r |
289 | \r |
290 | /**\r |
291 | Concatenates one Null-terminated Unicode string to another Null-terminated\r |
292 | Unicode string, and returns the concatenated Unicode string.\r |
293 | \r |
294 | This function concatenates two Null-terminated Unicode strings. The contents\r |
295 | of Null-terminated Unicode string Source are concatenated to the end of\r |
296 | Null-terminated Unicode string Destination. The Null-terminated concatenated\r |
297 | Unicode String is returned. If Source and Destination overlap, then the\r |
298 | results are undefined.\r |
299 | \r |
300 | If Destination is NULL, then ASSERT().\r |
301 | If Source is NULL, then ASSERT().\r |
302 | If Source and Destination overlap, then ASSERT().\r |
303 | If PcdMaximumUnicodeStringLength is not zero, and Destination contains more\r |
304 | than PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r |
305 | If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r |
306 | PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r |
307 | If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination\r |
308 | and Source results in a Unicode string with more than\r |
309 | PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r |
310 | \r |
311 | @param Destination Pointer to a Null-terminated Unicode string.\r |
312 | @param Source Pointer to a Null-terminated Unicode string.\r |
313 | \r |
314 | @return Destination\r |
315 | \r |
316 | **/\r |
317 | CHAR16 *\r |
318 | EFIAPI\r |
319 | StrCat (\r |
320 | IN OUT CHAR16 *Destination,\r |
321 | IN CONST CHAR16 *Source\r |
322 | )\r |
323 | {\r |
324 | StrCpy (Destination + StrLen (Destination), Source);\r |
325 | \r |
326 | //\r |
327 | // Size of the resulting string should never be zero.\r |
328 | // PcdMaximumUnicodeStringLength is tested inside StrLen().\r |
329 | //\r |
330 | ASSERT (StrSize (Destination) != 0);\r |
331 | return Destination;\r |
332 | }\r |
333 | \r |
334 | /**\r |
335 | Concatenates one Null-terminated Unicode string with a maximum length to the\r |
336 | end of another Null-terminated Unicode string, and returns the concatenated\r |
337 | Unicode string.\r |
338 | \r |
339 | This function concatenates two Null-terminated Unicode strings. The contents\r |
340 | of Null-terminated Unicode string Source are concatenated to the end of\r |
341 | Null-terminated Unicode string Destination, and Destination is returned. At\r |
342 | most, Length Unicode characters are concatenated from Source to the end of\r |
343 | Destination, and Destination is always Null-terminated. If Length is 0, then\r |
344 | Destination is returned unmodified. If Source and Destination overlap, then\r |
345 | the results are undefined.\r |
346 | \r |
347 | If Destination is NULL, then ASSERT().\r |
348 | If Source is NULL, then ASSERT().\r |
349 | If Source and Destination overlap, then ASSERT().\r |
350 | If PcdMaximumUnicodeStringLength is not zero, and Destination contains more\r |
351 | than PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r |
352 | If PcdMaximumUnicodeStringLength is not zero, and Source contains more than\r |
353 | PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r |
354 | If PcdMaximumUnicodeStringLength is not zero, and concatenating Destination\r |
355 | and Source results in a Unicode string with more than\r |
356 | PcdMaximumUnicodeStringLength Unicode characters, then ASSERT().\r |
357 | \r |
358 | @param Destination Pointer to a Null-terminated Unicode string.\r |
359 | @param Source Pointer to a Null-terminated Unicode string.\r |
360 | @param Length Maximum number of Unicode characters to concatenate from\r |
361 | Source.\r |
362 | \r |
363 | @return Destination\r |
364 | \r |
365 | **/\r |
366 | CHAR16 *\r |
367 | EFIAPI\r |
368 | StrnCat (\r |
369 | IN OUT CHAR16 *Destination,\r |
370 | IN CONST CHAR16 *Source,\r |
371 | IN UINTN Length\r |
372 | )\r |
373 | {\r |
374 | StrnCpy (Destination + StrLen (Destination), Source, Length);\r |
375 | \r |
376 | //\r |
377 | // Size of the resulting string should never be zero.\r |
378 | // PcdMaximumUnicodeStringLength is tested inside StrLen().\r |
379 | //\r |
380 | ASSERT (StrSize (Destination) != 0);\r |
381 | return Destination;\r |
382 | }\r |
383 | \r |
384 | /**\r |
385 | Copies one Null-terminated ASCII string to another Null-terminated ASCII\r |
386 | string and returns the new ASCII string.\r |
387 | \r |
388 | This function copies the contents of the ASCII string Source to the ASCII\r |
389 | string Destination, and returns Destination. If Source and Destination\r |
390 | overlap, then the results are undefined.\r |
391 | \r |
392 | If Destination is NULL, then ASSERT().\r |
393 | If Source is NULL, then ASSERT().\r |
394 | If Source and Destination overlap, then ASSERT().\r |
395 | If PcdMaximumAsciiStringLength is not zero and Source contains more than\r |
396 | PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r |
397 | \r |
398 | @param Destination Pointer to a Null-terminated ASCII string.\r |
399 | @param Source Pointer to a Null-terminated ASCII string.\r |
400 | \r |
401 | @return Destination\r |
402 | \r |
403 | **/\r |
404 | CHAR8 *\r |
405 | EFIAPI\r |
406 | AsciiStrCpy (\r |
407 | OUT CHAR8 *Destination,\r |
408 | IN CONST CHAR8 *Source\r |
409 | )\r |
410 | {\r |
411 | CHAR8 *ReturnValue;\r |
412 | \r |
413 | //\r |
414 | // Destination cannot be NULL\r |
415 | //\r |
416 | ASSERT (Destination != NULL);\r |
417 | \r |
418 | //\r |
419 | // Destination and source cannot overlap\r |
420 | //\r |
421 | ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));\r |
422 | ASSERT ((UINTN)(Source - Destination) > AsciiStrLen (Source));\r |
423 | \r |
424 | ReturnValue = Destination;\r |
425 | while (*Source) {\r |
426 | *(Destination++) = *(Source++);\r |
427 | }\r |
428 | *Destination = 0;\r |
429 | return ReturnValue;\r |
430 | }\r |
431 | \r |
432 | /**\r |
433 | Copies one Null-terminated ASCII string with a maximum length to another\r |
434 | Null-terminated ASCII string with a maximum length and returns the new ASCII\r |
435 | string.\r |
436 | \r |
437 | This function copies the contents of the ASCII string Source to the ASCII\r |
438 | string Destination, and returns Destination. At most, Length ASCII characters\r |
439 | are copied from Source to Destination. If Length is 0, then Destination is\r |
440 | returned unmodified. If Length is greater that the number of ASCII characters\r |
441 | in Source, then Destination is padded with Null ASCII characters. If Source\r |
442 | and Destination overlap, then the results are undefined.\r |
443 | \r |
444 | If Destination is NULL, then ASSERT().\r |
445 | If Source is NULL, then ASSERT().\r |
446 | If Source and Destination overlap, then ASSERT().\r |
447 | If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r |
448 | PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r |
449 | \r |
450 | @param Destination Pointer to a Null-terminated ASCII string.\r |
451 | @param Source Pointer to a Null-terminated ASCII string.\r |
452 | @param Length Maximum number of ASCII characters to copy.\r |
453 | \r |
454 | @return Destination\r |
455 | \r |
456 | **/\r |
457 | CHAR8 *\r |
458 | EFIAPI\r |
459 | AsciiStrnCpy (\r |
460 | OUT CHAR8 *Destination,\r |
461 | IN CONST CHAR8 *Source,\r |
462 | IN UINTN Length\r |
463 | )\r |
464 | {\r |
465 | CHAR8 *ReturnValue;\r |
466 | \r |
467 | if (Length == 0) {\r |
468 | return Destination;\r |
469 | }\r |
470 | \r |
471 | //\r |
472 | // Destination cannot be NULL\r |
473 | //\r |
474 | ASSERT (Destination != NULL);\r |
475 | \r |
476 | //\r |
477 | // Destination and source cannot overlap\r |
478 | //\r |
479 | ASSERT ((UINTN)(Destination - Source) > AsciiStrLen (Source));\r |
480 | ASSERT ((UINTN)(Source - Destination) >= Length);\r |
481 | \r |
482 | ReturnValue = Destination;\r |
483 | \r |
6cfb0c24 |
484 | while (*Source && Length > 0) {\r |
878ddf1f |
485 | *(Destination++) = *(Source++);\r |
486 | Length--;\r |
487 | }\r |
488 | \r |
489 | ZeroMem (Destination, Length * sizeof (*Destination));\r |
490 | return ReturnValue;\r |
491 | }\r |
492 | \r |
493 | /**\r |
494 | Returns the length of a Null-terminated ASCII string.\r |
495 | \r |
496 | This function returns the number of ASCII characters in the Null-terminated\r |
497 | ASCII string specified by String.\r |
498 | \r |
499 | If String is NULL, then ASSERT().\r |
500 | If PcdMaximumAsciiStringLength is not zero and String contains more than\r |
501 | PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r |
502 | \r |
503 | @param String Pointer to a Null-terminated ASCII string.\r |
504 | \r |
505 | @return The length of String.\r |
506 | \r |
507 | **/\r |
508 | UINTN\r |
509 | EFIAPI\r |
510 | AsciiStrLen (\r |
511 | IN CONST CHAR8 *String\r |
512 | )\r |
513 | {\r |
514 | UINTN Length;\r |
515 | \r |
516 | ASSERT (String != NULL);\r |
517 | \r |
518 | for (Length = 0; *String != '\0'; String++, Length++) {\r |
519 | //\r |
520 | // If PcdMaximumUnicodeStringLength is not zero,\r |
521 | // length should not more than PcdMaximumUnicodeStringLength\r |
522 | //\r |
dc530c7b |
523 | if (PcdGet32 (PcdMaximumAsciiStringLength) != 0) {\r |
524 | ASSERT (Length < PcdGet32 (PcdMaximumAsciiStringLength));\r |
878ddf1f |
525 | }\r |
526 | }\r |
527 | return Length;\r |
528 | }\r |
529 | \r |
530 | /**\r |
531 | Returns the size of a Null-terminated ASCII string in bytes, including the\r |
532 | Null terminator.\r |
533 | \r |
534 | This function returns the size, in bytes, of the Null-terminated ASCII string\r |
535 | specified by String.\r |
536 | \r |
537 | If String is NULL, then ASSERT().\r |
538 | If PcdMaximumAsciiStringLength is not zero and String contains more than\r |
539 | PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r |
540 | \r |
541 | @param String Pointer to a Null-terminated ASCII string.\r |
542 | \r |
543 | @return The size of String.\r |
544 | \r |
545 | **/\r |
546 | UINTN\r |
547 | EFIAPI\r |
548 | AsciiStrSize (\r |
549 | IN CONST CHAR8 *String\r |
550 | )\r |
551 | {\r |
552 | return (AsciiStrLen (String) + 1) * sizeof (*String);\r |
553 | }\r |
554 | \r |
555 | /**\r |
556 | Compares two Null-terminated ASCII strings, and returns the difference\r |
557 | between the first mismatched ASCII characters.\r |
558 | \r |
559 | This function compares the Null-terminated ASCII string FirstString to the\r |
560 | Null-terminated ASCII string SecondString. If FirstString is identical to\r |
561 | SecondString, then 0 is returned. Otherwise, the value returned is the first\r |
562 | mismatched ASCII character in SecondString subtracted from the first\r |
563 | mismatched ASCII character in FirstString.\r |
564 | \r |
565 | If FirstString is NULL, then ASSERT().\r |
566 | If SecondString is NULL, then ASSERT().\r |
567 | If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r |
568 | PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r |
569 | If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r |
570 | than PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r |
571 | \r |
572 | @param FirstString Pointer to a Null-terminated ASCII string.\r |
573 | @param SecondString Pointer to a Null-terminated ASCII string.\r |
574 | \r |
575 | @retval 0 FirstString is identical to SecondString.\r |
576 | @retval !=0 FirstString is not identical to SecondString.\r |
577 | \r |
578 | **/\r |
579 | INTN\r |
580 | EFIAPI\r |
581 | AsciiStrCmp (\r |
582 | IN CONST CHAR8 *FirstString,\r |
583 | IN CONST CHAR8 *SecondString\r |
584 | )\r |
585 | {\r |
586 | //\r |
587 | // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r |
588 | //\r |
589 | ASSERT (AsciiStrSize (FirstString));\r |
590 | ASSERT (AsciiStrSize (SecondString));\r |
591 | \r |
592 | while ((*FirstString != '\0') && (*FirstString == *SecondString)) {\r |
593 | FirstString++;\r |
594 | SecondString++;\r |
595 | }\r |
596 | \r |
597 | return *FirstString - *SecondString;\r |
598 | }\r |
599 | \r |
600 | STATIC\r |
601 | CHAR8\r |
602 | EFIAPI\r |
603 | AsciiToUpper (\r |
604 | IN CHAR8 Chr\r |
605 | )\r |
606 | {\r |
607 | return (Chr >= 'a' && Chr <= 'z') ? Chr - ('a' - 'A') : Chr;\r |
608 | }\r |
609 | \r |
610 | /**\r |
611 | Performs a case insensitive comparison of two Null-terminated ASCII strings,\r |
612 | and returns the difference between the first mismatched ASCII characters.\r |
613 | \r |
614 | This function performs a case insensitive comparison of the Null-terminated\r |
615 | ASCII string FirstString to the Null-terminated ASCII string SecondString. If\r |
616 | FirstString is identical to SecondString, then 0 is returned. Otherwise, the\r |
617 | value returned is the first mismatched lower case ASCII character in\r |
618 | SecondString subtracted from the first mismatched lower case ASCII character\r |
619 | in FirstString.\r |
620 | \r |
621 | If FirstString is NULL, then ASSERT().\r |
622 | If SecondString is NULL, then ASSERT().\r |
623 | If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r |
624 | PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r |
625 | If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r |
626 | than PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r |
627 | \r |
628 | @param FirstString Pointer to a Null-terminated ASCII string.\r |
629 | @param SecondString Pointer to a Null-terminated ASCII string.\r |
630 | \r |
631 | @retval 0 FirstString is identical to SecondString using case insensitive\r |
632 | comparisons.\r |
633 | @retval !=0 FirstString is not identical to SecondString using case\r |
634 | insensitive comparisons.\r |
635 | \r |
636 | **/\r |
637 | INTN\r |
638 | EFIAPI\r |
639 | AsciiStriCmp (\r |
640 | IN CONST CHAR8 *FirstString,\r |
641 | IN CONST CHAR8 *SecondString\r |
642 | )\r |
643 | {\r |
644 | //\r |
645 | // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r |
646 | //\r |
647 | ASSERT (AsciiStrSize (FirstString));\r |
648 | ASSERT (AsciiStrSize (SecondString));\r |
649 | \r |
650 | while ((*FirstString != '\0') &&\r |
651 | (AsciiToUpper (*FirstString) == AsciiToUpper (*SecondString))) {\r |
652 | FirstString++;\r |
653 | SecondString++;\r |
654 | }\r |
655 | \r |
656 | return AsciiToUpper (*FirstString) - AsciiToUpper (*SecondString);\r |
657 | }\r |
658 | \r |
659 | /**\r |
660 | Compares two Null-terminated ASCII strings with maximum lengths, and returns\r |
661 | the difference between the first mismatched ASCII characters.\r |
662 | \r |
663 | This function compares the Null-terminated ASCII string FirstString to the\r |
664 | Null-terminated ASCII string SecondString. At most, Length ASCII characters\r |
665 | will be compared. If Length is 0, then 0 is returned. If FirstString is\r |
666 | identical to SecondString, then 0 is returned. Otherwise, the value returned\r |
667 | is the first mismatched ASCII character in SecondString subtracted from the\r |
668 | first mismatched ASCII character in FirstString.\r |
669 | \r |
670 | If FirstString is NULL, then ASSERT().\r |
671 | If SecondString is NULL, then ASSERT().\r |
672 | If PcdMaximumAsciiStringLength is not zero and FirstString contains more than\r |
673 | PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r |
674 | If PcdMaximumAsciiStringLength is not zero and SecondString contains more\r |
675 | than PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r |
676 | \r |
677 | @param FirstString Pointer to a Null-terminated ASCII string.\r |
678 | @param SecondString Pointer to a Null-terminated ASCII string.\r |
679 | \r |
680 | @retval 0 FirstString is identical to SecondString.\r |
681 | @retval !=0 FirstString is not identical to SecondString.\r |
682 | \r |
683 | **/\r |
684 | INTN\r |
685 | EFIAPI\r |
686 | AsciiStrnCmp (\r |
687 | IN CONST CHAR8 *FirstString,\r |
688 | IN CONST CHAR8 *SecondString,\r |
689 | IN UINTN Length\r |
690 | )\r |
691 | {\r |
692 | //\r |
693 | // ASSERT both strings are less long than PcdMaximumAsciiStringLength\r |
694 | //\r |
695 | ASSERT (AsciiStrSize (FirstString));\r |
696 | ASSERT (AsciiStrSize (SecondString));\r |
697 | \r |
698 | while ((*FirstString != '\0') &&\r |
267f7f10 |
699 | (*FirstString == *SecondString) &&\r |
878ddf1f |
700 | (Length > 1)) {\r |
701 | FirstString++;\r |
702 | SecondString++;\r |
703 | Length--;\r |
704 | }\r |
705 | return *FirstString - *SecondString;\r |
706 | }\r |
707 | \r |
708 | /**\r |
709 | Concatenates one Null-terminated ASCII string to another Null-terminated\r |
710 | ASCII string, and returns the concatenated ASCII string.\r |
711 | \r |
712 | This function concatenates two Null-terminated ASCII strings. The contents of\r |
713 | Null-terminated ASCII string Source are concatenated to the end of Null-\r |
714 | terminated ASCII string Destination. The Null-terminated concatenated ASCII\r |
715 | String is returned.\r |
716 | \r |
717 | If Destination is NULL, then ASSERT().\r |
718 | If Source is NULL, then ASSERT().\r |
719 | If PcdMaximumAsciiStringLength is not zero and Destination contains more than\r |
720 | PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r |
721 | If PcdMaximumAsciiStringLength is not zero and Source contains more than\r |
722 | PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r |
723 | If PcdMaximumAsciiStringLength is not zero and concatenating Destination and\r |
724 | Source results in a ASCII string with more than PcdMaximumAsciiStringLength\r |
725 | ASCII characters, then ASSERT().\r |
726 | \r |
727 | @param Destination Pointer to a Null-terminated ASCII string.\r |
728 | @param Source Pointer to a Null-terminated ASCII string.\r |
729 | \r |
730 | @return Destination\r |
731 | \r |
732 | **/\r |
733 | CHAR8 *\r |
734 | EFIAPI\r |
735 | AsciiStrCat (\r |
736 | IN OUT CHAR8 *Destination,\r |
737 | IN CONST CHAR8 *Source\r |
738 | )\r |
739 | {\r |
740 | AsciiStrCpy (Destination + AsciiStrLen (Destination), Source);\r |
741 | \r |
742 | //\r |
743 | // Size of the resulting string should never be zero.\r |
744 | // PcdMaximumUnicodeStringLength is tested inside StrLen().\r |
745 | //\r |
746 | ASSERT (AsciiStrSize (Destination) != 0);\r |
747 | return Destination;\r |
748 | }\r |
749 | \r |
750 | /**\r |
751 | Concatenates one Null-terminated ASCII string with a maximum length to the\r |
752 | end of another Null-terminated ASCII string, and returns the concatenated\r |
753 | ASCII string.\r |
754 | \r |
755 | This function concatenates two Null-terminated ASCII strings. The contents\r |
756 | of Null-terminated ASCII string Source are concatenated to the end of Null-\r |
757 | terminated ASCII string Destination, and Destination is returned. At most,\r |
758 | Length ASCII characters are concatenated from Source to the end of\r |
759 | Destination, and Destination is always Null-terminated. If Length is 0, then\r |
760 | Destination is returned unmodified. If Source and Destination overlap, then\r |
761 | the results are undefined.\r |
762 | \r |
763 | If Destination is NULL, then ASSERT().\r |
764 | If Source is NULL, then ASSERT().\r |
765 | If Source and Destination overlap, then ASSERT().\r |
766 | If PcdMaximumAsciiStringLength is not zero, and Destination contains more\r |
767 | than PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r |
768 | If PcdMaximumAsciiStringLength is not zero, and Source contains more than\r |
769 | PcdMaximumAsciiStringLength ASCII characters, then ASSERT().\r |
770 | If PcdMaximumAsciiStringLength is not zero, and concatenating Destination and\r |
771 | Source results in a ASCII string with more than PcdMaximumAsciiStringLength\r |
772 | ASCII characters, then ASSERT().\r |
773 | \r |
774 | @param Destination Pointer to a Null-terminated ASCII string.\r |
775 | @param Source Pointer to a Null-terminated ASCII string.\r |
776 | @param Length Maximum number of ASCII characters to concatenate from\r |
777 | Source.\r |
778 | \r |
779 | @return Destination\r |
780 | \r |
781 | **/\r |
782 | CHAR8 *\r |
783 | EFIAPI\r |
784 | AsciiStrnCat (\r |
785 | IN OUT CHAR8 *Destination,\r |
786 | IN CONST CHAR8 *Source,\r |
787 | IN UINTN Length\r |
788 | )\r |
789 | {\r |
790 | AsciiStrnCpy (Destination + AsciiStrLen (Destination), Source, Length);\r |
791 | \r |
792 | //\r |
793 | // Size of the resulting string should never be zero.\r |
794 | // PcdMaximumUnicodeStringLength is tested inside StrLen().\r |
795 | //\r |
796 | ASSERT (AsciiStrSize (Destination) != 0);\r |
797 | return Destination;\r |
798 | }\r |
dee4740a |
799 | \r |
800 | /**\r |
801 | Converts an 8-bit value to an 8-bit BCD value.\r |
802 | \r |
803 | Converts the 8-bit value specified by Value to BCD. The BCD value is\r |
804 | returned.\r |
805 | \r |
806 | If Value >= 100, then ASSERT().\r |
807 | \r |
808 | @param Value The 8-bit value to convert to BCD. Range 0..99.\r |
809 | \r |
810 | @return The BCD value\r |
811 | \r |
812 | **/\r |
813 | UINT8\r |
814 | EFIAPI\r |
815 | DecimalToBcd8 (\r |
816 | IN UINT8 Value\r |
817 | )\r |
818 | {\r |
819 | ASSERT (Value < 100);\r |
820 | return ((Value / 10) << 4) | (Value % 10);\r |
821 | }\r |
822 | \r |
823 | /**\r |
824 | Converts an 8-bit BCD value to an 8-bit value.\r |
825 | \r |
826 | Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit\r |
827 | value is returned.\r |
828 | \r |
829 | If Value >= 0xA0, then ASSERT().\r |
830 | If (Value & 0x0F) >= 0x0A, then ASSERT().\r |
831 | \r |
832 | @param Value The 8-bit BCD value to convert to an 8-bit value.\r |
833 | \r |
834 | @return The 8-bit value is returned.\r |
835 | \r |
836 | **/\r |
837 | UINT8\r |
838 | EFIAPI\r |
839 | BcdToDecimal8 (\r |
840 | IN UINT8 Value\r |
841 | )\r |
842 | {\r |
843 | ASSERT (Value < 0xa0);\r |
844 | ASSERT ((Value & 0xf) < 0xa);\r |
845 | return (Value >> 4) * 10 + (Value & 0xf);\r |
846 | }\r |