def testFromFile(self):
     s = CBS(filename='test.m1v')
     self.assertEqual(s[0:32].hex, '000001b3')
     self.assertEqual(s.read(8 * 4).hex, '000001b3')
     width = s.read(12).uint
     height = s.read(12).uint
     self.assertEqual((width, height), (352, 288))
    def from_bytes(cls, bitstream):
        packet = cls()

        # Convert to ConstBitStream (if not already provided)
        if not isinstance(bitstream, ConstBitStream):
            if isinstance(bitstream, Bits):
                bitstream = ConstBitStream(auto=bitstream)
            else:
                bitstream = ConstBitStream(bytes=bitstream)

        # Read the next header type
        packet.next_header = bitstream.read('uint:8')

        # Read the header length, given in multiples of 8 octets
        header_length = bitstream.read('uint:8') + 1

        # Read the options
        options_length = (header_length * 8) - 2
        packet.options = bitstream.read('bytes:%d' % options_length)

        # And the rest is payload
        remaining = bitstream[bitstream.pos:]
        packet.payload = remaining.bytes

        payload_class = protocol_registry.get_type_class(packet.next_header)
        if payload_class:
            packet.payload = payload_class.from_bytes(packet.payload)

        # Verify that the properties make sense
        packet.sanitize()

        return packet
 def testReading(self):
     s = CBS(uie=333)
     a = s.read('uie')
     self.assertEqual(a, 333)
     s = CBS('uie=12, sie=-9, sie=9, uie=1000000')
     u = s.unpack('uie, 2*sie, uie')
     self.assertEqual(u, [12, -9, 9, 1000000])
Exemplo n.º 4
0
    def from_bytes(cls, bitstream):
        '''
           +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
           |  /|    Priority   |    Weight     |  M Priority   |   M Weight    |
           | L +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
           | o |        Unused Flags     |L|p|R|           Loc-AFI             |
           | c +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
           |  \|                             Locator                           |
		   +-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
        '''
        record = cls()

        # Convert to ConstBitStream (if not already provided)
        if not isinstance(bitstream, ConstBitStream):
            if isinstance(bitstream, Bits):
                bitstream = ConstBitStream(auto=bitstream)
            else:
                bitstream = ConstBitStream(bytes=bitstream)

        # Read the priorities and weights
        (record.priority, record.weight, record.m_priority,
         record.m_weight) = bitstream.readlist('4*uint:8')

        # Read over unused flags
        record.reserved = bitstream.read(13)

        # Read the flags
        (record.local,
         record.probed_locator,
         record.reachable) = bitstream.readlist('3*bool')

        # Read the locator
        record.address = read_afi_address_from_bitstream(bitstream)

        return record
 def testReadingLines(self):
     s = b"This is a test\nof reading lines\nof text\n"
     b = CBS(bytes=s)
     n = bitstring.Bits(bytes=b'\n')
     self.assertEqual(b.readto(n).bytes, b'This is a test\n')
     self.assertEqual(b.readto(n).bytes, b'of reading lines\n')
     self.assertEqual(b.readto(n).bytes, b'of text\n')
 def testReadingErrors(self):
     s = CBS(10)
     with self.assertRaises(bitstring.ReadError):
         s.read('uie')
     self.assertEqual(s.pos, 0)
     with self.assertRaises(bitstring.ReadError):
         s.read('sie')
     self.assertEqual(s.pos, 0)
 def testByteAligned(self):
     a = CBS('0xaabb00aa00bb')
     b = a.readto('0x00', bytealigned=True)
     self.assertEqual(b, '0xaabb00')
     self.assertEqual(a.bytepos, 3)
     b = a.readto('0xaa', bytealigned=True)
     self.assertEqual(b, '0xaa')
     self.assertRaises(bitstring.ReadError, a.readto, '0xcc', bytealigned=True)
 def testUnpackingBytes(self):
     s = CBS(80)
     t = s.unpack('bytes:1')
     self.assertEqual(t[0], b'\x00')
     a, b, c = s.unpack('bytes:1, bytes, bytes:2')
     self.assertEqual(a, b'\x00')
     self.assertEqual(b, b'\x00' * 7)
     self.assertEqual(c, b'\x00' * 2)
Exemplo n.º 9
0
 def testByteAligned(self):
     a = CBS("0xaabb00aa00bb")
     b = a.readto("0x00", bytealigned=True)
     self.assertEqual(b, "0xaabb00")
     self.assertEqual(a.bytepos, 3)
     b = a.readto("0xaa", bytealigned=True)
     self.assertEqual(b, "0xaa")
     self.assertRaises(bitstring.ReadError, a.readto, "0xcc", bytealigned=True)
Exemplo n.º 10
0
 def testUnpackingBytes(self):
     s = CBS(80)
     t = s.unpack("bytes:1")
     self.assertEqual(t[0], b"\x00")
     a, b, c = s.unpack("bytes:1, bytes, bytes:2")
     self.assertEqual(a, b"\x00")
     self.assertEqual(b, b"\x00" * 7)
     self.assertEqual(c, b"\x00" * 2)
def tableBtreeEqualitySearch(currentPageBitstream, fpt, rowid, pageSize):
    """
    -equality search in a table btree for (a,a) and (a, b)
        based on the Emp_ID (the indexed column)
    -the other two only need to scan, no performance enhancement
    -only the leaf pages have the data

        @param currentPageBitstream: the bitstream reader of a page
        @param fpt: the file pointer of a page
        @param rowid: look for a record with this rowid
        @param pageSize: the page size of the db
    """
    pageType, numCells, toPosition, rightMostPointer = _pageInfo(currentPageBitstream)
    readCounts(pageType)
    # read each cell offset within the page from the cell pointer array
    for i in range(0, numCells):

        cellOffset = bitstreamReadAtOffset(currentPageBitstream, int, 'bytes:2', i * 2 + toPosition)

        # read the cell from cellOffset 
        nxtChildPage, record = parse_cell_content(cellOffset, currentPageBitstream, pageType, fpt, pageSize)
        
        # interior cell cases
        if nxtChildPage:
            
            # get the rowid
            currentRowid, _, _ = readVarintAtOffset(cellOffset + POINTER_SIZE, currentPageBitstream)
            
            if rowid <= currentRowid:
                # keep traverse to the left of this cell
                nxtPagebitstream = ConstBitStream(readPage(nxtChildPage, fpt, pageSize))
                tableBtreeEqualitySearch(nxtPagebitstream, fpt, rowid, pageSize)
                return 
            # when the rowid > currentRowid ==> iterate nxt cell to try
            continue

        '''in the leaf page'''

        # get the rowid of the cell
        _, _, varintBytes = readVarintAtOffset(cellOffset, currentPageBitstream)
        currentRowid, _, _ = readVarintAtOffset(cellOffset + varintBytes, currentPageBitstream)
       
        if currentRowid == rowid:
            # found the record
            print("Full Name: {} {} {}".format(record[FIRST_NAME_INDEX],
                                               record[MIDDLE_NAME_INDEX],
                                               record[LAST_NAME_INDEX]))
            return
        elif currentRowid > rowid: # ==> the rest of cell in this page has greater rowid
            return
         # iterate the nxt cell to check equality

    if rightMostPointer:
        nxtPagebitstream = ConstBitStream(readPage(rightMostPointer, fpt, pageSize))
        tableBtreeEqualitySearch(nxtPagebitstream, fpt, rowid, pageSize)
    else:
        # sanity check for debug
        print("record not found")
 def testStringRepresentationFromFile(self):
     s = CBS(filename='test.m1v', pos=2001)
     self.assertEqual(
         s.__repr__(),
         "ConstBitStream(filename='test.m1v', length=1002400, pos=2001)")
     s.pos = 0
     self.assertEqual(
         s.__repr__(),
         "ConstBitStream(filename='test.m1v', length=1002400)")
