-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.py
executable file
·120 lines (107 loc) · 3.3 KB
/
utils.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
#!/usr/bin/python
#
# Copyright 2019 Steve White
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included
# in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# DEALINGS IN THE SOFTWARE.
#
import re
def round_up(a, b):
return (((a) + ((b)-1)) & ~((b)-1))
def string_to_bits(str):
bits = []
match = re.match("^([0-9]*)'([BbHhOoDd])([0-9a-fA-F_]*)$", str)
if match is None:
radix = 'd'
size = 0
else:
comps = match.groups()
size = int(comps[0])
radix = comps[1].lower()
str = comps[2]
assert radix in ["b","d","h"]
if radix == 'b':
for char in str:
if char == '_':
continue
bits.append(1 if char == '1' else 0)
elif radix == 'd':
val = int(str)
size = 1
while val > 0:
bits.append(val & 1)
val = val >> 1
bits = bits[::-1]
elif radix == 'h':
for char in str:
if char == '_':
continue
nibble = int(char, 16)
for idx in range(0, 4):
bits.append((nibble >> (3-idx)) & 1)
while len(bits) < size:
bits.insert(0, 0)
return bits
def num_to_bits(num, length):
bits = []
for idx in range(length-1, -1, -1):
bits.append((num >> idx) & 1)
return bits
def bytes_to_num(bytes):
num = 0
for byte in bytes:
num = (num << 8) | byte
return num
def bits_invert(bits):
return [int(not bit) for bit in bits]
def bits_to_num(bits):
result = 0
for bit in bits:
result = (result << 1) | bit
return result
def bits_to_string(bits, spacing=0, prefix=False):
r = bits
if spacing > 0:
r = r[:]
num = 0
for i in range(spacing, len(bits), spacing):
r.insert(i + num, '_')
num += 1
base = ''
if prefix:
base = str(len(bits)) + '\'b'
return base + ''.join([str(x) for x in r])
def bytes_to_bits(bytes):
bits = []
for byte in bytes:
for i in range(0,8):
bits.append((byte >> 7) & 1)
byte = byte << 1
return bits
def bits_to_bytes(bits):
bytes = []
byte = 0
for i in range(0, len(bits)):
bit = bits[i]
byte = (byte << 1) | bit
if i & 7 == 7:
bytes.append(byte)
byte = 0
if i & 7 != 7:
bytes.append(byte)
return bytes