Python mmap.ACCESS_WRITE Examples

The following are 13 code examples of mmap.ACCESS_WRITE(). 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 mmap , or try the search function .
Example #1
Source File: installer.py    From attack_monitor with GNU General Public License v3.0 6 votes vote down vote up
def create_shortcut(lnk_out_path, target, parameters, working_dir, description, icon=None, run_as_admin=False, minimized=False):
    shell = win32com.client.Dispatch("WScript.Shell")
    shortcut = shell.CreateShortCut(lnk_out_path)
    shortcut.Targetpath = target
    shortcut.Arguments = '"{}"'.format(parameters)
    shortcut.Description = description
    shortcut.WorkingDirectory = working_dir
    if not icon is None:
        shortcut.IconLocation = icon
    if minimized:# 7 - Minimized, 3 - Maximized, 1 - Normal
        shortcut.WindowStyle = 7
    else:
        shortcut.WindowStyle = 1
    shortcut.save()

    if run_as_admin:
        with open(lnk_out_path, "r+b") as f:
            with contextlib.closing(mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_WRITE)) as m:
                m[0x15] = m[0x15] | 0x20 # Enable 6th bit = Responsible for Run As Admin
                #m.flush() 
Example #2
Source File: BrundleFuzzClient.py    From BrundleFuzz with MIT License 6 votes vote down vote up
def _initialize_shared_memory(self):
        """
        This is the IPC channel between us (Python)
        and the PinTool (C/C++)
        """
        s_uint32 = self.utils.get_size_uint32()
        shm_name = "Local\\NaFlSharedMemory"

        self.shm_size = self.bitmap_size * s_uint32  # architecture dependent :)
        self.shm = mmap.mmap(0,
                        self.shm_size,
                        shm_name,
                        access = mmap.ACCESS_WRITE)

        if not self.shm:
            # Oops!
            self.ml.info('[!] Could not create the shared memory region')
            self.ml.info('[!] Aborting...')
            sys.exit(1) 
Example #3
Source File: ArrayUtils.py    From MobileNetV2-PoseEstimation with MIT License 6 votes vote down vote up
def __new__(subtype, shape, dtype=float, buffer=None, offset=0, strides=None, order=None, info=None):
		
		# Determine the size in bytes required to hold the array
		numBytes = _requiredSize(shape, dtype)
		
		# Create the temporary file, resize it, and map it into memory
		tempFile = tempfile.TemporaryFile()
		tempFile.truncate(numBytes)
		buf = mmap.mmap(tempFile.fileno(), numBytes, access=mmap.ACCESS_WRITE)
		
		# Create the ndarray with the memory map as the underlying buffer
		obj = super(TempfileBackedArray, subtype).__new__(subtype, shape, dtype, buf, 0, None, order)
		
		# Attach the file reference to the ndarray object
		obj._file = tempFile
		return obj 
Example #4
Source File: test_seeds.py    From btcrecover with GNU General Public License v2.0 6 votes vote down vote up
def test_file_update(self):
        aset   = AddressSet(self.TABLE_LEN)
        dbfile = tempfile.NamedTemporaryFile(delete=False)
        try:
            aset.tofile(dbfile)
            dbfile.seek(0)
            aset = AddressSet.fromfile(dbfile, mmap_access=mmap.ACCESS_WRITE)
            addr = "".join(chr(b) for b in xrange(20))
            aset.add(addr)
            aset.close()
            self.assertTrue(dbfile.closed)
            dbfile = open(dbfile.name, "rb")
            aset = AddressSet.fromfile(dbfile)
            self.assertIn(addr, aset)
            self.assertEqual(len(aset), 1)
        finally:
            aset.close()
            dbfile.close()
            os.remove(dbfile.name) 
