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.

1702 lines
50 KiB

  1. /*++
  2. Copyright (c) 1996-1999 Microsoft Corporation
  3. Module Name:
  4. ib587res.c
  5. Abstract:
  6. Implementation of GPD command callback for "test.gpd":
  7. OEMCommandCallback
  8. Environment:
  9. Windows NT Unidrv driver
  10. --*/
  11. #include "pdev.h"
  12. #include <windows.h>
  13. #include <stdio.h>
  14. #include <strsafe.h>
  15. HANDLE RevertToPrinterSelf( VOID );
  16. BOOL ImpersonatePrinterClient( HANDLE );
  17. /*--------------------------------------------------------------------------*/
  18. /* G L O B A L V A L U E */
  19. /*--------------------------------------------------------------------------*/
  20. //Command strings
  21. //
  22. const BYTE CMD_BEGIN_DOC_1[] = {0x1B,0x7E,0xB0,0x00,0x12,0x01} ;
  23. const BYTE CMD_BEGIN_DOC_2[] = {0x01,0x01,0x00} ;
  24. const BYTE CMD_BEGIN_DOC_3[] = {0x02,0x02,0xFF,0xFF} ;
  25. const BYTE CMD_BEGIN_DOC_4[] = {0x03,0x02,0xFF,0xFF} ;
  26. const BYTE CMD_BEGIN_DOC_5[] = {0x04,0x04,0xFF,0xFF,0xFF,0xFF} ;
  27. // ISSUE-2002/3/18-takashim - Not sure why CMD_BEGIN_PAGE[] was defined as 2 bytes.
  28. //const BYTE CMD_BEGIN_PAGE[] = {0xD4, 0x00} ;
  29. const BYTE CMD_BEGIN_PAGE[] = {0xD4} ;
  30. const BYTE CMD_END_JOB[] = {0x1B,0x7E,0xB0,0x00,0x04,0x01,0x01,0x01,0x01} ;
  31. const BYTE CMD_END_PAGE[] = {0x20};
  32. //SetPac
  33. #define CMD_SETPAC pOEM->SetPac
  34. #define CMD_SETPAC_FRONT_TRAY_PAPER_SIZE 4
  35. #define CMD_SETPAC_INPUT_BIN 5
  36. #define CMD_SETPAC_RESOLUTION 12
  37. #define CMD_SETPAC_1ST_CASSETTE_PAPER_SIZE 14
  38. #define CMD_SETPAC_2ND_CASSETTE_PAPER_SIZE 15
  39. #define CMD_SETPAC_PAGE_LENGTH 19 // 4 bytes
  40. #define CMD_SETPAC_TONER_SAVE_MODE 25
  41. #define CMD_SETPAC_CUSTOM_DOTS_PER_LINE 26 // 2 bytes
  42. #define CMD_SETPAC_CUSTOM_LINES_PER_PAGE 28 // 2 bytes
  43. const BYTE CMD_SETPAC_TMPL[
  44. CMD_SETPAC_SIZE] ={ 0xD7,
  45. 0x01,
  46. 0xD0,
  47. 0x1D, //CommandLength
  48. 0x00, //Front tray paper size
  49. 0x00, //Input-bin
  50. 0x01,
  51. 0x04, //Bit-assign1
  52. 0xD9, //Bit-assign2
  53. 0x04, //EET
  54. 0x02, //PrintDensity
  55. 0x01,
  56. 0x00, //Resolution
  57. 0x01,
  58. 0x00, //1st cassette paper size
  59. 0x00, //2nd cassette paper size
  60. 0x0F, //Time to Power Save
  61. 0x00,
  62. 0x01, //Compression Mode
  63. 0x00,0x00,0x00,0x00, //PageLength
  64. 0x07,
  65. 0x00, //Number of Copies
  66. 0x00, //Toner save mode
  67. 0x00,0x00, //Dot per line for custom size
  68. 0x00,0x00, //Data lines per page for custom size
  69. 0x00} ;
  70. const BYTE Mask[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01} ;
  71. const POINTL phySize300[] = {
  72. // Width,Height Physical paper size for 300dpi
  73. {3416,4872}, //A3
  74. {2392,3416}, //A4
  75. {1672,2392}, //A5
  76. {2944,4208}, //B4
  77. {2056,2944}, //B5
  78. {1088,1656}, //PostCard
  79. {2456,3208}, //Letter
  80. {2456,4112}, //Legal
  81. {0000,0000}, //user define
  82. };
  83. const POINTL phySize600[] = {
  84. // Width,Height Physical paper size for 600dpi
  85. {6832,9736}, //A3
  86. {4776,6832}, //A4
  87. {3336,4776}, //A5
  88. {5888,8416}, //B4
  89. {4112,5888}, //B5
  90. {2176,3312}, //PostCard
  91. {4912,6416}, //Letter
  92. {4912,8216}, //Legal
  93. {0000,0000}, //user define
  94. };
  95. /******************* FUNCTIONS *************************/
  96. BOOL MyDeleteFile(PDEVOBJ pdevobj, LPSB lpsb) ;
  97. BOOL InitSpoolBuffer(LPSB lpsb) ;
  98. BOOL MyCreateFile(PDEVOBJ pdevobj, LPSB lpsb) ;
  99. BOOL MySpool(PDEVOBJ pdevobj, LPSB lpsb, PBYTE pBuf, DWORD dwLen) ;
  100. BOOL SpoolOut(PDEVOBJ pdevobj, LPSB lpsb) ;
  101. BOOL MyEndDoc(PDEVOBJ pdevobj) ;
  102. BOOL WriteFileForP_Paper(PDEVOBJ pdevobj, PBYTE pBuf, DWORD dwLen) ;
  103. BOOL WriteFileForL_Paper(PDEVOBJ pdevobj, PBYTE pBuf, DWORD dwLen) ;
  104. BOOL FillPageRestData(PDEVOBJ pdevobj) ;
  105. BOOL SpoolWhiteData(PDEVOBJ pdevobj, DWORD dwWhiteLen, BOOL fComp) ;
  106. BOOL SendPageData(PDEVOBJ pdevobj, PBYTE pSrcImage, DWORD dwLen) ;
  107. BOOL SpoolOutChangedData(PDEVOBJ pdevobj, LPSB lpsb) ;
  108. WORD GetPrintableArea(WORD physSize, INT iRes) ;
  109. BOOL AllocTempBuffer(PIBMPDEV pOEM, DWORD dwNewBufLen) ;
  110. BOOL MyEndPage(PDEVOBJ pdevobj) ;
  111. BOOL MyStartDoc(PDEVOBJ pdevobj) ;
  112. BOOL SpoolOutCompStart(PSOCOMP pSoc);
  113. BOOL SpoolOutCompEnd(PSOCOMP pSoc, PDEVOBJ pdevobj, LPSB psb);
  114. BOOL SpoolOutComp(PSOCOMP pSoc, PDEVOBJ pdevobj, LPSB psb,
  115. PBYTE pjBuf, DWORD dwLen);
  116. /*****************************************************************************/
  117. /* */
  118. /* Module: IB587RES.DLL *
  119. /* */
  120. /* Function: OEMEnablePDEV */
  121. /* */
  122. /* Syntax: PDEVOEM APIENTRY OEMEnablePDEV( */
  123. /* PDEVOBJ pdevobj, */
  124. /* PWSTR pPrinterName, */
  125. /* ULONG cPatterns, */
  126. /* HSURF *phsurfPatterns, */
  127. /* ULONG cjGdiInfo, */
  128. /* GDIINFO *pGdiInfo, */
  129. /* ULONG cjDevInfo, */
  130. /* DEVINFO *pDevInfo, */
  131. /* DRVENABLEDATA *pded) */
  132. /* */
  133. /* Description: Allocate buffer of private data to pdevobj */
  134. /* */
  135. /*****************************************************************************/
  136. PDEVOEM APIENTRY
  137. OEMEnablePDEV(
  138. PDEVOBJ pdevobj,
  139. PWSTR pPrinterName,
  140. ULONG cPatterns,
  141. HSURF *phsurfPatterns,
  142. ULONG cjGdiInfo,
  143. GDIINFO *pGdiInfo,
  144. ULONG cjDevInfo,
  145. DEVINFO *pDevInfo,
  146. DRVENABLEDATA *pded)
  147. {
  148. PIBMPDEV pOEM;
  149. if (!VALID_PDEVOBJ(pdevobj))
  150. {
  151. return NULL;
  152. }
  153. if(!pdevobj->pdevOEM)
  154. {
  155. if(!(pdevobj->pdevOEM = MemAlloc(sizeof(IBMPDEV))))
  156. {
  157. //DBGPRINT(DBG_WARNING, (ERRORTEXT("OEMEnablePDEV:Memory alloc failed.\n")));
  158. return NULL;
  159. }
  160. }
  161. pOEM = (PIBMPDEV)(pdevobj->pdevOEM);
  162. // Setup pdev specific conrol block fields
  163. ZeroMemory(pOEM, sizeof(IBMPDEV));
  164. CopyMemory(pOEM->SetPac, CMD_SETPAC_TMPL, sizeof(CMD_SETPAC_TMPL));
  165. return pdevobj->pdevOEM;
  166. }
  167. /*****************************************************************************/
  168. /* */
  169. /* Module: IB587RES.DLL */
  170. /* */
  171. /* Function: OEMDisablePDEV */
  172. /* */
  173. /* Description: Free buffer of private data */
  174. /* */
  175. /*****************************************************************************/
  176. VOID APIENTRY
  177. OEMDisablePDEV(
  178. PDEVOBJ pdevobj)
  179. {
  180. if (!VALID_PDEVOBJ(pdevobj))
  181. {
  182. return;
  183. }
  184. if(pdevobj->pdevOEM)
  185. {
  186. if (((PIBMPDEV)(pdevobj->pdevOEM))->pTempImage) {
  187. MemFree(((PIBMPDEV)(pdevobj->pdevOEM))->pTempImage);
  188. ((PIBMPDEV)(pdevobj->pdevOEM))->pTempImage = 0;
  189. }
  190. MemFree(pdevobj->pdevOEM);
  191. pdevobj->pdevOEM = NULL;
  192. }
  193. return;
  194. }
  195. BOOL APIENTRY OEMResetPDEV(
  196. PDEVOBJ pdevobjOld,
  197. PDEVOBJ pdevobjNew)
  198. {
  199. PIBMPDEV pOEMOld, pOEMNew;
  200. PBYTE pTemp;
  201. DWORD dwTemp;
  202. if (!VALID_PDEVOBJ(pdevobjOld)
  203. || !VALID_PDEVOBJ(pdevobjNew))
  204. {
  205. return FALSE;
  206. }
  207. pOEMOld = (PIBMPDEV)pdevobjOld->pdevOEM;
  208. pOEMNew = (PIBMPDEV)pdevobjNew->pdevOEM;
  209. if (pOEMOld != NULL && pOEMNew != NULL) {
  210. // Save pointer and length
  211. pTemp = pOEMNew->pTempImage;
  212. dwTemp = pOEMNew->dwTempBufLen;
  213. *pOEMNew = *pOEMOld;
  214. // Restore..
  215. pOEMNew->pTempImage = pTemp;
  216. pOEMNew->dwTempBufLen = dwTemp;
  217. }
  218. return TRUE;
  219. }
  220. /*****************************************************************************/
  221. /* */
  222. /* Module: OEMFilterGraphics */
  223. /* */
  224. /* Function: */
  225. /* */
  226. /* Syntax: BOOL APIENTRY OEMFilterGraphics(PDEVOBJ, PBYTE, DWORD) */
  227. /* */
  228. /* Input: pdevobj address of PDEVICE structure */
  229. /* pBuf points to buffer of graphics data */
  230. /* dwLen length of buffer in bytes */
  231. /* */
  232. /* Output: BOOL */
  233. /* */
  234. /* Notice: nFunction and Escape numbers are the same */
  235. /* */
  236. /*****************************************************************************/
  237. BOOL
  238. APIENTRY
  239. OEMFilterGraphics(
  240. PDEVOBJ pdevobj,
  241. PBYTE pBuf,
  242. DWORD dwLen)
  243. {
  244. PIBMPDEV pOEM;
  245. BOOL bRet;
  246. if (!VALID_PDEVOBJ(pdevobj))
  247. {
  248. return FALSE;
  249. }
  250. pOEM = (PIBMPDEV)pdevobj->pdevOEM;
  251. bRet = TRUE;
  252. if(pOEM->fChangeDirection) {
  253. bRet = WriteFileForL_Paper(pdevobj, pBuf, dwLen);
  254. }else{
  255. bRet = WriteFileForP_Paper(pdevobj, pBuf, dwLen);
  256. }
  257. return bRet;
  258. }
  259. /*****************************************************************************/
  260. /* */
  261. /* Module: OEMCommandCallback */
  262. /* */
  263. /* Function: */
  264. /* */
  265. /* Syntax: INT APIENTRY OEMCommandCallback(PDEVOBJ,DWORD,DWORD,PDWORD) */
  266. /* */
  267. /* Input: pdevobj */
  268. /* dwCmdCbID */
  269. /* dwCount */
  270. /* pdwParams */
  271. /* */
  272. /* Output: INT */
  273. /* */
  274. /* Notice: */
  275. /* */
  276. /*****************************************************************************/
  277. INT APIENTRY
  278. OEMCommandCallback(
  279. PDEVOBJ pdevobj, // Points to private data required by the Unidriver.dll
  280. DWORD dwCmdCbID, // Callback ID
  281. DWORD dwCount, // Counts of command parameter
  282. PDWORD pdwParams ) // points to values of command params
  283. {
  284. PIBMPDEV pOEM;
  285. WORD wPhysWidth;
  286. WORD wPhysHeight;
  287. WORD wDataLen ;
  288. WORD wLines ;
  289. WORD wNumOfByte ;
  290. POINTL ptlUserDefSize;
  291. BYTE byOutput[64];
  292. DWORD dwNeeded;
  293. DWORD dwOptionsReturned;
  294. INT iRet;
  295. if (!VALID_PDEVOBJ(pdevobj))
  296. {
  297. return FALSE;
  298. }
  299. pOEM = (PIBMPDEV)pdevobj->pdevOEM;
  300. iRet = 0;
  301. switch(dwCmdCbID)
  302. {
  303. case PAGECONTROL_BEGIN_DOC:
  304. if (!InitSpoolBuffer(&(pOEM->sb)))
  305. goto fail;
  306. if (!MyCreateFile(pdevobj, &(pOEM->sb)))
  307. goto fail;
  308. if (!MyCreateFile(pdevobj, &(pOEM->sbcomp)))
  309. goto fail;
  310. pOEM->fChangeDirection = FALSE ;
  311. pOEM->sPageNum = 0 ;
  312. if (!MyStartDoc(pdevobj))
  313. goto fail;
  314. break;
  315. case PAGECONTROL_BEGIN_PAGE:
  316. pOEM->dwCurCursorY = 0 ;
  317. pOEM->dwOffset = 0 ;
  318. pOEM->sPageNum ++ ;
  319. if(pOEM->fChangeDirection == FALSE) {
  320. if (!MySpool(pdevobj, &(pOEM->sb),
  321. (PBYTE)CMD_BEGIN_PAGE, sizeof(CMD_BEGIN_PAGE)))
  322. goto fail;
  323. if (!SpoolOutCompStart(&pOEM->Soc))
  324. goto fail;
  325. }
  326. break;
  327. case PAGECONTROL_END_PAGE:
  328. if(pOEM->fChangeDirection == FALSE){
  329. if (!FillPageRestData(pdevobj))
  330. goto fail;
  331. if (!SpoolOutCompEnd(&pOEM->Soc, pdevobj, &pOEM->sb))
  332. goto fail;
  333. if (!MySpool(pdevobj,&(pOEM->sb),
  334. (PBYTE)CMD_END_PAGE, sizeof(CMD_END_PAGE)))
  335. goto fail;
  336. }else{
  337. if (!SpoolOutChangedData(pdevobj, &(pOEM->sbcomp)))
  338. goto fail;
  339. }
  340. if (!MyEndPage(pdevobj))
  341. goto fail;
  342. break;
  343. case PAGECONTROL_ABORT_DOC:
  344. case PAGECONTROL_END_DOC:
  345. if (!MyEndDoc(pdevobj))
  346. goto fail;
  347. break;
  348. case RESOLUTION_300:
  349. pOEM->ulHorzRes = 300;
  350. pOEM->ulVertRes = 300;
  351. CMD_SETPAC[CMD_SETPAC_RESOLUTION] = 0x02 ;
  352. if (pOEM->sPaperSize < PHYS_PAPER_BASE
  353. || pOEM->sPaperSize > PHYS_PAPER_MAX)
  354. {
  355. goto fail;
  356. }
  357. else if( pOEM->sPaperSize == PHYS_PAPER_UNFIXED){
  358. pOEM->szlPhysSize.cx = PARAM(pdwParams, 0);
  359. pOEM->szlPhysSize.cy = PARAM(pdwParams, 1);
  360. ptlUserDefSize.x = GetPrintableArea((WORD)pOEM->szlPhysSize.cx, RESOLUTION_300);
  361. ptlUserDefSize.y = GetPrintableArea((WORD)pOEM->szlPhysSize.cy, RESOLUTION_300);
  362. pOEM->ptlLogSize = ptlUserDefSize;
  363. CMD_SETPAC[CMD_SETPAC_CUSTOM_DOTS_PER_LINE] = LOBYTE((WORD)(ptlUserDefSize.x));
  364. CMD_SETPAC[CMD_SETPAC_CUSTOM_DOTS_PER_LINE + 1] = HIBYTE((WORD)(ptlUserDefSize.x));
  365. CMD_SETPAC[CMD_SETPAC_CUSTOM_LINES_PER_PAGE] = LOBYTE((WORD)(ptlUserDefSize.y));
  366. CMD_SETPAC[CMD_SETPAC_CUSTOM_LINES_PER_PAGE + 1] = HIBYTE((WORD)(ptlUserDefSize.y));
  367. }
  368. else
  369. {
  370. pOEM->ptlLogSize = phySize300[
  371. pOEM->sPaperSize-PHYS_PAPER_BASE];
  372. }
  373. break;
  374. case RESOLUTION_600:
  375. pOEM->ulHorzRes = 600;
  376. pOEM->ulVertRes = 600;
  377. pOEM->ptlLogSize = phySize600[pOEM->sPaperSize-50];
  378. CMD_SETPAC[CMD_SETPAC_RESOLUTION] = 0x20 ;
  379. if (pOEM->sPaperSize < PHYS_PAPER_BASE
  380. || pOEM->sPaperSize > PHYS_PAPER_MAX)
  381. {
  382. goto fail;
  383. }
  384. else if( pOEM->sPaperSize == PHYS_PAPER_UNFIXED){
  385. pOEM->szlPhysSize.cx = PARAM(pdwParams, 0);
  386. pOEM->szlPhysSize.cy = PARAM(pdwParams, 1);
  387. ptlUserDefSize.x = GetPrintableArea((WORD)pOEM->szlPhysSize.cx, RESOLUTION_600);
  388. ptlUserDefSize.y = GetPrintableArea((WORD)pOEM->szlPhysSize.cy, RESOLUTION_600);
  389. pOEM->ptlLogSize = ptlUserDefSize;
  390. CMD_SETPAC[CMD_SETPAC_CUSTOM_DOTS_PER_LINE] = LOBYTE((WORD)(ptlUserDefSize.x));
  391. CMD_SETPAC[CMD_SETPAC_CUSTOM_DOTS_PER_LINE + 1] = HIBYTE((WORD)(ptlUserDefSize.x));
  392. CMD_SETPAC[CMD_SETPAC_CUSTOM_LINES_PER_PAGE] = LOBYTE((WORD)(ptlUserDefSize.y));
  393. CMD_SETPAC[CMD_SETPAC_CUSTOM_LINES_PER_PAGE + 1] = HIBYTE((WORD)(ptlUserDefSize.y));
  394. }
  395. else {
  396. pOEM->ptlLogSize = phySize600[
  397. pOEM->sPaperSize-PHYS_PAPER_BASE];
  398. }
  399. break;
  400. case SEND_BLOCK_DATA:
  401. wNumOfByte = (WORD)PARAM(pdwParams, 0);
  402. pOEM->wImgHeight = (WORD)PARAM(pdwParams, 1);
  403. pOEM->wImgWidth = (WORD)PARAM(pdwParams, 2);
  404. break;
  405. case ORIENTATION_PORTRAIT: // 28
  406. case ORIENTATION_LANDSCAPE: // 29
  407. switch(pOEM->sPaperSize){
  408. case PHYS_PAPER_A3 :
  409. case PHYS_PAPER_B4 :
  410. case PHYS_PAPER_LEGAL :
  411. case PHYS_PAPER_POSTCARD :
  412. pOEM->fChangeDirection = FALSE ;
  413. pOEM->fComp = TRUE ;
  414. break;
  415. case PHYS_PAPER_A4 :
  416. case PHYS_PAPER_A5 :
  417. case PHYS_PAPER_B5 :
  418. case PHYS_PAPER_LETTER :
  419. pOEM->fChangeDirection = TRUE ;
  420. pOEM->fComp = FALSE ;
  421. break;
  422. case PHYS_PAPER_UNFIXED : /* Paper is not rotated in UNFIXED case */
  423. pOEM->fChangeDirection = FALSE ;
  424. pOEM->fComp = TRUE ;
  425. break;
  426. }
  427. break;
  428. case PHYS_PAPER_A3: // 50
  429. pOEM->sPaperSize = PHYS_PAPER_A3 ;
  430. CMD_SETPAC[CMD_SETPAC_FRONT_TRAY_PAPER_SIZE] = 0x04 ;
  431. CMD_SETPAC[CMD_SETPAC_1ST_CASSETTE_PAPER_SIZE] = 0x04 ;
  432. CMD_SETPAC[CMD_SETPAC_2ND_CASSETTE_PAPER_SIZE] = 0x04 ;
  433. break ;
  434. case PHYS_PAPER_A4: // 51
  435. pOEM->sPaperSize = PHYS_PAPER_A4 ;
  436. CMD_SETPAC[CMD_SETPAC_FRONT_TRAY_PAPER_SIZE] = 0x83 ;
  437. CMD_SETPAC[CMD_SETPAC_1ST_CASSETTE_PAPER_SIZE] = 0x83 ;
  438. CMD_SETPAC[CMD_SETPAC_2ND_CASSETTE_PAPER_SIZE] = 0x83 ;
  439. break ;
  440. case PHYS_PAPER_B4: // 54
  441. pOEM->sPaperSize = PHYS_PAPER_B4 ;
  442. CMD_SETPAC[CMD_SETPAC_FRONT_TRAY_PAPER_SIZE] = 0x07 ;
  443. CMD_SETPAC[CMD_SETPAC_1ST_CASSETTE_PAPER_SIZE] = 0x07 ;
  444. CMD_SETPAC[CMD_SETPAC_2ND_CASSETTE_PAPER_SIZE] = 0x07 ;
  445. break ;
  446. case PHYS_PAPER_LETTER: // 57
  447. pOEM->sPaperSize = PHYS_PAPER_LETTER ;
  448. CMD_SETPAC[CMD_SETPAC_FRONT_TRAY_PAPER_SIZE] = 0x90 ;
  449. CMD_SETPAC[CMD_SETPAC_1ST_CASSETTE_PAPER_SIZE] = 0x90 ;
  450. CMD_SETPAC[CMD_SETPAC_2ND_CASSETTE_PAPER_SIZE] = 0x90 ;
  451. break ;
  452. case PHYS_PAPER_LEGAL: // 58
  453. pOEM->sPaperSize = PHYS_PAPER_LEGAL ;
  454. CMD_SETPAC[CMD_SETPAC_FRONT_TRAY_PAPER_SIZE] = 0x11 ;
  455. CMD_SETPAC[CMD_SETPAC_1ST_CASSETTE_PAPER_SIZE] = 0x11 ;
  456. CMD_SETPAC[CMD_SETPAC_2ND_CASSETTE_PAPER_SIZE] = 0x11 ;
  457. break ;
  458. case PHYS_PAPER_B5: // 55
  459. pOEM->sPaperSize = PHYS_PAPER_B5 ;
  460. CMD_SETPAC[CMD_SETPAC_FRONT_TRAY_PAPER_SIZE] = 0x86 ;
  461. CMD_SETPAC[CMD_SETPAC_1ST_CASSETTE_PAPER_SIZE] = 0x86 ;
  462. CMD_SETPAC[CMD_SETPAC_2ND_CASSETTE_PAPER_SIZE] = 0x86 ;
  463. break ;
  464. case PHYS_PAPER_A5: // 52
  465. pOEM->sPaperSize = PHYS_PAPER_A5 ;
  466. CMD_SETPAC[CMD_SETPAC_FRONT_TRAY_PAPER_SIZE] = 0x82 ;
  467. CMD_SETPAC[CMD_SETPAC_1ST_CASSETTE_PAPER_SIZE] = 0x82 ;
  468. CMD_SETPAC[CMD_SETPAC_2ND_CASSETTE_PAPER_SIZE] = 0x82 ;
  469. break ;
  470. case PHYS_PAPER_POSTCARD: // 59
  471. pOEM->sPaperSize = PHYS_PAPER_POSTCARD ;
  472. CMD_SETPAC[CMD_SETPAC_FRONT_TRAY_PAPER_SIZE] = 0x17 ;
  473. CMD_SETPAC[CMD_SETPAC_1ST_CASSETTE_PAPER_SIZE] = 0x17 ;
  474. CMD_SETPAC[CMD_SETPAC_2ND_CASSETTE_PAPER_SIZE] = 0x17 ;
  475. break ;
  476. case PHYS_PAPER_UNFIXED: // 60
  477. pOEM->sPaperSize = PHYS_PAPER_UNFIXED ;
  478. CMD_SETPAC[CMD_SETPAC_FRONT_TRAY_PAPER_SIZE] = 0x3F ;
  479. CMD_SETPAC[CMD_SETPAC_1ST_CASSETTE_PAPER_SIZE] = 0x3F ;
  480. CMD_SETPAC[CMD_SETPAC_2ND_CASSETTE_PAPER_SIZE] = 0x3F ;
  481. break ;
  482. case PAPER_SRC_FTRAY:
  483. CMD_SETPAC[CMD_SETPAC_INPUT_BIN] = 0x01 ;
  484. break ;
  485. case PAPER_SRC_CAS1:
  486. CMD_SETPAC[CMD_SETPAC_INPUT_BIN] = 0x02 ;
  487. break;
  488. case PAPER_SRC_CAS2:
  489. CMD_SETPAC[CMD_SETPAC_INPUT_BIN] = 0x04 ;
  490. break;
  491. case PAPER_SRC_AUTO:
  492. CMD_SETPAC[CMD_SETPAC_INPUT_BIN] = 0x04 ;
  493. break;
  494. case TONER_SAVE_MEDIUM: // 100
  495. CMD_SETPAC[CMD_SETPAC_TONER_SAVE_MODE] = 0x02 ;
  496. break;
  497. case TONER_SAVE_DARK: // 101
  498. CMD_SETPAC[CMD_SETPAC_TONER_SAVE_MODE] = 0x04 ;
  499. break;
  500. case TONER_SAVE_LIGHT: // 102
  501. CMD_SETPAC[CMD_SETPAC_TONER_SAVE_MODE] = 0x01 ;
  502. break;
  503. case PAGECONTROL_MULTI_COPIES:
  504. CMD_SETPAC[24] = (BYTE)PARAM(pdwParams, 0);
  505. pOEM->sCopyNum = (BYTE)PARAM(pdwParams, 0);
  506. break;
  507. case Y_REL_MOVE :
  508. if (0 == pOEM->ulHorzRes)
  509. goto fail;
  510. pOEM->dwYmove=(WORD)*pdwParams/(MASTERUNIT/(WORD)pOEM->ulHorzRes);
  511. // ISSUE-2002/3/18-takashim - Faking Unidrv here?
  512. // iRet = 0; below is intentional: Retuning dwYmove will cause
  513. // incorrect outputs.
  514. // DestYRel in GPD means the coordinate relative to the current
  515. // cursor position. Here, minidriver is always returning 0 (no move)
  516. // to Unidrv, so it is always absolute coordinate (relative to
  517. // the origin)?
  518. if(pOEM->dwCurCursorY < pOEM->dwYmove){
  519. pOEM->dwYmove -= pOEM->dwCurCursorY ;
  520. }else{
  521. pOEM->dwYmove = 0 ;
  522. }
  523. // iRet = pOEM->dwYmove;
  524. iRet = 0;
  525. break;
  526. default:
  527. break;
  528. }
  529. return iRet;
  530. fail:
  531. return -1;
  532. }
  533. /*****************************************************************************/
  534. /* */
  535. /* Module: GetPrintableArea */
  536. /* */
  537. /* Function: Calculate PrintableArea for user defined paper */
  538. /* */
  539. /* Syntax: WORD GetPrintableArea(WORD physSize, INT iRes) */
  540. /* */
  541. /* Input: physSize */
  542. /* iRes */
  543. /* */
  544. /* Output: WORD */
  545. /* */
  546. /* Notice: */
  547. /* */
  548. /*****************************************************************************/
  549. WORD GetPrintableArea(WORD physSize, INT iRes)
  550. {
  551. DWORD dwArea ;
  552. DWORD dwPhysSizeMMx10 = physSize * 254 / MASTERUNIT;
  553. /* Unit of phySize is MASTERUNIT(=1200) */
  554. if(iRes == RESOLUTION_300){
  555. dwArea = (((WORD)(( ( (DWORD)(dwPhysSizeMMx10*300/25.4) -
  556. 2*( (DWORD)(4*300*10/25.4) ) ) / 10 +7)/8))) * 8;
  557. }else{
  558. dwArea = (((WORD)(( ( (DWORD)(dwPhysSizeMMx10*600/25.4) -
  559. 2*( (DWORD)(4*600*10/25.4) ) ) / 10 +7)/8))) * 8;
  560. }
  561. return (WORD)dwArea ;
  562. }
  563. // NOTICE-2002/3/18/-takashim - Comment
  564. // // #94193: shold create temp. file on spooler directory.
  565. //
  566. /*++
  567. Routine Description:
  568. This function comes up with a name for a spool file that we should be
  569. able to write to.
  570. Note: The file name returned has already been created.
  571. Arguments:
  572. hPrinter - handle to the printer that we want a spool file for.
  573. ppwchSpoolFileName: pointer that will receive an allocated buffer
  574. containing the file name to spool to. CALLER
  575. MUST FREE. Use LocalFree().
  576. Return Value:
  577. TRUE if everything goes as expected.
  578. FALSE if anything goes wrong.
  579. --*/
  580. BOOL
  581. GetSpoolFileName(
  582. IN HANDLE hPrinter,
  583. IN OUT PWCHAR pwchSpoolPath
  584. )
  585. {
  586. PBYTE pBuffer = NULL;
  587. DWORD dwAllocSize;
  588. DWORD dwNeeded = 0;
  589. DWORD dwRetval;
  590. HANDLE hToken=NULL;
  591. hToken = RevertToPrinterSelf();
  592. //
  593. // In order to find out where the spooler's directory is, we add
  594. // call GetPrinterData with DefaultSpoolDirectory.
  595. //
  596. dwAllocSize = ( MAX_PATH ) * sizeof (WCHAR);
  597. for (;;)
  598. {
  599. pBuffer = LocalAlloc( LMEM_FIXED, dwAllocSize );
  600. if ( pBuffer == NULL )
  601. {
  602. ERR((DLLTEXT("LocalAlloc faild, %d\n"), GetLastError()));
  603. goto Failure;
  604. }
  605. if ( GetPrinterData( hPrinter,
  606. SPLREG_DEFAULT_SPOOL_DIRECTORY,
  607. NULL,
  608. pBuffer,
  609. dwAllocSize,
  610. &dwNeeded ) == ERROR_SUCCESS )
  611. {
  612. break;
  613. }
  614. if ( ( dwNeeded < dwAllocSize ) ||( GetLastError() != ERROR_MORE_DATA ))
  615. {
  616. ERR((DLLTEXT("GetPrinterData failed in a non-understood way.\n")));
  617. goto Failure;
  618. }
  619. //
  620. // Free the current buffer and increase the size that we try to allocate
  621. // next time around.
  622. //
  623. LocalFree( pBuffer );
  624. dwAllocSize = dwNeeded;
  625. }
  626. // FUTURE-2002/3/18-takashim - Temp file path restricted to ANSI.
  627. // According to the SDK document, the pathname handled by GetTempFileName
  628. // must be consist of ANSI characters. What happens with double-byte
  629. // characters, etc??
  630. if( !GetTempFileName( (LPWSTR)pBuffer, TEMP_NAME_PREFIX, 0, pwchSpoolPath ))
  631. {
  632. goto Failure;
  633. }
  634. //
  635. // At this point, the spool file name should be done. Free the structure
  636. // we used to get the spooler temp dir and return.
  637. //
  638. LocalFree( pBuffer );
  639. if (NULL != hToken) {
  640. if (!ImpersonatePrinterClient(hToken))
  641. {
  642. // failure..
  643. return FALSE;
  644. }
  645. }
  646. return( TRUE );
  647. Failure:
  648. //
  649. // Clean up and fail.
  650. //
  651. if ( pBuffer != NULL )
  652. {
  653. LocalFree( pBuffer );
  654. }
  655. if (hToken != NULL)
  656. {
  657. (void)ImpersonatePrinterClient(hToken);
  658. }
  659. return ( FALSE );
  660. }
  661. //SPLBUF is used for control temp files.
  662. //This printer need the number of bytes of whole page data.
  663. BOOL InitSpoolBuffer(LPSB lpsb)
  664. {
  665. lpsb->dwWrite = 0 ;
  666. lpsb->TempName[0] = __TEXT('\0') ;
  667. lpsb->hFile = INVALID_HANDLE_VALUE ;
  668. return TRUE;
  669. }
  670. BOOL MyCreateFile(PDEVOBJ pdevobj, LPSB lpsb)
  671. {
  672. HANDLE hToken = NULL;
  673. BOOL bRet = FALSE;
  674. if (!GetSpoolFileName(pdevobj->hPrinter, lpsb->TempName)) {
  675. //DBGPRINT(DBG_WARNING, ("GetSpoolFileName failed.\n"));
  676. goto fail;
  677. }
  678. hToken = RevertToPrinterSelf();
  679. lpsb->hFile = CreateFile((LPCTSTR)lpsb->TempName,
  680. (GENERIC_READ | GENERIC_WRITE),
  681. 0,
  682. NULL,
  683. CREATE_ALWAYS,
  684. FILE_ATTRIBUTE_NORMAL,
  685. NULL) ;
  686. if(lpsb->hFile == INVALID_HANDLE_VALUE)
  687. {
  688. //DBGPRINT(DBG_WARNING, ("Tmp file cannot create.\n"));
  689. DeleteFile(lpsb->TempName);
  690. lpsb->TempName[0] = __TEXT('\0') ;
  691. goto fail;
  692. }
  693. bRet = TRUE;
  694. fail:
  695. if (hToken) (void)ImpersonatePrinterClient(hToken);
  696. return bRet ;
  697. }
  698. BOOL MyDeleteFile(PDEVOBJ pdevobj, LPSB lpsb)
  699. {
  700. HANDLE hToken = NULL;
  701. BOOL bRet = FALSE;
  702. if(lpsb->hFile != INVALID_HANDLE_VALUE){
  703. if (0 == CloseHandle(lpsb->hFile)) {
  704. //DBGPRINT(DBG_WARNING, ("CloseHandle error %d\n"));
  705. goto fail;
  706. }
  707. lpsb->hFile = INVALID_HANDLE_VALUE ;
  708. hToken = RevertToPrinterSelf();
  709. if (0 == DeleteFile(lpsb->TempName)) {
  710. //DBGPRINT(DBG_WARNING, ("DeleteName error %d\n",GetLastError()));
  711. goto fail;
  712. }
  713. lpsb->TempName[0] = __TEXT('\0');
  714. }
  715. bRet = TRUE;
  716. fail:
  717. if (hToken) (void)ImpersonatePrinterClient(hToken);
  718. return bRet;
  719. }
  720. //Spool page data to temp file
  721. BOOL MySpool
  722. (PDEVOBJ pdevobj,
  723. LPSB lpsb,
  724. PBYTE pBuf,
  725. DWORD dwLen)
  726. {
  727. DWORD dwTemp, dwTemp2;
  728. BYTE *pTemp;
  729. if (lpsb->hFile != INVALID_HANDLE_VALUE) {
  730. pTemp = pBuf;
  731. dwTemp = dwLen;
  732. while (dwTemp > 0) {
  733. if (0 == WriteFile(lpsb->hFile,
  734. pTemp,
  735. dwTemp,
  736. &dwTemp2,
  737. NULL)
  738. || dwTemp2 > dwTemp) {
  739. ERR((DLLTEXT("WriteFile error in CacheData %d.\n"),
  740. GetLastError()));
  741. return FALSE;
  742. }
  743. pTemp += dwTemp2;
  744. dwTemp -= dwTemp2;
  745. lpsb->dwWrite += dwTemp2 ;
  746. }
  747. return TRUE;
  748. }
  749. else {
  750. return WRITESPOOLBUF(pdevobj, pBuf, dwLen);
  751. }
  752. }
  753. //Dump out temp file to printer
  754. BOOL
  755. SpoolOut(PDEVOBJ pdevobj, LPSB lpsb)
  756. {
  757. DWORD dwSize, dwTemp, dwTemp2;
  758. HANDLE hFile;
  759. BYTE Buf[SPOOL_OUT_BUF_SIZE];
  760. hFile = lpsb->hFile ;
  761. dwSize = lpsb->dwWrite ;
  762. VERBOSE(("dwSize=%ld\n", dwSize));
  763. if (0L != SetFilePointer(hFile, 0L, NULL, FILE_BEGIN)) {
  764. ERR((DLLTEXT("SetFilePointer failed %d\n"),
  765. GetLastError()));
  766. return FALSE;
  767. }
  768. for ( ; dwSize > 0; dwSize -= dwTemp2) {
  769. dwTemp = ((SPOOL_OUT_BUF_SIZE < dwSize)
  770. ? SPOOL_OUT_BUF_SIZE : dwSize);
  771. if (0 == ReadFile(hFile, Buf, dwTemp, &dwTemp2, NULL)
  772. || dwTemp2 > dwTemp) {
  773. ERR((DLLTEXT("ReadFile error in SendCachedData.\n")));
  774. return FALSE;
  775. }
  776. if (dwTemp2 > 0) {
  777. if (!WRITESPOOLBUF(pdevobj, Buf, dwTemp2))
  778. return FALSE;
  779. }
  780. }
  781. return TRUE;
  782. }
  783. BOOL MyStartDoc(PDEVOBJ pdevobj)
  784. {
  785. return
  786. WRITESPOOLBUF(pdevobj, (PBYTE)CMD_BEGIN_DOC_1, sizeof(CMD_BEGIN_DOC_1)) &&
  787. WRITESPOOLBUF(pdevobj, (PBYTE)CMD_BEGIN_DOC_2, sizeof(CMD_BEGIN_DOC_2)) &&
  788. WRITESPOOLBUF(pdevobj, (PBYTE)CMD_BEGIN_DOC_3, sizeof(CMD_BEGIN_DOC_3)) &&
  789. WRITESPOOLBUF(pdevobj, (PBYTE)CMD_BEGIN_DOC_4, sizeof(CMD_BEGIN_DOC_4)) &&
  790. WRITESPOOLBUF(pdevobj, (PBYTE)CMD_BEGIN_DOC_5, sizeof(CMD_BEGIN_DOC_5));
  791. }
  792. BOOL MyEndPage(PDEVOBJ pdevobj)
  793. {
  794. PIBMPDEV pOEM;
  795. LPSB lpsb, lpsbco ;
  796. DWORD dwPageLen ;
  797. WORD wTmph, wTmpl ;
  798. pOEM = (PIBMPDEV)pdevobj->pdevOEM;
  799. lpsb = &(pOEM->sb) ;
  800. lpsbco = &(pOEM->sbcomp) ;
  801. if(pOEM->fChangeDirection == FALSE) {
  802. dwPageLen = lpsb->dwWrite;
  803. }
  804. else {
  805. dwPageLen = lpsbco->dwWrite ;
  806. }
  807. // NOTICE-2002/3/18-takashim - What is this?
  808. dwPageLen -= 3 ; //End page Command Len
  809. VERBOSE(("MyEndPage - dwPageLen=%ld\n",
  810. dwPageLen));
  811. wTmpl = LOWORD(dwPageLen) ;
  812. wTmph = HIWORD(dwPageLen) ;
  813. CMD_SETPAC[CMD_SETPAC_PAGE_LENGTH] = LOBYTE(wTmpl);
  814. CMD_SETPAC[CMD_SETPAC_PAGE_LENGTH + 1] = HIBYTE(wTmpl);
  815. CMD_SETPAC[CMD_SETPAC_PAGE_LENGTH + 2] = LOBYTE(wTmph);
  816. CMD_SETPAC[CMD_SETPAC_PAGE_LENGTH + 3] = HIBYTE(wTmph);
  817. if (!WRITESPOOLBUF(pdevobj, CMD_SETPAC, sizeof(CMD_SETPAC)))
  818. return FALSE;
  819. if(pOEM->fChangeDirection == FALSE){
  820. if (!SpoolOut(pdevobj, lpsb))
  821. return FALSE;
  822. }else{
  823. if (!SpoolOut(pdevobj, lpsbco))
  824. return FALSE;
  825. }
  826. //InitFiles
  827. lpsbco->dwWrite = 0 ;
  828. lpsb->dwWrite = 0 ;
  829. if(INVALID_SET_FILE_POINTER==SetFilePointer(lpsb->hFile,0,NULL,FILE_BEGIN)){
  830. ERR((DLLTEXT("SetFilePointer failed %d\n"),
  831. GetLastError()));
  832. return FALSE;
  833. }
  834. if(INVALID_SET_FILE_POINTER==SetFilePointer(lpsbco->hFile,0,NULL,FILE_BEGIN)){
  835. ERR((DLLTEXT("SetFilePointer failed %d\n"),
  836. GetLastError()));
  837. return FALSE;
  838. }
  839. return TRUE;
  840. }
  841. BOOL MyEndDoc(PDEVOBJ pdevobj)
  842. {
  843. PIBMPDEV pOEM;
  844. LPSB lpsb, lpsbco ;
  845. WORD wTmph, wTmpl ;
  846. DWORD dwPageLen ;
  847. SHORT i ;
  848. LPPD lppdTemp ;
  849. BOOL bRet = FALSE;
  850. pOEM = (PIBMPDEV)pdevobj->pdevOEM;
  851. lpsb = &(pOEM->sb) ;
  852. lpsbco = &(pOEM->sbcomp) ;
  853. if (!WRITESPOOLBUF(pdevobj, (PBYTE)CMD_END_JOB, sizeof(CMD_END_JOB)))
  854. goto fail;
  855. if (!MyDeleteFile(pdevobj, lpsb))
  856. goto fail;
  857. if (!MyDeleteFile(pdevobj, lpsbco))
  858. goto fail;
  859. bRet = TRUE;
  860. fail:
  861. return bRet;
  862. }
  863. BOOL WriteFileForP_Paper(PDEVOBJ pdevobj, PBYTE pBuf, DWORD dwLen)
  864. {
  865. PIBMPDEV pOEM;
  866. ULONG ulHorzPixel;
  867. WORD wCompLen;
  868. DWORD dwWhiteLen ;
  869. DWORD dwTmp ;
  870. pOEM = (PIBMPDEV)pdevobj->pdevOEM;
  871. if (pOEM->dwYmove > 0) {
  872. dwWhiteLen = pOEM->wImgWidth * pOEM->dwYmove;
  873. if (!SpoolWhiteData(pdevobj, dwWhiteLen, TRUE))
  874. return FALSE;
  875. pOEM->dwCurCursorY += pOEM->dwYmove;
  876. pOEM->dwYmove = 0;
  877. }
  878. pOEM->dwCurCursorY += dwLen/pOEM->wImgWidth - 1 ;
  879. return SendPageData(pdevobj, pBuf, dwLen) ;
  880. }
  881. BOOL WriteFileForL_Paper(PDEVOBJ pdevobj, PBYTE pBuf, DWORD dwLen)
  882. {
  883. PIBMPDEV pOEM;
  884. ULONG ulHorzPixel;
  885. WORD wCompLen;
  886. DWORD dwWhiteLen ;
  887. DWORD i, j;
  888. DWORD dwHeight, dwWidth;
  889. PBYTE pTemp;
  890. pOEM = (PIBMPDEV)pdevobj->pdevOEM;
  891. if (pOEM->dwYmove > 0) {
  892. dwWhiteLen = pOEM->wImgWidth * pOEM->dwYmove;
  893. if (!SpoolWhiteData(pdevobj, dwWhiteLen, FALSE))
  894. return FALSE;
  895. pOEM->dwCurCursorY += pOEM->dwYmove;
  896. pOEM->dwYmove = 0;
  897. }
  898. pOEM->dwCurCursorY += dwLen/pOEM->wImgWidth - 1 ;
  899. pOEM->dwOffset ++ ;
  900. return MySpool(pdevobj, &(pOEM->sb), pBuf,dwLen);
  901. }
  902. //fill page blanks.
  903. BOOL FillPageRestData(PDEVOBJ pdevobj)
  904. {
  905. PIBMPDEV pOEM ;
  906. DWORD dwRestHigh ;
  907. DWORD dwWhiteLen ;
  908. pOEM = (PIBMPDEV)pdevobj->pdevOEM;
  909. dwRestHigh = pOEM->ptlLogSize.y - pOEM->dwCurCursorY ;
  910. if(dwRestHigh <= 0)
  911. return TRUE;
  912. dwWhiteLen = pOEM->ptlLogSize.x * dwRestHigh;
  913. return SpoolWhiteData(pdevobj, dwWhiteLen, pOEM->fComp);
  914. }
  915. //not white data
  916. BOOL SendPageData(PDEVOBJ pdevobj, PBYTE pSrcImage, DWORD dwLen)
  917. {
  918. PIBMPDEV pOEM;
  919. pOEM = (PIBMPDEV)pdevobj->pdevOEM;
  920. return SpoolOutComp(&pOEM->Soc, pdevobj, &pOEM->sb, pSrcImage, dwLen);
  921. }
  922. BOOL SpoolWhiteData(PDEVOBJ pdevobj, DWORD dwWhiteLen, BOOL fComp)
  923. {
  924. PIBMPDEV pOEM;
  925. PBYTE pWhite ;
  926. WORD wCompLen ;
  927. DWORD dwTempLen ;
  928. pOEM = (PIBMPDEV)pdevobj->pdevOEM;
  929. if(dwWhiteLen == 0)
  930. return TRUE;
  931. if(dwWhiteLen > MAXIMGSIZE){
  932. dwTempLen = MAXIMGSIZE ;
  933. }else{
  934. dwTempLen = dwWhiteLen ;
  935. }
  936. if (!AllocTempBuffer(pOEM, dwTempLen))
  937. return FALSE;
  938. pWhite = pOEM->pTempImage;
  939. ZeroMemory(pWhite, dwTempLen);
  940. if(fComp == TRUE)
  941. {
  942. DWORD dwTemp;
  943. while (0 < dwWhiteLen) {
  944. if (MAXIMGSIZE <= dwWhiteLen)
  945. dwTemp = MAXIMGSIZE;
  946. else
  947. dwTemp = dwWhiteLen;
  948. if (!SpoolOutComp(&pOEM->Soc, pdevobj, &pOEM->sb, pWhite, dwTemp))
  949. return FALSE;
  950. dwWhiteLen -= dwTemp;
  951. }
  952. }
  953. else{
  954. if(dwWhiteLen > MAXIMGSIZE){
  955. while(dwWhiteLen > MAXIMGSIZE){
  956. if (!MySpool(pdevobj, &pOEM->sb, pWhite, MAXIMGSIZE))
  957. return FALSE;
  958. dwWhiteLen -= MAXIMGSIZE ;
  959. }
  960. }
  961. if(dwWhiteLen > 0){
  962. if (!MySpool(pdevobj, &pOEM->sb, pWhite, dwWhiteLen))
  963. return FALSE;
  964. }
  965. }
  966. return TRUE;
  967. }
  968. BOOL SpoolOutChangedData(PDEVOBJ pdevobj, LPSB lpsb)
  969. {
  970. PIBMPDEV pOEM;
  971. POINTL ptlDataPos ;
  972. DWORD dwFilePos, dwTemp;
  973. HANDLE hFile ;
  974. PBYTE pSaveFileData ;
  975. PBYTE pTemp;
  976. PBYTE pTransBuf ;
  977. DWORD X, Y;
  978. DWORD dwFirstPos ;
  979. INT h, i, j, k;
  980. POINTL ptlBand;
  981. PBYTE pSrc, pDst, pSrcSave;
  982. DWORD dwBandY, dwImageY, dwImageX;
  983. BOOL bBlank, bZero;
  984. BOOL bRet = FALSE;
  985. pOEM = (PIBMPDEV)pdevobj->pdevOEM;
  986. hFile = pOEM->sb.hFile ;
  987. // band size in pixels
  988. ptlBand.x = pOEM->ptlLogSize.y;
  989. ptlBand.y = TRANS_BAND_Y_SIZE;
  990. //�t�@�C���̓ǂݍ��݊J�n�ʒu���v�Z
  991. // Calculate read start positoin in the file
  992. ptlDataPos.x = 0 ;
  993. ptlDataPos.y = pOEM->dwCurCursorY + pOEM->dwOffset ;
  994. //�c�����A�P�s�̂��������]��
  995. // Y direction, blank area beneth.
  996. dwImageX = ((ptlDataPos.y + 7) / 8) * 8;
  997. // Buffer for loading file data (scan lines for a band)
  998. pSaveFileData = (PBYTE)MemAlloc((ptlBand.y / 8) * dwImageX);
  999. if (NULL == pSaveFileData) {
  1000. ERR(("Failed to allocate memory.\n"));
  1001. return FALSE;
  1002. }
  1003. // Buffer for transpositions (one scan line)
  1004. pTransBuf = (PBYTE)MemAlloc((ptlBand.x / 8));
  1005. if (NULL == pTransBuf) {
  1006. ERR(("Failed to allocate memory.\n"));
  1007. // #441444: PREFIX: reference NULL pointer.
  1008. goto out;
  1009. }
  1010. //�t�@�C���̓ǂݍ��݈ʒu�w���B
  1011. // Specify read start positoin in the file
  1012. dwFirstPos = pOEM->wImgWidth - 1;
  1013. if (!MySpool(pdevobj,&(pOEM->sbcomp),
  1014. (PBYTE)CMD_BEGIN_PAGE, sizeof(CMD_BEGIN_PAGE)))
  1015. goto out;
  1016. if (!SpoolOutCompStart(&pOEM->Soc))
  1017. goto out;
  1018. dwImageY = pOEM->wImgWidth;
  1019. bBlank = FALSE;
  1020. bZero = FALSE;
  1021. for (X = 0; X < (DWORD)pOEM->ptlLogSize.x / 8; X += dwBandY) {
  1022. //�]�����������ɃZ�b�g�B�ǂݔ��΂��B
  1023. // Set blank area, then read skip.
  1024. dwBandY = ptlBand.y / 8;
  1025. if (dwBandY > (DWORD)pOEM->ptlLogSize.x / 8 - X)
  1026. dwBandY = (DWORD)pOEM->ptlLogSize.x / 8 - X;
  1027. // White scanlines. Currently the trailing ones only,
  1028. // desired to be udpated to include others.
  1029. if (X >= dwImageY) {
  1030. bBlank = TRUE;
  1031. }
  1032. // Output white scanline.
  1033. if (bBlank) {
  1034. if (!bZero) {
  1035. ZeroMemory(pTransBuf, (ptlBand.x / 8));
  1036. bZero = TRUE;
  1037. }
  1038. for (i = 0; i < (INT)dwBandY * 8; i++) {
  1039. if (!SpoolOutComp(&pOEM->Soc, pdevobj, &pOEM->sbcomp,
  1040. (PBYTE)pTransBuf, (ptlBand.x / 8)))
  1041. goto out;
  1042. }
  1043. continue;
  1044. }
  1045. // Non-white scanlines.
  1046. pTemp = pSaveFileData ;
  1047. dwFilePos = pOEM->wImgWidth - X - dwBandY;
  1048. //�c�����P�s���t�@�C�������ǂݎ����B
  1049. // Read vertial one line from the file.
  1050. for (Y = 0; Y < dwImageX; pTemp += dwBandY, Y++) {
  1051. if (Y >= (DWORD)ptlDataPos.y) {
  1052. ZeroMemory(pTemp, dwBandY);
  1053. continue;
  1054. }
  1055. if(INVALID_SET_FILE_POINTER==SetFilePointer(hFile,dwFilePos,NULL,FILE_BEGIN)){
  1056. ERR((DLLTEXT("SetFilePointer failed %d\n"),
  1057. GetLastError()));
  1058. // #441442: PREFIX: leaking memory.
  1059. // return;
  1060. goto out;
  1061. }
  1062. if (0 == ReadFile(hFile, pTemp, dwBandY, &dwTemp, NULL)
  1063. || dwTemp > dwBandY) {
  1064. ERR(("Faild reading data from file. (%d)\n",
  1065. GetLastError()));
  1066. // #441442: PREFIX: leaking memory.
  1067. // return;
  1068. goto out;
  1069. }
  1070. dwFilePos += pOEM->wImgWidth;
  1071. }//End of Y loop
  1072. // Transposition and output dwBandY * 8 scan lines
  1073. for (h = 0; h < (INT)dwBandY; h++) {
  1074. //VERBOSE(("> %d/%d\n", h, dwBandY));
  1075. pSrcSave = pSaveFileData + dwBandY - 1 - h;
  1076. // Transposition and output eight scan lines
  1077. for (j = 0; j < 8; j++) {
  1078. pSrc = pSrcSave;
  1079. pDst = pTransBuf;
  1080. ZeroMemory(pDst, (ptlBand.x / 8));
  1081. // Transposition one scan line
  1082. for (i = 0; i < (INT)(dwImageX / 8); i++){
  1083. for (k = 0; k < 8; k++) {
  1084. if (0 != (*pSrc & Mask[7 - j])) {
  1085. *pDst |= Mask[k];
  1086. }
  1087. pSrc += dwBandY;
  1088. }
  1089. pDst++;
  1090. }
  1091. // Output one scan line
  1092. if (!SpoolOutComp(&pOEM->Soc, pdevobj, &pOEM->sbcomp,
  1093. (PBYTE)pTransBuf, (ptlBand.x / 8)))
  1094. goto out;
  1095. }
  1096. }
  1097. }
  1098. // Mark end of image
  1099. if (!SpoolOutCompEnd(&pOEM->Soc, pdevobj, &pOEM->sbcomp))
  1100. goto out;
  1101. if (!MySpool(pdevobj, &(pOEM->sbcomp),
  1102. (PBYTE)CMD_END_PAGE, sizeof(CMD_END_PAGE)))
  1103. goto out;
  1104. bRet = TRUE;
  1105. // #441442: PREFIX: leaking memory.
  1106. out:
  1107. if (NULL != pSaveFileData){
  1108. MemFree(pSaveFileData);
  1109. }
  1110. if(NULL != pTransBuf){
  1111. MemFree(pTransBuf);
  1112. }
  1113. return bRet;
  1114. }
  1115. BOOL
  1116. AllocTempBuffer(
  1117. PIBMPDEV pOEM,
  1118. DWORD dwNewBufLen)
  1119. {
  1120. if (NULL == pOEM->pTempImage ||
  1121. dwNewBufLen > pOEM->dwTempBufLen) {
  1122. if (NULL != pOEM->pTempImage) {
  1123. MemFree(pOEM->pTempImage);
  1124. }
  1125. pOEM->pTempImage = (PBYTE)MemAlloc(dwNewBufLen);
  1126. if (NULL == pOEM->pTempImage) {
  1127. WARNING(("Failed to allocate memory. (%d)\n",
  1128. GetLastError()));
  1129. pOEM->dwTempBufLen = 0;
  1130. return FALSE;
  1131. }
  1132. pOEM->dwTempBufLen = dwNewBufLen;
  1133. }
  1134. return TRUE;
  1135. }
  1136. BOOL
  1137. SpoolOutCompStart(
  1138. PSOCOMP pSoc)
  1139. {
  1140. pSoc->iNRCnt = 0;
  1141. pSoc->iRCnt = 0;
  1142. pSoc->iPrv = -1;
  1143. return TRUE;
  1144. }
  1145. BOOL
  1146. SpoolOutCompEnd(
  1147. PSOCOMP pSoc,
  1148. PDEVOBJ pdevobj,
  1149. LPSB psb)
  1150. {
  1151. BYTE jTemp;
  1152. if (0 < pSoc->iNRCnt) {
  1153. jTemp = ((BYTE)pSoc->iNRCnt) - 1;
  1154. if (!MySpool(pdevobj, psb, &jTemp, 1))
  1155. return FALSE;
  1156. if (!MySpool(pdevobj, psb, pSoc->pjNRBuf, pSoc->iNRCnt))
  1157. return FALSE;
  1158. pSoc->iNRCnt = 0;
  1159. }
  1160. if (0 < pSoc->iRCnt) {
  1161. jTemp = (0 - (BYTE)pSoc->iRCnt) + 1;
  1162. if (!MySpool(pdevobj, psb, &jTemp, 1))
  1163. return FALSE;
  1164. if (!MySpool(pdevobj, psb, &pSoc->iPrv, 1))
  1165. return FALSE;
  1166. pSoc->iRCnt = 0;
  1167. }
  1168. return TRUE;
  1169. }
  1170. BOOL
  1171. SpoolOutComp(
  1172. PSOCOMP pSoc,
  1173. PDEVOBJ pdevobj,
  1174. LPSB psb,
  1175. PBYTE pjBuf,
  1176. DWORD dwLen)
  1177. {
  1178. BYTE jCur, jTemp;
  1179. while (0 < dwLen--) {
  1180. jCur = *pjBuf++;
  1181. if (pSoc->iPrv == jCur) {
  1182. if (0 < pSoc->iNRCnt) {
  1183. if (1 < pSoc->iNRCnt) {
  1184. jTemp = ((BYTE)pSoc->iNRCnt - 1) - 1;
  1185. if (!MySpool(pdevobj, psb, &jTemp, 1))
  1186. return FALSE;
  1187. if (!MySpool(pdevobj, psb, pSoc->pjNRBuf, pSoc->iNRCnt - 1))
  1188. return FALSE;
  1189. }
  1190. pSoc->iNRCnt = 0;
  1191. pSoc->iRCnt = 1;
  1192. }
  1193. pSoc->iRCnt++;
  1194. if (RPEAK == pSoc->iRCnt) {
  1195. jTemp = (0 - (BYTE)pSoc->iRCnt) + 1;
  1196. if (!MySpool(pdevobj, psb, &jTemp, 1))
  1197. return FALSE;
  1198. if (!MySpool(pdevobj, psb, &jCur, 1))
  1199. return FALSE;
  1200. pSoc->iRCnt = 0;
  1201. }
  1202. }
  1203. else {
  1204. if (0 < pSoc->iRCnt) {
  1205. jTemp = (0 - (BYTE)pSoc->iRCnt) + 1;
  1206. if (!MySpool(pdevobj, psb, &jTemp, 1))
  1207. return FALSE;
  1208. if (!MySpool(pdevobj, psb, &pSoc->iPrv, 1))
  1209. return FALSE;
  1210. pSoc->iRCnt = 0;
  1211. }
  1212. pSoc->pjNRBuf[pSoc->iNRCnt++] = jCur;
  1213. if (NRPEAK == pSoc->iNRCnt) {
  1214. jTemp = ((BYTE)pSoc->iNRCnt) - 1;
  1215. if (!MySpool(pdevobj, psb, &jTemp, 1))
  1216. return FALSE;
  1217. if (!MySpool(pdevobj, psb, pSoc->pjNRBuf, pSoc->iNRCnt))
  1218. return FALSE;
  1219. pSoc->iNRCnt = 0;
  1220. }
  1221. }
  1222. pSoc->iPrv = jCur;
  1223. }
  1224. return TRUE;
  1225. }
  1226. /*++
  1227. Routine Name
  1228. ImpersonationToken
  1229. Routine Description:
  1230. This routine checks if a token is a primary token or an impersonation
  1231. token.
  1232. Arguments:
  1233. hToken - impersonation token or primary token of the process
  1234. Return Value:
  1235. TRUE, if the token is an impersonation token
  1236. FALSE, otherwise.
  1237. --*/
  1238. BOOL
  1239. ImpersonationToken(
  1240. IN HANDLE hToken
  1241. )
  1242. {
  1243. BOOL bRet = TRUE;
  1244. TOKEN_TYPE eTokenType;
  1245. DWORD cbNeeded;
  1246. DWORD LastError;
  1247. //
  1248. // Preserve the last error. Some callers of ImpersonatePrinterClient (which
  1249. // calls ImpersonationToken) rely on the fact that ImpersonatePrinterClient
  1250. // does not alter the last error.
  1251. //
  1252. LastError = GetLastError();
  1253. //
  1254. // Get the token type from the thread token. The token comes
  1255. // from RevertToPrinterSelf. An impersonation token cannot be
  1256. // queried, because RevertToPRinterSelf doesn't open it with
  1257. // TOKEN_QUERY access. That's why we assume that hToken is
  1258. // an impersonation token by default
  1259. //
  1260. if (GetTokenInformation(hToken,
  1261. TokenType,
  1262. &eTokenType,
  1263. sizeof(eTokenType),
  1264. &cbNeeded))
  1265. {
  1266. bRet = eTokenType == TokenImpersonation;
  1267. }
  1268. SetLastError(LastError);
  1269. return bRet;
  1270. }
  1271. /*++
  1272. Routine Name
  1273. RevertToPrinterSelf
  1274. Routine Description:
  1275. This routine will revert to the local system. It returns the token that
  1276. ImpersonatePrinterClient then uses to imersonate the client again. If the
  1277. current thread doesn't impersonate, then the function merely returns the
  1278. primary token of the process. (instead of returning NULL) Thus we honor
  1279. a request for reverting to printer self, even if the thread is not impersonating.
  1280. Arguments:
  1281. None.
  1282. Return Value:
  1283. NULL, if the function failed
  1284. HANDLE to token, otherwise.
  1285. --*/
  1286. HANDLE
  1287. RevertToPrinterSelf(
  1288. VOID
  1289. )
  1290. {
  1291. HANDLE NewToken, OldToken, cToken;
  1292. BOOL Status;
  1293. NewToken = NULL;
  1294. Status = OpenThreadToken(GetCurrentThread(),
  1295. TOKEN_IMPERSONATE,
  1296. TRUE,
  1297. &OldToken);
  1298. if (Status)
  1299. {
  1300. //
  1301. // We are currently impersonating
  1302. //
  1303. cToken = GetCurrentThread();
  1304. Status = SetThreadToken(&cToken,
  1305. NewToken);
  1306. if (!Status) {
  1307. return NULL;
  1308. }
  1309. }
  1310. else if (GetLastError() == ERROR_NO_TOKEN)
  1311. {
  1312. //
  1313. // We are not impersonating
  1314. //
  1315. Status = OpenProcessToken(GetCurrentProcess(),
  1316. TOKEN_QUERY,
  1317. &OldToken);
  1318. if (!Status) {
  1319. return NULL;
  1320. }
  1321. }
  1322. return OldToken;
  1323. }
  1324. /*++
  1325. Routine Name
  1326. ImpersonatePrinterClient
  1327. Routine Description:
  1328. This routine attempts to set the passed in hToken as the token for the
  1329. current thread. If hToken is not an impersonation token, then the routine
  1330. will simply close the token.
  1331. Arguments:
  1332. hToken - impersonation token or primary token of the process
  1333. Return Value:
  1334. TRUE, if the function succeeds in setting hToken
  1335. FALSE, otherwise.
  1336. --*/
  1337. BOOL
  1338. ImpersonatePrinterClient(
  1339. HANDLE hToken)
  1340. {
  1341. BOOL Status;
  1342. HANDLE cToken;
  1343. //
  1344. // Check if we have an impersonation token
  1345. //
  1346. if (ImpersonationToken(hToken))
  1347. {
  1348. cToken = GetCurrentThread();
  1349. Status = SetThreadToken(&cToken,
  1350. hToken);
  1351. if (!Status)
  1352. {
  1353. return FALSE;
  1354. }
  1355. }
  1356. CloseHandle(hToken);
  1357. return TRUE;
  1358. }