Leaked source code of windows server 2003
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.

202 lines
6.2 KiB

  1. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  2. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  3. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  4. // PARTICULAR PURPOSE.
  5. //
  6. // Copyright 1998 - 2003 Microsoft Corporation. All Rights Reserved.
  7. //
  8. // FILE: Devmode.cpp
  9. //
  10. //
  11. // PURPOSE: Implementation of Devmode functions shared with OEM UI and OEM rendering modules.
  12. //
  13. //
  14. // Functions:
  15. //
  16. //
  17. //
  18. //
  19. // PLATFORMS: Windows 2000, Windows XP, Windows Server 2003
  20. //
  21. //
  22. #include "precomp.h"
  23. #include "oemui.h"
  24. #include "debug.h"
  25. #include "devmode.h"
  26. // StrSafe.h needs to be included last
  27. // to disallow bad string functions.
  28. #include <STRSAFE.H>
  29. HRESULT hrOEMDevMode(DWORD dwMode, POEMDMPARAM pOemDMParam)
  30. {
  31. POEMDEV pOEMDevIn;
  32. POEMDEV pOEMDevOut;
  33. // Verify parameters.
  34. if( (NULL == pOemDMParam)
  35. ||
  36. ( (OEMDM_SIZE != dwMode)
  37. &&
  38. (OEMDM_DEFAULT != dwMode)
  39. &&
  40. (OEMDM_CONVERT != dwMode)
  41. &&
  42. (OEMDM_MERGE != dwMode)
  43. )
  44. )
  45. {
  46. ERR(ERRORTEXT("DevMode() ERROR_INVALID_PARAMETER.\r\n"));
  47. VERBOSE(DLLTEXT("\tdwMode = %d, pOemDMParam = %#lx.\r\n"), dwMode, pOemDMParam);
  48. SetLastError(ERROR_INVALID_PARAMETER);
  49. return E_FAIL;
  50. }
  51. // Cast generic (i.e. PVOID) to OEM private devomode pointer type.
  52. pOEMDevIn = (POEMDEV) pOemDMParam->pOEMDMIn;
  53. pOEMDevOut = (POEMDEV) pOemDMParam->pOEMDMOut;
  54. switch(dwMode)
  55. {
  56. case OEMDM_SIZE:
  57. pOemDMParam->cbBufSize = sizeof(OEMDEV);
  58. break;
  59. case OEMDM_DEFAULT:
  60. pOEMDevOut->dmOEMExtra.dwSize = sizeof(OEMDEV);
  61. pOEMDevOut->dmOEMExtra.dwSignature = OEM_SIGNATURE;
  62. pOEMDevOut->dmOEMExtra.dwVersion = OEM_VERSION;
  63. pOEMDevOut->dwDriverData = 0;
  64. pOEMDevOut->dwAdvancedData = 0;
  65. VERBOSE(DLLTEXT("pOEMDevOut after setting default values:\r\n"));
  66. Dump(pOEMDevOut);
  67. break;
  68. case OEMDM_CONVERT:
  69. ConvertOEMDevmode(pOEMDevIn, pOEMDevOut, pOemDMParam->cbBufSize);
  70. break;
  71. case OEMDM_MERGE:
  72. ConvertOEMDevmode(pOEMDevIn, pOEMDevOut, pOemDMParam->cbBufSize);
  73. MakeOEMDevmodeValid(pOEMDevOut);
  74. break;
  75. }
  76. Dump(pOemDMParam);
  77. return S_OK;
  78. }
  79. BOOL ConvertOEMDevmode(PCOEMDEV pOEMDevIn, POEMDEV pOEMDevOut, DWORD dwSize)
  80. {
  81. if( (NULL == pOEMDevIn)
  82. ||
  83. (NULL == pOEMDevOut)
  84. ||
  85. (dwSize < sizeof(OEMDEV))
  86. )
  87. {
  88. ERR(ERRORTEXT("ConvertOEMDevmode() invalid parameters.\r\n"));
  89. return FALSE;
  90. }
  91. // Check OEM Signature, if it doesn't match ours,
  92. // then just assume DMIn is bad and use defaults.
  93. if(pOEMDevIn->dmOEMExtra.dwSignature == pOEMDevOut->dmOEMExtra.dwSignature)
  94. {
  95. VERBOSE(DLLTEXT("Converting private OEM Devmode.\r\n"));
  96. VERBOSE(DLLTEXT("pOEMDevIn:\r\n"));
  97. Dump(pOEMDevIn);
  98. // Set the devmode defaults so that anything the isn't copied over will
  99. // be set to the default value.
  100. pOEMDevOut->dwDriverData = 0;
  101. pOEMDevOut->dwAdvancedData = 0;
  102. // Copy the old structure in to the new using which ever size is the smaller.
  103. // Devmode maybe from newer Devmode (not likely since there is only one), or
  104. // Devmode maybe a newer Devmode, in which case it maybe larger,
  105. // but the first part of the structure should be the same.
  106. // DESIGN ASSUMPTION: the private DEVMODE structure only gets added to;
  107. // the fields that are in the DEVMODE never change only new fields get added to the end.
  108. memcpy(pOEMDevOut, pOEMDevIn, __min(dwSize, __min(pOEMDevOut->dmOEMExtra.dwSize, pOEMDevIn->dmOEMExtra.dwSize)));
  109. // Re-fill in the size and version fields to indicated
  110. // that the DEVMODE is the current private DEVMODE version.
  111. pOEMDevOut->dmOEMExtra.dwSize = sizeof(OEMDEV);
  112. pOEMDevOut->dmOEMExtra.dwVersion = OEM_VERSION;
  113. }
  114. else
  115. {
  116. WARNING(DLLTEXT("Unknown DEVMODE signature, pOEMDMIn ignored.\r\n"));
  117. // Don't know what the input DEVMODE is, so just use defaults.
  118. pOEMDevOut->dmOEMExtra.dwSize = sizeof(OEMDEV);
  119. pOEMDevOut->dmOEMExtra.dwSignature = OEM_SIGNATURE;
  120. pOEMDevOut->dmOEMExtra.dwVersion = OEM_VERSION;
  121. pOEMDevOut->dwDriverData = 0;
  122. pOEMDevOut->dwAdvancedData = 0;
  123. }
  124. return TRUE;
  125. }
  126. BOOL MakeOEMDevmodeValid(POEMDEV pOEMDevmode)
  127. {
  128. if(NULL == pOEMDevmode)
  129. {
  130. return FALSE;
  131. }
  132. // ASSUMPTION: pOEMDevmode is large enough to contain OEMDEV structure.
  133. // Make sure that dmOEMExtra indicates the current OEMDEV structure.
  134. pOEMDevmode->dmOEMExtra.dwSize = sizeof(OEMDEV);
  135. pOEMDevmode->dmOEMExtra.dwSignature = OEM_SIGNATURE;
  136. pOEMDevmode->dmOEMExtra.dwVersion = OEM_VERSION;
  137. // Set driver data, if not valid.
  138. if(pOEMDevmode->dwDriverData > 100)
  139. {
  140. pOEMDevmode->dwDriverData = 0;
  141. }
  142. // Set Advanced driver data, if not valid.
  143. if(pOEMDevmode->dwAdvancedData > 100)
  144. {
  145. pOEMDevmode->dwAdvancedData = 0;
  146. }
  147. return TRUE;
  148. }
  149. void Dump(PCOEMDEV pOEMDevmode)
  150. {
  151. if( (NULL != pOEMDevmode)
  152. &&
  153. (pOEMDevmode->dmOEMExtra.dwSize >= sizeof(OEMDEV))
  154. &&
  155. (OEM_SIGNATURE == pOEMDevmode->dmOEMExtra.dwSignature)
  156. )
  157. {
  158. VERBOSE(TEXT("\tdmOEMExtra.dwSize = %d\r\n"), pOEMDevmode->dmOEMExtra.dwSize);
  159. VERBOSE(TEXT("\tdmOEMExtra.dwSignature = %#x\r\n"), pOEMDevmode->dmOEMExtra.dwSignature);
  160. VERBOSE(TEXT("\tdmOEMExtra.dwVersion = %#x\r\n"), pOEMDevmode->dmOEMExtra.dwVersion);
  161. VERBOSE(TEXT("\tdwDriverData = %#x\r\n"), pOEMDevmode->dwDriverData);
  162. VERBOSE(TEXT("\tdwAdvancedData = %#x\r\n"), pOEMDevmode->dwAdvancedData);
  163. }
  164. else
  165. {
  166. ERR(ERRORTEXT("Dump(POEMDEV) unknown private OEM DEVMODE.\r\n"));
  167. }
  168. }