Example #5
Source File: mmap_dict.py    From client_python with Apache License 2.0 6 votes vote down vote up
def __init__(self, filename, read_mode=False):
        self._f = open(filename, 'rb' if read_mode else 'a+b')
        self._fname = filename
        capacity = os.fstat(self._f.fileno()).st_size
        if capacity == 0:
            self._f.truncate(_INITIAL_MMAP_SIZE)
            capacity = _INITIAL_MMAP_SIZE
        self._capacity = capacity
        self._m = mmap.mmap(self._f.fileno(), self._capacity,
                            access=mmap.ACCESS_READ if read_mode else mmap.ACCESS_WRITE)

        self._positions = {}
        self._used = _unpack_integer(self._m, 0)[0]
        if self._used == 0:
            self._used = 8
            _pack_integer(self._m, 0, self._used)
        else:
            if not read_mode:
                for key, _, pos in self._read_all_values():
                    self._positions[key] = pos 
Example #6
Source File: ArrayUtils.py    From slidingwindow with MIT License 6 votes vote down vote up
def __new__(subtype, shape, dtype=float, buffer=None, offset=0, strides=None, order=None, info=None):
		
		# Determine the size in bytes required to hold the array
		numBytes = _requiredSize(shape, dtype)
		
		# Create the temporary file, resize it, and map it into memory
		tempFile = tempfile.TemporaryFile()
		tempFile.truncate(numBytes)
		buf = mmap.mmap(tempFile.fileno(), numBytes, access=mmap.ACCESS_WRITE)
		
		# Create the ndarray with the memory map as the underlying buffer
		obj = super(TempfileBackedArray, subtype).__new__(subtype, shape, dtype, buf, 0, None, order)
		
		# Attach the file reference to the ndarray object
		obj._file = tempFile
		return obj 
Example #7
Source File: ArrayUtils.py    From tf-pose-estimation with Apache License 2.0 6 votes vote down vote up
def __new__(subtype, shape, dtype=float, buffer=None, offset=0, strides=None, order=None, info=None):
		
		# Determine the size in bytes required to hold the array
		numBytes = _requiredSize(shape, dtype)
		
		# Create the temporary file, resize it, and map it into memory
		tempFile = tempfile.TemporaryFile()
		tempFile.truncate(numBytes)
		buf = mmap.mmap(tempFile.fileno(), numBytes, access=mmap.ACCESS_WRITE)
		
		# Create the ndarray with the memory map as the underlying buffer
		obj = super(TempfileBackedArray, subtype).__new__(subtype, shape, dtype, buf, 0, None, order)
		
		# Attach the file reference to the ndarray object
		obj._file = tempFile
		return obj 
Example #8
Source File: mmap_dict.py    From Carnets with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def __init__(self, filename, read_mode=False):
        self._f = open(filename, 'rb' if read_mode else 'a+b')
        self._fname = filename
        if os.fstat(self._f.fileno()).st_size == 0:
            self._f.truncate(_INITIAL_MMAP_SIZE)
        self._capacity = os.fstat(self._f.fileno()).st_size
        self._m = mmap.mmap(self._f.fileno(), self._capacity, access=mmap.ACCESS_READ if read_mode else mmap.ACCESS_WRITE)

        self._positions = {}
        self._used = _unpack_integer(self._m, 0)[0]
        if self._used == 0:
            self._used = 8
            _pack_integer(self._m, 0, self._used)
        else:
            if not read_mode:
                for key, _, pos in self._read_all_values():
                    self._positions[key] = pos 
Example #9
Source File: addressset.py    From btcrecover with GNU General Public License v2.0 5 votes vote down vote up
def __setstate__(self, state):
        # If the object contained an mmap, recreate it from scratch
        if "dbfilename" in state:
            new = self.fromfile(open(state["dbfilename"], "r+b" if state["mmap_access"]==mmap.ACCESS_WRITE else "rb"),
                                mmap_access=state["mmap_access"], preload=False)
            self.__dict__ = new.__dict__.copy()
            new._dbfile = new._data = None  # ensure new's __del__() doesn't close() anything
        else:
            self.__dict__ = state 
