コード例 #1
0
ファイル: huffman.py プロジェクト: dhda/Experiments
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
コード例 #2
0
 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))
コード例 #3
0
ファイル: parsingThermalData.py プロジェクト: mjs/python-cptv
def compression_1(f):
    # TODO: reuse frame arrays instead of allocating new ones all
    # the time.
    prevFrame = np.zeros((y_res, x_res), dtype="uint16")
    deltaFrame = np.zeros((y_res, x_res), dtype="int32")
    n = 0
    while f.read(1) == "F":
        print "=== FRAME %d ===" % n
        n += 1
        offset, bit_width, frame_size = get_frame_headers(f)
        print "bit width: %d" % bit_width

        stream = ConstBitStream(bytes=f.read(frame_size))
        val = stream.read('intle:32')
        deltaFrame[0][0] = val
        num_deltas = (x_res * y_res) - 1

        # offset by 1 b/c we already have initial value
        for i in range(1, num_deltas + 1):
            y = i // x_res
            x = i % x_res
            # Deltas are "snaked" so work backwards through every
            # second row.
            if y % 2 == 1:
                x = x_res - x - 1

            val += stream.read('int:' + str(bit_width))
            deltaFrame[y][x] = val

        # Calculate the frame by applying the delta frame to the
        # previously decompressed frame.
        frame = (prevFrame + deltaFrame).astype('uint16')

        print frame
        prevFrame = frame
コード例 #4
0
class SaveReader():
    """Class designed to retrieve basic data from files."""
    def __init__(self, file_name):
        self.file = open(file_name, 'r')
        self.stream = ConstBitStream(self.file)

    def __enter__(self):
        return self

    def __exit__(self, type_, value, traceback):
        self.file.close()

    def read_bytes(self, count):
        """Read a number of bytes and return a bitstring."""
        return self.stream.read(count * 8)

    def find_blocks(self):
        """Find all data blocks and return a tuple of their offsets."""
        return tuple(self.stream.findall('0x40000000', bytealigned=True))

    def read_int(self):
        """Read a 4 byte little endian int."""
        return self.stream.read(32).intle

    def read_ints(self, count):
        """Return a tuple of ints."""
        return tuple(
            map(lambda x: x.read(32).intle,
                self.read_bytes(count * 4).cut(32)))

    def read_string(self):
        """Read a string with the length given in the first 4 bytes."""
        return self.stream.read('bytes:{}'.format(self.read_int())).decode(
            "utf-8", 'replace')
コード例 #5
0
def _get_translation_tables():
    raw = ConstBitStream(filename='text_eng.dat', offset=0x8 * 8)
    tables = []

    try:
        while True:
            table_len = raw.read('intle:32')
            table = {}

            for _ in range(table_len):
                parsed_id, str_len = raw.readlist('intle:32, intle:32')
                parsed_str = raw.read('hex:{}'.format(str_len *
                                                      8))[:-4].decode('hex')

                # Replace random unicode shit with closest ASCII equivalent
                parsed_str = parsed_str.replace(
                    '\xe2\x80\x93', '-')  # U-2013 EN-DASH to ASCII dash
                table[parsed_id] = parsed_str.decode('ascii', 'ignore')

            tables.append(table)
    except ReadError:
        # EOF
        pass

    return tables
コード例 #6
0
    def png_to_gim(self,
                   png_file,
                   gim_file=None,
                   quant_type=QuantizeType.auto):
        # So there's no confusion.
        png_file = os.path.abspath(png_file)

        if gim_file == None:
            gim_file = os.path.splitext(png_file)[0] + ".gim"

        png_file = self.quantize_png(png_file, quant_type)

        data = ConstBitStream(filename=png_file)
        data.bytepos = 0x18

        options = ["-jar", "tools/gimexport.jar", png_file, gim_file, "3"]

        depth = data.read("int:8")
        color_type = data.read("int:8")

        if color_type == 3:  # Indexed
            options.append("true")
        else:
            options.append("false")

        self.process.start("java", options)
        self.process.waitForFinished(-1)
コード例 #7
0
    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
コード例 #8
0
ファイル: parser.py プロジェクト: patricklaw/mc
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))
コード例 #9
0
ファイル: day16.py プロジェクト: Lexicality/advent-of-code
 def consume(self, data: ConstBitStream) -> None:
     value = BitStream()
     keep_reading = True
     while keep_reading:
         keep_reading = data.read("bool")
         value += data.read(4)
     self.value = value.uint
コード例 #10
0
 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)
