4This package defines classes that simplify bit-wise creation, manipulation and
9Bits -- An immutable container for binary data.
10BitArray -- A mutable container for binary data.
11ConstBitStream -- An immutable container with streaming methods.
12BitStream -- A mutable container with streaming methods.
16 + mutating methods / \ + streaming methods
18 BitArray ConstBitStream
26pack -- Create a BitStream
from a format string.
30Error -- Module exception base
class.
31CreationError -- Error during creation.
32InterpretError -- Inappropriate interpretation of binary data.
33ByteAlignError -- Whole byte position
or length needed.
34ReadError -- Reading
or peeking past the end of a bitstring.
36https://github.com/scott-griffiths/bitstring
42Copyright (c) 2006-2016 Scott Griffiths (dr.scottgriffiths
@gmail.com)
44Permission
is hereby granted, free of charge, to any person obtaining a copy
45of this software
and associated documentation files (the
"Software"), to deal
46in the Software without restriction, including without limitation the rights
47to use, copy, modify, merge, publish, distribute, sublicense,
and/
or sell
48copies of the Software,
and to permit persons to whom the Software
is
49furnished to do so, subject to the following conditions:
51The above copyright notice
and this permission notice shall be included
in
52all copies
or substantial portions of the Software.
54THE SOFTWARE IS PROVIDED
"AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
55IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
56FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
57AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
58LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
59OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
65__author__ = "Scott Griffiths"
79byteorder = sys.byteorder
82"""Determines whether a number of methods default to working only on byte boundaries.
"""
84# Maximum number of digits to use in __str__ and __repr__.
87# Maximum size of caches used for speed optimisations.
91class Error(Exception):
92 """Base class for errors in the bitstring module."""
94 def __init__(self, *params):
95 self.
msg = params[0]
if params
else ''
105 """Reading or peeking past the end of a bitstring."""
107 def __init__(self, *params):
108 Error.__init__(self, *params)
112 """Inappropriate interpretation of binary data."""
114 def __init__(self, *params):
115 Error.__init__(self, *params)
119 """Whole-byte position or length needed."""
121 def __init__(self, *params):
122 Error.__init__(self, *params)
126 """Inappropriate argument during bitstring creation."""
128 def __init__(self, *params):
129 Error.__init__(self, *params)
133 """Stores raw bytes together with a bit offset and length.
135 Used internally - not part of public interface.
138 __slots__ = ('offset',
'_rawarray',
'bitlength')
140 def __init__(self, data, bitlength=None, offset=None):
141 """data is either a bytearray or a MmapByteArray"""
145 if bitlength
is None:
146 bitlength = 8 * len(data) - offset
150 def getbit(self, pos):
152 byte, bit = divmod(self.
offset + pos, 8)
153 return bool(self.
_rawarray[byte] & (128 >> bit))
156 """Direct access to byte data."""
160 """Direct access to byte data."""
165 def bytelength(self):
176 """Join another store on to the end of this one."""
177 if not store.bitlength:
183 joinval = (self.
_rawarray.pop() & (255 ^ (255 >> store.offset)) |
184 (store.getbyte(0) & (255 >> store.offset)))
186 self.
_rawarray.extend(store._rawarray[1:])
192 """Join another store on to the start of this one."""
193 if not store.bitlength:
199 assert (store.offset + store.bitlength) % 8 == self.
offset % 8
200 bit_offset = self.
offset % 8
203 store.setbyte(-1, (store.getbyte(-1) & (255 ^ (255 >> bit_offset)) |
209 self.
offset = store.offset
213 def byteoffset(self):
222 """Adding mutating methods to ConstByteStore
224 Used internally - not part of public interface.
228 def setbit(self, pos):
230 byte, bit = divmod(self.
offset + pos, 8)
233 def unsetbit(self, pos):
235 byte, bit = divmod(self.
offset + pos, 8)
238 def invertbit(self, pos):
240 byte, bit = divmod(self.
offset + pos, 8)
243 def setbyte(self, pos, value):
246 def setbyteslice(self, start, end, value):
251 """Return a copy of a ByteStore with the newoffset.
253 Not part of public interface.
255 assert 0 <= newoffset < 8
259 if newoffset == s.offset % 8:
260 return ByteStore(s.getbyteslice(s.byteoffset, s.byteoffset + s.bytelength), s.bitlength, newoffset)
263 assert newoffset != s.offset % 8
264 if newoffset < s.offset % 8:
266 shiftleft = s.offset % 8 - newoffset
268 for x
in range(s.byteoffset, s.byteoffset + s.bytelength - 1):
269 newdata.append(((d[x] << shiftleft) & 0xff) +
270 (d[x + 1] >> (8 - shiftleft)))
271 bits_in_last_byte = (s.offset + s.bitlength) % 8
272 if not bits_in_last_byte:
273 bits_in_last_byte = 8
274 if bits_in_last_byte > shiftleft:
275 newdata.append((d[s.byteoffset + s.bytelength - 1] << shiftleft) & 0xff)
277 shiftright = newoffset - s.offset % 8
278 newdata.append(s.getbyte(0) >> shiftright)
279 for x
in range(s.byteoffset + 1, s.byteoffset + s.bytelength):
280 newdata.append(((d[x - 1] << (8 - shiftright)) & 0xff) +
281 (d[x] >> shiftright))
282 bits_in_last_byte = (s.offset + s.bitlength) % 8
283 if not bits_in_last_byte:
284 bits_in_last_byte = 8
285 if bits_in_last_byte + shiftright > 8:
286 newdata.append((d[s.byteoffset + s.bytelength - 1] << (8 - shiftright)) & 0xff)
287 new_s =
ByteStore(bytearray(newdata), s.bitlength, newoffset)
288 assert new_s.offset == newoffset
293 """Return True if ByteStores a == b.
295 Not part of public interface.
300 a_bitlength = a.bitlength
301 b_bitlength = b.bitlength
302 if a_bitlength != b_bitlength:
305 assert b_bitlength == 0
308 if (a.offset % 8) > (b.offset % 8):
311 a_bitoff = a.offset % 8
312 b_bitoff = b.offset % 8
313 a_byteoffset = a.byteoffset
314 b_byteoffset = b.byteoffset
315 a_bytelength = a.bytelength
316 b_bytelength = b.bytelength
321 if da
is db
and a.offset == b.offset:
324 if a_bitoff == b_bitoff:
325 bits_spare_in_last_byte = 8 - (a_bitoff + a_bitlength) % 8
326 if bits_spare_in_last_byte == 8:
327 bits_spare_in_last_byte = 0
329 if a_bytelength == 1:
330 a_val = ((da[a_byteoffset] << a_bitoff) & 0xff) >> (8 - a_bitlength)
331 b_val = ((db[b_byteoffset] << b_bitoff) & 0xff) >> (8 - b_bitlength)
332 return a_val == b_val
334 if da[a_byteoffset] & (0xff >> a_bitoff) != db[b_byteoffset] & (0xff >> b_bitoff):
337 b_a_offset = b_byteoffset - a_byteoffset
338 for x
in range(1 + a_byteoffset, a_byteoffset + a_bytelength - 1):
339 if da[x] != db[b_a_offset + x]:
342 return (da[a_byteoffset + a_bytelength - 1] >> bits_spare_in_last_byte ==
343 db[b_byteoffset + b_bytelength - 1] >> bits_spare_in_last_byte)
345 assert a_bitoff != b_bitoff
347 shift = b_bitoff - a_bitoff
349 if b_bytelength == 1:
350 assert a_bytelength == 1
351 a_val = ((da[a_byteoffset] << a_bitoff) & 0xff) >> (8 - a_bitlength)
352 b_val = ((db[b_byteoffset] << b_bitoff) & 0xff) >> (8 - b_bitlength)
353 return a_val == b_val
355 if a_bytelength == 1:
356 assert b_bytelength == 2
357 a_val = ((da[a_byteoffset] << a_bitoff) & 0xff) >> (8 - a_bitlength)
358 b_val = ((db[b_byteoffset] << 8) + db[b_byteoffset + 1]) << b_bitoff
360 b_val >>= 16 - b_bitlength
361 return a_val == b_val
364 if (da[a_byteoffset] & (0xff >> a_bitoff)) >> shift != db[b_byteoffset] & (0xff >> b_bitoff):
367 for x
in range(1, b_bytelength - 1):
369 b_val = db[b_byteoffset + x]
370 a_val = ((da[a_byteoffset + x - 1] << 8) + da[a_byteoffset + x]) >> shift
376 final_b_bits = (b.offset + b_bitlength) % 8
379 b_val = db[b_byteoffset + b_bytelength - 1] >> (8 - final_b_bits)
380 final_a_bits = (a.offset + a_bitlength) % 8
383 if b.bytelength > a_bytelength:
384 assert b_bytelength == a_bytelength + 1
385 a_val = da[a_byteoffset + a_bytelength - 1] >> (8 - final_a_bits)
386 a_val &= 0xff >> (8 - final_b_bits)
387 return a_val == b_val
388 assert a_bytelength == b_bytelength
389 a_val = da[a_byteoffset + a_bytelength - 2] << 8
390 a_val += da[a_byteoffset + a_bytelength - 1]
391 a_val >>= (8 - final_a_bits)
392 a_val &= 0xff >> (8 - final_b_bits)
393 return a_val == b_val
397 """Looks like a bytearray, but from an mmap.
399 Not part of public interface.
402 __slots__ = ('filemap',
'filelength',
'source',
'byteoffset',
'bytelength')
404 def __init__(self, source, bytelength=None, byteoffset=None):
406 source.seek(0, os.SEEK_END)
408 if byteoffset
is None:
410 if bytelength
is None:
414 self.
filemap = mmap.mmap(source.fileno(), 0, access=mmap.ACCESS_READ)
416 def __getitem__(self, key):
420 except AttributeError:
432 assert key.step
is None
436 return bytearray(self.
filemap.__getitem__(s))
444BYTE_REVERSAL_DICT = dict()
451 BYTE_REVERSAL_DICT[i] = chr(int(
"{0:08b}".format(i)[::-1], 2))
454 BYTE_REVERSAL_DICT[i] = bytes([int(
"{0:08b}".format(i)[::-1], 2)])
455 from io
import IOBase
as file
460LEADING_OCT_CHARS = len(oct(1)) - 1
464 """Return string made lowercase and with all whitespace removed."""
465 s =
''.join(s.split()).lower()
469INIT_NAMES = (
'uint',
'int',
'ue',
'se',
'sie',
'uie',
'hex',
'oct',
'bin',
'bits',
470 'uintbe',
'intbe',
'uintle',
'intle',
'uintne',
'intne',
471 'float',
'floatbe',
'floatle',
'floatne',
'bytes',
'bool',
'pad')
473TOKEN_RE = re.compile(
r'(?P<name>' +
'|'.join(INIT_NAMES) +
474 r')((:(?P<len>[^=]+)))?(=(?P<value>.*))?$', re.IGNORECASE)
475DEFAULT_UINT = re.compile(
r'(?P<len>[^=]+)?(=(?P<value>.*))?$', re.IGNORECASE)
477MULTIPLICATIVE_RE = re.compile(
r'(?P<factor>.*)\*(?P<token>.+)')
480LITERAL_RE = re.compile(
r'(?P<name>0(x|o|b))(?P<value>.+)', re.IGNORECASE)
483STRUCT_PACK_RE = re.compile(
r'(?P<endian><|>|@)?(?P<fmt>(?:\d*[bBhHlLqQfd])+)$')
486STRUCT_SPLIT_RE = re.compile(
r'\d*[bBhHlLqQfd]')
490REPLACEMENTS_BE = {
'b':
'intbe:8',
'B':
'uintbe:8',
491 'h':
'intbe:16',
'H':
'uintbe:16',
492 'l':
'intbe:32',
'L':
'uintbe:32',
493 'q':
'intbe:64',
'Q':
'uintbe:64',
494 'f':
'floatbe:32',
'd':
'floatbe:64'}
496REPLACEMENTS_LE = {
'b':
'intle:8',
'B':
'uintle:8',
497 'h':
'intle:16',
'H':
'uintle:16',
498 'l':
'intle:32',
'L':
'uintle:32',
499 'q':
'intle:64',
'Q':
'uintle:64',
500 'f':
'floatle:32',
'd':
'floatle:64'}
503PACK_CODE_SIZE = {
'b': 1,
'B': 1,
'h': 2,
'H': 2,
'l': 4,
'L': 4,
504 'q': 8,
'Q': 8,
'f': 4,
'd': 8}
506_tokenname_to_initialiser = {
'hex':
'hex',
'0x':
'hex',
'0X':
'hex',
'oct':
'oct',
507 '0o':
'oct',
'0O':
'oct',
'bin':
'bin',
'0b':
'bin',
508 '0B':
'bin',
'bits':
'auto',
'bytes':
'bytes',
'pad':
'pad'}
512 """Parse struct-like format string token into sub-token list."""
513 m = STRUCT_PACK_RE.match(token)
517 endian = m.group(
'endian')
521 formatlist = re.findall(STRUCT_SPLIT_RE, m.group(
'fmt'))
523 fmt =
''.join([f[-1] * int(f[:-1])
if len(f) != 1
else
524 f
for f
in formatlist])
527 if byteorder ==
'little':
530 assert byteorder ==
'big'
533 tokens = [REPLACEMENTS_LE[c]
for c
in fmt]
536 tokens = [REPLACEMENTS_BE[c]
for c
in fmt]
541 """Divide the format string into tokens and parse them.
543 Return stretchy token and list of [initialiser, length, value]
544 initialiser
is one of: hex, oct, bin, uint, int, se, ue, 0x, 0o, 0b etc.
545 length
is None if not known,
as is value.
547 If the token
is in the keyword dictionary (keys) then it counts
as a
548 special case
and isn
't messed with.
550 tokens must be of the form: [factor*][initialiser][:][length][=value]
553 if token_cache
is None:
556 return token_cache[(fmt, keys)]
558 token_key = (fmt, keys)
564 meta_tokens = (
''.join(f.split())
for f
in fmt.split(
','))
566 stretchy_token =
False
567 for meta_token
in meta_tokens:
569 m = MULTIPLICATIVE_RE.match(meta_token)
573 factor = int(m.group(
'factor'))
574 meta_token = m.group(
'token')
579 if keys
and token
in keys:
581 ret_vals.append([token,
None,
None])
583 value = length =
None
587 m = LITERAL_RE.match(token)
589 name = m.group(
'name')
590 value = m.group(
'value')
591 ret_vals.append([name, length, value])
594 m1 = TOKEN_RE.match(token)
597 m2 = DEFAULT_UINT.match(token)
599 raise ValueError(
"Don't understand token '{0}'.".format(token))
601 name = m1.group(
'name')
602 length = m1.group(
'len')
603 if m1.group(
'value'):
604 value = m1.group(
'value')
608 length = m2.group(
'len')
609 if m2.group(
'value'):
610 value = m2.group(
'value')
612 if length
is not None:
613 raise ValueError(
"You can't specify a length with bool tokens - they are always one bit.")
615 if length
is None and name
not in (
'se',
'ue',
'sie',
'uie'):
616 stretchy_token =
True
617 if length
is not None:
627 raise ValueError(
"Can't read a token with a negative length.")
629 if not keys
or length
not in keys:
630 raise ValueError(
"Don't understand length '{0}' of token.".format(length))
631 ret_vals.append([name, length, value])
637 return_values.extend(ret_vals * factor)
638 return_values = [tuple(x)
for x
in return_values]
639 if len(token_cache) < CACHE_SIZE:
640 token_cache[token_key] = stretchy_token, return_values
641 return stretchy_token, return_values
645BRACKET_RE = re.compile(
r'(?P<factor>\d+)\*\(')
649 """Remove whitespace and expand all brackets."""
650 s =
''.join(s.split())
666 raise ValueError(
"Unbalanced parenthesis in '{0}'.".format(s))
667 if start == 0
or s[start - 1] !=
'*':
668 s = s[0:start] + s[start + 1:p] + s[p + 1:]
670 m = BRACKET_RE.search(s)
672 factor = int(m.group(
'factor'))
673 matchstart = m.start(
'factor')
674 s = s[0:matchstart] + (factor - 1) * (s[start + 1:p] +
',') + s[start + 1:p] + s[p + 1:]
676 raise ValueError(
"Failed to parse '{0}'.".format(s))
681OCT_TO_BITS = [
'{0:03b}'.format(i)
for i
in xrange(8)]
684BIT_COUNT = dict(zip(xrange(256), [bin(i).count(
'1')
for i
in xrange(256)]))
688 """A container holding an immutable sequence of bits.
690 For a mutable container use the BitArray class instead.
694 all() -- Check
if all specified bits are set to 1
or 0.
695 any() -- Check
if any of specified bits are set to 1
or 0.
696 count() -- Count the number of bits set to 1
or 0.
697 cut() -- Create generator of constant sized chunks.
698 endswith() -- Return whether the bitstring ends
with a sub-string.
699 find() -- Find a sub-bitstring
in the current bitstring.
700 findall() -- Find all occurrences of a sub-bitstring
in the current bitstring.
701 join() -- Join bitstrings together using current bitstring.
702 rfind() -- Seek backwards to find a sub-bitstring.
703 split() -- Create generator of chunks split by a delimiter.
704 startswith() -- Return whether the bitstring starts
with a sub-bitstring.
705 tobytes() -- Return bitstring
as bytes, padding
if needed.
706 tofile() -- Write bitstring to file, padding
if needed.
707 unpack() -- Interpret bits using format string.
711 Also available are the operators [], ==, !=, +, *, ~, <<, >>, &, |, ^.
715 bin -- The bitstring
as a binary string.
716 bool -- For single bit bitstrings, interpret
as True or False.
717 bytes -- The bitstring
as a bytes object.
718 float -- Interpret
as a floating point number.
719 floatbe -- Interpret
as a big-endian floating point number.
720 floatle -- Interpret
as a little-endian floating point number.
721 floatne -- Interpret
as a native-endian floating point number.
722 hex -- The bitstring
as a hexadecimal string.
723 int -- Interpret
as a two
's complement signed integer.
724 intbe -- Interpret as a big-endian signed integer.
725 intle -- Interpret
as a little-endian signed integer.
726 intne -- Interpret
as a native-endian signed integer.
727 len -- Length of the bitstring
in bits.
728 oct -- The bitstring
as an octal string.
729 se -- Interpret
as a signed exponential-Golomb code.
730 ue -- Interpret
as an unsigned exponential-Golomb code.
731 sie -- Interpret
as a signed interleaved exponential-Golomb code.
732 uie -- Interpret
as an unsigned interleaved exponential-Golomb code.
733 uint -- Interpret
as a two
's complement unsigned integer.
734 uintbe -- Interpret as a big-endian unsigned integer.
735 uintle -- Interpret
as a little-endian unsigned integer.
736 uintne -- Interpret
as a native-endian unsigned integer.
740 __slots__ = ('_datastore')
742 def __init__(self, auto=None, length=None, offset=None, **kwargs):
743 """Either specify an 'auto' initialiser:
744 auto -- a string of comma separated tokens, an integer, a file object,
745 a bytearray, a boolean iterable, an array or another bitstring.
747 Or initialise via **kwargs
with one (
and only one) of:
748 bytes -- raw data
as a string,
for example read
from a binary file.
749 bin -- binary string representation, e.g.
'0b001010'.
750 hex -- hexadecimal string representation, e.g.
'0x2ef'
751 oct -- octal string representation, e.g.
'0o777'.
752 uint -- an unsigned integer.
753 int -- a signed integer.
754 float -- a floating point number.
755 uintbe -- an unsigned big-endian whole byte integer.
756 intbe -- a signed big-endian whole byte integer.
757 floatbe - a big-endian floating point number.
758 uintle -- an unsigned little-endian whole byte integer.
759 intle -- a signed little-endian whole byte integer.
760 floatle -- a little-endian floating point number.
761 uintne -- an unsigned native-endian whole byte integer.
762 intne -- a signed native-endian whole byte integer.
763 floatne -- a native-endian floating point number.
764 se -- a signed exponential-Golomb code.
765 ue -- an unsigned exponential-Golomb code.
766 sie -- a signed interleaved exponential-Golomb code.
767 uie -- an unsigned interleaved exponential-Golomb code.
768 bool -- a boolean (
True or False).
769 filename -- a file which will be opened
in binary read-only mode.
771 Other keyword arguments:
772 length -- length of the bitstring
in bits,
if needed
and appropriate.
773 It must be supplied
for all integer
and float initialisers.
774 offset -- bit offset to the data. These offset bits are
775 ignored
and this
is mainly intended
for use when
776 initialising using
'bytes' or 'filename'.
780 def __new__(cls, auto=None, length=None, offset=None, _cache=None, **kwargs):
786 if isinstance(auto, basestring):
790 x = object.__new__(Bits)
793 except ValueError
as e:
797 x._datastore._appendstore(Bits._init_with_token(*token)._datastore)
798 assert x._assertsanity()
799 if len(_cache) < CACHE_SIZE:
802 if isinstance(auto, Bits):
806 x = super(Bits, cls).__new__(cls)
807 x._initialise(auto, length, offset, **kwargs)
810 def _initialise(self, auto, length, offset, **kwargs):
811 if length
is not None and length < 0:
813 if offset
is not None and offset < 0:
820 if length
is not None and length != 0:
821 data = bytearray((length + 7) // 8)
826 k, v = kwargs.popitem()
828 init_without_length_or_offset[k](self, v)
829 if length
is not None or offset
is not None:
830 raise CreationError(
"Cannot use length or offset with this initialiser.")
833 init_with_length_only[k](self, v, length)
834 if offset
is not None:
835 raise CreationError(
"Cannot use offset with this initialiser.")
840 init_with_length_and_offset[k](self, v, length, offset)
842 raise CreationError(
"Unrecognised keyword '{0}' used to initialise.", k)
844 def _initialise_from_auto(self, auto, length, offset):
851 """Return a new copy of the Bits for the copy module."""
856 def __lt__(self, other):
857 raise TypeError(
"unorderable type: {0}".format(type(self).__name__))
859 def __gt__(self, other):
860 raise TypeError(
"unorderable type: {0}".format(type(self).__name__))
862 def __le__(self, other):
863 raise TypeError(
"unorderable type: {0}".format(type(self).__name__))
865 def __ge__(self, other):
866 raise TypeError(
"unorderable type: {0}".format(type(self).__name__))
869 """Concatenate bitstrings and return new bitstring.
871 bs -- the bitstring to append.
875 if bs.len <= self.
len:
880 s = self.__class__(s)
885 """Append current bitstring to bs and return new bitstring.
887 bs -- the string for the
'auto' initialiser that will be appended to.
891 return bs.__add__(self)
894 """Return a new bitstring representing a slice of the current bitstring.
896 Indices are in units of the step parameter (default 1 bit).
897 Stepping
is used to specify the number of bits
in each item.
901 >>>
print BitArray(
'0x00112233')[1:3:8]
907 step = key.step
if key.step
is not None else 1
908 except AttributeError:
912 if not 0 <= key < length:
913 raise IndexError(
"Slice index out of range.")
919 bs = self.__class__()
922 start, stop = 0, length
923 if key.start
is not None:
927 if key.stop
is not None:
931 start = max(start, 0)
932 stop = min(stop, length)
934 return self.
_slice(start, stop)
936 return self.__class__()
939 """Return the length of the bitstring in bits."""
943 """Return approximate string representation of bitstring for printing.
945 Short strings will be given wholly in hexadecimal
or binary. Longer
946 strings may be part hexadecimal
and part binary. Very long strings will
947 be truncated
with '...'.
953 if length > MAX_CHARS * 4:
955 return ''.join((
'0x', self.
_readhex(MAX_CHARS * 4, 0),
'...'))
957 if length < 32
and length % 4 != 0:
958 return '0b' + self.
bin
961 return '0x' + self.
hex
964 bits_at_end = length % 4
965 return ''.join((
'0x', self.
_readhex(length - bits_at_end, 0),
967 self.
_readbin(bits_at_end, length - bits_at_end)))
970 """Return representation that could be used to recreate the bitstring.
972 If the returned string is too long it will be truncated. See
__str__().
976 if isinstance(self.
_datastore._rawarray, MmapByteArray):
979 offsetstring =
", offset=%d" % (self.
_datastore._rawarray.byteoffset * 8 + self.
_offset)
980 lengthstring =
", length=%d" % length
981 return "{0}(filename='{1}'{2}{3})".format(self.__class__.__name__,
982 self.
_datastore._rawarray.source.name, lengthstring, offsetstring)
986 if s.endswith(
'...'):
987 lengthstring =
" # length={0}".format(length)
988 return "{0}('{1}'){2}".format(self.__class__.__name__, s, lengthstring)
991 """Return True if two bitstrings have the same binary representation.
1004 """Return False if two bitstrings have the same binary representation.
1010 return not self.
__eq__(bs)
1013 """Return bitstring with every bit inverted.
1015 Raises Error if the bitstring
is empty.
1019 raise Error(
"Cannot invert empty bitstring.")
1025 """Return bitstring with bits shifted by n to the left.
1027 n -- the number of bits to shift. Must be >= 0.
1031 raise ValueError(
"Cannot shift by a negative amount.")
1033 raise ValueError(
"Cannot shift an empty bitstring.")
1034 n = min(n, self.
len)
1040 """Return bitstring with bits shifted by n to the right.
1042 n -- the number of bits to shift. Must be >= 0.
1046 raise ValueError(
"Cannot shift by a negative amount.")
1048 raise ValueError(
"Cannot shift an empty bitstring.")
1051 s = self.__class__(length=min(n, self.
len))
1052 s._append(self[:-n])
1056 """Return bitstring consisting of n concatenations of self.
1058 Called for expression of the form
'a = b*3'.
1059 n -- The number of concatenations. Must be >= 0.
1063 raise ValueError(
"Cannot multiply by a negative integer.")
1065 return self.__class__()
1071 """Return bitstring consisting of n concatenations of self.
1073 Called for expressions of the form
'a = 3*b'.
1074 n -- The number of concatenations. Must be >= 0.
1080 """Bit-wise 'and' between two bitstrings. Returns new bitstring.
1082 bs -- The bitstring to '&' with.
1084 Raises ValueError
if the two bitstrings have differing lengths.
1088 if self.
len != bs.len:
1089 raise ValueError(
"Bitstrings must have the same length "
1096 """Bit-wise 'and' between two bitstrings. Returns new bitstring.
1098 bs -- the bitstring to '&' with.
1100 Raises ValueError
if the two bitstrings have differing lengths.
1106 """Bit-wise 'or' between two bitstrings. Returns new bitstring.
1108 bs -- The bitstring to '|' with.
1110 Raises ValueError
if the two bitstrings have differing lengths.
1114 if self.
len != bs.len:
1115 raise ValueError(
"Bitstrings must have the same length "
1122 """Bit-wise 'or' between two bitstrings. Returns new bitstring.
1124 bs -- The bitstring to '|' with.
1126 Raises ValueError
if the two bitstrings have differing lengths.
1132 """Bit-wise 'xor' between two bitstrings. Returns new bitstring.
1134 bs -- The bitstring to '^' with.
1136 Raises ValueError
if the two bitstrings have differing lengths.
1140 if self.
len != bs.len:
1141 raise ValueError(
"Bitstrings must have the same length "
1148 """Bit-wise 'xor' between two bitstrings. Returns new bitstring.
1150 bs -- The bitstring to '^' with.
1152 Raises ValueError
if the two bitstrings have differing lengths.
1158 """Return whether bs is contained in the current bitstring.
1160 bs -- The bitstring to search for.
1166 except AttributeError:
1168 found = Bits.find(self, bs, bytealigned=
False)
1171 except AttributeError:
1176 """Return an integer hash of the object."""
1184 shorter = self[:80] + self[-80:]
1186 for byte
in shorter.tobytes():
1188 h = (h << 4) + ord(byte)
1196 return h % 1442968193
1200 """Return True if any bits are set to 1, otherwise return False."""
1201 return self.
any(
True)
1204 __bool__ = __nonzero__
1207 """Check internal self consistency as a debugging aid."""
1208 assert self.
len >= 0
1214 def _init_with_token(cls, name, token_length, value):
1215 if token_length
is not None:
1216 token_length = int(token_length)
1217 if token_length == 0:
1221 return cls(token_length)
1224 if token_length
is None:
1225 error =
"Token has no value ({0}=???).".format(name)
1227 error =
"Token has no value ({0}:{1}=???).".format(name, token_length)
1228 raise ValueError(error)
1230 b = cls(**{_tokenname_to_initialiser[name]: value})
1232 if name
in (
'se',
'ue',
'sie',
'uie'):
1233 b = cls(**{name: int(value)})
1234 elif name
in (
'uint',
'int',
'uintbe',
'intbe',
'uintle',
'intle',
'uintne',
'intne'):
1235 b = cls(**{name: int(value),
'length': token_length})
1236 elif name
in (
'float',
'floatbe',
'floatle',
'floatne'):
1237 b = cls(**{name: float(value),
'length': token_length})
1238 elif name ==
'bool':
1239 if value
in (1,
'True',
'1'):
1241 elif value
in (0,
'False',
'0'):
1244 raise CreationError(
"bool token can only be 'True' or 'False'.")
1247 if token_length
is not None and b.len != token_length:
1248 msg =
"Token with length {0} packed with value of length {1} ({2}:{3}={4})."
1249 raise CreationError(msg, token_length, b.len, name, token_length, value)
1253 """Reset the bitstring to an empty state."""
1257 """Set bitstring from a bitstring, file, bool, integer, array, iterable or string."""
1262 if isinstance(s, Bits):
1264 length = s.len - offset
1267 if isinstance(s, file):
1271 length = os.path.getsize(s.name) * 8 - offset
1272 byteoffset, offset = divmod(offset, 8)
1273 bytelength = (length + byteoffset * 8 + offset + 7) // 8 - byteoffset
1275 if length + byteoffset * 8 + offset > m.filelength * 8:
1276 raise CreationError(
"File is not long enough for specified "
1277 "length and offset.")
1280 if length
is not None:
1281 raise CreationError(
"The length keyword isn't applicable to this initialiser.")
1283 raise CreationError(
"The offset keyword isn't applicable to this initialiser.")
1284 if isinstance(s, basestring):
1286 assert bs._offset == 0
1289 if isinstance(s, (bytes, bytearray)):
1292 if isinstance(s, array.array):
1296 if isinstance(s, numbers.Integral):
1299 msg =
"Can't create bitstring of negative length {0}."
1301 data = bytearray((s + 7) // 8)
1304 if isinstance(s, collections.abc.Iterable):
1308 raise TypeError(
"Cannot initialise bitstring from {0}.".format(type(s)))
1311 """Use file as source of bits."""
1312 source = open(filename,
'rb')
1316 length = os.path.getsize(source.name) * 8 - offset
1317 byteoffset, offset = divmod(offset, 8)
1318 bytelength = (length + byteoffset * 8 + offset + 7) // 8 - byteoffset
1320 if length + byteoffset * 8 + offset > m.filelength * 8:
1321 raise CreationError(
"File is not long enough for specified "
1322 "length and offset.")
1326 """Set the data from a string."""
1327 data = bytearray(data)
1330 length = len(data) * 8 - offset
1333 if length + offset > len(data) * 8:
1334 msg =
"Not enough data present. Need {0} bits, have {1}."
1342 """Unchecked version of _setbytes_safe."""
1347 """Read bytes and return them. Note that length is in bits."""
1348 assert length % 8 == 0
1349 assert start + length <= self.
len
1350 if not (start + self.
_offset) % 8:
1352 (start + self.
_offset + length) // 8))
1356 """Return the data as an ordinary string."""
1358 raise InterpretError(
"Cannot interpret as bytes unambiguously - "
1359 "not multiple of 8 bits.")
1363 """Reset the bitstring to have given unsigned int interpretation."""
1368 except AttributeError:
1372 if length
is None or length == 0:
1373 raise CreationError(
"A non-zero length must be specified with a "
1374 "uint initialiser.")
1375 if uint >= (1 << length):
1376 msg =
"{0} is too large an unsigned integer for a bitstring of length {1}. "\
1377 "The allowed range is [0, {2}]."
1380 raise CreationError(
"uint cannot be initialsed by a negative number.")
1386 data = bytes.fromhex(s)
1387 except AttributeError:
1389 data = binascii.unhexlify(s)
1391 extrabytes = ((length + 7) // 8) - len(data)
1393 data = b
'\x00' * extrabytes + data
1394 offset = 8 - (length % 8)
1400 """Read bits and interpret as an unsigned int."""
1405 startbyte = (start + offset) // 8
1406 endbyte = (start + offset + length - 1) // 8
1408 b = binascii.hexlify(bytes(self.
_datastore.getbyteslice(startbyte, endbyte + 1)))
1411 final_bits = 8 - ((start + offset + length) % 8)
1414 i &= (1 << length) - 1
1418 """Return data as an unsigned int."""
1422 """Reset the bitstring to have given signed int interpretation."""
1424 if length
is None and hasattr(self,
'len')
and self.
len != 0:
1426 if length
is None or length == 0:
1427 raise CreationError(
"A non-zero length must be specified with an int initialiser.")
1428 if int_ >= (1 << (length - 1))
or int_ < -(1 << (length - 1)):
1429 raise CreationError(
"{0} is too large a signed integer for a bitstring of length {1}. "
1430 "The allowed range is [{2}, {3}].", int_, length, -(1 << (length - 1)),
1431 (1 << (length - 1)) - 1)
1445 """Read bits and interpret as a signed int"""
1447 if not ui >> (length - 1):
1451 tmp = (~(ui - 1)) & ((1 << length) - 1)
1455 """Return data as a two's complement signed int."""
1459 """Set the bitstring to a big-endian unsigned int interpretation."""
1460 if length
is not None and length % 8 != 0:
1461 raise CreationError(
"Big-endian integers must be whole-byte. "
1462 "Length = {0} bits.", length)
1466 """Read bits and interpret as a big-endian unsigned int."""
1469 "Length = {0} bits.", length)
1473 """Return data as a big-endian two's complement unsigned int."""
1477 """Set bitstring to a big-endian signed int interpretation."""
1478 if length
is not None and length % 8 != 0:
1479 raise CreationError(
"Big-endian integers must be whole-byte. "
1480 "Length = {0} bits.", length)
1484 """Read bits and interpret as a big-endian signed int."""
1487 "Length = {0} bits.", length)
1488 return self.
_readint(length, start)
1491 """Return data as a big-endian two's complement signed int."""
1494 def _setuintle(self, uintle, length=None):
1495 if length
is not None and length % 8 != 0:
1496 raise CreationError(
"Little-endian integers must be whole-byte. "
1497 "Length = {0} bits.", length)
1502 """Read bits and interpret as a little-endian unsigned int."""
1504 raise InterpretError(
"Little-endian integers must be whole-byte. "
1505 "Length = {0} bits.", length)
1506 assert start + length <= self.
len
1507 absolute_pos = start + self.
_offset
1508 startbyte, offset = divmod(absolute_pos, 8)
1511 endbyte = (absolute_pos + length - 1) // 8
1513 while endbyte - chunksize + 1 >= startbyte:
1514 val <<= 8 * chunksize
1515 val += struct.unpack(
'<L', bytes(self.
_datastore.getbyteslice(endbyte + 1 - chunksize, endbyte + 1)))[0]
1516 endbyte -= chunksize
1517 for b
in xrange(endbyte, startbyte - 1, -1):
1521 data = self.
_slice(start, start + length)
1522 assert data.len % 8 == 0
1523 data._reversebytes(0, self.
len)
1524 for b
in bytearray(data.bytes):
1529 def _getuintle(self):
1532 def _setintle(self, intle, length=None):
1533 if length
is not None and length % 8 != 0:
1534 raise CreationError(
"Little-endian integers must be whole-byte. "
1535 "Length = {0} bits.", length)
1540 """Read bits and interpret as a little-endian signed int."""
1542 if not ui >> (length - 1):
1546 tmp = (~(ui - 1)) & ((1 << length) - 1)
1549 def _getintle(self):
1552 def _setfloat(self, f, length=None):
1554 if length
is None and hasattr(self,
'len')
and self.
len != 0:
1556 if length
is None or length == 0:
1557 raise CreationError(
"A non-zero length must be specified with a "
1558 "float initialiser.")
1560 b = struct.pack(
'>f', f)
1562 b = struct.pack(
'>d', f)
1564 raise CreationError(
"floats can only be 32 or 64 bits long, "
1565 "not {0} bits", length)
1569 """Read bits and interpret as a float."""
1570 if not (start + self.
_offset) % 8:
1571 startbyte = (start + self.
_offset) // 8
1573 f, = struct.unpack(
'>f', bytes(self.
_datastore.getbyteslice(startbyte, startbyte + 4)))
1575 f, = struct.unpack(
'>d', bytes(self.
_datastore.getbyteslice(startbyte, startbyte + 8)))
1578 f, = struct.unpack(
'>f', self.
_readbytes(32, start))
1580 f, = struct.unpack(
'>d', self.
_readbytes(64, start))
1584 raise InterpretError(
"floats can only be 32 or 64 bits long, not {0} bits", length)
1587 """Interpret the whole bitstring as a float."""
1590 def _setfloatle(self, f, length=None):
1592 if length
is None and hasattr(self,
'len')
and self.
len != 0:
1594 if length
is None or length == 0:
1595 raise CreationError(
"A non-zero length must be specified with a "
1596 "float initialiser.")
1598 b = struct.pack(
'<f', f)
1600 b = struct.pack(
'<d', f)
1602 raise CreationError(
"floats can only be 32 or 64 bits long, "
1603 "not {0} bits", length)
1607 """Read bits and interpret as a little-endian float."""
1608 startbyte, offset = divmod(start + self.
_offset, 8)
1611 f, = struct.unpack(
'<f', bytes(self.
_datastore.getbyteslice(startbyte, startbyte + 4)))
1613 f, = struct.unpack(
'<d', bytes(self.
_datastore.getbyteslice(startbyte, startbyte + 8)))
1616 f, = struct.unpack(
'<f', self.
_readbytes(32, start))
1618 f, = struct.unpack(
'<d', self.
_readbytes(64, start))
1623 "not {0} bits", length)
1626 """Interpret the whole bitstring as a little-endian float."""
1630 """Initialise bitstring with unsigned exponential-Golomb code for integer i.
1632 Raises CreationError if i < 0.
1636 raise CreationError(
"Cannot use negative initialiser for unsigned "
1637 "exponential-Golomb.")
1646 remainingpart = i + 1 - (1 << leadingzeros)
1647 binstring =
'0' * leadingzeros +
'1' +
Bits(uint=remainingpart,
1648 length=leadingzeros).bin
1652 """Return interpretation of next bits as unsigned exponential-Golomb code.
1654 Raises ReadError if the end of the bitstring
is encountered
while
1660 while not self[pos]:
1663 raise ReadError(
"Read off end of bitstring trying to read code.")
1664 leadingzeros = pos - oldpos
1665 codenum = (1 << leadingzeros) - 1
1666 if leadingzeros > 0:
1667 if pos + leadingzeros + 1 > self.
len:
1668 raise ReadError(
"Read off end of bitstring trying to read code.")
1669 codenum += self.
_readuint(leadingzeros, pos + 1)
1670 pos += leadingzeros + 1
1677 """Return data as unsigned exponential-Golomb code.
1679 Raises InterpretError if bitstring
is not a single exponential-Golomb code.
1683 value, newpos = self.
_readue(0)
1684 if value
is None or newpos != self.
len:
1687 raise InterpretError(
"Bitstring is not a single exponential-Golomb code.")
1691 """Initialise bitstring with signed exponential-Golomb code for integer i."""
1699 """Return data as signed exponential-Golomb code.
1701 Raises InterpretError if bitstring
is not a single exponential-Golomb code.
1705 value, newpos = self.
_readse(0)
1706 if value
is None or newpos != self.
len:
1709 raise InterpretError(
"Bitstring is not a single exponential-Golomb code.")
1713 """Return interpretation of next bits as a signed exponential-Golomb code.
1715 Advances position to after the read code.
1717 Raises ReadError if the end of the bitstring
is encountered
while
1721 codenum, pos = self._readue(pos)
1722 m = (codenum + 1) // 2
1729 """Initialise bitstring with unsigned interleaved exponential-Golomb code for integer i.
1731 Raises CreationError if i < 0.
1735 raise CreationError(
"Cannot use negative initialiser for unsigned "
1736 "interleaved exponential-Golomb.")
1737 self.
_setbin_unsafe(
'1' if i == 0
else '0' +
'0'.join(bin(i + 1)[3:]) +
'1')
1740 """Return interpretation of next bits as unsigned interleaved exponential-Golomb code.
1742 Raises ReadError if the end of the bitstring
is encountered
while
1748 while not self[pos]:
1751 codenum += self[pos]
1755 raise ReadError(
"Read off end of bitstring trying to read code.")
1760 """Return data as unsigned interleaved exponential-Golomb code.
1762 Raises InterpretError if bitstring
is not a single exponential-Golomb code.
1767 if value
is None or newpos != self.
len:
1770 raise InterpretError(
"Bitstring is not a single interleaved exponential-Golomb code.")
1774 """Initialise bitstring with signed interleaved exponential-Golomb code for integer i."""
1782 """Return data as signed interleaved exponential-Golomb code.
1784 Raises InterpretError if bitstring
is not a single exponential-Golomb code.
1789 if value
is None or newpos != self.
len:
1792 raise InterpretError(
"Bitstring is not a single interleaved exponential-Golomb code.")
1796 """Return interpretation of next bits as a signed interleaved exponential-Golomb code.
1798 Advances position to after the read code.
1800 Raises ReadError if the end of the bitstring
is encountered
while
1809 return -codenum, pos + 1
1811 return codenum, pos + 1
1813 raise ReadError(
"Read off end of bitstring trying to read code.")
1815 def _setbool(self, value):
1818 if value
in (1,
'True'):
1820 elif value
in (0,
'False'):
1823 raise CreationError(
'Cannot initialise boolean with {0}.', value)
1827 msg =
"For a bool interpretation a bitstring must be 1 bit long, not {0} bits."
1831 def _readbool(self, pos):
1832 return self[pos], pos + 1
1835 """Reset the bitstring to the value given in binstring."""
1838 binstring = binstring.replace(
'0b',
'')
1842 """Same as _setbin_safe, but input isn't sanity checked. binstring mustn't start with '0b'."""
1843 length = len(binstring)
1845 boundary = ((length + 7) // 8) * 8
1846 padded_binstring = binstring +
'0' * (boundary - length)\
1847 if len(binstring) < boundary
else binstring
1849 bytelist = [int(padded_binstring[x:x + 8], 2)
1850 for x
in xrange(0, len(padded_binstring), 8)]
1852 raise CreationError(
"Invalid character in bin initialiser {0}.", binstring)
1856 """Read bits and interpret as a binary string."""
1860 startbyte, startoffset = divmod(start + self.
_offset, 8)
1861 endbyte = (start + self.
_offset + length - 1) // 8
1862 b = self.
_datastore.getbyteslice(startbyte, endbyte + 1)
1865 c =
"{:0{}b}".format(int(binascii.hexlify(b), 16), 8 * len(b))
1868 c =
"{0:0{1}b}".format(int(binascii.hexlify(str(b)), 16), 8 * len(b))
1870 return c[startoffset:startoffset + length]
1873 """Return interpretation as a binary string."""
1877 """Reset the bitstring to have the value given in octstring."""
1880 octstring = octstring.replace(
'0o',
'')
1884 if not 0 <= int(i) < 8:
1886 binlist.append(OCT_TO_BITS[int(i)])
1888 raise CreationError(
"Invalid symbol '{0}' in oct initialiser.", i)
1892 """Read bits and interpret as an octal string."""
1895 "not multiple of 3 bits.")
1900 end = oct(self.
_readuint(length, start))[LEADING_OCT_CHARS:]
1901 if end.endswith(
'L'):
1903 middle =
'0' * (length // 3 - len(end))
1907 """Return interpretation as an octal string."""
1911 """Reset the bitstring to have the value given in hexstring."""
1914 hexstring = hexstring.replace(
'0x',
'')
1915 length = len(hexstring)
1920 data = bytearray.fromhex(hexstring)
1923 data = bytearray.fromhex(unicode(hexstring))
1929 """Read bits and interpret as a hex string."""
1932 "not multiple of 4 bits.")
1938 except AttributeError:
1941 s = str(binascii.hexlify(s).decode(
'utf-8'))
1943 return s[:-1]
if (length // 4) % 2
else s
1946 """Return the hexadecimal representation as a string prefixed with '0x'.
1948 Raises an InterpretError if the bitstring
's length is not a multiple of 4.
1953 def _getoffset(self):
1957 """Return the length of the bitstring in bits."""
1961 """Ensure the data is held in memory, not in a file."""
1967 """Convert bs to a bitstring and return it.
1969 offset gives the suggested bit offset of first significant
1970 bit, to optimise append etc.
1975 if isinstance(bs, Bits):
1978 return cache[(bs, offset)]
1980 if isinstance(bs, basestring):
1984 except ValueError
as e:
1987 b._append(Bits._init_with_token(*tokens[0]))
1988 b._datastore =
offsetcopy(b._datastore, offset)
1989 for token
in tokens[1:]:
1990 b._append(Bits._init_with_token(*token))
1991 assert b._assertsanity()
1992 assert b.len == 0
or b._offset == offset
1993 if len(cache) < CACHE_SIZE:
1994 cache[(bs, offset)] = b
2002 """Create and return a new copy of the Bits (always in memory)."""
2003 s_copy = self.__class__()
2009 """Used internally to get a slice, without error checking."""
2011 return self.__class__()
2013 startbyte, newoffset = divmod(start + offset, 8)
2014 endbyte = (end + offset - 1) // 8
2015 bs = self.__class__()
2016 bs._setbytes_unsafe(self.
_datastore.getbyteslice(startbyte, endbyte + 1), end - start, newoffset)
2020 """Reads a token from the bitstring and returns the result."""
2021 if length
is not None and int(length) > self.
length - pos:
2022 raise ReadError(
"Reading off the end of the data. "
2023 "Tried to read {0} bits when only {1} available.".format(int(length), self.
length - pos))
2025 val = name_to_read[name](self, length, pos)
2026 return val, pos + length
2029 return None, pos + length
2030 raise ValueError(
"Can't parse token {0}:{1}".format(name, length))
2033 return name_to_read[name](self, pos)
2036 """Append a bitstring to the current bitstring."""
2040 """Prepend a bitstring to the current bitstring."""
2044 """Reverse all bits in-place."""
2046 n = [BYTE_REVERSAL_DICT[b]
for b
in self.
_datastore.rawbytes]
2050 newoffset = 8 - (self.
_offset + self.
len) % 8
2056 """Truncate bits from the start of the bitstring."""
2057 assert 0 <= bits <= self.
len
2060 if bits == self.
len:
2063 bytepos, offset = divmod(self.
_offset + bits, 8)
2069 """Truncate bits from the end of the bitstring."""
2070 assert 0 <= bits <= self.
len
2073 if bits == self.
len:
2076 newlength_in_bytes = (self.
_offset + self.
len - bits + 7) // 8
2082 """Insert bs at pos."""
2083 assert 0 <= pos <= self.
len
2084 if pos > self.
len // 2:
2092 start = self.
_slice(0, pos)
2097 self.
_pos = pos + bs.len
2098 except AttributeError:
2103 """Overwrite with bs at pos."""
2104 assert 0 <= pos < self.
len
2109 firstbytepos = (self.
_offset + pos) // 8
2110 lastbytepos = (self.
_offset + pos + bs.len - 1) // 8
2111 bytepos, bitoffset = divmod(self.
_offset + pos, 8)
2112 if firstbytepos == lastbytepos:
2113 mask = ((1 << bs.len) - 1) << (8 - bs.len - bitoffset)
2119 mask = (1 << (8 - bitoffset)) - 1
2124 self.
_datastore.setbyteslice(firstbytepos + 1, lastbytepos, d.getbyteslice(1, lastbytepos - firstbytepos))
2126 bitsleft = (self.
_offset + pos + bs.len) % 8
2129 mask = (1 << (8 - bitsleft)) - 1
2132 self.
_datastore.getbyte(lastbytepos) | (d.getbyte(d.bytelength - 1) & ~mask))
2136 """Delete bits at pos."""
2137 assert 0 <= pos <= self.
len
2138 assert pos + bits <= self.
len
2143 if pos + bits == self.
len:
2147 if pos > self.
len - pos - bits:
2151 assert self.
len - pos > 0
2156 start = self.
_slice(0, pos)
2162 """Reverse bytes in-place."""
2165 newoffset = 8 - (start % 8)
2170 toreverse = bytearray(self.
_datastore.getbyteslice((newoffset + start) // 8, (newoffset + end) // 8))
2172 self.
_datastore.setbyteslice((newoffset + start) // 8, (newoffset + end) // 8, toreverse)
2175 """Set bit at pos to 1."""
2176 assert 0 <= pos < self.
len
2180 """Set bit at pos to 0."""
2181 assert 0 <= pos < self.
len
2185 """Flip bit at pos 1<->0."""
2186 assert 0 <= pos < self.
len
2190 """Invert every bit."""
2194 set(p, 256 + ~get(p))
2197 """Shift bits by n to the left in place. Return self."""
2198 assert 0 < n <= self.
len
2204 """Shift bits by n to the right in place. Return self."""
2205 assert 0 < n <= self.
len
2211 """Concatenate n copies of self in place. Return self."""
2221 self.
_append(self[0:(n - m) * old_len])
2225 """Helper function containing most of the __ior__, __iand__, __ixor__ code."""
2227 self_byteoffset, self_bitoffset = divmod(self.
_offset, 8)
2228 bs_byteoffset, bs_bitoffset = divmod(bs._offset, 8)
2229 if bs_bitoffset != self_bitoffset:
2230 if not self_bitoffset:
2235 b = bs._datastore.rawbytes
2236 for i
in xrange(len(a)):
2237 a[i] = f(a[i + self_byteoffset], b[i + bs_byteoffset])
2243 def _iand(self, bs):
2246 def _ixor(self, bs):
2250 """Read some bits from the bitstring and return newly constructed bitstring."""
2251 return self.
_slice(start, start + length)
2254 """Validate start and end and return them as positive bit positions."""
2263 if not 0 <= end <= self.
len:
2264 raise ValueError(
"end is not a valid position in the bitstring.")
2265 if not 0 <= start <= self.
len:
2266 raise ValueError(
"start is not a valid position in the bitstring.")
2268 raise ValueError(
"end must not be less than start.")
2272 """Interpret the whole bitstring using fmt and return list.
2274 fmt -- A single string or a list of strings
with comma separated tokens
2275 describing how to interpret the bits
in the bitstring. Items
2276 can also be integers,
for reading new bitstring of the given length.
2277 kwargs -- A dictionary
or keyword-value pairs - the keywords used
in the
2278 format string will be replaced
with their given value.
2280 Raises ValueError
if the format
is not understood. If
not enough bits
2281 are available then all bits to the end of the bitstring will be used.
2283 See the docstring
for 'read' for token examples.
2286 return self.
_readlist(fmt, 0, **kwargs)[0]
2288 def _readlist(self, fmt, pos, **kwargs):
2290 stretchy_token =
None
2291 if isinstance(fmt, basestring):
2295 for i, f
in enumerate(fmt):
2296 if isinstance(f, numbers.Integral):
2297 fmt[i] =
"bits:{0}".format(f)
2299 stretchy, tkns =
tokenparser(f_item, tuple(sorted(kwargs.keys())))
2302 raise Error(
"It's not possible to have more than one 'filler' token.")
2303 stretchy_token = stretchy
2305 if not stretchy_token:
2307 for name, length, _
in tokens:
2308 if length
in kwargs:
2309 length = kwargs[length]
2312 if name
in kwargs
and length
is None:
2314 value, pos = self.
_readtoken(
'uint', pos, kwargs[name])
2317 value, pos = self.
_readtoken(name, pos, length)
2318 if value
is not None:
2321 stretchy_token =
False
2322 bits_after_stretchy_token = 0
2323 for token
in tokens:
2324 name, length, _ = token
2325 if length
in kwargs:
2326 length = kwargs[length]
2329 if name
in kwargs
and length
is None:
2331 length = kwargs[name]
2333 if name
in (
'se',
'ue',
'sie',
'uie'):
2334 raise Error(
"It's not possible to parse a variable"
2335 "length token after a 'filler' token.")
2338 raise Error(
"It's not possible to have more than "
2339 "one 'filler' token.")
2340 bits_after_stretchy_token += length
2341 if length
is None and name
not in (
'se',
'ue',
'sie',
'uie'):
2342 assert not stretchy_token
2343 stretchy_token = token
2344 bits_left = self.
len - pos
2346 for token
in tokens:
2347 name, length, _ = token
2348 if token
is stretchy_token:
2350 length = max(bits_left - bits_after_stretchy_token, 0)
2351 if length
in kwargs:
2352 length = kwargs[length]
2355 if name
in kwargs
and length
is None:
2357 length = kwargs[name]
2358 if length
is not None:
2360 value, pos = self.
_readtoken(name, pos, length)
2361 if value
is not None:
2362 return_values.append(value)
2363 return return_values, pos
2366 """Quicker version of find when everything's whole byte
2371 assert bytealigned
is True
2373 bytepos = (start + 7) // 8
2377 increment = max(1024, len(bytes_) * 10)
2378 buffersize = increment + len(bytes_)
2381 buf = bytearray(self.
_datastore.getbyteslice(p, min(p + buffersize, finalpos)))
2382 pos = buf.find(bytes_)
2393 """Find first occurrence of a compiled regular expression.
2395 Note that this doesn't support arbitrary regexes, in particular they
2396 must match a known length.
2400 length = len(reg_ex.pattern)
2403 increment = max(4096, length * 10)
2404 buffersize = increment + length
2406 buf = self.
_readbin(min(buffersize, end - p), p)
2408 m = reg_ex.search(buf)
2414 if not bytealigned
or (p + pos) % 8 == 0:
2424 def find(self, bs, start=None, end=None, bytealigned=None):
2425 """Find first occurrence of substring bs.
2427 Returns a single item tuple with the bit position
if found,
or an
2428 empty tuple
if not found. The bit position (pos property) will
2429 also be set to the start of the substring
if it
is found.
2431 bs -- The bitstring to find.
2432 start -- The bit position to start the search. Defaults to 0.
2433 end -- The bit position one past the last bit to search.
2434 Defaults to self.
len.
2435 bytealigned -- If
True the bitstring will only be
2436 found on byte boundaries.
2438 Raises ValueError
if bs
is empty,
if start < 0,
if end > self.
len or
2447 raise ValueError(
"Cannot find an empty bitstring.")
2449 if bytealigned
is None:
2450 bytealigned = globals()[
'bytealigned']
2451 if bytealigned
and not bs.len % 8
and not self.
_datastore.offset:
2452 p = self.
_findbytes(bs.bytes, start, end, bytealigned)
2454 p = self.
_findregex(re.compile(bs._getbin()), start, end, bytealigned)
2458 except (AttributeError, IndexError):
2462 def findall(self, bs, start=None, end=None, count=None, bytealigned=None):
2463 """Find all occurrences of bs. Return generator of bit positions.
2465 bs -- The bitstring to find.
2466 start -- The bit position to start the search. Defaults to 0.
2467 end -- The bit position one past the last bit to search.
2468 Defaults to self.len.
2469 count -- The maximum number of occurrences to find.
2470 bytealigned -- If True the bitstring will only be found on
2473 Raises ValueError
if bs
is empty,
if start < 0,
if end > self.
len or
2476 Note that all occurrences of bs are found, even
if they overlap.
2479 if count
is not None and count < 0:
2480 raise ValueError(
"In findall, count must be >= 0.")
2483 if bytealigned
is None:
2484 bytealigned = globals()[
'bytealigned']
2486 if bytealigned
and not bs.len % 8
and not self.
_datastore.offset:
2492 x = re.compile(bs._getbin())
2495 p = f(x, start, end, bytealigned)
2498 if count
is not None and c >= count:
2503 except AttributeError:
2514 def rfind(self, bs, start=None, end=None, bytealigned=None):
2515 """Find final occurrence of substring bs.
2517 Returns a single item tuple with the bit position
if found,
or an
2518 empty tuple
if not found. The bit position (pos property) will
2519 also be set to the start of the substring
if it
is found.
2521 bs -- The bitstring to find.
2522 start -- The bit position to end the reverse search. Defaults to 0.
2523 end -- The bit position one past the first bit to reverse search.
2524 Defaults to self.
len.
2525 bytealigned -- If
True the bitstring will only be found on byte
2528 Raises ValueError
if bs
is empty,
if start < 0,
if end > self.
len or
2534 if bytealigned
is None:
2535 bytealigned = globals()[
'bytealigned']
2537 raise ValueError(
"Cannot find an empty bitstring.")
2540 increment = max(8192, bs.len * 80)
2541 buffersize = min(increment + bs.len, end - start)
2542 pos = max(start, end - buffersize)
2544 found = list(self.
findall(bs, start=pos, end=pos + buffersize,
2545 bytealigned=bytealigned))
2549 pos = max(start, pos - increment)
2553 def cut(self, bits, start=None, end=None, count=None):
2554 """Return bitstring generator by cutting into bits sized chunks.
2556 bits -- The size in bits of the bitstring chunks to generate.
2557 start -- The bit position to start the first cut. Defaults to 0.
2558 end -- The bit position one past the last bit to use
in the cut.
2559 Defaults to self.
len.
2560 count -- If specified then at most count items are generated.
2561 Default
is to cut
as many times
as possible.
2565 if count
is not None and count < 0:
2566 raise ValueError(
"Cannot cut - count must be >= 0.")
2568 raise ValueError(
"Cannot cut - bits must be >= 0.")
2570 while count
is None or c < count:
2572 nextchunk = self.
_slice(start, min(start + bits, end))
2573 if nextchunk.len != bits:
2575 assert nextchunk._assertsanity()
2580 def split(self, delimiter, start=None, end=None, count=None,
2582 """Return bitstring generator by splittling using a delimiter.
2584 The first item returned is the initial bitstring before the delimiter,
2585 which may be an empty bitstring.
2587 delimiter -- The bitstring used
as the divider.
2588 start -- The bit position to start the split. Defaults to 0.
2589 end -- The bit position one past the last bit to use
in the split.
2590 Defaults to self.
len.
2591 count -- If specified then at most count items are generated.
2592 Default
is to split
as many times
as possible.
2593 bytealigned -- If
True splits will only occur on byte boundaries.
2595 Raises ValueError
if the delimiter
is empty.
2598 delimiter = Bits(delimiter)
2599 if not delimiter.len:
2600 raise ValueError(
"split delimiter cannot be empty.")
2602 if bytealigned
is None:
2603 bytealigned = globals()[
'bytealigned']
2604 if count
is not None and count < 0:
2605 raise ValueError(
"Cannot split - count must be >= 0.")
2608 if bytealigned
and not delimiter.len % 8
and not self.
_datastore.offset:
2611 x = delimiter._getbytes()
2614 x = re.compile(delimiter._getbin())
2615 found = f(x, start, end, bytealigned)
2618 yield self.
_slice(start, end)
2621 yield self.
_slice(start, found[0])
2622 startpos = pos = found[0]
2624 while count
is None or c < count:
2625 pos += delimiter.len
2626 found = f(x, pos, end, bytealigned)
2629 yield self.
_slice(startpos, end)
2632 yield self.
_slice(startpos, found[0])
2633 startpos = pos = found[0]
2637 def join(self, sequence):
2638 """Return concatenation of bitstrings joined by self.
2640 sequence -- A sequence of bitstrings.
2643 s = self.__class__()
2646 s._append(
Bits(next(i)))
2651 except StopIteration:
2656 """Return the bitstring as bytes, padding with zero bits if needed.
2658 Up to seven zero bits will be added at the end to byte align.
2663 unusedbits = 8 - self.
len % 8
2665 d[-1] &= (0xff << unusedbits)
2669 """Write the bitstring to a file object, padding with zero bits if needed.
2671 Up to seven zero bits will be added at the end to byte align.
2676 chunksize = 1024 * 1024
2680 p = self.
_datastore.getbyteslice(a, min(a + chunksize, bytelen - 1))
2681 while len(p) == chunksize:
2684 p = self.
_datastore.getbyteslice(a, min(a + chunksize, bytelen - 1))
2687 bits_in_final_byte = self.
len % 8
2688 if not bits_in_final_byte:
2689 bits_in_final_byte = 8
2690 f.write(self[-bits_in_final_byte:].
tobytes())
2694 b = a + chunksize * 8
2695 while b <= self.
len:
2703 """Return whether the current bitstring starts with prefix.
2705 prefix -- The bitstring to search for.
2706 start -- The bit position to start
from. Defaults to 0.
2707 end -- The bit position to end at. Defaults to self.
len.
2710 prefix = Bits(prefix)
2712 if end < start + prefix.len:
2714 end = start + prefix.len
2715 return self.
_slice(start, end) == prefix
2718 """Return whether the current bitstring ends with suffix.
2720 suffix -- The bitstring to search for.
2721 start -- The bit position to start
from. Defaults to 0.
2722 end -- The bit position to end at. Defaults to self.
len.
2725 suffix = Bits(suffix)
2727 if start + suffix.len > end:
2729 start = end - suffix.len
2730 return self.
_slice(start, end) == suffix
2732 def all(self, value, pos=None):
2733 """Return True if one or many bits are all set to value.
2735 value -- If value is True then checks
for bits set to 1, otherwise
2736 checks
for bits set to 0.
2737 pos -- An iterable of bit positions. Negative numbers are treated
in
2738 the same way
as slice indices. Defaults to the whole bitstring.
2744 pos = xrange(self.
len)
2748 if not 0 <= p < length:
2749 raise IndexError(
"Bit position {0} out of range.".format(p))
2754 def any(self, value, pos=None):
2755 """Return True if any of one or many bits are set to value.
2757 value -- If value is True then checks
for bits set to 1, otherwise
2758 checks
for bits set to 0.
2759 pos -- An iterable of bit positions. Negative numbers are treated
in
2760 the same way
as slice indices. Defaults to the whole bitstring.
2766 pos = xrange(self.
len)
2770 if not 0 <= p < length:
2771 raise IndexError(
"Bit position {0} out of range.".format(p))
2777 """Return count of total number of either zero or one bits.
2779 value -- If True then bits set to 1 are counted, otherwise bits set
2790 count = sum(BIT_COUNT[self.
_datastore.getbyte(i)]
for i
in xrange(self.
_datastore.bytelength - 1))
2797 return count
if value
else self.
len - count
2800 if byteorder ==
'little':
2801 _setfloatne = _setfloatle
2802 _readfloatne = _readfloatle
2803 _getfloatne = _getfloatle
2804 _setuintne = _setuintle
2805 _readuintne = _readuintle
2806 _getuintne = _getuintle
2807 _setintne = _setintle
2808 _readintne = _readintle
2809 _getintne = _getintle
2811 _setfloatne = _setfloat
2812 _readfloatne = _readfloat
2813 _getfloatne = _getfloat
2814 _setuintne = _setuintbe
2815 _readuintne = _readuintbe
2816 _getuintne = _getuintbe
2817 _setintne = _setintbe
2818 _readintne = _readintbe
2819 _getintne = _getintbe
2821 _offset = property(_getoffset)
2823 len = property(_getlength,
2824 doc=
"""The length of the bitstring in bits. Read only.
2826 length = property(_getlength,
2827 doc="""The length of the bitstring in bits. Read only.
2829 bool = property(_getbool,
2830 doc="""The bitstring as a bool (True or False). Read only.
2832 hex = property(_gethex,
2833 doc="""The bitstring as a hexadecimal string. Read only.
2835 bin = property(_getbin,
2836 doc="""The bitstring as a binary string. Read only.
2838 oct = property(_getoct,
2839 doc="""The bitstring as an octal string. Read only.
2841 bytes = property(_getbytes,
2842 doc="""The bitstring as a bytes object. Read only.
2844 int = property(_getint,
2845 doc="""The bitstring as a two's complement signed int. Read only.
2847 uint = property(_getuint,
2848 doc="""The bitstring as a two's complement unsigned int. Read only.
2850 float = property(_getfloat,
2851 doc="""The bitstring as a floating point number. Read only.
2853 intbe = property(_getintbe,
2854 doc="""The bitstring as a two's complement big-endian signed int. Read only.
2856 uintbe = property(_getuintbe,
2857 doc="""The bitstring as a two's complement big-endian unsigned int. Read only.
2859 floatbe = property(_getfloat,
2860 doc="""The bitstring as a big-endian floating point number. Read only.
2862 intle = property(_getintle,
2863 doc="""The bitstring as a two's complement little-endian signed int. Read only.
2865 uintle = property(_getuintle,
2866 doc="""The bitstring as a two's complement little-endian unsigned int. Read only.
2868 floatle = property(_getfloatle,
2869 doc="""The bitstring as a little-endian floating point number. Read only.
2871 intne = property(_getintne,
2872 doc="""The bitstring as a two's complement native-endian signed int. Read only.
2874 uintne = property(_getuintne,
2875 doc="""The bitstring as a two's complement native-endian unsigned int. Read only.
2877 floatne = property(_getfloatne,
2878 doc="""The bitstring as a native-endian floating point number. Read only.
2880 ue = property(_getue,
2881 doc="""The bitstring as an unsigned exponential-Golomb code. Read only.
2883 se = property(_getse,
2884 doc="""The bitstring as a signed exponential-Golomb code. Read only.
2886 uie = property(_getuie,
2887 doc="""The bitstring as an unsigned interleaved exponential-Golomb code. Read only.
2889 sie = property(_getsie,
2890 doc="""The bitstring as a signed interleaved exponential-Golomb code. Read only.
2894# Dictionary that maps token names to the function that reads them.
2895name_to_read = {'uint': Bits._readuint,
2896 'uintle': Bits._readuintle,
2897 'uintbe': Bits._readuintbe,
2898 'uintne': Bits._readuintne,
2899 'int': Bits._readint,
2900 'intle': Bits._readintle,
2901 'intbe': Bits._readintbe,
2902 'intne': Bits._readintne,
2903 'float': Bits._readfloat,
2904 'floatbe': Bits._readfloat,
2905 'floatle': Bits._readfloatle,
2906 'floatne': Bits._readfloatne,
2907 'hex': Bits._readhex,
2908 'oct': Bits._readoct,
2909 'bin': Bits._readbin,
2910 'bits': Bits._readbits,
2911 'bytes': Bits._readbytes,
2914 'uie': Bits._readuie,
2915 'sie': Bits._readsie,
2916 'bool': Bits._readbool,
2920init_with_length_and_offset = {
'bytes': Bits._setbytes_safe,
2921 'filename': Bits._setfile,
2924init_with_length_only = {
'uint': Bits._setuint,
2925 'int': Bits._setint,
2926 'float': Bits._setfloat,
2927 'uintbe': Bits._setuintbe,
2928 'intbe': Bits._setintbe,
2929 'floatbe': Bits._setfloat,
2930 'uintle': Bits._setuintle,
2931 'intle': Bits._setintle,
2932 'floatle': Bits._setfloatle,
2933 'uintne': Bits._setuintne,
2934 'intne': Bits._setintne,
2935 'floatne': Bits._setfloatne,
2938init_without_length_or_offset = {
'bin': Bits._setbin_safe,
2939 'hex': Bits._sethex,
2940 'oct': Bits._setoct,
2943 'uie': Bits._setuie,
2944 'sie': Bits._setsie,
2945 'bool': Bits._setbool,
2950 """A container holding a mutable sequence of bits.
2952 Subclass of the immutable Bits class. Inherits all of its
2953 methods (
except __hash__)
and adds mutating methods.
2957 append() -- Append a bitstring.
2958 byteswap() -- Change byte endianness
in-place.
2959 insert() -- Insert a bitstring.
2960 invert() -- Flip bit(s) between one
and zero.
2961 overwrite() -- Overwrite a section
with a new bitstring.
2962 prepend() -- Prepend a bitstring.
2963 replace() -- Replace occurrences of one bitstring
with another.
2964 reverse() -- Reverse bits
in-place.
2965 rol() -- Rotate bits to the left.
2966 ror() -- Rotate bits to the right.
2967 set() -- Set bit(s) to 1
or 0.
2969 Methods inherited
from Bits:
2971 all() -- Check
if all specified bits are set to 1
or 0.
2972 any() -- Check
if any of specified bits are set to 1
or 0.
2973 count() -- Count the number of bits set to 1
or 0.
2974 cut() -- Create generator of constant sized chunks.
2975 endswith() -- Return whether the bitstring ends
with a sub-string.
2976 find() -- Find a sub-bitstring
in the current bitstring.
2977 findall() -- Find all occurrences of a sub-bitstring
in the current bitstring.
2978 join() -- Join bitstrings together using current bitstring.
2979 rfind() -- Seek backwards to find a sub-bitstring.
2980 split() -- Create generator of chunks split by a delimiter.
2981 startswith() -- Return whether the bitstring starts
with a sub-bitstring.
2982 tobytes() -- Return bitstring
as bytes, padding
if needed.
2983 tofile() -- Write bitstring to file, padding
if needed.
2984 unpack() -- Interpret bits using format string.
2988 Mutating operators are available: [], <<=, >>=, +=, *=, &=, |=
and ^=
2989 in addition to the inherited [], ==, !=, +, *, ~, <<, >>, &, |
and ^.
2993 bin -- The bitstring
as a binary string.
2994 bool -- For single bit bitstrings, interpret
as True or False.
2995 bytepos -- The current byte position
in the bitstring.
2996 bytes -- The bitstring
as a bytes object.
2997 float -- Interpret
as a floating point number.
2998 floatbe -- Interpret
as a big-endian floating point number.
2999 floatle -- Interpret
as a little-endian floating point number.
3000 floatne -- Interpret
as a native-endian floating point number.
3001 hex -- The bitstring
as a hexadecimal string.
3002 int -- Interpret
as a two
's complement signed integer.
3003 intbe -- Interpret as a big-endian signed integer.
3004 intle -- Interpret
as a little-endian signed integer.
3005 intne -- Interpret
as a native-endian signed integer.
3006 len -- Length of the bitstring
in bits.
3007 oct -- The bitstring
as an octal string.
3008 pos -- The current bit position
in the bitstring.
3009 se -- Interpret
as a signed exponential-Golomb code.
3010 ue -- Interpret
as an unsigned exponential-Golomb code.
3011 sie -- Interpret
as a signed interleaved exponential-Golomb code.
3012 uie -- Interpret
as an unsigned interleaved exponential-Golomb code.
3013 uint -- Interpret
as a two
's complement unsigned integer.
3014 uintbe -- Interpret as a big-endian unsigned integer.
3015 uintle -- Interpret
as a little-endian unsigned integer.
3016 uintne -- Interpret
as a native-endian unsigned integer.
3025 def __init__(self, auto=None, length=None, offset=None, **kwargs):
3026 """Either specify an 'auto' initialiser:
3027 auto -- a string of comma separated tokens, an integer, a file object,
3028 a bytearray, a boolean iterable or another bitstring.
3030 Or initialise via **kwargs
with one (
and only one) of:
3031 bytes -- raw data
as a string,
for example read
from a binary file.
3032 bin -- binary string representation, e.g.
'0b001010'.
3033 hex -- hexadecimal string representation, e.g.
'0x2ef'
3034 oct -- octal string representation, e.g.
'0o777'.
3035 uint -- an unsigned integer.
3036 int -- a signed integer.
3037 float -- a floating point number.
3038 uintbe -- an unsigned big-endian whole byte integer.
3039 intbe -- a signed big-endian whole byte integer.
3040 floatbe - a big-endian floating point number.
3041 uintle -- an unsigned little-endian whole byte integer.
3042 intle -- a signed little-endian whole byte integer.
3043 floatle -- a little-endian floating point number.
3044 uintne -- an unsigned native-endian whole byte integer.
3045 intne -- a signed native-endian whole byte integer.
3046 floatne -- a native-endian floating point number.
3047 se -- a signed exponential-Golomb code.
3048 ue -- an unsigned exponential-Golomb code.
3049 sie -- a signed interleaved exponential-Golomb code.
3050 uie -- an unsigned interleaved exponential-Golomb code.
3051 bool -- a boolean (
True or False).
3052 filename -- a file which will be opened
in binary read-only mode.
3054 Other keyword arguments:
3055 length -- length of the bitstring
in bits,
if needed
and appropriate.
3056 It must be supplied
for all integer
and float initialisers.
3057 offset -- bit offset to the data. These offset bits are
3058 ignored
and this
is intended
for use when
3059 initialising using
'bytes' or 'filename'.
3063 if not isinstance(self.
_datastore, ByteStore):
3066 def __new__(cls, auto=None, length=None, offset=None, **kwargs):
3067 x = super(BitArray, cls).__new__(cls)
3068 y = Bits.__new__(BitArray, auto, length, offset, **kwargs)
3069 x._datastore = y._datastore
3073 """Append bs to current bitstring. Return self.
3075 bs -- the bitstring to append.
3082 """Return a new copy of the BitArray."""
3084 if not isinstance(self.
_datastore, ByteStore):
3089 s_copy._datastore = copy.copy(self.
_datastore)
3093 """Set item or range to new value.
3095 Indices are in units of the step parameter (default 1 bit).
3096 Stepping
is used to specify the number of bits
in each item.
3098 If the length of the bitstring
is changed then pos will be moved
3099 to after the inserted section, otherwise it will remain unchanged.
3102 >>> s[0:1:4] =
'0xe'
3113 if key.step
is not None:
3115 except AttributeError:
3119 if not 0 <= key < self.
len:
3120 raise IndexError(
"Slice index out of range.")
3121 if isinstance(value, numbers.Integral):
3125 if value
in (1, -1):
3128 raise ValueError(
"Cannot set a single bit with integer {0}.".format(value))
3146 temp.__setitem__(key, v)
3152 if not isinstance(value, numbers.Integral):
3157 raise TypeError(
"Bitstring, integer or string expected. "
3158 "Got {0}.".format(type(value)))
3159 if key.start
is not None:
3166 if key.stop
is not None:
3174 if isinstance(value, numbers.Integral):
3176 value = self.__class__(uint=value, length=stop - start)
3178 value = self.__class__(int=value, length=stop - start)
3179 stop = min(stop, self.
len)
3180 start = max(start, 0)
3181 start = min(start, stop)
3182 if (stop - start) == value.len:
3188 self.
_overwrite(value.__getitem__(slice(
None,
None, 1)), start)
3192 self.
_delete(stop - start, start)
3196 self.
_insert(value.__getitem__(slice(
None,
None, 1)), start)
3201 """Delete item or range.
3203 Indices are in units of the step parameter (default 1 bit).
3204 Stepping
is used to specify the number of bits
in each item.
3215 step = key.step
if key.step
is not None else 1
3216 except AttributeError:
3220 if not 0 <= key < self.
len:
3221 raise IndexError(
"Slice index out of range.")
3229 temp.__delitem__(key)
3233 if key.start
is not None:
3235 if key.start < 0
and stop
is None:
3243 stop = min(stop, self.
len)
3244 start = max(start, 0)
3245 start = min(start, stop)
3246 self.
_delete(stop - start, start)
3250 """Shift bits by n to the left in place. Return self.
3252 n -- the number of bits to shift. Must be >= 0.
3256 raise ValueError(
"Cannot shift by a negative amount.")
3258 raise ValueError(
"Cannot shift an empty bitstring.")
3261 n = min(n, self.
len)
3265 """Shift bits by n to the right in place. Return self.
3267 n -- the number of bits to shift. Must be >= 0.
3271 raise ValueError(
"Cannot shift by a negative amount.")
3273 raise ValueError(
"Cannot shift an empty bitstring.")
3276 n = min(n, self.
len)
3280 """Concatenate n copies of self in place. Return self.
3282 Called for expressions of the form
'a *= 3'.
3283 n -- The number of concatenations. Must be >= 0.
3287 raise ValueError(
"Cannot multiply by a negative integer.")
3288 return self.
_imul(n)
3290 def __ior__(self, bs):
3292 if self.
len != bs.len:
3293 raise ValueError(
"Bitstrings must have the same length "
3295 return self.
_ior(bs)
3297 def __iand__(self, bs):
3299 if self.
len != bs.len:
3300 raise ValueError(
"Bitstrings must have the same length "
3302 return self.
_iand(bs)
3304 def __ixor__(self, bs):
3306 if self.
len != bs.len:
3307 raise ValueError(
"Bitstrings must have the same length "
3309 return self.
_ixor(bs)
3311 def replace(self, old, new, start=None, end=None, count=None,
3313 """Replace all occurrences of old with new in place.
3315 Returns number of replacements made.
3317 old -- The bitstring to replace.
3318 new -- The replacement bitstring.
3319 start -- Any occurrences that start before this will not be replaced.
3321 end -- Any occurrences that finish after this will
not be replaced.
3322 Defaults to self.
len.
3323 count -- The maximum number of replacements to make. Defaults to
3324 replace all occurrences.
3325 bytealigned -- If
True replacements will only be made on byte
3328 Raises ValueError
if old
is empty
or if start
or end are
3335 raise ValueError(
"Empty bitstring cannot be replaced.")
3337 if bytealigned
is None:
3338 bytealigned = globals()[
'bytealigned']
3340 if count
is not None:
3342 sections = self.
split(old, start, end, count, bytealigned)
3343 lengths = [s.len
for s
in sections]
3344 if len(lengths) == 1:
3349 new = copy.copy(self)
3350 positions = [lengths[0] + start]
3351 for k
in lengths[1:-1]:
3353 positions.append(positions[-1] + k)
3361 self[p:p + old.len] = new
3362 if old.len != new.len:
3363 diff = new.len - old.len
3367 if p + old.len <= newpos:
3372 except AttributeError:
3374 self[p:p + old.len] = new
3376 return len(lengths) - 1
3379 """Insert bs at bit position pos.
3381 bs -- The bitstring to insert.
3382 pos -- The bit position to insert at.
3384 Raises ValueError if pos < 0
or pos > self.
len.
3395 except AttributeError:
3396 raise TypeError(
"insert require a bit position for this type.")
3399 if not 0 <= pos <= self.
len:
3400 raise ValueError(
"Invalid insert position.")
3404 """Overwrite with bs at bit position pos.
3406 bs -- The bitstring to overwrite with.
3407 pos -- The bit position to begin overwriting
from.
3409 Raises ValueError
if pos < 0
or pos + bs.len > self.
len
3418 except AttributeError:
3419 raise TypeError(
"overwrite require a bit position for this type.")
3422 if pos < 0
or pos + bs.len > self.
len:
3423 raise ValueError(
"Overwrite exceeds boundary of bitstring.")
3427 except AttributeError:
3431 """Append a bitstring to the current bitstring.
3433 bs -- The bitstring to append.
3441 """Prepend a bitstring to the current bitstring.
3443 bs -- The bitstring to prepend.
3450 """Reverse bits in-place.
3452 start -- Position of first bit to reverse. Defaults to 0.
3453 end -- One past the position of the last bit to reverse.
3454 Defaults to self.len.
3456 Using on an empty bitstring will have no effect.
3458 Raises ValueError if start < 0, end > self.
len or end < start.
3462 if start == 0
and end == self.
len:
3465 s = self.
_slice(start, end)
3469 def set(self, value, pos=None):
3470 """Set one or many bits to 1 or 0.
3472 value -- If True bits are set to 1, otherwise they are set to 0.
3473 pos -- Either a single bit position
or an iterable of bit positions.
3474 Negative numbers are treated
in the same way
as slice indices.
3475 Defaults to the entire bitstring.
3477 Raises IndexError
if pos < -self.
len or pos >= self.
len.
3482 pos = xrange(self.
len)
3488 if not 0 <= p < length:
3489 raise IndexError(
"Bit position {0} out of range.".format(p))
3495 if not 0 <= pos < length:
3496 raise IndexError(
"Bit position {0} out of range.".format(pos))
3500 """Invert one or many bits from 0 to 1 or vice versa.
3502 pos -- Either a single bit position or an iterable of bit positions.
3503 Negative numbers are treated
in the same way
as slice indices.
3505 Raises IndexError
if pos < -self.
len or pos >= self.
len.
3511 if not isinstance(pos, collections.abc.Iterable):
3518 if not 0 <= p < length:
3519 raise IndexError(
"Bit position {0} out of range.".format(p))
3522 def ror(self, bits, start=None, end=None):
3523 """Rotate bits to the right in-place.
3525 bits -- The number of bits to rotate by.
3526 start -- Start of slice to rotate. Defaults to 0.
3527 end -- End of slice to rotate. Defaults to self.len.
3529 Raises ValueError if bits < 0.
3533 raise Error(
"Cannot rotate an empty bitstring.")
3535 raise ValueError(
"Cannot rotate right by negative amount.")
3537 bits %= (end - start)
3540 rhs = self.
_slice(end - bits, end)
3541 self.
_delete(bits, end - bits)
3544 def rol(self, bits, start=None, end=None):
3545 """Rotate bits to the left in-place.
3547 bits -- The number of bits to rotate by.
3548 start -- Start of slice to rotate. Defaults to 0.
3549 end -- End of slice to rotate. Defaults to self.len.
3551 Raises ValueError if bits < 0.
3555 raise Error(
"Cannot rotate an empty bitstring.")
3557 raise ValueError(
"Cannot rotate left by negative amount.")
3559 bits %= (end - start)
3562 lhs = self.
_slice(start, start + bits)
3566 def byteswap(self, fmt=None, start=None, end=None, repeat=True):
3567 """Change the endianness in-place. Return number of repeats of fmt done.
3569 fmt -- A compact structure string, an integer number of bytes or
3570 an iterable of integers. Defaults to 0, which byte reverses the
3572 start -- Start bit position, defaults to 0.
3573 end -- End bit position, defaults to self.
len.
3574 repeat -- If
True (the default) the byte swapping pattern
is repeated
3575 as much
as possible.
3579 if fmt
is None or fmt == 0:
3581 bytesizes = [(end - start) // 8]
3582 elif isinstance(fmt, numbers.Integral):
3584 raise ValueError(
"Improper byte length {0}.".format(fmt))
3586 elif isinstance(fmt, basestring):
3587 m = STRUCT_PACK_RE.match(fmt)
3589 raise ValueError(
"Cannot parse format string {0}.".format(fmt))
3591 formatlist = re.findall(STRUCT_SPLIT_RE, m.group(
'fmt'))
3594 for f
in formatlist:
3596 bytesizes.append(PACK_CODE_SIZE[f])
3598 bytesizes.extend([PACK_CODE_SIZE[f[-1]]] * int(f[:-1]))
3599 elif isinstance(fmt, collections.abc.Iterable):
3601 for bytesize
in bytesizes:
3602 if not isinstance(bytesize, numbers.Integral)
or bytesize < 0:
3603 raise ValueError(
"Improper byte length {0}.".format(bytesize))
3605 raise TypeError(
"Format must be an integer, string or iterable.")
3608 totalbitsize = 8 * sum(bytesizes)
3609 if not totalbitsize:
3616 finalbit = start + totalbitsize
3617 for patternend
in xrange(start + totalbitsize, finalbit + 1, totalbitsize):
3618 bytestart = patternend - totalbitsize
3619 for bytesize
in bytesizes:
3620 byteend = bytestart + bytesize * 8
3622 bytestart += bytesize * 8
3627 """Remove all bits, reset to zero length."""
3631 """Return a copy of the bitstring."""
3634 int = property(Bits._getint, Bits._setint,
3635 doc=
"""The bitstring as a two's complement signed int. Read and write.
3637 uint = property(Bits._getuint, Bits._setuint,
3638 doc="""The bitstring as a two's complement unsigned int. Read and write.
3640 float = property(Bits._getfloat, Bits._setfloat,
3641 doc="""The bitstring as a floating point number. Read and write.
3643 intbe = property(Bits._getintbe, Bits._setintbe,
3644 doc="""The bitstring as a two's complement big-endian signed int. Read and write.
3646 uintbe = property(Bits._getuintbe, Bits._setuintbe,
3647 doc="""The bitstring as a two's complement big-endian unsigned int. Read and write.
3649 floatbe = property(Bits._getfloat, Bits._setfloat,
3650 doc="""The bitstring as a big-endian floating point number. Read and write.
3652 intle = property(Bits._getintle, Bits._setintle,
3653 doc="""The bitstring as a two's complement little-endian signed int. Read and write.
3655 uintle = property(Bits._getuintle, Bits._setuintle,
3656 doc="""The bitstring as a two's complement little-endian unsigned int. Read and write.
3658 floatle = property(Bits._getfloatle, Bits._setfloatle,
3659 doc="""The bitstring as a little-endian floating point number. Read and write.
3661 intne = property(Bits._getintne, Bits._setintne,
3662 doc="""The bitstring as a two's complement native-endian signed int. Read and write.
3664 uintne = property(Bits._getuintne, Bits._setuintne,
3665 doc="""The bitstring as a two's complement native-endian unsigned int. Read and write.
3667 floatne = property(Bits._getfloatne, Bits._setfloatne,
3668 doc="""The bitstring as a native-endian floating point number. Read and write.
3670 ue = property(Bits._getue, Bits._setue,
3671 doc="""The bitstring as an unsigned exponential-Golomb code. Read and write.
3673 se = property(Bits._getse, Bits._setse,
3674 doc="""The bitstring as a signed exponential-Golomb code. Read and write.
3676 uie = property(Bits._getuie, Bits._setuie,
3677 doc="""The bitstring as an unsigned interleaved exponential-Golomb code. Read and write.
3679 sie = property(Bits._getsie, Bits._setsie,
3680 doc="""The bitstring as a signed interleaved exponential-Golomb code. Read and write.
3682 hex = property(Bits._gethex, Bits._sethex,
3683 doc="""The bitstring as a hexadecimal string. Read and write.
3685 bin = property(Bits._getbin, Bits._setbin_safe,
3686 doc="""The bitstring as a binary string. Read and write.
3688 oct = property(Bits._getoct, Bits._setoct,
3689 doc="""The bitstring as an octal string. Read and write.
3691 bool = property(Bits._getbool, Bits._setbool,
3692 doc="""The bitstring as a bool (True or False). Read and write.
3694 bytes = property(Bits._getbytes, Bits._setbytes_safe,
3695 doc="""The bitstring as a ordinary string. Read and write.
3699class ConstBitStream(Bits):
3700 """A container or stream holding an immutable sequence of bits.
3702 For a mutable container use the BitStream class instead.
3704 Methods inherited
from Bits:
3706 all() -- Check
if all specified bits are set to 1
or 0.
3707 any() -- Check
if any of specified bits are set to 1
or 0.
3708 count() -- Count the number of bits set to 1
or 0.
3709 cut() -- Create generator of constant sized chunks.
3710 endswith() -- Return whether the bitstring ends
with a sub-string.
3711 find() -- Find a sub-bitstring
in the current bitstring.
3712 findall() -- Find all occurrences of a sub-bitstring
in the current bitstring.
3713 join() -- Join bitstrings together using current bitstring.
3714 rfind() -- Seek backwards to find a sub-bitstring.
3715 split() -- Create generator of chunks split by a delimiter.
3716 startswith() -- Return whether the bitstring starts
with a sub-bitstring.
3717 tobytes() -- Return bitstring
as bytes, padding
if needed.
3718 tofile() -- Write bitstring to file, padding
if needed.
3719 unpack() -- Interpret bits using format string.
3723 bytealign() -- Align to next byte boundary.
3724 peek() -- Peek at
and interpret next bits
as a single item.
3725 peeklist() -- Peek at
and interpret next bits
as a list of items.
3726 read() -- Read
and interpret next bits
as a single item.
3727 readlist() -- Read
and interpret next bits
as a list of items.
3731 Also available are the operators [], ==, !=, +, *, ~, <<, >>, &, |, ^.
3735 bin -- The bitstring
as a binary string.
3736 bool -- For single bit bitstrings, interpret
as True or False.
3737 bytepos -- The current byte position
in the bitstring.
3738 bytes -- The bitstring
as a bytes object.
3739 float -- Interpret
as a floating point number.
3740 floatbe -- Interpret
as a big-endian floating point number.
3741 floatle -- Interpret
as a little-endian floating point number.
3742 floatne -- Interpret
as a native-endian floating point number.
3743 hex -- The bitstring
as a hexadecimal string.
3744 int -- Interpret
as a two
's complement signed integer.
3745 intbe -- Interpret as a big-endian signed integer.
3746 intle -- Interpret
as a little-endian signed integer.
3747 intne -- Interpret
as a native-endian signed integer.
3748 len -- Length of the bitstring
in bits.
3749 oct -- The bitstring
as an octal string.
3750 pos -- The current bit position
in the bitstring.
3751 se -- Interpret
as a signed exponential-Golomb code.
3752 ue -- Interpret
as an unsigned exponential-Golomb code.
3753 sie -- Interpret
as a signed interleaved exponential-Golomb code.
3754 uie -- Interpret
as an unsigned interleaved exponential-Golomb code.
3755 uint -- Interpret
as a two
's complement unsigned integer.
3756 uintbe -- Interpret as a big-endian unsigned integer.
3757 uintle -- Interpret
as a little-endian unsigned integer.
3758 uintne -- Interpret
as a native-endian unsigned integer.
3762 __slots__ = ('_pos')
3764 def __init__(self, auto=None, length=None, offset=None, **kwargs):
3765 """Either specify an 'auto' initialiser:
3766 auto -- a string of comma separated tokens, an integer, a file object,
3767 a bytearray, a boolean iterable or another bitstring.
3769 Or initialise via **kwargs
with one (
and only one) of:
3770 bytes -- raw data
as a string,
for example read
from a binary file.
3771 bin -- binary string representation, e.g.
'0b001010'.
3772 hex -- hexadecimal string representation, e.g.
'0x2ef'
3773 oct -- octal string representation, e.g.
'0o777'.
3774 uint -- an unsigned integer.
3775 int -- a signed integer.
3776 float -- a floating point number.
3777 uintbe -- an unsigned big-endian whole byte integer.
3778 intbe -- a signed big-endian whole byte integer.
3779 floatbe - a big-endian floating point number.
3780 uintle -- an unsigned little-endian whole byte integer.
3781 intle -- a signed little-endian whole byte integer.
3782 floatle -- a little-endian floating point number.
3783 uintne -- an unsigned native-endian whole byte integer.
3784 intne -- a signed native-endian whole byte integer.
3785 floatne -- a native-endian floating point number.
3786 se -- a signed exponential-Golomb code.
3787 ue -- an unsigned exponential-Golomb code.
3788 sie -- a signed interleaved exponential-Golomb code.
3789 uie -- an unsigned interleaved exponential-Golomb code.
3790 bool -- a boolean (
True or False).
3791 filename -- a file which will be opened
in binary read-only mode.
3793 Other keyword arguments:
3794 length -- length of the bitstring
in bits,
if needed
and appropriate.
3795 It must be supplied
for all integer
and float initialisers.
3796 offset -- bit offset to the data. These offset bits are
3797 ignored
and this
is intended
for use when
3798 initialising using
'bytes' or 'filename'.
3803 def __new__(cls, auto=None, length=None, offset=None, **kwargs):
3804 x = super(ConstBitStream, cls).__new__(cls)
3805 x._initialise(auto, length, offset, **kwargs)
3809 """Move to absolute byte-aligned position in stream."""
3813 """Return the current position in the stream in bytes. Must be byte aligned."""
3819 """Move to absolute postion bit in bitstream."""
3821 raise ValueError(
"Bit position cannot be negative.")
3823 raise ValueError(
"Cannot seek past the end of the data.")
3827 """Return the current position in the stream in bits."""
3835 """Return a new copy of the ConstBitStream for the copy module."""
3845 """Concatenate bitstrings and return new bitstring.
3847 bs -- the bitstring to append.
3850 s = Bits.__add__(self, bs)
3855 """Interpret next bits according to the format string and return result.
3857 fmt -- Token string describing how to interpret the next bits.
3859 Token examples: 'int:12' : 12 bits
as a signed integer
3860 'uint:8' : 8 bits
as an unsigned integer
3861 'float:64' : 8 bytes
as a big-endian float
3862 'intbe:16' : 2 bytes
as a big-endian signed integer
3863 'uintbe:16' : 2 bytes
as a big-endian unsigned integer
3864 'intle:32' : 4 bytes
as a little-endian signed integer
3865 'uintle:32' : 4 bytes
as a little-endian unsigned integer
3866 'floatle:64': 8 bytes
as a little-endian float
3867 'intne:24' : 3 bytes
as a native-endian signed integer
3868 'uintne:24' : 3 bytes
as a native-endian unsigned integer
3869 'floatne:32': 4 bytes
as a native-endian float
3870 'hex:80' : 80 bits
as a hex string
3871 'oct:9' : 9 bits
as an octal string
3872 'bin:1' : single bit binary string
3873 'ue' : next bits
as unsigned exp-Golomb code
3874 'se' : next bits
as signed exp-Golomb code
3875 'uie' : next bits
as unsigned interleaved exp-Golomb code
3876 'sie' : next bits
as signed interleaved exp-Golomb code
3877 'bits:5' : 5 bits
as a bitstring
3878 'bytes:10' : 10 bytes
as a bytes object
3879 'bool' : 1 bit
as a bool
3880 'pad:3' : 3 bits of padding to ignore - returns
None
3882 fmt may also be an integer, which will be treated like the
'bits' token.
3884 The position
in the bitstring
is advanced to after the read items.
3886 Raises ReadError
if not enough bits are available.
3887 Raises ValueError
if the format
is not understood.
3890 if isinstance(fmt, numbers.Integral):
3892 raise ValueError(
"Cannot read negative amount.")
3894 raise ReadError(
"Cannot read {0} bits, only {1} available.",
3903 raise ValueError(
"Format string should be a single token, not {0} "
3904 "tokens - use readlist() instead.".format(len(token)))
3905 name, length, _ = token[0]
3912 """Interpret next bits according to format string(s) and return list.
3914 fmt -- A single string or list of strings
with comma separated tokens
3915 describing how to interpret the next bits
in the bitstring. Items
3916 can also be integers,
for reading new bitstring of the given length.
3917 kwargs -- A dictionary
or keyword-value pairs - the keywords used
in the
3918 format string will be replaced
with their given value.
3920 The position
in the bitstring
is advanced to after the read items.
3922 Raises ReadError
is not enough bits are available.
3923 Raises ValueError
if the format
is not understood.
3925 See the docstring
for 'read' for token examples.
'pad' tokens are skipped
3926 and not added to the returned list.
3928 >>> h, b1, b2 = s.readlist(
'hex:20, bin:5, bin:3')
3929 >>> i, bs1, bs2 = s.readlist([
'uint:12', 10, 10])
3936 """Read up to and including next occurrence of bs and return result.
3938 bs -- The bitstring to find. An integer is not permitted.
3939 bytealigned -- If
True the bitstring will only be
3940 found on byte boundaries.
3942 Raises ValueError
if bs
is empty.
3943 Raises ReadError
if bs
is not found.
3946 if isinstance(bs, numbers.Integral):
3947 raise ValueError(
"Integers cannot be searched for")
3950 p = self.
find(bs, self.
_pos_pos, bytealigned=bytealigned)
3957 """Interpret next bits according to format string and return result.
3959 fmt -- Token string describing how to interpret the next bits.
3961 The position in the bitstring
is not changed. If
not enough bits are
3962 available then all bits to the end of the bitstring will be used.
3964 Raises ReadError
if not enough bits are available.
3965 Raises ValueError
if the format
is not understood.
3967 See the docstring
for 'read' for token examples.
3971 value = self.read(fmt)
3976 """Interpret next bits according to format string(s) and return list.
3978 fmt -- One or more strings
with comma separated tokens describing
3979 how to interpret the next bits
in the bitstring.
3980 kwargs -- A dictionary
or keyword-value pairs - the keywords used
in the
3981 format string will be replaced
with their given value.
3983 The position
in the bitstring
is not changed. If
not enough bits are
3984 available then all bits to the end of the bitstring will be used.
3986 Raises ReadError
if not enough bits are available.
3987 Raises ValueError
if the format
is not understood.
3989 See the docstring
for 'read' for token examples.
3993 return_values = self.readlist(fmt, **kwargs)
3995 return return_values
3998 """Align to next byte and return number of skipped bits.
4000 Raises ValueError if the end of the bitstring
is reached before
4001 aligning to the next byte.
4004 skipped = (8 - (self._pos_pos % 8)) % 8
4009 pos = property(_getbitpos, _setbitpos,
4010 doc=
"""The position in the bitstring in bits. Read and write.
4012 bitpos = property(_getbitpos, _setbitpos,
4013 doc="""The position in the bitstring in bits. Read and write.
4015 bytepos = property(_getbytepos, _setbytepos,
4016 doc="""The position in the bitstring in bytes. Read and write.
4020class BitStream(ConstBitStream, BitArray):
4021 """A container or stream holding a mutable sequence of bits
4023 Subclass of the ConstBitStream and BitArray classes. Inherits all of
4028 all() -- Check
if all specified bits are set to 1
or 0.
4029 any() -- Check
if any of specified bits are set to 1
or 0.
4030 append() -- Append a bitstring.
4031 bytealign() -- Align to next byte boundary.
4032 byteswap() -- Change byte endianness
in-place.
4033 count() -- Count the number of bits set to 1
or 0.
4034 cut() -- Create generator of constant sized chunks.
4035 endswith() -- Return whether the bitstring ends
with a sub-string.
4036 find() -- Find a sub-bitstring
in the current bitstring.
4037 findall() -- Find all occurrences of a sub-bitstring
in the current bitstring.
4038 insert() -- Insert a bitstring.
4039 invert() -- Flip bit(s) between one
and zero.
4040 join() -- Join bitstrings together using current bitstring.
4041 overwrite() -- Overwrite a section
with a new bitstring.
4042 peek() -- Peek at
and interpret next bits
as a single item.
4043 peeklist() -- Peek at
and interpret next bits
as a list of items.
4044 prepend() -- Prepend a bitstring.
4045 read() -- Read
and interpret next bits
as a single item.
4046 readlist() -- Read
and interpret next bits
as a list of items.
4047 replace() -- Replace occurrences of one bitstring
with another.
4048 reverse() -- Reverse bits
in-place.
4049 rfind() -- Seek backwards to find a sub-bitstring.
4050 rol() -- Rotate bits to the left.
4051 ror() -- Rotate bits to the right.
4052 set() -- Set bit(s) to 1
or 0.
4053 split() -- Create generator of chunks split by a delimiter.
4054 startswith() -- Return whether the bitstring starts
with a sub-bitstring.
4055 tobytes() -- Return bitstring
as bytes, padding
if needed.
4056 tofile() -- Write bitstring to file, padding
if needed.
4057 unpack() -- Interpret bits using format string.
4061 Mutating operators are available: [], <<=, >>=, +=, *=, &=, |=
and ^=
4062 in addition to [], ==, !=, +, *, ~, <<, >>, &, |
and ^.
4066 bin -- The bitstring
as a binary string.
4067 bool -- For single bit bitstrings, interpret
as True or False.
4068 bytepos -- The current byte position
in the bitstring.
4069 bytes -- The bitstring
as a bytes object.
4070 float -- Interpret
as a floating point number.
4071 floatbe -- Interpret
as a big-endian floating point number.
4072 floatle -- Interpret
as a little-endian floating point number.
4073 floatne -- Interpret
as a native-endian floating point number.
4074 hex -- The bitstring
as a hexadecimal string.
4075 int -- Interpret
as a two
's complement signed integer.
4076 intbe -- Interpret as a big-endian signed integer.
4077 intle -- Interpret
as a little-endian signed integer.
4078 intne -- Interpret
as a native-endian signed integer.
4079 len -- Length of the bitstring
in bits.
4080 oct -- The bitstring
as an octal string.
4081 pos -- The current bit position
in the bitstring.
4082 se -- Interpret
as a signed exponential-Golomb code.
4083 ue -- Interpret
as an unsigned exponential-Golomb code.
4084 sie -- Interpret
as a signed interleaved exponential-Golomb code.
4085 uie -- Interpret
as an unsigned interleaved exponential-Golomb code.
4086 uint -- Interpret
as a two
's complement unsigned integer.
4087 uintbe -- Interpret as a big-endian unsigned integer.
4088 uintle -- Interpret
as a little-endian unsigned integer.
4089 uintne -- Interpret
as a native-endian unsigned integer.
4098 def __init__(self, auto=None, length=None, offset=None, **kwargs):
4099 """Either specify an 'auto' initialiser:
4100 auto -- a string of comma separated tokens, an integer, a file object,
4101 a bytearray, a boolean iterable or another bitstring.
4103 Or initialise via **kwargs
with one (
and only one) of:
4104 bytes -- raw data
as a string,
for example read
from a binary file.
4105 bin -- binary string representation, e.g.
'0b001010'.
4106 hex -- hexadecimal string representation, e.g.
'0x2ef'
4107 oct -- octal string representation, e.g.
'0o777'.
4108 uint -- an unsigned integer.
4109 int -- a signed integer.
4110 float -- a floating point number.
4111 uintbe -- an unsigned big-endian whole byte integer.
4112 intbe -- a signed big-endian whole byte integer.
4113 floatbe - a big-endian floating point number.
4114 uintle -- an unsigned little-endian whole byte integer.
4115 intle -- a signed little-endian whole byte integer.
4116 floatle -- a little-endian floating point number.
4117 uintne -- an unsigned native-endian whole byte integer.
4118 intne -- a signed native-endian whole byte integer.
4119 floatne -- a native-endian floating point number.
4120 se -- a signed exponential-Golomb code.
4121 ue -- an unsigned exponential-Golomb code.
4122 sie -- a signed interleaved exponential-Golomb code.
4123 uie -- an unsigned interleaved exponential-Golomb code.
4124 bool -- a boolean (
True or False).
4125 filename -- a file which will be opened
in binary read-only mode.
4127 Other keyword arguments:
4128 length -- length of the bitstring
in bits,
if needed
and appropriate.
4129 It must be supplied
for all integer
and float initialisers.
4130 offset -- bit offset to the data. These offset bits are
4131 ignored
and this
is intended
for use when
4132 initialising using
'bytes' or 'filename'.
4137 if not isinstance(self.
_datastore, ByteStore):
4140 def __new__(cls, auto=None, length=None, offset=None, **kwargs):
4141 x = super(BitStream, cls).__new__(cls)
4142 x._initialise(auto, length, offset, **kwargs)
4146 """Return a new copy of the BitStream."""
4149 if not isinstance(self.
_datastore, ByteStore):
4160 """Prepend a bitstring to the current bitstring.
4162 bs -- The bitstring to prepend.
4170def pack(fmt, *values, **kwargs):
4171 """Pack the values according to the format string and return a new BitStream.
4173 fmt -- A single string or a list of strings
with comma separated tokens
4174 describing how to create the BitStream.
4175 values -- Zero
or more values to pack according to the format.
4176 kwargs -- A dictionary
or keyword-value pairs - the keywords used
in the
4177 format string will be replaced
with their given value.
4179 Token examples:
'int:12' : 12 bits
as a signed integer
4180 'uint:8' : 8 bits
as an unsigned integer
4181 'float:64' : 8 bytes
as a big-endian float
4182 'intbe:16' : 2 bytes
as a big-endian signed integer
4183 'uintbe:16' : 2 bytes
as a big-endian unsigned integer
4184 'intle:32' : 4 bytes
as a little-endian signed integer
4185 'uintle:32' : 4 bytes
as a little-endian unsigned integer
4186 'floatle:64': 8 bytes
as a little-endian float
4187 'intne:24' : 3 bytes
as a native-endian signed integer
4188 'uintne:24' : 3 bytes
as a native-endian unsigned integer
4189 'floatne:32': 4 bytes
as a native-endian float
4190 'hex:80' : 80 bits
as a hex string
4191 'oct:9' : 9 bits
as an octal string
4192 'bin:1' : single bit binary string
4193 'ue' /
'uie': next bits
as unsigned exp-Golomb code
4194 'se' /
'sie': next bits
as signed exp-Golomb code
4195 'bits:5' : 5 bits
as a bitstring object
4196 'bytes:10' : 10 bytes
as a bytes object
4197 'bool' : 1 bit
as a bool
4198 'pad:3' : 3 zero bits
as padding
4200 >>> s = pack(
'uint:12, bits', 100,
'0xffe')
4201 >>> t = pack([
'bits',
'bin:3'], s,
'111')
4202 >>> u = pack(
'uint:8=a, uint:8=b, uint:55=a', a=6, b=44)
4206 if isinstance(fmt, basestring):
4210 _, tkns =
tokenparser(f_item, tuple(sorted(kwargs.keys())))
4212 except ValueError
as e:
4214 value_iter = iter(values)
4217 for name, length, value
in tokens:
4220 value = kwargs[value]
4222 if length
in kwargs:
4223 length = kwargs[length]
4225 if name
in kwargs
and length
is None and value
is None:
4226 s.append(kwargs[name])
4228 if length
is not None:
4229 length = int(length)
4230 if value
is None and name !=
'pad':
4232 value = next(value_iter)
4233 s._append(BitStream._init_with_token(name, length, value))
4234 except StopIteration:
4235 raise CreationError(
"Not enough parameters present to pack according to the "
4236 "format. {0} values are needed.", len(tokens))
4239 except StopIteration:
4242 raise CreationError(
"Too many parameters present to pack according to the format.")
4247BitString = BitStream
4249__all__ = [
'ConstBitArray',
'ConstBitStream',
'BitStream',
'BitArray',
4250 'Bits',
'BitString',
'pack',
'Error',
'ReadError',
4251 'InterpretError',
'ByteAlignError',
'CreationError',
'bytealigned']
def byteswap(self, fmt=None, start=None, end=None, repeat=True)
def ror(self, bits, start=None, end=None)
def reverse(self, start=None, end=None)
def replace(self, old, new, start=None, end=None, count=None, bytealigned=None)
def invert(self, pos=None)
def overwrite(self, bs, pos=None)
def __delitem__(self, key)
def set(self, value, pos=None)
def insert(self, bs, pos=None)
def rol(self, bits, start=None, end=None)
def __init__(self, auto=None, length=None, offset=None, **kwargs)
def __setitem__(self, key, value)
def __init__(self, auto=None, length=None, offset=None, **kwargs)
def __contains__(self, bs)
def _readuint(self, length, start)
def _setauto(self, s, length, offset)
def _readint(self, length, start)
def _truncatestart(self, bits)
def _readbits(self, length, start)
def _readoct(self, length, start)
def _findregex(self, reg_ex, start, end, bytealigned)
def _initialise_from_auto(self, auto, length, offset)
def _setbytes_safe(self, data, length=None, offset=0)
def _readintbe(self, length, start)
def _setbin_safe(self, binstring)
def find(self, bs, start=None, end=None, bytealigned=None)
def findall(self, bs, start=None, end=None, count=None, bytealigned=None)
def _setfile(self, filename, length, offset)
def _readintle(self, length, start)
def _readfloat(self, length, start)
def _truncateend(self, bits)
def _setintbe(self, intbe, length=None)
def _reversebytes(self, start, end)
def _readhex(self, length, start)
def _validate_slice(self, start, end)
def rfind(self, bs, start=None, end=None, bytealigned=None)
def _setbin_unsafe(self, binstring)
def _findbytes(self, bytes_, start, end, bytealigned)
def cut(self, bits, start=None, end=None, count=None)
def _readbytes(self, length, start)
def _setoct(self, octstring)
def split(self, delimiter, start=None, end=None, count=None, bytealigned=None)
def any(self, value, pos=None)
def _readfloatle(self, length, start)
def __getitem__(self, key)
def _delete(self, bits, pos)
def startswith(self, prefix, start=None, end=None)
def _sethex(self, hexstring)
def _slice(self, start, end)
def _readbin(self, length, start)
def _insert(self, bs, pos)
def _setuintbe(self, uintbe, length=None)
def endswith(self, suffix, start=None, end=None)
def _ensureinmemory(self)
def _readuintbe(self, length, start)
def _readlist(self, fmt, pos, **kwargs)
def all(self, value, pos=None)
def __init__(self, auto=None, length=None, offset=None, **kwargs)
def _converttobitstring(cls, bs, offset=0, cache=None)
def _inplace_logical_helper(self, bs, f)
def _readuintle(self, length, start)
def _setuint(self, uint, length=None)
def _setint(self, int_, length=None)
def _overwrite(self, bs, pos)
def _readtoken(self, name, pos, length)
def _setbytes_unsafe(self, data, length, offset)
def _setbitpos(self, pos)
def readto(self, bs, bytealigned=None)
def peeklist(self, fmt, **kwargs)
def readlist(self, fmt, **kwargs)
def _setbytepos(self, bytepos)
def __init__(self, auto=None, length=None, offset=None, **kwargs)
def getbyteslice(self, start, end)
def _prependstore(self, store)
def _appendstore(self, store)
def __init__(self, data, bitlength=None, offset=None)
def tokenparser(fmt, keys=None, token_cache=None)
def offsetcopy(s, newoffset)