当前位置: 首页>>代码示例>>Python>>正文


Python ConstBitStream.peek方法代码示例

本文整理汇总了Python中bitstring.ConstBitStream.peek方法的典型用法代码示例。如果您正苦于以下问题:Python ConstBitStream.peek方法的具体用法?Python ConstBitStream.peek怎么用?Python ConstBitStream.peek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在bitstring.ConstBitStream的用法示例。


在下文中一共展示了ConstBitStream.peek方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: from_bytes

# 需要导入模块: from bitstring import ConstBitStream [as 别名]
# 或者: from bitstring.ConstBitStream import peek [as 别名]
    def from_bytes(cls, bitstream):
        '''
        Look at the type of the message, instantiate the correct class and
        let it parse the message.
        '''
        from pylisp.packet.lisp.control import type_registry

        # 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)

        # Peek at the bitstream to see which type it is
        type_nr = bitstream.peek('uint:4')

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

        # Let the specific class handle it from now on
        return type_class.from_bytes(bitstream)
开发者ID:SeleneLI,项目名称:LIG_measurement,代码行数:26,代码来源:base.py

示例2: ConstBitStream

# 需要导入模块: from bitstring import ConstBitStream [as 别名]
# 或者: from bitstring.ConstBitStream import peek [as 别名]
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from bitstring import ConstBitStream
import sys

c = ConstBitStream(filename=sys.argv[1])
#Most log entry ends with 0A, this helps us to seek to the beginning of an entry
start = c.find('0x0A', bytealigned=True)[0]
#Seek 8 byte into the stream to skip EOL from previus log entry
start += 8

c.pos = start

while True:
  #Read and print the binary log header
  header = c.readlist('8*uintle:32')
  print header
  #Get the size in bits of the log message
  msgSize = (header[0] * 8 - 256)
  #Move pointer after message
  c.pos += msgSize
  #Check if EOL is present after message, if true, move pointer 8 byte
  if c.peek(8).hex == '0a':
    c.pos += 8
开发者ID:intenso,项目名称:plogclient,代码行数:27,代码来源:plogprinter.py

示例3: from_bytes

# 需要导入模块: from bitstring import ConstBitStream [as 别名]
# 或者: from bitstring.ConstBitStream import peek [as 别名]
    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 IPv4 packet')

        # Read the header length
        ihl = bitstream.read('uint:4')
        if ihl < 5:
            raise ValueError('Invalid IPv4 header length')

        # Now that we know the length of the header we store it to be able
        # to easily recalculate the header checksum later
        remaining_header_bits = (ihl * 32) - 8
        header = (BitStream('uint:4=4, uint:4=%d' % ihl) +
                  bitstream.peek(remaining_header_bits))

        # Read the type of service
        packet.tos = bitstream.read('uint:8')

        # Read the total length
        total_length = bitstream.read('uint:16')
        if total_length < ihl * 4:
            raise ValueError('Total length is shorter than the header')

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

        # Read the flags
        (reserved,
         packet.dont_fragment,
         packet.more_fragments) = bitstream.readlist('3*bool')

        if reserved:
            raise ValueError('Reserved flag must be 0')

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

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

        # Read the protocol number
        packet.protocol = bitstream.read('uint:8')

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

        # Set the checksum bits in the header to 0 and re-calculate
        header[80:96] = BitStream(16)
        my_checksum = checksum.ones_complement(header.bytes)

        if my_checksum != header_checksum:
            raise ValueError('Header checksum does not match')

        # Read the source and destination addresses
        packet.source = IPv4Address(bitstream.read('uint:32'))
        packet.destination = IPv4Address(bitstream.read('uint:32'))

        # Read the options
        option_len = (ihl - 5) * 4
        packet.options = bitstream.read('bytes:%d' % option_len)

        # And the rest is payload
        payload_bytes = (total_length) - (ihl * 4)
        packet.payload = bitstream.read('bytes:%d' % payload_bytes)

        if decode_payload:
            payload_class = protocol_registry.get_type_class(packet.protocol)
            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
开发者ID:SeleneLI,项目名称:LIG_measurement,代码行数:94,代码来源:ipv4.py

示例4: __init__

# 需要导入模块: from bitstring import ConstBitStream [as 别名]
# 或者: from bitstring.ConstBitStream import peek [as 别名]
class FileReader:
    """ Some basic functionality for reading data from Binary files. """

    def __init__(self, filename):
        self.filename = filename
        if isinstance(filename, str):
            self.file = open(filename, 'rb')
            self.bits = ConstBitStream(open(filename, 'rb'))

    def __enter__(self):
        return self

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

    def skip_bytes(self,count=1):
        self.bits.read('pad:{0}'.format(count*8))

    def peek_int(self):
        return self.bits.peek(32).uintle

    def forward_to_first_non_zero_byte(self, start, end):
        self.bits.pos = start
        while self.bits.pos < end and self.bits.read(8).intle == 0:
            pass
        self.bits.pos -= 8

    def read_strings_from_block(self, start, end, stopAtEmptyString=False):
        self.bits.pos = start
        r = []
        while self.bits.pos < end:
            s = self.read_string()
            if s == "" and stopAtEmptyString:
                return r
            r.append(s)
        return tuple(r)

    def read_int(self):
        """ Read a single little endian 4 byte integer """
        return self.bits.read(32).intle

    def findall(self,bs):
        return self.bits.findall(bs,bytealigned=True)

    def read_bytes(self, count):
        return self.bits.read(count*8)

    def read_byte(self, skip = 0):
        i = self.bits.read(8).uint
        if skip > 0:
            self.skip_bytes(skip)

        return i

    def read_string_safe(self):
        return self.bits.read('bytes:{0}'.format(self.read_byte(skip=3))).decode("utf-8", 'replace')

    def find(self, bs, start, end):
        return self.bits.find(bs,start, end, True )

    def find_first(self, bs):
        return self.bits.find(bs)

    def extract_compressed_payloads(self):
        files = []
        occ = self.findall('0x789C')

        i = 0
        readSize = 2**12

        for pos in occ:
            self.bits.pos = pos

            #read the start of the stream into a buffer.
            if (self.bits.length - self.pos) < 8*2**12:
                readSize = int((self.bits.length - self.pos) / 8)

            buf = self.bits.read('bytes:{0}'.format(readSize))
            zo = zlib.decompressobj()

            #start the decompression
            try:
                stream = zo.decompress(buf)
            except zlib.error: # right magic number but not a zlib stream.
                continue

            while zo.unused_data == b'' and readSize >= 2**12:
                if (self.bits.length - self.pos) < 8*2**12:
                    readSize = int((self.bits.length - self.pos) / 8)

                block = self.bits.read('bytes:{0}'.format(readSize))
                if len(block)> 0:
                    try:
                        stream += zo.decompress(block)
                    except zlib.error:
                        pass
                else:
                    break # we've reached EOF

            with open(self.filename + '_' + str(i) + '.decompressed', 'wb') as fh:
#.........这里部分代码省略.........
开发者ID:rivarolle,项目名称:civ5-saveparser,代码行数:103,代码来源:FileReader.py


注:本文中的bitstring.ConstBitStream.peek方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。