Exemplo n.º 13
0
	def __init__(self, fileString):
		self.stream = ConstBitStream(filename=fileString)
		
		name_b, self.width, self.height, self.depth, self.timepoints, self.channels, self.bytesPerPix, packingOrder_idx = self.stream.readlist('bytes:64, uintle:32, uintle:32, uintle:32, uintle:32, uintle:32, uintle:32, uintle:32')
		self.packingOrder = GetFilePackingOrder(packingOrder_idx)
		self.name = StringFromBytes(name_b)

		self.channelNames = []
		for i in range(self.channels):
			self.channelNames.append(StringFromBytes(self.stream.read('bytes:32')))

		self.imageMap = []
		n = self.channels * self.depth * self.timepoints
		for i in range(n):
			nBytes = self.stream.read('uintle:32')
			self.imageMap.append({'offset': self.stream.bytepos, 'nBytes': nBytes})
			if i < (n - 1):
				self.stream.bytepos += nBytes

		def BaseLookUpFunc(first, second, third, n1, n2):
			nf = len(first)
			ns = len(second)
			nt = len(third)

			first = np.repeat(first, ns * nt)
			second = np.tile(np.repeat(second, nt), nf)
			third = np.tile(third, nf * ns)

			return (first * n1) + (second * n2) + third

		if self.packingOrder == file_packing_order['ZTC']:
			def LookUpFunc(z, t, c):
				return BaseLookUpFunc(c, t, z, self.depth * self.timepoints, self.depth)
		elif self.packingOrder == file_packing_order['ZCT']:
			def LookUpFunc(z, t, c):
				return BaseLookUpFunc(t, c, z, self.depth * self.channels, self.depth)	
		elif self.packingOrder == file_packing_order['TZC']:
			def LookUpFunc(z, t, c):
				return BaseLookUpFunc(c, z, t, self.depth * self.timepoints, self.timepoints)
		elif self.packingOrder == file_packing_order['TCZ']:
			def LookUpFunc(z, t, c):
				return BaseLookUpFunc(z, c, t, self.timepoints * self.channels, self.timepoints)
		elif self.packingOrder == file_packing_order['CZT']:
			def LookUpFunc(z, t, c):
				return BaseLookUpFunc(t, z, c, self.depth * self.channels, self.channels)
		elif self.packingOrder == file_packing_order['CTZ']:
			def LookUpFunc(z, t, c):
				return BaseLookUpFunc(z, t, c, self.timepoints * self.channels, self.channels)
		self._LookUpFunc_ = LookUpFunc

		if self.bytesPerPix == 4:
			dt = np.dtype(np.uint32)
		elif self.bytesPerPix == 2:
			dt = np.dtype(np.uint16)
		elif self.bytesPerPix == 1:
			dt = np.dtype(np.uint8)
		self._dt_ = dt.newbyteorder('<')
Exemplo n.º 14
0
 def __init__(self, src, *args, **kwargs):
     super(EC3SpecificBox,self).__init__(src, *args,**kwargs)
     data = src.read(self.size)
     bs = ConstBitStream(bytes=data)
     self.data_rate = bs.read('uint:13')
     self.num_ind_sub = 1+bs.read('uint:3')
     self.substreams = []
     for i in range(self.num_ind_sub):
         self.substreams.append(EC3SpecificBox.SubStream(bs))
Exemplo n.º 15
0
 def __init__(self, f, read=True):
     if read:
         self.file = ConstBitStream(f.read())
     else:
         self.file = ConstBitStream(f)
     self.file.pos = self.identifier_offset
     identifier = self.file.peek(f'bytes:{self.identifier_size}')
     if identifier != b'Joker':
         raise ValueError('Input not a Joker container file')
    def pack(self, for_game=False):

        is_translated = False

        if not self.solution.translated == "":
            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.translated),
                                     length=16)
        else:
            output += ConstBitStream(uintle=len(self.solution.original),
                                     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.translated)
            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.original)
                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.original)
            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 __init__(self, file_name):
     """
     Init method or constructor
     @:param self The object pointer
     @:param file_name it's the file that we want to read
     @:param stream it's the 8 bit stream
     """
     self.file_name = file_name
     self.stream = ConstBitStream(filename=file_name)
Exemplo n.º 18
0
def _parse_binary_feed(feed):
    logger.debug("Parsing binary feed %s", feed)

    binaryfeed = bytearray(b64decode(feed))
    bitstream = ConstBitStream(binaryfeed)

    payload_encoding = binaryfeed[0]
    if payload_encoding != bitstream.read("uint:8"):
        raise PyiCloudBinaryFeedParseError(
            "Missmatch betweeen binaryfeed and bistream payload encoding")

    ASSET_PAYLOAD = 255
    ASSET_WITH_ORIENTATION_PAYLOAD = 254
    ASPECT_RATIOS = [
        0.75, 4.0 / 3.0 - 3.0 * (4.0 / 3.0 - 1.0) / 4.0,
        4.0 / 3.0 - 2.0 * (4.0 / 3.0 - 1.0) / 4.0, 1.25, 4.0 / 3.0,
        1.5 - 2.0 * (1.5 - 4.0 / 3.0) / 3.0,
        1.5 - 1.0 * (1.5 - 4.0 / 3.0) / 3.0, 1.5, 1.5694444444444444,
        1.6388888888888888, 1.7083333333333333, 16.0 / 9.0,
        2.0 - 2.0 * (2.0 - 16.0 / 9.0) / 3.0,
        2.0 - 1.0 * (2.0 - 16.0 / 9.0) / 3.0, 2, 3
    ]

    valid_payloads = [ASSET_PAYLOAD, ASSET_WITH_ORIENTATION_PAYLOAD]
    if payload_encoding not in valid_payloads:
        raise PyiCloudBinaryFeedParseError("Unknown payload encoding '%s'" %
                                           payload_encoding)

    assets = {}
    while len(bitstream) - bitstream.pos >= 48:
        range_start = bitstream.read("uint:24")
        range_length = bitstream.read("uint:24")
        range_end = range_start + range_length

        logger.debug("Decoding indexes [%s-%s) (length %s)", range_start,
                     range_end, range_length)

        previous_asset_id = 0
        for index in range(range_start, range_end):
            aspect_ratio = ASPECT_RATIOS[bitstream.read("uint:4")]

            id_size = bitstream.read("uint:2")
            if id_size:
                # A size has been reserved for the asset id
                asset_id = bitstream.read("uint:%s" % (2 + 8 * id_size))
            else:
                # The id is just an increment to a previous id
                asset_id = previous_asset_id + bitstream.read("uint:2") + 1

            orientation = None
            if payload_encoding == ASSET_WITH_ORIENTATION_PAYLOAD:
                orientation = bitstream.read("uint:3")

            assets[index] = PhotoAsset(asset_id, aspect_ratio, orientation)
            previous_asset_id = asset_id

    return assets
 def testByteAligned(self):
     a = CBS('0xaabb00aa00bb')
     b = a.readto('0x00', bytealigned=True)
     self.assertEqual(b, '0xaabb00')
     self.assertEqual(a.bytepos, 3)
     b = a.readto('0xaa', bytealigned=True)
     self.assertEqual(b, '0xaa')
     with self.assertRaises(bitstring.ReadError):
         b.readto('0xcc', bytealigned=True)
