1 # Copyright (c) 2022 Intel and/or its affiliates.
2 # Licensed under the Apache License, Version 2.0 (the "License");
3 # you may not use this file except in compliance with the License.
4 # You may obtain a copy of the License at:
6 # http://www.apache.org/licenses/LICENSE-2.0
8 # Unless required by applicable law or agreed to in writing, software
9 # distributed under the License is distributed on an "AS IS" BASIS,
10 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 # See the License for the specific language governing permissions and
12 # limitations under the License.
14 from InputFormat import InputFormat
17 def ByteArrayToString(data):
23 for i in range(len(data) - 1):
24 sb.append("%02x" % data[i])
26 sb.append("%02x" % data[len(data) - 1])
37 if exp.startswith("0x"):
48 def ToIPv4Address(exp):
52 tokens = exp.split(".")
55 return False, bytes(4)
60 return False, bytes(4)
64 return True, bytes(ipv4)
67 def ToIPv6Address(exp):
71 tokens = exp.split(":")
74 return False, bytes(16)
77 u16 = int(tokens[i], 16)
79 return False, bytes(16)
81 ipv6[i * 2] = u16 >> 8
82 ipv6[i * 2 + 1] = u16 & 0xFF
84 return True, bytes(ipv6)
87 def ToMacAddress(exp):
91 tokens = exp.split(":")
94 return False, bytes(6)
97 u8 = int(tokens[i], 16)
99 return False, bytes(6)
103 return True, bytes(mac)
106 def ToByteArray(exp):
108 tokens = exp.split(",")
110 tmp = [] * len(tokens)
112 for i in range(len(tokens)):
113 _, num = ToNum(tokens[i])
115 return False, bytes(len(tokens))
117 tmp[i] = ToNum(tokens[i])
119 return True, bytes(tmp)
122 def Verify(format, expression):
124 format == InputFormat.u8
125 or format == InputFormat.u16
126 or format == InputFormat.u32
127 or format == InputFormat.u64
129 return ToNum(expression)
130 elif format == InputFormat.ipv4:
131 return ToIPv4Address(expression)
132 elif format == InputFormat.ipv6:
133 return ToIPv6Address(expression)
134 elif format == InputFormat.mac:
135 return ToMacAddress(expression)
136 elif format == InputFormat.bytearray:
137 return ToByteArray(expression)
142 def IncreaseValue(expression, size):
143 if expression == None:
146 _, num = ToNum(expression)
147 return str(num + size)
154 _, num_1 = ToNum(exp)
158 _, num_2 = ToNum(val)
162 return num_1 == num_2