Coverage Report

Created: 2024-10-17 06:29

/src/hbfa-fl/HBFA/UefiHostTestPkg/Library/BaseLibHost/String.c
Line
Count
Source (jump to first uncovered line)
1
/** @file
2
  Unicode and ASCII string primitives.
3
4
  Copyright (c) 2006 - 2018, Intel Corporation. All rights reserved.<BR>
5
  SPDX-License-Identifier: BSD-2-Clause-Patent
6
7
**/
8
9
#include <Base.h>
10
#include <Library/BaseLib.h>
11
#include <Library/DebugLib.h>
12
13
/**
14
  Returns the length of a Null-terminated Unicode string.
15
16
  This function returns the number of Unicode characters in the Null-terminated
17
  Unicode string specified by String.
18
19
  If String is NULL, then ASSERT().
20
  If String is not aligned on a 16-bit boundary, then ASSERT().
21
  If PcdMaximumUnicodeStringLength is not zero, and String contains more than
22
  PcdMaximumUnicodeStringLength Unicode characters, not including the
23
  Null-terminator, then ASSERT().
24
25
  @param  String  A pointer to a Null-terminated Unicode string.
26
27
  @return The length of String.
28
29
**/
30
UINTN
31
EFIAPI
32
StrLen (
33
  IN      CONST CHAR16              *String
34
  )
35
11.0k
{
36
11.0k
  UINTN                             Length;
37
38
11.0k
  ASSERT (String != NULL);
39
11.0k
  ASSERT (((UINTN) String & BIT0) == 0);
40
41
925k
  for (Length = 0; *String != L'\0'; String++, Length++) {
42
    //
43
    // If PcdMaximumUnicodeStringLength is not zero,
44
    // length should not more than PcdMaximumUnicodeStringLength
45
    //
46
914k
  }
47
11.0k
  return Length;
48
11.0k
}
49
50
/**
51
  Returns the size of a Null-terminated Unicode string in bytes, including the
52
  Null terminator.
53
54
  This function returns the size, in bytes, of the Null-terminated Unicode string
55
  specified by String.
56
57
  If String is NULL, then ASSERT().
58
  If String is not aligned on a 16-bit boundary, then ASSERT().
59
  If PcdMaximumUnicodeStringLength is not zero, and String contains more than
60
  PcdMaximumUnicodeStringLength Unicode characters, not including the
61
  Null-terminator, then ASSERT().
62
63
  @param  String  A pointer to a Null-terminated Unicode string.
64
65
  @return The size of String.
66
67
**/
68
UINTN
69
EFIAPI
70
StrSize (
71
  IN      CONST CHAR16              *String
72
  )
73
3.76k
{
74
3.76k
  return (StrLen (String) + 1) * sizeof (*String);
75
3.76k
}
76
77
/**
78
  Compares two Null-terminated Unicode strings, and returns the difference
79
  between the first mismatched Unicode characters.
80
81
  This function compares the Null-terminated Unicode string FirstString to the
82
  Null-terminated Unicode string SecondString. If FirstString is identical to
83
  SecondString, then 0 is returned. Otherwise, the value returned is the first
84
  mismatched Unicode character in SecondString subtracted from the first
85
  mismatched Unicode character in FirstString.
86
87
  If FirstString is NULL, then ASSERT().
88
  If FirstString is not aligned on a 16-bit boundary, then ASSERT().
89
  If SecondString is NULL, then ASSERT().
90
  If SecondString is not aligned on a 16-bit boundary, then ASSERT().
91
  If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more
92
  than PcdMaximumUnicodeStringLength Unicode characters, not including the
93
  Null-terminator, then ASSERT().
94
  If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more
95
  than PcdMaximumUnicodeStringLength Unicode characters, not including the
96
  Null-terminator, then ASSERT().
97
98
  @param  FirstString   A pointer to a Null-terminated Unicode string.
99
  @param  SecondString  A pointer to a Null-terminated Unicode string.
100
101
  @retval 0      FirstString is identical to SecondString.
102
  @return others FirstString is not identical to SecondString.
103
104
**/
105
INTN
106
EFIAPI
107
StrCmp (
108
  IN      CONST CHAR16              *FirstString,
109
  IN      CONST CHAR16              *SecondString
110
  )
