OpenCore  1.0.4
OpenCore Bootloader
Loading...
Searching...
No Matches
OcAsciiLib.c
Go to the documentation of this file.
1
15#include <Base.h>
16
17#include <Library/BaseLib.h>
18#include <Library/BaseOverflowLib.h>
19#include <Library/DebugLib.h>
20#include <Library/MemoryAllocationLib.h>
21#include <Library/PrintLib.h>
22#include <Library/OcStringLib.h>
23
24// IsAsciiPrint
25
32BOOLEAN
34 IN CHAR8 Char
35 )
36{
37 return ((Char >= ' ') && (Char < '~'));
38}
39
40// IsAsciiAlpha
41
48INTN
50 IN CHAR8 Char
51 )
52{
53 return ((Char >= 'A' && Char <= 'Z') || (Char >= 'a' && Char <= 'z'));
54}
55
56// IsAsciiSpace
57
64INTN
66 IN CHAR8 Char
67 )
68{
69 return ( (Char == ' ')
70 || (Char == '\t')
71 || (Char == '\v')
72 || (Char == '\f')
73 || (Char == '\r')
74 || (Char == '\n'));
75}
76
77BOOLEAN
79 IN CHAR8 Char
80 )
81{
82 return Char >= L'0' && Char <= L'9';
83}
84
85VOID
87 IN OUT CHAR8 *String
88 )
89{
90 CHAR8 *Needle;
91
92 Needle = String;
93 while ((Needle = AsciiStrStr (Needle, "/")) != NULL) {
94 *Needle++ = '\\';
95 }
96}
97
98VOID
100 IN OUT CHAR8 *String
101 )
102{
103 CHAR8 *Needle;
104
105 Needle = String;
106 while ((Needle = AsciiStrStr (Needle, "\\")) != NULL) {
107 *Needle++ = '/';
108 }
109}
110
118CHAR16 *
120 IN CONST CHAR8 *AsciiString,
121 IN UINTN Length
122 )
123{
124 CHAR16 *UnicodeString;
125 CHAR16 *UnicodeStringWalker;
126 UINTN UnicodeStringSize;
127
128 ASSERT (AsciiString != NULL);
129
130 if (Length == 0) {
131 Length = AsciiStrLen (AsciiString);
132 }
133
134 UnicodeStringSize = (Length + 1) * sizeof (CHAR16);
135 UnicodeString = AllocatePool (UnicodeStringSize);
136
137 if (UnicodeString != NULL) {
138 UnicodeStringWalker = UnicodeString;
139 while (*AsciiString != '\0' && Length--) {
140 *(UnicodeStringWalker++) = *(AsciiString++);
141 }
142
143 *UnicodeStringWalker = L'\0';
144 }
145
146 return UnicodeString;
147}
148
149BOOLEAN
151 OUT CHAR8 *Buffer,
152 IN UINT32 BufferSize,
153 IN UINT64 Value
154 )
155{
156 CONST UINT32 MaxShifts = (sizeof (UINT64) * 8) - 4;
157 UINT32 Index;
158 BOOLEAN Printed;
159 UINT8 Curr;
160
161 if (BufferSize < 4) {
162 return FALSE;
163 }
164
165 *Buffer++ = '0';
166 *Buffer++ = 'x';
167
168 if (Value > 0) {
169 BufferSize -= 2;
170 for (Printed = FALSE, Index = MaxShifts; Index <= MaxShifts; Index -= 4) {
171 Curr = (UINT8)(RShiftU64 (Value, Index) & 0xFU);
172 Printed |= Curr > 0;
173 if (Printed) {
174 *Buffer++ = "0123456789abcdef"[Curr];
175 if (--BufferSize == 0) {
176 return FALSE;
177 }
178 }
179 }
180 } else {
181 *Buffer++ = '0';
182 }
183
184 *Buffer++ = '\0';
185 return TRUE;
186}
187
188EFI_STATUS
189EFIAPI
191 OUT CHAR8 *StartOfBuffer,
192 IN UINTN BufferSize,
193 IN CONST CHAR8 *FormatString,
194 ...
195 )
196{
197 EFI_STATUS Status;
198 VA_LIST Marker;
199 VA_LIST Marker2;
200 UINTN NumberOfPrinted;
201
202 ASSERT (StartOfBuffer != NULL);
203 ASSERT (BufferSize > 0);
204 ASSERT (FormatString != NULL);
205
206 VA_START (Marker, FormatString);
207
208 VA_COPY (Marker2, Marker);
209 NumberOfPrinted = SPrintLengthAsciiFormat (FormatString, Marker2);
210 VA_END (Marker2);
211
212 if (BufferSize - 1 >= NumberOfPrinted) {
213 AsciiVSPrint (StartOfBuffer, BufferSize, FormatString, Marker);
214 Status = EFI_SUCCESS;
215 } else {
216 Status = EFI_OUT_OF_RESOURCES;
217 }
218
219 VA_END (Marker);
220
221 return Status;
222}
223
224INTN
225EFIAPI
227 IN CONST CHAR8 *FirstString,
228 IN CONST CHAR8 *SecondString,
229 IN UINTN Length
230 )
231{
232 CHAR8 UpperFirstString;
233 CHAR8 UpperSecondString;
234
235 if (Length == 0) {
236 return 0;
237 }
238
239 //
240 // ASSERT both strings are less long than PcdMaximumAsciiStringLength.
241 // Length tests are performed inside AsciiStrLen().
242 //
243 ASSERT (AsciiStrSize (FirstString) != 0);
244 ASSERT (AsciiStrSize (SecondString) != 0);
245
246 UpperFirstString = AsciiCharToUpper (*FirstString);
247 UpperSecondString = AsciiCharToUpper (*SecondString);
248 while ((*FirstString != '\0') &&
249 (*SecondString != '\0') &&
250 (UpperFirstString == UpperSecondString) &&
251 (Length > 1))
252 {
253 FirstString++;
254 SecondString++;
255 UpperFirstString = AsciiCharToUpper (*FirstString);
256 UpperSecondString = AsciiCharToUpper (*SecondString);
257 Length--;
258 }
259
260 return UpperFirstString - UpperSecondString;
261}
262
263BOOLEAN
264EFIAPI
266 IN CONST CHAR8 *String,
267 IN CONST CHAR8 *SearchString,
268 IN BOOLEAN CaseInsensitiveMatch
269 )
270{
271 UINTN StringLength;
272 UINTN SearchStringLength;
273
274 ASSERT (String != NULL);
275 ASSERT (SearchString != NULL);
276
277 StringLength = AsciiStrLen (String);
278 SearchStringLength = AsciiStrLen (SearchString);
279
280 if (CaseInsensitiveMatch) {
281 return StringLength >= SearchStringLength
282 && OcAsciiStrniCmp (&String[StringLength - SearchStringLength], SearchString, SearchStringLength) == 0;
283 }
284
285 return StringLength >= SearchStringLength
286 && AsciiStrnCmp (&String[StringLength - SearchStringLength], SearchString, SearchStringLength) == 0;
287}
288
289BOOLEAN
290EFIAPI
292 IN CONST CHAR8 *String,
293 IN CONST CHAR8 *SearchString,
294 IN BOOLEAN CaseInsensitiveMatch
295 )
296{
297 CHAR8 First;
298 CHAR8 Second;
299
300 ASSERT (String != NULL);
301 ASSERT (SearchString != NULL);
302
303 while (TRUE) {
304 First = *String++;
305 Second = *SearchString++;
306 if (Second == '\0') {
307 return TRUE;
308 }
309
310 if (First == '\0') {
311 return FALSE;
312 }
313
314 if (CaseInsensitiveMatch) {
315 First = AsciiCharToUpper (First);
316 Second = AsciiCharToUpper (Second);
317 }
318
319 if (First != Second) {
320 return FALSE;
321 }
322 }
323}
324
325CHAR8 *
326EFIAPI
328 IN CONST CHAR8 *String,
329 IN CONST CHAR8 *SearchString
330 )
331{
332 CONST CHAR8 *FirstMatch;
333 CONST CHAR8 *SearchStringTmp;
334
335 ASSERT (AsciiStrSize (String) != 0);
336 ASSERT (AsciiStrSize (SearchString) != 0);
337
338 if (*SearchString == '\0') {
339 return (CHAR8 *)String;
340 }
341
342 while (*String != '\0') {
343 SearchStringTmp = SearchString;
344 FirstMatch = String;
345
346 while ( (AsciiCharToUpper (*String) == AsciiCharToUpper (*SearchStringTmp))
347 && (*String != '\0'))
348 {
349 String++;
350 SearchStringTmp++;
351 }
352
353 if (*SearchStringTmp == '\0') {
354 return (CHAR8 *)FirstMatch;
355 }
356
357 if (*String == '\0') {
358 return NULL;
359 }
360
361 String = FirstMatch + 1;
362 }
363
364 return NULL;
365}
366
367CHAR8 *
368EFIAPI
370 IN CONST CHAR8 *String,
371 IN CHAR8 Char
372 )
373{
374 ASSERT (AsciiStrSize (String) != 0);
375
376 while (*String != '\0') {
377 //
378 // Return immediately when matching first occurrence of Char.
379 //
380 if (*String == Char) {
381 return (CHAR8 *)String;
382 }
383
384 ++String;
385 }
386
387 return NULL;
388}
389
390CHAR8 *
391EFIAPI
393 IN CONST CHAR8 *String,
394 IN CHAR8 Char
395 )
396{
397 CHAR8 *Save;
398
399 ASSERT (AsciiStrSize (String) != 0);
400
401 Save = NULL;
402
403 while (*String != '\0') {
404 //
405 // Record the last occurrence of Char.
406 //
407 if (*String == Char) {
408 Save = (CHAR8 *)String;
409 }
410
411 ++String;
412 }
413
414 return Save;
415}
416
417BOOLEAN
419 IN CONST CHAR8 *String,
420 IN UINTN Number
421 )
422{
423 UINTN Index;
424
425 for (Index = 0; Index < Number; ++Index) {
426 //
427 // Terminate search if non-printable character is found.
428 // The next IFs determine the return value.
429 //
430 if (!IsAsciiPrint (String[Index])) {
431 break;
432 }
433 }
434
435 //
436 // If the loop above can be terminated without errors, Index should equal to ValueSize.
437 // And this is all good.
438 //
439 if (Index == Number) {
440 return TRUE;
441 }
442
443 //
444 // If the last character is not ASCII-printable but is '\0', then it is fine.
445 //
446 if (Index == Number - 1) {
447 return String[Index] == '\0';
448 }
449
450 //
451 // Otherwise, the string is broken.
452 //
453 return FALSE;
454}
455
456EFI_STATUS
457EFIAPI
459 IN CONST CHAR8 *String,
460 OUT GUID *Guid
461 )
462{
463 EFI_STATUS Status;
464
465 Status = AsciiStrToGuid (String, Guid);
466 if (EFI_ERROR (Status)) {
467 return Status;
468 }
469
470 //
471 // Swap little endian numbers to their big endian counter parts.
472 //
473 Guid->Data1 = SwapBytes32 (Guid->Data1);
474 Guid->Data2 = SwapBytes16 (Guid->Data2);
475 Guid->Data3 = SwapBytes16 (Guid->Data3);
476 return EFI_SUCCESS;
477}
478
479VOID
481 IN OUT CHAR8 *String,
482 IN BOOLEAN SingleLine
483 )
484{
485 while (*String != L'\0') {
486 if ((*String & 0x7F) != *String) {
487 //
488 // Remove all unicode characters.
489 //
490 *String = '_';
491 } else if (SingleLine && ((*String == '\r') || (*String == '\n'))) {
492 //
493 // Stop after printing one line.
494 //
495 *String = '\0';
496 break;
497 } else if ((*String < 0x20) || (*String == 0x7F)) {
498 //
499 // Drop all unprintable spaces but space including tabs.
500 //
501 *String = '_';
502 }
503
504 ++String;
505 }
506}
507
508VOID
509EFIAPI
511 IN OUT CHAR8 **AsciiBuffer,
512 IN OUT UINTN *AsciiBufferSize,
513 IN CONST CHAR8 *FormatString,
514 ...
515 )
516{
517 EFI_STATUS Status;
518 VA_LIST Marker;
519 CHAR8 Tmp[256];
520 CHAR8 *NewBuffer;
521 UINTN NewBufferSize;
522
523 if (*AsciiBuffer == NULL) {
524 return;
525 }
526
527 VA_START (Marker, FormatString);
528 AsciiVSPrint (Tmp, sizeof (Tmp), FormatString, Marker);
529 VA_END (Marker);
530
531 Status = AsciiStrCatS (*AsciiBuffer, *AsciiBufferSize, Tmp);
532 if (Status == EFI_BUFFER_TOO_SMALL) {
533 if (BaseOverflowMulUN (*AsciiBufferSize, 2, &NewBufferSize)) {
534 return;
535 }
536
537 NewBuffer = ReallocatePool (*AsciiBufferSize, NewBufferSize, *AsciiBuffer);
538 if (NewBuffer == NULL) {
539 FreePool (*AsciiBuffer);
540
541 *AsciiBuffer = NULL;
542 *AsciiBufferSize = 0;
543
544 return;
545 }
546
547 *AsciiBuffer = NewBuffer;
548 *AsciiBufferSize = NewBufferSize;
549
550 AsciiStrCatS (*AsciiBuffer, *AsciiBufferSize, Tmp);
551 }
552}
553
554CHAR8 *
556 CHAR8 *Str
557 )
558{
559 UINTN Index;
560
561 ASSERT (Str != NULL);
562
563 for (Index = 0; Str[Index] != '\0'; ++Index) {
564 if ((Str[Index] >= 'A') && (Str[Index] <= 'Z')) {
565 Str[Index] -= ('A' - 'a');
566 }
567 }
568
569 return Str;
570}
UINT64 Length
BOOLEAN OcAsciiStringNPrintable(IN CONST CHAR8 *String, IN UINTN Number)
Definition OcAsciiLib.c:418
VOID AsciiFilterString(IN OUT CHAR8 *String, IN BOOLEAN SingleLine)
Definition OcAsciiLib.c:480
INTN IsAsciiAlpha(IN CHAR8 Char)
Definition OcAsciiLib.c:49
CHAR16 * AsciiStrCopyToUnicode(IN CONST CHAR8 *AsciiString, IN UINTN Length)
Definition OcAsciiLib.c:119
VOID EFIAPI OcAsciiPrintBuffer(IN OUT CHAR8 **AsciiBuffer, IN OUT UINTN *AsciiBufferSize, IN CONST CHAR8 *FormatString,...)
Definition OcAsciiLib.c:510
CHAR8 * OcAsciiToLower(CHAR8 *Str)
Definition OcAsciiLib.c:555
VOID AsciiUefiSlashes(IN OUT CHAR8 *String)
Definition OcAsciiLib.c:86
VOID AsciiUnixSlashes(IN OUT CHAR8 *String)
Definition OcAsciiLib.c:99
CHAR8 *EFIAPI OcAsciiStrrChr(IN CONST CHAR8 *String, IN CHAR8 Char)
Definition OcAsciiLib.c:392
BOOLEAN IsAsciiNumber(IN CHAR8 Char)
Definition OcAsciiLib.c:78
EFI_STATUS EFIAPI OcAsciiStrToRawGuid(IN CONST CHAR8 *String, OUT GUID *Guid)
Definition OcAsciiLib.c:458
BOOLEAN IsAsciiPrint(IN CHAR8 Char)
Definition OcAsciiLib.c:33
CHAR8 *EFIAPI OcAsciiStrChr(IN CONST CHAR8 *String, IN CHAR8 Char)
Definition OcAsciiLib.c:369
BOOLEAN EFIAPI OcAsciiEndsWith(IN CONST CHAR8 *String, IN CONST CHAR8 *SearchString, IN BOOLEAN CaseInsensitiveMatch)
Definition OcAsciiLib.c:265
EFI_STATUS EFIAPI OcAsciiSafeSPrint(OUT CHAR8 *StartOfBuffer, IN UINTN BufferSize, IN CONST CHAR8 *FormatString,...)
Definition OcAsciiLib.c:190
INTN IsAsciiSpace(IN CHAR8 Char)
Definition OcAsciiLib.c:65
CHAR8 *EFIAPI OcAsciiStriStr(IN CONST CHAR8 *String, IN CONST CHAR8 *SearchString)
Definition OcAsciiLib.c:327
INTN EFIAPI OcAsciiStrniCmp(IN CONST CHAR8 *FirstString, IN CONST CHAR8 *SecondString, IN UINTN Length)
Definition OcAsciiLib.c:226
BOOLEAN AsciiUint64ToLowerHex(OUT CHAR8 *Buffer, IN UINT32 BufferSize, IN UINT64 Value)
Definition OcAsciiLib.c:150
BOOLEAN EFIAPI OcAsciiStartsWith(IN CONST CHAR8 *String, IN CONST CHAR8 *SearchString, IN BOOLEAN CaseInsensitiveMatch)
Definition OcAsciiLib.c:291
OC_TYPING_BUFFER_ENTRY Buffer[OC_TYPING_BUFFER_SIZE]
Definition OcTypingLib.h:42
UINT64 EFIAPI RShiftU64(IN UINT64 Operand, IN UINTN Count)
Definition UserMath.c:86
#define ASSERT(x)
Definition coder.h:55