コード例 #11
0
ファイル: mp4.py プロジェクト: asrashley/dash-live
 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))
コード例 #12
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)
コード例 #13
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)
コード例 #14
0
    def from_bytes(cls, bitstream):
        '''
        Parse the given packet and update properties accordingly
        '''
        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 type
        type_nr = bitstream.read('uint:4')
        if type_nr != packet.message_type:
            msg = 'Invalid bitstream for a {0} packet'
            class_name = packet.__class__.__name__
            raise ValueError(msg.format(class_name))

        # Read if this is a reply
        packet.is_reply = bitstream.read('bool')

        # Skip reserved bits
        packet._reserved1 = bitstream.read(27)

        # Read the nonce
        packet.nonce = bitstream.read('bytes:8')

        # Read the key id
        packet.key_id = bitstream.read('uint:16')

        # Read the authentication data
        data_length = bitstream.read('uint:16')
        packet.authentication_data = bitstream.read('bytes:%d' % data_length)

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

        # Skip reserved bits
        packet._reserved2 = bitstream.read(8)

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

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

        # Read the reply
        packet.reply = read_afi_address_from_bitstream(bitstream)

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

        return packet
コード例 #15
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)
コード例 #16
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
コード例 #17
0
    def from_bytes(cls, bitstream):
        '''
        Parse the given packet and update properties accordingly
        '''
        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 type
        type_nr = bitstream.read('uint:4')
        if type_nr != packet.message_type:
            msg = 'Invalid bitstream for a {0} packet'
            class_name = packet.__class__.__name__
            raise ValueError(msg.format(class_name))

        # Read the flags
        has_xtr_site_id = bitstream.read('bool')

        # Skip reserved bits
        packet._reserved1 = bitstream.read(19)

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

        # Read the nonce
        packet.nonce = bitstream.read('bytes:8')

        # Read the key id
        packet.key_id = bitstream.read('uint:16')

        # Read the authentication data
        data_length = bitstream.read('uint:16')
        packet.authentication_data = bitstream.read('bytes:%d' % data_length)

        # Read the records
        for dummy in range(record_count):
            record = MapRegisterRecord.from_bytes(bitstream)
            packet.records.append(record)

        # Read the xtr-id and site-id
        if has_xtr_site_id:
            packet.xtr_id = bitstream.read('uint:128')
            packet.site_id = bitstream.read('uint:64')

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

        return packet
コード例 #18
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
コード例 #19
0
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()
コード例 #20
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)
コード例 #21
0
def lz77huff_decode(encoded_data, num_symbols, serialized_tree, window_bits=DEFAULT_WINDOW_BITS):
    bytestream = huffman_decode(encoded_data, num_symbols, serialized_tree, symbol_bits=window_bits)
    bits = ConstBitStream(bytestream)
    tokens = []
    try:
        while True:
            t = (bits.read(f'uint:{window_bits}'),
                 bits.read(f'uint:{window_bits}'),
                 bits.read(f'uint:{window_bits}'))
            tokens.append(t)
    except ReadError:
        pass
    return lz77_decode_from_tokens(tokens)
コード例 #22
0
def verifyBlocksX(image,
                  pixelWidth,
                  blockWidthEstimate,
                  combinedColors,
                  initializerPaletteADict,
                  initializerPaletteBDict,
                  override=False):
    '''This is a function used within frameLockOn().  It verifies the correct values for the X axis.'''

    calibratorBitsX = BitArray()
    for xBlock in range(17):
        snappedValue = colorSnap(scanBlock(image, pixelWidth, xBlock, 0),
                                 combinedColors)

        if xBlock % 2 == 0:
            calibratorBitsX.append(
                initializerPaletteADict.getValue(snappedValue))

        else:
            calibratorBitsX.append(
                initializerPaletteBDict.getValue(snappedValue))

    calibratorBitsX.reverse()
    readCalibratorX = ConstBitStream(calibratorBitsX)

    if readCalibratorX.read('uint:16') != blockWidthEstimate:
        if override == True:
            logging.warning(
                'blockWidthOverride is not equal to what was read on calibrator.  Aborting...'
            )

        else:
            logging.warning(
                'blockWidth verification does not match initial read.  This could be the result of \n'
                'sufficiently distorted frames.  Aborting...')

        return False

    if readCalibratorX.read('bool') != False:
        logging.warning('0,0 block unexpected value.  Aborting...')
        return False

    if override == True:
        logging.info('blockWidthOverride successfully verified.')

    else:
        logging.debug('blockWidth successfully verified.')

    return True
