-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathByteAdder.py
More file actions
executable file
·155 lines (122 loc) · 3.58 KB
/
Copy pathByteAdder.py
File metadata and controls
executable file
·155 lines (122 loc) · 3.58 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#!/usr/bin/python3
# Program : ByteAdder.py
# Author : Brian Mork ("Hermit")
# Thanks to: haystack-ia (https://github.com/haystack-ia) for removing Hexdump dependency
# Date : 2019-04-01
# Contact : https://blog.stackattack.net
# Requires : Python3+
import sys
import os
THEFILE = ''
def showHelpInfo():
print('Usage: ByteAdder.py {sum|high|low|lsb|plsb|all} FILE')
print(' sum Sum all bytes in FILE')
print(' high Sum all bytes in the high nibble of FILE')
print(' low Sum all bytes in the low nibble of FILE')
print(' lsb Sum only least significant bits')
print(' plsb Print least significant bits')
print(' alls Run all SUM variants. This can take a long time!')
print(' allall Run all variants. This can take a long time!')
def showHeader():
print('+-----------------------+')
print('| ByteAdder version 1.0 |')
print('| by Hermit |')
print('+-----------------------+')
print()
def showFooter():
print()
print('+-----------------------+')
print('| ~~~ { fin }~~~ |')
print('+-----------------------+')
def showInvalidMode():
print('ERROR: Invalid mode selected.');
showHelpInfo();
sys.exit(1);
def printSumResult(CKSUM):
CKSUMH = hex(CKSUM)
print(' Sum in dec is: ', str(CKSUM))
print(' Sum in hex is: ', str(CKSUMH))
print()
def __masklist(AFILE, mask, shift=0):
'''
returns list of masked bytes in bytes object
'''
return [(x & mask) >> shift for x in AFILE]
def __masksum(AFILE, mask, shift=0):
'''
sums (byte & mask) for byte in AFILE
'''
return sum(__masklist(AFILE, mask, shift=shift))
def sumBytes(AFILE):
'''
prints sum of bytes in file
'''
print('[Summing all bytes]')
cksum = __masksum(AFILE, 0xFF)
printSumResult(cksum)
def sumHighNibble(AFILE):
'''
prints sum of high nibbles of a bytes object
'''
print('[Summing high nibble bytes]')
cksum = __masksum(AFILE, 0xF0, shift=4)
printSumResult(cksum)
def sumLowNibble(AFILE):
'''
prints sum of low nibbles of a bytes object
'''
print('[Summing low nibble bytes]')
cksum = __masksum(AFILE, 0x0F)
printSumResult(cksum)
def sumLSBs(AFILE):
'''
prints sum of least significant bits of a bytes object
'''
print('[Summing least significant bits]')
bit_sum = __masksum(AFILE, 0x1)
printSumResult(bit_sum)
def printLSBs(AFILE):
'''
prints least significant bits of a bytes object
'''
print('[Printing least significant bits]')
bitstream = ''.join([str(x) for x in __masklist(AFILE, 0x1)])
print(bitstream)
if ((len(sys.argv) != 3)):
showHelpInfo()
sys.exit(1)
else:
THEFILE = sys.argv[2]
fExists = os.path.isfile(THEFILE)
if (fExists):
theFile = open(THEFILE, 'rb').read()
else:
print('ERROR: The input file %s was not found' % THEFILE)
sys.exit(1)
THEOPTION = sys.argv[1]
showHeader()
# Thanks to haystack-ia for conversion of if/elif/else to dictionary
funs = {
'sum': sumBytes,
'high': sumHighNibble,
'low': sumLowNibble,
'lsb': sumLSBs,
'plsb': printLSBs,
'alls': [#ALL!
sumBytes, sumHighNibble, sumLowNibble,
sumLSBs
],
'allall': [#NO, ALL!
sumBytes, sumHighNibble, sumLowNibble,
sumLSBs, printLSBs
]
}
fun_to_run = funs.get(THEOPTION, None)
if fun_to_run is None:
showInvalidMode()
elif type(fun_to_run) is list:
[fun(theFile) for fun in fun_to_run]
else:
fun_to_run(theFile)
showFooter()
sys.exit(0)