Exemplo n.º 20
0
def main():
    file = open(sys.argv[1], "rb")
    msg = ConstBitStream(file)

    s_in = BitArray()
    keys = {Bits(''): 0}
    s_out = BitArray()
    count = 1
    n_bits = 0
    while True:
        try:
            s_in.append(msg.read(1))
        except ReadError:
            break

        #Se a palavra nao tiver no dicionario
        if Bits(s_in) not in keys:
            # x = yb
            y = s_in[:-1]
            b = s_in[-1:]

            pos = keys[Bits(y)]

            #log base 2 |keys|
            n_bits = ceil(log2(len(keys)))

            if n_bits != 0:
                prefix = Bits(uint=int(pos), length=n_bits)
            else:
                prefix = Bits('')

            s_out.append(Bits('0b' + str(prefix.bin) + str(b.bin)))

            keys[Bits(s_in)] = count
            count += 1
            s_in.clear()

    #Add padding: 00000 10101
    #Numero de zeros é o tamanho dos bits extra para depois no descompressor saber
    if s_in[:1].bin == Bits(1):
        z = Bits('0b' + '0' * len(s_in))
    else:
        z = Bits('0b' + '1' * len(s_in))

    s_in.reverse()
    s_out.reverse()

    s_out.append(s_in)
    s_out.append(z)

    s_out.reverse()

    with open(sys.argv[2], 'wb') as f_out:
        s_out.tofile(f_out)

    file.close()
def indexBtreeEqualitySearch(currentPageBitstream, fpt, empID, ops, pageSize):
    """
    equality search in the index btree (c,b) and (d, c)
        may need to search through this to get the rowid then
        go back to the table btree to get the actual record

    return the rowid of empID record
        @param currentPageBitstream: the bitstream for the index page; 
                start from the root page of a index tree
        @param fpt: the file pointer of a page
        @param empID: the condition to be search; 
                assume empID is the indexed column; should be sorted in the index btree
        @param ops: the operation to be done for each record
        @param pageSize: the page size of the db
    """

    pageType, numCells, toPosition, rightMostPointer = _pageInfo(currentPageBitstream)
    readCounts(pageType)
    # store the child pointer of the previous cell
    result = -1
    # determine the direction of traversing the cells
    start, end, step = 0, numCells, 1

    # read each cell offset within the page from the cell pointer array
    for i in range(start, end, step):

        cellOffset = bitstreamReadAtOffset(currentPageBitstream, int, 'bytes:2', i * 2 + toPosition)
        # read the cell from cellOffset 
        nxtChildPage, record = parse_cell_content(cellOffset, currentPageBitstream, pageType, fpt, pageSize)

        # for debug
        if not record:
            print("This is not a index btree page")
            break

        # if found a matching record ==> no need to search
        result = ops(record)
        if result:
            break

        # need to traversethe pointer of the cell since we want to find the best matching        
        if empID < record[0]:

            # by the sorted properties and in the leaf page ==> empID << record for all cells
            if not nxtChildPage:
                break
            nxtPagebitstream = ConstBitStream(readPage(nxtChildPage, fpt, pageSize))
            return indexBtreeEqualitySearch(nxtPagebitstream, fpt, empID, ops, pageSize)

        # if empID > record[0], iterate the nxt cell; let the cell key get closer to the empID from the left

    if rightMostPointer:
        nxtPagebitstream = ConstBitStream(readPage(rightMostPointer, fpt, pageSize))
        return indexBtreeEqualitySearch(nxtPagebitstream, fpt, empID, ops, pageSize)

    return result
Exemplo n.º 22
0
def handleDump(fileObj):
    tech = []
    raw = ConstBitStream(fileObj)
    try:
        while True:
            tech.append(raw.read('floatle:32'))
    except:
        pass
    del(raw)
    return tech 
Exemplo n.º 23
0
 def testRead(self):
     s = CBS("0b100011110001")
     a = s.read("pad:1")
     self.assertEqual(a, None)
     self.assertEqual(s.pos, 1)
     a = s.read(3)
     self.assertEqual(a, CBS("0b000"))
     a = s.read("pad:0")
     self.assertEqual(a, None)
     self.assertEqual(s.pos, 4)
Exemplo n.º 24
0
 def make_step(self, motor_id, direction, steps_per_click):
     data = ConstBitStream('0x1400')
     data += ConstBitStream('intle:16=%d' % command.MAKE_STEP)
     data += ConstBitStream('intle:16=%d' %
                            (2 * (motor_id == 0) *
                             (direction - .5) * steps_per_click))
     data += ConstBitStream('intle:16=%d' %
                            (2 * (motor_id == 1) *
                             (direction - .5) * steps_per_click))
     tmp = self._make_connection(data)