コード例 #23
0
ファイル: photos.py プロジェクト: RohanNijhawan/pyicloud
    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()
コード例 #24
0
ファイル: map_reply.py プロジェクト: SeleneLI/LIG_measurement
    def from_bytes(cls, bitstream):
        '''
        Parse the given packet and update properties accordingly
        '''
        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 type
        type_nr = bitstream.read('uint:4')
        if type_nr != packet.message_type:
            msg = 'Invalid bitstream for a {0} packet'
            class_name = packet.__class__.__name__
            raise ValueError(msg.format(class_name))

        # Read the flags
        (packet.probe,
         packet.enlra_enabled,
         packet.security) = bitstream.readlist('3*bool')

        # Skip reserved bits
        packet._reserved1 = bitstream.read(17)

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

        # Read the nonce
        packet.nonce = bitstream.read('bytes:8')

        # Read the records
        for dummy in range(record_count):
            record = MapReplyRecord.from_bytes(bitstream)
            packet.records.append(record)

        # If the security flag is set then there should be security data left
        # TODO: deal with security flag [LISP-Security]
        if packet.security:
            raise NotImplementedError('Handling security data is not ' +
                                      'implemented yet')

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

        return packet
コード例 #25
0
ファイル: base.py プロジェクト: hansomesong/pylisp
    def from_bytes(cls, bitstream, decode_payload=True):
        """
        Parse the given packet and update properties accordingly
        """
        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 version
        version = bitstream.read("uint:4")
        if version != packet.version:
            raise ValueError("Provided bytes do not contain an IPv6 packet")

        # Read the traffic class
        packet.traffic_class = bitstream.read("uint:8")

        # Read the flow label
        packet.flow_label = bitstream.read("uint:20")

        # Read the payload length
        payload_length = bitstream.read("uint:16")

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

        # Read the hop limit
        packet.hop_limit = bitstream.read("uint:8")

        # Read the source and destination addresses
        packet.source = IPv6Address(bitstream.read("uint:128"))
        packet.destination = IPv6Address(bitstream.read("uint:128"))

        # And the rest is payload
        packet.payload = bitstream.read("bytes:%d" % payload_length)

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

        # There should be no remaining bits
        if bitstream.pos != bitstream.len:
            raise ValueError("Bits remaining after processing packet")

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

        return packet
コード例 #26
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
コード例 #27
0
def lzwv_decode(in_array):
    max_entries = 2**MAX_CODE_LEN
    in_stream = ConstBitStream(in_array)
    dictionary = {i: bytes([i]) for i in range(256)}
    code_len = 9
    out_array = bytearray()
    while True:
        try:
            # Read a code
            k = in_stream.read(f'uint:{code_len}')

            # Look up code and emit value
            v = dictionary[k]
            out_array += v

            # Add v + v_next[0] to dictionary
            c = len(dictionary)
            if c < max_entries:
                if c == 2**code_len - 1:
                    code_len = min(MAX_CODE_LEN, code_len + 1)
                k_next = in_stream.peek(f'uint:{code_len}')
                if k_next < c:
                    v_next = dictionary[k_next]
                    dictionary[c] = v + v_next[:1]
                else:
                    dictionary[c] = v + v[:1]
        except ReadError:
            break
    return bytes(out_array)
コード例 #28
0
ファイル: encoder.py プロジェクト: blanu/Dust
  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:])
コード例 #29
0
ファイル: day16.py プロジェクト: Lexicality/advent-of-code
 def _consume_length(self, data: ConstBitStream) -> None:
     packet_length = data.read("uint:15")
     end_pos = data.pos + packet_length
     while data.pos < end_pos:
         self.contents.append(_parse_packet(data))
     if data.pos > end_pos:
         raise RuntimeError("Consumed too much data!")
コード例 #30
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:])
コード例 #31
0
    def integer(self, N, M, offset=0):
        '''
        Provide an array of random integers following uniform distribution.

        Parameters:
            N:  int
                Amount of provided integers.
            M:  int
                Upper range (exclusive) of the integers,
                Which can only take a value among 0, 1, ..., M-1.
            offset: int
                Amount of bits at the beginning of the file to be skipped over.

        Returns:
            result: 1d numpy array
        '''
        width = int(np.ceil(np.log2(M)))
        blob = ConstBitStream(filename=self.source)
        result = np.empty(N, dtype="u4") # 32-bit unsigned integer in native endian
        blob.pos = offset
        for i in range(N):
            while True:
                number = blob.read(width).uint
                if number < M:
                    result[i] = number
                    break
        return result
