]> git.proxmox.com Git - mirror_edk2.git/blame - SignedCapsulePkg/Library/IniParsingLib/IniParsingLib.c
SignedCapsulePkg SystemCapsuleLib: Change some dbg level to DEBUG_INFO
[mirror_edk2.git] / SignedCapsulePkg / Library / IniParsingLib / IniParsingLib.c
CommitLineData
384070fd
JY
1/** @file\r
2 This library parses the INI configuration file.\r
3\r
4 The INI file format is:\r
5 ================\r
6 [SectionName]\r
7 EntryName=EntryValue\r
8 ================\r
9\r
10 Where:\r
11 1) SectionName is an ASCII string. The valid format is [A-Za-z0-9_]+\r
12 2) EntryName is an ASCII string. The valid format is [A-Za-z0-9_]+\r
13 3) EntryValue can be:\r
14 3.1) an ASCII String. The valid format is [A-Za-z0-9_]+\r
15 3.2) a GUID. The valid format is xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx, where x is [A-Fa-f0-9]\r
16 3.3) a decimal value. The valid format is [0-9]+\r
17 3.4) a heximal value. The valid format is 0x[A-Fa-f0-9]+\r
18 4) '#' or ';' can be used as comment at anywhere.\r
19 5) TAB(0x20) or SPACE(0x9) can be used as separator.\r
20 6) LF(\n, 0xA) or CR(\r, 0xD) can be used as line break.\r
21\r
22 Caution: This module requires additional review when modified.\r
23 This driver will have external input - INI data file.\r
24\r
25 OpenIniFile(), PreProcessDataFile(), ProfileGetSection(), ProfileGetEntry()\r
26 will receive untrusted input and do basic validation.\r
27\r
bf717f10 28 Copyright (c) 2016 - 2017, Intel Corporation. All rights reserved.<BR>\r
384070fd
JY
29\r
30 This program and the accompanying materials\r
31 are licensed and made available under the terms and conditions\r
32 of the BSD License which accompanies this distribution. The\r
33 full text of the license may be found at\r
34 http://opensource.org/licenses/bsd-license.php\r
35\r
36 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
37 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
38\r
39**/\r
40\r
41#include <Uefi.h>\r
42#include <Library/BaseLib.h>\r
43#include <Library/BaseMemoryLib.h>\r
44#include <Library/DebugLib.h>\r
45#include <Library/MemoryAllocationLib.h>\r
46\r
47#define IS_HYPHEN(a) ((a) == '-')\r
48#define IS_NULL(a) ((a) == '\0')\r
49\r
50// This is default allocation. Reallocation will happen if it is not enough.\r
51#define MAX_LINE_LENGTH 512\r
52\r
67c09dd5
DB
53typedef struct _INI_SECTION_ITEM SECTION_ITEM;\r
54struct _INI_SECTION_ITEM {\r
384070fd
JY
55 CHAR8 *PtrSection;\r
56 UINTN SecNameLen;\r
57 CHAR8 *PtrEntry;\r
58 CHAR8 *PtrValue;\r
59 SECTION_ITEM *PtrNext;\r
60};\r
61\r
67c09dd5
DB
62typedef struct _INI_COMMENT_LINE COMMENT_LINE;\r
63struct _INI_COMMENT_LINE {\r
384070fd
JY
64 CHAR8 *PtrComment;\r
65 COMMENT_LINE *PtrNext;\r
66};\r
67\r
68typedef struct {\r
69 SECTION_ITEM *SectionHead;\r
70 COMMENT_LINE *CommentHead;\r
71} INI_PARSING_LIB_CONTEXT;\r
72\r
73/**\r
74 Return if the digital char is valid.\r
75\r
76 @param[in] DigitalChar The digital char to be checked.\r
77 @param[in] IncludeHex If it include HEX char.\r
78\r
79 @retval TRUE The digital char is valid.\r
80 @retval FALSE The digital char is invalid.\r
81**/\r
82BOOLEAN\r
83IsValidDigitalChar (\r
84 IN CHAR8 DigitalChar,\r
85 IN BOOLEAN IncludeHex\r
86 )\r
87{\r
88 if (DigitalChar >= '0' && DigitalChar <= '9') {\r
89 return TRUE;\r
90 }\r
91 if (IncludeHex) {\r
92 if (DigitalChar >= 'a' && DigitalChar <= 'f') {\r
93 return TRUE;\r
94 }\r
95 if (DigitalChar >= 'A' && DigitalChar <= 'F') {\r
96 return TRUE;\r
97 }\r
98 }\r
99 return FALSE;\r
100}\r
101\r
102/**\r
103 Return if the name char is valid.\r
104\r
105 @param[in] NameChar The name char to be checked.\r
106\r
107 @retval TRUE The name char is valid.\r
108 @retval FALSE The name char is invalid.\r
109**/\r
110BOOLEAN\r
111IsValidNameChar (\r
112 IN CHAR8 NameChar\r
113 )\r
114{\r
115 if (NameChar >= 'a' && NameChar <= 'z') {\r
116 return TRUE;\r
117 }\r
118 if (NameChar >= 'A' && NameChar <= 'Z') {\r
119 return TRUE;\r
120 }\r
121 if (NameChar >= '0' && NameChar <= '9') {\r
122 return TRUE;\r
123 }\r
124 if (NameChar == '_') {\r
125 return TRUE;\r
126 }\r
127 return FALSE;\r
128}\r
129\r
130/**\r
131 Return if the digital string is valid.\r
132\r
133 @param[in] Digital The digital to be checked.\r
134 @param[in] Length The length of digital string in bytes.\r
135 @param[in] IncludeHex If it include HEX char.\r
136\r
137 @retval TRUE The digital string is valid.\r
138 @retval FALSE The digital string is invalid.\r
139**/\r
140BOOLEAN\r
141IsValidDigital (\r
142 IN CHAR8 *Digital,\r
143 IN UINTN Length,\r
144 IN BOOLEAN IncludeHex\r
145 )\r
146{\r
147 UINTN Index;\r
148 for (Index = 0; Index < Length; Index++) {\r
149 if (!IsValidDigitalChar(Digital[Index], IncludeHex)) {\r
150 return FALSE;\r
151 }\r
152 }\r
153 return TRUE;\r
154}\r
155\r
156/**\r
157 Return if the decimal string is valid.\r
158\r
159 @param[in] Decimal The decimal string to be checked.\r
160 @param[in] Length The length of decimal string in bytes.\r
161\r
162 @retval TRUE The decimal string is valid.\r
163 @retval FALSE The decimal string is invalid.\r
164**/\r
165BOOLEAN\r
166IsValidDecimalString (\r
167 IN CHAR8 *Decimal,\r
168 IN UINTN Length\r
169 )\r
170{\r
171 return IsValidDigital(Decimal, Length, FALSE);\r
172}\r
173\r
174/**\r
175 Return if the heximal string is valid.\r
176\r
177 @param[in] Hex The heximal string to be checked.\r
178 @param[in] Length The length of heximal string in bytes.\r
179\r
180 @retval TRUE The heximal string is valid.\r
181 @retval FALSE The heximal string is invalid.\r
182**/\r
183BOOLEAN\r
184IsValidHexString (\r
185 IN CHAR8 *Hex,\r
186 IN UINTN Length\r
187 )\r
188{\r
189 if (Length <= 2) {\r
190 return FALSE;\r
191 }\r
192 if (Hex[0] != '0') {\r
193 return FALSE;\r
194 }\r
195 if (Hex[1] != 'x' && Hex[1] != 'X') {\r
196 return FALSE;\r
197 }\r
198 return IsValidDigital(&Hex[2], Length - 2, TRUE);\r
199}\r
200\r
201/**\r
202 Return if the name string is valid.\r
203\r
204 @param[in] Name The name to be checked.\r
205 @param[in] Length The length of name string in bytes.\r
206\r
207 @retval TRUE The name string is valid.\r
208 @retval FALSE The name string is invalid.\r
209**/\r
210BOOLEAN\r
211IsValidName (\r
212 IN CHAR8 *Name,\r
213 IN UINTN Length\r
214 )\r
215{\r
216 UINTN Index;\r
217 for (Index = 0; Index < Length; Index++) {\r
218 if (!IsValidNameChar(Name[Index])) {\r
219 return FALSE;\r
220 }\r
221 }\r
222 return TRUE;\r
223}\r
224\r
225/**\r
226 Return if the value string is valid GUID.\r
227\r
228 @param[in] Value The value to be checked.\r
229 @param[in] Length The length of value string in bytes.\r
230\r
231 @retval TRUE The value string is valid GUID.\r
232 @retval FALSE The value string is invalid GUID.\r
233**/\r
234BOOLEAN\r
235IsValidGuid (\r
236 IN CHAR8 *Value,\r
237 IN UINTN Length\r
238 )\r
239{\r
240 if (Length != sizeof("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx") - 1) {\r
241 return FALSE;\r
242 }\r
243 if (!IS_HYPHEN(Value[8])) {\r
244 return FALSE;\r
245 }\r
246 if (!IS_HYPHEN(Value[13])) {\r
247 return FALSE;\r
248 }\r
249 if (!IS_HYPHEN(Value[18])) {\r
250 return FALSE;\r
251 }\r
252 if (!IS_HYPHEN(Value[23])) {\r
253 return FALSE;\r
254 }\r
255 if (!IsValidDigital(&Value[0], 8, TRUE)) {\r
256 return FALSE;\r
257 }\r
258 if (!IsValidDigital(&Value[9], 4, TRUE)) {\r
259 return FALSE;\r
260 }\r
261 if (!IsValidDigital(&Value[14], 4, TRUE)) {\r
262 return FALSE;\r
263 }\r
264 if (!IsValidDigital(&Value[19], 4, TRUE)) {\r
265 return FALSE;\r
266 }\r
267 if (!IsValidDigital(&Value[24], 12, TRUE)) {\r
268 return FALSE;\r
269 }\r
270 return TRUE;\r
271}\r
272\r
273/**\r
274 Return if the value string is valid.\r
275\r
276 @param[in] Value The value to be checked.\r
277 @param[in] Length The length of value string in bytes.\r
278\r
279 @retval TRUE The name string is valid.\r
280 @retval FALSE The name string is invalid.\r
281**/\r
282BOOLEAN\r
283IsValidValue (\r
284 IN CHAR8 *Value,\r
285 IN UINTN Length\r
286 )\r
287{\r
288 if (IsValidName(Value, Length) || IsValidGuid(Value, Length)) {\r
289 return TRUE;\r
290 }\r
291 return FALSE;\r
292}\r
293\r
294/**\r
295 Dump an INI config file context.\r
296\r
297 @param[in] Context INI Config file context.\r
298**/\r
299VOID\r
300DumpIniSection (\r
301 IN VOID *Context\r
302 )\r
303{\r
304 INI_PARSING_LIB_CONTEXT *IniContext;\r
305 SECTION_ITEM *PtrSection;\r
306 SECTION_ITEM *Section;\r
307\r
308 if (Context == NULL) {\r
309 return;\r
310 }\r
311\r
312 IniContext = Context;\r
313 Section = IniContext->SectionHead;\r
314\r
315 while (Section != NULL) {\r
316 PtrSection = Section;\r
317 Section = Section->PtrNext;\r
318 if (PtrSection->PtrSection != NULL) {\r
319 DEBUG((DEBUG_VERBOSE, "Section - %a\n", PtrSection->PtrSection));\r
320 }\r
321 if (PtrSection->PtrEntry != NULL) {\r
322 DEBUG ((DEBUG_VERBOSE, " Entry - %a\n", PtrSection->PtrEntry));\r
323 }\r
324 if (PtrSection->PtrValue != NULL) {\r
325 DEBUG((DEBUG_VERBOSE, " Value - %a\n", PtrSection->PtrValue));\r
326 }\r
327 }\r
328}\r
329\r
330/**\r
331 Copy one line data from buffer data to the line buffer.\r
332\r
333 @param[in] Buffer Buffer data.\r
334 @param[in] BufferSize Buffer Size.\r
335 @param[in, out] LineBuffer Line buffer to store the found line data.\r
336 @param[in, out] LineSize On input, size of the input line buffer.\r
337 On output, size of the actual line buffer.\r
338\r
339 @retval EFI_BUFFER_TOO_SMALL The size of input line buffer is not enough.\r
340 @retval EFI_SUCCESS Copy line data into the line buffer.\r
341\r
342**/\r
343EFI_STATUS\r
344ProfileGetLine (\r
345 IN UINT8 *Buffer,\r
346 IN UINTN BufferSize,\r
347 IN OUT UINT8 *LineBuffer,\r
348 IN OUT UINTN *LineSize\r
349 )\r
350{\r
351 UINTN Length;\r
352 UINT8 *PtrBuf;\r
353 UINTN PtrEnd;\r
354\r
355 PtrBuf = Buffer;\r
356 PtrEnd = (UINTN)Buffer + BufferSize;\r
357\r
358 //\r
359 // 0x0D indicates a line break. Otherwise there is no line break\r
360 //\r
361 while ((UINTN)PtrBuf < PtrEnd) {\r
362 if (*PtrBuf == 0x0D || *PtrBuf == 0x0A) {\r
363 break;\r
364 }\r
365 PtrBuf++;\r
366 }\r
367\r
368 if ((UINTN)PtrBuf >= (PtrEnd - 1)) {\r
369 //\r
370 // The buffer ends without any line break\r
371 // or it is the last character of the buffer\r
372 //\r
373 Length = BufferSize;\r
374 } else if (*(PtrBuf + 1) == 0x0A) {\r
375 //\r
376 // Further check if a 0x0A follows. If yes, count 0xA\r
377 //\r
378 Length = (UINTN) PtrBuf - (UINTN) Buffer + 2;\r
379 } else {\r
380 Length = (UINTN) PtrBuf - (UINTN) Buffer + 1;\r
381 }\r
382\r
383 if (Length > (*LineSize)) {\r
384 *LineSize = Length;\r
385 return EFI_BUFFER_TOO_SMALL;\r
386 }\r
387\r
388 SetMem (LineBuffer, *LineSize, 0x0);\r
389 *LineSize = Length;\r
390 CopyMem (LineBuffer, Buffer, Length);\r
391\r
392 return EFI_SUCCESS;\r
393}\r
394\r
395/**\r
396 Trim Buffer by removing all CR, LF, TAB, and SPACE chars in its head and tail.\r
397\r
398 @param[in, out] Buffer On input, buffer data to be trimed.\r
399 On output, the trimmed buffer.\r
400 @param[in, out] BufferSize On input, size of original buffer data.\r
401 On output, size of the trimmed buffer.\r
402\r
403**/\r
404VOID\r
405ProfileTrim (\r
406 IN OUT UINT8 *Buffer,\r
407 IN OUT UINTN *BufferSize\r
408 )\r
409{\r
410 UINTN Length;\r
411 UINT8 *PtrBuf;\r
412 UINT8 *PtrEnd;\r
413\r
414 if (*BufferSize == 0) {\r
415 return;\r
416 }\r
417\r
418 //\r
419 // Trim the tail first, include CR, LF, TAB, and SPACE.\r
420 //\r
421 Length = *BufferSize;\r
422 PtrBuf = (UINT8 *) ((UINTN) Buffer + Length - 1);\r
423 while (PtrBuf >= Buffer) {\r
424 if ((*PtrBuf != 0x0D) && (*PtrBuf != 0x0A )\r
425 && (*PtrBuf != 0x20) && (*PtrBuf != 0x09)) {\r
426 break;\r
427 }\r
428 PtrBuf --;\r
429 }\r
430\r
431 //\r
432 // all spaces, a blank line, return directly;\r
433 //\r
434 if (PtrBuf < Buffer) {\r
435 *BufferSize = 0;\r
436 return;\r
437 }\r
438\r
439 Length = (UINTN)PtrBuf - (UINTN)Buffer + 1;\r
440 PtrEnd = PtrBuf;\r
441 PtrBuf = Buffer;\r
442\r
443 //\r
444 // Now skip the heading CR, LF, TAB and SPACE\r
445 //\r
446 while (PtrBuf <= PtrEnd) {\r
447 if ((*PtrBuf != 0x0D) && (*PtrBuf != 0x0A )\r
448 && (*PtrBuf != 0x20) && (*PtrBuf != 0x09)) {\r
449 break;\r
450 }\r
451 PtrBuf++;\r
452 }\r
453\r
454 //\r
455 // If no heading CR, LF, TAB or SPACE, directly return\r
456 //\r
457 if (PtrBuf == Buffer) {\r
458 *BufferSize = Length;\r
459 return;\r
460 }\r
461\r
462 *BufferSize = (UINTN)PtrEnd - (UINTN)PtrBuf + 1;\r
463\r
464 //\r
465 // The first Buffer..PtrBuf characters are CR, LF, TAB or SPACE.\r
466 // Now move out all these characters.\r
467 //\r
468 while (PtrBuf <= PtrEnd) {\r
469 *Buffer = *PtrBuf;\r
470 Buffer++;\r
471 PtrBuf++;\r
472 }\r
473\r
474 return;\r
475}\r
476\r
477/**\r
478 Insert new comment item into comment head.\r
479\r
480 @param[in] Buffer Comment buffer to be added.\r
481 @param[in] BufferSize Size of comment buffer.\r
482 @param[in, out] CommentHead Comment Item head entry.\r
483\r
484 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.\r
485 @retval EFI_SUCCESS New comment item is inserted.\r
486\r
487**/\r
488EFI_STATUS\r
489ProfileGetComments (\r
490 IN UINT8 *Buffer,\r
491 IN UINTN BufferSize,\r
492 IN OUT COMMENT_LINE **CommentHead\r
493 )\r
494{\r
495 COMMENT_LINE *CommentItem;\r
496\r
497 CommentItem = NULL;\r
498 CommentItem = AllocatePool (sizeof (COMMENT_LINE));\r
499 if (CommentItem == NULL) {\r
500 return EFI_OUT_OF_RESOURCES;\r
501 }\r
502\r
503 CommentItem->PtrNext = *CommentHead;\r
504 *CommentHead = CommentItem;\r
505\r
506 //\r
507 // Add a trailing '\0'\r
508 //\r
509 CommentItem->PtrComment = AllocatePool (BufferSize + 1);\r
510 if (CommentItem->PtrComment == NULL) {\r
511 FreePool (CommentItem);\r
512 return EFI_OUT_OF_RESOURCES;\r
513 }\r
514 CopyMem (CommentItem->PtrComment, Buffer, BufferSize);\r
515 *(CommentItem->PtrComment + BufferSize) = '\0';\r
516\r
517 return EFI_SUCCESS;\r
518}\r
519\r
520/**\r
521 Add new section item into Section head.\r
522\r
523 @param[in] Buffer Section item data buffer.\r
524 @param[in] BufferSize Size of section item.\r
525 @param[in, out] SectionHead Section item head entry.\r
526\r
527 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.\r
528 @retval EFI_SUCCESS Section item is NULL or Section item is added.\r
529\r
530**/\r
531EFI_STATUS\r
532ProfileGetSection (\r
533 IN UINT8 *Buffer,\r
534 IN UINTN BufferSize,\r
535 IN OUT SECTION_ITEM **SectionHead\r
536 )\r
537{\r
538 SECTION_ITEM *SectionItem;\r
539 UINTN Length;\r
540 UINT8 *PtrBuf;\r
541 UINT8 *PtrEnd;\r
542\r
543 ASSERT(BufferSize >= 1);\r
544 //\r
545 // The first character of Buffer is '[', now we want for ']'\r
546 //\r
547 PtrEnd = (UINT8 *)((UINTN)Buffer + BufferSize - 1);\r
548 PtrBuf = (UINT8 *)((UINTN)Buffer + 1);\r
549 while (PtrBuf <= PtrEnd) {\r
550 if (*PtrBuf == ']') {\r
551 break;\r
552 }\r
553 PtrBuf ++;\r
554 }\r
555 if (PtrBuf > PtrEnd) {\r
556 //\r
557 // Not found. Invalid line\r
558 //\r
559 return EFI_NOT_FOUND;\r
560 }\r
561 if (PtrBuf <= Buffer + 1) {\r
562 // Empty name\r
563 return EFI_NOT_FOUND;\r
564 }\r
565\r
566 //\r
567 // excluding the heading '[' and tailing ']'\r
568 //\r
569 Length = PtrBuf - Buffer - 1;\r
570 ProfileTrim (\r
571 Buffer + 1,\r
572 &Length\r
573 );\r
574\r
575 //\r
576 // Invalid line if the section name is null\r
577 //\r
578 if (Length == 0) {\r
579 return EFI_NOT_FOUND;\r
580 }\r
581\r
582 if (!IsValidName((CHAR8 *)Buffer + 1, Length)) {\r
583 return EFI_INVALID_PARAMETER;\r
584 }\r
585\r
586 SectionItem = AllocatePool (sizeof (SECTION_ITEM));\r
587 if (SectionItem == NULL) {\r
588 return EFI_OUT_OF_RESOURCES;\r
589 }\r
590\r
591 SectionItem->PtrSection = NULL;\r
592 SectionItem->SecNameLen = Length;\r
593 SectionItem->PtrEntry = NULL;\r
594 SectionItem->PtrValue = NULL;\r
595 SectionItem->PtrNext = *SectionHead;\r
596 *SectionHead = SectionItem;\r
597\r
598 //\r
599 // Add a trailing '\0'\r
600 //\r
601 SectionItem->PtrSection = AllocatePool (Length + 1);\r
602 if (SectionItem->PtrSection == NULL) {\r
603 return EFI_OUT_OF_RESOURCES;\r
604 }\r
605\r
606 //\r
607 // excluding the heading '['\r
608 //\r
609 CopyMem (SectionItem->PtrSection, Buffer + 1, Length);\r
610 *(SectionItem->PtrSection + Length) = '\0';\r
611\r
612 return EFI_SUCCESS;\r
613}\r
614\r
615/**\r
616 Add new section entry and entry value into Section head.\r
617\r
618 @param[in] Buffer Section entry data buffer.\r
619 @param[in] BufferSize Size of section entry.\r
620 @param[in, out] SectionHead Section item head entry.\r
621\r
622 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.\r
623 @retval EFI_SUCCESS Section entry is added.\r
624 @retval EFI_NOT_FOUND Section entry is not found.\r
625 @retval EFI_INVALID_PARAMETER Section entry is invalid.\r
626\r
627**/\r
628EFI_STATUS\r
629ProfileGetEntry (\r
630 IN UINT8 *Buffer,\r
631 IN UINTN BufferSize,\r
632 IN OUT SECTION_ITEM **SectionHead\r
633 )\r
634{\r
635 EFI_STATUS Status;\r
636 SECTION_ITEM *SectionItem;\r
637 SECTION_ITEM *PtrSection;\r
638 UINTN Length;\r
639 UINT8 *PtrBuf;\r
640 UINT8 *PtrEnd;\r
641\r
642 Status = EFI_SUCCESS;\r
643 PtrBuf = Buffer;\r
644 PtrEnd = (UINT8 *) ((UINTN)Buffer + BufferSize - 1);\r
645\r
646 //\r
647 // First search for '='\r
648 //\r
649 while (PtrBuf <= PtrEnd) {\r
650 if (*PtrBuf == '=') {\r
651 break;\r
652 }\r
653 PtrBuf++;\r
654 }\r
655 if (PtrBuf > PtrEnd) {\r
656 //\r
657 // Not found. Invalid line\r
658 //\r
659 return EFI_NOT_FOUND;\r
660 }\r
661 if (PtrBuf <= Buffer) {\r
662 // Empty name\r
663 return EFI_NOT_FOUND;\r
664 }\r
665\r
666 //\r
667 // excluding the tailing '='\r
668 //\r
669 Length = PtrBuf - Buffer;\r
670 ProfileTrim (\r
671 Buffer,\r
672 &Length\r
673 );\r
674\r
675 //\r
676 // Invalid line if the entry name is null\r
677 //\r
678 if (Length == 0) {\r
679 return EFI_NOT_FOUND;\r
680 }\r
681\r
682 if (!IsValidName((CHAR8 *)Buffer, Length)) {\r
683 return EFI_INVALID_PARAMETER;\r
684 }\r
685\r
686 //\r
687 // Omit this line if no section header has been found before\r
688 //\r
689 if (*SectionHead == NULL) {\r
690 return Status;\r
691 }\r
692 PtrSection = *SectionHead;\r
693\r
694 SectionItem = AllocatePool (sizeof (SECTION_ITEM));\r
695 if (SectionItem == NULL) {\r
696 return EFI_OUT_OF_RESOURCES;\r
697 }\r
698\r
699 SectionItem->PtrSection = NULL;\r
700 SectionItem->PtrEntry = NULL;\r
701 SectionItem->PtrValue = NULL;\r
702 SectionItem->SecNameLen = PtrSection->SecNameLen;\r
703 SectionItem->PtrNext = *SectionHead;\r
704 *SectionHead = SectionItem;\r
705\r
706 //\r
707 // SectionName, add a trailing '\0'\r
708 //\r
709 SectionItem->PtrSection = AllocatePool (PtrSection->SecNameLen + 1);\r
710 if (SectionItem->PtrSection == NULL) {\r
711 return EFI_OUT_OF_RESOURCES;\r
712 }\r
713 CopyMem (SectionItem->PtrSection, PtrSection->PtrSection, PtrSection->SecNameLen + 1);\r
714\r
715 //\r
716 // EntryName, add a trailing '\0'\r
717 //\r
718 SectionItem->PtrEntry = AllocatePool (Length + 1);\r
719 if (SectionItem->PtrEntry == NULL) {\r
720 FreePool(SectionItem->PtrSection);\r
721 return EFI_OUT_OF_RESOURCES;\r
722 }\r
723 CopyMem (SectionItem->PtrEntry, Buffer, Length);\r
724 *(SectionItem->PtrEntry + Length) = '\0';\r
725\r
726 //\r
727 // Next search for '#' or ';'\r
728 //\r
729 PtrBuf = PtrBuf + 1;\r
730 Buffer = PtrBuf;\r
731 while (PtrBuf <= PtrEnd) {\r
732 if (*PtrBuf == '#' || *PtrBuf == ';') {\r
733 break;\r
734 }\r
735 PtrBuf++;\r
736 }\r
737 if (PtrBuf <= Buffer) {\r
738 // Empty name\r
739 FreePool(SectionItem->PtrEntry);\r
740 FreePool(SectionItem->PtrSection);\r
741 return EFI_NOT_FOUND;\r
742 }\r
743 Length = PtrBuf - Buffer;\r
744 ProfileTrim (\r
745 Buffer,\r
746 &Length\r
747 );\r
748\r
749 //\r
750 // Invalid line if the entry value is null\r
751 //\r
752 if (Length == 0) {\r
753 FreePool(SectionItem->PtrEntry);\r
754 FreePool(SectionItem->PtrSection);\r
755 return EFI_NOT_FOUND;\r
756 }\r
757\r
758 if (!IsValidValue((CHAR8 *)Buffer, Length)) {\r
759 FreePool(SectionItem->PtrEntry);\r
760 FreePool(SectionItem->PtrSection);\r
761 return EFI_INVALID_PARAMETER;\r
762 }\r
763\r
764 //\r
765 // EntryValue, add a trailing '\0'\r
766 //\r
767 SectionItem->PtrValue = AllocatePool (Length + 1);\r
768 if (SectionItem->PtrValue == NULL) {\r
769 FreePool(SectionItem->PtrEntry);\r
770 FreePool(SectionItem->PtrSection);\r
771 return EFI_OUT_OF_RESOURCES;\r
772 }\r
773 CopyMem (SectionItem->PtrValue, Buffer, Length);\r
774 *(SectionItem->PtrValue + Length) = '\0';\r
775\r
776 return EFI_SUCCESS;\r
777}\r
778\r
779/**\r
780 Free all comment entry and section entry.\r
781\r
782 @param[in] Section Section entry list.\r
783 @param[in] Comment Comment entry list.\r
784\r
785**/\r
786VOID\r
787FreeAllList (\r
788 IN SECTION_ITEM *Section,\r
789 IN COMMENT_LINE *Comment\r
790 )\r
791{\r
792 SECTION_ITEM *PtrSection;\r
793 COMMENT_LINE *PtrComment;\r
794\r
795 while (Section != NULL) {\r
796 PtrSection = Section;\r
797 Section = Section->PtrNext;\r
798 if (PtrSection->PtrEntry != NULL) {\r
799 FreePool (PtrSection->PtrEntry);\r
800 }\r
801 if (PtrSection->PtrSection != NULL) {\r
802 FreePool (PtrSection->PtrSection);\r
803 }\r
804 if (PtrSection->PtrValue != NULL) {\r
805 FreePool (PtrSection->PtrValue);\r
806 }\r
807 FreePool (PtrSection);\r
808 }\r
809\r
810 while (Comment != NULL) {\r
811 PtrComment = Comment;\r
812 Comment = Comment->PtrNext;\r
813 if (PtrComment->PtrComment != NULL) {\r
814 FreePool (PtrComment->PtrComment);\r
815 }\r
816 FreePool (PtrComment);\r
817 }\r
818\r
819 return;\r
820}\r
821\r
822/**\r
823 Get section entry value.\r
824\r
825 @param[in] Section Section entry list.\r
826 @param[in] SectionName Section name.\r
827 @param[in] EntryName Section entry name.\r
828 @param[out] EntryValue Point to the got entry value.\r
829\r
830 @retval EFI_NOT_FOUND Section is not found.\r
831 @retval EFI_SUCCESS Section entry value is got.\r
832\r
833**/\r
834EFI_STATUS\r
835UpdateGetProfileString (\r
836 IN SECTION_ITEM *Section,\r
837 IN CHAR8 *SectionName,\r
838 IN CHAR8 *EntryName,\r
839 OUT CHAR8 **EntryValue\r
840 )\r
841{\r
842 *EntryValue = NULL;\r
843\r
844 while (Section != NULL) {\r
845 if (AsciiStrCmp ((CONST CHAR8 *) Section->PtrSection, (CONST CHAR8 *) SectionName) == 0) {\r
846 if (Section->PtrEntry != NULL) {\r
847 if (AsciiStrCmp ((CONST CHAR8 *) Section->PtrEntry, (CONST CHAR8 *) EntryName) == 0) {\r
848 break;\r
849 }\r
850 }\r
851 }\r
852 Section = Section->PtrNext;\r
853 }\r
854\r
855 if (Section == NULL) {\r
856 return EFI_NOT_FOUND;\r
857 }\r
858\r
859 *EntryValue = Section->PtrValue;\r
860\r
861 return EFI_SUCCESS;\r
862}\r
863\r
384070fd
JY
864/**\r
865 Pre process config data buffer into Section entry list and Comment entry list.\r
866\r
867 @param[in] DataBuffer Config raw file buffer.\r
868 @param[in] BufferSize Size of raw buffer.\r
869 @param[in, out] SectionHead Pointer to the section entry list.\r
870 @param[in, out] CommentHead Pointer to the comment entry list.\r
871\r
872 @retval EFI_OUT_OF_RESOURCES No enough memory is allocated.\r
873 @retval EFI_SUCCESS Config data buffer is preprocessed.\r
874 @retval EFI_NOT_FOUND Config data buffer is invalid, because Section or Entry is not found.\r
875 @retval EFI_INVALID_PARAMETER Config data buffer is invalid, because Section or Entry is invalid.\r
876\r
877**/\r
878EFI_STATUS\r
879PreProcessDataFile (\r
880 IN UINT8 *DataBuffer,\r
881 IN UINTN BufferSize,\r
882 IN OUT SECTION_ITEM **SectionHead,\r
883 IN OUT COMMENT_LINE **CommentHead\r
884 )\r
885{\r
886 EFI_STATUS Status;\r
887 CHAR8 *Source;\r
888 CHAR8 *CurrentPtr;\r
889 CHAR8 *BufferEnd;\r
890 CHAR8 *PtrLine;\r
891 UINTN LineLength;\r
892 UINTN SourceLength;\r
893 UINTN MaxLineLength;\r
894\r
895 *SectionHead = NULL;\r
896 *CommentHead = NULL;\r
897 BufferEnd = (CHAR8 *) ( (UINTN) DataBuffer + BufferSize);\r
898 CurrentPtr = (CHAR8 *) DataBuffer;\r
899 MaxLineLength = MAX_LINE_LENGTH;\r
900 Status = EFI_SUCCESS;\r
901\r
902 PtrLine = AllocatePool (MaxLineLength);\r
903 if (PtrLine == NULL) {\r
904 return EFI_OUT_OF_RESOURCES;\r
905 }\r
906\r
907 while (CurrentPtr < BufferEnd) {\r
908 Source = CurrentPtr;\r
909 SourceLength = (UINTN)BufferEnd - (UINTN)CurrentPtr;\r
910 LineLength = MaxLineLength;\r
911 //\r
912 // With the assumption that line length is less than 512\r
913 // characters. Otherwise BUFFER_TOO_SMALL will be returned.\r
914 //\r
915 Status = ProfileGetLine (\r
916 (UINT8 *) Source,\r
917 SourceLength,\r
918 (UINT8 *) PtrLine,\r
919 &LineLength\r
920 );\r
921 if (EFI_ERROR (Status)) {\r
922 if (Status == EFI_BUFFER_TOO_SMALL) {\r
923 //\r
924 // If buffer too small, re-allocate the buffer according\r
925 // to the returned LineLength and try again.\r
926 //\r
927 FreePool (PtrLine);\r
928 PtrLine = NULL;\r
929 PtrLine = AllocatePool (LineLength);\r
930 if (PtrLine == NULL) {\r
931 Status = EFI_OUT_OF_RESOURCES;\r
932 break;\r
933 }\r
934 SourceLength = LineLength;\r
935 Status = ProfileGetLine (\r
936 (UINT8 *) Source,\r
937 SourceLength,\r
938 (UINT8 *) PtrLine,\r
939 &LineLength\r
940 );\r
941 if (EFI_ERROR (Status)) {\r
942 break;\r
943 }\r
944 MaxLineLength = LineLength;\r
945 } else {\r
946 break;\r
947 }\r
948 }\r
949 CurrentPtr = (CHAR8 *) ( (UINTN) CurrentPtr + LineLength);\r
950\r
951 //\r
952 // Line got. Trim the line before processing it.\r
953 //\r
954 ProfileTrim (\r
955 (UINT8 *) PtrLine,\r
956 &LineLength\r
957 );\r
958\r
959 //\r
960 // Blank line\r
961 //\r
962 if (LineLength == 0) {\r
963 continue;\r
964 }\r
965\r
966 if (PtrLine[0] == '#' || PtrLine[0] == ';') {\r
967 Status = ProfileGetComments (\r
968 (UINT8 *) PtrLine,\r
969 LineLength,\r
970 CommentHead\r
971 );\r
972 } else if (PtrLine[0] == '[') {\r
973 Status = ProfileGetSection (\r
974 (UINT8 *) PtrLine,\r
975 LineLength,\r
976 SectionHead\r
977 );\r
978 } else {\r
979 Status = ProfileGetEntry (\r
980 (UINT8 *) PtrLine,\r
981 LineLength,\r
982 SectionHead\r
983 );\r
984 }\r
985\r
986 if (EFI_ERROR (Status)) {\r
987 break;\r
988 }\r
989 }\r
990\r
991 //\r
992 // Free buffer\r
993 //\r
994 FreePool (PtrLine);\r
995\r
996 return Status;\r
997}\r
998\r
999/**\r
1000 Open an INI config file and return a context.\r
1001\r
1002 @param[in] DataBuffer Config raw file buffer.\r
1003 @param[in] BufferSize Size of raw buffer.\r
1004\r
1005 @return Config data buffer is opened and context is returned.\r
1006 @retval NULL No enough memory is allocated.\r
1007 @retval NULL Config data buffer is invalid.\r
1008**/\r
1009VOID *\r
1010EFIAPI\r
1011OpenIniFile (\r
1012 IN UINT8 *DataBuffer,\r
1013 IN UINTN BufferSize\r
1014 )\r
1015{\r
1016 EFI_STATUS Status;\r
1017 INI_PARSING_LIB_CONTEXT *IniContext;\r
1018\r
1019 if (DataBuffer == NULL || BufferSize == 0) {\r
1020 return NULL;\r
1021 }\r
1022\r
1023 IniContext = AllocateZeroPool(sizeof(INI_PARSING_LIB_CONTEXT));\r
1024 if (IniContext == NULL) {\r
1025 return NULL;\r
1026 }\r
1027\r
1028 //\r
1029 // First process the data buffer and get all sections and entries\r
1030 //\r
1031 Status = PreProcessDataFile (\r
1032 DataBuffer,\r
1033 BufferSize,\r
1034 &IniContext->SectionHead,\r
1035 &IniContext->CommentHead\r
1036 );\r
1037 if (EFI_ERROR(Status)) {\r
1038 FreePool(IniContext);\r
1039 return NULL;\r
1040 }\r
1041 DEBUG_CODE_BEGIN ();\r
1042 DumpIniSection(IniContext);\r
1043 DEBUG_CODE_END ();\r
1044 return IniContext;\r
1045}\r
1046\r
1047/**\r
1048 Get section entry string value.\r
1049\r
1050 @param[in] Context INI Config file context.\r
1051 @param[in] SectionName Section name.\r
1052 @param[in] EntryName Section entry name.\r
1053 @param[out] EntryValue Point to the got entry string value.\r
1054\r
1055 @retval EFI_SUCCESS Section entry string value is got.\r
1056 @retval EFI_NOT_FOUND Section is not found.\r
1057**/\r
1058EFI_STATUS\r
1059EFIAPI\r
1060GetStringFromDataFile(\r
1061 IN VOID *Context,\r
1062 IN CHAR8 *SectionName,\r
1063 IN CHAR8 *EntryName,\r
1064 OUT CHAR8 **EntryValue\r
1065 )\r
1066{\r
1067 INI_PARSING_LIB_CONTEXT *IniContext;\r
1068 EFI_STATUS Status;\r
1069\r
1070 if (Context == NULL || SectionName == NULL || EntryName == NULL || EntryValue == NULL) {\r
1071 return EFI_INVALID_PARAMETER;\r
1072 }\r
1073\r
1074 IniContext = Context;\r
1075\r
1076 *EntryValue = NULL;\r
1077 Status = UpdateGetProfileString (\r
1078 IniContext->SectionHead,\r
1079 SectionName,\r
1080 EntryName,\r
1081 EntryValue\r
1082 );\r
1083 return Status;\r
1084}\r
1085\r
1086/**\r
1087 Get section entry GUID value.\r
1088\r
1089 @param[in] Context INI Config file context.\r
1090 @param[in] SectionName Section name.\r
1091 @param[in] EntryName Section entry name.\r
1092 @param[out] Guid Point to the got GUID value.\r
1093\r
1094 @retval EFI_SUCCESS Section entry GUID value is got.\r
1095 @retval EFI_NOT_FOUND Section is not found.\r
1096**/\r
1097EFI_STATUS\r
1098EFIAPI\r
1099GetGuidFromDataFile (\r
1100 IN VOID *Context,\r
1101 IN CHAR8 *SectionName,\r
1102 IN CHAR8 *EntryName,\r
1103 OUT EFI_GUID *Guid\r
1104 )\r
1105{\r
1106 CHAR8 *Value;\r
1107 EFI_STATUS Status;\r
58974b6e 1108 RETURN_STATUS RStatus;\r
384070fd
JY
1109\r
1110 if (Context == NULL || SectionName == NULL || EntryName == NULL || Guid == NULL) {\r
1111 return EFI_INVALID_PARAMETER;\r
1112 }\r
1113\r
1114 Status = GetStringFromDataFile(\r
1115 Context,\r
1116 SectionName,\r
1117 EntryName,\r
1118 &Value\r
1119 );\r
1120 if (EFI_ERROR(Status)) {\r
1121 return EFI_NOT_FOUND;\r
1122 }\r
cf2ddcf1 1123 ASSERT (Value != NULL);\r
58974b6e
RN
1124 RStatus = AsciiStrToGuid (Value, Guid);\r
1125 if (RETURN_ERROR (RStatus) || (Value[GUID_STRING_LENGTH] != '\0')) {\r
384070fd
JY
1126 return EFI_NOT_FOUND;\r
1127 }\r
1128 return EFI_SUCCESS;\r
1129}\r
1130\r
1131/**\r
1132 Get section entry decimal UINTN value.\r
1133\r
1134 @param[in] Context INI Config file context.\r
1135 @param[in] SectionName Section name.\r
1136 @param[in] EntryName Section entry name.\r
1137 @param[out] Data Point to the got decimal UINTN value.\r
1138\r
1139 @retval EFI_SUCCESS Section entry decimal UINTN value is got.\r
1140 @retval EFI_NOT_FOUND Section is not found.\r
1141**/\r
1142EFI_STATUS\r
1143EFIAPI\r
1144GetDecimalUintnFromDataFile (\r
1145 IN VOID *Context,\r
1146 IN CHAR8 *SectionName,\r
1147 IN CHAR8 *EntryName,\r
1148 OUT UINTN *Data\r
1149 )\r
1150{\r
1151 CHAR8 *Value;\r
1152 EFI_STATUS Status;\r
1153\r
1154 if (Context == NULL || SectionName == NULL || EntryName == NULL || Data == NULL) {\r
1155 return EFI_INVALID_PARAMETER;\r
1156 }\r
1157\r
1158 Status = GetStringFromDataFile(\r
1159 Context,\r
1160 SectionName,\r
1161 EntryName,\r
1162 &Value\r
1163 );\r
1164 if (EFI_ERROR(Status)) {\r
1165 return EFI_NOT_FOUND;\r
1166 }\r
cf2ddcf1 1167 ASSERT (Value != NULL);\r
384070fd
JY
1168 if (!IsValidDecimalString(Value, AsciiStrLen(Value))) {\r
1169 return EFI_NOT_FOUND;\r
1170 }\r
1171 *Data = AsciiStrDecimalToUintn(Value);\r
1172 return EFI_SUCCESS;\r
1173}\r
1174\r
1175/**\r
1176 Get section entry heximal UINTN value.\r
1177\r
1178 @param[in] Context INI Config file context.\r
1179 @param[in] SectionName Section name.\r
1180 @param[in] EntryName Section entry name.\r
1181 @param[out] Data Point to the got heximal UINTN value.\r
1182\r
1183 @retval EFI_SUCCESS Section entry heximal UINTN value is got.\r
1184 @retval EFI_NOT_FOUND Section is not found.\r
1185**/\r
1186EFI_STATUS\r
1187EFIAPI\r
1188GetHexUintnFromDataFile (\r
1189 IN VOID *Context,\r
1190 IN CHAR8 *SectionName,\r
1191 IN CHAR8 *EntryName,\r
1192 OUT UINTN *Data\r
1193 )\r
1194{\r
1195 CHAR8 *Value;\r
1196 EFI_STATUS Status;\r
1197\r
1198 if (Context == NULL || SectionName == NULL || EntryName == NULL || Data == NULL) {\r
1199 return EFI_INVALID_PARAMETER;\r
1200 }\r
1201\r
1202 Status = GetStringFromDataFile(\r
1203 Context,\r
1204 SectionName,\r
1205 EntryName,\r
1206 &Value\r
1207 );\r
1208 if (EFI_ERROR(Status)) {\r
1209 return EFI_NOT_FOUND;\r
1210 }\r
cf2ddcf1 1211 ASSERT (Value != NULL);\r
384070fd
JY
1212 if (!IsValidHexString(Value, AsciiStrLen(Value))) {\r
1213 return EFI_NOT_FOUND;\r
1214 }\r
1215 *Data = AsciiStrHexToUintn(Value);\r
1216 return EFI_SUCCESS;\r
1217}\r
1218\r
1219/**\r
1220 Get section entry heximal UINT64 value.\r
1221\r
1222 @param[in] Context INI Config file context.\r
1223 @param[in] SectionName Section name.\r
1224 @param[in] EntryName Section entry name.\r
1225 @param[out] Data Point to the got heximal UINT64 value.\r
1226\r
1227 @retval EFI_SUCCESS Section entry heximal UINT64 value is got.\r
1228 @retval EFI_NOT_FOUND Section is not found.\r
1229**/\r
1230EFI_STATUS\r
1231EFIAPI\r
1232GetHexUint64FromDataFile (\r
1233 IN VOID *Context,\r
1234 IN CHAR8 *SectionName,\r
1235 IN CHAR8 *EntryName,\r
1236 OUT UINT64 *Data\r
1237 )\r
1238{\r
1239 CHAR8 *Value;\r
1240 EFI_STATUS Status;\r
1241\r
1242 if (Context == NULL || SectionName == NULL || EntryName == NULL || Data == NULL) {\r
1243 return EFI_INVALID_PARAMETER;\r
1244 }\r
1245\r
1246 Status = GetStringFromDataFile(\r
1247 Context,\r
1248 SectionName,\r
1249 EntryName,\r
1250 &Value\r
1251 );\r
1252 if (EFI_ERROR(Status)) {\r
1253 return EFI_NOT_FOUND;\r
1254 }\r
cf2ddcf1 1255 ASSERT (Value != NULL);\r
384070fd
JY
1256 if (!IsValidHexString(Value, AsciiStrLen(Value))) {\r
1257 return EFI_NOT_FOUND;\r
1258 }\r
1259 *Data = AsciiStrHexToUint64(Value);\r
1260 return EFI_SUCCESS;\r
1261}\r
1262\r
1263/**\r
1264 Close an INI config file and free the context.\r
1265\r
1266 @param[in] Context INI Config file context.\r
1267**/\r
1268VOID\r
1269EFIAPI\r
1270CloseIniFile (\r
1271 IN VOID *Context\r
1272 )\r
1273{\r
1274 INI_PARSING_LIB_CONTEXT *IniContext;\r
1275\r
1276 if (Context == NULL) {\r
1277 return ;\r
1278 }\r
1279\r
1280 IniContext = Context;\r
1281 FreeAllList(IniContext->SectionHead, IniContext->CommentHead);\r
1282\r
1283 return;\r
1284}\r