1 ## @file ParserValidate.py
2 # Functions for parser validation
4 # Copyright (c) 2011 - 2014, Intel Corporation. All rights reserved.<BR>
6 # This program and the accompanying materials are licensed and made available
7 # under the terms and conditions of the BSD License which accompanies this
8 # distribution. The full text of the license may be found at
9 # http://opensource.org/licenses/bsd-license.php
11 # THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
23 from Library
.DataType
import MODULE_LIST
24 from Library
.DataType
import COMPONENT_TYPE_LIST
25 from Library
.DataType
import PCD_USAGE_TYPE_LIST_OF_MODULE
26 from Library
.DataType
import TAB_SPACE_SPLIT
27 from Library
.String
import GetSplitValueList
28 from Library
.ExpressionValidate
import IsValidBareCString
29 from Library
.ExpressionValidate
import IsValidFeatureFlagExp
31 ## __HexDigit() method
33 # Whether char input is a Hex data bit
35 # @param TempChar: The char to test
37 def __HexDigit(TempChar
):
38 if (TempChar
>= 'a' and TempChar
<= 'f') or \
39 (TempChar
>= 'A' and TempChar
<= 'F') \
40 or (TempChar
>= '0' and TempChar
<= '9'):
45 ## IsValidHex() method
47 # Whether char input is a Hex data.
49 # @param TempChar: The char to test
51 def IsValidHex(HexStr
):
52 if not HexStr
.upper().startswith("0X"):
54 CharList
= [c
for c
in HexStr
[2:] if not __HexDigit(c
)]
55 if len(CharList
) == 0:
60 ## Judge the input string is valid bool type or not.
62 # <TRUE> ::= {"TRUE"} {"true"} {"True"} {"0x1"} {"0x01"}
63 # <FALSE> ::= {"FALSE"} {"false"} {"False"} {"0x0"} {"0x00"}
64 # <BoolType> ::= {<TRUE>} {<FALSE>}
66 # @param BoolString: A string contained the value need to be judged.
68 def IsValidBoolType(BoolString
):
72 if BoolString
== 'TRUE' or \
73 BoolString
== 'True' or \
74 BoolString
== 'true' or \
75 BoolString
== '0x1' or \
81 elif BoolString
== 'FALSE' or \
82 BoolString
== 'False' or \
83 BoolString
== 'false' or \
84 BoolString
== '0x0' or \
93 ## Is Valid Module Type List or not
95 # @param ModuleTypeList: A list contain ModuleType strings need to be
98 def IsValidInfMoudleTypeList(ModuleTypeList
):
99 for ModuleType
in ModuleTypeList
:
100 return IsValidInfMoudleType(ModuleType
)
102 ## Is Valid Module Type or not
104 # @param ModuleType: A string contain ModuleType need to be judged.
106 def IsValidInfMoudleType(ModuleType
):
107 if ModuleType
in MODULE_LIST
:
112 ## Is Valid Component Type or not
114 # @param ComponentType: A string contain ComponentType need to be judged.
116 def IsValidInfComponentType(ComponentType
):
117 if ComponentType
.upper() in COMPONENT_TYPE_LIST
:
123 ## Is valid Tool Family or not
125 # @param ToolFamily: A string contain Tool Family need to be judged.
126 # Famlily := [A-Z]([a-zA-Z0-9])*
128 def IsValidToolFamily(ToolFamily
):
129 ReIsValieFamily
= re
.compile(r
"^[A-Z]+[A-Za-z0-9]{0,}$", re
.DOTALL
)
130 if ReIsValieFamily
.match(ToolFamily
) == None:
134 ## Is valid Tool TagName or not
136 # The TagName sample is MYTOOLS and VS2005.
138 # @param TagName: A string contain Tool TagName need to be judged.
140 def IsValidToolTagName(TagName
):
141 if TagName
.strip() == '':
143 if TagName
.strip() == '*':
145 if not IsValidWord(TagName
):
149 ## Is valid arch or not
151 # @param Arch The arch string need to be validated
152 # <OA> ::= (a-zA-Z)(A-Za-z0-9){0,}
153 # <arch> ::= {"IA32"} {"X64"} {"IPF"} {"EBC"} {<OA>}
155 # @param Arch: Input arch
157 def IsValidArch(Arch
):
160 ReIsValieArch
= re
.compile(r
"^[a-zA-Z]+[a-zA-Z0-9]{0,}$", re
.DOTALL
)
161 if ReIsValieArch
.match(Arch
) == None:
165 ## Is valid family or not
167 # <Family> ::= {"MSFT"} {"GCC"} {"INTEL"} {<Usr>} {"*"}
168 # <Usr> ::= [A-Z][A-Za-z0-9]{0,}
170 # @param family: The family string need to be validated
172 def IsValidFamily(Family
):
173 Family
= Family
.strip()
180 ReIsValidFamily
= re
.compile(r
"^[A-Z]+[A-Za-z0-9]{0,}$", re
.DOTALL
)
181 if ReIsValidFamily
.match(Family
) == None:
185 ## Is valid build option name or not
187 # @param BuildOptionName: The BuildOptionName string need to be validated
189 def IsValidBuildOptionName(BuildOptionName
):
190 if not BuildOptionName
:
193 ToolOptionList
= GetSplitValueList(BuildOptionName
, '_', 4)
195 if len(ToolOptionList
) != 5:
198 ReIsValidBuildOption1
= re
.compile(r
"^\s*(\*)|([A-Z][a-zA-Z0-9]*)$")
199 ReIsValidBuildOption2
= re
.compile(r
"^\s*(\*)|([a-zA-Z][a-zA-Z0-9]*)$")
201 if ReIsValidBuildOption1
.match(ToolOptionList
[0]) == None:
204 if ReIsValidBuildOption1
.match(ToolOptionList
[1]) == None:
207 if ReIsValidBuildOption2
.match(ToolOptionList
[2]) == None:
210 if ToolOptionList
[3] == "*" and ToolOptionList
[4] not in ['FAMILY', 'DLL', 'DPATH']:
217 # Check if pattern string matches total token
219 # @param ReString: regular string
220 # @param Token: Token to be matched
222 def IsValidToken(ReString
, Token
):
223 Match
= re
.compile(ReString
).match(Token
)
224 return Match
and Match
.start() == 0 and Match
.end() == len(Token
)
228 # Check if path exist
230 # @param Path: Absolute path or relative path to be checked
231 # @param Root: Root path
233 def IsValidPath(Path
, Root
):
235 OrigPath
= Path
.replace('\\', '/')
237 Path
= os
.path
.normpath(Path
).replace('\\', '/')
238 Root
= os
.path
.normpath(Root
).replace('\\', '/')
239 FullPath
= os
.path
.normpath(os
.path
.join(Root
, Path
)).replace('\\', '/')
241 if not os
.path
.exists(FullPath
):
245 # If Path is absolute path.
246 # It should be in Root.
248 if os
.path
.isabs(Path
):
249 if not Path
.startswith(Root
):
254 # Check illegal character
256 for Rel
in ['/', './', '../']:
257 if OrigPath
.startswith(Rel
):
259 for Rel
in ['//', '/./', '/../']:
262 for Rel
in ['/.', '/..', '/']:
263 if OrigPath
.endswith(Rel
):
266 Path
= Path
.rstrip('/')
269 # Check relative path
271 for Word
in Path
.split('/'):
272 if not IsValidWord(Word
):
277 ## IsValidInstallPath
279 # Check if an install path valid or not.
281 # Absolute path or path starts with '.' or path contains '..' are invalid.
283 # @param Path: path to be checked
285 def IsValidInstallPath(Path
):
286 if platform
.platform().find("Windows") >= 0:
287 if os
.path
.isabs(Path
):
292 if os
.path
.isabs(Path
):
294 if Path
.startswith('.'):
297 if Path
.find('..') != -1:
303 ## IsValidCFormatGuid
305 # Check if GUID format has the from of {8,4,4,{2,2,2,2,2,2,2,2}}
307 # @param Guid: Guid to be checked
309 def IsValidCFormatGuid(Guid
):
311 # Valid: { 0xf0b11735, 0x87a0, 0x4193, {0xb2, 0x66, 0x53, 0x8c, 0x38,
312 # 0xaf, 0x48, 0xce }}
313 # Invalid: { 0xf0b11735, 0x87a0, 0x4193, {0xb2, 0x66, 0x53, 0x8c, 0x38,
314 # 0xaf, 0x48, 0xce }} 0x123
315 # Invalid: { 0xf0b1 1735, 0x87a0, 0x4193, {0xb2, 0x66, 0x53, 0x8c, 0x38,
316 # 0xaf, 0x48, 0xce }}
318 List
= ['{', 10, ',', 6, ',', 6, ',{', 4, ',', 4, ',', 4,
319 ',', 4, ',', 4, ',', 4, ',', 4, ',', 4, '}}']
324 if Char
not in '{},\t ':
330 # Index may out of bound
332 if not SepValue
or SepValue
!= List
[Index
]:
337 if not Value
.startswith('0x') and not Value
.startswith('0X'):
341 # Index may out of bound
343 if type(List
[Index
]) != type(1) or \
344 len(Value
) > List
[Index
] or len(Value
) < 3:
348 # Check if string can be converted to integer
349 # Throw exception if not
352 except BaseException
:
354 # Exception caught means invalid format
362 return SepValue
== '}}' and Value
== ''
366 # Check whether the PCD type is valid
368 # @param PcdTypeString: The PcdType string need to be checked.
370 def IsValidPcdType(PcdTypeString
):
371 if PcdTypeString
.upper() in PCD_USAGE_TYPE_LIST_OF_MODULE
:
378 # Check whether the word is valid.
379 # <Word> ::= (a-zA-Z0-9_)(a-zA-Z0-9_-){0,} Alphanumeric characters with
381 # dash "-" and/or underscore "_" characters. No whitespace
382 # characters are permitted.
384 # @param Word: The word string need to be checked.
386 def IsValidWord(Word
):
390 # The first char should be alpha, _ or Digit.
392 if not Word
[0].isalnum() and \
393 not Word
[0] == '_' and \
394 not Word
[0].isdigit():
398 for Char
in Word
[1:]:
399 if (not Char
.isalpha()) and \
400 (not Char
.isdigit()) and \
405 if Char
== '.' and LastChar
== '.':
414 # Check whether the SimpleWord is valid.
415 # <SimpleWord> ::= (a-zA-Z0-9)(a-zA-Z0-9_-){0,}
416 # A word that cannot contain a period character.
418 # @param Word: The word string need to be checked.
420 def IsValidSimpleWord(Word
):
421 ReIsValidSimpleWord
= \
422 re
.compile(r
"^[0-9A-Za-z][0-9A-Za-z\-_]*$", re
.DOTALL
)
427 if not ReIsValidSimpleWord
.match(Word
):
434 # Check whether the decimal version is valid.
435 # <DecVersion> ::= (0-9){1,} ["." (0-9){1,}]
437 # @param Word: The word string need to be checked.
439 def IsValidDecVersion(Word
):
440 if Word
.find('.') > -1:
441 ReIsValidDecVersion
= re
.compile(r
"[0-9]+\.?[0-9]+$")
443 ReIsValidDecVersion
= re
.compile(r
"[0-9]+$")
444 if ReIsValidDecVersion
.match(Word
) == None:
450 # Check whether the hex version is valid.
451 # <HexVersion> ::= "0x" <Major> <Minor>
452 # <Major> ::= <HexDigit>{4}
453 # <Minor> ::= <HexDigit>{4}
455 # @param Word: The word string need to be checked.
457 def IsValidHexVersion(Word
):
458 ReIsValidHexVersion
= re
.compile(r
"[0][xX][0-9A-Fa-f]{8}$", re
.DOTALL
)
459 if ReIsValidHexVersion
.match(Word
) == None:
464 ## IsValidBuildNumber
466 # Check whether the BUILD_NUMBER is valid.
467 # ["BUILD_NUMBER" "=" <Integer>{1,4} <EOL>]
469 # @param Word: The BUILD_NUMBER string need to be checked.
471 def IsValidBuildNumber(Word
):
472 ReIsValieBuildNumber
= re
.compile(r
"[0-9]{1,4}$", re
.DOTALL
)
473 if ReIsValieBuildNumber
.match(Word
) == None:
480 # Check whether the Depex is valid.
482 # @param Word: The Depex string need to be checked.
484 def IsValidDepex(Word
):
485 Index
= Word
.upper().find("PUSH")
487 return IsValidCFormatGuid(Word
[Index
+4:].strip())
489 ReIsValidCName
= re
.compile(r
"^[A-Za-z_][0-9A-Za-z_\s\.]*$", re
.DOTALL
)
490 if ReIsValidCName
.match(Word
) == None:
495 ## IsValidNormalizedString
498 # <NormalizedString> ::= <DblQuote> [{<Word>} {<Space>}]{1,} <DblQuote>
501 # @param String: string to be checked
503 def IsValidNormalizedString(String
):
511 StringList
= GetSplitValueList(String
, TAB_SPACE_SPLIT
)
513 for Item
in StringList
:
516 if not IsValidWord(Item
):
523 # Check whether the IdString is valid.
525 # @param IdString: The IdString need to be checked.
527 def IsValidIdString(String
):
528 if IsValidSimpleWord(String
.strip()):
531 if String
.strip().startswith('"') and \
532 String
.strip().endswith('"'):
533 String
= String
[1:-1]
534 if String
.strip() == "":
536 if IsValidNormalizedString(String
):
541 ## IsValidVersionString
543 # Check whether the VersionString is valid.
544 # <AsciiString> ::= [ [<WhiteSpace>]{0,} [<AsciiChars>]{0,} ] {0,}
545 # <WhiteSpace> ::= {<Tab>} {<Space>}
548 # <AsciiChars> ::= (0x21 - 0x7E)
550 # @param VersionString: The VersionString need to be checked.
552 def IsValidVersionString(VersionString
):
553 VersionString
= VersionString
.strip()
554 for Char
in VersionString
:
555 if not (Char
>= 0x21 and Char
<= 0x7E):
562 # Check whether the PcdValue is valid.
564 # @param VersionString: The PcdValue need to be checked.
566 def IsValidPcdValue(PcdValue
):
567 for Char
in PcdValue
:
568 if Char
== '\n' or Char
== '\t' or Char
== '\f':
574 if IsValidFeatureFlagExp(PcdValue
, True)[0]:
578 # <Number> ::= {<Integer>} {<HexNumber>}
579 # <Integer> ::= {(0-9)} {(1-9)(0-9){1,}}
580 # <HexNumber> ::= "0x" <HexDigit>{1,}
581 # <HexDigit> ::= (a-fA-F0-9)
583 if IsValidHex(PcdValue
):
586 ReIsValidIntegerSingle
= re
.compile(r
"^\s*[0-9]\s*$", re
.DOTALL
)
587 if ReIsValidIntegerSingle
.match(PcdValue
) != None:
590 ReIsValidIntegerMulti
= re
.compile(r
"^\s*[1-9][0-9]+\s*$", re
.DOTALL
)
591 if ReIsValidIntegerMulti
.match(PcdValue
) != None:
595 # <StringVal> ::= {<StringType>} {<Array>} {"$(" <MACRO> ")"}
596 # <StringType> ::= {<UnicodeString>} {<CString>}
598 ReIsValidStringType
= re
.compile(r
"^\s*[\"L
].*[\"]\s
*$
")
599 if ReIsValidStringType.match(PcdValue):
601 if PcdValue.strip().startswith('L\"'):
602 StringValue = PcdValue.strip().lstrip('L\"').rstrip('\"')
603 if IsValidBareCString(StringValue):
605 elif PcdValue.strip().startswith('\"'):
606 StringValue = PcdValue.strip().lstrip('\"').rstrip('\"')
607 if IsValidBareCString(StringValue):
613 # <Array> ::= {<CArray>} {<NList>} {<CFormatGUID>}
614 # <CArray> ::= "{" [<NList>] <CArray>{0,} "}"
615 # <NList> ::= <HexByte> ["," <HexByte>]{0,}
616 # <HexDigit> ::= (a-fA-F0-9)
617 # <HexByte> ::= "0x
" <HexDigit>{1,2}
619 if IsValidCFormatGuid(PcdValue):
622 ReIsValidByteHex = re.compile(r"^\s
*0x
[0-9a
-fA
-F
]{1,2}\s
*$
", re.DOTALL)
623 if PcdValue.strip().startswith('{') and PcdValue.strip().endswith('}') :
624 StringValue = PcdValue.strip().lstrip('{').rstrip('}')
625 ValueList = StringValue.split(',')
627 for ValueItem in ValueList:
628 if not ReIsValidByteHex.match(ValueItem.strip()):
638 ValueList = PcdValue.split(',')
639 for ValueItem in ValueList:
640 if not ReIsValidByteHex.match(ValueItem.strip()):
648 ## IsValidCVariableName
650 # Check whether the PcdValue is valid.
652 # @param VersionString: The PcdValue need to be checked.
654 def IsValidCVariableName(CName):
655 ReIsValidCName = re.compile(r"^
[A
-Za
-z_
][0-9A
-Za
-z_
]*$
", re.DOTALL)
656 if ReIsValidCName.match(CName) == None:
663 # <Identifier> ::= <NonDigit> <Chars>{0,}
664 # <Chars> ::= (a-zA-Z0-9_)
665 # <NonDigit> ::= (a-zA-Z_)
667 # @param Ident: identifier to be checked
669 def IsValidIdentifier(Ident):
670 ReIdent = re.compile(r"^
[A
-Za
-z_
][0-9A
-Za
-z_
]*$
", re.DOTALL)
671 if ReIdent.match(Ident) == None:
676 ## IsValidDecVersionVal
678 # {(0-9){1,} "." (0-99)}
680 # @param Ver: version to be checked
682 def IsValidDecVersionVal(Ver):
683 ReVersion = re.compile(r"[0-9]+(\
.[0-9]{1,2})$
")
685 if ReVersion.match(Ver) == None:
693 # (A-Z)(a-zA-Z0-9){0,} and could not be "NULL
"
695 def IsValidLibName(LibName):
696 if LibName == 'NULL':
698 ReLibName = re.compile("^
[A
-Z
]+[a
-zA
-Z0
-9]*$
")
699 if not ReLibName.match(LibName):
706 # <UserId> ::= (a-zA-Z)(a-zA-Z0-9_.){0,}
707 # Words that contain period "." must be encapsulated in double quotation marks.
709 def IsValidUserId(UserId):
710 UserId = UserId.strip()
712 if UserId.startswith('"') and UserId.endswith('"'):
714 UserId = UserId[1:-1]
715 if not UserId or not UserId[0].isalpha():
717 for Char in UserId[1:]:
718 if not Char.isalnum() and not Char in '_.':
720 if Char == '.' and not Quoted:
725 # Check if a UTF16-LE file has a BOM header
727 def CheckUTF16FileHeader(File):
728 FileIn = open(File, 'rb').read(2)
729 if FileIn != '\xff\xfe':