Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

137 lines
2.9 KiB

  1. /*++
  2. Copyright (c) 2000 Microsoft Corporation
  3. Module Name:
  4. EmulateWriteFile.cpp
  5. Abstract:
  6. On NT, WriteFile requires the buffer passed in to be non-null. But Win9x
  7. assumes you want to write zeroes if the buffer is NULL. This shim emulates
  8. the Win9x behavior.
  9. Notes:
  10. This is a general purpose shim.
  11. History:
  12. 01/21/2000 linstev Created
  13. --*/
  14. #include "precomp.h"
  15. IMPLEMENT_SHIM_BEGIN(EmulateWriteFile)
  16. #include "ShimHookMacro.h"
  17. APIHOOK_ENUM_BEGIN
  18. APIHOOK_ENUM_ENTRY(WriteFile)
  19. APIHOOK_ENUM_ENTRY(AVIStreamWrite)
  20. APIHOOK_ENUM_END
  21. typedef HRESULT (*_pfn_AVIStreamWrite)(PAVISTREAM pavi, LONG lStart, LONG lSamples, LPVOID lpBuffer, LONG cbBuffer, DWORD dwFlags, LONG * plSampWritten, LONG * plBytesWritten);
  22. /*++
  23. Allocate a buffer as required.
  24. --*/
  25. BOOL
  26. APIHOOK(WriteFile)(
  27. HANDLE hFile,
  28. LPCVOID lpBuffer,
  29. DWORD nNumberOfBytesToWrite,
  30. LPDWORD lpNumberOfBytesWritten,
  31. LPOVERLAPPED lpOverlapped
  32. )
  33. {
  34. BOOL bRet;
  35. if (!lpBuffer) {
  36. void* pBuf = malloc(nNumberOfBytesToWrite);
  37. if (pBuf == NULL) {
  38. LOGN(eDbgLevelError, "[WriteFile] Failed to allocate %d bytes.", nNumberOfBytesToWrite);
  39. } else {
  40. ZeroMemory(pBuf, nNumberOfBytesToWrite);
  41. }
  42. bRet = ORIGINAL_API(WriteFile)(hFile, pBuf, nNumberOfBytesToWrite,
  43. lpNumberOfBytesWritten, lpOverlapped);
  44. free(pBuf);
  45. LOGN(eDbgLevelError, "[WriteFile] - null buffer of size %d.", nNumberOfBytesToWrite);
  46. } else {
  47. bRet = ORIGINAL_API(WriteFile)(hFile, lpBuffer, nNumberOfBytesToWrite,
  48. lpNumberOfBytesWritten, lpOverlapped);
  49. }
  50. return bRet;
  51. }
  52. /*++
  53. Allocate a buffer as required.
  54. --*/
  55. HRESULT
  56. APIHOOK(AVIStreamWrite)(
  57. PAVISTREAM pavi,
  58. LONG lStart,
  59. LONG lSamples,
  60. LPVOID lpBuffer,
  61. LONG cbBuffer,
  62. DWORD dwFlags,
  63. LONG * plSampWritten,
  64. LONG * plBytesWritten
  65. )
  66. {
  67. HRESULT hRet;
  68. if (!lpBuffer) {
  69. void* pBuf = malloc(cbBuffer);
  70. if (pBuf == NULL) {
  71. LOGN(eDbgLevelError, "[AVIStreamWrite] Failed to allocate %d bytes.", cbBuffer);
  72. } else {
  73. ZeroMemory(pBuf, cbBuffer);
  74. }
  75. hRet = ORIGINAL_API(AVIStreamWrite)(pavi, lStart, lSamples, pBuf,
  76. cbBuffer, dwFlags, plSampWritten, plBytesWritten);
  77. free(pBuf);
  78. LOGN(eDbgLevelError, "[AVIStreamWrite] - null buffer of size %d", cbBuffer);
  79. } else {
  80. hRet = ORIGINAL_API(AVIStreamWrite)(pavi, lStart, lSamples, lpBuffer,
  81. cbBuffer, dwFlags, plSampWritten, plBytesWritten);
  82. }
  83. return hRet;
  84. }
  85. /*++
  86. Register hooked functions
  87. --*/
  88. HOOK_BEGIN
  89. APIHOOK_ENTRY(KERNEL32.DLL, WriteFile)
  90. APIHOOK_ENTRY(AVIFIL32.DLL, AVIStreamWrite)
  91. HOOK_END
  92. IMPLEMENT_SHIM_END