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.

107 lines
2.5 KiB

  1. /*++
  2. Copyright (c) 2001 Microsoft Corporation
  3. Module Name:
  4. Khunmin2Album.cpp
  5. Abstract:
  6. When doing WideCharToMultiByte, the app does not pass the ansi string
  7. buffer length correctly, while it seems the app allocates enough big
  8. buffer, fix this by correcting the ansi string length, (try-except
  9. protected in case the buffer allocated is not enough).
  10. Notes:
  11. This is an app specific shim.
  12. History:
  13. 05/15/2001 xiaoz Created
  14. --*/
  15. #include "precomp.h"
  16. IMPLEMENT_SHIM_BEGIN(Khunmin2Album)
  17. #include "ShimHookMacro.h"
  18. APIHOOK_ENUM_BEGIN
  19. APIHOOK_ENUM_ENTRY(WideCharToMultiByte)
  20. APIHOOK_ENUM_END
  21. /*++
  22. Correct ansi string length if necessary
  23. --*/
  24. int
  25. APIHOOK(WideCharToMultiByte)(
  26. UINT CodePage, // code page
  27. DWORD dwFlags, // performance and mapping flags
  28. LPCWSTR lpWideCharStr, // wide-character string
  29. int cchWideChar, // number of chars in string
  30. LPSTR lpMultiByteStr, // buffer for new string
  31. int cbMultiByte, // size of buffer
  32. LPCSTR lpDefaultChar, // default for unmappable chars
  33. LPBOOL lpUsedDefaultChar // set when default char used
  34. )
  35. {
  36. int nMultiByte;
  37. int nOriginalMultiByte = cbMultiByte;
  38. //
  39. // Get the exact size in byte needed to convert the string from unicode to
  40. // ansi
  41. //
  42. nMultiByte = ORIGINAL_API(WideCharToMultiByte)(CodePage, dwFlags,
  43. lpWideCharStr, cchWideChar, lpMultiByteStr, 0, lpDefaultChar,
  44. lpUsedDefaultChar);
  45. //
  46. // See if we need to correct the buffer size
  47. //
  48. if (nMultiByte > cbMultiByte)
  49. {
  50. LOGN(eDbgLevelWarning, "Buffer size corrected");
  51. cbMultiByte = nMultiByte;
  52. }
  53. __try
  54. {
  55. return ORIGINAL_API(WideCharToMultiByte)(CodePage, dwFlags,
  56. lpWideCharStr, cchWideChar, lpMultiByteStr, cbMultiByte,
  57. lpDefaultChar, lpUsedDefaultChar);
  58. }
  59. __except(EXCEPTION_EXECUTE_HANDLER)
  60. {
  61. //
  62. // If somehow exception happens, probally AV , since we enlarge
  63. // the buffer size, we will use what ever the parameter originally
  64. // passed in, and do nothing
  65. //
  66. return ORIGINAL_API(WideCharToMultiByte)(CodePage, dwFlags,
  67. lpWideCharStr, cchWideChar, lpMultiByteStr, nOriginalMultiByte,
  68. lpDefaultChar, lpUsedDefaultChar);
  69. }
  70. }
  71. /*++
  72. Register hooked functions
  73. --*/
  74. HOOK_BEGIN
  75. APIHOOK_ENTRY(KERNEL32.DLL, WideCharToMultiByte)
  76. HOOK_END
  77. IMPLEMENT_SHIM_END