Exemplo n.º 25
0
 def testRead(self):
     s = CBS('0b100011110001')
     a = s.read('pad:1')
     self.assertEqual(a, None)
     self.assertEqual(s.pos, 1)
     a = s.read(3)
     self.assertEqual(a, CBS('0b000'))
     a = s.read('pad:0')
     self.assertEqual(a, None)
     self.assertEqual(s.pos, 4)
	def handle_read(self):
		#format: 20 bytes in total. Size: intle:16
		#Incomming messages comes with 160 bytes..
		data0 = self.recv(160);
		if data0:			
			data = ConstBitStream(bytes=data0, length=160)
			#print "All: %s" % data.bin
			
			msize = data.read('intle:16')
			mtype = data.read('intle:16')
			mtime = data.read('intle:64')
			
			# RA: 
			ant_pos = data.bitpos
			ra = data.read('hex:32')
			data.bitpos = ant_pos
			ra_uint = data.read('uintle:32')
			
			# DEC:
			ant_pos = data.bitpos
			dec = data.read('hex:32')
			data.bitpos = ant_pos
			dec_int = data.read('intle:32')
			
			#______ Testing:
			# Sends back to Stellarium the received coordinates, in order to update the field of view indicator
			(sra, sdec, stime) = coords.eCoords2str(float("%f" % ra_uint), float("%f" % dec_int), float("%f" %  mtime))
			self.act_pos(coords.hourStr_2_rad(sra), coords.degStr_2_rad(sdec))
			#______ End Testing
			
			# Emits the signal with received equatorial coordinates (for use in external Qt Gui..)
			self.stell_pos_recv.emit("%f" % ra_uint, "%f" % dec_int, "%f" %  mtime)
Exemplo n.º 27
0
    def handle_read(self):
        #format: 20 bytes in total. Size: intle:16
        #Incomming messages comes with 160 bytes..
        data0 = self.recv(160);
        if data0:            
            data = ConstBitStream(bytes=data0, length=160)
            print "All: %s" % data.bin
 
            msize = data.read('intle:16')
            mtype = data.read('intle:16')
            mtime = data.read('intle:64')
 
            # RA: 
            ant_pos = data.bitpos
            ra = data.read('hex:32')
            data.bitpos = ant_pos
            ra_uint = data.read('uintle:32')
 
            # DEC:
            ant_pos = data.bitpos
            dec = data.read('hex:32')
            data.bitpos = ant_pos
            dec_int = data.read('intle:32')
 
            logging.debug("Size: %d, Type: %d, Time: %d, RA: %d (%s), DEC: %d (%s)" % (msize, mtype, mtime, ra_uint, ra, dec_int, dec))
            (sra, sdec, stime) = coords.eCoords2str(float("%f" % ra_uint), float("%f" % dec_int), float("%f" %  mtime))
 
            #Sends back the coordinates to Stellarium
            self.act_pos(coords.hourStr_2_rad(sra), coords.degStr_2_rad(sdec))
Exemplo n.º 28
0
def main():
    test_file = sys.argv[1]
    f = open(os.path.join(os.getcwd(), test_file), "rb")
    s = ConstBitStream(f)
    try:
        file_type = s.read("bytes:3")
        if file_type == "FWS":
            print "Standard SWF"
            print "Version:", s.read("uintle:8")
            print "Size:", s.read("uintle:32"), "bytes"
            s = datatypes.rect(s)
            print "Frame rate: %d.%d" % datatypes.fixed_8(s)
            print "Frame count:", s.read("uintle:16")
            read_tag_headers(s)
        elif file_type == "CWS":
            print "Compressed SWF"
            print "Version:", s.read("uintle:8")
            print "Uncompressed size:", s.read("uintle:32"), "bytes"
            to_decompress = s[64:].tobytes()
            s = ConstBitStream(bytes=zlib.decompress(to_decompress))
            s = datatypes.rect(s)
            print "Frame rate: %d.%d" % datatypes.fixed_8(s)
            print "Frame count:", s.read("uintle:16")
            read_tag_headers(s)
            # print "[Cannot currently parse]"
    finally:
        f.close()
def search():
    try:
        s = ConstBitStream(filename=kipname)
        global find
        global find2
        find = s.find('0x003C00121F280071'
                      )  # Atmosphere 11 and 12 (set for 1 byte patch)
        find2 = s.find('0x01c0be121f00016b'
                       )  # Atmosphere 13 > Current (set for 1 byte patch)
    except OSError as e:
        print("Unable to search files for this reason! %s" % e)
Exemplo n.º 30
0
class JokerContainerFile:
    file = None

    # Offset/sizes in bytes
    identifier_offset = 0
    identifier_size = 5
    mode_offset = identifier_offset + identifier_size
    mode_size = 2
    data_offset = mode_offset + mode_size + 1  # Skips unknown byte 0x01 between mode and data
    mode_f100_offset = data_offset + (7 * 8)

    def __init__(self, f, read=True):
        if read:
            self.file = ConstBitStream(f.read())
        else:
            self.file = ConstBitStream(f)
        self.file.pos = self.identifier_offset
        identifier = self.file.peek(f'bytes:{self.identifier_size}')
        if identifier != b'Joker':
            raise ValueError('Input not a Joker container file')

    @property
    def mode(self):
        self.file.pos = self.mode_offset * 8
        return self.file.peek(f'uintle:{self.mode_size * 8}')

    @property
    def _data(self):
        return self.file[self.data_offset * 8:]

    @property
    def data(self):
        data = self._data
        if self.mode == 0x0300 or self.mode == 0x0200:
            # Compressed/encrypted
            decompressed = zlib.decompress(data.tobytes())[:-1]
            decoded = bytes.fromhex(base64.b64decode(decompressed, validate=True).decode())
            data = JokerCipher.decrypt(decoded)

            if self.mode == 0x300:
                data = self._process_mode_300(data)

        elif self.mode == 0xF100:
            return data[self.mode_f100_offset:]

        return data

    @staticmethod
    def _process_mode_300(data):
        decoded = base64.decodebytes(data)
        decrypted = DefaultCipher.decrypt(decoded)
        decompressed = zlib.decompress(decrypted)

        return decompressed
    def parse_file_data(self, file_offset, length, data):
        # filter on the file ID for instance
        if file_offset.id == 64:
            # parse the data, in this example we are assuming the file data contains a temperature value in decicelsius
            # stored as int16, as transmitted by the sensor examples of OSS-7
            s = ConstBitStream(bytes=bytearray(data))
            sensor_value = s.read("uintbe:16") / 10.0
            yield 'temperature', sensor_value, DataPointType.telemetry
            # note this is a generator function, so multiple values can be returned

        return
Exemplo n.º 32
0
 def decompress(data, bit_length, output_length):
     stream = ConstBitStream(bytes=data)
     result = []
     while len(result) < output_length:
         t = stream.read(1)
         if t:
             count = stream.read(8).uint + 1
         else:
             count = 1
         result.extend([stream.read(bit_length).uint] * count)
     return bytes(result)