111
1.56k
{
112
  //
113
  // ASSERT both strings are less long than PcdMaximumUnicodeStringLength
114
  //
115
1.56k
  ASSERT (StrSize (FirstString) != 0);
116
1.56k
  ASSERT (StrSize (SecondString) != 0);
117
118
2.43k
  while ((*FirstString != L'\0') && (*FirstString == *SecondString)) {
119
870
    FirstString++;
120
870
    SecondString++;
121
870
  }
122
1.56k
  return *FirstString - *SecondString;
123
1.56k
}
124
125
/**
126
  Compares up to a specified length the contents of two Null-terminated Unicode strings,
127
  and returns the difference between the first mismatched Unicode characters.
128
129
  This function compares the Null-terminated Unicode string FirstString to the
130
  Null-terminated Unicode string SecondString. At most, Length Unicode
131
  characters will be compared. If Length is 0, then 0 is returned. If
132
  FirstString is identical to SecondString, then 0 is returned. Otherwise, the
133
  value returned is the first mismatched Unicode character in SecondString
134
  subtracted from the first mismatched Unicode character in FirstString.
135
136
  If Length > 0 and FirstString is NULL, then ASSERT().
137
  If Length > 0 and FirstString is not aligned on a 16-bit boundary, then ASSERT().
138
  If Length > 0 and SecondString is NULL, then ASSERT().
139
  If Length > 0 and SecondString is not aligned on a 16-bit boundary, then ASSERT().
140
  If PcdMaximumUnicodeStringLength is not zero, and Length is greater than
141
  PcdMaximumUnicodeStringLength, then ASSERT().
142
  If PcdMaximumUnicodeStringLength is not zero, and FirstString contains more than
143
  PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
144
  then ASSERT().
145
  If PcdMaximumUnicodeStringLength is not zero, and SecondString contains more than
146
  PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
147
  then ASSERT().
148
149
  @param  FirstString   A pointer to a Null-terminated Unicode string.
150
  @param  SecondString  A pointer to a Null-terminated Unicode string.
151
  @param  Length        The maximum number of Unicode characters to compare.
152
153
  @retval 0      FirstString is identical to SecondString.
154
  @return others FirstString is not identical to SecondString.
155
156
**/
157
INTN
158
EFIAPI
159
StrnCmp (
160
  IN      CONST CHAR16              *FirstString,
161
  IN      CONST CHAR16              *SecondString,
162
  IN      UINTN                     Length
163
  )
164
0
{
165
0
  if (Length == 0) {
166
0
    return 0;
167
0
  }
168
169
  //
170
  // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
171
  // Length tests are performed inside StrLen().
172
  //
173
0
  ASSERT (StrSize (FirstString) != 0);
174
0
  ASSERT (StrSize (SecondString) != 0);
175
176
0
  while ((*FirstString != L'\0') &&
177
0
         (*SecondString != L'\0') &&
178
0
         (*FirstString == *SecondString) &&
179
0
         (Length > 1)) {
180
0
    FirstString++;
181
0
    SecondString++;
182
0
    Length--;
183
0
  }
184
185
0
  return *FirstString - *SecondString;
186
0
}
187
188
/**
189
  Returns the first occurrence of a Null-terminated Unicode sub-string
190
  in a Null-terminated Unicode string.
191
192
  This function scans the contents of the Null-terminated Unicode string
193
  specified by String and returns the first occurrence of SearchString.
194
  If SearchString is not found in String, then NULL is returned.  If
195
  the length of SearchString is zero, then String is
196
  returned.
197
198
  If String is NULL, then ASSERT().
199
  If String is not aligned on a 16-bit boundary, then ASSERT().
200
  If SearchString is NULL, then ASSERT().
201
  If SearchString is not aligned on a 16-bit boundary, then ASSERT().
202
203
  If PcdMaximumUnicodeStringLength is not zero, and SearchString
204
  or String contains more than PcdMaximumUnicodeStringLength Unicode
205
  characters, not including the Null-terminator, then ASSERT().
206
207
  @param  String          A pointer to a Null-terminated Unicode string.
208
  @param  SearchString    A pointer to a Null-terminated Unicode string to search for.
209
210
  @retval NULL            If the SearchString does not appear in String.
211
  @return others          If there is a match.
212
213
**/
214
CHAR16 *
215
EFIAPI
216
StrStr (
217
  IN      CONST CHAR16              *String,
218
  IN      CONST CHAR16              *SearchString
219
  )
220
207
{
221
207
  CONST CHAR16 *FirstMatch;
222
207
  CONST CHAR16 *SearchStringTmp;
223
224
  //
225
  // ASSERT both strings are less long than PcdMaximumUnicodeStringLength.
226
  // Length tests are performed inside StrLen().
227
  //
228
207
  ASSERT (StrSize (String) != 0);
229
207
  ASSERT (StrSize (SearchString) != 0);
230
231
207
  if (*SearchString == L'\0') {
232
0
    return (CHAR16 *) String;
233
0
  }
234
235
565
  while (*String != L'\0') {
236
473
    SearchStringTmp = SearchString;
237
473
    FirstMatch = String;
238
239
588
    while ((*String == *SearchStringTmp)
240
588
            && (*String != L'\0')) {
241
115
      String++;
242
115
      SearchStringTmp++;
243
115
    }
244
245
473
    if (*SearchStringTmp == L'\0') {
246
115
      return (CHAR16 *) FirstMatch;
247
115
    }
248
249
358
    if (*String == L'\0') {
250
0
      return NULL;
251
0
    }
252
253
358
    String = FirstMatch + 1;
254
358
  }
255
256
92
  return NULL;
257
207
}
258
259
/**
260
  Check if a Unicode character is a decimal character.
261
262
  This internal function checks if a Unicode character is a
263
  decimal character. The valid decimal character is from
264
  L'0' to L'9'.
265
266
  @param  Char  The character to check against.
267
268
  @retval TRUE  If the Char is a decmial character.
269
  @retval FALSE If the Char is not a decmial character.
270
271
**/
272
BOOLEAN
273
EFIAPI
274
InternalIsDecimalDigitCharacter (
275
  IN      CHAR16                    Char
276
  )
