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.

536 lines
16 KiB

  1. /*++
  2. Copyright (c) 1996-2002 Microsoft Corp. & Ricoh Co., Ltd. All rights reserved.
  3. FILE: COMMON.C
  4. Abstract: Implementation of common functions for rendering & UI
  5. plugin module.
  6. Functions: OEMGetInfo
  7. OEMDevMode
  8. RWFileData
  9. Environment: Windows NT Unidrv5 driver
  10. Revision History:
  11. 04/07/1997 -zhanw-
  12. Created it.
  13. 02/29/2000 -Masatoshi Kubokura-
  14. Modified for PCL5e/PScript plugin from RPDL code.
  15. 05/31/2000 -Masatoshi Kubokura-
  16. V.1.11
  17. 08/02/2000 -Masatoshi Kubokura-
  18. V.1.11 for NT4
  19. 09/22/2000 -Masatoshi Kubokura-
  20. Last modified for XP inbox.
  21. 02/26/2002 -Masatoshi Kubokura-
  22. Include strsafe.h
  23. Add FileNameBufSize as arg3 at RWFileData().
  24. --*/
  25. #include <minidrv.h>
  26. #include "devmode.h"
  27. #include "oem.h"
  28. #include "resource.h"
  29. #ifndef WINNT_40
  30. #include "strsafe.h" // @Feb/26/2002
  31. #endif // !WINNT_40
  32. // shared data file between rendering and UI plugin
  33. #ifndef WINNT_40
  34. #define SHAREDFILENAME L"RIAF5.BIN"
  35. #else // WINNT_40
  36. #define SHAREDFILENAME L"\\2\\RIA%.3ls%02x.BIN"
  37. DWORD gdwDrvMemPoolTag = 'meoD'; // minidrv.h requires this global var
  38. #endif // WINNT_40
  39. #if DBG && !defined(KM_DRIVER)
  40. INT giDebugLevel = DBG_ERROR;
  41. #endif
  42. ////////////////////////////////////////////////////////
  43. // INTERNAL PROTOTYPES
  44. ////////////////////////////////////////////////////////
  45. static BOOL BInitOEMExtraData(POEMUD_EXTRADATA pOEMExtra);
  46. static BOOL BMergeOEMExtraData(POEMUD_EXTRADATA pdmIn, POEMUD_EXTRADATA pdmOut);
  47. static BOOL BIsValidOEMDevModeParam(DWORD dwMode, POEMDMPARAM pOEMDevModeParam);
  48. #if DBG
  49. static void VDumpOEMDevModeParam(POEMDMPARAM pOEMDevModeParam);
  50. #endif // DBG
  51. //////////////////////////////////////////////////////////////////////////
  52. // Function: OEMGetInfo
  53. //////////////////////////////////////////////////////////////////////////
  54. BOOL APIENTRY OEMGetInfo(DWORD dwInfo, PVOID pBuffer, DWORD cbSize, PDWORD pcbNeeded)
  55. {
  56. #if DBG
  57. LPCSTR OEM_INFO[] = { "Bad Index",
  58. "OEMGI_GETSIGNATURE",
  59. "OEMGI_GETINTERFACEVERSION",
  60. "OEMGI_GETVERSION",
  61. };
  62. VERBOSE((DLLTEXT("OEMGetInfo(%s) entry.\n"), OEM_INFO[dwInfo]));
  63. #endif // DBG
  64. // Validate parameters.
  65. if( ( (OEMGI_GETSIGNATURE != dwInfo) &&
  66. (OEMGI_GETINTERFACEVERSION != dwInfo) &&
  67. (OEMGI_GETVERSION != dwInfo) ) ||
  68. (NULL == pcbNeeded)
  69. )
  70. {
  71. ERR(("OEMGetInfo() ERROR_INVALID_PARAMETER.\n"));
  72. // Did not write any bytes.
  73. if(NULL != pcbNeeded)
  74. *pcbNeeded = 0;
  75. return FALSE;
  76. }
  77. // Need/wrote 4 bytes.
  78. *pcbNeeded = 4;
  79. // Validate buffer size. Minimum size is four bytes.
  80. if( (NULL == pBuffer) || (4 > cbSize) )
  81. {
  82. ERR(("OEMGetInfo() ERROR_INSUFFICIENT_BUFFER.\n"));
  83. return FALSE;
  84. }
  85. // Write information to buffer.
  86. switch(dwInfo)
  87. {
  88. case OEMGI_GETSIGNATURE:
  89. *(LPDWORD)pBuffer = OEM_SIGNATURE;
  90. break;
  91. case OEMGI_GETINTERFACEVERSION:
  92. *(LPDWORD)pBuffer = PRINTER_OEMINTF_VERSION;
  93. break;
  94. case OEMGI_GETVERSION:
  95. *(LPDWORD)pBuffer = OEM_VERSION;
  96. break;
  97. }
  98. return TRUE;
  99. } //*** OEMGetInfo
  100. //////////////////////////////////////////////////////////////////////////
  101. // Function: OEMDevMode
  102. //////////////////////////////////////////////////////////////////////////
  103. BOOL APIENTRY OEMDevMode(DWORD dwMode, POEMDMPARAM pOEMDevModeParam)
  104. {
  105. #if DBG
  106. LPCSTR OEMDevMode_fMode[] = { "NULL",
  107. "OEMDM_SIZE",
  108. "OEMDM_DEFAULT",
  109. "OEMDM_CONVERT",
  110. "OEMDM_MERGE",
  111. };
  112. VERBOSE((DLLTEXT("OEMDevMode(%s) entry.\n"), OEMDevMode_fMode[dwMode]));
  113. #endif // DBG
  114. // Validate parameters.
  115. if(!BIsValidOEMDevModeParam(dwMode, pOEMDevModeParam))
  116. {
  117. #if DBG
  118. ERR(("OEMDevMode() ERROR_INVALID_PARAMETER.\n"));
  119. VDumpOEMDevModeParam(pOEMDevModeParam);
  120. #endif // DBG
  121. return FALSE;
  122. }
  123. // Verify OEM extra data size.
  124. if( (dwMode != OEMDM_SIZE) &&
  125. sizeof(OEMUD_EXTRADATA) > pOEMDevModeParam->cbBufSize )
  126. {
  127. ERR(("OEMDevMode() ERROR_INSUFFICIENT_BUFFER.\n"));
  128. return FALSE;
  129. }
  130. // Handle dwMode.
  131. switch(dwMode)
  132. {
  133. case OEMDM_SIZE:
  134. pOEMDevModeParam->cbBufSize = sizeof(OEMUD_EXTRADATA);
  135. break;
  136. case OEMDM_DEFAULT:
  137. #if defined(WINNT_40) && defined(DISKLESSMODEL) // @Aug/02/2000
  138. // Because NT4 spooler doesn't support collate, we clear dmCollate.
  139. // Later at OEMUICallBack, if printer collate is available, we set
  140. // dmCollate.
  141. pOEMDevModeParam->pPublicDMIn->dmCollate = DMCOLLATE_FALSE;
  142. pOEMDevModeParam->pPublicDMIn->dmFields &= ~DM_COLLATE;
  143. #endif
  144. return BInitOEMExtraData((POEMUD_EXTRADATA)pOEMDevModeParam->pOEMDMOut);
  145. case OEMDM_CONVERT:
  146. case OEMDM_MERGE:
  147. if(!BMergeOEMExtraData((POEMUD_EXTRADATA)pOEMDevModeParam->pOEMDMIn,
  148. (POEMUD_EXTRADATA)pOEMDevModeParam->pOEMDMOut) )
  149. {
  150. ERR(("OEMUD OEMDevMode(): not valid OEM Extra Data.\n"));
  151. return FALSE;
  152. }
  153. break;
  154. }
  155. return TRUE;
  156. } //*** OEMDevMode
  157. //////////////////////////////////////////////////////////////////////////
  158. // Function: BInitOEMExtraData
  159. //
  160. // Description: Initializes OEM Extra data.
  161. //
  162. // Parameters:
  163. // pOEMExtra Pointer to a OEM Extra data.
  164. // dwSize Size of OEM extra data.
  165. //
  166. // Returns: TRUE if successful; FALSE otherwise.
  167. //
  168. // Comments:
  169. //
  170. // History:
  171. // 02/11/1997 APresley Created.
  172. // 02/28/2000 Masatoshi Kubokura Modified for PCL5e/PScript
  173. //
  174. //////////////////////////////////////////////////////////////////////////
  175. BOOL BInitOEMExtraData(POEMUD_EXTRADATA pOEMExtra)
  176. {
  177. INT num;
  178. // Initialize OEM Extra data.
  179. pOEMExtra->dmOEMExtra.dwSize = sizeof(OEMUD_EXTRADATA);
  180. pOEMExtra->dmOEMExtra.dwSignature = OEM_SIGNATURE;
  181. pOEMExtra->dmOEMExtra.dwVersion = OEM_VERSION;
  182. pOEMExtra->fUiOption = 0;
  183. pOEMExtra->JobType = IDC_RADIO_JOB_NORMAL;
  184. pOEMExtra->LogDisabled = IDC_RADIO_LOG_DISABLED;
  185. return TRUE;
  186. } //*** BInitOEMExtraData
  187. //////////////////////////////////////////////////////////////////////////
  188. // Function: BMergeOEMExtraData
  189. //
  190. // Description: Validates and merges OEM Extra data.
  191. //
  192. // Parameters:
  193. // pdmIn pointer to an input OEM private devmode containing the settings
  194. // to be validated and merged. Its size is current.
  195. // pdmOut pointer to the output OEM private devmode containing the
  196. // default settings.
  197. //
  198. // Returns: TRUE if valid; FALSE otherwise.
  199. //
  200. // Comments:
  201. //
  202. // History:
  203. // 02/11/1997 APresley Created.
  204. // 04/08/1997 ZhanW Modified the interface
  205. // 08/11/1997 Masatoshi Kubokura Modified for RPDL
  206. //
  207. //////////////////////////////////////////////////////////////////////////
  208. BOOL BMergeOEMExtraData(POEMUD_EXTRADATA pdmIn, POEMUD_EXTRADATA pdmOut)
  209. {
  210. if(pdmIn) {
  211. LPBYTE pDst = (LPBYTE)&(pdmOut->fUiOption);
  212. LPBYTE pSrc = (LPBYTE)&(pdmIn->fUiOption);
  213. DWORD dwCount = sizeof(OEMUD_EXTRADATA) - sizeof(OEM_DMEXTRAHEADER);
  214. //
  215. // copy over the private fields, if they are valid
  216. //
  217. while (dwCount-- > 0)
  218. *pDst++ = *pSrc++;
  219. }
  220. return TRUE;
  221. } //*** BMergeOEMExtraData
  222. //////////////////////////////////////////////////////////////////////////
  223. // Function: BIsValidOEMDevModeParam
  224. //
  225. // Description: Validates OEM_DEVMODEPARAM structure.
  226. //
  227. // Parameters:
  228. // dwMode calling mode
  229. // pOEMDevModeParam Pointer to a OEMDEVMODEPARAM structure.
  230. //
  231. // Returns: TRUE if valid; FALSE otherwise.
  232. //
  233. // Comments:
  234. //
  235. // History:
  236. // 02/11/1997 APresley Created.
  237. //
  238. //////////////////////////////////////////////////////////////////////////
  239. static BOOL BIsValidOEMDevModeParam(DWORD dwMode, POEMDMPARAM pOEMDevModeParam)
  240. {
  241. BOOL bValid = TRUE;
  242. if(NULL == pOEMDevModeParam)
  243. {
  244. ERR(("OEMUD IsValidOEMDevModeParam(): pOEMDevModeParam is NULL.\n"));
  245. return FALSE;
  246. }
  247. if(sizeof(OEMDMPARAM) > pOEMDevModeParam->cbSize)
  248. {
  249. ERR(("OEMUD IsValidOEMDevModeParam(): cbSize is smaller than sizeof(OEM_DEVMODEPARAM).\n"));
  250. bValid = FALSE;
  251. }
  252. if(NULL == pOEMDevModeParam->hPrinter)
  253. {
  254. ERR(("OEMUD IsValidOEMDevModeParam(): hPrinter is NULL.\n"));
  255. bValid = FALSE;
  256. }
  257. if(NULL == pOEMDevModeParam->hModule)
  258. {
  259. ERR(("OEMUD IsValidOEMDevModeParam(): hModule is NULL.\n"));
  260. bValid = FALSE;
  261. }
  262. if( (0 != pOEMDevModeParam->cbBufSize) &&
  263. (NULL == pOEMDevModeParam->pOEMDMOut)
  264. )
  265. {
  266. ERR(("OEMUD IsValidOEMDevModeParam(): pOEMDMOut is NULL when it should not be.\n"));
  267. bValid = FALSE;
  268. }
  269. if( (OEMDM_MERGE == dwMode) && (NULL == pOEMDevModeParam->pOEMDMIn) )
  270. {
  271. ERR(("OEMUD IsValidOEMDevModeParam(): pOEMDMIn is NULL when it should not be.\n"));
  272. bValid = FALSE;
  273. }
  274. return bValid;
  275. } //*** BIsValidOEMDevModeParam
  276. #if DBG
  277. //////////////////////////////////////////////////////////////////////////
  278. // Function: VDumpOEMDevModeParam
  279. //
  280. // Description: Debug dump of OEM_DEVMODEPARAM structure.
  281. //
  282. // Parameters:
  283. // pOEMDevModeParam Pointer to an OEM DevMode param structure.
  284. //
  285. // Returns: N/A.
  286. //
  287. // Comments:
  288. //
  289. // History:
  290. // 02/18/1997 APresley Created.
  291. //
  292. //////////////////////////////////////////////////////////////////////////
  293. static void VDumpOEMDevModeParam(POEMDMPARAM pOEMDevModeParam)
  294. {
  295. // Can't dump if pOEMDevModeParam NULL.
  296. if(NULL != pOEMDevModeParam)
  297. {
  298. VERBOSE(("\n\tOEM_DEVMODEPARAM dump:\n\n"));
  299. VERBOSE(("\tcbSize = %d.\n", pOEMDevModeParam->cbSize));
  300. VERBOSE(("\thPrinter = %#lx.\n", pOEMDevModeParam->hPrinter));
  301. VERBOSE(("\thModule = %#lx.\n", pOEMDevModeParam->hModule));
  302. VERBOSE(("\tpPublicDMIn = %#lx.\n", pOEMDevModeParam->pPublicDMIn));
  303. VERBOSE(("\tpPublicDMOut = %#lx.\n", pOEMDevModeParam->pPublicDMOut));
  304. VERBOSE(("\tpOEMDMIn = %#lx.\n", pOEMDevModeParam->pOEMDMIn));
  305. VERBOSE(("\tpOEMDMOut = %#lx.\n", pOEMDevModeParam->pOEMDMOut));
  306. VERBOSE(("\tcbBufSize = %d.\n", pOEMDevModeParam->cbBufSize));
  307. }
  308. } //*** VDumpOEMDevModeParam
  309. #endif // DBG
  310. //////////////////////////////////////////////////////////////////////////
  311. // Function: RWFileData
  312. //
  313. // Description: Read/Write common file between UI plugin and rendering
  314. // plugin
  315. //
  316. // Parameters:
  317. // pFileData pointer to file data structure
  318. // pwszFileName pointer to file name of private devmode
  319. // FileNameBufSize file name buffer size (add 02/26/2002)
  320. // type GENERIC_READ/GENERIC_WRITE
  321. //
  322. // Returns: TRUE if valid; FALSE otherwise.
  323. //
  324. // Comments: Rendering plugin records printing-done flag to the file.
  325. // Both rendering plugin and UI plugin can know that status.
  326. //
  327. // History:
  328. // 09/30/1998 Masatoshi Kubokura Created.
  329. // 08/16/1999 takashim modified for Unidrv5.4 on NT4.
  330. // 09/01/1999 Kubokura modified for Unidrv5.4 on NT4.
  331. // 02/26/2002 Kubokura added FileNameBufSize param.
  332. //
  333. //////////////////////////////////////////////////////////////////////////
  334. BOOL RWFileData(PFILEDATA pFileData, LPWSTR pwszFileName, LONG FileNameBufSize, LONG type)
  335. {
  336. HANDLE hFile;
  337. DWORD dwSize;
  338. BOOL bRet = FALSE;
  339. #ifndef KM_DRIVER
  340. WCHAR szFileName[MY_MAX_PATH]; // MY_MAX_PATH=80
  341. #endif // KM_DRIVER
  342. #if DBG
  343. giDebugLevel = DBG_VERBOSE;
  344. #endif
  345. VERBOSE(("** Filename[0]=%d (%ls) **\n", pwszFileName[0], pwszFileName));
  346. #ifndef KM_DRIVER
  347. #ifndef WINNT_40
  348. //
  349. // CAUTION:
  350. // TempPath is different whether EMF spool is enable(system) or not(user).
  351. // We need to store the file name to private devmode.
  352. //
  353. // Set data data file name to private devmode at first time
  354. if (0 == pwszFileName[0])
  355. {
  356. if (0 == (dwSize = GetTempPath(MY_MAX_PATH, szFileName)))
  357. {
  358. ERR(("Could not get temp directory."));
  359. return bRet;
  360. }
  361. // @Feb/26/2002 ->
  362. // wcscpy(&szFileName[dwSize], SHAREDFILENAME);
  363. StringCbCopyW(&szFileName[dwSize], sizeof(szFileName) - dwSize, SHAREDFILENAME);
  364. // @Feb/26/2002 <-
  365. VERBOSE(("** Set Filename: %ls **\n", szFileName));
  366. // copy file name to private devmode
  367. // @Feb/26/2002 ->
  368. // wcscpy(pwszFileName, szFileName);
  369. StringCbCopyW(pwszFileName, FileNameBufSize, szFileName);
  370. // @Feb/26/2002 <-
  371. }
  372. #else // WINNT_40
  373. //
  374. // CAUTION:
  375. // The file path differs on each PC in printer sharing.
  376. // We always update the file name (with path) of private devmode.
  377. // (@Sep/03/99)
  378. //
  379. // Kernel-mode driver (NT4 RPDLRES.DLL) can access files under
  380. // %systemroot%\system32. Driver directory will be OK.
  381. if (GetPrinterDriverDirectory(NULL, NULL, 1, (PBYTE)szFileName,
  382. sizeof(szFileName), &dwSize))
  383. {
  384. WCHAR szValue[MY_MAX_PATH] = L"XXXX";
  385. DWORD dwSize2;
  386. DWORD dwNum = 0; // @Sep/21/99
  387. PWCHAR pwszTmp; // @Sep/21/99
  388. // Make unique filename "RIAXXXNN.BIN". "XXX" is filled with top 3char of username.
  389. dwSize2 = GetEnvironmentVariable(L"USERNAME", szValue, MY_MAX_PATH);
  390. // @Sep/21/99 ->
  391. // wsprintf(&szFileName[dwSize/sizeof(WCHAR)-1], SHAREDFILENAME, szValue, dwSize2);
  392. pwszTmp = szValue;
  393. while (dwSize2-- > 0)
  394. dwNum += (DWORD)*pwszTmp++;
  395. wsprintf(&szFileName[dwSize/sizeof(WCHAR)-1], SHAREDFILENAME, szValue, (BYTE)dwNum);
  396. // @Sep/21/99 <-
  397. VERBOSE(("** Set Filename: %ls **\n", szFileName));
  398. // copy file name to private devmode
  399. wcscpy(pwszFileName, szFileName);
  400. }
  401. else
  402. {
  403. ERR(("Could not get printer driver directory.(dwSize=%d)", dwSize));
  404. return bRet;
  405. }
  406. #endif // WINNT_40
  407. hFile = CreateFile((LPTSTR) pwszFileName, // filename
  408. type, // open for read/write
  409. FILE_SHARE_READ, // share to read
  410. NULL, // no security
  411. OPEN_ALWAYS, // open existing file,or open new if not exist
  412. FILE_ATTRIBUTE_NORMAL, // normal file
  413. NULL); // no attr. template
  414. if (INVALID_HANDLE_VALUE == hFile)
  415. {
  416. ERR(("Could not create data file."));
  417. return bRet;
  418. }
  419. if (GENERIC_WRITE == type)
  420. bRet = WriteFile(hFile, (PBYTE)pFileData, sizeof(FILEDATA), &dwSize, NULL);
  421. else if (GENERIC_READ == type)
  422. bRet = ReadFile(hFile, (PBYTE)pFileData, sizeof(FILEDATA), &dwSize, NULL);
  423. VERBOSE(("** RWFileData: bRet=%d, dwSize=%d**\n", bRet, dwSize));
  424. // Close files.
  425. CloseHandle(hFile);
  426. #else // KM_DRIVER
  427. if (0 != pwszFileName[0])
  428. {
  429. PBYTE pTemp;
  430. if (GENERIC_WRITE == type)
  431. {
  432. hFile = DrvMapFileForWrite(pwszFileName, sizeof (FILEDATA),
  433. &pTemp, &dwSize);
  434. if (NULL != hFile)
  435. {
  436. memcpy(pTemp, pFileData, sizeof (FILEDATA));
  437. DrvUnMapFile(hFile);
  438. bRet = TRUE;
  439. }
  440. }
  441. else
  442. {
  443. hFile = DrvMapFileForRead(pwszFileName, &pTemp, &dwSize);
  444. if (NULL != hFile)
  445. {
  446. memcpy(pFileData, pTemp, sizeof (FILEDATA));
  447. DrvUnMapFile(hFile);
  448. bRet = TRUE;
  449. }
  450. }
  451. }
  452. #endif // KM_DRIVER
  453. #if DBG
  454. giDebugLevel = DBG_ERROR;
  455. #endif
  456. return bRet;
  457. } //*** RWFileData