Exemplo n.º 33
0
    def _parse_binary_feed(self, feed):
        binaryfeed = bytearray(b64decode(feed))
        bitstream = ConstBitStream(binaryfeed)

        payload_encoding = binaryfeed[0]
        if payload_encoding != bitstream.read("uint:8"):
            raise PyiCloudBinaryFeedParseError("Missmatch betweeen binaryfeed and bistream payload encoding")

        ASSET_PAYLOAD = 255
        ASSET_WITH_ORIENTATION_PAYLOAD = 254
        ASPECT_RATIOS = [
            0.75,
            4.0 / 3.0 - 3.0 * (4.0 / 3.0 - 1.0) / 4.0,
            4.0 / 3.0 - 2.0 * (4.0 / 3.0 - 1.0) / 4.0,
            1.25,
            4.0 / 3.0,
            1.5 - 2.0 * (1.5 - 4.0 / 3.0) / 3.0,
            1.5 - 1.0 * (1.5 - 4.0 / 3.0) / 3.0,
            1.5,
            1.5694444444444444,
            1.6388888888888888,
            1.7083333333333333,
            16.0 / 9.0,
            2.0 - 2.0 * (2.0 - 16.0 / 9.0) / 3.0,
            2.0 - 1.0 * (2.0 - 16.0 / 9.0) / 3.0,
            2,
            3,
        ]

        valid_payloads = [ASSET_PAYLOAD, ASSET_WITH_ORIENTATION_PAYLOAD]
        if payload_encoding not in valid_payloads:
            raise PyiCloudBinaryFeedParseError("Unknown payload encoding '%s'" % payload_encoding)

        assets = {}
        while len(bitstream) - bitstream.pos >= 48:
            range_start = bitstream.read("uint:24")
            range_length = bitstream.read("uint:24")
            range_end = range_start + range_length

            previous_asset_id = 0
            for index in range(range_start, range_end):
                aspect_ratio = ASPECT_RATIOS[bitstream.read("uint:4")]

                id_size = bitstream.read("uint:2")
                if id_size:
                    # A size has been reserved for the asset id
                    asset_id = bitstream.read("uint:%s" % (2 + 8 * id_size))
                else:
                    # The id is just an increment to a previous id
                    asset_id = previous_asset_id + bitstream.read("uint:2") + 1

                orientation = None
                if payload_encoding == ASSET_WITH_ORIENTATION_PAYLOAD:
                    orientation = bitstream.read("uint:3")

                assets[index] = PhotoAsset(index, asset_id, aspect_ratio, orientation, self)
                previous_asset_id = asset_id

        return assets.values()
Exemplo n.º 34
0
def render_calibrator(image, block_height, block_width, pixel_width):
    '''This creates the checkboard-like pattern along the top and left of the first frame of video streams, and every
    frame of image streams.  This is what the reader uses to initially lock onto the frame.  Stream block_width and
    block_height are encoded into this pattern, using alternating color palettes so no two repeating values produce a
    continuous block of color, interfering with the frame lock process.'''

    initializer_palette_dict_a = ValuesToColor(palette_grabber('1'),
                                               'initializer_palette A')
    initializer_palette_dict_b = ValuesToColor(palette_grabber('11'),
                                               'initializer_palette B')

    draw = ImageDraw.Draw(image)
    draw.rectangle((0, 0, pixel_width - 1, pixel_width - 1), fill='rgb(0,0,0)')

    block_width_encoded = BitArray(uint=block_width, length=block_width - 1)
    block_width_encoded.reverse()
    readable_block_width = ConstBitStream(block_width_encoded)

    for i in range(block_width - 1):
        next_bit = readable_block_width.read('bits : 1')

        if i % 2 == 0:
            color_value = initializer_palette_dict_b.get_color(
                ConstBitStream(next_bit))

        else:
            color_value = initializer_palette_dict_a.get_color(
                ConstBitStream(next_bit))

        draw.rectangle((pixel_width * i + pixel_width, 0, pixel_width *
                        (i + 1) - 1 + pixel_width, pixel_width - 1),
                       fill=f'rgb{str(color_value)}')

    block_height_encoded = BitArray(uint=block_height, length=block_height - 1)
    block_height_encoded.reverse()
    readable_block_height = ConstBitStream(block_height_encoded)

    for i in range(block_height - 1):
        next_bit = readable_block_height.read('bits : 1')

        if i % 2 == 0:
            color_value = initializer_palette_dict_b.get_color(
                ConstBitStream(next_bit))

        else:
            color_value = initializer_palette_dict_a.get_color(
                ConstBitStream(next_bit))

        draw.rectangle(
            (0, pixel_width * i + pixel_width, pixel_width - 1, pixel_width *
             (i + 1) - 1 + pixel_width),
            fill=f'rgb{str(color_value)}')

    return image
Exemplo n.º 35
0
def search():
    try:
        from bitstring import ConstBitStream
        s = ConstBitStream(filename=kipname)
        global find
        find = s.find(pattern)
        global findnew
        findnew = s.find(pattern2)

    except OSError as e:
        print("Error: %s : %s" % ("Search: ", e.strerror))
Exemplo n.º 36
0
def degree_decodeFile(compressed_file_name):
    encoded_text = readFile(compressed_file_name)
    encoded_text = encoded_text.read('bin:{0}'.format(len(encoded_text)))

    encoded_text, file_name_lenght = getBits(encoded_text, 8)
    file_name_lenght = int(file_name_lenght, base=2)  # gaunam failo pavadinimo ilgi

    encoded_text, file_name = getBits(encoded_text, file_name_lenght)
    file_name = ''.join(
        [chr(int(file_name[i:i + 8], base=2)) for i in range(0, len(file_name), 8)])  # gaunam orig. failo pav.

    encoded_text, degree = getBits(encoded_text, 8)
    degree = int(degree, base=2)

    encoded_text, context_huffman_list_lenght = getBits(encoded_text, 16 * int(degree))
    context_huffman_list_lenght = int(context_huffman_list_lenght, base=2)  # lenteles ilgis

    context_huffman_list = []
    huffman_list = []
    for i in range(context_huffman_list_lenght):
        encoded_text, context = getBits(encoded_text, 8 * degree)
        encoded_text, list_lenght = getBits(encoded_text, 8)
        list_lenght = int(list_lenght, base=2)
        for j in range(list_lenght):
            encoded_text, bite = getBits(encoded_text, 8)

            encoded_text, v_lenght = getBits(encoded_text, 8)
            v_lenght = int(v_lenght, base=2)  # uzkodavimo ilgis

            v_size = bestSize(v_lenght)

            encoded_text, temp_value = getBits(encoded_text, v_size)
            temp_value = temp_value.replace('0', '', v_size - v_lenght)
            huffman_list.append([bite, temp_value])
        context_huffman_list.append([context, huffman_list])
        huffman_list = []

    encoded_text, text_lenght = getBits(encoded_text, 32)
    text_lenght = int(text_lenght, base=2)  # teksto ilgis

    text_full_size = bestSize(text_lenght)

    encoded_text, encoded_text = getBits(encoded_text, text_full_size)  # uzkoduotas tekstas
    encoded_text = encoded_text.replace('0', '', text_full_size - text_lenght)
    if int(degree) == 1:
        bites = degree_decodeText(context_huffman_list, encoded_text)  # atkoduojame
    else:
        bites = second_degree_burn_decodeText(context_huffman_list, encoded_text)
    text = ''.join(bites)
    # print(context_huffman_list)
    # print(text)
    with open(file_name, 'wb') as f:  # orig. teksta atgal i faila
        write_buffer = ConstBitStream(bin=text)
        write_buffer.tofile(f)
