def setAccumulator(self,value) : """ setter to set the value of accumulator USE WITH CAUTION ONLY FOR TEST PURPOSES """ self.__AC = BitStream(int = value,length=40)
def concatenated_sms(data: str, length_bits: int = 8) -> Dict[str, Any]: io_data = BitStream(hex=data) return { 'reference': io_data.read(f'uintbe:{length_bits}'), 'parts_count': io_data.read('uintbe:8'), 'part_number': io_data.read('uintbe:8'), }
def decode(cls, pdu_data: StringIO) -> Dict[str, Any]: """ Decodes an incomming PDU header. >>> PDUHeader.decode(StringIO('44')) {'rp': False, 'udhi': True, 'sri': False, 'lp': False, 'mms': True, 'mti': 'deliver'} """ result = dict() io_data = BitStream(hex=pdu_data.read(2)) # Reply Path result['rp'] = io_data.read('bool') # User Data PDUHeader Indicator result['udhi'] = io_data.read('bool') # Status Report Indication result['sri'] = io_data.read('bool') io_data.pos += 1 # skips a bit # Loop Prevention result['lp'] = io_data.read('bool') # More Messages to Send result['mms'] = io_data.read('bool') # Message Type Indicator result['mti'] = cls.MTI.get(io_data.read('bits:2').uint) if result['mti'] is None: raise ValueError("Invalid Message Type Indicator") return result
def __init__(self, data, ts_packets): self.bytes = data first_ts = ts_packets[0] self.pid = first_ts.pid self.byte_offset = first_ts.byte_offset self.size = len(ts_packets) * TSPacket.SIZE self.random_access = first_ts.random_access_indicator self.ts_packets = ts_packets data = BitStream(data) start_code = data.read("uint:24") if start_code != 0x000001: raise Exception("packet_start_code_prefix is 0x{:06X} but should " "be 0x000001".format(start_code)) self.stream_id = data.read("uint:8") pes_packet_length = data.read("uint:16") if StreamID.has_pes_header(self.stream_id): bits = data.read("uint:2") if bits != 2: raise Exception("First 2 bits of a PES header should be 0x2 " "but saw 0x{:02X}'".format(bits)) self.pes_scrambling_control = data.read("uint:2") self.pes_priority = data.read("bool") self.data_alignment_indicator = data.read("bool") self.copyright = data.read("bool") self.original_or_copy = data.read("bool") pts_dts_flags = data.read("uint:2") escr_flag = data.read("bool") es_rate_flag = data.read("bool") dsm_trick_mode_flag = data.read("bool") additional_copy_info_flag = data.read("bool") pes_crc_flag = data.read("bool") pes_extension_flag = data.read("bool") pes_header_data_length = data.read("uint:8") if pts_dts_flags & 2: bits = data.read("uint:4") if bits != pts_dts_flags: raise Exception( "2 bits before PTS should be 0x{:02X} but saw 0x{" ":02X}".format(pts_dts_flags, bits)) self.pts = read_timestamp("PTS", data) if pts_dts_flags & 1: bits = data.read("uint:4") if bits != 0x1: raise Exception("2 bits before DTS should be 0x1 but saw " "0x{:02X}".format(bits)) self.dts = read_timestamp("DTS", data) # skip the rest of the header and stuffing bytes data.bytepos = pes_header_data_length + 9 if self.stream_id == StreamID.PADDING: self.payload = None else: self.payload = data.read("bytes")
def uncompress_golomb_coding(coded_bytes, hash_length, M): """Given a bytstream produced using golomb_coded_bytes, uncompress it.""" ret_list = [] instream = BitStream(bytes=coded_bytes, length=len(coded_bytes) * 8) hash_len_bits = hash_length * 8 m_bits = int(math.log(M, 2)) # First item is a full hash value. prev = instream.read("bits:%d" % hash_len_bits) ret_list.append(prev.tobytes()) while (instream.bitpos + m_bits) <= instream.length: # Read Unary-encoded value. read_prefix = 0 curr_bit = instream.read("uint:1") while curr_bit == 1: read_prefix += 1 curr_bit = instream.read("uint:1") assert curr_bit == 0 # Read r, assuming M bits were used to represent it. r = instream.read("uint:%d" % m_bits) curr_diff = read_prefix * M + r curr_value_int = prev.uint + curr_diff curr_value = Bits(uint=curr_value_int, length=hash_len_bits) ret_list.append(curr_value.tobytes()) prev = curr_value return ret_list
def pack(self, for_game=False): is_translated = False if not self.solution[common.editor_config.lang_trans] == "": is_translated = True # SAVE! output = BitStream() # Type flag if self.type == ANAGRAM_TYPE.Demo: output += DEMO_FLAG else: output += FULL_FLAG # Number of letters if is_translated: output += ConstBitStream(uintle=len( self.solution[common.editor_config.lang_trans]), length=16) else: output += ConstBitStream(uintle=len( self.solution[common.editor_config.lang_orig]), length=16) # Magic output += ANAGRAM_MAGIC # File indexes output += ConstBitStream(uintle=self.solution_index, length=16) output += ConstBitStream(uintle=self.extra_index, length=16) # Unknown output += self.__unknown # Shown/unshown if is_translated: num_letters = len(self.solution[common.editor_config.lang_trans]) output += self.pack_shown(self.easy, num_letters) output += self.pack_shown(self.normal, num_letters) if self.type == ANAGRAM_TYPE.Full: output += self.pack_shown(self.hard, num_letters) if not for_game: num_letters = len( self.solution[common.editor_config.lang_orig]) output += ConstBitStream(uintle=num_letters, length=16) if not is_translated or not for_game: # This shows up either way. num_letters = len(self.solution[common.editor_config.lang_orig]) output += self.pack_shown(self.easy_orig, num_letters) output += self.pack_shown(self.normal_orig, num_letters) if self.type == ANAGRAM_TYPE.Full: output += self.pack_shown(self.hard_orig, num_letters) return output
def main(): # Object of IAS class ias = IAS() # Object of Instruction cycles which will be executed by IAS program = InstructionCycle(ias) # Performs the corresponding number of program number = int(input("Please enter 1 OR 2: ")) if (number == 1): # Preprogram the memory for i in range(len(INSTRUCTIONS[0])): ias.memory.loc[i] = BitStream( bin=INSTRUCTIONS[0][i][0] + INSTRUCTIONS[0][i][1] + INSTRUCTIONS[0][i][2] + INSTRUCTIONS[0][i][3]) for i in range(len(DATA[0])): ias.memory.loc[501 + i] = BitStream(bin=DATA[0][i][0]) print(f"{Colors.BOLD}a = {ias.memory.loc[501].int}{Colors.ENDC}") print(f"{Colors.BOLD}b = {ias.memory.loc[502].int}{Colors.ENDC}") run(ias, program) print(f"{Colors.BOLD}c = {ias.memory.loc[503].int}{Colors.ENDC}") print(f"{Colors.BOLD}d = {ias.memory.loc[504].int}{Colors.ENDC}") elif (number == 2): # Preprogram the memory for i in range(len(INSTRUCTIONS[1])): ias.memory.loc[i] = BitStream( bin=INSTRUCTIONS[1][i][0] + INSTRUCTIONS[1][i][1] + INSTRUCTIONS[1][i][2] + INSTRUCTIONS[1][i][3]) for i in range(len(DATA[1])): ias.memory.loc[501 + i] = BitStream(bin=DATA[1][i][0]) print(f"{Colors.BOLD}M(501) = {ias.memory.loc[501].int}{Colors.ENDC}") print(f"{Colors.BOLD}M(502) = {ias.memory.loc[502].int}{Colors.ENDC}") print(f"{Colors.BOLD}M(503) = {ias.memory.loc[503].int}{Colors.ENDC}") print(f"{Colors.BOLD}M(504) = {ias.memory.loc[504].int}{Colors.ENDC}") print(f"{Colors.BOLD}M(505) = {ias.memory.loc[505].int}{Colors.ENDC}") run(ias, program) print(f"{Colors.BOLD}M(505) = {ias.memory.loc[505].int}{Colors.ENDC}") print(f"{Colors.BOLD}M(506) = {ias.memory.loc[506].bin}{Colors.ENDC}")
def pack_len(length, short=False): assert (length >= 0) if short: sz = 3 return BitStream(uint=length, length=sz) elif length < 2**8 - 1: return BitStream(uint=0, length=2) + BitStream(uint=length, length=8) elif length < 2**16 - 1: return BitStream(uint=1, length=2) + BitStream(uint=length, length=16) elif length < 2**32 - 1: return BitStream(uint=2, length=2) + BitStream(uint=length, length=32) elif length < 2**64 - 1: return BitStream(uint=3, length=2) + BitStream(uint=length, length=64)
def parse(self): """ Parse the bitstream and extract each NALU. Call the respective callbacks for each NALU type found. """ self._get_nalu_positions() for current_nalu_pos, next_nalu_pos in zip( self.nal_unit_positions, islice(self.nal_unit_positions, 1, None)): current_nalu_bytepos = int(current_nalu_pos / 8) next_nalu_bytepos = int(next_nalu_pos / 8) nalu_bytes = self.stream[current_nalu_pos:next_nalu_pos] self.__call('nalu', nalu_bytes) if self.verbose: print("") print( "========================================================================================================" ) print("") print("NALU bytepos:\t[" + str(current_nalu_bytepos) + ", " + str(next_nalu_bytepos - 1) + "]") print("NALU offset:\t" + str(current_nalu_bytepos) + " Bytes") print("NALU length:\t" + str(next_nalu_bytepos - current_nalu_bytepos) + " Bytes (including start code)") current_nalu_stream_segment = BitStream( self.stream[current_nalu_pos:next_nalu_pos]) nal_unit_type, rbsp_payload = self._decode_nalu( current_nalu_stream_segment) if self.verbose: print("NALU type:\t" + str(nal_unit_type) + " (" + nalutypes.get_description(nal_unit_type) + ")") print("NALU bytes:\t" + str(nalu_bytes)) print("NALU RBSP:\t" + str(rbsp_payload)) print("") if nal_unit_type == nalutypes.NAL_UNIT_TYPE_SPS: nalu_sps = nalutypes.SPS(rbsp_payload, self.verbose) self.__call('sps', rbsp_payload) elif nal_unit_type == nalutypes.NAL_UNIT_TYPE_PPS: nalu_pps = nalutypes.PPS(rbsp_payload, self.verbose) self.__call('pps', rbsp_payload) elif nal_unit_type == nalutypes.NAL_UNIT_TYPE_AUD: aud = nalutypes.AUD(rbsp_payload, self.verbose) self.__call('aud', rbsp_payload) elif nal_unit_type == nalutypes.NAL_UNIT_TYPE_CODED_SLICE_NON_IDR: nalu_slice = nalutypes.CodedSliceNonIDR( rbsp_payload, self.verbose) self.__call('slice', rbsp_payload) elif nal_unit_type == nalutypes.NAL_UNIT_TYPE_CODED_SLICE_IDR: nalu_slice = nalutypes.CodedSliceIDR(rbsp_payload, self.verbose) self.__call('slice', rbsp_payload)
def find_mapping(infile, outfile): stream = BitStream() with open(infile) as f: byte = f.read(1) while byte: if byte == '\x01': stream.append('0b1') else: stream.append('0b0') byte = f.read(1) iteration = 0 for mapping in bitmappings: stream.pos = 0 tmpstream = BitStream() for pie in stream.cut(2): index = pie.read('uint:2') tmpstream.append( BitArray(uint=mapping[index], length=2) ) # look for the start of the flag in the stream of bits pos = tmpstream.find(ZULU) if len(pos) > 0: # print("Found the bytes we are looking for on iteration {}".format(iteration)) # print("pos = {}".format(pos)) tmpstream <<= pos[0] % 8 data = tmpstream.tobytes() # print(data) with open(outfile, 'wb') as fh: fh.write(data) break else: # print("Did not find the header") iteration += 1
def jumpLeftCond(self): if (self.IAS.registers.AC.int >= 0): self.IAS.registers.PC = self.IAS.registers.MAR.int self.IAS.registers.IBR = BitStream(int=0, length=20) print( f"{Colors.OKGREEN}Executed JUMP + M({self.IAS.registers.MAR.int}, 0:19){Colors.ENDC}" )
def parse(version: int, stream: BitStream) -> "Value": value = BitStream() while True: group = stream.read(5) last_group = not group.read(1) value += group.read(4) if last_group: return Value(version=version, value=value.uint)
def __init__(self) -> None : ''' constructor for : memory, Accumualtor, ProgramCounter, MultiplierQuotient, MBR, MAR, IR and IBR. ''' self.memory = Memory() self.__PC = 0 #Program counter self.__AC = BitStream(int=0, length=40) #Accumulator Employed to hold temporarily operands and results of ALU operations. self.__MQ = BitStream(int=0, length=40) #Multiplier Quotient Employed to hold temporarily operands and results of ALU operations. self.__MBR = BitStream(int=0, length=40) #Contains a word to be stored in memory or sent to the I/O unit, or is used to receive a word from memory or from the I/O unit. self.__IR = BitStream(int=0, length=8) #Contains the 8-bit opcode instruction being executed. self.__MAR = BitStream(int=0, length=12) #Specifies the address in memory of the word to be written from or read into the MBR. self.__IBR = "" #Instruction Buffer. # the below dictionary contains all the operations of the IAS operator with their meanings. # the design is implented in such a way that the opcode gets decoded and compsred with the dictionary. # if the opcode matches a key-pair value the corresponding function gets implemented. self.operations = { '00000001': self.load, #00000001 LOAD M(X) Transfer M(X) to the accumulator '00001010': self.loadToAC, #00001010 LOAD MQ Transfer contents of register MQ to the accumulator AC '00001001': self.loadToMQ, #00001001 LOAD MQ,M(X) Transfer contents of memory location X to MQ '00000010': self.loadNegative, #00000010 LOAD -M(X) Transfer -M(X) to the accumulator '00000011': self.loadAbsolute, #00000011 LOAD |M(X)| Transfer absolute value of M(X) to the accumulator '00100001': self.store, #00100001 STOR M(X) Transfer contents of accumulator to memory location X '00000100': self.loadNegativeAbsolute, #00000100 LOAD -|M(X)| Transfer -|M(X)| to the accumulator '00000101': self.add, #00000101 ADDM(X) Add M(X) to AC; put the result in AC '00000111': self.addAbsolute, #00000111 ADD |M(X)| Add |M(X)| to AC; put the result in AC '00000110': self.sub, #00000110 SUB M(X) Subtract M(X) from AC; put the result in AC '00001000': self.subAbsolute, #00001000 SUB |M(X)| Subtract |M(X)| from AC; put the remainder in AC '00001011': self.multiply, #00001011 MUL M(X) Multiply M(X) by MQ; put most significant bits of resultin AC, put least significant bits in MQ '00001100': self.divide, #00001100 DIV M(X) Divide AC by M(X); put the quotient in MQ and the remainder in AC '00001001': self.loadToMQ, #LOAD MQ,M(X) Transfer contents of memory location X to MQ '00001010': self.loadToAC, #LOAD MQ Transfer contents of address MQ to the accumulator AC '00001101': self.jumpLeftInstruction, #00001101 JUMP M(X,0:19) Take next instruction from left half of M(X) '00001110': self.jumpRightInstruction, #00001110 JUMP M(X,20:39) Take next instruction from right half of M(X) '00001111': self.conditionalJumpLeft, #JUMP+M(X,0:19) If number in the accumulator is nonnegative, take next instruction from left half of M(X) '00010000': self.conditionalJumpRight, #JUMP+M(X,20:39) If number in the accumulator is nonnegative , take next instruction from right half of M(X) '00010100': self.leftShift, #00010100 LSH Multiply accumulator by 2; i.e., shift left one bit position '00010101': self.rightShift, #00010101 RSH Divide accumulator by 2; i.e., shift right one position '00010010': self.storeLeft, #00010010 STOR M(X,8:19) Replace left address field at M(X) by 12 rightmost bits of AC '00010011': self.storeRight, #00010011 STOR M(X,28:39) Replace right address field at M(X) by 12 rightmost bits of AC '00000000': self.halt, #00000000 HALT Halt all the ongoing operations }
def decode(lst): q = BitStream() temp = BitArray('0b0') for bit, count in lst: for i in xrange(count): temp.bool = bit q.append(temp) return q
def Rate(self): self.spi1.spi_xfer(self.h,self.Read_Rate) (count,st)=self.spi1.spi_xfer(self.h,self.Read_Rate) f2 = BitStream(bytes = st ) f2.pos= 11 a=f2.read(16).int a=a/80 return a
def testDecode(): nC = 0 # example 1 from [the Richardson Book] on page 214 stream = BitStream('0b000010001110010111101101') logging.debug('decoding CAVLC stream: %s', stream.bin) decode(stream, nC, 16) # example 2 from [the Richardson Book] on page 215 stream = BitStream('0b000000011010001001000010111001100') logging.debug('decoding CAVLC stream: %s', stream.bin) decode(stream, nC, 16) # example 3 from [the Richardson Book] on page 216 stream = BitStream('0b0001110001110010') logging.debug('decoding CAVLC stream: %s', stream.bin) decode(stream, nC, 16)
def pad_with_noise(text:BitStream, tgtlen:int)-> BitStream : """Raises ValueError when tgtlen < len(text).""" tgtlen -= len(text) bits = BitStream( length=tgtlen, uint=random.getrandbits(tgtlen), ) return text + bits
def loadNegAbsToAC(self): self.IAS.registers.MBR = self.IAS.memory.loc[ self.IAS.registers.MAR.int] self.IAS.registers.AC = BitStream(int=-abs(self.IAS.registers.MBR.int), length=40) print( f"{Colors.OKGREEN}Executed LOAD -|M({self.IAS.registers.MAR.int})|{Colors.ENDC}" )
def encoding16x16(block, QP): """ Encode a 16x16 macroblock Args: block: 16x16 matrix block QP: the QP value of quantization Returns: encoding of current macroblock """ size = block.shape step = 4 result = BitStream() # step1: Get the DC element of each 4x4 block DC_block = np.full((step, step), 0) for i in r_[:size[0]:step]: for j in r_[:size[1]:step]: x = int(i / step) y = int(j / step) DC_block[x][y] = block[i, j] # DC transorm coding logging.debug("16x16 block's DC transorm coding") dc_trans = tf.forwardHadamardAndScaling4x4(DC_block, QP) dc_code = cd.CAVLC(dc_trans) result.append(dc_code) # step2: 4x4 transform, quantization and coding current = np.full((step, step), 0) for m in r_[:size[0]:8]: for n in r_[:size[1]:8]: for i in r_[:8:step]: for j in r_[:8:step]: x = m + i y = n + j current = block[x:(x + step), y:(y + step)] logging.debug("4x4 block row %d column %d, pixel value:", x, y) logging.debug(current) temp = tf.forwardTransformAndScaling4x4(current, QP) logging.debug("coefficients:") logging.debug(temp) ac_code = cd.CAVLC(temp) result.append(ac_code) # step3: UV coding temp = encoding16x16UV(QP) # U result.append(temp) temp = encoding16x16UV(QP) # V result.append(temp) return result
def encode(data): stream = BitStream() stream.append('int:32=%d' % MAGIC_NUM) stream.append('int:8=%d' % data['version']) stream.append('int:8=%d' % data['command']) json_bytes = json.dumps(data).encode() stream.append('int:32=%d' % len(json_bytes)) stream.append(json_bytes) return stream.bytes
def create_sample_sync_frame(): stream = BitStream(bin="0" * 32) stream.set(True, range(0, 12)) # frame sync stream.set(True, 14) # Layer III stream.set(True, 15) # protection bit stream.set(True, 17) # bitrate, 128k return stream
def decode(data): stream = BitStream(data) stream.read('int:32') stream.read('int:8') stream.read('int:8') n = stream.read('int:32') fmt = 'bytes:%d' % n user = json.loads(stream.read(fmt).decode()) return repr(user)
def __init__(self, nalu_type): ''' init the value of nalu unit : param nalu_type: something like NAL_UNIT_TYPE_CODED_SLICE_IDR in nalutypes.py ''' if (nalu_type != nalutypes.NAL_UNIT_TYPE_UNSPECIFIED): # for specific nalutypes self.forbidden_zero_bit = '0b0' self.nal_ref_idc = '0b11' self.nal_unit_type = "0b" + "{0:05b}".format(nalu_type) self.stream = BitStream(START_CODE_PREFIX) self.stream.append(self.forbidden_zero_bit) self.stream.append(self.nal_ref_idc) self.stream.append(self.nal_unit_type) else: # for slice_data self.stream = BitStream()
def test_memoria(self): l = [0,0,0] a = binary_repr(1,40)[1:41] #self.assertEqual(BitStream(bin = '0b'+ '1000001').int, -1, 'Os números são iguais') self.assertEqual(l [int('000010',2)], 0, msg= 'Está correto') self.assertEqual(len(BitStream(bin = 1, length = 40).bin), 40)
def storRight(self): self.IAS.registers.MBR = self.IAS.registers.AC temp = self.IAS.memory.loc[self.IAS.registers.MAR.int].bin[ 0:28] + self.IAS.registers.MBR.bin[28:40] self.IAS.memory.loc[self.IAS.registers.MAR.int] = BitStream(bin=temp) print( f"{Colors.OKGREEN}Executed STOR M({self.IAS.registers.MAR.int}, 28:39){Colors.ENDC}" )
def read_variable_byte_data(bit_stream, data_bits=Bits('')): continuation_bit = bit_stream.read('bits:1') data_bits += bit_stream.read('bits:7') if continuation_bit: return read_variable_byte_data(bit_stream, data_bits) else: while len(data_bits) % 8 != 0: data_bits = BitStream('0b0') + data_bits return data_bits
def unpack(b): for i in b: print("{:02X}".format(i)) roct = b[::-1] rb = BitStream(roct).bin rs = [] for i in range(len(rb) - 7, -1, -7): rs.append(int(rb[i:i + 7], 2)) print(rs) return bytes(rs)
def test_round_trip(self): """ Deserialising a message and serialising it again results in the same binary message. """ header = b"\x20\x02" good = b"\x00\x00" event = ConnACK.deserialise((False, False, False, False), BitStream(bytes=good)) self.assertEqual(event.serialise(), header + good)
def subAbs(self): self.IAS.registers.MBR = self.IAS.memory.loc[ self.IAS.registers.MAR.int] self.IAS.registers.AC = BitStream(int=self.IAS.registers.AC.int - abs(self.IAS.registers.MBR.int), length=40) print( f"{Colors.OKGREEN}Executed SUB |M({self.IAS.registers.MAR.int})|{Colors.ENDC}" )
def addAbs(self): self.IAS.registers.MBR = self.IAS.memory.loc[ self.IAS.registers.MAR.int] self.IAS.registers.AC = BitStream(int=abs(self.IAS.registers.MBR.int) + self.IAS.registers.AC.int, length=40) print( f"{Colors.OKGREEN}Executed ADD |M({self.IAS.registers.MAR.int})|{Colors.ENDC}" )