コード例 #32
0
ファイル: sh1kom1zue.py プロジェクト: Cyb3rk9/sh1kom1zue
def Hide():

    # file_in = 'xeyes' #Payload script
    # image_in = 'burger2.png' #Carrier image

    def bitPadder(data, image):
        '''
        Function to pad the bitstream so that it's
        the same size as the number of pixels in the
        carrier image
        '''
        padded_list = []
        for index in range(image.size[0] * image.size[1]):
            if index < len(data):
                padded_list.append(data[index])
                #If index refers to a bit in the bitstream, add it
            else:
                padded_list.append(random.randint(0, 1))
                #If the index has exceeded the size of the bitstream,
                #insert a random bit
        return padded_list

    size = os.path.getsize(file_in)
    bitsize = size * 8

    fh = open(file_in, 'rb')
    binary = fh.read()

    s = ConstBitStream(binary)
    bits = s.read(f'bin:{bitsize}')

    im = Image.open(image_in)
    imagecopy = im.copy()

    bitlist = list(bits)
    #list of 0's and 1's as strings

    #Append delimiter of 256 1's to bitlist
    for i in range(256):
        bitlist.append(1)
    # print(len(bitlist))

    padded_list = bitPadder(bitlist, imagecopy)
    #print(len(padded_list))

    #Encode the bitstream into the LSB of the blue channel
    pixels = imagecopy.load()
    rgb_im = imagecopy.convert('RGB')
    for y in range(imagecopy.size[1]):
        for x in range(imagecopy.size[0]):
            # if (y * image.size[0] + x) > len(file_bits):
            # return image
            r, g, b = rgb_im.getpixel((x, y))
            b_zero = (b & 0xFE)  # Sets LSB to zero
            new_bit = int(padded_list[(y * imagecopy.size[0]) + x])
            pixels[x, y] = (r, g, b_zero ^ new_bit)

    #Show the steganographic image, then save it
    imagecopy.show()
    imagecopy.save(output_name)
コード例 #33
0
def lzwf_decode(in_array, code_len=DEFAULT_CODE_LEN):
    max_entries = 2**code_len
    in_stream = ConstBitStream(in_array)
    dictionary = {i: bytes([i]) for i in range(256)}
    out_array = bytes()
    while True:
        try:
            # Read a code
            k = in_stream.read(f'uint:{code_len}')

            # Look up code and emit value
            v = dictionary[k]
            out_array = out_array + v

            # Add v + v_next[0] to dictionary
            c = len(dictionary)
            if c < max_entries:
                k_next = in_stream.peek(f'uint:{code_len}')
                if k_next < c:
                    v_next = dictionary[k_next]
                    dictionary[c] = v + v_next[:1]
                else:
                    dictionary[c] = v + v[:1]
        except ReadError:
            break
    return bytes(out_array)
コード例 #34
0
 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])
コード例 #35
0
ファイル: parser.py プロジェクト: Conaras/pyd7a
    def parse_one_command_from_buffer(self):
        retry = True  # until we have one or don't have enough
        errors = []
        cmd = None
        message_type = None
        bits_parsed = 0
        while retry and len(self.buffer) > 0:
            try:
                s = ConstBitStream(bytes=self.buffer)
                cmd_length, message_type = self.parse_serial_interface_header(
                    s)
                if message_type == MessageType.REBOOTED.value:
                    cmd = s.read("uint:8")
                elif message_type == MessageType.LOGGING.value:
                    cmd = (s.readlist('bytes:b', b=cmd_length)[0])
                else:
                    if self.skip_alp_parsing:
                        if s.length < cmd_length:
                            raise ReadError

                        cmd = s.read("bytes:" + str(cmd_length))
                    else:
                        cmd = AlpParser().parse(s, cmd_length)

                bits_parsed = s.pos
                self.shift_buffer(bits_parsed / 8)
                retry = False  # got one, carry on
            except ReadError:  # not enough to read, carry on and wait for more
                retry = False
            except Exception as e:  # actual problem with current buffer, need to skip
                errors.append({
                    "error":
                    e.args[0],
                    "buffer":
                    " ".join(map(lambda b: format(b, "02x"), self.buffer)),
                    "pos":
                    s.pos,
                    "skipped":
                    self.skip_bad_buffer_content()
                })

        info = {
            "parsed": bits_parsed,
            "buffer": len(self.buffer) * 8,
            "errors": errors
        }
        return (message_type, cmd, info)
