Source code of Windows XP (NT5)
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.

351 lines
7.5 KiB

  1. /*++
  2. Copyright (c) 1993 Microsoft Corporation
  3. Module Name:
  4. splutil.c
  5. Abstract:
  6. This module provides all the utility functions for the netware print
  7. provider.
  8. Author:
  9. Yi-Hsin Sung (yihsins) 15-Apr-1993
  10. Revision History:
  11. --*/
  12. #include <nt.h>
  13. #include <ntrtl.h>
  14. #include <nturtl.h>
  15. #include <windows.h>
  16. #include <winspool.h>
  17. #include <splutil.h>
  18. DWORD_PTR PrinterInfo1Offsets[]={offsetof(LPPRINTER_INFO_1W, pDescription),
  19. offsetof(LPPRINTER_INFO_1W, pName),
  20. offsetof(LPPRINTER_INFO_1W, pComment),
  21. 0xFFFFFFFF};
  22. DWORD_PTR PrinterInfo2Offsets[]={offsetof(LPPRINTER_INFO_2W, pServerName),
  23. offsetof(LPPRINTER_INFO_2W, pPrinterName),
  24. offsetof(LPPRINTER_INFO_2W, pShareName),
  25. offsetof(LPPRINTER_INFO_2W, pPortName),
  26. offsetof(LPPRINTER_INFO_2W, pDriverName),
  27. offsetof(LPPRINTER_INFO_2W, pComment),
  28. offsetof(LPPRINTER_INFO_2W, pLocation),
  29. offsetof(LPPRINTER_INFO_2W, pDevMode),
  30. offsetof(LPPRINTER_INFO_2W, pSepFile),
  31. offsetof(LPPRINTER_INFO_2W, pPrintProcessor),
  32. offsetof(LPPRINTER_INFO_2W, pDatatype),
  33. offsetof(LPPRINTER_INFO_2W, pParameters),
  34. offsetof(LPPRINTER_INFO_2W, pSecurityDescriptor),
  35. 0xFFFFFFFF};
  36. DWORD_PTR PrinterInfo3Offsets[]={offsetof(LPPRINTER_INFO_3, pSecurityDescriptor),
  37. 0xFFFFFFFF};
  38. DWORD_PTR JobInfo1Offsets[]={offsetof(LPJOB_INFO_1W, pPrinterName),
  39. offsetof(LPJOB_INFO_1W, pMachineName),
  40. offsetof(LPJOB_INFO_1W, pUserName),
  41. offsetof(LPJOB_INFO_1W, pDocument),
  42. offsetof(LPJOB_INFO_1W, pDatatype),
  43. offsetof(LPJOB_INFO_1W, pStatus),
  44. 0xFFFFFFFF};
  45. DWORD_PTR JobInfo2Offsets[]={offsetof(LPJOB_INFO_2W, pPrinterName),
  46. offsetof(LPJOB_INFO_2W, pMachineName),
  47. offsetof(LPJOB_INFO_2W, pUserName),
  48. offsetof(LPJOB_INFO_2W, pDocument),
  49. offsetof(LPJOB_INFO_2W, pNotifyName),
  50. offsetof(LPJOB_INFO_2W, pDatatype),
  51. offsetof(LPJOB_INFO_2W, pPrintProcessor),
  52. offsetof(LPJOB_INFO_2W, pParameters),
  53. offsetof(LPJOB_INFO_2W, pDriverName),
  54. offsetof(LPJOB_INFO_2W, pDevMode),
  55. offsetof(LPJOB_INFO_2W, pStatus),
  56. offsetof(LPJOB_INFO_2W, pSecurityDescriptor),
  57. 0xFFFFFFFF};
  58. DWORD_PTR AddJobInfo1Offsets[]={offsetof(LPADDJOB_INFO_1W, Path),
  59. 0xFFFFFFFF};
  60. VOID
  61. MarshallUpStructure(
  62. LPBYTE lpStructure,
  63. PDWORD_PTR lpOffsets,
  64. LPBYTE lpBufferStart
  65. )
  66. {
  67. register DWORD i=0;
  68. while (lpOffsets[i] != -1) {
  69. if ((*(LPBYTE *)(lpStructure+lpOffsets[i]))) {
  70. (*(LPBYTE *)(lpStructure+lpOffsets[i]))+=(DWORD_PTR)lpBufferStart;
  71. }
  72. i++;
  73. }
  74. }
  75. VOID
  76. MarshallDownStructure(
  77. LPBYTE lpStructure,
  78. PDWORD_PTR lpOffsets,
  79. LPBYTE lpBufferStart
  80. )
  81. {
  82. register DWORD i=0;
  83. if (!lpStructure)
  84. return;
  85. while (lpOffsets[i] != -1) {
  86. if ((*(LPBYTE*)(lpStructure+lpOffsets[i]))) {
  87. (*(LPBYTE*)(lpStructure+lpOffsets[i]))-=(DWORD_PTR)lpBufferStart;
  88. }
  89. i++;
  90. }
  91. }
  92. LPVOID
  93. AllocNwSplMem(
  94. DWORD flags,
  95. DWORD cb
  96. )
  97. /*++
  98. Routine Description:
  99. This function will allocate local memory. It will possibly allocate extra
  100. memory and fill this with debugging information for the debugging version.
  101. Arguments:
  102. flags - Flags to be passed to LocalAlloc
  103. cb - The amount of memory to allocate in bytes
  104. Return Value:
  105. NON-NULL - A pointer to the allocated memory
  106. --*/
  107. {
  108. LPDWORD pMem;
  109. DWORD cbNew;
  110. #if DBG
  111. cbNew = cb + 2*sizeof(DWORD);
  112. if (cbNew & 3)
  113. cbNew += sizeof(DWORD) - (cbNew & 3);
  114. #else
  115. cbNew = cb;
  116. #endif
  117. pMem = (LPDWORD) LocalAlloc( flags, cbNew );
  118. if ( !pMem )
  119. {
  120. KdPrint(("Memory Allocation in AllocNwSplMem failed for %d bytes\n", cbNew));
  121. return NULL;
  122. }
  123. #if DBG
  124. *pMem = cb;
  125. *(LPDWORD)((LPBYTE)pMem+cbNew-sizeof(DWORD)) = 0xdeadbeef;
  126. return (LPVOID) (pMem + 1);
  127. #else
  128. return (LPVOID) pMem;
  129. #endif
  130. }
  131. VOID
  132. FreeNwSplMem(
  133. LPVOID pMem,
  134. DWORD cb
  135. )
  136. /*++
  137. Routine Description:
  138. This function will frees the local memory allocated by AllocSplMem.
  139. Extra checking will be performed in the debug version to ensure that
  140. the size to be freed is indeed the size we allocated through AllocSplMem.
  141. Arguments:
  142. pMem - A pointer to the allocated memory
  143. cb - The amount of memory to free
  144. Return Value:
  145. --*/
  146. {
  147. DWORD cbNew;
  148. LPDWORD pNewMem;
  149. if ( !pMem )
  150. return;
  151. pNewMem = pMem;
  152. #if DBG
  153. pNewMem--;
  154. cbNew = cb + 2*sizeof(DWORD);
  155. if ( cbNew & 3 )
  156. cbNew += sizeof(DWORD) - (cbNew & 3);
  157. if ( ( *pNewMem != cb )
  158. || (*(LPDWORD)((LPBYTE)pNewMem + cbNew - sizeof(DWORD)) != 0xdeadbeef)
  159. )
  160. {
  161. KdPrint(("Corrupt Memory in FreeNwSplMem : %0lx\n", pNewMem ));
  162. return;
  163. }
  164. #else
  165. cbNew = cb;
  166. #endif
  167. LocalFree( (LPVOID) pNewMem );
  168. }
  169. LPWSTR
  170. AllocNwSplStr(
  171. LPWSTR pStr
  172. )
  173. /*++
  174. Routine Description:
  175. This function will allocate enough local memory to store the specified
  176. string, and copy that string to the allocated memory
  177. Arguments:
  178. pStr - Pointer to the string that needs to be allocated and stored
  179. Return Value:
  180. NON-NULL - A pointer to the allocated memory containing the string
  181. --*/
  182. {
  183. LPWSTR pMem;
  184. if ( !pStr )
  185. return NULL;
  186. if ( pMem = AllocNwSplMem(0, (wcslen(pStr) + 1) * sizeof(WCHAR)))
  187. wcscpy(pMem, pStr);
  188. return pMem;
  189. }
  190. VOID
  191. FreeNwSplStr(
  192. LPWSTR pStr
  193. )
  194. /*++
  195. Routine Description:
  196. This function will frees the string allocated by AllocSplStr.
  197. Extra checking will be performed in the debug version to ensure that
  198. the size to be freed is indeed the size we allocated through AllocSplStr.
  199. Arguments:
  200. pStr - A pointer to the allocated string
  201. Return Value:
  202. --*/
  203. {
  204. if ( pStr )
  205. FreeNwSplMem(pStr, (wcslen(pStr) + 1) * sizeof(WCHAR));
  206. }
  207. BOOL
  208. ValidateUNCName(
  209. LPWSTR pName
  210. )
  211. /*++
  212. Routine Description:
  213. This function will checks whether the given name is a valid UNC
  214. name ( in the form \\server\name) or not.
  215. Arguments:
  216. pName - The supplied name
  217. Return Value:
  218. TRUE - The name given is a valid UNC name
  219. FALSE - Otherwise
  220. --*/
  221. {
  222. if ( pName
  223. && (*pName++ == L'\\')
  224. && (*pName++ == L'\\')
  225. && (wcschr(pName, L'\\'))
  226. )
  227. {
  228. return TRUE;
  229. }
  230. return FALSE;
  231. }
  232. #ifndef NOT_USED
  233. LPWSTR
  234. GetNextElement(LPWSTR *pPtr, WCHAR token)
  235. {
  236. LPWSTR pszRestOfString = *pPtr;
  237. LPWSTR pszRetval = NULL;
  238. LPWSTR pszStr = NULL;
  239. if ( *pszRestOfString == L'\0')
  240. return NULL;
  241. if ((pszStr = wcschr (pszRestOfString, token))== NULL )
  242. {
  243. pszRetval = *pPtr;
  244. *pPtr += wcslen(*pPtr);
  245. return pszRetval;
  246. }
  247. else
  248. {
  249. *pszStr = L'\0';
  250. pszRetval = *pPtr ;
  251. *pPtr = ++pszStr ;
  252. return pszRetval ;
  253. }
  254. }
  255. #endif