blob: 1f9c73fedfa1d7101a5c2918948f9473f23820a0 [file] [log] [blame]
Ting Xuce4b6452022-04-24 06:14:25 +00001# 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:
5#
6# http://www.apache.org/licenses/LICENSE-2.0
7#
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.
13
14from InputFormat import InputFormat
15
16
17def ByteArrayToString(data):
18 if len(data) == 0:
19 return ""
20
21 sb = []
22
23 for i in range(len(data) - 1):
24 sb.append("%02x" % data[i])
25
26 sb.append("%02x" % data[len(data) - 1])
27
28 return sb
29
30
31def ToNum(exp):
32 if exp == None:
33 return True, None
34
35 exp = exp.strip()
36
37 if exp.startswith("0x"):
38 out = int(exp, 16)
39 else:
40 try:
41 out = int(exp)
42 except:
43 return False, None
44
45 return True, out
46
47
48def ToIPv4Address(exp):
49 ipv4 = [0] * 4
50
51 exp = exp.strip()
52 tokens = exp.split(".")
53
54 if len(tokens) != 4:
55 return False, bytes(4)
56
57 for i in range(4):
58 u8 = int(tokens[i])
59 if u8 == None:
60 return False, bytes(4)
61
62 ipv4[i] = u8
63
64 return True, bytes(ipv4)
65
66
67def ToIPv6Address(exp):
68 ipv6 = [0] * 16
69
70 exp = exp.strip()
71 tokens = exp.split(":")
72
73 if len(tokens) != 8:
74 return False, bytes(16)
75
76 for i in range(8):
77 u16 = int(tokens[i], 16)
78 if u16 == None:
79 return False, bytes(16)
80
81 ipv6[i * 2] = u16 >> 8
82 ipv6[i * 2 + 1] = u16 & 0xFF
83
84 return True, bytes(ipv6)
85
86
87def ToMacAddress(exp):
88 mac = [0] * 6
89
90 exp = exp.strip()
91 tokens = exp.split(":")
92
93 if len(tokens) != 6:
94 return False, bytes(6)
95
96 for i in range(6):
97 u8 = int(tokens[i], 16)
98 if u8 == None:
99 return False, bytes(6)
100
101 mac[i] = u8
102
103 return True, bytes(mac)
104
105
106def ToByteArray(exp):
107 exp = exp.strip()
108 tokens = exp.split(",")
109
110 tmp = [] * len(tokens)
111
112 for i in range(len(tokens)):
113 _, num = ToNum(tokens[i])
114 if num == 0:
115 return False, bytes(len(tokens))
116
117 tmp[i] = ToNum(tokens[i])
118
119 return True, bytes(tmp)
120
121
122def Verify(format, expression):
123 if (
124 format == InputFormat.u8
125 or format == InputFormat.u16
126 or format == InputFormat.u32
127 or format == InputFormat.u64
128 ):
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)
138 else:
139 return False, 0
140
141
142def IncreaseValue(expression, size):
143 if expression == None:
144 return str(size)
145
146 _, num = ToNum(expression)
147 return str(num + size)
148
149
150def Equal(exp, val):
151 if exp == None:
152 num_1 = 0
153 else:
154 _, num_1 = ToNum(exp)
155 if not num_1:
156 return False
157
158 _, num_2 = ToNum(val)
159 if not num_2:
160 return False
161
162 return num_1 == num_2