277
0
{
278
0
  return (BOOLEAN) (Char >= L'0' && Char <= L'9');
279
0
}
280
281
/**
282
  Convert a Unicode character to upper case only if
283
  it maps to a valid small-case ASCII character.
284
285
  This internal function only deal with Unicode character
286
  which maps to a valid small-case ASCII character, i.e.
287
  L'a' to L'z'. For other Unicode character, the input character
288
  is returned directly.
289
290
  @param  Char  The character to convert.
291
292
  @retval LowerCharacter   If the Char is with range L'a' to L'z'.
293
  @retval Unchanged        Otherwise.
294
295
**/
296
CHAR16
297
EFIAPI
298
InternalCharToUpper (
299
  IN      CHAR16                    Char
300
  )
301
0
{
302
0
  if (Char >= L'a' && Char <= L'z') {
303
0
    return (CHAR16) (Char - (L'a' - L'A'));
304
0
  }
305
306
0
  return Char;
307
0
}
308
309
/**
310
  Convert a Unicode character to numerical value.
311
312
  This internal function only deal with Unicode character
313
  which maps to a valid hexadecimal ASII character, i.e.
314
  L'0' to L'9', L'a' to L'f' or L'A' to L'F'. For other
315
  Unicode character, the value returned does not make sense.
316
317
  @param  Char  The character to convert.
318
319
  @return The numerical value converted.
320
321
**/
322
UINTN
323
EFIAPI
324
InternalHexCharToUintn (
325
  IN      CHAR16                    Char
326
  )
327
0
{
328
0
  if (InternalIsDecimalDigitCharacter (Char)) {
329
0
    return Char - L'0';
330
0
  }
331
332
0
  return (10 + InternalCharToUpper (Char) - L'A');
333
0
}
334
335
/**
336
  Check if a Unicode character is a hexadecimal character.
337
338
  This internal function checks if a Unicode character is a
339
  decimal character.  The valid hexadecimal character is
340
  L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
341
342
343
  @param  Char  The character to check against.
344
345
  @retval TRUE  If the Char is a hexadecmial character.
346
  @retval FALSE If the Char is not a hexadecmial character.
347
348
**/
349
BOOLEAN
350
EFIAPI
351
InternalIsHexaDecimalDigitCharacter (
352
  IN      CHAR16                    Char
353
  )
354
0
{
355
356
0
  return (BOOLEAN) (InternalIsDecimalDigitCharacter (Char) ||
357
0
    (Char >= L'A' && Char <= L'F') ||
358
0
    (Char >= L'a' && Char <= L'f'));
359
0
}
360
361
/**
362
  Convert a Null-terminated Unicode decimal string to a value of
363
  type UINTN.
364
365
  This function returns a value of type UINTN by interpreting the contents
366
  of the Unicode string specified by String as a decimal number. The format
367
  of the input Unicode string String is:
368
369
                  [spaces] [decimal digits].
370
371
  The valid decimal digit character is in the range [0-9]. The
372
  function will ignore the pad space, which includes spaces or
373
  tab characters, before [decimal digits]. The running zero in the
374
  beginning of [decimal digits] will be ignored. Then, the function
375
  stops at the first character that is a not a valid decimal character
376
  or a Null-terminator, whichever one comes first.
377
378
  If String is NULL, then ASSERT().
379
  If String is not aligned in a 16-bit boundary, then ASSERT().
380
  If String has only pad spaces, then 0 is returned.
381
  If String has no pad spaces or valid decimal digits,
382
  then 0 is returned.
383
  If the number represented by String overflows according
384
  to the range defined by UINTN, then MAX_UINTN is returned.
385
386
  If PcdMaximumUnicodeStringLength is not zero, and String contains
387
  more than PcdMaximumUnicodeStringLength Unicode characters, not including
388
  the Null-terminator, then ASSERT().
389
390
  @param  String      A pointer to a Null-terminated Unicode string.
391
392
  @retval Value translated from String.
393
394
**/
395
UINTN
396
EFIAPI
397
StrDecimalToUintn (
398
  IN      CONST CHAR16              *String
399
  )
400
0
{
401
0
  UINTN     Result;
402
403
0
  StrDecimalToUintnS (String, (CHAR16 **) NULL, &Result);
404
0
  return Result;
405
0
}
406
407
408
/**
409
  Convert a Null-terminated Unicode decimal string to a value of
410
  type UINT64.
411
412
  This function returns a value of type UINT64 by interpreting the contents
413
  of the Unicode string specified by String as a decimal number. The format
414
  of the input Unicode string String is:
415
416
                  [spaces] [decimal digits].
417
418
  The valid decimal digit character is in the range [0-9]. The
419
  function will ignore the pad space, which includes spaces or
420
  tab characters, before [decimal digits]. The running zero in the
421
  beginning of [decimal digits] will be ignored. Then, the function
422
  stops at the first character that is a not a valid decimal character
423
  or a Null-terminator, whichever one comes first.
424
425
  If String is NULL, then ASSERT().
426
  If String is not aligned in a 16-bit boundary, then ASSERT().
427
  If String has only pad spaces, then 0 is returned.
428
  If String has no pad spaces or valid decimal digits,
429
  then 0 is returned.
430
  If the number represented by String overflows according
431
  to the range defined by UINT64, then MAX_UINT64 is returned.
432
433
  If PcdMaximumUnicodeStringLength is not zero, and String contains
434
  more than PcdMaximumUnicodeStringLength Unicode characters, not including
435
  the Null-terminator, then ASSERT().
436
437
  @param  String          A pointer to a Null-terminated Unicode string.
438
439
  @retval Value translated from String.
440
441
**/
442
UINT64
443
EFIAPI
444
StrDecimalToUint64 (
445
  IN      CONST CHAR16              *String
446
  )
