-
Notifications
You must be signed in to change notification settings - Fork 0
/
hex_types.py
131 lines (111 loc) · 2.89 KB
/
hex_types.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
"""
hex_types.to_hex(data)
convert data into a list of strings with the hex representation of data
to be used inside a BPF map as key or value
data can be:
- a list
- an object that supports a to_hex() method
- an int (converted to 8 bytes little-endian)
- an ipaddress.IPv6Address (converted in big-endian)
"""
import math
import ipaddress
class uGeneric:
def __init__(self, size, data):
self.size = size
self.set(data)
def __call__(self):
return self.get()
def set(self,data):
if data < 0:
raise OverflowError
elif data < 2**(self.size):
#self.v = data % 2**self.size
self.v = data
else:
raise OverflowError
def get(self):
return self.v
def toHex(self):
tmp = self.v
hex_list = []
for i in range (0,math.ceil(self.size/8)):
hex_list.append("{:02x}".format(tmp % 256))
tmp = tmp >> 8
return hex_list
class sGeneric(uGeneric):
def __init__(self, size, data):
super().__init__(size, data)
def set(self,data):
if data < - (2**(self.size-1)):
raise OverflowError
#elif data < 0:
# self.v = data
elif data < 2**(self.size-1):
self.v = data
else:
raise OverflowError
class u256(uGeneric):
def __init__(self, data):
super().__init__(256, data)
class u128(uGeneric):
def __init__(self, data):
super().__init__(128, data)
class u96(uGeneric):
def __init__(self, data):
super().__init__(96, data)
class u64(uGeneric):
def __init__(self, data):
super().__init__(64, data)
class u48(uGeneric):
def __init__(self, data):
super().__init__(48, data)
class u32(uGeneric):
def __init__(self, data):
super().__init__(32, data)
class u16(uGeneric):
def __init__(self, data):
super().__init__(16, data)
class u8(uGeneric):
def __init__(self, data):
super().__init__(8, data)
class s128(sGeneric):
def __init__(self, data):
super().__init__(128, data)
class s64(sGeneric):
def __init__(self, data):
super().__init__(64, data)
class s32(sGeneric):
def __init__(self, data):
super().__init__(32, data)
class s16(sGeneric):
def __init__(self, data):
super().__init__(16, data)
class s8(sGeneric):
def __init__(self, data):
super().__init__(8, data)
def to_hex (data):
#print (type(data))
hex_list = []
if type(data) == type([]):
for e in data:
hex_list.extend(to_hex(e))
elif type(data) == type(ipaddress.IPv6Address('::1')):
hex_list = to_hex(u128(int(data)))
hex_list.reverse()
elif type(data) == type(0):
hex_list = to_hex(u64(data))
else:
hex_list = data.toHex()
return hex_list
"""
converts a list of 16 integers 0-255 (big-endian) to an integer (128 bit equivalent)
"""
def ipv6_int128_from_int8(input_list):
ipv6_int128 = 0
i = 15
for int8 in input_list:
ipv6_int128 = ipv6_int128 | (int8 << (i*8))
i = i - 1
#print (ipv6_int128)
return ipv6_int128