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.

435 lines
12 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. helper.c
  5. Abstract:
  6. Helper functions
  7. Environment:
  8. Windows NT printer drivers
  9. Revision History:
  10. --*/
  11. #include "lib.h"
  12. VOID
  13. VFreeParserInfo(
  14. IN PPARSERINFO pParserInfo
  15. )
  16. /*++
  17. Routine Description:
  18. This function frees and unload binary data
  19. Arguments:
  20. pParserInfo - Pointer to parser information
  21. Return Value:
  22. None
  23. --*/
  24. {
  25. if (pParserInfo->pInfoHeader)
  26. {
  27. FreeBinaryData(pParserInfo->pInfoHeader);
  28. pParserInfo->pInfoHeader = NULL;
  29. }
  30. if ( pParserInfo->pRawData)
  31. {
  32. UnloadRawBinaryData(pParserInfo->pRawData);
  33. pParserInfo->pRawData = NULL;
  34. }
  35. }
  36. PUIINFO
  37. PGetUIInfo(
  38. IN HANDLE hPrinter,
  39. IN PRAWBINARYDATA pRawData,
  40. IN POPTSELECT pCombineOptions,
  41. IN POPTSELECT pOptions,
  42. OUT PPARSERINFO pParserInfo,
  43. OUT PDWORD pdwFeatureCount
  44. )
  45. /*++
  46. Routine Description:
  47. This function loads up the binary data and returns the pUIInfo
  48. Arguments:
  49. hPrinter - Identifies the printer in question
  50. pRawData - Points to raw binary printer description data
  51. pCombineOptions - Points to buffer to contain the combined options array
  52. pParserInfo - Points to struct that contains pInfoHeader and pRawData
  53. pdwFeatureCount - Retrieve the count of the features
  54. Return Value:
  55. Point to UIINFO struct
  56. --*/
  57. {
  58. OPTSELECT DocOptions[MAX_PRINTER_OPTIONS], PrinterOptions[MAX_PRINTER_OPTIONS];
  59. PINFOHEADER pInfoHeader = NULL;
  60. PUIINFO pUIInfo = NULL;
  61. ASSERT(pRawData != NULL);
  62. if (pOptions == NULL)
  63. {
  64. if (! InitDefaultOptions(pRawData,
  65. PrinterOptions,
  66. MAX_PRINTER_OPTIONS,
  67. MODE_PRINTER_STICKY))
  68. goto getuiinfo_exit;
  69. }
  70. if (! InitDefaultOptions(pRawData,
  71. DocOptions,
  72. MAX_PRINTER_OPTIONS,
  73. MODE_DOCUMENT_STICKY))
  74. goto getuiinfo_exit;
  75. //
  76. // Combine doc sticky options with printer sticky items
  77. //
  78. CombineOptionArray(pRawData,
  79. pCombineOptions,
  80. MAX_COMBINED_OPTIONS,
  81. DocOptions,
  82. pOptions ? pOptions : PrinterOptions);
  83. //
  84. // Get an updated instance of printer description data
  85. //
  86. pInfoHeader = InitBinaryData(pRawData,
  87. NULL,
  88. pCombineOptions);
  89. if (pInfoHeader == NULL)
  90. {
  91. ERR(("InitBinaryData failed\n"));
  92. goto getuiinfo_exit;
  93. }
  94. pUIInfo = OFFSET_TO_POINTER(pInfoHeader, pInfoHeader->loUIInfoOffset);
  95. if (pdwFeatureCount)
  96. *pdwFeatureCount = pRawData->dwDocumentFeatures + pRawData->dwPrinterFeatures;
  97. getuiinfo_exit:
  98. //
  99. // PGetUIInfo always use the passed in pRawData. We assign NULL to
  100. // pParserInfo->pRawData, so VFreeParserInfo won't unload it.
  101. //
  102. pParserInfo->pRawData = NULL;
  103. pParserInfo->pInfoHeader = pInfoHeader;
  104. if (pUIInfo == NULL)
  105. VFreeParserInfo(pParserInfo);
  106. return pUIInfo;
  107. }
  108. PSTR
  109. PstrConvertIndexToKeyword(
  110. IN HANDLE hPrinter,
  111. IN POPTSELECT pOptions,
  112. IN PDWORD pdwKeywordSize,
  113. IN PUIINFO pUIInfo,
  114. IN POPTSELECT pCombineOptions,
  115. IN DWORD dwFeatureCount
  116. )
  117. /*++
  118. Routine Description:
  119. This function convert the indexed based pOptions array to
  120. Feature.Option keywordname.
  121. Arguments:
  122. hPrinter - Identifies the printer in question
  123. pOptions - Index based optionsarray (pPrinterData->aOptions)
  124. pdwKeywordSize - Retrieve the size of the buffer to write to registry
  125. pUIInfo - Pointer to UIINFO
  126. pCombinedOptions - Pointer to the combined options
  127. dwFeatureCount - Number of features in pCombinedOptions
  128. Return Value:
  129. Pointer to buffer containing Feature.Option keyword names
  130. --*/
  131. {
  132. PFEATURE pFeature;
  133. POPTION pOption;
  134. PSTR pstrKeywordBuf, pstrEnd, pstrBufTop = NULL;
  135. DWORD i;
  136. PSTR pFeatureKeyword, pOptionKeyword;
  137. BYTE ubNext, ubCurOptIndex;
  138. if ((pCombineOptions && pUIInfo && dwFeatureCount) &&
  139. (pUIInfo->dwMaxPrnKeywordSize) &&
  140. (pFeature = PGetIndexedFeature(pUIInfo, 0)) &&
  141. (pstrBufTop = pstrKeywordBuf = MemAllocZ( pUIInfo->dwMaxPrnKeywordSize )))
  142. {
  143. pstrEnd = pstrBufTop + pUIInfo->dwMaxPrnKeywordSize;
  144. for (i = 0; i < dwFeatureCount; i++ , pFeature++)
  145. {
  146. if (pFeature && pFeature->dwFeatureType == FEATURETYPE_PRINTERPROPERTY)
  147. {
  148. pFeatureKeyword = OFFSET_TO_POINTER(pUIInfo->pubResourceData,
  149. pFeature->loKeywordName);
  150. ASSERT(pFeatureKeyword != NULL);
  151. if (pFeatureKeyword != NULL)
  152. {
  153. StringCchCopyA(pstrKeywordBuf, pstrEnd - pstrKeywordBuf, pFeatureKeyword);
  154. pstrKeywordBuf += strlen(pFeatureKeyword) + 1;
  155. }
  156. if ((pFeatureKeyword == NULL) || (pstrKeywordBuf >= pstrEnd))
  157. {
  158. ERR(("ConvertToKeyword, Feature failed"));
  159. MemFree(pstrBufTop);
  160. pstrBufTop = NULL;
  161. goto converttokeyword_exit;
  162. }
  163. //
  164. // Handle multiple selections
  165. //
  166. ubNext = (BYTE)i;
  167. while (1)
  168. {
  169. if (ubNext == NULL_OPTSELECT )
  170. break;
  171. ubCurOptIndex = pCombineOptions[ubNext].ubCurOptIndex;
  172. pOption = PGetIndexedOption(pUIInfo, pFeature,
  173. ubCurOptIndex == OPTION_INDEX_ANY ?
  174. 0 : ubCurOptIndex);
  175. ubNext = pCombineOptions[ubNext].ubNext;
  176. ASSERT(pOption != NULL);
  177. if (pOption == NULL)
  178. break;
  179. pOptionKeyword = OFFSET_TO_POINTER(pUIInfo->pubResourceData,
  180. pOption->loKeywordName);
  181. ASSERT(pOptionKeyword != NULL);
  182. if (pOptionKeyword != NULL)
  183. {
  184. StringCchCopyA(pstrKeywordBuf, pstrEnd - pstrKeywordBuf, pOptionKeyword);
  185. pstrKeywordBuf += strlen(pOptionKeyword) + 1;
  186. }
  187. if ((pOptionKeyword == NULL) || (pstrKeywordBuf >= pstrEnd))
  188. {
  189. ERR(("ConvertToKeyword, Option failed"));
  190. MemFree(pstrBufTop);
  191. pstrBufTop = NULL;
  192. goto converttokeyword_exit;
  193. }
  194. }
  195. //
  196. // terminate the Feature.Option... with valid delimiter
  197. //
  198. *pstrKeywordBuf++ = END_OF_FEATURE;
  199. if (pstrKeywordBuf >= pstrEnd)
  200. {
  201. ERR(("ConvertToKeyword, Over writing buffer"));
  202. MemFree(pstrBufTop);
  203. pstrBufTop = NULL;
  204. goto converttokeyword_exit;
  205. }
  206. }
  207. }
  208. //
  209. // Add 2 NULs termination for MULTI_SZ buffer
  210. //
  211. if ((pstrEnd - pstrKeywordBuf) < 2)
  212. {
  213. ERR(("ConvertToKeyword, Over writing buffer"));
  214. MemFree(pstrBufTop);
  215. pstrBufTop = NULL;
  216. goto converttokeyword_exit;
  217. }
  218. *pstrKeywordBuf++ = NUL;
  219. *pstrKeywordBuf++ = NUL;
  220. if (pdwKeywordSize)
  221. *pdwKeywordSize = (DWORD)(pstrKeywordBuf - pstrBufTop);
  222. }
  223. converttokeyword_exit:
  224. return pstrBufTop;
  225. }
  226. VOID
  227. VConvertKeywordToIndex(
  228. IN HANDLE hPrinter,
  229. IN PSTR pstrKeyword,
  230. IN DWORD dwKeywordSize,
  231. OUT POPTSELECT pOptions,
  232. IN PRAWBINARYDATA pRawData,
  233. IN PUIINFO pUIInfo,
  234. IN POPTSELECT pCombineOptions,
  235. IN DWORD dwFeatureCount
  236. )
  237. /*++
  238. Routine Description:
  239. Arguments:
  240. hPrinter - Identifies the printer in question
  241. ptstrKeyword - Buffer containing Feature.Option keyword names
  242. pOptions - Index based options array contain the conversion
  243. pUIInfo - Pointer to UIINFO
  244. pCombinedOptions - Pointer to the combined options
  245. dwFeatureCount - Number of features in pCombinedOptions
  246. Return Value:
  247. None, if for some reasons we could not convert, we get the default.
  248. --*/
  249. {
  250. PSTR pstrEnd = pstrKeyword + dwKeywordSize;
  251. if (pCombineOptions && pUIInfo && dwFeatureCount)
  252. {
  253. CHAR achName[256];
  254. BOOL abEnableOptions[MAX_PRINTER_OPTIONS];
  255. PFEATURE pFeature;
  256. POPTION pOption;
  257. DWORD dwFeatureIndex, dwOptionIndex = OPTION_INDEX_ANY;
  258. while (pstrKeyword < pstrEnd && *pstrKeyword != NUL)
  259. {
  260. ZeroMemory(abEnableOptions, sizeof(abEnableOptions));
  261. //
  262. // Get the feature keyword name
  263. //
  264. StringCchCopyA(achName, CCHOF(achName), pstrKeyword);
  265. pstrKeyword += strlen(achName) + 1;
  266. if (pstrKeyword >= pstrEnd)
  267. {
  268. ERR(("Feature: Over writing the allocated buffer \n"));
  269. goto converttoindex_exit;
  270. }
  271. pFeature = PGetNamedFeature(pUIInfo,
  272. achName,
  273. &dwFeatureIndex);
  274. if (pFeature == NULL)
  275. {
  276. //
  277. // If we can't map the registry Feature name to a valid feature,
  278. // we need to skip all the feature's option names in the registry.
  279. //
  280. while (*pstrKeyword != END_OF_FEATURE && pstrKeyword < pstrEnd)
  281. pstrKeyword++;
  282. pstrKeyword++;
  283. continue;
  284. }
  285. //
  286. // Handle multiple selection
  287. //
  288. while (pstrKeyword < pstrEnd && *pstrKeyword != END_OF_FEATURE)
  289. {
  290. StringCchCopyA(achName, CCHOF(achName), pstrKeyword);
  291. pstrKeyword += strlen(achName) + 1;
  292. if (pstrKeyword >= pstrEnd)
  293. {
  294. ERR(("Option: Over writing the allocated buffer \n"));
  295. goto converttoindex_exit;
  296. }
  297. pOption = PGetNamedOption(pUIInfo,
  298. pFeature,
  299. achName,
  300. &dwOptionIndex);
  301. if (pOption)
  302. abEnableOptions[dwOptionIndex] = TRUE;
  303. }
  304. if (dwOptionIndex != OPTION_INDEX_ANY)
  305. ReconstructOptionArray(pRawData,
  306. pCombineOptions,
  307. MAX_COMBINED_OPTIONS,
  308. dwFeatureIndex,
  309. abEnableOptions);
  310. //
  311. // skip our delimiter to go to next feature
  312. //
  313. pstrKeyword++;
  314. }
  315. SeparateOptionArray(pRawData,
  316. pCombineOptions,
  317. pOptions,
  318. MAX_PRINTER_OPTIONS,
  319. MODE_PRINTER_STICKY);
  320. }
  321. converttoindex_exit:
  322. return;
  323. }