447
0
{
448
0
  UINT64     Result;
449
450
0
  StrDecimalToUint64S (String, (CHAR16 **) NULL, &Result);
451
0
  return Result;
452
0
}
453
454
/**
455
  Convert a Null-terminated Unicode hexadecimal string to a value of type UINTN.
456
457
  This function returns a value of type UINTN by interpreting the contents
458
  of the Unicode string specified by String as a hexadecimal number.
459
  The format of the input Unicode string String is:
460
461
                  [spaces][zeros][x][hexadecimal digits].
462
463
  The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
464
  The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
465
  If "x" appears in the input string, it must be prefixed with at least one 0.
466
  The function will ignore the pad space, which includes spaces or tab characters,
467
  before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
468
  [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
469
  first valid hexadecimal digit. Then, the function stops at the first character that is
470
  a not a valid hexadecimal character or NULL, whichever one comes first.
471
472
  If String is NULL, then ASSERT().
473
  If String is not aligned in a 16-bit boundary, then ASSERT().
474
  If String has only pad spaces, then zero is returned.
475
  If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
476
  then zero is returned.
477
  If the number represented by String overflows according to the range defined by
478
  UINTN, then MAX_UINTN is returned.
479
480
  If PcdMaximumUnicodeStringLength is not zero, and String contains more than
481
  PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
482
  then ASSERT().
483
484
  @param  String          A pointer to a Null-terminated Unicode string.
485
486
  @retval Value translated from String.
487
488
**/
489
UINTN
490
EFIAPI
491
StrHexToUintn (
492
  IN      CONST CHAR16              *String
493
  )
494
0
{
495
0
  UINTN     Result;
496
497
0
  StrHexToUintnS (String, (CHAR16 **) NULL, &Result);
498
0
  return Result;
499
0
}
500
501
502
/**
503
  Convert a Null-terminated Unicode hexadecimal string to a value of type UINT64.
504
505
  This function returns a value of type UINT64 by interpreting the contents
506
  of the Unicode string specified by String as a hexadecimal number.
507
  The format of the input Unicode string String is
508
509
                  [spaces][zeros][x][hexadecimal digits].
510
511
  The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
512
  The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix.
513
  If "x" appears in the input string, it must be prefixed with at least one 0.
514
  The function will ignore the pad space, which includes spaces or tab characters,
515
  before [zeros], [x] or [hexadecimal digit]. The running zero before [x] or
516
  [hexadecimal digit] will be ignored. Then, the decoding starts after [x] or the
517
  first valid hexadecimal digit. Then, the function stops at the first character that is
518
  a not a valid hexadecimal character or NULL, whichever one comes first.
519
520
  If String is NULL, then ASSERT().
521
  If String is not aligned in a 16-bit boundary, then ASSERT().
522
  If String has only pad spaces, then zero is returned.
523
  If String has no leading pad spaces, leading zeros or valid hexadecimal digits,
524
  then zero is returned.
525
  If the number represented by String overflows according to the range defined by
526
  UINT64, then MAX_UINT64 is returned.
527
528
  If PcdMaximumUnicodeStringLength is not zero, and String contains more than
529
  PcdMaximumUnicodeStringLength Unicode characters, not including the Null-terminator,
530
  then ASSERT().
531
532
  @param  String          A pointer to a Null-terminated Unicode string.
533
534
  @retval Value translated from String.
535
536
**/
537
UINT64
538
EFIAPI
539
StrHexToUint64 (
540
  IN      CONST CHAR16             *String
541
  )
542
0
{
543
0
  UINT64    Result;
544
545
0
  StrHexToUint64S (String, (CHAR16 **) NULL, &Result);
546
0
  return Result;
547
0
}
548
549
/**
550
  Check if a ASCII character is a decimal character.
551
552
  This internal function checks if a Unicode character is a
553
  decimal character. The valid decimal character is from
554
  '0' to '9'.
555
556
  @param  Char  The character to check against.
557
558
  @retval TRUE  If the Char is a decmial character.
559
  @retval FALSE If the Char is not a decmial character.
560
561
**/
562
BOOLEAN
563
EFIAPI
564
InternalAsciiIsDecimalDigitCharacter (
565
  IN      CHAR8                     Char
566
  )
567
0
{
568
0
  return (BOOLEAN) (Char >= '0' && Char <= '9');
569
0
}
570
571
/**
572
  Check if a ASCII character is a hexadecimal character.
573
574
  This internal function checks if a ASCII character is a
575
  decimal character.  The valid hexadecimal character is
576
  L'0' to L'9', L'a' to L'f', or L'A' to L'F'.
577
578
579
  @param  Char  The character to check against.
580
581
  @retval TRUE  If the Char is a hexadecmial character.
582
  @retval FALSE If the Char is not a hexadecmial character.
583
584
**/
585
BOOLEAN
586
EFIAPI
587
InternalAsciiIsHexaDecimalDigitCharacter (
588
  IN      CHAR8                    Char
589
  )
