| // Copyright 2017 The Chromium Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| #include "base/win/patch_util.h" |
| #include "base/logging.h" |
| DWORD ModifyCode(void* destination, const void* source, int length) { |
| if ((NULL == destination) || (NULL == source) || (0 == length)) { |
| return ERROR_INVALID_PARAMETER; |
| // Change the page protection so that we can write. |
| MEMORY_BASIC_INFORMATION memory_info; |
| DWORD old_page_protection = 0; |
| if (!VirtualQuery(destination, &memory_info, sizeof(memory_info))) { |
| DWORD is_executable = (PAGE_EXECUTE | PAGE_EXECUTE_READ | |
| PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY) & |
| if (VirtualProtect(destination, length, |
| is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE, |
| CopyMemory(destination, source, length); |
| // Restore the old page protection. |
| VirtualProtect(destination, length, old_page_protection, |