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.

220 lines
4.8 KiB

  1. /*++
  2. Copyright (c) 1996 Microsoft Corporation
  3. Module Name:
  4. kmfuncs.c
  5. Abstract:
  6. Kernel-mode specific library functions
  7. Environment:
  8. Windows NT printer drivers
  9. Revision History:
  10. 10/19/97 -fengy-
  11. added MapFileIntoMemoryForWrite,
  12. GenerateTempFileName.
  13. 03/16/96 -davidx-
  14. Created it.
  15. mm/dd/yy -author-
  16. description
  17. --*/
  18. #ifndef USERMODE_DRIVER
  19. #include "lib.h"
  20. //
  21. // Maximum number of time to try to generate a unique name
  22. //
  23. #define MAX_UNIQUE_NAME_TRY 9
  24. HANDLE
  25. MapFileIntoMemoryForWrite(
  26. IN LPCTSTR ptstrFilename,
  27. IN DWORD dwDesiredSize,
  28. OUT PVOID *ppvData,
  29. OUT PDWORD pdwSize
  30. )
  31. /*++
  32. Routine Description:
  33. Map a file into process memory space for write.
  34. Arguments:
  35. ptstrFilename - Specifies the name of the file to be mapped
  36. dwDesiredSize - Specifies the desired size of the file to be mapped
  37. ppvData - Points to a variable for returning mapped memory address
  38. pdwSize - Points to a variable for returning the actual size of the mapped file
  39. Return Value:
  40. Handle to identify the mapped file, NULL if there is an error
  41. --*/
  42. {
  43. HANDLE hModule = NULL;
  44. DWORD dwSize;
  45. if (hModule = EngLoadModuleForWrite((PWSTR)ptstrFilename, dwDesiredSize))
  46. {
  47. if (*ppvData = EngMapModule(hModule, &dwSize))
  48. {
  49. if (pdwSize)
  50. *pdwSize = dwSize;
  51. }
  52. else
  53. {
  54. ERR(("EngMapModule failed: %d\n", GetLastError()));
  55. EngFreeModule(hModule);
  56. hModule = NULL;
  57. }
  58. }
  59. else
  60. ERR(("EngLoadModuleForWrite failed: %d\n", GetLastError()));
  61. return hModule;
  62. }
  63. PTSTR
  64. GenerateTempFileName(
  65. IN LPCTSTR lpszPath,
  66. IN DWORD dwSeed
  67. )
  68. /*++
  69. Routine Description:
  70. Generate a temporary filename in kernel mode.
  71. Arguments:
  72. lpszPath - A null-terminated string which specifies the path of the temp file.
  73. It should contain the trailing backslash.
  74. dwSeed - a number used to generate unique file name
  75. Return Value:
  76. Pointer to a null-terminated full path filename string, NULL if there is an error.
  77. Caller is responsible for freeing the returned string.
  78. --*/
  79. {
  80. ENG_TIME_FIELDS currentTime;
  81. ULONG ulNameValue,ulExtValue;
  82. PTSTR ptstr,tempName[36],tempStr[16];
  83. INT iPathLength,iNameLength,i;
  84. HFILEMAP hFileMap;
  85. PVOID pvData;
  86. DWORD dwSize;
  87. BOOL bNameUnique=FALSE;
  88. INT iTry=0;
  89. ASSERT(lpszPath != NULL);
  90. while (!bNameUnique && iTry < MAX_UNIQUE_NAME_TRY)
  91. {
  92. EngQueryLocalTime(&currentTime);
  93. //
  94. // Use the seed number and current local time to compose the temporary file name
  95. //
  96. ulNameValue = currentTime.usDay * 1000000 +
  97. currentTime.usHour * 10000 +
  98. currentTime.usMinute * 100 +
  99. currentTime.usSecond;
  100. ulExtValue = currentTime.usMilliseconds;
  101. _ultot((ULONG)dwSeed, (PTSTR)tempName, 10);
  102. _tcsncat((PTSTR)tempName, TEXT("_"), 1);
  103. _ultot(ulNameValue, (PTSTR)tempStr, 10);
  104. _tcsncat((PTSTR)tempName, (PTSTR)tempStr, _tcslen((PTSTR)tempStr));
  105. _tcsncat((PTSTR)tempName, TEXT("."), 1);
  106. _ultot(ulExtValue, (PTSTR)tempStr, 10);
  107. _tcsncat((PTSTR)tempName, (PTSTR)tempStr, _tcslen((PTSTR)tempStr));
  108. iPathLength = _tcslen(lpszPath);
  109. iNameLength = _tcslen((PTSTR)tempName);
  110. if ((ptstr = MemAlloc((iPathLength + iNameLength + 1) * sizeof(TCHAR))) != NULL)
  111. {
  112. CopyMemory(ptstr, lpszPath, iPathLength * sizeof(TCHAR));
  113. CopyMemory(ptstr+iPathLength, (PTSTR)tempName, (iNameLength+1) * sizeof(TCHAR));
  114. //
  115. // Verify if a file with the same name already exists
  116. //
  117. if (!(hFileMap = MapFileIntoMemory(ptstr, &pvData, &dwSize)))
  118. bNameUnique = TRUE;
  119. else
  120. {
  121. //
  122. // Need to generate another temporary file name
  123. //
  124. UnmapFileFromMemory(hFileMap);
  125. MemFree(ptstr);
  126. ptstr = NULL;
  127. iTry++;
  128. }
  129. }
  130. else
  131. {
  132. ERR(("Memory allocation failed\n"));
  133. SetLastError(ERROR_NOT_ENOUGH_MEMORY);
  134. break;
  135. }
  136. }
  137. return ptstr;
  138. }
  139. #if DBG
  140. //
  141. // Functions for outputting debug messages
  142. //
  143. VOID
  144. DbgPrint(
  145. IN PCSTR pstrFormat,
  146. ...
  147. )
  148. {
  149. va_list ap;
  150. va_start(ap, pstrFormat);
  151. EngDebugPrint("", (PCHAR) pstrFormat, ap);
  152. va_end(ap);
  153. }
  154. #endif
  155. #endif //!USERMODE_DRIVER