590
0
{
591
592
0
  return (BOOLEAN) (InternalAsciiIsDecimalDigitCharacter (Char) ||
593
0
    (Char >= 'A' && Char <= 'F') ||
594
0
    (Char >= 'a' && Char <= 'f'));
595
0
}
596
597
/**
598
  Returns the length of a Null-terminated ASCII string.
599
600
  This function returns the number of ASCII characters in the Null-terminated
601
  ASCII string specified by String.
602
603
  If Length > 0 and Destination is NULL, then ASSERT().
604
  If Length > 0 and Source is NULL, then ASSERT().
605
  If PcdMaximumAsciiStringLength is not zero and String contains more than
606
  PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
607
  then ASSERT().
608
609
  @param  String  A pointer to a Null-terminated ASCII string.
610
611
  @return The length of String.
612
613
**/
614
UINTN
615
EFIAPI
616
AsciiStrLen (
617
  IN      CONST CHAR8               *String
618
  )
619
0
{
620
0
  UINTN                             Length;
621
622
0
  ASSERT (String != NULL);
623
624
0
  for (Length = 0; *String != '\0'; String++, Length++) {
625
    //
626
    // If PcdMaximumUnicodeStringLength is not zero,
627
    // length should not more than PcdMaximumUnicodeStringLength
628
    //
629
0
  }
630
0
  return Length;
631
0
}
632
633
/**
634
  Returns the size of a Null-terminated ASCII string in bytes, including the
635
  Null terminator.
636
637
  This function returns the size, in bytes, of the Null-terminated ASCII string
638
  specified by String.
639
640
  If String is NULL, then ASSERT().
641
  If PcdMaximumAsciiStringLength is not zero and String contains more than
642
  PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
643
  then ASSERT().
644
645
  @param  String  A pointer to a Null-terminated ASCII string.
646
647
  @return The size of String.
648
649
**/
650
UINTN
651
EFIAPI
652
AsciiStrSize (
653
  IN      CONST CHAR8               *String
654
  )
655
0
{
656
0
  return (AsciiStrLen (String) + 1) * sizeof (*String);
657
0
}
658
659
/**
660
  Compares two Null-terminated ASCII strings, and returns the difference
661
  between the first mismatched ASCII characters.
662
663
  This function compares the Null-terminated ASCII string FirstString to the
664
  Null-terminated ASCII string SecondString. If FirstString is identical to
665
  SecondString, then 0 is returned. Otherwise, the value returned is the first
666
  mismatched ASCII character in SecondString subtracted from the first
667
  mismatched ASCII character in FirstString.
668
669
  If FirstString is NULL, then ASSERT().
670
  If SecondString is NULL, then ASSERT().
671
  If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
672
  PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
673
  then ASSERT().
674
  If PcdMaximumAsciiStringLength is not zero and SecondString contains more
675
  than PcdMaximumAsciiStringLength ASCII characters, not including the
676
  Null-terminator, then ASSERT().
677
678
  @param  FirstString   A pointer to a Null-terminated ASCII string.
679
  @param  SecondString  A pointer to a Null-terminated ASCII string.
680
681
  @retval ==0      FirstString is identical to SecondString.
682
  @retval !=0      FirstString is not identical to SecondString.
683
684
**/
685
INTN
686
EFIAPI
687
AsciiStrCmp (
688
  IN      CONST CHAR8               *FirstString,
689
  IN      CONST CHAR8               *SecondString
690
  )
691
0
{
692
  //
693
  // ASSERT both strings are less long than PcdMaximumAsciiStringLength
694
  //
695
0
  ASSERT (AsciiStrSize (FirstString));
696
0
  ASSERT (AsciiStrSize (SecondString));
697
698
0
  while ((*FirstString != '\0') && (*FirstString == *SecondString)) {
699
0
    FirstString++;
700
0
    SecondString++;
701
0
  }
702
703
0
  return *FirstString - *SecondString;
704
0
}
705
706
/**
707
  Converts a lowercase Ascii character to upper one.
708
709
  If Chr is lowercase Ascii character, then converts it to upper one.
710
711
  If Value >= 0xA0, then ASSERT().
712
  If (Value & 0x0F) >= 0x0A, then ASSERT().
713
714
  @param  Chr   one Ascii character
715
716
  @return The uppercase value of Ascii character
717
718
**/
719
CHAR8
720
EFIAPI
721
InternalBaseLibAsciiToUpper (
722
  IN      CHAR8                     Chr
723
  )
724
0
{
725
0
  return (UINT8) ((Chr >= 'a' && Chr <= 'z') ? Chr - ('a' - 'A') : Chr);
726
0
}
727
728
/**
729
  Convert a ASCII character to numerical value.
730
731
  This internal function only deal with Unicode character
732
  which maps to a valid hexadecimal ASII character, i.e.
733
  '0' to '9', 'a' to 'f' or 'A' to 'F'. For other
734
  ASCII character, the value returned does not make sense.
735
736
  @param  Char  The character to convert.
737
738
  @return The numerical value converted.
739
740
**/
741
UINTN
742
EFIAPI
743
InternalAsciiHexCharToUintn (
744
  IN      CHAR8                    Char
745
  )
