1 ## @file ParserValidate.py
2 # Functions for parser validation
4 # Copyright (c) 2011 - 2018, Intel Corporation. All rights reserved.<BR>
6 # SPDX-License-Identifier: BSD-2-Clause-Patent
17 from Library
.DataType
import MODULE_LIST
18 from Library
.DataType
import COMPONENT_TYPE_LIST
19 from Library
.DataType
import PCD_USAGE_TYPE_LIST_OF_MODULE
20 from Library
.DataType
import TAB_SPACE_SPLIT
21 from Library
.StringUtils
import GetSplitValueList
22 from Library
.ExpressionValidate
import IsValidBareCString
23 from Library
.ExpressionValidate
import IsValidFeatureFlagExp
24 from Common
.MultipleWorkspace
import MultipleWorkspace
as mws
26 ## __HexDigit() method
28 # Whether char input is a Hex data bit
30 # @param TempChar: The char to test
32 def __HexDigit(TempChar
):
33 if (TempChar
>= 'a' and TempChar
<= 'f') or \
34 (TempChar
>= 'A' and TempChar
<= 'F') \
35 or (TempChar
>= '0' and TempChar
<= '9'):
40 ## IsValidHex() method
42 # Whether char input is a Hex data.
44 # @param TempChar: The char to test
46 def IsValidHex(HexStr
):
47 if not HexStr
.upper().startswith("0X"):
49 CharList
= [c
for c
in HexStr
[2:] if not __HexDigit(c
)]
50 if len(CharList
) == 0:
55 ## Judge the input string is valid bool type or not.
57 # <TRUE> ::= {"TRUE"} {"true"} {"True"} {"0x1"} {"0x01"}
58 # <FALSE> ::= {"FALSE"} {"false"} {"False"} {"0x0"} {"0x00"}
59 # <BoolType> ::= {<TRUE>} {<FALSE>}
61 # @param BoolString: A string contained the value need to be judged.
63 def IsValidBoolType(BoolString
):
67 if BoolString
== 'TRUE' or \
68 BoolString
== 'True' or \
69 BoolString
== 'true' or \
70 BoolString
== '0x1' or \
76 elif BoolString
== 'FALSE' or \
77 BoolString
== 'False' or \
78 BoolString
== 'false' or \
79 BoolString
== '0x0' or \
88 ## Is Valid Module Type List or not
90 # @param ModuleTypeList: A list contain ModuleType strings need to be
93 def IsValidInfMoudleTypeList(ModuleTypeList
):
94 for ModuleType
in ModuleTypeList
:
95 return IsValidInfMoudleType(ModuleType
)
97 ## Is Valid Module Type or not
99 # @param ModuleType: A string contain ModuleType need to be judged.
101 def IsValidInfMoudleType(ModuleType
):
102 if ModuleType
in MODULE_LIST
:
107 ## Is Valid Component Type or not
109 # @param ComponentType: A string contain ComponentType need to be judged.
111 def IsValidInfComponentType(ComponentType
):
112 if ComponentType
.upper() in COMPONENT_TYPE_LIST
:
118 ## Is valid Tool Family or not
120 # @param ToolFamily: A string contain Tool Family need to be judged.
121 # Family := [A-Z]([a-zA-Z0-9])*
123 def IsValidToolFamily(ToolFamily
):
124 ReIsValidFamily
= re
.compile(r
"^[A-Z]+[A-Za-z0-9]{0,}$", re
.DOTALL
)
125 if ReIsValidFamily
.match(ToolFamily
) is None:
129 ## Is valid Tool TagName or not
131 # The TagName sample is MYTOOLS and VS2005.
133 # @param TagName: A string contain Tool TagName need to be judged.
135 def IsValidToolTagName(TagName
):
136 if TagName
.strip() == '':
138 if TagName
.strip() == '*':
140 if not IsValidWord(TagName
):
144 ## Is valid arch or not
146 # @param Arch The arch string need to be validated
147 # <OA> ::= (a-zA-Z)(A-Za-z0-9){0,}
148 # <arch> ::= {"IA32"} {"X64"} {"IPF"} {"EBC"} {<OA>}
150 # @param Arch: Input arch
152 def IsValidArch(Arch
):
155 ReIsValidArch
= re
.compile(r
"^[a-zA-Z]+[a-zA-Z0-9]{0,}$", re
.DOTALL
)
156 if ReIsValidArch
.match(Arch
) is None:
160 ## Is valid family or not
162 # <Family> ::= {"MSFT"} {"GCC"} {"INTEL"} {<Usr>} {"*"}
163 # <Usr> ::= [A-Z][A-Za-z0-9]{0,}
165 # @param family: The family string need to be validated
167 def IsValidFamily(Family
):
168 Family
= Family
.strip()
175 ReIsValidFamily
= re
.compile(r
"^[A-Z]+[A-Za-z0-9]{0,}$", re
.DOTALL
)
176 if ReIsValidFamily
.match(Family
) is None:
180 ## Is valid build option name or not
182 # @param BuildOptionName: The BuildOptionName string need to be validated
184 def IsValidBuildOptionName(BuildOptionName
):
185 if not BuildOptionName
:
188 ToolOptionList
= GetSplitValueList(BuildOptionName
, '_', 4)
190 if len(ToolOptionList
) != 5:
193 ReIsValidBuildOption1
= re
.compile(r
"^\s*(\*)|([A-Z][a-zA-Z0-9]*)$")
194 ReIsValidBuildOption2
= re
.compile(r
"^\s*(\*)|([a-zA-Z][a-zA-Z0-9]*)$")
196 if ReIsValidBuildOption1
.match(ToolOptionList
[0]) is None:
199 if ReIsValidBuildOption1
.match(ToolOptionList
[1]) is None:
202 if ReIsValidBuildOption2
.match(ToolOptionList
[2]) is None:
205 if ToolOptionList
[3] == "*" and ToolOptionList
[4] not in ['FAMILY', 'DLL', 'DPATH']:
212 # Check if pattern string matches total token
214 # @param ReString: regular string
215 # @param Token: Token to be matched
217 def IsValidToken(ReString
, Token
):
218 Match
= re
.compile(ReString
).match(Token
)
219 return Match
and Match
.start() == 0 and Match
.end() == len(Token
)
223 # Check if path exist
225 # @param Path: Absolute path or relative path to be checked
226 # @param Root: Root path
228 def IsValidPath(Path
, Root
):
230 OrigPath
= Path
.replace('\\', '/')
232 Path
= os
.path
.normpath(Path
).replace('\\', '/')
233 Root
= os
.path
.normpath(Root
).replace('\\', '/')
234 FullPath
= mws
.join(Root
, Path
)
236 if not os
.path
.exists(FullPath
):
240 # If Path is absolute path.
241 # It should be in Root.
243 if os
.path
.isabs(Path
):
244 if not Path
.startswith(Root
):
249 # Check illegal character
251 for Rel
in ['/', './', '../']:
252 if OrigPath
.startswith(Rel
):
254 for Rel
in ['//', '/./', '/../']:
257 for Rel
in ['/.', '/..', '/']:
258 if OrigPath
.endswith(Rel
):
261 Path
= Path
.rstrip('/')
264 # Check relative path
266 for Word
in Path
.split('/'):
267 if not IsValidWord(Word
):
272 ## IsValidInstallPath
274 # Check if an install path valid or not.
276 # Absolute path or path starts with '.' or path contains '..' are invalid.
278 # @param Path: path to be checked
280 def IsValidInstallPath(Path
):
281 if platform
.platform().find("Windows") >= 0:
282 if os
.path
.isabs(Path
):
287 if os
.path
.isabs(Path
):
289 if Path
.startswith('.'):
292 if Path
.find('..') != -1:
298 ## IsValidCFormatGuid
300 # Check if GUID format has the from of {8,4,4,{2,2,2,2,2,2,2,2}}
302 # @param Guid: Guid to be checked
304 def IsValidCFormatGuid(Guid
):
306 # Valid: { 0xf0b11735, 0x87a0, 0x4193, {0xb2, 0x66, 0x53, 0x8c, 0x38,
307 # 0xaf, 0x48, 0xce }}
308 # Invalid: { 0xf0b11735, 0x87a0, 0x4193, {0xb2, 0x66, 0x53, 0x8c, 0x38,
309 # 0xaf, 0x48, 0xce }} 0x123
310 # Invalid: { 0xf0b1 1735, 0x87a0, 0x4193, {0xb2, 0x66, 0x53, 0x8c, 0x38,
311 # 0xaf, 0x48, 0xce }}
313 List
= ['{', 10, ',', 6, ',', 6, ',{', 4, ',', 4, ',', 4,
314 ',', 4, ',', 4, ',', 4, ',', 4, ',', 4, '}}']
319 if Char
not in '{},\t ':
325 # Index may out of bound
327 if not SepValue
or SepValue
!= List
[Index
]:
332 if not Value
.startswith('0x') and not Value
.startswith('0X'):
336 # Index may out of bound
338 if not isinstance(List
[Index
], type(1)) or \
339 len(Value
) > List
[Index
] or len(Value
) < 3:
343 # Check if string can be converted to integer
344 # Throw exception if not
347 except BaseException
:
349 # Exception caught means invalid format
357 return SepValue
== '}}' and Value
== ''
361 # Check whether the PCD type is valid
363 # @param PcdTypeString: The PcdType string need to be checked.
365 def IsValidPcdType(PcdTypeString
):
366 if PcdTypeString
.upper() in PCD_USAGE_TYPE_LIST_OF_MODULE
:
373 # Check whether the word is valid.
374 # <Word> ::= (a-zA-Z0-9_)(a-zA-Z0-9_-){0,} Alphanumeric characters with
376 # dash "-" and/or underscore "_" characters. No whitespace
377 # characters are permitted.
379 # @param Word: The word string need to be checked.
381 def IsValidWord(Word
):
385 # The first char should be alpha, _ or Digit.
387 if not Word
[0].isalnum() and \
388 not Word
[0] == '_' and \
389 not Word
[0].isdigit():
393 for Char
in Word
[1:]:
394 if (not Char
.isalpha()) and \
395 (not Char
.isdigit()) and \
400 if Char
== '.' and LastChar
== '.':
409 # Check whether the SimpleWord is valid.
410 # <SimpleWord> ::= (a-zA-Z0-9)(a-zA-Z0-9_-){0,}
411 # A word that cannot contain a period character.
413 # @param Word: The word string need to be checked.
415 def IsValidSimpleWord(Word
):
416 ReIsValidSimpleWord
= \
417 re
.compile(r
"^[0-9A-Za-z][0-9A-Za-z\-_]*$", re
.DOTALL
)
422 if not ReIsValidSimpleWord
.match(Word
):
429 # Check whether the decimal version is valid.
430 # <DecVersion> ::= (0-9){1,} ["." (0-9){1,}]
432 # @param Word: The word string need to be checked.
434 def IsValidDecVersion(Word
):
435 if Word
.find('.') > -1:
436 ReIsValidDecVersion
= re
.compile(r
"[0-9]+\.?[0-9]+$")
438 ReIsValidDecVersion
= re
.compile(r
"[0-9]+$")
439 if ReIsValidDecVersion
.match(Word
) is None:
445 # Check whether the hex version is valid.
446 # <HexVersion> ::= "0x" <Major> <Minor>
447 # <Major> ::= <HexDigit>{4}
448 # <Minor> ::= <HexDigit>{4}
450 # @param Word: The word string need to be checked.
452 def IsValidHexVersion(Word
):
453 ReIsValidHexVersion
= re
.compile(r
"[0][xX][0-9A-Fa-f]{8}$", re
.DOTALL
)
454 if ReIsValidHexVersion
.match(Word
) is None:
459 ## IsValidBuildNumber
461 # Check whether the BUILD_NUMBER is valid.
462 # ["BUILD_NUMBER" "=" <Integer>{1,4} <EOL>]
464 # @param Word: The BUILD_NUMBER string need to be checked.
466 def IsValidBuildNumber(Word
):
467 ReIsValieBuildNumber
= re
.compile(r
"[0-9]{1,4}$", re
.DOTALL
)
468 if ReIsValieBuildNumber
.match(Word
) is None:
475 # Check whether the Depex is valid.
477 # @param Word: The Depex string need to be checked.
479 def IsValidDepex(Word
):
480 Index
= Word
.upper().find("PUSH")
482 return IsValidCFormatGuid(Word
[Index
+4:].strip())
484 ReIsValidCName
= re
.compile(r
"^[A-Za-z_][0-9A-Za-z_\s\.]*$", re
.DOTALL
)
485 if ReIsValidCName
.match(Word
) is None:
490 ## IsValidNormalizedString
493 # <NormalizedString> ::= <DblQuote> [{<Word>} {<Space>}]{1,} <DblQuote>
496 # @param String: string to be checked
498 def IsValidNormalizedString(String
):
506 StringList
= GetSplitValueList(String
, TAB_SPACE_SPLIT
)
508 for Item
in StringList
:
511 if not IsValidWord(Item
):
518 # Check whether the IdString is valid.
520 # @param IdString: The IdString need to be checked.
522 def IsValidIdString(String
):
523 if IsValidSimpleWord(String
.strip()):
526 if String
.strip().startswith('"') and \
527 String
.strip().endswith('"'):
528 String
= String
[1:-1]
529 if String
.strip() == "":
531 if IsValidNormalizedString(String
):
536 ## IsValidVersionString
538 # Check whether the VersionString is valid.
539 # <AsciiString> ::= [ [<WhiteSpace>]{0,} [<AsciiChars>]{0,} ] {0,}
540 # <WhiteSpace> ::= {<Tab>} {<Space>}
543 # <AsciiChars> ::= (0x21 - 0x7E)
545 # @param VersionString: The VersionString need to be checked.
547 def IsValidVersionString(VersionString
):
548 VersionString
= VersionString
.strip()
549 for Char
in VersionString
:
550 if not (Char
>= 0x21 and Char
<= 0x7E):
557 # Check whether the PcdValue is valid.
559 # @param VersionString: The PcdValue need to be checked.
561 def IsValidPcdValue(PcdValue
):
562 for Char
in PcdValue
:
563 if Char
== '\n' or Char
== '\t' or Char
== '\f':
569 if IsValidFeatureFlagExp(PcdValue
, True)[0]:
573 # <Number> ::= {<Integer>} {<HexNumber>}
574 # <Integer> ::= {(0-9)} {(1-9)(0-9){1,}}
575 # <HexNumber> ::= "0x" <HexDigit>{1,}
576 # <HexDigit> ::= (a-fA-F0-9)
578 if IsValidHex(PcdValue
):
581 ReIsValidIntegerSingle
= re
.compile(r
"^\s*[0-9]\s*$", re
.DOTALL
)
582 if ReIsValidIntegerSingle
.match(PcdValue
) is not None:
585 ReIsValidIntegerMulti
= re
.compile(r
"^\s*[1-9][0-9]+\s*$", re
.DOTALL
)
586 if ReIsValidIntegerMulti
.match(PcdValue
) is not None:
590 # <StringVal> ::= {<StringType>} {<Array>} {"$(" <MACRO> ")"}
591 # <StringType> ::= {<UnicodeString>} {<CString>}
593 ReIsValidStringType
= re
.compile(r
"^\s*[\"L
].*[\"]\s
*$
")
594 if ReIsValidStringType.match(PcdValue):
596 if PcdValue.strip().startswith('L\"'):
597 StringValue = PcdValue.strip().lstrip('L\"').rstrip('\"')
598 if IsValidBareCString(StringValue):
600 elif PcdValue.strip().startswith('\"'):
601 StringValue = PcdValue.strip().lstrip('\"').rstrip('\"')
602 if IsValidBareCString(StringValue):
608 # <Array> ::= {<CArray>} {<NList>} {<CFormatGUID>}
609 # <CArray> ::= "{" [<NList>] <CArray>{0,} "}"
610 # <NList> ::= <HexByte> ["," <HexByte>]{0,}
611 # <HexDigit> ::= (a-fA-F0-9)
612 # <HexByte> ::= "0x
" <HexDigit>{1,2}
614 if IsValidCFormatGuid(PcdValue):
617 ReIsValidByteHex = re.compile(r"^\s
*0x
[0-9a
-fA
-F
]{1,2}\s
*$
", re.DOTALL)
618 if PcdValue.strip().startswith('{') and PcdValue.strip().endswith('}') :
619 StringValue = PcdValue.strip().lstrip('{').rstrip('}')
620 ValueList = StringValue.split(',')
622 for ValueItem in ValueList:
623 if not ReIsValidByteHex.match(ValueItem.strip()):
633 ValueList = PcdValue.split(',')
634 for ValueItem in ValueList:
635 if not ReIsValidByteHex.match(ValueItem.strip()):
643 ## IsValidCVariableName
645 # Check whether the PcdValue is valid.
647 # @param VersionString: The PcdValue need to be checked.
649 def IsValidCVariableName(CName):
650 ReIsValidCName = re.compile(r"^
[A
-Za
-z_
][0-9A
-Za
-z_
]*$
", re.DOTALL)
651 if ReIsValidCName.match(CName) is None:
658 # <Identifier> ::= <NonDigit> <Chars>{0,}
659 # <Chars> ::= (a-zA-Z0-9_)
660 # <NonDigit> ::= (a-zA-Z_)
662 # @param Ident: identifier to be checked
664 def IsValidIdentifier(Ident):
665 ReIdent = re.compile(r"^
[A
-Za
-z_
][0-9A
-Za
-z_
]*$
", re.DOTALL)
666 if ReIdent.match(Ident) is None:
671 ## IsValidDecVersionVal
673 # {(0-9){1,} "." (0-99)}
675 # @param Ver: version to be checked
677 def IsValidDecVersionVal(Ver):
678 ReVersion = re.compile(r"[0-9]+(\
.[0-9]{1,2})$
")
680 if ReVersion.match(Ver) is None:
688 # (A-Z)(a-zA-Z0-9){0,} and could not be "NULL
"
690 def IsValidLibName(LibName):
691 if LibName == 'NULL':
693 ReLibName = re.compile("^
[A
-Z
]+[a
-zA
-Z0
-9]*$
")
694 if not ReLibName.match(LibName):
701 # <UserId> ::= (a-zA-Z)(a-zA-Z0-9_.){0,}
702 # Words that contain period "." must be encapsulated in double quotation marks.
704 def IsValidUserId(UserId):
705 UserId = UserId.strip()
707 if UserId.startswith('"') and UserId.endswith('"'):
709 UserId = UserId[1:-1]
710 if not UserId or not UserId[0].isalpha():
712 for Char in UserId[1:]:
713 if not Char.isalnum() and not Char in '_.':
715 if Char == '.' and not Quoted:
720 # Check if a UTF16-LE file has a BOM header
722 def CheckUTF16FileHeader(File):
723 FileIn = open(File, 'rb').read(2)
724 if FileIn != b'\xff\xfe':