2 This library parses the INI configuration file.
4 The INI file format is:
11 1) SectionName is an ASCII string. The valid format is [A-Za-z0-9_]+
12 2) EntryName is an ASCII string. The valid format is [A-Za-z0-9_]+
14 3.1) an ASCII String. The valid format is [A-Za-z0-9_]+
15 3.2) a GUID. The valid format is xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, where x is [A-Fa-f0-9]
16 3.3) a decimal value. The valid format is [0-9]+
17 3.4) a heximal value. The valid format is 0x[A-Fa-f0-9]+
18 4) '#' or ';' can be used as comment at anywhere.
19 5) TAB(0x20) or SPACE(0x9) can be used as separator.
20 6) LF(\n, 0xA) or CR(\r, 0xD) can be used as line break.
22 Caution: This module requires additional review when modified.
23 This driver will have external input - INI data file.
25 OpenIniFile(), PreProcessDataFile(), ProfileGetSection(), ProfileGetEntry()
26 will receive untrusted input and do basic validation.
28 Copyright (c) 2016, Intel Corporation. All rights reserved.<BR>
30 This program and the accompanying materials
31 are licensed and made available under the terms and conditions
32 of the BSD License which accompanies this distribution. The
33 full text of the license may be found at
34 http://opensource.org/licenses/bsd-license.php
36 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
37 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
42 #include <Library/BaseLib.h>
43 #include <Library/BaseMemoryLib.h>
44 #include <Library/DebugLib.h>
45 #include <Library/MemoryAllocationLib.h>
47 #define IS_HYPHEN(a) ((a) == '-')
48 #define IS_NULL(a) ((a) == '\0')
50 // This is default allocation. Reallocation will happen if it is not enough.
51 #define MAX_LINE_LENGTH 512
53 typedef struct _INI_SECTION_ITEM SECTION_ITEM
;
54 struct _INI_SECTION_ITEM
{
59 SECTION_ITEM
*PtrNext
;
62 typedef struct _INI_COMMENT_LINE COMMENT_LINE
;
63 struct _INI_COMMENT_LINE
{
65 COMMENT_LINE
*PtrNext
;
69 SECTION_ITEM
*SectionHead
;
70 COMMENT_LINE
*CommentHead
;
71 } INI_PARSING_LIB_CONTEXT
;
74 Return if the digital char is valid.
76 @param[in] DigitalChar The digital char to be checked.
77 @param[in] IncludeHex If it include HEX char.
79 @retval TRUE The digital char is valid.
80 @retval FALSE The digital char is invalid.
88 if (DigitalChar
>= '0' && DigitalChar
<= '9') {
92 if (DigitalChar
>= 'a' && DigitalChar
<= 'f') {
95 if (DigitalChar
>= 'A' && DigitalChar
<= 'F') {
103 Return if the name char is valid.
105 @param[in] NameChar The name char to be checked.
107 @retval TRUE The name char is valid.
108 @retval FALSE The name char is invalid.
115 if (NameChar
>= 'a' && NameChar
<= 'z') {
118 if (NameChar
>= 'A' && NameChar
<= 'Z') {
121 if (NameChar
>= '0' && NameChar
<= '9') {
124 if (NameChar
== '_') {
131 Return if the digital string is valid.
133 @param[in] Digital The digital to be checked.
134 @param[in] Length The length of digital string in bytes.
135 @param[in] IncludeHex If it include HEX char.
137 @retval TRUE The digital string is valid.
138 @retval FALSE The digital string is invalid.
144 IN BOOLEAN IncludeHex
148 for (Index
= 0; Index
< Length
; Index
++) {
149 if (!IsValidDigitalChar(Digital
[Index
], IncludeHex
)) {
157 Return if the decimal string is valid.
159 @param[in] Decimal The decimal string to be checked.
160 @param[in] Length The length of decimal string in bytes.
162 @retval TRUE The decimal string is valid.
163 @retval FALSE The decimal string is invalid.
166 IsValidDecimalString (
171 return IsValidDigital(Decimal
, Length
, FALSE
);
175 Return if the heximal string is valid.
177 @param[in] Hex The heximal string to be checked.
178 @param[in] Length The length of heximal string in bytes.
180 @retval TRUE The heximal string is valid.
181 @retval FALSE The heximal string is invalid.
195 if (Hex
[1] != 'x' && Hex
[1] != 'X') {
198 return IsValidDigital(&Hex
[2], Length
- 2, TRUE
);
202 Return if the name string is valid.
204 @param[in] Name The name to be checked.
205 @param[in] Length The length of name string in bytes.
207 @retval TRUE The name string is valid.
208 @retval FALSE The name string is invalid.
217 for (Index
= 0; Index
< Length
; Index
++) {
218 if (!IsValidNameChar(Name
[Index
])) {
226 Return if the value string is valid GUID.
228 @param[in] Value The value to be checked.
229 @param[in] Length The length of value string in bytes.
231 @retval TRUE The value string is valid GUID.
232 @retval FALSE The value string is invalid GUID.
240 if (Length
!= sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") - 1) {
243 if (!IS_HYPHEN(Value
[8])) {
246 if (!IS_HYPHEN(Value
[13])) {
249 if (!IS_HYPHEN(Value
[18])) {
252 if (!IS_HYPHEN(Value
[23])) {
255 if (!IsValidDigital(&Value
[0], 8, TRUE
)) {
258 if (!IsValidDigital(&Value
[9], 4, TRUE
)) {
261 if (!IsValidDigital(&Value
[14], 4, TRUE
)) {
264 if (!IsValidDigital(&Value
[19], 4, TRUE
)) {
267 if (!IsValidDigital(&Value
[24], 12, TRUE
)) {
274 Return if the value string is valid.
276 @param[in] Value The value to be checked.
277 @param[in] Length The length of value string in bytes.
279 @retval TRUE The name string is valid.
280 @retval FALSE The name string is invalid.
288 if (IsValidName(Value
, Length
) || IsValidGuid(Value
, Length
)) {
295 Dump an INI config file context.
297 @param[in] Context INI Config file context.
304 INI_PARSING_LIB_CONTEXT
*IniContext
;
305 SECTION_ITEM
*PtrSection
;
306 SECTION_ITEM
*Section
;
308 if (Context
== NULL
) {
312 IniContext
= Context
;
313 Section
= IniContext
->SectionHead
;
315 while (Section
!= NULL
) {
316 PtrSection
= Section
;
317 Section
= Section
->PtrNext
;
318 if (PtrSection
->PtrSection
!= NULL
) {
319 DEBUG((DEBUG_VERBOSE
, "Section - %a\n", PtrSection
->PtrSection
));
321 if (PtrSection
->PtrEntry
!= NULL
) {
322 DEBUG ((DEBUG_VERBOSE
, " Entry - %a\n", PtrSection
->PtrEntry
));
324 if (PtrSection
->PtrValue
!= NULL
) {
325 DEBUG((DEBUG_VERBOSE
, " Value - %a\n", PtrSection
->PtrValue
));
331 Copy one line data from buffer data to the line buffer.
333 @param[in] Buffer Buffer data.
334 @param[in] BufferSize Buffer Size.
335 @param[in, out] LineBuffer Line buffer to store the found line data.
336 @param[in, out] LineSize On input, size of the input line buffer.
337 On output, size of the actual line buffer.
339 @retval EFI_BUFFER_TOO_SMALL The size of input line buffer is not enough.
340 @retval EFI_SUCCESS Copy line data into the line buffer.
347 IN OUT UINT8
*LineBuffer
,
348 IN OUT UINTN
*LineSize
356 PtrEnd
= (UINTN
)Buffer
+ BufferSize
;
359 // 0x0D indicates a line break. Otherwise there is no line break
361 while ((UINTN
)PtrBuf
< PtrEnd
) {
362 if (*PtrBuf
== 0x0D || *PtrBuf
== 0x0A) {
368 if ((UINTN
)PtrBuf
>= (PtrEnd
- 1)) {
370 // The buffer ends without any line break
371 // or it is the last character of the buffer
374 } else if (*(PtrBuf
+ 1) == 0x0A) {
376 // Further check if a 0x0A follows. If yes, count 0xA
378 Length
= (UINTN
) PtrBuf
- (UINTN
) Buffer
+ 2;
380 Length
= (UINTN
) PtrBuf
- (UINTN
) Buffer
+ 1;
383 if (Length
> (*LineSize
)) {
385 return EFI_BUFFER_TOO_SMALL
;
388 SetMem (LineBuffer
, *LineSize
, 0x0);
390 CopyMem (LineBuffer
, Buffer
, Length
);
396 Trim Buffer by removing all CR, LF, TAB, and SPACE chars in its head and tail.
398 @param[in, out] Buffer On input, buffer data to be trimed.
399 On output, the trimmed buffer.
400 @param[in, out] BufferSize On input, size of original buffer data.
401 On output, size of the trimmed buffer.
406 IN OUT UINT8
*Buffer
,
407 IN OUT UINTN
*BufferSize
414 if (*BufferSize
== 0) {
419 // Trim the tail first, include CR, LF, TAB, and SPACE.
421 Length
= *BufferSize
;
422 PtrBuf
= (UINT8
*) ((UINTN
) Buffer
+ Length
- 1);
423 while (PtrBuf
>= Buffer
) {
424 if ((*PtrBuf
!= 0x0D) && (*PtrBuf
!= 0x0A )
425 && (*PtrBuf
!= 0x20) && (*PtrBuf
!= 0x09)) {
432 // all spaces, a blank line, return directly;
434 if (PtrBuf
< Buffer
) {
439 Length
= (UINTN
)PtrBuf
- (UINTN
)Buffer
+ 1;
444 // Now skip the heading CR, LF, TAB and SPACE
446 while (PtrBuf
<= PtrEnd
) {
447 if ((*PtrBuf
!= 0x0D) && (*PtrBuf
!= 0x0A )
448 && (*PtrBuf
!= 0x20) && (*PtrBuf
!= 0x09)) {
455 // If no heading CR, LF, TAB or SPACE, directly return
457 if (PtrBuf
== Buffer
) {
458 *BufferSize
= Length
;
462 *BufferSize
= (UINTN
)PtrEnd
- (UINTN
)PtrBuf
+ 1;
465 // The first Buffer..PtrBuf characters are CR, LF, TAB or SPACE.
466 // Now move out all these characters.
468 while (PtrBuf
<= PtrEnd
) {
478 Insert new comment item into comment head.
480 @param[in] Buffer Comment buffer to be added.
481 @param[in] BufferSize Size of comment buffer.
482 @param[in, out] CommentHead Comment Item head entry.
484 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.
485 @retval EFI_SUCCESS New comment item is inserted.
492 IN OUT COMMENT_LINE
**CommentHead
495 COMMENT_LINE
*CommentItem
;
498 CommentItem
= AllocatePool (sizeof (COMMENT_LINE
));
499 if (CommentItem
== NULL
) {
500 return EFI_OUT_OF_RESOURCES
;
503 CommentItem
->PtrNext
= *CommentHead
;
504 *CommentHead
= CommentItem
;
507 // Add a trailing '\0'
509 CommentItem
->PtrComment
= AllocatePool (BufferSize
+ 1);
510 if (CommentItem
->PtrComment
== NULL
) {
511 FreePool (CommentItem
);
512 return EFI_OUT_OF_RESOURCES
;
514 CopyMem (CommentItem
->PtrComment
, Buffer
, BufferSize
);
515 *(CommentItem
->PtrComment
+ BufferSize
) = '\0';
521 Add new section item into Section head.
523 @param[in] Buffer Section item data buffer.
524 @param[in] BufferSize Size of section item.
525 @param[in, out] SectionHead Section item head entry.
527 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.
528 @retval EFI_SUCCESS Section item is NULL or Section item is added.
535 IN OUT SECTION_ITEM
**SectionHead
538 SECTION_ITEM
*SectionItem
;
543 ASSERT(BufferSize
>= 1);
545 // The first character of Buffer is '[', now we want for ']'
547 PtrEnd
= (UINT8
*)((UINTN
)Buffer
+ BufferSize
- 1);
548 PtrBuf
= (UINT8
*)((UINTN
)Buffer
+ 1);
549 while (PtrBuf
<= PtrEnd
) {
550 if (*PtrBuf
== ']') {
555 if (PtrBuf
> PtrEnd
) {
557 // Not found. Invalid line
559 return EFI_NOT_FOUND
;
561 if (PtrBuf
<= Buffer
+ 1) {
563 return EFI_NOT_FOUND
;
567 // excluding the heading '[' and tailing ']'
569 Length
= PtrBuf
- Buffer
- 1;
576 // Invalid line if the section name is null
579 return EFI_NOT_FOUND
;
582 if (!IsValidName((CHAR8
*)Buffer
+ 1, Length
)) {
583 return EFI_INVALID_PARAMETER
;
586 SectionItem
= AllocatePool (sizeof (SECTION_ITEM
));
587 if (SectionItem
== NULL
) {
588 return EFI_OUT_OF_RESOURCES
;
591 SectionItem
->PtrSection
= NULL
;
592 SectionItem
->SecNameLen
= Length
;
593 SectionItem
->PtrEntry
= NULL
;
594 SectionItem
->PtrValue
= NULL
;
595 SectionItem
->PtrNext
= *SectionHead
;
596 *SectionHead
= SectionItem
;
599 // Add a trailing '\0'
601 SectionItem
->PtrSection
= AllocatePool (Length
+ 1);
602 if (SectionItem
->PtrSection
== NULL
) {
603 return EFI_OUT_OF_RESOURCES
;
607 // excluding the heading '['
609 CopyMem (SectionItem
->PtrSection
, Buffer
+ 1, Length
);
610 *(SectionItem
->PtrSection
+ Length
) = '\0';
616 Add new section entry and entry value into Section head.
618 @param[in] Buffer Section entry data buffer.
619 @param[in] BufferSize Size of section entry.
620 @param[in, out] SectionHead Section item head entry.
622 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.
623 @retval EFI_SUCCESS Section entry is added.
624 @retval EFI_NOT_FOUND Section entry is not found.
625 @retval EFI_INVALID_PARAMETER Section entry is invalid.
632 IN OUT SECTION_ITEM
**SectionHead
636 SECTION_ITEM
*SectionItem
;
637 SECTION_ITEM
*PtrSection
;
642 Status
= EFI_SUCCESS
;
644 PtrEnd
= (UINT8
*) ((UINTN
)Buffer
+ BufferSize
- 1);
647 // First search for '='
649 while (PtrBuf
<= PtrEnd
) {
650 if (*PtrBuf
== '=') {
655 if (PtrBuf
> PtrEnd
) {
657 // Not found. Invalid line
659 return EFI_NOT_FOUND
;
661 if (PtrBuf
<= Buffer
) {
663 return EFI_NOT_FOUND
;
667 // excluding the tailing '='
669 Length
= PtrBuf
- Buffer
;
676 // Invalid line if the entry name is null
679 return EFI_NOT_FOUND
;
682 if (!IsValidName((CHAR8
*)Buffer
, Length
)) {
683 return EFI_INVALID_PARAMETER
;
687 // Omit this line if no section header has been found before
689 if (*SectionHead
== NULL
) {
692 PtrSection
= *SectionHead
;
694 SectionItem
= AllocatePool (sizeof (SECTION_ITEM
));
695 if (SectionItem
== NULL
) {
696 return EFI_OUT_OF_RESOURCES
;
699 SectionItem
->PtrSection
= NULL
;
700 SectionItem
->PtrEntry
= NULL
;
701 SectionItem
->PtrValue
= NULL
;
702 SectionItem
->SecNameLen
= PtrSection
->SecNameLen
;
703 SectionItem
->PtrNext
= *SectionHead
;
704 *SectionHead
= SectionItem
;
707 // SectionName, add a trailing '\0'
709 SectionItem
->PtrSection
= AllocatePool (PtrSection
->SecNameLen
+ 1);
710 if (SectionItem
->PtrSection
== NULL
) {
711 return EFI_OUT_OF_RESOURCES
;
713 CopyMem (SectionItem
->PtrSection
, PtrSection
->PtrSection
, PtrSection
->SecNameLen
+ 1);
716 // EntryName, add a trailing '\0'
718 SectionItem
->PtrEntry
= AllocatePool (Length
+ 1);
719 if (SectionItem
->PtrEntry
== NULL
) {
720 FreePool(SectionItem
->PtrSection
);
721 return EFI_OUT_OF_RESOURCES
;
723 CopyMem (SectionItem
->PtrEntry
, Buffer
, Length
);
724 *(SectionItem
->PtrEntry
+ Length
) = '\0';
727 // Next search for '#' or ';'
731 while (PtrBuf
<= PtrEnd
) {
732 if (*PtrBuf
== '#' || *PtrBuf
== ';') {
737 if (PtrBuf
<= Buffer
) {
739 FreePool(SectionItem
->PtrEntry
);
740 FreePool(SectionItem
->PtrSection
);
741 return EFI_NOT_FOUND
;
743 Length
= PtrBuf
- Buffer
;
750 // Invalid line if the entry value is null
753 FreePool(SectionItem
->PtrEntry
);
754 FreePool(SectionItem
->PtrSection
);
755 return EFI_NOT_FOUND
;
758 if (!IsValidValue((CHAR8
*)Buffer
, Length
)) {
759 FreePool(SectionItem
->PtrEntry
);
760 FreePool(SectionItem
->PtrSection
);
761 return EFI_INVALID_PARAMETER
;
765 // EntryValue, add a trailing '\0'
767 SectionItem
->PtrValue
= AllocatePool (Length
+ 1);
768 if (SectionItem
->PtrValue
== NULL
) {
769 FreePool(SectionItem
->PtrEntry
);
770 FreePool(SectionItem
->PtrSection
);
771 return EFI_OUT_OF_RESOURCES
;
773 CopyMem (SectionItem
->PtrValue
, Buffer
, Length
);
774 *(SectionItem
->PtrValue
+ Length
) = '\0';
780 Free all comment entry and section entry.
782 @param[in] Section Section entry list.
783 @param[in] Comment Comment entry list.
788 IN SECTION_ITEM
*Section
,
789 IN COMMENT_LINE
*Comment
792 SECTION_ITEM
*PtrSection
;
793 COMMENT_LINE
*PtrComment
;
795 while (Section
!= NULL
) {
796 PtrSection
= Section
;
797 Section
= Section
->PtrNext
;
798 if (PtrSection
->PtrEntry
!= NULL
) {
799 FreePool (PtrSection
->PtrEntry
);
801 if (PtrSection
->PtrSection
!= NULL
) {
802 FreePool (PtrSection
->PtrSection
);
804 if (PtrSection
->PtrValue
!= NULL
) {
805 FreePool (PtrSection
->PtrValue
);
807 FreePool (PtrSection
);
810 while (Comment
!= NULL
) {
811 PtrComment
= Comment
;
812 Comment
= Comment
->PtrNext
;
813 if (PtrComment
->PtrComment
!= NULL
) {
814 FreePool (PtrComment
->PtrComment
);
816 FreePool (PtrComment
);
823 Get section entry value.
825 @param[in] Section Section entry list.
826 @param[in] SectionName Section name.
827 @param[in] EntryName Section entry name.
828 @param[out] EntryValue Point to the got entry value.
830 @retval EFI_NOT_FOUND Section is not found.
831 @retval EFI_SUCCESS Section entry value is got.
835 UpdateGetProfileString (
836 IN SECTION_ITEM
*Section
,
837 IN CHAR8
*SectionName
,
839 OUT CHAR8
**EntryValue
844 while (Section
!= NULL
) {
845 if (AsciiStrCmp ((CONST CHAR8
*) Section
->PtrSection
, (CONST CHAR8
*) SectionName
) == 0) {
846 if (Section
->PtrEntry
!= NULL
) {
847 if (AsciiStrCmp ((CONST CHAR8
*) Section
->PtrEntry
, (CONST CHAR8
*) EntryName
) == 0) {
852 Section
= Section
->PtrNext
;
855 if (Section
== NULL
) {
856 return EFI_NOT_FOUND
;
859 *EntryValue
= Section
->PtrValue
;
865 Converts a list of string to a specified buffer.
867 @param[out] Buf The output buffer that contains the string.
868 @param[in] BufferLength The length of the buffer
869 @param[in] Str The input string that contains the hex number
871 @retval EFI_SUCCESS The string was successfully converted to the buffer.
877 IN UINTN BufferLength
,
889 // Two hex char make up one byte
891 StrLength
= BufferLength
* 2;
893 for(Index
= 0; Index
< StrLength
; Index
++, Str
++) {
895 if ((*Str
>= 'a') && (*Str
<= 'f')) {
896 Digit
= (UINT8
) (*Str
- 'a' + 0x0A);
897 } else if ((*Str
>= 'A') && (*Str
<= 'F')) {
898 Digit
= (UINT8
) (*Str
- 'A' + 0x0A);
899 } else if ((*Str
>= '0') && (*Str
<= '9')) {
900 Digit
= (UINT8
) (*Str
- '0');
902 return EFI_INVALID_PARAMETER
;
906 // For odd characters, write the upper nibble for each buffer byte,
907 // and for even characters, the lower nibble.
909 if ((Index
& 1) == 0) {
910 Byte
= (UINT8
) (Digit
<< 4);
912 Byte
= Buf
[Index
/ 2];
914 Byte
= (UINT8
) (Byte
| Digit
);
917 Buf
[Index
/ 2] = Byte
;
924 Converts a string to GUID value.
925 Guid Format is xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
927 @param[in] Str The registry format GUID string that contains the GUID value.
928 @param[out] Guid A pointer to the converted GUID value.
930 @retval EFI_SUCCESS The GUID string was successfully converted to the GUID value.
931 @retval EFI_UNSUPPORTED The input string is not in registry format.
932 @return others Some error occurred when converting part of GUID value.
942 // Get the first UINT32 data
944 Guid
->Data1
= (UINT32
) AsciiStrHexToUint64 (Str
);
945 while (!IS_HYPHEN (*Str
) && !IS_NULL (*Str
)) {
949 if (IS_HYPHEN (*Str
)) {
952 return EFI_UNSUPPORTED
;
956 // Get the second UINT16 data
958 Guid
->Data2
= (UINT16
) AsciiStrHexToUint64 (Str
);
959 while (!IS_HYPHEN (*Str
) && !IS_NULL (*Str
)) {
963 if (IS_HYPHEN (*Str
)) {
966 return EFI_UNSUPPORTED
;
970 // Get the third UINT16 data
972 Guid
->Data3
= (UINT16
) AsciiStrHexToUint64 (Str
);
973 while (!IS_HYPHEN (*Str
) && !IS_NULL (*Str
)) {
977 if (IS_HYPHEN (*Str
)) {
980 return EFI_UNSUPPORTED
;
984 // Get the following 8 bytes data
986 AsciiStrToBuf (&Guid
->Data4
[0], 2, Str
);
988 // Skip 2 byte hex chars
992 if (IS_HYPHEN (*Str
)) {
995 return EFI_UNSUPPORTED
;
997 AsciiStrToBuf (&Guid
->Data4
[2], 6, Str
);
1003 Pre process config data buffer into Section entry list and Comment entry list.
1005 @param[in] DataBuffer Config raw file buffer.
1006 @param[in] BufferSize Size of raw buffer.
1007 @param[in, out] SectionHead Pointer to the section entry list.
1008 @param[in, out] CommentHead Pointer to the comment entry list.
1010 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.
1011 @retval EFI_SUCCESS Config data buffer is preprocessed.
1012 @retval EFI_NOT_FOUND Config data buffer is invalid, because Section or Entry is not found.
1013 @retval EFI_INVALID_PARAMETER Config data buffer is invalid, because Section or Entry is invalid.
1017 PreProcessDataFile (
1018 IN UINT8
*DataBuffer
,
1019 IN UINTN BufferSize
,
1020 IN OUT SECTION_ITEM
**SectionHead
,
1021 IN OUT COMMENT_LINE
**CommentHead
1031 UINTN MaxLineLength
;
1033 *SectionHead
= NULL
;
1034 *CommentHead
= NULL
;
1035 BufferEnd
= (CHAR8
*) ( (UINTN
) DataBuffer
+ BufferSize
);
1036 CurrentPtr
= (CHAR8
*) DataBuffer
;
1037 MaxLineLength
= MAX_LINE_LENGTH
;
1038 Status
= EFI_SUCCESS
;
1040 PtrLine
= AllocatePool (MaxLineLength
);
1041 if (PtrLine
== NULL
) {
1042 return EFI_OUT_OF_RESOURCES
;
1045 while (CurrentPtr
< BufferEnd
) {
1046 Source
= CurrentPtr
;
1047 SourceLength
= (UINTN
)BufferEnd
- (UINTN
)CurrentPtr
;
1048 LineLength
= MaxLineLength
;
1050 // With the assumption that line length is less than 512
1051 // characters. Otherwise BUFFER_TOO_SMALL will be returned.
1053 Status
= ProfileGetLine (
1059 if (EFI_ERROR (Status
)) {
1060 if (Status
== EFI_BUFFER_TOO_SMALL
) {
1062 // If buffer too small, re-allocate the buffer according
1063 // to the returned LineLength and try again.
1067 PtrLine
= AllocatePool (LineLength
);
1068 if (PtrLine
== NULL
) {
1069 Status
= EFI_OUT_OF_RESOURCES
;
1072 SourceLength
= LineLength
;
1073 Status
= ProfileGetLine (
1079 if (EFI_ERROR (Status
)) {
1082 MaxLineLength
= LineLength
;
1087 CurrentPtr
= (CHAR8
*) ( (UINTN
) CurrentPtr
+ LineLength
);
1090 // Line got. Trim the line before processing it.
1100 if (LineLength
== 0) {
1104 if (PtrLine
[0] == '#' || PtrLine
[0] == ';') {
1105 Status
= ProfileGetComments (
1110 } else if (PtrLine
[0] == '[') {
1111 Status
= ProfileGetSection (
1117 Status
= ProfileGetEntry (
1124 if (EFI_ERROR (Status
)) {
1138 Open an INI config file and return a context.
1140 @param[in] DataBuffer Config raw file buffer.
1141 @param[in] BufferSize Size of raw buffer.
1143 @return Config data buffer is opened and context is returned.
1144 @retval NULL No enough memory is allocated.
1145 @retval NULL Config data buffer is invalid.
1150 IN UINT8
*DataBuffer
,
1155 INI_PARSING_LIB_CONTEXT
*IniContext
;
1157 if (DataBuffer
== NULL
|| BufferSize
== 0) {
1161 IniContext
= AllocateZeroPool(sizeof(INI_PARSING_LIB_CONTEXT
));
1162 if (IniContext
== NULL
) {
1167 // First process the data buffer and get all sections and entries
1169 Status
= PreProcessDataFile (
1172 &IniContext
->SectionHead
,
1173 &IniContext
->CommentHead
1175 if (EFI_ERROR(Status
)) {
1176 FreePool(IniContext
);
1179 DEBUG_CODE_BEGIN ();
1180 DumpIniSection(IniContext
);
1186 Get section entry string value.
1188 @param[in] Context INI Config file context.
1189 @param[in] SectionName Section name.
1190 @param[in] EntryName Section entry name.
1191 @param[out] EntryValue Point to the got entry string value.
1193 @retval EFI_SUCCESS Section entry string value is got.
1194 @retval EFI_NOT_FOUND Section is not found.
1198 GetStringFromDataFile(
1200 IN CHAR8
*SectionName
,
1201 IN CHAR8
*EntryName
,
1202 OUT CHAR8
**EntryValue
1205 INI_PARSING_LIB_CONTEXT
*IniContext
;
1208 if (Context
== NULL
|| SectionName
== NULL
|| EntryName
== NULL
|| EntryValue
== NULL
) {
1209 return EFI_INVALID_PARAMETER
;
1212 IniContext
= Context
;
1215 Status
= UpdateGetProfileString (
1216 IniContext
->SectionHead
,
1225 Get section entry GUID value.
1227 @param[in] Context INI Config file context.
1228 @param[in] SectionName Section name.
1229 @param[in] EntryName Section entry name.
1230 @param[out] Guid Point to the got GUID value.
1232 @retval EFI_SUCCESS Section entry GUID value is got.
1233 @retval EFI_NOT_FOUND Section is not found.
1237 GetGuidFromDataFile (
1239 IN CHAR8
*SectionName
,
1240 IN CHAR8
*EntryName
,
1247 if (Context
== NULL
|| SectionName
== NULL
|| EntryName
== NULL
|| Guid
== NULL
) {
1248 return EFI_INVALID_PARAMETER
;
1251 Status
= GetStringFromDataFile(
1257 if (EFI_ERROR(Status
)) {
1258 return EFI_NOT_FOUND
;
1260 ASSERT (Value
!= NULL
);
1261 if (!IsValidGuid(Value
, AsciiStrLen(Value
))) {
1262 return EFI_NOT_FOUND
;
1264 Status
= AsciiStrToGuid(Value
, Guid
);
1265 if (EFI_ERROR (Status
)) {
1266 return EFI_NOT_FOUND
;
1272 Get section entry decimal UINTN value.
1274 @param[in] Context INI Config file context.
1275 @param[in] SectionName Section name.
1276 @param[in] EntryName Section entry name.
1277 @param[out] Data Point to the got decimal UINTN value.
1279 @retval EFI_SUCCESS Section entry decimal UINTN value is got.
1280 @retval EFI_NOT_FOUND Section is not found.
1284 GetDecimalUintnFromDataFile (
1286 IN CHAR8
*SectionName
,
1287 IN CHAR8
*EntryName
,
1294 if (Context
== NULL
|| SectionName
== NULL
|| EntryName
== NULL
|| Data
== NULL
) {
1295 return EFI_INVALID_PARAMETER
;
1298 Status
= GetStringFromDataFile(
1304 if (EFI_ERROR(Status
)) {
1305 return EFI_NOT_FOUND
;
1307 ASSERT (Value
!= NULL
);
1308 if (!IsValidDecimalString(Value
, AsciiStrLen(Value
))) {
1309 return EFI_NOT_FOUND
;
1311 *Data
= AsciiStrDecimalToUintn(Value
);
1316 Get section entry heximal UINTN value.
1318 @param[in] Context INI Config file context.
1319 @param[in] SectionName Section name.
1320 @param[in] EntryName Section entry name.
1321 @param[out] Data Point to the got heximal UINTN value.
1323 @retval EFI_SUCCESS Section entry heximal UINTN value is got.
1324 @retval EFI_NOT_FOUND Section is not found.
1328 GetHexUintnFromDataFile (
1330 IN CHAR8
*SectionName
,
1331 IN CHAR8
*EntryName
,
1338 if (Context
== NULL
|| SectionName
== NULL
|| EntryName
== NULL
|| Data
== NULL
) {
1339 return EFI_INVALID_PARAMETER
;
1342 Status
= GetStringFromDataFile(
1348 if (EFI_ERROR(Status
)) {
1349 return EFI_NOT_FOUND
;
1351 ASSERT (Value
!= NULL
);
1352 if (!IsValidHexString(Value
, AsciiStrLen(Value
))) {
1353 return EFI_NOT_FOUND
;
1355 *Data
= AsciiStrHexToUintn(Value
);
1360 Get section entry heximal UINT64 value.
1362 @param[in] Context INI Config file context.
1363 @param[in] SectionName Section name.
1364 @param[in] EntryName Section entry name.
1365 @param[out] Data Point to the got heximal UINT64 value.
1367 @retval EFI_SUCCESS Section entry heximal UINT64 value is got.
1368 @retval EFI_NOT_FOUND Section is not found.
1372 GetHexUint64FromDataFile (
1374 IN CHAR8
*SectionName
,
1375 IN CHAR8
*EntryName
,
1382 if (Context
== NULL
|| SectionName
== NULL
|| EntryName
== NULL
|| Data
== NULL
) {
1383 return EFI_INVALID_PARAMETER
;
1386 Status
= GetStringFromDataFile(
1392 if (EFI_ERROR(Status
)) {
1393 return EFI_NOT_FOUND
;
1395 ASSERT (Value
!= NULL
);
1396 if (!IsValidHexString(Value
, AsciiStrLen(Value
))) {
1397 return EFI_NOT_FOUND
;
1399 *Data
= AsciiStrHexToUint64(Value
);
1404 Close an INI config file and free the context.
1406 @param[in] Context INI Config file context.
1414 INI_PARSING_LIB_CONTEXT
*IniContext
;
1416 if (Context
== NULL
) {
1420 IniContext
= Context
;
1421 FreeAllList(IniContext
->SectionHead
, IniContext
->CommentHead
);