746
0
{
747
0
  if (InternalIsDecimalDigitCharacter (Char)) {
748
0
    return Char - '0';
749
0
  }
750
751
0
  return (10 + InternalBaseLibAsciiToUpper (Char) - 'A');
752
0
}
753
754
755
/**
756
  Performs a case insensitive comparison of two Null-terminated ASCII strings,
757
  and returns the difference between the first mismatched ASCII characters.
758
759
  This function performs a case insensitive comparison of the Null-terminated
760
  ASCII string FirstString to the Null-terminated ASCII string SecondString. If
761
  FirstString is identical to SecondString, then 0 is returned. Otherwise, the
762
  value returned is the first mismatched lower case ASCII character in
763
  SecondString subtracted from the first mismatched lower case ASCII character
764
  in FirstString.
765
766
  If FirstString is NULL, then ASSERT().
767
  If SecondString is NULL, then ASSERT().
768
  If PcdMaximumAsciiStringLength is not zero and FirstString contains more than
769
  PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
770
  then ASSERT().
771
  If PcdMaximumAsciiStringLength is not zero and SecondString contains more
772
  than PcdMaximumAsciiStringLength ASCII characters, not including the
773
  Null-terminator, then ASSERT().
774
775
  @param  FirstString   A pointer to a Null-terminated ASCII string.
776
  @param  SecondString  A pointer to a Null-terminated ASCII string.
777
778
  @retval ==0    FirstString is identical to SecondString using case insensitive
779
                 comparisons.
780
  @retval !=0    FirstString is not identical to SecondString using case
781
                 insensitive comparisons.
782
783
**/
784
INTN
785
EFIAPI
786
AsciiStriCmp (
787
  IN      CONST CHAR8               *FirstString,
788
  IN      CONST CHAR8               *SecondString
789
  )
790
0
{
791
0
  CHAR8  UpperFirstString;
792
0
  CHAR8  UpperSecondString;
793
794
  //
795
  // ASSERT both strings are less long than PcdMaximumAsciiStringLength
796
  //
797
0
  ASSERT (AsciiStrSize (FirstString));
798
0
  ASSERT (AsciiStrSize (SecondString));
799
800
0
  UpperFirstString  = InternalBaseLibAsciiToUpper (*FirstString);
801
0
  UpperSecondString = InternalBaseLibAsciiToUpper (*SecondString);
802
0
  while ((*FirstString != '\0') && (*SecondString != '\0') && (UpperFirstString == UpperSecondString)) {
803
0
    FirstString++;
804
0
    SecondString++;
805
0
    UpperFirstString  = InternalBaseLibAsciiToUpper (*FirstString);
806
0
    UpperSecondString = InternalBaseLibAsciiToUpper (*SecondString);
807
0
  }
808
809
0
  return UpperFirstString - UpperSecondString;
810
0
}
811
812
/**
813
  Compares two Null-terminated ASCII strings with maximum lengths, and returns
814
  the difference between the first mismatched ASCII characters.
815
816
  This function compares the Null-terminated ASCII string FirstString to the
817
  Null-terminated ASCII  string SecondString. At most, Length ASCII characters
818
  will be compared. If Length is 0, then 0 is returned. If FirstString is
819
  identical to SecondString, then 0 is returned. Otherwise, the value returned
820
  is the first mismatched ASCII character in SecondString subtracted from the
821
  first mismatched ASCII character in FirstString.
822
823
  If Length > 0 and FirstString is NULL, then ASSERT().
824
  If Length > 0 and SecondString is NULL, then ASSERT().
825
  If PcdMaximumAsciiStringLength is not zero, and Length is greater than
826
  PcdMaximumAsciiStringLength, then ASSERT().
827
  If PcdMaximumAsciiStringLength is not zero, and FirstString contains more than
828
  PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
829
  then ASSERT().
830
  If PcdMaximumAsciiStringLength is not zero, and SecondString contains more than
831
  PcdMaximumAsciiStringLength ASCII characters, not including the Null-terminator,
832
  then ASSERT().
833
834
  @param  FirstString   A pointer to a Null-terminated ASCII string.
835
  @param  SecondString  A pointer to a Null-terminated ASCII string.
836
  @param  Length        The maximum number of ASCII characters for compare.
837
838
  @retval ==0       FirstString is identical to SecondString.
839
  @retval !=0       FirstString is not identical to SecondString.
840
841
**/
842
INTN
843
EFIAPI
844
AsciiStrnCmp (
845
  IN      CONST CHAR8               *FirstString,
846
  IN      CONST CHAR8               *SecondString,
847
  IN      UINTN                     Length
848
  )
849
0
{
850
0
  if (Length == 0) {
851
0
    return 0;
852
0
  }
853
854
  //
855
  // ASSERT both strings are less long than PcdMaximumAsciiStringLength
856
  //
857
0
  ASSERT (AsciiStrSize (FirstString));
858
0
  ASSERT (AsciiStrSize (SecondString));
859
860
0
  while ((*FirstString != '\0') &&
861
0
         (*SecondString != '\0') &&
862
0
         (*FirstString == *SecondString) &&
863
0
         (Length > 1)) {
864
0
    FirstString++;
865
0
    SecondString++;
866
0
    Length--;
867
0
  }
868
0
  return *FirstString - *SecondString;
869
0
}
870
871
/**
872
  Returns the first occurrence of a Null-terminated ASCII sub-string
873
  in a Null-terminated ASCII string.
874
875
  This function scans the contents of the ASCII string specified by String
876
  and returns the first occurrence of SearchString. If SearchString is not
877
  found in String, then NULL is returned. If the length of SearchString is zero,
878
  then String is returned.
879
880
  If String is NULL, then ASSERT().
881
  If SearchString is NULL, then ASSERT().
882
883
  If PcdMaximumAsciiStringLength is not zero, and SearchString or
884
  String contains more than PcdMaximumAsciiStringLength Unicode characters
885
  not including the Null-terminator, then ASSERT().
886
887
  @param  String          A pointer to a Null-terminated ASCII string.
888
  @param  SearchString    A pointer to a Null-terminated ASCII string to search for.
889
890
  @retval NULL            If the SearchString does not appear in String.
891
  @retval others          If there is a match return the first occurrence of SearchingString.
892
                          If the length of SearchString is zero,return String.
893
894
**/
895
CHAR8 *
896
EFIAPI
897
AsciiStrStr (
898
  IN      CONST CHAR8               *String,
899
  IN      CONST CHAR8               *SearchString
900
  )