Exemplo n.º 37
0
    def test_parsing(self):
        length_bytes = [0x01]
        length = Length.parse(ConstBitStream(bytes=length_bytes))
        self.assertEqual(length.value, 1)

        length_bytes = [0x40, 0x41]
        length = Length.parse(ConstBitStream(bytes=length_bytes))
        self.assertEqual(length.value, 65)

        length_bytes = [0xC0, 0x41, 0x10, 0x00]
        length = Length.parse(ConstBitStream(bytes=length_bytes))
        self.assertEqual(length.value, 4263936)
Exemplo n.º 38
0
 def __init__(self, bin_data=None, rw=False):
     """Initialising the class with an byte (octet) array or make a new, empty one.
     :param bin_data: Byte array.
     :param rw: If bin_data is set, make it read/write-able, otherwise it's read-only.
     """
     if bin_data is None:
         self._data = BitStream()
     elif rw:
         self._data = BitStream(bytes=bin_data)
     else:
         self._data = ConstBitStream(bytes=bin_data)
     self.reset()
Exemplo n.º 39
0
def _parse_request(bytes, path):
    stream = ConstBitStream(bytes=bytes)
    pos = stream.find(Bits(bytes=b'\r\n\r\n'), bytealigned=True)[0]
    # For now I don't care about the request body.
    header_bytes, _ = stream[:pos], stream[pos + 32:]
    headers = dict()
    header_lines = header_bytes.bytes.decode("ascii").split('\r\n')
    for h in header_lines[1:]:
        splits = h.split(":")
        headers[splits[0]] = splits[1].strip()

    return HttpRequest(headers, path, None)
Exemplo n.º 40
0
def apply_sys_lang(eboot):
  
  if LANG_CFG_ID in common.editor_config.hacks:
    sys_menu_lang = common.editor_config.hacks[LANG_CFG_ID]
  else:
    sys_menu_lang = 1
    common.editor_config.hacks[LANG_CFG_ID] = sys_menu_lang
  
  patch_loc = 0x1B300
  patch = ConstBitStream(uintle = sys_menu_lang, length = 8) + ConstBitStream(hex = "0x000224")
  eboot.overwrite(patch, patch_loc * 8)
  return eboot
Exemplo n.º 41
0
    def from_bytes(cls, bitstream):
        """
        Parse the given record and update properties accordingly
        """
        record = cls()

        # Convert to ConstBitStream (if not already provided)
        if not isinstance(bitstream, ConstBitStream):
            if isinstance(bitstream, Bits):
                bitstream = ConstBitStream(auto=bitstream)
            else:
                bitstream = ConstBitStream(bytes=bitstream)

        # Read the record TTL
        record.ttl = bitstream.read("uint:32")

        # Store the locator record count until we need it
        referral_count = bitstream.read("uint:8")

        # Store the EID prefix mask length until we need it
        eid_prefix_len = bitstream.read("uint:8")

        # Read the Negative Map_Reply action
        record.action = bitstream.read("uint:3")

        # Read the flags
        (record.authoritative, record.incomplete) = bitstream.readlist("2*bool")

        # Read reserved bits
        record._reserved1 = bitstream.read(11)

        # Read the signature count
        sig_count = bitstream.read("uint:4")

        # Read the map version
        record.map_version = bitstream.read("uint:12")

        # Read the EID prefix
        record.eid_prefix = read_afi_address_from_bitstream(bitstream, eid_prefix_len)

        # Read the locator records
        for dummy in range(referral_count):
            locator_record = LocatorRecord.from_bytes(bitstream)
            record.locator_records.append(locator_record)

        # TODO: Can't handle signatures yet! [LISP-Security]
        if sig_count:
            raise NotImplementedError("Cannot handle signatures yet")

        # Verify that the properties make sense
        record.sanitize()

        return record
def bzip0_decode(in_bytes):
    in_data = ConstBitStream(in_bytes)
    out_data = BitArray()
    try:
        while True:
            encoded_block_size = in_data.read(f'uint:{BLOCK_SIZE_BITS}')
            encoded_block = in_data.read(f'bytes:{encoded_block_size}')
            decoded_block = decode_block(encoded_block)
            out_data.append(decoded_block)
    except ReadError:
        pass
    return out_data.tobytes()
Exemplo n.º 43
0
    def parse_file_data(self, file_offset, data):
        # filter on the file ID for instance
        if file_offset.id == 64:
            # parse the data, in this example we are assuming the file data contains 2 sensor values of type int8
            # note this is a generator function, so multiple values can be returned
            s = ConstBitStream(bytes=bytearray(data))
            sensor_value = s.read("int:8")
            yield 'my-sensorvalue1', sensor_value, DataPointType.telemetry
            sensor_value = s.read("int:8")
            yield 'my-sensorvalue2', sensor_value, DataPointType.telemetry

        return
def bzip0_encode(in_bytes):
    in_data = ConstBitStream(in_bytes)
    out_data = BitArray()
    while in_data.bitpos < in_data.length:
        block_size = min((in_data.length - in_data.bitpos) // 8,
                         2**BLOCK_SIZE_BITS - 1)
        block_data = in_data.read(f'bytes:{block_size}')
        encoded_block = encode_block(block_data)
        encoded_block_size = len(encoded_block)
        out_data.append(Bits(uint=encoded_block_size, length=BLOCK_SIZE_BITS))
        out_data.append(encoded_block)
    return out_data.tobytes()
Exemplo n.º 45
0
def lz77_decode(encoded_data, window_bits=DEFAULT_WINDOW_BITS):
    encoded = ConstBitStream(encoded_data)
    tokens = []
    try:
        while True:
            pfx_dist = encoded.read(f'uint:{window_bits}')
            pfx_len = encoded.read(f'uint:{REFERENCE_SIZE_BITS}')
            next_ch = encoded.read('uint:8')
            tokens.append((pfx_dist, pfx_len, next_ch))
    except ReadError:
        pass
    return lz77_decode_from_tokens(tokens)
Exemplo n.º 46
0
  def parse_one_frame_from_buffer(self):
    retry       = True    # until we have one or don't have enough
    errors      = []
    frame       = None
    bits_parsed = 0
    while retry and len(self.buffer) > 0:
      try:
        self.s      = ConstBitStream(bytes=self.buffer)
        frame         = self.parse_frame()
        bits_parsed = self.s.pos
        self.shift_buffer(bits_parsed/8)
        retry = False         # got one, carry on
      except ReadError as e:       # not enough to read, carry on and wait for more
        retry = False
      except ParseError as e: # actual problem with current buffer, need to skip
        errors.append({
          "error"   : e.args[0],
          "buffer"  : list(self.buffer),
          "pos"     : self.s.pos,
          "skipped" : self.skip_bad_buffer_content()
        })

    info = {
      "parsed" : bits_parsed,
      "buffer" : len(self.buffer) * 8,
      "errors" : errors
    }
    return (frame, info)