コード例 #36
0
ファイル: udp.py プロジェクト: SeleneLI/LIG_measurement
    def from_bytes(cls, bitstream):
        '''
        Parse the given packet and update properties accordingly
        '''
        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 source and destination ports
        (packet.source_port,
         packet.destination_port) = bitstream.readlist('2*uint:16')

        # Store the length
        length = bitstream.read('uint:16')
        if length < 8:
            raise ValueError('Invalid UDP length')

        # Read the checksum
        packet.checksum = bitstream.read('uint:16')

        # And the rest is payload
        payload_bytes = length - 8
        packet.payload = bitstream.read('bytes:%d' % payload_bytes)

        # LISP-specific handling
        if packet.source_port == 4341 or packet.destination_port == 4341:
            # Payload is a LISP data packet
            from pylisp.packet.lisp.data import DataPacket
            packet.payload = DataPacket.from_bytes(packet.payload)
        elif packet.source_port == 4342 or packet.destination_port == 4342:
            # Payload is a LISP control message
            from pylisp.packet.lisp.control.base import ControlMessage
            packet.payload = ControlMessage.from_bytes(packet.payload)

        # There should be no remaining bits
        if bitstream.pos != bitstream.len:
            raise ValueError('Bits remaining after processing packet')

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

        return packet
コード例 #37
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
コード例 #38
0
 def __init__(self, rawbytes=None):
     data = ConstBitStream(rawbytes)
     self.unparsed = bytes()
     if data is not None:
         start = data.bytepos
         i = 0
         for p in self.parsemap:
             if isinstance(p, str):
                 pad = data.read(p)
                 self.values[f"UNPARSED_{i}"] = pad
             else:
                 self.values[p.name] = p(data)
             i += 1
         rest = data.read("bin")
         if rest:
             self.values[f"UNPARSED_REST"] = rest
         self.rawdata = data.bytes[start:data.bytepos]
コード例 #39
0
ファイル: header.py プロジェクト: pombredanne/py-swf-parse
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()
コード例 #40
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()
コード例 #41
0
def handleDump(fileObj):
    tech = []
    raw = ConstBitStream(fileObj)
    try:
        while True:
            tech.append(raw.read('floatle:32'))
    except:
        pass
    del(raw)
    return tech 
コード例 #42
0
def convert_msk(filename, out_file):
    data = ConstBitStream(filename=filename)

    magic = data.read("bytes:4")
    size = data.read("uintle:32")
    w = data.read("uintle:16")
    h = data.read("uintle:16")
    cmp_size = data.read("uintle:32")

    print(w, h, cmp_size)
    w = adjust_w(w)
    chunk = data.read(cmp_size * 8)
    chunk = decompress(chunk)
    dump_to_file(chunk)
    chunk = get_grayscale(chunk, w, h, crop=False)
    chunk.save(out_file)


################################################################################
コード例 #43
0
ファイル: pypgf.py プロジェクト: Falaina/pypgf
   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
コード例 #44
0
def _get_translation_tables():
    raw = ConstBitStream(filename='bestiary/com2us_data/text_eng.dat', offset=0x8 * 8)
    tables = []

    try:
        while True:
            table_len = raw.read('intle:32')
            table = {}

            for _ in range(table_len):
                parsed_id, str_len = raw.readlist('intle:32, intle:32')
                parsed_str = binascii.a2b_hex(raw.read('hex:{}'.format(str_len * 8))[:-4])
                table[parsed_id] = parsed_str.decode("utf-8")

            tables.append(table)
    except ReadError:
        # EOF
        pass

    return tables
コード例 #45
0
    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
コード例 #46
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')
			
			#______ 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)
コード例 #47
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))
コード例 #48
0
ファイル: base.py プロジェクト: SeleneLI/LIG_measurement
    def from_bytes(cls, bitstream, prefix_len=None):
        '''
        Look at the type of the message, instantiate the correct class and
        let it parse the message.
        '''
        # 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)

        # Skip the reserved bits
        rsvd1 = bitstream.read(8)

        # Read the flags (and ignore them, no flags are defined yet)
        flags = bitstream.readlist('8*bool')

        # Read the type
        type_nr = bitstream.read('uint:8')

        # Skip the reserved bits
        rsvd2 = bitstream.read(8)

        # Read the length
        length = bitstream.read('uint:16')

        # Read the data
        data = bitstream.read(length * 8)

        # Look for the right class
        from pylisp.utils.lcaf import type_registry
        type_class = type_registry.get_type_class(type_nr)
        if not type_class:
            raise ValueError("Can't handle LCAF type {0}".format(type_nr))

        # Let the specific class handle it from now on
        return type_class._from_data_bytes(data, prefix_len,
                                           rsvd1, flags, rsvd2)
