Python struct.calcsize() Examples

The following are 30 code examples of struct.calcsize(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module struct , or try the search function .
Example #1
Source File: vachat.py    From The-chat-room with MIT License 10 votes vote down vote up
def run(self):
        print("VEDIO server starts...")
        self.sock.bind(self.ADDR)
        self.sock.listen(1)
        conn, addr = self.sock.accept()
        print("remote VEDIO client success connected...")
        data = "".encode("utf-8")
        payload_size = struct.calcsize("L")
        cv2.namedWindow('Remote', cv2.WINDOW_AUTOSIZE)
        while True:
            while len(data) < payload_size:
                data += conn.recv(81920)
            packed_size = data[:payload_size]
            data = data[payload_size:]
            msg_size = struct.unpack("L", packed_size)[0]
            while len(data) < msg_size:
                data += conn.recv(81920)
            zframe_data = data[:msg_size]
            data = data[msg_size:]
            frame_data = zlib.decompress(zframe_data)
            frame = pickle.loads(frame_data)
            cv2.imshow('Remote', frame)
            if cv2.waitKey(1) & 0xFF == 27:
                break 
Example #2
Source File: buddy.py    From ds_store with MIT License 6 votes vote down vote up
def read(self, size_or_format):
        if isinstance(size_or_format, (str, unicode, bytes)):
            size = struct.calcsize(size_or_format)
            fmt = size_or_format
        else:
            size = size_or_format
            fmt = None

        if self._size - self._pos < size:
            raise BuddyError('Unable to read %lu bytes in block' % size)

        data = self._value[self._pos:self._pos + size]
        self._pos += size
        
        if fmt is not None:
            if isinstance(data, bytearray):
                return struct.unpack_from(fmt, bytes(data))
            else:
                return struct.unpack(fmt, data)
        else:
            return data 
Example #3
Source File: aff4_map.py    From pyaff4 with Apache License 2.0 6 votes vote down vote up
def LoadFromURN(self):
        map_urn = self.urn.Append("map")
        map_idx_urn = self.urn.Append("idx")

        # Parse the map out of the map stream. If the stream does not exist yet
        # we just start with an empty map.
        try:
            with self.resolver.AFF4FactoryOpen(map_idx_urn) as map_idx:
                self.targets = [rdfvalue.URN(utils.SmartUnicode(x))
                                for x in map_idx.Read(map_idx.Size()).splitlines()]

            with self.resolver.AFF4FactoryOpen(map_urn) as map_stream:
                read_length = struct.calcsize(Range.format_str)
                while 1:
                    data = map_stream.Read(read_length)
                    if not data:
                        break
                    range = self.deserializeMapPoint(data)
                    if range.length > 0:
                        self.tree.addi(range.map_offset, range.map_end, range)


        except IOError:
            traceback.print_exc()
            pass 
Example #4
Source File: win32.py    From rekall with GNU General Public License v2.0 6 votes vote down vote up
def ParseMemoryRuns(self, fhandle):
        # Set acquisition mode. If the driver does not support this mode it will
        # just fall back to the default.
        win32file.DeviceIoControl(
            fhandle, CTRL_IOCTRL,
            struct.pack("I", PMEM_MODE_PTE), 4, None)

        result = win32file.DeviceIoControl(
            fhandle, INFO_IOCTRL, b"", 102400, None)

        fmt_string = "Q" * len(self.FIELDS)
        self.memory_parameters = dict(zip(self.FIELDS, struct.unpack_from(
            fmt_string, result)))

        offset = struct.calcsize(fmt_string)
        for x in range(self.memory_parameters["NumberOfRuns"]):
            start, length = struct.unpack_from("QQ", result, x * 16 + offset)
            self.add_run(start, start, length, self.fhandle_as) 
Example #5
Source File: registry.py    From rekall with GNU General Public License v2.0 6 votes vote down vote up
def _decode_data(self, data):
        """Decode the data according to our type."""
        valtype = str(self.Type)

        if valtype in ["REG_DWORD", "REG_DWORD_BIG_ENDIAN", "REG_QWORD"]:
            if len(data) != struct.calcsize(self.value_formats[valtype]):
                return obj.NoneObject(
                    "Value data did not match the expected data "
                    "size for a {0}".format(valtype))

        if valtype in ["REG_SZ", "REG_EXPAND_SZ", "REG_LINK"]:
            data = data.decode('utf-16-le', "ignore")

        elif valtype == "REG_MULTI_SZ":
            data = data.decode('utf-16-le', "ignore").split('\0')

        elif valtype in ["REG_DWORD", "REG_DWORD_BIG_ENDIAN", "REG_QWORD"]:
            data = struct.unpack(self.value_formats[valtype], data)[0]

        return data 
Example #6
Source File: winpmem.py    From rekall with GNU General Public License v2.0 6 votes vote down vote up
def ParseMemoryRuns(self):
        self.runs = []

        result = win32file.DeviceIoControl(
            self.fd, INFO_IOCTRL, "", 102400, None)

        fmt_string = "Q" * len(self.FIELDS)
        self.memory_parameters = dict(zip(self.FIELDS, struct.unpack_from(
                    fmt_string, result)))

        self.dtb = self.memory_parameters["CR3"]
        self.kdbg = self.memory_parameters["KDBG"]

        offset = struct.calcsize(fmt_string)

        for x in range(self.memory_parameters["NumberOfRuns"]):
            start, length = struct.unpack_from("QQ", result, x * 16 + offset)
            self.runs.append((start, length)) 
Example #7
Source File: arm_chromeos.py    From script.module.inputstreamhelper with MIT License 6 votes vote down vote up
def chromeos_offset(self):
        """Calculate the Chrome OS losetup start offset"""
        part_format = '<16s16sQQQ72s'
        entries_start, entries_num, entry_size = self.gpt_header()  # assuming partition table is GPT
        lba_size = config.CHROMEOS_BLOCK_SIZE  # assuming LBA size
        self.seek_stream(entries_start * lba_size)

        if not calcsize(part_format) == entry_size:
            log(4, 'Partition table entries are not 128 bytes long')
            return 0

        for index in range(1, entries_num + 1):  # pylint: disable=unused-variable
            # Entry: type_guid, unique_guid, first_lba, last_lba, attr_flags, part_name
            _, _, first_lba, _, _, part_name = unpack(part_format, self.read_stream(entry_size))
            part_name = part_name.decode('utf-16').strip('\x00')
            if part_name == 'ROOT-A':  # assuming partition name is ROOT-A
                offset = first_lba * lba_size
                break

        if not offset:
            log(4, 'Failed to calculate losetup offset.')
            return 0

        return offset 
Example #8
Source File: p0f.py    From dionaea with GNU General Public License v2.0 6 votes vote down vote up
def handle_io_in(self, data):
        fmt = "IIB20s40sB30s30sBBBhHi"
        if len(data) != calcsize(fmt):
            return 0
        values = unpack(fmt, data)
        names=["magic","id","type","genre","detail","dist","link",
               "tos","fw","nat","real","score","mflags","uptime"]
        icd = incident(origin='dionaea.modules.python.p0f')
        for i in range(len(values)):
            s = values[i]
            if type(s) == bytes:
                if s.find(b'\x00'):
                    s = s[:s.find(b'\x00')]
                try:
                    s = s.decode("ascii")
                except UnicodeDecodeError:
                    logger.warning("Unable to decode p0f information %s=%r", i, s, exc_info=True)
                icd.set(names[i], s)
            elif type(s) == int:
                icd.set(names[i], str(s))
        icd.set('con',self.con)
        icd.report()
        self.close()
        return len(data) 
Example #9
Source File: aes-file-decrypt.py    From Effective-Python-Penetration-Testing with MIT License 6 votes vote down vote up
def decrypt_file(key, filename, chunk_size=24*1024):
        
    output_filename = os.path.splitext(filename)[0]

    with open(filename, 'rb') as infile:
        origsize = struct.unpack('<Q', infile.read(struct.calcsize('Q')))[0]
        iv = infile.read(16)
        decryptor = AES.new(key, AES.MODE_CBC, iv)

        with open(output_filename, 'wb') as outfile:
            while True:
                chunk = infile.read(chunk_size)
                if len(chunk) == 0:
                    break
                outfile.write(decryptor.decrypt(chunk))

            outfile.truncate(origsize) 
Example #10
Source File: _definitions.py    From imageio-ffmpeg with BSD 2-Clause "Simplified" License 6 votes vote down vote up
def get_platform():
    bits = struct.calcsize("P") * 8
    if sys.platform.startswith("linux"):
        return "linux{}".format(bits)
    elif sys.platform.startswith("win"):
        return "win{}".format(bits)
    elif sys.platform.startswith("cygwin"):
        return "win{}".format(bits)
    elif sys.platform.startswith("darwin"):
        return "osx{}".format(bits)
    else:  # pragma: no cover
        return None


# The Linux static builds (https://johnvansickle.com/ffmpeg/) are build
# for Linux kernels 2.6.32 and up (at the time of writing, ffmpeg v4.1).
# This corresponds to CentOS 6. This means we should use manylinux2010 and not
# manylinux1.
# manylinux1: https://www.python.org/dev/peps/pep-0513
# manylinux2010: https://www.python.org/dev/peps/pep-0571


# Platform string -> ffmpeg filename 
Example #11
Source File: msi.py    From briefcase with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def support_package_url_query(self):
        """
        The query arguments to use in a support package query request.
        """
        return [
            ('platform', self.platform),
            ('version', self.python_version_tag),
            ('arch', "amd64" if (struct.calcsize("P") * 8) == 64 else "win32"),
        ] 
Example #12
Source File: Ping.py    From Computer-Networking-A-Top-Down-Approach-NOTES with MIT License 5 votes vote down vote up
def receiveOnePing(mySocket, ID, sequence, destAddr, timeout):
    timeLeft = timeout

    while 1:
        startedSelect = time.time()
        whatReady = select.select([mySocket], [], [], timeLeft)
        howLongInSelect = (time.time() - startedSelect)
        if whatReady[0] == []:  # Timeout
            return None

        timeReceived = time.time()
        recPacket, addr = mySocket.recvfrom(1024)

        # Fill in start
        header = recPacket[20: 28]
        type, code, checksum, packetID, sequence = struct.unpack("!bbHHh", header)
        if type == 0 and packetID == ID:  # type should be 0
            byte_in_double = struct.calcsize("!d")
            timeSent = struct.unpack("!d", recPacket[28: 28 + byte_in_double])[0]
            delay = timeReceived - timeSent
            ttl = ord(struct.unpack("!c", recPacket[8:9])[0].decode())
            return (delay, ttl, byte_in_double)
        # Fill in end

        timeLeft = timeLeft - howLongInSelect
        if timeLeft <= 0:
            return None 
Example #13
Source File: easy_install.py    From jbox with MIT License 5 votes vote down vote up
def is_64bit():
    return struct.calcsize("P") == 8 
Example #14
Source File: obj.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def obj_size(self):
        return struct.calcsize(self.format_string) 
Example #15
Source File: i3dm.py    From gltf2glb with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def unpack(self, fmt, data):
		calc_len = struct.calcsize(fmt)
		self.offset += calc_len
		return struct.unpack(fmt, data[self.offset - calc_len : self.offset])[0] 
Example #16
Source File: IcmpPing.py    From Computer-Networking-A-Top-Down-Approach-NOTES with MIT License 5 votes vote down vote up
def receiveOnePing(mySocket, ID, timeout, destAddr):
	timeLeft = timeout
	
	while 1: 
		startedSelect = time.time()
		whatReady = select.select([mySocket], [], [], timeLeft)
		howLongInSelect = (time.time() - startedSelect)
		if whatReady[0] == []: # Timeout
			return "Request timed out."
	
		timeReceived = time.time() 
		recPacket, addr = mySocket.recvfrom(1024)
		
		# Fetch the ICMPHeader from the IP
		icmpHeader = recPacket[20:28]

		rawTTL = struct.unpack("s", bytes([recPacket[8]]))[0]  
		
		# binascii -- Convert between binary and ASCII  
		TTL = int(binascii.hexlify(rawTTL), 16) 
		
		icmpType, code, checksum, packetID, sequence = struct.unpack("bbHHh", icmpHeader)
		
		if packetID == ID:
			byte = struct.calcsize("d") 
			timeSent = struct.unpack("d", recPacket[28:28 + byte])[0]
			return "Reply from %s: bytes=%d time=%f5ms TTL=%d" % (destAddr, len(recPacket), (timeReceived - timeSent)*1000, TTL)
		
		timeLeft = timeLeft - howLongInSelect
		if timeLeft <= 0:
			return "Request timed out." 
Example #17
Source File: ZabbixSender.py    From mqttwarn with Eclipse Public License 2.0 5 votes vote down vote up
def Send(self):
        self.__MakeSendData()
        so = socket.socket()
        so.connect((self.server_ip, self.server_port))
        wobj = so.makefile(u'wb')
        wobj.write(self.send_data)
        wobj.close()
        robj = so.makefile(u'rb')
        recv_data = robj.read()
        robj.close()
        so.close()
        tmp_data = struct.unpack("<4sBq" + str(len(recv_data) - struct.calcsize("<4sBq")) + "s", recv_data)
        recv_json = json.loads(tmp_data[3])
        #JPM return recv_data
        return recv_json 
Example #18
Source File: b3dm.py    From gltf2glb with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def unpack(self, fmt, data):
		calc_len = struct.calcsize(fmt)
		self.offset += calc_len
		return struct.unpack(fmt, data[self.offset - calc_len : self.offset])[0] 
Example #19
Source File: winpmem.py    From rekall with GNU General Public License v2.0 5 votes vote down vote up
def GetInfoDeprecated(self):
        result = win32file.DeviceIoControl(self.fd, INFO_IOCTRL_DEPRECATED, "",
                                           1024, None)
        fmt_string = "QQl"
        offset = struct.calcsize(fmt_string)

        cr3, kpcr, number_of_runs = struct.unpack_from(fmt_string, result)
        for x in range(number_of_runs):
            start, length = struct.unpack_from("QQ", result, x * 16 + offset)
            print("0x%X\t\t0x%X" % (start, length)) 
Example #20
Source File: scripts.py    From jbox with MIT License 5 votes vote down vote up
def _get_launcher(self, kind):
            if struct.calcsize('P') == 8:   # 64-bit
                bits = '64'
            else:
                bits = '32'
            name = '%s%s.exe' % (kind, bits)
            # Issue 31: don't hardcode an absolute package name, but
            # determine it relative to the current package
            distlib_package = __name__.rsplit('.', 1)[0]
            result = finder(distlib_package).find(name).bytes
            return result

    # Public API follows 
Example #21
Source File: stata.py    From recruit with Apache License 2.0 5 votes vote down vote up
def _calcsize(self, fmt):
        return (type(fmt) is int and fmt or
                struct.calcsize(self.byteorder + fmt)) 
Example #22
Source File: struct_parser.py    From pyaff4 with Apache License 2.0 5 votes vote down vote up
def sizeof(cls):
        return struct.calcsize(cls._format_string) 
Example #23
Source File: __init__.py    From recruit with Apache License 2.0 5 votes vote down vote up
def is_platform_32bit():
    return struct.calcsize("P") * 8 < 64 
Example #24
Source File: aff4_image.py    From pyaff4 with Apache License 2.0 5 votes vote down vote up
def _parse_bevy_index(self, bevy):
        bevy_index_urn = rdfvalue.URN("%s.index" % bevy.urn)
        with self.resolver.AFF4FactoryOpen(bevy_index_urn) as bevy_index:
            bevy_index_data = bevy_index.Read(bevy_index.Size())
            number_of_entries = bevy_index.Size() // struct.calcsize("QI")
            format_string = "<" + "QI" * number_of_entries
            data = struct.unpack(format_string, bevy_index_data)

            res = [(data[2*i], data[2*i+1]) for i in range(len(data)//2)]
            if LOGGER.isEnabledFor(logging.INFO):
                LOGGER.info("Parse Bevy Index %s size=%x entries=%x", bevy_index_urn, bevy_index.Size(), len(res))
            return res 
Example #25
Source File: cpio.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, off, buf):
        self.off = off
        self.buf = buf

        if (off & 3):
            raise Exception("invalid offset %d" % off)

        fmt = "6s8s8s8s8s8s8s8s8s8s8s8s8s8s"
        off = self.off + struct.calcsize(fmt)

        fields = struct.unpack(fmt, buf[self.off:off])

        if fields[0] != "070701":
            raise Exception("invalid cpio header %s" % self.c_magic)

        names = ("c_ino", "c_mode", "c_uid", "c_gid",
                "c_nlink", "c_mtime", "c_filesize",
                "c_devmajor", "c_devminor", "c_rdevmajor",
                "c_rdevminor", "c_namesize", "c_check")
        for (n, v) in zip(names, fields[1:]):
            setattr(self, n, int(v, 16))

        nlen = self.c_namesize - 1
        self.name = struct.unpack('%ds' % nlen, buf[off:off + nlen])[0]
        off = off + nlen + 1
        if (off & 3):
            off = off + 4 - (off & 3)   # padding
        self.payloadstart = off 
Example #26
Source File: tfjs2pytorch.py    From posenet-pytorch with Apache License 2.0 5 votes vote down vote up
def load_variables(chkpoint, base_dir=BASE_DIR):
    manifest_path = os.path.join(base_dir, chkpoint, "manifest.json")
    if not os.path.exists(manifest_path):
        print('Weights for checkpoint %s are not downloaded. Downloading to %s ...' % (chkpoint, base_dir))
        from posenet.converter.wget import download
        download(chkpoint, base_dir)
        assert os.path.exists(manifest_path)

    manifest = open(manifest_path)
    variables = json.load(manifest)
    manifest.close()

    state_dict = {}
    for x in variables:
        torch_name = to_torch_name(x)
        if not torch_name:
            continue
        filename = variables[x]["filename"]
        byte = open(os.path.join(base_dir, chkpoint, filename), 'rb').read()
        fmt = str(int(len(byte) / struct.calcsize('f'))) + 'f'
        d = struct.unpack(fmt, byte)
        d = np.array(d, dtype=np.float32)
        shape = variables[x]["shape"]
        if len(shape) == 4:
            tpt = (2, 3, 0, 1) if 'depthwise' in filename else (3, 2, 0, 1)
            d = np.reshape(d, shape).transpose(tpt)
        state_dict[torch_name] = torch.Tensor(d)

    return state_dict 
Example #27
Source File: cpio.py    From openSUSE-release-tools with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, off, buf):
        self.off = off
        self.buf = buf

        if (off & 3):
            raise Exception("invalid offset %d" % off)

        fmt = "6s8s8s8s8s8s8s8s8s8s8s8s8s8s"
        off = self.off + struct.calcsize(fmt)

        fields = struct.unpack(fmt, buf[self.off:off])

        if fields[0] != "070701":
            raise Exception("invalid cpio header %s" % self.c_magic)

        names = ("c_ino", "c_mode", "c_uid", "c_gid",
                "c_nlink", "c_mtime", "c_filesize",
                "c_devmajor", "c_devminor", "c_rdevmajor",
                "c_rdevminor", "c_namesize", "c_check")
        for (n, v) in zip(names, fields[1:]):
            setattr(self, n, int(v, 16))

        nlen = self.c_namesize - 1
        self.name = struct.unpack('%ds' % nlen, buf[off:off + nlen])[0]
        off = off + nlen + 1
        if (off & 3):
            off = off + 4 - (off & 3)   # padding
        self.payloadstart = off 
Example #28
Source File: spi.py    From Adafruit_Python_PureIO with MIT License 5 votes vote down vote up
def _ioc_encode(direction, number, structure):
    """
    ioctl command encoding helper function
    Calculates the appropriate spidev ioctl op argument given the direction,
    command number, and argument structure in python's struct.pack format.
    Returns a tuple of the calculated op and the struct.pack format
    See Linux kernel source file /include/uapi/asm/ioctl.h
    """
    ioc_magic = ord("k")
    ioc_nrbits = 8
    ioc_typebits = 8
    if platform.machine() == "mips":
        ioc_sizebits = 13
    else:
        ioc_sizebits = 14

    ioc_nrshift = 0
    ioc_typeshift = ioc_nrshift + ioc_nrbits
    ioc_sizeshift = ioc_typeshift + ioc_typebits
    ioc_dirshift = ioc_sizeshift + ioc_sizebits

    size = struct.calcsize(structure)

    operation = (
        (direction << ioc_dirshift)
        | (ioc_magic << ioc_typeshift)
        | (number << ioc_nrshift)
        | (size << ioc_sizeshift)
    )

    return direction, operation, structure 
Example #29
Source File: wallpaper.py    From wallgen with MIT License 5 votes vote down vote up
def is_64bit_windows():
    """Check if 64 bit Windows OS"""
    return struct.calcsize('P') * 8 == 64 
Example #30
Source File: oggparse.py    From discord.py with MIT License 5 votes vote down vote up
def __init__(self, stream):
        try:
            header = stream.read(struct.calcsize(self._header.format))

            self.flag, self.gran_pos, self.serial, \
            self.pagenum, self.crc, self.segnum = self._header.unpack(header)

            self.segtable = stream.read(self.segnum)
            bodylen = sum(struct.unpack('B'*self.segnum, self.segtable))
            self.data = stream.read(bodylen)
        except Exception:
            raise OggError('bad data stream') from None