Exemplo n.º 47
0
    def test_afi_2_with_prefixlen(self):
        '''
        Test decoding of AFI 2 (IPv6) prefixes
        '''
        afi_address_hex = '000220010db80102abcd0000000000000000'
        bitstream = ConstBitStream(hex=afi_address_hex)

        address = afi.read_afi_address_from_bitstream(bitstream, 64)

        self.assertEqual(address, IPv6Network(u'2001:db8:102:abcd::/64'))
        self.assertEqual(bitstream.pos, bitstream.len,
                         'unprocessed bits remaining in bitstream')

        new_bitstream = afi.get_bitstream_for_afi_address(address)

        self.assertEqual(new_bitstream.tobytes(), bitstream.tobytes())
Exemplo n.º 48
0
    def test_afi_0_without_prefixlen(self):
        '''
        Test en/decoding of empty AFI addresses
        '''
        afi_address_hex = '0000'
        bitstream = ConstBitStream(hex=afi_address_hex)

        address = afi.read_afi_address_from_bitstream(bitstream)

        self.assertIsNone(address, 'wrong address')
        self.assertEqual(bitstream.pos, bitstream.len,
                         'unprocessed bits remaining in bitstream')

        new_bitstream = afi.get_bitstream_for_afi_address(address)

        self.assertEqual(new_bitstream.tobytes(), bitstream.tobytes())
Exemplo n.º 49
0
    def test_afi_1_with_prefixlen(self):
        '''
        Test decoding of AFI 1 (IPv4) prefixes
        '''
        afi_address_hex = '0001c0000200'
        bitstream = ConstBitStream(hex=afi_address_hex)

        address = afi.read_afi_address_from_bitstream(bitstream, 24)

        self.assertEqual(address, IPv4Network(u'192.0.2.0/24'))
        self.assertEqual(bitstream.pos, bitstream.len,
                         'unprocessed bits remaining in bitstream')

        new_bitstream = afi.get_bitstream_for_afi_address(address)

        self.assertEqual(new_bitstream.tobytes(), bitstream.tobytes())
Exemplo n.º 50
0
def ff(file, mode='unknown'):
    '''Read header from data file.'''
    infile = open(file, 'r')

    oldheader = infile.read(8)

    # Finds "Dartmouth"
    s = ConstBitStream(filename=file)
    found = s.find('0x446172746d6f757468', bytealigned=True)
    if found:
        print("Found start code at byte offset %d." % found[0])
        # s0f0, length, bitdepth, height, width = s.readlist('hex: 16, uint: 16,
        # uint: 8, 2 * uint: 16')
        # print("Width %d, Height %d" % (width, height))
    else:
        print("No way!!")

    if oldheader[0:6] == "999999" and mode == 'unknown':
        if oldheader[6:8] == "01":
            mode = 'head'
        elif oldheader[6:8] == "04":
            mode = 'tail'
        else:
            print "ff(): Mode not defined in input or old header"
            return(1)
    elif oldheader[0:6] != "999999" and mode != 'tail':
        print "ff(): No valid GGSE header signature."
        return(1)

    if mode == 'head':
        header = infile.read(8184)
    elif mode == 'tail':
        infile.seek(-8192, os.SEEK_END)
        header = infile.read(8192)
    else:
        print "ff(): Unknown mode."
        return(1)

    infile.close()

    try:
        fhead = fs(header)
    except confp.ParsingError or confp.MissingSectionHeaderError:
        print "ff(): error parsing file: No header in " + mode + " of file?"
        fhead = 1

    return fhead
Exemplo n.º 51
0
 def parse(self):
     f = open(self.filename,"rb")
     self._bitstream = ConstBitStream(f)
     try:
         self.parse_header()
         self.parse_tags()
     finally:
         f.close()
Exemplo n.º 52
0
    def test_afi_0_with_prefixlen_0(self):
        '''
        Although a prefix length of 0 is allowed (since that is how many
        implementations transmit it on the wire)
        '''
        afi_address_hex = '0000'
        bitstream = ConstBitStream(hex=afi_address_hex)

        address = afi.read_afi_address_from_bitstream(bitstream, 0)

        self.assertIsNone(address, 'wrong address')
        self.assertEqual(bitstream.pos, bitstream.len,
                         'unprocessed bits remaining in bitstream')

        new_bitstream = afi.get_bitstream_for_afi_address(address)

        self.assertEqual(new_bitstream.tobytes(), bitstream.tobytes())
Exemplo n.º 53
0
def huff_decode(enc_file):
	enc = ConstBitStream(enc_file)
	
	tree = []

	tree_sz = enc.read("uint:8")
	while tree_sz > 0:
		s    = enc.read("bytes:1")
		c_sz = enc.read("uint:8")
		c    = BitArray(enc.read("bits:"+str(c_sz)))
		tree.append([s, c])
		tree_sz -= 1

	tree = sorted(tree, key = lambda v: v[1].len)
	
	text = ""
	while True:
		try:
			found = False
			for s,c in tree:
				if enc.startswith(c, enc.pos):
					print enc[enc.pos:].bin
					code = enc.read("bits:"+str(c.len))
					text += s
					found = True
					break
			if found == False:
				raise ReadError
		except ReadError:
			break
	
	return text
Exemplo n.º 54
0
    def handle_read(self):
       
        data0 = self.recv(160);
        if data0:            
            data = ConstBitStream(bytes=data0, length=160)
            # print "All: %s" % data.bin
 
            msize = data.read('intle:16')
            mtype = data.read('intle:16')
            mtime = data.read('intle:64')
 
            # RA: 
            ant_pos = data.bitpos
            ra = data.read('hex:32')
            data.bitpos = ant_pos
            ra_uint = data.read('uintle:32')
 
            # DEC:
            ant_pos = data.bitpos
            dec = data.read('hex:32')
            data.bitpos = ant_pos
            dec_int = data.read('intle:32')
             
            logging.debug("Size: %d, Type: %d, Time: %d, RA: %d (%s), DEC: %d (%s)" % (msize, mtype, mtime, ra_uint, ra, dec_int, dec))
                       
            (ra, dec, time) = coords.int_2_rads(ra_uint, dec_int, mtime)

            x = transformar_coordenadas(dec, ra)
            az,alt = x.get_azi_alt()

            #instancia o motor de azimute nos pinos 12, 16, 20 e 21 do RPi
            motor_az = Motor([31,33,35,37])
            motor_az.rpm = 5
            motor_az.mode = 2
            motor_az.move_to(az-self.az_anterior)
            self.az_anterior = az

            #instancia o motor de azimute nos pinos 32, 36, 38 e 40 do RPi
            motor_alt = Motor([32,36,38,40])
            motor_alt.rpm = 5
            motor_alt.mode = 2
            motor_alt.move_to(alt-self.alt_anterior)
            self.alt_anterior = alt

            logging.debug("Azimute: %d, Altitude: %d" % (az,alt))
            
            # envia as cordenadas para o Stellarium
            self.act_pos(ra, dec)