コード例 #49
0
    def from_bytes(cls, bitstream):
        '''
        Parse the given packet and update properties accordingly
        '''
        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 type
        type_nr = bitstream.read('uint:4')
        if type_nr != packet.message_type:
            msg = 'Invalid bitstream for a {0} packet'
            class_name = packet.__class__.__name__
            raise ValueError(msg.format(class_name))

        # Skip reserved bits
        packet._reserved1 = bitstream.read(20)

        # Store the record count
        record_count = bitstream.read('uint:8')

        # Store the nonce
        packet.nonce = bitstream.read('bytes:8')

        # Read the records
        for dummy in range(record_count):
            record = MapReferralRecord.from_bytes(bitstream)
            packet.records.append(record)

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

        return packet
コード例 #50
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
コード例 #51
0
    def png_to_gim(self, png_file, gim_file=None, quant_type=QuantizeType.auto):
        # So there's no confusion.
        png_file = os.path.abspath(png_file)

        if gim_file == None:
            gim_file = os.path.splitext(png_file)[0] + ".gim"

        png_file = self.quantize_png(png_file, quant_type)

        data = ConstBitStream(filename=png_file)
        data.bytepos = 0x18

        options = ["-jar", "tools/gimexport.jar", png_file, gim_file, "3"]

        depth = data.read("int:8")
        color_type = data.read("int:8")

        if color_type == 3:  # Indexed
            options.append("true")
        else:
            options.append("false")

        self.process.start("java", options)
        self.process.waitForFinished(-1)
コード例 #52
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
コード例 #53
0
ファイル: servidor.py プロジェクト: israelps/telescopioRpi
    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)
コード例 #54
0
    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')

        # Skip over reserved bits
        bitstream.read(8)

        # Read the fragment offset
        packet.fragment_offset = bitstream.read('uint:13')

        # Skip over reserved bits
        bitstream.read(2)

        # Read the more fragments
        packet.more_fragments = bitstream.read('bool')

        # Read the identification
        packet.identification = bitstream.read('uint:32')

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

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

        return packet
コード例 #55
0
 def load(self, filename):
   
   self.filename = filename
   dive = ConstBitStream(filename = os.path.join(common.editor_config.data01_dir, DIVE_DIR, self.filename))
   
   # Header
   # XX XX XX XX -- ??? (Always 0x02)
   # XX XX XX XX -- Header length? (Always 0x10)
   # XX XX XX XX -- File size - header length
   # XX XX XX XX -- Padding?
   dive.read(32)
   header_len = dive.read("uintle:32")
   file_len   = dive.read("uintle:32")
   dive.read(32)
   
   num_rows = dive.read("uintle:32")
   
   for i in range(num_rows):
     offset = dive.read("uintle:32") + header_len
     row    = dive[offset * 8 : (offset + ROW_LEN) * 8].readlist("uint:8," * ROW_LEN)
     self.rows.append(row)
