Windows NT 4.0 source code leak
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.

322 lines
5.4 KiB

4 years ago
  1. /*++
  2. Copyright (c) 1990 Microsoft Corporation
  3. Module Name:
  4. util.c
  5. Abstract:
  6. This module provides all the utility functions for the Routing Layer and
  7. the local Print Providor
  8. Author:
  9. Dave Snipp (DaveSn) 15-Mar-1991
  10. Revision History:
  11. --*/
  12. #define NOMINMAX
  13. #include <windows.h>
  14. #include <winspool.h>
  15. #include <spltypes.h>
  16. #include <local.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <stdio.h>
  20. #include <stdarg.h>
  21. VOID
  22. SplInSem(
  23. VOID
  24. )
  25. {
  26. if ((DWORD)SpoolerSection.OwningThread != GetCurrentThreadId()) {
  27. DBGMSG(DBG_ERROR, ("Not in spooler semaphore\n"));
  28. }
  29. }
  30. VOID
  31. SplOutSem(
  32. VOID
  33. )
  34. {
  35. if ((DWORD)SpoolerSection.OwningThread == GetCurrentThreadId()) {
  36. DBGMSG(DBG_ERROR, ("Inside spooler semaphore !!\n"));
  37. }
  38. }
  39. VOID
  40. EnterSplSem(
  41. VOID
  42. )
  43. {
  44. EnterCriticalSection(&SpoolerSection);
  45. // WaitForSingleObject(HeapSemaphore, -1);
  46. }
  47. VOID
  48. LeaveSplSem(
  49. VOID
  50. )
  51. {
  52. LeaveCriticalSection(&SpoolerSection);
  53. // ReleaseSemaphore(HeapSemaphore, 1, NULL);
  54. }
  55. LPVOID
  56. AllocSplMem(
  57. DWORD cb
  58. )
  59. /*++
  60. Routine Description:
  61. This function will allocate local memory. It will possibly allocate extra
  62. memory and fill this with debugging information for the debugging version.
  63. Arguments:
  64. cb - The amount of memory to allocate
  65. Return Value:
  66. NON-NULL - A pointer to the allocated memory
  67. FALSE/NULL - The operation failed. Extended error status is available
  68. using GetLastError.
  69. --*/
  70. {
  71. LPDWORD pMem;
  72. DWORD cbNew;
  73. SplInSem();
  74. cbNew = cb+2*sizeof(DWORD);
  75. if (cbNew & 3)
  76. cbNew += sizeof(DWORD) - (cbNew & 3);
  77. pMem=(LPDWORD)HeapAlloc(hHeap, 0, cbNew);
  78. if (!pMem) {
  79. DBGMSG(DBG_ERROR, ("Heap Allocation failed for %d bytes: hHeap %x\n", cbNew, hHeap));
  80. return 0;
  81. }
  82. memset(pMem, 0, cbNew); // This might go later if done in NT
  83. *pMem=cb;
  84. *(LPDWORD)((LPBYTE)pMem+cbNew-sizeof(DWORD))=0xdeadbeef;
  85. return (LPVOID)(pMem+1);
  86. }
  87. BOOL
  88. FreeSplMem(
  89. LPVOID pMem,
  90. DWORD cb
  91. )
  92. {
  93. DWORD cbNew;
  94. LPDWORD pNewMem;
  95. SplInSem();
  96. pNewMem = pMem;
  97. pNewMem--;
  98. cbNew = cb+2*sizeof(DWORD);
  99. if (cbNew & 3)
  100. cbNew += sizeof(DWORD) - (cbNew & 3);
  101. if ((*pNewMem != cb) ||
  102. (*(LPDWORD)((LPBYTE)pNewMem + cbNew - sizeof(DWORD)) != 0xdeadbeef)) {
  103. DBGMSG(DBG_ERROR, ("Corrupt Memory in spooler : %0lx\n", pNewMem));
  104. }
  105. memset(pNewMem, 0, cbNew);
  106. return HeapFree(hHeap, 0, (LPVOID)pNewMem);
  107. }
  108. LPVOID
  109. ReallocSplMem(
  110. LPVOID pOldMem,
  111. DWORD cbOld,
  112. DWORD cbNew
  113. )
  114. {
  115. LPVOID pNewMem;
  116. pNewMem=AllocSplMem(cbNew);
  117. if (pOldMem) {
  118. memcpy(pNewMem, pOldMem, min(cbNew, cbOld));
  119. FreeSplMem(pOldMem, cbOld);
  120. }
  121. return pNewMem;
  122. }
  123. LPWSTR
  124. AllocSplStr(
  125. LPWSTR pStr
  126. )
  127. /*++
  128. Routine Description:
  129. This function will allocate enough local memory to store the specified
  130. string, and copy that string to the allocated memory
  131. Arguments:
  132. pStr - Pointer to the string that needs to be allocated and stored
  133. Return Value:
  134. NON-NULL - A pointer to the allocated memory containing the string
  135. FALSE/NULL - The operation failed. Extended error status is available
  136. using GetLastError.
  137. --*/
  138. {
  139. LPWSTR pMem;
  140. if (!pStr)
  141. return 0;
  142. if (pMem = AllocSplMem( wcslen(pStr)*sizeof(WCHAR) + sizeof(WCHAR) ))
  143. wcscpy(pMem, pStr);
  144. return pMem;
  145. }
  146. BOOL
  147. FreeSplStr(
  148. LPWSTR pStr
  149. )
  150. {
  151. return pStr ? FreeSplMem(pStr, wcslen(pStr)*sizeof(WCHAR)+sizeof(WCHAR))
  152. : FALSE;
  153. }
  154. BOOL
  155. ReallocSplStr(
  156. LPWSTR *ppStr,
  157. LPWSTR pStr
  158. )
  159. {
  160. FreeSplStr(*ppStr);
  161. *ppStr=AllocSplStr(pStr);
  162. return TRUE;
  163. }
  164. PINIENTRY
  165. FindName(
  166. PINIENTRY pIniKey,
  167. LPWSTR pName
  168. )
  169. {
  170. if (pName) {
  171. while (pIniKey) {
  172. if (!wcscmp(pIniKey->pName, pName)) {
  173. return pIniKey;
  174. }
  175. pIniKey=pIniKey->pNext;
  176. }
  177. }
  178. return FALSE;
  179. }
  180. PINIENTRY
  181. FindIniKey(
  182. PINIENTRY pIniEntry,
  183. LPWSTR pName
  184. )
  185. {
  186. if (!pName)
  187. return NULL;
  188. SplInSem();
  189. while (pIniEntry && _wcsicmp(pName, pIniEntry->pName))
  190. pIniEntry = pIniEntry->pNext;
  191. return pIniEntry;
  192. }
  193. LPBYTE
  194. PackStrings(
  195. LPWSTR *pSource,
  196. LPBYTE pDest,
  197. DWORD *DestOffsets,
  198. LPBYTE pEnd
  199. )
  200. {
  201. while (*DestOffsets != -1) {
  202. if (*pSource) {
  203. pEnd-=wcslen(*pSource)*sizeof(WCHAR) + sizeof(WCHAR);
  204. *(LPWSTR *)(pDest+*DestOffsets)=wcscpy((LPWSTR)pEnd, *pSource);
  205. } else
  206. *(LPWSTR *)(pDest+*DestOffsets)=0;
  207. pSource++;
  208. DestOffsets++;
  209. }
  210. return pEnd;
  211. }
  212. /* Message
  213. *
  214. * Displays a message by loading the strings whose IDs are passed into
  215. * the function, and substituting the supplied variable argument list
  216. * using the varargs macros.
  217. *
  218. */
  219. int Message(HWND hwnd, DWORD Type, int CaptionID, int TextID, ...)
  220. {
  221. WCHAR MsgText[128];
  222. WCHAR MsgFormat[128];
  223. WCHAR MsgCaption[40];
  224. va_list vargs;
  225. if( ( LoadString( hInst, TextID, MsgFormat, sizeof MsgFormat ) > 0 )
  226. && ( LoadString( hInst, CaptionID, MsgCaption, sizeof MsgCaption ) > 0 ) )
  227. {
  228. va_start( vargs, TextID );
  229. wvsprintf( MsgText, MsgFormat, vargs );
  230. va_end( vargs );
  231. return MessageBox(hwnd, MsgText, MsgCaption, Type);
  232. }
  233. else
  234. return 0;
  235. }
  236. #if DBG
  237. VOID DbgMsg( CHAR *MsgFormat, ... )
  238. {
  239. CHAR MsgText[256];
  240. va_list vargs;
  241. va_start( vargs, MsgFormat );
  242. wvsprintfA( MsgText, MsgFormat, vargs );
  243. va_end( vargs );
  244. OutputDebugStringA( MsgText );
  245. }
  246. #endif /* DBG*/