901
0
{
902
0
  CONST CHAR8 *FirstMatch;
903
0
  CONST CHAR8 *SearchStringTmp;
904
905
  //
906
  // ASSERT both strings are less long than PcdMaximumAsciiStringLength
907
  //
908
0
  ASSERT (AsciiStrSize (String) != 0);
909
0
  ASSERT (AsciiStrSize (SearchString) != 0);
910
911
0
  if (*SearchString == '\0') {
912
0
    return (CHAR8 *) String;
913
0
  }
914
915
0
  while (*String != '\0') {
916
0
    SearchStringTmp = SearchString;
917
0
    FirstMatch = String;
918
919
0
    while ((*String == *SearchStringTmp)
920
0
            && (*String != '\0')) {
921
0
      String++;
922
0
      SearchStringTmp++;
923
0
    }
924
925
0
    if (*SearchStringTmp == '\0') {
926
0
      return (CHAR8 *) FirstMatch;
927
0
    }
928
929
0
    if (*String == '\0') {
930
0
      return NULL;
931
0
    }
932
933
0
    String = FirstMatch + 1;
934
0
  }
935
936
0
  return NULL;
937
0
}
938
939
/**
940
  Convert a Null-terminated ASCII decimal string to a value of type
941
  UINTN.
942
943
  This function returns a value of type UINTN by interpreting the contents
944
  of the ASCII string String as a decimal number. The format of the input
945
  ASCII string String is:
946
947
                    [spaces] [decimal digits].
948
949
  The valid decimal digit character is in the range [0-9]. The function will
950
  ignore the pad space, which includes spaces or tab characters, before the digits.
951
  The running zero in the beginning of [decimal digits] will be ignored. Then, the
952
  function stops at the first character that is a not a valid decimal character or
953
  Null-terminator, whichever on comes first.
954
955
  If String has only pad spaces, then 0 is returned.
956
  If String has no pad spaces or valid decimal digits, then 0 is returned.
957
  If the number represented by String overflows according to the range defined by
958
  UINTN, then MAX_UINTN is returned.
959
  If String is NULL, then ASSERT().
960
  If PcdMaximumAsciiStringLength is not zero, and String contains more than
961
  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
962
  then ASSERT().
963
964
  @param  String          A pointer to a Null-terminated ASCII string.
965
966
  @retval Value translated from String.
967
968
**/
969
UINTN
970
EFIAPI
971
AsciiStrDecimalToUintn (
972
  IN      CONST CHAR8               *String
973
  )
974
0
{
975
0
  UINTN     Result;
976
977
0
  AsciiStrDecimalToUintnS (String, (CHAR8 **) NULL, &Result);
978
0
  return Result;
979
0
}
980
981
982
/**
983
  Convert a Null-terminated ASCII decimal string to a value of type
984
  UINT64.
985
986
  This function returns a value of type UINT64 by interpreting the contents
987
  of the ASCII string String as a decimal number. The format of the input
988
  ASCII string String is:
989
990
                    [spaces] [decimal digits].
991
992
  The valid decimal digit character is in the range [0-9]. The function will
993
  ignore the pad space, which includes spaces or tab characters, before the digits.
994
  The running zero in the beginning of [decimal digits] will be ignored. Then, the
995
  function stops at the first character that is a not a valid decimal character or
996
  Null-terminator, whichever on comes first.
997
998
  If String has only pad spaces, then 0 is returned.
999
  If String has no pad spaces or valid decimal digits, then 0 is returned.
1000
  If the number represented by String overflows according to the range defined by
1001
  UINT64, then MAX_UINT64 is returned.
1002
  If String is NULL, then ASSERT().
1003
  If PcdMaximumAsciiStringLength is not zero, and String contains more than
1004
  PcdMaximumAsciiStringLength ASCII characters not including the Null-terminator,
1005
  then ASSERT().
1006
1007
  @param  String          A pointer to a Null-terminated ASCII string.
1008
1009
  @retval Value translated from String.
1010
1011
**/
1012
UINT64
1013
EFIAPI
1014
AsciiStrDecimalToUint64 (
1015
  IN      CONST CHAR8               *String
1016
  )
