def TestToolFuncs(): assert IsValidCArray('{0x1, 0x23}') # Empty after comma assert not IsValidCArray('{0x1, 0x23, }') # 0x2345 too long assert not IsValidCArray('{0x1, 0x2345}') # Must end with '}' assert not IsValidCArray('{0x1, 0x23, ') # Whitespace between numbers assert not IsValidCArray('{0x1, 0x2 3, }') assert IsValidPcdDatum('VOID*', '"test"')[0] assert IsValidPcdDatum('VOID*', 'L"test"')[0] assert IsValidPcdDatum('BOOLEAN', 'TRUE')[0] assert IsValidPcdDatum('BOOLEAN', 'FALSE')[0] assert IsValidPcdDatum('BOOLEAN', '0')[0] assert IsValidPcdDatum('BOOLEAN', '1')[0] assert IsValidPcdDatum('UINT8', '0xab')[0] assert not IsValidPcdDatum('UNKNOWNTYPE', '0xabc')[0] assert not IsValidPcdDatum('UINT8', 'not number')[0] assert (IsValidCFormatGuid( '{ 0xfa0b1735 , 0x87a0, 0x4193, {0xb2, 0x66 , 0x53, 0x8c , 0x38, 0xaf, 0x48, 0xce }}' )) assert (not IsValidCFormatGuid( '{ 0xfa0b1735 , 0x87a0, 0x4193, {0xb2, 0x66 , 0x53, 0x8c , 0x38, 0xaf, 0x48, 0xce }} 0xaa' ))
def IsValidPcdDatum(Type, Value): if not Value: return False, ST.ERR_DECPARSE_PCD_VALUE_EMPTY Valid = True Cause = "" if Type not in ["UINT8", "UINT16", "UINT32", "UINT64", "VOID*", "BOOLEAN"]: return False, ST.ERR_DECPARSE_PCD_TYPE if Type == "VOID*": if not ((Value.startswith('L"') or Value.startswith('"') and \ Value.endswith('"')) or (IsValidCArray(Value)) or (IsValidCFormatGuid(Value)) \ or (IsValidNList(Value)) or (CheckGuidRegFormat(Value)) ): return False, ST.ERR_DECPARSE_PCD_VOID % (Value, Type) RealString = Value[Value.find('"') + 1:-1] if RealString: if not IsValidBareCString(RealString): return False, ST.ERR_DECPARSE_PCD_VOID % (Value, Type) elif Type == 'BOOLEAN': if Value in [ 'TRUE', 'FALSE', 'true', 'false', 'True', 'False', '0x1', '0x01', '1', '0x0', '0x00', '0' ]: return True, "" Valid, Cause = IsValidStringTest(Value, True) if not Valid: Valid, Cause = IsValidFeatureFlagExp(Value, True) if not Valid: return False, Cause else: if Value and (Value[0] == '-' or Value[0] == '+'): return False, ST.ERR_DECPARSE_PCD_INT_NEGTIVE % (Value, Type) try: StrVal = Value if Value and not Value.startswith('0x') \ and not Value.startswith('0X'): Value = Value.lstrip('0') if not Value: return True, "" Value = int(Value, 0) MAX_VAL_TYPE = { "BOOLEAN": 0x01, 'UINT8': 0xFF, 'UINT16': 0xFFFF, 'UINT32': 0xFFFFFFFF, 'UINT64': 0xFFFFFFFFFFFFFFFF } if Value > MAX_VAL_TYPE[Type]: return False, ST.ERR_DECPARSE_PCD_INT_EXCEED % (StrVal, Type) except BaseException: Valid, Cause = IsValidLogicalExpr(Value, True) if not Valid: return False, Cause return True, ""
def _ParseItem(self): Line = self._RawData.CurrentLine TokenList = GetSplitValueList(Line, DT.TAB_EQUAL_SPLIT, 1) if len(TokenList) < 2: self._LoggerError(ST.ERR_DECPARSE_CGUID) if TokenList[0] == '': self._LoggerError(ST.ERR_DECPARSE_CGUID_NAME) if TokenList[1] == '': self._LoggerError(ST.ERR_DECPARSE_CGUID_GUID) if not IsValidToken(CVAR_PATTERN, TokenList[0]): self._LoggerError(ST.ERR_DECPARSE_PCD_CVAR_GUID) self._CheckReDefine(TokenList[0]) if TokenList[1][0] != '{': if not CheckGuidRegFormat(TokenList[1]): self._LoggerError(ST.ERR_DECPARSE_DEFINE_PKGGUID) GuidString = TokenList[1] else: # # Convert C format GUID to GUID string and Simple error check # GuidString = GuidStructureStringToGuidString(TokenList[1]) if TokenList[1][0] != '{' or TokenList[1][ -1] != '}' or GuidString == '': self._LoggerError(ST.ERR_DECPARSE_CGUID_GUIDFORMAT) # # Check C format GUID # if not IsValidCFormatGuid(TokenList[1]): self._LoggerError(ST.ERR_DECPARSE_CGUID_GUIDFORMAT) Item = DecGuidItemObject(TokenList[0], TokenList[1], GuidString) ItemObject = self.ObjectDict[self._RawData.CurrentScope[0][0]] ItemObject.AddItem(Item, self._RawData.CurrentScope) return Item
def IsValidPcdDatum(Type, Value): if not Value: return False, ST.ERR_DECPARSE_PCD_VALUE_EMPTY Valid = True Cause = "" if Type not in ["UINT8", "UINT16", "UINT32", "UINT64", "VOID*", "BOOLEAN"]: return False, ST.ERR_DECPARSE_PCD_TYPE if Type == "VOID*": if not ((Value.startswith('L"') or Value.startswith('"') and \ Value.endswith('"')) or (IsValidCArray(Value)) or (IsValidCFormatGuid(Value)) \ or (IsValidNList(Value)) or (CheckGuidRegFormat(Value)) ): return False, ST.ERR_DECPARSE_PCD_VOID % (Value, Type) RealString = Value[Value.find('"') + 1 :-1] if RealString: if not IsValidBareCString(RealString): return False, ST.ERR_DECPARSE_PCD_VOID % (Value, Type) elif Type == 'BOOLEAN': if Value in ['TRUE', 'FALSE', 'true', 'false', 'True', 'False', '0x1', '0x01', '1', '0x0', '0x00', '0']: return True, "" Valid, Cause = IsValidStringTest(Value, True) if not Valid: Valid, Cause = IsValidFeatureFlagExp(Value, True) if not Valid: return False, Cause else: if Value and (Value[0] == '-' or Value[0] == '+'): return False, ST.ERR_DECPARSE_PCD_INT_NEGTIVE % (Value, Type) try: StrVal = Value if Value and not Value.startswith('0x') \ and not Value.startswith('0X'): Value = Value.lstrip('0') if not Value: return True, "" Value = long(Value, 0) TypeLenMap = { # # 0x00 - 0xff # 'UINT8' : 2, # # 0x0000 - 0xffff # 'UINT16' : 4, # # 0x00000000 - 0xffffffff # 'UINT32' : 8, # # 0x0 - 0xffffffffffffffff # 'UINT64' : 16 } HexStr = hex(Value) # # First two chars of HexStr are 0x and tail char is L # if TypeLenMap[Type] < len(HexStr) - 3: return False, ST.ERR_DECPARSE_PCD_INT_EXCEED % (StrVal, Type) except BaseException: Valid, Cause = IsValidLogicalExpr(Value, True) if not Valid: return False, Cause return True, ""