コード例 #56
0
 def load(self, filename):
   filename = filename.lower()
   
   if not filename in DIR_MAP:
     _LOGGER.error("Invalid nonstop file: %s" % filename)
     return
   
   self.filename = filename
   
   script_dir = DIR_MAP[filename]
   self.script_pack = ScriptPack(script_dir, common.editor_config.data01_dir)
   
   file_order = []
   
   # --- NONSTOP FORMAT ---
   # XX XX -- ???
   # XX XX -- Number of lines (not necessarily files)
   # 
   # [68 bytes per line]
   # XX XX       -- File number
   # XX XX XX XX
   #  * 0x00000000 = normal line
   #  * 0x01000100 = chatter
   #  * 0x01000000 = ??? (Only appears in SDR2)
   #  * 0x02000000 = ??? (Only appears in SDR2)
   #  * 0x03000000 = ??? (Only appears in SDR2)
   #  * 0x04000000 = ??? (Only appears in SDR2)
   # XX XX       -- Ammo ID that reacts to this line.
   # XX XX       -- Converted line ID that reacts to this line.
   # XX XX       -- ???
   # XX XX       -- 1 = has a weak point, 0 = has no weak point
   # XX XX       -- The amount of time before the next line is shown (in sixtieths of seconds (frames?)).
   # XX XX       -- Unknown (Possibly line orientation? Only 0 in the first game, but sometimes 2 in the second.)
   # XX XX       -- Effect used when transitioning text in.
   # XX XX       -- Effect used when transitioning text out.
   #  * 0: fade
   #  * 1: spin in/out
   #  * 2: zoom out
   #  * 3: slide in/out
   # XX XX       -- The amount of the the line stays on-screen (in sixtieths of seconds (frames?)).
   # XX XX       -- Initial X position (text centered around this pos).
   # XX XX       -- Initial Y position (text centered around this pos).
   # XX XX       -- Text velocity.
   # XX XX       -- Angle of motion.
   # XX XX       -- Initial text zoom (in percent).
   # XX XX       -- Change in zoom over time (in percent).
   #  * 90% means it gradually shrinks.
   #  * 100% means it stays the same size the whole time.
   #  * 110% means it gradually grows.
   # XX XX       -- 0 = no shake, 1 = shake
   # XX XX       -- Rotate the text clockwise to this angle.
   # XX XX       -- Text spins clockwise at this rate.
   # XX XX       -- Speaker (00 00 if chatter)
   # XX XX       -- Sprite ID (00 00 if chatter)
   # XX XX XX XX -- ???
   # XX XX       -- Voice index (FF FF if chatter)
   # XX XX       -- ???
   # XX XX       -- Chapter
   # XX XX XX XX -- ??? (padding?)
   nonstop = ConstBitStream(filename = os.path.join(common.editor_config.data01_dir, NONSTOP_DIR, self.filename))
   
   self.magic = nonstop.read(16)
   num_lines = nonstop.read('uintle:16')
   
   # Four byte header plus 68 bytes per line.
   if nonstop.len < (4 + (num_lines * 68)) * 8:
     raise Exception("Invalid nonstop file.")
   
   prev_non_chatter = -1
   self.lines = []
   
   for i in range(num_lines):
     line              = NonstopLine()
     
     line.file_num     = nonstop.read('uintle:16')
     
     line.line_type    = nonstop.read(32)
     if line.line_type in LINE_TYPE_MAP:
       line.line_type = LINE_TYPE_MAP[line.line_type]
     
     line.ammo_id      = nonstop.read('intle:16')
     line.converted_id = nonstop.read('intle:16')
     line.unknown1     = nonstop.read(16)
     line.weak_point   = nonstop.read('uintle:16')
     
     line.delay        = nonstop.read('intle:16')
     line.orientation  = nonstop.read('intle:16')
     line.in_effect    = nonstop.read('intle:16')
     line.out_effect   = nonstop.read('intle:16')
     line.time_visible = nonstop.read('intle:16')
     line.x_start      = nonstop.read('intle:16')
     line.y_start      = nonstop.read('intle:16')
     line.velocity     = nonstop.read('intle:16')
     line.angle        = nonstop.read('intle:16')
     line.zoom_start   = nonstop.read('intle:16')
     line.zoom_change  = nonstop.read('intle:16')
     line.shake        = nonstop.read('intle:16')
     line.rot_angle    = nonstop.read('intle:16')
     line.spin_vel     = nonstop.read('intle:16')
     
     line.speaker      = nonstop.read('intle:16')
     
     # Since we mess with speaker a little bit later, we want to keep the ID for the sprite.
     line.char_id      = line.speaker
     line.sprite_id    = nonstop.read('intle:16')
     
     line.unknown3     = nonstop.read(32)
     line.voice_id     = nonstop.read('intle:16')
     line.unknown4     = nonstop.read(16)
     line.chapter      = nonstop.read('intle:16')
     line.unknown5     = nonstop.read(32)
     line.unknown6     = nonstop.read(64)
     
     format = copy.deepcopy(TEXT_FORMATS[common.SCENE_MODES.debate])
     format.orient = TEXT_ORIENT.hor if line.orientation == 0 else TEXT_ORIENT.ver
     format.align  = TEXT_ALIGN.center
     
     if format.orient == TEXT_ORIENT.ver:
       format.y = format.h
       format.x = format.w / 3.5
     
     self.script_pack[line.file_num].scene_info.format = format
     
     if line.line_type == NONSTOP_LINE_TYPE.normal:
       prev_non_chatter = line.file_num
       
       # Fixing some weirdness.
       # if filename in ["nonstop_06_003.dat", "nonstop_06_005.dat", "nonstop_06_006.dat", "nonstop_06_007.dat"] and line.speaker == 16:
         # line.speaker = 15
       # if filename[:10] == "nonstop_06" and int(filename[11:14]) >= 10 and line.speaker == 10:
         # line.speaker = 18
       # if filename in ["nonstop_02_003.dat", "nonstop_02_005.dat", "nonstop_04_005.dat", "nonstop_04_006.dat"] and line.speaker == 10:
         # line.speaker = 18
       
       self.script_pack[line.file_num].scene_info.speaker = line.speaker
       
       sprite = SpriteId(SPRITE_TYPE.stand, line.char_id, line.sprite_id)
       self.script_pack[line.file_num].scene_info.sprite = sprite
       
       voice = VoiceId(line.speaker, line.chapter, line.voice_id)
       self.script_pack[line.file_num].scene_info.voice = voice
       
       self.script_pack[line.file_num].scene_info.special = common.SCENE_SPECIAL.debate
     
     elif "hanron" in str(line.line_type):
       
       self.script_pack[line.file_num].scene_info.speaker = line.speaker
       
       sprite = SpriteId(SPRITE_TYPE.stand, line.char_id, line.sprite_id)
       self.script_pack[line.file_num].scene_info.sprite = sprite
       
       voice = VoiceId(line.speaker, line.chapter, line.voice_id)
       self.script_pack[line.file_num].scene_info.voice = voice
       
       self.script_pack[line.file_num].scene_info.special   = common.SCENE_SPECIAL.hanron
     
     elif line.line_type == NONSTOP_LINE_TYPE.chatter:
       self.script_pack[line.file_num].scene_info.speaker   = -1
       self.script_pack[line.file_num].scene_info.special   = common.SCENE_SPECIAL.chatter
       self.script_pack[line.file_num].scene_info.extra_val = prev_non_chatter
     
     else:
       _LOGGER.error("Invalid line type: %s" % line.line_type)
     
     file_order.append(line.file_num)
     self.lines.append(line)
   
   for index in xrange(len(self.script_pack)):
     if not index in file_order:
       file_order.append(index)
   
   self.script_pack.script_files = [self.script_pack[i] for i in file_order]
