-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbitarray_.py
More file actions
358 lines (276 loc) · 12.8 KB
/
bitarray_.py
File metadata and controls
358 lines (276 loc) · 12.8 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
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
from __future__ import annotations
import copy
import numbers
import re
from collections import abc
from typing import Union, List, Iterable, Any, Optional
from bitstring import utils
from bitstring.exceptions import CreationError, Error
from bitstring.bits import Bits, BitsType, TBits
import bitstring.dtypes
class BitArray(Bits):
"""A container holding a mutable sequence of bits.
Subclass of the immutable Bits class. Inherits all of its
methods (except __hash__) and adds mutating methods.
Mutating methods:
append() -- Append a bitstring.
byteswap() -- Change byte endianness in-place.
clear() -- Remove all bits from the bitstring.
insert() -- Insert a bitstring.
invert() -- Flip bit(s) between one and zero.
overwrite() -- Overwrite a section with a new bitstring.
prepend() -- Prepend a bitstring.
replace() -- Replace occurrences of one bitstring with another.
reverse() -- Reverse bits in-place.
rol() -- Rotate bits to the left.
ror() -- Rotate bits to the right.
set() -- Set bit(s) to 1 or 0.
Methods inherited from Bits:
all() -- Check if all specified bits are set to 1 or 0.
any() -- Check if any of specified bits are set to 1 or 0.
copy() -- Return a copy of the bitstring.
count() -- Count the number of bits set to 1 or 0.
cut() -- Create generator of constant sized chunks.
endswith() -- Return whether the bitstring ends with a sub-string.
find() -- Find a sub-bitstring in the current bitstring.
findall() -- Find all occurrences of a sub-bitstring in the current bitstring.
fromstring() -- Create a bitstring from a formatted string.
join() -- Join bitstrings together using current bitstring.
pp() -- Pretty print the bitstring.
rfind() -- Seek backwards to find a sub-bitstring.
split() -- Create generator of chunks split by a delimiter.
startswith() -- Return whether the bitstring starts with a sub-bitstring.
tobitarray() -- Return bitstring as a bitarray from the bitarray package.
tobytes() -- Return bitstring as bytes, padding if needed.
tofile() -- Write bitstring to file, padding if needed.
unpack() -- Interpret bits using format string.
Special methods:
Mutating operators are available: [], <<=, >>=, +=, *=, &=, |= and ^=
in addition to the inherited [], ==, !=, +, *, ~, <<, >>, &, | and ^.
Properties:
[GENERATED_PROPERTY_DESCRIPTIONS]
len -- Length of the bitstring in bits.
"""
__slots__ = ()
__hash__: None = None
def __init__(self, auto: Optional[Union[BitsType, int]]=None, /, length: Optional[int]=None, offset: Optional[int]=None, **kwargs) -> None:
"""Either specify an 'auto' initialiser:
A string of comma separated tokens, an integer, a file object,
a bytearray, a boolean iterable or another bitstring.
Or initialise via **kwargs with one (and only one) of:
bin -- binary string representation, e.g. '0b001010'.
hex -- hexadecimal string representation, e.g. '0x2ef'
oct -- octal string representation, e.g. '0o777'.
bytes -- raw data as a bytes object, for example read from a binary file.
int -- a signed integer.
uint -- an unsigned integer.
float / floatbe -- a big-endian floating point number.
bool -- a boolean (True or False).
se -- a signed exponential-Golomb code.
ue -- an unsigned exponential-Golomb code.
sie -- a signed interleaved exponential-Golomb code.
uie -- an unsigned interleaved exponential-Golomb code.
floatle -- a little-endian floating point number.
floatne -- a native-endian floating point number.
bfloat / bfloatbe - a big-endian bfloat format 16-bit floating point number.
bfloatle -- a little-endian bfloat format 16-bit floating point number.
bfloatne -- a native-endian bfloat format 16-bit floating point number.
intbe -- a signed big-endian whole byte integer.
intle -- a signed little-endian whole byte integer.
intne -- a signed native-endian whole byte integer.
uintbe -- an unsigned big-endian whole byte integer.
uintle -- an unsigned little-endian whole byte integer.
uintne -- an unsigned native-endian whole byte integer.
filename -- the path of a file which will be opened in binary read-only mode.
Other keyword arguments:
length -- length of the bitstring in bits, if needed and appropriate.
It must be supplied for all integer and float initialisers.
offset -- bit offset to the data. These offset bits are
ignored and this is intended for use when
initialising using 'bytes' or 'filename'.
"""
if self._bitstore.immutable:
self._bitstore = self._bitstore._copy()
self._bitstore.immutable = False
def copy(self: TBits) -> TBits:
"""Return a copy of the bitstring."""
pass
def __setattr__(self, attribute, value) -> None:
try:
super().__setattr__(attribute, value)
except AttributeError:
dtype = bitstring.dtypes.Dtype(attribute)
x = object.__new__(Bits)
if (set_fn := dtype.set_fn) is None:
raise AttributeError(f"Cannot set attribute '{attribute}' as it does not have a set_fn.")
set_fn(x, value)
if len(x) != dtype.bitlength:
raise CreationError(f"Can't initialise with value of length {len(x)} bits, as attribute has length of {dtype.bitlength} bits.")
self._bitstore = x._bitstore
return
def __iadd__(self, bs: BitsType) -> BitArray:
"""Append bs to current bitstring. Return self.
bs -- the bitstring to append.
"""
self._append(bs)
return self
def __copy__(self) -> BitArray:
"""Return a new copy of the BitArray."""
s_copy = BitArray()
s_copy._bitstore = self._bitstore._copy()
assert s_copy._bitstore.immutable is False
return s_copy
def __setitem__(self, key: Union[slice, int], value: BitsType) -> None:
if isinstance(key, numbers.Integral):
self._setitem_int(int(key), value)
else:
self._setitem_slice(key, value)
def __delitem__(self, key: Union[slice, int]) -> None:
"""Delete item or range.
>>> a = BitArray('0x001122')
>>> del a[8:16]
>>> print a
0x0022
"""
self._bitstore.__delitem__(key)
return
def __ilshift__(self: TBits, n: int) -> TBits:
"""Shift bits by n to the left in place. Return self.
n -- the number of bits to shift. Must be >= 0.
"""
if n < 0:
raise ValueError('Cannot shift by a negative amount.')
if not len(self):
raise ValueError('Cannot shift an empty bitstring.')
if not n:
return self
n = min(n, len(self))
return self._ilshift(n)
def __irshift__(self: TBits, n: int) -> TBits:
"""Shift bits by n to the right in place. Return self.
n -- the number of bits to shift. Must be >= 0.
"""
if n < 0:
raise ValueError('Cannot shift by a negative amount.')
if not len(self):
raise ValueError('Cannot shift an empty bitstring.')
if not n:
return self
n = min(n, len(self))
return self._irshift(n)
def __imul__(self: TBits, n: int) -> TBits:
"""Concatenate n copies of self in place. Return self.
Called for expressions of the form 'a *= 3'.
n -- The number of concatenations. Must be >= 0.
"""
if n < 0:
raise ValueError('Cannot multiply by a negative integer.')
return self._imul(n)
def __ior__(self: TBits, bs: BitsType) -> TBits:
bs = self._create_from_bitstype(bs)
self._bitstore |= bs._bitstore
return self
def __iand__(self: TBits, bs: BitsType) -> TBits:
bs = self._create_from_bitstype(bs)
self._bitstore &= bs._bitstore
return self
def __ixor__(self: TBits, bs: BitsType) -> TBits:
bs = self._create_from_bitstype(bs)
self._bitstore ^= bs._bitstore
return self
def replace(self, old: BitsType, new: BitsType, start: Optional[int]=None, end: Optional[int]=None, count: Optional[int]=None, bytealigned: Optional[bool]=None) -> int:
"""Replace all occurrences of old with new in place.
Returns number of replacements made.
old -- The bitstring to replace.
new -- The replacement bitstring.
start -- Any occurrences that start before this will not be replaced.
Defaults to 0.
end -- Any occurrences that finish after this will not be replaced.
Defaults to len(self).
count -- The maximum number of replacements to make. Defaults to
replace all occurrences.
bytealigned -- If True replacements will only be made on byte
boundaries.
Raises ValueError if old is empty or if start or end are
out of range.
"""
pass
def insert(self, bs: BitsType, pos: int) -> None:
"""Insert bs at bit position pos.
bs -- The bitstring to insert.
pos -- The bit position to insert at.
Raises ValueError if pos < 0 or pos > len(self).
"""
pass
def overwrite(self, bs: BitsType, pos: int) -> None:
"""Overwrite with bs at bit position pos.
bs -- The bitstring to overwrite with.
pos -- The bit position to begin overwriting from.
Raises ValueError if pos < 0 or pos > len(self).
"""
pass
def append(self, bs: BitsType) -> None:
"""Append a bitstring to the current bitstring.
bs -- The bitstring to append.
"""
pass
def prepend(self, bs: BitsType) -> None:
"""Prepend a bitstring to the current bitstring.
bs -- The bitstring to prepend.
"""
pass
def reverse(self, start: Optional[int]=None, end: Optional[int]=None) -> None:
"""Reverse bits in-place.
start -- Position of first bit to reverse. Defaults to 0.
end -- One past the position of the last bit to reverse.
Defaults to len(self).
Using on an empty bitstring will have no effect.
Raises ValueError if start < 0, end > len(self) or end < start.
"""
pass
def set(self, value: Any, pos: Optional[Union[int, Iterable[int]]]=None) -> None:
"""Set one or many bits to 1 or 0.
value -- If bool(value) is True bits are set to 1, otherwise they are set to 0.
pos -- Either a single bit position or an iterable of bit positions.
Negative numbers are treated in the same way as slice indices.
Defaults to the entire bitstring.
Raises IndexError if pos < -len(self) or pos >= len(self).
"""
pass
def invert(self, pos: Optional[Union[Iterable[int], int]]=None) -> None:
"""Invert one or many bits from 0 to 1 or vice versa.
pos -- Either a single bit position or an iterable of bit positions.
Negative numbers are treated in the same way as slice indices.
Raises IndexError if pos < -len(self) or pos >= len(self).
"""
pass
def ror(self, bits: int, start: Optional[int]=None, end: Optional[int]=None) -> None:
"""Rotate bits to the right in-place.
bits -- The number of bits to rotate by.
start -- Start of slice to rotate. Defaults to 0.
end -- End of slice to rotate. Defaults to len(self).
Raises ValueError if bits < 0.
"""
pass
def rol(self, bits: int, start: Optional[int]=None, end: Optional[int]=None) -> None:
"""Rotate bits to the left in-place.
bits -- The number of bits to rotate by.
start -- Start of slice to rotate. Defaults to 0.
end -- End of slice to rotate. Defaults to len(self).
Raises ValueError if bits < 0.
"""
pass
def byteswap(self, fmt: Optional[Union[int, Iterable[int], str]]=None, start: Optional[int]=None, end: Optional[int]=None, repeat: bool=True) -> int:
"""Change the endianness in-place. Return number of repeats of fmt done.
fmt -- A compact structure string, an integer number of bytes or
an iterable of integers. Defaults to 0, which byte reverses the
whole bitstring.
start -- Start bit position, defaults to 0.
end -- End bit position, defaults to len(self).
repeat -- If True (the default) the byte swapping pattern is repeated
as much as possible.
"""
pass
def clear(self) -> None:
"""Remove all bits, reset to zero length."""
pass