Example #10
Source File: file.py    From Carnets with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _mmap_available(cls):
        """Tests that mmap, and specifically mmap.flush works.  This may
        be the case on some uncommon platforms (see
        https://github.com/astropy/astropy/issues/968).

        If mmap.flush is found not to work, ``self.memmap = False`` is
        set and a warning is issued.
        """

        tmpfd, tmpname = tempfile.mkstemp()
        try:
            # Windows does not allow mappings on empty files
            os.write(tmpfd, b' ')
            os.fsync(tmpfd)
            try:
                mm = mmap.mmap(tmpfd, 1, access=mmap.ACCESS_WRITE)
            except OSError as exc:
                warnings.warn('Failed to create mmap: {}; mmap use will be '
                              'disabled'.format(str(exc)), AstropyUserWarning)
                del exc
                return False
            try:
                mm.flush()
            except OSError:
                warnings.warn('mmap.flush is unavailable on this platform; '
                              'using mmap in writeable mode will be disabled',
                              AstropyUserWarning)
                return False
            finally:
                mm.close()
        finally:
            os.close(tmpfd)
            os.remove(tmpname)

        return True 
Example #11
Source File: addressset.py    From btcrecover with GNU General Public License v2.0 4 votes vote down vote up
def fromfile(cls, dbfile, mmap_access = mmap.ACCESS_READ, preload = True):
        """Load the address set from a file

        :param dbfile: an open file object from which the set is loaded;
                       it will be closed by AddressSet when no longer needed
        :type dbfile: io.FileIO or file
        :param mmap_access: mmap.ACCESS_READ, .ACCESS_WRITE, or .ACCESS_COPY
        :type mmap_access: int
        :param preload: True to preload the entire address set, False to load on demand
        :type preload: bool
        """
        if "b" not in dbfile.mode:
            raise ValueError("must open file in binary mode")
        header_pos = dbfile.tell()
        if header_pos % mmap.ALLOCATIONGRANULARITY != 0:
            raise ValueError("header position in file must be a multiple of {}".format(mmap.ALLOCATIONGRANULARITY))
        #
        # Read in the header safely (ast.literal_eval() is safe for untrusted data)
        header = dbfile.read(cls.HEADER_LEN)
        if not header.startswith(cls.MAGIC):
            raise ValueError("unrecognized file format (invalid magic)")
        magic_len  = len(cls.MAGIC)
        config_end = header.find(b"\0", magic_len, cls.HEADER_LEN)
        assert config_end > 0
        config = ast.literal_eval(header[magic_len:config_end])
        if config["version"] != cls.VERSION:
            raise ValueError("can't load address database version {} (only supports {})"
                             .format(config["version"], cls.VERSION))
        #
        # Create an AddressSet object and replace its attributes
        self = cls(1)  # (size is irrelevant since it's getting replaced)
        cls._remove_nonheader_attribs(self.__dict__)
        for attr in self.__dict__.keys():  # only load expected attributes from untrusted data
            self.__dict__[attr] = config[attr]
        self._mmap_access = mmap_access
        #
        # The hash table is memory-mapped directly from the file instead of being loaded
        self._data = mmap.mmap(dbfile.fileno(), self._table_bytes, access=mmap_access,
                                offset= header_pos + cls.HEADER_LEN)
        if mmap_access == mmap.ACCESS_WRITE:
            dbfile.seek(header_pos)  # prepare for writing an updated header in close()
        else:
            dbfile.close()
        self._dbfile = dbfile
        #
        # Most of the time it makes sense to load the file serially instead of letting
        # the OS load each page as it's touched in random order, especially with HDDs;
        # reading a byte from each page is sufficient (CPython doesn't optimize this away)
        if preload:
            for i in xrange(self._table_bytes // mmap.PAGESIZE):
                self._data[i * mmap.PAGESIZE]
        #
        return self 
Example #12
Source File: win_pageant.py    From imoocc with GNU General Public License v2.0 4 votes vote down vote up
def _query_pageant(msg):
    hwnd = _get_pageant_window_object()
    if not hwnd:
        # Raise a failure to connect exception, pageant isn't running anymore!
        return None

    # Write our pageant request string into the file (pageant will read this to determine what to do)
    filename = tempfile.mktemp('.pag')
    map_filename = os.path.basename(filename)

    f = open(filename, 'w+b')
    f.write(msg )
    # Ensure the rest of the file is empty, otherwise pageant will read this
    f.write('\0' * (_AGENT_MAX_MSGLEN - len(msg)))
    # Create the shared file map that pageant will use to read from
    pymap = mmap.mmap(f.fileno(), _AGENT_MAX_MSGLEN, tagname=map_filename, access=mmap.ACCESS_WRITE)
    try:
        # Create an array buffer containing the mapped filename
        char_buffer = array.array("c", map_filename + '\0')
        char_buffer_address, char_buffer_size = char_buffer.buffer_info()
        # Create a string to use for the SendMessage function call
        cds = COPYDATASTRUCT(_AGENT_COPYDATA_ID, char_buffer_size, char_buffer_address)

        if _has_win32all:
            # win32gui.SendMessage should also allow the same pattern as
            # ctypes, but let's keep it like this for now...
            response = win32gui.SendMessage(hwnd, win32con_WM_COPYDATA, ctypes.sizeof(cds), ctypes.addressof(cds))
        elif _has_ctypes:
            response = ctypes.windll.user32.SendMessageA(hwnd, win32con_WM_COPYDATA, ctypes.sizeof(cds), ctypes.byref(cds))
        else:
            response = 0

        if response > 0:
            datalen = pymap.read(4)
            retlen = struct.unpack('>I', datalen)[0]
            return datalen + pymap.read(retlen)
        return None
    finally:
        pymap.close()
        f.close()
        # Remove the file, it was temporary only
        os.unlink(filename) 
Example #13
Source File: win_pageant.py    From imoocc with GNU General Public License v2.0 4 votes vote down vote up
def _query_pageant(msg):
    hwnd = _get_pageant_window_object()
    if not hwnd:
        # Raise a failure to connect exception, pageant isn't running anymore!
        return None

    # Write our pageant request string into the file (pageant will read this to determine what to do)
    filename = tempfile.mktemp('.pag')
    map_filename = os.path.basename(filename)

    f = open(filename, 'w+b')
    f.write(msg )
    # Ensure the rest of the file is empty, otherwise pageant will read this
    f.write('\0' * (_AGENT_MAX_MSGLEN - len(msg)))
    # Create the shared file map that pageant will use to read from
    pymap = mmap.mmap(f.fileno(), _AGENT_MAX_MSGLEN, tagname=map_filename, access=mmap.ACCESS_WRITE)
    try:
        # Create an array buffer containing the mapped filename
        char_buffer = array.array("c", map_filename + '\0')
        char_buffer_address, char_buffer_size = char_buffer.buffer_info()
        # Create a string to use for the SendMessage function call
        cds = COPYDATASTRUCT(_AGENT_COPYDATA_ID, char_buffer_size, char_buffer_address)

        if _has_win32all:
            # win32gui.SendMessage should also allow the same pattern as
            # ctypes, but let's keep it like this for now...
            response = win32gui.SendMessage(hwnd, win32con_WM_COPYDATA, ctypes.sizeof(cds), ctypes.addressof(cds))
        elif _has_ctypes:
            response = ctypes.windll.user32.SendMessageA(hwnd, win32con_WM_COPYDATA, ctypes.sizeof(cds), ctypes.byref(cds))
        else:
            response = 0

        if response > 0:
            datalen = pymap.read(4)
            retlen = struct.unpack('>I', datalen)[0]
            return datalen + pymap.read(retlen)
        return None
    finally:
        pymap.close()
        f.close()
        # Remove the file, it was temporary only
        os.unlink(filename)