1017
0
{
1018
0
  UINT64     Result;
1019
1020
0
  AsciiStrDecimalToUint64S (String, (CHAR8 **) NULL, &Result);
1021
0
  return Result;
1022
0
}
1023
1024
/**
1025
  Convert a Null-terminated ASCII hexadecimal string to a value of type UINTN.
1026
1027
  This function returns a value of type UINTN by interpreting the contents of
1028
  the ASCII string String as a hexadecimal number. The format of the input ASCII
1029
  string String is:
1030
1031
                  [spaces][zeros][x][hexadecimal digits].
1032
1033
  The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1034
  The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1035
  appears in the input string, it must be prefixed with at least one 0. The function
1036
  will ignore the pad space, which includes spaces or tab characters, before [zeros],
1037
  [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1038
  will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1039
  digit. Then, the function stops at the first character that is a not a valid
1040
  hexadecimal character or Null-terminator, whichever on comes first.
1041
1042
  If String has only pad spaces, then 0 is returned.
1043
  If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1044
  0 is returned.
1045
1046
  If the number represented by String overflows according to the range defined by UINTN,
1047
  then MAX_UINTN is returned.
1048
  If String is NULL, then ASSERT().
1049
  If PcdMaximumAsciiStringLength is not zero,
1050
  and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1051
  the Null-terminator, then ASSERT().
1052
1053
  @param  String          A pointer to a Null-terminated ASCII string.
1054
1055
  @retval Value translated from String.
1056
1057
**/
1058
UINTN
1059
EFIAPI
1060
AsciiStrHexToUintn (
1061
  IN      CONST CHAR8               *String
1062
  )
1063
0
{
1064
0
  UINTN     Result;
1065
1066
0
  AsciiStrHexToUintnS (String, (CHAR8 **) NULL, &Result);
1067
0
  return Result;
1068
0
}
1069
1070
1071
/**
1072
  Convert a Null-terminated ASCII hexadecimal string to a value of type UINT64.
1073
1074
  This function returns a value of type UINT64 by interpreting the contents of
1075
  the ASCII string String as a hexadecimal number. The format of the input ASCII
1076
  string String is:
1077
1078
                  [spaces][zeros][x][hexadecimal digits].
1079
1080
  The valid hexadecimal digit character is in the range [0-9], [a-f] and [A-F].
1081
  The prefix "0x" is optional. Both "x" and "X" is allowed in "0x" prefix. If "x"
1082
  appears in the input string, it must be prefixed with at least one 0. The function
1083
  will ignore the pad space, which includes spaces or tab characters, before [zeros],
1084
  [x] or [hexadecimal digits]. The running zero before [x] or [hexadecimal digits]
1085
  will be ignored. Then, the decoding starts after [x] or the first valid hexadecimal
1086
  digit. Then, the function stops at the first character that is a not a valid
1087
  hexadecimal character or Null-terminator, whichever on comes first.
1088
1089
  If String has only pad spaces, then 0 is returned.
1090
  If String has no leading pad spaces, leading zeros or valid hexadecimal digits, then
1091
  0 is returned.
1092
1093
  If the number represented by String overflows according to the range defined by UINT64,
1094
  then MAX_UINT64 is returned.
1095
  If String is NULL, then ASSERT().
1096
  If PcdMaximumAsciiStringLength is not zero,
1097
  and String contains more than PcdMaximumAsciiStringLength ASCII characters not including
1098
  the Null-terminator, then ASSERT().
1099
1100
  @param  String          A pointer to a Null-terminated ASCII string.
1101
1102
  @retval Value translated from String.
1103
1104
**/
1105
UINT64
1106
EFIAPI
1107
AsciiStrHexToUint64 (
1108
  IN      CONST CHAR8                *String
1109
  )
1110
0
{
1111
0
  UINT64    Result;
1112
1113
0
  AsciiStrHexToUint64S (String, (CHAR8 **) NULL, &Result);
1114
0
  return Result;
1115
0
}
1116
1117
/**
1118
  Converts an 8-bit value to an 8-bit BCD value.
1119
1120
  Converts the 8-bit value specified by Value to BCD. The BCD value is
1121
  returned.
1122
1123
  If Value >= 100, then ASSERT().
1124
1125
  @param  Value The 8-bit value to convert to BCD. Range 0..99.
1126
1127
  @return The BCD value.
1128
1129
**/
1130
UINT8
1131
EFIAPI
1132
DecimalToBcd8 (
1133
  IN      UINT8                     Value
1134
  )
1135
0
{
1136
0
  ASSERT (Value < 100);
1137
0
  return (UINT8) (((Value / 10) << 4) | (Value % 10));
1138
0
}
1139
1140
/**
1141
  Converts an 8-bit BCD value to an 8-bit value.
1142
1143
  Converts the 8-bit BCD value specified by Value to an 8-bit value. The 8-bit
1144
  value is returned.
1145
1146
  If Value >= 0xA0, then ASSERT().
1147
  If (Value & 0x0F) >= 0x0A, then ASSERT().
1148
1149
  @param  Value The 8-bit BCD value to convert to an 8-bit value.
1150
1151
  @return The 8-bit value is returned.
1152
1153
**/
1154
UINT8
1155
EFIAPI
1156
BcdToDecimal8 (
1157
  IN      UINT8                     Value
1158
  )
1159
0
{
1160
0
  ASSERT (Value < 0xa0);
1161
0
  ASSERT ((Value & 0xf) < 0xa);
1162
0
  return (UINT8) ((Value >> 4) * 10 + (Value & 0xf));
1163
0
}