-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathellyBits.py
executable file
·410 lines (349 loc) · 11.2 KB
/
ellyBits.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
#!/usr/bin/python3
# PyElly - rule-based tool for analyzing natural language (Python v3.8)
#
# ellyBits.py : 09nov2019 CPM
# ------------------------------------------------------------------------------
# Copyright (c) 2019, Clinton Prentiss Mah
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# -----------------------------------------------------------------------------
"""
define bit operations on byte arrays required by Elly parsing
"""
import array
hxh = { '0':0 , '1':1 , '2':2 , '3':3 , '4':4 , # convert hex digit to integer
'5':5 , '6':6 , '7':7 , '8':8 , '9':9 , #
'A':10 , 'B':11 , 'C':12 , #
'D':13 , 'E':14 , 'F':15 #
}
hxd = '0123456789ABCDEF' # for converting to hexadecimal
sel = [ 0o200, 0o100, 0o040, 0o020, 0o010, 0o004, 0o002, 0o001 ] # define bits in single byte
usl = [ 0o177, 0o277, 0o337, 0o357, 0o367, 0o373, 0o375, 0o373 ] # define bits in single byte
N = len(sel) # bit-count in byte
nB = 2 # default byte count for bit string
def _com(x): return(255-x) # hack to get complement of unsigned byte x
# but should NOT have to do this!
def getByteCountFor ( nbc ):
"""
set default byte count from bit count wanted
arguments:
nbc - bit count wanted
returns:
number of bytes
"""
n , m = divmod(nbc,N)
if m > 0: n += 1
return n
def join ( a , b ):
"""
combine pair of EllyBits to compare against compound bits
arguments:
a - EllyBits object
b - EllyBits object
returns:
merged array of bytes for comparison
"""
return a.data + b.data
def check ( bs , ps ):
"""
check for match of compounded bits with positive and negative bits
arguments:
bs - compounded bits as btye array
ps - positive and negative pattern bits as btye array
returns:
True on match, False otherwise
"""
n = len(bs)
for i in range(n):
if (bs[i] & ps[i]) != 0: return False
return True
def show ( bs ):
"""
convert join'ed bits into hexadecimal
arguments:
bs - bit string as array of bytes
returns:
hexadecimal representation
"""
try:
return ' '.join(map(lambda b: '{:02x}'.format(b),bs))
except ValueError:
return '--'
class EllyBits(object):
"""
bit string as Python array of byte values
attributes:
data - array for bits
"""
def __init__ ( self , nob=nB*N ):
"""
initialize
arguments:
self -
nob - number of bits in string
"""
# print ( 'nob=' , nob )
n = getByteCountFor(nob)
self.data = array.array('B',(0,)*n) # want unsigned char as array element = 'B'
def __str__ ( self ):
"""
represent bit data
arguments:
self
returns:
hexadecimal string for bits
"""
return 'EllyBits: ' + show(self.data)
def set ( self , k ):
"""
set kth bit by OR'ing
arguments:
self -
k - which bit
"""
n , m = divmod(k,N)
if n >= len(self.data): return
self.data[n] |= sel[m]
def unset ( self , k ):
"""
unset kth bit by AND'ing
arguments:
self -
k - which bit
"""
n , m = divmod(k,N)
if n >= len(self.data): return
self.data[n] &= usl[m]
def test ( self , k ):
"""
test kth bit
arguments:
self -
k - which bit
returns:
True if kth bit == 1, False otherwise
"""
n , m = divmod(k,N)
if n >= len(self.data): return False
return (self.data[n] & sel[m]) != 0
def match ( self , r ):
"""
compare bits with reference
arguments:
self -
r - bit string to compare with
returns:
True if ON bits include all reference ON bits, False otherwise
"""
m = len(self.data)
n = len(r.data)
if n > m: n = m
for i in range(n):
if _com(self.data[i]) & r.data[i] != 0: return False
return True
def equal ( self , r ):
"""
check that bits are the same as reference
arguments:
self -
r - bit string in byte array to compare with
returns:
True if all bits the same, False otherwise
"""
for i in range(len(r.data)):
if self.data[i] != r.data[i]: return False
return True
def intersect ( self , r ):
"""
intersect bits with reference
arguments:
self -
r - bit string in byte array to compare with
returns:
True if bits intersect, False otherwise
"""
m = len(self.data)
n = len(r.data)
if n > m: n = m
for i in range(n):
if self.data[i] & r.data[i] != 0: return True
return False
def zeroed ( self ):
"""
check if all bits zero
arguments:
self
returns:
True if all zero, False otherwise
"""
for i in range(len(self.data)):
if self.data[i] != 0: return False
return True
def clear ( self ):
"""
reset all bits to zero
arguments:
self
"""
for i in range(len(self.data)): self.data[i] = 0
def complement ( self ):
"""
set all bits to their complement
arguments:
self
"""
for i in range(len(self.data)): self.data[i] = _com(self.data[i])
def combine ( self , r ):
"""
form disjunction with other bit string
arguments:
self -
r - bit string in byte array to OR with
"""
m = len(self.data)
n = len(r.data)
if n > m: n = m
for i in range(n):
self.data[i] |= r.data[i] # disjunction
def reset ( self , r ):
"""
reset unselected bits
arguments:
self -
r - = 1 for each bit to keep
"""
m = len(self.data)
n = len(r.data)
if n > m: n = m
for i in range(n):
self.data[i] &= r.data[i] # conjunction
def compound ( self ):
"""
build compound representation for positive and negative bit testing
arguments:
self
returns:
combination of complemented bits as byte array
"""
cm = map(lambda x:_com(x),self.data) # complement current bits
return array.array('B',cm) + self.data # combine with uncomplemented
def hexadecimal ( self , divide=True ):
"""
convert bits to hexadecimal
arguments:
self -
divide - flag to split output into 2-char segments
returns:
hexadecimal string
"""
bs = [ ]
for b in self.data:
n , m = divmod(b,16)
bs.append(hxd[n])
bs.append(hxd[m])
if divide:
bs.append(' ')
return ''.join(bs).rstrip()
def reinit ( self , hexb ):
"""
reset bits to hexadecimal specification
arguments:
self -
hexb - hexadecimal bit specification as string
"""
ln = len(self.data)
if len(hexb)//2 != ln: return
for i in range(ln):
bs = hexb[:2]
hexb = hexb[2:]
self.data[i] = 16*hxh[bs[0]] + hxh[bs[1]]
def count ( self ):
"""
get byte length of data
arguments:
self
returns:
integer coutn
"""
return len(self.data)
#
# unit test
#
if __name__ == "__main__":
K = 12 # nominal bit count to support in testing (should be > 8)
bbs = EllyBits(K)
bbs.set(0)
bbs.set(6)
bbs.set(11)
print ( 'bbs=' , bbs )
print ( "bbs before:" , bbs.data , "hex=" , bbs.hexadecimal() )
bbt = EllyBits(K)
bbt.set(9)
print ( "bbt before:" , bbt.data , "hex=" , bbt.hexadecimal() )
bbs.combine(bbt)
print ( "bbs combined with bbs:" , bbs.data , "hex=" , bbs.hexadecimal() )
print ( "test bit 9 of bbs" , bbs.test(9) )
print ( "test bit 10 of bbs" , bbs.test(10) )
print ( "test bit 11 of bbs" , bbs.test(11) )
cbs = bbs.compound()
print ( "compound bbs=" , type(cbs) , show(cbs) )
pbs = EllyBits(K)
nbs = EllyBits(K)
pbs.set(6)
nbs.set(7)
print ( 'pbs=' , pbs.hexadecimal() )
print ( 'nbs=' , nbs.hexadecimal() )
tbs = join(pbs,nbs)
print ( "join pbs, nbs=" , type(tbs) , show(tbs) )
print ( 'check compound versus join=' , check(cbs,tbs) )
print ( "current bbs" , bbs.data , "hex=" , bbs.hexadecimal() )
bbs.complement()
print ( "complemented " , bbs.data , "hex=" , bbs.hexadecimal() )
bbs.clear()
print ( "cleared bbs" , bbs.data , "hex=" , bbs.hexadecimal() )
pbs.set(7)
print ( 'comparing x=' , pbs.hexadecimal() , 'and y=' , nbs.hexadecimal() )
print ( 'x equal y:' , pbs.equal(nbs) )
print ( 'x match y:' , pbs.match(nbs) )
print ( 'y match x:' , nbs.match(pbs) )
nbs.set(6)
print ( 'z=' , nbs.hexadecimal() )
print ( 'x match z:' , pbs.equal(nbs) )
bbs.reinit('FFEE')
print ( 'set bbs to FFEE' )
print ( "bbs:" , bbs.data , "hex=" , bbs.hexadecimal() )
bbs.reinit('FF12')
print ( 'set bbs to FF12' )
print ( '=' , bbs.hexadecimal(False) )
print ( 'resetting with EEEE' )
rbs = EllyBits(K)
rbs.reinit('EEEE')
bbs.reset(rbs)
print ( '=' , bbs.hexadecimal(False) )
bbs.reinit('0000')
print ( '=' , bbs , 'zeroed=' , bbs.zeroed() )
bbs.set(1)
print ( '=' , bbs , 'set 1 =' , bbs.zeroed() )
bbs.set(11)
bbs.unset(1)
print ( '=' , bbs , 'unset +' , str(bbs) )