Exemplo n.º 55
0
    def from_bytes(cls, bitstream):
        '''
        Parse the given record and update properties accordingly
        '''
        record = cls()

        # Convert to ConstBitStream (if not already provided)
        if not isinstance(bitstream, ConstBitStream):
            if isinstance(bitstream, Bits):
                bitstream = ConstBitStream(auto=bitstream)
            else:
                bitstream = ConstBitStream(bytes=bitstream)

        # Read the record TTL
        record.ttl = bitstream.read('uint:32')

        # Store the locator record count until we need it
        locator_record_count = bitstream.read('uint:8')

        # Store the EID prefix mask length until we need it
        eid_prefix_len = bitstream.read('uint:8')

        # Read the Negative Map_Reply action
        record.action = bitstream.read('uint:3')

        # Read the flag
        record.authoritative = bitstream.read('bool')

        # Read reserved bits
        record._reserved1 = bitstream.read(12 + 4)

        # Read the map version
        record.map_version = bitstream.read('uint:12')

        # Read the EID prefix
        record.eid_prefix = read_afi_address_from_bitstream(bitstream,
                                                            eid_prefix_len)

        # Read the locator records
        for dummy in range(locator_record_count):
            locator_record = LocatorRecord.from_bytes(bitstream)
            record.locator_records.append(locator_record)

        # Verify that the properties make sense
        record.sanitize()

        return record
Exemplo n.º 56
0
   def __new__(cls, font_filename):
      """
      Create a new PGFFont from tile font file specified by 
      `font_filename`
      """
      log.info('Opening font file %s' % (font_filename,))
      bits = ConstBitStream(filename=font_filename)
      obj = super(PGFFont, cls).__new__(cls)
      obj.bits = bits

      log.info('Parsing Font Information')
      obj._size = bits.len / 8
      for (field_name, fmt_str, offset) in PGFFont._fields_:
         assert bits.bytepos == offset, \
             'Incorrect offset while parsing field %s: %x\n%s' % \
             (field_name, bits.bytepos, obj)
         setattr(obj, field_name, bits.read(fmt_str))

      log.info('Parsed %s' % (obj,))
      obj._validate_parse()
      obj.tables = OrderedDict()
      
      for name in PGFFont._int64_tables_:
         entries = getattr(obj, 'len_%s' % (name,))
         buf = bits.read('bytes: %d' % (entries * 2 * 4))
         obj.tables[name] = array('I', buf)

      for name in PGFFont._tables_:
         entries = getattr(obj, 'len_%s' % (name,))
         bpe = 64
         bpe_name = 'bpe_%s' % (name,)
         if bpe_name in obj.__dict__:
            bpe = getattr(obj, bpe_name)
         obj.tables[name] = Table(name, bits, entries, bpe)
         log.debug('Parsed table offset:%x - name=%s - %s' % 
                   (bits.bytepos, name, obj.tables[name]))

      obj.fontdatasize = (bits.len - bits.pos)/8
      obj.fontdata     = FontData(obj, bits, obj.fontdatasize)

      char = obj.tables['charmap'][ord(u'o')]
      return obj
Exemplo n.º 57
0
def mc_parser():
    out, buff = yield

    while True:
        header_bytes = yield from buff.read(24)
        header = ConstBitStream(header_bytes)

        readlist_fmt = 'uint:8, uint:8, uint:16'
        magic, opcode, key_length = header.readlist(readlist_fmt)
        extras_length, data_type, status = header.readlist(readlist_fmt)
        total_body_length = header.read('uint:32')
        opaque = header.read('uint:32')
        cas = header.read('uint:64')

        extras = None
        if extras_length:
            extras = yield from buff.read(extras_length)

        key = None
        if key_length:
            key = yield from buff.read(key_length)

        value_length = total_body_length - (key_length + extras_length)
        value = None
        if value_length:
            value = yield from buff.read(value_length)

        out.feed_data(MCResponse(opcode, data_type, status, cas,
                                 extras, key, value))
Exemplo n.º 58
0
  def encode(self, infile, outfile):
    s=''
    chars=[]

    print(BitArray(filename=infile).bin[2:])

    f=ConstBitStream(filename=infile)
    done=False
    eof=False
    while not done:
      found=False
      cursor=self.encoding
      while not found:
        try:
          bit=f.read('uint:1')
        except:
          eof=True
          bit=0
        cursor=cursor[bit+1]
        if len(cursor)==2: # leaf
          found=True
          val=cursor[1]
          s=s+chr(val)
          chars.append(val)
          if eof:
            done=True

    f=open(outfile, 'wb')
    f.write(s)
    f.close()

    print(chars)
    print(BitArray(filename=outfile).bin[2:])
Exemplo n.º 59
0
 def testReadList(self):
     s = CBS("0b10001111001")
     t = s.readlist("pad:1, uint:3, pad:4, uint:3")
     self.assertEqual(t, [0, 1])
     s.pos = 0
     t = s.readlist("pad:1, pad:5")
     self.assertEqual(t, [])
     self.assertEqual(s.pos, 6)
     s.pos = 0
     t = s.readlist("pad:1, bin, pad:4, uint:3")
     self.assertEqual(t, ["000", 1])
     s.pos = 0
     t = s.readlist("pad, bin:3, pad:4, uint:3")
     self.assertEqual(t, ["000", 1])
Exemplo n.º 60
-1
def parseAxisBlock(axBlkStr):
	ret = dict()
	if len(axBlkStr) != AX_BLK_SIZE:
		raise ValueError("Invalid passed string length")

	keys = ["status", "switches", "stopCode", "refPos", "motorPos",
	"posError", "auxPos", "vel", "torque", "analog"]

	statusKeys = ["moving", "motionMode1", "motionMode1", "findingEdge",
				  "homing", "homeP1Done", "homeP2Done", "coordMotion",
				  "movingNeg", "contourMode", "slewingMode", "stopping",
				  "finalDecel", "latchArmed", "offOnErrArmed", "motorOff"]

	# The f*****g galil is little endian, so [:2] splits off the segment of the string we want, and [::-1] reverses it
	statusBs = ConstBitStream(bytes=axBlkStr[:2][::-1])

	# Status is 16 boolean values packed into a uint_16
	statusVals = statusBs.readlist(["uint:1"]*16)

	# zip flags and names into dict
	zipped = zip(statusKeys, statusVals)

	vals = [dict(zipped)]
	vals.extend(struct.unpack(AX_BLK_PARSE_STR, axBlkStr))

	ret = dict(zip(keys, vals))

	return ret