/** @brief Write 0x90's to create a NOP sled at the specified address. @param [IN] pInstructions - A pointer to the instructions to overwrite with a nop sled. @param [IN] dwInstructionCount - The number of instructions to overwrite with NOPs. @param [OUT] pOriginalInstructions - (optional) A buffer to receive the instructions that were at the address before they get overwritten with NOPs. If this is NULL, the parameter is ignored. @return DWORD number of BYTES written. **/ DWORD writeNopSled(PVOID pInstructions, DWORD dwInstructionCount, PBYTE pOriginalInstructions){ PVOID pAddr = pInstructions; // Count the bytes. for(DWORD i=0;i<dwInstructionCount;i++){ pAddr = (PVOID)((DWORD)pAddr + getInstructionLength(pAddr)); } // This gets optomized out, but its nice to look at in C. DWORD byteCount = (DWORD)pAddr - (DWORD)pInstructions; // Just a nice way to look at things. PBYTE pOpCode = (PBYTE)pInstructions; // Save off the old bytes. if(pOriginalInstructions != NULL){ for(DWORD i=0;i<byteCount;i++){ pOriginalInstructions[i] = pOpCode[i]; } } // Write out NOP Sled. for(DWORD i=0;i<byteCount;i++){ pOpCode[i]=0x90; } return byteCount; }
