Python bitstring.BitArray() Examples
The following are 30
code examples of bitstring.BitArray().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
bitstring
, or try the search function
.
Example #1
Source File: resource_manager.py From voltha with Apache License 2.0 | 6 votes |
def _format_resource(self, pon_intf_id, start_idx, end_idx): """ Format resource as json. :param pon_intf_id: OLT PON interface id :param start_idx: start index for id pool :param end_idx: end index for id pool :return dictionary: resource formatted as dictionary """ # Format resource as json to be stored in backend store resource = dict() resource[PONResourceManager.PON_INTF_ID] = pon_intf_id resource[PONResourceManager.START_IDX] = start_idx resource[PONResourceManager.END_IDX] = end_idx # resource pool stored in backend store as binary string resource[PONResourceManager.POOL] = BitArray(end_idx).bin return json.dumps(resource)
Example #2
Source File: h.py From knob with MIT License | 6 votes |
def rotate(key): """"Each Byte is rotated 3 positions on the left (not shifted).""" assert len(key) == Ar_KEY_LEN+1 and type(key) == bytearray rotated_key = bytearray() for i in range(0, 17): byte = BitArray(key[i:i+1]) assert len(byte.bin) == 8 # log.debug('rotate {} byte: {}, {}'.format(i, byte.bin, byte.uint)) # rotated_byte = byte << 3 rotated_byte = byte rotated_byte.rol(3) assert len(rotated_byte.bin) == 8 # log.debug('rotate {} rotated_byte: {}, {}'.format(i, rotated_byte.bin, rotated_byte.uint)) # NOTE: byte.uint is unsigned, byte.int is signed rotated_key.append(rotated_byte.uint) # log.debug('rotate rotated_key: {}'.format(repr(rotated_key))) assert len(rotated_key) == Ar_KEY_LEN+1 return rotated_key
Example #3
Source File: dcc_general_packet.py From dccpi with GNU General Public License v3.0 | 6 votes |
def to_bit_array(self): """ Builds a single string that should end up being serialized. Returns an array of True/False """ packet = BitArray() packet.append(self.preamble) packet.append(self.packet_start_bit) packet.append(self.address_byte) for byte in self.data_bytes: packet.append(self.data_byte_start_bit) packet.append(byte) packet.append(self.packet_end_bit) return map(int, packet)
Example #4
Source File: sort_rows.py From dataflows with MIT License | 6 votes |
def __call__(self, row): context = row.copy() for key, value in row.items(): # We need to stringify some types to make them properly comparable if key in self.key_list: # numbers # https://www.h-schmidt.net/FloatConverter/IEEE754.html if isinstance(value, (int, float, decimal.Decimal)): bits = BitArray(float=value, length=64) # invert the sign bit bits.invert(0) # invert negative numbers if value < 0: bits.invert(range(1, 64)) context[key] = bits.hex return self.key_spec.format(**context)
Example #5
Source File: dcc_general_packet.py From dccpi with GNU General Public License v3.0 | 6 votes |
def __init__(self, address_byte, data_bytes=[]): """ All arguments simle binary/hex strings: 0xFF 0b2121 """ # A command station must send a minimum of 14 preamble bits self.preamble = BitArray('0b1111111111111111') self.packet_start_bit = BitArray('0b0') self.address_byte = BitArray(address_byte) self.data_byte_start_bit = BitArray('0b0') self.data_bytes = map(BitArray, data_bytes) if sys.version_info.major >= 3: self.data_bytes = list(self.data_bytes) self.packet_end_bit = BitArray('0b1') assert(len(self.address_byte) == 8) for byte in self.data_bytes: assert(len(byte) == 8)
Example #6
Source File: ambe_bridge.py From dmr_utils with GNU General Public License v3.0 | 6 votes |
def __encode_voice_header( self, _rx_slot, _sync, _dtype ): _src_id = _rx_slot.rf_src _dst_id = _rx_slot.dst_id _cc = _rx_slot.cc # create lc lc = '\x00\x00\x00' + _dst_id + _src_id # PF + Reserved + FLCO + FID + Service Options + Group Address + Source Address # encode lc into info full_lc_encode = bptc.encode_header_lc(lc) _rx_slot.emblc = bptc.encode_emblc(lc) # save off the emb lc for voice frames B-E _rx_slot.emblc[5] = bitarray(32) # NULL message (F) # create slot_type slot_type = chr((_cc << 4) | (_dtype & 0x0f)) # data type is Header or Term # generate FEC for slot type slot_with_fec = BitArray(uint=golay.encode_2087(slot_type), length=20) # construct final frame - info[0:98] + slot_type[0:10] + DMR_DATA_SYNC_MS + slot_type[10:20] + info[98:196] frame_bits = full_lc_encode[0:98] + slot_with_fec[0:10] + decode.to_bits(_sync) + slot_with_fec[10:20] + full_lc_encode[98:196] return decode.to_bytes(frame_bits) # Create a voice header DMR frame
Example #7
Source File: ambe_bridge.py From dmr_utils with GNU General Public License v3.0 | 6 votes |
def send_voice72(self, _rx_slot, _ambe): ambe72_1 = BitArray('0x' + ahex(_ambe[0:9]))[0:72] ambe72_2 = BitArray('0x' + ahex(_ambe[9:18]))[0:72] ambe72_3 = BitArray('0x' + ahex(_ambe[18:27]))[0:72] ambe49_1 = ambe_utils.convert72BitTo49BitAMBE(ambe72_1) ambe49_2 = ambe_utils.convert72BitTo49BitAMBE(ambe72_2) ambe49_3 = ambe_utils.convert72BitTo49BitAMBE(ambe72_3) ambe49_1.append(False) ambe49_2.append(False) ambe49_3.append(False) ambe = ambe49_1 + ambe49_2 + ambe49_3 _frame = self._tempVoice[_rx_slot.vf][:33] + ambe.tobytes() + self._tempVoice[_rx_slot.vf][52:] # Insert the 3 49 bit AMBE frames self.rewriteFrame(_frame, _rx_slot.slot, _rx_slot.dst_id, _rx_slot.rf_src, _rx_slot.repeater_id) _rx_slot.vf = (_rx_slot.vf + 1) % 6 # the voice frame counter which is always mod 6 pass
Example #8
Source File: keychain.py From chia-blockchain with Apache License 2.0 | 6 votes |
def bytes_to_mnemonic(seed_bytes: bytes): seed_array = bytearray(seed_bytes) word_list = bip39_word_list().splitlines() checksum = bytes(std_hash(seed_bytes)) seed_array.append(checksum[0]) bytes_for_mnemonic = bytes(seed_array) bitarray = BitArray(bytes_for_mnemonic) mnemonics = [] for i in range(0, 24): start = i * 11 end = start + 11 bits = bitarray[start:end] m_word_poition = bits.uint m_word = word_list[m_word_poition] mnemonics.append(m_word) return mnemonics
Example #9
Source File: keychain.py From chia-blockchain with Apache License 2.0 | 6 votes |
def seed_from_mnemonic(mnemonic: List[str]): word_list = {word: i for i, word in enumerate(bip39_word_list().splitlines())} bit_array = BitArray() for i in range(0, 24): word = mnemonic[i] value = word_list[word] bit_array.append(BitArray(uint=value, length=11)) all_bytes = bit_array.bytes entropy_bytes = all_bytes[:32] checksum_bytes = all_bytes[32] checksum = std_hash(entropy_bytes) if checksum[0] != checksum_bytes: raise ValueError("Invalid order of mnemonic words") return entropy_bytes
Example #10
Source File: gen_cmd.py From credssp with MIT License | 6 votes |
def replaceKey(certFile, subPubKey): s = '' mainSection = 0 for i in open(certFile,'rt'): if '---' in i: mainSection=(not mainSection) elif mainSection: s+=i[:-1] cert = Certificate.decode(s.decode('base64')) arr = BitArray(bytes=subPubKey) cert['tbsCertificate']['subjectPublicKeyInfo']['subjectPublicKey'] = arr.bin bytes = cert.encode() st = bytes.encode('base64').replace('\n', '') print "-----BEGIN CERTIFICATE-----" for i in xrange(0,len(st), 64): print st[i:i+64] print "-----END CERTIFICATE-----"
Example #11
Source File: adtran_resource_manager.py From voltha with Apache License 2.0 | 6 votes |
def _format_resource(self, pon_intf_id, start_idx, end_idx): """ Format resource as json. :param pon_intf_id: OLT PON interface id :param start_idx: start index for id pool :param end_idx: end index for id pool :return dictionary: resource formatted as dictionary """ # Format resource as json to be stored in backend store resource = dict() resource[PONResourceManager.PON_INTF_ID] = pon_intf_id resource[PONResourceManager.START_IDX] = start_idx resource[PONResourceManager.END_IDX] = end_idx # resource pool stored in backend store as binary string resource[PONResourceManager.POOL] = BitArray(end_idx-start_idx).bin return json.dumps(resource)
Example #12
Source File: omci_entities.py From voltha with Apache License 2.0 | 6 votes |
def json_from_value(value): bits = BitArray(hex=hexlify(value)) temp = AdtnVlanTaggingOperation( filter_outer_priority=bits[0:4].uint, # 4 <-size filter_outer_vid=bits[4:17].uint, # 13 filter_outer_tpid_de=bits[17:20].uint, # 3 # pad 12 filter_inner_priority=bits[32:36].uint, # 4 filter_inner_vid=bits[36:49].uint, # 13 filter_inner_tpid_de=bits[49:52].uint, # 3 # pad 8 filter_ether_type=bits[60:64].uint, # 4 treatment_tags_to_remove=bits[64:66].uint, # 2 # pad 10 treatment_outer_priority=bits[76:80].uint, # 4 treatment_outer_vid=bits[80:93].uint, # 13 treatment_outer_tpid_de=bits[93:96].uint, # 3 # pad 12 treatment_inner_priority=bits[108:112].uint, # 4 treatment_inner_vid=bits[112:125].uint, # 13 treatment_inner_tpid_de=bits[125:128].uint, # 3 ) return json.dumps(temp.fields, separators=(',', ':'))
Example #13
Source File: omci_entities.py From voltha with Apache License 2.0 | 6 votes |
def json_from_value(value): bits = BitArray(hex=hexlify(value)) temp = VlanTaggingOperation( filter_outer_priority=bits[0:4].uint, # 4 <-size filter_outer_vid=bits[4:17].uint, # 13 filter_outer_tpid_de=bits[17:20].uint, # 3 # pad 12 filter_inner_priority=bits[32:36].uint, # 4 filter_inner_vid=bits[36:49].uint, # 13 filter_inner_tpid_de=bits[49:52].uint, # 3 # pad 8 filter_ether_type=bits[60:64].uint, # 4 treatment_tags_to_remove=bits[64:66].uint, # 2 # pad 10 treatment_outer_priority=bits[76:80].uint, # 4 treatment_outer_vid=bits[80:93].uint, # 13 treatment_outer_tpid_de=bits[93:96].uint, # 3 # pad 12 treatment_inner_priority=bits[108:112].uint, # 4 treatment_inner_vid=bits[112:125].uint, # 13 treatment_inner_tpid_de=bits[125:128].uint, # 3 ) return json.dumps(temp.fields, separators=(',', ':'))
Example #14
Source File: resource_manager.py From voltha with Apache License 2.0 | 6 votes |
def _get_resource(self, path): """ Get resource from kv store. :param path: path to get resource :return: resource if resource present in kv store else None """ # get resource from kv store result = self._kv_store.get_from_kv_store(path) if result is None: return result self._log.info("dumping resource", result=result) resource = result if resource is not None: # decode resource fetched from backend store to dictionary resource = json.loads(resource) # resource pool in backend store stored as binary string whereas to # access the pool to generate/release IDs it need to be converted # as BitArray resource[PONResourceManager.POOL] = \ BitArray('0b' + resource[PONResourceManager.POOL]) return resource
Example #15
Source File: instrs_riscv.py From angr-platforms with BSD 2-Clause "Simplified" License | 5 votes |
def fetch_operands(self): i = self.data['j'] parsed = "{0}{1}{2}{3}{4}{5}{6}{7}0".format(i[0],i[4],i[2:4], i[6], i[5], i[10], i[1], i[7:10]) val = self.constant(BitArray(bin=parsed).int, Type.int_32) return val.signed,
Example #16
Source File: instrs_riscv.py From angr-platforms with BSD 2-Clause "Simplified" License | 5 votes |
def get_imm(self): val = "{0}{1}00".format(self.data['i'][3:6],self.data['i'][0:3]) res = self.constant(BitArray(bin=val).uint, Type.int_32) return res.signed
Example #17
Source File: instrs_riscv.py From angr-platforms with BSD 2-Clause "Simplified" License | 5 votes |
def get_imm(self): val = BitArray(bin=self.data['i']).int return self.constant(val, Type.int_32)
Example #18
Source File: instrs_riscv.py From angr-platforms with BSD 2-Clause "Simplified" License | 5 votes |
def get_offset(self): begin = self.data['i'][0:4] middle = self.data['I'][1:7] x = self.data['i'][4] sign = self.data['I'][0] offset = "{3}{2}{1}{0}0".format(begin, middle, x, sign) b = BitArray(bin=offset) val = self.constant(b.int, Type.int_32) return val.signed
Example #19
Source File: instrs_riscv.py From angr-platforms with BSD 2-Clause "Simplified" License | 5 votes |
def get_addr(self): addr = self.get(int(self.data['s'], 2), Type.int_32) offset = BitArray(bin = '{0}{1}'.format(self.data['I'], self.data['i'])).int return addr+offset
Example #20
Source File: instrs_riscv.py From angr-platforms with BSD 2-Clause "Simplified" License | 5 votes |
def compute_result(self, sp): i = self.data['i'] immstr = '{0}{1}{2}{3}{4}0000'.format(self.data['I'], i[2:4], i[1], i[4], i[0]) result = sp + self.constant(BitArray(bin=immstr).int, Type.int_32).signed self.put(result, 2)
Example #21
Source File: instrs_riscv.py From angr-platforms with BSD 2-Clause "Simplified" License | 5 votes |
def get_shift_amount(self): num = BitArray(bin = self.data['I']).int; return self.constant(num, Type.int_8)
Example #22
Source File: instrs_riscv.py From angr-platforms with BSD 2-Clause "Simplified" License | 5 votes |
def get_imm(self): data = BitArray(bin="{0}{1}".format(self.data['i'],self.data['I'])).int return self.constant(data, Type.int_32)
Example #23
Source File: indexpool.py From voltha with Apache License 2.0 | 5 votes |
def __init__(self, max_entries, offset): self.max_entries = max_entries self.offset = offset self.indices = BitArray(self.max_entries)
Example #24
Source File: dcc_general_packet.py From dccpi with GNU General Public License v3.0 | 5 votes |
def from_bit_array(int_array): """ Given [1, 1,...] array try to decode a packet """ packet = BitArray(int_array) # preamble = packet[0:12] address_byte = packet[13:21] data_bytes = packet[22:-1] dbit = 0 data_bytes_a = [] while dbit < len(data_bytes): data_bytes_a.append(data_bytes[dbit:dbit+8]) dbit += 9 # skip start bit from next data byte return DCCGeneralPacket(address_byte, data_bytes_a)
Example #25
Source File: test_adtran_resource_manager.py From voltha with Apache License 2.0 | 5 votes |
def test__get_resource_returns_resources(resource_manager, resource_map, path, onu_id): resource_manager._kv_store.get_from_kv_store.return_value = json.dumps(resource_map) result = resource_manager._get_resource(path, onu_id) if 'alloc_id' in path: assert result[AdtranPONResourceManager.ONU_MAP]['1'][AdtranPONResourceManager.POOL] == BitArray('0b11111') else: assert result[AdtranPONResourceManager.POOL] == BitArray('0b1111')
Example #26
Source File: test_adtran_resource_manager.py From voltha with Apache License 2.0 | 5 votes |
def resource_bin(): return {'onu_map': {'1': {'end_idx': 6, 'start_idx': 1, 'pool': BitArray('0b11000')}, '2': {'end_idx': 5, 'start_idx': 1, 'pool': BitArray('0b1100')}}, 'pon_intf_id': 1}
Example #27
Source File: adtran_resource_manager.py From voltha with Apache License 2.0 | 5 votes |
def _format_map_resource(self, pon_intf_id, resource_map): """ Format resource as json. # TODO: Refactor the resource BitArray to be just a list of the resources. # This is used to store available alloc-id's on a per-onu/pon basis # which in BitArray string form, is a 768 byte string for just 4 possible # alloc-IDs. This equates to 1.57 MB of storage when you take into # account 128 ONUs and 16 PONs pre-provisioneed :param pon_intf_id: OLT PON interface id :param resource_map: (dict) ONU ID -> Scattered list of IDs :return dictionary: resource formatted as dictionary """ # Format resource as json to be stored in backend store resource = dict() resource[PONResourceManager.PON_INTF_ID] = pon_intf_id onu_dict = dict() for onu_id, resources in resource_map.items(): start_idx = min(resources) end_idx = max(resources) + 1 onu_dict[onu_id] = { PONResourceManager.START_IDX: start_idx, PONResourceManager.END_IDX: end_idx, } # Set non-allowed values as taken resource_map = BitArray(end_idx - start_idx) not_available = {pos for pos in xrange(end_idx-start_idx) if pos + start_idx not in resources} resource_map.set(True, not_available) onu_dict[onu_id][PONResourceManager.POOL] = resource_map.bin resource[AdtranPONResourceManager.ONU_MAP] = onu_dict return json.dumps(resource)
Example #28
Source File: dcc_packet_factory.py From dccpi with GNU General Public License v3.0 | 5 votes |
def function_group_one_packet(address, fl, fl1, fl2, fl3, fl4): address_bin = BitArray('uint:8=%d' % address) functions = [fl, fl4, fl3, fl2, fl1] instruction_bin = BitArray('0b100') for f in functions: if f: instruction_bin.append('0b1') else: instruction_bin.append('0b0') error = address_bin ^ instruction_bin data = [instruction_bin, error] return DCCGeneralPacket(address_bin, data)
Example #29
Source File: adtran_resource_manager.py From voltha with Apache License 2.0 | 5 votes |
def _get_resource(self, path, onu_id=None): """ Get resource from kv store. :param path: path to get resource :return: resource if resource present in kv store else None """ # get resource from kv store result = self._kv_store.get_from_kv_store(path) if result is None: return result self._log.info("dumping-resource", result=result) resource = result if resource is not None: # decode resource fetched from backend store to dictionary resource = json.loads(resource) if 'alloc_id' in path.lower(): assert onu_id is not None poolResource = resource[AdtranPONResourceManager.ONU_MAP][str(onu_id)] poolResource[PONResourceManager.POOL] = \ BitArray('0b' + poolResource[PONResourceManager.POOL]) else: # resource pool in backend store stored as binary string whereas to # access the pool to generate/release IDs it need to be converted # as BitArray resource[PONResourceManager.POOL] = \ BitArray('0b' + resource[PONResourceManager.POOL]) return resource
Example #30
Source File: instrs_riscv.py From angr-platforms with BSD 2-Clause "Simplified" License | 5 votes |
def compute_result(self, src1): imm = "{0}{1}{2}{3}{4}".format(self.data['O'][2:4], self.data['I'][1:3], self.data["O"][4], self.data['O'][0:2], self.data['I'][0]) offset = self.constant(BitArray(bin=imm).int, Type.int_32) addr = self.addr + offset self.jump(src1 != self.constant(0, Type.int_32), addr)