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.

1074 lines
32 KiB

  1. /*++
  2. Copyright (c) 1990 - 1995 Microsoft Corporation
  3. Module Name:
  4. forms.c
  5. Abstract:
  6. This module provides all the public exported APIs relating to the
  7. Driver-based Spooler Apis for the Local Print Providor
  8. SplAddForm
  9. SplDeleteForm
  10. SplSetForm
  11. SplGetForm
  12. SplEnumForms
  13. Support Functions in forms.c - (Warning! Do Not Add to this list!!)
  14. Author:
  15. Dave Snipp (DaveSn) 15-Mar-1991
  16. Revision History:
  17. --*/
  18. #include <precomp.h>
  19. #include <offsets.h>
  20. VOID
  21. BroadcastChangeForms(
  22. PINISPOOLER pIniSpooler);
  23. DWORD
  24. BroadcastChangeFormsThread(
  25. PINISPOOLER pIniSpooler);
  26. typedef struct _REG_FORM_INFO {
  27. SIZEL Size;
  28. RECTL ImageableArea;
  29. DWORD cFormOrder;
  30. DWORD Flags;
  31. } REG_FORM_INFO, *PREG_FORM_INFO;
  32. // These figures are accurate to .001 mm
  33. // There are 25.4 mm per inch
  34. BUILTIN_FORM BuiltInForms[] = {
  35. 0, IDS_FORM_LETTER, 215900, 279400, 0, 0, 215900, 279400,
  36. 0, IDS_FORM_LETTER_SMALL, 215900, 279400, 0, 0, 215900, 279400,
  37. 0, IDS_FORM_TABLOID, 279400, 431800, 0, 0, 279400, 431800,
  38. 0, IDS_FORM_LEDGER, 431800, 279400, 0, 0, 431800, 279400,
  39. 0, IDS_FORM_LEGAL, 215900, 355600, 0, 0, 215900, 355600,
  40. 0, IDS_FORM_STATEMENT, 139700, 215900, 0, 0, 139700, 215900,
  41. 0, IDS_FORM_EXECUTIVE, 184150, 266700, 0, 0, 184150, 266700,
  42. 0, IDS_FORM_A3, 297000, 420000, 0, 0, 297000, 420000,
  43. 0, IDS_FORM_A4, 210000, 297000, 0, 0, 210000, 297000,
  44. 0, IDS_FORM_A4_SMALL, 210000, 297000, 0, 0, 210000, 297000,
  45. 0, IDS_FORM_A5, 148000, 210000, 0, 0, 148000, 210000,
  46. 0, IDS_FORM_B4, 257000, 364000, 0, 0, 257000, 364000,
  47. 0, IDS_FORM_B5, 182000, 257000, 0, 0, 182000, 257000,
  48. 0, IDS_FORM_FOLIO, 215900, 330200, 0, 0, 215900, 330200,
  49. 0, IDS_FORM_QUARTO, 215000, 275000, 0, 0, 215000, 275000,
  50. 0, IDS_FORM_10X14, 254000, 355600, 0, 0, 254000, 355600,
  51. 0, IDS_FORM_11X17, 279400, 431800, 0, 0, 279400, 431800,
  52. 0, IDS_FORM_NOTE, 215900, 279400, 0, 0, 215900, 279400,
  53. 0, IDS_FORM_ENVELOPE9, 98425, 225425, 0, 0, 98425, 225425,
  54. 0, IDS_FORM_ENVELOPE10, 104775, 241300, 0, 0, 104775, 241300,
  55. 0, IDS_FORM_ENVELOPE11, 114300, 263525, 0, 0, 114300, 263525,
  56. 0, IDS_FORM_ENVELOPE12, 120650, 279400, 0, 0, 120650, 279400,
  57. 0, IDS_FORM_ENVELOPE14, 127000, 292100, 0, 0, 127000, 292100,
  58. 0, IDS_FORM_ENVELOPE_CSIZE_SHEET, 431800, 558800, 0, 0, 431800, 558800,
  59. 0, IDS_FORM_ENVELOPE_DSIZE_SHEET, 558800, 863600, 0, 0, 558800, 863600,
  60. 0, IDS_FORM_ENVELOPE_ESIZE_SHEET, 863600,1117600, 0, 0, 863600,1117600,
  61. 0, IDS_FORM_ENVELOPE_DL, 110000, 220000, 0, 0, 110000, 220000,
  62. 0, IDS_FORM_ENVELOPE_C5, 162000, 229000, 0, 0, 162000, 229000,
  63. 0, IDS_FORM_ENVELOPE_C3, 324000, 458000, 0, 0, 324000, 458000,
  64. 0, IDS_FORM_ENVELOPE_C4, 229000, 324000, 0, 0, 229000, 324000,
  65. 0, IDS_FORM_ENVELOPE_C6, 114000, 162000, 0, 0, 114000, 162000,
  66. 0, IDS_FORM_ENVELOPE_C65, 114000, 229000, 0, 0, 114000, 229000,
  67. 0, IDS_FORM_ENVELOPE_B4, 250000, 353000, 0, 0, 250000, 353000,
  68. 0, IDS_FORM_ENVELOPE_B5, 176000, 250000, 0, 0, 176000, 250000,
  69. 0, IDS_FORM_ENVELOPE_B6, 176000, 125000, 0, 0, 176000, 125000,
  70. 0, IDS_FORM_ENVELOPE, 110000, 230000, 0, 0, 110000, 230000,
  71. 0, IDS_FORM_ENVELOPE_MONARCH, 98425, 190500, 0, 0, 98425, 190500,
  72. 0, IDS_FORM_SIX34_ENVELOPE, 92075, 165100, 0, 0, 92075, 165100,
  73. 0, IDS_FORM_US_STD_FANFOLD, 377825, 279400, 0, 0, 377825, 279400,
  74. 0, IDS_FORM_GMAN_STD_FANFOLD, 215900, 304800, 0, 0, 215900, 304800,
  75. 0, IDS_FORM_GMAN_LEGAL_FANFOLD, 215900, 330200, 0, 0, 215900, 330200,
  76. // Predefined forms currently availble only in Win95. Included here
  77. // for compatibility.
  78. 0, IDS_FORM_ISO_B4, 250000, 353000, 0, 0, 250000, 353000,
  79. 0, IDS_FORM_JAPANESE_POSTCARD, 100000, 148000, 0, 0, 100000, 148000,
  80. 0, IDS_FORM_9X11, 228600, 279400, 0, 0, 228600, 279400,
  81. 0, IDS_FORM_10X11, 254000, 279400, 0, 0, 254000, 279400,
  82. 0, IDS_FORM_15X11, 381000, 279400, 0, 0, 381000, 279400,
  83. 0, IDS_FORM_ENV_INVITE, 220000, 220000, 0, 0, 220000, 220000,
  84. 0, IDS_FORM_RESERVED_48, 1, 1, 0, 0, 1, 1,
  85. 0, IDS_FORM_RESERVED_49, 1, 1, 0, 0, 1, 1,
  86. 0, IDS_FORM_LETTER_EXTRA, 241300, 304800, 0, 0, 241300, 304800,
  87. 0, IDS_FORM_LEGAL_EXTRA, 241300, 381000, 0, 0, 241300, 381000,
  88. 0, IDS_FORM_TABLOID_EXTRA, 304800, 457200, 0, 0, 304800, 457200,
  89. 0, IDS_FORM_A4_EXTRA, 235458, 322326, 0, 0, 235458, 322326,
  90. 0, IDS_FORM_LETTER_TRANSVERSE, 215900, 279400, 0, 0, 215900, 279400,
  91. 0, IDS_FORM_A4_TRANSVERSE, 210000, 297000, 0, 0, 210000, 297000,
  92. 0, IDS_FORM_LETTER_EXTRA_TRANSVERSE, 241300, 304800, 0, 0, 241300, 304800,
  93. 0, IDS_FORM_A_PLUS, 227000, 356000, 0, 0, 227000, 356000,
  94. 0, IDS_FORM_B_PLUS, 305000, 487000, 0, 0, 305000, 487000,
  95. 0, IDS_FORM_LETTER_PLUS, 215900, 322326, 0, 0, 215900, 322326,
  96. 0, IDS_FORM_A4_PLUS, 210000, 330000, 0, 0, 210000, 330000,
  97. 0, IDS_FORM_A5_TRANSVERSE, 148000, 210000, 0, 0, 148000, 210000,
  98. 0, IDS_FORM_B5_TRANSVERSE, 182000, 257000, 0, 0, 182000, 257000,
  99. 0, IDS_FORM_A3_EXTRA, 322000, 445000, 0, 0, 322000, 445000,
  100. 0, IDS_FORM_A5_EXTRA, 174000, 235000, 0, 0, 174000, 235000,
  101. 0, IDS_FORM_B5_EXTRA, 201000, 276000, 0, 0, 201000, 276000,
  102. 0, IDS_FORM_A2, 420000, 594000, 0, 0, 420000, 594000,
  103. 0, IDS_FORM_A3_TRANSVERSE, 297000, 420000, 0, 0, 297000, 420000,
  104. 0, IDS_FORM_A3_EXTRA_TRANSVERSE, 322000, 445000, 0, 0, 322000, 445000,
  105. 0, IDS_FORM_DBL_JAPANESE_POSTCARD, 200000, 148000, 0, 0, 200000, 148000,
  106. 0, IDS_FORM_A6, 105000, 148000, 0, 0, 105000, 148000,
  107. 0, IDS_FORM_JENV_KAKU2, 240000, 332000, 0, 0, 240000, 332000,
  108. 0, IDS_FORM_JENV_KAKU3, 216000, 277000, 0, 0, 216000, 277000,
  109. 0, IDS_FORM_JENV_CHOU3, 120000, 235000, 0, 0, 120000, 235000,
  110. 0, IDS_FORM_JENV_CHOU4, 90000, 205000, 0, 0, 90000, 205000,
  111. 0, IDS_FORM_LETTER_ROTATED, 279400, 215900, 0, 0, 279400, 215900,
  112. 0, IDS_FORM_A3_ROTATED, 420000, 297000, 0, 0, 420000, 297000,
  113. 0, IDS_FORM_A4_ROTATED, 297000, 210000, 0, 0, 297000, 210000,
  114. 0, IDS_FORM_A5_ROTATED, 210000, 148000, 0, 0, 210000, 148000,
  115. 0, IDS_FORM_B4_JIS_ROTATED, 364000, 257000, 0, 0, 364000, 257000,
  116. 0, IDS_FORM_B5_JIS_ROTATED, 257000, 182000, 0, 0, 257000, 182000,
  117. 0, IDS_FORM_JAPANESE_POSTCARD_ROTATED, 148000, 100000, 0, 0, 148000, 100000,
  118. 0, IDS_FORM_DBL_JAPANESE_POSTCARD_ROTATED, 148000, 200000, 0, 0, 148000, 200000,
  119. 0, IDS_FORM_A6_ROTATED, 148000, 105000, 0, 0, 148000, 105000,
  120. 0, IDS_FORM_JENV_KAKU2_ROTATED, 332000, 240000, 0, 0, 332000, 240000,
  121. 0, IDS_FORM_JENV_KAKU3_ROTATED, 277000, 216000, 0, 0, 277000, 216000,
  122. 0, IDS_FORM_JENV_CHOU3_ROTATED, 235000, 120000, 0, 0, 235000, 120000,
  123. 0, IDS_FORM_JENV_CHOU4_ROTATED, 205000, 90000, 0, 0, 205000, 90000,
  124. 0, IDS_FORM_B6_JIS, 128000, 182000, 0, 0, 128000, 182000,
  125. 0, IDS_FORM_B6_JIS_ROTATED, 182000, 128000, 0, 0, 182000, 128000,
  126. 0, IDS_FORM_12X11, 304932, 279521, 0, 0, 304932, 279521,
  127. 0, IDS_FORM_JENV_YOU4, 105000, 235000, 0, 0, 105000, 235000,
  128. 0, IDS_FORM_JENV_YOU4_ROTATED, 235000, 105000, 0, 0, 235000, 105000,
  129. 0, IDS_FORM_P16K, 188000, 260000, 0, 0, 188000, 260000,
  130. 0, IDS_FORM_P32K, 130000, 184000, 0, 0, 130000, 184000,
  131. 0, IDS_FORM_P32KBIG, 140000, 203000, 0, 0, 140000, 203000,
  132. 0, IDS_FORM_PENV_1, 102000, 165000, 0, 0, 102000, 165000,
  133. 0, IDS_FORM_PENV_2, 102000, 176000, 0, 0, 102000, 176000,
  134. 0, IDS_FORM_PENV_3, 125000, 176000, 0, 0, 125000, 176000,
  135. 0, IDS_FORM_PENV_4, 110000, 208000, 0, 0, 110000, 208000,
  136. 0, IDS_FORM_PENV_5, 110000, 220000, 0, 0, 110000, 220000,
  137. 0, IDS_FORM_PENV_6, 120000, 230000, 0, 0, 120000, 230000,
  138. 0, IDS_FORM_PENV_7, 160000, 230000, 0, 0, 160000, 230000,
  139. 0, IDS_FORM_PENV_8, 120000, 309000, 0, 0, 120000, 309000,
  140. 0, IDS_FORM_PENV_9, 229000, 324000, 0, 0, 229000, 324000,
  141. 0, IDS_FORM_PENV_10, 324000, 458000, 0, 0, 324000, 458000,
  142. 0, IDS_FORM_P16K_ROTATED, 260000, 188000, 0, 0, 260000, 188000,
  143. 0, IDS_FORM_P32K_ROTATED, 184000, 130000, 0, 0, 184000, 130000,
  144. 0, IDS_FORM_P32KBIG_ROTATED, 203000, 140000, 0, 0, 203000, 140000,
  145. 0, IDS_FORM_PENV_1_ROTATED, 165000, 102000, 0, 0, 165000, 102000,
  146. 0, IDS_FORM_PENV_2_ROTATED, 176000, 102000, 0, 0, 176000, 102000,
  147. 0, IDS_FORM_PENV_3_ROTATED, 176000, 125000, 0, 0, 176000, 125000,
  148. 0, IDS_FORM_PENV_4_ROTATED, 208000, 110000, 0, 0, 208000, 110000,
  149. 0, IDS_FORM_PENV_5_ROTATED, 220000, 110000, 0, 0, 220000, 110000,
  150. 0, IDS_FORM_PENV_6_ROTATED, 230000, 120000, 0, 0, 230000, 120000,
  151. 0, IDS_FORM_PENV_7_ROTATED, 230000, 160000, 0, 0, 230000, 160000,
  152. 0, IDS_FORM_PENV_8_ROTATED, 309000, 120000, 0, 0, 309000, 120000,
  153. 0, IDS_FORM_PENV_9_ROTATED, 324000, 229000, 0, 0, 324000, 229000,
  154. 0, IDS_FORM_PENV_10_ROTATED, 458000, 324000, 0, 0, 458000, 324000,
  155. 0, 0, 0, 0, 0, 0, 0, 0
  156. };
  157. PINIFORM
  158. CreateFormEntry(
  159. LPWSTR pFormName,
  160. SIZEL Size,
  161. RECTL *pImageableArea,
  162. DWORD Type,
  163. DWORD cFormOrder,
  164. PINISPOOLER pIniSpooler
  165. )
  166. /*++
  167. Routine Description:
  168. Creates a Form entry, and insert it into the right place in
  169. pIniSpooler.
  170. Arguments:
  171. pFormName - Name of Form.
  172. Size - Size of form.
  173. pImageableArea - Area of form that the printer can print to.
  174. Type - Type of form (usually indicates if BUILTIN form).
  175. cFormOrder - Where the form should be inserted for user-defined forms,
  176. form order increases from beginning to end.
  177. If this value is -1, generate a new cFormOrder for this form.
  178. (Put it at the end.)
  179. Return Value:
  180. pIniForm - Created form, NULL = error.
  181. Notes:
  182. This routine ensures that forms are put in proper order so that
  183. EnumForms always returns them in the same order. We do this by
  184. scanning the list and inserting the new form such that all forms
  185. with cFormOrder =< the current on are to the left of it.
  186. This routine updates pIniSpooler->pIniForm (inserts or appends) and
  187. updates pIniSpooler->cFormOrderMax if necessary.
  188. i.e., 0 0 0 2 3 4 6
  189. ^
  190. New 0 inserted here.
  191. --*/
  192. {
  193. DWORD cb;
  194. PINIFORM pIniForm, pForm;
  195. cb = sizeof(INIFORM) + wcslen(pFormName)*sizeof(WCHAR) + sizeof(WCHAR);
  196. if ( pIniForm = AllocSplMem(cb) ) {
  197. StringCbCopy((LPWSTR)(pIniForm+1), cb - sizeof(*pIniForm), pFormName);
  198. pIniForm->pName = (LPWSTR)(pIniForm+1);
  199. pIniForm->pNext = NULL;
  200. pIniForm->signature = IFO_SIGNATURE;
  201. pIniForm->Size = Size;
  202. pIniForm->ImageableArea = *pImageableArea;
  203. pIniForm->Type = Type;
  204. pIniForm->cFormOrder = cFormOrder;
  205. //
  206. // This code will insert the item in order, but will always never
  207. // insert before the first item. The built-in forms are always
  208. // at the front so this is not an issue.
  209. //
  210. if ( pForm = pIniSpooler->pShared->pIniForm ) {
  211. for( ; pForm->pNext; pForm = pForm->pNext ){
  212. //
  213. // If the next form is greater than the one we want
  214. // to insert, then insert it right now.
  215. //
  216. if( pForm->pNext->cFormOrder > cFormOrder ){
  217. //
  218. // The current from should be inserted here.
  219. //
  220. break;
  221. }
  222. }
  223. //
  224. // Link it up.
  225. //
  226. pIniForm->pNext = pForm->pNext;
  227. pForm->pNext = pIniForm;
  228. } else {
  229. pIniSpooler->pShared->pIniForm = pIniForm;
  230. }
  231. //
  232. // If the added form has a higher count than the current global
  233. // count, update the global.
  234. //
  235. if( cFormOrder > pIniSpooler->cFormOrderMax ){
  236. pIniSpooler->cFormOrderMax = cFormOrder;
  237. }
  238. }
  239. return pIniForm;
  240. }
  241. BOOL
  242. InitializeForms(
  243. PINISPOOLER pIniSpooler
  244. )
  245. {
  246. PBUILTIN_FORM pBuiltInForm;
  247. HKEY hFormsKey;
  248. DWORD cUserDefinedForms;
  249. WCHAR FormName[MAX_PATH];
  250. WCHAR FormBuffer[FORM_NAME_LEN+1];
  251. DWORD cchFormName;
  252. REG_FORM_INFO RegFormInfo;
  253. DWORD cbRegFormInfo;
  254. DWORD dwError;
  255. BOOL bUpgradeTried = FALSE;
  256. BOOL bStatus = TRUE;
  257. WORD PrimaryLangId = PRIMARYLANGID( GetSystemDefaultLangID( ));
  258. //
  259. // For win32spl pIniSpoolers, the built-in forms are part of the cache
  260. // database. They will be stored under registry, together with user forms.
  261. // This solves the cross language synchronization of the cache.
  262. //
  263. if (pIniSpooler->SpoolerFlags & SPL_TYPE_LOCAL)
  264. {
  265. for( pBuiltInForm = BuiltInForms; pBuiltInForm->NameId; pBuiltInForm++ ) {
  266. FormBuffer[0] = 0;
  267. LoadString( hInst,
  268. pBuiltInForm->NameId,
  269. FormBuffer,
  270. FORM_NAME_LEN+1 );
  271. if ( CreateFormEntry( FormBuffer,
  272. pBuiltInForm->Size,
  273. &pBuiltInForm->ImageableArea,
  274. FORM_BUILTIN,
  275. 0,
  276. pIniSpooler ) == NULL)
  277. {
  278. bStatus = FALSE;
  279. break;
  280. }
  281. }
  282. }
  283. if ( bStatus )
  284. {
  285. //
  286. // Now see if there are any user-defined forms in the registry:
  287. //
  288. if ( RegOpenKeyEx( HKEY_LOCAL_MACHINE,
  289. pIniSpooler->pszRegistryForms,
  290. 0,
  291. KEY_READ | KEY_WRITE,
  292. &hFormsKey) == NO_ERROR ) {
  293. for( cUserDefinedForms = 0; TRUE; ++cUserDefinedForms ){
  294. cchFormName = COUNTOF( FormName );
  295. cbRegFormInfo = sizeof( RegFormInfo );
  296. dwError = RegEnumValue( hFormsKey,
  297. cUserDefinedForms,
  298. (LPWSTR)FormName,
  299. &cchFormName,
  300. NULL,
  301. NULL,
  302. (LPBYTE)&RegFormInfo,
  303. &cbRegFormInfo );
  304. if( dwError ){
  305. break;
  306. }
  307. //
  308. // We will attempt the upgrade only if
  309. // we are on the first item,
  310. // the size is incorrect, and
  311. // we haven't tried upgrading once in the function before.
  312. //
  313. if ( CreateFormEntry( FormName,
  314. RegFormInfo.Size,
  315. &RegFormInfo.ImageableArea,
  316. RegFormInfo.Flags,
  317. RegFormInfo.cFormOrder,
  318. pIniSpooler ) == NULL )
  319. {
  320. bStatus = FALSE;
  321. break;
  322. }
  323. }
  324. RegCloseKey( hFormsKey );
  325. hFormsKey = NULL;
  326. }
  327. }
  328. return bStatus;
  329. }
  330. DWORD
  331. GetFormSize(
  332. PINIFORM pIniForm,
  333. DWORD Level
  334. )
  335. {
  336. DWORD cb;
  337. switch (Level) {
  338. case 1:
  339. cb=sizeof(FORM_INFO_1) +
  340. wcslen(pIniForm->pName)*sizeof(WCHAR) + sizeof(WCHAR);
  341. break;
  342. default:
  343. cb = 0;
  344. break;
  345. }
  346. return cb;
  347. }
  348. // We are being a bit naughty here as we are not sure exactly how much
  349. // memory to allocate for the source strings. We will just assume that
  350. // FORM_INFO_1 is the biggest structure around for the moment.
  351. LPBYTE
  352. CopyIniFormToForm(
  353. PINIFORM pIniForm,
  354. DWORD Level,
  355. LPBYTE pFormInfo,
  356. LPBYTE pEnd
  357. )
  358. {
  359. LPWSTR SourceStrings[sizeof(FORM_INFO_1)/sizeof(LPWSTR)];
  360. LPWSTR *pSourceStrings=SourceStrings;
  361. LPFORM_INFO_1 pFormInfo1=(LPFORM_INFO_1)pFormInfo;
  362. DWORD *pOffsets;
  363. switch (Level) {
  364. case 1:
  365. pOffsets = FormInfo1Strings;
  366. break;
  367. default:
  368. return pEnd;
  369. }
  370. switch (Level) {
  371. case 1:
  372. *pSourceStrings++=pIniForm->pName;
  373. pEnd = PackStrings(SourceStrings, pFormInfo, pOffsets, pEnd);
  374. pFormInfo1->Flags |= pIniForm->Type;
  375. pFormInfo1->Size = pIniForm->Size;
  376. pFormInfo1->ImageableArea = pIniForm->ImageableArea;
  377. break;
  378. default:
  379. return pEnd;
  380. }
  381. return pEnd;
  382. }
  383. /* Checks for logically impossible sizes.
  384. */
  385. BOOL
  386. ValidateForm(
  387. LPBYTE pForm
  388. )
  389. {
  390. LPFORM_INFO_1 pFormInfo = (LPFORM_INFO_1)pForm;
  391. DWORD Error = NO_ERROR;
  392. if( !pForm ||
  393. (pFormInfo->Flags != FORM_USER &&
  394. pFormInfo->Flags != FORM_BUILTIN &&
  395. pFormInfo->Flags != FORM_PRINTER)) {
  396. Error = ERROR_INVALID_PARAMETER;
  397. } else
  398. /* Make sure name isn't longer than GDI DEVMODE specifies:
  399. */
  400. if( ( !pFormInfo->pName ) ||
  401. ( !pFormInfo->pName[0] ) ||
  402. ( wcslen( pFormInfo->pName ) > FORM_NAME_LEN )){
  403. Error = ERROR_INVALID_FORM_NAME;
  404. } else
  405. if( ( pFormInfo->Size.cx <= 0 ) /* Check for negative width */
  406. ||( pFormInfo->Size.cy <= 0 ) /* ... and height */
  407. /* Check for strange imageable area:
  408. */
  409. || (pFormInfo->ImageableArea.right < 0)
  410. || (pFormInfo->ImageableArea.left < 0)
  411. || (pFormInfo->ImageableArea.bottom < 0)
  412. || (pFormInfo->ImageableArea.top < 0)
  413. ||( pFormInfo->ImageableArea.right <= pFormInfo->ImageableArea.left )
  414. ||( pFormInfo->ImageableArea.bottom <= pFormInfo->ImageableArea.top ) ) {
  415. Error = ERROR_INVALID_FORM_SIZE;
  416. }
  417. if( Error != NO_ERROR ) {
  418. SetLastError(Error);
  419. return FALSE;
  420. }
  421. return TRUE;
  422. }
  423. BOOL
  424. SplAddForm(
  425. HANDLE hPrinter,
  426. DWORD Level,
  427. LPBYTE pForm
  428. )
  429. {
  430. LPFORM_INFO_1 pFormInfo;
  431. PINIFORM pIniForm;
  432. HKEY hFormsKey;
  433. REG_FORM_INFO RegFormInfo;
  434. DWORD Status;
  435. PSPOOL pSpool = (PSPOOL)hPrinter;
  436. PINISPOOLER pIniSpooler;
  437. HANDLE hToken = INVALID_HANDLE_VALUE;
  438. if (!ValidateSpoolHandle( pSpool, 0 )) {
  439. return(FALSE);
  440. }
  441. pIniSpooler = pSpool->pIniSpooler;
  442. if (Level != 1) {
  443. return FALSE;
  444. }
  445. if (!ValidateForm(pForm)) {
  446. /* ValidateForm sets the appropriate error code:
  447. */
  448. return FALSE;
  449. }
  450. if ( !ValidateObjectAccess(SPOOLER_OBJECT_SERVER,
  451. SERVER_ACCESS_ADMINISTER,
  452. NULL, NULL, pIniSpooler )) {
  453. return FALSE;
  454. }
  455. EnterSplSem();
  456. pFormInfo = (LPFORM_INFO_1)pForm;
  457. pIniForm = FindForm(pFormInfo->pName, pIniSpooler);
  458. /* If there's already a form by this name, don't go on:
  459. */
  460. if (pIniForm) {
  461. /* Is there a better error code than this?? */
  462. SetLastError(ERROR_FILE_EXISTS);
  463. LeaveSplSem();
  464. return FALSE;
  465. }
  466. //
  467. // Revert to LocalSystem, since a regular user cannot do this CreateKey call.
  468. //
  469. hToken = RevertToPrinterSelf();
  470. Status = RegCreateKeyEx(HKEY_LOCAL_MACHINE, pIniSpooler->pszRegistryForms, 0, NULL, 0,
  471. KEY_WRITE, NULL, &hFormsKey, NULL);
  472. if (Status == NO_ERROR) {
  473. RegFormInfo.Size = pFormInfo->Size;
  474. RegFormInfo.ImageableArea = pFormInfo->ImageableArea;
  475. RegFormInfo.cFormOrder = pIniSpooler->cFormOrderMax + 1;
  476. RegFormInfo.Flags = pFormInfo->Flags;
  477. Status = RegSetValueEx( hFormsKey, pFormInfo->pName, 0, REG_BINARY,
  478. (LPBYTE)&RegFormInfo, sizeof RegFormInfo );
  479. RegCloseKey( hFormsKey );
  480. if ( Status == NO_ERROR ) {
  481. CreateFormEntry( pFormInfo->pName,
  482. pFormInfo->Size,
  483. &pFormInfo->ImageableArea,
  484. RegFormInfo.Flags,
  485. RegFormInfo.cFormOrder,
  486. pIniSpooler );
  487. SetPrinterChange( NULL,
  488. NULL,
  489. NULL,
  490. PRINTER_CHANGE_ADD_FORM,
  491. pIniSpooler );
  492. BroadcastChangeForms( pIniSpooler );
  493. }
  494. }
  495. ImpersonatePrinterClient( hToken );
  496. RunForEachSpooler( NULL, DsUpdateAllDriverKeys);
  497. LeaveSplSem();
  498. if ( Status != NO_ERROR )
  499. SetLastError( Status );
  500. SplLogEvent( pIniSpooler,
  501. LOG_INFO,
  502. MSG_FORM_ADDED,
  503. FALSE,
  504. pFormInfo->pName,
  505. NULL );
  506. return ( Status == NO_ERROR );
  507. }
  508. BOOL
  509. DeleteFormEntry(
  510. PINIFORM pIniForm,
  511. PINISPOOLER pIniSpooler
  512. )
  513. {
  514. PINIFORM *ppCurForm;
  515. ppCurForm = &pIniSpooler->pShared->pIniForm;
  516. while (*ppCurForm != pIniForm)
  517. ppCurForm = &(*ppCurForm)->pNext;
  518. *ppCurForm = (*ppCurForm)->pNext;
  519. FreeSplMem(pIniForm);
  520. return TRUE;
  521. }
  522. BOOL
  523. SplDeleteForm(
  524. HANDLE hPrinter,
  525. LPWSTR pFormName
  526. )
  527. {
  528. HKEY hFormsKey;
  529. DWORD Status;
  530. PINIFORM pIniForm;
  531. PSPOOL pSpool = (PSPOOL) hPrinter;
  532. HANDLE hToken = INVALID_HANDLE_VALUE;
  533. PINISPOOLER pIniSpooler;
  534. if (!ValidateSpoolHandle( pSpool, 0 )) {
  535. return(FALSE);
  536. }
  537. pIniSpooler = pSpool->pIniSpooler;
  538. if (!pFormName) {
  539. SetLastError(ERROR_INVALID_PARAMETER);
  540. return FALSE;
  541. }
  542. if ( !ValidateObjectAccess(SPOOLER_OBJECT_SERVER,
  543. SERVER_ACCESS_ADMINISTER,
  544. NULL, NULL, pIniSpooler )) {
  545. return FALSE;
  546. }
  547. EnterSplSem();
  548. pIniForm = FindForm(pFormName, pIniSpooler);
  549. //
  550. // Built-in forms cannot be deleted for local iniSpooler(s).
  551. //
  552. if (!pIniForm || ((pIniForm->Type == FORM_BUILTIN) &&
  553. pIniSpooler->SpoolerFlags & SPL_TYPE_LOCAL)) {
  554. SetLastError( pIniForm ?
  555. ERROR_INVALID_PARAMETER :
  556. ERROR_INVALID_FORM_NAME );
  557. LeaveSplSem();
  558. return FALSE;
  559. }
  560. hToken = RevertToPrinterSelf();
  561. Status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, pIniSpooler->pszRegistryForms, 0,
  562. KEY_WRITE, &hFormsKey);
  563. if (Status == NO_ERROR) {
  564. Status = RegDeleteValue(hFormsKey, pFormName);
  565. RegCloseKey(hFormsKey);
  566. if (Status == NO_ERROR) {
  567. DeleteFormEntry(pIniForm , pIniSpooler );
  568. SetPrinterChange(NULL,
  569. NULL,
  570. NULL,
  571. PRINTER_CHANGE_DELETE_FORM,
  572. pIniSpooler);
  573. BroadcastChangeForms(pIniSpooler);
  574. RunForEachSpooler( NULL, DsUpdateAllDriverKeys);
  575. }
  576. }
  577. if(hToken != INVALID_HANDLE_VALUE)
  578. {
  579. ImpersonatePrinterClient( hToken );
  580. }
  581. LeaveSplSem();
  582. if (Status != NO_ERROR)
  583. SetLastError(Status);
  584. SplLogEvent( pIniSpooler,
  585. LOG_INFO,
  586. MSG_FORM_DELETED,
  587. FALSE,
  588. pFormName,
  589. NULL );
  590. return (Status == NO_ERROR);
  591. }
  592. BOOL
  593. SplGetForm(
  594. HANDLE hPrinter,
  595. LPWSTR pFormName,
  596. DWORD Level,
  597. LPBYTE pForm,
  598. DWORD cbBuf,
  599. LPDWORD pcbNeeded
  600. )
  601. {
  602. PINIFORM pIniForm;
  603. DWORD cb;
  604. LPBYTE pEnd;
  605. PSPOOL pSpool = (PSPOOL)hPrinter;
  606. PINISPOOLER pIniSpooler;
  607. if (!ValidateSpoolHandle(pSpool, 0 )) {
  608. return(FALSE);
  609. }
  610. if (!pSpool->pIniPrinter ||
  611. !pSpool->pIniSpooler ||
  612. (pSpool->pIniPrinter->signature != IP_SIGNATURE)) {
  613. SetLastError(ERROR_INVALID_HANDLE);
  614. return FALSE;
  615. }
  616. EnterSplSem();
  617. SPLASSERT(pSpool->pIniSpooler->signature == ISP_SIGNATURE);
  618. pIniSpooler = pSpool->pIniSpooler;
  619. cb=0;
  620. if (pIniForm=FindForm(pFormName, pIniSpooler)) {
  621. cb=GetFormSize(pIniForm, Level);
  622. *pcbNeeded=cb;
  623. if (cb > cbBuf) {
  624. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  625. LeaveSplSem();
  626. SplOutSem();
  627. return FALSE;
  628. }
  629. pEnd=pForm+cbBuf;
  630. CopyIniFormToForm(pIniForm, Level, pForm, pEnd);
  631. } else {
  632. SetLastError( ERROR_INVALID_FORM_NAME );
  633. }
  634. LeaveSplSem();
  635. SplOutSem();
  636. return !!pIniForm;
  637. }
  638. BOOL
  639. SplSetForm(
  640. HANDLE hPrinter,
  641. LPWSTR pFormName,
  642. DWORD Level,
  643. LPBYTE pForm
  644. )
  645. {
  646. HKEY hFormsKey;
  647. DWORD Status;
  648. PINIFORM pIniForm;
  649. LPFORM_INFO_1 pFormInfo;
  650. REG_FORM_INFO RegFormInfo;
  651. PINISPOOLER pIniSpooler;
  652. PSPOOL pSpool = (PSPOOL)hPrinter;
  653. //
  654. // Validate this Printer Handle
  655. // Disallow Mask: PRINTER_HANDLE_SERVER
  656. //
  657. if (!ValidateSpoolHandle( pSpool , 0 )) {
  658. return(FALSE);
  659. }
  660. pIniSpooler = pSpool->pIniSpooler;
  661. if (Level != 1) {
  662. SetLastError(ERROR_INVALID_LEVEL);
  663. return FALSE;
  664. }
  665. if (!ValidateForm(pForm)) {
  666. /* ValidateForm sets the appropriate error code:
  667. */
  668. return FALSE;
  669. }
  670. if ( !ValidateObjectAccess(SPOOLER_OBJECT_SERVER,
  671. SERVER_ACCESS_ADMINISTER,
  672. NULL, NULL, pIniSpooler )) {
  673. return FALSE;
  674. }
  675. EnterSplSem();
  676. SPLASSERT( pIniSpooler->signature == ISP_SIGNATURE );
  677. pFormInfo = (LPFORM_INFO_1)pForm;
  678. pIniForm = FindForm(pFormName, pIniSpooler);
  679. if (!pIniForm || (pIniForm->Type == FORM_BUILTIN) || (pFormInfo->Flags & FORM_BUILTIN)) {
  680. SetLastError( pIniForm ?
  681. ERROR_INVALID_PARAMETER :
  682. ERROR_INVALID_FORM_NAME );
  683. LeaveSplSem();
  684. return FALSE;
  685. }
  686. Status = RegOpenKeyEx(HKEY_LOCAL_MACHINE, pIniSpooler->pszRegistryForms, 0,
  687. KEY_WRITE, &hFormsKey);
  688. if (Status == NO_ERROR) {
  689. RegFormInfo.Size = pFormInfo->Size;
  690. RegFormInfo.ImageableArea = pFormInfo->ImageableArea;
  691. RegFormInfo.Flags = pFormInfo->Flags;
  692. Status = RegSetValueEx(hFormsKey, pFormInfo->pName, 0, REG_BINARY,
  693. (LPBYTE)&RegFormInfo, sizeof RegFormInfo);
  694. RegCloseKey(hFormsKey);
  695. }
  696. if (Status == NO_ERROR) {
  697. pIniForm->Size = pFormInfo->Size;
  698. pIniForm->ImageableArea = pFormInfo->ImageableArea;
  699. pIniForm->Type = pFormInfo->Flags;
  700. SetPrinterChange(NULL,
  701. NULL,
  702. NULL,
  703. PRINTER_CHANGE_SET_FORM,
  704. pIniSpooler);
  705. BroadcastChangeForms(pIniSpooler);
  706. }
  707. LeaveSplSem();
  708. return (Status == NO_ERROR);
  709. }
  710. BOOL
  711. SplEnumForms(
  712. HANDLE hPrinter,
  713. DWORD Level,
  714. LPBYTE pForm,
  715. DWORD cbBuf,
  716. LPDWORD pcbNeeded,
  717. LPDWORD pcReturned
  718. )
  719. {
  720. PINIFORM pIniForm;
  721. DWORD cb;
  722. LPBYTE pEnd;
  723. PSPOOL pSpool = (PSPOOL)hPrinter;
  724. PINISPOOLER pIniSpooler;
  725. if (!ValidateSpoolHandle(pSpool, 0 )) {
  726. return FALSE;
  727. }
  728. *pcReturned=0;
  729. EnterSplSem();
  730. pIniSpooler = pSpool->pIniSpooler;
  731. SPLASSERT( ( pIniSpooler != NULL ) &&
  732. ( pIniSpooler->signature == ISP_SIGNATURE ));
  733. cb=0;
  734. pIniForm=pIniSpooler->pShared->pIniForm;
  735. while (pIniForm) {
  736. cb+=GetFormSize(pIniForm, Level);
  737. pIniForm=pIniForm->pNext;
  738. }
  739. *pcbNeeded=cb;
  740. if (cb > cbBuf) {
  741. SetLastError(ERROR_INSUFFICIENT_BUFFER);
  742. LeaveSplSem();
  743. SplOutSem();
  744. return FALSE;
  745. }
  746. pIniForm=pIniSpooler->pShared->pIniForm;
  747. pEnd=pForm+cbBuf;
  748. while (pIniForm) {
  749. pEnd = CopyIniFormToForm(pIniForm, Level, pForm, pEnd);
  750. switch (Level) {
  751. case 1:
  752. pForm+=sizeof(FORM_INFO_1);
  753. break;
  754. }
  755. pIniForm=pIniForm->pNext;
  756. (*pcReturned)++;
  757. }
  758. LeaveSplSem();
  759. SplOutSem();
  760. return TRUE;
  761. }
  762. DWORD dwBroadcastChangeFormsThread = 0;
  763. VOID
  764. BroadcastChangeForms(
  765. PINISPOOLER pIniSpooler)
  766. /*++
  767. Routine Description:
  768. Notify all applications that their devmode may have changed (when
  769. a form is changed).
  770. Arguments:
  771. Return Value:
  772. --*/
  773. {
  774. PINIPRINTER pIniPrinter;
  775. WCHAR PrinterName[ MAX_UNC_PRINTER_NAME ];
  776. UINT MachineNameLen;
  777. SplInSem();
  778. if( !( pIniSpooler->SpoolerFlags & SPL_FORMS_CHANGE )) {
  779. return;
  780. }
  781. INCSPOOLERREF( pIniSpooler );
  782. ++dwBroadcastChangeFormsThread;
  783. if ( pIniSpooler != pLocalIniSpooler ) {
  784. //
  785. // For Non Local Printers prepend the Machine Name
  786. //
  787. StringCchPrintf(PrinterName, COUNTOF(PrinterName), L"%ws\\", pIniSpooler->pMachineName );
  788. } else {
  789. PrinterName[0] = L'\0';
  790. }
  791. MachineNameLen = wcslen( PrinterName ) ;
  792. for( pIniPrinter = pIniSpooler->pIniPrinter;
  793. pIniPrinter;
  794. pIniPrinter = pIniPrinter->pNext ) {
  795. StringCchCopy(&PrinterName[MachineNameLen], COUNTOF(PrinterName) - MachineNameLen, pIniPrinter->pName);
  796. //
  797. // Stress testing has shown that SendNotifyMessage can take
  798. // a long time to return, so leave critical section.
  799. //
  800. INCPRINTERREF( pIniPrinter );
  801. LeaveSplSem();
  802. SplOutSem();
  803. SendNotifyMessage(HWND_BROADCAST,
  804. WM_DEVMODECHANGE,
  805. 0,
  806. (LPARAM)PrinterName);
  807. EnterSplSem();
  808. DECPRINTERREF( pIniPrinter );
  809. }
  810. DECSPOOLERREF( pIniSpooler );
  811. }