コード例 #57
0
ファイル: unpacker.py プロジェクト: nilp0inter/pydvbstp
def unpack(raw):
    """Unpack ``raw`` and return an object."""
    packet = ConstBitStream(raw)

    version = packet.read('uint:2')
    reserved = packet.read('uint:3')
    encryption = packet.read('uint:2')
    has_crc = packet.read('uint:1')
    total_segment_size = packet.read('uint:24')
    payload_id = packet.read('uintbe:8')
    segment_id = packet.read('uintbe:16')
    segment_version = packet.read('uintbe:8')
    section_number = packet.read('uint:12')
    last_section_number = packet.read('uint:12')
    compression = packet.read('uint:3')
    has_provider_id = packet.read('uint:1')
    private_header_length = packet.read('uint:4')

    if has_provider_id:
        provider_id = packet.read('uintbe:32')
    else:
        provider_id = None

    private_header_data = packet.read('bytes:' + str(private_header_length))

    if has_crc:
        payload = raw[packet.bytepos:-4]
        crc = struct.unpack('!I', raw[-4:])[0]
    else:
        crc = None
        payload = raw[packet.bytepos:]

    del raw
    del packet

    return Section(**locals())
コード例 #58
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))
			calibrate = raw_input("Do you want to calibrate? - Y/N \n")
			if calibrate == "Y":
				
				#Calibration GUI

				app = Tk()
				#app.title("Calibration")

				#app.configure(background = "grey")

				tframe = Frame(app)
				tframe.pack()

				lrframe = Frame(app)
				lrframe.pack()

				dframe = Frame(app)
				dframe.pack()

				bframe = Frame(app)
				bframe.pack(side = BOTTOM)

				def upCallBack():
					ser.write('w\n');
					#tkMessageBox.showinfo( "UP")


				def downCallBack():
					ser.write("s\n");
					#tkMessageBox.showinfo( "DOWN")
				def leftCallBack():
					ser.write("a\n");
					#tkMessageBox.showinfo( "LEFT")

				def rightCallBack():
					ser.write("d\n");
					#tkMessageBox.showinfo( "RIGHT")
				def close_window():
					app.destroy();


				UpButton = Button(tframe, text = "Up", command = upCallBack);
				UpButton.pack()
				DownButton = Button(dframe, text = "Down", command = downCallBack);
				DownButton.pack()
				LeftButton = Button(lrframe, text = "Left", command = leftCallBack);
				LeftButton.pack( side = LEFT)
				RightButton = Button(lrframe, text = "Right", command = rightCallBack);
				RightButton.pack( side = LEFT)
				DoneButton = Button(bframe, text = "Done", command = close_window);
				DoneButton.pack( side = BOTTOM)

				app.mainloop()